[vtkusers] How can i generate a surface from a set of points?
de Boer Ingo
I.deBoer at polytec.de
Thu Mar 25 02:23:14 EST 2004
Hi,
try this... it should work.
I know this doesn't find your bug in the program, but I am a bit
lazy this morning ;)
greets
Ingo
---
Dr.-Ing. Ingo H. de Boer
Polytec GmbH
Polytec-Platz 1-7, 76337 Waldbronn, Germany
phone: ++49 7243 604 106
fax : ++49 7243 604 255
#include "vtkPolyData.h"
#include "vtkPoints.h"
#include "vtkSurfaceReconstructionFilter.h"
#include "vtkContourFilter.h"
#include "vtkReverseSense.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkActor.h"
#include "vtkCamera.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkDecimatePro.h"
#include "vtkSmoothPolyDataFilter.h"
#include "vtkPolyDataNormals.h"
int main( int argc, char *argv[] )
{
// Read some 3d points.
FILE *datei = fopen(argv[1],"rt");
vtkPolyData *data = vtkPolyData::New();
vtkPoints *points = vtkPoints::New();
int iCounter = 0;
while (!feof(datei))
{
fprintf(stdout,"reading points: %i\r", iCounter);
float x, y, z;
fscanf(datei,"%f %f %f\n", &x, &y, &z);
points->InsertPoint(iCounter++, x, y, z);
}
data->SetPoints(points);
fclose(datei);
fprintf(stdout,"\n\n");
// Construct the surface and create isosurface
vtkSurfaceReconstructionFilter *surf = vtkSurfaceReconstructionFilter::New();
surf->SetInput(data);
vtkContourFilter *cf = vtkContourFilter::New();
cf->SetInput((vtkDataSet*)surf->GetOutput());
cf->SetValue(0, 0.0);
//#define _SMOOTHING_
#ifdef _SMOOTHING_
/* vtkDecimatePro *decimate = vtkDecimatePro::New();
decimate->SetInput(cf->GetOutput());
decimate->SetTargetReduction(0.9);
decimate->PreserveTopologyOn();
*/
vtkSmoothPolyDataFilter *smoother = vtkSmoothPolyDataFilter::New();
smoother->SetInput(cf->GetOutput());
smoother->SetNumberOfIterations(50);
vtkPolyDataNormals *normals = vtkPolyDataNormals::New();
normals->SetInput(smoother->GetOutput());
normals->FlipNormalsOn();
vtkPolyDataMapper *map = vtkPolyDataMapper::New();
map->SetInput(normals->GetOutput());
#else
vtkReverseSense *reverse = vtkReverseSense::New();
reverse->SetInput(cf->GetOutput());
reverse->ReverseCellsOn();
reverse->ReverseNormalsOn();
vtkPolyDataMapper *map = vtkPolyDataMapper::New();
map->SetInput(reverse->GetOutput());
#endif
map->ScalarVisibilityOff();
vtkActor *surfaceActor = vtkActor::New();
surfaceActor->SetMapper(map);
surfaceActor->GetProperty()->SetDiffuseColor(1.0000, 0.3882, 0.2784);
surfaceActor->GetProperty()->SetSpecularColor(1, 1, 1);
surfaceActor->GetProperty()->SetSpecular(.4);
surfaceActor->GetProperty()->SetSpecularPower(50);
// The usual rendering stuff.
vtkCamera *camera = vtkCamera::New();
camera->SetPosition(1,1,1);
camera->SetFocalPoint(0,0,0);
vtkRenderer *renderer = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(renderer);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
renderer->AddActor(surfaceActor);
renderer->SetActiveCamera(camera);
renderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);
renderer->GetActiveCamera()->SetPosition(1, 0, 0);
renderer->GetActiveCamera()->SetViewUp(0, 0, 1);
renderer->ResetCamera();
renderer->GetActiveCamera()->Azimuth(20);
renderer->GetActiveCamera()->Elevation(30);
renderer->GetActiveCamera()->Dolly(1.2);
renderer->ResetCameraClippingRange();
renderer->SetBackground(1,1,1);
renWin->SetSize(300,300);
// interact with data
renWin->Render();
iren->Start();
// Clean up
points->Delete();
data->Delete();
surf->Delete();
cf->Delete();
#ifdef _SMOOTHING_
// decimate->Delete();
smoother->Delete();
normals->Delete();
#else
reverse->Delete();
#endif
map->Delete();
surfaceActor->Delete();
camera->Delete();
renderer->Delete();
renWin->Delete();
iren->Delete();
return 0;
}
More information about the vtkusers
mailing list