-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshapeCollectionObject.py
More file actions
115 lines (102 loc) · 3.67 KB
/
shapeCollectionObject.py
File metadata and controls
115 lines (102 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from ShapeObject import ShapeObject
from FaceObject import FaceObject
import math
import random
import copy
class shapeCollectionObject:
"""
this class handles merging shapes & other shenanigans
NOTE: merge only works when there are 2 shapes in this collection.
you may only add a shape when you have 1 or less shapes
"""
def __init__(self):
self.shapes = []
def getNewSpawnPoint(self,shape):
if len(self.shapes) == 0 :
return (0,0,0)
elif len(self.shapes) == 1:
return (0, 0, int(self.shapes[0].calculateRadius() + self.shapes[0].z + shape.calculateRadius()))
return (-1,-1,-1)
def canMerge(self):
if len(self.shapes) <= 1:
return False
shared = self.getSharedFaces()
print("can merge ", len(shared) > 0)
return len(shared) > 0
def mergeAll(self):
if not self.canMerge():
print("ERROR: can't merge")
return
self.mergeShape()
self.shapes.pop()
for shape in self.shapes:
print(shape)
def getSharedFaces(self):
shared = set()
for face1 in self.shapes[0].faces:
for face2 in self.shapes[1].faces:
if face1 != None and face1 == face2:
print(len(face1.order), " ---- ",len(face2.order))
print(face1 , " and ", face2, " are the same")
shared.add(face1)
return shared
def getAllPoints(self):
temp = copy.deepcopy(self.shapes[0].points)
for p in self.shapes[1].points:
if p not in temp:
temp.append(p)
return temp
def mergeShape(self):
shared = self.getSharedFaces()
if len(shared) == 0:
return
print("shared faces", shared)
print("original length of shape 0:", len(self.shapes[0].faces))
print("original length of shape 1:", len(self.shapes[1].faces))
for face in shared:
self.shapes[0].faces.remove(face)
self.shapes[1].faces.remove(face)
# we want all the deduplicated points
allPoints = self.getAllPoints()
self.shapes[0].points=allPoints
print(len(allPoints), allPoints)
maxindex = self.findMaxIndex(self.shapes[0].faces)
for i in range(len(self.shapes[1].faces)):
oldFace = self.shapes[1].faces[i]
newfaceIndex = maxindex + i+1
newOrder = []
actualPoints = []
for order in oldFace.order:
actualPoints.append(self.shapes[1].points[order])
for point in actualPoints:
newOrder.append(allPoints.index(point))
newFace = FaceObject(newfaceIndex, allPoints,newOrder)
self.shapes[0].faces.append(newFace)
def findMaxIndex (self, lis):
maxx = -1
for f in lis:
if f.index > maxx:
maxx = f.index
return maxx
def removeShape(self, index):
if len(self.shapes) == 0 or index > len(self.shapes):
return
if len(self.shapes) == 1:
self.shapes.pop()
else:
self.shapes.pop(index)
def addShape(self, shape):
if len(self.shapes) == 0 :
self.shapes.append(shape)
elif len(self.shapes) < 2:
newSpawn = self.getNewSpawnPoint(shape)
shape.moveCenter(newSpawn)
self.shapes.append(shape)
collection = shapeCollectionObject()
s1 = ShapeObject((0,0,0), 1,0)
collection.addShape(s1)
s2 = ShapeObject((0,0,4), 1,0)
collection.addShape(s2)
print(collection.canMerge())
collection.mergeAll()
print(collection.shapes[0])