f751596e |
1 | // Created on: 2014-05-22 |
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 <NCollection_Vector.hxx> |
17 | #include <Poly_Array1OfTriangle.hxx> |
18 | |
19 | #include <SelectMgr_RectangularFrustum.hxx> |
20 | |
f751596e |
21 | // ======================================================================= |
22 | // function : segmentSegmentDistance |
23 | // purpose : |
24 | // ======================================================================= |
25 | void SelectMgr_RectangularFrustum::segmentSegmentDistance (const gp_Pnt& theSegPnt1, |
26 | const gp_Pnt& theSegPnt2, |
27 | Standard_Real& theDepth) |
28 | { |
3bf9a45f |
29 | gp_XYZ anU = theSegPnt2.XYZ() - theSegPnt1.XYZ(); |
30 | gp_XYZ aV = myViewRayDir.XYZ(); |
31 | gp_XYZ aW = theSegPnt1.XYZ() - myNearPickedPnt.XYZ(); |
32 | |
33 | Standard_Real anA = anU.Dot (anU); |
34 | Standard_Real aB = anU.Dot (aV); |
35 | Standard_Real aC = aV.Dot (aV); |
36 | Standard_Real aD = anU.Dot (aW); |
37 | Standard_Real anE = aV.Dot (aW); |
f751596e |
38 | Standard_Real aCoef = anA * aC - aB * aB; |
a52c06d5 |
39 | Standard_Real aSn = aCoef; |
f751596e |
40 | Standard_Real aTc, aTn, aTd = aCoef; |
41 | |
42 | if (aCoef < Precision::Confusion()) |
43 | { |
f751596e |
44 | aTn = anE; |
45 | aTd = aC; |
46 | } |
47 | else |
48 | { |
49 | aSn = (aB * anE - aC * aD); |
50 | aTn = (anA * anE - aB * aD); |
51 | if (aSn < 0.0) |
52 | { |
f751596e |
53 | aTn = anE; |
54 | aTd = aC; |
55 | } |
a52c06d5 |
56 | else if (aSn > aCoef) |
f751596e |
57 | { |
f751596e |
58 | aTn = anE + aB; |
59 | aTd = aC; |
60 | } |
61 | } |
62 | |
63 | if (aTn < 0.0) |
64 | { |
65 | aTn = 0.0; |
f751596e |
66 | } |
67 | else if (aTn > aTd) |
68 | { |
69 | aTn = aTd; |
f751596e |
70 | } |
f751596e |
71 | aTc = (Abs (aTn) < Precision::Confusion() ? 0.0 : aTn / aTd); |
72 | |
3bf9a45f |
73 | gp_Pnt aClosestPnt = myNearPickedPnt.XYZ() + myViewRayDir.XYZ() * aTc; |
7479f643 |
74 | theDepth = myNearPickedPnt.Distance (aClosestPnt) * myScale; |
f751596e |
75 | } |
76 | |
77 | // ======================================================================= |
78 | // function : segmentPlaneIntersection |
79 | // purpose : |
80 | // ======================================================================= |
3bf9a45f |
81 | void SelectMgr_RectangularFrustum::segmentPlaneIntersection (const gp_Vec& thePlane, |
f751596e |
82 | const gp_Pnt& thePntOnPlane, |
83 | Standard_Real& theDepth) |
84 | { |
3bf9a45f |
85 | gp_XYZ anU = myViewRayDir.XYZ(); |
86 | gp_XYZ aW = myNearPickedPnt.XYZ() - thePntOnPlane.XYZ(); |
87 | Standard_Real aD = thePlane.Dot (anU); |
88 | Standard_Real aN = -thePlane.Dot (aW); |
f751596e |
89 | |
90 | if (Abs (aD) < Precision::Confusion()) |
91 | { |
92 | if (Abs (aN) < Precision::Angular()) |
93 | { |
94 | theDepth = DBL_MAX; |
95 | return; |
96 | } |
97 | else |
98 | { |
99 | theDepth = DBL_MAX; |
100 | return; |
101 | } |
102 | } |
103 | |
104 | Standard_Real aParam = aN / aD; |
105 | if (aParam < 0.0 || aParam > 1.0) |
106 | { |
107 | theDepth = DBL_MAX; |
108 | return; |
109 | } |
110 | |
3bf9a45f |
111 | gp_Pnt aClosestPnt = myNearPickedPnt.XYZ() + anU * aParam; |
7479f643 |
112 | theDepth = myNearPickedPnt.Distance (aClosestPnt) * myScale; |
f751596e |
113 | } |
114 | |
2157d6ac |
115 | namespace |
116 | { |
3bf9a45f |
117 | // ======================================================================= |
118 | // function : computeFrustum |
119 | // purpose : Computes base frustum data: its vertices and edge directions |
120 | // ======================================================================= |
121 | void computeFrustum (const gp_Pnt2d theMinPnt, const gp_Pnt2d& theMaxPnt, |
122 | const Handle(SelectMgr_FrustumBuilder)& theBuilder, |
123 | gp_Pnt* theVertices, gp_Vec* theEdges) |
124 | { |
125 | // LeftTopNear |
126 | theVertices[0] = theBuilder->ProjectPntOnViewPlane (theMinPnt.X(), |
127 | theMaxPnt.Y(), |
128 | 0.0); |
129 | // LeftTopFar |
130 | theVertices[1] = theBuilder->ProjectPntOnViewPlane (theMinPnt.X(), |
131 | theMaxPnt.Y(), |
132 | 1.0); |
133 | // LeftBottomNear |
134 | theVertices[2] = theBuilder->ProjectPntOnViewPlane (theMinPnt.X(), |
135 | theMinPnt.Y(), |
136 | 0.0); |
137 | // LeftBottomFar |
138 | theVertices[3] = theBuilder->ProjectPntOnViewPlane (theMinPnt.X(), |
139 | theMinPnt.Y(), |
140 | 1.0); |
141 | // RightTopNear |
142 | theVertices[4] = theBuilder->ProjectPntOnViewPlane (theMaxPnt.X(), |
143 | theMaxPnt.Y(), |
144 | 0.0); |
145 | // RightTopFar |
146 | theVertices[5] = theBuilder->ProjectPntOnViewPlane (theMaxPnt.X(), |
147 | theMaxPnt.Y(), |
148 | 1.0); |
149 | // RightBottomNear |
150 | theVertices[6] = theBuilder->ProjectPntOnViewPlane (theMaxPnt.X(), |
151 | theMinPnt.Y(), |
152 | 0.0); |
153 | // RightBottomFar |
154 | theVertices[7] = theBuilder->ProjectPntOnViewPlane (theMaxPnt.X(), |
155 | theMinPnt.Y(), |
156 | 1.0); |
157 | |
158 | // Horizontal |
159 | theEdges[0] = theVertices[4].XYZ() - theVertices[0].XYZ(); |
160 | // Vertical |
161 | theEdges[1] = theVertices[2].XYZ() - theVertices[0].XYZ(); |
162 | // LeftLower |
163 | theEdges[2] = theVertices[2].XYZ() - theVertices[3].XYZ(); |
164 | // RightLower |
165 | theEdges[3] = theVertices[6].XYZ() - theVertices[7].XYZ(); |
166 | // LeftUpper |
167 | theEdges[4] = theVertices[0].XYZ() - theVertices[1].XYZ(); |
168 | // RightUpper |
169 | theEdges[5] = theVertices[4].XYZ() - theVertices[5].XYZ(); |
170 | } |
171 | |
2157d6ac |
172 | // ======================================================================= |
173 | // function : computeNormals |
174 | // purpose : Computes normals to frustum faces |
175 | // ======================================================================= |
3bf9a45f |
176 | void computeNormals (const gp_Vec* theEdges, gp_Vec* theNormals) |
2157d6ac |
177 | { |
178 | // Top |
3bf9a45f |
179 | theNormals[0] = theEdges[0].Crossed (theEdges[4]); |
2157d6ac |
180 | // Bottom |
3bf9a45f |
181 | theNormals[1] = theEdges[2].Crossed (theEdges[3]); |
2157d6ac |
182 | // Left |
3bf9a45f |
183 | theNormals[2] = theEdges[4].Crossed (theEdges[1]); |
2157d6ac |
184 | // Right |
3bf9a45f |
185 | theNormals[3] = theEdges[5].Crossed (theEdges[3]); |
2157d6ac |
186 | // Near |
3bf9a45f |
187 | theNormals[4] = theEdges[0].Crossed (theEdges[1]); |
2157d6ac |
188 | // Far |
3bf9a45f |
189 | theNormals[5] = -theNormals[4]; |
2157d6ac |
190 | } |
191 | } |
192 | |
f751596e |
193 | // ======================================================================= |
3bf9a45f |
194 | // function : cacheVertexProjections |
195 | // purpose : Caches projection of frustum's vertices onto its plane directions |
196 | // and {i, j, k} |
f751596e |
197 | // ======================================================================= |
3bf9a45f |
198 | void SelectMgr_RectangularFrustum::cacheVertexProjections (SelectMgr_RectangularFrustum* theFrustum) |
f751596e |
199 | { |
3bf9a45f |
200 | if (theFrustum->myIsOrthographic) |
f751596e |
201 | { |
3bf9a45f |
202 | // project vertices onto frustum normals |
203 | // Since orthographic view volume's faces are always a pairwise translation of |
204 | // one another, only 2 vertices that belong to opposite faces can be projected |
205 | // to simplify calculations. |
206 | Standard_Integer aVertIdxs[6] = { LeftTopNear, LeftBottomNear, // opposite planes in height direction |
207 | LeftBottomNear, RightBottomNear, // opposite planes in width direcion |
208 | LeftBottomFar, RightBottomNear }; // opposite planes in depth direction |
209 | for (Standard_Integer aPlaneIdx = 0; aPlaneIdx < 5; aPlaneIdx += 2) |
f751596e |
210 | { |
3bf9a45f |
211 | Standard_Real aProj1 = theFrustum->myPlanes[aPlaneIdx].XYZ().Dot (theFrustum->myVertices[aVertIdxs[aPlaneIdx]].XYZ()); |
212 | Standard_Real aProj2 = theFrustum->myPlanes[aPlaneIdx].XYZ().Dot (theFrustum->myVertices[aVertIdxs[aPlaneIdx + 1]].XYZ()); |
213 | theFrustum->myMinVertsProjections[aPlaneIdx] = Min (aProj1, aProj2); |
214 | theFrustum->myMaxVertsProjections[aPlaneIdx] = Max (aProj1, aProj2); |
f751596e |
215 | } |
f751596e |
216 | } |
3bf9a45f |
217 | else |
f751596e |
218 | { |
3bf9a45f |
219 | // project all vertices onto frustum normals |
220 | for (Standard_Integer aPlaneIdx = 0; aPlaneIdx < 6; ++aPlaneIdx) |
221 | { |
222 | Standard_Real aMax = -DBL_MAX; |
223 | Standard_Real aMin = DBL_MAX; |
224 | const gp_XYZ& aPlane = theFrustum->myPlanes[aPlaneIdx].XYZ(); |
225 | for (Standard_Integer aVertIdx = 0; aVertIdx < 8; ++aVertIdx) |
226 | { |
227 | Standard_Real aProjection = aPlane.Dot (theFrustum->myVertices[aVertIdx].XYZ()); |
228 | aMin = Min (aMin, aProjection); |
229 | aMax = Max (aMax, aProjection); |
230 | } |
231 | theFrustum->myMinVertsProjections[aPlaneIdx] = aMin; |
232 | theFrustum->myMaxVertsProjections[aPlaneIdx] = aMax; |
233 | } |
234 | } |
f751596e |
235 | |
3bf9a45f |
236 | // project vertices onto {i, j, k} |
f751596e |
237 | for (Standard_Integer aDim = 0; aDim < 3; ++aDim) |
238 | { |
239 | Standard_Real aMax = -DBL_MAX; |
3bf9a45f |
240 | Standard_Real aMin = DBL_MAX; |
f751596e |
241 | for (Standard_Integer aVertIdx = 0; aVertIdx < 8; ++aVertIdx) |
242 | { |
3bf9a45f |
243 | const gp_XYZ& aVert = theFrustum->myVertices[aVertIdx].XYZ(); |
244 | aMax = Max (aVert.GetData()[aDim], aMax); |
245 | aMin = Min (aVert.GetData()[aDim], aMin); |
f751596e |
246 | } |
3bf9a45f |
247 | theFrustum->myMaxOrthoVertsProjections[aDim] = aMax; |
248 | theFrustum->myMinOrthoVertsProjections[aDim] = aMin; |
f751596e |
249 | } |
3bf9a45f |
250 | } |
251 | |
252 | // ======================================================================= |
253 | // function : Build |
254 | // purpose : Build volume according to the point and given pixel |
255 | // tolerance |
256 | // ======================================================================= |
257 | void SelectMgr_RectangularFrustum::Build (const gp_Pnt2d &thePoint) |
258 | { |
259 | myNearPickedPnt = myBuilder->ProjectPntOnViewPlane (thePoint.X(), thePoint.Y(), 0.0); |
260 | myFarPickedPnt = myBuilder->ProjectPntOnViewPlane (thePoint.X(), thePoint.Y(), 1.0); |
261 | myViewRayDir = myFarPickedPnt.XYZ() - myNearPickedPnt.XYZ(); |
262 | myMousePos = thePoint; |
f751596e |
263 | |
3bf9a45f |
264 | gp_Pnt2d aMinPnt (thePoint.X() - myPixelTolerance * 0.5, |
265 | thePoint.Y() - myPixelTolerance * 0.5); |
266 | gp_Pnt2d aMaxPnt (thePoint.X() + myPixelTolerance * 0.5, |
267 | thePoint.Y() + myPixelTolerance * 0.5); |
268 | |
269 | // calculate base frustum characteristics: vertices and edge directions |
270 | computeFrustum (aMinPnt, aMaxPnt, myBuilder, myVertices, myEdgeDirs); |
271 | |
272 | // compute frustum normals |
273 | computeNormals (myEdgeDirs, myPlanes); |
274 | |
275 | // compute vertices projections onto frustum normals and |
276 | // {i, j, k} vectors and store them to corresponding class fields |
277 | cacheVertexProjections (this); |
7479f643 |
278 | |
e9312c0f |
279 | myViewClipRange.Clear(); |
280 | |
7479f643 |
281 | myScale = 1.0; |
f751596e |
282 | } |
283 | |
284 | // ======================================================================= |
285 | // function : Build |
286 | // purpose : Build volume according to the selected rectangle |
287 | // ======================================================================= |
288 | void SelectMgr_RectangularFrustum::Build (const gp_Pnt2d& theMinPnt, |
289 | const gp_Pnt2d& theMaxPnt) |
290 | { |
291 | myNearPickedPnt = myBuilder->ProjectPntOnViewPlane ((theMinPnt.X() + theMaxPnt.X()) * 0.5, |
292 | (theMinPnt.Y() + theMaxPnt.Y()) * 0.5, |
293 | 0.0); |
294 | myFarPickedPnt = myBuilder->ProjectPntOnViewPlane ((theMinPnt.X() + theMaxPnt.X()) * 0.5, |
295 | (theMinPnt.Y() + theMaxPnt.Y()) * 0.5, |
296 | 1.0); |
3bf9a45f |
297 | myViewRayDir = myFarPickedPnt.XYZ() - myNearPickedPnt.XYZ(); |
f751596e |
298 | |
3bf9a45f |
299 | // calculate base frustum characteristics: vertices and edge directions |
300 | computeFrustum (theMinPnt, theMaxPnt, myBuilder, myVertices, myEdgeDirs); |
f751596e |
301 | |
3bf9a45f |
302 | // compute frustum normals |
303 | computeNormals (myEdgeDirs, myPlanes); |
f751596e |
304 | |
3bf9a45f |
305 | // compute vertices projections onto frustum normals and |
306 | // {i, j, k} vectors and store them to corresponding class fields |
307 | cacheVertexProjections (this); |
7479f643 |
308 | |
e9312c0f |
309 | myViewClipRange.Clear(); |
310 | |
7479f643 |
311 | myScale = 1.0; |
f751596e |
312 | } |
313 | |
314 | // ======================================================================= |
3bf9a45f |
315 | // function : ScaleAndTransform |
316 | // purpose : IMPORTANT: Scaling makes sense only for frustum built on a single point! |
317 | // Note that this method does not perform any checks on type of the frustum. |
318 | // Returns a copy of the frustum resized according to the scale factor given |
319 | // and transforms it using the matrix given. |
320 | // There are no default parameters, but in case if: |
321 | // - transformation only is needed: @theScaleFactor must be initialized |
322 | // as any negative value; |
323 | // - scale only is needed: @theTrsf must be set to gp_Identity. |
f751596e |
324 | // ======================================================================= |
3bf9a45f |
325 | NCollection_Handle<SelectMgr_BaseFrustum> SelectMgr_RectangularFrustum::ScaleAndTransform (const Standard_Integer theScaleFactor, |
91d96372 |
326 | const gp_GTrsf& theTrsf) |
f751596e |
327 | { |
3bf9a45f |
328 | Standard_ASSERT_RAISE (theScaleFactor > 0, |
329 | "Error! Pixel tolerance for selection should be greater than zero"); |
330 | |
f751596e |
331 | SelectMgr_RectangularFrustum* aRes = new SelectMgr_RectangularFrustum(); |
3bf9a45f |
332 | const Standard_Boolean isToScale = theScaleFactor != 1; |
333 | const Standard_Boolean isToTrsf = theTrsf.Form() != gp_Identity; |
f751596e |
334 | |
3bf9a45f |
335 | if (!isToScale && !isToTrsf) |
336 | return aRes; |
f751596e |
337 | |
338 | aRes->myIsOrthographic = myIsOrthographic; |
3bf9a45f |
339 | SelectMgr_RectangularFrustum* aRef = this; |
f751596e |
340 | |
3bf9a45f |
341 | if (isToScale) |
28ee613b |
342 | { |
3bf9a45f |
343 | aRes->myNearPickedPnt = myNearPickedPnt; |
344 | aRes->myFarPickedPnt = myFarPickedPnt; |
345 | aRes->myViewRayDir = myViewRayDir; |
28ee613b |
346 | |
3bf9a45f |
347 | const gp_Pnt2d aMinPnt (myMousePos.X() - theScaleFactor * 0.5, |
348 | myMousePos.Y() - theScaleFactor * 0.5); |
349 | const gp_Pnt2d aMaxPnt (myMousePos.X() + theScaleFactor * 0.5, |
350 | myMousePos.Y() + theScaleFactor * 0.5); |
28ee613b |
351 | |
3bf9a45f |
352 | // recompute base frustum characteristics from scratch |
353 | computeFrustum (aMinPnt, aMaxPnt, myBuilder, aRes->myVertices, aRes->myEdgeDirs); |
f751596e |
354 | |
3bf9a45f |
355 | aRef = aRes; |
f751596e |
356 | } |
357 | |
3bf9a45f |
358 | if (isToTrsf) |
f751596e |
359 | { |
91d96372 |
360 | const Standard_Real aRefScale = aRef->myFarPickedPnt.SquareDistance (aRef->myNearPickedPnt); |
361 | |
362 | gp_Pnt aPoint = aRef->myNearPickedPnt; |
363 | theTrsf.Transforms (aPoint.ChangeCoord()); |
364 | aRes->myNearPickedPnt = aPoint; |
365 | |
366 | aPoint.SetXYZ (aRef->myFarPickedPnt.XYZ()); |
367 | theTrsf.Transforms (aPoint.ChangeCoord()); |
368 | aRes->myFarPickedPnt = aPoint; |
369 | |
3bf9a45f |
370 | aRes->myViewRayDir = aRes->myFarPickedPnt.XYZ() - aRes->myNearPickedPnt.XYZ(); |
371 | |
91d96372 |
372 | for (Standard_Integer anIt = 0; anIt < 8; anIt++) |
373 | { |
374 | aPoint = aRef->myVertices[anIt]; |
375 | theTrsf.Transforms (aPoint.ChangeCoord()); |
376 | aRes->myVertices[anIt] = aPoint; |
377 | } |
3bf9a45f |
378 | |
379 | // Horizontal |
380 | aRes->myEdgeDirs[0] = aRes->myVertices[4].XYZ() - aRes->myVertices[0].XYZ(); |
381 | // Vertical |
382 | aRes->myEdgeDirs[1] = aRes->myVertices[2].XYZ() - aRes->myVertices[0].XYZ(); |
383 | // LeftLower |
384 | aRes->myEdgeDirs[2] = aRes->myVertices[2].XYZ() - aRes->myVertices[3].XYZ(); |
385 | // RightLower |
386 | aRes->myEdgeDirs[3] = aRes->myVertices[6].XYZ() - aRes->myVertices[7].XYZ(); |
387 | // LeftUpper |
388 | aRes->myEdgeDirs[4] = aRes->myVertices[0].XYZ() - aRes->myVertices[1].XYZ(); |
389 | // RightUpper |
390 | aRes->myEdgeDirs[5] = aRes->myVertices[4].XYZ() - aRes->myVertices[5].XYZ(); |
7479f643 |
391 | |
91d96372 |
392 | // Compute scale to transform depth from local coordinate system to world coordinate system |
393 | aRes->myScale = Sqrt (aRefScale / aRes->myFarPickedPnt.SquareDistance (aRes->myNearPickedPnt)); |
f751596e |
394 | } |
395 | |
3bf9a45f |
396 | // compute frustum normals |
397 | computeNormals (aRes->myEdgeDirs, aRes->myPlanes); |
398 | |
399 | cacheVertexProjections (aRes); |
f751596e |
400 | |
e9312c0f |
401 | aRes->myViewClipRange = myViewClipRange; |
402 | |
f751596e |
403 | return NCollection_Handle<SelectMgr_BaseFrustum> (aRes); |
404 | } |
405 | |
406 | // ======================================================================= |
407 | // function : Overlaps |
408 | // purpose : Returns true if selecting volume is overlapped by |
409 | // axis-aligned bounding box with minimum corner at point |
410 | // theMinPnt and maximum at point theMaxPnt |
411 | // ======================================================================= |
2157d6ac |
412 | Standard_Boolean SelectMgr_RectangularFrustum::Overlaps (const SelectMgr_Vec3& theBoxMin, |
413 | const SelectMgr_Vec3& theBoxMax, |
414 | Standard_Boolean* theInside) |
f751596e |
415 | { |
2157d6ac |
416 | return hasOverlap (theBoxMin, theBoxMax, theInside); |
f751596e |
417 | } |
418 | |
419 | // ======================================================================= |
420 | // function : Overlaps |
421 | // purpose : SAT intersection test between defined volume and |
422 | // given axis-aligned box |
423 | // ======================================================================= |
3bf9a45f |
424 | Standard_Boolean SelectMgr_RectangularFrustum::Overlaps (const SelectMgr_Vec3& theBoxMin, |
425 | const SelectMgr_Vec3& theBoxMax, |
7ab15952 |
426 | Standard_Real& theDepth) |
f751596e |
427 | { |
3bf9a45f |
428 | if (!hasOverlap (theBoxMin, theBoxMax)) |
f751596e |
429 | return Standard_False; |
430 | |
3bf9a45f |
431 | gp_Pnt aNearestPnt (RealLast(), RealLast(), RealLast()); |
432 | aNearestPnt.SetX (Max (Min (myNearPickedPnt.X(), theBoxMax.x()), theBoxMin.x())); |
433 | aNearestPnt.SetY (Max (Min (myNearPickedPnt.Y(), theBoxMax.y()), theBoxMin.y())); |
434 | aNearestPnt.SetZ (Max (Min (myNearPickedPnt.Z(), theBoxMax.z()), theBoxMin.z())); |
f751596e |
435 | |
3bf9a45f |
436 | theDepth = aNearestPnt.Distance (myNearPickedPnt); |
f751596e |
437 | |
e9312c0f |
438 | return isViewClippingOk (theDepth); |
f751596e |
439 | } |
440 | |
441 | // ======================================================================= |
442 | // function : Overlaps |
443 | // purpose : Intersection test between defined volume and given point |
444 | // ======================================================================= |
7ab15952 |
445 | Standard_Boolean SelectMgr_RectangularFrustum::Overlaps (const gp_Pnt& thePnt, |
446 | Standard_Real& theDepth) |
f751596e |
447 | { |
448 | if (!hasOverlap (thePnt)) |
449 | return Standard_False; |
450 | |
3bf9a45f |
451 | gp_XYZ aV = thePnt.XYZ() - myNearPickedPnt.XYZ(); |
452 | gp_Pnt aDetectedPnt = |
453 | myNearPickedPnt.XYZ() + myViewRayDir.XYZ() * (aV.Dot (myViewRayDir.XYZ()) / myViewRayDir.Dot (myViewRayDir)); |
f751596e |
454 | |
7479f643 |
455 | theDepth = aDetectedPnt.Distance (myNearPickedPnt) * myScale; |
f751596e |
456 | |
e9312c0f |
457 | return isViewClippingOk (theDepth); |
f751596e |
458 | } |
459 | |
3bf9a45f |
460 | // ======================================================================= |
461 | // function : Overlaps |
462 | // purpose : Intersection test between defined volume and given point |
463 | // ======================================================================= |
464 | Standard_Boolean SelectMgr_RectangularFrustum::Overlaps (const gp_Pnt& thePnt) |
465 | { |
466 | return hasOverlap (thePnt); |
467 | } |
468 | |
f751596e |
469 | // ======================================================================= |
470 | // function : Overlaps |
471 | // purpose : Checks if line segment overlaps selecting frustum |
472 | // ======================================================================= |
7ab15952 |
473 | Standard_Boolean SelectMgr_RectangularFrustum::Overlaps (const gp_Pnt& thePnt1, |
474 | const gp_Pnt& thePnt2, |
475 | Standard_Real& theDepth) |
f751596e |
476 | { |
477 | theDepth = -DBL_MAX; |
478 | if (!hasOverlap (thePnt1, thePnt2)) |
479 | return Standard_False; |
480 | |
481 | segmentSegmentDistance (thePnt1, thePnt2, theDepth); |
e9312c0f |
482 | |
483 | return isViewClippingOk (theDepth); |
f751596e |
484 | } |
485 | |
486 | // ======================================================================= |
487 | // function : Overlaps |
488 | // purpose : SAT intersection test between defined volume and given |
489 | // ordered set of points, representing line segments. The test |
490 | // may be considered of interior part or boundary line defined |
491 | // by segments depending on given sensitivity type |
492 | // ======================================================================= |
114b7bf1 |
493 | Standard_Boolean SelectMgr_RectangularFrustum::Overlaps (const TColgp_Array1OfPnt& theArrayOfPnts, |
7ab15952 |
494 | Select3D_TypeOfSensitivity theSensType, |
495 | Standard_Real& theDepth) |
f751596e |
496 | { |
497 | if (theSensType == Select3D_TOS_BOUNDARY) |
498 | { |
499 | Standard_Integer aMatchingSegmentsNb = -1; |
500 | theDepth = DBL_MAX; |
114b7bf1 |
501 | const Standard_Integer aLower = theArrayOfPnts.Lower(); |
502 | const Standard_Integer anUpper = theArrayOfPnts.Upper(); |
f751596e |
503 | for (Standard_Integer aPntIter = aLower; aPntIter <= anUpper; ++aPntIter) |
504 | { |
114b7bf1 |
505 | const gp_Pnt& aStartPnt = theArrayOfPnts.Value (aPntIter); |
506 | const gp_Pnt& aEndPnt = theArrayOfPnts.Value (aPntIter == anUpper ? aLower : (aPntIter + 1)); |
f751596e |
507 | if (hasOverlap (aStartPnt, aEndPnt)) |
508 | { |
509 | aMatchingSegmentsNb++; |
510 | Standard_Real aSegmentDepth = RealLast(); |
511 | segmentSegmentDistance (aStartPnt, aEndPnt, aSegmentDepth); |
512 | theDepth = Min (theDepth, aSegmentDepth); |
513 | } |
514 | } |
515 | |
516 | if (aMatchingSegmentsNb == -1) |
517 | return Standard_False; |
518 | } |
519 | else if (theSensType == Select3D_TOS_INTERIOR) |
520 | { |
3bf9a45f |
521 | gp_Vec aPolyNorm (gp_XYZ (RealLast(), RealLast(), RealLast())); |
f751596e |
522 | if (!hasOverlap (theArrayOfPnts, aPolyNorm)) |
523 | return Standard_False; |
524 | |
525 | segmentPlaneIntersection (aPolyNorm, |
114b7bf1 |
526 | theArrayOfPnts.Value (theArrayOfPnts.Lower()), |
f751596e |
527 | theDepth); |
528 | } |
529 | |
e9312c0f |
530 | return isViewClippingOk (theDepth); |
f751596e |
531 | } |
532 | |
533 | // ======================================================================= |
534 | // function : Overlaps |
535 | // purpose : SAT intersection test between defined volume and given |
536 | // triangle. The test may be considered of interior part or |
537 | // boundary line defined by triangle vertices depending on |
538 | // given sensitivity type |
539 | // ======================================================================= |
7ab15952 |
540 | Standard_Boolean SelectMgr_RectangularFrustum::Overlaps (const gp_Pnt& thePnt1, |
541 | const gp_Pnt& thePnt2, |
542 | const gp_Pnt& thePnt3, |
543 | Select3D_TypeOfSensitivity theSensType, |
544 | Standard_Real& theDepth) |
f751596e |
545 | { |
546 | if (theSensType == Select3D_TOS_BOUNDARY) |
547 | { |
114b7bf1 |
548 | const gp_Pnt aPntsArrayBuf[4] = { thePnt1, thePnt2, thePnt3, thePnt1 }; |
549 | const TColgp_Array1OfPnt aPntsArray (aPntsArrayBuf[0], 1, 4); |
f751596e |
550 | return Overlaps (aPntsArray, Select3D_TOS_BOUNDARY, theDepth); |
551 | } |
552 | else if (theSensType == Select3D_TOS_INTERIOR) |
553 | { |
3bf9a45f |
554 | gp_Vec aTriangleNormal (gp_XYZ (RealLast(), RealLast(), RealLast())); |
f751596e |
555 | if (!hasOverlap (thePnt1, thePnt2, thePnt3, aTriangleNormal)) |
556 | return Standard_False; |
557 | |
558 | // check if intersection point belongs to triangle's interior part |
3bf9a45f |
559 | gp_XYZ aTrEdges[3] = { thePnt2.XYZ() - thePnt1.XYZ(), |
560 | thePnt3.XYZ() - thePnt2.XYZ(), |
561 | thePnt1.XYZ() - thePnt3.XYZ() }; |
7e17e8f0 |
562 | |
3bf9a45f |
563 | Standard_Real anAlpha = aTriangleNormal.Dot (myViewRayDir); |
7e17e8f0 |
564 | if (Abs (anAlpha) < gp::Resolution()) |
565 | { |
566 | // handle degenerated triangles: in this case, there is no possible way to detect overlap correctly. |
3bf9a45f |
567 | if (aTriangleNormal.SquareMagnitude() < gp::Resolution()) |
7e17e8f0 |
568 | { |
569 | theDepth = std::numeric_limits<Standard_Real>::max(); |
570 | return Standard_False; |
571 | } |
572 | |
573 | // handle the case when triangle normal and selecting frustum direction are orthogonal: for this case, overlap |
574 | // is detected correctly, and distance to triangle's plane can be measured as distance to its arbitrary vertex. |
3bf9a45f |
575 | const gp_XYZ aDiff = myNearPickedPnt.XYZ() - thePnt1.XYZ(); |
7479f643 |
576 | theDepth = aTriangleNormal.Dot (aDiff) * myScale; |
e9312c0f |
577 | |
578 | return isViewClippingOk (theDepth); |
7e17e8f0 |
579 | } |
580 | |
3bf9a45f |
581 | gp_XYZ anEdge = (thePnt1.XYZ() - myNearPickedPnt.XYZ()) * (1.0 / anAlpha); |
f751596e |
582 | |
3bf9a45f |
583 | Standard_Real aTime = aTriangleNormal.Dot (anEdge); |
f751596e |
584 | |
3bf9a45f |
585 | gp_XYZ aVec = myViewRayDir.XYZ().Crossed (anEdge); |
f751596e |
586 | |
3bf9a45f |
587 | Standard_Real anU = aVec.Dot (aTrEdges[2]); |
588 | Standard_Real aV = aVec.Dot (aTrEdges[0]); |
f751596e |
589 | |
590 | Standard_Boolean isInterior = (aTime >= 0.0) && (anU >= 0.0) && (aV >= 0.0) && (anU + aV <= 1.0); |
591 | |
592 | if (isInterior) |
593 | { |
3bf9a45f |
594 | gp_Pnt aDetectedPnt = myNearPickedPnt.XYZ() + myViewRayDir.XYZ() * aTime; |
7479f643 |
595 | theDepth = myNearPickedPnt.Distance (aDetectedPnt) * myScale; |
e9312c0f |
596 | |
597 | return isViewClippingOk (theDepth); |
f751596e |
598 | } |
599 | |
600 | gp_Pnt aPnts[3] = {thePnt1, thePnt2, thePnt3}; |
601 | Standard_Real aMinDist = RealLast(); |
602 | Standard_Integer aNearestEdgeIdx = -1; |
3bf9a45f |
603 | gp_Pnt aPtOnPlane = myNearPickedPnt.XYZ() + myViewRayDir.XYZ() * aTime; |
f751596e |
604 | for (Standard_Integer anEdgeIdx = 0; anEdgeIdx < 3; ++anEdgeIdx) |
605 | { |
3bf9a45f |
606 | gp_XYZ aW = aPtOnPlane.XYZ() - aPnts[anEdgeIdx].XYZ(); |
607 | Standard_Real aCoef = aTrEdges[anEdgeIdx].Dot (aW) / aTrEdges[anEdgeIdx].Dot (aTrEdges[anEdgeIdx]); |
608 | Standard_Real aDist = aPtOnPlane.Distance (aPnts[anEdgeIdx].XYZ() + aCoef * aTrEdges[anEdgeIdx]); |
f751596e |
609 | if (aMinDist > aDist) |
610 | { |
611 | aMinDist = aDist; |
612 | aNearestEdgeIdx = anEdgeIdx; |
613 | } |
614 | } |
615 | segmentSegmentDistance (aPnts[aNearestEdgeIdx], aPnts[(aNearestEdgeIdx + 1) % 3], theDepth); |
616 | } |
617 | |
e9312c0f |
618 | return isViewClippingOk (theDepth); |
f751596e |
619 | } |
620 | |
621 | // ======================================================================= |
622 | // function : DistToGeometryCenter |
623 | // purpose : Measures distance between 3d projection of user-picked |
624 | // screen point and given point theCOG |
625 | // ======================================================================= |
7ab15952 |
626 | Standard_Real SelectMgr_RectangularFrustum::DistToGeometryCenter (const gp_Pnt& theCOG) |
f751596e |
627 | { |
7479f643 |
628 | return theCOG.Distance (myNearPickedPnt) * myScale; |
f751596e |
629 | } |
630 | |
631 | // ======================================================================= |
632 | // function : DetectedPoint |
633 | // purpose : Calculates the point on a view ray that was detected during |
634 | // the run of selection algo by given depth |
635 | // ======================================================================= |
3bf9a45f |
636 | gp_Pnt SelectMgr_RectangularFrustum::DetectedPoint (const Standard_Real theDepth) const |
f751596e |
637 | { |
3bf9a45f |
638 | return myNearPickedPnt.XYZ() + myViewRayDir.Normalized().XYZ() * theDepth; |
f751596e |
639 | } |
640 | |
641 | // ======================================================================= |
e9312c0f |
642 | // function : computeClippingRange |
643 | // purpose : |
f751596e |
644 | // ======================================================================= |
e9312c0f |
645 | void SelectMgr_RectangularFrustum::computeClippingRange (const Graphic3d_SequenceOfHClipPlane& thePlanes, |
646 | Standard_Real& theDepthMin, |
647 | Standard_Real& theDepthMax) |
f751596e |
648 | { |
e9312c0f |
649 | theDepthMax = DBL_MAX; |
650 | theDepthMin = -DBL_MAX; |
f751596e |
651 | Standard_Real aPlaneA, aPlaneB, aPlaneC, aPlaneD; |
e9312c0f |
652 | for (Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (thePlanes); aPlaneIt.More(); aPlaneIt.Next()) |
f751596e |
653 | { |
654 | const Handle(Graphic3d_ClipPlane)& aClipPlane = aPlaneIt.Value(); |
655 | if (!aClipPlane->IsOn()) |
656 | continue; |
657 | |
658 | gp_Pln aGeomPlane = aClipPlane->ToPlane(); |
659 | |
660 | aGeomPlane.Coefficients (aPlaneA, aPlaneB, aPlaneC, aPlaneD); |
661 | |
662 | const gp_XYZ& aPlaneDirXYZ = aGeomPlane.Axis().Direction().XYZ(); |
663 | |
e9312c0f |
664 | Standard_Real aDotProduct = myViewRayDir.XYZ ().Dot (aPlaneDirXYZ); |
665 | Standard_Real aDistance = - myNearPickedPnt.XYZ ().Dot (aPlaneDirXYZ) |
666 | - aPlaneD; |
f751596e |
667 | |
668 | // check whether the pick line is parallel to clip plane |
669 | if (Abs (aDotProduct) < Precision::Angular()) |
670 | { |
671 | // line lies below the plane and is not clipped, skip |
672 | continue; |
673 | } |
674 | |
675 | // compute distance to point of pick line intersection with the plane |
676 | Standard_Real aParam = aDistance / aDotProduct; |
677 | |
678 | // check if ray intersects the plane, in case aIntDist < 0 |
679 | // the plane is "behind" the ray |
680 | if (aParam < 0.0) |
681 | { |
682 | continue; |
683 | } |
684 | |
3bf9a45f |
685 | const gp_Pnt anIntersectionPt = myNearPickedPnt.XYZ() + myViewRayDir.XYZ() * aParam; |
686 | const Standard_Real aDistToPln = anIntersectionPt.Distance (myNearPickedPnt); |
f751596e |
687 | |
688 | // change depth limits for case of opposite and directed planes |
689 | if (aDotProduct < 0.0) |
690 | { |
e9312c0f |
691 | theDepthMax = Min (aDistToPln, theDepthMax); |
f751596e |
692 | } |
e9312c0f |
693 | else if (aDistToPln > theDepthMin) |
f751596e |
694 | { |
e9312c0f |
695 | theDepthMin = Max (aDistToPln, theDepthMin); |
f751596e |
696 | } |
697 | } |
e9312c0f |
698 | } |
699 | |
700 | // ======================================================================= |
701 | // function : IsClipped |
702 | // purpose : Checks if the point of sensitive in which selection was |
703 | // detected belongs to the region defined by clipping planes |
704 | // ======================================================================= |
705 | Standard_Boolean SelectMgr_RectangularFrustum::IsClipped (const Graphic3d_SequenceOfHClipPlane& thePlanes, |
706 | const Standard_Real theDepth) |
707 | { |
708 | Standard_Real aMaxDepth, aMinDepth; |
709 | computeClippingRange (thePlanes, aMinDepth, aMaxDepth); |
f751596e |
710 | |
711 | return (theDepth <= aMinDepth || theDepth >= aMaxDepth); |
712 | } |
e9312c0f |
713 | |
714 | // ======================================================================= |
715 | // function : SetViewClipping |
716 | // purpose : |
717 | // ======================================================================= |
718 | void SelectMgr_RectangularFrustum::SetViewClipping (const Graphic3d_SequenceOfHClipPlane& thePlanes) |
719 | { |
720 | if (thePlanes.Size() == 0) |
721 | { |
722 | myViewClipRange.Clear(); |
723 | return; |
724 | } |
725 | |
726 | Standard_Real aMaxDepth, aMinDepth; |
727 | computeClippingRange (thePlanes, aMinDepth, aMaxDepth); |
728 | myViewClipRange.Set (aMinDepth, aMaxDepth); |
729 | } |
730 | |
731 | // ======================================================================= |
732 | // function : isViewClippingOk |
733 | // purpose : |
734 | // ======================================================================= |
735 | Standard_Boolean SelectMgr_RectangularFrustum::isViewClippingOk (const Standard_Real theDepth) const |
736 | { |
737 | if (!myViewClipRange.IsValid()) |
738 | return Standard_True; |
739 | |
740 | return myViewClipRange.MaxDepth() > theDepth |
741 | && myViewClipRange.MinDepth() < theDepth; |
742 | } |