LayerPolygon.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. from UM.Math.Color import Color
  2. import numpy
  3. class LayerPolygon:
  4. NoneType = 0
  5. Inset0Type = 1
  6. InsetXType = 2
  7. SkinType = 3
  8. SupportType = 4
  9. SkirtType = 5
  10. InfillType = 6
  11. SupportInfillType = 7
  12. MoveCombingType = 8
  13. MoveRetractionType = 9
  14. SupportInterfaceType = 10
  15. __jump_map = numpy.logical_or(numpy.logical_or(numpy.arange(11) == NoneType, numpy.arange(11) == MoveCombingType), numpy.arange(11) == MoveRetractionType)
  16. def __init__(self, mesh, extruder, line_types, data, line_widths):
  17. self._mesh = mesh
  18. self._extruder = extruder
  19. self._types = line_types
  20. self._data = data
  21. self._line_widths = line_widths
  22. self._vertex_begin = 0
  23. self._vertex_end = 0
  24. self._index_begin = 0
  25. self._index_end = 0
  26. self._jump_mask = self.__jump_map[self._types]
  27. self._jump_count = numpy.sum(self._jump_mask)
  28. self._mesh_line_count = len(self._types)-self._jump_count
  29. self._vertex_count = self._mesh_line_count + numpy.sum( self._types[1:] == self._types[:-1])
  30. # Buffering the colors shouldn't be necessary as it is not
  31. # re-used and can save alot of memory usage.
  32. self._color_map = self.__color_map * [1, 1, 1, self._extruder] # The alpha component is used to store the extruder nr
  33. self._colors = self._color_map[self._types]
  34. # When type is used as index returns true if type == LayerPolygon.InfillType or type == LayerPolygon.SkinType or type == LayerPolygon.SupportInfillType
  35. # Should be generated in better way, not hardcoded.
  36. self._isInfillOrSkinTypeMap = numpy.array([0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1], dtype=numpy.bool)
  37. self._build_cache_line_mesh_mask = None
  38. self._build_cache_needed_points = None
  39. def buildCache(self):
  40. # For the line mesh we do not draw Infill or Jumps. Therefore those lines are filtered out.
  41. self._build_cache_line_mesh_mask = numpy.logical_not(numpy.logical_or(self._jump_mask, self._types == LayerPolygon.InfillType ))
  42. mesh_line_count = numpy.sum(self._build_cache_line_mesh_mask)
  43. self._index_begin = 0
  44. self._index_end = mesh_line_count
  45. self._build_cache_needed_points = numpy.ones((len(self._types), 2), dtype=numpy.bool)
  46. # Only if the type of line segment changes do we need to add an extra vertex to change colors
  47. self._build_cache_needed_points[1:, 0][:, numpy.newaxis] = self._types[1:] != self._types[:-1]
  48. # Mark points as unneeded if they are of types we don't want in the line mesh according to the calculated mask
  49. numpy.logical_and(self._build_cache_needed_points, self._build_cache_line_mesh_mask, self._build_cache_needed_points )
  50. self._vertex_begin = 0
  51. self._vertex_end = numpy.sum( self._build_cache_needed_points )
  52. def build(self, vertex_offset, index_offset, vertices, colors, indices):
  53. if (self._build_cache_line_mesh_mask is None) or (self._build_cache_needed_points is None ):
  54. self.buildCache()
  55. line_mesh_mask = self._build_cache_line_mesh_mask
  56. needed_points_list = self._build_cache_needed_points
  57. # Index to the points we need to represent the line mesh. This is constructed by generating simple
  58. # start and end points for each line. For line segment n these are points n and n+1. Row n reads [n n+1]
  59. # Then then the indices for the points we don't need are thrown away based on the pre-calculated list.
  60. index_list = ( numpy.arange(len(self._types)).reshape((-1, 1)) + numpy.array([[0, 1]]) ).reshape((-1, 1))[needed_points_list.reshape((-1, 1))]
  61. # The relative values of begin and end indices have already been set in buildCache, so we only need to offset them to the parents offset.
  62. self._vertex_begin += vertex_offset
  63. self._vertex_end += vertex_offset
  64. # Points are picked based on the index list to get the vertices needed.
  65. vertices[self._vertex_begin:self._vertex_end, :] = self._data[index_list, :]
  66. # Create an array with colors for each vertex and remove the color data for the points that has been thrown away.
  67. colors[self._vertex_begin:self._vertex_end, :] = numpy.tile(self._colors, (1, 2)).reshape((-1, 4))[needed_points_list.ravel()]
  68. colors[self._vertex_begin:self._vertex_end, :] *= numpy.array([[0.5, 0.5, 0.5, 1.0]], numpy.float32)
  69. # The relative values of begin and end indices have already been set in buildCache, so we only need to offset them to the parents offset.
  70. self._index_begin += index_offset
  71. self._index_end += index_offset
  72. indices[self._index_begin:self._index_end, :] = numpy.arange(self._index_end-self._index_begin, dtype=numpy.int32).reshape((-1, 1))
  73. # When the line type changes the index needs to be increased by 2.
  74. indices[self._index_begin:self._index_end, :] += numpy.cumsum(needed_points_list[line_mesh_mask.ravel(), 0], dtype=numpy.int32).reshape((-1, 1))
  75. # Each line segment goes from it's starting point p to p+1, offset by the vertex index.
  76. # The -1 is to compensate for the neccecarily True value of needed_points_list[0,0] which causes an unwanted +1 in cumsum above.
  77. indices[self._index_begin:self._index_end, :] += numpy.array([self._vertex_begin - 1, self._vertex_begin])
  78. self._build_cache_line_mesh_mask = None
  79. self._build_cache_needed_points = None
  80. def getColors(self):
  81. return self._colors
  82. def mapLineTypeToColor(self, line_types):
  83. return self._color_map[line_types]
  84. def isInfillOrSkinType(self, line_types):
  85. return self._isInfillOrSkinTypeMap[line_types]
  86. def lineMeshVertexCount(self):
  87. return (self._vertex_end - self._vertex_begin)
  88. def lineMeshElementCount(self):
  89. return (self._index_end - self._index_begin)
  90. @property
  91. def extruder(self):
  92. return self._extruder
  93. @property
  94. def types(self):
  95. return self._types
  96. @property
  97. def data(self):
  98. return self._data
  99. @property
  100. def elementCount(self):
  101. return (self._index_end - self._index_begin) * 2 # The range of vertices multiplied by 2 since each vertex is used twice
  102. @property
  103. def lineWidths(self):
  104. return self._line_widths
  105. @property
  106. def jumpMask(self):
  107. return self._jump_mask
  108. @property
  109. def meshLineCount(self):
  110. return self._mesh_line_count
  111. @property
  112. def jumpCount(self):
  113. return self._jump_count
  114. # Calculate normals for the entire polygon using numpy.
  115. def getNormals(self):
  116. normals = numpy.copy(self._data)
  117. normals[:, 1] = 0.0 # We are only interested in 2D normals
  118. # Calculate the edges between points.
  119. # The call to numpy.roll shifts the entire array by one so that
  120. # we end up subtracting each next point from the current, wrapping
  121. # around. This gives us the edges from the next point to the current
  122. # point.
  123. normals = numpy.diff(normals, 1, 0)
  124. # Calculate the length of each edge using standard Pythagoras
  125. lengths = numpy.sqrt(normals[:, 0] ** 2 + normals[:, 2] ** 2)
  126. # The normal of a 2D vector is equal to its x and y coordinates swapped
  127. # and then x inverted. This code does that.
  128. normals[:, [0, 2]] = normals[:, [2, 0]]
  129. normals[:, 0] *= -1
  130. # Normalize the normals.
  131. normals[:, 0] /= lengths
  132. normals[:, 2] /= lengths
  133. return normals
  134. # Should be generated in better way, not hardcoded.
  135. __color_map = numpy.array([
  136. [1.0, 1.0, 1.0, 1.0], # NoneType
  137. [1.0, 0.0, 0.0, 1.0], # Inset0Type
  138. [0.0, 1.0, 0.0, 1.0], # InsetXType
  139. [1.0, 1.0, 0.0, 1.0], # SkinType
  140. [0.0, 1.0, 1.0, 1.0], # SupportType
  141. [0.0, 1.0, 1.0, 1.0], # SkirtType
  142. [1.0, 0.75, 0.0, 1.0], # InfillType
  143. [0.0, 1.0, 1.0, 1.0], # SupportInfillType
  144. [0.0, 0.0, 1.0, 1.0], # MoveCombingType
  145. [0.5, 0.5, 1.0, 1.0], # MoveRetractionType
  146. [0.25, 0.75, 1.0, 1.0] # SupportInterfaceType
  147. ])