def test_idlist(self): """Test if vtkIdList behaves in a Pythonic fashion.""" f = tvtk.IdList() a = numpy.array([0, 1, 2, 3]) f.from_array(a) for i, j in zip(a, f): self.assertEqual(i, j) self.assertEqual(f[-1], 3) self.assertEqual(f[0], 0) self.assertEqual(repr(f), '[0, 1, 2, 3]') f.append(4) f.extend([5, 6]) self.assertEqual(len(f), 7) f[1] = -1 self.assertEqual(f[1], -1) self.assertRaises(IndexError, f.__getitem__, 100) self.assertRaises(IndexError, f.__setitem__, 100, 100)
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 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 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
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) # Create a mesh from these lists mesh = tvtk.PolyData() mesh.points = verts mesh.polys = polygons mesh.point_data.scalars = temperature # Set the mapper to scale temperature range # across the entire range of colors mapper = tvtk.PolyDataMapper() mapper.input = mesh