[vtkusers] vtkTubeFilter with independently varying radius and color

Marcus Thamson markie_thomson at yahoo.de
Sat Dec 26 05:56:54 EST 2009


Dear Dave,

Thanks for your feedback.  I have managed to get the desired results by using:

polyData->GetPointData()->SetScalars(...)

twice - first for the tube radius array, then for the "array" of RGB triples.  A stand-alone C++ code is given below.

However, if I exchange the order of the two SetScalars commands (RGB array first, then radius scalars), then the RGB colors are not mapped.  This seems to be due to changing which of the two scalar arrays are "active" in polyData.

In trying to follow your advice, I gave the RGB array a name (see below, cols->SetName("cRGB")), and then try to use:

mapper->SelectColorArray("cRGB");

This doesn't appear to work. It seems I am not using these commands correctly.

Could you please advise how to explicitly use SelectColorArray in the present context to force the correct behaviour, irrespective of the order of SetScalars commands (which I might not always be able to choose due to code flow reasons).

Many thanks, MT


C++ code:
----------------------------------------

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

#include <math.h>
#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=64;		// No. of vertices
	unsigned int ncyc=2;	// No. of spiral cycles
	double Rt1=0.1, Rt2=0.5;				// Start/end tube radii
	double Rs=2;				// Spiral radius
	double h=3;				// 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 - linearly between start/end values
	vtkSmartPointer<vtkDoubleArray> tuberad = vtkSmartPointer<vtkDoubleArray>::New();
	tuberad->SetNumberOfTuples(nV);
	for (i=0 ;i<nV ; i++) tuberad->SetTuple1(i,Rt1+(Rt2-Rt1)*i/(nV-1));
	polyData->GetPointData()->SetScalars(tuberad);	

	// RBG array
	// Varying from blue to red
	vtkSmartPointer<vtkUnsignedCharArray> cols = vtkSmartPointer<vtkUnsignedCharArray>::New();
	cols->SetName("cRGB");
	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)) );
	}
	// and add to polyData
	polyData->GetPointData()->SetScalars(cols);
	vtkSmartPointer<vtkTubeFilter> tube = vtkSmartPointer<vtkTubeFilter>::New();
	tube->SetNumberOfSides(Ntv);
	tube->SetInput(polyData);
	tube->SetVaryRadiusToVaryRadiusOff();

	// Set tube radius scaling parameters to achieve "physical" radii
	vtkFloatingPointType tuberad_range[2];
	tuberad->GetRange(tuberad_range);
	tube->SetVaryRadiusToVaryRadiusByScalar();
	tube->SetRadius(tuberad_range[0]);
	tube->SetRadiusFactor(tuberad_range[1]/tuberad_range[0]);

	vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
	mapper->SetInput(tube->GetOutput());
	mapper->ScalarVisibilityOn();
	mapper->SelectColorArray("cRGB");  
	// This last line doesn't seem to help if the order of the two SetScalars() commands is reversed 

	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 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20091226/0756dfc2/attachment.htm>


More information about the vtkusers mailing list