0027207: New universal method for extracting of results of work of HLRBRep_Algo algorithm
[occt.git] / src / SelectMgr / SelectMgr_TriangularFrustum.cxx
CommitLineData
f751596e 1// Created on: 2014-11-21
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_TriangularFrustum.hxx>
17
92efcf78 18IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_TriangularFrustum,Standard_Transient)
19
f751596e 20SelectMgr_TriangularFrustum::~SelectMgr_TriangularFrustum()
21{
22 Clear();
23}
24
2157d6ac 25namespace
26{
3bf9a45f 27 void computeFrustumNormals (const gp_Vec* theEdges, gp_Vec* theNormals)
2157d6ac 28 {
29 // V0V1
3bf9a45f 30 theNormals[0] = theEdges[0].Crossed (theEdges[1]);
2157d6ac 31 // V1V2
3bf9a45f 32 theNormals[1] = theEdges[1].Crossed (theEdges[2]);
2157d6ac 33 // V0V2
3bf9a45f 34 theNormals[2] = theEdges[0].Crossed (theEdges[2]);
2157d6ac 35 // Near
3bf9a45f 36 theNormals[3] = theEdges[3].Crossed (theEdges[5]);
2157d6ac 37 // Far
3bf9a45f 38 theNormals[4] = -theNormals[3];
2157d6ac 39 }
40}
41
3bf9a45f 42// =======================================================================
43// function : cacheVertexProjections
44// purpose : Caches projection of frustum's vertices onto its plane directions
45// and {i, j, k}
46// =======================================================================
47void SelectMgr_TriangularFrustum::cacheVertexProjections (SelectMgr_TriangularFrustum* theFrustum)
f751596e 48{
f751596e 49 for (Standard_Integer aPlaneIdx = 0; aPlaneIdx < 5; ++aPlaneIdx)
50 {
51 Standard_Real aMax = -DBL_MAX;
52 Standard_Real aMin = DBL_MAX;
3bf9a45f 53 const gp_XYZ& aPlane = theFrustum->myPlanes[aPlaneIdx].XYZ();
f751596e 54 for (Standard_Integer aVertIdx = 0; aVertIdx < 6; ++aVertIdx)
55 {
3bf9a45f 56 Standard_Real aProjection = aPlane.Dot (theFrustum->myVertices[aVertIdx].XYZ());
f751596e 57 aMax = Max (aMax, aProjection);
58 aMin = Min (aMin, aProjection);
59 }
3bf9a45f 60 theFrustum->myMaxVertsProjections[aPlaneIdx] = aMax;
61 theFrustum->myMinVertsProjections[aPlaneIdx] = aMin;
f751596e 62 }
63
f751596e 64 for (Standard_Integer aDim = 0; aDim < 3; ++aDim)
65 {
66 Standard_Real aMax = -DBL_MAX;
67 Standard_Real aMin = DBL_MAX;
68 for (Standard_Integer aVertIdx = 0; aVertIdx < 6; ++aVertIdx)
69 {
3bf9a45f 70 Standard_Real aProjection = theFrustum->myVertices[aVertIdx].XYZ().GetData()[aDim];
f751596e 71 aMax = Max (aMax, aProjection);
72 aMin = Min (aMin, aProjection);
73 }
3bf9a45f 74 theFrustum->myMaxOrthoVertsProjections[aDim] = aMax;
75 theFrustum->myMinOrthoVertsProjections[aDim] = aMin;
f751596e 76 }
3bf9a45f 77}
78
79//=======================================================================
80// function : SelectMgr_TriangularFrustum
81// purpose : Creates new triangular frustum with bases of triangles with
82// vertices theP1, theP2 and theP3 projections onto near and
83// far view frustum planes
84//=======================================================================
85void SelectMgr_TriangularFrustum::Build (const gp_Pnt2d& theP1,
86 const gp_Pnt2d& theP2,
87 const gp_Pnt2d& theP3)
88{
89 // V0_Near
90 myVertices[0] = myBuilder->ProjectPntOnViewPlane (theP1.X(), theP1.Y(), 0.0);
91 // V1_Near
92 myVertices[1] = myBuilder->ProjectPntOnViewPlane (theP2.X(), theP2.Y(), 0.0);
93 // V2_Near
94 myVertices[2] = myBuilder->ProjectPntOnViewPlane (theP3.X(), theP3.Y(), 0.0);
95 // V0_Far
96 myVertices[3] = myBuilder->ProjectPntOnViewPlane (theP1.X(), theP1.Y(), 1.0);
97 // V1_Far
98 myVertices[4] = myBuilder->ProjectPntOnViewPlane (theP2.X(), theP2.Y(), 1.0);
99 // V2_Far
100 myVertices[5] = myBuilder->ProjectPntOnViewPlane (theP3.X(), theP3.Y(), 1.0);
f751596e 101
102 // V0_Near - V0_Far
3bf9a45f 103 myEdgeDirs[0] = myVertices[0].XYZ() - myVertices[3].XYZ();
f751596e 104 // V1_Near - V1_Far
3bf9a45f 105 myEdgeDirs[1] = myVertices[1].XYZ() - myVertices[4].XYZ();
f751596e 106 // V2_Near - V1_Far
3bf9a45f 107 myEdgeDirs[2] = myVertices[2].XYZ() - myVertices[5].XYZ();
f751596e 108 // V1_Near - V0_Near
3bf9a45f 109 myEdgeDirs[3] = myVertices[1].XYZ() - myVertices[0].XYZ();
f751596e 110 // V2_Near - V1_Near
3bf9a45f 111 myEdgeDirs[4] = myVertices[2].XYZ() - myVertices[1].XYZ();
f751596e 112 // V1_Near - V0_Near
3bf9a45f 113 myEdgeDirs[5] = myVertices[2].XYZ() - myVertices[0].XYZ();
114
115 computeFrustumNormals (myEdgeDirs, myPlanes);
116
117 cacheVertexProjections (this);
f751596e 118}
119
120//=======================================================================
3bf9a45f 121// function : ScaleAndTransform
122// purpose : IMPORTANT: Scaling makes sense only for frustum built on a single point!
123// Note that this method does not perform any checks on type of the frustum.
124// Returns a copy of the frustum resized according to the scale factor given
125// and transforms it using the matrix given.
126// There are no default parameters, but in case if:
127// - transformation only is needed: @theScaleFactor must be initialized
128// as any negative value;
129// - scale only is needed: @theTrsf must be set to gp_Identity.
f751596e 130//=======================================================================
3bf9a45f 131NCollection_Handle<SelectMgr_BaseFrustum> SelectMgr_TriangularFrustum::ScaleAndTransform (const Standard_Integer /*theScale*/,
132 const gp_Trsf& theTrsf)
f751596e 133{
134 SelectMgr_TriangularFrustum* aRes = new SelectMgr_TriangularFrustum();
135
136 // V0_Near
3bf9a45f 137 aRes->myVertices[0] = myVertices[0].Transformed (theTrsf);
f751596e 138 // V1_Near
3bf9a45f 139 aRes->myVertices[1] = myVertices[1].Transformed (theTrsf);
f751596e 140 // V2_Near
3bf9a45f 141 aRes->myVertices[2] = myVertices[2].Transformed (theTrsf);
f751596e 142 // V0_Far
3bf9a45f 143 aRes->myVertices[3] = myVertices[3].Transformed (theTrsf);
f751596e 144 // V1_Far
3bf9a45f 145 aRes->myVertices[4] = myVertices[4].Transformed (theTrsf);
f751596e 146 // V2_Far
3bf9a45f 147 aRes->myVertices[5] = myVertices[5].Transformed (theTrsf);
f751596e 148
149 aRes->myIsOrthographic = myIsOrthographic;
150
f751596e 151 // V0_Near - V0_Far
3bf9a45f 152 aRes->myEdgeDirs[0] = aRes->myVertices[0].XYZ() - aRes->myVertices[3].XYZ();
f751596e 153 // V1_Near - V1_Far
3bf9a45f 154 aRes->myEdgeDirs[1] = aRes->myVertices[1].XYZ() - aRes->myVertices[4].XYZ();
f751596e 155 // V2_Near - V1_Far
3bf9a45f 156 aRes->myEdgeDirs[2] = aRes->myVertices[2].XYZ() - aRes->myVertices[5].XYZ();
f751596e 157 // V1_Near - V0_Near
3bf9a45f 158 aRes->myEdgeDirs[3] = aRes->myVertices[1].XYZ() - aRes->myVertices[0].XYZ();
f751596e 159 // V2_Near - V1_Near
3bf9a45f 160 aRes->myEdgeDirs[4] = aRes->myVertices[2].XYZ() - aRes->myVertices[1].XYZ();
f751596e 161 // V1_Near - V0_Near
3bf9a45f 162 aRes->myEdgeDirs[5] = aRes->myVertices[2].XYZ() - aRes->myVertices[0].XYZ();
163
164 computeFrustumNormals (aRes->myEdgeDirs, aRes->myPlanes);
165
166 cacheVertexProjections (aRes);
f751596e 167
168 return NCollection_Handle<SelectMgr_BaseFrustum> (aRes);
169}
170
171//=======================================================================
172// function : Overlaps
173// purpose : SAT intersection test between defined volume and
174// given axis-aligned box
175//=======================================================================
3bf9a45f 176Standard_Boolean SelectMgr_TriangularFrustum::Overlaps (const SelectMgr_Vec3& theMinPt,
177 const SelectMgr_Vec3& theMaxPt,
7ab15952 178 Standard_Real& /*theDepth*/)
f751596e 179{
3bf9a45f 180 return hasOverlap (theMinPt, theMaxPt);
f751596e 181}
182
183// =======================================================================
184// function : Overlaps
185// purpose : Returns true if selecting volume is overlapped by
186// axis-aligned bounding box with minimum corner at point
187// theMinPt and maximum at point theMaxPt
188// =======================================================================
7ab15952 189Standard_Boolean SelectMgr_TriangularFrustum::Overlaps (const SelectMgr_Vec3& theMinPt,
2157d6ac 190 const SelectMgr_Vec3& theMaxPt,
191 Standard_Boolean* /*theInside*/)
f751596e 192{
2157d6ac 193 return hasOverlap (theMinPt, theMaxPt, NULL);
f751596e 194}
195
196// =======================================================================
197// function : Overlaps
198// purpose : Intersection test between defined volume and given point
199// =======================================================================
7ab15952 200Standard_Boolean SelectMgr_TriangularFrustum::Overlaps (const gp_Pnt& thePnt,
201 Standard_Real& /*theDepth*/)
f751596e 202{
203 return hasOverlap (thePnt);
204}
205
206// =======================================================================
207// function : Overlaps
208// purpose : SAT intersection test between defined volume and given
209// ordered set of points, representing line segments. The test
210// may be considered of interior part or boundary line defined
211// by segments depending on given sensitivity type
212// =======================================================================
7ab15952 213Standard_Boolean SelectMgr_TriangularFrustum::Overlaps (const Handle(TColgp_HArray1OfPnt)& theArrayOfPnts,
214 Select3D_TypeOfSensitivity theSensType,
215 Standard_Real& /*theDepth*/)
f751596e 216{
217 if (theSensType == Select3D_TOS_BOUNDARY)
218 {
219 Standard_Integer aLower = theArrayOfPnts->Lower();
220 Standard_Integer anUpper = theArrayOfPnts->Upper();
221
222 for (Standard_Integer aPtIdx = aLower; aPtIdx <= anUpper; ++aPtIdx)
223 {
224 const gp_Pnt& aStartPt = theArrayOfPnts->Value (aPtIdx);
225 const gp_Pnt& aEndPt = aPtIdx == anUpper ? theArrayOfPnts->Value (aLower) : theArrayOfPnts->Value (aPtIdx + 1);
226
227 if (!hasOverlap (aStartPt, aEndPt))
228 {
229 return Standard_False;
230 }
231 }
232 }
233 else if (theSensType == Select3D_TOS_INTERIOR)
234 {
3bf9a45f 235 gp_Vec aNorm (gp_XYZ (RealLast(), RealLast(), RealLast()));
f751596e 236 return hasOverlap (theArrayOfPnts, aNorm);
237 }
238
239 return Standard_False;
240}
241
242// =======================================================================
243// function : Overlaps
244// purpose : Checks if line segment overlaps selecting frustum
245// =======================================================================
7ab15952 246Standard_Boolean SelectMgr_TriangularFrustum::Overlaps (const gp_Pnt& thePnt1,
247 const gp_Pnt& thePnt2,
248 Standard_Real& /*theDepth*/)
f751596e 249{
250 return hasOverlap (thePnt1, thePnt2);
251}
252
253// =======================================================================
254// function : Overlaps
255// purpose : SAT intersection test between defined volume and given
256// triangle. The test may be considered of interior part or
257// boundary line defined by triangle vertices depending on
258// given sensitivity type
259// =======================================================================
7ab15952 260Standard_Boolean SelectMgr_TriangularFrustum::Overlaps (const gp_Pnt& thePnt1,
261 const gp_Pnt& thePnt2,
262 const gp_Pnt& thePnt3,
263 Select3D_TypeOfSensitivity theSensType,
264 Standard_Real& theDepth)
f751596e 265{
266 if (theSensType == Select3D_TOS_BOUNDARY)
267 {
268 Handle(TColgp_HArray1OfPnt) aPtsArray = new TColgp_HArray1OfPnt(1, 4);
269 aPtsArray->SetValue (1, thePnt1);
270 aPtsArray->SetValue (2, thePnt2);
271 aPtsArray->SetValue (3, thePnt3);
272 return Overlaps (aPtsArray, Select3D_TOS_BOUNDARY, theDepth);
273 }
274 else if (theSensType == Select3D_TOS_INTERIOR)
275 {
3bf9a45f 276 gp_Vec aNorm (gp_XYZ (RealLast(), RealLast(), RealLast()));
f751596e 277 return hasOverlap (thePnt1, thePnt2, thePnt3, aNorm);
278 }
279
280 return Standard_True;
281}
282
283// =======================================================================
284// function : Clear
285// purpose : Nullifies the handle for corresponding builder instance to prevent
286// memory leaks
287// =======================================================================
288void SelectMgr_TriangularFrustum::Clear()
289{
290 myBuilder.Nullify();
291}