1 // Created on: 2011-10-20
2 // Created by: Roman KOZLOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <IVtkOCC_ViewerSelector.hxx>
17 #include <Select3D_SensitiveBox.hxx>
18 #include <TColgp_Array1OfPnt2d.hxx>
19 #include <gp_Quaternion.hxx>
20 #include <Graphic3d_Camera.hxx>
23 IMPLEMENT_STANDARD_RTTIEXT(IVtkOCC_ViewerSelector,SelectMgr_ViewerSelector)
25 //============================================================================
26 // Method: Constructor
28 //============================================================================
29 IVtkOCC_ViewerSelector::IVtkOCC_ViewerSelector()
30 : SelectMgr_ViewerSelector(),
32 myToUpdateTol(Standard_True) {}
34 //============================================================================
36 // Purpose: Implements point picking
37 //============================================================================
38 void IVtkOCC_ViewerSelector::Pick (const Standard_Integer theXPix,
39 const Standard_Integer theYPix,
40 const IVtk_IView::Handle& theView)
44 // Compute and set a sensitivity tolerance according to the renderer (viewport).
45 // TODO: Think if this works well in perspective view...'cause result depends
46 // on position on the screen, but we always use the point close to the
48 mySelectingVolumeMgr.SetPixelTolerance (myPixTol);
50 myToUpdateTol = Standard_False;
53 Standard_Integer aWidth = 0, aHeight = 0;
54 Graphic3d_Mat4d aProj, anOrient;
55 Standard_Boolean isOrthographic = Standard_False;
56 Standard_Real aX = RealLast(), aY = RealLast();
57 Standard_Real aVpWidth = RealLast(), aVpHeight = RealLast();
59 mySelectingVolumeMgr.SetActiveSelectionType (SelectMgr_SelectingVolumeManager::Point);
60 theView->GetCamera (aProj, anOrient, isOrthographic);
61 mySelectingVolumeMgr.SetCamera (aProj, anOrient, isOrthographic);
63 theView->GetWindowSize (aWidth, aHeight);
64 mySelectingVolumeMgr.SetWindowSize (aWidth, aHeight);
66 theView->GetViewport (aX, aY, aVpWidth, aVpHeight);
67 mySelectingVolumeMgr.SetViewport (aX, aY, aVpWidth, aVpHeight);
69 gp_Pnt2d aMousePos (static_cast<Standard_Real> (theXPix),
70 static_cast<Standard_Real> (theYPix));
71 mySelectingVolumeMgr.BuildSelectingVolume (aMousePos);
76 //============================================================================
78 // Purpose: Picking by rectangle
79 //============================================================================
80 void IVtkOCC_ViewerSelector::Pick (const Standard_Integer theXMin,
81 const Standard_Integer theYMin,
82 const Standard_Integer theXMax,
83 const Standard_Integer theYMax,
84 const IVtk_IView::Handle& theView)
88 // Compute and set a sensitivity tolerance according to the renderer (viewport).
89 // TODO: Think if this works well in perspective view...'cause result depends
90 // on position on the screen, but we always use the point close to the
92 mySelectingVolumeMgr.SetPixelTolerance (myPixTol);
94 myToUpdateTol = Standard_False;
97 Standard_Integer aWidth = 0, aHeight = 0;
98 Graphic3d_Mat4d aProj, anOrient;
99 Standard_Boolean isOrthographic = Standard_False;
100 Standard_Real aX = RealLast(), aY = RealLast();
101 Standard_Real aVpWidth = RealLast(), aVpHeight = RealLast();
103 mySelectingVolumeMgr.SetActiveSelectionType (SelectMgr_SelectingVolumeManager::Box);
104 theView->GetCamera (aProj, anOrient, isOrthographic);
105 mySelectingVolumeMgr.SetCamera (aProj, anOrient, isOrthographic);
107 theView->GetWindowSize (aWidth, aHeight);
108 mySelectingVolumeMgr.SetWindowSize (aWidth, aHeight);
110 theView->GetViewport (aX, aY, aVpWidth, aVpHeight);
111 mySelectingVolumeMgr.SetViewport (aX, aY, aVpWidth, aVpHeight);
113 gp_Pnt2d aMinMousePos (static_cast<Standard_Real> (theXMin),
114 static_cast<Standard_Real> (theYMin));
115 gp_Pnt2d aMaxMousePos (static_cast<Standard_Real> (theXMax),
116 static_cast<Standard_Real> (theYMax));
118 mySelectingVolumeMgr.BuildSelectingVolume (aMinMousePos,
121 TraverseSensitives();
124 //============================================================================
127 //============================================================================
128 void IVtkOCC_ViewerSelector::Pick (double** thePoly,
129 const int theNbPoints,
130 const IVtk_IView::Handle& theView)
132 TColgp_Array1OfPnt2d aPolyline (1, theNbPoints);
136 // Compute and set a sensitivity tolerance according to the renderer (viewport).
137 // TODO: Think if this works well in perspective view...'cause result depends
138 // on position on the screen, but we always use the point close to the
139 // screen's origin...
140 mySelectingVolumeMgr.SetPixelTolerance (myPixTol);
142 myToUpdateTol = Standard_False;
145 // Build TColgp_Array1OfPnt2d from input array of doubles
148 for (Standard_Integer anIt = 0; anIt < theNbPoints; anIt++)
150 gp_XY aDispPnt = thePoly[anIt][2] != 0 ? gp_XY (thePoly[anIt][0] / thePoly[anIt][2], thePoly[anIt][1] / thePoly[anIt][2])
151 : gp_XY (thePoly[anIt][0], thePoly[anIt][1]);
152 aPolyline.SetValue (anIt + 1, aDispPnt);
155 Standard_Integer aWidth = 0, aHeight = 0;
156 Graphic3d_Mat4d aProj, anOrient;
157 Standard_Boolean isOrthographic = Standard_False;
158 Standard_Real aX = RealLast(), aY = RealLast();
159 Standard_Real aVpWidth = RealLast(), aVpHeight = RealLast();
161 mySelectingVolumeMgr.SetActiveSelectionType (SelectMgr_SelectingVolumeManager::Polyline);
162 theView->GetCamera (aProj, anOrient, isOrthographic);
163 mySelectingVolumeMgr.SetCamera (aProj, anOrient, isOrthographic);
165 theView->GetWindowSize (aWidth, aHeight);
166 mySelectingVolumeMgr.SetWindowSize (aWidth, aHeight);
168 theView->GetViewport (aX, aY, aVpWidth, aVpHeight);
169 mySelectingVolumeMgr.SetViewport (aX, aY, aVpWidth, aVpHeight);
171 mySelectingVolumeMgr.BuildSelectingVolume (aPolyline);
173 TraverseSensitives();
176 //============================================================================
178 // Purpose: Activates the given selection
179 //============================================================================
180 void IVtkOCC_ViewerSelector::Activate (const Handle(SelectMgr_Selection)& theSelection)
182 for (theSelection->Init(); theSelection->More(); theSelection->Next())
184 theSelection->Sensitive()->SetActiveForSelection();
187 theSelection->SetSelectionState (SelectMgr_SOS_Activated);
189 myTolerances.Add (theSelection->Sensitivity());
190 myToUpdateTolerance = Standard_True;
193 //============================================================================
194 // Method: Deactivate
195 // Purpose: Deactivate the given selection
196 //============================================================================
197 void IVtkOCC_ViewerSelector::Deactivate (const Handle(SelectMgr_Selection)& theSelection)
199 for (theSelection->Init(); theSelection->More(); theSelection->Next())
201 theSelection->Sensitive()->ResetSelectionActiveStatus();
204 theSelection->SetSelectionState (SelectMgr_SOS_Deactivated);
206 myTolerances.Decrement (theSelection->Sensitivity());
207 myToUpdateTolerance = Standard_True;