0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / SelectMgr / SelectMgr_TriangularFrustum.cxx
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
18 #include <SelectMgr_FrustumBuilder.hxx>
19 #include <SelectMgr_ViewClipRange.hxx>
20
21 IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_TriangularFrustum, Standard_Transient)
22
23 namespace
24 {
25   void computeFrustumNormals (const gp_Vec* theEdges, gp_Vec* theNormals)
26   {
27     // V0V1
28     theNormals[0] = theEdges[0].Crossed (theEdges[3]);
29     // V1V2
30     theNormals[1] = theEdges[1].Crossed (theEdges[4]);
31     // V0V2
32     theNormals[2] = theEdges[0].Crossed (theEdges[5]);
33     // Near
34     theNormals[3] = theEdges[3].Crossed (theEdges[4]);
35     // Far
36     theNormals[4] = -theNormals[3];
37   }
38 }
39
40 // =======================================================================
41 // function : SelectMgr_TriangularFrustum
42 // purpose  :
43 // =======================================================================
44 SelectMgr_TriangularFrustum::SelectMgr_TriangularFrustum()
45 {
46   //
47 }
48
49 // =======================================================================
50 // function : ~SelectMgr_TriangularFrustum
51 // purpose  :
52 // =======================================================================
53 SelectMgr_TriangularFrustum::~SelectMgr_TriangularFrustum()
54 {
55   Clear();
56 }
57
58 // =======================================================================
59 // function : cacheVertexProjections
60 // purpose  : Caches projection of frustum's vertices onto its plane directions
61 //            and {i, j, k}
62 // =======================================================================
63 void SelectMgr_TriangularFrustum::cacheVertexProjections (SelectMgr_TriangularFrustum* theFrustum) const
64 {
65   for (Standard_Integer aPlaneIdx = 0; aPlaneIdx < 5; ++aPlaneIdx)
66   {
67     Standard_Real aMax = -DBL_MAX;
68     Standard_Real aMin =  DBL_MAX;
69     const gp_XYZ& aPlane = theFrustum->myPlanes[aPlaneIdx].XYZ();
70     for (Standard_Integer aVertIdx = 0; aVertIdx < 6; ++aVertIdx)
71     {
72       Standard_Real aProjection = aPlane.Dot (theFrustum->myVertices[aVertIdx].XYZ());
73       aMax = Max (aMax, aProjection);
74       aMin = Min (aMin, aProjection);
75     }
76     theFrustum->myMaxVertsProjections[aPlaneIdx] = aMax;
77     theFrustum->myMinVertsProjections[aPlaneIdx] = aMin;
78   }
79
80   for (Standard_Integer aDim = 0; aDim < 3; ++aDim)
81   {
82     Standard_Real aMax = -DBL_MAX;
83     Standard_Real aMin =  DBL_MAX;
84     for (Standard_Integer aVertIdx = 0; aVertIdx < 6; ++aVertIdx)
85     {
86       Standard_Real aProjection = theFrustum->myVertices[aVertIdx].XYZ().GetData()[aDim];
87       aMax = Max (aMax, aProjection);
88       aMin = Min (aMin, aProjection);
89     }
90     theFrustum->myMaxOrthoVertsProjections[aDim] = aMax;
91     theFrustum->myMinOrthoVertsProjections[aDim] = aMin;
92   }
93 }
94
95 //=======================================================================
96 // function : Init
97 // purpose  :
98 //=======================================================================
99 void SelectMgr_TriangularFrustum::Init (const gp_Pnt2d& theP1,
100                                         const gp_Pnt2d& theP2,
101                                         const gp_Pnt2d& theP3)
102 {
103   mySelTriangle.Points[0] = theP1;
104   mySelTriangle.Points[1] = theP2;
105   mySelTriangle.Points[2] = theP3;
106 }
107
108 //=======================================================================
109 // function : Build
110 // purpose  :
111 //=======================================================================
112 void SelectMgr_TriangularFrustum::Build()
113 {
114   // V0_Near
115   myVertices[0] = myBuilder->ProjectPntOnViewPlane (mySelTriangle.Points[0].X(), mySelTriangle.Points[0].Y(), 0.0);
116   // V1_Near
117   myVertices[1] = myBuilder->ProjectPntOnViewPlane (mySelTriangle.Points[1].X(), mySelTriangle.Points[1].Y(), 0.0);
118   // V2_Near
119   myVertices[2] = myBuilder->ProjectPntOnViewPlane (mySelTriangle.Points[2].X(), mySelTriangle.Points[2].Y(), 0.0);
120   // V0_Far
121   myVertices[3] = myBuilder->ProjectPntOnViewPlane (mySelTriangle.Points[0].X(), mySelTriangle.Points[0].Y(), 1.0);
122   // V1_Far
123   myVertices[4] = myBuilder->ProjectPntOnViewPlane (mySelTriangle.Points[1].X(), mySelTriangle.Points[1].Y(), 1.0);
124   // V2_Far
125   myVertices[5] = myBuilder->ProjectPntOnViewPlane (mySelTriangle.Points[2].X(), mySelTriangle.Points[2].Y(), 1.0);
126
127   // V0_Near - V0_Far
128   myEdgeDirs[0] = myVertices[0].XYZ() - myVertices[3].XYZ();
129   // V1_Near - V1_Far
130   myEdgeDirs[1] = myVertices[1].XYZ() - myVertices[4].XYZ();
131   // V2_Near - V1_Far
132   myEdgeDirs[2] = myVertices[2].XYZ() - myVertices[5].XYZ();
133   // V1_Near - V0_Near
134   myEdgeDirs[3] = myVertices[1].XYZ() - myVertices[0].XYZ();
135   // V2_Near - V1_Near
136   myEdgeDirs[4] = myVertices[2].XYZ() - myVertices[1].XYZ();
137   // V1_Near - V0_Near
138   myEdgeDirs[5] = myVertices[2].XYZ() - myVertices[0].XYZ();
139
140   computeFrustumNormals (myEdgeDirs, myPlanes);
141
142   cacheVertexProjections (this);
143 }
144
145 //=======================================================================
146 // function : ScaleAndTransform
147 // purpose  : IMPORTANT: Scaling makes sense only for frustum built on a single point!
148 //            Note that this method does not perform any checks on type of the frustum.
149 //            Returns a copy of the frustum resized according to the scale factor given
150 //            and transforms it using the matrix given.
151 //            There are no default parameters, but in case if:
152 //                - transformation only is needed: @theScaleFactor must be initialized
153 //                  as any negative value;
154 //                - scale only is needed: @theTrsf must be set to gp_Identity.
155 //=======================================================================
156 Handle(SelectMgr_BaseIntersector) SelectMgr_TriangularFrustum::ScaleAndTransform (const Standard_Integer,
157                                                                                   const gp_GTrsf& theTrsf,
158                                                                                   const Handle(SelectMgr_FrustumBuilder)&) const
159 {
160   Handle(SelectMgr_TriangularFrustum) aRes = new SelectMgr_TriangularFrustum();
161   aRes->SetCamera (myCamera);
162
163   for (Standard_Integer anIt = 0; anIt < 6; anIt++)
164   {
165     gp_Pnt aPoint = myVertices[anIt];
166     theTrsf.Transforms (aPoint.ChangeCoord());
167     aRes->myVertices[anIt] = aPoint;
168   }
169
170   // V0_Near - V0_Far
171   aRes->myEdgeDirs[0] = aRes->myVertices[0].XYZ() - aRes->myVertices[3].XYZ();
172   // V1_Near - V1_Far
173   aRes->myEdgeDirs[1] = aRes->myVertices[1].XYZ() - aRes->myVertices[4].XYZ();
174   // V2_Near - V1_Far
175   aRes->myEdgeDirs[2] = aRes->myVertices[2].XYZ() - aRes->myVertices[5].XYZ();
176   // V1_Near - V0_Near
177   aRes->myEdgeDirs[3] = aRes->myVertices[1].XYZ() - aRes->myVertices[0].XYZ();
178   // V2_Near - V1_Near
179   aRes->myEdgeDirs[4] = aRes->myVertices[2].XYZ() - aRes->myVertices[1].XYZ();
180   // V1_Near - V0_Near
181   aRes->myEdgeDirs[5] = aRes->myVertices[2].XYZ() - aRes->myVertices[0].XYZ();
182
183   computeFrustumNormals (aRes->myEdgeDirs, aRes->myPlanes);
184
185   cacheVertexProjections (aRes.get());
186
187   aRes->mySelTriangle = mySelTriangle;
188
189   return aRes;
190 }
191
192 //=======================================================================
193 // function : CopyWithBuilder
194 // purpose  : Returns a copy of the frustum using the given frustum builder configuration.
195 //            Returned frustum should be re-constructed before being used.
196 //=======================================================================
197 Handle(SelectMgr_BaseIntersector) SelectMgr_TriangularFrustum::CopyWithBuilder (const Handle(SelectMgr_FrustumBuilder)& theBuilder) const
198 {
199   Handle(SelectMgr_TriangularFrustum) aRes = new SelectMgr_TriangularFrustum();
200   aRes->mySelTriangle = mySelTriangle;
201   aRes->SetBuilder (theBuilder);
202
203   return aRes;
204 }
205
206 //=======================================================================
207 // function : OverlapsBox
208 // purpose  : SAT intersection test between defined volume and
209 //            given axis-aligned box
210 //=======================================================================
211 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsBox (const SelectMgr_Vec3& theMinPt,
212                                                            const SelectMgr_Vec3& theMaxPt,
213                                                            const SelectMgr_ViewClipRange& /*theClipRange*/,
214                                                            SelectBasics_PickResult& /*thePickResult*/) const
215 {
216   return hasBoxOverlap (theMinPt, theMaxPt);
217 }
218
219 // =======================================================================
220 // function : OverlapsBox
221 // purpose  : Returns true if selecting volume is overlapped by
222 //            axis-aligned bounding box with minimum corner at point
223 //            theMinPt and maximum at point theMaxPt
224 // =======================================================================
225 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsBox (const SelectMgr_Vec3& theMinPt,
226                                                            const SelectMgr_Vec3& theMaxPt,
227                                                            Standard_Boolean* /*theInside*/) const
228 {
229   return hasBoxOverlap (theMinPt, theMaxPt, NULL);
230 }
231
232 // =======================================================================
233 // function : OverlapsPoint
234 // purpose  : Intersection test between defined volume and given point
235 // =======================================================================
236 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsPoint (const gp_Pnt& thePnt,
237                                                              const SelectMgr_ViewClipRange& /*theClipRange*/,
238                                                              SelectBasics_PickResult& /*thePickResult*/) const
239 {
240   return hasPointOverlap (thePnt);
241 }
242
243 // =======================================================================
244 // function : OverlapsPolygon
245 // purpose  : SAT intersection test between defined volume and given
246 //            ordered set of points, representing line segments. The test
247 //            may be considered of interior part or boundary line defined
248 //            by segments depending on given sensitivity type
249 // =======================================================================
250 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsPolygon (const TColgp_Array1OfPnt& theArrayOfPnts,
251                                                                Select3D_TypeOfSensitivity theSensType,
252                                                                const SelectMgr_ViewClipRange& /*theClipRange*/,
253                                                                SelectBasics_PickResult& /*thePickResult*/) const
254 {
255   if (theSensType == Select3D_TOS_BOUNDARY)
256   {
257     const Standard_Integer aLower  = theArrayOfPnts.Lower();
258     const Standard_Integer anUpper = theArrayOfPnts.Upper();
259     for (Standard_Integer aPtIdx = aLower; aPtIdx <= anUpper; ++aPtIdx)
260     {
261       const gp_Pnt& aStartPt = theArrayOfPnts.Value (aPtIdx);
262       const gp_Pnt& aEndPt   = theArrayOfPnts.Value (aPtIdx == anUpper ? aLower : (aPtIdx + 1));
263       if (!hasSegmentOverlap (aStartPt, aEndPt))
264       {
265         return Standard_False;
266       }
267     }
268   }
269   else if (theSensType == Select3D_TOS_INTERIOR)
270   {
271     gp_Vec aNorm (gp_XYZ (RealLast(), RealLast(), RealLast()));
272     return hasPolygonOverlap (theArrayOfPnts, aNorm);
273   }
274
275   return Standard_False;
276 }
277
278 // =======================================================================
279 // function : OverlapsSegment
280 // purpose  : Checks if line segment overlaps selecting frustum
281 // =======================================================================
282 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsSegment (const gp_Pnt& thePnt1,
283                                                                const gp_Pnt& thePnt2,
284                                                                const SelectMgr_ViewClipRange& /*theClipRange*/,
285                                                                SelectBasics_PickResult& /*thePickResult*/) const
286 {
287   return hasSegmentOverlap (thePnt1, thePnt2);
288 }
289
290 // =======================================================================
291 // function : OverlapsTriangle
292 // purpose  : SAT intersection test between defined volume and given
293 //            triangle. The test may be considered of interior part or
294 //            boundary line defined by triangle vertices depending on
295 //            given sensitivity type
296 // =======================================================================
297 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsTriangle (const gp_Pnt& thePnt1,
298                                                                 const gp_Pnt& thePnt2,
299                                                                 const gp_Pnt& thePnt3,
300                                                                 Select3D_TypeOfSensitivity theSensType,
301                                                                 const SelectMgr_ViewClipRange& theClipRange,
302                                                                 SelectBasics_PickResult& thePickResult) const
303 {
304   if (theSensType == Select3D_TOS_BOUNDARY)
305   {
306     const gp_Pnt aPntsArrayBuf[3] = { thePnt1, thePnt2, thePnt3 };
307     const TColgp_Array1OfPnt aPntsArray (aPntsArrayBuf[0], 1, 3);
308     return OverlapsPolygon (aPntsArray, Select3D_TOS_BOUNDARY, theClipRange, thePickResult);
309   }
310   else if (theSensType == Select3D_TOS_INTERIOR)
311   {
312     gp_Vec aNorm (gp_XYZ (RealLast(), RealLast(), RealLast()));
313     return hasTriangleOverlap (thePnt1, thePnt2, thePnt3, aNorm);
314   }
315
316   return Standard_True;
317 }
318
319 //=======================================================================
320 // function : OverlapsSphere
321 // purpose  :
322 //=======================================================================
323 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsSphere (const gp_Pnt& theCenter,
324                                                               const Standard_Real theRadius,
325                                                               Standard_Boolean* theInside) const
326 {
327   (void) theInside;
328   return hasBoxOverlap (SelectMgr_Vec3 (theCenter.X() - theRadius, theCenter.Y() - theRadius, theCenter.Z() - theRadius),
329                         SelectMgr_Vec3 (theCenter.X() + theRadius, theCenter.Y() + theRadius, theCenter.Z() + theRadius), NULL);
330 }
331
332 //=======================================================================
333 // function : OverlapsSphere
334 // purpose  :
335 //=======================================================================
336 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsSphere (const gp_Pnt& theCenter,
337                                                               const Standard_Real theRadius,
338                                                               const SelectMgr_ViewClipRange& theClipRange,
339                                                               SelectBasics_PickResult& thePickResult) const
340 {
341   (void )theClipRange;
342   (void )thePickResult;
343   return hasSphereOverlap (theCenter, theRadius);
344 }
345
346 //=======================================================================
347 // function : OverlapsCylinder
348 // purpose  :
349 //=======================================================================
350 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsCylinder (const Standard_Real theBottomRad,
351                                                                 const Standard_Real theTopRad,
352                                                                 const Standard_Real theHeight,
353                                                                 const gp_Trsf& theTrsf,
354                                                                 const Standard_Boolean theIsHollow,
355                                                                 const SelectMgr_ViewClipRange& theClipRange,
356                                                                 SelectBasics_PickResult& thePickResult) const
357 {
358   (void)theClipRange;
359   (void)thePickResult;
360   return hasCylinderOverlap (theBottomRad, theTopRad, theHeight, theTrsf, theIsHollow);
361 }
362
363 //=======================================================================
364 // function : OverlapsCylinder
365 // purpose  :
366 //=======================================================================
367 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsCylinder (const Standard_Real theBottomRad,
368                                                                 const Standard_Real theTopRad,
369                                                                 const Standard_Real theHeight,
370                                                                 const gp_Trsf& theTrsf,
371                                                                 const Standard_Boolean theIsHollow,
372                                                                 Standard_Boolean* theInside) const
373 {
374   (void) theInside;
375   return hasCylinderOverlap (theBottomRad, theTopRad, theHeight, theTrsf, theIsHollow);
376 }
377
378 //=======================================================================
379 // function : OverlapsCircle
380 // purpose  :
381 //=======================================================================
382 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsCircle (const Standard_Real theRadius,
383                                                               const gp_Trsf& theTrsf,
384                                                               const Standard_Boolean theIsFilled,
385                                                               const SelectMgr_ViewClipRange& theClipRange,
386                                                               SelectBasics_PickResult& thePickResult) const
387 {
388   (void)theClipRange;
389   (void)thePickResult;
390   return hasCircleOverlap (theRadius, theTrsf, theIsFilled);
391 }
392
393 //=======================================================================
394 // function : OverlapsCircle
395 // purpose  :
396 //=======================================================================
397 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsCircle (const Standard_Real theRadius,
398                                                               const gp_Trsf& theTrsf,
399                                                               const Standard_Boolean theIsFilled,
400                                                               Standard_Boolean* theInside) const
401 {
402   (void)theInside;
403   return hasCircleOverlap (theRadius, theTrsf, theIsFilled);
404 }
405
406 // =======================================================================
407 // function : Clear
408 // purpose  : Nullifies the handle for corresponding builder instance to prevent
409 //            memory leaks
410 // =======================================================================
411 void SelectMgr_TriangularFrustum::Clear()
412 {
413   myBuilder.Nullify();
414 }
415
416 // =======================================================================
417 // function : GetPlanes
418 // purpose  :
419 // =======================================================================
420 void SelectMgr_TriangularFrustum::GetPlanes (NCollection_Vector<SelectMgr_Vec4>& thePlaneEquations) const
421 {
422   SelectMgr_Vec4 aPlaneEquation;
423   for (Standard_Integer aPlaneIdx = 0; aPlaneIdx < 5; ++aPlaneIdx)
424   {
425     const gp_Vec& aNorm = myPlanes[aPlaneIdx];
426     aPlaneEquation.x() = aNorm.X();
427     aPlaneEquation.y() = aNorm.Y();
428     aPlaneEquation.z() = aNorm.Z();
429     aPlaneEquation.w() = - (aNorm.XYZ().Dot (myVertices[aPlaneIdx % 2 == 0 ? aPlaneIdx : 1].XYZ()));
430     thePlaneEquations.Append (aPlaneEquation);
431   }
432 }
433
434 //=======================================================================
435 //function : DumpJson
436 //purpose  : 
437 //=======================================================================
438 void SelectMgr_TriangularFrustum::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
439 {
440   OCCT_DUMP_CLASS_BEGIN (theOStream, SelectMgr_TriangularFrustum)
441   OCCT_DUMP_BASE_CLASS (theOStream, theDepth, SelectMgr_Frustum)
442 }