0025574: gp_YawPitchRoll Euler Angle computation gives wrong results
[occt.git] / src / IVtkOCC / IVtkOCC_ViewerSelector.cxx
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>
19 #include <Graphic3d_Camera.hxx>
20
21
22 IMPLEMENT_STANDARD_RTTIEXT(IVtkOCC_ViewerSelector,SelectMgr_ViewerSelector)
23
24 //============================================================================
25 // Method:  Constructor
26 // Purpose:
27 //============================================================================
28 IVtkOCC_ViewerSelector::IVtkOCC_ViewerSelector()
29 : SelectMgr_ViewerSelector(),
30 myPixTol(2),
31 myToUpdateTol(Standard_True) {}
32
33 //============================================================================
34 // Method:  Pick
35 // Purpose: Implements point picking
36 //============================================================================
37 void IVtkOCC_ViewerSelector::Pick (const Standard_Integer theXPix,
38                                    const Standard_Integer theYPix,
39                                    const IVtk_IView::Handle&    theView)
40 {
41   if (myToUpdateTol)
42   {
43     // Compute and set a sensitivity tolerance according to the renderer (viewport).
44     // TODO: Think if this works well in perspective view...'cause result depends
45     // on position on the screen, but we always use the point close to the
46     // screen's origin...
47     mySelectingVolumeMgr.SetPixelTolerance (myPixTol);
48
49     myToUpdateTol = Standard_False;
50   }
51
52   Standard_Integer aWidth = 0, aHeight = 0;
53   Graphic3d_Mat4d aProj, anOrient;
54   Standard_Boolean isOrthographic = Standard_False;
55   Standard_Real aX = RealLast(), aY = RealLast();
56   Standard_Real aVpWidth = RealLast(), aVpHeight = RealLast();
57
58   mySelectingVolumeMgr.SetActiveSelectionType (SelectMgr_SelectingVolumeManager::Point);
59   theView->GetCamera (aProj, anOrient, isOrthographic);
60   mySelectingVolumeMgr.SetCamera (aProj, anOrient, isOrthographic);
61
62   theView->GetWindowSize (aWidth, aHeight);
63   mySelectingVolumeMgr.SetWindowSize (aWidth, aHeight);
64
65   theView->GetViewport (aX, aY, aVpWidth, aVpHeight);
66   mySelectingVolumeMgr.SetViewport (aX, aY, aVpWidth, aVpHeight);
67
68   gp_Pnt2d aMousePos (static_cast<Standard_Real> (theXPix),
69                       static_cast<Standard_Real> (theYPix));
70   mySelectingVolumeMgr.BuildSelectingVolume (aMousePos);
71
72   TraverseSensitives();
73 }
74
75 //============================================================================
76 // Method:  Pick
77 // Purpose: Picking by rectangle
78 //============================================================================
79 void IVtkOCC_ViewerSelector::Pick (const Standard_Integer    theXMin,
80                                    const Standard_Integer    theYMin,
81                                    const Standard_Integer    theXMax,
82                                    const Standard_Integer    theYMax,
83                                    const IVtk_IView::Handle& theView)
84 {
85   if (myToUpdateTol)
86   {
87     // Compute and set a sensitivity tolerance according to the renderer (viewport).
88     // TODO: Think if this works well in perspective view...'cause result depends
89     // on position on the screen, but we always use the point close to the
90     // screen's origin...
91     mySelectingVolumeMgr.SetPixelTolerance (myPixTol);
92
93     myToUpdateTol = Standard_False;
94   }
95
96   Standard_Integer aWidth = 0, aHeight = 0;
97   Graphic3d_Mat4d aProj, anOrient;
98   Standard_Boolean isOrthographic = Standard_False;
99   Standard_Real aX = RealLast(), aY = RealLast();
100   Standard_Real aVpWidth = RealLast(), aVpHeight = RealLast();
101
102   mySelectingVolumeMgr.SetActiveSelectionType (SelectMgr_SelectingVolumeManager::Box);
103   theView->GetCamera (aProj, anOrient, isOrthographic);
104   mySelectingVolumeMgr.SetCamera (aProj, anOrient, isOrthographic);
105
106   theView->GetWindowSize (aWidth, aHeight);
107   mySelectingVolumeMgr.SetWindowSize (aWidth, aHeight);
108
109   theView->GetViewport (aX, aY, aVpWidth, aVpHeight);
110   mySelectingVolumeMgr.SetViewport (aX, aY, aVpWidth, aVpHeight);
111
112   gp_Pnt2d aMinMousePos (static_cast<Standard_Real> (theXMin),
113                          static_cast<Standard_Real> (theYMin));
114   gp_Pnt2d aMaxMousePos (static_cast<Standard_Real> (theXMax),
115                          static_cast<Standard_Real> (theYMax));
116
117   mySelectingVolumeMgr.BuildSelectingVolume (aMinMousePos,
118                                              aMaxMousePos);
119
120   TraverseSensitives();
121 }
122
123 //============================================================================
124 // Method:  Pick
125 // Purpose:
126 //============================================================================
127 void IVtkOCC_ViewerSelector::Pick (double**                  thePoly,
128                                    const int                 theNbPoints,
129                                    const IVtk_IView::Handle& theView)
130 {
131   TColgp_Array1OfPnt2d aPolyline (1, theNbPoints);
132
133   if (myToUpdateTol)
134   {
135     // Compute and set a sensitivity tolerance according to the renderer (viewport).
136     // TODO: Think if this works well in perspective view...'cause result depends
137     // on position on the screen, but we always use the point close to the
138     // screen's origin...
139     mySelectingVolumeMgr.SetPixelTolerance (myPixTol);
140
141     myToUpdateTol = Standard_False;
142   }
143
144   // Build TColgp_Array1OfPnt2d from input array of doubles
145   gp_XYZ aWorldPnt;
146
147   for (Standard_Integer anIt = 0; anIt < theNbPoints; anIt++)
148   {
149     gp_XY aDispPnt = thePoly[anIt][2] != 0 ? gp_XY (thePoly[anIt][0] / thePoly[anIt][2], thePoly[anIt][1] / thePoly[anIt][2])
150                                            : gp_XY (thePoly[anIt][0], thePoly[anIt][1]);
151     aPolyline.SetValue (anIt + 1, aDispPnt);
152   }
153
154   Standard_Integer aWidth = 0, aHeight = 0;
155   Graphic3d_Mat4d aProj, anOrient;
156   Standard_Boolean isOrthographic = Standard_False;
157   Standard_Real aX = RealLast(), aY = RealLast();
158   Standard_Real aVpWidth = RealLast(), aVpHeight = RealLast();
159
160   mySelectingVolumeMgr.SetActiveSelectionType (SelectMgr_SelectingVolumeManager::Polyline);
161   theView->GetCamera (aProj, anOrient, isOrthographic);
162   mySelectingVolumeMgr.SetCamera (aProj, anOrient, isOrthographic);
163
164   theView->GetWindowSize (aWidth, aHeight);
165   mySelectingVolumeMgr.SetWindowSize (aWidth, aHeight);
166
167   theView->GetViewport (aX, aY, aVpWidth, aVpHeight);
168   mySelectingVolumeMgr.SetViewport (aX, aY, aVpWidth, aVpHeight);
169
170   mySelectingVolumeMgr.BuildSelectingVolume (aPolyline);
171
172   TraverseSensitives();
173 }
174
175 //============================================================================
176 // Method:  Activate
177 // Purpose: Activates the given selection
178 //============================================================================
179 void IVtkOCC_ViewerSelector::Activate (const Handle(SelectMgr_Selection)& theSelection)
180 {
181   for (theSelection->Init(); theSelection->More(); theSelection->Next())
182   {
183     theSelection->Sensitive()->SetActiveForSelection();
184   }
185
186   theSelection->SetSelectionState (SelectMgr_SOS_Activated);
187
188   myTolerances.Add (theSelection->Sensitivity());
189   myToUpdateTolerance = Standard_True;
190 }
191
192 //============================================================================
193 // Method:  Deactivate
194 // Purpose: Deactivate the given selection
195 //============================================================================
196 void IVtkOCC_ViewerSelector::Deactivate (const Handle(SelectMgr_Selection)& theSelection)
197 {
198   for (theSelection->Init(); theSelection->More(); theSelection->Next())
199   {
200     theSelection->Sensitive()->ResetSelectionActiveStatus();
201   }
202
203   theSelection->SetSelectionState (SelectMgr_SOS_Deactivated);
204
205   myTolerances.Decrement (theSelection->Sensitivity());
206   myToUpdateTolerance = Standard_True;
207 }