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(); |
7bd071ed |
40 | IMeshData::VectorOfInteger anIndexes(aPtsUpper - aPtsLower, anAllocator); |
f751596e |
41 | for (Standard_Integer aPtIdx = aPtsLower; aPtIdx <= aPtsUpper; ++aPtIdx) |
42 | { |
7bd071ed |
43 | BRepMesh_Vertex aVertex(thePoints.Value(aPtIdx).XY(), aPtIdx, BRepMesh_Frontier); |
44 | anIndexes.Append(aMeshStructure->AddNode(aVertex)); |
f751596e |
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); |
7bd071ed |
67 | const IMeshData::MapOfInteger& aTriangles = aMeshStructure->ElementsOfDomain(); |
f751596e |
68 | if (aTriangles.Extent() < 1) |
69 | return; |
70 | |
7bd071ed |
71 | IMeshData::IteratorOfMapOfInteger aTriangleIt (aTriangles); |
f751596e |
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 | // ======================================================================= |
099f3513 |
111 | Handle(SelectMgr_BaseFrustum) SelectMgr_TriangularFrustumSet::ScaleAndTransform (const Standard_Integer theScale, |
112 | const gp_GTrsf& theTrsf) const |
f751596e |
113 | { |
099f3513 |
114 | Handle(SelectMgr_TriangularFrustumSet) aRes = new SelectMgr_TriangularFrustumSet(); |
f751596e |
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 | |
099f3513 |
121 | return aRes; |
f751596e |
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, |
d7fa57a7 |
130 | const SelectMgr_ViewClipRange& theClipRange, |
4a056d20 |
131 | SelectBasics_PickResult& thePickResult) const |
f751596e |
132 | { |
133 | for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next()) |
134 | { |
d7fa57a7 |
135 | if (anIter.Value()->Overlaps (theMinPnt, theMaxPnt, theClipRange, thePickResult)) |
f751596e |
136 | return Standard_True; |
137 | } |
138 | |
139 | return Standard_False; |
140 | } |
141 | |
142 | // ======================================================================= |
143 | // function : Overlaps |
144 | // purpose : |
145 | // ======================================================================= |
7ab15952 |
146 | Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const SelectMgr_Vec3& theMinPnt, |
2157d6ac |
147 | const SelectMgr_Vec3& theMaxPnt, |
4a056d20 |
148 | Standard_Boolean* /*theInside*/) const |
f751596e |
149 | { |
150 | for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next()) |
151 | { |
2157d6ac |
152 | if (anIter.Value()->Overlaps (theMinPnt, theMaxPnt, NULL)) |
f751596e |
153 | return Standard_True; |
154 | } |
155 | |
156 | return Standard_False; |
157 | } |
158 | |
159 | // ======================================================================= |
160 | // function : Overlaps |
161 | // purpose : |
162 | // ======================================================================= |
7ab15952 |
163 | Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const gp_Pnt& thePnt, |
d7fa57a7 |
164 | const SelectMgr_ViewClipRange& theClipRange, |
4a056d20 |
165 | SelectBasics_PickResult& thePickResult) const |
f751596e |
166 | { |
167 | for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next()) |
168 | { |
d7fa57a7 |
169 | if (anIter.Value()->Overlaps (thePnt, theClipRange, thePickResult)) |
f751596e |
170 | return Standard_True; |
171 | } |
172 | |
173 | return Standard_False; |
174 | } |
175 | |
176 | // ======================================================================= |
177 | // function : Overlaps |
178 | // purpose : |
179 | // ======================================================================= |
114b7bf1 |
180 | Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const TColgp_Array1OfPnt& theArrayOfPts, |
7ab15952 |
181 | Select3D_TypeOfSensitivity theSensType, |
d7fa57a7 |
182 | const SelectMgr_ViewClipRange& theClipRange, |
4a056d20 |
183 | SelectBasics_PickResult& thePickResult) const |
f751596e |
184 | { |
185 | for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next()) |
186 | { |
d7fa57a7 |
187 | if (anIter.Value()->Overlaps (theArrayOfPts, theSensType, theClipRange, thePickResult)) |
f751596e |
188 | return Standard_True; |
189 | } |
190 | |
191 | return Standard_False; |
192 | } |
193 | |
194 | // ======================================================================= |
195 | // function : Overlaps |
196 | // purpose : |
197 | // ======================================================================= |
7ab15952 |
198 | Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const gp_Pnt& thePnt1, |
199 | const gp_Pnt& thePnt2, |
d7fa57a7 |
200 | const SelectMgr_ViewClipRange& theClipRange, |
4a056d20 |
201 | SelectBasics_PickResult& thePickResult) const |
f751596e |
202 | { |
203 | for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next()) |
204 | { |
d7fa57a7 |
205 | if (anIter.Value()->Overlaps (thePnt1, thePnt2, theClipRange, thePickResult)) |
f751596e |
206 | return Standard_True; |
207 | } |
208 | |
209 | return Standard_False; |
210 | } |
211 | |
212 | // ======================================================================= |
213 | // function : Overlaps |
214 | // purpose : |
215 | // ======================================================================= |
7ab15952 |
216 | Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const gp_Pnt& thePnt1, |
217 | const gp_Pnt& thePnt2, |
218 | const gp_Pnt& thePnt3, |
219 | Select3D_TypeOfSensitivity theSensType, |
d7fa57a7 |
220 | const SelectMgr_ViewClipRange& theClipRange, |
4a056d20 |
221 | SelectBasics_PickResult& thePickResult) const |
f751596e |
222 | { |
223 | for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next()) |
224 | { |
d7fa57a7 |
225 | if (anIter.Value()->Overlaps (thePnt1, thePnt2, thePnt3, theSensType, theClipRange, thePickResult)) |
f751596e |
226 | return Standard_True; |
227 | } |
228 | |
229 | return Standard_False; |
230 | } |
231 | |
871dcdc2 |
232 | // ======================================================================= |
233 | // function : GetPlanes |
234 | // purpose : |
235 | // ======================================================================= |
236 | void SelectMgr_TriangularFrustumSet::GetPlanes (NCollection_Vector<SelectMgr_Vec4>& thePlaneEquations) const |
237 | { |
238 | thePlaneEquations.Clear(); |
239 | |
240 | for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next()) |
241 | { |
242 | anIter.Value()->GetPlanes (thePlaneEquations); |
243 | } |
244 | } |
245 | |
f751596e |
246 | #undef MEMORY_BLOCK_SIZE |