b5ac8292 |
1 | // Created on: 2013-05-29 |
2 | // Created by: Anton POLETAEV |
3 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
4 | // |
5 | // This file is part of Open CASCADE Technology software library. |
6 | // |
d5f74e42 |
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 |
b5ac8292 |
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 <gp_Pln.hxx> |
17 | #include <Standard_ShortReal.hxx> |
18 | |
b5ac8292 |
19 | #include <Graphic3d_Camera.hxx> |
825aa485 |
20 | #include <Graphic3d_Vec4.hxx> |
21 | #include <Graphic3d_WorldViewProjState.hxx> |
b5ac8292 |
22 | |
23 | #include <Standard_Atomic.hxx> |
197ac94e |
24 | #include <Standard_Assert.hxx> |
b5ac8292 |
25 | |
ed063270 |
26 | #include <NCollection_Sequence.hxx> |
27 | |
b5ac8292 |
28 | |
29 | namespace |
30 | { |
31 | // (degrees -> radians) * 0.5 |
197ac94e |
32 | static const Standard_Real DTR_HALF = 0.5 * 0.0174532925; |
33 | |
34 | // default property values |
35 | static const Standard_Real DEFAULT_ZNEAR = 0.001; |
36 | static const Standard_Real DEFAULT_ZFAR = 3000.0; |
b5ac8292 |
37 | |
38 | // atomic state counter |
39 | static volatile Standard_Integer THE_STATE_COUNTER = 0; |
3c648527 |
40 | |
41 | // minimum camera distance |
42 | static const Standard_Real MIN_DISTANCE = Pow (0.1, ShortRealDigits() - 2); |
ea764884 |
43 | |
44 | // z-range tolerance compatible with for floating point. |
45 | static Standard_Real zEpsilon() |
46 | { |
47 | return FLT_EPSILON; |
48 | } |
49 | |
50 | // relative z-range tolerance compatible with for floating point. |
51 | static Standard_Real zEpsilon (const Standard_Real theValue) |
52 | { |
5c8908e0 |
53 | Standard_Real anAbsValue = Abs (theValue); |
54 | if (anAbsValue <= (double)FLT_MIN) |
825aa485 |
55 | { |
5c8908e0 |
56 | return FLT_MIN; |
825aa485 |
57 | } |
5c8908e0 |
58 | Standard_Real aLogRadix = Log10 (anAbsValue) / Log10 (FLT_RADIX); |
ea764884 |
59 | Standard_Real aExp = Floor (aLogRadix); |
60 | return FLT_EPSILON * Pow (FLT_RADIX, aExp); |
61 | }; |
b5ac8292 |
62 | }; |
63 | |
64 | // ======================================================================= |
65 | // function : Graphic3d_Camera |
66 | // purpose : |
67 | // ======================================================================= |
68 | Graphic3d_Camera::Graphic3d_Camera() |
69 | : myUp (0.0, 1.0, 0.0), |
70 | myEye (0.0, 0.0, -1500.0), |
71 | myCenter (0.0, 0.0, 0.0), |
b5ac8292 |
72 | myAxialScale (1.0, 1.0, 1.0), |
73 | myProjType (Projection_Orthographic), |
74 | myFOVy (45.0), |
197ac94e |
75 | myZNear (DEFAULT_ZNEAR), |
76 | myZFar (DEFAULT_ZFAR), |
b5ac8292 |
77 | myAspect (1.0), |
78 | myScale (1000.0), |
79 | myZFocus (1.0), |
80 | myZFocusType (FocusType_Relative), |
81 | myIOD (0.05), |
197ac94e |
82 | myIODType (IODType_Relative) |
b5ac8292 |
83 | { |
825aa485 |
84 | myWorldViewProjState.Initialize ((Standard_Size)Standard_Atomic_Increment (&THE_STATE_COUNTER), |
85 | (Standard_Size)Standard_Atomic_Increment (&THE_STATE_COUNTER), |
86 | this); |
b5ac8292 |
87 | } |
88 | |
89 | // ======================================================================= |
90 | // function : Graphic3d_Camera |
91 | // purpose : |
92 | // ======================================================================= |
93 | Graphic3d_Camera::Graphic3d_Camera (const Handle(Graphic3d_Camera)& theOther) |
b5ac8292 |
94 | { |
825aa485 |
95 | myWorldViewProjState.Initialize (this); |
96 | |
b5ac8292 |
97 | Copy (theOther); |
98 | } |
99 | |
100 | // ======================================================================= |
101 | // function : CopyMappingData |
102 | // purpose : |
103 | // ======================================================================= |
104 | void Graphic3d_Camera::CopyMappingData (const Handle(Graphic3d_Camera)& theOtherCamera) |
105 | { |
bf02aa7d |
106 | SetFOVy (theOtherCamera->FOVy()); |
107 | SetZRange (theOtherCamera->ZNear(), theOtherCamera->ZFar()); |
108 | SetAspect (theOtherCamera->Aspect()); |
109 | SetScale (theOtherCamera->Scale()); |
110 | SetZFocus (theOtherCamera->ZFocusType(), theOtherCamera->ZFocus()); |
111 | SetIOD (theOtherCamera->GetIODType(), theOtherCamera->IOD()); |
112 | SetProjectionType (theOtherCamera->ProjectionType()); |
b5ac8292 |
113 | } |
114 | |
115 | // ======================================================================= |
116 | // function : CopyOrientationData |
117 | // purpose : |
118 | // ======================================================================= |
119 | void Graphic3d_Camera::CopyOrientationData (const Handle(Graphic3d_Camera)& theOtherCamera) |
120 | { |
bf02aa7d |
121 | SetUp (theOtherCamera->Up()); |
122 | SetEye (theOtherCamera->Eye()); |
123 | SetCenter (theOtherCamera->Center()); |
124 | SetAxialScale (theOtherCamera->AxialScale()); |
b5ac8292 |
125 | } |
126 | |
127 | // ======================================================================= |
128 | // function : Copy |
129 | // purpose : |
130 | // ======================================================================= |
131 | void Graphic3d_Camera::Copy (const Handle(Graphic3d_Camera)& theOther) |
132 | { |
b5ac8292 |
133 | CopyMappingData (theOther); |
134 | CopyOrientationData (theOther); |
b5ac8292 |
135 | } |
136 | |
137 | // ======================================================================= |
138 | // function : SetEye |
139 | // purpose : |
140 | // ======================================================================= |
141 | void Graphic3d_Camera::SetEye (const gp_Pnt& theEye) |
142 | { |
bf02aa7d |
143 | if (Eye().IsEqual (theEye, 0.0)) |
144 | { |
145 | return; |
146 | } |
147 | |
b5ac8292 |
148 | myEye = theEye; |
197ac94e |
149 | InvalidateOrientation(); |
b5ac8292 |
150 | } |
151 | |
152 | // ======================================================================= |
153 | // function : SetCenter |
154 | // purpose : |
155 | // ======================================================================= |
156 | void Graphic3d_Camera::SetCenter (const gp_Pnt& theCenter) |
157 | { |
bf02aa7d |
158 | if (Center().IsEqual (theCenter, 0.0)) |
159 | { |
160 | return; |
161 | } |
162 | |
b5ac8292 |
163 | myCenter = theCenter; |
197ac94e |
164 | InvalidateOrientation(); |
b5ac8292 |
165 | } |
166 | |
167 | // ======================================================================= |
168 | // function : SetUp |
169 | // purpose : |
170 | // ======================================================================= |
171 | void Graphic3d_Camera::SetUp (const gp_Dir& theUp) |
172 | { |
bf02aa7d |
173 | if (Up().IsEqual (theUp, 0.0)) |
174 | { |
175 | return; |
176 | } |
177 | |
b5ac8292 |
178 | myUp = theUp; |
197ac94e |
179 | InvalidateOrientation(); |
b5ac8292 |
180 | } |
181 | |
182 | // ======================================================================= |
183 | // function : SetAxialScale |
184 | // purpose : |
185 | // ======================================================================= |
197ac94e |
186 | void Graphic3d_Camera::SetAxialScale (const gp_XYZ& theAxialScale) |
b5ac8292 |
187 | { |
bf02aa7d |
188 | if (AxialScale().IsEqual (theAxialScale, 0.0)) |
189 | { |
190 | return; |
191 | } |
192 | |
b5ac8292 |
193 | myAxialScale = theAxialScale; |
197ac94e |
194 | InvalidateOrientation(); |
b5ac8292 |
195 | } |
196 | |
197 | // ======================================================================= |
198 | // function : SetDistance |
199 | // purpose : |
200 | // ======================================================================= |
201 | void Graphic3d_Camera::SetDistance (const Standard_Real theDistance) |
202 | { |
bf02aa7d |
203 | if (Distance() == theDistance) |
204 | { |
205 | return; |
206 | } |
207 | |
b5ac8292 |
208 | gp_Vec aCenter2Eye (Direction()); |
209 | aCenter2Eye.Reverse(); |
3c648527 |
210 | |
211 | // Camera should have non-zero distance. |
212 | aCenter2Eye.Scale (Max (theDistance, MIN_DISTANCE)); |
b5ac8292 |
213 | SetEye (Center().Translated (aCenter2Eye)); |
214 | } |
215 | |
216 | // ======================================================================= |
217 | // function : Distance |
218 | // purpose : |
219 | // ======================================================================= |
220 | Standard_Real Graphic3d_Camera::Distance() const |
221 | { |
222 | return myEye.Distance (myCenter); |
223 | } |
224 | |
225 | // ======================================================================= |
226 | // function : SetDirection |
227 | // purpose : |
228 | // ======================================================================= |
229 | void Graphic3d_Camera::SetDirection (const gp_Dir& theDir) |
230 | { |
bf02aa7d |
231 | if (Direction().IsEqual (theDir, 0.0)) |
232 | { |
233 | return; |
234 | } |
235 | |
b5ac8292 |
236 | gp_Vec aScaledDir (theDir); |
237 | aScaledDir.Scale (Distance()); |
238 | aScaledDir.Reverse(); |
239 | SetEye (Center().Translated (aScaledDir)); |
240 | } |
241 | |
242 | // ======================================================================= |
243 | // function : Direction |
244 | // purpose : |
245 | // ======================================================================= |
246 | gp_Dir Graphic3d_Camera::Direction() const |
247 | { |
248 | return gp_Dir (gp_Vec (myEye, myCenter)); |
249 | } |
250 | |
251 | // ======================================================================= |
252 | // function : SetScale |
253 | // purpose : |
254 | // ======================================================================= |
255 | void Graphic3d_Camera::SetScale (const Standard_Real theScale) |
256 | { |
c357e426 |
257 | if (Scale() == theScale) |
258 | { |
259 | return; |
260 | } |
261 | |
b5ac8292 |
262 | myScale = theScale; |
263 | |
264 | switch (myProjType) |
265 | { |
266 | case Projection_Perspective : |
267 | case Projection_Stereo : |
268 | case Projection_MonoLeftEye : |
269 | case Projection_MonoRightEye : |
270 | { |
271 | Standard_Real aDistance = theScale * 0.5 / Tan(myFOVy * M_PI / 360.0); |
272 | SetDistance (aDistance); |
273 | } |
274 | |
275 | default : |
276 | break; |
277 | } |
278 | |
197ac94e |
279 | InvalidateProjection(); |
b5ac8292 |
280 | } |
281 | |
282 | // ======================================================================= |
283 | // function : Scale |
284 | // purpose : |
285 | // ======================================================================= |
286 | Standard_Real Graphic3d_Camera::Scale() const |
287 | { |
288 | switch (myProjType) |
289 | { |
290 | case Projection_Orthographic : |
291 | return myScale; |
292 | |
293 | // case Projection_Perspective : |
294 | // case Projection_Stereo : |
295 | // case Projection_MonoLeftEye : |
296 | // case Projection_MonoRightEye : |
297 | default : |
197ac94e |
298 | return Distance() * 2.0 * Tan (myFOVy * M_PI / 360.0); |
b5ac8292 |
299 | } |
300 | } |
301 | |
302 | // ======================================================================= |
303 | // function : SetProjectionType |
304 | // purpose : |
305 | // ======================================================================= |
306 | void Graphic3d_Camera::SetProjectionType (const Projection theProjectionType) |
307 | { |
c357e426 |
308 | Projection anOldType = ProjectionType(); |
b5ac8292 |
309 | |
310 | if (anOldType == theProjectionType) |
311 | { |
312 | return; |
313 | } |
314 | |
197ac94e |
315 | if (anOldType == Projection_Orthographic) |
b5ac8292 |
316 | { |
197ac94e |
317 | if (myZNear <= RealEpsilon()) |
318 | { |
319 | myZNear = DEFAULT_ZNEAR; |
320 | } |
321 | if (myZFar <= RealEpsilon()) |
322 | { |
323 | myZFar = DEFAULT_ZFAR; |
324 | } |
b5ac8292 |
325 | } |
326 | |
197ac94e |
327 | myProjType = theProjectionType; |
328 | |
329 | InvalidateProjection(); |
b5ac8292 |
330 | } |
331 | |
332 | // ======================================================================= |
333 | // function : SetFOVy |
334 | // purpose : |
335 | // ======================================================================= |
336 | void Graphic3d_Camera::SetFOVy (const Standard_Real theFOVy) |
337 | { |
c357e426 |
338 | if (FOVy() == theFOVy) |
339 | { |
340 | return; |
341 | } |
342 | |
b5ac8292 |
343 | myFOVy = theFOVy; |
c357e426 |
344 | |
197ac94e |
345 | InvalidateProjection(); |
b5ac8292 |
346 | } |
347 | |
348 | // ======================================================================= |
197ac94e |
349 | // function : SetZRange |
b5ac8292 |
350 | // purpose : |
351 | // ======================================================================= |
197ac94e |
352 | void Graphic3d_Camera::SetZRange (const Standard_Real theZNear, |
353 | const Standard_Real theZFar) |
b5ac8292 |
354 | { |
197ac94e |
355 | Standard_ASSERT_RAISE (theZFar > theZNear, "ZFar should be greater than ZNear"); |
356 | if (!IsOrthographic()) |
b5ac8292 |
357 | { |
197ac94e |
358 | Standard_ASSERT_RAISE (theZNear > 0.0, "Only positive Z-Near is allowed for perspective camera"); |
359 | Standard_ASSERT_RAISE (theZFar > 0.0, "Only positive Z-Far is allowed for perspective camera"); |
b5ac8292 |
360 | } |
361 | |
c357e426 |
362 | if (ZNear() == theZNear |
363 | && ZFar () == theZFar) |
364 | { |
365 | return; |
366 | } |
367 | |
197ac94e |
368 | myZNear = theZNear; |
369 | myZFar = theZFar; |
b5ac8292 |
370 | |
197ac94e |
371 | InvalidateProjection(); |
b5ac8292 |
372 | } |
373 | |
374 | // ======================================================================= |
375 | // function : SetAspect |
376 | // purpose : |
377 | // ======================================================================= |
378 | void Graphic3d_Camera::SetAspect (const Standard_Real theAspect) |
379 | { |
c357e426 |
380 | if (Aspect() == theAspect) |
381 | { |
382 | return; |
383 | } |
384 | |
b5ac8292 |
385 | myAspect = theAspect; |
c357e426 |
386 | |
197ac94e |
387 | InvalidateProjection(); |
b5ac8292 |
388 | } |
389 | |
390 | // ======================================================================= |
391 | // function : SetZFocus |
392 | // purpose : |
393 | // ======================================================================= |
394 | void Graphic3d_Camera::SetZFocus(const FocusType theType, const Standard_Real theZFocus) |
395 | { |
c357e426 |
396 | if (ZFocusType() == theType |
397 | && ZFocus () == theZFocus) |
398 | { |
399 | return; |
400 | } |
401 | |
b5ac8292 |
402 | myZFocusType = theType; |
c357e426 |
403 | myZFocus = theZFocus; |
404 | |
197ac94e |
405 | InvalidateProjection(); |
b5ac8292 |
406 | } |
407 | |
408 | // ======================================================================= |
409 | // function : SetIOD |
410 | // purpose : |
411 | // ======================================================================= |
412 | void Graphic3d_Camera::SetIOD (const IODType theType, const Standard_Real theIOD) |
413 | { |
bf02aa7d |
414 | if (GetIODType() == theType |
415 | && IOD () == theIOD) |
c357e426 |
416 | { |
417 | return; |
418 | } |
419 | |
b5ac8292 |
420 | myIODType = theType; |
c357e426 |
421 | myIOD = theIOD; |
422 | |
197ac94e |
423 | InvalidateProjection(); |
b5ac8292 |
424 | } |
425 | |
426 | // ======================================================================= |
427 | // function : OrthogonalizeUp |
428 | // purpose : |
429 | // ======================================================================= |
430 | void Graphic3d_Camera::OrthogonalizeUp() |
431 | { |
197ac94e |
432 | SetUp (OrthogonalizedUp()); |
b5ac8292 |
433 | } |
434 | |
435 | // ======================================================================= |
197ac94e |
436 | // function : OrthogonalizedUp |
b5ac8292 |
437 | // purpose : |
438 | // ======================================================================= |
197ac94e |
439 | gp_Dir Graphic3d_Camera::OrthogonalizedUp() const |
b5ac8292 |
440 | { |
197ac94e |
441 | gp_Dir aDir = Direction(); |
442 | gp_Dir aLeft = aDir.Crossed (Up()); |
b5ac8292 |
443 | |
197ac94e |
444 | // recompute up as: up = left x direction |
445 | return aLeft.Crossed (aDir); |
b5ac8292 |
446 | } |
447 | |
448 | // ======================================================================= |
449 | // function : Transform |
450 | // purpose : |
451 | // ======================================================================= |
452 | void Graphic3d_Camera::Transform (const gp_Trsf& theTrsf) |
453 | { |
bf02aa7d |
454 | if (theTrsf.Form() == gp_Identity) |
455 | { |
456 | return; |
457 | } |
458 | |
459 | SetUp (myUp.Transformed (theTrsf)); |
460 | SetEye (myEye.Transformed (theTrsf)); |
461 | SetCenter (myCenter.Transformed (theTrsf)); |
b5ac8292 |
462 | } |
463 | |
464 | // ======================================================================= |
465 | // function : safePointCast |
466 | // purpose : |
467 | // ======================================================================= |
197ac94e |
468 | static Graphic3d_Vec4d safePointCast (const gp_Pnt& thePnt) |
b5ac8292 |
469 | { |
470 | Standard_Real aLim = 1e15f; |
197ac94e |
471 | |
b5ac8292 |
472 | // have to deal with values greater then max float |
473 | gp_Pnt aSafePoint = thePnt; |
474 | const Standard_Real aBigFloat = aLim * 0.1f; |
475 | if (Abs (aSafePoint.X()) > aLim) |
476 | aSafePoint.SetX (aSafePoint.X() >= 0 ? aBigFloat : -aBigFloat); |
477 | if (Abs (aSafePoint.Y()) > aLim) |
478 | aSafePoint.SetY (aSafePoint.Y() >= 0 ? aBigFloat : -aBigFloat); |
479 | if (Abs (aSafePoint.Z()) > aLim) |
480 | aSafePoint.SetZ (aSafePoint.Z() >= 0 ? aBigFloat : -aBigFloat); |
481 | |
482 | // convert point |
197ac94e |
483 | Graphic3d_Vec4d aPnt (aSafePoint.X(), aSafePoint.Y(), aSafePoint.Z(), 1.0); |
b5ac8292 |
484 | |
485 | return aPnt; |
486 | } |
487 | |
488 | // ======================================================================= |
489 | // function : Project |
490 | // purpose : |
491 | // ======================================================================= |
492 | gp_Pnt Graphic3d_Camera::Project (const gp_Pnt& thePnt) const |
493 | { |
197ac94e |
494 | const Graphic3d_Mat4d& aViewMx = OrientationMatrix(); |
495 | const Graphic3d_Mat4d& aProjMx = ProjectionMatrix(); |
b5ac8292 |
496 | |
497 | // use compatible type of point |
197ac94e |
498 | Graphic3d_Vec4d aPnt = safePointCast (thePnt); |
b5ac8292 |
499 | |
500 | aPnt = aViewMx * aPnt; // convert to view coordinate space |
501 | aPnt = aProjMx * aPnt; // convert to projection coordinate space |
502 | |
503 | const Standard_Real aInvW = 1.0 / Standard_Real (aPnt.w()); |
504 | |
505 | return gp_Pnt (aPnt.x() * aInvW, aPnt.y() * aInvW, aPnt.z() * aInvW); |
506 | } |
507 | |
508 | // ======================================================================= |
509 | // function : UnProject |
510 | // purpose : |
511 | // ======================================================================= |
512 | gp_Pnt Graphic3d_Camera::UnProject (const gp_Pnt& thePnt) const |
513 | { |
197ac94e |
514 | const Graphic3d_Mat4d& aViewMx = OrientationMatrix(); |
515 | const Graphic3d_Mat4d& aProjMx = ProjectionMatrix(); |
b5ac8292 |
516 | |
197ac94e |
517 | Graphic3d_Mat4d aInvView; |
518 | Graphic3d_Mat4d aInvProj; |
b5ac8292 |
519 | |
520 | // this case should never happen |
521 | if (!aViewMx.Inverted (aInvView) || !aProjMx.Inverted (aInvProj)) |
522 | { |
523 | return gp_Pnt (0.0, 0.0, 0.0); |
524 | } |
525 | |
526 | // use compatible type of point |
197ac94e |
527 | Graphic3d_Vec4d aPnt = safePointCast (thePnt); |
b5ac8292 |
528 | |
529 | aPnt = aInvProj * aPnt; // convert to view coordinate space |
530 | aPnt = aInvView * aPnt; // convert to world coordinate space |
531 | |
532 | const Standard_Real aInvW = 1.0 / Standard_Real (aPnt.w()); |
533 | |
534 | return gp_Pnt (aPnt.x() * aInvW, aPnt.y() * aInvW, aPnt.z() * aInvW); |
535 | } |
536 | |
537 | // ======================================================================= |
538 | // function : ConvertView2Proj |
539 | // purpose : |
540 | // ======================================================================= |
541 | gp_Pnt Graphic3d_Camera::ConvertView2Proj (const gp_Pnt& thePnt) const |
542 | { |
197ac94e |
543 | const Graphic3d_Mat4d& aProjMx = ProjectionMatrix(); |
b5ac8292 |
544 | |
545 | // use compatible type of point |
197ac94e |
546 | Graphic3d_Vec4d aPnt = safePointCast (thePnt); |
b5ac8292 |
547 | |
548 | aPnt = aProjMx * aPnt; // convert to projection coordinate space |
549 | |
550 | const Standard_Real aInvW = 1.0 / Standard_Real (aPnt.w()); |
551 | |
552 | return gp_Pnt (aPnt.x() * aInvW, aPnt.y() * aInvW, aPnt.z() * aInvW); |
553 | } |
554 | |
555 | // ======================================================================= |
556 | // function : ConvertProj2View |
557 | // purpose : |
558 | // ======================================================================= |
559 | gp_Pnt Graphic3d_Camera::ConvertProj2View (const gp_Pnt& thePnt) const |
560 | { |
197ac94e |
561 | const Graphic3d_Mat4d& aProjMx = ProjectionMatrix(); |
b5ac8292 |
562 | |
197ac94e |
563 | Graphic3d_Mat4d aInvProj; |
b5ac8292 |
564 | |
565 | // this case should never happen, but... |
566 | if (!aProjMx.Inverted (aInvProj)) |
567 | { |
197ac94e |
568 | return gp_Pnt (0, 0, 0); |
b5ac8292 |
569 | } |
570 | |
571 | // use compatible type of point |
197ac94e |
572 | Graphic3d_Vec4d aPnt = safePointCast (thePnt); |
b5ac8292 |
573 | |
574 | aPnt = aInvProj * aPnt; // convert to view coordinate space |
575 | |
576 | const Standard_Real aInvW = 1.0 / Standard_Real (aPnt.w()); |
577 | |
578 | return gp_Pnt (aPnt.x() * aInvW, aPnt.y() * aInvW, aPnt.z() * aInvW); |
579 | } |
580 | |
581 | // ======================================================================= |
582 | // function : ConvertWorld2View |
583 | // purpose : |
584 | // ======================================================================= |
585 | gp_Pnt Graphic3d_Camera::ConvertWorld2View (const gp_Pnt& thePnt) const |
586 | { |
197ac94e |
587 | const Graphic3d_Mat4d& aViewMx = OrientationMatrix(); |
b5ac8292 |
588 | |
589 | // use compatible type of point |
197ac94e |
590 | Graphic3d_Vec4d aPnt = safePointCast (thePnt); |
b5ac8292 |
591 | |
592 | aPnt = aViewMx * aPnt; // convert to view coordinate space |
593 | |
594 | const Standard_Real aInvW = 1.0 / Standard_Real (aPnt.w()); |
595 | |
596 | return gp_Pnt (aPnt.x() * aInvW, aPnt.y() * aInvW, aPnt.z() * aInvW); |
597 | } |
598 | |
599 | // ======================================================================= |
600 | // function : ConvertView2World |
601 | // purpose : |
602 | // ======================================================================= |
603 | gp_Pnt Graphic3d_Camera::ConvertView2World (const gp_Pnt& thePnt) const |
604 | { |
197ac94e |
605 | const Graphic3d_Mat4d& aViewMx = OrientationMatrix(); |
b5ac8292 |
606 | |
197ac94e |
607 | Graphic3d_Mat4d aInvView; |
b5ac8292 |
608 | |
609 | if (!aViewMx.Inverted (aInvView)) |
610 | { |
611 | return gp_Pnt(0, 0, 0); |
612 | } |
613 | |
614 | // use compatible type of point |
197ac94e |
615 | Graphic3d_Vec4d aPnt = safePointCast (thePnt); |
b5ac8292 |
616 | |
617 | aPnt = aInvView * aPnt; // convert to world coordinate space |
618 | |
619 | const Standard_Real aInvW = 1.0 / Standard_Real (aPnt.w()); |
620 | |
621 | return gp_Pnt (aPnt.x() * aInvW, aPnt.y() * aInvW, aPnt.z() * aInvW); |
622 | } |
623 | |
624 | // ======================================================================= |
625 | // function : ViewDimensions |
626 | // purpose : |
627 | // ======================================================================= |
197ac94e |
628 | gp_XYZ Graphic3d_Camera::ViewDimensions() const |
b5ac8292 |
629 | { |
630 | // view plane dimensions |
539d3a1b |
631 | Standard_Real aSize = IsOrthographic() ? myScale : (2.0 * Distance() * Tan (DTR_HALF * myFOVy)); |
632 | Standard_Real aSizeX, aSizeY; |
633 | if (myAspect > 1.0) |
634 | { |
635 | aSizeX = aSize * myAspect; |
636 | aSizeY = aSize; |
637 | } |
638 | else |
639 | { |
640 | aSizeX = aSize; |
641 | aSizeY = aSize / myAspect; |
642 | } |
b5ac8292 |
643 | |
644 | // and frustum depth |
197ac94e |
645 | return gp_XYZ (aSizeX, aSizeY, myZFar - myZNear); |
b5ac8292 |
646 | } |
647 | |
648 | // ======================================================================= |
197ac94e |
649 | // function : Frustum |
b5ac8292 |
650 | // purpose : |
651 | // ======================================================================= |
197ac94e |
652 | void Graphic3d_Camera::Frustum (gp_Pln& theLeft, |
653 | gp_Pln& theRight, |
654 | gp_Pln& theBottom, |
655 | gp_Pln& theTop, |
656 | gp_Pln& theNear, |
657 | gp_Pln& theFar) const |
b5ac8292 |
658 | { |
197ac94e |
659 | gp_Vec aProjection = gp_Vec (Direction()); |
660 | gp_Vec anUp = OrthogonalizedUp(); |
661 | gp_Vec aSide = aProjection ^ anUp; |
662 | |
663 | Standard_ASSERT_RAISE ( |
664 | !aProjection.IsParallel (anUp, Precision::Angular()), |
665 | "Can not derive SIDE = PROJ x UP - directions are parallel"); |
666 | |
667 | theNear = gp_Pln (Eye().Translated (aProjection * ZNear()), aProjection); |
668 | theFar = gp_Pln (Eye().Translated (aProjection * ZFar()), -aProjection); |
669 | |
670 | Standard_Real aHScaleHor = Scale() * 0.5 * Aspect(); |
671 | Standard_Real aHScaleVer = Scale() * 0.5; |
672 | |
673 | gp_Pnt aPntLeft = Center().Translated (aHScaleHor * -aSide); |
674 | gp_Pnt aPntRight = Center().Translated (aHScaleHor * aSide); |
675 | gp_Pnt aPntBottom = Center().Translated (aHScaleVer * -anUp); |
676 | gp_Pnt aPntTop = Center().Translated (aHScaleVer * anUp); |
677 | |
678 | gp_Vec aDirLeft = aSide; |
679 | gp_Vec aDirRight = -aSide; |
680 | gp_Vec aDirBottom = anUp; |
681 | gp_Vec aDirTop = -anUp; |
682 | if (!IsOrthographic()) |
683 | { |
684 | Standard_Real aHFOVHor = ATan (Tan (DTR_HALF * FOVy()) * Aspect()); |
685 | Standard_Real aHFOVVer = DTR_HALF * FOVy(); |
686 | aDirLeft.Rotate (gp_Ax1 (gp::Origin(), anUp), aHFOVHor); |
687 | aDirRight.Rotate (gp_Ax1 (gp::Origin(), anUp), -aHFOVHor); |
688 | aDirBottom.Rotate (gp_Ax1 (gp::Origin(), aSide), -aHFOVVer); |
689 | aDirTop.Rotate (gp_Ax1 (gp::Origin(), aSide), aHFOVVer); |
690 | } |
691 | |
692 | theLeft = gp_Pln (aPntLeft, aDirLeft); |
693 | theRight = gp_Pln (aPntRight, aDirRight); |
694 | theBottom = gp_Pln (aPntBottom, aDirBottom); |
695 | theTop = gp_Pln (aPntTop, aDirTop); |
696 | } |
697 | |
698 | // ======================================================================= |
699 | // function : OrientationMatrix |
700 | // purpose : |
701 | // ======================================================================= |
702 | const Graphic3d_Mat4d& Graphic3d_Camera::OrientationMatrix() const |
703 | { |
704 | return *UpdateOrientation (myMatricesD).Orientation; |
705 | } |
706 | |
707 | // ======================================================================= |
708 | // function : OrientationMatrixF |
709 | // purpose : |
710 | // ======================================================================= |
711 | const Graphic3d_Mat4& Graphic3d_Camera::OrientationMatrixF() const |
712 | { |
713 | return *UpdateOrientation (myMatricesF).Orientation; |
714 | } |
715 | |
716 | // ======================================================================= |
717 | // function : ProjectionMatrix |
718 | // purpose : |
719 | // ======================================================================= |
720 | const Graphic3d_Mat4d& Graphic3d_Camera::ProjectionMatrix() const |
721 | { |
722 | return *UpdateProjection (myMatricesD).MProjection; |
723 | } |
724 | |
725 | // ======================================================================= |
726 | // function : ProjectionMatrixF |
727 | // purpose : |
728 | // ======================================================================= |
729 | const Graphic3d_Mat4& Graphic3d_Camera::ProjectionMatrixF() const |
730 | { |
731 | return *UpdateProjection (myMatricesF).MProjection; |
732 | } |
733 | |
734 | // ======================================================================= |
735 | // function : ProjectionStereoLeft |
736 | // purpose : |
737 | // ======================================================================= |
738 | const Graphic3d_Mat4d& Graphic3d_Camera::ProjectionStereoLeft() const |
739 | { |
740 | return *UpdateProjection (myMatricesD).LProjection; |
741 | } |
742 | |
743 | // ======================================================================= |
744 | // function : ProjectionStereoLeftF |
745 | // purpose : |
746 | // ======================================================================= |
747 | const Graphic3d_Mat4& Graphic3d_Camera::ProjectionStereoLeftF() const |
748 | { |
749 | return *UpdateProjection (myMatricesF).LProjection; |
750 | } |
751 | |
752 | // ======================================================================= |
753 | // function : ProjectionStereoRight |
754 | // purpose : |
755 | // ======================================================================= |
756 | const Graphic3d_Mat4d& Graphic3d_Camera::ProjectionStereoRight() const |
757 | { |
758 | return *UpdateProjection (myMatricesD).RProjection; |
759 | } |
760 | |
761 | // ======================================================================= |
762 | // function : ProjectionStereoRightF |
763 | // purpose : |
764 | // ======================================================================= |
765 | const Graphic3d_Mat4& Graphic3d_Camera::ProjectionStereoRightF() const |
766 | { |
767 | return *UpdateProjection (myMatricesF).RProjection; |
b5ac8292 |
768 | } |
769 | |
770 | // ======================================================================= |
771 | // function : UpdateProjection |
772 | // purpose : |
773 | // ======================================================================= |
197ac94e |
774 | template <typename Elem_t> |
775 | Graphic3d_Camera::TransformMatrices<Elem_t>& |
776 | Graphic3d_Camera::UpdateProjection (TransformMatrices<Elem_t>& theMatrices) const |
b5ac8292 |
777 | { |
197ac94e |
778 | if (theMatrices.IsProjectionValid()) |
b5ac8292 |
779 | { |
197ac94e |
780 | return theMatrices; // for inline accessors |
b5ac8292 |
781 | } |
782 | |
197ac94e |
783 | theMatrices.InitProjection(); |
b5ac8292 |
784 | |
785 | // sets top of frustum based on FOVy and near clipping plane |
197ac94e |
786 | Elem_t aScale = static_cast<Elem_t> (myScale); |
787 | Elem_t aZNear = static_cast<Elem_t> (myZNear); |
788 | Elem_t aZFar = static_cast<Elem_t> (myZFar); |
789 | Elem_t anAspect = static_cast<Elem_t> (myAspect); |
ab1c121b |
790 | Elem_t aDXHalf = 0.0, aDYHalf = 0.0; |
b5ac8292 |
791 | if (IsOrthographic()) |
792 | { |
ab1c121b |
793 | aDXHalf = aScale * Elem_t (0.5); |
197ac94e |
794 | aDYHalf = aScale * Elem_t (0.5); |
b5ac8292 |
795 | } |
796 | else |
797 | { |
ab1c121b |
798 | aDXHalf = aZNear * Elem_t (Tan (DTR_HALF * myFOVy)); |
197ac94e |
799 | aDYHalf = aZNear * Elem_t (Tan (DTR_HALF * myFOVy)); |
b5ac8292 |
800 | } |
801 | |
ab1c121b |
802 | if (anAspect > 1.0) |
803 | { |
804 | aDXHalf *= anAspect; |
805 | } |
806 | else |
807 | { |
808 | aDYHalf /= anAspect; |
809 | } |
810 | |
b5ac8292 |
811 | // sets right of frustum based on aspect ratio |
197ac94e |
812 | Elem_t aLeft = -aDXHalf; |
813 | Elem_t aRight = aDXHalf; |
814 | Elem_t aBot = -aDYHalf; |
815 | Elem_t aTop = aDYHalf; |
b5ac8292 |
816 | |
197ac94e |
817 | Elem_t aIOD = myIODType == IODType_Relative |
818 | ? static_cast<Elem_t> (myIOD * Distance()) |
819 | : static_cast<Elem_t> (myIOD); |
b5ac8292 |
820 | |
197ac94e |
821 | Elem_t aFocus = myZFocusType == FocusType_Relative |
822 | ? static_cast<Elem_t> (myZFocus * Distance()) |
823 | : static_cast<Elem_t> (myZFocus); |
b5ac8292 |
824 | |
825 | switch (myProjType) |
826 | { |
827 | case Projection_Orthographic : |
197ac94e |
828 | OrthoProj (aLeft, aRight, aBot, aTop, aZNear, aZFar, *theMatrices.MProjection); |
b5ac8292 |
829 | break; |
830 | |
831 | case Projection_Perspective : |
197ac94e |
832 | PerspectiveProj (aLeft, aRight, aBot, aTop, aZNear, aZFar, *theMatrices.MProjection); |
b5ac8292 |
833 | break; |
834 | |
835 | case Projection_MonoLeftEye : |
836 | { |
197ac94e |
837 | StereoEyeProj (aLeft, aRight, aBot, aTop, |
838 | aZNear, aZFar, aIOD, aFocus, |
839 | Standard_True, *theMatrices.MProjection); |
38a0206f |
840 | *theMatrices.LProjection = *theMatrices.MProjection; |
b5ac8292 |
841 | break; |
842 | } |
843 | |
844 | case Projection_MonoRightEye : |
845 | { |
197ac94e |
846 | StereoEyeProj (aLeft, aRight, aBot, aTop, |
847 | aZNear, aZFar, aIOD, aFocus, |
848 | Standard_False, *theMatrices.MProjection); |
38a0206f |
849 | *theMatrices.RProjection = *theMatrices.MProjection; |
b5ac8292 |
850 | break; |
851 | } |
852 | |
853 | case Projection_Stereo : |
854 | { |
197ac94e |
855 | PerspectiveProj (aLeft, aRight, aBot, aTop, aZNear, aZFar, *theMatrices.MProjection); |
b5ac8292 |
856 | |
197ac94e |
857 | StereoEyeProj (aLeft, aRight, aBot, aTop, |
858 | aZNear, aZFar, aIOD, aFocus, |
859 | Standard_True, |
860 | *theMatrices.LProjection); |
b5ac8292 |
861 | |
197ac94e |
862 | StereoEyeProj (aLeft, aRight, aBot, aTop, |
863 | aZNear, aZFar, aIOD, aFocus, |
864 | Standard_False, |
865 | *theMatrices.RProjection); |
b5ac8292 |
866 | break; |
867 | } |
868 | } |
197ac94e |
869 | |
870 | return theMatrices; // for inline accessors |
b5ac8292 |
871 | } |
872 | |
873 | // ======================================================================= |
874 | // function : UpdateOrientation |
875 | // purpose : |
876 | // ======================================================================= |
197ac94e |
877 | template <typename Elem_t> |
878 | Graphic3d_Camera::TransformMatrices<Elem_t>& |
879 | Graphic3d_Camera::UpdateOrientation (TransformMatrices<Elem_t>& theMatrices) const |
b5ac8292 |
880 | { |
197ac94e |
881 | if (theMatrices.IsOrientationValid()) |
b5ac8292 |
882 | { |
197ac94e |
883 | return theMatrices; // for inline accessors |
b5ac8292 |
884 | } |
885 | |
197ac94e |
886 | theMatrices.InitOrientation(); |
887 | |
888 | NCollection_Vec3<Elem_t> anEye (static_cast<Elem_t> (myEye.X()), |
889 | static_cast<Elem_t> (myEye.Y()), |
890 | static_cast<Elem_t> (myEye.Z())); |
891 | |
892 | NCollection_Vec3<Elem_t> aCenter (static_cast<Elem_t> (myCenter.X()), |
893 | static_cast<Elem_t> (myCenter.Y()), |
894 | static_cast<Elem_t> (myCenter.Z())); |
b5ac8292 |
895 | |
197ac94e |
896 | NCollection_Vec3<Elem_t> anUp (static_cast<Elem_t> (myUp.X()), |
897 | static_cast<Elem_t> (myUp.Y()), |
898 | static_cast<Elem_t> (myUp.Z())); |
b5ac8292 |
899 | |
197ac94e |
900 | NCollection_Vec3<Elem_t> anAxialScale (static_cast<Elem_t> (myAxialScale.X()), |
901 | static_cast<Elem_t> (myAxialScale.Y()), |
902 | static_cast<Elem_t> (myAxialScale.Z())); |
b5ac8292 |
903 | |
197ac94e |
904 | LookOrientation (anEye, aCenter, anUp, anAxialScale, *theMatrices.Orientation); |
b5ac8292 |
905 | |
197ac94e |
906 | return theMatrices; // for inline accessors |
907 | } |
b5ac8292 |
908 | |
197ac94e |
909 | // ======================================================================= |
910 | // function : InvalidateProjection |
911 | // purpose : |
912 | // ======================================================================= |
913 | void Graphic3d_Camera::InvalidateProjection() |
914 | { |
915 | myMatricesD.ResetProjection(); |
916 | myMatricesF.ResetProjection(); |
825aa485 |
917 | myWorldViewProjState.ProjectionState() = (Standard_Size)Standard_Atomic_Increment (&THE_STATE_COUNTER); |
197ac94e |
918 | } |
b5ac8292 |
919 | |
197ac94e |
920 | // ======================================================================= |
921 | // function : InvalidateOrientation |
922 | // purpose : |
923 | // ======================================================================= |
924 | void Graphic3d_Camera::InvalidateOrientation() |
925 | { |
926 | myMatricesD.ResetOrientation(); |
927 | myMatricesF.ResetOrientation(); |
825aa485 |
928 | myWorldViewProjState.WorldViewState() = (Standard_Size)Standard_Atomic_Increment (&THE_STATE_COUNTER); |
b5ac8292 |
929 | } |
930 | |
931 | // ======================================================================= |
932 | // function : OrthoProj |
933 | // purpose : |
934 | // ======================================================================= |
197ac94e |
935 | template <typename Elem_t> |
936 | void Graphic3d_Camera::OrthoProj (const Elem_t theLeft, |
937 | const Elem_t theRight, |
938 | const Elem_t theBottom, |
939 | const Elem_t theTop, |
940 | const Elem_t theNear, |
941 | const Elem_t theFar, |
942 | NCollection_Mat4<Elem_t>& theOutMx) |
b5ac8292 |
943 | { |
944 | // row 0 |
197ac94e |
945 | theOutMx.ChangeValue (0, 0) = Elem_t (2.0) / (theRight - theLeft); |
946 | theOutMx.ChangeValue (0, 1) = Elem_t (0.0); |
947 | theOutMx.ChangeValue (0, 2) = Elem_t (0.0); |
b5ac8292 |
948 | theOutMx.ChangeValue (0, 3) = - (theRight + theLeft) / (theRight - theLeft); |
949 | |
950 | // row 1 |
197ac94e |
951 | theOutMx.ChangeValue (1, 0) = Elem_t (0.0); |
952 | theOutMx.ChangeValue (1, 1) = Elem_t (2.0) / (theTop - theBottom); |
953 | theOutMx.ChangeValue (1, 2) = Elem_t (0.0); |
b5ac8292 |
954 | theOutMx.ChangeValue (1, 3) = - (theTop + theBottom) / (theTop - theBottom); |
955 | |
956 | // row 2 |
197ac94e |
957 | theOutMx.ChangeValue (2, 0) = Elem_t (0.0); |
958 | theOutMx.ChangeValue (2, 1) = Elem_t (0.0); |
959 | theOutMx.ChangeValue (2, 2) = Elem_t (-2.0) / (theFar - theNear); |
b5ac8292 |
960 | theOutMx.ChangeValue (2, 3) = - (theFar + theNear) / (theFar - theNear); |
961 | |
962 | // row 3 |
197ac94e |
963 | theOutMx.ChangeValue (3, 0) = Elem_t (0.0); |
964 | theOutMx.ChangeValue (3, 1) = Elem_t (0.0); |
965 | theOutMx.ChangeValue (3, 2) = Elem_t (0.0); |
966 | theOutMx.ChangeValue (3, 3) = Elem_t (1.0); |
b5ac8292 |
967 | } |
968 | |
969 | // ======================================================================= |
970 | // function : PerspectiveProj |
971 | // purpose : |
972 | // ======================================================================= |
197ac94e |
973 | template <typename Elem_t> |
974 | void Graphic3d_Camera::PerspectiveProj (const Elem_t theLeft, |
975 | const Elem_t theRight, |
976 | const Elem_t theBottom, |
977 | const Elem_t theTop, |
978 | const Elem_t theNear, |
979 | const Elem_t theFar, |
980 | NCollection_Mat4<Elem_t>& theOutMx) |
b5ac8292 |
981 | { |
982 | // column 0 |
197ac94e |
983 | theOutMx.ChangeValue (0, 0) = (Elem_t (2.0) * theNear) / (theRight - theLeft); |
984 | theOutMx.ChangeValue (1, 0) = Elem_t (0.0); |
985 | theOutMx.ChangeValue (2, 0) = Elem_t (0.0); |
986 | theOutMx.ChangeValue (3, 0) = Elem_t (0.0); |
b5ac8292 |
987 | |
988 | // column 1 |
197ac94e |
989 | theOutMx.ChangeValue (0, 1) = Elem_t (0.0); |
990 | theOutMx.ChangeValue (1, 1) = (Elem_t (2.0) * theNear) / (theTop - theBottom); |
991 | theOutMx.ChangeValue (2, 1) = Elem_t (0.0); |
992 | theOutMx.ChangeValue (3, 1) = Elem_t (0.0); |
b5ac8292 |
993 | |
994 | // column 2 |
995 | theOutMx.ChangeValue (0, 2) = (theRight + theLeft) / (theRight - theLeft); |
996 | theOutMx.ChangeValue (1, 2) = (theTop + theBottom) / (theTop - theBottom); |
997 | theOutMx.ChangeValue (2, 2) = -(theFar + theNear) / (theFar - theNear); |
197ac94e |
998 | theOutMx.ChangeValue (3, 2) = Elem_t (-1.0); |
b5ac8292 |
999 | |
1000 | // column 3 |
197ac94e |
1001 | theOutMx.ChangeValue (0, 3) = Elem_t (0.0); |
1002 | theOutMx.ChangeValue (1, 3) = Elem_t (0.0); |
1003 | theOutMx.ChangeValue (2, 3) = -(Elem_t (2.0) * theFar * theNear) / (theFar - theNear); |
1004 | theOutMx.ChangeValue (3, 3) = Elem_t (0.0); |
b5ac8292 |
1005 | } |
1006 | |
1007 | // ======================================================================= |
1008 | // function : StereoEyeProj |
1009 | // purpose : |
1010 | // ======================================================================= |
197ac94e |
1011 | template <typename Elem_t> |
1012 | void Graphic3d_Camera::StereoEyeProj (const Elem_t theLeft, |
1013 | const Elem_t theRight, |
1014 | const Elem_t theBottom, |
1015 | const Elem_t theTop, |
1016 | const Elem_t theNear, |
1017 | const Elem_t theFar, |
1018 | const Elem_t theIOD, |
1019 | const Elem_t theZFocus, |
1020 | const Standard_Boolean theIsLeft, |
1021 | NCollection_Mat4<Elem_t>& theOutMx) |
b5ac8292 |
1022 | { |
197ac94e |
1023 | Elem_t aDx = theIsLeft ? Elem_t (0.5) * theIOD : Elem_t (-0.5) * theIOD; |
1024 | Elem_t aDXStereoShift = aDx * theNear / theZFocus; |
b5ac8292 |
1025 | |
1026 | // construct eye projection matrix |
1027 | PerspectiveProj (theLeft + aDXStereoShift, |
1028 | theRight + aDXStereoShift, |
1029 | theBottom, theTop, theNear, theFar, |
b5ac8292 |
1030 | theOutMx); |
1031 | |
197ac94e |
1032 | if (theIOD != Elem_t (0.0)) |
b5ac8292 |
1033 | { |
1034 | // X translation to cancel parallax |
197ac94e |
1035 | theOutMx.Translate (NCollection_Vec3<Elem_t> (aDx, Elem_t (0.0), Elem_t (0.0))); |
b5ac8292 |
1036 | } |
1037 | } |
1038 | |
1039 | // ======================================================================= |
1040 | // function : LookOrientation |
1041 | // purpose : |
1042 | // ======================================================================= |
197ac94e |
1043 | template <typename Elem_t> |
1044 | void Graphic3d_Camera::LookOrientation (const NCollection_Vec3<Elem_t>& theEye, |
1045 | const NCollection_Vec3<Elem_t>& theLookAt, |
1046 | const NCollection_Vec3<Elem_t>& theUpDir, |
1047 | const NCollection_Vec3<Elem_t>& theAxialScale, |
1048 | NCollection_Mat4<Elem_t>& theOutMx) |
b5ac8292 |
1049 | { |
197ac94e |
1050 | NCollection_Vec3<Elem_t> aForward = theLookAt - theEye; |
b5ac8292 |
1051 | aForward.Normalize(); |
1052 | |
1053 | // side = forward x up |
197ac94e |
1054 | NCollection_Vec3<Elem_t> aSide = NCollection_Vec3<Elem_t>::Cross (aForward, theUpDir); |
b5ac8292 |
1055 | aSide.Normalize(); |
1056 | |
1057 | // recompute up as: up = side x forward |
197ac94e |
1058 | NCollection_Vec3<Elem_t> anUp = NCollection_Vec3<Elem_t>::Cross (aSide, aForward); |
b5ac8292 |
1059 | |
197ac94e |
1060 | NCollection_Mat4<Elem_t> aLookMx; |
b5ac8292 |
1061 | aLookMx.SetRow (0, aSide); |
1062 | aLookMx.SetRow (1, anUp); |
1063 | aLookMx.SetRow (2, -aForward); |
1064 | |
197ac94e |
1065 | theOutMx.InitIdentity(); |
1066 | theOutMx.Multiply (aLookMx); |
b5ac8292 |
1067 | theOutMx.Translate (-theEye); |
1068 | |
197ac94e |
1069 | NCollection_Mat4<Elem_t> anAxialScaleMx; |
b5ac8292 |
1070 | anAxialScaleMx.ChangeValue (0, 0) = theAxialScale.x(); |
1071 | anAxialScaleMx.ChangeValue (1, 1) = theAxialScale.y(); |
1072 | anAxialScaleMx.ChangeValue (2, 2) = theAxialScale.z(); |
1073 | |
1074 | theOutMx.Multiply (anAxialScaleMx); |
1075 | } |
6bc6a6fc |
1076 | |
1077 | //============================================================================= |
1078 | //function : ZFitAll |
1079 | //purpose : |
1080 | //============================================================================= |
10dbdf34 |
1081 | bool Graphic3d_Camera::ZFitAll (const Standard_Real theScaleFactor, |
1082 | const Bnd_Box& theMinMax, |
1083 | const Bnd_Box& theGraphicBB, |
1084 | Standard_Real& theZNear, |
1085 | Standard_Real& theZFar) const |
6bc6a6fc |
1086 | { |
1087 | Standard_ASSERT_RAISE (theScaleFactor > 0.0, "Zero or negative scale factor is not allowed."); |
1088 | |
ea764884 |
1089 | // Method changes zNear and zFar parameters of camera so as to fit graphical structures |
1090 | // by their graphical boundaries. It precisely fits min max boundaries of primary application |
1091 | // objects (second argument), while it can sacrifice the real graphical boundaries of the |
1092 | // scene with infinite or helper objects (third argument) for the sake of perspective projection. |
ed063270 |
1093 | if (theGraphicBB.IsVoid()) |
6bc6a6fc |
1094 | { |
10dbdf34 |
1095 | theZNear = DEFAULT_ZNEAR; |
1096 | theZFar = DEFAULT_ZFAR; |
1097 | return false; |
6bc6a6fc |
1098 | } |
1099 | |
ea764884 |
1100 | // Measure depth of boundary points from camera eye. |
ed063270 |
1101 | NCollection_Sequence<gp_Pnt> aPntsToMeasure; |
1102 | |
ea764884 |
1103 | Standard_Real aGraphicBB[6]; |
ed063270 |
1104 | theGraphicBB.Get (aGraphicBB[0], aGraphicBB[1], aGraphicBB[2], aGraphicBB[3], aGraphicBB[4], aGraphicBB[5]); |
1105 | |
1106 | aPntsToMeasure.Append (gp_Pnt (aGraphicBB[0], aGraphicBB[1], aGraphicBB[2])); |
1107 | aPntsToMeasure.Append (gp_Pnt (aGraphicBB[0], aGraphicBB[1], aGraphicBB[5])); |
1108 | aPntsToMeasure.Append (gp_Pnt (aGraphicBB[0], aGraphicBB[4], aGraphicBB[2])); |
1109 | aPntsToMeasure.Append (gp_Pnt (aGraphicBB[0], aGraphicBB[4], aGraphicBB[5])); |
1110 | aPntsToMeasure.Append (gp_Pnt (aGraphicBB[3], aGraphicBB[1], aGraphicBB[2])); |
1111 | aPntsToMeasure.Append (gp_Pnt (aGraphicBB[3], aGraphicBB[1], aGraphicBB[5])); |
1112 | aPntsToMeasure.Append (gp_Pnt (aGraphicBB[3], aGraphicBB[4], aGraphicBB[2])); |
1113 | aPntsToMeasure.Append (gp_Pnt (aGraphicBB[3], aGraphicBB[4], aGraphicBB[5])); |
1114 | |
5a19c303 |
1115 | Standard_Boolean isFiniteMinMax = !theMinMax.IsVoid() && !theMinMax.IsWhole(); |
1116 | |
1117 | if (isFiniteMinMax) |
6bc6a6fc |
1118 | { |
ea764884 |
1119 | Standard_Real aMinMax[6]; |
ed063270 |
1120 | theMinMax.Get (aMinMax[0], aMinMax[1], aMinMax[2], aMinMax[3], aMinMax[4], aMinMax[5]); |
1121 | |
1122 | aPntsToMeasure.Append (gp_Pnt (aMinMax[0], aMinMax[1], aMinMax[2])); |
1123 | aPntsToMeasure.Append (gp_Pnt (aMinMax[0], aMinMax[1], aMinMax[5])); |
1124 | aPntsToMeasure.Append (gp_Pnt (aMinMax[0], aMinMax[4], aMinMax[2])); |
1125 | aPntsToMeasure.Append (gp_Pnt (aMinMax[0], aMinMax[4], aMinMax[5])); |
1126 | aPntsToMeasure.Append (gp_Pnt (aMinMax[3], aMinMax[1], aMinMax[2])); |
1127 | aPntsToMeasure.Append (gp_Pnt (aMinMax[3], aMinMax[1], aMinMax[5])); |
1128 | aPntsToMeasure.Append (gp_Pnt (aMinMax[3], aMinMax[4], aMinMax[2])); |
1129 | aPntsToMeasure.Append (gp_Pnt (aMinMax[3], aMinMax[4], aMinMax[5])); |
1130 | } |
6bc6a6fc |
1131 | |
ea764884 |
1132 | // Camera eye plane. |
6bc6a6fc |
1133 | gp_Dir aCamDir = Direction(); |
1134 | gp_Pnt aCamEye = myEye; |
1135 | gp_Pln aCamPln (aCamEye, aCamDir); |
1136 | |
5a19c303 |
1137 | Standard_Real aModelMinDist = RealLast(); |
1138 | Standard_Real aModelMaxDist = RealFirst(); |
1139 | Standard_Real aGraphMinDist = RealLast(); |
1140 | Standard_Real aGraphMaxDist = RealFirst(); |
6bc6a6fc |
1141 | |
1142 | const gp_XYZ& anAxialScale = myAxialScale; |
1143 | |
ea764884 |
1144 | // Get minimum and maximum distances to the eye plane. |
ed063270 |
1145 | Standard_Integer aCounter = 0; |
1146 | NCollection_Sequence<gp_Pnt>::Iterator aPntIt(aPntsToMeasure); |
1147 | for (; aPntIt.More(); aPntIt.Next()) |
6bc6a6fc |
1148 | { |
ed063270 |
1149 | gp_Pnt aMeasurePnt = aPntIt.Value(); |
6bc6a6fc |
1150 | |
1151 | aMeasurePnt = gp_Pnt (aMeasurePnt.X() * anAxialScale.X(), |
ed063270 |
1152 | aMeasurePnt.Y() * anAxialScale.Y(), |
1153 | aMeasurePnt.Z() * anAxialScale.Z()); |
6bc6a6fc |
1154 | |
1155 | Standard_Real aDistance = aCamPln.Distance (aMeasurePnt); |
1156 | |
ea764884 |
1157 | // Check if the camera is intruded into the scene. |
6bc6a6fc |
1158 | if (aCamDir.IsOpposite (gp_Vec (aCamEye, aMeasurePnt), M_PI * 0.5)) |
1159 | { |
1160 | aDistance *= -1; |
1161 | } |
1162 | |
ea764884 |
1163 | // The first eight points are from theGraphicBB, the last eight points are from theMinMax (can be absent). |
5a19c303 |
1164 | Standard_Real& aChangeMinDist = aCounter >= 8 ? aModelMinDist : aGraphMinDist; |
1165 | Standard_Real& aChangeMaxDist = aCounter >= 8 ? aModelMaxDist : aGraphMaxDist; |
6bc6a6fc |
1166 | aChangeMinDist = Min (aDistance, aChangeMinDist); |
1167 | aChangeMaxDist = Max (aDistance, aChangeMaxDist); |
ed063270 |
1168 | aCounter++; |
6bc6a6fc |
1169 | } |
1170 | |
ea764884 |
1171 | // Compute depth of bounding box center. |
5a19c303 |
1172 | Standard_Real aMidDepth = (aGraphMinDist + aGraphMaxDist) * 0.5; |
1173 | Standard_Real aHalfDepth = (aGraphMaxDist - aGraphMinDist) * 0.5; |
6bc6a6fc |
1174 | |
ea764884 |
1175 | // Compute enlarged or shrank near and far z ranges. |
35c4a17c |
1176 | Standard_Real aZNear = aMidDepth - aHalfDepth * theScaleFactor; |
1177 | Standard_Real aZFar = aMidDepth + aHalfDepth * theScaleFactor; |
6bc6a6fc |
1178 | |
1179 | if (!IsOrthographic()) |
1180 | { |
ea764884 |
1181 | // Everything is behind the perspective camera. |
1182 | if (aZFar < zEpsilon()) |
6bc6a6fc |
1183 | { |
10dbdf34 |
1184 | theZNear = DEFAULT_ZNEAR; |
1185 | theZFar = DEFAULT_ZFAR; |
1186 | return false; |
6bc6a6fc |
1187 | } |
6bc6a6fc |
1188 | } |
1189 | |
ea764884 |
1190 | // |
1191 | // Consider clipping errors due to double to single precision floating-point conversion. |
1192 | // |
1193 | |
1194 | // Model to view transformation performs translation of points against eye position |
1195 | // in three dimensions. Both point coordinate and eye position values are converted from |
1196 | // double to single precision floating point numbers producing conversion errors. |
1197 | // Epsilon (Mod) * 3.0 should safely compensate precision error for z coordinate after |
1198 | // translation assuming that the: |
1199 | // Epsilon (Eye.Mod()) * 3.0 > Epsilon (Eye.X()) + Epsilon (Eye.Y()) + Epsilon (Eye.Z()). |
1200 | Standard_Real aEyeConf = 3.0 * zEpsilon (myEye.XYZ().Modulus()); |
1201 | |
1202 | // Model to view transformation performs rotation of points according to view direction. |
1203 | // New z coordinate is computed as a multiplication of point's x, y, z coordinates by the |
1204 | // "forward" direction vector's x, y, z coordinates. Both point's and "z" direction vector's |
1205 | // values are converted from double to single precision floating point numbers producing |
1206 | // conversion errors. |
1207 | // Epsilon (Mod) * 6.0 should safely compensate the precision errors for the multiplication |
1208 | // of point coordinates by direction vector. |
1209 | gp_Pnt aGraphicMin = theGraphicBB.CornerMin(); |
1210 | gp_Pnt aGraphicMax = theGraphicBB.CornerMax(); |
1211 | |
1212 | Standard_Real aModelConf = 6.0 * zEpsilon (aGraphicMin.XYZ().Modulus()) + |
1213 | 6.0 * zEpsilon (aGraphicMax.XYZ().Modulus()); |
1214 | |
1215 | // Compensate floating point conversion errors by increasing zNear, zFar to avoid clipping. |
1216 | aZNear -= zEpsilon (aZNear) + aEyeConf + aModelConf; |
1217 | aZFar += zEpsilon (aZFar) + aEyeConf + aModelConf; |
1218 | |
1219 | if (!IsOrthographic()) |
6bc6a6fc |
1220 | { |
5a19c303 |
1221 | // For perspective projection, the value of z in normalized device coordinates is non-linear |
1222 | // function of eye z coordinate. For fixed-point depth representation resolution of z in |
1223 | // model-view space will grow towards zFar plane and its scale depends mostly on how far is zNear |
1224 | // against camera's eye. The purpose of the code below is to select most appropriate zNear distance |
1225 | // to balance between clipping (less zNear, more chances to observe closely small models without clipping) |
1226 | // and resolution of depth. A well applicable criteria to this is a ratio between resolution of z at center |
1227 | // of model boundaries and the distance to that center point. The ratio is chosen empirically and validated |
1228 | // by tests database. It is considered to be ~0.001 (0.1%) for 24 bit depth buffer, for less depth bitness |
1229 | // the zNear will be placed similarly giving lower resolution. |
1230 | // Approximation of the formula for respectively large z range is: |
1231 | // zNear = [z * (1 + k) / (k * c)], |
1232 | // where: |
1233 | // z - distance to center of model boundaries; |
1234 | // k - chosen ratio, c - capacity of depth buffer; |
1235 | // k = 0.001, k * c = 1677.216, (1 + k) / (k * c) ~ 5.97E-4 |
1236 | // |
1237 | // The function uses center of model boundaries computed from "theMinMax" boundaries (instead of using real |
1238 | // graphical boundaries of all displayed objects). That means that it can sacrifice resolution of presentation |
1239 | // of non primary ("infinite") application graphical objects in favor of better perspective projection of the |
1240 | // small applicative objects measured with "theMinMax" values. |
1241 | Standard_Real aZRange = isFiniteMinMax ? aModelMaxDist - aModelMinDist : aGraphMaxDist - aGraphMinDist; |
1242 | Standard_Real aZMin = isFiniteMinMax ? aModelMinDist : aGraphMinDist; |
1243 | Standard_Real aZ = aZMin < 0 ? aZRange / 2.0 : aZRange / 2.0 + aZMin; |
1244 | Standard_Real aZNearMin = aZ * 5.97E-4; |
1245 | if (aZNear < aZNearMin) |
1246 | { |
1247 | // Clip zNear according to the minimum value matching the quality. |
1248 | aZNear = aZNearMin; |
1249 | } |
1250 | else |
1251 | { |
1252 | // Compensate zNear conversion errors for perspective projection. |
1253 | aZNear -= aZFar * zEpsilon (aZNear) / (aZFar - zEpsilon (aZNear)); |
1254 | } |
1255 | |
1256 | // Compensate zFar conversion errors for perspective projection. |
1257 | aZFar += zEpsilon (aZFar); |
ea764884 |
1258 | |
1259 | // Ensure that after all the zNear is not a negative value. |
1260 | if (aZNear < zEpsilon()) |
1261 | { |
1262 | aZNear = zEpsilon(); |
1263 | } |
6bc6a6fc |
1264 | } |
1265 | |
10dbdf34 |
1266 | theZNear = aZNear; |
1267 | theZFar = aZFar; |
1268 | return true; |
6bc6a6fc |
1269 | } |