1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkRenderedAreaPicker.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
16 
17 #include "vtkRenderedAreaPicker.h"
18 #include "vtkObjectFactory.h"
19 #include "vtkMapper.h"
20 #include "vtkAbstractVolumeMapper.h"
21 #include "vtkImageMapper3D.h"
22 #include "vtkAbstractMapper3D.h"
23 #include "vtkProp.h"
24 #include "vtkLODProp3D.h"
25 #include "vtkActor.h"
26 #include "vtkPropCollection.h"
27 #include "vtkProp3DCollection.h"
28 #include "vtkAssemblyPath.h"
29 #include "vtkVolume.h"
30 #include "vtkRenderer.h"
31 #include "vtkProperty.h"
32 #include "vtkCommand.h"
33 #include "vtkPlanes.h"
34 #include "vtkPlane.h"
35 #include "vtkPoints.h"
36 
37 vtkStandardNewMacro(vtkRenderedAreaPicker);
38 
39 //--------------------------------------------------------------------------
40 vtkRenderedAreaPicker::vtkRenderedAreaPicker() = default;
41 
42 //--------------------------------------------------------------------------
43 vtkRenderedAreaPicker::~vtkRenderedAreaPicker() = default;
44 
45 //--------------------------------------------------------------------------
46 // Does what this class is meant to do.
AreaPick(double x0,double y0,double x1,double y1,vtkRenderer * renderer)47 int vtkRenderedAreaPicker::AreaPick(double x0, double y0, double x1, double y1,
48                                     vtkRenderer *renderer)
49 {
50   int picked = 0;
51   vtkProp *propCandidate;
52   vtkAbstractMapper3D *mapper = nullptr;
53   int pickable;
54 
55   //  Initialize picking process
56   this->Initialize();
57   this->Renderer = renderer;
58 
59   this->SelectionPoint[0] = (x0+x1)*0.5;
60   this->SelectionPoint[1] = (y0+y1)*0.5;
61   this->SelectionPoint[2] = 0.0;
62 
63   // Invoke start pick method if defined
64   this->InvokeEvent(vtkCommand::StartPickEvent,nullptr);
65 
66   this->DefineFrustum(x0, y0, x1, y1, renderer);
67 
68   // Ask the renderer do the hardware pick
69   vtkPropCollection* pickList = nullptr;
70   if(this->PickFromList)
71   {
72     pickList = this->PickList;
73   }
74 
75   this->SetPath(renderer->PickPropFrom(x0, y0, x1, y1,pickList));
76 
77   // Software pick resulted in a hit.
78   if ( this->Path )
79   {
80     picked = 1;
81 
82     //invoke the pick event
83     propCandidate = this->Path->GetLastNode()->GetViewProp();
84 
85     //find the mapper and dataset corresponding to the picked prop
86     pickable = this->TypeDecipher(propCandidate, &mapper);
87     if ( pickable )
88     {
89       if ( mapper )
90       {
91         this->Mapper = mapper;
92         vtkMapper *map1;
93         vtkAbstractVolumeMapper *vmap;
94         vtkImageMapper3D *imap;
95         if ( (map1=vtkMapper::SafeDownCast(mapper)) != nullptr )
96         {
97           this->DataSet = map1->GetInput();
98           this->Mapper = map1;
99         }
100         else if ( (vmap=vtkAbstractVolumeMapper::SafeDownCast(mapper)) != nullptr )
101         {
102           this->DataSet = vmap->GetDataSetInput();
103           this->Mapper = vmap;
104         }
105         else if ( (imap=vtkImageMapper3D::SafeDownCast(mapper)) != nullptr )
106         {
107           this->DataSet = imap->GetDataSetInput();
108           this->Mapper = imap;
109         }
110         else
111         {
112           this->DataSet = nullptr;
113         }
114       }//mapper
115     }//pickable
116 
117     //go through list of props the renderer got for us and put only
118     //the prop3Ds into this->Prop3Ds
119     vtkPropCollection *pProps = renderer->GetPickResultProps();
120     pProps->InitTraversal();
121 
122     vtkProp *prop;
123     vtkAssemblyPath *path;
124     while ((prop = pProps->GetNextProp()))
125     {
126       for ( prop->InitPathTraversal(); (path=prop->GetNextPath()); )
127       {
128         propCandidate = path->GetLastNode()->GetViewProp();
129         pickable = this->TypeDecipher(propCandidate, &mapper);
130         if ( pickable && !this->Prop3Ds->IsItemPresent(prop) )
131         {
132           this->Prop3Ds->AddItem(static_cast<vtkProp3D *>(prop));
133         }
134       }
135     }
136 
137     // Invoke pick method if one defined - prop goes first
138     this->Path->GetFirstNode()->GetViewProp()->Pick();
139     this->InvokeEvent(vtkCommand::PickEvent,nullptr);
140   }
141 
142   this->InvokeEvent(vtkCommand::EndPickEvent,nullptr);
143 
144   return picked;
145 }
146 
147 //----------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)148 void vtkRenderedAreaPicker::PrintSelf(ostream& os, vtkIndent indent)
149 {
150   this->Superclass::PrintSelf(os,indent);
151 }
152