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;
239 //=============================================================================
242 //=============================================================================
243 void V3d_View::Redraw() const
245 if (!myView->IsDefined()
246 || !myView->IsActive())
251 myIsInvalidatedImmediate = Standard_False;
252 Handle(Graphic3d_StructureManager) aStructureMgr = MyViewer->StructureManager();
253 for (Standard_Integer aRetryIter = 0; aRetryIter < 2; ++aRetryIter)
255 if (aStructureMgr->IsDeviceLost())
257 aStructureMgr->RecomputeStructures();
264 if (!aStructureMgr->IsDeviceLost())
271 //=============================================================================
272 //function : RedrawImmediate
274 //=============================================================================
275 void V3d_View::RedrawImmediate() const
277 if (!myView->IsDefined()
278 || !myView->IsActive())
283 myIsInvalidatedImmediate = Standard_False;
284 myView->RedrawImmediate();
287 //=============================================================================
288 //function : Invalidate
290 //=============================================================================
291 void V3d_View::Invalidate() const
293 if (!myView->IsDefined())
298 myView->Invalidate();
301 //=============================================================================
302 //function : IsInvalidated
304 //=============================================================================
305 Standard_Boolean V3d_View::IsInvalidated() const
307 return !myView->IsDefined()
308 || myView->IsInvalidated();
311 // ========================================================================
312 // function : SetAutoZFitMode
314 // ========================================================================
315 void V3d_View::SetAutoZFitMode (const Standard_Boolean theIsOn,
316 const Standard_Real theScaleFactor)
318 Standard_ASSERT_RAISE (theScaleFactor > 0.0, "Zero or negative scale factor is not allowed.");
319 myAutoZFitScaleFactor = theScaleFactor;
320 myAutoZFitIsOn = theIsOn;
323 //=============================================================================
324 //function : AutoZFit
326 //=============================================================================
327 void V3d_View::AutoZFit() const
334 ZFitAll (myAutoZFitScaleFactor);
337 //=============================================================================
340 //=============================================================================
341 void V3d_View::ZFitAll (const Standard_Real theScaleFactor) const
343 Bnd_Box aMinMaxBox = myView->MinMaxValues (Standard_False); // applicative min max boundaries
344 Bnd_Box aGraphicBox = myView->MinMaxValues (Standard_True); // real graphical boundaries (not accounting infinite flag).
346 myView->Camera()->ZFitAll (theScaleFactor, aMinMaxBox, aGraphicBox);
349 //=============================================================================
352 //=============================================================================
353 Standard_Boolean V3d_View::IsEmpty() const
355 Standard_Boolean TheStatus = Standard_True ;
356 if( myView->IsDefined() ) {
357 Standard_Integer Nstruct = myView->NumberOfDisplayedStructures() ;
358 if( Nstruct > 0 ) TheStatus = Standard_False ;
363 //=============================================================================
364 //function : UpdateLights
366 //=============================================================================
367 void V3d_View::UpdateLights() const
369 Handle(Graphic3d_LightSet) aLights = new Graphic3d_LightSet();
370 for (V3d_ListOfLight::Iterator anActiveLightIter (myActiveLights); anActiveLightIter.More(); anActiveLightIter.Next())
372 aLights->Add (anActiveLightIter.Value());
374 myView->SetLights (aLights);
377 //=============================================================================
378 //function : DoMapping
380 //=============================================================================
381 void V3d_View::DoMapping()
383 if (!myView->IsDefined())
388 myView->Window()->DoMapping();
391 //=============================================================================
392 //function : MustBeResized
394 //=============================================================================
395 void V3d_View::MustBeResized()
397 if (!myView->IsDefined())
405 if (myImmediateUpdate)
411 //=============================================================================
412 //function : SetBackgroundColor
414 //=============================================================================
415 void V3d_View::SetBackgroundColor (const Quantity_TypeOfColor theType,
416 const Standard_Real theV1,
417 const Standard_Real theV2,
418 const Standard_Real theV3)
420 Standard_Real aV1 = Max (Min (theV1, 1.0), 0.0);
421 Standard_Real aV2 = Max (Min (theV2, 1.0), 0.0);
422 Standard_Real aV3 = Max (Min (theV3, 1.0), 0.0);
424 SetBackgroundColor (Quantity_Color (aV1, aV2, aV3, theType));
427 //=============================================================================
428 //function : SetBackgroundColor
430 //=============================================================================
431 void V3d_View::SetBackgroundColor (const Quantity_Color& theColor)
433 myView->SetBackground (Aspect_Background (theColor));
435 if (myImmediateUpdate)
441 //=============================================================================
442 //function : SetBgGradientColors
444 //=============================================================================
445 void V3d_View::SetBgGradientColors (const Quantity_Color& theColor1,
446 const Quantity_Color& theColor2,
447 const Aspect_GradientFillMethod theFillStyle,
448 const Standard_Boolean theToUpdate)
450 Aspect_GradientBackground aGradientBg (theColor1, theColor2, theFillStyle);
452 myView->SetGradientBackground (aGradientBg);
454 if (myImmediateUpdate || theToUpdate)
460 //=============================================================================
461 //function : SetBgGradientStyle
463 //=============================================================================
464 void V3d_View::SetBgGradientStyle (const Aspect_GradientFillMethod theFillStyle, const Standard_Boolean theToUpdate)
466 Quantity_Color aColor1;
467 Quantity_Color aColor2;
468 GradientBackground().Colors (aColor1, aColor2);
470 SetBgGradientColors (aColor1, aColor2, theFillStyle, theToUpdate);
473 //=============================================================================
474 //function : SetBackgroundImage
476 //=============================================================================
477 void V3d_View::SetBackgroundImage (const Standard_CString theFileName,
478 const Aspect_FillMethod theFillStyle,
479 const Standard_Boolean theToUpdate)
481 myView->SetBackgroundImage (theFileName);
482 myView->SetBackgroundImageStyle (theFillStyle);
484 if (myImmediateUpdate || theToUpdate)
490 //=============================================================================
491 //function : SetBgImageStyle
493 //=============================================================================
494 void V3d_View::SetBgImageStyle (const Aspect_FillMethod theFillStyle, const Standard_Boolean theToUpdate)
496 myView->SetBackgroundImageStyle (theFillStyle);
498 if (myImmediateUpdate || theToUpdate)
504 //=============================================================================
507 //=============================================================================
508 void V3d_View::SetAxis (const Standard_Real theX, const Standard_Real theY, const Standard_Real theZ,
509 const Standard_Real theVx, const Standard_Real theVy, const Standard_Real theVz)
511 myDefaultViewPoint.SetCoord (theX, theY, theZ);
512 myDefaultViewAxis.SetCoord (theVx, theVy, theVz);
515 //=============================================================================
516 //function : SetShadingModel
518 //=============================================================================
519 void V3d_View::SetShadingModel (const Graphic3d_TypeOfShadingModel theShadingModel)
521 myView->SetShadingModel (theShadingModel);
524 //=============================================================================
525 //function : SetTextureEnv
527 //=============================================================================
528 void V3d_View::SetTextureEnv (const Handle(Graphic3d_TextureEnv)& theTexture)
530 myView->SetTextureEnv (theTexture);
532 if (myImmediateUpdate)
538 //=============================================================================
539 //function : SetVisualization
541 //=============================================================================
542 void V3d_View::SetVisualization (const V3d_TypeOfVisualization theType)
544 myView->SetVisualizationType (static_cast <Graphic3d_TypeOfVisualization> (theType));
546 if (myImmediateUpdate)
552 //=============================================================================
553 //function : SetFront
555 //=============================================================================
556 void V3d_View::SetFront()
558 gp_Ax3 a = MyViewer->PrivilegedPlane();
559 Standard_Real xo, yo, zo, vx, vy, vz, xu, yu, zu;
561 a.Direction().Coord(vx,vy,vz);
562 a.YDirection().Coord(xu,yu,zu);
563 a.Location().Coord(xo,yo,zo);
565 Handle(Graphic3d_Camera) aCamera = Camera();
567 aCamera->SetCenter (gp_Pnt (xo, yo, zo));
571 aCamera->SetDirection (gp_Dir (vx, vy, vz));
575 aCamera->SetDirection (gp_Dir (vx, vy, vz).Reversed());
578 aCamera->SetUp (gp_Dir (xu, yu, zu));
582 SwitchSetFront = !SwitchSetFront;
587 //=============================================================================
590 //=============================================================================
591 void V3d_View::Rotate (const Standard_Real ax,
592 const Standard_Real ay,
593 const Standard_Real az,
594 const Standard_Boolean Start)
596 Standard_Real Ax = ax;
597 Standard_Real Ay = ay;
598 Standard_Real Az = az;
600 if( Ax > 0. ) while ( Ax > DEUXPI ) Ax -= DEUXPI;
601 else if( Ax < 0. ) while ( Ax < -DEUXPI ) Ax += DEUXPI;
602 if( Ay > 0. ) while ( Ay > DEUXPI ) Ay -= DEUXPI;
603 else if( Ay < 0. ) while ( Ay < -DEUXPI ) Ay += DEUXPI;
604 if( Az > 0. ) while ( Az > DEUXPI ) Az -= DEUXPI;
605 else if( Az < 0. ) while ( Az < -DEUXPI ) Az += DEUXPI;
607 Handle(Graphic3d_Camera) aCamera = Camera();
611 myCamStartOpUp = aCamera->Up();
612 myCamStartOpDir = aCamera->Direction();
613 myCamStartOpEye = aCamera->Eye();
614 myCamStartOpCenter = aCamera->Center();
617 aCamera->SetUp (myCamStartOpUp);
618 aCamera->SetEyeAndCenter (myCamStartOpEye, myCamStartOpCenter);
619 aCamera->SetDirectionFromEye (myCamStartOpDir);
621 // rotate camera around 3 initial axes
622 gp_Dir aBackDir = -myCamStartOpDir;
623 gp_Dir aXAxis (myCamStartOpUp.Crossed (aBackDir));
624 gp_Dir aYAxis (aBackDir.Crossed (aXAxis));
625 gp_Dir aZAxis (aXAxis.Crossed (aYAxis));
627 gp_Trsf aRot[3], aTrsf;
628 aRot[0].SetRotation (gp_Ax1 (myCamStartOpCenter, aYAxis), -Ax);
629 aRot[1].SetRotation (gp_Ax1 (myCamStartOpCenter, aXAxis), Ay);
630 aRot[2].SetRotation (gp_Ax1 (myCamStartOpCenter, aZAxis), Az);
631 aTrsf.Multiply (aRot[0]);
632 aTrsf.Multiply (aRot[1]);
633 aTrsf.Multiply (aRot[2]);
635 aCamera->Transform (aTrsf);
642 //=============================================================================
645 //=============================================================================
646 void V3d_View::Rotate(const Standard_Real ax, const Standard_Real ay, const Standard_Real az,
647 const Standard_Real X, const Standard_Real Y, const Standard_Real Z, const Standard_Boolean Start)
650 Standard_Real Ax = ax ;
651 Standard_Real Ay = ay ;
652 Standard_Real Az = az ;
654 if( Ax > 0. ) while ( Ax > DEUXPI ) Ax -= DEUXPI ;
655 else if( Ax < 0. ) while ( Ax < -DEUXPI ) Ax += DEUXPI ;
656 if( Ay > 0. ) while ( Ay > DEUXPI ) Ay -= DEUXPI ;
657 else if( Ay < 0. ) while ( Ay < -DEUXPI ) Ay += DEUXPI ;
658 if( Az > 0. ) while ( Az > DEUXPI ) Az -= DEUXPI ;
659 else if( Az < 0. ) while ( Az < -DEUXPI ) Az += DEUXPI ;
661 Handle(Graphic3d_Camera) aCamera = Camera();
665 myGravityReferencePoint.SetCoord (X, Y, Z);
666 myCamStartOpUp = aCamera->Up();
667 myCamStartOpDir = aCamera->Direction();
668 myCamStartOpEye = aCamera->Eye();
669 myCamStartOpCenter = aCamera->Center();
672 const Graphic3d_Vertex& aVref = myGravityReferencePoint;
674 aCamera->SetUp (myCamStartOpUp);
675 aCamera->SetEyeAndCenter (myCamStartOpEye, myCamStartOpCenter);
676 aCamera->SetDirectionFromEye (myCamStartOpDir);
678 // rotate camera around 3 initial axes
679 gp_Pnt aRCenter (aVref.X(), aVref.Y(), aVref.Z());
681 gp_Dir aZAxis (aCamera->Direction().Reversed());
682 gp_Dir aYAxis (aCamera->Up());
683 gp_Dir aXAxis (aYAxis.Crossed (aZAxis));
685 gp_Trsf aRot[3], aTrsf;
686 aRot[0].SetRotation (gp_Ax1 (aRCenter, aYAxis), -Ax);
687 aRot[1].SetRotation (gp_Ax1 (aRCenter, aXAxis), Ay);
688 aRot[2].SetRotation (gp_Ax1 (aRCenter, aZAxis), Az);
689 aTrsf.Multiply (aRot[0]);
690 aTrsf.Multiply (aRot[1]);
691 aTrsf.Multiply (aRot[2]);
693 aCamera->Transform (aTrsf);
700 //=============================================================================
703 //=============================================================================
704 void V3d_View::Rotate(const V3d_TypeOfAxe Axe, const Standard_Real angle, const Standard_Boolean Start)
708 Rotate(angle,0.,0.,Start);
711 Rotate(0.,angle,0.,Start);
714 Rotate(0.,0.,angle,Start);
719 //=============================================================================
722 //=============================================================================
723 void V3d_View::Rotate (const V3d_TypeOfAxe theAxe, const Standard_Real theAngle,
724 const Standard_Real theX, const Standard_Real theY, const Standard_Real theZ, const Standard_Boolean theStart)
726 Standard_Real anAngle = theAngle;
728 if (anAngle > 0.0) while (anAngle > DEUXPI) anAngle -= DEUXPI;
729 else if (anAngle < 0.0) while (anAngle < -DEUXPI) anAngle += DEUXPI;
731 Handle(Graphic3d_Camera) aCamera = Camera();
735 myGravityReferencePoint.SetCoord (theX, theY, theZ);
736 myCamStartOpUp = aCamera->Up();
737 myCamStartOpDir = aCamera->Direction();
738 myCamStartOpEye = aCamera->Eye();
739 myCamStartOpCenter = aCamera->Center();
742 case V3d_X: myViewAxis = gp::DX(); break;
743 case V3d_Y: myViewAxis = gp::DY(); break;
744 case V3d_Z: myViewAxis = gp::DZ(); break;
748 const Graphic3d_Vertex& aVref = myGravityReferencePoint;
750 aCamera->SetUp (myCamStartOpUp);
751 aCamera->SetEyeAndCenter (myCamStartOpEye, myCamStartOpCenter);
752 aCamera->SetDirectionFromEye (myCamStartOpDir);
754 // rotate camera around passed axis
756 gp_Pnt aRCenter (aVref.X(), aVref.Y(), aVref.Z());
757 gp_Dir aRAxis ((theAxe == V3d_X) ? 1.0 : 0.0,
758 (theAxe == V3d_Y) ? 1.0 : 0.0,
759 (theAxe == V3d_Z) ? 1.0 : 0.0);
761 aRotation.SetRotation (gp_Ax1 (aRCenter, aRAxis), anAngle);
763 aCamera->Transform (aRotation);
770 //=============================================================================
773 //=============================================================================
774 void V3d_View::Rotate(const Standard_Real angle, const Standard_Boolean Start)
776 Standard_Real Angle = angle;
778 if( Angle > 0. ) while ( Angle > DEUXPI ) Angle -= DEUXPI ;
779 else if( Angle < 0. ) while ( Angle < -DEUXPI ) Angle += DEUXPI ;
781 Handle(Graphic3d_Camera) aCamera = Camera();
785 myCamStartOpUp = aCamera->Up();
786 myCamStartOpDir = aCamera->Direction();
787 myCamStartOpEye = aCamera->Eye();
788 myCamStartOpCenter = aCamera->Center();
791 aCamera->SetUp (myCamStartOpUp);
792 aCamera->SetEyeAndCenter (myCamStartOpEye, myCamStartOpCenter);
793 aCamera->SetDirectionFromEye (myCamStartOpDir);
796 gp_Pnt aRCenter (myDefaultViewPoint);
797 gp_Dir aRAxis (myDefaultViewAxis);
798 aRotation.SetRotation (gp_Ax1 (aRCenter, aRAxis), Angle);
800 aCamera->Transform (aRotation);
807 //=============================================================================
810 //=============================================================================
811 void V3d_View::Turn(const Standard_Real ax, const Standard_Real ay, const Standard_Real az, const Standard_Boolean Start)
813 Standard_Real Ax = ax;
814 Standard_Real Ay = ay;
815 Standard_Real Az = az;
817 if( Ax > 0. ) while ( Ax > DEUXPI ) Ax -= DEUXPI ;
818 else if( Ax < 0. ) while ( Ax < -DEUXPI ) Ax += DEUXPI ;
819 if( Ay > 0. ) while ( Ay > DEUXPI ) Ay -= DEUXPI ;
820 else if( Ay < 0. ) while ( Ay < -DEUXPI ) Ay += DEUXPI ;
821 if( Az > 0. ) while ( Az > DEUXPI ) Az -= DEUXPI ;
822 else if( Az < 0. ) while ( Az < -DEUXPI ) Az += DEUXPI ;
824 Handle(Graphic3d_Camera) aCamera = Camera();
828 myCamStartOpUp = aCamera->Up();
829 myCamStartOpDir = aCamera->Direction();
830 myCamStartOpEye = aCamera->Eye();
831 myCamStartOpCenter = aCamera->Center();
834 aCamera->SetUp (myCamStartOpUp);
835 aCamera->SetEyeAndCenter (myCamStartOpEye, myCamStartOpCenter);
836 aCamera->SetDirectionFromEye (myCamStartOpDir);
838 // rotate camera around 3 initial axes
839 gp_Pnt aRCenter = aCamera->Eye();
840 gp_Dir aZAxis (aCamera->Direction().Reversed());
841 gp_Dir aYAxis (aCamera->Up());
842 gp_Dir aXAxis (aYAxis.Crossed (aZAxis));
844 gp_Trsf aRot[3], aTrsf;
845 aRot[0].SetRotation (gp_Ax1 (aRCenter, aYAxis), -Ax);
846 aRot[1].SetRotation (gp_Ax1 (aRCenter, aXAxis), Ay);
847 aRot[2].SetRotation (gp_Ax1 (aRCenter, aZAxis), Az);
848 aTrsf.Multiply (aRot[0]);
849 aTrsf.Multiply (aRot[1]);
850 aTrsf.Multiply (aRot[2]);
852 aCamera->Transform (aTrsf);
859 //=============================================================================
862 //=============================================================================
863 void V3d_View::Turn(const V3d_TypeOfAxe Axe, const Standard_Real angle, const Standard_Boolean Start)
867 Turn(angle,0.,0.,Start);
870 Turn(0.,angle,0.,Start);
873 Turn(0.,0.,angle,Start);
878 //=============================================================================
881 //=============================================================================
882 void V3d_View::Turn(const Standard_Real angle, const Standard_Boolean Start)
884 Standard_Real Angle = angle ;
886 if( Angle > 0. ) while ( Angle > DEUXPI ) Angle -= DEUXPI ;
887 else if( Angle < 0. ) while ( Angle < -DEUXPI ) Angle += DEUXPI ;
889 Handle(Graphic3d_Camera) aCamera = Camera();
893 myCamStartOpUp = aCamera->Up();
894 myCamStartOpDir = aCamera->Direction();
895 myCamStartOpEye = aCamera->Eye();
896 myCamStartOpCenter = aCamera->Center();
899 aCamera->SetUp (myCamStartOpUp);
900 aCamera->SetEyeAndCenter (myCamStartOpEye, myCamStartOpCenter);
901 aCamera->SetDirectionFromEye (myCamStartOpDir);
904 gp_Pnt aRCenter = aCamera->Eye();
905 gp_Dir aRAxis (myDefaultViewAxis);
906 aRotation.SetRotation (gp_Ax1 (aRCenter, aRAxis), Angle);
908 aCamera->Transform (aRotation);
915 //=============================================================================
916 //function : SetTwist
918 //=============================================================================
919 void V3d_View::SetTwist(const Standard_Real angle)
921 Standard_Real Angle = angle ;
923 if( Angle > 0. ) while ( Angle > DEUXPI ) Angle -= DEUXPI ;
924 else if( Angle < 0. ) while ( Angle < -DEUXPI ) Angle += DEUXPI ;
926 Handle(Graphic3d_Camera) aCamera = Camera();
928 const gp_Dir aReferencePlane (aCamera->Direction().Reversed());
929 if (!screenAxis (aReferencePlane, gp::DZ(), myXscreenAxis, myYscreenAxis, myZscreenAxis)
930 && !screenAxis (aReferencePlane, gp::DY(), myXscreenAxis, myYscreenAxis, myZscreenAxis)
931 && !screenAxis (aReferencePlane, gp::DZ(), myXscreenAxis, myYscreenAxis, myZscreenAxis))
933 throw V3d_BadValue ("V3d_ViewSetTwist, alignment of Eye,At,Up,");
936 gp_Pnt aRCenter = aCamera->Center();
937 gp_Dir aZAxis (aCamera->Direction().Reversed());
940 aTrsf.SetRotation (gp_Ax1 (aRCenter, aZAxis), Angle);
942 aCamera->SetUp (gp_Dir (myYscreenAxis));
943 aCamera->Transform (aTrsf);
950 //=============================================================================
953 //=============================================================================
954 void V3d_View::SetEye(const Standard_Real X,const Standard_Real Y,const Standard_Real Z)
956 Standard_Real aTwistBefore = Twist();
958 Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
960 Handle(Graphic3d_Camera) aCamera = Camera();
962 aCamera->SetEye (gp_Pnt (X, Y, Z));
964 SetTwist (aTwistBefore);
968 SetImmediateUpdate (wasUpdateEnabled);
973 //=============================================================================
974 //function : SetDepth
976 //=============================================================================
977 void V3d_View::SetDepth(const Standard_Real Depth)
979 V3d_BadValue_Raise_if (Depth == 0. ,"V3d_View::SetDepth, bad depth");
981 Handle(Graphic3d_Camera) aCamera = Camera();
985 // Move eye using center (target) as anchor.
986 aCamera->SetDistance (Depth);
990 // Move the view ref point instead of the eye.
991 gp_Vec aDir (aCamera->Direction());
992 gp_Pnt aCameraEye = aCamera->Eye();
993 gp_Pnt aCameraCenter = aCameraEye.Translated (aDir.Multiplied (Abs (Depth)));
995 aCamera->SetCenter (aCameraCenter);
1003 //=============================================================================
1004 //function : SetProj
1006 //=============================================================================
1007 void V3d_View::SetProj( const Standard_Real Vx,const Standard_Real Vy, const Standard_Real Vz )
1009 V3d_BadValue_Raise_if( Sqrt(Vx*Vx + Vy*Vy + Vz*Vz) <= 0.,
1010 "V3d_View::SetProj, null projection vector");
1012 Standard_Real aTwistBefore = Twist();
1014 Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
1016 Camera()->SetDirection (gp_Dir (Vx, Vy, Vz).Reversed());
1018 SetTwist(aTwistBefore);
1022 SetImmediateUpdate (wasUpdateEnabled);
1027 //=============================================================================
1028 //function : SetProj
1030 //=============================================================================
1031 void V3d_View::SetProj (const V3d_TypeOfOrientation theOrientation,
1032 const Standard_Boolean theIsYup)
1034 Graphic3d_Vec3d anUp = theIsYup ? Graphic3d_Vec3d (0.0, 1.0, 0.0) : Graphic3d_Vec3d (0.0, 0.0, 1.0);
1037 if (theOrientation == V3d_Ypos
1038 || theOrientation == V3d_Yneg)
1040 anUp.SetValues (0.0, 0.0, -1.0);
1045 if (theOrientation == V3d_Zpos)
1047 anUp.SetValues (0.0, 1.0, 0.0);
1049 else if (theOrientation == V3d_Zneg)
1051 anUp.SetValues (0.0, -1.0, 0.0);
1055 const gp_Dir aBck = V3d::GetProjAxis (theOrientation);
1057 // retain camera panning from origin when switching projection
1058 const Handle(Graphic3d_Camera)& aCamera = Camera();
1059 const gp_Pnt anOriginVCS = aCamera->ConvertWorld2View (gp::Origin());
1061 const Standard_Real aNewDist = aCamera->Eye().Distance (gp_Pnt (0, 0, 0));
1062 aCamera->SetEyeAndCenter (gp_XYZ (0, 0, 0) + aBck.XYZ() * aNewDist,
1064 aCamera->SetDirectionFromEye (-aBck);
1065 aCamera->SetUp (gp_Dir (anUp.x(), anUp.y(), anUp.z()));
1066 aCamera->OrthogonalizeUp();
1068 Panning (anOriginVCS.X(), anOriginVCS.Y());
1075 //=============================================================================
1078 //=============================================================================
1079 void V3d_View::SetAt(const Standard_Real X,const Standard_Real Y,const Standard_Real Z)
1081 Standard_Real aTwistBefore = Twist();
1083 Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
1085 Camera()->SetCenter (gp_Pnt (X, Y, Z));
1087 SetTwist (aTwistBefore);
1091 SetImmediateUpdate (wasUpdateEnabled);
1096 //=============================================================================
1099 //=============================================================================
1100 void V3d_View::SetUp (const Standard_Real theVx, const Standard_Real theVy, const Standard_Real theVz)
1102 Handle(Graphic3d_Camera) aCamera = Camera();
1104 const gp_Dir aReferencePlane (aCamera->Direction().Reversed());
1105 const gp_Dir anUp (theVx, theVy, theVz);
1106 if (!screenAxis (aReferencePlane, anUp, myXscreenAxis, myYscreenAxis, myZscreenAxis)
1107 && !screenAxis (aReferencePlane, gp::DZ(), myXscreenAxis, myYscreenAxis, myZscreenAxis)
1108 && !screenAxis (aReferencePlane, gp::DY(), myXscreenAxis, myYscreenAxis, myZscreenAxis)
1109 && !screenAxis (aReferencePlane, gp::DX(), myXscreenAxis, myYscreenAxis, myZscreenAxis))
1111 throw V3d_BadValue ("V3d_View::Setup, alignment of Eye,At,Up");
1114 aCamera->SetUp (gp_Dir (myYscreenAxis));
1121 //=============================================================================
1124 //=============================================================================
1125 void V3d_View::SetUp (const V3d_TypeOfOrientation theOrientation)
1127 Handle(Graphic3d_Camera) aCamera = Camera();
1129 const gp_Dir aReferencePlane (aCamera->Direction().Reversed());
1130 const gp_Dir anUp = V3d::GetProjAxis (theOrientation);
1131 if (!screenAxis (aReferencePlane, anUp, myXscreenAxis, myYscreenAxis, myZscreenAxis)
1132 && !screenAxis (aReferencePlane, gp::DZ(), myXscreenAxis, myYscreenAxis, myZscreenAxis)
1133 && !screenAxis (aReferencePlane, gp::DY(), myXscreenAxis, myYscreenAxis, myZscreenAxis)
1134 && !screenAxis (aReferencePlane, gp::DX(), myXscreenAxis, myYscreenAxis, myZscreenAxis))
1136 throw V3d_BadValue ("V3d_View::SetUp, alignment of Eye,At,Up");
1139 aCamera->SetUp (gp_Dir (myYscreenAxis));
1146 //=============================================================================
1147 //function : SetViewOrientationDefault
1149 //=============================================================================
1150 void V3d_View::SetViewOrientationDefault()
1152 myDefaultCamera->CopyOrientationData (Camera());
1155 //=======================================================================
1156 //function : SetViewMappingDefault
1158 //=======================================================================
1159 void V3d_View::SetViewMappingDefault()
1161 myDefaultCamera->CopyMappingData (Camera());
1164 //=============================================================================
1165 //function : ResetViewOrientation
1167 //=============================================================================
1168 void V3d_View::ResetViewOrientation()
1170 Camera()->CopyOrientationData (myDefaultCamera);
1177 //=======================================================================
1178 //function : ResetViewMapping
1180 //=======================================================================
1181 void V3d_View::ResetViewMapping()
1183 Camera()->CopyMappingData (myDefaultCamera);
1190 //=============================================================================
1193 //=============================================================================
1194 void V3d_View::Reset (const Standard_Boolean theToUpdate)
1196 Camera()->Copy (myDefaultCamera);
1200 SwitchSetFront = Standard_False;
1202 if (myImmediateUpdate || theToUpdate)
1208 //=======================================================================
1209 //function : SetCenter
1211 //=======================================================================
1212 void V3d_View::SetCenter (const Standard_Integer theXp,
1213 const Standard_Integer theYp)
1215 Standard_Real aXv, aYv;
1216 Convert (theXp, theYp, aXv, aYv);
1217 Translate (Camera(), aXv, aYv);
1222 //=============================================================================
1223 //function : SetSize
1225 //=============================================================================
1226 void V3d_View::SetSize (const Standard_Real theSize)
1228 V3d_BadValue_Raise_if (theSize <= 0.0, "V3d_View::SetSize, Window Size is NULL");
1230 Handle(Graphic3d_Camera) aCamera = Camera();
1232 aCamera->SetScale (aCamera->Aspect() >= 1.0 ? theSize / aCamera->Aspect() : theSize);
1239 //=============================================================================
1240 //function : SetZSize
1242 //=============================================================================
1243 void V3d_View::SetZSize (const Standard_Real theSize)
1245 Handle(Graphic3d_Camera) aCamera = Camera();
1247 Standard_Real Zmax = theSize / 2.;
1249 Standard_Real aDistance = aCamera->Distance();
1256 // ShortReal precision factor used to add meaningful tolerance to
1257 // ZNear, ZFar values in order to avoid equality after type conversion
1258 // to ShortReal matrices type.
1259 const Standard_Real aPrecision = 1.0 / Pow (10.0, ShortRealDigits() - 1);
1261 Standard_Real aZFar = Zmax + aDistance * 2.0;
1262 Standard_Real aZNear = -Zmax + aDistance;
1263 aZNear -= Abs (aZNear) * aPrecision;
1264 aZFar += Abs (aZFar) * aPrecision;
1266 if (!aCamera->IsOrthographic())
1268 if (aZFar < aPrecision)
1270 // Invalid case when both values are negative
1271 aZNear = aPrecision;
1272 aZFar = aPrecision * 2.0;
1274 else if (aZNear < Abs (aZFar) * aPrecision)
1276 // Z is less than 0.0, try to fix it using any appropriate z-scale
1277 aZNear = Abs (aZFar) * aPrecision;
1281 // If range is too small
1282 if (aZFar < (aZNear + Abs (aZFar) * aPrecision))
1284 aZFar = aZNear + Abs (aZFar) * aPrecision;
1287 aCamera->SetZRange (aZNear, aZFar);
1289 if (myImmediateUpdate)
1295 //=============================================================================
1296 //function : SetZoom
1298 //=============================================================================
1299 void V3d_View::SetZoom (const Standard_Real theCoef,const Standard_Boolean theToStart)
1301 V3d_BadValue_Raise_if (theCoef <= 0., "V3d_View::SetZoom, bad coefficient");
1303 Handle(Graphic3d_Camera) aCamera = Camera();
1307 myCamStartOpEye = aCamera->Eye();
1308 myCamStartOpCenter = aCamera->Center();
1311 Standard_Real aViewWidth = aCamera->ViewDimensions().X();
1312 Standard_Real aViewHeight = aCamera->ViewDimensions().Y();
1314 // ensure that zoom will not be too small or too big
1315 Standard_Real aCoef = theCoef;
1316 if (aViewWidth < aCoef * Precision::Confusion())
1318 aCoef = aViewWidth / Precision::Confusion();
1320 else if (aViewWidth > aCoef * 1e12)
1322 aCoef = aViewWidth / 1e12;
1324 if (aViewHeight < aCoef * Precision::Confusion())
1326 aCoef = aViewHeight / Precision::Confusion();
1328 else if (aViewHeight > aCoef * 1e12)
1330 aCoef = aViewHeight / 1e12;
1333 aCamera->SetEye (myCamStartOpEye);
1334 aCamera->SetCenter (myCamStartOpCenter);
1335 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);
1361 //=============================================================================
1362 //function : SetAxialScale
1364 //=============================================================================
1365 void V3d_View::SetAxialScale( const Standard_Real Sx, const Standard_Real Sy, const Standard_Real Sz )
1367 V3d_BadValue_Raise_if( Sx <= 0. || Sy <= 0. || Sz <= 0.,"V3d_View::SetAxialScale, bad coefficient");
1369 Camera()->SetAxialScale (gp_XYZ (Sx, Sy, Sz));
1374 //=============================================================================
1375 //function : SetRatio
1377 //=============================================================================
1378 void V3d_View::SetRatio()
1380 if (MyWindow.IsNull())
1385 Standard_Integer aWidth = 0;
1386 Standard_Integer aHeight = 0;
1387 MyWindow->Size (aWidth, aHeight);
1388 if (aWidth > 0 && aHeight > 0)
1390 Standard_Real aRatio = static_cast<Standard_Real> (aWidth) /
1391 static_cast<Standard_Real> (aHeight);
1393 Camera() ->SetAspect (aRatio);
1394 myDefaultCamera->SetAspect (aRatio);
1398 //=============================================================================
1401 //=============================================================================
1402 void V3d_View::FitAll (const Standard_Real theMargin, const Standard_Boolean theToUpdate)
1404 FitAll (myView->MinMaxValues(), theMargin, theToUpdate);
1407 //=============================================================================
1410 //=============================================================================
1411 void V3d_View::FitAll (const Bnd_Box& theBox, const Standard_Real theMargin, const Standard_Boolean theToUpdate)
1413 Standard_ASSERT_RAISE(theMargin >= 0.0 && theMargin < 1.0, "Invalid margin coefficient");
1415 if (myView->NumberOfDisplayedStructures() == 0)
1420 if (!FitMinMax (Camera(), theBox, theMargin, 10.0 * Precision::Confusion()))
1427 if (myImmediateUpdate || theToUpdate)
1433 //=============================================================================
1434 //function : DepthFitAll
1436 //=============================================================================
1437 void V3d_View::DepthFitAll(const Standard_Real Aspect,
1438 const Standard_Real Margin)
1440 Standard_Real Xmin,Ymin,Zmin,Xmax,Ymax,Zmax,U,V,W,U1,V1,W1 ;
1441 Standard_Real Umin,Vmin,Wmin,Umax,Vmax,Wmax ;
1442 Standard_Real Dx,Dy,Dz,Size;
1444 Standard_Integer Nstruct = myView->NumberOfDisplayedStructures() ;
1446 if((Nstruct <= 0) || (Aspect < 0.) || (Margin < 0.) || (Margin > 1.)) {
1451 Bnd_Box aBox = myView->MinMaxValues();
1457 aBox.Get (Xmin,Ymin,Zmin,Xmax,Ymax,Zmax);
1458 Project (Xmin,Ymin,Zmin,U,V,W) ;
1459 Project (Xmax,Ymax,Zmax,U1,V1,W1) ;
1460 Umin = Min(U,U1) ; Umax = Max(U,U1) ;
1461 Vmin = Min(V,V1) ; Vmax = Max(V,V1) ;
1462 Wmin = Min(W,W1) ; Wmax = Max(W,W1) ;
1463 Project (Xmin,Ymin,Zmax,U,V,W) ;
1464 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1465 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1466 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1467 Project (Xmax,Ymin,Zmax,U,V,W) ;
1468 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1469 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1470 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1471 Project (Xmax,Ymin,Zmin,U,V,W) ;
1472 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1473 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1474 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1475 Project (Xmax,Ymax,Zmin,U,V,W) ;
1476 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1477 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1478 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1479 Project (Xmin,Ymax,Zmax,U,V,W) ;
1480 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1481 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1482 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1483 Project (Xmin,Ymax,Zmin,U,V,W) ;
1484 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1485 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1486 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1489 Wmax = Max(Abs(Wmin),Abs(Wmax)) ;
1490 Dz = 2.*Wmax + Margin * Wmax;
1492 // Compute depth value
1493 Dx = Abs(Umax - Umin) ; Dy = Abs(Vmax - Vmin) ; // Dz = Abs(Wmax - Wmin);
1494 Dx += Margin * Dx; Dy += Margin * Dy;
1495 Size = Sqrt(Dx*Dx + Dy*Dy + Dz*Dz);
1498 SetDepth( Aspect * Size / 2.);
1504 //=======================================================================
1505 //function : WindowFit
1507 //=======================================================================
1508 void V3d_View::WindowFit (const Standard_Integer theMinXp,
1509 const Standard_Integer theMinYp,
1510 const Standard_Integer theMaxXp,
1511 const Standard_Integer theMaxYp)
1513 Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
1515 Handle(Graphic3d_Camera) aCamera = Camera();
1517 if (!aCamera->IsOrthographic())
1519 // normalize view coordinates
1520 Standard_Integer aWinWidth, aWinHeight;
1521 MyWindow->Size (aWinWidth, aWinHeight);
1523 // z coordinate of camera center
1524 Standard_Real aDepth = aCamera->Project (aCamera->Center()).Z();
1526 // camera projection coordinate are in NDC which are normalized [-1, 1]
1527 Standard_Real aUMin = (2.0 / aWinWidth) * theMinXp - 1.0;
1528 Standard_Real aUMax = (2.0 / aWinWidth) * theMaxXp - 1.0;
1529 Standard_Real aVMin = (2.0 / aWinHeight) * theMinYp - 1.0;
1530 Standard_Real aVMax = (2.0 / aWinHeight) * theMaxYp - 1.0;
1532 // compute camera panning
1533 gp_Pnt aScreenCenter (0.0, 0.0, aDepth);
1534 gp_Pnt aFitCenter ((aUMin + aUMax) * 0.5, (aVMin + aVMax) * 0.5, aDepth);
1535 gp_Pnt aPanTo = aCamera->ConvertProj2View (aFitCenter);
1536 gp_Pnt aPanFrom = aCamera->ConvertProj2View (aScreenCenter);
1537 gp_Vec aPanVec (aPanFrom, aPanTo);
1539 // compute section size
1540 gp_Pnt aFitTopRight (aUMax, aVMax, aDepth);
1541 gp_Pnt aFitBotLeft (aUMin, aVMin, aDepth);
1542 gp_Pnt aViewBotLeft = aCamera->ConvertProj2View (aFitBotLeft);
1543 gp_Pnt aViewTopRight = aCamera->ConvertProj2View (aFitTopRight);
1545 Standard_Real aUSize = aViewTopRight.X() - aViewBotLeft.X();
1546 Standard_Real aVSize = aViewTopRight.Y() - aViewBotLeft.Y();
1548 Translate (aCamera, aPanVec.X(), -aPanVec.Y());
1549 Scale (aCamera, aUSize, aVSize);
1554 Standard_Real aX1, aY1, aX2, aY2;
1555 Convert (theMinXp, theMinYp, aX1, aY1);
1556 Convert (theMaxXp, theMaxYp, aX2, aY2);
1557 FitAll (aX1, aY1, aX2, aY2);
1560 SetImmediateUpdate (wasUpdateEnabled);
1565 //=======================================================================
1566 //function : ConvertToGrid
1568 //=======================================================================
1569 void V3d_View::ConvertToGrid(const Standard_Integer Xp,
1570 const Standard_Integer Yp,
1573 Standard_Real& Zg) const
1575 Graphic3d_Vertex aVrp;
1576 Standard_Real anX, anY, aZ;
1577 Convert (Xp, Yp, anX, anY, aZ);
1578 aVrp.SetCoord (anX, anY, aZ);
1580 if( MyViewer->Grid()->IsActive() ) {
1581 Graphic3d_Vertex aNewVrp = Compute (aVrp) ;
1582 aNewVrp.Coord (Xg,Yg,Zg) ;
1584 aVrp.Coord (Xg,Yg,Zg) ;
1587 //=======================================================================
1588 //function : ConvertToGrid
1590 //=======================================================================
1591 void V3d_View::ConvertToGrid(const Standard_Real X,
1592 const Standard_Real Y,
1593 const Standard_Real Z,
1596 Standard_Real& Zg) const
1598 if( MyViewer->Grid()->IsActive() ) {
1599 Graphic3d_Vertex aVrp (X,Y,Z) ;
1600 Graphic3d_Vertex aNewVrp = Compute (aVrp) ;
1601 aNewVrp.Coord(Xg,Yg,Zg) ;
1603 Xg = X; Yg = Y; Zg = Z;
1607 //=======================================================================
1608 //function : Convert
1610 //=======================================================================
1611 Standard_Real V3d_View::Convert(const Standard_Integer Vp) const
1613 Standard_Integer aDxw, aDyw ;
1615 V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
1617 MyWindow->Size (aDxw, aDyw);
1618 Standard_Real aValue;
1620 gp_Pnt aViewDims = Camera()->ViewDimensions();
1621 aValue = aViewDims.X() * (Standard_Real)Vp / (Standard_Real)aDxw;
1626 //=======================================================================
1627 //function : Convert
1629 //=======================================================================
1630 void V3d_View::Convert(const Standard_Integer Xp,
1631 const Standard_Integer Yp,
1633 Standard_Real& Yv) const
1635 Standard_Integer aDxw, aDyw;
1637 V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
1639 MyWindow->Size (aDxw, aDyw);
1641 gp_Pnt aPoint (Xp * 2.0 / aDxw - 1.0, (aDyw - Yp) * 2.0 / aDyw - 1.0, 0.0);
1642 aPoint = Camera()->ConvertProj2View (aPoint);
1648 //=======================================================================
1649 //function : Convert
1651 //=======================================================================
1652 Standard_Integer V3d_View::Convert(const Standard_Real Vv) const
1654 V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
1656 Standard_Integer aDxw, aDyw;
1657 MyWindow->Size (aDxw, aDyw);
1659 gp_Pnt aViewDims = Camera()->ViewDimensions();
1660 Standard_Integer aValue = RealToInt (aDxw * Vv / (aViewDims.X()));
1665 //=======================================================================
1666 //function : Convert
1668 //=======================================================================
1669 void V3d_View::Convert(const Standard_Real Xv,
1670 const Standard_Real Yv,
1671 Standard_Integer& Xp,
1672 Standard_Integer& Yp) const
1674 V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
1676 Standard_Integer aDxw, aDyw;
1677 MyWindow->Size (aDxw, aDyw);
1679 gp_Pnt aPoint (Xv, Yv, 0.0);
1680 aPoint = Camera()->ConvertView2Proj (aPoint);
1681 aPoint = gp_Pnt ((aPoint.X() + 1.0) * aDxw / 2.0, aDyw - (aPoint.Y() + 1.0) * aDyw / 2.0, 0.0);
1683 Xp = RealToInt (aPoint.X());
1684 Yp = RealToInt (aPoint.Y());
1687 //=======================================================================
1688 //function : Convert
1690 //=======================================================================
1691 void V3d_View::Convert(const Standard_Integer Xp,
1692 const Standard_Integer Yp,
1695 Standard_Real& Z) const
1697 V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
1698 Standard_Integer aHeight, aWidth;
1699 MyWindow->Size (aWidth, aHeight);
1701 Standard_Real anX = 2.0 * Xp / aWidth - 1.0;
1702 Standard_Real anY = 2.0 * (aHeight - 1 - Yp) / aHeight - 1.0;
1703 Standard_Real aZ = 2.0 * 0.0 - 1.0;
1705 gp_Pnt aResult = Camera()->UnProject (gp_Pnt (anX, anY, aZ));
1712 //=======================================================================
1713 //function : ConvertWithProj
1715 //=======================================================================
1716 void V3d_View::ConvertWithProj(const Standard_Integer theXp,
1717 const Standard_Integer theYp,
1718 Standard_Real& theX,
1719 Standard_Real& theY,
1720 Standard_Real& theZ,
1721 Standard_Real& theDx,
1722 Standard_Real& theDy,
1723 Standard_Real& theDz) const
1725 V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
1726 Standard_Integer aHeight = 0, aWidth = 0;
1727 MyWindow->Size (aWidth, aHeight);
1729 const Standard_Real anX = 2.0 * theXp / aWidth - 1.0;
1730 const Standard_Real anY = 2.0 * (aHeight - 1 - theYp) / aHeight - 1.0;
1731 const Standard_Real aZ = 2.0 * 0.0 - 1.0;
1733 const Handle(Graphic3d_Camera)& aCamera = Camera();
1734 const gp_Pnt aResult1 = aCamera->UnProject (gp_Pnt (anX, anY, aZ));
1735 const gp_Pnt aResult2 = aCamera->UnProject (gp_Pnt (anX, anY, aZ - 10.0));
1737 theX = aResult1.X();
1738 theY = aResult1.Y();
1739 theZ = aResult1.Z();
1740 Graphic3d_Vec3d aNormDir (theX - aResult2.X(),
1741 theY - aResult2.Y(),
1742 theZ - aResult2.Z());
1743 aNormDir.Normalize();
1745 theDx = aNormDir.x();
1746 theDy = aNormDir.y();
1747 theDz = aNormDir.z();
1750 //=======================================================================
1751 //function : Convert
1753 //=======================================================================
1754 void V3d_View::Convert(const Standard_Real X,
1755 const Standard_Real Y,
1756 const Standard_Real Z,
1757 Standard_Integer& Xp,
1758 Standard_Integer& Yp) const
1760 V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
1761 Standard_Integer aHeight, aWidth;
1762 MyWindow->Size (aWidth, aHeight);
1764 gp_Pnt aPoint = Camera()->Project (gp_Pnt (X, Y, Z));
1766 Xp = RealToInt ((aPoint.X() + 1) * 0.5 * aWidth);
1767 Yp = RealToInt (aHeight - 1 - (aPoint.Y() + 1) * 0.5 * aHeight);
1770 //=======================================================================
1771 //function : Project
1773 //=======================================================================
1774 void V3d_View::Project (const Standard_Real theX,
1775 const Standard_Real theY,
1776 const Standard_Real theZ,
1777 Standard_Real& theXp,
1778 Standard_Real& theYp) const
1781 Project (theX, theY, theZ, theXp, theYp, aZp);
1784 //=======================================================================
1785 //function : Project
1787 //=======================================================================
1788 void V3d_View::Project (const Standard_Real theX,
1789 const Standard_Real theY,
1790 const Standard_Real theZ,
1791 Standard_Real& theXp,
1792 Standard_Real& theYp,
1793 Standard_Real& theZp) const
1795 Handle(Graphic3d_Camera) aCamera = Camera();
1797 gp_XYZ aViewSpaceDimensions = aCamera->ViewDimensions();
1798 Standard_Real aXSize = aViewSpaceDimensions.X();
1799 Standard_Real aYSize = aViewSpaceDimensions.Y();
1800 Standard_Real aZSize = aViewSpaceDimensions.Z();
1802 gp_Pnt aPoint = aCamera->Project (gp_Pnt (theX, theY, theZ));
1804 // NDC [-1, 1] --> PROJ [ -size / 2, +size / 2 ]
1805 theXp = aPoint.X() * aXSize * 0.5;
1806 theYp = aPoint.Y() * aYSize * 0.5;
1807 theZp = aPoint.Z() * aZSize * 0.5;
1810 //=======================================================================
1811 //function : BackgroundColor
1813 //=======================================================================
1814 void V3d_View::BackgroundColor(const Quantity_TypeOfColor Type,
1817 Standard_Real& V3) const
1819 Quantity_Color C = BackgroundColor() ;
1820 C.Values(V1,V2,V3,Type) ;
1823 //=======================================================================
1824 //function : BackgroundColor
1826 //=======================================================================
1827 Quantity_Color V3d_View::BackgroundColor() const
1829 return myView->Background().Color() ;
1832 //=======================================================================
1833 //function : GradientBackgroundColors
1835 //=======================================================================
1836 void V3d_View::GradientBackgroundColors (Quantity_Color& theColor1, Quantity_Color& theColor2) const
1838 myView->GradientBackground().Colors (theColor1, theColor2);
1841 //=======================================================================
1842 //function : GradientBackground
1844 //=======================================================================
1845 Aspect_GradientBackground V3d_View::GradientBackground() const
1847 return myView->GradientBackground();
1850 //=======================================================================
1853 //=======================================================================
1854 Standard_Real V3d_View::Scale() const
1856 return myDefaultCamera->Scale() / Camera()->Scale();
1859 //=======================================================================
1860 //function : AxialScale
1862 //=======================================================================
1863 void V3d_View::AxialScale(Standard_Real& Sx, Standard_Real& Sy, Standard_Real& Sz) const
1865 gp_Pnt anAxialScale = Camera()->AxialScale();
1866 Sx = anAxialScale.X();
1867 Sy = anAxialScale.Y();
1868 Sz = anAxialScale.Z();
1871 //=======================================================================
1874 //=======================================================================
1875 void V3d_View::Size(Standard_Real& Width, Standard_Real& Height) const
1877 gp_Pnt aViewDims = Camera()->ViewDimensions();
1879 Width = aViewDims.X();
1880 Height = aViewDims.Y();
1883 //=======================================================================
1886 //=======================================================================
1887 Standard_Real V3d_View::ZSize() const
1889 gp_Pnt aViewDims = Camera()->ViewDimensions();
1891 return aViewDims.Z();
1894 //=======================================================================
1897 //=======================================================================
1898 Standard_Integer V3d_View::MinMax(Standard_Real& Umin,
1899 Standard_Real& Vmin,
1900 Standard_Real& Umax,
1901 Standard_Real& Vmax) const
1903 Standard_Real Wmin,Wmax,U,V,W ;
1904 Standard_Real Xmin,Ymin,Zmin,Xmax,Ymax,Zmax ;
1906 Standard_Integer Nstruct = myView->NumberOfDisplayedStructures() ;
1909 Bnd_Box aBox = myView->MinMaxValues();
1910 aBox.Get (Xmin,Ymin,Zmin,Xmax,Ymax,Zmax);
1911 Project (Xmin,Ymin,Zmin,Umin,Vmin,Wmin) ;
1912 Project (Xmax,Ymax,Zmax,Umax,Vmax,Wmax) ;
1913 Project (Xmin,Ymin,Zmax,U,V,W) ;
1914 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1915 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1916 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1917 Project (Xmax,Ymin,Zmax,U,V,W) ;
1918 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1919 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1920 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1921 Project (Xmax,Ymin,Zmin,U,V,W) ;
1922 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1923 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1924 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1925 Project (Xmax,Ymax,Zmin,U,V,W) ;
1926 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1927 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1928 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1929 Project (Xmin,Ymax,Zmax,U,V,W) ;
1930 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1931 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1932 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1933 Project (Xmin,Ymax,Zmin,U,V,W) ;
1934 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1935 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1936 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1941 //=======================================================================
1944 //=======================================================================
1945 Standard_Integer V3d_View::MinMax(Standard_Real& Xmin,
1946 Standard_Real& Ymin,
1947 Standard_Real& Zmin,
1948 Standard_Real& Xmax,
1949 Standard_Real& Ymax,
1950 Standard_Real& Zmax) const
1953 // Standard_Integer Nstruct = (MyView->DisplayedStructures())->Extent() ;
1954 Standard_Integer Nstruct = myView->NumberOfDisplayedStructures() ;
1957 Bnd_Box aBox = myView->MinMaxValues();
1958 aBox.Get (Xmin,Ymin,Zmin,Xmax,Ymax,Zmax);
1963 //=======================================================================
1964 //function : GravityPoint
1966 //=======================================================================
1967 gp_Pnt V3d_View::GravityPoint() const
1969 Graphic3d_MapOfStructure aSetOfStructures;
1970 myView->DisplayedStructures (aSetOfStructures);
1972 Standard_Boolean hasSelection = Standard_False;
1973 for (Graphic3d_MapIteratorOfMapOfStructure aStructIter (aSetOfStructures);
1974 aStructIter.More(); aStructIter.Next())
1976 if (aStructIter.Key()->IsHighlighted()
1977 && aStructIter.Key()->IsVisible())
1979 hasSelection = Standard_True;
1984 Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
1985 Standard_Integer aNbPoints = 0;
1986 gp_XYZ aResult (0.0, 0.0, 0.0);
1987 for (Graphic3d_MapIteratorOfMapOfStructure aStructIter (aSetOfStructures);
1988 aStructIter.More(); aStructIter.Next())
1990 const Handle(Graphic3d_Structure)& aStruct = aStructIter.Key();
1991 if (!aStruct->IsVisible()
1992 || aStruct->IsInfinite()
1993 || (hasSelection && !aStruct->IsHighlighted()))
1998 const Graphic3d_BndBox3d& aBox = aStruct->CStructure()->BoundingBox();
1999 if (!aBox.IsValid())
2004 // skip transformation-persistent objects
2005 if (!aStruct->TransformPersistence().IsNull())
2010 // use camera projection to find gravity point
2011 Xmin = aBox.CornerMin().x();
2012 Ymin = aBox.CornerMin().y();
2013 Zmin = aBox.CornerMin().z();
2014 Xmax = aBox.CornerMax().x();
2015 Ymax = aBox.CornerMax().y();
2016 Zmax = aBox.CornerMax().z();
2017 gp_Pnt aPnts[THE_NB_BOUND_POINTS] =
2019 gp_Pnt (Xmin, Ymin, Zmin), gp_Pnt (Xmin, Ymin, Zmax),
2020 gp_Pnt (Xmin, Ymax, Zmin), gp_Pnt (Xmin, Ymax, Zmax),
2021 gp_Pnt (Xmax, Ymin, Zmin), gp_Pnt (Xmax, Ymin, Zmax),
2022 gp_Pnt (Xmax, Ymax, Zmin), gp_Pnt (Xmax, Ymax, Zmax)
2025 for (Standard_Integer aPntIt = 0; aPntIt < THE_NB_BOUND_POINTS; ++aPntIt)
2027 const gp_Pnt& aBndPnt = aPnts[aPntIt];
2028 const gp_Pnt aProjected = Camera()->Project (aBndPnt);
2029 if (Abs (aProjected.X()) <= 1.0
2030 && Abs (aProjected.Y()) <= 1.0)
2032 aResult += aBndPnt.XYZ();
2040 // fallback - just use bounding box of entire scene
2041 Bnd_Box aBox = myView->MinMaxValues();
2044 aBox.Get (Xmin, Ymin, Zmin,
2046 gp_Pnt aPnts[THE_NB_BOUND_POINTS] =
2048 gp_Pnt (Xmin, Ymin, Zmin), gp_Pnt (Xmin, Ymin, Zmax),
2049 gp_Pnt (Xmin, Ymax, Zmin), gp_Pnt (Xmin, Ymax, Zmax),
2050 gp_Pnt (Xmax, Ymin, Zmin), gp_Pnt (Xmax, Ymin, Zmax),
2051 gp_Pnt (Xmax, Ymax, Zmin), gp_Pnt (Xmax, Ymax, Zmax)
2054 for (Standard_Integer aPntIt = 0; aPntIt < THE_NB_BOUND_POINTS; ++aPntIt)
2056 const gp_Pnt& aBndPnt = aPnts[aPntIt];
2057 aResult += aBndPnt.XYZ();
2065 aResult /= aNbPoints;
2071 //=======================================================================
2074 //=======================================================================
2075 void V3d_View::Eye(Standard_Real& X, Standard_Real& Y, Standard_Real& Z) const
2077 gp_Pnt aCameraEye = Camera()->Eye();
2083 //=============================================================================
2084 //function : ProjReferenceAxe
2086 //=============================================================================
2087 void V3d_View::ProjReferenceAxe(const Standard_Integer Xpix,
2088 const Standard_Integer Ypix,
2094 Standard_Real& VZ) const
2096 Standard_Real Xo,Yo,Zo;
2098 Convert (Xpix, Ypix, XP, YP, ZP);
2099 if ( Type() == V3d_PERSPECTIVE )
2101 FocalReferencePoint (Xo,Yo,Zo);
2112 //=============================================================================
2115 //=============================================================================
2116 Standard_Real V3d_View::Depth() const
2118 return Camera()->Distance();
2121 //=============================================================================
2124 //=============================================================================
2125 void V3d_View::Proj(Standard_Real& Dx, Standard_Real& Dy, Standard_Real& Dz) const
2127 gp_Dir aCameraDir = Camera()->Direction().Reversed();
2128 Dx = aCameraDir.X();
2129 Dy = aCameraDir.Y();
2130 Dz = aCameraDir.Z();
2133 //=============================================================================
2136 //=============================================================================
2137 void V3d_View::At(Standard_Real& X, Standard_Real& Y, Standard_Real& Z) const
2139 gp_Pnt aCameraCenter = Camera()->Center();
2140 X = aCameraCenter.X();
2141 Y = aCameraCenter.Y();
2142 Z = aCameraCenter.Z();
2145 //=============================================================================
2148 //=============================================================================
2149 void V3d_View::Up(Standard_Real& Vx, Standard_Real& Vy, Standard_Real& Vz) const
2151 gp_Dir aCameraUp = Camera()->Up();
2157 //=============================================================================
2160 //=============================================================================
2161 Standard_Real V3d_View::Twist() const
2163 gp_Vec Xaxis, Yaxis, Zaxis;
2164 const gp_Dir aReferencePlane (Camera()->Direction().Reversed());
2165 if (!screenAxis (aReferencePlane, gp::DZ(), Xaxis, Yaxis, Zaxis)
2166 && !screenAxis (aReferencePlane, gp::DY(), Xaxis, Yaxis, Zaxis)
2167 && !screenAxis (aReferencePlane, gp::DX(), Xaxis, Yaxis, Zaxis))
2172 // Compute Cross Vector From Up & Origin
2173 const gp_Dir aCameraUp = Camera()->Up();
2174 const gp_XYZ aP = Yaxis.XYZ().Crossed (aCameraUp.XYZ());
2177 Standard_Real anAngle = ASin (Max (Min (aP.Modulus(), 1.0), -1.0));
2178 if (Yaxis.Dot (aCameraUp.XYZ()) < 0.0)
2180 anAngle = M_PI - anAngle;
2185 const gp_Dir aProjDir = Camera()->Direction().Reversed();
2186 if (aP.Dot (aProjDir.XYZ()) < 0.0)
2188 anAngle = DEUXPI - anAngle;
2194 //=============================================================================
2195 //function : ShadingModel
2197 //=============================================================================
2198 Graphic3d_TypeOfShadingModel V3d_View::ShadingModel() const
2200 return myView->ShadingModel();
2203 //=============================================================================
2204 //function : TextureEnv
2206 //=============================================================================
2207 Handle(Graphic3d_TextureEnv) V3d_View::TextureEnv() const
2209 return myView->TextureEnv();
2212 //=============================================================================
2213 //function : Visualization
2215 //=============================================================================
2216 V3d_TypeOfVisualization V3d_View::Visualization() const
2218 return static_cast<V3d_TypeOfVisualization> (myView->VisualizationType());
2221 //=============================================================================
2222 //function : IfWindow
2224 //=============================================================================
2225 Standard_Boolean V3d_View::IfWindow() const
2227 return myView->IsDefined();
2230 //=============================================================================
2233 //=============================================================================
2234 V3d_TypeOfView V3d_View::Type() const
2236 return Camera()->IsOrthographic() ? V3d_ORTHOGRAPHIC : V3d_PERSPECTIVE;
2239 //=============================================================================
2240 //function : SetFocale
2242 //=============================================================================
2243 void V3d_View::SetFocale( const Standard_Real focale )
2245 Handle(Graphic3d_Camera) aCamera = Camera();
2247 if (aCamera->IsOrthographic())
2252 Standard_Real aFOVyRad = ATan (focale / (aCamera->Distance() * 2.0));
2254 aCamera->SetFOVy (aFOVyRad * (360 / M_PI));
2259 //=============================================================================
2262 //=============================================================================
2263 Standard_Real V3d_View::Focale() const
2265 Handle(Graphic3d_Camera) aCamera = Camera();
2267 if (aCamera->IsOrthographic())
2272 return aCamera->Distance() * 2.0 * Tan (aCamera->FOVy() * M_PI / 360.0);
2275 //=============================================================================
2276 //function : screenAxis
2278 //=============================================================================
2279 Standard_Boolean V3d_View::screenAxis (const gp_Dir& theVpn, const gp_Dir& theVup,
2280 gp_Vec& theXaxe, gp_Vec& theYaxe, gp_Vec& theZaxe)
2282 theXaxe = theVup.XYZ().Crossed (theVpn.XYZ());
2283 if (theXaxe.Magnitude() <= gp::Resolution())
2285 return Standard_False;
2287 theXaxe.Normalize();
2289 theYaxe = theVpn.XYZ().Crossed (theXaxe.XYZ());
2290 if (theYaxe.Magnitude() <= gp::Resolution())
2292 return Standard_False;
2294 theYaxe.Normalize();
2296 theZaxe = theVpn.XYZ();
2297 theZaxe.Normalize();
2298 return Standard_True;
2301 //=============================================================================
2302 //function : TrsPoint
2304 //=============================================================================
2305 gp_XYZ V3d_View::TrsPoint (const Graphic3d_Vertex& thePnt, const TColStd_Array2OfReal& theMat)
2308 const Standard_Integer lr = theMat.LowerRow();
2309 const Standard_Integer ur = theMat.UpperRow();
2310 const Standard_Integer lc = theMat.LowerCol();
2311 const Standard_Integer uc = theMat.UpperCol();
2312 if ((ur - lr + 1 != 4) || (uc - lc + 1 != 4))
2314 return gp_XYZ (thePnt.X(), thePnt.Y(), thePnt.Z());
2317 Standard_Real X, Y, Z;
2318 thePnt.Coord (X,Y,Z);
2319 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);
2320 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);
2321 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);
2322 return gp_XYZ (XX, YY, ZZ);
2325 //=======================================================================
2328 //=======================================================================
2329 void V3d_View::Pan (const Standard_Integer theDXp,
2330 const Standard_Integer theDYp,
2331 const Standard_Real theZoomFactor,
2332 const Standard_Boolean theToStart)
2334 Panning (Convert (theDXp), Convert (theDYp), theZoomFactor, theToStart);
2337 //=======================================================================
2338 //function : Panning
2340 //=======================================================================
2341 void V3d_View::Panning (const Standard_Real theDXv,
2342 const Standard_Real theDYv,
2343 const Standard_Real theZoomFactor,
2344 const Standard_Boolean theToStart)
2346 Standard_ASSERT_RAISE (theZoomFactor > 0.0, "Bad zoom factor");
2348 Handle(Graphic3d_Camera) aCamera = Camera();
2352 myCamStartOpDir = aCamera->Direction();
2353 myCamStartOpEye = aCamera->Eye();
2354 myCamStartOpCenter = aCamera->Center();
2357 Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
2359 gp_Pnt aViewDims = aCamera->ViewDimensions();
2361 aCamera->SetEyeAndCenter (myCamStartOpEye, myCamStartOpCenter);
2362 aCamera->SetDirectionFromEye (myCamStartOpDir);
2363 Translate (aCamera, -theDXv, -theDYv);
2364 Scale (aCamera, aViewDims.X() / theZoomFactor, aViewDims.Y() / theZoomFactor);
2366 SetImmediateUpdate (wasUpdateEnabled);
2371 //=======================================================================
2374 //=======================================================================
2375 void V3d_View::Zoom (const Standard_Integer theXp1,
2376 const Standard_Integer theYp1,
2377 const Standard_Integer theXp2,
2378 const Standard_Integer theYp2)
2380 Standard_Integer aDx = theXp2 - theXp1;
2381 Standard_Integer aDy = theYp2 - theYp1;
2382 if (aDx != 0 || aDy != 0)
2384 Standard_Real aCoeff = Sqrt( (Standard_Real)(aDx * aDx + aDy * aDy) ) / 100.0 + 1.0;
2385 aCoeff = (aDx > 0) ? aCoeff : 1.0 / aCoeff;
2386 SetZoom (aCoeff, Standard_True);
2390 //=======================================================================
2391 //function : StartZoomAtPoint
2393 //=======================================================================
2394 void V3d_View::StartZoomAtPoint (const Standard_Integer theXp,
2395 const Standard_Integer theYp)
2397 MyZoomAtPointX = theXp;
2398 MyZoomAtPointY = theYp;
2401 //=======================================================================
2402 //function : ZoomAtPoint
2404 //=======================================================================
2405 void V3d_View::ZoomAtPoint (const Standard_Integer theMouseStartX,
2406 const Standard_Integer theMouseStartY,
2407 const Standard_Integer theMouseEndX,
2408 const Standard_Integer theMouseEndY)
2410 Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
2413 Standard_Real aDxy = Standard_Real ((theMouseEndX + theMouseEndY) - (theMouseStartX + theMouseStartY));
2414 Standard_Real aDZoom = Abs (aDxy) / 100.0 + 1.0;
2415 aDZoom = (aDxy > 0.0) ? aDZoom : 1.0 / aDZoom;
2417 V3d_BadValue_Raise_if (aDZoom <= 0.0, "V3d_View::ZoomAtPoint, bad coefficient");
2419 Handle(Graphic3d_Camera) aCamera = Camera();
2421 Standard_Real aViewWidth = aCamera->ViewDimensions().X();
2422 Standard_Real aViewHeight = aCamera->ViewDimensions().Y();
2424 // ensure that zoom will not be too small or too big.
2425 Standard_Real aCoef = aDZoom;
2426 if (aViewWidth < aCoef * Precision::Confusion())
2428 aCoef = aViewWidth / Precision::Confusion();
2430 else if (aViewWidth > aCoef * 1e12)
2432 aCoef = aViewWidth / 1e12;
2434 if (aViewHeight < aCoef * Precision::Confusion())
2436 aCoef = aViewHeight / Precision::Confusion();
2438 else if (aViewHeight > aCoef * 1e12)
2440 aCoef = aViewHeight / 1e12;
2443 Standard_Real aZoomAtPointXv = 0.0;
2444 Standard_Real aZoomAtPointYv = 0.0;
2445 Convert (MyZoomAtPointX, MyZoomAtPointY, aZoomAtPointXv, aZoomAtPointYv);
2447 Standard_Real aDxv = aZoomAtPointXv / aCoef;
2448 Standard_Real aDyv = aZoomAtPointYv / aCoef;
2450 aCamera->SetScale (aCamera->Scale() / aCoef);
2451 Translate (aCamera, aZoomAtPointXv - aDxv, aZoomAtPointYv - aDyv);
2455 SetImmediateUpdate (wasUpdateEnabled);
2460 //=============================================================================
2461 //function : AxialScale
2463 //=============================================================================
2464 void V3d_View::AxialScale (const Standard_Integer Dx,
2465 const Standard_Integer Dy,
2466 const V3d_TypeOfAxe Axis)
2468 if( Dx != 0. || Dy != 0. ) {
2469 Standard_Real Sx, Sy, Sz;
2470 AxialScale( Sx, Sy, Sz );
2471 Standard_Real dscale = Sqrt(Dx*Dx + Dy*Dy) / 100. + 1;
2472 dscale = (Dx > 0) ? dscale : 1./dscale;
2473 if( Axis == V3d_X ) Sx = dscale;
2474 if( Axis == V3d_Y ) Sy = dscale;
2475 if( Axis == V3d_Z ) Sz = dscale;
2476 SetAxialScale( Sx, Sy, Sz );
2480 //=============================================================================
2483 //=============================================================================
2484 void V3d_View::FitAll(const Standard_Real theXmin,
2485 const Standard_Real theYmin,
2486 const Standard_Real theXmax,
2487 const Standard_Real theYmax)
2489 Handle(Graphic3d_Camera) aCamera = Camera();
2490 Standard_Real anAspect = aCamera->Aspect();
2492 Standard_Real aFitSizeU = Abs (theXmax - theXmin);
2493 Standard_Real aFitSizeV = Abs (theYmax - theYmin);
2494 Standard_Real aFitAspect = aFitSizeU / aFitSizeV;
2495 if (aFitAspect >= anAspect)
2497 aFitSizeV = aFitSizeU / anAspect;
2501 aFitSizeU = aFitSizeV * anAspect;
2504 Translate (aCamera, (theXmin + theXmax) * 0.5, (theYmin + theYmax) * 0.5);
2505 Scale (aCamera, aFitSizeU, aFitSizeV);
2512 //=============================================================================
2513 //function : StartRotation
2515 //=============================================================================
2516 void V3d_View::StartRotation(const Standard_Integer X,
2517 const Standard_Integer Y,
2518 const Standard_Real zRotationThreshold)
2523 rx = Standard_Real(Convert(x));
2524 ry = Standard_Real(Convert(y));
2525 myRotateGravity = GravityPoint();
2526 Rotate (0.0, 0.0, 0.0,
2527 myRotateGravity.X(), myRotateGravity.Y(), myRotateGravity.Z(),
2529 myZRotation = Standard_False;
2530 if( zRotationThreshold > 0. ) {
2531 Standard_Real dx = Abs(sx - rx/2.);
2532 Standard_Real dy = Abs(sy - ry/2.);
2533 // if( dx > rx/3. || dy > ry/3. ) myZRotation = Standard_True;
2534 Standard_Real dd = zRotationThreshold * (rx + ry)/2.;
2535 if( dx > dd || dy > dd ) myZRotation = Standard_True;
2540 //=============================================================================
2541 //function : Rotation
2543 //=============================================================================
2544 void V3d_View::Rotation(const Standard_Integer X,
2545 const Standard_Integer Y)
2547 if( rx == 0. || ry == 0. ) {
2551 Standard_Real dx=0.,dy=0.,dz=0.;
2553 dz = atan2(Standard_Real(X)-rx/2., ry/2.-Standard_Real(Y)) -
2554 atan2(sx-rx/2.,ry/2.-sy);
2556 dx = (Standard_Real(X) - sx) * M_PI / rx;
2557 dy = (sy - Standard_Real(Y)) * M_PI / ry;
2561 myRotateGravity.X(), myRotateGravity.Y(), myRotateGravity.Z(),
2565 //=============================================================================
2566 //function : SetComputedMode
2568 //=============================================================================
2569 void V3d_View::SetComputedMode (const Standard_Boolean theMode)
2575 myView->SetComputedMode (Standard_True);
2580 myView->SetComputedMode (Standard_False);
2584 //=============================================================================
2585 //function : ComputedMode
2587 //=============================================================================
2588 Standard_Boolean V3d_View::ComputedMode() const
2590 return myView->ComputedMode();
2593 //=============================================================================
2594 //function : SetBackFacingModel
2596 //=============================================================================
2597 void V3d_View::SetBackFacingModel (const V3d_TypeOfBackfacingModel theModel)
2599 myView->SetBackfacingModel (static_cast<Graphic3d_TypeOfBackfacingModel> (theModel));
2603 //=============================================================================
2604 //function : BackFacingModel
2606 //=============================================================================
2607 V3d_TypeOfBackfacingModel V3d_View::BackFacingModel() const
2609 return static_cast<V3d_TypeOfBackfacingModel> (myView->BackfacingModel());
2612 //=============================================================================
2615 //=============================================================================
2616 void V3d_View::Init()
2618 myComputedMode = MyViewer->ComputedMode();
2619 if (!myComputedMode || !MyViewer->DefaultComputedMode())
2621 SetComputedMode (Standard_False);
2625 //=============================================================================
2628 //=============================================================================
2629 Standard_Boolean V3d_View::Dump (const Standard_CString theFile,
2630 const Graphic3d_BufferType& theBufferType)
2632 Standard_Integer aWinWidth, aWinHeight;
2633 MyWindow->Size (aWinWidth, aWinHeight);
2634 Image_AlienPixMap anImage;
2636 return ToPixMap (anImage, aWinWidth, aWinHeight, theBufferType) && anImage.Save (theFile);
2639 //=============================================================================
2640 //function : ToPixMap
2642 //=============================================================================
2643 Standard_Boolean V3d_View::ToPixMap (Image_PixMap& theImage,
2644 const V3d_ImageDumpOptions& theParams)
2646 Graphic3d_Vec2i aTargetSize (theParams.Width, theParams.Height);
2647 if (aTargetSize.x() != 0
2648 && aTargetSize.y() != 0)
2650 // allocate image buffer for dumping
2651 if (theImage.IsEmpty()
2652 || theImage.SizeX() != Standard_Size(aTargetSize.x())
2653 || theImage.SizeY() != Standard_Size(aTargetSize.y()))
2655 Image_Format aFormat = Image_Format_UNKNOWN;
2656 switch (theParams.BufferType)
2658 case Graphic3d_BT_RGB: aFormat = Image_Format_RGB; break;
2659 case Graphic3d_BT_RGBA: aFormat = Image_Format_RGBA; break;
2660 case Graphic3d_BT_Depth: aFormat = Image_Format_GrayF; break;
2661 case Graphic3d_BT_RGB_RayTraceHdrLeft: aFormat = Image_Format_RGBF; break;
2664 if (!theImage.InitZero (aFormat, Standard_Size(aTargetSize.x()), Standard_Size(aTargetSize.y())))
2666 Message::DefaultMessenger()->Send (TCollection_AsciiString ("Fail to allocate an image ") + aTargetSize.x() + "x" + aTargetSize.y()
2667 + " for view dump", Message_Fail);
2668 return Standard_False;
2672 if (theImage.IsEmpty())
2674 Message::DefaultMessenger()->Send (TCollection_AsciiString ("V3d_View::ToPixMap() has been called without image dimensions"), Message_Fail);
2675 return Standard_False;
2677 aTargetSize.x() = (Standard_Integer )theImage.SizeX();
2678 aTargetSize.y() = (Standard_Integer )theImage.SizeY();
2680 Handle(Standard_Transient) aFBOPtr;
2681 Handle(Standard_Transient) aPrevFBOPtr = myView->FBO();
2682 Graphic3d_Vec2i aFBOVPSize = aTargetSize;
2684 bool isTiling = false;
2685 if (theParams.TileSize > 0)
2687 if (aFBOVPSize.x() > theParams.TileSize
2688 || aFBOVPSize.y() > theParams.TileSize)
2690 aFBOVPSize.x() = Min (aFBOVPSize.x(), theParams.TileSize);
2691 aFBOVPSize.y() = Min (aFBOVPSize.y(), theParams.TileSize);
2696 Graphic3d_Vec2i aPrevFBOVPSize;
2697 if (!aPrevFBOPtr.IsNull())
2699 Graphic3d_Vec2i aPrevFBOSizeMax;
2700 myView->FBOGetDimensions (aPrevFBOPtr,
2701 aPrevFBOVPSize.x(), aPrevFBOVPSize.y(),
2702 aPrevFBOSizeMax.x(), aPrevFBOSizeMax.y());
2703 if (aFBOVPSize.x() <= aPrevFBOSizeMax.x()
2704 && aFBOVPSize.y() <= aPrevFBOSizeMax.y())
2706 aFBOPtr = aPrevFBOPtr;
2710 if (aFBOPtr.IsNull())
2712 Standard_Integer aMaxTexSizeX = MyViewer->Driver()->InquireLimit (Graphic3d_TypeOfLimit_MaxViewDumpSizeX);
2713 Standard_Integer aMaxTexSizeY = MyViewer->Driver()->InquireLimit (Graphic3d_TypeOfLimit_MaxViewDumpSizeY);
2714 if (theParams.TileSize > aMaxTexSizeX
2715 || theParams.TileSize > aMaxTexSizeY)
2717 Message::DefaultMessenger()->Send (TCollection_AsciiString ("Image dump can not be performed - specified tile size (")
2718 + theParams.TileSize + ") exceeds hardware limits (" + aMaxTexSizeX + "x" + aMaxTexSizeY + ")", Message_Fail);
2719 return Standard_False;
2722 if (aFBOVPSize.x() > aMaxTexSizeX
2723 || aFBOVPSize.y() > aMaxTexSizeY)
2725 if (MyViewer->Driver()->InquireLimit (Graphic3d_TypeOfLimit_IsWorkaroundFBO))
2727 Message::DefaultMessenger ()->Send (TCollection_AsciiString ("Warning, workaround for Intel driver problem with empty FBO for images with big width is applyed."), Message_Warning);
2729 Message::DefaultMessenger()->Send (TCollection_AsciiString ("Info, tiling image dump is used, image size (")
2730 + aFBOVPSize.x() + "x" + aFBOVPSize.y() + ") exceeds hardware limits (" + aMaxTexSizeX + "x" + aMaxTexSizeY + ")", Message_Info);
2731 aFBOVPSize.x() = Min (aFBOVPSize.x(), aMaxTexSizeX);
2732 aFBOVPSize.y() = Min (aFBOVPSize.y(), aMaxTexSizeY);
2736 // Try to create hardware accelerated buffer
2737 aFBOPtr = myView->FBOCreate (aFBOVPSize.x(), aFBOVPSize.y());
2739 myView->SetFBO (aFBOPtr);
2741 if (aFBOPtr.IsNull())
2743 // try to use on-screen buffer
2744 Graphic3d_Vec2i aWinSize;
2745 MyWindow->Size (aWinSize.x(), aWinSize.y());
2746 if (aFBOVPSize.x() != aWinSize.x()
2747 || aFBOVPSize.y() != aWinSize.y())
2751 aFBOVPSize = aWinSize;
2753 Message::DefaultMessenger()->Send (TCollection_AsciiString ("Warning, on screen buffer is used for image dump - content might be invalid"), Message_Warning);
2756 // backup camera parameters
2757 Handle(Graphic3d_Camera) aStoreMapping = new Graphic3d_Camera();
2758 Handle(Graphic3d_Camera) aCamera = Camera();
2759 aStoreMapping->Copy (aCamera);
2760 if (aCamera->IsStereo())
2762 switch (theParams.StereoOptions)
2766 aCamera->SetProjectionType (Graphic3d_Camera::Projection_Perspective);
2769 case V3d_SDO_LEFT_EYE:
2771 aCamera->SetProjectionType (Graphic3d_Camera::Projection_MonoLeftEye);
2774 case V3d_SDO_RIGHT_EYE:
2776 aCamera->SetProjectionType (Graphic3d_Camera::Projection_MonoRightEye);
2779 case V3d_SDO_BLENDED:
2781 break; // dump as is
2785 if (theParams.ToAdjustAspect)
2787 aCamera->SetAspect (Standard_Real(aTargetSize.x()) / Standard_Real(aTargetSize.y()));
2791 // render immediate structures into back buffer rather than front
2792 const Standard_Boolean aPrevImmediateMode = myView->SetImmediateModeDrawToFront (Standard_False);
2794 Standard_Boolean isSuccess = Standard_True;
2797 if (!aFBOPtr.IsNull())
2799 myView->FBOChangeViewport (aFBOPtr, aTargetSize.x(), aTargetSize.y());
2802 isSuccess = isSuccess && myView->BufferDump (theImage, theParams.BufferType);
2806 Image_PixMap aTilePixMap;
2807 aTilePixMap.SetTopDown (theImage.IsTopDown());
2809 Graphic3d_Vec2i anOffset (0, 0);
2810 for (; anOffset.y() < aTargetSize.y(); anOffset.y() += aFBOVPSize.y())
2813 for (; anOffset.x() < aTargetSize.x(); anOffset.x() += aFBOVPSize.x())
2815 Graphic3d_CameraTile aTileUncropped;
2816 aTileUncropped.Offset = anOffset;
2817 aTileUncropped.TotalSize = aTargetSize;
2818 aTileUncropped.TileSize = aFBOVPSize;
2819 const Graphic3d_CameraTile aTile = aTileUncropped.Cropped();
2820 if (aTile.TileSize.x() < 1
2821 || aTile.TileSize.y() < 1)
2826 const Standard_Integer aLeft = aTile.Offset.x();
2827 Standard_Integer aBottom = aTile.Offset.y();
2828 if (theImage.IsTopDown())
2830 const Standard_Integer aTop = aTile.Offset.y() + aTile.TileSize.y();
2831 aBottom = aTargetSize.y() - aTop;
2833 aTilePixMap.InitWrapper (theImage.Format(), theImage.ChangeData()
2834 + theImage.SizeRowBytes() * aBottom + theImage.SizePixelBytes() * aLeft,
2835 aTile.TileSize.x(), aTile.TileSize.y(),
2836 theImage.SizeRowBytes());
2838 if (!aFBOPtr.IsNull())
2840 aCamera->SetTile (aTile);
2841 myView->FBOChangeViewport (aFBOPtr, aTile.TileSize.x(), aTile.TileSize.y());
2845 // no API to resize viewport of on-screen buffer - render uncropped
2846 aCamera->SetTile (aTileUncropped);
2849 isSuccess = isSuccess && myView->BufferDump (aTilePixMap, theParams.BufferType);
2863 myView->SetImmediateModeDrawToFront (aPrevImmediateMode);
2864 aCamera->Copy (aStoreMapping);
2865 if (aFBOPtr != aPrevFBOPtr)
2867 myView->FBORelease (aFBOPtr);
2869 else if (!aPrevFBOPtr.IsNull())
2871 myView->FBOChangeViewport (aPrevFBOPtr, aPrevFBOVPSize.x(), aPrevFBOVPSize.y());
2873 myView->SetFBO (aPrevFBOPtr);
2877 //=============================================================================
2878 //function : ImmediateUpdate
2880 //=============================================================================
2881 void V3d_View::ImmediateUpdate() const
2883 if (myImmediateUpdate)
2889 //=============================================================================
2890 //function : SetImmediateUpdate
2892 //=============================================================================
2893 Standard_Boolean V3d_View::SetImmediateUpdate (const Standard_Boolean theImmediateUpdate)
2895 Standard_Boolean aPreviousMode = myImmediateUpdate;
2896 myImmediateUpdate = theImmediateUpdate;
2897 return aPreviousMode;
2900 // =======================================================================
2901 // function : SetCamera
2903 // =======================================================================
2904 void V3d_View::SetCamera (const Handle(Graphic3d_Camera)& theCamera)
2906 myView->SetCamera (theCamera);
2911 // =======================================================================
2912 // function : GetCamera
2914 // =======================================================================
2915 const Handle(Graphic3d_Camera)& V3d_View::Camera() const
2917 return myView->Camera();
2920 // =======================================================================
2921 // function : FitMinMax
2922 // purpose : Internal
2923 // =======================================================================
2924 Standard_Boolean V3d_View::FitMinMax (const Handle(Graphic3d_Camera)& theCamera,
2925 const Bnd_Box& theBox,
2926 const Standard_Real theMargin,
2927 const Standard_Real theResolution,
2928 const Standard_Boolean theToEnlargeIfLine) const
2930 // Check bounding box for validness
2931 if (theBox.IsVoid())
2933 return Standard_False; // bounding box is out of bounds...
2936 // Apply "axial scaling" to the bounding points.
2937 // It is not the best approach to make this scaling as a part of fit all operation,
2938 // but the axial scale is integrated into camera orientation matrix and the other
2939 // option is to perform frustum plane adjustment algorithm in view camera space,
2940 // which will lead to a number of additional world-view space conversions and
2941 // loosing precision as well.
2942 gp_Pnt aBndMin = theBox.CornerMin().XYZ().Multiplied (theCamera->AxialScale());
2943 gp_Pnt aBndMax = theBox.CornerMax().XYZ().Multiplied (theCamera->AxialScale());
2945 if (aBndMax.IsEqual (aBndMin, RealEpsilon()))
2947 return Standard_False; // nothing to fit all
2950 // Prepare camera frustum planes.
2951 NCollection_Array1<gp_Pln> aFrustumPlane (1, 6);
2952 theCamera->Frustum (aFrustumPlane.ChangeValue (1),
2953 aFrustumPlane.ChangeValue (2),
2954 aFrustumPlane.ChangeValue (3),
2955 aFrustumPlane.ChangeValue (4),
2956 aFrustumPlane.ChangeValue (5),
2957 aFrustumPlane.ChangeValue (6));
2959 // Prepare camera up, side, direction vectors.
2960 gp_Dir aCamUp = theCamera->OrthogonalizedUp();
2961 gp_Dir aCamDir = theCamera->Direction();
2962 gp_Dir aCamSide = aCamDir ^ aCamUp;
2964 // Prepare scene bounding box parameters.
2965 gp_Pnt aBndCenter = (aBndMin.XYZ() + aBndMax.XYZ()) / 2.0;
2967 NCollection_Array1<gp_Pnt> aBndCorner (1, 8);
2968 aBndCorner.ChangeValue (1) = gp_Pnt (aBndMin.X(), aBndMin.Y(), aBndMin.Z());
2969 aBndCorner.ChangeValue (2) = gp_Pnt (aBndMin.X(), aBndMin.Y(), aBndMax.Z());
2970 aBndCorner.ChangeValue (3) = gp_Pnt (aBndMin.X(), aBndMax.Y(), aBndMin.Z());
2971 aBndCorner.ChangeValue (4) = gp_Pnt (aBndMin.X(), aBndMax.Y(), aBndMax.Z());
2972 aBndCorner.ChangeValue (5) = gp_Pnt (aBndMax.X(), aBndMin.Y(), aBndMin.Z());
2973 aBndCorner.ChangeValue (6) = gp_Pnt (aBndMax.X(), aBndMin.Y(), aBndMax.Z());
2974 aBndCorner.ChangeValue (7) = gp_Pnt (aBndMax.X(), aBndMax.Y(), aBndMin.Z());
2975 aBndCorner.ChangeValue (8) = gp_Pnt (aBndMax.X(), aBndMax.Y(), aBndMax.Z());
2977 // Perspective-correct camera projection vector, matching the bounding box is determined geometrically.
2978 // Knowing the initial shape of a frustum it is possible to match it to a bounding box.
2979 // Then, knowing the relation of camera projection vector to the frustum shape it is possible to
2980 // set up perspective-correct camera projection matching the bounding box.
2981 // These steps support non-asymmetric transformations of view-projection space provided by camera.
2982 // The zooming can be done by calculating view plane size matching the bounding box at center of
2983 // the bounding box. The only limitation here is that the scale of camera should define size of
2984 // its view plane passing through the camera center, and the center of camera should be on the
2985 // same line with the center of bounding box.
2987 // The following method is applied:
2988 // 1) Determine normalized asymmetry of camera projection vector by frustum planes.
2989 // 2) Determine new location of frustum planes, "matching" the bounding box.
2990 // 3) Determine new camera projection vector using the normalized asymmetry.
2991 // 4) Determine new zooming in view space.
2993 // 1. Determine normalized projection asymmetry (if any).
2994 Standard_Real anAssymX = Tan (( aCamSide).Angle (aFrustumPlane (1).Axis().Direction()))
2995 - Tan ((-aCamSide).Angle (aFrustumPlane (2).Axis().Direction()));
2996 Standard_Real anAssymY = Tan (( aCamUp) .Angle (aFrustumPlane (3).Axis().Direction()))
2997 - Tan ((-aCamUp) .Angle (aFrustumPlane (4).Axis().Direction()));
2999 // 2. Determine how far should be the frustum planes placed from center
3000 // of bounding box, in order to match the bounding box closely.
3001 NCollection_Array1<Standard_Real> aFitDistance (1, 6);
3002 aFitDistance.ChangeValue (1) = 0.0;
3003 aFitDistance.ChangeValue (2) = 0.0;
3004 aFitDistance.ChangeValue (3) = 0.0;
3005 aFitDistance.ChangeValue (4) = 0.0;
3006 aFitDistance.ChangeValue (5) = 0.0;
3007 aFitDistance.ChangeValue (6) = 0.0;
3009 for (Standard_Integer anI = aFrustumPlane.Lower(); anI <= aFrustumPlane.Upper(); ++anI)
3011 // Measure distances from center of bounding box to its corners towards the frustum plane.
3012 const gp_Dir& aPlaneN = aFrustumPlane.ChangeValue (anI).Axis().Direction();
3014 Standard_Real& aFitDist = aFitDistance.ChangeValue (anI);
3016 for (Standard_Integer aJ = aBndCorner.Lower(); aJ <= aBndCorner.Upper(); ++aJ)
3018 aFitDist = Max (aFitDist, gp_Vec (aBndCenter, aBndCorner (aJ)).Dot (aPlaneN));
3021 // The center of camera is placed on the same line with center of bounding box.
3022 // The view plane section crosses the bounding box at its center.
3023 // To compute view plane size, evaluate coefficients converting "point -> plane distance"
3024 // into view section size between the point and the frustum plane.
3026 // /|\ right half of frame //
3028 // point o<-- distance * coeff -->//---- (view plane section)
3037 aFitDistance.ChangeValue (1) *= Sqrt(1 + Pow (Tan ( aCamSide .Angle (aFrustumPlane (1).Axis().Direction())), 2.0));
3038 aFitDistance.ChangeValue (2) *= Sqrt(1 + Pow (Tan ((-aCamSide).Angle (aFrustumPlane (2).Axis().Direction())), 2.0));
3039 aFitDistance.ChangeValue (3) *= Sqrt(1 + Pow (Tan ( aCamUp .Angle (aFrustumPlane (3).Axis().Direction())), 2.0));
3040 aFitDistance.ChangeValue (4) *= Sqrt(1 + Pow (Tan ((-aCamUp) .Angle (aFrustumPlane (4).Axis().Direction())), 2.0));
3041 aFitDistance.ChangeValue (5) *= Sqrt(1 + Pow (Tan ( aCamDir .Angle (aFrustumPlane (5).Axis().Direction())), 2.0));
3042 aFitDistance.ChangeValue (6) *= Sqrt(1 + Pow (Tan ((-aCamDir) .Angle (aFrustumPlane (6).Axis().Direction())), 2.0));
3044 Standard_Real aViewSizeXv = aFitDistance (1) + aFitDistance (2);
3045 Standard_Real aViewSizeYv = aFitDistance (3) + aFitDistance (4);
3046 Standard_Real aViewSizeZv = aFitDistance (5) + aFitDistance (6);
3048 // 3. Place center of camera on the same line with center of bounding
3049 // box applying corresponding projection asymmetry (if any).
3050 Standard_Real anAssymXv = anAssymX * aViewSizeXv * 0.5;
3051 Standard_Real anAssymYv = anAssymY * aViewSizeYv * 0.5;
3052 Standard_Real anOffsetXv = (aFitDistance (2) - aFitDistance (1)) * 0.5 + anAssymXv;
3053 Standard_Real anOffsetYv = (aFitDistance (4) - aFitDistance (3)) * 0.5 + anAssymYv;
3054 gp_Vec aTranslateSide = gp_Vec (aCamSide) * anOffsetXv;
3055 gp_Vec aTranslateUp = gp_Vec (aCamUp) * anOffsetYv;
3056 gp_Pnt aCamNewCenter = aBndCenter.Translated (aTranslateSide).Translated (aTranslateUp);
3058 gp_Trsf aCenterTrsf;
3059 aCenterTrsf.SetTranslation (theCamera->Center(), aCamNewCenter);
3060 theCamera->Transform (aCenterTrsf);
3061 theCamera->SetDistance (aFitDistance (6) + aFitDistance (5));
3063 // Bounding box collapses to a point or thin line going in depth of the screen
3064 if (aViewSizeXv < theResolution && aViewSizeYv < theResolution)
3066 if (aViewSizeXv < theResolution || !theToEnlargeIfLine)
3068 return Standard_True; // This is just one point or line and zooming has no effect.
3071 // Looking along line and "theToEnlargeIfLine" is requested.
3072 // Fit view to see whole scene on rotation.
3073 aViewSizeXv = aViewSizeZv;
3074 aViewSizeYv = aViewSizeZv;
3077 Scale (theCamera, aViewSizeXv, aViewSizeYv);
3079 const Standard_Real aZoomCoef = myView->ConsiderZoomPersistenceObjects();
3081 Scale (theCamera, theCamera->ViewDimensions().X() * (aZoomCoef + theMargin), theCamera->ViewDimensions().Y() * (aZoomCoef + theMargin));
3083 return Standard_True;
3086 // =======================================================================
3088 // purpose : Internal
3089 // =======================================================================
3090 void V3d_View::Scale (const Handle(Graphic3d_Camera)& theCamera,
3091 const Standard_Real theSizeXv,
3092 const Standard_Real theSizeYv) const
3094 Standard_Real anAspect = theCamera->Aspect();
3097 theCamera->SetScale (Max (theSizeXv / anAspect, theSizeYv));
3101 theCamera->SetScale (Max (theSizeXv, theSizeYv * anAspect));
3105 // =======================================================================
3106 // function : Translate
3107 // purpose : Internal
3108 // =======================================================================
3109 void V3d_View::Translate (const Handle(Graphic3d_Camera)& theCamera,
3110 const Standard_Real theDXv,
3111 const Standard_Real theDYv) const
3113 const gp_Pnt& aCenter = theCamera->Center();
3114 const gp_Dir& aDir = theCamera->Direction();
3115 const gp_Dir& anUp = theCamera->Up();
3116 gp_Ax3 aCameraCS (aCenter, aDir.Reversed(), aDir ^ anUp);
3118 gp_Vec aCameraPanXv = gp_Vec (aCameraCS.XDirection()) * theDXv;
3119 gp_Vec aCameraPanYv = gp_Vec (aCameraCS.YDirection()) * theDYv;
3120 gp_Vec aCameraPan = aCameraPanXv + aCameraPanYv;
3122 aPanTrsf.SetTranslation (aCameraPan);
3124 theCamera->Transform (aPanTrsf);
3127 // =======================================================================
3128 // function : DiagnosticInformation
3130 // =======================================================================
3131 void V3d_View::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
3132 Graphic3d_DiagnosticInfo theFlags) const
3134 myView->DiagnosticInformation (theDict, theFlags);
3137 //=======================================================================
3138 //function : StatisticInformation
3140 //=======================================================================
3141 void V3d_View::StatisticInformation (TColStd_IndexedDataMapOfStringString& theDict) const
3143 myView->StatisticInformation (theDict);
3146 // =======================================================================
3147 // function : StatisticInformation
3149 // =======================================================================
3150 TCollection_AsciiString V3d_View::StatisticInformation() const
3152 return myView->StatisticInformation();
3155 //=============================================================================
3156 //function : RenderingParams
3158 //=============================================================================
3159 const Graphic3d_RenderingParams& V3d_View::RenderingParams() const
3161 return myView->RenderingParams();
3164 //=============================================================================
3165 //function : ChangeRenderingParams
3167 //=============================================================================
3168 Graphic3d_RenderingParams& V3d_View::ChangeRenderingParams()
3170 return myView->ChangeRenderingParams();