[vtkusers] vtk scaler colouring issue

madz madaramh at gmail.com
Tue Dec 10 07:06:46 EST 2013


I'm trying to draw a 3D graph (x,y,z value graph) in which I have to set the
the colouring values by a separate array. I tried two different methods and
each gave different results. The first was by vtkElevationFilter, which I
assume is using z values(?) to colour the graph the second is by setting a
scaler to pointdata (z value array). These two gave very different results.
I prefer the output of the elevation method, but cannot use it since I need
to change the colour by a separate array (other than elevation/z value) in
the future. How can I achieve similar results with the scalar method?
I have included a boolean  "setScaler" variable to test the two methods in
the below code. If set to false it will use elevation method.


#include <vtkSmartPointer.h>
#include <vtkLookupTable.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkLinearSubdivisionFilter.h>
#include <vtkDoubleArray.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkDelaunay2D.h>
#include <vtkMath.h>
#include <vtkPointData.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkCamera.h>
#include <vtkButterflySubdivisionFilter.h>
#include <vtkFloatArray.h>
#include <vtkElevationFilter.h>

int main(int, char *[])
{
	bool setScaler = false; //set true to test with elevation
	std::vector<double> zVal;
	// Create a grid of points (height/terrian map)
	vtkSmartPointer<vtkPoints> points =  vtkSmartPointer<vtkPoints>::New();

	std::vector<std::vector&lt;double>> p;
	for(int i=0; i<4; i++){
		for(int ii=0;ii<3;ii++){
			double z = vtkMath::Random(0, 3);
			points->InsertNextPoint(i, ii, z);
			zVal.push_back(z);
		}	
	}

	vtkSmartPointer<vtkPolyData> inputPolyData =
vtkSmartPointer<vtkPolyData>::New();
	inputPolyData->SetPoints(points);

	vtkSmartPointer<vtkDelaunay2D> delaunay =
vtkSmartPointer<vtkDelaunay2D>::New();
#if VTK_MAJOR_VERSION <= 5
	delaunay->SetInput(inputPolyData);
#else
	delaunay->SetInputData(inputPolyData);
#endif
	delaunay->Update();

	vtkSmartPointer<vtkPolyData> outputPolyData =
vtkSmartPointer<vtkPolyData>::New();
	vtkSmartPointer<vtkLookupTable> colorLookupTable
=vtkSmartPointer<vtkLookupTable>::New();

	double tempBounds[6];
	points->GetBounds(tempBounds);

	if(!setScaler){
		vtkSmartPointer<vtkElevationFilter> elevationFilter =
vtkSmartPointer<vtkElevationFilter>::New();
		elevationFilter->SetInputConnection(delaunay->GetOutputPort());
		elevationFilter->SetLowPoint(0.0, 0.0, tempBounds[4]);
		elevationFilter->SetHighPoint(0.0, 0.0, tempBounds[5]);
		elevationFilter->Update();
	
outputPolyData->ShallowCopy(vtkPolyData::SafeDownCast(elevationFilter->GetOutput()));
	}else{

		outputPolyData = delaunay->GetOutput();
		vtkSmartPointer<vtkDoubleArray> tempArray =
vtkSmartPointer<vtkDoubleArray>::New();
		tempArray->SetName("colordata");
		for(int i=0;i<zVal.size();i++)
		{
			tempArray->InsertNextValue(zVal[i]);
		}
		outputPolyData->GetPointData()->SetScalars(tempArray);
	}

	double min = tempBounds[4], max = tempBounds[5];
	colorLookupTable->SetTableRange(min,max);
	double vmin,vmax; 
	colorLookupTable->GetHueRange(vmin,vmax);
	if(vmin!=vmax)
	{
		colorLookupTable->SetHueRange(0.6667,0.0);
	}
	colorLookupTable->Build();

	vtkSmartPointer<vtkLinearSubdivisionFilter> subdivide2 =
vtkSmartPointer<vtkLinearSubdivisionFilter>::New();
	subdivide2->SetNumberOfSubdivisions(2);
#if VTK_MAJOR_VERSION <= 5
	subdivide2->SetInputConnection(outputPolyData->GetProducerPort());
#else
	//subdivide->SetInputData(outputPolyData);
#endif
	subdivide2->Update();

	vtkSmartPointer<vtkButterflySubdivisionFilter> subdivide =
vtkSmartPointer<vtkButterflySubdivisionFilter>::New();
	subdivide->SetNumberOfSubdivisions(4);
#if VTK_MAJOR_VERSION <= 5
	subdivide->SetInputConnection(subdivide2->GetOutputPort());
#else
	//subdivide->SetInputData(outputPolyData);
#endif
	subdivide->Update();

	// Create a mapper and actor
	vtkSmartPointer<vtkPolyDataMapper> mapper
=vtkSmartPointer<vtkPolyDataMapper>::New();
	mapper->SetInputConnection(subdivide->GetOutputPort()); 
	mapper->ScalarVisibilityOn();
	mapper->SetColorModeToMapScalars();
	mapper->SetLookupTable(colorLookupTable);

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

	vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
	vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
	renderWindow->AddRenderer(renderer);
	vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
	renderWindowInteractor->SetRenderWindow(renderWindow);

	//prevents the pointer option from going haywire
	vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
	renderWindowInteractor->SetInteractorStyle(style);

	// Add the actor to the scene
	renderer->AddActor(actor);
	renderWindow->Render();
	renderer->GetActiveCamera()->Elevation(290);
	renderWindow->Render();

	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}


Thank you.






--
View this message in context: http://vtk.1045678.n5.nabble.com/vtk-scaler-colouring-issue-tp5724940.html
Sent from the VTK - Users mailing list archive at Nabble.com.


More information about the vtkusers mailing list