ConvexHullJob.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  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. from . import ConvexHullNode
  8. class ConvexHullJob(Job):
  9. def __init__(self, node):
  10. super().__init__()
  11. self._node = node
  12. def run(self):
  13. if not self._node or not self._node.getMeshData():
  14. return
  15. mesh = self._node.getMeshData()
  16. vertexData = mesh.getTransformed(self._node.getWorldTransformation()).getVertices()
  17. hull = Polygon(numpy.rint(vertexData[:, [0, 2]]).astype(int))
  18. # First, calculate the normal convex hull around the points
  19. hull = hull.getConvexHull()
  20. # Then, do a Minkowski hull with a simple 1x1 quad to outset and round the normal convex hull.
  21. hull = hull.getMinkowskiHull(Polygon(numpy.array([[-1, -1], [-1, 1], [1, 1], [1, -1]], numpy.float32)))
  22. hull_node = ConvexHullNode.ConvexHullNode(self._node, hull, Application.getInstance().getController().getScene().getRoot())
  23. self._node._convex_hull = hull
  24. delattr(self._node, "_convex_hull_job")