vtk中的vtkLine、vtkLineSource两个都能起到绘制线的作用。它们的接口说明如下:
vtkLine:
Intersection |
将两条有限的三维线投影到二维平面上。如果两条直线在垂直于这两条直线的叉乘的平面上的投影相交,就会得到一个交点。参数(u,v)是直线在最接近点处的参数坐标。 |
Intersection3D |
求有限三维线的交点。如果这两条直线在垂直于这两条直线的叉乘的平面上的投影相交,并且最近的接近点之间的距离在相对公差范围内,就可以找到交点。参数(u,v)是直线在最接近点处的参数坐标。注意:“Intersection()决定两条直线在平面上的投影是否相交,而Intersection3D()决定直线本身在三维空间中是否相交,且在公差范围内。” |
static double DistanceToLine(const double x[3], const double p1[3], const double p2[3]); |
求前顶点到由提供的顶点定义的边的距离。返回距离的平方。注:线的范围假设为无穷大。 |
LineSource:
vtkLineSource派生于vtkPolyDataAlgorithm,无单独重要接口。
vtkLine:
void ESLine::createLine()
{
auto pts = m_pTrackReader->getPoints();
vtkSmartPointer lineCell = vtkSmartPointer::New();
vtkSmartPointer linePts = vtkSmartPointer::New();
VTKPoint3D pt1, pt2;
int size = pts.size();
for (int index = 0; index < size - 1; ++index)
{
vtkSmartPointer line = vtkSmartPointer::New();
pt1 = pts.at(index);
pt2 = pts.at(index + 1);
vtkIdType id1 = linePts->InsertNextPoint(pt1.m_x, pt1.m_y, pt1.m_z);
vtkIdType id2 = linePts->InsertNextPoint(pt2.m_x, pt2.m_y, pt2.m_z);
line->GetPointIds()->SetId(0, id1);
line->GetPointIds()->SetId(1, id2);
lineCell->InsertNextCell(line);
}
vtkSmartPointer polyData = vtkSmartPointer::New();
polyData->SetPoints(linePts);
polyData->SetLines(lineCell);
vtkSmartPointer mapper = vtkSmartPointer::New();
mapper->SetInputData(polyData);
vtkSmartPointer actor = vtkSmartPointer::New();
actor->SetMapper(mapper);
double* scale = m_vtkView->zoomScale();
actor->SetScale(scale[0], scale[1], scale[2]);
m_pRender->AddActor(actor);
m_pRender->ResetCamera();
m_pRender->ResetCameraClippingRange();
m_pRenderWnd->Render();
}
vtkLineSource:
void ESLine::createLineSource()
{
auto pts = m_pTrackReader->getPoints();
vtkSmartPointer lineSource = vtkSmartPointer::New();
vtkSmartPointer inputPts = vtkSmartPointer::New();
inputPts->SetDataType(VTK_DOUBLE);
//DataArea.IffyArea();
auto pt = pts.front();
int size = pts.size();
for (int index = 0; index < size; ++index)
{
pt = pts.at(index);
inputPts->InsertNextPoint(pt.m_x, pt.m_y, pt.m_z);
}
lineSource->SetPoints(inputPts);
lineSource->Update();
vtkSmartPointer tubeMapper = vtkSmartPointer::New();
tubeMapper->SetInputConnection(/*tubeFilter*/lineSource->GetOutputPort());
vtkSmartPointer actor = vtkSmartPointer::New();
actor->SetMapper(tubeMapper);
double* scale = m_vtkView->zoomScale();
actor->SetScale(scale[0], scale[1], scale[2]);
m_pRender->AddActor(actor);
m_pRender->ResetCamera();
m_pRender->ResetCameraClippingRange();
m_pRenderWnd->Render();
}
vtk中可以用vtkPoints画点和vtkSphereSource画球来表示三维空间中的点。下面重点说明下vtkSphereSource及其接口。
vtkSphereSource创建一个以原点为中心的指定半径的球体(由多边形表示)。可以指定纬度(phi)和经度(θ)方向上的分辨率(多边形离散化)。也可以通过指定最大phi和θ角来创建部分球体。默认情况下,球体的曲面细分使用三角形; 但是,您可以设置LatLongTessellation以使用四边形生成曲面细分。
vtkSphereSource的接口如下:
SetRadius (double) |
设置球体半径。默认值为0.5 |
SetCenter (double, double, double) |
设置球体的中心。默认值为0,0,0。 |
SetThetaResolution (int) |
设置经度方向上的点数(从StartTheta到EndTheta)。 |
SetPhiResolution (int) |
设置纬度方向上的点数(范围从StartPhi到EndPhi)。 |
SetStartTheta (double) |
设置起始经度角。默认情况下,StartTheta = 0度。 |
SetEndTheta (double) |
设置结束经度角。默认情况下,EndTheta = 360度。 |
SetStartPhi (double) |
设置起始纬度角度(0表示北极)。默认情况下,StartPhi = 0度。 |
SetEndPhi (double) |
设置结束纬度角度。默认情况下,EndPhi = 180度。 |
SetLatLongTessellation (vtkTypeBool) |
使球体沿经纬度线的边缘镶嵌在一起。如果关闭,则在非极性区域生成三角形,这将导致不平行于经纬度线的边缘。如果打开,四边形将在除两极以外的所有地方生成。这对于生成具有自然经纬度线的线框球体非常有用。 |
SetOutputPointsPrecision (int) |
设置/获得输出点所需的精度。vtkAlgorithm :: SINGLE_PRECISION - 输出单精度浮点数。vtkAlgorithm :: DOUBLE_PRECISION - 输出双精度浮点数。 |
显示点:
void ESPoint::createPoint(const VTKPoint3D& pt)
{
vtkSmartPointer points = vtkSmartPointer::New();
vtkSmartPointer vertices = vtkSmartPointer::New();
vtkIdType pid[1];
pid[0] = points->InsertNextPoint(pt.m_x, pt.m_y, pt.m_z);
vertices->InsertNextCell(1, pid);
vtkSmartPointer point = vtkSmartPointer::New();
point->SetPoints(points);
point->SetVerts(vertices);
vtkSmartPointer mapper = vtkSmartPointer::New();
mapper->SetInputData(point);
vtkSmartPointer actor = vtkSmartPointer::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(1, 0, 0);
actor->GetProperty()->SetPointSize(10);
double* scale = m_vtkView->zoomScale();
actor->SetScale(scale[0], scale[1], scale[2]);
m_pRender->AddActor(actor);
}
这种方式显示的点,无法在放大缩小时,点跟着放大缩小。
显示球:
void ESPoint::createSphere(const VTKPoint3D& pt)
{
double* scale = m_vtkView->zoomScale();
vtkSmartPointer sphereSource = vtkSmartPointer::New();
sphereSource->SetCenter(pt.m_x * scale[0]/* / 2.0*/, pt.m_y * scale[1], pt.m_z * scale[2]);
sphereSource->SetRadius(100);
sphereSource->SetThetaResolution(40);
sphereSource->SetPhiResolution(40);
sphereSource->Update();
//create a mapper
vtkSmartPointer sphereMapper = vtkSmartPointer::New();
sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
// create an actor
vtkSmartPointer sphereActor = vtkSmartPointer::New();
sphereActor->SetMapper(sphereMapper);
sphereActor->GetProperty()->SetColor(1, 0, 0);
sphereActor->SetScale(1.0, 1, 1/*scale[2]*/);
m_pRender->AddActor(sphereActor);
}
还可以通过SetScale来控制让点绘制成椭球状。