VTK学习笔记(十一)高亮拾取点

最基础的物体还是点, 看看如何给点进行拾取并高亮。 想要拾取,先要激活窗口,然后按下字母'r',然后用鼠标进行选择,选择后就输出结果。如图所示。

VTK学习笔记(十一)高亮拾取点_第1张图片

 代码如下

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#if VTK_VERSION_NUMBER >= 89000000000ULL
#define VTK890 1
#endif

#include 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

namespace {
// Define interaction style
class InteractorStyle : public vtkInteractorStyleRubberBandPick
{
public:
  static InteractorStyle* New();
  vtkTypeMacro(InteractorStyle, vtkInteractorStyleRubberBandPick);

  InteractorStyle()
  {
    this->SelectedMapper = vtkSmartPointer::New();
    this->SelectedActor = vtkSmartPointer::New();
    this->SelectedActor->SetMapper(SelectedMapper);
  }

  virtual void OnLeftButtonUp() override
  {
    vtkNew colors;


    // Forward events
    vtkInteractorStyleRubberBandPick::OnLeftButtonUp();


    vtkPlanes* frustum =
        static_cast(this->GetInteractor()->GetPicker())
            ->GetFrustum();


    vtkNew extractGeometry;
    extractGeometry->SetImplicitFunction(frustum);
    extractGeometry->SetInputData(this->Points);
    extractGeometry->Update();


    vtkNew glyphFilter;
    glyphFilter->SetInputConnection(extractGeometry->GetOutputPort());
    glyphFilter->Update();


    vtkPolyData* selected = glyphFilter->GetOutput();
    std::cout << "Selected " << selected->GetNumberOfPoints() << " points."
              << std::endl;
    std::cout << "Selected " << selected->GetNumberOfCells() << " cells."
              << std::endl;
    this->SelectedMapper->SetInputData(selected);
    this->SelectedMapper->ScalarVisibilityOff();


    vtkIdTypeArray* ids = dynamic_cast(
        selected->GetPointData()->GetArray("OriginalIds"));
    for (vtkIdType i = 0; i < ids->GetNumberOfTuples(); i++)
    {
      std::cout << "Id " << i << " : " << ids->GetValue(i) << std::endl;
    }


    this->SelectedActor->GetProperty()->SetColor(
        colors->GetColor3d("Red").GetData());
    this->SelectedActor->GetProperty()->SetPointSize(5);


    this->CurrentRenderer->AddActor(SelectedActor);
    this->GetInteractor()->GetRenderWindow()->Render();
    this->HighlightProp(NULL);
  }


  void SetPoints(vtkSmartPointer points)
  {
    this->Points = points;
  }


private:
  vtkSmartPointer Points;
  vtkSmartPointer SelectedActor;
  vtkSmartPointer SelectedMapper;
};


vtkStandardNewMacro(InteractorStyle);
} // namespace


int main(int, char*[])
{
  vtkNew colors;


  vtkNew pointSource;
  pointSource->SetNumberOfPoints(20);
  pointSource->Update();


  vtkNew idFilter;
  idFilter->SetInputConnection(pointSource->GetOutputPort());
#if VTK890
  idFilter->SetCellIdsArrayName("OriginalIds");
  idFilter->SetPointIdsArrayName("OriginalIds");
#else
  idFilter->SetIdsArrayName("OriginalIds");
#endif
  idFilter->Update();


  vtkNew surfaceFilter;
  surfaceFilter->SetInputConnection(idFilter->GetOutputPort());
  surfaceFilter->Update();


  vtkPolyData* input = surfaceFilter->GetOutput();


  // Create a mapper and actor
  vtkNew mapper;
  mapper->SetInputData(input);
  mapper->ScalarVisibilityOff();


  vtkNew actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetPointSize(3);
  actor->GetProperty()->SetColor(colors->GetColor3d("Gold").GetData());


  // Visualize
  vtkNew renderer;
  vtkNew renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("HighlightSelectedPoints");


  vtkNew areaPicker;
  vtkNew renderWindowInteractor;
  renderWindowInteractor->SetPicker(areaPicker);
  renderWindowInteractor->SetRenderWindow(renderWindow);


  renderer->AddActor(actor);
  renderer->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());


  renderWindow->Render();


  vtkNew style;
  style->SetPoints(input);
  renderWindowInteractor->SetInteractorStyle(style);


  renderWindowInteractor->Start();


  return EXIT_SUCCESS;
}

你可能感兴趣的:(Qt,VTK,c++)