Adapting code to merge of simple_convex_hull and code cleanup.

This commit is contained in:
Johan Kristensen 2016-06-28 23:50:00 +02:00
parent 788f79f5ba
commit 41503d7d5b
5 changed files with 65 additions and 50 deletions

View file

@ -59,7 +59,7 @@ class Layer:
result_index_offset += polygon.lineMeshElementCount() result_index_offset += polygon.lineMeshElementCount()
self._element_count += polygon.elementCount self._element_count += polygon.elementCount
return (result_vertex_offset,result_index_offset) return (result_vertex_offset, result_index_offset)
def createMesh(self): def createMesh(self):
return self.createMeshOrJumps(True) return self.createMeshOrJumps(True)
@ -68,56 +68,47 @@ class Layer:
return self.createMeshOrJumps(False) return self.createMeshOrJumps(False)
def createMeshOrJumps(self, make_mesh): def createMeshOrJumps(self, make_mesh):
builder = MeshBuilder() # This is never really used, only the mesh_data inside builder = MeshBuilder()
index_pattern = numpy.array([[0,3,2,0,1,3]],dtype = numpy.int32 ) index_pattern = numpy.array([[0, 3, 2, 0, 1, 3]], dtype = numpy.int32 )
line_count = 0 line_count = 0
if make_mesh: if make_mesh:
for polygon in self._polygons: for polygon in self._polygons:
line_count += polygon._mesh_line_count line_count += polygon.meshLineCount
else: else:
for polygon in self._polygons: for polygon in self._polygons:
line_count += polygon._jump_count line_count += polygon.jumpCount
# Reserve the neccesary space for the data upfront # Reserve the neccesary space for the data upfront
builder.reserveFaceAndVerticeCount( 2*line_count, 4*line_count ) builder.reserveFaceAndVertexCount(2 * line_count, 4 * line_count)
for polygon in self._polygons: for polygon in self._polygons:
#if make_mesh and (polygon.type == LayerPolygon.MoveCombingType or polygon.type == LayerPolygon.MoveRetractionType): # Filter out the types of lines we are not interesed in depending on whether we are drawing the mesh or the jumps.
# continue index_mask = numpy.logical_not(polygon.jumpMask) if make_mesh else polygon.jumpMask
#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 keep those we whant to draw based on make_mesh
points = numpy.concatenate((polygon.data[:-1], polygon.data[1:]), 1)[index_mask.ravel()]
# 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 of the points we want to draw
line_types = polygon._types[index_mask] line_types = polygon.types[index_mask]
# Shift the z-axis according to previous implementation.
#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: if make_mesh:
points[polygon._orInfillSkin[line_types],1::3] -= 0.01 points[polygon.isInfillOrSkinType(line_types), 1::3] -= 0.01
else: else:
points[:,1::3] += 0.01 points[:, 1::3] += 0.01
# Create an array with normals and tile 2 copies to match size of points variable # 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)) 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. # Scale all normals by the line width of the current line so we can easily offset.
normals *= (polygon.lineWidths[index_mask.ravel()] / 2) 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 # 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)) 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 # 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_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) f_colors = numpy.repeat(polygon.mapLineTypeToColor(line_types), 4, 0)
builder.addFacesWithColor(f_points, f_indices, f_colors) builder.addFacesWithColor(f_points, f_indices, f_colors)

View file

@ -65,7 +65,6 @@ class LayerDataBuilder(MeshBuilder):
( vertex_offset, index_offset ) = data.build( vertex_offset, index_offset, vertices, colors, indices) ( vertex_offset, index_offset ) = data.build( vertex_offset, index_offset, vertices, colors, indices)
self._element_counts[layer] = data.elementCount self._element_counts[layer] = data.elementCount
self.clear()
self.addVertices(vertices) self.addVertices(vertices)
self.addColors(colors) self.addColors(colors)
self.addIndices(indices.flatten()) self.addIndices(indices.flatten())

View file

