chiark / gitweb /
Split the scene logic into another file, so we can add auto-placement there without...
[cura.git] / Cura / util / objectScene.py
1 import random
2 import numpy
3
4 class Scene():
5         def __init__(self):
6                 self._objectList = []
7
8         def objects(self):
9                 return self._objectList
10
11         def add(self, obj):
12                 self._objectList.append(obj)
13                 self.pushFree()
14
15         def remove(self, obj):
16                 self._objectList.remove(obj)
17
18         def pushFree(self):
19                 while self._pushFree():
20                         pass
21
22         def _pushFree(self):
23                 for a in self._objectList:
24                         for b in self._objectList:
25                                 if not self._checkHit(a, b):
26                                         continue
27                                 posDiff = a.getPosition() - b.getPosition()
28                                 if posDiff[0] == 0.0 and posDiff[1] == 0.0:
29                                         posDiff[1] = 1.0
30                                 if abs(posDiff[0]) > abs(posDiff[1]):
31                                         axis = 0
32                                 else:
33                                         axis = 1
34                                 aPos = a.getPosition()
35                                 bPos = b.getPosition()
36                                 center = (aPos[axis] + bPos[axis]) / 2
37                                 distance = (a.getSize()[axis] + b.getSize()[axis]) / 2 + 0.1
38                                 if posDiff[axis] < 0:
39                                         distance = -distance
40                                 aPos[axis] = center + distance / 2
41                                 bPos[axis] = center - distance / 2
42                                 a.setPosition(aPos)
43                                 b.setPosition(bPos)
44                                 return True
45                 return False
46
47         def _checkHit(self, a, b):
48                 if a == b:
49                         return False
50                 posDiff = a.getPosition() - b.getPosition()
51                 if abs(posDiff[0]) < (a.getSize()[0] + b.getSize()[0]) / 2 and abs(posDiff[1]) < (a.getSize()[1] + b.getSize()[1]) / 2:
52                         return True
53                 return False