def linesBody(mb, bodyName, successorJointsName): """ Return a mesh represented by lines and the appropriate static transform. """ apd = tvtk.AppendPolyData() sources = [] # create a line from the body base to the next joint for s in map(mb.jointIndexByName, successorJointsName[bodyName]): X_s = mb.transform(s) sources.append(tvtk.LineSource(point1=(0., 0., 0.), point2=tuple(X_s.translation()))) # add an empty source to avoid a warning if AppendPolyData have 0 source if len(sources) == 0: sources.append(tvtk.PointSource(radius=0.)) map(lambda s: apd.add_input(s.output), sources) apd.update() pdm = tvtk.PolyDataMapper() pdm.input_connection = apd.output_port actor = tvtk.Actor(mapper=pdm) actor.property.color = (0., 0., 0.) actor.user_transform = tvtk.Transform() return actor, sva.PTransformd.Identity()
def endEffectorBody(X_s, size, color): """ Return a end effector reprsented by a plane and the appropriate static transform. """ apd = tvtk.AppendPolyData() ls = tvtk.LineSource(point1=(0., 0., 0.), point2=tuple(X_s.translation())) p1 = (sva.PTransformd(e.Vector3d.UnitX() * size) * X_s).translation() p2 = (sva.PTransformd(e.Vector3d.UnitY() * size) * X_s).translation() ps = tvtk.PlaneSource(origin=tuple(X_s.translation()), point1=tuple(p1), point2=tuple(p2), center=tuple(X_s.translation())) apd.add_input(ls.output) apd.add_input(ps.output) pdm = tvtk.PolyDataMapper(input=apd.output) actor = tvtk.Actor(mapper=pdm) actor.property.color = color actor.user_transform = tvtk.Transform() return actor, sva.PTransformd.Identity()
def test_property_change_notification(self): """Test if changes to properties generate notification events.""" # Create a dummy class to test with. class Junk: def f(self, obj, name, old, new): self.data = obj, name, old, new z = Junk() cs = tvtk.ConeSource() m = tvtk.PolyDataMapper() if vtk_major_version < 6: m.on_trait_change(z.f, 'input') m.input = cs.output self.assertEqual(z.data, (m, 'input', None, cs.output)) m.input = None self.assertEqual(z.data, (m, 'input', cs.output, None)) m.on_trait_change(z.f, 'input', remove=True) m.input = cs.output else: m.on_trait_change(z.f, 'input_connection') m.input_connection = cs.output_port self.assertEqual(z.data, (m, 'input_connection', None, cs.output_port)) m.input_connection = None self.assertEqual(z.data, (m, 'input_connection', cs.output_port, None)) m.on_trait_change(z.f, 'input_connection', remove=True) m.input_connection = cs.output_port a = tvtk.Actor() a.on_trait_change(z.f, 'mapper') a.on_trait_change(z.f, 'property') a.mapper = m self.assertEqual(z.data, (a, 'mapper', None, m)) old = a.property new = tvtk.Property() a.property = new self.assertEqual(z.data, (a, 'property', old, new)) # Check if property notification occurs on add_input/remove_input a = tvtk.AppendPolyData() pd = tvtk.PolyData() if vtk_major_version < 6: a.on_trait_change(z.f, 'input') a.add_input(pd) old, new = None, pd self.assertEqual(z.data, (a, 'input', old, new)) a.remove_input(pd) old, new = pd, None self.assertEqual(z.data, (a, 'input', old, new)) a.remove_all_inputs() old, new = None, None self.assertEqual(z.data, (a, 'input', old, new)) else: a.add_input_data(pd) self.assertEqual(a.input, pd) a.remove_input_data(pd) self.assertEqual(a.input, None) a.remove_all_inputs() self.assertEqual(a.input, None)
def _pipeline_default(self): sqrt2 = numpy.sqrt(2) x = -sqrt2 * numpy.cos(numpy.pi * 2. / 3) y = sqrt2 * numpy.sin(numpy.pi * 2. / 3) p1 = tvtk.PlaneSource(origin=(0,0,0), \ point1=(-sqrt2,0,1), point2=(x,y,1)) p2 = tvtk.PlaneSource(origin=(0,0,0), \ point1=(x,y,1), point2=(x,-y,1)) p3 = tvtk.PlaneSource(origin=(0,0,0), \ point1=(x,-y,1), point2=(-sqrt2,0,1)) for p in (p1, p2, p3): p.set_resolution(32, 32) append = tvtk.AppendPolyData() append.add_input_connection(p1.output_port) append.add_input_connection(p2.output_port) append.add_input_connection(p3.output_port) scale_f = tvtk.TransformFilter(input_connection=append.output_port, transform=self.scale) trans = tvtk.Transform() trans.rotate_x(90.0) self.imp_cyl.transform = trans clip = tvtk.ClipPolyData(input_connection=scale_f.output_port, clip_function=self.imp_cyl, inside_out=True) transF2 = tvtk.TransformFilter(input_connection=clip.output_port, transform=self.transform) self.config_pipeline() return transF2
def _pipeline_default(self): cyl = self.vtk_disk norms = tvtk.PolyDataNormals(input_connection=cyl.output_port) tube = tvtk.TubeFilter(input_connection=self.line.output_port) append = tvtk.AppendPolyData() append.add_input_connection(norms.output_port) append.add_input_connection(tube.output_port) transF1 = tvtk.TransformFilter(input_connection=append.output_port, transform=self.cyl_trans) transF2 = tvtk.TransformFilter(input_connection=transF1.output_port, transform=self.transform) self.config_pipeline() return transF2
def _pipeline_default(self): grid = self.vtk_grid #grid.set_execute_method(self.create_grid) grid.modified() trans = tvtk.Transform() trans.rotate_x(90.) cyl = self.vtk_cylinder cyl.transform = trans clip1 = tvtk.ClipVolume(input_connection=grid.output_port, clip_function=self.vtk_cylinder, inside_out=1) self.clip2.set(input_connection=clip1.output_port, clip_function=self.vtk_sphere1, inside_out=1) self.clip3.set(input_connection=self.clip2.output_port, clip_function=self.vtk_sphere3, inside_out=1) clip4 = tvtk.ClipDataSet(input_connection=self.clip3.output_port, clip_function=self.vtk_sphere2, inside_out=1) clip5 = tvtk.ClipDataSet(input_connection=self.clip3.output_port, clip_function=self.vtk_sphere2, inside_out=0) topoly = tvtk.GeometryFilter(input_connection=clip4.output_port) topoly2 = tvtk.GeometryFilter(input_connection=clip5.output_port) append = tvtk.AppendPolyData() append.add_input_connection(topoly.output_port) append.add_input_connection(topoly2.output_port) norms = tvtk.PolyDataNormals(input_connection=append.output_port) transF = tvtk.TransformFilter(input_connection=norms.output_port, transform=self.transform) self.on_geometry_changed() self.config_pipeline() grid.modified() return transF
def endEffectorBody(X_s, size, color): """ Return a end effector reprsented by a plane and the appropriate static transform. """ apd = tvtk.AppendPolyData() ls = tvtk.LineSource(point1=(0., 0., 0.), point2=tuple(X_s.translation())) p1 = (sva.PTransformd(e.Vector3d.UnitX()*size)*X_s).translation() p2 = (sva.PTransformd(e.Vector3d.UnitY()*size)*X_s).translation() ps = tvtk.PlaneSource(origin=tuple(X_s.translation()), point1=tuple(p1), point2=tuple(p2), center=tuple(X_s.translation())) # apd.add_input(ls.output) # apd.add_input(ps.output) # https://github.com/enthought/mayavi/blob/ac5c8e316335078c25461a0bce4a724ae86f1836/tvtk/tests/test_tvtk.py#L586 apd.add_input_data(ls.output) apd.add_input_data(ps.output) # pdm = tvtk.PolyDataMapper(input=apd.output) # arcPdm = tvtk.PolyDataMapper(input=arcSource.output) pdm = tvtk.PolyDataMapper() # https://stackoverflow.com/questions/35089379/how-to-fix-traiterror-the-input-trait-of-a-instance-is-read-only # configure_input_data(textPdm, textSource)# https://github.com/enthought/mayavi/issues/521 pdm.input_connection = apd.output_port prop = tvtk.Property(color=color) # https://github.com/enthought/mayavi/issues/521 # arcPdm.input_connection = arcSource.output_port actor = tvtk.Actor(mapper=pdm, property=prop) actor.property.color = color actor.user_transform = tvtk.Transform() return actor, sva.PTransformd.Identity()
def test_append_poly_data_input(self): """Test if AppendPolyData has its get_input wrapped right.""" a = tvtk.AppendPolyData() self.assertEqual(hasattr(a, 'get_input'), True) self.assertEqual(a.input, None)
def img2polydata_simple(image, dictionnaire=None, verbose=True): """ Convert a |SpatialImage| to a PolyData object with cells surface : Parameters : dictionnaire : cell->scalar dictionary """ labels_provi = list(np.unique(image)) #ici on filtre déjà les listes #~ labels= [i for i in labels_provi if i not in list_remove] labels = labels_provi try: labels.remove(0) except: pass try: labels.remove(1) except: pass #print image.shape xyz = {} if verbose: print "on récupère les bounding box" bbox = nd.find_objects(image) #print labels for label in xrange(2, max(labels) + 1): if not label in labels: continue if verbose: print "% until cells are built", label / float(max(labels)) * 100 slices = bbox[label - 1] label_image = (image[slices] == label) #here we could add a laplacian function to only have the external shape mask = nd.laplace(label_image) label_image[mask != 0] = 0 mask = nd.laplace(label_image) label_image[mask == 0] = 0 # compute the indices of voxel with adequate label a = np.array(label_image.nonzero()).T a += [slices[0].start, slices[1].start, slices[2].start] #print a.shape if a.shape[1] == 4: #print a pass else: xyz[label] = a vx, vy, vz = image.resolution polydata = tvtk.AppendPolyData() polys = {} filtre = [i for i in xyz.keys() if i in dictionnaire.keys()] k = 0.0 for c in filtre: if verbose: print "% until first polydata is built", k / float( len(filtre)) * 100 k += 1. p = xyz[c] p = p.astype(np.float) pd = tvtk.PolyData(points=xyz[c].astype(np.float)) pd.point_data.scalars = [ float(dictionnaire[c]) for i in xrange(len(xyz[c])) ] f = tvtk.VertexGlyphFilter(input=pd) f2 = tvtk.PointDataToCellData(input=f.output) polys[c] = f2.output polydata.add_input(polys[c]) polydata.set_input_array_to_process(0, 0, 0, 0, 0) return polydata
def img2polydata_complexe(image, list_remove=[], sc=None, verbose=False): """ Convert a |SpatialImage| to a PolyData object with cells surface : Parameters : list_remove : a list of cells to be removed from the tissue before putting it on screen sc : if you give a parameter here, it will use it as scalar. you need to give a cell->scalar dictionary """ labels_provi = list(np.unique(image)) #ici on filtre déjà les listes #~ labels= [i for i in labels_provi if i not in list_remove] labels = labels_provi try: labels.remove(0) except: pass try: labels.remove(1) except: pass #print image.shape xyz = {} if verbose: print "on récupère les bounding box" bbox = nd.find_objects(image) #print labels for label in xrange(2, max(labels) + 1): if not label in labels: continue if verbose: print "% until cells are built", label / float(max(labels)) * 100 slices = bbox[label - 1] label_image = (image[slices] == label) #here we could add a laplacian function to only have the external shape mask = nd.laplace(label_image) label_image[mask != 0] = 0 mask = nd.laplace(label_image) label_image[mask == 0] = 0 # compute the indices of voxel with adequate label a = np.array(label_image.nonzero()).T a += [slices[0].start, slices[1].start, slices[2].start] #print a.shape if a.shape[1] == 4: #print a pass else: xyz[label] = a vx, vy, vz = image.resolution polydata = tvtk.AppendPolyData() polys = {} filtre = [i for i in xyz.keys() if i not in list_remove] k = 0.0 for c in filtre: if verbose: print "% until first polydata is built", k / float( len(filtre)) * 100 k += 1. p = xyz[c] p = p.astype(np.float) pd = tvtk.PolyData(points=xyz[c].astype(np.float)) if sc: try: pd.point_data.scalars = [ float(sc[c]) for i in xrange(len(xyz[c])) ] except: pd.point_data.scalars = [float(0) for i in xrange(len(xyz[c]))] else: pd.point_data.scalars = [float(c) for i in xrange(len(xyz[c]))] f = tvtk.VertexGlyphFilter(input=pd) f2 = tvtk.PointDataToCellData(input=f.output) polys[c] = f2.output polydata.add_input(polys[c]) polydata.set_input_array_to_process(0, 0, 0, 0, 0) try: labels_not_in_sc = list(set(list(np.unique(image))) - set(sc)) except TypeError: labels_not_in_sc = [] if 0 in labels_not_in_sc: labels_not_in_sc.remove(0) if 1 in labels_not_in_sc: labels_not_in_sc.remove(1) filtre = [ i for i in xyz.keys() if i in list_remove or i in labels_not_in_sc ] if filtre != []: polydata2 = tvtk.AppendPolyData() polys2 = {} k = 0.0 for c in filtre: if verbose: print "% until second polydata is built", k / float( len(filtre)) * 100 k += 1. p = xyz[c] p = p.astype(np.float) pd = tvtk.PolyData(points=xyz[c].astype(np.float)) pd.point_data.scalars = [0. for i in xrange(len(xyz[c]))] f = tvtk.VertexGlyphFilter(input=pd) f2 = tvtk.PointDataToCellData(input=f.output) polys2[c] = f2.output polydata2.add_input(polys2[c]) else: polydata2 = tvtk.AppendPolyData() polydata2.set_input_array_to_process(0, 0, 0, 0, 0) polys2 = {} pd = tvtk.PolyData() polydata2.add_input(pd) return polydata, polydata2