PageRenderTime 64ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/Program_Files/replicatorg-0025/skein_engines/skeinforge-35/fabmetheus_utilities/geometry/geometry_utilities/booleansolid.py

https://github.com/sialan/autonomous-sprayer
Python | 246 lines | 232 code | 0 blank | 14 comment | 8 complexity | 32383bd5bcec464795136bb7877d94ef MD5 | raw file
  1. """
  2. This page is in the table of contents.
  3. The xml.py script is an import translator plugin to get a carving from an Art of Illusion xml file.
  4. An import plugin is a script in the interpret_plugins folder which has the function getCarving. It is meant to be run from the interpret tool. To ensure that the plugin works on platforms which do not handle file capitalization properly, give the plugin a lower case name.
  5. The getCarving function takes the file name of an xml file and returns the carving.
  6. This example gets a triangle mesh for the xml file boolean.xml. This example is run in a terminal in the folder which contains boolean.xml and xml.py.
  7. > python
  8. Python 2.5.1 (r251:54863, Sep 22 2007, 01:43:31)
  9. [GCC 4.2.1 (SUSE Linux)] on linux2
  10. Type "help", "copyright", "credits" or "license" for more information.
  11. >>> import xml
  12. >>> xml.getCarving().getCarveRotatedBoundaryLayers()
  13. [-1.159765625, None, [[(-18.925000000000001-2.4550000000000001j), (-18.754999999999981-2.4550000000000001j)
  14. ..
  15. many more lines of the carving
  16. ..
  17. An xml file can be exported from Art of Illusion by going to the "File" menu, then going into the "Export" menu item, then picking the XML choice. This will bring up the XML file chooser window, choose a place to save the file then click "OK". Leave the "compressFile" checkbox unchecked. All the objects from the scene will be exported, this plugin will ignore the light and camera. If you want to fabricate more than one object at a time, you can have multiple objects in the Art of Illusion scene and they will all be carved, then fabricated together.
  18. """
  19. from __future__ import absolute_import
  20. #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.
  21. import __init__
  22. from fabmetheus_utilities.geometry.geometry_utilities import evaluate
  23. from fabmetheus_utilities.geometry.solids import group
  24. from fabmetheus_utilities.geometry.solids import trianglemesh
  25. from fabmetheus_utilities.vector3 import Vector3
  26. from fabmetheus_utilities import euclidean
  27. from fabmetheus_utilities import gcodec
  28. from fabmetheus_utilities import intercircle
  29. import math
  30. __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
  31. __credits__ = 'Nophead <http://hydraraptor.blogspot.com/>\nArt of Illusion <http://www.artofillusion.org/>'
  32. __date__ = '$Date: 2008/21/04 $'
  33. __license__ = 'GPL 3.0'
  34. def addLineLoopsIntersections( loopLoopsIntersections, loops, pointBegin, pointEnd ):
  35. "Add intersections of the line with the loops."
  36. normalizedSegment = pointEnd - pointBegin
  37. normalizedSegmentLength = abs( normalizedSegment )
  38. if normalizedSegmentLength <= 0.0:
  39. return
  40. lineLoopsIntersections = []
  41. normalizedSegment /= normalizedSegmentLength
  42. segmentYMirror = complex( normalizedSegment.real, - normalizedSegment.imag )
  43. pointBeginRotated = segmentYMirror * pointBegin
  44. pointEndRotated = segmentYMirror * pointEnd
  45. addLoopsXSegmentIntersections( lineLoopsIntersections, loops, pointBeginRotated.real, pointEndRotated.real, segmentYMirror, pointBeginRotated.imag )
  46. for lineLoopsIntersection in lineLoopsIntersections:
  47. point = complex( lineLoopsIntersection, pointBeginRotated.imag ) * normalizedSegment
  48. loopLoopsIntersections.append(point)
  49. def addLineXSegmentIntersection( lineLoopsIntersections, segmentFirstX, segmentSecondX, vector3First, vector3Second, y ):
  50. "Add intersections of the line with the x segment."
  51. xIntersection = euclidean.getXIntersectionIfExists( vector3First, vector3Second, y )
  52. if xIntersection == None:
  53. return
  54. if xIntersection < min( segmentFirstX, segmentSecondX ):
  55. return
  56. if xIntersection <= max( segmentFirstX, segmentSecondX ):
  57. lineLoopsIntersections.append( xIntersection )
  58. def addLoopLoopsIntersections( loop, loopsLoopsIntersections, otherLoops ):
  59. "Add intersections of the loop with the other loops."
  60. for pointIndex in xrange(len(loop)):
  61. pointBegin = loop[pointIndex]
  62. pointEnd = loop[(pointIndex + 1) % len(loop)]
  63. addLineLoopsIntersections( loopsLoopsIntersections, otherLoops, pointBegin, pointEnd )
  64. def addLoopsXSegmentIntersections( lineLoopsIntersections, loops, segmentFirstX, segmentSecondX, segmentYMirror, y ):
  65. "Add intersections of the loops with the x segment."
  66. for loop in loops:
  67. addLoopXSegmentIntersections( lineLoopsIntersections, loop, segmentFirstX, segmentSecondX, segmentYMirror, y )
  68. def addLoopXSegmentIntersections( lineLoopsIntersections, loop, segmentFirstX, segmentSecondX, segmentYMirror, y ):
  69. "Add intersections of the loop with the x segment."
  70. rotatedLoop = euclidean.getPointsRoundZAxis( segmentYMirror, loop )
  71. for pointIndex in xrange( len( rotatedLoop ) ):
  72. pointFirst = rotatedLoop[ pointIndex ]
  73. pointSecond = rotatedLoop[ (pointIndex + 1) % len( rotatedLoop ) ]
  74. addLineXSegmentIntersection( lineLoopsIntersections, segmentFirstX, segmentSecondX, pointFirst, pointSecond, y )
  75. def getInBetweenLoopsFromLoops( importRadius, loops ):
  76. "Get the in between loops from loops."
  77. inBetweenLoops = []
  78. for loop in loops:
  79. inBetweenLoop = []
  80. for pointIndex in xrange(len(loop)):
  81. pointBegin = loop[pointIndex]
  82. pointEnd = loop[(pointIndex + 1) % len(loop)]
  83. intercircle.addPointsFromSegment( pointBegin, pointEnd, inBetweenLoop, importRadius )
  84. inBetweenLoops.append( inBetweenLoop )
  85. return inBetweenLoops
  86. def getInsetPointsByInsetLoop( insetLoop, inside, loops, radius ):
  87. "Get the inset points of the inset loop inside the loops."
  88. insetPointsByInsetLoop = []
  89. for pointIndex in xrange( len( insetLoop ) ):
  90. pointBegin = insetLoop[ ( pointIndex + len( insetLoop ) - 1 ) % len( insetLoop ) ]
  91. pointCenter = insetLoop[ pointIndex ]
  92. pointEnd = insetLoop[ (pointIndex + 1) % len( insetLoop ) ]
  93. if getIsInsetPointInsideLoops( inside, loops, pointBegin, pointCenter, pointEnd, radius ):
  94. insetPointsByInsetLoop.append( pointCenter )
  95. return insetPointsByInsetLoop
  96. def getInsetPointsByInsetLoops( insetLoops, inside, loops, radius ):
  97. "Get the inset points of the inset loops inside the loops."
  98. insetPointsByInsetLoops = []
  99. for insetLoop in insetLoops:
  100. insetPointsByInsetLoops += getInsetPointsByInsetLoop( insetLoop, inside, loops, radius )
  101. return insetPointsByInsetLoops
  102. def getIsInsetPointInsideLoops( inside, loops, pointBegin, pointCenter, pointEnd, radius ):
  103. "Determine if the inset point is inside the loops."
  104. centerMinusBegin = euclidean.getNormalized( pointCenter - pointBegin )
  105. centerMinusBeginWiddershins = complex( - centerMinusBegin.imag, centerMinusBegin.real )
  106. endMinusCenter = euclidean.getNormalized( pointEnd - pointCenter )
  107. endMinusCenterWiddershins = complex( - endMinusCenter.imag, endMinusCenter.real )
  108. widdershinsNormalized = euclidean.getNormalized( centerMinusBeginWiddershins + endMinusCenterWiddershins ) * radius
  109. return euclidean.getIsInFilledRegion( loops, pointCenter + widdershinsNormalized ) == inside
  110. def getLoopsDifference(importRadius, loopLists):
  111. "Get difference loops."
  112. negativeLoops = getLoopsUnified(importRadius, loopLists[1 :])
  113. intercircle.directLoops(False, negativeLoops)
  114. positiveLoops = loopLists[0]
  115. intercircle.directLoops(True, positiveLoops)
  116. radiusSide = 0.01 * importRadius
  117. corners = getLoopsListsIntersections(loopLists)
  118. corners += getInsetPointsByInsetLoops(negativeLoops, True, positiveLoops, radiusSide)
  119. corners += getInsetPointsByInsetLoops(positiveLoops, False, negativeLoops, radiusSide)
  120. allPoints = corners[:]
  121. allPoints += getInsetPointsByInsetLoops(getInBetweenLoopsFromLoops(importRadius, negativeLoops), True, positiveLoops, radiusSide)
  122. allPoints += getInsetPointsByInsetLoops(getInBetweenLoopsFromLoops(importRadius, positiveLoops), False, negativeLoops, radiusSide)
  123. return trianglemesh.getDescendingAreaLoops( allPoints, corners, importRadius)
  124. def getLoopsIntersection( importRadius, loopLists ):
  125. "Get intersection loops."
  126. intercircle.directLoopLists( True, loopLists )
  127. if len(loopLists) < 1:
  128. return []
  129. if len(loopLists) < 2:
  130. return loopLists[0]
  131. loopsIntersection = loopLists[0]
  132. for loopList in loopLists[1 :]:
  133. loopsIntersection = getLoopsIntersectionByPair( importRadius, loopsIntersection, loopList )
  134. return loopsIntersection
  135. def getLoopsIntersectionByPair( importRadius, loopsFirst, loopsLast ):
  136. "Get intersection loops for a pair of loop lists."
  137. radiusSide = 0.01 * importRadius
  138. corners = getLoopsListsIntersections( [ loopsFirst, loopsLast ] )
  139. corners += getInsetPointsByInsetLoops( loopsFirst, True, loopsLast, radiusSide )
  140. corners += getInsetPointsByInsetLoops( loopsLast, True, loopsFirst, radiusSide )
  141. allPoints = corners[:]
  142. allPoints += getInsetPointsByInsetLoops( getInBetweenLoopsFromLoops( importRadius, loopsFirst ), True, loopsLast, radiusSide )
  143. allPoints += getInsetPointsByInsetLoops( getInBetweenLoopsFromLoops( importRadius, loopsLast ), True, loopsFirst, radiusSide )
  144. return trianglemesh.getDescendingAreaLoops(allPoints, corners, importRadius)
  145. def getLoopsListsIntersections( loopsList ):
  146. "Get intersections betweens the loops lists."
  147. loopsListsIntersections = []
  148. for loopsIndex in xrange( len( loopsList ) ):
  149. loops = loopsList[ loopsIndex ]
  150. for otherLoops in loopsList[ : loopsIndex ]:
  151. loopsListsIntersections += getLoopsLoopsIntersections( loops, otherLoops )
  152. return loopsListsIntersections
  153. def getLoopsLoopsIntersections( loops, otherLoops ):
  154. "Get all the intersections of the loops with the other loops."
  155. loopsLoopsIntersections = []
  156. for loop in loops:
  157. addLoopLoopsIntersections( loop, loopsLoopsIntersections, otherLoops )
  158. return loopsLoopsIntersections
  159. def getLoopsUnified( importRadius, loopLists ):
  160. "Get joined loops sliced through shape."
  161. allPoints = []
  162. corners = getLoopsListsIntersections(loopLists)
  163. radiusSide = 0.01 * importRadius
  164. intercircle.directLoopLists( True, loopLists )
  165. for loopListIndex in xrange( len(loopLists) ):
  166. insetLoops = loopLists[ loopListIndex ]
  167. inBetweenInsetLoops = getInBetweenLoopsFromLoops( importRadius, insetLoops )
  168. otherLoops = euclidean.getConcatenatedList( loopLists[ : loopListIndex ] + loopLists[ loopListIndex + 1 : ] )
  169. corners += getInsetPointsByInsetLoops( insetLoops, False, otherLoops, radiusSide )
  170. allPoints += getInsetPointsByInsetLoops( inBetweenInsetLoops, False, otherLoops, radiusSide )
  171. allPoints += corners[:]
  172. return trianglemesh.getDescendingAreaLoops(allPoints, corners, importRadius)
  173. def getVisibleObjectLoopsList( importRadius, visibleObjects, z ):
  174. "Get visible object loops list."
  175. visibleObjectLoopsList = []
  176. for visibleObject in visibleObjects:
  177. visibleObjectLoops = visibleObject.getLoops(importRadius, z)
  178. visibleObjectLoopsList.append( visibleObjectLoops )
  179. return visibleObjectLoopsList
  180. class BooleanSolid( group.Group ):
  181. "A boolean solid object."
  182. def getDifference( self, importRadius, visibleObjectLoopsList ):
  183. "Get subtracted loops sliced through shape."
  184. return getLoopsDifference( importRadius, visibleObjectLoopsList )
  185. def getIntersection( self, importRadius, visibleObjectLoopsList ):
  186. "Get intersected loops sliced through shape."
  187. return getLoopsIntersection( importRadius, visibleObjectLoopsList )
  188. def getLoops(self, importRadius, z):
  189. "Get loops sliced through shape."
  190. visibleObjects = evaluate.getVisibleObjects(self.archivableObjects)
  191. if len( visibleObjects ) < 1:
  192. return []
  193. visibleObjectLoopsList = getVisibleObjectLoopsList( importRadius, visibleObjects, z )
  194. loops = self.getLoopsFromObjectLoopsList( importRadius, visibleObjectLoopsList )
  195. return euclidean.getSimplifiedLoops( loops, importRadius )
  196. def getLoopsFromObjectLoopsList( self, importRadius, visibleObjectLoopsList ):
  197. "Get loops from visible object loops list."
  198. return self.operationFunction( importRadius, visibleObjectLoopsList )
  199. def getTransformedPaths(self):
  200. "Get all transformed paths."
  201. importRadius = self.xmlElement.getCascadeFloat(1.5 * evaluate.getLayerThickness(self.xmlElement), 'importRadius')
  202. return euclidean.getVector3Paths(self.getLoopsFromObjectLoopsList(importRadius, self.getComplexTransformedPathLists()))
  203. def getUnion( self, importRadius, visibleObjectLoopsList ):
  204. "Get joined loops sliced through shape."
  205. return getLoopsUnified( importRadius, visibleObjectLoopsList )
  206. def getXMLClassName(self):
  207. "Get xml class name."
  208. return self.operationFunction.__name__.lower()[ len('get') : ]