def _get_cell_points(self, cell, points): cell_points = tvtk.Points() for idx in cell: vertex = points[idx] cell_points.insert_next_point(vertex) return cell_points
def test_points_lookup(self): """ Test if points can be looked up. """ points = tvtk.Points() points.insert_next_point((0, 1, 2)) pt = points[0] self.assertEqual(pt, (0, 1, 2)) get_pt = points.get_point(0) self.assertEqual(get_pt, (0, 1, 2))
def test_points_lookup(self): """ Test if points can be looked up with both int and long keys. Fixes GH Issue 173. """ points = tvtk.Points() points.insert_next_point((0, 1, 2)) pt = points[0] self.assertEqual(pt, (0, 1, 2)) ptl = points[0L] self.assertEqual(ptl, (0, 1, 2)) get_pt = points.get_point(0) self.assertEqual(get_pt, (0, 1, 2)) get_ptl = points.get_point(0L) self.assertEqual(get_ptl, (0, 1, 2))
def _configure(self, points): self.vtk_points = tvtk.Points() self.lines = tvtk.CellArray() for point in points: self.vtk_points.insert_next_point(self._to_float_tuple(point)) for i in xrange(len(points)-1): line = tvtk.Line() line.point_ids.set_id(0, i) line.point_ids.set_id(1, i+1) self.lines.insert_next_cell(line) self.poly_data = tvtk.PolyData(points=self.vtk_points, lines=self.lines) self._set_actor()
def intersect_line(self, lineP0, lineP1): ''' 计算与线段相交的交点,这里调用了tvtk的方法 lineP0: 线段的第一个点,长度3的数组 lineP1: 线段的第二个点 return points: 所有的交点坐标 cell_ids: 每个交点所属的面片ID ''' intersectPoints = tvtk.to_vtk(tvtk.Points()) intersectCells = tvtk.to_vtk(tvtk.IdList()) self._obb_tree.intersect_with_line(lineP0, lineP1, intersectPoints, intersectCells) intersectPoints = tvtk.to_tvtk(intersectPoints) intersectCells = tvtk.to_tvtk(intersectCells) points = intersectPoints.to_array() cell_ids = [ intersectCells.get_id(i) for i in range(intersectCells.number_of_ids) ] return points, cell_ids
def get_trunkcone(b, a): phi_base, theta_base = sph.to(a, b)[1:] quads = tvtk.CellArray() #vtk.vtkCellArray() points = tvtk.Points() #vtk.vtkPoints() Nface = 3 for i in range(Nface + 1): # rotate phi, theta = convdir((i % Nface) * 2 * pi / Nface, pi * 0.5, phi_base, theta_base) # generate new points p = tuple(sph.xyz(a[3] * 0.5 * radius_factor, phi, theta, a[:3])) q = tuple(sph.xyz(b[3] * 0.5 * radius_factor, phi, theta, b[:3])) # insert points points.append(p) points.append(q) if i >= 1: # create a face quad = tvtk.Quad() n = points.number_of_points - 1 quad.point_ids.set_id(0, n - 3) # p quad.point_ids.set_id(1, n - 2) # q quad.point_ids.set_id(2, n) # q quad.point_ids.set_id(3, n - 1) # p # insert the new face quads.insert_next_cell(quad) # create the actor polydata = tvtk.PolyData(points=points, polys=quads) if new_tvtk: mapper = tvtk.PolyDataMapper() configure_input(mapper, polydata) else: mapper = tvtk.PolyDataMapper(input=polydata) actor = tvtk.Actor(mapper=mapper) fig.scene.add_actor(actor) return actor
def vtkCone(p, q): from math import pi phi_base, theta_base = misc.Spherical.to(q, p)[1:] quads = tvtk.CellArray() #vtk.vtkCellArray() points = tvtk.Points() #vtk.vtkPoints() for i in range(11): # rotate phi, theta = ConvertDirection((i % 10) * 2 * pi / 10, pi * .5, phi_base, theta_base, True) # generate new points _p = tuple( misc.Spherical.xyz(p[3] * .5 * cone_factor, phi, theta, p[0:3])) _q = tuple( misc.Spherical.xyz(q[3] * .5 * cone_factor, phi, theta, q[0:3])) # insert points points.append(_p) points.append(_q) if i >= 1: # create a face quad = tvtk.Quad() n = points.number_of_points - 1 quad.point_ids.set_id(0, n - 3) # p quad.point_ids.set_id(1, n - 2) # q quad.point_ids.set_id(2, n) # q quad.point_ids.set_id(3, n - 1) # p # insert the new face quads.insert_next_cell(quad) # create the actor polydata = tvtk.PolyData(points=points, polys=quads) mapper = tvtk.PolyDataMapper(input=polydata) actor = tvtk.Actor(mapper=mapper) return actor
def create_labels(img, render): liste = list(img.labels()) xmin = 0 xLength = 1000 xmax = xmin + xLength ymin = 00 yLength = 1000 ymax = ymin + yLength # Create labels for barycenters m = tvtk.PolyData() vertex = tvtk.Points() provi1 = tvtk.LongArray() p = 0 cell_barycentre = img.center_of_mass() x, y, z = img.image.resolution for c, toto in enumerate(liste): vertex.insert_point(p, cell_barycentre[c][0] / x, cell_barycentre[c][1] / y, cell_barycentre[c][2] / z) provi1.insert_value(p, toto) p += 1 m.points = vertex m.point_data.scalars = provi1 vtkLabels = m #here the idea is to mask labels behind surfaces visPts = tvtk.SelectVisiblePoints() visPts.input = vtkLabels visPts.renderer = render visPts.selection_window = 1 visPts.selection = (xmin, xmin + xLength, ymin, ymin + yLength) # Create the mapper to display the point ids. Specify the format to # use for the labels. Also create the associated actor. ldm = tvtk.LabeledDataMapper() # ldm.SetLabelFormat("%g") ldm.input = visPts.output ldm.label_mode = 'label_scalars' vtk_labels = tvtk.Actor2D() vtk_labels.mapper = ldm return vtk_labels
def main(): xo = yo = zo = 0.0 while True: a, b, c = state.uniform(size=3) xo = bounds[0] + (bounds[1] - bounds[0]) * a yo = bounds[2] + (bounds[3] - bounds[2]) * b zo = bounds[4] + (bounds[5] - bounds[4]) * c r = pow(250 - xo, 2) + pow(250 - yo, 2) print "coordinate: {},{},{}, {}".format(xo, yo, zo, r) # is_inside_surface ==1 when point is INSIDE enclosed surface if r < rad_om2 and not enclosed.is_inside_surface(xo, yo, zo): break idlist = tvtk.IdList() points = tvtk.Points() for pid in range(100001): while True: xr, yr, zr = eps * (state.uniform(size=3) - 0.5) r = pow(250 - (xo + xr), 2) + pow(250 - (yo + yr), 2) if (r < rad_om2 and not enclosed.is_inside_surface(xo+xr, yo+yr, zo+zr)): break (xo, yo, zo) = (xo + xr, yo + yr, zo + zr) idlist.insert_next_id(pid) points.insert_next_point((xo, yo, zo)) points.update_traits() # write out path trace to vtk every 5000 timesteps if pid % 5000 == 0: _array = tvtk.CellArray() rand_walk = tvtk.PolyData(points=points, lines=_array) rand_walk.insert_next_cell(4, idlist) # VTK_POLY_LINE == 4 writer = tvtk.PolyDataWriter(file_name='run_{}.vtk'.format(pid), input=rand_walk) writer.update()
def test_points(self): """Test if vtkPoints behaves in a Pythonic fashion.""" f = tvtk.Points() a = numpy.array([[0., 0, 0], [1, 1, 1]]) f.from_array(a) self.assertEqual(mysum(f.to_array() - a), 0) for i, j in zip(a, f): self.assertEqual(mysum(i - j), 0) a[0, 0] = 1.0 # Should change the VTK data! # Make sure that everything works even when the original array # is deleted. del a self.assertEqual(f[0], (1.0, 0.0, 0.0)) self.assertEqual(f[-1], (1., 1., 1.)) self.assertEqual(repr(f), '[(1.0, 0.0, 0.0), (1.0, 1.0, 1.0)]') f.append((2, 2, 2)) f.extend([[3, 3, 3], [4, 4, 4]]) self.assertEqual(len(f), 5) f[1] = [-1, -1, -1] self.assertEqual(f[1], (-1.0, -1.0, -1.0)) self.assertRaises(IndexError, f.__getitem__, 100) self.assertRaises(IndexError, f.__setitem__, 100, 100)
def trace_segment(self, seg, last_optic=None, last_cell=None): """ Finds the intersection of the given ray-segment with the object geometry data """ p1 = seg.origin p2 = p1 + seg.MAX_RAY_LENGTH*seg.direction pts = tvtk.Points() ids = tvtk.IdList() ret = self.obb.intersect_with_line(p1, p2, pts, ids) sqrt = numpy.sqrt array = numpy.array if ret==0: return None if self is not last_optic: last_cell = None data = [ (sqrt(((array(p) - p1)**2).sum()), p, Id) for p, Id in zip(pts, ids) if Id is not last_cell ] if not data: return None short = min(data, key=lambda a: a[0]) return short[0], short[1], short[2], self
self.initialize() # self.start() def add_actor(self, actor): self.renderer.add_actor(actor) # update window here. ### DATA points = array([[0, 0, 0, 10], [1, 0, 0, 20], [0, 1, 0, 20], [0, 0, 1, 30]]) triangles = array([[0, 1, 3], [0, 3, 2], [1, 2, 3], [0, 2, 1]]) ### PIPELINE # Convert list of points to VTK structure verts = tvtk.Points() temperature = tvtk.FloatArray() for p in points: verts.insert_next_point(p[0], p[1], p[2]) temperature.insert_next_value(p[3]) # Define triangular cells from the vertex # "ids" (index) and append to polygon list. polygons = tvtk.CellArray() for tri in triangles: cell = tvtk.IdList() cell.insert_next_id(tri[0]) cell.insert_next_id(tri[1]) cell.insert_next_id(tri[2]) polygons.insert_next_cell(cell)
def transformPoints(t, pts): """Apply a vtkTransform to a numpy array of points""" out = tvtk.Points() t.transform_points(pts, out) return numpy.asarray(out)
def _parse_OFF(cls, filename): points = tvtk.Points() faces = tvtk.CellArray() n_read = False vertices_read = faces_read = 0 fp_regexp = '(-?)(0[\.\d*]?|([1-9]\d*\.?\d*)|(\.\d+))([Ee][+-]?\d+)?' with open(filename, 'r') as _file: lines = _file.readlines() for i, line in enumerate(lines): line = line.strip() if i == 0 and line != 'OFF': raise Exception('Wrong format!') elif i == 0: continue if not line or line[0] == '#': continue if n_read is False: match = re.match('(\d+)\s+(\d+)\s+(\d+).*', line) if match is None: raise Exception('Wrong format!') n_verts = int(match.groups()[0]) n_faces = int(match.groups()[1]) n_read = True continue if vertices_read < n_verts: match = re.findall(fp_regexp, line) if not match: raise Exception('Wrong format!') x = float(match[0][1]) if match[0][0] == '-': x = -x y = float(match[1][1]) if match[1][0] == '-': y = -y z = float(match[2][1]) if match[2][0] == '-': z = -z points.insert_next_point((x, y, z)) vertices_read += 1 continue if faces_read < n_faces: # TODO: add support for non-triangular faces. match = re.match( '(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s*(\d+)?.*', line) if match is None: raise Exception('Wrong format!') f_verts = int(match.groups()[0]) if f_verts not in [3, 4]: raise Exception( 'Faces should have three or four vertices!') vert0 = int(match.groups()[1]) vert1 = int(match.groups()[2]) vert2 = int(match.groups()[3]) polygon = tvtk.Polygon() polygon.point_ids.number_of_ids = f_verts polygon.point_ids.set_id(0, vert0) polygon.point_ids.set_id(1, vert1) polygon.point_ids.set_id(2, vert2) if f_verts == 4: vert3 = int(match.groups()[4]) polygon.point_ids.set_id(3, vert3) faces.insert_next_cell(polygon) faces_read += 1 continue return faces, points