def create_branch(renderer, lut, centerlines, cid, branch_cells, radius, end_point_ids): print("\n---------- create_branch cid {0:d} ----------".format(cid)) points = centerlines.GetPoints() num_lines = centerlines.GetNumberOfLines() ## Find ends of line. # branch_end_point_ids = [] branch_end_cell_ids = [] for cell_id in branch_cells[cid]: cell = centerlines.GetCell(cell_id) cell_pids = cell.GetPointIds() num_ids = cell_pids.GetNumberOfIds() pid1 = cell_pids.GetId(0) pid2 = cell_pids.GetId(1) if pid1 in end_point_ids: start_cell = cell_id branch_end_point_ids.append(pid1) branch_end_cell_ids.append(cell_id) elif pid2 in end_point_ids: branch_end_point_ids.append(pid2) branch_end_cell_ids.append(cell_id) print("[create_branch] End point IDs: {0:s}".format(str(branch_end_point_ids))) print("[create_branch] End cell IDs: {0:s}".format(str(branch_end_cell_ids))) for pid in branch_end_point_ids: pt = points.GetPoint(pid) color = [0.0, 0.0, 0.0] lut.GetColor(cid, color) gr.add_sphere(renderer, pt, radius, color=color, wire=True) ## Create branch geometry. # branch_geom = vtk.vtkPolyData() branch_geom.SetPoints(points) branch_lines = vtk.vtkCellArray() for cell_id in branch_cells[cid]: cell = centerlines.GetCell(cell_id) cell_pids = cell.GetPointIds() num_ids = cell_pids.GetNumberOfIds() pid1 = cell_pids.GetId(0) pid2 = cell_pids.GetId(1) line = vtk.vtkLine() line.GetPointIds().SetId(0, pid1) line.GetPointIds().SetId(1, pid2) branch_lines.InsertNextCell(line) branch_geom.SetLines(branch_lines) return branch_geom
def show_region(renderer, surface, conn_cells, color): points = surface.GetPoints() radius = 0.05 for cell_id in conn_cells: #print('----- cell {0:d} -----'.format(i)) cell = surface.GetCell(cell_id) cell_pids = cell.GetPointIds() pid1 = cell_pids.GetId(0) pt1 = points.GetPoint(pid1) pid2 = cell_pids.GetId(1) pt2 = points.GetPoint(pid2) pid3 = cell_pids.GetId(2) pt3 = points.GetPoint(pid3) center = [(pt1[i] + pt2[i] + pt3[i]) / 3.0 for i in range(3)] gr.add_sphere(renderer, center, radius, color=color, wire=True)
def show_branch(renderer, lut, centerlines, cid, branch_cells, radius): num_lines = centerlines.GetNumberOfLines() cell_mask = centerlines.GetCellData().GetArray("CellMask") for i in range(num_lines): cell_mask.SetValue(i,0); for cell_id in branch_cells[cid]: cell_mask.SetValue(cell_id,1); color = [0.0, 0.0, 0.0] lut.GetColor(cid, color) points = centerlines.GetPoints() cell_id = branch_cells[cid][0] cell = centerlines.GetCell(cell_id) cell_pids = cell.GetPointIds() pid = cell_pids.GetId(0) start_pt = points.GetPoint(pid) gr.add_sphere(renderer, start_pt, radius, color=color, wire=True) #print("Start cell ID: {0:d}".format(cell_id)) #print("Start point ID: {0:d}".format(pid)) cell_id = branch_cells[cid][-1] cell = centerlines.GetCell(cell_id) cell_pids = cell.GetPointIds() pid = cell_pids.GetId(0) end_pt = points.GetPoint(pid) gr.add_sphere(renderer, end_pt, radius, color=color, wire=False) #print("End cell ID: {0:d}".format(cell_id)) #print("End point ID: {0:d}".format(pid)) thresh = vtk.vtkThreshold() thresh.SetInputData(centerlines) thresh.ThresholdBetween(1, 1) thresh.SetInputArrayToProcess(0, 0, 0, "vtkDataObject::FIELD_ASSOCIATION_CELLS", "CellMask") thresh.Update() surfacefilter = vtk.vtkDataSetSurfaceFilter() surfacefilter.SetInputData(thresh.GetOutput()) surfacefilter.Update() branch_geom = surfacefilter.GetOutput() gr.add_geometry(renderer, branch_geom, color=color, line_width=4) return branch_geom
## Create renderer and graphics window. win_width = 500 win_height = 500 renderer, renderer_window = gr.init_graphics(win_width, win_height) ## Create a segmentation at path index 5. seg1 = create_segmentation(renderer, path, path_index=0) #normal = seg1.get_normal() #print("Normal: " + str(normal)) ## Show segmentation. gr.create_segmentation_geometry(renderer, seg1, color=[1.0, 0.0, 0.0]) ## Change segmentation frame. #curve_frame = path.get_curve_frame(10) #print("New segmentation center: {0:s}".format(str(curve_frame.position))) #seg1.set_frame(frame=curve_frame) #gr.create_segmentation_geometry(renderer, seg1, color=[0.0, 1.0, 1.0]) #gr.add_sphere(renderer, curve_frame.position, 0.2, color=[1.0, 1.0, 0.0], wire=True) # Show path. gr.create_path_geometry(renderer, path) ## Add a sphere at the origin. gr.add_sphere(renderer, [0.0,0.0,0.0], 0.5, color=[1.0, 1.0, 1.0], wire=True) # Display window. gr.display(renderer_window)
contours = sv_contour.read_contours() ## cont1 # cont1 = contours[10] center = cont1.get_center() cont1_polydata = cont1.get_polydata() gr.create_contour_geometry(renderer, cont1) # Create interpolated geometry for the contour. cont1_ipd = sv.geometry.interpolate_closed_curve(cont1_polydata, num_samples) # Get two points on interpolated curve. pt = 3 * [0.0] cont1_ipd.GetPoints().GetPoint(0, pt) gr.add_sphere(renderer, pt, radius, color=[1, 0, 0]) cont1_ipd.GetPoints().GetPoint(4, pt) gr.add_sphere(renderer, pt, radius, color=[0, 1, 0]) gr.add_geometry(renderer, cont1_ipd) ## cont2 # cont2 = contours[11] points2 = cont2.get_points() cont2_polydata = cont2.get_polydata() gr.create_contour_geometry(renderer, cont2) cont2_ipd = sv.geometry.interpolate_closed_curve(cont2_polydata, num_samples) ## Align contours. use_dist = False
## Write the model. file_name = str(script_path / "cylinder-model-write") file_format = "vtp" cylinder.write(file_name=file_name, format=file_format) ## Create renderer and graphics window. win_width = 500 win_height = 500 renderer, renderer_window = gr.init_graphics(win_width, win_height) ## Add model polydata. gr.add_geometry(renderer, cylinder_pd, color=[0.0, 1.0, 0.0], wire=True, edges=False) ## Add a sphere. gr.add_sphere(renderer, center=center, radius=0.1, color=[1.0, 1.0, 1.0], wire=True) pt1 = center pt2 = [center[i] + length / 2.0 * axis[i] for i in range(3)] gr.add_line(renderer, pt1, pt2, color=[0.5, 0.0, 0.0], width=4) # Display window. gr.display(renderer_window)
def create_branch_old(renderer, lut, centerlines, cid, branch_cells, radius, end_point_ids): print("\n---------- create_branch cid {0:d} ----------".format(cid)) points = centerlines.GetPoints() num_lines = centerlines.GetNumberOfLines() start_cell = None start_ids = [] point_map = defaultdict(int) cell_map = defaultdict(list) num_branch_points = 0 branch_point_ids = [] for i,cell_id in enumerate(branch_cells[cid]): #print("[create_branch] cell_id: {0:d}".format(cell_id)) #print("[create_branch] pid1: {0:d} pid2: {1:d}".format(pid1 ,pid2)) cell = centerlines.GetCell(cell_id) cell_pids = cell.GetPointIds() num_ids = cell_pids.GetNumberOfIds() pid1 = cell_pids.GetId(0) pid2 = cell_pids.GetId(1) print("[create_branch] cell: {0:d} {1:d}".format(pid1 ,pid2)) if pid1 in end_point_ids: start_cell = cell_id start_ids.append(pid1) #print("[create_branch] i: {0:d}".format(i)) #print("[create_branch] cell_id: {0:d}".format(cell_id)) #print("[create_branch] 1) pid1: {0:d} pid2: {1:d}".format(pid1 ,pid2)) elif pid2 in end_point_ids: start_cell = cell_id start_ids.append(pid2) #print("[create_branch] i: {0:d}".format(i)) #print("[create_branch] cell_id: {0:d}".format(cell_id)) #print("[create_branch] 2) pid1: {0:d} pid2: {1:d}".format(pid1 ,pid2)) cell_map[pid1].append(pid2) if pid1 not in point_map: point_map[pid1] = num_branch_points branch_point_ids.append(pid1) num_branch_points += 1 if pid2 not in point_map: point_map[pid2] = num_branch_points branch_point_ids.append(pid2) num_branch_points += 1 print("[create_branch] Number of branch points: {0:d}".format(num_branch_points)) print("[create_branch] cell_map[] size: {0:d}".format(len(cell_map))) print("[create_branch] Start cell: {0:d}".format(start_cell)) print("[create_branch] Start point IDs: {0:s}".format(str(start_ids))) if len(start_ids) == 2: max_radius_data = centerlines.GetPointData().GetArray('MaximumInscribedSphereRadius') if max_radius_data.GetValue(start_ids[0]) > max_radius_data.GetValue(start_ids[1]): start_id = start_ids[0] else: start_id = start_ids[1] else: start_id = start_ids[0] print("[create_branch] Start point ID: {0:d}".format(start_id)) start_pt = points.GetPoint(start_id) color = [0.0, 0.0, 0.0] lut.GetColor(cid, color) gr.add_sphere(renderer, start_pt, radius, color=color, wire=True) pt = points.GetPoint(146) gr.add_sphere(renderer, pt, 1.5*radius, color=[1,0,0], wire=True) ## Create branch geometry starting from start_pt. # branch_geom = vtk.vtkPolyData() branch_points = vtk.vtkPoints() pt = 3*[0.0] for pid in branch_point_ids: points.GetPoint(pid, pt) branch_points.InsertNextPoint(pt); branch_geom.SetPoints(points) #branch_geom.SetPoints(branch_points) branch_lines = vtk.vtkCellArray() pid1 = start_id pid2 = cell_map[pid1][0] num_branch_lines = 0 while (pid2 != start_id): #print("cell {0:d} {1:d}".format(pid1, pid2)) bpid1 = point_map[pid1] bpid2 = point_map[pid2] line = vtk.vtkLine() line.GetPointIds().SetId(0, bpid1) line.GetPointIds().SetId(1, bpid2) #line.GetPointIds().SetId(0, pid1) #line.GetPointIds().SetId(1, pid2) branch_lines.InsertNextCell(line) if pid2 not in cell_map: print("cell {0:d} {1:d}".format(pid1, pid2)) print("**** pid2 {0:d} not in map".format(pid2)) print(" len(cell_map[pid1]) {0:d} ".format(len(cell_map[pid1]))) if len(cell_map[pid1]) == 1: break pid1 = cell_map[pid1][1] print(" pid1 {0:d}".format(pid1)) else: pid1 = pid2 pid2 = cell_map[pid1][0] num_branch_lines += 1 print("num_branch_lines: {0:d}".format(num_branch_lines)) branch_geom.SetLines(branch_lines) return branch_geom
for (key, value) in sorted(options.get_values().items()) ] print("\n\n") ## Read in a model used to visualize the blend radius. file_name = str(data_path / 'geometry' / 'two-cyls-with-GlobalBoundaryPoints.vtp') reader = vtk.vtkXMLPolyDataReader() reader.SetFileName(file_name) reader.Update() bnd_model = reader.GetOutput() ## Compute center of union junction and visualize the blend radius. blend_radius = 1.0 center = compute_junction_center(renderer, bnd_model) gr.add_sphere(renderer, center, blend_radius, color=[0.0, 1.0, 0.0], wire=True) ## Read model to blend. file_name = str(data_path / 'geometry' / 'two-cyls.vtp') reader = vtk.vtkXMLPolyDataReader() reader.SetFileName(file_name) reader.Update() model = reader.GetOutput() ## Set faces to blend. if "two-cyls.vtp" in file_name: blend_faces = [{'radius': blend_radius, 'face1': 1, 'face2': 2}] ## Perform the blend operation. blend = sv.geometry.local_blend(surface=model, faces=blend_faces,
## Write the mesh. file_name = str(script_path / (model_name + '-sphere-refine-mesh.vtu')) mesher.write_mesh(file_name) ## Get the mesh as a vtkUnstructuredGrid. mesh = mesher.get_mesh() print("Mesh:") print(" Number of nodes: {0:d}".format(mesh.GetNumberOfPoints())) print(" Number of elements: {0:d}".format(mesh.GetNumberOfCells())) ## Show the mesh. # show_mesh = True if show_mesh: win_width = 500 win_height = 500 renderer, renderer_window = gr.init_graphics(win_width, win_height) #mesh_polydata = gr.convert_ug_to_polydata(mesh) mesh_surface = mesher.get_surface() gr.add_geometry(renderer, mesh_surface, color=[1.0, 1.0, 1.0], wire=False, edges=True) gr.add_sphere(renderer, center, radius, color=[0, 1, 0], wire=True) gr.display(renderer_window)
def add_cap(self, inlet=True): print("========== add caps ==========") print("[add_caps] Inlet: {0:d}".format(inlet)) print("[add_caps] Number of inner edge components: {0:d}".format( len(self.inner_surface_edges_comp))) print("[add_caps] Number of outer edge components: {0:d}".format( len(self.outer_surface_edges_comp))) inner_contour, outer_contour = self.get_edge_component(inlet) inner_points = inner_contour.GetPoints() num_inner_points = inner_points.GetNumberOfPoints() #print("[add_caps] Number of inner points: {0:d}".format(num_inner_points)) outer_points = outer_contour.GetPoints() num_outer_points = outer_points.GetNumberOfPoints() #print("[add_caps] Number of outer points: {0:d}".format(num_outer_points)) gr.add_geometry(self.renderer, inner_contour, [0.0, 1.0, 0.0], wire=False) gr.add_geometry(self.renderer, outer_contour, [1.0, 0.0, 0.0], wire=False) cap_polydata = vtk.vtkPolyData() cap_points = vtk.vtkPoints() cap_cells = vtk.vtkCellArray() tri = vtk.vtkTriangle() ## Add points. # pt = 3 * [0.0] for i in range(num_inner_points): pt = inner_points.GetPoint(i) cap_points.InsertNextPoint(pt[0], pt[1], pt[2]) for i in range(num_inner_points): pt = outer_points.GetPoint(i) cap_points.InsertNextPoint(pt[0], pt[1], pt[2]) r = 0.02 gr.add_sphere(renderer, inner_points.GetPoint(0), r, color=[1.0, 0.0, 0.0], wire=False) gr.add_sphere(renderer, inner_points.GetPoint(1), r, color=[0.0, 1.0, 0.0], wire=False) gr.add_sphere(renderer, inner_points.GetPoint(2), r, color=[0.0, 0.0, 1.0], wire=False) gr.add_sphere(renderer, inner_points.GetPoint(3), r, color=[1.0, 1.0, 0.0], wire=False) gr.add_sphere(renderer, inner_points.GetPoint(4), r, color=[1.0, 0.0, 1.0], wire=False) gr.add_sphere(renderer, outer_points.GetPoint(0), r, color=[1.0, 0.0, 0.0], wire=False) gr.add_sphere(renderer, outer_points.GetPoint(1), r, color=[0.0, 1.0, 0.0], wire=False) gr.add_sphere(renderer, outer_points.GetPoint(2), r, color=[0.0, 0.0, 1.0], wire=False) gr.add_sphere(renderer, outer_points.GetPoint(3), r, color=[1.0, 1.0, 0.0], wire=False) gr.add_sphere(renderer, outer_points.GetPoint(4), r, color=[1.0, 0.0, 1.0], wire=False) ## Add triangles. # n = num_inner_points #for i in range(0,2): for i in range(num_inner_points): j = (i + 1) % num_inner_points #print("i {0:d} j {1:d}".format(i,j)) tri = vtk.vtkTriangle() tri.GetPointIds().SetId(0, i) tri.GetPointIds().SetId(1, i + n) tri.GetPointIds().SetId(2, j + n) cap_cells.InsertNextCell(tri) tri = vtk.vtkTriangle() tri.GetPointIds().SetId(0, i) tri.GetPointIds().SetId(1, j + n) tri.GetPointIds().SetId(2, j) cap_cells.InsertNextCell(tri) cap_polydata.SetPoints(cap_points) cap_polydata.SetPolys(cap_cells) gr.add_geometry(self.renderer, cap_polydata, [1.0, 1.0, 1.0], wire=False) return cap_polydata