0026139: AIS_InteractiveContext::Display performance regression
[occt.git] / src / SelectMgr / SelectMgr_SelectingVolumeManager.cxx
CommitLineData
f751596e 1// Created on: 2014-05-22
2// Created by: Varvara POSKONINA
3// Copyright (c) 2005-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 <SelectMgr_SelectingVolumeManager.hxx>
17
18//=======================================================================
19// function : SelectMgr_SelectingVolumeManager
20// purpose : Creates instances of all available selecting volume types
21//=======================================================================
22SelectMgr_SelectingVolumeManager::SelectMgr_SelectingVolumeManager (Standard_Boolean theToAllocateFrustums)
23{
24 myActiveSelectionType = Unknown;
2157d6ac 25 myToAllowOverlap = Standard_False;
f751596e 26
27 if (theToAllocateFrustums)
28 {
29 mySelectingVolumes[Frustum] = new SelectMgr_RectangularFrustum();
30 mySelectingVolumes[FrustumSet] = new SelectMgr_TriangularFrustumSet();
31 }
32}
33
34//=======================================================================
35// function : Transform
36// purpose : Returns a copy of active frustum transformed according to the matrix given
37//=======================================================================
38SelectMgr_SelectingVolumeManager SelectMgr_SelectingVolumeManager::Transform (const gp_Trsf& theTrsf)
39{
40 SelectMgr_SelectingVolumeManager aMgr (Standard_False);
41
42 if (myActiveSelectionType == Unknown)
43 return aMgr;
44
45 aMgr.myActiveSelectionType = myActiveSelectionType;
46
47 aMgr.mySelectingVolumes[myActiveSelectionType / 2] = mySelectingVolumes[myActiveSelectionType / 2]->Transform (theTrsf);
2157d6ac 48 aMgr.myToAllowOverlap = myToAllowOverlap;
f751596e 49
50 return aMgr;
51}
52
28ee613b 53//=======================================================================
54// function : Scale
55// purpose : IMPORTANT: Makes sense only for point selection!
56// Returns a copy of the frustum resized according to the scale factor given
57//=======================================================================
58SelectMgr_SelectingVolumeManager SelectMgr_SelectingVolumeManager::Scale (const Standard_Real theScaleFactor)
59{
60 if (myActiveSelectionType != Point)
61 return SelectMgr_SelectingVolumeManager (Standard_False);
62
63 SelectMgr_SelectingVolumeManager aMgr (Standard_False);
64
65 aMgr.myActiveSelectionType = Point;
66
67 aMgr.mySelectingVolumes[Point] = mySelectingVolumes[Point]->Scale (theScaleFactor);
68
69 return aMgr;
70}
71
f751596e 72//=======================================================================
73// function : GetActiveSelectionType
74// purpose :
75//=======================================================================
7ab15952 76Standard_Integer SelectMgr_SelectingVolumeManager::GetActiveSelectionType() const
f751596e 77{
78 return myActiveSelectionType;
79}
80
81//=======================================================================
82// function : SetActiveSelectionType
83// purpose :
84//=======================================================================
85void SelectMgr_SelectingVolumeManager::SetActiveSelectionType (const SelectionType& theType)
86{
87 myActiveSelectionType = theType;
88}
89
90//=======================================================================
91// function : SetCamera
92// purpose : Updates camera projection and orientation matrices in all
93// selecting volumes
94//=======================================================================
95void SelectMgr_SelectingVolumeManager::SetCamera (const Handle(Graphic3d_Camera) theCamera)
96{
97 for (Standard_Integer anIdx = 0; anIdx < VolumeTypesNb; ++anIdx)
98 {
99 mySelectingVolumes[anIdx]->SetCamera (theCamera);
100 }
101}
102
103//=======================================================================
104// function : SetCamera
105// purpose : Updates camera projection and orientation matrices in all
106// selecting volumes
107//=======================================================================
108void SelectMgr_SelectingVolumeManager::SetCamera (const Graphic3d_Mat4d& theProjection,
109 const Graphic3d_Mat4d& theOrientation,
110 const Standard_Boolean theIsOrthographic)
111{
112 for (Standard_Integer anIdx = 0; anIdx < VolumeTypesNb; ++anIdx)
113 {
114 mySelectingVolumes[anIdx]->SetCamera (theProjection, theOrientation, theIsOrthographic);
115 }
116}
117
118//=======================================================================
119// function : SetCamera
120// purpose : Updates viewport in all selecting volumes
121//=======================================================================
122void SelectMgr_SelectingVolumeManager::SetViewport (const Standard_Real theX,
123 const Standard_Real theY,
124 const Standard_Real theWidth,
125 const Standard_Real theHeight)
126{
127 for (Standard_Integer anIdx = 0; anIdx < VolumeTypesNb; ++anIdx)
128 {
129 mySelectingVolumes[anIdx]->SetViewport (theX, theY, theWidth, theHeight);
130 }
131}
132
133//=======================================================================
134// function : SetWindowSize
135// purpose : Updates window size in all selecting volumes
136//=======================================================================
137void SelectMgr_SelectingVolumeManager::SetWindowSize (const Standard_Integer theWidth,
138 const Standard_Integer theHeight)
139{
140 for (Standard_Integer anIdx = 0; anIdx < VolumeTypesNb; ++anIdx)
141 {
142 mySelectingVolumes[anIdx]->SetWindowSize (theWidth, theHeight);
143 }
144}
145
146//=======================================================================
147// function : SetPixelTolerance
148// purpose : Updates pixel tolerance in all selecting volumes
149//=======================================================================
150void SelectMgr_SelectingVolumeManager::SetPixelTolerance (const Standard_Real theTolerance)
151{
152 for (Standard_Integer anIdx = 0; anIdx < VolumeTypesNb; ++anIdx)
153 {
154 mySelectingVolumes[anIdx]->SetPixelTolerance (theTolerance);
155 }
156}
157
158//=======================================================================
159// function : BuildSelectingVolume
160// purpose : Builds rectangular selecting frustum for point selection
161//=======================================================================
162void SelectMgr_SelectingVolumeManager::BuildSelectingVolume (const gp_Pnt2d& thePoint)
163{
164 if (myActiveSelectionType != Point)
165 return;
166
167 mySelectingVolumes[Frustum]->Build (thePoint);
168}
169
170//=======================================================================
171// function : BuildSelectingVolume
172// purpose : Builds rectangular selecting frustum for box selection
173//=======================================================================
174void SelectMgr_SelectingVolumeManager::BuildSelectingVolume (const gp_Pnt2d& theMinPt,
175 const gp_Pnt2d& theMaxPt)
176{
177 if (myActiveSelectionType != Box)
178 return;
179
180 mySelectingVolumes[Frustum]->Build (theMinPt, theMaxPt);
181}
182
183//=======================================================================
184// function : BuildSelectingVolume
185// purpose : Builds set of triangular selecting frustums for polyline
186// selection
187//=======================================================================
188void SelectMgr_SelectingVolumeManager::BuildSelectingVolume (const TColgp_Array1OfPnt2d& thePoints)
189{
190 if (myActiveSelectionType != Polyline)
191 return;
192
193 mySelectingVolumes[FrustumSet]->Build (thePoints);
194}
195
196//=======================================================================
197// function : Overlaps
198// purpose : SAT intersection test between defined volume and
199// given axis-aligned box
200//=======================================================================
7ab15952 201Standard_Boolean SelectMgr_SelectingVolumeManager::Overlaps (const BVH_Box<Standard_Real, 3>& theBndBox,
202 Standard_Real& theDepth)
f751596e 203{
204 if (myActiveSelectionType == Unknown)
205 return Standard_False;
206
2157d6ac 207 return mySelectingVolumes[myActiveSelectionType / 2]->Overlaps (theBndBox, theDepth);
f751596e 208}
209
210//=======================================================================
211// function : Overlaps
212// purpose : Intersection test between defined volume and given point
213//=======================================================================
2157d6ac 214Standard_Boolean SelectMgr_SelectingVolumeManager::Overlaps (const SelectMgr_Vec3& theBoxMin,
215 const SelectMgr_Vec3& theBoxMax,
216 Standard_Boolean* theInside)
f751596e 217{
218 if (myActiveSelectionType == Unknown)
219 return Standard_False;
220
2157d6ac 221 return mySelectingVolumes[myActiveSelectionType / 2]->Overlaps (theBoxMin, theBoxMax, theInside);
f751596e 222}
223
224//=======================================================================
225// function : Overlaps
226// purpose : Intersection test between defined volume and given point
227//=======================================================================
7ab15952 228Standard_Boolean SelectMgr_SelectingVolumeManager::Overlaps (const gp_Pnt& thePt,
229 Standard_Real& theDepth)
f751596e 230{
231 if (myActiveSelectionType == Unknown)
232 return Standard_False;
233
234 return mySelectingVolumes[myActiveSelectionType / 2]->Overlaps (thePt,
235 theDepth);
236}
237
238//=======================================================================
239// function : Overlaps
240// purpose : SAT intersection test between defined volume and given
241// ordered set of points, representing line segments. The test
242// may be considered of interior part or boundary line defined
243// by segments depending on given sensitivity type
244//=======================================================================
7ab15952 245Standard_Boolean SelectMgr_SelectingVolumeManager::Overlaps (const Handle(TColgp_HArray1OfPnt)& theArrayOfPts,
246 Standard_Integer theSensType,
247 Standard_Real& theDepth)
f751596e 248{
249 if (myActiveSelectionType == Unknown)
250 return Standard_False;
251
252 return mySelectingVolumes[myActiveSelectionType / 2]->Overlaps (theArrayOfPts,
253 (Select3D_TypeOfSensitivity)theSensType,
254 theDepth);
255}
256
257//=======================================================================
258// function : Overlaps
259// purpose : Checks if line segment overlaps selecting volume
260//=======================================================================
7ab15952 261Standard_Boolean SelectMgr_SelectingVolumeManager::Overlaps (const gp_Pnt& thePt1,
262 const gp_Pnt& thePt2,
263 Standard_Real& theDepth)
f751596e 264{
265 if (myActiveSelectionType == Unknown)
266 return Standard_False;
267
268 return mySelectingVolumes[myActiveSelectionType / 2]->Overlaps (thePt1, thePt2, theDepth);
269}
270
271//=======================================================================
272// function : Overlaps
273// purpose : SAT intersection test between defined volume and given
274// triangle. The test may be considered of interior part or
275// boundary line defined by triangle vertices depending on
276// given sensitivity type
277//=======================================================================
7ab15952 278Standard_Boolean SelectMgr_SelectingVolumeManager::Overlaps (const gp_Pnt& thePt1,
279 const gp_Pnt& thePt2,
280 const gp_Pnt& thePt3,
281 Standard_Integer theSensType,
282 Standard_Real& theDepth)
f751596e 283{
284 if (myActiveSelectionType == Unknown)
285 return Standard_False;
286
287 return mySelectingVolumes[myActiveSelectionType / 2]->Overlaps (thePt1,
288 thePt2,
289 thePt3,
290 (Select3D_TypeOfSensitivity)theSensType,
291 theDepth);
292}
293
294//=======================================================================
295// function : DistToGeometryCenter
296// purpose : Measures distance between 3d projection of user-picked
297// screen point and given point theCOG
298//=======================================================================
7ab15952 299Standard_Real SelectMgr_SelectingVolumeManager::DistToGeometryCenter (const gp_Pnt& theCOG)
f751596e 300{
301 if (myActiveSelectionType == Unknown)
302 return Standard_False;
303
304 return mySelectingVolumes[myActiveSelectionType / 2]->DistToGeometryCenter (theCOG);
305}
306
307// =======================================================================
308// function : DetectedPoint
309// purpose : Calculates the point on a view ray that was detected during
310// the run of selection algo by given depth. Is valid for point
311// selection only
312// =======================================================================
313NCollection_Vec3<Standard_Real> SelectMgr_SelectingVolumeManager::DetectedPoint (const Standard_Real theDepth) const
314{
315 if (myActiveSelectionType != Point)
316 return NCollection_Vec3<Standard_Real> (RealLast());
317
318 return mySelectingVolumes[Frustum]->DetectedPoint (theDepth);
319}
320
321//=======================================================================
322// function : IsClipped
323// purpose : Checks if the point of sensitive in which selection was
324// detected belongs to the region defined by clipping planes
325//=======================================================================
7ab15952 326Standard_Boolean SelectMgr_SelectingVolumeManager::IsClipped (const Graphic3d_SequenceOfHClipPlane& thePlanes,
327 const Standard_Real& theDepth)
f751596e 328{
329 if (myActiveSelectionType == Point)
330 return Standard_False;
331
332 return mySelectingVolumes[Frustum]->IsClipped (thePlanes, theDepth);
333}
2157d6ac 334
335//=======================================================================
336// function : AllowOverlapDetection
337// purpose : If theIsToAllow is false, only fully included sensitives will
338// be detected, otherwise the algorithm will mark both included
339// and overlapped entities as matched
340//=======================================================================
341void SelectMgr_SelectingVolumeManager::AllowOverlapDetection (const Standard_Boolean theIsToAllow)
342{
343 myToAllowOverlap = theIsToAllow;
344}
345
346//=======================================================================
347// function : IsOverlapAllowed
348// purpose :
349//=======================================================================
350Standard_Boolean SelectMgr_SelectingVolumeManager::IsOverlapAllowed() const
351{
352 return myActiveSelectionType != Box || myToAllowOverlap;
353}