ShapeArray.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import numpy
  2. import copy
  3. from UM.Math.Polygon import Polygon
  4. ## Polygon representation as an array for use with Arrange
  5. class ShapeArray:
  6. def __init__(self, arr, offset_x, offset_y, scale = 1):
  7. self.arr = arr
  8. self.offset_x = offset_x
  9. self.offset_y = offset_y
  10. self.scale = scale
  11. ## Instantiate from a bunch of vertices
  12. # \param vertices
  13. # \param scale scale the coordinates
  14. @classmethod
  15. def fromPolygon(cls, vertices, scale = 1):
  16. # scale
  17. vertices = vertices * scale
  18. # flip y, x -> x, y
  19. flip_vertices = numpy.zeros((vertices.shape))
  20. flip_vertices[:, 0] = vertices[:, 1]
  21. flip_vertices[:, 1] = vertices[:, 0]
  22. flip_vertices = flip_vertices[::-1]
  23. # offset, we want that all coordinates have positive values
  24. offset_y = int(numpy.amin(flip_vertices[:, 0]))
  25. offset_x = int(numpy.amin(flip_vertices[:, 1]))
  26. flip_vertices[:, 0] = numpy.add(flip_vertices[:, 0], -offset_y)
  27. flip_vertices[:, 1] = numpy.add(flip_vertices[:, 1], -offset_x)
  28. shape = [int(numpy.amax(flip_vertices[:, 0])), int(numpy.amax(flip_vertices[:, 1]))]
  29. arr = cls.arrayFromPolygon(shape, flip_vertices)
  30. return cls(arr, offset_x, offset_y)
  31. ## Instantiate an offset and hull ShapeArray from a scene node.
  32. # \param node source node where the convex hull must be present
  33. # \param min_offset offset for the offset ShapeArray
  34. # \param scale scale the coordinates
  35. @classmethod
  36. def fromNode(cls, node, min_offset, scale = 0.5):
  37. transform = node._transformation
  38. transform_x = transform._data[0][3]
  39. transform_y = transform._data[2][3]
  40. hull_verts = node.callDecoration("getConvexHull")
  41. # For one_at_a_time printing you need the convex hull head.
  42. hull_head_verts = node.callDecoration("getConvexHullHead") or hull_verts
  43. # If a model is to small then it will not contain any points
  44. if not hull_verts.getPoints().any():
  45. return None, None
  46. offset_verts = hull_head_verts.getMinkowskiHull(Polygon.approximatedCircle(min_offset))
  47. offset_points = copy.deepcopy(offset_verts._points) # x, y
  48. offset_points[:, 0] = numpy.add(offset_points[:, 0], -transform_x)
  49. offset_points[:, 1] = numpy.add(offset_points[:, 1], -transform_y)
  50. offset_shape_arr = ShapeArray.fromPolygon(offset_points, scale = scale)
  51. hull_points = copy.deepcopy(hull_verts._points)
  52. hull_points[:, 0] = numpy.add(hull_points[:, 0], -transform_x)
  53. hull_points[:, 1] = numpy.add(hull_points[:, 1], -transform_y)
  54. hull_shape_arr = ShapeArray.fromPolygon(hull_points, scale = scale) # x, y
  55. return offset_shape_arr, hull_shape_arr
  56. ## Create np.array with dimensions defined by shape
  57. # Fills polygon defined by vertices with ones, all other values zero
  58. # Only works correctly for convex hull vertices
  59. # Originally from: http://stackoverflow.com/questions/37117878/generating-a-filled-polygon-inside-a-numpy-array
  60. # \param shape numpy format shape, [x-size, y-size]
  61. # \param vertices
  62. @classmethod
  63. def arrayFromPolygon(cls, shape, vertices):
  64. base_array = numpy.zeros(shape, dtype=float) # Initialize your array of zeros
  65. fill = numpy.ones(base_array.shape) * True # Initialize boolean array defining shape fill
  66. # Create check array for each edge segment, combine into fill array
  67. for k in range(vertices.shape[0]):
  68. fill = numpy.all([fill, cls._check(vertices[k - 1], vertices[k], base_array)], axis=0)
  69. # Set all values inside polygon to one
  70. base_array[fill] = 1
  71. return base_array
  72. ## Return indices that mark one side of the line, used by arrayFromPolygon
  73. # Uses the line defined by p1 and p2 to check array of
  74. # input indices against interpolated value
  75. # Returns boolean array, with True inside and False outside of shape
  76. # Originally from: http://stackoverflow.com/questions/37117878/generating-a-filled-polygon-inside-a-numpy-array
  77. # \param p1 2-tuple with x, y for point 1
  78. # \param p2 2-tuple with x, y for point 2
  79. # \param base_array boolean array to project the line on
  80. @classmethod
  81. def _check(cls, p1, p2, base_array):
  82. if p1[0] == p2[0] and p1[1] == p2[1]:
  83. return
  84. idxs = numpy.indices(base_array.shape) # Create 3D array of indices
  85. p1 = p1.astype(float)
  86. p2 = p2.astype(float)
  87. if p2[0] == p1[0]:
  88. sign = numpy.sign(p2[1] - p1[1])
  89. return idxs[1] * sign
  90. if p2[1] == p1[1]:
  91. sign = numpy.sign(p2[0] - p1[0])
  92. return idxs[1] * sign
  93. # Calculate max column idx for each row idx based on interpolated line between two points
  94. max_col_idx = (idxs[0] - p1[0]) / (p2[0] - p1[0]) * (p2[1] - p1[1]) + p1[1]
  95. sign = numpy.sign(p2[0] - p1[0])
  96. return idxs[1] * sign <= max_col_idx * sign