[vtkusers] Pick point on the surface

de Boer Ingo I.deBoer at polytec.de
Tue Feb 10 04:34:13 EST 2004


Hi,

> The problem I have now is that the poit return by vtkPointPcker always shift
> from the mouse pointer and sometimes the point is not on the surface.
> Does any one of you have this kind of problem? Did I do something wrong?
I have/had the same problem. I wrote a small sample where it works, but I
have still the problem in my main application but haven't figured out why yet.

Maybe this sample will help. Please let me know when you found out more on
that one.

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 "vtkActor.h"
#include "vtkCamera.h"
#include "vtkCellArray.h"
#include "vtkFloatArray.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkWin32RenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkBMPReader.h"
#include "vtkImageData.h"
#include "vtkOpenGLTexture.h"
#include "vtkPolyDataWriter.h"
#include "vtkCommand.h"
#include "vtkPointPicker.h"
#include "vtkSphereSource.h"
#include "vtkProperty.h"

static vtkActor *sphereActor;

class vtkMyCallback : public vtkCommand
{
public:
  static vtkMyCallback *New() 
    { return new vtkMyCallback; }
  void Delete()
    { delete this; }
  void PrintSelf(ostream& os, vtkIndent indent) { }
  void PrintTrailer(ostream& os, vtkIndent indent) { }
  void PrintHeader(ostream& os, vtkIndent indent) { }
  void CollectRevisions(ostream& os) {}

  virtual void Execute(vtkObject *caller, unsigned long, void*)
    {
      vtkRenderWindowInteractor *iren = reinterpret_cast<vtkRenderWindowInteractor*>(caller);
      vtkPointPicker *picker = (vtkPointPicker *)iren->GetPicker();
      cout << "PointId: " << picker->GetPointId() << "\n";
      if (picker->GetPointId() != -1)
        sphereActor->SetPosition(picker->GetPickPosition());
    }
};


int main( int argc, char *argv[] )
{
    // We'll create the building blocks of polydata including data attributes.
    vtkPolyData *cube = vtkPolyData::New();
    vtkPoints *points = vtkPoints::New();
    vtkCellArray *polys = vtkCellArray::New();

    float x0[3] = { 0.467102, 0.966564, 0.000000 }; points->InsertPoint(0,x0);
    float x1[3] = { 0.467102, 0.933231, 0.010000 }; points->InsertPoint(1,x1);
    float x2[3] = { 0.467102, 0.899898, 0.010000 }; points->InsertPoint(2,x2);
    float x3[3] = { 0.467102, 0.866564, -0.010000 }; points->InsertPoint(3,x3);
    float x4[3] = { 0.467102, 0.833231, -0.010000 }; points->InsertPoint(4,x4);
    float x5[3] = { 0.467102, 0.799898, 0.010000 }; points->InsertPoint(5,x5);
    float x6[3] = { 0.467102, 0.766564, 0.010000 }; points->InsertPoint(6,x6);
    float x7[3] = { 0.467102, 0.733231, -0.010000 }; points->InsertPoint(7,x7);
    float x8[3] = { 0.467102, 0.699898, -0.010000 }; points->InsertPoint(8,x8);
    float x9[3] = { 0.467102, 0.666564, 0.000000 }; points->InsertPoint(9,x9);
    float x10[3] = { 0.467102, 0.633231, 0.010000 }; points->InsertPoint(10,x10);
    float x47[3] = { 0.500435, 0.633231, 0.010000 }; points->InsertPoint(47,x47);
    float x48[3] = { 0.500435, 0.666564, -0.010000 }; points->InsertPoint(48,x48);
    float x49[3] = { 0.500435, 0.699898, -0.010000 }; points->InsertPoint(49,x49);
    float x50[3] = { 0.500435, 0.733231, 0.010000 }; points->InsertPoint(50,x50);
    float x51[3] = { 0.500435, 0.766564, 0.010000 }; points->InsertPoint(51,x51);
    float x52[3] = { 0.500435, 0.799898, -0.010000 }; points->InsertPoint(52,x52);
    float x53[3] = { 0.500435, 0.833231, -0.010000 }; points->InsertPoint(53,x53);
    float x54[3] = { 0.500435, 0.866564, 0.010000 }; points->InsertPoint(54,x54);
    float x55[3] = { 0.500435, 0.899898, 0.010000 }; points->InsertPoint(55,x55);
    float x56[3] = { 0.500435, 0.933231, -0.010000 }; points->InsertPoint(56,x56);
    float x57[3] = { 0.500435, 0.966564, -0.010000 }; points->InsertPoint(57,x57);

    vtkIdType p0[4] = {0, 57, 56, 1 }; polys->InsertNextCell(4, p0);
    vtkIdType p1[4] = {1, 56, 55, 2 }; polys->InsertNextCell(4, p1);
    vtkIdType p2[4] = {2, 55, 54, 3 }; polys->InsertNextCell(4, p2);
    vtkIdType p3[4] = {3, 54, 53, 4 }; polys->InsertNextCell(4, p3);
    vtkIdType p4[4] = {4, 53, 52, 5 }; polys->InsertNextCell(4, p4);
    vtkIdType p5[4] = {5, 52, 51, 6 }; polys->InsertNextCell(4, p5);
    vtkIdType p6[4] = {6, 51, 50, 7 }; polys->InsertNextCell(4, p6);
    vtkIdType p7[4] = {7, 50, 49, 8 }; polys->InsertNextCell(4, p7);
    vtkIdType p8[4] = {8, 49, 48, 9 }; polys->InsertNextCell(4, p8);
    vtkIdType p9[4] = {9, 48, 47, 10 }; polys->InsertNextCell(4, p9);

    // We now assign the pieces to the vtkPolyData.
    cube->SetPoints(points);
    points->Delete();
    cube->SetPolys(polys);
    polys->Delete();

    // Now we'll look at it.
    vtkPolyDataMapper *cubeMapper = vtkPolyDataMapper::New();
    cubeMapper->SetInput(cube);
    vtkActor *cubeActor = vtkActor::New();
    cubeActor->SetMapper(cubeMapper);

    // 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);

    // create marker for pick
    vtkSphereSource *sphere = vtkSphereSource::New();
    sphere->SetThetaResolution(8); sphere->SetPhiResolution(8);
    sphere->SetRadius(0.01);
    vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
    sphereMapper->SetInput(sphere->GetOutput());
    sphereActor = vtkActor::New();
    sphereActor->SetMapper(sphereMapper);
    sphereActor->GetProperty()->SetColor(1,0,0);
    sphereActor->PickableOff();
    sphereActor->SetPosition(x0);
    renderer->AddActor(sphereActor);

    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
    iren->SetRenderWindow(renWin);

    // init picker
    vtkPointPicker *picker = vtkPointPicker::New();
    picker->SetTolerance(0.01);
    iren->SetPicker(picker);

    // init callback
    vtkMyCallback *callback = vtkMyCallback::New();
    iren->AddObserver(vtkCommand::EndPickEvent, callback);

    renderer->AddActor(cubeActor);
    renderer->SetActiveCamera(camera);
    renderer->ResetCamera();
    renderer->SetBackground(1,1,1);

    renWin->SetSize(300,300);

    // interact with data
    renWin->Render();
    iren->Start();

    // Clean up
    sphereActor->Delete();
    cube->Delete();
    cubeMapper->Delete();
    cubeActor->Delete();
    camera->Delete();
    renderer->Delete();
    renWin->Delete();
    iren->Delete();

    return 0;
}




More information about the vtkusers mailing list