def mayavi_addPoly(fig, polydata, color=(0.5, 0.5, 0.5)): r"""Draw a VKT polydata object to a mayavi figure mesh = mayavi_addPoly(fig, polydata) Convert the polydata to V,F then plot inside mayavi Parameters ---------- fig : mlab.figure() Mayavi figure to draw into polydata : vtk.vtkPolyData object Polyhedron object from VTK Author ------ Shankar Kulumani GWU [email protected] """ vertices, faces = wavefront.polydatatomesh(polydata) scalars = np.tile(0.5, vertices.shape[0]) mesh = mlab.triangular_mesh(vertices[:, 0], vertices[:, 1], vertices[:, 2], faces, color=color, figure=fig, representation='surface') return mesh
def test_update_raycasting_mesh(): # load a polyhedron ast = asteroid.Asteroid('castalia', 0, 'obj') v, f = ast.V, ast.F nv = ast.rotate_vertices(0) nf = f # define the raycaster object caster = raycaster.RayCaster.loadmesh(v, f, flag='obb', scale=1.0) ncaster = raycaster.RayCaster.updatemesh(nv, nf) # test methods of caster V, F = wavefront.polydatatomesh(caster.polydata) nV, nF = wavefront.polydatatomesh(ncaster.polydata) np.testing.assert_allclose(V, v)
class TestCompareVTKAndWavefrontWritingOBJ(): """Here we test that we can write to OBJ files using both VTK and Wavefront modules """ filename = './data/shape_model/ITOKAWA/itokawa_low.obj' vtkPolyData = wavefront.read_obj_to_polydata(filename) input_vertices, input_faces = wavefront.polydatatomesh(vtkPolyData) # write OBJ file using both VTK and Wavefront wavefront.write_vtkPolyData(vtkPolyData, '/tmp/vtk_output') wavefront.write_obj(input_vertices, input_faces, '/tmp/wavefront_output.obj') # now read our two files polydata_vtk_output = wavefront.read_obj_to_polydata('/tmp/vtk_output.obj') vtk_output_vertices, vtk_output_faces = wavefront.polydatatomesh( polydata_vtk_output) polydata_wavefront_output = wavefront.read_obj_to_polydata( '/tmp/wavefront_output.obj') wavefront_output_vertices, wavefront_output_faces = wavefront.polydatatomesh( polydata_wavefront_output) def test_number_vertices(self): np.testing.assert_allclose(self.vtk_output_vertices.shape, self.wavefront_output_vertices.shape) def test_number_of_faces(self): np.testing.assert_allclose(self.vtk_output_faces.shape, self.wavefront_output_faces.shape) def test_vertices_equal(self): np.testing.assert_allclose(self.vtk_output_vertices, self.wavefront_output_vertices) def test_faces_equal(self): np.testing.assert_allclose(self.vtk_output_faces, self.wavefront_output_faces)
class TestVTKReadingOBJFilesItokawaHigh(): filename = './data/shape_model/ITOKAWA/itokawa_high.obj' vtkPolyData = wavefront.read_obj_to_polydata(filename) vtk_vertices, vtk_faces = wavefront.polydatatomesh(vtkPolyData) wavefront_verts, wavefront_faces = wavefront.read_obj(filename) def test_numpy_vertices_equal(self): """Convert both to numpy representation and compare """ np.testing.assert_allclose(self.vtk_vertices, self.wavefront_verts) def test_numpy_faces_equal(self): """Convert both to polydata representation and compare """ np.testing.assert_allclose(self.vtk_faces, self.wavefront_faces) def test_number_of_faces_equal(self): np.testing.assert_allclose(self.vtkPolyData.GetNumberOfPolys(), self.wavefront_faces.shape[0]) def test_number_of_vertices_equal(self): np.testing.assert_allclose(self.vtkPolyData.GetNumberOfPoints(), self.wavefront_verts.shape[0])
def vtk_mesh_subdivision(): """Subdivide a mesh into more triangles The subdivisions are increasing the faces by 4^# subdivisions """ # get the polydata for a mesh v, f = wavefront.ellipsoid_mesh(1, 2, 3, density=20) polydata = wavefront.meshtopolydata(v, f) # subdivide using appropriate filter smooth_loop = vtk.vtkLoopSubdivisionFilter() smooth_loop.SetNumberOfSubdivisions(1) # can define the number of subdivisions smooth_loop.SetInputData(polydata) smooth_loop.Update() poly_loop = vtk.vtkPolyData() poly_loop.ShallowCopy(smooth_loop.GetOutput()) smooth_butterfly = vtk.vtkButterflySubdivisionFilter() smooth_butterfly.SetNumberOfSubdivisions(3) smooth_butterfly.SetInputData(polydata) smooth_butterfly.Update() poly_butterfly = vtk.vtkPolyData() poly_butterfly.ShallowCopy(smooth_butterfly.GetOutput()) # Create a mapper and actor for initial dataset mapper = vtk.vtkPolyDataMapper() mapper.SetInputData(polydata) actor = vtk.vtkActor() actor.SetMapper(mapper) # Create a mapper and actor for smoothed dataset (vtkLoopSubdivisionFilter) mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(smooth_loop.GetOutputPort()) actor_loop = vtk.vtkActor() actor_loop.SetMapper(mapper) actor_loop.SetPosition(3, 0, 0) # Create a mapper and actor for smoothed dataset (vtkButterflySubdivisionFilter) mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(smooth_butterfly.GetOutputPort()) actor_butterfly = vtk.vtkActor() actor_butterfly.SetMapper(mapper) actor_butterfly.SetPosition(6, 0, 0) # Visualise renderer = vtk.vtkRenderer() renderWindow = vtk.vtkRenderWindow() renderWindow.AddRenderer(renderer) renderWindowInteractor = vtk.vtkRenderWindowInteractor() renderWindowInteractor.SetRenderWindow(renderWindow) # Add actors and render renderer.AddActor(actor) renderer.AddActor(actor_loop) renderer.AddActor(actor_butterfly) renderer.SetBackground(1, 1, 1) # Background color white renderWindow.SetSize(800, 800) renderWindow.Render() renderWindowInteractor.Start() # output to a new v, f array # convert polydata to numpy v_loop, f_loop = wavefront.polydatatomesh(poly_loop) v_butterfly, f_butterfly = wavefront.polydatatomesh(poly_butterfly) print('Original #V : {} #F : {}'.format(v.shape[0], f.shape[0])) print('Loop #V : {} #F : {}'.format(v_loop.shape[0], f_loop.shape[0])) print('BFly #V : {} #F : {}'.format(v_butterfly.shape[0], f_butterfly.shape[0]))
def test_polydata_subdivision_butterfly(self): poly_out = wavefront.polydata_subdivide(self.poly_ellip, 1, 'butterfly') v, f = wavefront.polydatatomesh(poly_out) np.testing.assert_allclose(v.shape[0], 1146) np.testing.assert_allclose(f.shape[0], self.f.shape[0] * 4)
class TestRayCaster(): # load a polyhedron v, f = wavefront.read_obj('./data/shape_model/ITOKAWA/itokawa_low.obj') # define the raycaster object caster = raycaster.RayCaster.loadmesh(v, f, flag='obb', scale=1.0) # test methods of caster V, F = wavefront.polydatatomesh(caster.polydata) bounds = caster.polydata.GetBounds() def test_vertices_equal(self): np.testing.assert_allclose(self.V, self.v) def test_faces_equal(self): np.testing.assert_allclose(self.F, self.f) def test_bounds_x(self): xmin, xmax = self.bounds[0:2] np.testing.assert_allclose((xmin, xmax), (min(self.v[:,0]), max(self.v[:, 0]))) def test_bounds_y(self): ymin, ymax = self.bounds[2:4] np.testing.assert_allclose((ymin, ymax), (min(self.v[:,1]), max(self.v[:, 1]))) def test_bounds_z(self): zmin, zmax = self.bounds[4:6] np.testing.assert_allclose((zmin, zmax), (min(self.v[:,2]), max(self.v[:, 2]))) def test_scale_factor(self): scale = 1e3 caster = raycaster.RayCaster.loadmesh(self.v, self.f, scale=scale) xmin, xmax = caster.polydata.GetBounds()[0:2] np.testing.assert_allclose((xmin, xmax), scale * np.array([min(self.v[:, 0]), max(self.v[:, 0])])) def test_castray_number_intersections(self): intersections = self.caster.castray([5, 0, 0], [-5, 0, 0], all_out=True) np.testing.assert_allclose(intersections.shape, (2,3)) def test_castray_no_intersections(self): intersections = self.caster.castray([5, 0, 0], [6, 0, 0]) np.testing.assert_allclose(intersections.shape, (0,)) def test_castray_coordinates(self): intersections = self.caster.castray([5, 0, 0], [-5, 0, 0], all_out=True) np.testing.assert_allclose(intersections[0, :], np.array([0.29648888, 0, 0])) np.testing.assert_allclose(intersections[1, :], np.array([-0.2460786, 0, 0])) def test_ispointinside_yes(self): point = np.array([0,0,0]) np.testing.assert_allclose(self.caster.ispointinside(point), True) def test_ispointinside_no(self): point = np.array([5,5,5]) np.testing.assert_allclose(self.caster.ispointinside(point),False) def test_distance(self): pa = np.array([5, 0, 0]) pb = np.array([0, 0, 0]) np.testing.assert_allclose(self.caster.distance(pa, pb), 5) def test_source_inside_body(self): psource = np.array([0, 0, 0]) ptarget = np.array([5, 0, 0]) intersections = self.caster.castray(psource, ptarget) np.testing.assert_allclose(intersections.shape, (3))