def doExport(inputFilePath, outputFilePath, resolution, FOLDERNAME): mesh = list(stl_reader.read_stl_verticies(inputFilePath)) (scale, shift, bounding_box) = slice.calculateScaleAndShift(mesh, resolution) mesh = list(slice.scaleAndShiftMesh(mesh, scale, shift)) #Note: vol should be addressed with vol[z][x][y] vol = np.zeros((bounding_box[2],bounding_box[0],bounding_box[1]), dtype=bool) for height in range(bounding_box[2]): print('Processing layer %d/%d'%(height+1,bounding_box[2])) lines = slice.toIntersectingLines(mesh, height) prepixel = np.zeros((bounding_box[0], bounding_box[1]), dtype=bool) perimeter.linesToVoxels(lines, prepixel) vol[height] = prepixel vol, bounding_box = padVoxelArray(vol) outputFilePattern, outputFileExtension = os.path.splitext(outputFilePath) currentPath = os.getcwd() outputFilePath = os.path.join(currentPath, outputFilePath) if outputFileExtension == '.png': exportPngs(vol, bounding_box, outputFilePath) elif outputFileExtension == '.xyz': exportXyz(vol, bounding_box, outputFilePath) elif outputFileExtension == '.svx': exportSvx(vol, bounding_box, outputFilePath, scale, shift) voxelSum(FOLDERNAME) for i in range(2): os.chdir('..')
def calculateResolution(inputFilePath, voxelSpacing): mesh = list(stl_reader.read_stl_verticies(inputFilePath)) allPoints = [item for sublist in mesh for item in sublist] mins = np.zeros(3) maxs = np.zeros(3) for i in range(3): mins[i] = min(allPoints, key=lambda tri: tri[i])[i] maxs[i] = max(allPoints, key=lambda tri: tri[i])[i] ranges = maxs - mins maxRange = ranges[:2].max() # Add a 0.5 px buffer on each side. return math.ceil(maxRange / voxelSpacing) + 1
def voxelsFromStl (inputFilePath, resolution): mesh = list(stl_reader.read_stl_verticies(inputFilePath)) (scale, shift, bounding_box) = slice.calculateScaleAndShift(mesh, resolution) mesh = list(slice.scaleAndShiftMesh(mesh, scale, shift)) #Note: vol should be addressed with vol[z][x][y] vol = np.zeros((bounding_box[2],bounding_box[0],bounding_box[1]), dtype=bool) for height in range(bounding_box[2]): # print('Processing layer %d/%d'%(height+1,bounding_box[2])) lines = slice.toIntersectingLines(mesh, height) prepixel = np.zeros((bounding_box[0], bounding_box[1]), dtype=bool) perimeter.linesToVoxels(lines, prepixel) vol[height] = prepixel vol, bounding_box = padVoxelArray(vol) # outputFilePattern, outputFileExtension = os.path.splitext(outputFilePath) return vol
def doExport(inputFilePath, outputFilePath, resolution, pad): mesh = stl_reader.read_stl_verticies(inputFilePath) (scale, shift, bounding_box) = slice.calculateScaleAndShift(mesh, resolution) if not any(scale): print('Too small resolution: %d' % resolution) return mesh = slice.scaleAndShiftMesh(mesh, scale, shift) vol, bounding_box = slice.meshToPlane(mesh, bounding_box, pad) outputFilePattern, outputFileExtension = os.path.splitext(outputFilePath) if outputFileExtension == '.png': exportPngs(vol, bounding_box, outputFilePath) elif outputFileExtension == '.xyz': exportXyz(vol, bounding_box, outputFilePath) elif outputFileExtension == '.svx': exportSvx(vol, bounding_box, outputFilePath, scale, shift)
def voxelsFromStl(inputFilePath, resolution): mesh = list(stl_reader.read_stl_verticies(inputFilePath)) (scale, shift, bounding_box) = slice.calculateScaleAndShift(mesh, resolution) mesh = list(slice.scaleAndShiftMesh(mesh, scale, shift)) #Note: vol should be addressed with vol[z][x][y] vol = np.zeros((bounding_box[2], bounding_box[0], bounding_box[1]), dtype=bool) for height in range(bounding_box[2]): # print('Processing layer %d/%d'%(height+1,bounding_box[2])) lines = slice.toIntersectingLines(mesh, height) prepixel = np.zeros((bounding_box[0], bounding_box[1]), dtype=bool) perimeter.linesToVoxels(lines, prepixel) vol[height] = prepixel vol, bounding_box = padVoxelArray(vol) # outputFilePattern, outputFileExtension = os.path.splitext(outputFilePath) return vol
def doExport(inputFilePath, outputFilePath, resolution, size): mesh = list(stl_reader.read_stl_verticies(inputFilePath)) scale, shift, bounding_box = slice.calculateScaleAndShift(mesh, resolution) if size: size = np.array(size) # Here, bounding box is still (x,y,z) for i, d in enumerate(['x', 'y', 'z']): if size[i] < bounding_box[i]: raise ValueError("Supplied size for Dimension {} ({}) is less than computed bounding box ({})!".format(d, size[i], bounding_box[i])) print("Overwriting computed size {} with new size {}".format(bounding_box, size), file=sys.stderr) # Need to adjust the scale in order to center the image in the frame offset = (size - bounding_box) / 2 # apply scaling offset /= scale # Add to shift shift += offset # set new bounding box bounding_box = size mesh = list(slice.scaleAndShiftMesh(mesh, scale, shift)) #Note: vol should be addressed with vol[z][x][y] vol = np.empty((bounding_box[2], bounding_box[0], bounding_box[1]), dtype=bool) for height in tqdm.tqdm(range(bounding_box[2]), desc='Processing Slice'): lines = slice.toIntersectingLines(mesh, height) prepixel = np.zeros((bounding_box[0], bounding_box[1]), dtype=bool) perimeter.linesToVoxels(lines, prepixel) vol[height] = prepixel if not size: # Adds two extra voxels # Only needed if bbox is not given explicitly vol, bounding_box = padVoxelArray(vol) _, outputFileExtension = os.path.splitext(outputFilePath) if outputFileExtension == '.png': exportPngs(vol, bounding_box, outputFilePath) elif outputFileExtension == '.xyz': exportXyz(vol, bounding_box, outputFilePath) elif outputFileExtension == '.svx': exportSvx(vol, bounding_box, outputFilePath, scale, shift) elif outputFileExtension == '.mhd': exportMhd(vol, bounding_box, outputFilePath, scale)
def doExport(inputFilePath, outputFilePath, resolution): mesh = list(stl_reader.read_stl_verticies(inputFilePath)) (scale, shift, bounding_box) = slice.calculateScaleAndShift(mesh, resolution) mesh = list(slice.scaleAndShiftMesh(mesh, scale, shift)) # Note: vol should be addressed with vol[z][x][y] vol = np.zeros((bounding_box[2], bounding_box[0], bounding_box[1]), dtype=bool) events = slice.generateEvents(mesh) current_triangle_indecies = set() slice_height = -1 for (z, status, tri_ind) in events: while z - slice_height >= 1: slice_height += 1 print('Processing layer %d/%d' % (slice_height + 1, bounding_box[2])) prepixel = np.zeros((bounding_box[0], bounding_box[1]), dtype=bool) mesh_subset = [] for index in current_triangle_indecies: mesh_subset.append(mesh[index]) lines = slice.toIntersectingLines(mesh_subset, slice_height) perimeter.linesToVoxels(lines, prepixel) vol[slice_height] = prepixel if status == 'start': assert tri_ind not in current_triangle_indecies current_triangle_indecies.add(tri_ind) elif status == 'end': assert tri_ind in current_triangle_indecies current_triangle_indecies.remove(tri_ind) vol, bounding_box = padVoxelArray(vol) outputFilePattern, outputFileExtension = os.path.splitext(outputFilePath) if outputFileExtension == '.png': exportPngs(vol, bounding_box, outputFilePath) elif outputFileExtension == '.xyz': exportXyz(vol, bounding_box, outputFilePath) elif outputFileExtension == '.svx': exportSvx(vol, bounding_box, outputFilePath, scale, shift)
def doExport(inputFilePath, outputFilePath, resolution=100, pad=1, parallel=False): org_mesh = stl_reader.read_stl_verticies(inputFilePath) vol_mesh, scale, shift, bounding_box = slice.scaleAndShiftMesh( org_mesh, resolution) if scale == 0: print('Too small resolution: %d' % resolution) return vol, bounding_box = slice.meshToPlane(vol_mesh, bounding_box, pad, parallel) outputFilePattern, outputFileExtension = os.path.splitext(outputFilePath) if outputFileExtension == '.png': exportPngs(vol, bounding_box, outputFilePath) elif outputFileExtension == '.xyz': exportXyz(vol, bounding_box, outputFilePath) elif outputFileExtension == '.svx': exportSvx(vol, bounding_box, outputFilePath, scale, shift)
def testslice(self): mesh = list(stl_reader.read_stl_verticies("./stls/cube.stl"))