[vtkusers] Smooth out surface(interpolate)

madz madaramh at gmail.com
Thu Nov 21 05:21:10 EST 2013


I'm trying to build a 3D surface plot like the below image,

<http://vtk.1045678.n5.nabble.com/file/n5724545/delpeaks1.gif> 

The plot I was able to create looks the following image,

<http://vtk.1045678.n5.nabble.com/file/n5724545/not_smooth.png> 

How do I smooth out the peaks of the grid, I want it to have an interpolated
like quality as in the first image.
My code so far is,

#include <vtkVersion.h>
#include <vtkSmartPointer.h>
#include <vtkActor.h>
#include <vtkDelaunay2D.h>
#include <vtkLookupTable.h>
#include <vtkMath.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 <vtkVertexGlyphFilter.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkCubeAxesActor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkAxisActor.h>
#include <vtkCamera.h>

int main(int, char *[])
{
	// Create a grid of points (height/terrian map)
	vtkSmartPointer<vtkPoints> points = 
		vtkSmartPointer<vtkPoints>::New();

	unsigned int GridSize = 5;
	double xx, yy, zz;
	for(unsigned int x = 0; x < GridSize; x++)
	{
		for(unsigned int y = 0; y < GridSize; y++)
		{
			xx = x + vtkMath::Random(-.2, .2);
			yy = y + vtkMath::Random(-.2, .2);
			zz = vtkMath::Random(-4, 4);
			points->InsertNextPoint(xx, yy, zz);
		}
	}

	// Add the grid points to a polydata object
	vtkSmartPointer<vtkPolyData> inputPolyData = 
		vtkSmartPointer<vtkPolyData>::New();
	inputPolyData->SetPoints(points);

	// Triangulate the grid points
	vtkSmartPointer<vtkDelaunay2D> delaunay = 
		vtkSmartPointer<vtkDelaunay2D>::New();
#if VTK_MAJOR_VERSION <= 5
	delaunay->SetInput(inputPolyData);
#else
	delaunay->SetInputData(inputPolyData);
#endif
	delaunay->Update();
	vtkPolyData* outputPolyData = delaunay->GetOutput();

	double bounds[6];
	outputPolyData->GetBounds(bounds);

	// Find min and max z
	double minz = bounds[4];
	double maxz = bounds[5];

	std::cout << "minz: " << minz << std::endl;
	std::cout << "maxz: " << maxz << std::endl;

	// Create the color map
	vtkSmartPointer<vtkLookupTable> colorLookupTable = 
		vtkSmartPointer<vtkLookupTable>::New();
	colorLookupTable->SetTableRange(minz, maxz);
	colorLookupTable->Build();

	// Generate the colors for each point based on the color map
	vtkSmartPointer<vtkUnsignedCharArray> colors = 
		vtkSmartPointer<vtkUnsignedCharArray>::New();
	colors->SetNumberOfComponents(3);
	colors->SetName("Colors");

	std::cout << "There are " << outputPolyData->GetNumberOfPoints()
		<< " points." << std::endl;

	for(int i = 0; i < outputPolyData->GetNumberOfPoints(); i++)
	{
		double p[3];
		outputPolyData->GetPoint(i,p);

		double dcolor[3];
		colorLookupTable->GetColor(p[2], dcolor);
		unsigned char color[3];
		for(unsigned int j = 0; j < 3; j++)
		{
			color[j] = static_cast<unsigned char>(255.0 * dcolor[j]);
		}
		colors->InsertNextTupleValue(color);
	}

	outputPolyData->GetPointData()->SetScalars(colors);

	// Create a mapper and actor
	vtkSmartPointer<vtkPolyDataMapper> mapper = 
		vtkSmartPointer<vtkPolyDataMapper>::New();
#if VTK_MAJOR_VERSION <= 5
	mapper->SetInputConnection(outputPolyData->GetProducerPort());
#else
	mapper->SetInputData(outputPolyData);
#endif

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

	vtkSmartPointer<vtkCubeAxesActor> axes =
vtkSmartPointer<vtkCubeAxesActor>::New();
	axes->SetBounds(delaunay->GetOutput()->GetBounds());
	axes->SetFlyModeToOuterEdges();
	axes->XAxisMinorTickVisibilityOff();
	axes->YAxisMinorTickVisibilityOff();
	axes->ZAxisMinorTickVisibilityOff();
	axes->SetXLabelFormat("%6.1f");

	// Create a renderer, render window, and interactor
	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);
	renderer->SetBackground(.1, .2, .3);

	// Render and interact
	renderWindow->Render();

	renderer->GetActiveCamera()->Elevation(290);
	renderer->AddViewProp(axes);
	axes->SetCamera(renderer->GetActiveCamera());

	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

Thank you.




--
View this message in context: http://vtk.1045678.n5.nabble.com/Smooth-out-surface-interpolate-tp5724545.html
Sent from the VTK - Users mailing list archive at Nabble.com.


More information about the vtkusers mailing list