[vtkusers] Highlight Picked Point

kenichiro yoshimi rccm.kyoshimi at gmail.com
Thu Nov 15 06:57:19 EST 2018


Hi,

The following code highlights a picked point tentatively, although I
don't know if this is really a formal and correct way of using vtk.
-------
#include <vtkSmartPointer.h>
#include <vtkRendererCollection.h>
#include <vtkPointPicker.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkVertexGlyphFilter.h>
#include <vtkProperty.h>
#include <vtkActor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkObjectFactory.h>

// Define interaction style
class MouseInteractorStylePP : public vtkInteractorStyleTrackballCamera
{
  public:
    static MouseInteractorStylePP* New();
    vtkTypeMacro(MouseInteractorStylePP, vtkInteractorStyleTrackballCamera);

    MouseInteractorStylePP()
    {
      this->PointPicker = vtkSmartPointer<vtkPointPicker>::New();

      // Setup ghost glyph
      vtkSmartPointer<vtkPoints> points =
        vtkSmartPointer<vtkPoints>::New();
      points->InsertNextPoint(0,0,0);
      this->SelectedPolyData = vtkSmartPointer<vtkPolyData>::New();
      this->SelectedPolyData->SetPoints(points);
      this->SelectedGlyphFilter = vtkSmartPointer<vtkVertexGlyphFilter>::New();
#if VTK_MAJOR_VERSION <= 5
      this->SelectedGlyphFilter->SetInputConnection(
        this->SelectedPolyData->GetProducerPort());
#else
      this->SelectedGlyphFilter->SetInputData(this->SelectedPolyData);
#endif
      this->SelectedGlyphFilter->Update();

      this->SelectedMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
      this->SelectedMapper->SetInputConnection(this->SelectedGlyphFilter->GetOutputPort());

      this->SelectedActor = vtkSmartPointer<vtkActor>::New();
      this->SelectedActor->SetMapper(SelectedMapper);
      this->SelectedActor->VisibilityOff();
      this->SelectedActor->GetProperty()->SetPointSize(10);
      this->SelectedActor->GetProperty()->SetColor(1,0,0);
    }

    virtual void OnLeftButtonDown()
    {
      // Forward events
      vtkInteractorStyleTrackballCamera::OnLeftButtonDown();

      // Get the selected point
      int x = this->Interactor->GetEventPosition()[0];
      int y = this->Interactor->GetEventPosition()[1];
      this->FindPokedRenderer(x, y);

      std::cout << "Picking pixel: " <<
this->Interactor->GetEventPosition()[0] << " " <<
this->Interactor->GetEventPosition()[1] << std::endl;
      this->PointPicker->Pick(this->Interactor->GetEventPosition()[0],
          this->Interactor->GetEventPosition()[1],
          0,  // always zero.
          this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer());

      if (this->PointPicker->GetPointId() >= 0)
      {
        this->SelectedActor->VisibilityOn();
        this->HighlightProp(NULL);

        this->SelectedPoint = this->PointPicker->GetPointId();

        double p[3];
        this->Points->GetPoint(this->SelectedPoint, p);
        this->SelectedActor->SetPosition(p);

        this->GetCurrentRenderer()->AddActor(this->SelectedActor);
      }
    }

    void SetPoints(vtkSmartPointer<vtkPolyData> points) {this->Points = points;}
  private:
    vtkSmartPointer<vtkPolyData> Points;
    vtkSmartPointer<vtkPointPicker> PointPicker;
    vtkSmartPointer<vtkActor> SelectedActor;
    vtkSmartPointer<vtkPolyDataMapper> SelectedMapper;
    vtkSmartPointer<vtkPolyData> SelectedPolyData;
    vtkSmartPointer<vtkVertexGlyphFilter> SelectedGlyphFilter;

    vtkIdType SelectedPoint;
};

vtkStandardNewMacro(MouseInteractorStylePP);

int main(int, char *[])
{
  vtkSmartPointer<vtkSphereSource> sphereSource =
    vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->Update();

  vtkSmartPointer<vtkPointPicker> pointPicker =
    vtkSmartPointer<vtkPointPicker>::New();

  vtkSmartPointer<vtkPolyData> points =
    vtkSmartPointer<vtkPolyData>::New();
  points->SetPoints(sphereSource->GetOutput()->GetPoints());

  // Create a mapper and actor
  vtkSmartPointer<vtkPolyDataMapper> mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInputConnection(sphereSource->GetOutputPort());
  vtkSmartPointer<vtkActor> actor =
    vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);
  actor->GetProperty()->EdgeVisibilityOn();

  // Create a renderer, render window, and interactor
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetPicker(pointPicker);
  renderWindowInteractor->SetRenderWindow(renderWindow);

  vtkSmartPointer<MouseInteractorStylePP> style =
    vtkSmartPointer<MouseInteractorStylePP>::New();
  style->SetPoints(points);
  renderWindowInteractor->SetInteractorStyle( style );

  // Add the actor to the scene
  renderer->AddActor(actor);
  renderer->SetBackground(1,1,1); // Background color white

  // Render and interact
  renderWindow->Render();
  renderWindowInteractor->Start();
  return EXIT_SUCCESS;
}
-----
Hope that helps

2018年11月15日(木) 8:54 meakcey <meakcey at gmail.com>:
>
> Hi
> How can i extend Point Pick example to highlighted point pick. Something
> like combination of examples below:
> https://lorensen.github.io/VTKExamples/site/Cxx/Interaction/PointPicker/
> https://lorensen.github.io/VTKExamples/site/Cxx/Picking/HighlightSelectedPoints/
>
> Is there any direct way to reach point ids and properties and also setting
> properties by point picker?
> Thanks
>
>
>
> --
> Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>
> Search the list archives at: http://markmail.org/search/?q=vtkusers
>
> Follow this link to subscribe/unsubscribe:
> https://public.kitware.com/mailman/listinfo/vtkusers


More information about the vtkusers mailing list