LayerDataBuilder.py 4.7 KB

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