[vtkusers] Segmenting Visible Points from Polydata
Daniel Mirota
dan at cs.jhu.edu
Thu Sep 22 14:59:22 EDT 2011
Dear VTKusers,
I am still fairly new to VTK and I am trying to use it to segment the
visible points from a mesh (Polydata). I followed the tutorial I
found for using vtkSelectVisiblePoints (
http://vtk.org/gitweb?p=VTK.git;a=blob;f=Examples/Annotation/Cxx/LabeledMesh/LabeledMesh.cxx
) how it is returning all of the points in the view frustum not just
the closest points. I tried vtkHardwareSelector (
http://vtk.org/gitweb?p=VTK.git;a=blob;f=Rendering/Testing/Cxx/TestAreaSelections.cxx
) but it returned no points. The code is rewritten to provide a c
library interface. The methods are called in the order they appear in
the source by a client program.
I'm using the vtk version the Slicer github github.com/pieper/SlicerVTK.git
Thanks,
Dan
Here is my source below:
#include <math.h>
#include <stdio.h>
/* VTK includes */
#include "vtkIdTypeArray.h"
#include "vtkCellArray.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPointData.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderer.h"
#include "vtkOBBTree.h"
#include "vtkSmartPointer.h"
#include "vtkCamera.h"
#include "vtkActor.h"
#include <vtkMath.h>
#include <vtkMatrix4x4.h>
#include <vtkProperty.h>
#include <vtkIdFilter.h>
#include <vtkCellCenters.h>
#include <vtkSelectVisiblePoints.h>
#include <vtkSelection.h>
#include <vtkSelectionNode.h>
#include <vtkExtractSelectedPolyDataIds.h>
#include <vtkGenericCell.h>
#include <vtkRenderWindow.h>
#include <vtkOpenGLRenderWindow.h>
#include <vtkGraphicsFactory.h>
#include <vtkImagingFactory.h>
#include <vtkCellLocator.h>
#include <vtkModifiedBSPTree.h>
#include <vtkVertexGlyphFilter.h>
#include <vtkCellData.h>
#include <vtkCleanPolyData.h>
#include <vtkHardwareSelector.h>
#include <vtkRenderedAreaPicker.h>
#define VTK_CREATE(type, name) vtkSmartPointer<type> name =
vtkSmartPointer<type>::New()
#ifdef __APPLE__ // OS X doesn't like VTK_CREATE for static types
#define VTK_DEFINE(type, name) vtkSmartPointer<type> name
#define VTK_INIT(type, name) name.TakeReference(type::New())
#else
#define VTK_DEFINE(type, name) VTK_CREATE(type, name)
#define VTK_INIT(type, name)
#endif
static VTK_DEFINE(vtkPolyData,myPolyData);
static VTK_DEFINE(vtkPolyDataMapper,myPolyDataMapper);
static VTK_DEFINE(vtkActor,myActor);
static VTK_DEFINE(vtkCellLocator,myCellLocator);
static VTK_DEFINE(vtkRenderer,myRenderer);
static VTK_DEFINE(vtkPoints,myOutputPoints);
static VTK_DEFINE(vtkRenderWindow,myRenderWindow);
static VTK_DEFINE(vtkMatrix4x4,F);
static VTK_DEFINE(vtkIdFilter,ids);
static VTK_DEFINE(vtkCellCenters,cellCenters);
static VTK_DEFINE(vtkSelectVisiblePoints,visible);
static VTK_DEFINE(vtkExtractSelectedPolyDataIds,selFilter);
static double * points_out = NULL;
static float * visible_points_out = NULL;
static unsigned int current_number_of_points = 0;
static unsigned int current_number_of_visible_points = 0;
static bool enough_cells = false;
static bool myUpdateCameraOnly = false;
void initialize(){
VTK_INIT(vtkPolyData,myPolyData);
VTK_INIT(vtkPolyDataMapper,myPolyDataMapper);
VTK_INIT(vtkActor,myActor);
VTK_INIT(vtkCellLocator,myCellLocator);
VTK_INIT(vtkRenderer,myRenderer);
VTK_INIT(vtkPoints,myOutputPoints);
VTK_INIT(vtkRenderWindow,myRenderWindow);
VTK_INIT(vtkMatrix4x4,F);
VTK_INIT(vtkIdFilter,ids);
VTK_INIT(vtkCellCenters,cellCenters);
VTK_INIT(vtkSelectVisiblePoints,visible);
VTK_INIT(vtkExtractSelectedPolyDataIds,selFilter);
}
void set_polygon_data(float * vertices_in,unsigned int
number_of_vertices_in,long long * faces_in, unsigned int
number_of_faces_in){
VTK_CREATE(vtkFloatArray,vertices_raw);
VTK_CREATE(vtkPoints,vertices);
VTK_CREATE(vtkCellArray,faces);
VTK_CREATE(vtkIdTypeArray,faces_raw);
VTK_CREATE(vtkPolyData,tempPolyData);
vertices_raw->SetNumberOfComponents(3);
vertices_raw->SetArray(vertices_in,number_of_vertices_in*3,1);
faces_raw->SetArray(static_cast<vtkIdType
*>(faces_in),static_cast<vtkIdType>(number_of_faces_in)*4,1);
faces->SetCells(static_cast<vtkIdType>(number_of_faces_in),faces_raw);
faces->InitTraversal();
vertices->SetDataTypeToDouble();
vertices->SetData(vertices_raw);
tempPolyData->SetPoints(vertices);
tempPolyData->SetPolys(faces);
myPolyData->DeepCopy(tempPolyData);
myPolyDataMapper->SetInput(myPolyData);
myActor->SetMapper(myPolyDataMapper);
myActor->GetProperty()->BackfaceCullingOn();
myActor->GetProperty()->SetRepresentationToSurface();
myRenderer->AddActor(myActor);
myRenderer->SetBackground(0.2,0.2,0.2);
ids->SetInput(myPolyData);
ids->PointIdsOn();
ids->CellIdsOn();
ids->FieldDataOn();
cellCenters->SetInputConnection(ids->GetOutputPort());
visible->SetInputConnection(cellCenters->GetOutputPort());
visible->SetRenderer(myRenderer);
}
void set_camera_parameter(double * F_in, double * fc, double * cc, int
* image_size){
//reset camera parameters
VTK_CREATE(vtkMatrix4x4,F);
F->DeepCopy(F_in);
vtkCamera * myCamera = myRenderer->GetActiveCamera();
myCamera->SetPosition(F->GetElement(0,3),F->GetElement(1,3),F->GetElement(2,3));
myCamera->SetViewUp(-F->GetElement(0,1),-F->GetElement(1,1),-F->GetElement(2,1));
myCamera->SetFocalPoint(F->GetElement(0,3) + F->GetElement(0,2),
F->GetElement(1,3) + F->GetElement(1,2), F->GetElement(2,3) +
F->GetElement(2,2));
double view_angle = vtkMath::DegreesFromRadians(2.0 *
atan2(image_size[1] / 2.0, fc[1]));
myCamera->SetViewAngle(view_angle);
if(!myUpdateCameraOnly){
if(!myRenderWindow->GetOffScreenRendering()){
myRenderWindow->OffScreenRenderingOn();
}
if(!myRenderWindow->HasRenderer(myRenderer)){
myRenderWindow->AddRenderer(myRenderer);
}
int * current_size = myRenderWindow->GetSize();
if(current_size[0] != image_size[0] || current_size[1] !=
image_size[1] ){
myRenderWindow->SetSize(image_size);
}
if(myRenderWindow->GetNeverRendered()){
myRenderWindow->Render();
}
}
}
void update_visible_polygons(){
visible->Update();
VTK_CREATE(vtkSelection,sel);
VTK_CREATE(vtkSelectionNode,node);
sel->AddNode(node);
node->SetContentType(vtkSelectionNode::INDICES);
node->SetFieldType(vtkSelectionNode::CELL);
node->SetSelectionList(visible->GetOutput()->GetPointData()->GetArray(0));
selFilter->SetInput(1,sel);
selFilter->SetInput(0,myPolyData);
selFilter->Update();
}
float * get_visible_points( unsigned int * number_of_vertices_out){
//VTK_CREATE(vtkRenderedAreaPicker, areaPicker);
//int * current_size = myRenderWindow->GetSize();
//areaPicker->AreaPick(0,0,current_size[0],current_size[1],myRenderer);
//VTK_CREATE(vtkHardwareSelector,selector);
//selector->SetRenderer(myRenderer);
//double x0 = myRenderer->GetPickX1();
//double y0 = myRenderer->GetPickY1();
//double x1 = myRenderer->GetPickX2();
//double y1 = myRenderer->GetPickY2();
//int * current_size = myRenderWindow->GetSize();
//selector->SetArea(static_cast<int>(x0),static_cast<int>(y0),static_cast<int>(x1),static_cast<int>(y1));
//selector->SetFieldAssociation(vtkDataObject::FIELD_ASSOCIATION_POINTS);
//VTK_CREATE(vtkSelection,selection);
//selection.TakeReference(selector->Select());
//selFilter->SetInput(1,selection);
//selFilter->SetInput(0,myPolyData);
//selFilter->Update();
//clean with vtkCleanPolyData
VTK_CREATE(vtkCleanPolyData,cleaner);
cleaner->SetInputConnection(selFilter->GetOutputPort());
cleaner->Update();
//get points and return
unsigned int number_of_vertices = cleaner->GetOutput()->GetNumberOfPoints();
if(current_number_of_visible_points < number_of_vertices){
if(visible_points_out != NULL){
delete [] visible_points_out;
visible_points_out = NULL;
}
visible_points_out = new float[number_of_vertices*3];
//
current_number_of_points = number_of_vertices;
}
memset(visible_points_out,0,number_of_vertices*3*sizeof(float));
(*number_of_vertices_out) = number_of_vertices;
//float * vertices_out = new float[number_of_vertices*3];
vtkPoints * points = cleaner->GetOutput()->GetPoints();
points->Print(std::cout);
std::cout.flush();
vtkDataArray * points_data = points->GetData();
points_data->Print(std::cout);
std::cout.flush();
vtkFloatArray * points_float_data =
vtkFloatArray::SafeDownCast(points_data);
points_float_data->Print(std::cout);
std::cout.flush();
unsigned int i = 0;
float * point_out = NULL;
float * zPtr = NULL;
while(i < number_of_vertices){
point_out = &visible_points_out[i*3];
points_float_data->GetTupleValue(i,point_out);
i++;
}
return visible_points_out;
}
More information about the vtkusers
mailing list