def calculate_centerlines(model_name, model_polydata_name, source_ids, target_ids): ''' Calculate centerlines and the distance to centerlines array. The distance to centerlines is stored in polydata referenced by 'dist_name'. ''' lines_name = model_name + "_lines" sep_lines_name = model_name + "_sep_lines" voronoi_name = model_name + "_voronoi" dist_name = model_name + "_distance" sv.vmtk_utils.centerlines(model_polydata_name, source_ids, target_ids, lines_name, voronoi_name) sv.vmtk_utils.separate_centerlines(lines_name, sep_lines_name) sv.vmtk_utils.distance_to_centerlines(model_polydata_name, sep_lines_name, dist_name) # Display the centerlines. lines_actor = sv_vis.pRepos(renderer, sep_lines_name)[1] lines_actor.GetProperty().SetColor(0, 1, 0) lines_file_name = lines_name + '.vtp' sv.repository.write_vtk_polydata(sep_lines_name, "ascii", lines_file_name) dist_pd = sv.repository.export_to_vtk(dist_name) dist_array = dist_pd.GetPointData().GetArray('DistanceToCenterlines') dist_range = 2 * [0.0] dist_array.GetRange(dist_range, 0) print("Minumum distance: {0:f}".format(dist_range[0])) return dist_name
def create_contours(path_name, control_points, radius, ren): print("=============== create_contours ============") p = Path.pyPath() p.NewObject(path_name) ## Set path points. for i in range(0, len(control_points)): p.AddPoint(control_points[i]) p.CreatePath() points = p.GetPathPosPts() control_points = p.GetControlPts() pos_pts = p.GetPathPosPts() ## Create circle contours at each control point. # for i in range(0, len(control_points)): name = path_name + "_ct" + str(i) print("Create a circle Contour: " + name) Contour.set_contour_kernel('Circle') c = Contour.pyContour() pt = control_points[i] ## Find index of the point in pos_pts[] corresponding # to the ith control point. # min_d = 1e9 min_i = -1 for j in range(0, len(pos_pts)): pos = pos_pts[j] d = sum([(pt[k] - pos[k]) * (pt[k] - pos[k]) for k in range(3)]) if (d < min_d): min_d = d min_i = j #__for j in range(0,len(pos_pts)) print(">>> min_d %g min_id %d" % (min_d, min_i)) ## Create a contour at the min_i th pos_pts[]. c.new_object(name, path_name, min_i) # Set control points. center = control_points[i] c.SetCtrlPtsByRadius(center, radius) # Creat contour. c.Create() print(">>> Contour center: " + str(c.Center())) # Get countour PolyData pname = name + 'p' c.GetPolyData(pname) #c.GetPolyData('ctp') act = vis.pRepos(ren, pname)
def read_solid_model(model_name): ''' Read in a solid model. ''' solid_file_name = os.getcwd() + '/' + model_name + '.vtp' sv.solid.set_kernel('PolyData') solid = sv.solid.SolidModel() solid.read_native(model_name, solid_file_name) solid.get_boundary_faces(60) print("Model face IDs: " + str(solid.get_face_ids())) model_polydata_name = model_name + "_pd" solid.get_polydata(model_polydata_name) model_actor = sv_vis.pRepos(renderer, model_polydata_name)[1] model_actor.GetProperty().SetColor(0.8, 0.8, 0.8) #sv_vis.polyDisplayWireframe(renderer, model_polydata) sv_vis.polyDisplayPoints(renderer, model_polydata_name) return solid, model_polydata_name, solid_file_name
def display_sphere(model_polydata_name, id): ''' Display a sphere at the given point. ''' model_polydata = sv.repository.export_to_vtk(model_polydata_name) points = model_polydata.GetPoints() pt = [0.0, 0.0, 0.0] points.GetPoint(id, pt) sphere = vtk.vtkSphereSource() sphere.SetCenter(pt[0], pt[1], pt[2]) sphere.SetRadius(0.05) sphere.Update() sphere_name = "sphere" + str(id) sv.repository.import_vtk_polydata(sphere.GetOutput(), sphere_name) sphere_actor = sv_vis.pRepos(renderer, sphere_name)[1] sphere_actor.GetProperty().SetColor(1, 1, 1) sv_vis.polyDisplayWireframe(renderer, sphere_name)
def get_face_center(solid, face_id, color=[1, 0, 0]): ''' Get the center of a solid model face. ''' model_face = model_name + "_face_" + str(face_id) solid.get_face_polydata(model_face, face_id) face_pd = sv.repository.export_to_vtk(model_face) com_filter = vtk.vtkCenterOfMass() com_filter.SetInputData(face_pd) com_filter.Update() face_center = com_filter.GetCenter() # Show the face. face_actor = sv_vis.pRepos(renderer, model_face)[1] #sv_vis.polyDisplayWireframe(renderer, model_face_2) face_actor.GetProperty().SetColor(color[0], color[1], color[2]) return face_center
sfile.write('bct_write_dat {0:s}\n'.format('bct.dat')) sfile.write('bct_write_vtp {0:s}\n'.format('bct.vtp')) # Outlet BCs if solid_name == 'cylinder': sfile.write("pressure_vtp {0:s} {1:f} \n".format( sim_mesh_surf + "face_2.vtp", 0.0)) elif solid_name == "demo": sfile.write("pressure_vtp {0:s} {1:f} \n".format( sim_mesh_surf + "face_3.vtp", 0.0)) sfile.write("pressure_vtp {0:s} {1:f} \n".format( sim_mesh_surf + "face_4.vtp", 0.0)) # Write bc files. sfile.write('write_numstart 0 {0:s}\n'.format('numstart.dat')) sfile.write('write_geombc {0:s}\n'.format('geombc.dat.1')) sfile.write('write_restart {0:s}\n'.format('restart.0.1')) #-------------------------------------------------------------# # G r a p h i c s # #-------------------------------------------------------------# # Optional display of models and meshes. if (False): vis.pRepos(renderer, solid_pd) # Show graphics window. if use_graphics: vis.interact(renderer, sys.maxsize)
solid.ReadNative(inner_solid_name, 'aorta-inner.vtp') # Create polydata to display the model. inner_solid_pd = inner_solid_name + "_pd" solid.GetPolyData(inner_solid_pd, 1.0) #vis.pRepos(renderer, inner_solid_pd) ## Read outer aorta solid model. solid = sv.Solid.pySolidModel() outer_solid_name = 'outer_aorta' solid.ReadNative(outer_solid_name, 'aorta-outer.vtp') # Create polydata to display the model. outer_solid_pd = outer_solid_name + "_pd" solid.GetPolyData(outer_solid_pd, 1.0) #vis.pRepos(renderer, outer_solid_pd) #vis.polyDisplayWireframe(renderer, outer_solid_pd) ## Subtract inner aorta from outer aorta. wall_solid_name = 'subtract_inner_outer_aorta' solid.Subtract(wall_solid_name, outer_solid_name, inner_solid_name) wall_solid_pd = wall_solid_name + '_pd' solid.GetBoundaryFaces(80) print("Model face IDs: " + str(solid.GetFaceIds())) solid.GetPolyData(wall_solid_pd, 0.1) vis.pRepos(renderer, wall_solid_pd) # Write the wall model. solid.WriteNative('aorta-wall.vtp') vis.interact(renderer, sys.maxsize)
min_i = j #__for j in range(0,len(pos_pts)) print(">>> min_d %g min_id %d" % (min_d, min_i)) c.NewObject(name, 'path1', min_i) # Set control points. radius = 2.0 center = control_pts[i] #center = [0.0, 0.0, 0.0] c.SetCtrlPtsByRadius(center, radius) #print ("Radius: " + str(radius)) #print ("Center: " + str(center)) # Creat contour. c.Create() print("Get contour properties ") print(" Center: " + str(c.Center())) print(" Area: " + str(c.Area())) # Get countour PolyData pname = name + 'p' c.GetPolyData(pname) #c.GetPolyData('ctp') act = vis.pRepos(ren, pname) #vis.polyDisplayWireframe(ren, name) ## Display the contour. ## vis.interact(ren, 1500000000)
vtk.vtkMath.Normalize(axis) center = [(pt2[i] + pt1[i])/2.0 for i in range(0,3)] print('[PathFitCyl] Axis: [{0:f} {1:f} {2:f}]'.format(axis[0], axis[1], axis[2])) print('[PathFitCyl] Center: [{0:f} {1:f} {2:f}]'.format(center[0], center[1], center[2])) Solid.SetKernel('PolyData') solid = Solid.pySolidModel() radius = 1.0 length = dist solid.Cylinder('cylinder', radius, length, center, axis) solid.GetPolyData('cylPolydata', 0.5) ren, renwin = vis.initRen('demo') vis.pRepos(ren,'cylPolydata') ## Create a circle contour. # for i in range(0,len(control_pts)): name = "ct" + str(i) print ("==================================") print ("Create a circle Contour: " + name) Contour.set_contour_kernel('Circle') c = Contour.pyContour() pt = control_pts[i] min_d = 1e9 min_i = -1 for j in range(0,len(pos_pts)): pos = pos_pts[j]
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. from sv import * import sv_vis as vis #displat a box and a cylinder in a render window Solid.SetKernel('PolyData') ctr = [0, 0, 0] axis = [0, 0, 1] a = Solid.pySolidModel() a.Cylinder('cyl', 1, 5, ctr, axis) a.GetPolyData('poly', 0.5) b = Solid.pySolidModel() b.Box3d('box', [5, 0.5, 0.5], [0, 0, 0]) b.GetPolyData('poly2', 0.5) ren, renwin = vis.initRen('demo') act = vis.pRepos(ren, 'poly') act2 = vis.pRepos(ren, 'poly2') vis.polyDisplayWireframe(ren, 'poly2') vis.interact(ren, 150)
center = [0.0, 0.0, 0.0] axis = [0.0, 0.0, 1.0] solid.cylinder('cyl', 1.5, 10, center, axis) # Store solid polydata in repository under the name 'cyl_pd'. solid.get_polydata('cyl_pd', 0.5) #------------------------------- # Show what's in the repository #------------------------------- repo_list = sv.repository.list() print("Repository: " + str(repo_list)) #print(dir(sv)) sv.dmg.import_polydata_from_repository('cyl_pd') #-------------------------------------------------- # Show the solid geometry stored in the repository #-------------------------------------------------- renderer, render_window = vis.initRen('demo') try: actor = vis.pRepos(renderer, 'cyl_pd') # can't export this. actor = vis.pRepos(renderer, 'cyl') except sv.repository.RepositoryException as e: print(e) vis.interact(renderer, sys.maxsize)
path = sv.path.Path() path.new_object(path_name) ## Set path points. for i in range(0,len(path_control_points)): path.add_control_point(path_control_points[i]) ## Create path geometry? path.create() points = path.get_curve_points() control_points = path.get_control_points() # Get path PolyData path_pd_name = path_name + 'pd' path.get_polydata(path_pd_name) vis.pRepos(ren, path_pd_name) #---------------------------------------------------- # Create Contours #---------------------------------------------------- # Create circle contours at each path control point. # radius = 1.0 for i in range(0,len(path_control_points)): name = path_name + "_ct" + str(i) print ("Create a circle Contour: " + name) sv.contour.set_contour_kernel('Circle') cont = sv.contour.Contour() pt = control_points[i]
print("\n[geom_stats_demo] Geom.All_union()") inter_t = True destination_name = merged_solid_name_pd + "_merged_again" #result = Geom.All_union([path1_cap_surface_name, path2_cap_surface_name], inter_t, destination_name) #print(">>> result " + str(result)) intersected_solid_name = "intersect" #Geom.Intersect(path1_surface_name, path2_surface_name, intersected_solid_name) #Geom.Intersect(path1_cap_surface_name, path2_cap_surface_name, intersected_solid_name) # Render this all to a viewer. window_name = 'contour_to_lofted_model.py' ren, renwin = vis.initRen(window_name) actor1 = vis.pRepos(ren, path1_cap_surface_name) #actor1 = vis.pRepos(ren, path1_surface_name) actor2 = vis.pRepos(ren, path2_cap_surface_name) #actor2 = vis.pRepos(ren, path2_surface_name) #actor3 = vis.pRepos(ren, destination_name) actor3 = vis.pRepos(ren, merged_solid_name_pd) #actor3 = vis.pRepos(ren, intersected_solid_name) # Set the renderer to draw the solids as a wireframe. vis.polyDisplayWireframe(ren, path1_cap_surface_name) vis.polyDisplayWireframe(ren, path2_cap_surface_name) vis.interact(ren, 15000)
for i in range(0, len(path1_control_points)): path1.add_control_point(path1_control_points[i]) pt = [520.0, 9.0, 162.0] path1.add_control_point(pt, 2) ## Create path geometry? path1.create() points1 = path1.get_curve_points() control_points_1 = path1.get_control_points() print("Control points: {0:s}".format(str(control_points_1))) # Get path PolyData path1_pd_name = path1_name + 'pd' path1.get_polydata(path1_pd_name) vis.pRepos(ren, path1_pd_name) #------------------------------------------ # Create a path 2 #------------------------------------------ path2_name = "path2" path2_control_points = [] path2_control_points.append([518.0, 11.0, 165.0]) path2_control_points.append([519.0, 10.0, 162.0]) path2_control_points.append([520.0, 9.0, 162.0]) path2_control_points.append([521.0, 8.0, 160.0]) path2 = sv.path.Path() path2.new_object(path2_name) ## Set path points.
a.GetPolyData('cyl1_pd',0.5) b = Solid.pySolidModel() b.Cylinder('cyl2', 2, 2.0, ctr, axis) b.GetPolyData('cyl2_pd',0.5) ctr = [0.5, -0.5, -0.5] ctr = [0.481476,-0.0680931,-1] ctr = [-0.414211,-0.753699,1] c = Solid.pySolidModel() c.Sphere('sphere1', 0.1, ctr) c.GetPolyData('sphere1_pd',0.5) c.WriteNative("sphere") ren, renwin = vis.initRen('demo') act = vis.pRepos(ren,'cyl1_pd') vis.polyDisplayWireframe(ren, 'cyl1_pd') act2 = vis.pRepos(ren,'cyl2_pd') vis.polyDisplayWireframe(ren, 'cyl2_pd') act3 = vis.pRepos(ren,'sphere1_pd')[1] act3.GetProperty().SetColor(0.0, 1.0, 0.0) wall = Solid.pySolidModel() wall.Subtract('wall', 'cyl2', 'cyl1') #wall.Subtract('wall', 'cyl1', 'cyl2') #wall.Subtract('cyl1', 'cyl2', 'wall') wall.GetPolyData('wall_pd', 0.5) wall_act = vis.pRepos(ren,'wall_pd')
# Create new path object. path2_name = 'path2' path2 = Path.pyPath() path2.NewObject(path2_name) # Give it some points. path2.AddPoint([0.0, 100.0, 0.0]) path2.AddPoint([0.0, 100.0, 10.0]) path2.AddPoint([0.0, 100.0, 20.0]) path2.AddPoint([1.0, 100.0, 30.0]) path2.AddPoint([0.0, 100.0, 40.0]) path2.AddPoint([0.0, 100.0, 50.0]) path2.AddPoint([0.0, 100.0, 60.0]) # Generate the path from the added control points. path2.CreatePath() # Create solids from the paths. path1_solid_name = create_solid_from_path(path1_name, 5.0) path2_solid_name = create_solid_from_path(path2_name, 5.0) # Render this all to a viewer. window_name = 'contour_to_lofted_model.py' ren, renwin = vis.initRen(window_name) actor1 = vis.pRepos(ren, path1_solid_name) actor2 = vis.pRepos(ren, path2_solid_name) # Set the renderer to draw the solids as a wireframe. vis.polyDisplayWireframe(ren, path1_solid_name) vis.polyDisplayWireframe(ren, path2_solid_name) vis.interact(ren, 15000)
print(verts) lines = meshed_solid_pd.GetLines() print("[mesh_stats_demo] .GetLines(): (length: " + str(lines.GetSize()) + ")") print(lines) polys = meshed_solid_pd.GetPolys() print("[mesh_stats_demo] .GetPolys(): (length: " + str(polys.GetSize()) + ")") print(polys) strips = meshed_solid_pd.GetStrips() print("[mesh_stats_demo] .GetStrips(): (length: " + str(strips.GetSize()) + ")") print(strips) print("[mesh_stats_demo] .GetCellType(cellId: 1): " + str(meshed_solid_pd.GetCellType(1))) bounds = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0] meshed_solid_pd.GetCellBounds(1, bounds) print("[mesh_stats_demo] .GetCellBounds(cellId: 1): " + str(bounds)) # Render this all to a viewer. window_name = "RAW Model" ren1, renwin1 = vis.initRen(window_name) actor1 = vis.pRepos(ren1, merged_solid_name) # Set the renderer to draw the solids as a wireframe. vis.polyDisplayWireframe(ren1, merged_solid_name) # Render this all to a viewer. window_name = "MESHED Model" ren2, renwin2 = vis.initRen(window_name) actor2 = vis.pRepos(ren2, meshed_solid_pd_name) # Set the renderer to draw the solids as a wireframe. vis.polyDisplayWireframe(ren2, meshed_solid_pd_name) vis.interact(ren1, 15000) vis.interact(ren2, 15000)
#------------------------------------------ # Create a path #------------------------------------------ path_name = "path1" path_control_points = [] path_control_points.append([2.0, 2.0, 0.0]) path_control_points.append([3.0, 3.0, 0.0]) path_control_points.append([4.0, 4.0, 0.0]) path_control_points.append([5.0, 5.0, 0.0]) path = sv.path.Path() path.new_object(path_name) ## Set path points. for i in range(0, len(path_control_points)): path.add_control_point(path_control_points[i]) ## Create path geometry? path.create() points = path.get_curve_points() control_points = path.get_control_points() # Get path PolyData path_pd_name = path_name + 'pd' path.get_polydata(path_pd_name) vis.pRepos(ren, path_pd_name) ## Display the path. # vis.interact(ren, 1500000000)
import sv import sv_vis as vis import vtk print(dir(sv)) sv.Solid.SetKernel('PolyData') sv.Solid.SetKernel('Parasolid') axis = [1.0, 0.0, 0.0] center = [0.0, 0.0, 0.0] solid = sv.Solid.pySolidModel() radius = 1.0 length = 10.0 solid.Cylinder('cylinder', radius, length, center, axis) solid.GetPolyData('cylPolydata', 0.5) ren, renwin = vis.initRen('demo') vis.pRepos(ren,'cylPolydata') ## Display the contour. ## vis.interact(ren, 1500000000)
def add_geometry(self, name): actor = sv_vis.pRepos(self.renderer, name)
cleaned_name = merged_solid_name_pd + "_cleaned" Geom.Clean(merged_solid_name_pd, cleaned_name) # Sometimes errors out with: "current kernel is not valid (6)" ? print("\n[geom_stats_demo] Geom.All_union()") inter_t = True destination_name = merged_solid_name_pd + "_merged_again" result = Geom.All_union([path1_surface_name, path2_surface_name], inter_t, destination_name) print("\n[geom_stats_demo] Geom.Intersect()") intersected_solid_name = "intersected_solid" Geom.Intersect(merged_solid_name_pd, cube_name_pd, intersected_solid_name) window_name = "INTERSECTED Model" ren1, renwin1 = vis.initRen(window_name) actor1 = vis.pRepos(ren1, intersected_solid_name) # Set the renderer to draw the solids as a wireframe. vis.polyDisplayWireframe(ren1, intersected_solid_name) print("\n[geom_stats_demo] Geom.Subtract()") subtracted_solid_name = "subtracted_solid" Geom.Subtract(merged_solid_name_pd, cube_name_pd, subtracted_solid_name) window_name = "SUBTRACTED Model" ren2, renwin2 = vis.initRen(window_name) actor2 = vis.pRepos(ren2, subtracted_solid_name) # Set the renderer to draw the solids as a wireframe. vis.polyDisplayWireframe(ren2, subtracted_solid_name) vis.interact(ren1, 15000) vis.interact(ren2, 15000)
renderer, render_window = vis.initRen('demo') surf_name = "shapeSnakeDiaSmooth" surf_file_name = surf_name + ".vtp" ## Create a solid model from polydata. # sv.solid.set_kernel('PolyData') solid = sv.solid.SolidModel() # Read surface polydata. solid.read_native(surf_name, surf_file_name) ## Display surface. surf_pd = surf_name + "_pd" solid.get_polydata(surf_pd, 1.0) vis.pRepos(renderer, surf_pd) vis.polyDisplayWireframe(renderer, surf_pd) ## Cap the surface. capped_surf_name = surf_name + "_capped" surf_pd += 'b' num_filled = sv.vmtk_utils.cap_with_ids(surf_pd, capped_surf_name, 0, 0) print("Number of holes filled: {0}".format(num_filled)) vis.pRepos(renderer, capped_surf_name) vis.interact(renderer, sys.maxsize)
path2.AddPoint([0.0, 0.0, 0.0]) path2.AddPoint([0.0, 1.0, 0.0]) path2.AddPoint([0.0, 2.0, 0.0]) path2.AddPoint([0.0, 3.0, 0.0]) path2.AddPoint([0.0, 4.0, 0.0]) # Generate the path from the added control points. path2.CreatePath() # Create surfaces from the paths. path1_surface_name = create_surface_from_path(path1_name, 1.0) path2_surface_name = create_surface_from_path(path2_name, 2.0) merged_solid_name = "merged_solid" Geom.Union(path1_surface_name, path2_surface_name, merged_solid_name) info = Geom.Checksurface(merged_solid_name) print(info) print("[geom_check_broken_surface] Num free edges: " + str(info[0])) print("[geom_check_broken_surface] Num bad edges: " + str(info[1])) if info[1] != 0: print(("[geom_check_broken_surface]\tHey! This model contains an open" " surface and shouldn't be used!")) # Render this all to a viewer. window_name = "MERGED Model" ren1, renwin1 = vis.initRen(window_name) actor1 = vis.pRepos(ren1, merged_solid_name) # Set the renderer to draw the solids as a wireframe. # vis.polyDisplayWireframe(ren, merged_solid_cleaned_name) vis.interact(ren1, 15000)