[vtkusers] vtkTubeFilter with independently varying radius and color

Marcus Thamson markie_thomson at yahoo.de
Sat Jan 2 04:44:12 EST 2010


Dear All, 

I have finally solved my little problem, by using SetScalarModeToUsePointFieldData in conjunction with SelectColorArray (in the end nothing special, but I had to find the correct combination of commands...).

I came across about 5 other unresolved threads on this same issue, so I have pasted a demonstrative code below in case this helps anyone in the future.

Regards, MT

// VTK: Spiral with vtkTubeFilter
// Varying tube radius and independent RGB colors with an unsignedCharArray

#include <math.h>
#include <iostream>
using namespace std;

#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 <vtkInteractorStyleTrackballCamera.h>

#define PI 3.14159265

int main()
{
	// Spiral tube
	double vx, vy, vz;
	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

	unsigned int i;

	vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
	for(i=0; i<nV; i++) {
		// Spiral coordinates
		vx = Rs*cos(2*PI*ncyc*i/(nV-1));
		vy = Rs*sin(2*PI*ncyc*i/(nV-1));
		vz = h*i/nV;
		points->InsertPoint(i,vx,vy,vz);
	}

	vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
	lines->InsertNextCell(nV);
	for (i=0; i<nV; i++) lines->InsertCellPoint(i);

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

	// Varying tube radius - vary using sine-function
	vtkSmartPointer<vtkDoubleArray> tuberad = vtkSmartPointer<vtkDoubleArray>::New();
	tuberad->SetName("tuberad");
	tuberad->SetNumberOfTuples(nV);
	for (i=0 ;i<nV ; i++) tuberad->SetTuple1(i,Rt1+(Rt2-Rt1)*sin(PI*i/(nV-1)));
	polyData->GetPointData()->AddArray(tuberad);	
	polyData->GetPointData()->SetActiveScalars("tuberad");

	// RBG array (could add A channel too I guess...)
	// Varying from blue to red
	vtkSmartPointer<vtkUnsignedCharArray> cols = vtkSmartPointer<vtkUnsignedCharArray>::New();
	cols->SetName("cols");
	cols->SetNumberOfComponents(3);
	cols->SetNumberOfTuples(nV);
	for (i=0; i<nV ; i++) {
		cols->InsertTuple3(i, int(255*i/(nV-1)) , 0 , int(255*(nV-1-i)/(nV-1)) );
	}
	polyData->GetPointData()->AddArray(cols);
	
	vtkSmartPointer<vtkTubeFilter> tube = vtkSmartPointer<vtkTubeFilter>::New();
	tube->SetNumberOfSides(Ntv);
	tube->SetInput(polyData);
	tube->SetVaryRadiusToVaryRadiusByAbsoluteScalar();

	vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
	mapper->SetInput(tube->GetOutput());
	mapper->ScalarVisibilityOn();
	mapper->SetScalarModeToUsePointFieldData();
	mapper->SelectColorArray("cols");  

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

	vtkRenderer *renderer = vtkRenderer::New();
	renderer->AddActor(actor);

	vtkRenderWindow *renWin = vtkRenderWindow::New();
	vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();

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

	vtkInteractorStyleTrackballCamera *style =
		vtkInteractorStyleTrackballCamera::New();
	iren->SetInteractorStyle(style);

	iren->Start();

	renderer->Delete();
	renWin->Delete();
	iren->Delete();
	style->Delete();

	return 0;
}



__________________________________________________
Do You Yahoo!?
Sie sind Spam leid? Yahoo! Mail verfügt über einen herausragenden Schutz gegen Massenmails. 
http://mail.yahoo.com 



More information about the vtkusers mailing list