LayerDataBuilder.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from .Layer import Layer
  4. from .LayerPolygon import LayerPolygon
  5. from UM.Mesh.MeshBuilder import MeshBuilder
  6. from .LayerData import LayerData
  7. import numpy
  8. ## Builder class for constructing a LayerData object
  9. class LayerDataBuilder(MeshBuilder):
  10. def __init__(self):
  11. super().__init__()
  12. self._layers = {}
  13. self._element_counts = {}
  14. def addLayer(self, layer):
  15. if layer not in self._layers:
  16. self._layers[layer] = Layer(layer)
  17. def addPolygon(self, layer, polygon_type, data, line_width):
  18. if layer not in self._layers:
  19. self.addLayer(layer)
  20. p = LayerPolygon(self, polygon_type, data, line_width)
  21. self._layers[layer].polygons.append(p)
  22. def getLayer(self, layer):
  23. if layer in self._layers:
  24. return self._layers[layer]
  25. def getLayers(self):
  26. return self._layers
  27. def getElementCounts(self):
  28. return self._element_counts
  29. def setLayerHeight(self, layer, height):
  30. if layer not in self._layers:
  31. self.addLayer(layer)
  32. self._layers[layer].setHeight(height)
  33. def setLayerThickness(self, layer, thickness):
  34. if layer not in self._layers:
  35. self.addLayer(layer)
  36. self._layers[layer].setThickness(thickness)
  37. ## Return the layer data as LayerData.
  38. #
  39. # \param material_color_map: [r, g, b, a] for each extruder row.
  40. # \param line_type_brightness: compatibility layer view uses line type brightness of 0.5
  41. def build(self, material_color_map, line_type_brightness = 1.0):
  42. vertex_count = 0
  43. index_count = 0
  44. for layer, data in self._layers.items():
  45. vertex_count += data.lineMeshVertexCount()
  46. index_count += data.lineMeshElementCount()
  47. vertices = numpy.empty((vertex_count, 3), numpy.float32)
  48. line_dimensions = numpy.empty((vertex_count, 2), numpy.float32)
  49. colors = numpy.empty((vertex_count, 4), numpy.float32)
  50. indices = numpy.empty((index_count, 2), numpy.int32)
  51. extruders = numpy.empty((vertex_count), numpy.float32)
  52. line_types = numpy.empty((vertex_count), numpy.float32)
  53. vertex_offset = 0
  54. index_offset = 0
  55. for layer, data in sorted(self._layers.items()):
  56. ( vertex_offset, index_offset ) = data.build( vertex_offset, index_offset, vertices, colors, line_dimensions, extruders, line_types, indices)
  57. self._element_counts[layer] = data.elementCount
  58. self.addVertices(vertices)
  59. colors[:, 0:3] *= line_type_brightness
  60. self.addColors(colors)
  61. self.addIndices(indices.flatten())
  62. # Note: we're using numpy indexing here.
  63. # See also: https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
  64. material_colors = numpy.zeros((line_dimensions.shape[0], 4), dtype=numpy.float32)
  65. for extruder_nr in range(material_color_map.shape[0]):
  66. material_colors[extruders == extruder_nr] = material_color_map[extruder_nr]
  67. # Set material_colors with indices where line_types (also numpy array) == MoveCombingType
  68. material_colors[line_types == LayerPolygon.MoveCombingType] = colors[line_types == LayerPolygon.MoveCombingType]
  69. material_colors[line_types == LayerPolygon.MoveRetractionType] = colors[line_types == LayerPolygon.MoveRetractionType]
  70. attributes = {
  71. "line_dimensions": {
  72. "value": line_dimensions,
  73. "opengl_name": "a_line_dim",
  74. "opengl_type": "vector2f"
  75. },
  76. "extruders": {
  77. "value": extruders,
  78. "opengl_name": "a_extruder",
  79. "opengl_type": "float" # Strangely enough, the type has to be float while it is actually an int.
  80. },
  81. "colors": {
  82. "value": material_colors,
  83. "opengl_name": "a_material_color",
  84. "opengl_type": "vector4f"
  85. },
  86. "line_types": {
  87. "value": line_types,
  88. "opengl_name": "a_line_type",
  89. "opengl_type": "float"
  90. }
  91. }
  92. return LayerData(vertices=self.getVertices(), normals=self.getNormals(), indices=self.getIndices(),
  93. colors=self.getColors(), uvs=self.getUVCoordinates(), file_name=self.getFileName(),
  94. center_position=self.getCenterPosition(), layers=self._layers,
  95. element_counts=self._element_counts, attributes=attributes)