chiark / gitweb /
Add back the ultimaker platform, and made the platform mesh simpler.
[cura.git] / Cura / slice / cura_sf / fabmetheus_utilities / geometry / manipulation_meta / _array.py
1 """
2 Boolean geometry array.
3
4 """
5
6 from __future__ import absolute_import
7
8 from fabmetheus_utilities.geometry.geometry_utilities import evaluate
9 from fabmetheus_utilities.geometry.geometry_utilities import matrix
10 from fabmetheus_utilities import euclidean
11 import math
12
13
14 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
15 __credits__ = 'Art of Illusion <http://www.artofillusion.org/>'
16 __date__ = '$Date: 2008/02/05 $'
17 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
18
19
20 def addPathToGroup(derivation, groupDictionaryCopy, path, targetMatrix, totalIndex):
21         'Add path to the array group.'
22         for pointIndex, point in enumerate(path):
23                 arrayElement = derivation.target.getCopy(derivation.elementNode.getIDSuffix(totalIndex), derivation.elementNode)
24                 arrayDictionary = arrayElement.attributes
25                 arrayDictionary['visible'] = str(derivation.visible).lower()
26                 arrayDictionary.update(groupDictionaryCopy)
27                 euclidean.removeTrueFromDictionary(arrayDictionary, 'visible')
28                 vertexMatrix = matrix.Matrix(matrix.getTranslateTetragridByTranslation(point))
29                 zAngle = totalIndex * 50.0
30                 rotationMatrix = getRotationMatrix(arrayDictionary, derivation, path, point, pointIndex)
31                 arrayElementMatrix = vertexMatrix.getSelfTimesOther(rotationMatrix.getSelfTimesOther(targetMatrix.tetragrid).tetragrid)
32                 arrayDictionary.update(arrayElementMatrix.getAttributes('matrix.'))
33                 arrayDictionary['_arrayIndex'] = totalIndex
34                 arrayDictionary['_arrayPoint'] = point
35                 totalIndex += 1
36
37 def getNewDerivation(elementNode):
38         'Get new derivation.'
39         return ArrayDerivation(elementNode)
40
41 def getRotationMatrix(arrayDictionary, derivation, path, point, pointIndex):
42         'Get rotationMatrix.'
43         if len(path) < 2 or not derivation.track:
44                 return matrix.Matrix()
45         point = point.dropAxis()
46         begin = path[(pointIndex + len(path) - 1) % len(path)].dropAxis()
47         end = path[(pointIndex + 1) % len(path)].dropAxis()
48         pointMinusBegin = point - begin
49         pointMinusBeginLength = abs(pointMinusBegin)
50         endMinusPoint = end - point
51         endMinusPointLength = abs(endMinusPoint)
52         if not derivation.closed:
53                 if pointIndex == 0 and endMinusPointLength > 0.0:
54                         return getRotationMatrixByPolar(arrayDictionary, endMinusPoint, endMinusPointLength)
55                 elif pointIndex == len(path) - 1 and pointMinusBeginLength > 0.0:
56                         return getRotationMatrixByPolar(arrayDictionary, pointMinusBegin, pointMinusBeginLength)
57         if pointMinusBeginLength <= 0.0:
58                 print('Warning, point equals previous point in getRotationMatrix in array for:')
59                 print(path)
60                 print(pointIndex)
61                 print(derivation.elementNode)
62                 return matrix.Matrix()
63         pointMinusBegin /= pointMinusBeginLength
64         if endMinusPointLength <= 0.0:
65                 print('Warning, point equals next point in getRotationMatrix in array for:')
66                 print(path)
67                 print(pointIndex)
68                 print(derivation.elementNode)
69                 return matrix.Matrix()
70         endMinusPoint /= endMinusPointLength
71         averagePolar = pointMinusBegin + endMinusPoint
72         averagePolarLength = abs(averagePolar)
73         if averagePolarLength <= 0.0:
74                 print('Warning, averagePolarLength is zero in getRotationMatrix in array for:')
75                 print(path)
76                 print(pointIndex)
77                 print(derivation.elementNode)
78                 return matrix.Matrix()
79         return getRotationMatrixByPolar(arrayDictionary, averagePolar, averagePolarLength)
80
81 def getRotationMatrixByPolar(arrayDictionary, polar, polarLength):
82         'Get rotationMatrix by polar and polarLength.'
83         polar /= polarLength
84         arrayDictionary['_arrayRotation'] = math.degrees(math.atan2(polar.imag, polar.real))
85         return matrix.Matrix(matrix.getDiagonalSwitchedTetragridByPolar([0, 1], polar))
86
87 def processElementNode(elementNode):
88         "Process the xml element."
89         processElementNodeByDerivation(None, elementNode)
90
91 def processElementNodeByDerivation(derivation, elementNode):
92         'Process the xml element by derivation.'
93         if derivation == None:
94                 derivation = ArrayDerivation(elementNode)
95         if derivation.target == None:
96                 print('Warning, array could not get target for:')
97                 print(elementNode)
98                 return
99         if len(derivation.paths) < 1:
100                 print('Warning, array could not get paths for:')
101                 print(elementNode)
102                 return
103         groupDictionaryCopy = elementNode.attributes.copy()
104         euclidean.removeElementsFromDictionary(groupDictionaryCopy, ['closed', 'paths', 'target', 'track', 'vertexes'])
105         evaluate.removeIdentifiersFromDictionary(groupDictionaryCopy)
106         targetMatrix = matrix.getBranchMatrixSetElementNode(derivation.target)
107         elementNode.localName = 'group'
108         totalIndex = 0
109         for path in derivation.paths:
110                 addPathToGroup(derivation, groupDictionaryCopy, path, targetMatrix, totalIndex)
111         elementNode.getXMLProcessor().processElementNode(elementNode)
112
113
114 class ArrayDerivation(object):
115         "Class to hold array variables."
116         def __init__(self, elementNode):
117                 'Set defaults.'
118                 self.closed = evaluate.getEvaluatedBoolean(True, elementNode, 'closed')
119                 self.elementNode = elementNode
120                 self.paths = evaluate.getTransformedPathsByKey([], elementNode, 'paths')
121                 vertexTargets = evaluate.getElementNodesByKey(elementNode, 'vertexes')
122                 for vertexTarget in vertexTargets:
123                         self.paths.append(vertexTarget.getVertexes())
124                 self.target = evaluate.getElementNodeByKey(elementNode, 'target')
125                 self.track = evaluate.getEvaluatedBoolean(True, elementNode, 'track')
126                 self.visible = evaluate.getEvaluatedBoolean(True, elementNode, 'visible')