diff --git a/cura/LayerData.py b/cura/LayerData.py index 1cf13a1798..421b2589cb 100644 --- a/cura/LayerData.py +++ b/cura/LayerData.py @@ -131,7 +131,7 @@ class Layer(): continue if not make_mesh and not (polygon.type == Polygon.MoveCombingType or polygon.type == Polygon.MoveRetractionType): continue - + poly_color = polygon.getColor() points = numpy.copy(polygon.data) @@ -140,26 +140,7 @@ class Layer(): if polygon.type == Polygon.MoveCombingType or polygon.type == Polygon.MoveRetractionType: points[:,1] += 0.01 - # Calculate normals for the entire polygon using numpy. - normals = numpy.copy(points) - normals[:,1] = 0.0 # We are only interested in 2D normals - - # Calculate the edges between points. - # The call to numpy.roll shifts the entire array by one so that - # we end up subtracting each next point from the current, wrapping - # around. This gives us the edges from the next point to the current - # point. - normals[:] = normals[:] - numpy.roll(normals, -1, axis = 0) - # Calculate the length of each edge using standard Pythagoras - lengths = numpy.sqrt(normals[:,0] ** 2 + normals[:,2] ** 2) - # The normal of a 2D vector is equal to its x and y coordinates swapped - # and then x inverted. This code does that. - normals[:,[0, 2]] = normals[:,[2, 0]] - normals[:,0] *= -1 - - # Normalize the normals. - normals[:,0] /= lengths - normals[:,2] /= lengths + normals = polygon.getNormals() # Scale all by the line width of the polygon so we can easily offset. normals *= (polygon.lineWidth / 2) @@ -199,16 +180,33 @@ class Polygon(): self._data = data self._line_width = line_width / 1000 + if type == self.Inset0Type: + self._color = Color(1.0, 0.0, 0.0, 1.0) + elif self._type == self.InsetXType: + self._color = Color(0.0, 1.0, 0.0, 1.0) + elif self._type == self.SkinType: + self._color = Color(1.0, 1.0, 0.0, 1.0) + elif self._type == self.SupportType: + self._color = Color(0.0, 1.0, 1.0, 1.0) + elif self._type == self.SkirtType: + self._color = Color(0.0, 1.0, 1.0, 1.0) + elif self._type == self.InfillType: + self._color = Color(1.0, 0.74, 0.0, 1.0) + elif self._type == self.SupportInfillType: + self._color = Color(0.0, 1.0, 1.0, 1.0) + elif self._type == self.MoveCombingType: + self._color = Color(0.0, 0.0, 1.0, 1.0) + elif self._type == self.MoveRetractionType: + self._color = Color(0.5, 0.5, 1.0, 1.0) + else: + self._color = Color(1.0, 1.0, 1.0, 1.0) + def build(self, offset, vertices, colors, indices): self._begin = offset self._end = self._begin + len(self._data) - 1 - color = self.getColor() - color.setValues(color.r * 0.5, color.g * 0.5, color.b * 0.5, color.a) - color = numpy.array([color.r, color.g, color.b, color.a], numpy.float32) - vertices[self._begin:self._end + 1, :] = self._data[:, :] - colors[self._begin:self._end + 1, :] = color + 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) for i in range(self._begin, self._end): indices[i, 0] = i @@ -218,26 +216,7 @@ class Polygon(): indices[self._end, 1] = self._begin def getColor(self): - if self._type == self.Inset0Type: - return Color(1.0, 0.0, 0.0, 1.0) - elif self._type == self.InsetXType: - return Color(0.0, 1.0, 0.0, 1.0) - elif self._type == self.SkinType: - return Color(1.0, 1.0, 0.0, 1.0) - elif self._type == self.SupportType: - return Color(0.0, 1.0, 1.0, 1.0) - elif self._type == self.SkirtType: - return Color(0.0, 1.0, 1.0, 1.0) - elif self._type == self.InfillType: - return Color(1.0, 0.74, 0.0, 1.0) - elif self._type == self.SupportInfillType: - return Color(0.0, 1.0, 1.0, 1.0) - elif self._type == self.MoveCombingType: - return Color(0.0, 0.0, 1.0, 1.0) - elif self._type == self.MoveRetractionType: - return Color(0.5, 0.5, 1.0, 1.0) - else: - return Color(1.0, 1.0, 1.0, 1.0) + return self._color def vertexCount(self): return len(self._data) @@ -257,3 +236,27 @@ class Polygon(): @property def lineWidth(self): return self._line_width + + # Calculate normals for the entire polygon using numpy. + def getNormals(self): + normals = numpy.copy(self._data) + normals[:,1] = 0.0 # We are only interested in 2D normals + + # Calculate the edges between points. + # The call to numpy.roll shifts the entire array by one so that + # we end up subtracting each next point from the current, wrapping + # around. This gives us the edges from the next point to the current + # point. + normals[:] = normals[:] - numpy.roll(normals, -1, axis = 0) + # Calculate the length of each edge using standard Pythagoras + lengths = numpy.sqrt(normals[:,0] ** 2 + normals[:,2] ** 2) + # The normal of a 2D vector is equal to its x and y coordinates swapped + # and then x inverted. This code does that. + normals[:,[0, 2]] = normals[:,[2, 0]] + normals[:,0] *= -1 + + # Normalize the normals. + normals[:,0] /= lengths + normals[:,2] /= lengths + + return normals diff --git a/plugins/CuraEngineBackend/ProcessSlicedObjectListJob.py b/plugins/CuraEngineBackend/ProcessSlicedObjectListJob.py index 09acab8a7e..5176f79eeb 100644 --- a/plugins/CuraEngineBackend/ProcessSlicedObjectListJob.py +++ b/plugins/CuraEngineBackend/ProcessSlicedObjectListJob.py @@ -67,14 +67,14 @@ class ProcessSlicedObjectListJob(Job): continue for l in range(object.repeatedMessageCount("layers")): - layer = object.getRepeatedMessage("layers", i) + layer = object.getRepeatedMessage("layers", l) layer_data.addLayer(layer.id) layer_data.setLayerHeight(layer.id, layer.height) layer_data.setLayerThickness(layer.id, layer.thickness) for p in range(layer.repeatedMessageCount("polygons")): - polygon = layer.getRepeatedMessage("polygons", i) + polygon = layer.getRepeatedMessage("polygons", p) points = numpy.fromstring(polygon.points, dtype="i8") # Convert bytearray to numpy array points = points.reshape((-1,2)) # We get a linear list of pairs that make up the points, so make numpy interpret them correctly. @@ -88,8 +88,6 @@ class ProcessSlicedObjectListJob(Job): layer_data.addPolygon(layer.id, polygon.type, points, polygon.line_width) - Job.yieldThread() - current_layer += 1 progress = (current_layer / layer_count) * 100 # TODO: Rebuild the layer data mesh once the layer has been processed.