ConvexHullJob.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from UM.Job import Job
  4. from UM.Application import Application
  5. from UM.Math.Polygon import Polygon
  6. import numpy
  7. import copy
  8. from . import ConvexHullNode
  9. ## Job to async calculate the convex hull of a node.
  10. class ConvexHullJob(Job):
  11. def __init__(self, node):
  12. super().__init__()
  13. self._node = node
  14. def run(self):
  15. if not self._node:
  16. return
  17. ## If the scene node is a group, use the hull of the children to calculate its hull.
  18. if self._node.callDecoration("isGroup"):
  19. hull = Polygon(numpy.zeros((0, 2), dtype=numpy.int32))
  20. for child in self._node.getChildren():
  21. child_hull = child.callDecoration("getConvexHull")
  22. if child_hull:
  23. hull.setPoints(numpy.append(hull.getPoints(), child_hull.getPoints(), axis = 0))
  24. if hull.getPoints().size < 3:
  25. self._node.callDecoration("setConvexHull", None)
  26. self._node.callDecoration("setConvexHullJob", None)
  27. return
  28. Job.yieldThread()
  29. else:
  30. if not self._node.getMeshData():
  31. return
  32. mesh = self._node.getMeshData()
  33. vertex_data = mesh.getTransformed(self._node.getWorldTransformation()).getVertices()
  34. # Don't use data below 0.
  35. # TODO; We need a better check for this as this gives poor results for meshes with long edges.
  36. vertex_data = vertex_data[vertex_data[:,1] >= 0]
  37. # Round the vertex data to 1/10th of a mm, then remove all duplicate vertices
  38. # This is done to greatly speed up further convex hull calculations as the convex hull
  39. # becomes much less complex when dealing with highly detailed models.
  40. vertex_data = numpy.round(vertex_data, 1)
  41. vertex_data = vertex_data[:, [0, 2]] # Drop the Y components to project to 2D.
  42. # Grab the set of unique points.
  43. #
  44. # This basically finds the unique rows in the array by treating them as opaque groups of bytes
  45. # which are as long as the 2 float64s in each row, and giving this view to numpy.unique() to munch.
  46. # See http://stackoverflow.com/questions/16970982/find-unique-rows-in-numpy-array
  47. vertex_byte_view = numpy.ascontiguousarray(vertex_data).view(numpy.dtype((numpy.void, vertex_data.dtype.itemsize * vertex_data.shape[1])))
  48. _, idx = numpy.unique(vertex_byte_view, return_index=True)
  49. vertex_data = vertex_data[idx] # Select the unique rows by index.
  50. hull = Polygon(vertex_data)
  51. # First, calculate the normal convex hull around the points
  52. hull = hull.getConvexHull()
  53. # Then, do a Minkowski hull with a simple 1x1 quad to outset and round the normal convex hull.
  54. # This is done because of rounding errors.
  55. hull = hull.getMinkowskiHull(Polygon(numpy.array([[-0.5, -0.5], [-0.5, 0.5], [0.5, 0.5], [0.5, -0.5]], numpy.float32)))
  56. global_stack = Application.getInstance().getGlobalContainerStack()
  57. if global_stack:
  58. if global_stack.getProperty("print_sequence", "value")== "one_at_a_time" and not self._node.getParent().callDecoration("isGroup"):
  59. # Printing one at a time and it's not an object in a group
  60. self._node.callDecoration("setConvexHullBoundary", copy.deepcopy(hull))
  61. head_and_fans = Polygon(numpy.array(global_stack.getProperty("machine_head_with_fans_polygon", "value"), numpy.float32))
  62. # Full head hull is used to actually check the order.
  63. full_head_hull = hull.getMinkowskiHull(head_and_fans)
  64. self._node.callDecoration("setConvexHullHeadFull", full_head_hull)
  65. mirrored = copy.deepcopy(head_and_fans)
  66. mirrored.mirror([0, 0], [0, 1]) #Mirror horizontally.
  67. mirrored.mirror([0, 0], [1, 0]) #Mirror vertically.
  68. head_and_fans = head_and_fans.intersectionConvexHulls(mirrored)
  69. # Min head hull is used for the push free
  70. min_head_hull = hull.getMinkowskiHull(head_and_fans)
  71. self._node.callDecoration("setConvexHullHead", min_head_hull)
  72. hull = hull.getMinkowskiHull(Polygon(numpy.array(global_stack.getProperty("machine_head_polygon","value"),numpy.float32)))
  73. else:
  74. self._node.callDecoration("setConvexHullHead", None)
  75. if self._node.getParent() is None: # Node was already deleted before job is done.
  76. self._node.callDecoration("setConvexHullNode",None)
  77. self._node.callDecoration("setConvexHull", None)
  78. self._node.callDecoration("setConvexHullJob", None)
  79. return
  80. hull_node = ConvexHullNode.ConvexHullNode(self._node, hull, Application.getInstance().getController().getScene().getRoot())
  81. self._node.callDecoration("setConvexHullNode", hull_node)
  82. self._node.callDecoration("setConvexHull", hull)
  83. self._node.callDecoration("setConvexHullJob", None)
  84. if self._node.getParent() and self._node.getParent().callDecoration("isGroup"):
  85. job = self._node.getParent().callDecoration("getConvexHullJob")
  86. if job:
  87. job.cancel()
  88. self._node.getParent().callDecoration("setConvexHull", None)
  89. hull_node = self._node.getParent().callDecoration("getConvexHullNode")
  90. if hull_node:
  91. hull_node.setParent(None)