2 Boolean geometry array.
6 from __future__ import absolute_import
8 from fabmetheus_utilities.geometry.geometry_utilities import evaluate
9 from fabmetheus_utilities.geometry.geometry_utilities import matrix
10 from fabmetheus_utilities import euclidean
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'
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
37 def getNewDerivation(elementNode):
39 return ArrayDerivation(elementNode)
41 def getRotationMatrix(arrayDictionary, derivation, path, point, pointIndex):
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:')
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:')
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:')
77 print(derivation.elementNode)
78 return matrix.Matrix()
79 return getRotationMatrixByPolar(arrayDictionary, averagePolar, averagePolarLength)
81 def getRotationMatrixByPolar(arrayDictionary, polar, polarLength):
82 'Get rotationMatrix by polar and polarLength.'
84 arrayDictionary['_arrayRotation'] = math.degrees(math.atan2(polar.imag, polar.real))
85 return matrix.Matrix(matrix.getDiagonalSwitchedTetragridByPolar([0, 1], polar))
87 def processElementNode(elementNode):
88 "Process the xml element."
89 processElementNodeByDerivation(None, elementNode)
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:')
99 if len(derivation.paths) < 1:
100 print('Warning, array could not get paths for:')
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'
109 for path in derivation.paths:
110 addPathToGroup(derivation, groupDictionaryCopy, path, targetMatrix, totalIndex)
111 elementNode.getXMLProcessor().processElementNode(elementNode)
114 class ArrayDerivation(object):
115 "Class to hold array variables."
116 def __init__(self, elementNode):
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')