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>
22 #define MEMORY_BLOCK_SIZE 512 * 7
24 // =======================================================================
25 // function : BuildSelectingVolume
26 // purpose : Meshes polygon bounded by polyline. Than organizes a set of
27 // triangular frustums, where each triangle's projection onto
28 // near and far view frustum planes is considered as a frustum
30 // =======================================================================
31 void SelectMgr_TriangularFrustumSet::Build (const TColgp_Array1OfPnt2d& thePoints)
33 for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
35 SelectMgr_HTriangularFrustum& aFrust = anIter.ChangeValue();
40 Handle(NCollection_IncAllocator) anAllocator = new NCollection_IncAllocator (MEMORY_BLOCK_SIZE);
41 Handle(BRepMesh_DataStructureOfDelaun) aMeshStructure = new BRepMesh_DataStructureOfDelaun(anAllocator);
42 Standard_Integer aPtsLower = thePoints.Lower();
43 Standard_Integer aPtsUpper = thePoints.Upper();
44 BRepMesh::Array1OfInteger anIndexes (0, thePoints.Length() - 1);
45 for (Standard_Integer aPtIdx = aPtsLower; aPtIdx <= aPtsUpper; ++aPtIdx)
47 BRepMesh_Vertex aVertex (thePoints.Value (aPtIdx).XY(), aPtIdx, BRepMesh_Frontier);
48 anIndexes.ChangeValue (aPtIdx - aPtsLower) = aMeshStructure->AddNode (aVertex);
51 Standard_Real aPtSum = 0;
52 for (Standard_Integer aIdx = aPtsLower; aIdx <= aPtsUpper; ++aIdx)
54 Standard_Integer aNextIdx = (aIdx % thePoints.Length()) + 1;
55 aPtSum += (thePoints.Value (aNextIdx).Coord().X() - thePoints.Value (aIdx).Coord().X())
56 * (thePoints.Value (aNextIdx).Coord().Y() + thePoints.Value (aIdx).Coord().Y());
58 Standard_Boolean isClockwiseOrdered = aPtSum < 0;
60 for (Standard_Integer aIdx = 0; aIdx < anIndexes.Length(); ++aIdx)
62 Standard_Integer aPtIdx = isClockwiseOrdered ? aIdx : (aIdx + 1) % anIndexes.Length();
63 Standard_Integer aNextPtIdx = isClockwiseOrdered ? (aIdx + 1) % anIndexes.Length() : aIdx;
64 BRepMesh_Edge anEdge (anIndexes.Value (aPtIdx),
65 anIndexes.Value (aNextPtIdx),
67 aMeshStructure->AddLink (anEdge);
70 BRepMesh_Delaun aTriangulation (aMeshStructure, anIndexes);
71 const BRepMesh::MapOfInteger& aTriangles = aMeshStructure->ElementsOfDomain();
72 if (aTriangles.Extent() < 1)
75 BRepMesh::MapOfInteger::Iterator aTriangleIt (aTriangles);
76 for (; aTriangleIt.More(); aTriangleIt.Next())
78 const Standard_Integer aTriangleId = aTriangleIt.Key();
79 const BRepMesh_Triangle& aCurrentTriangle = aMeshStructure->GetElement (aTriangleId);
81 if (aCurrentTriangle.Movability() == BRepMesh_Deleted)
84 Standard_Integer aTriangleVerts[3];
85 aMeshStructure->ElementNodes (aCurrentTriangle, aTriangleVerts);
88 for (Standard_Integer aVertIdx = 0; aVertIdx < 3; ++aVertIdx)
90 const BRepMesh_Vertex& aVertex = aMeshStructure->GetNode (aTriangleVerts[aVertIdx]);
91 aPts[aVertIdx] = aVertex.Coord();
94 SelectMgr_HTriangularFrustum aTrFrustum = new SelectMgr_TriangularFrustum();
95 aTrFrustum->SetBuilder (myBuilder);
96 aTrFrustum->Build (aPts[0], aPts[1], aPts[2]);
97 myFrustums.Append (aTrFrustum);
100 aMeshStructure.Nullify();
101 anAllocator.Nullify();
104 // =======================================================================
105 // function : Transform
106 // purpose : Returns a copy of the frustum with all sub-volumes transformed
107 // according to the matrix given
108 // =======================================================================
109 NCollection_Handle<SelectMgr_BaseFrustum> SelectMgr_TriangularFrustumSet::Transform (const gp_Trsf& theTrsf)
111 SelectMgr_TriangularFrustumSet* aRes = new SelectMgr_TriangularFrustumSet();
113 for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
115 aRes->myFrustums.Append (NCollection_Handle<SelectMgr_TriangularFrustum>::DownCast (anIter.Value()->Transform (theTrsf)));
118 return NCollection_Handle<SelectMgr_BaseFrustum> (aRes);
121 // =======================================================================
122 // function : Overlaps
124 // =======================================================================
125 Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const BVH_Box<Standard_Real, 3>& theBox,
126 Standard_Real& theDepth)
128 for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
130 if (anIter.Value()->Overlaps (theBox, theDepth))
131 return Standard_True;
134 return Standard_False;
137 // =======================================================================
138 // function : Overlaps
140 // =======================================================================
141 Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const SelectMgr_Vec3& theMinPnt,
142 const SelectMgr_Vec3& theMaxPnt)
144 for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
146 if (anIter.Value()->Overlaps (theMinPnt, theMaxPnt))
147 return Standard_True;
150 return Standard_False;
153 // =======================================================================
154 // function : Overlaps
156 // =======================================================================
157 Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const gp_Pnt& thePnt,
158 Standard_Real& theDepth)
160 for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
162 if (anIter.Value()->Overlaps (thePnt, theDepth))
163 return Standard_True;
166 return Standard_False;
169 // =======================================================================
170 // function : Overlaps
172 // =======================================================================
173 Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const Handle(TColgp_HArray1OfPnt)& theArrayOfPts,
174 Select3D_TypeOfSensitivity theSensType,
175 Standard_Real& theDepth)
177 for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
179 if (anIter.Value()->Overlaps (theArrayOfPts, theSensType, theDepth))
180 return Standard_True;
183 return Standard_False;
186 // =======================================================================
187 // function : Overlaps
189 // =======================================================================
190 Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const gp_Pnt& thePnt1,
191 const gp_Pnt& thePnt2,
192 Standard_Real& theDepth)
194 for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
196 if (anIter.Value()->Overlaps (thePnt1, thePnt2, theDepth))
197 return Standard_True;
200 return Standard_False;
203 // =======================================================================
204 // function : Overlaps
206 // =======================================================================
207 Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const gp_Pnt& thePnt1,
208 const gp_Pnt& thePnt2,
209 const gp_Pnt& thePnt3,
210 Select3D_TypeOfSensitivity theSensType,
211 Standard_Real& theDepth)
213 for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
215 if (anIter.Value()->Overlaps (thePnt1, thePnt2, thePnt3, theSensType, theDepth))
216 return Standard_True;
219 return Standard_False;
222 #undef MEMORY_BLOCK_SIZE