vtkTubeFilter实例

时间:2023-03-09 23:37:36
vtkTubeFilter实例

filter that generates tubes around lines

vtkTubeFilter is a filter that generates a tube around each input line. The tubes are made up of triangle strips and rotate around the tube with the rotation of the line normals. (If no normals are present, they are computed automatically.) The radius of the tube can be set to vary with scalar or vector value. If the radius varies with scalar value the radius is linearly adjusted. If the radius varies with vector value, a mass flux preserving variation is used. The number of sides for the tube also can be specified. You can also specify which of the sides are visible. This is useful for generating interesting striping effects. Other options include the ability to cap the tube and generate texture coordinates. Texture coordinates can be used with an associated texture map to create interesting effects such as marking the tube with stripes corresponding to length or time.

This filter is typically used to create thick or dramatic lines. Another common use is to combine this filter with vtkStreamLine to generate streamtubes.

Warning:
The number of tube sides must be greater than 3. If you wish to use fewer sides (i.e., a ribbon), use vtkRibbonFilter.
The input line must not have duplicate points, or normals at points that are parallel to the incoming/outgoing line segments. (Duplicate points can be removed with vtkCleanPolyData.) If a line does not meet this criteria, then that line is not tubed.
See also:
vtkRibbonFilter vtkStreamLine

在本例中,先创建一个螺旋线,然后用vtkTubeFilter使线的半径随着螺旋放生变化。

vtkTubeFilter实例

 #ifndef INITIAL_OPENGL
#define INITIAL_OPENGL
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL)
VTK_MODULE_INIT(vtkInteractionStyle)
#endif
#include <iostream>
using namespace std;
// VTK: Spiral with vtkTubeFilter
// Varying tube radius and independent RGB colors with an unsignedCharArray
// Contributed by Marcus Thamson #include <vtkPolyData.h>
#include <vtkPoints.h>
#include <vtkCellArray.h>
#include <vtkDoubleArray.h>
#include <vtkPolyData.h>
#include <vtkPointData.h> #include <vtkCell.h>
#include <vtkCellData.h>
#include <vtkDataSet.h>
#include <vtkDataSetAttributes.h>
#include <vtkProperty.h>
#include <vtkSmartPointer.h>
#include <vtkTubeFilter.h> #include <vtkDataSetMapper.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkCamera.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkMath.h> int main()
{
//螺旋管道;
double vx,vy,vz;
unsigned int nv=;//vertices的个数
unsigned int nCyc=;//螺旋的旋转周数
double rT1=0.1,rT2=0.5;//管道的起点和终点半径
double rs=; //螺旋半径
double h=; //高度
unsigned int nTv=;//管道上的每个vertex,曲面数量
unsigned int i; //创建用于螺旋管的points和cells
vtkSmartPointer<vtkPoints>points=vtkSmartPointer<vtkPoints>::New();
double pi=vtkMath::Pi();
for(i=;i<nv;i++)
{
//螺旋坐标
vx=rs*cos(*pi*nCyc*i/(nv-));
vy=rs*sin(*pi*nCyc*i/(nv-));
vz=h*i/nv;
points->InsertPoint(i,vx,vy,vz);
}
vtkSmartPointer<vtkCellArray> lines=vtkSmartPointer<vtkCellArray>::New();
lines->InsertNextCell(nv);
for(i=;i<nv;i++)
{
lines->InsertCellPoint(i);
}
vtkSmartPointer<vtkPolyData>polyData=vtkSmartPointer<vtkPolyData>::New();
polyData->SetPoints(points);
polyData->SetLines(lines); //半径随着正弦函数曲线变化
vtkSmartPointer<vtkDoubleArray> tubeRadius=vtkSmartPointer<vtkDoubleArray>::New();
tubeRadius->SetName("TubeRadius");//
tubeRadius->SetNumberOfTuples(nv);
for(i=;i<nv;i++)
{
tubeRadius->SetTuple1(i,
rT1+(rT2-rT1)*sin(pi*i/(nv-)));
}
polyData->GetPointData()->AddArray(tubeRadius);
polyData->GetPointData()->SetActiveScalars("TubeRadius");
//RBG 数组(也许也可以添加Alpha通道)
//颜色从蓝-->到红
vtkSmartPointer<vtkUnsignedCharArray>colors=vtkSmartPointer<vtkUnsignedCharArray>::New();
colors->SetName("Colors");//为该数组起名为"Colors"
colors->SetNumberOfComponents();
colors->SetNumberOfTuples(nv);
for(i=;i<nv;i++)
{
colors->InsertTuple3(i,
int(*i/(nv-)),
,
int(*(nv--i)/(nv-)));
}
polyData->GetPointData()->AddArray(colors);//添加颜色属性标量scalar
//创建未经vtkTubeFilter处理的螺旋线Actor vtkSmartPointer<vtkPolyDataMapper>mapperSpiral=vtkSmartPointer<vtkPolyDataMapper>::New();
mapperSpiral->SetInputData(polyData);
mapperSpiral->ScalarVisibilityOn();
mapperSpiral->SetScalarModeToUsePointFieldData();//使用FieldData为对象上色
mapperSpiral->SelectColorArray("Colors");
vtkSmartPointer<vtkActor> actorSpiral=vtkSmartPointer<vtkActor>::New();
actorSpiral->SetMapper(mapperSpiral); //管道筛选器
vtkSmartPointer<vtkTubeFilter> tube=vtkSmartPointer<vtkTubeFilter>::New();
tube->SetInputData(polyData);
tube->SetNumberOfSides(nTv);
tube->SetVaryRadiusToVaryRadiusByAbsoluteScalar(); vtkSmartPointer<vtkPolyDataMapper>mapper=vtkSmartPointer<vtkPolyDataMapper>::New(); mapper->SetInputConnection(tube->GetOutputPort()); mapper->ScalarVisibilityOn();
mapper->SetScalarModeToUsePointFieldData();//使用FieldData为对象上色
mapper->SelectColorArray("Colors"); vtkSmartPointer<vtkActor> actor=vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer>renderer=vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
// actorSpiral->SetPosition(5,0,0);
actorSpiral->AddPosition(,,);
renderer->AddActor(actorSpiral);//添加未经vtkTubeFilter处理的螺旋线Actor renderer->SetBackground(0.2,0.3,0.4);
//设定一个倾斜的视角
renderer->GetActiveCamera()->Azimuth();
renderer->GetActiveCamera()->Elevation();
renderer->ResetCamera(); vtkSmartPointer<vtkRenderWindow>renWin=vtkSmartPointer<vtkRenderWindow>::New();
vtkSmartPointer<vtkRenderWindowInteractor>iren=vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleTrackballCamera>style=vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New(); renWin->AddRenderer(renderer);
renWin->SetSize(,);
renWin->Render();
iren->SetRenderWindow(renWin);
iren->SetInteractorStyle(style);
iren->Start();
return ;
}