Layer.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from .LayerPolygon import LayerPolygon
  2. from UM.Math.Vector import Vector
  3. from UM.Mesh.MeshBuilder import MeshBuilder
  4. import numpy
  5. class Layer:
  6. def __init__(self, layer_id):
  7. self._id = layer_id
  8. self._height = 0.0
  9. self._thickness = 0.0
  10. self._polygons = []
  11. self._element_count = 0
  12. @property
  13. def height(self):
  14. return self._height
  15. @property
  16. def thickness(self):
  17. return self._thickness
  18. @property
  19. def polygons(self):
  20. return self._polygons
  21. @property
  22. def elementCount(self):
  23. return self._element_count
  24. def setHeight(self, height):
  25. self._height = height
  26. def setThickness(self, thickness):
  27. self._thickness = thickness
  28. def vertexCount(self):
  29. result = 0
  30. for polygon in self._polygons:
  31. result += polygon.vertexCount()
  32. return result
  33. def build(self, offset, vertices, colors, indices):
  34. result = offset
  35. for polygon in self._polygons:
  36. if polygon.type == LayerPolygon.InfillType or polygon.type == LayerPolygon.MoveCombingType or polygon.type == LayerPolygon.MoveRetractionType:
  37. continue
  38. polygon.build(result, vertices, colors, indices)
  39. result += polygon.vertexCount()
  40. self._element_count += polygon.elementCount
  41. return result
  42. def createMesh(self):
  43. return self.createMeshOrJumps(True)
  44. def createJumps(self):
  45. return self.createMeshOrJumps(False)
  46. def createMeshOrJumps(self, make_mesh):
  47. builder = MeshBuilder()
  48. for polygon in self._polygons:
  49. if make_mesh and (polygon.type == LayerPolygon.MoveCombingType or polygon.type == LayerPolygon.MoveRetractionType):
  50. continue
  51. if not make_mesh and not (polygon.type == LayerPolygon.MoveCombingType or polygon.type == LayerPolygon.MoveRetractionType):
  52. continue
  53. poly_color = polygon.getColor()
  54. points = numpy.copy(polygon.data)
  55. if polygon.type == LayerPolygon.InfillType or polygon.type == LayerPolygon.SkinType or polygon.type == LayerPolygon.SupportInfillType:
  56. points[:,1] -= 0.01
  57. if polygon.type == LayerPolygon.MoveCombingType or polygon.type == LayerPolygon.MoveRetractionType:
  58. points[:,1] += 0.01
  59. normals = polygon.getNormals()
  60. # Scale all by the line width of the polygon so we can easily offset.
  61. normals *= (polygon.lineWidth / 2)
  62. #TODO: Use numpy magic to perform the vertex creation to speed up things.
  63. for i in range(len(points)):
  64. start = points[i - 1]
  65. end = points[i]
  66. normal = normals[i - 1]
  67. point1 = Vector(data = start - normal)
  68. point2 = Vector(data = start + normal)
  69. point3 = Vector(data = end + normal)
  70. point4 = Vector(data = end - normal)
  71. builder.addQuad(point1, point2, point3, point4, color = poly_color)
  72. return builder.build()