chiark / gitweb /
Add uppercase STL and HEX to file dialog filters for linux/MacOS
[cura.git] / Cura / cura_sf / fabmetheus_utilities / geometry / manipulation_shapes / equation.py
1 """
2 Equation for vertexes.
3
4 """
5
6 from __future__ import absolute_import
7 #Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
8 import __init__
9
10 from fabmetheus_utilities.geometry.geometry_utilities import evaluate
11 from fabmetheus_utilities.geometry.geometry_utilities import matrix
12 from fabmetheus_utilities import euclidean
13 import math
14
15
16 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
17 __credits__ = 'Art of Illusion <http://www.artofillusion.org/>'
18 __date__ = '$Date: 2008/02/05 $'
19 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
20
21
22 globalExecutionOrder = -100
23
24
25 def equate(point, returnValue):
26         "Get equation for rectangular."
27         point.setToVector3(evaluate.getVector3ByDictionaryListValue(returnValue, point))
28
29 def equatePoints(elementNode, points, prefix, revolutions):
30         "Equate the points."
31         derivation = EquationDerivation(elementNode, prefix)
32         for equationResult in derivation.equationResults:
33                 for point in points:
34                         returnValue = equationResult.getReturnValue(point, revolutions)
35                         if returnValue == None:
36                                 print('Warning, returnValue in alterVertexesByEquation in equation is None for:')
37                                 print(point)
38                                 print(elementNode)
39                         else:
40                                 equationResult.equationFunction(point, returnValue)
41
42 def equateX(point, returnValue):
43         "Get equation for rectangular x."
44         point.x = returnValue
45
46 def equateY(point, returnValue):
47         "Get equation for rectangular y."
48         point.y = returnValue
49
50 def equateZ(point, returnValue):
51         "Get equation for rectangular z."
52         point.z = returnValue
53
54 def getManipulatedGeometryOutput(elementNode, geometryOutput, prefix):
55         "Get equated geometryOutput."
56         equatePoints(elementNode, matrix.getVertexes(geometryOutput), prefix, None)
57         return geometryOutput
58
59 def getManipulatedPaths(close, elementNode, loop, prefix, sideLength):
60         "Get equated paths."
61         equatePoints(elementNode, loop, prefix, 0.0)
62         return [loop]
63
64 def getNewDerivation(elementNode, prefix, sideLength):
65         'Get new derivation.'
66         return EquationDerivation(elementNode, prefix)
67
68
69 class EquationDerivation:
70         "Class to hold equation variables."
71         def __init__(self, elementNode, prefix):
72                 'Set defaults.'
73                 self.equationResults = []
74                 self.addEquationResult(elementNode, equate, prefix)
75                 self.addEquationResult(elementNode, equateX, prefix)
76                 self.addEquationResult(elementNode, equateY, prefix)
77                 self.addEquationResult(elementNode, equateZ, prefix)
78
79         def addEquationResult(self, elementNode, equationFunction, prefix):
80                 'Add equation result to equationResults.'
81                 prefixedEquationName = prefix + equationFunction.__name__[ len('equate') : ].replace('Dot', '.').lower()
82                 if prefixedEquationName in elementNode.attributes:
83                         self.equationResults.append(EquationResult(elementNode, equationFunction, prefixedEquationName))
84
85
86 class EquationResult:
87         "Class to get equation results."
88         def __init__(self, elementNode, equationFunction, key):
89                 "Initialize."
90                 self.distance = 0.0
91                 elementNode.xmlObject = evaluate.getEvaluatorSplitWords(elementNode.attributes[key])
92                 self.equationFunction = equationFunction
93                 self.function = evaluate.Function(elementNode)
94                 self.points = []
95
96         def getReturnValue(self, point, revolutions):
97                 "Get return value."
98                 if self.function == None:
99                         return point
100                 self.function.localDictionary['azimuth'] = math.degrees(math.atan2(point.y, point.x))
101                 if len(self.points) > 0:
102                         self.distance += abs(point - self.points[-1])
103                 self.function.localDictionary['distance'] = self.distance
104                 self.function.localDictionary['radius'] = abs(point.dropAxis())
105                 if revolutions != None:
106                         if len( self.points ) > 0:
107                                 revolutions += 0.5 / math.pi * euclidean.getAngleAroundZAxisDifference(point, self.points[-1])
108                         self.function.localDictionary['revolutions'] = revolutions
109                 self.function.localDictionary['vertex'] = point
110                 self.function.localDictionary['vertexes'] = self.points
111                 self.function.localDictionary['vertexindex'] = len(self.points)
112                 self.function.localDictionary['x'] = point.x
113                 self.function.localDictionary['y'] = point.y
114                 self.function.localDictionary['z'] = point.z
115                 self.points.append(point)
116                 return self.function.getReturnValueWithoutDeletion()