[vtkusers] Reconstruct a surface from points with colors

AGPX agpxnet at yahoo.it
Tue Dec 1 07:47:57 EST 2009


Hi,

I have an unorganized points that could be fitted in a surface with the following code:

(taken from: http://www.cmake.org/Wiki/Create_a_surface_from_Unorganized_Points, in blue you can see my modification to that code)


#include "vtkSurfaceReconstructionFilter.h"
#include "vtkProgrammableSource.h"
#include "vtkContourFilter.h"
#include "vtkReverseSense.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkPolyData.h"
#include "vtkCamera.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include <vtkMath.h>
#include <cmath>
 
vtkPoints* readPoints();
 
int main(int argc, char **argv)
{
 
// Read some points
	vtkPoints* points = readPoints();
	vtkPolyData* polydata = vtkPolyData::New();
	polydata->SetPoints(points);
        
        ...generate scalar...
        
    polydata->GetPointData()->SetScalars(scalars);
 
// Construct the surface and create isosurface.	
	vtkSurfaceReconstructionFilter* surf = vtkSurfaceReconstructionFilter::New();
	surf->SetInput(polydata);
 
	vtkContourFilter* cf = vtkContourFilter::New();
	cf->SetInputConnection(surf->GetOutputPort());
	cf->SetValue(0, 0.0);
 
// Sometimes the contouring algorithm can create a volume whose gradient
// vector and ordering of polygon (using the right hand rule) are
// inconsistent. vtkReverseSense cures this problem.
	vtkReverseSense* reverse = vtkReverseSense::New();
	reverse->SetInputConnection(cf->GetOutputPort());
	reverse->ReverseCellsOn();
	reverse->ReverseNormalsOn();
 
vtkLookupTable *lut = vtkLookupTable::New();
	lut->SetHueRange(0.267, 0.5);
	lut->SetSaturationRange(1, 1);
	lut->SetValueRange(1, 1);
	lut->Build();

	vtkPolyDataMapper* map = vtkPolyDataMapper::New();
	map->SetInputConnection(reverse->GetOutputPort());
	map->ScalarVisibilityOn();
	map->SetScalarRange(0, 1);
	map->SetScalarModeToDefault();
	map->SetLookupTable(lut);
 
	vtkActor* surfaceActor = vtkActor::New();
	surfaceActor->SetMapper(map);

// Create the RenderWindow, Renderer and both Actors
	vtkRenderer* ren = vtkRenderer::New();
	vtkRenderWindow* renWin = vtkRenderWindow::New();
	renWin->AddRenderer(ren);
	vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
	iren->SetRenderWindow(renWin);
 
// Add the actors to the renderer, set the background and size
	ren->AddActor(surfaceActor);
	ren->SetBackground(1, 1, 1);
	renWin->SetSize(400, 400);
	ren->GetActiveCamera()->SetFocalPoint(0, 0, 0);
	ren->GetActiveCamera()->SetPosition(1, 0, 0);
	ren->GetActiveCamera()->SetViewUp(0, 0, 1);
	ren->ResetCamera();
	ren->GetActiveCamera()->Azimuth(20);
	ren->GetActiveCamera()->Elevation(30);
	ren->GetActiveCamera()->Dolly(1.2);
	ren->ResetCameraClippingRange();
 
	iren->Initialize();
	renWin->Render();
	iren->Start();
 
return 0;
 
}
 
vtkPoints* readPoints()
{
	vtkPoints* points = vtkPoints::New();
 
float x, y, z;
// generate random points on unit sphere
for(int i=0; i<100; i++) {
double phi, theta,u,v;
		u = vtkMath::Random(0.0,1.0);
		v = vtkMath::Random(0.0,1.0);
		phi = 2.0*3.14159265*u;
		theta = acos(2.0*v-1.0);
 
		x = std::cos(phi)*std::sin(theta);
		y = std::sin(phi)*std::sin(theta);
		z = std::cos(theta);
 
 
		points->InsertNextPoint(x, y, z);
}
return points;
}
My problem is that I need to associate a scalar to every point in order to generated a colored surface (with a lookup-table). However the surface have always an unique color and seems that the scalar data are ignored (or maybe lost by the filters). How I can obtain that result?

Thanks,

- AGPX



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20091201/9e31a0e1/attachment.htm>


More information about the vtkusers mailing list