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 <BRepMesh_DataStructureOfDelaun.hxx> |
17 | #include <BRepMesh_Delaun.hxx> |
18 | #include <NCollection_IncAllocator.hxx> |
19 | |
20 | #include <SelectMgr_TriangularFrustumSet.hxx> |
92efcf78 |
21 | #include <SelectMgr_TriangularFrustum.hxx> |
f751596e |
22 | |
23 | #define MEMORY_BLOCK_SIZE 512 * 7 |
24 | |
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 |
30 | // base |
31 | // ======================================================================= |
32 | void SelectMgr_TriangularFrustumSet::Build (const TColgp_Array1OfPnt2d& thePoints) |
33 | { |
f751596e |
34 | myFrustums.Clear(); |
35 | |
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) |
42 | { |
43 | BRepMesh_Vertex aVertex (thePoints.Value (aPtIdx).XY(), aPtIdx, BRepMesh_Frontier); |
44 | anIndexes.ChangeValue (aPtIdx - aPtsLower) = aMeshStructure->AddNode (aVertex); |
45 | } |
46 | |
47 | Standard_Real aPtSum = 0; |
48 | for (Standard_Integer aIdx = aPtsLower; aIdx <= aPtsUpper; ++aIdx) |
49 | { |
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()); |
53 | } |
54 | Standard_Boolean isClockwiseOrdered = aPtSum < 0; |
55 | |
56 | for (Standard_Integer aIdx = 0; aIdx < anIndexes.Length(); ++aIdx) |
57 | { |
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), |
62 | BRepMesh_Frontier); |
63 | aMeshStructure->AddLink (anEdge); |
64 | } |
65 | |
66 | BRepMesh_Delaun aTriangulation (aMeshStructure, anIndexes); |
67 | const BRepMesh::MapOfInteger& aTriangles = aMeshStructure->ElementsOfDomain(); |
68 | if (aTriangles.Extent() < 1) |
69 | return; |
70 | |
71 | BRepMesh::MapOfInteger::Iterator aTriangleIt (aTriangles); |
72 | for (; aTriangleIt.More(); aTriangleIt.Next()) |
73 | { |
74 | const Standard_Integer aTriangleId = aTriangleIt.Key(); |
75 | const BRepMesh_Triangle& aCurrentTriangle = aMeshStructure->GetElement (aTriangleId); |
76 | |
77 | if (aCurrentTriangle.Movability() == BRepMesh_Deleted) |
78 | continue; |
79 | |
80 | Standard_Integer aTriangleVerts[3]; |
81 | aMeshStructure->ElementNodes (aCurrentTriangle, aTriangleVerts); |
82 | |
83 | gp_Pnt2d aPts[3]; |
84 | for (Standard_Integer aVertIdx = 0; aVertIdx < 3; ++aVertIdx) |
85 | { |
86 | const BRepMesh_Vertex& aVertex = aMeshStructure->GetNode (aTriangleVerts[aVertIdx]); |
87 | aPts[aVertIdx] = aVertex.Coord(); |
88 | } |
89 | |
c04c30b3 |
90 | Handle(SelectMgr_TriangularFrustum) aTrFrustum = new SelectMgr_TriangularFrustum(); |
f751596e |
91 | aTrFrustum->SetBuilder (myBuilder); |
92 | aTrFrustum->Build (aPts[0], aPts[1], aPts[2]); |
93 | myFrustums.Append (aTrFrustum); |
94 | } |
95 | |
96 | aMeshStructure.Nullify(); |
97 | anAllocator.Nullify(); |
98 | } |
99 | |
100 | // ======================================================================= |
3bf9a45f |
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. |
f751596e |
110 | // ======================================================================= |
3bf9a45f |
111 | NCollection_Handle<SelectMgr_BaseFrustum> SelectMgr_TriangularFrustumSet::ScaleAndTransform (const Standard_Integer theScale, |
112 | const gp_Trsf& theTrsf) |
f751596e |
113 | { |
114 | SelectMgr_TriangularFrustumSet* aRes = new SelectMgr_TriangularFrustumSet(); |
115 | |
116 | for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next()) |
117 | { |
3bf9a45f |
118 | aRes->myFrustums.Append (Handle(SelectMgr_TriangularFrustum)::DownCast (anIter.Value()->ScaleAndTransform (theScale, theTrsf))); |
f751596e |
119 | } |
120 | |
121 | return NCollection_Handle<SelectMgr_BaseFrustum> (aRes); |
122 | } |
123 | |
124 | // ======================================================================= |
125 | // function : Overlaps |
126 | // purpose : |
127 | // ======================================================================= |
3bf9a45f |
128 | Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const SelectMgr_Vec3& theMinPnt, |
129 | const SelectMgr_Vec3& theMaxPnt, |
7ab15952 |
130 | Standard_Real& theDepth) |
f751596e |
131 | { |
132 | for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next()) |
133 | { |
3bf9a45f |
134 | if (anIter.Value()->Overlaps (theMinPnt, theMaxPnt, theDepth)) |
f751596e |
135 | return Standard_True; |
136 | } |
137 | |
138 | return Standard_False; |
139 | } |
140 | |
141 | // ======================================================================= |
142 | // function : Overlaps |
143 | // purpose : |
144 | // ======================================================================= |
7ab15952 |
145 | Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const SelectMgr_Vec3& theMinPnt, |
2157d6ac |
146 | const SelectMgr_Vec3& theMaxPnt, |
147 | Standard_Boolean* /*theInside*/) |
f751596e |
148 | { |
149 | for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next()) |
150 | { |
2157d6ac |
151 | if (anIter.Value()->Overlaps (theMinPnt, theMaxPnt, NULL)) |
f751596e |
152 | return Standard_True; |
153 | } |
154 | |
155 | return Standard_False; |
156 | } |
157 | |
158 | // ======================================================================= |
159 | // function : Overlaps |
160 | // purpose : |
161 | // ======================================================================= |
7ab15952 |
162 | Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const gp_Pnt& thePnt, |
163 | Standard_Real& theDepth) |
f751596e |
164 | { |
165 | for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next()) |
166 | { |
167 | if (anIter.Value()->Overlaps (thePnt, theDepth)) |
168 | return Standard_True; |
169 | } |
170 | |
171 | return Standard_False; |
172 | } |
173 | |
174 | // ======================================================================= |
175 | // function : Overlaps |
176 | // purpose : |
177 | // ======================================================================= |
7ab15952 |
178 | Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const Handle(TColgp_HArray1OfPnt)& theArrayOfPts, |
179 | Select3D_TypeOfSensitivity theSensType, |
180 | Standard_Real& theDepth) |
f751596e |
181 | { |
182 | for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next()) |
183 | { |
184 | if (anIter.Value()->Overlaps (theArrayOfPts, theSensType, theDepth)) |
185 | return Standard_True; |
186 | } |
187 | |
188 | return Standard_False; |
189 | } |
190 | |
191 | // ======================================================================= |
192 | // function : Overlaps |
193 | // purpose : |
194 | // ======================================================================= |
7ab15952 |
195 | Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const gp_Pnt& thePnt1, |
196 | const gp_Pnt& thePnt2, |
197 | Standard_Real& theDepth) |
f751596e |
198 | { |
199 | for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next()) |
200 | { |
201 | if (anIter.Value()->Overlaps (thePnt1, thePnt2, theDepth)) |
202 | return Standard_True; |
203 | } |
204 | |
205 | return Standard_False; |
206 | } |
207 | |
208 | // ======================================================================= |
209 | // function : Overlaps |
210 | // purpose : |
211 | // ======================================================================= |
7ab15952 |
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) |
f751596e |
217 | { |
218 | for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next()) |
219 | { |
220 | if (anIter.Value()->Overlaps (thePnt1, thePnt2, thePnt3, theSensType, theDepth)) |
221 | return Standard_True; |
222 | } |
223 | |
224 | return Standard_False; |
225 | } |
226 | |
227 | #undef MEMORY_BLOCK_SIZE |