1 // Created on: 2014-11-21
2 // Created by: Varvara POSKONINA
3 // Copyright (c) 2005-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 <BRepMesh_DataStructureOfDelaun.hxx>
17 #include <BRepMesh_Delaun.hxx>
18 #include <NCollection_IncAllocator.hxx>
20 #include <SelectMgr_TriangularFrustumSet.hxx>
21 #include <SelectMgr_TriangularFrustum.hxx>
23 #define MEMORY_BLOCK_SIZE 512 * 7
25 // =======================================================================
26 // function : BuildSelectingVolume
27 // purpose : Meshes polygon bounded by polyline. Than organizes a set of
28 // triangular frustums, where each triangle's projection onto
29 // near and far view frustum planes is considered as a frustum
31 // =======================================================================
32 void SelectMgr_TriangularFrustumSet::Build (const TColgp_Array1OfPnt2d& thePoints)
36 Handle(NCollection_IncAllocator) anAllocator = new NCollection_IncAllocator (MEMORY_BLOCK_SIZE);
37 Handle(BRepMesh_DataStructureOfDelaun) aMeshStructure = new BRepMesh_DataStructureOfDelaun(anAllocator);
38 Standard_Integer aPtsLower = thePoints.Lower();
39 Standard_Integer aPtsUpper = thePoints.Upper();
40 BRepMesh::Array1OfInteger anIndexes (0, thePoints.Length() - 1);
41 for (Standard_Integer aPtIdx = aPtsLower; aPtIdx <= aPtsUpper; ++aPtIdx)
43 BRepMesh_Vertex aVertex (thePoints.Value (aPtIdx).XY(), aPtIdx, BRepMesh_Frontier);
44 anIndexes.ChangeValue (aPtIdx - aPtsLower) = aMeshStructure->AddNode (aVertex);
47 Standard_Real aPtSum = 0;
48 for (Standard_Integer aIdx = aPtsLower; aIdx <= aPtsUpper; ++aIdx)
50 Standard_Integer aNextIdx = (aIdx % thePoints.Length()) + 1;
51 aPtSum += (thePoints.Value (aNextIdx).Coord().X() - thePoints.Value (aIdx).Coord().X())
52 * (thePoints.Value (aNextIdx).Coord().Y() + thePoints.Value (aIdx).Coord().Y());
54 Standard_Boolean isClockwiseOrdered = aPtSum < 0;
56 for (Standard_Integer aIdx = 0; aIdx < anIndexes.Length(); ++aIdx)
58 Standard_Integer aPtIdx = isClockwiseOrdered ? aIdx : (aIdx + 1) % anIndexes.Length();
59 Standard_Integer aNextPtIdx = isClockwiseOrdered ? (aIdx + 1) % anIndexes.Length() : aIdx;
60 BRepMesh_Edge anEdge (anIndexes.Value (aPtIdx),
61 anIndexes.Value (aNextPtIdx),
63 aMeshStructure->AddLink (anEdge);
66 BRepMesh_Delaun aTriangulation (aMeshStructure, anIndexes);
67 const BRepMesh::MapOfInteger& aTriangles = aMeshStructure->ElementsOfDomain();
68 if (aTriangles.Extent() < 1)
71 BRepMesh::MapOfInteger::Iterator aTriangleIt (aTriangles);
72 for (; aTriangleIt.More(); aTriangleIt.Next())
74 const Standard_Integer aTriangleId = aTriangleIt.Key();
75 const BRepMesh_Triangle& aCurrentTriangle = aMeshStructure->GetElement (aTriangleId);
77 if (aCurrentTriangle.Movability() == BRepMesh_Deleted)
80 Standard_Integer aTriangleVerts[3];
81 aMeshStructure->ElementNodes (aCurrentTriangle, aTriangleVerts);
84 for (Standard_Integer aVertIdx = 0; aVertIdx < 3; ++aVertIdx)
86 const BRepMesh_Vertex& aVertex = aMeshStructure->GetNode (aTriangleVerts[aVertIdx]);
87 aPts[aVertIdx] = aVertex.Coord();
90 Handle(SelectMgr_TriangularFrustum) aTrFrustum = new SelectMgr_TriangularFrustum();
91 aTrFrustum->SetBuilder (myBuilder);
92 aTrFrustum->Build (aPts[0], aPts[1], aPts[2]);
93 myFrustums.Append (aTrFrustum);
96 aMeshStructure.Nullify();
97 anAllocator.Nullify();
100 // =======================================================================
101 // function : ScaleAndTransform
102 // purpose : IMPORTANT: Scaling makes sense only for frustum built on a single point!
103 // Note that this method does not perform any checks on type of the frustum.
104 // Returns a copy of the frustum resized according to the scale factor given
105 // and transforms it using the matrix given.
106 // There are no default parameters, but in case if:
107 // - transformation only is needed: @theScaleFactor must be initialized
108 // as any negative value;
109 // - scale only is needed: @theTrsf must be set to gp_Identity.
110 // =======================================================================
111 Handle(SelectMgr_BaseFrustum) SelectMgr_TriangularFrustumSet::ScaleAndTransform (const Standard_Integer theScale,
112 const gp_GTrsf& theTrsf) const
114 Handle(SelectMgr_TriangularFrustumSet) aRes = new SelectMgr_TriangularFrustumSet();
116 for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
118 aRes->myFrustums.Append (Handle(SelectMgr_TriangularFrustum)::DownCast (anIter.Value()->ScaleAndTransform (theScale, theTrsf)));
124 // =======================================================================
125 // function : Overlaps
127 // =======================================================================
128 Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const SelectMgr_Vec3& theMinPnt,
129 const SelectMgr_Vec3& theMaxPnt,
130 Standard_Real& theDepth)
132 for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
134 if (anIter.Value()->Overlaps (theMinPnt, theMaxPnt, theDepth))
135 return Standard_True;
138 return Standard_False;
141 // =======================================================================
142 // function : Overlaps
144 // =======================================================================
145 Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const SelectMgr_Vec3& theMinPnt,
146 const SelectMgr_Vec3& theMaxPnt,
147 Standard_Boolean* /*theInside*/)
149 for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
151 if (anIter.Value()->Overlaps (theMinPnt, theMaxPnt, NULL))
152 return Standard_True;
155 return Standard_False;
158 // =======================================================================
159 // function : Overlaps
161 // =======================================================================
162 Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const gp_Pnt& thePnt,
163 Standard_Real& theDepth)
165 for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
167 if (anIter.Value()->Overlaps (thePnt, theDepth))
168 return Standard_True;
171 return Standard_False;
174 // =======================================================================
175 // function : Overlaps
177 // =======================================================================
178 Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const TColgp_Array1OfPnt& theArrayOfPts,
179 Select3D_TypeOfSensitivity theSensType,
180 Standard_Real& theDepth)
182 for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
184 if (anIter.Value()->Overlaps (theArrayOfPts, theSensType, theDepth))
185 return Standard_True;
188 return Standard_False;
191 // =======================================================================
192 // function : Overlaps
194 // =======================================================================
195 Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const gp_Pnt& thePnt1,
196 const gp_Pnt& thePnt2,
197 Standard_Real& theDepth)
199 for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
201 if (anIter.Value()->Overlaps (thePnt1, thePnt2, theDepth))
202 return Standard_True;
205 return Standard_False;
208 // =======================================================================
209 // function : Overlaps
211 // =======================================================================
212 Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const gp_Pnt& thePnt1,
213 const gp_Pnt& thePnt2,
214 const gp_Pnt& thePnt3,
215 Select3D_TypeOfSensitivity theSensType,
216 Standard_Real& theDepth)
218 for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
220 if (anIter.Value()->Overlaps (thePnt1, thePnt2, thePnt3, theSensType, theDepth))
221 return Standard_True;
224 return Standard_False;
227 // =======================================================================
228 // function : GetPlanes
230 // =======================================================================
231 void SelectMgr_TriangularFrustumSet::GetPlanes (NCollection_Vector<SelectMgr_Vec4>& thePlaneEquations) const
233 thePlaneEquations.Clear();
235 for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
237 anIter.Value()->GetPlanes (thePlaneEquations);
241 #undef MEMORY_BLOCK_SIZE