diff options
author | Johan K <johan@3dverkstan.se> | 2016-06-14 19:08:35 +0300 |
---|---|---|
committer | Johan Kristensen <johan@3dverkstan.se> | 2016-06-28 23:29:29 +0300 |
commit | f184baadf0a396743268b29027a1d4031d73991d (patch) | |
tree | f540a0cf093b58d573e7f2bc4469269c71afe55c /cura/Layer.py | |
parent | ac0f743855ed2df9d9542724b911a8f3a43dd07c (diff) |
Formulate layerview logic using numpy to speed up. Also changed layer data packets from engine to make it possible.
Diffstat (limited to 'cura/Layer.py')
-rw-r--r-- | cura/Layer.py | 114 |
1 files changed, 70 insertions, 44 deletions
diff --git a/cura/Layer.py b/cura/Layer.py index 904e5528a3..cb64d77c2d 100644 --- a/cura/Layer.py +++ b/cura/Layer.py @@ -35,24 +35,31 @@ class Layer: def setThickness(self, thickness): self._thickness = thickness - def vertexCount(self): + def lineMeshVertexCount(self): result = 0 for polygon in self._polygons: - result += polygon.vertexCount() + result += polygon.lineMeshVertexCount() return result - def build(self, offset, vertices, colors, indices): - result = offset + def lineMeshElementCount(self): + result = 0 for polygon in self._polygons: - if polygon.type == LayerPolygon.InfillType or polygon.type == LayerPolygon.MoveCombingType or polygon.type == LayerPolygon.MoveRetractionType: - continue + result += polygon.lineMeshElementCount() + + return result - polygon.build(result, vertices, colors, indices) - result += polygon.vertexCount() + def build(self, vertex_offset, index_offset, vertices, colors, indices): + result_vertex_offset = vertex_offset + result_index_offset = index_offset + self._element_count = 0 + for polygon in self._polygons: + polygon.build(result_vertex_offset, result_index_offset, vertices, colors, indices) + result_vertex_offset += polygon.lineMeshVertexCount() + result_index_offset += polygon.lineMeshElementCount() self._element_count += polygon.elementCount - return result + return (result_vertex_offset,result_index_offset) def createMesh(self): return self.createMeshOrJumps(True) @@ -61,39 +68,58 @@ class Layer: return self.createMeshOrJumps(False) def createMeshOrJumps(self, make_mesh): - builder = MeshBuilder() - + builder = MeshBuilder() # This is never really used, only the mesh_data inside + index_pattern = numpy.array([[0,3,2,0,1,3]],dtype = numpy.int32 ) + + line_count = 0 + if make_mesh: + for polygon in self._polygons: + line_count += polygon._mesh_line_count + else: + for polygon in self._polygons: + line_count += polygon._jump_count + + + # Reserve the neccesary space for the data upfront + builder.reserveFaceAndVerticeCount( 2*line_count, 4*line_count ) + for polygon in self._polygons: - if make_mesh and (polygon.type == LayerPolygon.MoveCombingType or polygon.type == LayerPolygon.MoveRetractionType): - continue - if not make_mesh and not (polygon.type == LayerPolygon.MoveCombingType or polygon.type == LayerPolygon.MoveRetractionType): - continue - - poly_color = polygon.getColor() - - points = numpy.copy(polygon.data) - if polygon.type == LayerPolygon.InfillType or polygon.type == LayerPolygon.SkinType or polygon.type == LayerPolygon.SupportInfillType: - points[:,1] -= 0.01 - if polygon.type == LayerPolygon.MoveCombingType or polygon.type == LayerPolygon.MoveRetractionType: - points[:,1] += 0.01 - - normals = polygon.getNormals() - - # Scale all by the line width of the polygon so we can easily offset. - normals *= (polygon.lineWidth / 2) - - #TODO: Use numpy magic to perform the vertex creation to speed up things. - for i in range(len(points)): - start = points[i - 1] - end = points[i] - - normal = normals[i - 1] - - point1 = Vector(data = start - normal) - point2 = Vector(data = start + normal) - point3 = Vector(data = end + normal) - point4 = Vector(data = end - normal) - - builder.addQuad(point1, point2, point3, point4, color = poly_color) - - return builder.build() + #if make_mesh and (polygon.type == LayerPolygon.MoveCombingType or polygon.type == LayerPolygon.MoveRetractionType): + # continue + #if not make_mesh and not (polygon.type == LayerPolygon.MoveCombingType or polygon.type == LayerPolygon.MoveRetractionType): + # continue + + index_mask = numpy.logical_not(polygon._jump_mask) if make_mesh else polygon._jump_mask + + # Create an array with rows [p p+1] and only save those we whant to draw based on make_mesh + points = numpy.concatenate((polygon.data[:-1],polygon.data[1:]),1)[index_mask.ravel()] + # Line types of the points we want to draw + line_types = polygon._types[index_mask] + + + #if polygon.type == LayerPolygon.InfillType or polygon.type == LayerPolygon.SkinType or polygon.type == LayerPolygon.SupportInfillType: + # points[:,1] -= 0.01 + #if polygon.type == LayerPolygon.MoveCombingType or polygon.type == LayerPolygon.MoveRetractionType: + # points[:,1] += 0.01 + # Shift the z-axis according to previous implementation. + if make_mesh: + points[polygon._orInfillSkin[line_types],1::3] -= 0.01 + else: + points[:,1::3] += 0.01 + + # Create an array with normals and tile 2 copies to match size of points variable + normals = numpy.tile( polygon.getNormals()[index_mask.ravel()], (1,2)) + + # Scale all normals by the line width of the current line so we can easily offset. + normals *= (polygon.lineWidths[index_mask.ravel()] / 2) + + # Create 4 points to draw each line segment, points +- normals results in 2 points each. Reshape to one point per line + f_points = numpy.concatenate((points-normals,points+normals),1).reshape((-1,3)) + # index_pattern defines which points to use to draw the two faces for each lines egment, the following linesegment is offset by 4 + f_indices = ( index_pattern + numpy.arange(0,4*len(normals),4,dtype=numpy.int32).reshape((-1,1)) ).reshape((-1,3)) + f_colors = numpy.repeat(polygon._color_map[line_types], 4, 0) + + builder.addFacesWithColor(f_points, f_indices, f_colors) + + + return builder.build()
\ No newline at end of file |