0022048: Visualization, AIS_InteractiveContext - single object selection should alway...
[occt.git] / src / IVtkOCC / IVtkOCC_ViewerSelector.cxx
CommitLineData
913a4c4a 1// Created on: 2011-10-20
2// Created by: Roman KOZLOV
3// Copyright (c) 2011-2014 OPEN CASCADE SAS
4//
5// This file is part of Open CASCADE Technology software library.
6//
7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
12//
13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
15
16#include <IVtkOCC_ViewerSelector.hxx>
17#include <Select3D_SensitiveBox.hxx>
18#include <TColgp_Array1OfPnt2d.hxx>
913a4c4a 19#include <Graphic3d_Camera.hxx>
20
913a4c4a 21
92efcf78 22IMPLEMENT_STANDARD_RTTIEXT(IVtkOCC_ViewerSelector,SelectMgr_ViewerSelector)
23
913a4c4a 24//============================================================================
25// Method: Constructor
26// Purpose:
27//============================================================================
28IVtkOCC_ViewerSelector::IVtkOCC_ViewerSelector()
29: SelectMgr_ViewerSelector(),
30myPixTol(2),
a2f76b15 31myToUpdateTol(Standard_True)
32{
33}
34
35//============================================================================
36// Method: Destructor
37// Purpose:
38//============================================================================
39IVtkOCC_ViewerSelector::~IVtkOCC_ViewerSelector()
40{
41}
913a4c4a 42
43//============================================================================
44// Method: Pick
45// Purpose: Implements point picking
46//============================================================================
47void IVtkOCC_ViewerSelector::Pick (const Standard_Integer theXPix,
48 const Standard_Integer theYPix,
49 const IVtk_IView::Handle& theView)
50{
f751596e 51 if (myToUpdateTol)
52 {
53 // Compute and set a sensitivity tolerance according to the renderer (viewport).
54 // TODO: Think if this works well in perspective view...'cause result depends
55 // on position on the screen, but we always use the point close to the
56 // screen's origin...
57 mySelectingVolumeMgr.SetPixelTolerance (myPixTol);
58
59 myToUpdateTol = Standard_False;
60 }
61
62 Standard_Integer aWidth = 0, aHeight = 0;
63 Graphic3d_Mat4d aProj, anOrient;
64 Standard_Boolean isOrthographic = Standard_False;
65 Standard_Real aX = RealLast(), aY = RealLast();
66 Standard_Real aVpWidth = RealLast(), aVpHeight = RealLast();
67
68 mySelectingVolumeMgr.SetActiveSelectionType (SelectMgr_SelectingVolumeManager::Point);
69 theView->GetCamera (aProj, anOrient, isOrthographic);
70 mySelectingVolumeMgr.SetCamera (aProj, anOrient, isOrthographic);
71
72 theView->GetWindowSize (aWidth, aHeight);
73 mySelectingVolumeMgr.SetWindowSize (aWidth, aHeight);
74
75 theView->GetViewport (aX, aY, aVpWidth, aVpHeight);
76 mySelectingVolumeMgr.SetViewport (aX, aY, aVpWidth, aVpHeight);
77
78 gp_Pnt2d aMousePos (static_cast<Standard_Real> (theXPix),
79 static_cast<Standard_Real> (theYPix));
80 mySelectingVolumeMgr.BuildSelectingVolume (aMousePos);
81
82 TraverseSensitives();
913a4c4a 83}
84
85//============================================================================
86// Method: Pick
87// Purpose: Picking by rectangle
88//============================================================================
89void IVtkOCC_ViewerSelector::Pick (const Standard_Integer theXMin,
90 const Standard_Integer theYMin,
91 const Standard_Integer theXMax,
92 const Standard_Integer theYMax,
93 const IVtk_IView::Handle& theView)
94{
95 if (myToUpdateTol)
96 {
97 // Compute and set a sensitivity tolerance according to the renderer (viewport).
98 // TODO: Think if this works well in perspective view...'cause result depends
99 // on position on the screen, but we always use the point close to the
100 // screen's origin...
f751596e 101 mySelectingVolumeMgr.SetPixelTolerance (myPixTol);
102
913a4c4a 103 myToUpdateTol = Standard_False;
104 }
913a4c4a 105
f751596e 106 Standard_Integer aWidth = 0, aHeight = 0;
107 Graphic3d_Mat4d aProj, anOrient;
108 Standard_Boolean isOrthographic = Standard_False;
109 Standard_Real aX = RealLast(), aY = RealLast();
110 Standard_Real aVpWidth = RealLast(), aVpHeight = RealLast();
111
112 mySelectingVolumeMgr.SetActiveSelectionType (SelectMgr_SelectingVolumeManager::Box);
113 theView->GetCamera (aProj, anOrient, isOrthographic);
114 mySelectingVolumeMgr.SetCamera (aProj, anOrient, isOrthographic);
913a4c4a 115
f751596e 116 theView->GetWindowSize (aWidth, aHeight);
117 mySelectingVolumeMgr.SetWindowSize (aWidth, aHeight);
913a4c4a 118
f751596e 119 theView->GetViewport (aX, aY, aVpWidth, aVpHeight);
120 mySelectingVolumeMgr.SetViewport (aX, aY, aVpWidth, aVpHeight);
913a4c4a 121
f751596e 122 gp_Pnt2d aMinMousePos (static_cast<Standard_Real> (theXMin),
123 static_cast<Standard_Real> (theYMin));
124 gp_Pnt2d aMaxMousePos (static_cast<Standard_Real> (theXMax),
125 static_cast<Standard_Real> (theYMax));
126
127 mySelectingVolumeMgr.BuildSelectingVolume (aMinMousePos,
128 aMaxMousePos);
129
130 TraverseSensitives();
913a4c4a 131}
132
133//============================================================================
134// Method: Pick
135// Purpose:
136//============================================================================
137void IVtkOCC_ViewerSelector::Pick (double** thePoly,
138 const int theNbPoints,
139 const IVtk_IView::Handle& theView)
140{
141 TColgp_Array1OfPnt2d aPolyline (1, theNbPoints);
142
143 if (myToUpdateTol)
144 {
145 // Compute and set a sensitivity tolerance according to the renderer (viewport).
146 // TODO: Think if this works well in perspective view...'cause result depends
147 // on position on the screen, but we always use the point close to the
148 // screen's origin...
f751596e 149 mySelectingVolumeMgr.SetPixelTolerance (myPixTol);
150
913a4c4a 151 myToUpdateTol = Standard_False;
152 }
153
913a4c4a 154 // Build TColgp_Array1OfPnt2d from input array of doubles
155 gp_XYZ aWorldPnt;
156
157 for (Standard_Integer anIt = 0; anIt < theNbPoints; anIt++)
158 {
159 gp_XY aDispPnt = thePoly[anIt][2] != 0 ? gp_XY (thePoly[anIt][0] / thePoly[anIt][2], thePoly[anIt][1] / thePoly[anIt][2])
160 : gp_XY (thePoly[anIt][0], thePoly[anIt][1]);
f751596e 161 aPolyline.SetValue (anIt + 1, aDispPnt);
913a4c4a 162 }
163
f751596e 164 Standard_Integer aWidth = 0, aHeight = 0;
165 Graphic3d_Mat4d aProj, anOrient;
166 Standard_Boolean isOrthographic = Standard_False;
167 Standard_Real aX = RealLast(), aY = RealLast();
168 Standard_Real aVpWidth = RealLast(), aVpHeight = RealLast();
913a4c4a 169
f751596e 170 mySelectingVolumeMgr.SetActiveSelectionType (SelectMgr_SelectingVolumeManager::Polyline);
171 theView->GetCamera (aProj, anOrient, isOrthographic);
172 mySelectingVolumeMgr.SetCamera (aProj, anOrient, isOrthographic);
913a4c4a 173
f751596e 174 theView->GetWindowSize (aWidth, aHeight);
175 mySelectingVolumeMgr.SetWindowSize (aWidth, aHeight);
913a4c4a 176
f751596e 177 theView->GetViewport (aX, aY, aVpWidth, aVpHeight);
178 mySelectingVolumeMgr.SetViewport (aX, aY, aVpWidth, aVpHeight);
913a4c4a 179
f751596e 180 mySelectingVolumeMgr.BuildSelectingVolume (aPolyline);
913a4c4a 181
f751596e 182 TraverseSensitives();
913a4c4a 183}
184
185//============================================================================
186// Method: Activate
187// Purpose: Activates the given selection
188//============================================================================
f751596e 189void IVtkOCC_ViewerSelector::Activate (const Handle(SelectMgr_Selection)& theSelection)
913a4c4a 190{
f751596e 191 for (theSelection->Init(); theSelection->More(); theSelection->Next())
913a4c4a 192 {
f751596e 193 theSelection->Sensitive()->SetActiveForSelection();
913a4c4a 194 }
f751596e 195
196 theSelection->SetSelectionState (SelectMgr_SOS_Activated);
197
198 myTolerances.Add (theSelection->Sensitivity());
f751596e 199 myToUpdateTolerance = Standard_True;
913a4c4a 200}
201
202//============================================================================
203// Method: Deactivate
204// Purpose: Deactivate the given selection
205//============================================================================
206void IVtkOCC_ViewerSelector::Deactivate (const Handle(SelectMgr_Selection)& theSelection)
207{
f751596e 208 for (theSelection->Init(); theSelection->More(); theSelection->Next())
913a4c4a 209 {
f751596e 210 theSelection->Sensitive()->ResetSelectionActiveStatus();
913a4c4a 211 }
913a4c4a 212
f751596e 213 theSelection->SetSelectionState (SelectMgr_SOS_Deactivated);
214
215 myTolerances.Decrement (theSelection->Sensitivity());
f751596e 216 myToUpdateTolerance = Standard_True;
913a4c4a 217}