VTK中的points和cell

vtkPoints:SetPoint( vtkIdType id, double x[3] )和InsertPoint( vtkIdType id, double x[3] )可以设置点的vtkIdType(类似ID值)和三维坐标。两个函数的区别在于:InsertPoint先要完成点的范围检查和内存分配工作,索引速度较慢。注:这两个函数的实质是调用SetTuple( const vtkIdType i, const float *tuple )和InsertTuple( const vtkIdType i, const float *tuple )在数组的第i个位置赋值。适用bit/char/data/double/float等所有数组。如 voidFloatArray::SetTuple( const vtkIdType i, const float *tuple ); 参数1位置,参数2实际数据:pcoords->SetTuple( i, pts[i] );

vtkCellArray中设置cell单元,调用InsertNextCell函数逐步添加新的cell,例如函数vtkCellArray::InsertNextCell ( vtkIdType    npts,   vtkIdType *    pts )的第一个参数值标是cell中点的个数,第二个参数指向那些点的坐标数据。(说明:vtkIdType *pts,存储的是所包括点在points中的顺序信息,其个数当然应该和前面的npts一致。这里,2点可以连成一条线,三点可以得到一个面。也就是说:vtk中关于点、线、面的那些信息都是存放在cellarray中,应用时也是直接对cellarray指针进行处理,数据的写入和读取在vtkCellArray类完成。

接下来的工作是定义一个vtkPolyData,得到包括顶点、线、多边形、三角形带在内的几何结构,即三维实体。这里通过函数SetPoints设置点信息,SetPolys设置单元排列(cell array)定义多边形,cell array设置单元排列(cell array)定义线,SetStrips设置单元排列(cell array)定义三角形带strip,SetVerts设置顶点,诸如此类。

#include "stdafx.h"
#include "vtkCellArray.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
#include "vtkIntArray.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
int _tmain(int argc, _TCHAR* argv[])
{
int i;
//创建一个浮点型数组存储"点"
vtkFloatArray *pcoords = vtkFloatArray::New();
//设置维度,点->3
pcoords->SetNumberOfComponents( 3 );
//设置数组个数
pcoords->SetNumberOfTuples( 4 );
//指定每一个数组,具体的点坐标
float pts[4][3] = { { 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 },
       { 1.0, 0.0, 0.0 }, { 1.0, 1.0, 0.0}} ;
for( i=0; i<4; i++ )
{
   //设置数组中的点
   pcoords->SetTuple( i, pts[i] );
}
vtkPoints *points = vtkPoints::New();
//获得四个点
points->SetData( pcoords );
//创建网格数组
vtkCellArray *strips = vtkCellArray::New();
//设置单元由几个点组成
/* strips->InsertNextCell( 4 );
strips->InsertCellPoint( 0 );
strips->InsertCellPoint( 1 );
strips->InsertCellPoint( 2 );
strips->InsertCellPoint( 3 );*/
strips->InsertNextCell( 3 );
strips->InsertCellPoint( 0 );
strips->InsertCellPoint( 1 );
strips->InsertCellPoint( 2 );
//strips->InsertCellPoint( 3 );
//创建整形数组
vtkIntArray *temperature = vtkIntArray::New();
temperature->SetName( "Temperature" );
temperature->InsertNextValue( 60 );
temperature->InsertNextValue( 70 );
temperature->InsertNextValue( 80 );
// temperature->InsertNextValue( 90 );
/* //创建双精度型数组
vtkDoubleArray *vorticity = vtkDoubleArray::New();
vorticity->SetName( "Vorticity" );
vorticity->InsertNextValue( 2.7 );
vorticity->InsertNextValue( 4.1 );
vorticity->InsertNextValue( 5.3 );
vorticity->InsertNextValue( 3.4 );*/
//创建数据集
vtkPolyData *polydata = vtkPolyData::New();
//指定点和网格
polydata->SetPoints( points );
polydata->SetStrips( strips );
//指定标量
polydata->GetPointData()->SetScalars( temperature );
//polydata->GetPointData()->AddArray( vorticity ); 
vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();
mapper->SetInput( polydata );
mapper->SetScalarRange( 0, 40 );
// Create an actor.
vtkActor* actor = vtkActor::New();
actor->SetMapper(mapper);
// Create the rendering objects.
vtkRenderer* ren = vtkRenderer::New();
ren->AddActor(actor);
vtkRenderWindow* renWin = vtkRenderWindow::New();
renWin->AddRenderer(ren);
vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
iren->Initialize();
iren->Start();
return 0;
}

### 使用 VTK 计算直线与 3D 模型的交点 在 VTK 中,可以通过 `vtkCellPicker` 或自定义逻辑来计算直线与 3D 模型之间的交点。具体来说,`vtkCellPicker` 是一种用于拾取场景中单元格的功能类,它可以返回射线与模型表面相交的第一个点坐标以及对应的单元格 ID[^1]。 以下是基于 VTK 的 Python 实现示例代码: ```python import vtk # 创建一个球体作为测试模型 sphereSource = vtk.vtkSphereSource() sphereSource.SetCenter(0, 0, 0) sphereSource.SetRadius(5.0) sphereSource.Update() # 将球体转换为多边形数据 polyData = sphereSource.GetOutput() # 定义一条直线(起点终点) lineStartPoint = [-10, 0, 0] lineEndPoint = [10, 0, 0] # 创建一个 Cell Locator 来加速查找过程 cellLocator = vtk.vtkCellLocator() cellLocator.SetDataSet(polyData) cellLocator.BuildLocator() # 初始化交点信息 tolerance = 1e-6 intersectionPoints = [] # 自定义函数:找到直线与模型的所有交点 def findIntersection(cellLocator, lineStartPoint, lineEndPoint, tolerance): tValue = vtk.mutable(0.0) # 参数 t 值 pos = [0.0, 0.0, 0.0] # 交点位置 pcoords = [0.0, 0.0, 0.0] # 单元格内的参数坐标 subId = vtk.mutable(0) # 子单元索引 cellId = vtk.mutable(-1) # 单元格 ID intersected = cellLocator.IntersectWithLine( lineStartPoint, lineEndPoint, tolerance, tValue, pos, pcoords, subId, cellId ) if intersected: intersectionPoints.append(pos) # 调用函数并获取交点 findIntersection(cellLocator, lineStartPoint, lineEndPoint, tolerance) # 输出结果 print(f"Intersections found: {len(intersectionPoints)}") for i, point in enumerate(intersectionPoints): print(f"Intersection Point {i + 1}: ({point[0]}, {point[1]}, {point[2]})") # 可视化部分(可选) mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(sphereSource.GetOutputPort()) actor = vtk.vtkActor() actor.SetMapper(mapper) renderer = vtk.vtkRenderer() renderWindow = vtk.vtkRenderWindow() renderWindow.AddRenderer(renderer) interactor = vtk.vtkRenderWindowInteractor() interactor.SetRenderWindow(renderWindow) # 添加球体到渲染器 renderer.AddActor(actor) renderer.SetBackground(0.1, 0.2, 0.4) # 显示交点 if len(intersectionPoints) > 0: points = vtk.vtkPoints() vertices = vtk.vtkCellArray() for point in intersectionPoints: pid = points.InsertNextPoint(point) vertices.InsertNextCell(1) vertices.InsertCellPoint(pid) polyVertex = vtk.vtkPolyData() polyVertex.SetPoints(points) polyVertex.SetVerts(vertices) vertexMapper = vtk.vtkPolyDataMapper() vertexMapper.SetInputData(polyVertex) vertexActor = vtk.vtkActor() vertexActor.SetMapper(vertexMapper) vertexActor.GetProperty().SetColor(1.0, 0.0, 0.0) # 设置颜色为红色 renderer.AddActor(vertexActor) renderWindow.Render() interactor.Start() ``` 上述代码实现了以下功能: 1. 创建了一个简单的球体作为测试模型。 2. 定义了一条穿过球体中心的直线。 3. 利用了 `vtkCellLocator` 其成员函数 `IntersectWithLine` 找到了该直线与球体表面的所有交点。 4. (可选)通过可视化工具展示了这些交点及其所在模型。 #### 关键说明 - **`vtkCellLocator`** 提供了高效的几何运算支持,能够快速定位单元格并与之交互[^3]。 - 如果需要处理更复杂的形状或多部件组合,则应确保输入的数据集已正确设置并更新至最新的状态。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值