@ -38,25 +38,24 @@ class LayerPolygon:
self._colors = self.__color_map[self._types] self._colors = self.__color_map[self._types]
self._color_map = self.__color_map self._color_map = self.__color_map
# type == LayerPolygon.InfillType or type == LayerPolygon.SkinType or type == LayerPolygon.SupportInfillType # When type is used as index returns true if type == LayerPolygon.InfillType or type == LayerPolygon.SkinType or type == LayerPolygon.SupportInfillType
# Should be generated in better way, not hardcoded. # Should be generated in better way, not hardcoded.
self._orInfillSkin = numpy.array([0,0,0,1,0,0,1,1,0,0],dtype=numpy.bool) self._isInfillOrSkinTypeMap = numpy.array([0, 0, 0, 1, 0, 0, 1, 1, 0, 0], dtype=numpy.bool)
self._build_cache_line_mesh_mask = None self._build_cache_line_mesh_mask = None
self._build_cache_needed_points = None self._build_cache_needed_points = None
def build_cache(self): def buildCache(self):
#if polygon.type == LayerPolygon.InfillType or polygon.type == LayerPolygon.MoveCombingType or polygon.type == LayerPolygon.MoveRetractionType: # For the line mesh we do not draw Infill or Jumps. Therefore those lines are filtered out.
# continue self._build_cache_line_mesh_mask = numpy.logical_not(numpy.logical_or(self._jump_mask, self._types == LayerPolygon.InfillType ))
self._build_cache_line_mesh_mask = numpy.logical_not(numpy.logical_or(self._jump_mask,self._types == LayerPolygon.InfillType ))
mesh_line_count = numpy.sum(self._build_cache_line_mesh_mask) mesh_line_count = numpy.sum(self._build_cache_line_mesh_mask)
self._index_begin = 0 self._index_begin = 0
self._index_end = mesh_line_count self._index_end = mesh_line_count
self._build_cache_needed_points = numpy.ones((len(self._types),2), dtype=numpy.bool) self._build_cache_needed_points = numpy.ones((len(self._types), 2), dtype=numpy.bool)
# Only if the type of line segment changes do we need to add an extra vertex to change colors # Only if the type of line segment changes do we need to add an extra vertex to change colors
self._build_cache_needed_points[1:,0][:,numpy.newaxis] = self._types[1:] != self._types[:-1] self._build_cache_needed_points[1:, 0][:, numpy.newaxis] = self._types[1:] != self._types[:-1]
# Remove points of types we don't want in the line mesh # Mark points as unneeded if they are of types we don't want in the line mesh according to the calculated mask
numpy.logical_and(self._build_cache_needed_points, self._build_cache_line_mesh_mask, self._build_cache_needed_points ) numpy.logical_and(self._build_cache_needed_points, self._build_cache_line_mesh_mask, self._build_cache_needed_points )
self._vertex_begin = 0 self._vertex_begin = 0
@ -65,29 +64,36 @@ class LayerPolygon:
def build(self, vertex_offset, index_offset, vertices, colors, indices): def build(self, vertex_offset, index_offset, vertices, colors, indices):
if (self._build_cache_line_mesh_mask == None) or (self._build_cache_needed_points == None ): if (self._build_cache_line_mesh_mask == None) or (self._build_cache_needed_points == None ):
self.build_cache() self.buildCache()
line_mesh_mask = self._build_cache_line_mesh_mask line_mesh_mask = self._build_cache_line_mesh_mask
needed_points_list = self._build_cache_needed_points needed_points_list = self._build_cache_needed_points
index_list = ( numpy.arange(len(self._types)).reshape((-1,1)) + numpy.array([[0,1]]) ).reshape((-1,1))[needed_points_list.reshape((-1,1))] # Index to the points we need to represent the line mesh. This is constructed by generating simple
# start and end points for each line. For line segment n these are points n and n+1. Row n reads [n n+1]
# Then then the indices for the points we don't need are thrown away based on the pre-calculated list.
index_list = ( numpy.arange(len(self._types)).reshape((-1, 1)) + numpy.array([[0, 1]]) ).reshape((-1, 1))[needed_points_list.reshape((-1, 1))]
# The relative values of begin and end indices have already been set in buildCache, so we only need to offset them to the parents offset.
self._vertex_begin += vertex_offset self._vertex_begin += vertex_offset
self._vertex_end += vertex_offset self._vertex_end += vertex_offset
# Points are picked based on the index list to get the vertices needed.
vertices[self._vertex_begin:self._vertex_end, :] = self._data[index_list, :] vertices[self._vertex_begin:self._vertex_end, :] = self._data[index_list, :]
colors[self._vertex_begin:self._vertex_end, :] = numpy.tile(self._colors,(1,2)).reshape((-1,4))[needed_points_list.ravel()] # Create an array with colors for each vertex and remove the color data for the points that has been thrown away.
colors[self._vertex_begin:self._vertex_end, :] = numpy.tile(self._colors, (1, 2)).reshape((-1, 4))[needed_points_list.ravel()]
colors[self._vertex_begin:self._vertex_end, :] *= numpy.array([[0.5, 0.5, 0.5, 1.0]], numpy.float32) colors[self._vertex_begin:self._vertex_end, :] *= numpy.array([[0.5, 0.5, 0.5, 1.0]], numpy.float32)
# The relative values of begin and end indices have already been set in buildCache, so we only need to offset them to the parents offset.
self._index_begin += index_offset self._index_begin += index_offset
self._index_end += index_offset self._index_end += index_offset
indices[self._index_begin:self._index_end,:] = numpy.arange(self._index_end-self._index_begin, dtype=numpy.int32).reshape((-1,1)) indices[self._index_begin:self._index_end, :] = numpy.arange(self._index_end-self._index_begin, dtype=numpy.int32).reshape((-1, 1))
# When the line type changes the index needs to be increased by 2. # When the line type changes the index needs to be increased by 2.
indices[self._index_begin:self._index_end,:] += numpy.cumsum(needed_points_list[line_mesh_mask.ravel(),0],dtype=numpy.int32).reshape((-1,1)) indices[self._index_begin:self._index_end, :] += numpy.cumsum(needed_points_list[line_mesh_mask.ravel(), 0], dtype=numpy.int32).reshape((-1, 1))
# Each line segment goes from it's starting point p to p+1, offset by the vertex index. # Each line segment goes from it's starting point p to p+1, offset by the vertex index.
# The -1 is to compensate for the neccecarily True value of needed_points_list[0,0] which causes an unwanted +1 in cumsum above. # The -1 is to compensate for the neccecarily True value of needed_points_list[0,0] which causes an unwanted +1 in cumsum above.
indices[self._index_begin:self._index_end,:] += numpy.array([self._vertex_begin - 1,self._vertex_begin]) indices[self._index_begin:self._index_end, :] += numpy.array([self._vertex_begin - 1, self._vertex_begin])
self._build_cache_line_mesh_mask = None self._build_cache_line_mesh_mask = None
self._build_cache_needed_points = None self._build_cache_needed_points = None
@ -95,6 +101,12 @@ class LayerPolygon:
def getColors(self): def getColors(self):
return self._colors return self._colors
def mapLineTypeToColor(self, line_types):
return self._color_map[line_types]
def isInfillOrSkinType(self, line_types):
return self._isInfillOrSkinTypeMap[line_types]
def lineMeshVertexCount(self): def lineMeshVertexCount(self):
return (self._vertex_end - self._vertex_begin) return (self._vertex_end - self._vertex_begin)
@ -117,6 +129,18 @@ class LayerPolygon:
def lineWidths(self): def lineWidths(self):
return self._line_widths return self._line_widths
@property
def jumpMask(self):
return self._jump_mask
@property
def meshLineCount(self):
return self._mesh_line_count
@property
def jumpCount(self):
return self._jump_count
# Calculate normals for the entire polygon using numpy. # Calculate normals for the entire polygon using numpy.
def getNormals(self): def getNormals(self):
normals = numpy.copy(self._data) normals = numpy.copy(self._data)
@ -166,4 +190,5 @@ class LayerPolygon:
[1.0, 0.74, 0.0, 1.0], [1.0, 0.74, 0.0, 1.0],
[0.0, 1.0, 1.0, 1.0], [0.0, 1.0, 1.0, 1.0],
[0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0],
[0.5, 0.5, 1.0, 1.0]]) [0.5, 0.5, 1.0, 1.0]
])

