LayerPolygon.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. def __init__(self, mesh, polygon_type, data, line_width):
  15. self._mesh = mesh
  16. self._type = polygon_type
  17. self._data = data
  18. self._line_width = line_width / 1000
  19. self._begin = 0
  20. self._end = 0
  21. self._color = self.__color_map[polygon_type]
  22. def build(self, offset, vertices, colors, indices):
  23. self._begin = offset
  24. self._end = self._begin + len(self._data) - 1
  25. vertices[self._begin:self._end + 1, :] = self._data[:, :]
  26. colors[self._begin:self._end + 1, :] = numpy.array([self._color.r * 0.5, self._color.g * 0.5, self._color.b * 0.5, self._color.a], numpy.float32)
  27. for i in range(self._begin, self._end):
  28. indices[i, 0] = i
  29. indices[i, 1] = i + 1
  30. indices[self._end, 0] = self._end
  31. indices[self._end, 1] = self._begin
  32. def getColor(self):
  33. return self._color
  34. def vertexCount(self):
  35. return len(self._data)
  36. @property
  37. def type(self):
  38. return self._type
  39. @property
  40. def data(self):
  41. return self._data
  42. @property
  43. def elementCount(self):
  44. return ((self._end - self._begin) + 1) * 2 # The range of vertices multiplied by 2 since each vertex is used twice
  45. @property
  46. def lineWidth(self):
  47. return self._line_width
  48. # Calculate normals for the entire polygon using numpy.
  49. def getNormals(self):
  50. normals = numpy.copy(self._data)
  51. normals[:, 1] = 0.0 # We are only interested in 2D normals
  52. # Calculate the edges between points.
  53. # The call to numpy.roll shifts the entire array by one so that
  54. # we end up subtracting each next point from the current, wrapping
  55. # around. This gives us the edges from the next point to the current
  56. # point.
  57. normals[:] = normals[:] - numpy.roll(normals, -1, axis = 0)
  58. # Calculate the length of each edge using standard Pythagoras
  59. lengths = numpy.sqrt(normals[:, 0] ** 2 + normals[:, 2] ** 2)
  60. # The normal of a 2D vector is equal to its x and y coordinates swapped
  61. # and then x inverted. This code does that.
  62. normals[:, [0, 2]] = normals[:, [2, 0]]
  63. normals[:, 0] *= -1
  64. # Normalize the normals.
  65. normals[:, 0] /= lengths
  66. normals[:, 2] /= lengths
  67. return normals
  68. __color_map = {
  69. NoneType: Color(1.0, 1.0, 1.0, 1.0),
  70. Inset0Type: Color(1.0, 0.0, 0.0, 1.0),
  71. InsetXType: Color(0.0, 1.0, 0.0, 1.0),
  72. SkinType: Color(1.0, 1.0, 0.0, 1.0),
  73. SupportType: Color(0.0, 1.0, 1.0, 1.0),
  74. SkirtType: Color(0.0, 1.0, 1.0, 1.0),
  75. InfillType: Color(1.0, 0.74, 0.0, 1.0),
  76. SupportInfillType: Color(0.0, 1.0, 1.0, 1.0),
  77. MoveCombingType: Color(0.0, 0.0, 1.0, 1.0),
  78. MoveRetractionType: Color(0.5, 0.5, 1.0, 1.0),
  79. }