[vtkusers] Re: Another Try: storing picked points
George Kamucha
kamucha at hfm.e-technik.uni-kassel.de
Mon Nov 4 08:23:33 EST 2002
Hi Steffen,
Thanks a lot for your fast response. Ja, you are very right, the redefining of the vtkPoints is
the whole problem. I had opted to using an array of pointers, "pointsArray" ( see the attached
snippet), to store the picked coordinates but also ended up getting useless data. Am I doing
something terribly wrong or is there any other alternative? This picking business is really
giving me a headache!
Gruss
George
#include "vtkPolyDataReader.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkActor.h"
#include "vtkPointPicker.h"
#include "vtkSphereSource.h"
#include "vtkGlyph3D.h"
#include "vtkSelectPolyData.h"
#include "vtkClipPolyData.h"
#include <stdio.h>
#include <ctype.h>
#include <iostream.h>
static void pickControl(void *);
static vtkRenderer *ren1;
static vtkRenderWindow *renWin;
static vtkPoints *points;
static int n, p;
static float *pointsArray[5];
vtkPolyDataReader *reader;
void main( int argc, char *argv[] )
{
n=0;
p=0;
//Load the original Laser surface data LaserNewd.vtk
reader = vtkPolyDataReader::New();
reader->SetFileName ("../../../vtkdata/LaserNewd.vtk");
reader->Update();
vtkPolyDataMapper *dataMapper = vtkPolyDataMapper::New();
dataMapper->SetInput(reader->GetOutput());
dataMapper->ScalarVisibilityOff();
vtkActor *dataActor = vtkActor::New();
dataActor->SetMapper(dataMapper);
dataActor->GetProperty()->SetColor(1, 1, 0);
dataActor->GetProperty()->SetOpacity(0.5);
vtkPointPicker *pointpicker = vtkPointPicker::New();
ren1 = vtkRenderer::New();
renWin = vtkRenderWindow::New();
renWin->AddRenderer(ren1);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
iren->SetPicker(pointpicker);
iren->SetEndPickMethod(pickControl, (void *)iren);
ren1->AddActor(dataActor);
// render the image
ren1->SetBackground(1, 1, 1);
renWin->SetSize(500,500);
iren->Initialize();
renWin->Render();
iren->Start();
//Clean up
ren1->Delete();
renWin->Delete();
iren->Delete();
//reader->Delete();
dataMapper->Delete();
dataActor->Delete();
pointpicker->Delete();
}
// Define picking method
static void pickControl(void *arg)
{
float *selPt;
float *pickpos;
float *pc;
int i;
//char KeyPressed;
//KeyPressed=getchar();
vtkRenderWindowInteractor *iren = (vtkRenderWindowInteractor *)arg;
vtkPointPicker *pointpicker = (vtkPointPicker *)iren->GetPicker();
selPt = pointpicker->GetSelectionPoint();
cout<<"Screen location:"<<selPt[0]<<" "<<selPt[1]<<"\n";
if ( pointpicker->GetPointId() >= 0 )
{
p = p+1; //count number of points picked
pickpos = pointpicker->GetPickPosition();
cout<<"Point location:"<<pickpos[0]<<" "<<pickpos[1]<<" "<<pickpos[2]<<"\n";
//Use an Array of pointers to store the picked coordinates
pointsArray[(p-1)] = pickpos;
//Define method for placing spheres
points=vtkPoints::New();
points-> InsertNextPoint(pickpos);
points-> Modified();
vtkPolyData *profile=vtkPolyData::New();
profile->SetPoints(points);
vtkSphereSource *sphere=vtkSphereSource::New();
sphere->SetRadius(2);
vtkGlyph3D *glyph=vtkGlyph3D::New();
glyph->SetInput(profile);
glyph->SetSource(sphere->GetOutput());
vtkPolyDataMapper *glyphMapper=vtkPolyDataMapper::New();
glyphMapper->SetInput(glyph->GetOutput());
vtkActor *glyphActor=vtkActor::New();
glyphActor->SetMapper(glyphMapper);
glyphActor->GetProperty()->SetColor(1, 0, 0); //Set colour to red
ren1-> AddActor(glyphActor);
renWin->Render();
//points-> Delete();
profile-> Delete();
sphere-> Delete();
glyph-> Delete();
glyphMapper-> Delete();
glyphActor-> Delete();
}
else if ( pointpicker->GetPointId() < 0 )
{
cout<<"No point picked here!"<<"\n";
n=n+1;
cout<<"n" <<n<<"\n";
if (n >=3)
{
cout<<"Selection completed!"<<"\n";
// check the picked points, stored in the array of pointers pointsArray
for (i=0; i<p; i++)
{
cout<<"Point:"<<pointsArray[i][0]<<" "<<pointsArray[i][1]<<"
"<<pointsArray[i][2]<<"\n"; //Output makes no sense
}
}
}
}
More information about the vtkusers
mailing list