View file

@ -114,7 +114,7 @@ class ProcessSlicedLayersJob(Job):
new_points /= 1000 new_points /= 1000
this_poly = LayerPolygon.LayerPolygon(layer_data, line_types, new_points, line_widths) this_poly = LayerPolygon.LayerPolygon(layer_data, line_types, new_points, line_widths)
this_poly.build_cache() this_poly.buildCache()
this_layer.polygons.append(this_poly) this_layer.polygons.append(this_poly)

View file

@ -262,9 +262,9 @@ class _CreateTopLayersJob(Job):
# Scale layer color by a brightness factor based on the current layer number # Scale layer color by a brightness factor based on the current layer number
# This will result in a range of 0.5 - 1.0 to multiply colors by. # This will result in a range of 0.5 - 1.0 to multiply colors by.
brightness = numpy.ones((1,4),dtype=numpy.float32) * (2.0 - (i / self._solid_layers)) / 2.0 brightness = numpy.ones((1,4), dtype=numpy.float32) * (2.0 - (i / self._solid_layers)) / 2.0
brightness[0,3] = 1.0; brightness[0, 3] = 1.0;
layer_mesh.addColors(layer.getColors() * brightness ) layer_mesh.addColors(layer.getColors() * brightness)
if self._cancel: if self._cancel:
return return