# TubesFromSplines

vtk-examples/Cxx/VisualizationAlgorithms/TubesFromSplines

### Description¶

This example shows how to interpolate a set of points and generate tubes around the resulting polyline. Scalars associated with the points are also interpolated and used to vary the radius of the tube.

Note

Question

### Code¶

TubesFromSplines.cxx

#include <vtkActor.h>
#include <vtkDoubleArray.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkParametricFunctionSource.h>
#include <vtkParametricSpline.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTubeFilter.h>
#include <vtkTupleInterpolator.h>

int main(int, char*[])
{
vtkNew<vtkPoints> points;
points->InsertPoint(0, 1, 0, 0);
points->InsertPoint(1, 2, 0, 0);
points->InsertPoint(2, 3, 1, 0);
points->InsertPoint(3, 4, 1, 0);
points->InsertPoint(4, 5, 0, 0);
points->InsertPoint(5, 6, 0, 0);

// Fit a spline to the points
vtkNew<vtkParametricSpline> spline;
spline->SetPoints(points);
vtkNew<vtkParametricFunctionSource> functionSource;
functionSource->SetParametricFunction(spline);
functionSource->SetUResolution(10 * points->GetNumberOfPoints());
functionSource->Update();

// Interpolate the scalars

unsigned int n = functionSource->GetOutput()->GetNumberOfPoints();
double r;
for (unsigned int i = 0; i < n; ++i)
{
auto t = (tMax - tMin) / (n - 1) * i + tMin;
}

// Add the scalars to the polydata
auto tubePolyData = functionSource->GetOutput();

// Create the tubes
vtkNew<vtkTubeFilter> tuber;
tuber->SetInputData(tubePolyData);
tuber->SetNumberOfSides(20);

//--------------
// Setup actors and mappers
vtkNew<vtkPolyDataMapper> lineMapper;
lineMapper->SetInputData(tubePolyData);
lineMapper->SetScalarRange(tubePolyData->GetScalarRange());

vtkNew<vtkPolyDataMapper> tubeMapper;
tubeMapper->SetInputConnection(tuber->GetOutputPort());
tubeMapper->SetScalarRange(tubePolyData->GetScalarRange());

vtkNew<vtkActor> lineActor;
lineActor->SetMapper(lineMapper);
lineActor->GetProperty()->SetLineWidth(3);
vtkNew<vtkActor> tubeActor;
tubeActor->SetMapper(tubeMapper);
tubeActor->GetProperty()->SetOpacity(0.6);

// Setup render window, renderer, and interactor
vtkNew<vtkNamedColors> colors;
vtkNew<vtkRenderer> renderer;
renderer->UseHiddenLineRemovalOn();
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetSize(640, 480);
renderWindow->SetWindowName("TubesFromSplines");

vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());

renderWindow->Render();
renderWindowInteractor->Start();

return EXIT_SUCCESS;
}


### CMakeLists.txt¶

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(TubesFromSplines)

find_package(VTK COMPONENTS
CommonColor
CommonComputationalGeometry
CommonCore
CommonDataModel
FiltersCore
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)

if (NOT VTK_FOUND)
message(FATAL_ERROR "TubesFromSplines: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
target_link_libraries(TubesFromSplines PRIVATE ${VTK_LIBRARIES} ) # vtk_module_autoinit is needed vtk_module_autoinit( TARGETS TubesFromSplines MODULES${VTK_LIBRARIES}
)


cd TubesFromSplines/build


If VTK is installed:

cmake ..


If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..


Build the project:

make


and run it:

./TubesFromSplines


WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.