1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <V3d_View.hxx>
16 #include <Aspect_GradientBackground.hxx>
17 #include <Aspect_Grid.hxx>
18 #include <Aspect_Window.hxx>
19 #include <Bnd_Box.hxx>
23 #include <Graphic3d_AspectMarker3d.hxx>
24 #include <Graphic3d_GraphicDriver.hxx>
25 #include <Graphic3d_Group.hxx>
26 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
27 #include <Graphic3d_MapOfStructure.hxx>
28 #include <Graphic3d_Structure.hxx>
29 #include <Graphic3d_TextureEnv.hxx>
30 #include <Image_AlienPixMap.hxx>
31 #include <Message.hxx>
32 #include <Message_Messenger.hxx>
33 #include <NCollection_Array1.hxx>
34 #include <Precision.hxx>
35 #include <Quantity_Color.hxx>
36 #include <Standard_Assert.hxx>
37 #include <Standard_DivideByZero.hxx>
38 #include <Standard_ErrorHandler.hxx>
39 #include <Standard_MultiplyDefined.hxx>
40 #include <Standard_ShortReal.hxx>
41 #include <Standard_Type.hxx>
42 #include <Standard_TypeMismatch.hxx>
43 #include <TColgp_Array1OfPnt.hxx>
44 #include <TColStd_Array2OfReal.hxx>
45 #include <TColStd_HSequenceOfInteger.hxx>
47 #include <V3d_BadValue.hxx>
48 #include <V3d_Light.hxx>
49 #include <V3d_StereoDumpOptions.hxx>
50 #include <V3d_UnMapped.hxx>
51 #include <V3d_Viewer.hxx>
53 IMPLEMENT_STANDARD_RTTIEXT(V3d_View,Standard_Transient)
55 #define DEUXPI (2. * M_PI)
59 static const Standard_Integer THE_NB_BOUND_POINTS = 8;
62 //=============================================================================
63 //function : Constructor
65 //=============================================================================
66 V3d_View::V3d_View (const Handle(V3d_Viewer)& theViewer, const V3d_TypeOfView theType)
67 : myIsInvalidatedImmediate (Standard_True),
68 MyViewer (theViewer.operator->()),
69 SwitchSetFront (Standard_False),
70 myZRotation (Standard_False),
71 myTrihedron (new V3d_Trihedron()),
74 myView = theViewer->Driver()->CreateView (theViewer->StructureManager());
76 myView->SetBackground (theViewer->GetBackgroundColor());
77 myView->SetGradientBackground (theViewer->GetGradientBackground());
79 ChangeRenderingParams() = theViewer->DefaultRenderingParams();
82 Handle(Graphic3d_Camera) aCamera = new Graphic3d_Camera();
83 aCamera->SetFOVy (45.0);
84 aCamera->SetIOD (Graphic3d_Camera::IODType_Relative, 0.05);
85 aCamera->SetZFocus (Graphic3d_Camera::FocusType_Relative, 1.0);
86 aCamera->SetProjectionType ((theType == V3d_ORTHOGRAPHIC)
87 ? Graphic3d_Camera::Projection_Orthographic
88 : Graphic3d_Camera::Projection_Perspective);
90 myDefaultCamera = new Graphic3d_Camera();
92 myImmediateUpdate = Standard_False;
93 SetAutoZFitMode (Standard_True, 1.0);
94 SetBackFacingModel (V3d_TOBM_AUTOMATIC);
96 SetAxis (0.,0.,0.,1.,1.,1.);
97 SetVisualization (theViewer->DefaultVisualization());
98 SetShadingModel (theViewer->DefaultShadingModel());
101 SetProj (theViewer->DefaultViewProj());
102 SetSize (theViewer->DefaultViewSize());
103 Standard_Real zsize = theViewer->DefaultViewSize();
105 SetDepth (theViewer->DefaultViewSize() / 2.0);
106 SetViewMappingDefault();
107 SetViewOrientationDefault();
108 theViewer->AddView (this);
110 myImmediateUpdate = Standard_True;
113 //=============================================================================
114 //function : Constructor
116 //=============================================================================
117 V3d_View::V3d_View (const Handle(V3d_Viewer)& theViewer, const Handle(V3d_View)& theView)
118 : myIsInvalidatedImmediate (Standard_True),
119 MyViewer (theViewer.operator->()),
120 SwitchSetFront(Standard_False),
121 myZRotation (Standard_False),
124 myView = theViewer->Driver()->CreateView (theViewer->StructureManager());
126 myView->CopySettings (theView->View());
127 myDefaultViewPoint = theView->myDefaultViewPoint;
128 myDefaultViewAxis = theView->myDefaultViewAxis;
130 myDefaultCamera = new Graphic3d_Camera (theView->DefaultCamera());
132 myImmediateUpdate = Standard_False;
133 SetAutoZFitMode (theView->AutoZFitMode(), theView->AutoZFitScaleFactor());
134 theViewer->AddView (this);
136 myImmediateUpdate = Standard_True;
139 //=============================================================================
140 //function : Destructor
142 //=============================================================================
143 V3d_View::~V3d_View()
145 if (!myView->IsRemoved())
151 //=============================================================================
152 //function : SetMagnify
154 //=============================================================================
155 void V3d_View::SetMagnify (const Handle(Aspect_Window)& theWindow,
156 const Handle(V3d_View)& thePreviousView,
157 const Standard_Integer theX1,
158 const Standard_Integer theY1,
159 const Standard_Integer theX2,
160 const Standard_Integer theY2)
162 if (!myView->IsRemoved() && !myView->IsDefined())
164 Standard_Real aU1, aV1, aU2, aV2;
165 thePreviousView->Convert (theX1, theY1, aU1, aV1);
166 thePreviousView->Convert (theX2, theY2, aU2, aV2);
167 myView->SetWindow (theWindow);
168 FitAll (aU1, aV1, aU2, aV2);
169 MyViewer->SetViewOn (this);
170 MyWindow = theWindow;
173 SetViewMappingDefault();
177 //=============================================================================
178 //function : SetWindow
180 //=============================================================================
181 void V3d_View::SetWindow (const Handle(Aspect_Window)& theWindow,
182 const Aspect_RenderingContext theContext)
184 if (myView->IsRemoved())
189 // method V3d_View::SetWindow() should assign the field MyWindow before calling Redraw()
190 MyWindow = theWindow;
191 myView->SetWindow (theWindow, theContext);
192 MyViewer->SetViewOn (this);
194 if (myImmediateUpdate)
200 //=============================================================================
203 //=============================================================================
204 void V3d_View::Remove() const
206 if (!MyGrid.IsNull())
210 if (!myTrihedron.IsNull())
212 myTrihedron->Erase();
215 MyViewer->DelView (this);
217 Handle(Aspect_Window)& aWin = const_cast<Handle(Aspect_Window)&> (MyWindow);
221 //=============================================================================
224 //=============================================================================
225 void V3d_View::Update() const
227 if (!myView->IsDefined()
228 || !myView->IsActive())
233 myIsInvalidatedImmediate = Standard_False;
240 //=============================================================================
243 //=============================================================================
244 void V3d_View::Redraw() const
246 if (!myView->IsDefined()
247 || !myView->IsActive())
252 myIsInvalidatedImmediate = Standard_False;
253 Handle(Graphic3d_StructureManager) aStructureMgr = MyViewer->StructureManager();
254 for (Standard_Integer aRetryIter = 0; aRetryIter < 2; ++aRetryIter)
256 if (aStructureMgr->IsDeviceLost())
258 aStructureMgr->RecomputeStructures();
265 if (!aStructureMgr->IsDeviceLost())
272 //=============================================================================
273 //function : RedrawImmediate
275 //=============================================================================
276 void V3d_View::RedrawImmediate() const
278 if (!myView->IsDefined()
279 || !myView->IsActive())
284 myIsInvalidatedImmediate = Standard_False;
285 myView->RedrawImmediate();
288 //=============================================================================
289 //function : Invalidate
291 //=============================================================================
292 void V3d_View::Invalidate() const
294 if (!myView->IsDefined())
299 myView->Invalidate();
302 //=============================================================================
303 //function : IsInvalidated
305 //=============================================================================
306 Standard_Boolean V3d_View::IsInvalidated() const
308 return !myView->IsDefined()
309 || myView->IsInvalidated();
312 // ========================================================================
313 // function : SetAutoZFitMode
315 // ========================================================================
316 void V3d_View::SetAutoZFitMode (const Standard_Boolean theIsOn,
317 const Standard_Real theScaleFactor)
319 Standard_ASSERT_RAISE (theScaleFactor > 0.0, "Zero or negative scale factor is not allowed.");
320 myAutoZFitScaleFactor = theScaleFactor;
321 myAutoZFitIsOn = theIsOn;
324 //=============================================================================
325 //function : AutoZFit
327 //=============================================================================
328 void V3d_View::AutoZFit() const
335 ZFitAll (myAutoZFitScaleFactor);
338 //=============================================================================
341 //=============================================================================
342 void V3d_View::ZFitAll (const Standard_Real theScaleFactor) const
344 Bnd_Box aMinMaxBox = myView->MinMaxValues (Standard_False); // applicative min max boundaries
345 Bnd_Box aGraphicBox = myView->MinMaxValues (Standard_True); // real graphical boundaries (not accounting infinite flag).
347 myView->Camera()->ZFitAll (theScaleFactor, aMinMaxBox, aGraphicBox);
350 //=============================================================================
353 //=============================================================================
354 Standard_Boolean V3d_View::IsEmpty() const
356 Standard_Boolean TheStatus = Standard_True ;
357 if( myView->IsDefined() ) {
358 Standard_Integer Nstruct = myView->NumberOfDisplayedStructures() ;
359 if( Nstruct > 0 ) TheStatus = Standard_False ;
364 //=============================================================================
365 //function : UpdateLights
367 //=============================================================================
368 void V3d_View::UpdateLights() const
370 Handle(Graphic3d_LightSet) aLights = new Graphic3d_LightSet();
371 for (V3d_ListOfLight::Iterator anActiveLightIter (myActiveLights); anActiveLightIter.More(); anActiveLightIter.Next())
373 aLights->Add (anActiveLightIter.Value());
375 myView->SetLights (aLights);
378 //=============================================================================
379 //function : DoMapping
381 //=============================================================================
382 void V3d_View::DoMapping()
384 if (!myView->IsDefined())
389 myView->Window()->DoMapping();
392 //=============================================================================
393 //function : MustBeResized
395 //=============================================================================
396 void V3d_View::MustBeResized()
398 if (!myView->IsDefined())
406 if (myImmediateUpdate)
412 //=============================================================================
413 //function : SetBackgroundColor
415 //=============================================================================
416 void V3d_View::SetBackgroundColor (const Quantity_TypeOfColor theType,
417 const Standard_Real theV1,
418 const Standard_Real theV2,
419 const Standard_Real theV3)
421 Standard_Real aV1 = Max (Min (theV1, 1.0), 0.0);
422 Standard_Real aV2 = Max (Min (theV2, 1.0), 0.0);
423 Standard_Real aV3 = Max (Min (theV3, 1.0), 0.0);
425 SetBackgroundColor (Quantity_Color (aV1, aV2, aV3, theType));
428 //=============================================================================
429 //function : SetBackgroundColor
431 //=============================================================================
432 void V3d_View::SetBackgroundColor (const Quantity_Color& theColor)
434 myView->SetBackground (Aspect_Background (theColor));
436 if (myImmediateUpdate)
442 //=============================================================================
443 //function : SetBgGradientColors
445 //=============================================================================
446 void V3d_View::SetBgGradientColors (const Quantity_Color& theColor1,
447 const Quantity_Color& theColor2,
448 const Aspect_GradientFillMethod theFillStyle,
449 const Standard_Boolean theToUpdate)
451 Aspect_GradientBackground aGradientBg (theColor1, theColor2, theFillStyle);
453 myView->SetGradientBackground (aGradientBg);
455 if (myImmediateUpdate || theToUpdate)
461 //=============================================================================
462 //function : SetBgGradientStyle
464 //=============================================================================
465 void V3d_View::SetBgGradientStyle (const Aspect_GradientFillMethod theFillStyle, const Standard_Boolean theToUpdate)
467 Quantity_Color aColor1;
468 Quantity_Color aColor2;
469 GradientBackground().Colors (aColor1, aColor2);
471 SetBgGradientColors (aColor1, aColor2, theFillStyle, theToUpdate);
474 //=============================================================================
475 //function : SetBackgroundImage
477 //=============================================================================
478 void V3d_View::SetBackgroundImage (const Standard_CString theFileName,
479 const Aspect_FillMethod theFillStyle,
480 const Standard_Boolean theToUpdate)
482 myView->SetBackgroundImage (theFileName);
483 myView->SetBackgroundImageStyle (theFillStyle);
485 if (myImmediateUpdate || theToUpdate)
491 //=============================================================================
492 //function : SetBgImageStyle
494 //=============================================================================
495 void V3d_View::SetBgImageStyle (const Aspect_FillMethod theFillStyle, const Standard_Boolean theToUpdate)
497 myView->SetBackgroundImageStyle (theFillStyle);
499 if (myImmediateUpdate || theToUpdate)
505 //=============================================================================
506 //function : SetBackgroundCubeMap
508 //=============================================================================
509 void V3d_View::SetBackgroundCubeMap (const Handle(Graphic3d_CubeMap)& theCubeMap,
510 Standard_Boolean theToUpdatePBREnv,
511 Standard_Boolean theToUpdate)
513 myView->SetBackgroundCubeMap (theCubeMap, theToUpdatePBREnv);
514 if (myImmediateUpdate || theToUpdate)
520 //=============================================================================
521 //function : GeneratePBREnvironment
523 //=============================================================================
524 void V3d_View::GeneratePBREnvironment (Standard_Boolean theToUpdate)
526 myView->GeneratePBREnvironment();
527 if (myImmediateUpdate || theToUpdate)
533 //=============================================================================
534 //function : ClearPBREnvironment
536 //=============================================================================
537 void V3d_View::ClearPBREnvironment (Standard_Boolean theToUpdate)
539 myView->ClearPBREnvironment();
540 if (myImmediateUpdate || theToUpdate)
546 //=============================================================================
549 //=============================================================================
550 void V3d_View::SetAxis (const Standard_Real theX, const Standard_Real theY, const Standard_Real theZ,
551 const Standard_Real theVx, const Standard_Real theVy, const Standard_Real theVz)
553 myDefaultViewPoint.SetCoord (theX, theY, theZ);
554 myDefaultViewAxis.SetCoord (theVx, theVy, theVz);
557 //=============================================================================
558 //function : SetShadingModel
560 //=============================================================================
561 void V3d_View::SetShadingModel (const Graphic3d_TypeOfShadingModel theShadingModel)
563 myView->SetShadingModel (theShadingModel);
566 //=============================================================================
567 //function : SetTextureEnv
569 //=============================================================================
570 void V3d_View::SetTextureEnv (const Handle(Graphic3d_TextureEnv)& theTexture)
572 myView->SetTextureEnv (theTexture);
574 if (myImmediateUpdate)
580 //=============================================================================
581 //function : SetVisualization
583 //=============================================================================
584 void V3d_View::SetVisualization (const V3d_TypeOfVisualization theType)
586 myView->SetVisualizationType (static_cast <Graphic3d_TypeOfVisualization> (theType));
588 if (myImmediateUpdate)
594 //=============================================================================
595 //function : SetFront
597 //=============================================================================
598 void V3d_View::SetFront()
600 gp_Ax3 a = MyViewer->PrivilegedPlane();
601 Standard_Real xo, yo, zo, vx, vy, vz, xu, yu, zu;
603 a.Direction().Coord(vx,vy,vz);
604 a.YDirection().Coord(xu,yu,zu);
605 a.Location().Coord(xo,yo,zo);
607 Handle(Graphic3d_Camera) aCamera = Camera();
609 aCamera->SetCenter (gp_Pnt (xo, yo, zo));
613 aCamera->SetDirection (gp_Dir (vx, vy, vz));
617 aCamera->SetDirection (gp_Dir (vx, vy, vz).Reversed());
620 aCamera->SetUp (gp_Dir (xu, yu, zu));
622 SwitchSetFront = !SwitchSetFront;
627 //=============================================================================
630 //=============================================================================
631 void V3d_View::Rotate (const Standard_Real ax,
632 const Standard_Real ay,
633 const Standard_Real az,
634 const Standard_Boolean Start)
636 Standard_Real Ax = ax;
637 Standard_Real Ay = ay;
638 Standard_Real Az = az;
640 if( Ax > 0. ) while ( Ax > DEUXPI ) Ax -= DEUXPI;
641 else if( Ax < 0. ) while ( Ax < -DEUXPI ) Ax += DEUXPI;
642 if( Ay > 0. ) while ( Ay > DEUXPI ) Ay -= DEUXPI;
643 else if( Ay < 0. ) while ( Ay < -DEUXPI ) Ay += DEUXPI;
644 if( Az > 0. ) while ( Az > DEUXPI ) Az -= DEUXPI;
645 else if( Az < 0. ) while ( Az < -DEUXPI ) Az += DEUXPI;
647 Handle(Graphic3d_Camera) aCamera = Camera();
651 myCamStartOpUp = aCamera->Up();
652 myCamStartOpDir = aCamera->Direction();
653 myCamStartOpEye = aCamera->Eye();
654 myCamStartOpCenter = aCamera->Center();
657 aCamera->SetUp (myCamStartOpUp);
658 aCamera->SetEyeAndCenter (myCamStartOpEye, myCamStartOpCenter);
659 aCamera->SetDirectionFromEye (myCamStartOpDir);
661 // rotate camera around 3 initial axes
662 gp_Dir aBackDir = -myCamStartOpDir;
663 gp_Dir aXAxis (myCamStartOpUp.Crossed (aBackDir));
664 gp_Dir aYAxis (aBackDir.Crossed (aXAxis));
665 gp_Dir aZAxis (aXAxis.Crossed (aYAxis));
667 gp_Trsf aRot[3], aTrsf;
668 aRot[0].SetRotation (gp_Ax1 (myCamStartOpCenter, aYAxis), -Ax);
669 aRot[1].SetRotation (gp_Ax1 (myCamStartOpCenter, aXAxis), Ay);
670 aRot[2].SetRotation (gp_Ax1 (myCamStartOpCenter, aZAxis), Az);
671 aTrsf.Multiply (aRot[0]);
672 aTrsf.Multiply (aRot[1]);
673 aTrsf.Multiply (aRot[2]);
675 aCamera->Transform (aTrsf);
680 //=============================================================================
683 //=============================================================================
684 void V3d_View::Rotate(const Standard_Real ax, const Standard_Real ay, const Standard_Real az,
685 const Standard_Real X, const Standard_Real Y, const Standard_Real Z, const Standard_Boolean Start)
688 Standard_Real Ax = ax ;
689 Standard_Real Ay = ay ;
690 Standard_Real Az = az ;
692 if( Ax > 0. ) while ( Ax > DEUXPI ) Ax -= DEUXPI ;
693 else if( Ax < 0. ) while ( Ax < -DEUXPI ) Ax += DEUXPI ;
694 if( Ay > 0. ) while ( Ay > DEUXPI ) Ay -= DEUXPI ;
695 else if( Ay < 0. ) while ( Ay < -DEUXPI ) Ay += DEUXPI ;
696 if( Az > 0. ) while ( Az > DEUXPI ) Az -= DEUXPI ;
697 else if( Az < 0. ) while ( Az < -DEUXPI ) Az += DEUXPI ;
699 Handle(Graphic3d_Camera) aCamera = Camera();
703 myGravityReferencePoint.SetCoord (X, Y, Z);
704 myCamStartOpUp = aCamera->Up();
705 myCamStartOpDir = aCamera->Direction();
706 myCamStartOpEye = aCamera->Eye();
707 myCamStartOpCenter = aCamera->Center();
710 const Graphic3d_Vertex& aVref = myGravityReferencePoint;
712 aCamera->SetUp (myCamStartOpUp);
713 aCamera->SetEyeAndCenter (myCamStartOpEye, myCamStartOpCenter);
714 aCamera->SetDirectionFromEye (myCamStartOpDir);
716 // rotate camera around 3 initial axes
717 gp_Pnt aRCenter (aVref.X(), aVref.Y(), aVref.Z());
719 gp_Dir aZAxis (aCamera->Direction().Reversed());
720 gp_Dir aYAxis (aCamera->Up());
721 gp_Dir aXAxis (aYAxis.Crossed (aZAxis));
723 gp_Trsf aRot[3], aTrsf;
724 aRot[0].SetRotation (gp_Ax1 (aRCenter, aYAxis), -Ax);
725 aRot[1].SetRotation (gp_Ax1 (aRCenter, aXAxis), Ay);
726 aRot[2].SetRotation (gp_Ax1 (aRCenter, aZAxis), Az);
727 aTrsf.Multiply (aRot[0]);
728 aTrsf.Multiply (aRot[1]);
729 aTrsf.Multiply (aRot[2]);
731 aCamera->Transform (aTrsf);
736 //=============================================================================
739 //=============================================================================
740 void V3d_View::Rotate(const V3d_TypeOfAxe Axe, const Standard_Real angle, const Standard_Boolean Start)
744 Rotate(angle,0.,0.,Start);
747 Rotate(0.,angle,0.,Start);
750 Rotate(0.,0.,angle,Start);
755 //=============================================================================
758 //=============================================================================
759 void V3d_View::Rotate (const V3d_TypeOfAxe theAxe, const Standard_Real theAngle,
760 const Standard_Real theX, const Standard_Real theY, const Standard_Real theZ, const Standard_Boolean theStart)
762 Standard_Real anAngle = theAngle;
764 if (anAngle > 0.0) while (anAngle > DEUXPI) anAngle -= DEUXPI;
765 else if (anAngle < 0.0) while (anAngle < -DEUXPI) anAngle += DEUXPI;
767 Handle(Graphic3d_Camera) aCamera = Camera();
771 myGravityReferencePoint.SetCoord (theX, theY, theZ);
772 myCamStartOpUp = aCamera->Up();
773 myCamStartOpDir = aCamera->Direction();
774 myCamStartOpEye = aCamera->Eye();
775 myCamStartOpCenter = aCamera->Center();
778 case V3d_X: myViewAxis = gp::DX(); break;
779 case V3d_Y: myViewAxis = gp::DY(); break;
780 case V3d_Z: myViewAxis = gp::DZ(); break;
784 const Graphic3d_Vertex& aVref = myGravityReferencePoint;
786 aCamera->SetUp (myCamStartOpUp);
787 aCamera->SetEyeAndCenter (myCamStartOpEye, myCamStartOpCenter);
788 aCamera->SetDirectionFromEye (myCamStartOpDir);
790 // rotate camera around passed axis
792 gp_Pnt aRCenter (aVref.X(), aVref.Y(), aVref.Z());
793 gp_Dir aRAxis ((theAxe == V3d_X) ? 1.0 : 0.0,
794 (theAxe == V3d_Y) ? 1.0 : 0.0,
795 (theAxe == V3d_Z) ? 1.0 : 0.0);
797 aRotation.SetRotation (gp_Ax1 (aRCenter, aRAxis), anAngle);
799 aCamera->Transform (aRotation);
804 //=============================================================================
807 //=============================================================================
808 void V3d_View::Rotate(const Standard_Real angle, const Standard_Boolean Start)
810 Standard_Real Angle = angle;
812 if( Angle > 0. ) while ( Angle > DEUXPI ) Angle -= DEUXPI ;
813 else if( Angle < 0. ) while ( Angle < -DEUXPI ) Angle += DEUXPI ;
815 Handle(Graphic3d_Camera) aCamera = Camera();
819 myCamStartOpUp = aCamera->Up();
820 myCamStartOpDir = aCamera->Direction();
821 myCamStartOpEye = aCamera->Eye();
822 myCamStartOpCenter = aCamera->Center();
825 aCamera->SetUp (myCamStartOpUp);
826 aCamera->SetEyeAndCenter (myCamStartOpEye, myCamStartOpCenter);
827 aCamera->SetDirectionFromEye (myCamStartOpDir);
830 gp_Pnt aRCenter (myDefaultViewPoint);
831 gp_Dir aRAxis (myDefaultViewAxis);
832 aRotation.SetRotation (gp_Ax1 (aRCenter, aRAxis), Angle);
834 aCamera->Transform (aRotation);
839 //=============================================================================
842 //=============================================================================
843 void V3d_View::Turn(const Standard_Real ax, const Standard_Real ay, const Standard_Real az, const Standard_Boolean Start)
845 Standard_Real Ax = ax;
846 Standard_Real Ay = ay;
847 Standard_Real Az = az;
849 if( Ax > 0. ) while ( Ax > DEUXPI ) Ax -= DEUXPI ;
850 else if( Ax < 0. ) while ( Ax < -DEUXPI ) Ax += DEUXPI ;
851 if( Ay > 0. ) while ( Ay > DEUXPI ) Ay -= DEUXPI ;
852 else if( Ay < 0. ) while ( Ay < -DEUXPI ) Ay += DEUXPI ;
853 if( Az > 0. ) while ( Az > DEUXPI ) Az -= DEUXPI ;
854 else if( Az < 0. ) while ( Az < -DEUXPI ) Az += DEUXPI ;
856 Handle(Graphic3d_Camera) aCamera = Camera();
860 myCamStartOpUp = aCamera->Up();
861 myCamStartOpDir = aCamera->Direction();
862 myCamStartOpEye = aCamera->Eye();
863 myCamStartOpCenter = aCamera->Center();
866 aCamera->SetUp (myCamStartOpUp);
867 aCamera->SetEyeAndCenter (myCamStartOpEye, myCamStartOpCenter);
868 aCamera->SetDirectionFromEye (myCamStartOpDir);
870 // rotate camera around 3 initial axes
871 gp_Pnt aRCenter = aCamera->Eye();
872 gp_Dir aZAxis (aCamera->Direction().Reversed());
873 gp_Dir aYAxis (aCamera->Up());
874 gp_Dir aXAxis (aYAxis.Crossed (aZAxis));
876 gp_Trsf aRot[3], aTrsf;
877 aRot[0].SetRotation (gp_Ax1 (aRCenter, aYAxis), -Ax);
878 aRot[1].SetRotation (gp_Ax1 (aRCenter, aXAxis), Ay);
879 aRot[2].SetRotation (gp_Ax1 (aRCenter, aZAxis), Az);
880 aTrsf.Multiply (aRot[0]);
881 aTrsf.Multiply (aRot[1]);
882 aTrsf.Multiply (aRot[2]);
884 aCamera->Transform (aTrsf);
889 //=============================================================================
892 //=============================================================================
893 void V3d_View::Turn(const V3d_TypeOfAxe Axe, const Standard_Real angle, const Standard_Boolean Start)
897 Turn(angle,0.,0.,Start);
900 Turn(0.,angle,0.,Start);
903 Turn(0.,0.,angle,Start);
908 //=============================================================================
911 //=============================================================================
912 void V3d_View::Turn(const Standard_Real angle, const Standard_Boolean Start)
914 Standard_Real Angle = angle ;
916 if( Angle > 0. ) while ( Angle > DEUXPI ) Angle -= DEUXPI ;
917 else if( Angle < 0. ) while ( Angle < -DEUXPI ) Angle += DEUXPI ;
919 Handle(Graphic3d_Camera) aCamera = Camera();
923 myCamStartOpUp = aCamera->Up();
924 myCamStartOpDir = aCamera->Direction();
925 myCamStartOpEye = aCamera->Eye();
926 myCamStartOpCenter = aCamera->Center();
929 aCamera->SetUp (myCamStartOpUp);
930 aCamera->SetEyeAndCenter (myCamStartOpEye, myCamStartOpCenter);
931 aCamera->SetDirectionFromEye (myCamStartOpDir);
934 gp_Pnt aRCenter = aCamera->Eye();
935 gp_Dir aRAxis (myDefaultViewAxis);
936 aRotation.SetRotation (gp_Ax1 (aRCenter, aRAxis), Angle);
938 aCamera->Transform (aRotation);
943 //=============================================================================
944 //function : SetTwist
946 //=============================================================================
947 void V3d_View::SetTwist(const Standard_Real angle)
949 Standard_Real Angle = angle ;
951 if( Angle > 0. ) while ( Angle > DEUXPI ) Angle -= DEUXPI ;
952 else if( Angle < 0. ) while ( Angle < -DEUXPI ) Angle += DEUXPI ;
954 Handle(Graphic3d_Camera) aCamera = Camera();
956 const gp_Dir aReferencePlane (aCamera->Direction().Reversed());
957 if (!screenAxis (aReferencePlane, gp::DZ(), myXscreenAxis, myYscreenAxis, myZscreenAxis)
958 && !screenAxis (aReferencePlane, gp::DY(), myXscreenAxis, myYscreenAxis, myZscreenAxis)
959 && !screenAxis (aReferencePlane, gp::DZ(), myXscreenAxis, myYscreenAxis, myZscreenAxis))
961 throw V3d_BadValue ("V3d_ViewSetTwist, alignment of Eye,At,Up,");
964 gp_Pnt aRCenter = aCamera->Center();
965 gp_Dir aZAxis (aCamera->Direction().Reversed());
968 aTrsf.SetRotation (gp_Ax1 (aRCenter, aZAxis), Angle);
970 aCamera->SetUp (gp_Dir (myYscreenAxis));
971 aCamera->Transform (aTrsf);
976 //=============================================================================
979 //=============================================================================
980 void V3d_View::SetEye(const Standard_Real X,const Standard_Real Y,const Standard_Real Z)
982 Standard_Real aTwistBefore = Twist();
984 Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
986 Handle(Graphic3d_Camera) aCamera = Camera();
988 aCamera->SetEye (gp_Pnt (X, Y, Z));
990 SetTwist (aTwistBefore);
992 SetImmediateUpdate (wasUpdateEnabled);
997 //=============================================================================
998 //function : SetDepth
1000 //=============================================================================
1001 void V3d_View::SetDepth(const Standard_Real Depth)
1003 V3d_BadValue_Raise_if (Depth == 0. ,"V3d_View::SetDepth, bad depth");
1005 Handle(Graphic3d_Camera) aCamera = Camera();
1009 // Move eye using center (target) as anchor.
1010 aCamera->SetDistance (Depth);
1014 // Move the view ref point instead of the eye.
1015 gp_Vec aDir (aCamera->Direction());
1016 gp_Pnt aCameraEye = aCamera->Eye();
1017 gp_Pnt aCameraCenter = aCameraEye.Translated (aDir.Multiplied (Abs (Depth)));
1019 aCamera->SetCenter (aCameraCenter);
1025 //=============================================================================
1026 //function : SetProj
1028 //=============================================================================
1029 void V3d_View::SetProj( const Standard_Real Vx,const Standard_Real Vy, const Standard_Real Vz )
1031 V3d_BadValue_Raise_if( Sqrt(Vx*Vx + Vy*Vy + Vz*Vz) <= 0.,
1032 "V3d_View::SetProj, null projection vector");
1034 Standard_Real aTwistBefore = Twist();
1036 Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
1038 Camera()->SetDirection (gp_Dir (Vx, Vy, Vz).Reversed());
1040 SetTwist(aTwistBefore);
1042 SetImmediateUpdate (wasUpdateEnabled);
1047 //=============================================================================
1048 //function : SetProj
1050 //=============================================================================
1051 void V3d_View::SetProj (const V3d_TypeOfOrientation theOrientation,
1052 const Standard_Boolean theIsYup)
1054 Graphic3d_Vec3d anUp = theIsYup ? Graphic3d_Vec3d (0.0, 1.0, 0.0) : Graphic3d_Vec3d (0.0, 0.0, 1.0);
1057 if (theOrientation == V3d_Ypos
1058 || theOrientation == V3d_Yneg)
1060 anUp.SetValues (0.0, 0.0, -1.0);
1065 if (theOrientation == V3d_Zpos)
1067 anUp.SetValues (0.0, 1.0, 0.0);
1069 else if (theOrientation == V3d_Zneg)
1071 anUp.SetValues (0.0, -1.0, 0.0);
1075 const gp_Dir aBck = V3d::GetProjAxis (theOrientation);
1077 // retain camera panning from origin when switching projection
1078 const Handle(Graphic3d_Camera)& aCamera = Camera();
1079 const gp_Pnt anOriginVCS = aCamera->ConvertWorld2View (gp::Origin());
1081 const Standard_Real aNewDist = aCamera->Eye().Distance (gp_Pnt (0, 0, 0));
1082 aCamera->SetEyeAndCenter (gp_XYZ (0, 0, 0) + aBck.XYZ() * aNewDist,
1084 aCamera->SetDirectionFromEye (-aBck);
1085 aCamera->SetUp (gp_Dir (anUp.x(), anUp.y(), anUp.z()));
1086 aCamera->OrthogonalizeUp();
1088 Panning (anOriginVCS.X(), anOriginVCS.Y());
1093 //=============================================================================
1096 //=============================================================================
1097 void V3d_View::SetAt(const Standard_Real X,const Standard_Real Y,const Standard_Real Z)
1099 Standard_Real aTwistBefore = Twist();
1101 Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
1103 Camera()->SetCenter (gp_Pnt (X, Y, Z));
1105 SetTwist (aTwistBefore);
1107 SetImmediateUpdate (wasUpdateEnabled);
1112 //=============================================================================
1115 //=============================================================================
1116 void V3d_View::SetUp (const Standard_Real theVx, const Standard_Real theVy, const Standard_Real theVz)
1118 Handle(Graphic3d_Camera) aCamera = Camera();
1120 const gp_Dir aReferencePlane (aCamera->Direction().Reversed());
1121 const gp_Dir anUp (theVx, theVy, theVz);
1122 if (!screenAxis (aReferencePlane, anUp, myXscreenAxis, myYscreenAxis, myZscreenAxis)
1123 && !screenAxis (aReferencePlane, gp::DZ(), myXscreenAxis, myYscreenAxis, myZscreenAxis)
1124 && !screenAxis (aReferencePlane, gp::DY(), myXscreenAxis, myYscreenAxis, myZscreenAxis)
1125 && !screenAxis (aReferencePlane, gp::DX(), myXscreenAxis, myYscreenAxis, myZscreenAxis))
1127 throw V3d_BadValue ("V3d_View::Setup, alignment of Eye,At,Up");
1130 aCamera->SetUp (gp_Dir (myYscreenAxis));
1135 //=============================================================================
1138 //=============================================================================
1139 void V3d_View::SetUp (const V3d_TypeOfOrientation theOrientation)
1141 Handle(Graphic3d_Camera) aCamera = Camera();
1143 const gp_Dir aReferencePlane (aCamera->Direction().Reversed());
1144 const gp_Dir anUp = V3d::GetProjAxis (theOrientation);
1145 if (!screenAxis (aReferencePlane, anUp, myXscreenAxis, myYscreenAxis, myZscreenAxis)
1146 && !screenAxis (aReferencePlane, gp::DZ(), myXscreenAxis, myYscreenAxis, myZscreenAxis)
1147 && !screenAxis (aReferencePlane, gp::DY(), myXscreenAxis, myYscreenAxis, myZscreenAxis)
1148 && !screenAxis (aReferencePlane, gp::DX(), myXscreenAxis, myYscreenAxis, myZscreenAxis))
1150 throw V3d_BadValue ("V3d_View::SetUp, alignment of Eye,At,Up");
1153 aCamera->SetUp (gp_Dir (myYscreenAxis));
1158 //=============================================================================
1159 //function : SetViewOrientationDefault
1161 //=============================================================================
1162 void V3d_View::SetViewOrientationDefault()
1164 myDefaultCamera->CopyOrientationData (Camera());
1167 //=======================================================================
1168 //function : SetViewMappingDefault
1170 //=======================================================================
1171 void V3d_View::SetViewMappingDefault()
1173 myDefaultCamera->CopyMappingData (Camera());
1176 //=============================================================================
1177 //function : ResetViewOrientation
1179 //=============================================================================
1180 void V3d_View::ResetViewOrientation()
1182 Camera()->CopyOrientationData (myDefaultCamera);
1186 //=======================================================================
1187 //function : ResetViewMapping
1189 //=======================================================================
1190 void V3d_View::ResetViewMapping()
1192 Camera()->CopyMappingData (myDefaultCamera);
1196 //=============================================================================
1199 //=============================================================================
1200 void V3d_View::Reset (const Standard_Boolean theToUpdate)
1202 Camera()->Copy (myDefaultCamera);
1204 SwitchSetFront = Standard_False;
1206 if (myImmediateUpdate || theToUpdate)
1212 //=======================================================================
1213 //function : SetCenter
1215 //=======================================================================
1216 void V3d_View::SetCenter (const Standard_Integer theXp,
1217 const Standard_Integer theYp)
1219 Standard_Real aXv, aYv;
1220 Convert (theXp, theYp, aXv, aYv);
1221 Translate (Camera(), aXv, aYv);
1226 //=============================================================================
1227 //function : SetSize
1229 //=============================================================================
1230 void V3d_View::SetSize (const Standard_Real theSize)
1232 V3d_BadValue_Raise_if (theSize <= 0.0, "V3d_View::SetSize, Window Size is NULL");
1234 Handle(Graphic3d_Camera) aCamera = Camera();
1236 aCamera->SetScale (aCamera->Aspect() >= 1.0 ? theSize / aCamera->Aspect() : theSize);
1241 //=============================================================================
1242 //function : SetZSize
1244 //=============================================================================
1245 void V3d_View::SetZSize (const Standard_Real theSize)
1247 Handle(Graphic3d_Camera) aCamera = Camera();
1249 Standard_Real Zmax = theSize / 2.;
1251 Standard_Real aDistance = aCamera->Distance();
1258 // ShortReal precision factor used to add meaningful tolerance to
1259 // ZNear, ZFar values in order to avoid equality after type conversion
1260 // to ShortReal matrices type.
1261 const Standard_Real aPrecision = 1.0 / Pow (10.0, ShortRealDigits() - 1);
1263 Standard_Real aZFar = Zmax + aDistance * 2.0;
1264 Standard_Real aZNear = -Zmax + aDistance;
1265 aZNear -= Abs (aZNear) * aPrecision;
1266 aZFar += Abs (aZFar) * aPrecision;
1268 if (!aCamera->IsOrthographic())
1270 if (aZFar < aPrecision)
1272 // Invalid case when both values are negative
1273 aZNear = aPrecision;
1274 aZFar = aPrecision * 2.0;
1276 else if (aZNear < Abs (aZFar) * aPrecision)
1278 // Z is less than 0.0, try to fix it using any appropriate z-scale
1279 aZNear = Abs (aZFar) * aPrecision;
1283 // If range is too small
1284 if (aZFar < (aZNear + Abs (aZFar) * aPrecision))
1286 aZFar = aZNear + Abs (aZFar) * aPrecision;
1289 aCamera->SetZRange (aZNear, aZFar);
1291 if (myImmediateUpdate)
1297 //=============================================================================
1298 //function : SetZoom
1300 //=============================================================================
1301 void V3d_View::SetZoom (const Standard_Real theCoef,const Standard_Boolean theToStart)
1303 V3d_BadValue_Raise_if (theCoef <= 0., "V3d_View::SetZoom, bad coefficient");
1305 Handle(Graphic3d_Camera) aCamera = Camera();
1309 myCamStartOpEye = aCamera->Eye();
1310 myCamStartOpCenter = aCamera->Center();
1313 Standard_Real aViewWidth = aCamera->ViewDimensions().X();
1314 Standard_Real aViewHeight = aCamera->ViewDimensions().Y();
1316 // ensure that zoom will not be too small or too big
1317 Standard_Real aCoef = theCoef;
1318 if (aViewWidth < aCoef * Precision::Confusion())
1320 aCoef = aViewWidth / Precision::Confusion();
1322 else if (aViewWidth > aCoef * 1e12)
1324 aCoef = aViewWidth / 1e12;
1326 if (aViewHeight < aCoef * Precision::Confusion())
1328 aCoef = aViewHeight / Precision::Confusion();
1330 else if (aViewHeight > aCoef * 1e12)
1332 aCoef = aViewHeight / 1e12;
1335 aCamera->SetEye (myCamStartOpEye);
1336 aCamera->SetCenter (myCamStartOpCenter);
1337 aCamera->SetScale (aCamera->Scale() / aCoef);
1342 //=============================================================================
1343 //function : SetScale
1345 //=============================================================================
1346 void V3d_View::SetScale( const Standard_Real Coef )
1348 V3d_BadValue_Raise_if( Coef <= 0. ,"V3d_View::SetScale, bad coefficient");
1350 Handle(Graphic3d_Camera) aCamera = Camera();
1352 Standard_Real aDefaultScale = myDefaultCamera->Scale();
1353 aCamera->SetAspect (myDefaultCamera->Aspect());
1354 aCamera->SetScale (aDefaultScale / Coef);
1359 //=============================================================================
1360 //function : SetAxialScale
1362 //=============================================================================
1363 void V3d_View::SetAxialScale( const Standard_Real Sx, const Standard_Real Sy, const Standard_Real Sz )
1365 V3d_BadValue_Raise_if( Sx <= 0. || Sy <= 0. || Sz <= 0.,"V3d_View::SetAxialScale, bad coefficient");
1367 Camera()->SetAxialScale (gp_XYZ (Sx, Sy, Sz));
1370 //=============================================================================
1371 //function : SetRatio
1373 //=============================================================================
1374 void V3d_View::SetRatio()
1376 if (MyWindow.IsNull())
1381 Standard_Integer aWidth = 0;
1382 Standard_Integer aHeight = 0;
1383 MyWindow->Size (aWidth, aHeight);
1384 if (aWidth > 0 && aHeight > 0)
1386 Standard_Real aRatio = static_cast<Standard_Real> (aWidth) /
1387 static_cast<Standard_Real> (aHeight);
1389 Camera() ->SetAspect (aRatio);
1390 myDefaultCamera->SetAspect (aRatio);
1394 //=============================================================================
1397 //=============================================================================
1398 void V3d_View::FitAll (const Standard_Real theMargin, const Standard_Boolean theToUpdate)
1400 FitAll (myView->MinMaxValues(), theMargin, theToUpdate);
1403 //=============================================================================
1406 //=============================================================================
1407 void V3d_View::FitAll (const Bnd_Box& theBox, const Standard_Real theMargin, const Standard_Boolean theToUpdate)
1409 Standard_ASSERT_RAISE(theMargin >= 0.0 && theMargin < 1.0, "Invalid margin coefficient");
1411 if (myView->NumberOfDisplayedStructures() == 0)
1416 if (!FitMinMax (Camera(), theBox, theMargin, 10.0 * Precision::Confusion()))
1421 if (myImmediateUpdate || theToUpdate)
1427 //=============================================================================
1428 //function : DepthFitAll
1430 //=============================================================================
1431 void V3d_View::DepthFitAll(const Standard_Real Aspect,
1432 const Standard_Real Margin)
1434 Standard_Real Xmin,Ymin,Zmin,Xmax,Ymax,Zmax,U,V,W,U1,V1,W1 ;
1435 Standard_Real Umin,Vmin,Wmin,Umax,Vmax,Wmax ;
1436 Standard_Real Dx,Dy,Dz,Size;
1438 Standard_Integer Nstruct = myView->NumberOfDisplayedStructures() ;
1440 if((Nstruct <= 0) || (Aspect < 0.) || (Margin < 0.) || (Margin > 1.)) {
1445 Bnd_Box aBox = myView->MinMaxValues();
1451 aBox.Get (Xmin,Ymin,Zmin,Xmax,Ymax,Zmax);
1452 Project (Xmin,Ymin,Zmin,U,V,W) ;
1453 Project (Xmax,Ymax,Zmax,U1,V1,W1) ;
1454 Umin = Min(U,U1) ; Umax = Max(U,U1) ;
1455 Vmin = Min(V,V1) ; Vmax = Max(V,V1) ;
1456 Wmin = Min(W,W1) ; Wmax = Max(W,W1) ;
1457 Project (Xmin,Ymin,Zmax,U,V,W) ;
1458 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1459 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1460 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1461 Project (Xmax,Ymin,Zmax,U,V,W) ;
1462 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1463 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1464 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1465 Project (Xmax,Ymin,Zmin,U,V,W) ;
1466 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1467 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1468 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1469 Project (Xmax,Ymax,Zmin,U,V,W) ;
1470 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1471 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1472 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1473 Project (Xmin,Ymax,Zmax,U,V,W) ;
1474 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1475 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1476 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1477 Project (Xmin,Ymax,Zmin,U,V,W) ;
1478 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1479 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1480 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1483 Wmax = Max(Abs(Wmin),Abs(Wmax)) ;
1484 Dz = 2.*Wmax + Margin * Wmax;
1486 // Compute depth value
1487 Dx = Abs(Umax - Umin) ; Dy = Abs(Vmax - Vmin) ; // Dz = Abs(Wmax - Wmin);
1488 Dx += Margin * Dx; Dy += Margin * Dy;
1489 Size = Sqrt(Dx*Dx + Dy*Dy + Dz*Dz);
1492 SetDepth( Aspect * Size / 2.);
1498 //=======================================================================
1499 //function : WindowFit
1501 //=======================================================================
1502 void V3d_View::WindowFit (const Standard_Integer theMinXp,
1503 const Standard_Integer theMinYp,
1504 const Standard_Integer theMaxXp,
1505 const Standard_Integer theMaxYp)
1507 Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
1509 Handle(Graphic3d_Camera) aCamera = Camera();
1511 if (!aCamera->IsOrthographic())
1513 // normalize view coordinates
1514 Standard_Integer aWinWidth, aWinHeight;
1515 MyWindow->Size (aWinWidth, aWinHeight);
1517 // z coordinate of camera center
1518 Standard_Real aDepth = aCamera->Project (aCamera->Center()).Z();
1520 // camera projection coordinate are in NDC which are normalized [-1, 1]
1521 Standard_Real aUMin = (2.0 / aWinWidth) * theMinXp - 1.0;
1522 Standard_Real aUMax = (2.0 / aWinWidth) * theMaxXp - 1.0;
1523 Standard_Real aVMin = (2.0 / aWinHeight) * theMinYp - 1.0;
1524 Standard_Real aVMax = (2.0 / aWinHeight) * theMaxYp - 1.0;
1526 // compute camera panning
1527 gp_Pnt aScreenCenter (0.0, 0.0, aDepth);
1528 gp_Pnt aFitCenter ((aUMin + aUMax) * 0.5, (aVMin + aVMax) * 0.5, aDepth);
1529 gp_Pnt aPanTo = aCamera->ConvertProj2View (aFitCenter);
1530 gp_Pnt aPanFrom = aCamera->ConvertProj2View (aScreenCenter);
1531 gp_Vec aPanVec (aPanFrom, aPanTo);
1533 // compute section size
1534 gp_Pnt aFitTopRight (aUMax, aVMax, aDepth);
1535 gp_Pnt aFitBotLeft (aUMin, aVMin, aDepth);
1536 gp_Pnt aViewBotLeft = aCamera->ConvertProj2View (aFitBotLeft);
1537 gp_Pnt aViewTopRight = aCamera->ConvertProj2View (aFitTopRight);
1539 Standard_Real aUSize = aViewTopRight.X() - aViewBotLeft.X();
1540 Standard_Real aVSize = aViewTopRight.Y() - aViewBotLeft.Y();
1542 Translate (aCamera, aPanVec.X(), -aPanVec.Y());
1543 Scale (aCamera, aUSize, aVSize);
1547 Standard_Real aX1, aY1, aX2, aY2;
1548 Convert (theMinXp, theMinYp, aX1, aY1);
1549 Convert (theMaxXp, theMaxYp, aX2, aY2);
1550 FitAll (aX1, aY1, aX2, aY2);
1553 SetImmediateUpdate (wasUpdateEnabled);
1558 //=======================================================================
1559 //function : ConvertToGrid
1561 //=======================================================================
1562 void V3d_View::ConvertToGrid(const Standard_Integer Xp,
1563 const Standard_Integer Yp,
1566 Standard_Real& Zg) const
1568 Graphic3d_Vertex aVrp;
1569 Standard_Real anX, anY, aZ;
1570 Convert (Xp, Yp, anX, anY, aZ);
1571 aVrp.SetCoord (anX, anY, aZ);
1573 if( MyViewer->Grid()->IsActive() ) {
1574 Graphic3d_Vertex aNewVrp = Compute (aVrp) ;
1575 aNewVrp.Coord (Xg,Yg,Zg) ;
1577 aVrp.Coord (Xg,Yg,Zg) ;
1580 //=======================================================================
1581 //function : ConvertToGrid
1583 //=======================================================================
1584 void V3d_View::ConvertToGrid(const Standard_Real X,
1585 const Standard_Real Y,
1586 const Standard_Real Z,
1589 Standard_Real& Zg) const
1591 if( MyViewer->Grid()->IsActive() ) {
1592 Graphic3d_Vertex aVrp (X,Y,Z) ;
1593 Graphic3d_Vertex aNewVrp = Compute (aVrp) ;
1594 aNewVrp.Coord(Xg,Yg,Zg) ;
1596 Xg = X; Yg = Y; Zg = Z;
1600 //=======================================================================
1601 //function : Convert
1603 //=======================================================================
1604 Standard_Real V3d_View::Convert(const Standard_Integer Vp) const
1606 Standard_Integer aDxw, aDyw ;
1608 V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
1610 MyWindow->Size (aDxw, aDyw);
1611 Standard_Real aValue;
1613 gp_Pnt aViewDims = Camera()->ViewDimensions();
1614 aValue = aViewDims.X() * (Standard_Real)Vp / (Standard_Real)aDxw;
1619 //=======================================================================
1620 //function : Convert
1622 //=======================================================================
1623 void V3d_View::Convert(const Standard_Integer Xp,
1624 const Standard_Integer Yp,
1626 Standard_Real& Yv) const
1628 Standard_Integer aDxw, aDyw;
1630 V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
1632 MyWindow->Size (aDxw, aDyw);
1634 gp_Pnt aPoint (Xp * 2.0 / aDxw - 1.0, (aDyw - Yp) * 2.0 / aDyw - 1.0, 0.0);
1635 aPoint = Camera()->ConvertProj2View (aPoint);
1641 //=======================================================================
1642 //function : Convert
1644 //=======================================================================
1645 Standard_Integer V3d_View::Convert(const Standard_Real Vv) const
1647 V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
1649 Standard_Integer aDxw, aDyw;
1650 MyWindow->Size (aDxw, aDyw);
1652 gp_Pnt aViewDims = Camera()->ViewDimensions();
1653 Standard_Integer aValue = RealToInt (aDxw * Vv / (aViewDims.X()));
1658 //=======================================================================
1659 //function : Convert
1661 //=======================================================================
1662 void V3d_View::Convert(const Standard_Real Xv,
1663 const Standard_Real Yv,
1664 Standard_Integer& Xp,
1665 Standard_Integer& Yp) const
1667 V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
1669 Standard_Integer aDxw, aDyw;
1670 MyWindow->Size (aDxw, aDyw);
1672 gp_Pnt aPoint (Xv, Yv, 0.0);
1673 aPoint = Camera()->ConvertView2Proj (aPoint);
1674 aPoint = gp_Pnt ((aPoint.X() + 1.0) * aDxw / 2.0, aDyw - (aPoint.Y() + 1.0) * aDyw / 2.0, 0.0);
1676 Xp = RealToInt (aPoint.X());
1677 Yp = RealToInt (aPoint.Y());
1680 //=======================================================================
1681 //function : Convert
1683 //=======================================================================
1684 void V3d_View::Convert(const Standard_Integer Xp,
1685 const Standard_Integer Yp,
1688 Standard_Real& Z) const
1690 V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
1691 Standard_Integer aHeight, aWidth;
1692 MyWindow->Size (aWidth, aHeight);
1694 Standard_Real anX = 2.0 * Xp / aWidth - 1.0;
1695 Standard_Real anY = 2.0 * (aHeight - 1 - Yp) / aHeight - 1.0;
1696 Standard_Real aZ = 2.0 * 0.0 - 1.0;
1698 gp_Pnt aResult = Camera()->UnProject (gp_Pnt (anX, anY, aZ));
1705 //=======================================================================
1706 //function : ConvertWithProj
1708 //=======================================================================
1709 void V3d_View::ConvertWithProj(const Standard_Integer theXp,
1710 const Standard_Integer theYp,
1711 Standard_Real& theX,
1712 Standard_Real& theY,
1713 Standard_Real& theZ,
1714 Standard_Real& theDx,
1715 Standard_Real& theDy,
1716 Standard_Real& theDz) const
1718 V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
1719 Standard_Integer aHeight = 0, aWidth = 0;
1720 MyWindow->Size (aWidth, aHeight);
1722 const Standard_Real anX = 2.0 * theXp / aWidth - 1.0;
1723 const Standard_Real anY = 2.0 * (aHeight - 1 - theYp) / aHeight - 1.0;
1724 const Standard_Real aZ = 2.0 * 0.0 - 1.0;
1726 const Handle(Graphic3d_Camera)& aCamera = Camera();
1727 const gp_Pnt aResult1 = aCamera->UnProject (gp_Pnt (anX, anY, aZ));
1728 const gp_Pnt aResult2 = aCamera->UnProject (gp_Pnt (anX, anY, aZ - 10.0));
1730 theX = aResult1.X();
1731 theY = aResult1.Y();
1732 theZ = aResult1.Z();
1733 Graphic3d_Vec3d aNormDir (theX - aResult2.X(),
1734 theY - aResult2.Y(),
1735 theZ - aResult2.Z());
1736 aNormDir.Normalize();
1738 theDx = aNormDir.x();
1739 theDy = aNormDir.y();
1740 theDz = aNormDir.z();
1743 //=======================================================================
1744 //function : Convert
1746 //=======================================================================
1747 void V3d_View::Convert(const Standard_Real X,
1748 const Standard_Real Y,
1749 const Standard_Real Z,
1750 Standard_Integer& Xp,
1751 Standard_Integer& Yp) const
1753 V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
1754 Standard_Integer aHeight, aWidth;
1755 MyWindow->Size (aWidth, aHeight);
1757 gp_Pnt aPoint = Camera()->Project (gp_Pnt (X, Y, Z));
1759 Xp = RealToInt ((aPoint.X() + 1) * 0.5 * aWidth);
1760 Yp = RealToInt (aHeight - 1 - (aPoint.Y() + 1) * 0.5 * aHeight);
1763 //=======================================================================
1764 //function : Project
1766 //=======================================================================
1767 void V3d_View::Project (const Standard_Real theX,
1768 const Standard_Real theY,
1769 const Standard_Real theZ,
1770 Standard_Real& theXp,
1771 Standard_Real& theYp) const
1774 Project (theX, theY, theZ, theXp, theYp, aZp);
1777 //=======================================================================
1778 //function : Project
1780 //=======================================================================
1781 void V3d_View::Project (const Standard_Real theX,
1782 const Standard_Real theY,
1783 const Standard_Real theZ,
1784 Standard_Real& theXp,
1785 Standard_Real& theYp,
1786 Standard_Real& theZp) const
1788 Handle(Graphic3d_Camera) aCamera = Camera();
1790 gp_XYZ aViewSpaceDimensions = aCamera->ViewDimensions();
1791 Standard_Real aXSize = aViewSpaceDimensions.X();
1792 Standard_Real aYSize = aViewSpaceDimensions.Y();
1793 Standard_Real aZSize = aViewSpaceDimensions.Z();
1795 gp_Pnt aPoint = aCamera->Project (gp_Pnt (theX, theY, theZ));
1797 // NDC [-1, 1] --> PROJ [ -size / 2, +size / 2 ]
1798 theXp = aPoint.X() * aXSize * 0.5;
1799 theYp = aPoint.Y() * aYSize * 0.5;
1800 theZp = aPoint.Z() * aZSize * 0.5;
1803 //=======================================================================
1804 //function : BackgroundColor
1806 //=======================================================================
1807 void V3d_View::BackgroundColor(const Quantity_TypeOfColor Type,
1810 Standard_Real& V3) const
1812 Quantity_Color C = BackgroundColor() ;
1813 C.Values(V1,V2,V3,Type) ;
1816 //=======================================================================
1817 //function : BackgroundColor
1819 //=======================================================================
1820 Quantity_Color V3d_View::BackgroundColor() const
1822 return myView->Background().Color() ;
1825 //=======================================================================
1826 //function : GradientBackgroundColors
1828 //=======================================================================
1829 void V3d_View::GradientBackgroundColors (Quantity_Color& theColor1, Quantity_Color& theColor2) const
1831 myView->GradientBackground().Colors (theColor1, theColor2);
1834 //=======================================================================
1835 //function : GradientBackground
1837 //=======================================================================
1838 Aspect_GradientBackground V3d_View::GradientBackground() const
1840 return myView->GradientBackground();
1843 //=======================================================================
1846 //=======================================================================
1847 Standard_Real V3d_View::Scale() const
1849 return myDefaultCamera->Scale() / Camera()->Scale();
1852 //=======================================================================
1853 //function : AxialScale
1855 //=======================================================================
1856 void V3d_View::AxialScale(Standard_Real& Sx, Standard_Real& Sy, Standard_Real& Sz) const
1858 gp_Pnt anAxialScale = Camera()->AxialScale();
1859 Sx = anAxialScale.X();
1860 Sy = anAxialScale.Y();
1861 Sz = anAxialScale.Z();
1864 //=======================================================================
1867 //=======================================================================
1868 void V3d_View::Size(Standard_Real& Width, Standard_Real& Height) const
1870 gp_Pnt aViewDims = Camera()->ViewDimensions();
1872 Width = aViewDims.X();
1873 Height = aViewDims.Y();
1876 //=======================================================================
1879 //=======================================================================
1880 Standard_Real V3d_View::ZSize() const
1882 gp_Pnt aViewDims = Camera()->ViewDimensions();
1884 return aViewDims.Z();
1887 //=======================================================================
1890 //=======================================================================
1891 Standard_Integer V3d_View::MinMax(Standard_Real& Umin,
1892 Standard_Real& Vmin,
1893 Standard_Real& Umax,
1894 Standard_Real& Vmax) const
1896 Standard_Real Wmin,Wmax,U,V,W ;
1897 Standard_Real Xmin,Ymin,Zmin,Xmax,Ymax,Zmax ;
1899 Standard_Integer Nstruct = myView->NumberOfDisplayedStructures() ;
1902 Bnd_Box aBox = myView->MinMaxValues();
1903 aBox.Get (Xmin,Ymin,Zmin,Xmax,Ymax,Zmax);
1904 Project (Xmin,Ymin,Zmin,Umin,Vmin,Wmin) ;
1905 Project (Xmax,Ymax,Zmax,Umax,Vmax,Wmax) ;
1906 Project (Xmin,Ymin,Zmax,U,V,W) ;
1907 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1908 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1909 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1910 Project (Xmax,Ymin,Zmax,U,V,W) ;
1911 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1912 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1913 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1914 Project (Xmax,Ymin,Zmin,U,V,W) ;
1915 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1916 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1917 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1918 Project (Xmax,Ymax,Zmin,U,V,W) ;
1919 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1920 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1921 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1922 Project (Xmin,Ymax,Zmax,U,V,W) ;
1923 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1924 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1925 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1926 Project (Xmin,Ymax,Zmin,U,V,W) ;
1927 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1928 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1929 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1934 //=======================================================================
1937 //=======================================================================
1938 Standard_Integer V3d_View::MinMax(Standard_Real& Xmin,
1939 Standard_Real& Ymin,
1940 Standard_Real& Zmin,
1941 Standard_Real& Xmax,
1942 Standard_Real& Ymax,
1943 Standard_Real& Zmax) const
1946 // Standard_Integer Nstruct = (MyView->DisplayedStructures())->Extent() ;
1947 Standard_Integer Nstruct = myView->NumberOfDisplayedStructures() ;
1950 Bnd_Box aBox = myView->MinMaxValues();
1951 aBox.Get (Xmin,Ymin,Zmin,Xmax,Ymax,Zmax);
1956 //=======================================================================
1957 //function : GravityPoint
1959 //=======================================================================
1960 gp_Pnt V3d_View::GravityPoint() const
1962 Graphic3d_MapOfStructure aSetOfStructures;
1963 myView->DisplayedStructures (aSetOfStructures);
1965 Standard_Boolean hasSelection = Standard_False;
1966 for (Graphic3d_MapIteratorOfMapOfStructure aStructIter (aSetOfStructures);
1967 aStructIter.More(); aStructIter.Next())
1969 if (aStructIter.Key()->IsHighlighted()
1970 && aStructIter.Key()->IsVisible())
1972 hasSelection = Standard_True;
1977 Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
1978 Standard_Integer aNbPoints = 0;
1979 gp_XYZ aResult (0.0, 0.0, 0.0);
1980 for (Graphic3d_MapIteratorOfMapOfStructure aStructIter (aSetOfStructures);
1981 aStructIter.More(); aStructIter.Next())
1983 const Handle(Graphic3d_Structure)& aStruct = aStructIter.Key();
1984 if (!aStruct->IsVisible()
1985 || aStruct->IsInfinite()
1986 || (hasSelection && !aStruct->IsHighlighted()))
1991 const Graphic3d_BndBox3d& aBox = aStruct->CStructure()->BoundingBox();
1992 if (!aBox.IsValid())
1997 // skip transformation-persistent objects
1998 if (!aStruct->TransformPersistence().IsNull())
2003 // use camera projection to find gravity point
2004 Xmin = aBox.CornerMin().x();
2005 Ymin = aBox.CornerMin().y();
2006 Zmin = aBox.CornerMin().z();
2007 Xmax = aBox.CornerMax().x();
2008 Ymax = aBox.CornerMax().y();
2009 Zmax = aBox.CornerMax().z();
2010 gp_Pnt aPnts[THE_NB_BOUND_POINTS] =
2012 gp_Pnt (Xmin, Ymin, Zmin), gp_Pnt (Xmin, Ymin, Zmax),
2013 gp_Pnt (Xmin, Ymax, Zmin), gp_Pnt (Xmin, Ymax, Zmax),
2014 gp_Pnt (Xmax, Ymin, Zmin), gp_Pnt (Xmax, Ymin, Zmax),
2015 gp_Pnt (Xmax, Ymax, Zmin), gp_Pnt (Xmax, Ymax, Zmax)
2018 for (Standard_Integer aPntIt = 0; aPntIt < THE_NB_BOUND_POINTS; ++aPntIt)
2020 const gp_Pnt& aBndPnt = aPnts[aPntIt];
2021 const gp_Pnt aProjected = Camera()->Project (aBndPnt);
2022 if (Abs (aProjected.X()) <= 1.0
2023 && Abs (aProjected.Y()) <= 1.0)
2025 aResult += aBndPnt.XYZ();
2033 // fallback - just use bounding box of entire scene
2034 Bnd_Box aBox = myView->MinMaxValues();
2037 aBox.Get (Xmin, Ymin, Zmin,
2039 gp_Pnt aPnts[THE_NB_BOUND_POINTS] =
2041 gp_Pnt (Xmin, Ymin, Zmin), gp_Pnt (Xmin, Ymin, Zmax),
2042 gp_Pnt (Xmin, Ymax, Zmin), gp_Pnt (Xmin, Ymax, Zmax),
2043 gp_Pnt (Xmax, Ymin, Zmin), gp_Pnt (Xmax, Ymin, Zmax),
2044 gp_Pnt (Xmax, Ymax, Zmin), gp_Pnt (Xmax, Ymax, Zmax)
2047 for (Standard_Integer aPntIt = 0; aPntIt < THE_NB_BOUND_POINTS; ++aPntIt)
2049 const gp_Pnt& aBndPnt = aPnts[aPntIt];
2050 aResult += aBndPnt.XYZ();
2058 aResult /= aNbPoints;
2064 //=======================================================================
2067 //=======================================================================
2068 void V3d_View::Eye(Standard_Real& X, Standard_Real& Y, Standard_Real& Z) const
2070 gp_Pnt aCameraEye = Camera()->Eye();
2076 //=============================================================================
2077 //function : ProjReferenceAxe
2079 //=============================================================================
2080 void V3d_View::ProjReferenceAxe(const Standard_Integer Xpix,
2081 const Standard_Integer Ypix,
2087 Standard_Real& VZ) const
2089 Standard_Real Xo,Yo,Zo;
2091 Convert (Xpix, Ypix, XP, YP, ZP);
2092 if ( Type() == V3d_PERSPECTIVE )
2094 FocalReferencePoint (Xo,Yo,Zo);
2105 //=============================================================================
2108 //=============================================================================
2109 Standard_Real V3d_View::Depth() const
2111 return Camera()->Distance();
2114 //=============================================================================
2117 //=============================================================================
2118 void V3d_View::Proj(Standard_Real& Dx, Standard_Real& Dy, Standard_Real& Dz) const
2120 gp_Dir aCameraDir = Camera()->Direction().Reversed();
2121 Dx = aCameraDir.X();
2122 Dy = aCameraDir.Y();
2123 Dz = aCameraDir.Z();
2126 //=============================================================================
2129 //=============================================================================
2130 void V3d_View::At(Standard_Real& X, Standard_Real& Y, Standard_Real& Z) const
2132 gp_Pnt aCameraCenter = Camera()->Center();
2133 X = aCameraCenter.X();
2134 Y = aCameraCenter.Y();
2135 Z = aCameraCenter.Z();
2138 //=============================================================================
2141 //=============================================================================
2142 void V3d_View::Up(Standard_Real& Vx, Standard_Real& Vy, Standard_Real& Vz) const
2144 gp_Dir aCameraUp = Camera()->Up();
2150 //=============================================================================
2153 //=============================================================================
2154 Standard_Real V3d_View::Twist() const
2156 gp_Vec Xaxis, Yaxis, Zaxis;
2157 const gp_Dir aReferencePlane (Camera()->Direction().Reversed());
2158 if (!screenAxis (aReferencePlane, gp::DZ(), Xaxis, Yaxis, Zaxis)
2159 && !screenAxis (aReferencePlane, gp::DY(), Xaxis, Yaxis, Zaxis)
2160 && !screenAxis (aReferencePlane, gp::DX(), Xaxis, Yaxis, Zaxis))
2165 // Compute Cross Vector From Up & Origin
2166 const gp_Dir aCameraUp = Camera()->Up();
2167 const gp_XYZ aP = Yaxis.XYZ().Crossed (aCameraUp.XYZ());
2170 Standard_Real anAngle = ASin (Max (Min (aP.Modulus(), 1.0), -1.0));
2171 if (Yaxis.Dot (aCameraUp.XYZ()) < 0.0)
2173 anAngle = M_PI - anAngle;
2178 const gp_Dir aProjDir = Camera()->Direction().Reversed();
2179 if (aP.Dot (aProjDir.XYZ()) < 0.0)
2181 anAngle = DEUXPI - anAngle;
2187 //=============================================================================
2188 //function : ShadingModel
2190 //=============================================================================
2191 Graphic3d_TypeOfShadingModel V3d_View::ShadingModel() const
2193 return myView->ShadingModel();
2196 //=============================================================================
2197 //function : TextureEnv
2199 //=============================================================================
2200 Handle(Graphic3d_TextureEnv) V3d_View::TextureEnv() const
2202 return myView->TextureEnv();
2205 //=============================================================================
2206 //function : Visualization
2208 //=============================================================================
2209 V3d_TypeOfVisualization V3d_View::Visualization() const
2211 return static_cast<V3d_TypeOfVisualization> (myView->VisualizationType());
2214 //=============================================================================
2215 //function : IfWindow
2217 //=============================================================================
2218 Standard_Boolean V3d_View::IfWindow() const
2220 return myView->IsDefined();
2223 //=============================================================================
2226 //=============================================================================
2227 V3d_TypeOfView V3d_View::Type() const
2229 return Camera()->IsOrthographic() ? V3d_ORTHOGRAPHIC : V3d_PERSPECTIVE;
2232 //=============================================================================
2233 //function : SetFocale
2235 //=============================================================================
2236 void V3d_View::SetFocale( const Standard_Real focale )
2238 Handle(Graphic3d_Camera) aCamera = Camera();
2240 if (aCamera->IsOrthographic())
2245 Standard_Real aFOVyRad = ATan (focale / (aCamera->Distance() * 2.0));
2247 aCamera->SetFOVy (aFOVyRad * (360 / M_PI));
2252 //=============================================================================
2255 //=============================================================================
2256 Standard_Real V3d_View::Focale() const
2258 Handle(Graphic3d_Camera) aCamera = Camera();
2260 if (aCamera->IsOrthographic())
2265 return aCamera->Distance() * 2.0 * Tan (aCamera->FOVy() * M_PI / 360.0);
2268 //=============================================================================
2269 //function : screenAxis
2271 //=============================================================================
2272 Standard_Boolean V3d_View::screenAxis (const gp_Dir& theVpn, const gp_Dir& theVup,
2273 gp_Vec& theXaxe, gp_Vec& theYaxe, gp_Vec& theZaxe)
2275 theXaxe = theVup.XYZ().Crossed (theVpn.XYZ());
2276 if (theXaxe.Magnitude() <= gp::Resolution())
2278 return Standard_False;
2280 theXaxe.Normalize();
2282 theYaxe = theVpn.XYZ().Crossed (theXaxe.XYZ());
2283 if (theYaxe.Magnitude() <= gp::Resolution())
2285 return Standard_False;
2287 theYaxe.Normalize();
2289 theZaxe = theVpn.XYZ();
2290 theZaxe.Normalize();
2291 return Standard_True;
2294 //=============================================================================
2295 //function : TrsPoint
2297 //=============================================================================
2298 gp_XYZ V3d_View::TrsPoint (const Graphic3d_Vertex& thePnt, const TColStd_Array2OfReal& theMat)
2301 const Standard_Integer lr = theMat.LowerRow();
2302 const Standard_Integer ur = theMat.UpperRow();
2303 const Standard_Integer lc = theMat.LowerCol();
2304 const Standard_Integer uc = theMat.UpperCol();
2305 if ((ur - lr + 1 != 4) || (uc - lc + 1 != 4))
2307 return gp_XYZ (thePnt.X(), thePnt.Y(), thePnt.Z());
2310 Standard_Real X, Y, Z;
2311 thePnt.Coord (X,Y,Z);
2312 const Standard_Real XX = (theMat(lr,lc+3) + X*theMat(lr,lc) + Y*theMat(lr,lc+1) + Z*theMat(lr,lc+2)) / theMat(lr+3,lc+3);
2313 const Standard_Real YY = (theMat(lr+1,lc+3) + X*theMat(lr+1,lc) + Y*theMat(lr+1,lc+1) + Z*theMat(lr+1,lc+2))/theMat(lr+3,lc+3);
2314 const Standard_Real ZZ = (theMat(lr+2,lc+3) + X*theMat(lr+2,lc) + Y*theMat(lr+2,lc+1) + Z*theMat(lr+2,lc+2))/theMat(lr+3,lc+3);
2315 return gp_XYZ (XX, YY, ZZ);
2318 //=======================================================================
2321 //=======================================================================
2322 void V3d_View::Pan (const Standard_Integer theDXp,
2323 const Standard_Integer theDYp,
2324 const Standard_Real theZoomFactor,
2325 const Standard_Boolean theToStart)
2327 Panning (Convert (theDXp), Convert (theDYp), theZoomFactor, theToStart);
2330 //=======================================================================
2331 //function : Panning
2333 //=======================================================================
2334 void V3d_View::Panning (const Standard_Real theDXv,
2335 const Standard_Real theDYv,
2336 const Standard_Real theZoomFactor,
2337 const Standard_Boolean theToStart)
2339 Standard_ASSERT_RAISE (theZoomFactor > 0.0, "Bad zoom factor");
2341 Handle(Graphic3d_Camera) aCamera = Camera();
2345 myCamStartOpDir = aCamera->Direction();
2346 myCamStartOpEye = aCamera->Eye();
2347 myCamStartOpCenter = aCamera->Center();
2350 Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
2352 gp_Pnt aViewDims = aCamera->ViewDimensions();
2354 aCamera->SetEyeAndCenter (myCamStartOpEye, myCamStartOpCenter);
2355 aCamera->SetDirectionFromEye (myCamStartOpDir);
2356 Translate (aCamera, -theDXv, -theDYv);
2357 Scale (aCamera, aViewDims.X() / theZoomFactor, aViewDims.Y() / theZoomFactor);
2359 SetImmediateUpdate (wasUpdateEnabled);
2364 //=======================================================================
2367 //=======================================================================
2368 void V3d_View::Zoom (const Standard_Integer theXp1,
2369 const Standard_Integer theYp1,
2370 const Standard_Integer theXp2,
2371 const Standard_Integer theYp2)
2373 Standard_Integer aDx = theXp2 - theXp1;
2374 Standard_Integer aDy = theYp2 - theYp1;
2375 if (aDx != 0 || aDy != 0)
2377 Standard_Real aCoeff = Sqrt( (Standard_Real)(aDx * aDx + aDy * aDy) ) / 100.0 + 1.0;
2378 aCoeff = (aDx > 0) ? aCoeff : 1.0 / aCoeff;
2379 SetZoom (aCoeff, Standard_True);
2383 //=======================================================================
2384 //function : StartZoomAtPoint
2386 //=======================================================================
2387 void V3d_View::StartZoomAtPoint (const Standard_Integer theXp,
2388 const Standard_Integer theYp)
2390 MyZoomAtPointX = theXp;
2391 MyZoomAtPointY = theYp;
2394 //=======================================================================
2395 //function : ZoomAtPoint
2397 //=======================================================================
2398 void V3d_View::ZoomAtPoint (const Standard_Integer theMouseStartX,
2399 const Standard_Integer theMouseStartY,
2400 const Standard_Integer theMouseEndX,
2401 const Standard_Integer theMouseEndY)
2403 Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
2406 Standard_Real aDxy = Standard_Real ((theMouseEndX + theMouseEndY) - (theMouseStartX + theMouseStartY));
2407 Standard_Real aDZoom = Abs (aDxy) / 100.0 + 1.0;
2408 aDZoom = (aDxy > 0.0) ? aDZoom : 1.0 / aDZoom;
2410 V3d_BadValue_Raise_if (aDZoom <= 0.0, "V3d_View::ZoomAtPoint, bad coefficient");
2412 Handle(Graphic3d_Camera) aCamera = Camera();
2414 Standard_Real aViewWidth = aCamera->ViewDimensions().X();
2415 Standard_Real aViewHeight = aCamera->ViewDimensions().Y();
2417 // ensure that zoom will not be too small or too big.
2418 Standard_Real aCoef = aDZoom;
2419 if (aViewWidth < aCoef * Precision::Confusion())
2421 aCoef = aViewWidth / Precision::Confusion();
2423 else if (aViewWidth > aCoef * 1e12)
2425 aCoef = aViewWidth / 1e12;
2427 if (aViewHeight < aCoef * Precision::Confusion())
2429 aCoef = aViewHeight / Precision::Confusion();
2431 else if (aViewHeight > aCoef * 1e12)
2433 aCoef = aViewHeight / 1e12;
2436 Standard_Real aZoomAtPointXv = 0.0;
2437 Standard_Real aZoomAtPointYv = 0.0;
2438 Convert (MyZoomAtPointX, MyZoomAtPointY, aZoomAtPointXv, aZoomAtPointYv);
2440 Standard_Real aDxv = aZoomAtPointXv / aCoef;
2441 Standard_Real aDyv = aZoomAtPointYv / aCoef;
2443 aCamera->SetScale (aCamera->Scale() / aCoef);
2444 Translate (aCamera, aZoomAtPointXv - aDxv, aZoomAtPointYv - aDyv);
2446 SetImmediateUpdate (wasUpdateEnabled);
2451 //=============================================================================
2452 //function : AxialScale
2454 //=============================================================================
2455 void V3d_View::AxialScale (const Standard_Integer Dx,
2456 const Standard_Integer Dy,
2457 const V3d_TypeOfAxe Axis)
2459 if( Dx != 0. || Dy != 0. ) {
2460 Standard_Real Sx, Sy, Sz;
2461 AxialScale( Sx, Sy, Sz );
2462 Standard_Real dscale = Sqrt(Dx*Dx + Dy*Dy) / 100. + 1;
2463 dscale = (Dx > 0) ? dscale : 1./dscale;
2464 if( Axis == V3d_X ) Sx = dscale;
2465 if( Axis == V3d_Y ) Sy = dscale;
2466 if( Axis == V3d_Z ) Sz = dscale;
2467 SetAxialScale( Sx, Sy, Sz );
2471 //=============================================================================
2474 //=============================================================================
2475 void V3d_View::FitAll(const Standard_Real theXmin,
2476 const Standard_Real theYmin,
2477 const Standard_Real theXmax,
2478 const Standard_Real theYmax)
2480 Handle(Graphic3d_Camera) aCamera = Camera();
2481 Standard_Real anAspect = aCamera->Aspect();
2483 Standard_Real aFitSizeU = Abs (theXmax - theXmin);
2484 Standard_Real aFitSizeV = Abs (theYmax - theYmin);
2485 Standard_Real aFitAspect = aFitSizeU / aFitSizeV;
2486 if (aFitAspect >= anAspect)
2488 aFitSizeV = aFitSizeU / anAspect;
2492 aFitSizeU = aFitSizeV * anAspect;
2495 Translate (aCamera, (theXmin + theXmax) * 0.5, (theYmin + theYmax) * 0.5);
2496 Scale (aCamera, aFitSizeU, aFitSizeV);
2501 //=============================================================================
2502 //function : StartRotation
2504 //=============================================================================
2505 void V3d_View::StartRotation(const Standard_Integer X,
2506 const Standard_Integer Y,
2507 const Standard_Real zRotationThreshold)
2512 rx = Standard_Real(Convert(x));
2513 ry = Standard_Real(Convert(y));
2514 myRotateGravity = GravityPoint();
2515 Rotate (0.0, 0.0, 0.0,
2516 myRotateGravity.X(), myRotateGravity.Y(), myRotateGravity.Z(),
2518 myZRotation = Standard_False;
2519 if( zRotationThreshold > 0. ) {
2520 Standard_Real dx = Abs(sx - rx/2.);
2521 Standard_Real dy = Abs(sy - ry/2.);
2522 // if( dx > rx/3. || dy > ry/3. ) myZRotation = Standard_True;
2523 Standard_Real dd = zRotationThreshold * (rx + ry)/2.;
2524 if( dx > dd || dy > dd ) myZRotation = Standard_True;
2529 //=============================================================================
2530 //function : Rotation
2532 //=============================================================================
2533 void V3d_View::Rotation(const Standard_Integer X,
2534 const Standard_Integer Y)
2536 if( rx == 0. || ry == 0. ) {
2540 Standard_Real dx=0.,dy=0.,dz=0.;
2542 dz = atan2(Standard_Real(X)-rx/2., ry/2.-Standard_Real(Y)) -
2543 atan2(sx-rx/2.,ry/2.-sy);
2545 dx = (Standard_Real(X) - sx) * M_PI / rx;
2546 dy = (sy - Standard_Real(Y)) * M_PI / ry;
2550 myRotateGravity.X(), myRotateGravity.Y(), myRotateGravity.Z(),
2554 //=============================================================================
2555 //function : SetComputedMode
2557 //=============================================================================
2558 void V3d_View::SetComputedMode (const Standard_Boolean theMode)
2564 myView->SetComputedMode (Standard_True);
2569 myView->SetComputedMode (Standard_False);
2573 //=============================================================================
2574 //function : ComputedMode
2576 //=============================================================================
2577 Standard_Boolean V3d_View::ComputedMode() const
2579 return myView->ComputedMode();
2582 //=============================================================================
2583 //function : SetBackFacingModel
2585 //=============================================================================
2586 void V3d_View::SetBackFacingModel (const V3d_TypeOfBackfacingModel theModel)
2588 myView->SetBackfacingModel (static_cast<Graphic3d_TypeOfBackfacingModel> (theModel));
2592 //=============================================================================
2593 //function : BackFacingModel
2595 //=============================================================================
2596 V3d_TypeOfBackfacingModel V3d_View::BackFacingModel() const
2598 return static_cast<V3d_TypeOfBackfacingModel> (myView->BackfacingModel());
2601 //=============================================================================
2604 //=============================================================================
2605 void V3d_View::Init()
2607 myComputedMode = MyViewer->ComputedMode();
2608 if (!myComputedMode || !MyViewer->DefaultComputedMode())
2610 SetComputedMode (Standard_False);
2614 //=============================================================================
2617 //=============================================================================
2618 Standard_Boolean V3d_View::Dump (const Standard_CString theFile,
2619 const Graphic3d_BufferType& theBufferType)
2621 Standard_Integer aWinWidth, aWinHeight;
2622 MyWindow->Size (aWinWidth, aWinHeight);
2623 Image_AlienPixMap anImage;
2625 return ToPixMap (anImage, aWinWidth, aWinHeight, theBufferType) && anImage.Save (theFile);
2628 //=============================================================================
2629 //function : ToPixMap
2631 //=============================================================================
2632 Standard_Boolean V3d_View::ToPixMap (Image_PixMap& theImage,
2633 const V3d_ImageDumpOptions& theParams)
2635 Graphic3d_Vec2i aTargetSize (theParams.Width, theParams.Height);
2636 if (aTargetSize.x() != 0
2637 && aTargetSize.y() != 0)
2639 // allocate image buffer for dumping
2640 if (theImage.IsEmpty()
2641 || theImage.SizeX() != Standard_Size(aTargetSize.x())
2642 || theImage.SizeY() != Standard_Size(aTargetSize.y()))
2644 Image_Format aFormat = Image_Format_UNKNOWN;
2645 switch (theParams.BufferType)
2647 case Graphic3d_BT_RGB: aFormat = Image_Format_RGB; break;
2648 case Graphic3d_BT_RGBA: aFormat = Image_Format_RGBA; break;
2649 case Graphic3d_BT_Depth: aFormat = Image_Format_GrayF; break;
2650 case Graphic3d_BT_RGB_RayTraceHdrLeft: aFormat = Image_Format_RGBF; break;
2651 case Graphic3d_BT_Red: aFormat = Image_Format_Gray; break;
2654 if (!theImage.InitZero (aFormat, Standard_Size(aTargetSize.x()), Standard_Size(aTargetSize.y())))
2656 Message::SendFail (TCollection_AsciiString ("Fail to allocate an image ") + aTargetSize.x() + "x" + aTargetSize.y() + " for view dump");
2657 return Standard_False;
2661 if (theImage.IsEmpty())
2663 Message::SendFail ("V3d_View::ToPixMap() has been called without image dimensions");
2664 return Standard_False;
2666 aTargetSize.x() = (Standard_Integer )theImage.SizeX();
2667 aTargetSize.y() = (Standard_Integer )theImage.SizeY();
2669 Handle(Standard_Transient) aFBOPtr;
2670 Handle(Standard_Transient) aPrevFBOPtr = myView->FBO();
2671 Graphic3d_Vec2i aFBOVPSize = aTargetSize;
2673 bool isTiling = false;
2674 if (theParams.TileSize > 0)
2676 if (aFBOVPSize.x() > theParams.TileSize
2677 || aFBOVPSize.y() > theParams.TileSize)
2679 aFBOVPSize.x() = Min (aFBOVPSize.x(), theParams.TileSize);
2680 aFBOVPSize.y() = Min (aFBOVPSize.y(), theParams.TileSize);
2685 Graphic3d_Vec2i aPrevFBOVPSize;
2686 if (!aPrevFBOPtr.IsNull())
2688 Graphic3d_Vec2i aPrevFBOSizeMax;
2689 myView->FBOGetDimensions (aPrevFBOPtr,
2690 aPrevFBOVPSize.x(), aPrevFBOVPSize.y(),
2691 aPrevFBOSizeMax.x(), aPrevFBOSizeMax.y());
2692 if (aFBOVPSize.x() <= aPrevFBOSizeMax.x()
2693 && aFBOVPSize.y() <= aPrevFBOSizeMax.y())
2695 aFBOPtr = aPrevFBOPtr;
2699 if (aFBOPtr.IsNull())
2701 Standard_Integer aMaxTexSizeX = MyViewer->Driver()->InquireLimit (Graphic3d_TypeOfLimit_MaxViewDumpSizeX);
2702 Standard_Integer aMaxTexSizeY = MyViewer->Driver()->InquireLimit (Graphic3d_TypeOfLimit_MaxViewDumpSizeY);
2703 if (theParams.TileSize > aMaxTexSizeX
2704 || theParams.TileSize > aMaxTexSizeY)
2706 Message::SendFail (TCollection_AsciiString ("Image dump can not be performed - specified tile size (")
2707 + theParams.TileSize + ") exceeds hardware limits (" + aMaxTexSizeX + "x" + aMaxTexSizeY + ")");
2708 return Standard_False;
2711 if (aFBOVPSize.x() > aMaxTexSizeX
2712 || aFBOVPSize.y() > aMaxTexSizeY)
2714 if (MyViewer->Driver()->InquireLimit (Graphic3d_TypeOfLimit_IsWorkaroundFBO))
2716 Message::SendWarning ("Warning, workaround for Intel driver problem with empty FBO for images with big width is applied");
2718 Message::SendInfo (TCollection_AsciiString ("Info, tiling image dump is used, image size (")
2719 + aFBOVPSize.x() + "x" + aFBOVPSize.y() + ") exceeds hardware limits (" + aMaxTexSizeX + "x" + aMaxTexSizeY + ")");
2720 aFBOVPSize.x() = Min (aFBOVPSize.x(), aMaxTexSizeX);
2721 aFBOVPSize.y() = Min (aFBOVPSize.y(), aMaxTexSizeY);
2725 // Try to create hardware accelerated buffer
2726 aFBOPtr = myView->FBOCreate (aFBOVPSize.x(), aFBOVPSize.y());
2728 myView->SetFBO (aFBOPtr);
2730 if (aFBOPtr.IsNull())
2732 // try to use on-screen buffer
2733 Graphic3d_Vec2i aWinSize;
2734 MyWindow->Size (aWinSize.x(), aWinSize.y());
2735 if (aFBOVPSize.x() != aWinSize.x()
2736 || aFBOVPSize.y() != aWinSize.y())
2740 aFBOVPSize = aWinSize;
2742 Message::SendWarning ("Warning, on screen buffer is used for image dump - content might be invalid");
2745 // backup camera parameters
2746 Handle(Graphic3d_Camera) aStoreMapping = new Graphic3d_Camera();
2747 Handle(Graphic3d_Camera) aCamera = Camera();
2748 aStoreMapping->Copy (aCamera);
2749 if (aCamera->IsStereo())
2751 switch (theParams.StereoOptions)
2755 aCamera->SetProjectionType (Graphic3d_Camera::Projection_Perspective);
2758 case V3d_SDO_LEFT_EYE:
2760 aCamera->SetProjectionType (Graphic3d_Camera::Projection_MonoLeftEye);
2763 case V3d_SDO_RIGHT_EYE:
2765 aCamera->SetProjectionType (Graphic3d_Camera::Projection_MonoRightEye);
2768 case V3d_SDO_BLENDED:
2770 break; // dump as is
2774 if (theParams.ToAdjustAspect)
2776 aCamera->SetAspect (Standard_Real(aTargetSize.x()) / Standard_Real(aTargetSize.y()));
2779 // render immediate structures into back buffer rather than front
2780 const Standard_Boolean aPrevImmediateMode = myView->SetImmediateModeDrawToFront (Standard_False);
2782 Standard_Boolean isSuccess = Standard_True;
2785 if (!aFBOPtr.IsNull())
2787 myView->FBOChangeViewport (aFBOPtr, aTargetSize.x(), aTargetSize.y());
2790 isSuccess = isSuccess && myView->BufferDump (theImage, theParams.BufferType);
2794 Image_PixMap aTilePixMap;
2795 aTilePixMap.SetTopDown (theImage.IsTopDown());
2797 Graphic3d_Vec2i anOffset (0, 0);
2798 for (; anOffset.y() < aTargetSize.y(); anOffset.y() += aFBOVPSize.y())
2801 for (; anOffset.x() < aTargetSize.x(); anOffset.x() += aFBOVPSize.x())
2803 Graphic3d_CameraTile aTileUncropped;
2804 aTileUncropped.Offset = anOffset;
2805 aTileUncropped.TotalSize = aTargetSize;
2806 aTileUncropped.TileSize = aFBOVPSize;
2807 const Graphic3d_CameraTile aTile = aTileUncropped.Cropped();
2808 if (aTile.TileSize.x() < 1
2809 || aTile.TileSize.y() < 1)
2814 const Standard_Integer aLeft = aTile.Offset.x();
2815 Standard_Integer aBottom = aTile.Offset.y();
2816 if (theImage.IsTopDown())
2818 const Standard_Integer aTop = aTile.Offset.y() + aTile.TileSize.y();
2819 aBottom = aTargetSize.y() - aTop;
2821 aTilePixMap.InitWrapper (theImage.Format(), theImage.ChangeData()
2822 + theImage.SizeRowBytes() * aBottom + theImage.SizePixelBytes() * aLeft,
2823 aTile.TileSize.x(), aTile.TileSize.y(),
2824 theImage.SizeRowBytes());
2826 if (!aFBOPtr.IsNull())
2828 aCamera->SetTile (aTile);
2829 myView->FBOChangeViewport (aFBOPtr, aTile.TileSize.x(), aTile.TileSize.y());
2833 // no API to resize viewport of on-screen buffer - render uncropped
2834 aCamera->SetTile (aTileUncropped);
2837 isSuccess = isSuccess && myView->BufferDump (aTilePixMap, theParams.BufferType);
2851 myView->SetImmediateModeDrawToFront (aPrevImmediateMode);
2852 aCamera->Copy (aStoreMapping);
2853 if (aFBOPtr != aPrevFBOPtr)
2855 myView->FBORelease (aFBOPtr);
2857 else if (!aPrevFBOPtr.IsNull())
2859 myView->FBOChangeViewport (aPrevFBOPtr, aPrevFBOVPSize.x(), aPrevFBOVPSize.y());
2861 myView->SetFBO (aPrevFBOPtr);
2865 //=============================================================================
2866 //function : ImmediateUpdate
2868 //=============================================================================
2869 void V3d_View::ImmediateUpdate() const
2871 if (myImmediateUpdate)
2877 //=============================================================================
2878 //function : SetImmediateUpdate
2880 //=============================================================================
2881 Standard_Boolean V3d_View::SetImmediateUpdate (const Standard_Boolean theImmediateUpdate)
2883 Standard_Boolean aPreviousMode = myImmediateUpdate;
2884 myImmediateUpdate = theImmediateUpdate;
2885 return aPreviousMode;
2888 // =======================================================================
2889 // function : SetCamera
2891 // =======================================================================
2892 void V3d_View::SetCamera (const Handle(Graphic3d_Camera)& theCamera)
2894 myView->SetCamera (theCamera);
2899 // =======================================================================
2900 // function : GetCamera
2902 // =======================================================================
2903 const Handle(Graphic3d_Camera)& V3d_View::Camera() const
2905 return myView->Camera();
2908 // =======================================================================
2909 // function : FitMinMax
2910 // purpose : Internal
2911 // =======================================================================
2912 Standard_Boolean V3d_View::FitMinMax (const Handle(Graphic3d_Camera)& theCamera,
2913 const Bnd_Box& theBox,
2914 const Standard_Real theMargin,
2915 const Standard_Real theResolution,
2916 const Standard_Boolean theToEnlargeIfLine) const
2918 // Check bounding box for validness
2919 if (theBox.IsVoid())
2921 return Standard_False; // bounding box is out of bounds...
2924 // Apply "axial scaling" to the bounding points.
2925 // It is not the best approach to make this scaling as a part of fit all operation,
2926 // but the axial scale is integrated into camera orientation matrix and the other
2927 // option is to perform frustum plane adjustment algorithm in view camera space,
2928 // which will lead to a number of additional world-view space conversions and
2929 // loosing precision as well.
2930 gp_Pnt aBndMin = theBox.CornerMin().XYZ().Multiplied (theCamera->AxialScale());
2931 gp_Pnt aBndMax = theBox.CornerMax().XYZ().Multiplied (theCamera->AxialScale());
2933 if (aBndMax.IsEqual (aBndMin, RealEpsilon()))
2935 return Standard_False; // nothing to fit all
2938 // Prepare camera frustum planes.
2939 NCollection_Array1<gp_Pln> aFrustumPlane (1, 6);
2940 theCamera->Frustum (aFrustumPlane.ChangeValue (1),
2941 aFrustumPlane.ChangeValue (2),
2942 aFrustumPlane.ChangeValue (3),
2943 aFrustumPlane.ChangeValue (4),
2944 aFrustumPlane.ChangeValue (5),
2945 aFrustumPlane.ChangeValue (6));
2947 // Prepare camera up, side, direction vectors.
2948 gp_Dir aCamUp = theCamera->OrthogonalizedUp();
2949 gp_Dir aCamDir = theCamera->Direction();
2950 gp_Dir aCamSide = aCamDir ^ aCamUp;
2952 // Prepare scene bounding box parameters.
2953 gp_Pnt aBndCenter = (aBndMin.XYZ() + aBndMax.XYZ()) / 2.0;
2955 NCollection_Array1<gp_Pnt> aBndCorner (1, 8);
2956 aBndCorner.ChangeValue (1) = gp_Pnt (aBndMin.X(), aBndMin.Y(), aBndMin.Z());
2957 aBndCorner.ChangeValue (2) = gp_Pnt (aBndMin.X(), aBndMin.Y(), aBndMax.Z());
2958 aBndCorner.ChangeValue (3) = gp_Pnt (aBndMin.X(), aBndMax.Y(), aBndMin.Z());
2959 aBndCorner.ChangeValue (4) = gp_Pnt (aBndMin.X(), aBndMax.Y(), aBndMax.Z());
2960 aBndCorner.ChangeValue (5) = gp_Pnt (aBndMax.X(), aBndMin.Y(), aBndMin.Z());
2961 aBndCorner.ChangeValue (6) = gp_Pnt (aBndMax.X(), aBndMin.Y(), aBndMax.Z());
2962 aBndCorner.ChangeValue (7) = gp_Pnt (aBndMax.X(), aBndMax.Y(), aBndMin.Z());
2963 aBndCorner.ChangeValue (8) = gp_Pnt (aBndMax.X(), aBndMax.Y(), aBndMax.Z());
2965 // Perspective-correct camera projection vector, matching the bounding box is determined geometrically.
2966 // Knowing the initial shape of a frustum it is possible to match it to a bounding box.
2967 // Then, knowing the relation of camera projection vector to the frustum shape it is possible to
2968 // set up perspective-correct camera projection matching the bounding box.
2969 // These steps support non-asymmetric transformations of view-projection space provided by camera.
2970 // The zooming can be done by calculating view plane size matching the bounding box at center of
2971 // the bounding box. The only limitation here is that the scale of camera should define size of
2972 // its view plane passing through the camera center, and the center of camera should be on the
2973 // same line with the center of bounding box.
2975 // The following method is applied:
2976 // 1) Determine normalized asymmetry of camera projection vector by frustum planes.
2977 // 2) Determine new location of frustum planes, "matching" the bounding box.
2978 // 3) Determine new camera projection vector using the normalized asymmetry.
2979 // 4) Determine new zooming in view space.
2981 // 1. Determine normalized projection asymmetry (if any).
2982 Standard_Real anAssymX = Tan (( aCamSide).Angle (aFrustumPlane (1).Axis().Direction()))
2983 - Tan ((-aCamSide).Angle (aFrustumPlane (2).Axis().Direction()));
2984 Standard_Real anAssymY = Tan (( aCamUp) .Angle (aFrustumPlane (3).Axis().Direction()))
2985 - Tan ((-aCamUp) .Angle (aFrustumPlane (4).Axis().Direction()));
2987 // 2. Determine how far should be the frustum planes placed from center
2988 // of bounding box, in order to match the bounding box closely.
2989 NCollection_Array1<Standard_Real> aFitDistance (1, 6);
2990 aFitDistance.ChangeValue (1) = 0.0;
2991 aFitDistance.ChangeValue (2) = 0.0;
2992 aFitDistance.ChangeValue (3) = 0.0;
2993 aFitDistance.ChangeValue (4) = 0.0;
2994 aFitDistance.ChangeValue (5) = 0.0;
2995 aFitDistance.ChangeValue (6) = 0.0;
2997 for (Standard_Integer anI = aFrustumPlane.Lower(); anI <= aFrustumPlane.Upper(); ++anI)
2999 // Measure distances from center of bounding box to its corners towards the frustum plane.
3000 const gp_Dir& aPlaneN = aFrustumPlane.ChangeValue (anI).Axis().Direction();
3002 Standard_Real& aFitDist = aFitDistance.ChangeValue (anI);
3004 for (Standard_Integer aJ = aBndCorner.Lower(); aJ <= aBndCorner.Upper(); ++aJ)
3006 aFitDist = Max (aFitDist, gp_Vec (aBndCenter, aBndCorner (aJ)).Dot (aPlaneN));
3009 // The center of camera is placed on the same line with center of bounding box.
3010 // The view plane section crosses the bounding box at its center.
3011 // To compute view plane size, evaluate coefficients converting "point -> plane distance"
3012 // into view section size between the point and the frustum plane.
3014 // /|\ right half of frame //
3016 // point o<-- distance * coeff -->//---- (view plane section)
3025 aFitDistance.ChangeValue (1) *= Sqrt(1 + Pow (Tan ( aCamSide .Angle (aFrustumPlane (1).Axis().Direction())), 2.0));
3026 aFitDistance.ChangeValue (2) *= Sqrt(1 + Pow (Tan ((-aCamSide).Angle (aFrustumPlane (2).Axis().Direction())), 2.0));
3027 aFitDistance.ChangeValue (3) *= Sqrt(1 + Pow (Tan ( aCamUp .Angle (aFrustumPlane (3).Axis().Direction())), 2.0));
3028 aFitDistance.ChangeValue (4) *= Sqrt(1 + Pow (Tan ((-aCamUp) .Angle (aFrustumPlane (4).Axis().Direction())), 2.0));
3029 aFitDistance.ChangeValue (5) *= Sqrt(1 + Pow (Tan ( aCamDir .Angle (aFrustumPlane (5).Axis().Direction())), 2.0));
3030 aFitDistance.ChangeValue (6) *= Sqrt(1 + Pow (Tan ((-aCamDir) .Angle (aFrustumPlane (6).Axis().Direction())), 2.0));
3032 Standard_Real aViewSizeXv = aFitDistance (1) + aFitDistance (2);
3033 Standard_Real aViewSizeYv = aFitDistance (3) + aFitDistance (4);
3034 Standard_Real aViewSizeZv = aFitDistance (5) + aFitDistance (6);
3036 // 3. Place center of camera on the same line with center of bounding
3037 // box applying corresponding projection asymmetry (if any).
3038 Standard_Real anAssymXv = anAssymX * aViewSizeXv * 0.5;
3039 Standard_Real anAssymYv = anAssymY * aViewSizeYv * 0.5;
3040 Standard_Real anOffsetXv = (aFitDistance (2) - aFitDistance (1)) * 0.5 + anAssymXv;
3041 Standard_Real anOffsetYv = (aFitDistance (4) - aFitDistance (3)) * 0.5 + anAssymYv;
3042 gp_Vec aTranslateSide = gp_Vec (aCamSide) * anOffsetXv;
3043 gp_Vec aTranslateUp = gp_Vec (aCamUp) * anOffsetYv;
3044 gp_Pnt aCamNewCenter = aBndCenter.Translated (aTranslateSide).Translated (aTranslateUp);
3046 gp_Trsf aCenterTrsf;
3047 aCenterTrsf.SetTranslation (theCamera->Center(), aCamNewCenter);
3048 theCamera->Transform (aCenterTrsf);
3049 theCamera->SetDistance (aFitDistance (6) + aFitDistance (5));
3051 // Bounding box collapses to a point or thin line going in depth of the screen
3052 if (aViewSizeXv < theResolution && aViewSizeYv < theResolution)
3054 if (aViewSizeXv < theResolution || !theToEnlargeIfLine)
3056 return Standard_True; // This is just one point or line and zooming has no effect.
3059 // Looking along line and "theToEnlargeIfLine" is requested.
3060 // Fit view to see whole scene on rotation.
3061 aViewSizeXv = aViewSizeZv;
3062 aViewSizeYv = aViewSizeZv;
3065 Scale (theCamera, aViewSizeXv, aViewSizeYv);
3067 const Standard_Real aZoomCoef = myView->ConsiderZoomPersistenceObjects();
3069 Scale (theCamera, theCamera->ViewDimensions().X() * (aZoomCoef + theMargin), theCamera->ViewDimensions().Y() * (aZoomCoef + theMargin));
3071 return Standard_True;
3074 // =======================================================================
3076 // purpose : Internal
3077 // =======================================================================
3078 void V3d_View::Scale (const Handle(Graphic3d_Camera)& theCamera,
3079 const Standard_Real theSizeXv,
3080 const Standard_Real theSizeYv) const
3082 Standard_Real anAspect = theCamera->Aspect();
3085 theCamera->SetScale (Max (theSizeXv / anAspect, theSizeYv));
3089 theCamera->SetScale (Max (theSizeXv, theSizeYv * anAspect));
3093 // =======================================================================
3094 // function : Translate
3095 // purpose : Internal
3096 // =======================================================================
3097 void V3d_View::Translate (const Handle(Graphic3d_Camera)& theCamera,
3098 const Standard_Real theDXv,
3099 const Standard_Real theDYv) const
3101 const gp_Pnt& aCenter = theCamera->Center();
3102 const gp_Dir& aDir = theCamera->Direction();
3103 const gp_Dir& anUp = theCamera->Up();
3104 gp_Ax3 aCameraCS (aCenter, aDir.Reversed(), aDir ^ anUp);
3106 gp_Vec aCameraPanXv = gp_Vec (aCameraCS.XDirection()) * theDXv;
3107 gp_Vec aCameraPanYv = gp_Vec (aCameraCS.YDirection()) * theDYv;
3108 gp_Vec aCameraPan = aCameraPanXv + aCameraPanYv;
3110 aPanTrsf.SetTranslation (aCameraPan);
3112 theCamera->Transform (aPanTrsf);
3115 // =======================================================================
3116 // function : DiagnosticInformation
3118 // =======================================================================
3119 void V3d_View::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
3120 Graphic3d_DiagnosticInfo theFlags) const
3122 myView->DiagnosticInformation (theDict, theFlags);
3125 //=======================================================================
3126 //function : StatisticInformation
3128 //=======================================================================
3129 void V3d_View::StatisticInformation (TColStd_IndexedDataMapOfStringString& theDict) const
3131 myView->StatisticInformation (theDict);
3134 // =======================================================================
3135 // function : StatisticInformation
3137 // =======================================================================
3138 TCollection_AsciiString V3d_View::StatisticInformation() const
3140 return myView->StatisticInformation();
3143 //=============================================================================
3144 //function : RenderingParams
3146 //=============================================================================
3147 const Graphic3d_RenderingParams& V3d_View::RenderingParams() const
3149 return myView->RenderingParams();
3152 //=============================================================================
3153 //function : ChangeRenderingParams
3155 //=============================================================================
3156 Graphic3d_RenderingParams& V3d_View::ChangeRenderingParams()
3158 return myView->ChangeRenderingParams();