LayerDataBuilder.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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, line_thickness, line_feedrate):
  18. if layer not in self._layers:
  19. self.addLayer(layer)
  20. p = LayerPolygon(self, polygon_type, data, line_width, line_thickness, line_feedrate)
  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. feedrates = numpy.empty((vertex_count), numpy.float32)
  52. extruders = numpy.empty((vertex_count), numpy.float32)
  53. line_types = numpy.empty((vertex_count), numpy.float32)
  54. vertex_offset = 0
  55. index_offset = 0
  56. for layer, data in sorted(self._layers.items()):
  57. ( vertex_offset, index_offset ) = data.build( vertex_offset, index_offset, vertices, colors, line_dimensions, feedrates, extruders, line_types, indices)
  58. self._element_counts[layer] = data.elementCount
  59. self.addVertices(vertices)
  60. colors[:, 0:3] *= line_type_brightness
  61. self.addColors(colors)
  62. self.addIndices(indices.flatten())
  63. # Note: we're using numpy indexing here.
  64. # See also: https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
  65. material_colors = numpy.zeros((line_dimensions.shape[0], 4), dtype=numpy.float32)
  66. for extruder_nr in range(material_color_map.shape[0]):
  67. material_colors[extruders == extruder_nr] = material_color_map[extruder_nr]
  68. # Set material_colors with indices where line_types (also numpy array) == MoveCombingType
  69. material_colors[line_types == LayerPolygon.MoveCombingType] = colors[line_types == LayerPolygon.MoveCombingType]
  70. material_colors[line_types == LayerPolygon.MoveRetractionType] = colors[line_types == LayerPolygon.MoveRetractionType]
  71. attributes = {
  72. "line_dimensions": {
  73. "value": line_dimensions,
  74. "opengl_name": "a_line_dim",
  75. "opengl_type": "vector2f"
  76. },
  77. "extruders": {
  78. "value": extruders,
  79. "opengl_name": "a_extruder",
  80. "opengl_type": "float" # Strangely enough, the type has to be float while it is actually an int.
  81. },
  82. "colors": {
  83. "value": material_colors,
  84. "opengl_name": "a_material_color",
  85. "opengl_type": "vector4f"
  86. },
  87. "line_types": {
  88. "value": line_types,
  89. "opengl_name": "a_line_type",
  90. "opengl_type": "float"
  91. },
  92. "feedrates": {
  93. "value": feedrates,
  94. "opengl_name": "a_feedrate",
  95. "opengl_type": "float"
  96. }
  97. }
  98. return LayerData(vertices=self.getVertices(), normals=self.getNormals(), indices=self.getIndices(),
  99. colors=self.getColors(), uvs=self.getUVCoordinates(), file_name=self.getFileName(),
  100. center_position=self.getCenterPosition(), layers=self._layers,
  101. element_counts=self._element_counts, attributes=attributes)