### Description¶

This example shows how to vary the radius of a tube with one scalar and color the tube with another.

• Contributed by Marcus Thamson

Note

This original source code for this example is here.

Question

### Code¶

#include <math.h>

#include <vtkCellArray.h>
#include <vtkDoubleArray.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>

#include <vtkCell.h>
#include <vtkCellData.h>
#include <vtkDataSet.h>
#include <vtkDataSetAttributes.h>
#include <vtkProperty.h>
#include <vtkSmartPointer.h>
#include <vtkTubeFilter.h>

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkDataSetMapper.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

#include <vtkMath.h>

int main(int, char*[])
{
// Spiral tube
unsigned int nV = 256;       // No. of vertices
unsigned int nCyc = 5;       // No. of spiral cycles
double rT1 = 0.1, rT2 = 0.5; // Start/end tube radii
double rS = 2;               // Spiral radius
double h = 10;               // Height
unsigned int nTv = 8;        // No. of surface elements for each tube vertex

// Create points and cells for the spiral
auto points =
vtkSmartPointer<vtkPoints>::New();
for (unsigned int i = 0; i < nV; i++)
{
// Spiral coordinates
auto vX = rS * cos(2 * vtkMath::Pi() * nCyc * i / (nV - 1));
auto vY = rS * sin(2 * vtkMath::Pi() * nCyc * i / (nV - 1));
auto vZ = h * i / nV;
points->InsertPoint(i, vX, vY, vZ);
}

auto lines =
vtkSmartPointer<vtkCellArray>::New();
lines->InsertNextCell(nV);
for (unsigned int i = 0; i < nV; i++)
{
lines->InsertCellPoint(i);
}

auto polyData =
vtkSmartPointer<vtkPolyData>::New();
polyData->SetPoints(points);
polyData->SetLines(lines);

// Varying tube radius using sine-function
vtkSmartPointer<vtkDoubleArray>::New();
for (unsigned int i = 0; i < nV; i++)
{
i, rT1 + (rT2 - rT1) * sin(vtkMath::Pi() * i / (nV - 1)));
}

// RBG array (could add Alpha channel too I guess...)
// Varying from blue to red
auto colors =
vtkSmartPointer<vtkUnsignedCharArray>::New();
colors->SetName("Colors");
colors->SetNumberOfComponents(3);
colors->SetNumberOfTuples(nV);
for (unsigned int i = 0; i < nV; i++)
{
colors->InsertTuple3(i, int(255 * i / (nV - 1)), 0,
int(255 * (nV - 1 - i) / (nV - 1)));
}

auto tube =
vtkSmartPointer<vtkTubeFilter>::New();
tube->SetInputData(polyData);
tube->SetNumberOfSides(nTv);

auto mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(tube->GetOutputPort());
mapper->ScalarVisibilityOn();
mapper->SetScalarModeToUsePointFieldData();
mapper->SelectColorArray("Colors");

auto actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);

auto renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->SetBackground(.2, .3, .4);

// Make an oblique view
renderer->GetActiveCamera()->Azimuth(30);
renderer->GetActiveCamera()->Elevation(30);
renderer->ResetCamera();

auto renWin =
vtkSmartPointer<vtkRenderWindow>::New();
auto iren =
vtkSmartPointer<vtkRenderWindowInteractor>::New();

iren->SetRenderWindow(renWin);
renWin->SetSize(500, 500);
renWin->Render();

auto style =
vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
iren->SetInteractorStyle(style);

iren->Start();

return EXIT_SUCCESS;
}


### CMakeLists.txt¶

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

find_package(VTK COMPONENTS
vtkCommonCore
vtkCommonDataModel
vtkFiltersCore
vtkInteractionStyle
vtkRenderingContextOpenGL2
vtkRenderingCore
vtkRenderingFreeType
vtkRenderingGL2PSOpenGL2
vtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
message("Skipping TubesWithVaryingRadiusAndColors: ${VTK_NOT_FOUND_MESSAGE}") return () endif() message (STATUS "VTK_VERSION:${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
# old system
include(${VTK_USE_FILE}) add_executable(TubesWithVaryingRadiusAndColors MACOSX_BUNDLE TubesWithVaryingRadiusAndColors.cxx ) target_link_libraries(TubesWithVaryingRadiusAndColors PRIVATE${VTK_LIBRARIES})
else ()
# include all components
target_link_libraries(TubesWithVaryingRadiusAndColors PRIVATE ${VTK_LIBRARIES}) # vtk_module_autoinit is needed vtk_module_autoinit( TARGETS TubesWithVaryingRadiusAndColors MODULES${VTK_LIBRARIES}
)
endif ()


cd TubesWithVaryingRadiusAndColors/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:

./TubesWithVaryingRadiusAndColors


WINDOWS USERS

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