ConvexHullJob.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. class ConvexHullJob(Job):
  10. def __init__(self, node):
  11. super().__init__()
  12. self._node = node
  13. def run(self):
  14. if not self._node:
  15. return
  16. ## If the scene node is a group, use the hull of the children to calculate its hull.
  17. if self._node.callDecoration("isGroup"):
  18. hull = Polygon(numpy.zeros((0, 2), dtype=numpy.int32))
  19. for child in self._node.getChildren():
  20. child_hull = child.callDecoration("getConvexHull")
  21. if child_hull:
  22. hull.setPoints(numpy.append(hull.getPoints(), child_hull.getPoints(), axis = 0))
  23. if hull.getPoints().size < 3:
  24. self._node.callDecoration("setConvexHull", None)
  25. self._node.callDecoration("setConvexHullJob", None)
  26. return
  27. else:
  28. if not self._node.getMeshData():
  29. return
  30. mesh = self._node.getMeshData()
  31. vertex_data = mesh.getTransformed(self._node.getWorldTransformation()).getVertices()
  32. hull = Polygon(numpy.rint(vertex_data[:, [0, 2]]).astype(int))
  33. # First, calculate the normal convex hull around the points
  34. hull = hull.getConvexHull()
  35. # Then, do a Minkowski hull with a simple 1x1 quad to outset and round the normal convex hull.
  36. # This is done because of rounding errors.
  37. hull = hull.getMinkowskiHull(Polygon(numpy.array([[-1, -1], [-1, 1], [1, 1], [1, -1]], numpy.float32)))
  38. settings = Application.getInstance().getActiveMachine()
  39. if settings.getSettingValueByKey("print_sequence") == "One at a time" and not self._node.getParent().callDecoration("isGroup"):
  40. # Printing one at a time and it's not an object in a group
  41. self._node.callDecoration("setConvexHullBoundary", copy.deepcopy(hull))
  42. head_hull = hull.getMinkowskiHull(Polygon(numpy.array(settings.getSettingValueByKey("machine_head_with_fans_polygon"),numpy.float32)))
  43. self._node.callDecoration("setConvexHullHead", head_hull)
  44. hull = hull.getMinkowskiHull(Polygon(numpy.array(settings.getSettingValueByKey("machine_head_polygon"),numpy.float32)))
  45. hull_node = ConvexHullNode.ConvexHullNode(self._node, hull, Application.getInstance().getController().getScene().getRoot())
  46. self._node.callDecoration("setConvexHullNode", hull_node)
  47. self._node.callDecoration("setConvexHull", hull)
  48. self._node.callDecoration("setConvexHullJob", None)
  49. if self._node.getParent().callDecoration("isGroup"):
  50. job = self._node.getParent().callDecoration("getConvexHullJob")
  51. if job:
  52. job.cancel()
  53. self._node.getParent().callDecoration("setConvexHull", None)
  54. hull_node = self._node.getParent().callDecoration("getConvexHullNode")
  55. if hull_node:
  56. hull_node.setParent(None)