Difference between revisions of "VTK/Examples/Cxx/Picking/AreaPicking"

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to: navigation, search
(Match cxx and page name)
(Replaced content with "= '''See [https://lorensen.github.io/VTKExamples/site/Cxx/Picking/AreaPicking AreaPicking] on the new [https://lorensen.github.io/VTKExamples/site/ VTKExamples website].''' =")
 
Line 1: Line 1:
<div class="floatright">[[File:VTK_Examples_Baseline_Picking_TestAreaPicker.png]]</div>
+
= '''See [https://lorensen.github.io/VTKExamples/site/Cxx/Picking/AreaPicking AreaPicking] on the new [https://lorensen.github.io/VTKExamples/site/ VTKExamples website].''' =
This example creates 3 points+vertices. Currently, the address of the picked prop is 0 (this is not correct). A red bounding box is drawn around every picked prop.
 
 
 
- For '''vtkInteractorStyleTrackballCamera''' - use 'p' to pick at the current mouse position
 
 
 
- For '''vtkInteractorStyleRubberBandPick''' - use 'r' and left-mouse to draw a selection box used to pick
 
 
 
==AreaPicking.cxx==
 
<source lang="cpp">
 
#include <vtkVersion.h>
 
#include <vtkSmartPointer.h>
 
#include <vtkPoints.h>
 
#include <vtkXMLPolyDataWriter.h>
 
#include <vtkPolyData.h>
 
#include <vtkCellArray.h>
 
#include <vtkPolyDataMapper.h>
 
#include <vtkActor.h>
 
#include <vtkRenderWindow.h>
 
#include <vtkRenderer.h>
 
#include <vtkRenderWindowInteractor.h>
 
#include <vtkInteractorStyleRubberBandPick.h>
 
#include <vtkInteractorStyleTrackballCamera.h>
 
#include <vtkInteractorStyleTrackball.h>
 
#include <vtkAreaPicker.h>
 
#include <vtkCallbackCommand.h>
 
#include <vtkProp3DCollection.h>
 
 
 
void PickCallbackFunction ( vtkObject* caller, long unsigned int eventId, void* clientData, void* callData );
 
 
int main(int, char *[])
 
{
 
  // Create a set of points
 
  vtkSmartPointer<vtkPoints> points =
 
    vtkSmartPointer<vtkPoints>::New();
 
  vtkSmartPointer<vtkCellArray> vertices =
 
    vtkSmartPointer<vtkCellArray>::New();
 
  vtkIdType pid[1];
 
  pid[0] = points->InsertNextPoint ( 1.0, 0.0, 0.0 );
 
  vertices->InsertNextCell ( 1,pid );
 
  pid[0] = points->InsertNextPoint ( 0.0, 0.0, 0.0 );
 
  vertices->InsertNextCell ( 1,pid );
 
  pid[0] = points->InsertNextPoint ( 0.0, 1.0, 0.0 );
 
  vertices->InsertNextCell ( 1,pid );
 
 
  // Create a polydata
 
  vtkSmartPointer<vtkPolyData> polydata =
 
    vtkSmartPointer<vtkPolyData>::New();
 
  polydata->SetPoints ( points );
 
  polydata->SetVerts ( vertices );
 
 
  // Visualize
 
  vtkSmartPointer<vtkPolyDataMapper> mapper =
 
    vtkSmartPointer<vtkPolyDataMapper>::New();
 
#if VTK_MAJOR_VERSION <= 5
 
  mapper->SetInput(polydata);
 
#else
 
  mapper->SetInputData(polydata);
 
#endif
 
  vtkSmartPointer<vtkActor> actor =
 
    vtkSmartPointer<vtkActor>::New();
 
  actor->SetMapper(mapper);
 
 
  vtkSmartPointer<vtkRenderer> renderer =
 
    vtkSmartPointer<vtkRenderer>::New();
 
  vtkSmartPointer<vtkRenderWindow> renderWindow =
 
    vtkSmartPointer<vtkRenderWindow>::New();
 
  renderWindow->AddRenderer(renderer);
 
 
  vtkSmartPointer<vtkAreaPicker> areaPicker =
 
    vtkSmartPointer<vtkAreaPicker>::New();
 
 
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
 
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
 
  renderWindowInteractor->SetRenderWindow(renderWindow);
 
  renderWindowInteractor->SetPicker(areaPicker);
 
 
 
  renderer->AddActor(actor);
 
  renderer->SetBackground(0,0,0.2); // Background color dark blue
 
 
  renderWindow->Render();
 
 
  // For vtkInteractorStyleRubberBandPick - use 'r' and left-mouse to draw a selection box used to pick
 
  //vtkSmartPointer<vtkInteractorStyleRubberBandPick> style =
 
    //vtkSmartPointer<vtkInteractorStyleRubberBandPick>::New();
 
 
 
  // For vtkInteractorStyleTrackballCamera - use 'p' to pick at the current mouse position
 
  vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
 
    vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New(); //like paraview
 
  style->SetCurrentRenderer(renderer);
 
  renderWindowInteractor->SetInteractorStyle( style );
 
 
  vtkSmartPointer<vtkCallbackCommand> pickCallback =
 
    vtkSmartPointer<vtkCallbackCommand>::New();
 
  pickCallback->SetCallback ( PickCallbackFunction );
 
 
  areaPicker->AddObserver ( vtkCommand::EndPickEvent, pickCallback );
 
 
  renderWindowInteractor->Start();
 
 
  return EXIT_SUCCESS;
 
}
 
 
 
void PickCallbackFunction(vtkObject* caller,
 
                          long unsigned int vtkNotUsed(eventId),
 
                          void* vtkNotUsed(clientData),
 
                          void* vtkNotUsed(callData))
 
{
 
  std::cout << "Pick." << std::endl;
 
  vtkAreaPicker* areaPicker = static_cast<vtkAreaPicker*>(caller);
 
  vtkProp3DCollection* props = areaPicker->GetProp3Ds();
 
 
 
  for(vtkIdType i = 0; i < props->GetNumberOfItems(); i++)
 
    {
 
    vtkProp3D* prop = props->GetNextProp3D();
 
    std::cout << "Picked prop: " << prop << std::endl;
 
    }
 
}
 
</source>
 
 
 
{{VTKCMakeLists|{{SUBPAGENAME}}}}
 
 
 
Display the IDs of the points that are inside your selection.
 
[[Category:VTKMakeMoreInteresting]]
 

Latest revision as of 23:30, 19 February 2019

See AreaPicking on the new VTKExamples website.