[vtkusers] Voxelization of a 3D Polygonal model
Koutsokeras Miltiadis
mkoutsok at iit.demokritos.gr
Mon Apr 11 05:36:35 EDT 2005
I have some strange problems concerning the vtkVoxelModeller class. I'm
currently trying to visualize a polygonal model (vtkPolyData) using
voxel through vtkVoxelModeller. Here's my code:
// vtkMesh3D inherits vtkPolyData and adds a function to read geometry
from OFF format files
class vtkMesh3D: public vtkPolyData
{
public:
bool LoadOFFfile(const char *filepath); // loads an OFF
file. The filesystem path of the file is given from filepath
};
Now here's is the code on my main function:
int main(int argc, char **argv)
{
if(argc != 3) // wrong number of command line
arguments
{
fprintf(stderr, "Usage:\nModelViewer.exe <3D
model file> <Voxelization resolution>\n");
return 1;
}
else
{
//
########################################################################
// ###### MODEL WINDOW
####################################################
// create a new model
vtkMesh3D *model = (vtkMesh3D
*)vtkMesh3D::New(); // create a new object of class vtkMesh3D:
public vtkPolyData
if(!model->LoadOFFfile(argv[1])) // load the
file
{
fprintf(stderr, "\nFile \"%s\"
cannot be loaded", argv[1]);
return -1;
}
// map to graphics library
vtkPolyDataMapper *model_map =
vtkPolyDataMapper::New();
model_map->SetInput(model);
// actor coordinates geometry, properties,
transformation
vtkActor *model_actor = vtkActor::New();
model_actor->SetMapper(model_map);
// setup a renderer and render window
vtkRenderer *model_renderer =
vtkRenderer::New();
vtkRenderWindow *model_Win =
vtkRenderWindow::New();
model_Win->AddRenderer(model_renderer);
model_Win->SetWindowName("3D Model Viewer");
#ifdef _DEBUG
//model_Win->Print(cout);
#endif
// an interactor
vtkRenderWindowInteractor *model_Inter =
vtkRenderWindowInteractor::New();
model_Inter->SetRenderWindow(model_Win);
// add the actor to the scene
model_renderer->AddActor(model_actor);
model_renderer->SetBackground(0, 0, 0); //
Background color white
// render an image (lights and cameras are
created automatically)
model_Win->Render();
// begin mouse interaction
model_Inter->Start();
//
########################################################################
// Up to now everything works perfect and the
model renders correctly in the first Window
// Now to the problems
//
########################################################################
// ###### VOXEL WINDOW
####################################################
//*
// Voxelize model
int vgrid_res = atoi(argv[2]);
// Create voxel model
vtkVoxelModeller *voxel_model =
vtkVoxelModeller::New();
voxel_model->SetInput(model);
voxel_model->SetSampleDimensions(vgrid_res,
vgrid_res, vgrid_res);
voxel_model->SetModelBounds(model->GetBounds());
// Create a filter
vtkContourFilter *filter =
vtkContourFilter::New();
// If you pass without casting the compiler
reports an type cast error
// This is strange because vtkDataSet is parent class to vtkImageData
// "In other words, an object of a derived class can be treated as an
object of its base class when manipulated
// through pointers and references." says mr. Stroustrup in his book
filter->SetInput((vtkDataSet
*)voxel_model->GetOutput());
filter->SetValue(0, 0.999);
// map to graphics library
vtkPolyDataMapper *voxel_map =
vtkPolyDataMapper::New();
voxel_map->SetInput(filter->GetOutput());
// actor coordinates geometry, properties,
transformation
vtkActor *voxel_actor = vtkActor::New();
voxel_actor->SetMapper(voxel_map);
voxel_actor->GetProperty()->SetColor(0, 0 ,1);
// setup a renderer and render window
vtkRenderer *voxel_renderer =
vtkRenderer::New();
vtkRenderWindow *voxel_Win =
vtkRenderWindow::New();
voxel_Win->AddRenderer(voxel_renderer);
voxel_Win->SetWindowName("Voxel Grid");
#ifdef _DEBUG
//voxel_Win->Print(cout);
#endif
// an interactor
vtkRenderWindowInteractor *voxel_Inter =
vtkRenderWindowInteractor::New();
voxel_Inter->SetRenderWindow(voxel_Win);
// add the actor to the scene
voxel_renderer->AddActor(voxel_actor);
voxel_renderer->SetBackground(0, 0, 0); //
Background color white
// render an image (lights and cameras are
created automatically)
voxel_Win->Render(); // IN THIS LINE THE
PROGRAM CRASHES: Unhandled exception in Voxelizer.exe (VTKCOMMON.dll):
0xC0000005: Access Violation
// begin mouse interaction
voxel_Inter->Start();
//*/
//
########################################################################
// free memory resources
// voxel
voxel_Inter->Delete();
voxel_Win->Delete();
voxel_renderer->Delete();
voxel_actor->Delete();
voxel_map->Delete();
filter->Delete();
voxel_model->Delete();
// model
model_Inter->Delete();
model_Win->Delete();
model_renderer->Delete();
model_actor->Delete();
model_map->Delete();
model->Delete();
}
return 0; // All OK, terminate applcation
}
So something is wrong about my setup of the voxel rendering process.
Anyone familiar with this problem? Is something wrong in my code?
Any suggestions will be highly appreciated.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20050411/a6abac32/attachment.htm>
More information about the vtkusers
mailing list