我在同一条线上创建了两个管,我的下一步是在两个管之间设置区域的颜色,但我不知道该怎么做。有帮助吗?先感谢您!我的代码如下所示。
#include <vtkSmartPointer.h>
#include <vtkVersion.h>
#include <vtkParametricFunctionSource.h>
#include <vtkTupleInterpolator.h>
#include <vtkTubeFilter.h>
#include <vtkParametricSpline.h>
#include <vtkDoubleArray.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPointData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
vtkSmartPointer<vtkActor> createTube(double rad1,double rad2,double rad3,double rad4,double rad5,double rad6)
{
vtkSmartPointer<vtkPoints> points =
vtkSmartPointer<vtkPoints>::New();
points->InsertPoint(0,1,0,0);
points->InsertPoint(1,2,0.3,0);
points->InsertPoint(2,3,0.1,0);
points->InsertPoint(3,4,0.2,0);
points->InsertPoint(4,5,0.5,0);
points->InsertPoint(5,6,0.4,0);
// Fit a spline to the points
vtkSmartPointer<vtkParametricSpline> spline =
vtkSmartPointer<vtkParametricSpline>::New();
spline->SetPoints(points);
vtkSmartPointer<vtkParametricFunctionSource> functionSource =
vtkSmartPointer<vtkParametricFunctionSource>::New();
functionSource->SetParametricFunction(spline);
functionSource->SetUResolution(10 * points->GetNumberOfPoints());
functionSource->Update();
// Interpolate the scalars
//double rad;
vtkSmartPointer<vtkTupleInterpolator> interpolatedRadius =
vtkSmartPointer<vtkTupleInterpolator> ::New();
interpolatedRadius->SetInterpolationTypeToLinear();
interpolatedRadius->SetNumberOfComponents(1);
interpolatedRadius->AddTuple(0,&rad1);
interpolatedRadius->AddTuple(1,&rad2);
interpolatedRadius->AddTuple(2,&rad3);
interpolatedRadius->AddTuple(3,&rad4);
interpolatedRadius->AddTuple(4,&rad5);
interpolatedRadius->AddTuple(5,&rad6);
// Generate the radius scalars
vtkSmartPointer<vtkDoubleArray> tubeRadius =
vtkSmartPointer<vtkDoubleArray>::New();
unsigned int n = functionSource->GetOutput()->GetNumberOfPoints();
tubeRadius->SetNumberOfTuples(n);
tubeRadius->SetName("TubeRadius");
double tMin = interpolatedRadius->GetMinimumT();
double tMax = interpolatedRadius->GetMaximumT();
double r;
for (unsigned int i = 0; i < n; ++i)
{
double t = (tMax - tMin) / (n - 1) * i + tMin;
interpolatedRadius->InterpolateTuple(t, &r);
tubeRadius->SetTuple1(i, r);
}
// Add the scalars to the polydata
vtkSmartPointer<vtkPolyData> tubePolyData =
vtkSmartPointer<vtkPolyData>::New();
tubePolyData = functionSource->GetOutput();
tubePolyData->GetPointData()->AddArray(tubeRadius);
tubePolyData->GetPointData()->SetActiveScalars("TubeRadius");
// Create the tubes
vtkSmartPointer<vtkTubeFilter> tuber =
vtkSmartPointer<vtkTubeFilter>::New();
tuber->SetInput(tubePolyData);
tuber->SetNumberOfSides(20);
tuber->SetVaryRadiusToVaryRadiusByAbsoluteScalar();
vtkSmartPointer<vtkPolyDataMapper> tubeMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
tubeMapper->SetInputConnection(tuber->GetOutputPort());
tubeMapper->SetScalarRange(tubePolyData->GetScalarRange());
//vtkSmartPointer<vtkActor> lineActor = vtkSmartPointer<vtkActor>::New();
//lineActor->SetMapper(lineMapper);
vtkSmartPointer<vtkActor> tubeActor = vtkSmartPointer<vtkActor>::New();
tubeActor->SetMapper(tubeMapper);
return tubeActor;
}
int main()
{
vtkSmartPointer<vtkActor> tubeActor_1 = vtkSmartPointer<vtkActor>::New();
tubeActor_1= createTube(0.15,0.14,0.13,0.12,0.11,0.10);
vtkSmartPointer<vtkActor> tubeActor_2 = vtkSmartPointer<vtkActor>::New();
tubeActor_2= createTube(0.5,0.155,0.145,0.145,0.125,0.115);
//tubeActor_2->GetProperty()->SetOpacity(0.25);
// Setup render window, renderer, and interactor
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(tubeActor_1);
renderer->AddActor(tubeActor_2);
renderer->SetBackground(1, 1, 1);
renderWindow->Render();
renderWindowInteractor->Start();
return 0;
}