Fix Layer view

This commit is contained in:
Arjen Hiemstra 2016-02-01 17:15:45 +01:00
parent e74d300fb3
commit ffec2484b7
2 changed files with 51 additions and 50 deletions

View file

@ -131,7 +131,7 @@ class Layer():
continue continue
if not make_mesh and not (polygon.type == Polygon.MoveCombingType or polygon.type == Polygon.MoveRetractionType): if not make_mesh and not (polygon.type == Polygon.MoveCombingType or polygon.type == Polygon.MoveRetractionType):
continue continue
poly_color = polygon.getColor() poly_color = polygon.getColor()
points = numpy.copy(polygon.data) points = numpy.copy(polygon.data)
@ -140,26 +140,7 @@ class Layer():
if polygon.type == Polygon.MoveCombingType or polygon.type == Polygon.MoveRetractionType: if polygon.type == Polygon.MoveCombingType or polygon.type == Polygon.MoveRetractionType:
points[:,1] += 0.01 points[:,1] += 0.01
# Calculate normals for the entire polygon using numpy. normals = polygon.getNormals()
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
# Scale all by the line width of the polygon so we can easily offset. # Scale all by the line width of the polygon so we can easily offset.
normals *= (polygon.lineWidth / 2) normals *= (polygon.lineWidth / 2)
@ -199,16 +180,33 @@ class Polygon():
self._data = data self._data = data
self._line_width = line_width / 1000 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): def build(self, offset, vertices, colors, indices):
self._begin = offset self._begin = offset
self._end = self._begin + len(self._data) - 1 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[:, :] 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): for i in range(self._begin, self._end):
indices[i, 0] = i indices[i, 0] = i
@ -218,26 +216,7 @@ class Polygon():
indices[self._end, 1] = self._begin indices[self._end, 1] = self._begin
def getColor(self): def getColor(self):
if self._type == self.Inset0Type: return self._color
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)
def vertexCount(self): def vertexCount(self):
return len(self._data) return len(self._data)
@ -257,3 +236,27 @@ class Polygon():
@property @property
def lineWidth(self): def lineWidth(self):
return self._line_width 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

View file

@ -67,14 +67,14 @@ class ProcessSlicedObjectListJob(Job):
continue continue
for l in range(object.repeatedMessageCount("layers")): for l in range(object.repeatedMessageCount("layers")):
layer = object.getRepeatedMessage("layers", i) layer = object.getRepeatedMessage("layers", l)
layer_data.addLayer(layer.id) layer_data.addLayer(layer.id)
layer_data.setLayerHeight(layer.id, layer.height) layer_data.setLayerHeight(layer.id, layer.height)
layer_data.setLayerThickness(layer.id, layer.thickness) layer_data.setLayerThickness(layer.id, layer.thickness)
for p in range(layer.repeatedMessageCount("polygons")): 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 = 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. 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) layer_data.addPolygon(layer.id, polygon.type, points, polygon.line_width)
Job.yieldThread()
current_layer += 1 current_layer += 1
progress = (current_layer / layer_count) * 100 progress = (current_layer / layer_count) * 100
# TODO: Rebuild the layer data mesh once the layer has been processed. # TODO: Rebuild the layer data mesh once the layer has been processed.