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 <Graphic3d_Vector.hxx>
31 #include <Image_AlienPixMap.hxx>
32 #include <Message.hxx>
33 #include <Message_Messenger.hxx>
34 #include <NCollection_Array1.hxx>
35 #include <Precision.hxx>
36 #include <Quantity_Color.hxx>
37 #include <Standard_Assert.hxx>
38 #include <Standard_DivideByZero.hxx>
39 #include <Standard_ErrorHandler.hxx>
40 #include <Standard_MultiplyDefined.hxx>
41 #include <Standard_ShortReal.hxx>
42 #include <Standard_Type.hxx>
43 #include <Standard_TypeMismatch.hxx>
44 #include <TColgp_Array1OfPnt.hxx>
45 #include <TColStd_Array2OfReal.hxx>
46 #include <TColStd_HSequenceOfInteger.hxx>
48 #include <V3d_BadValue.hxx>
49 #include <V3d_Light.hxx>
50 #include <V3d_StereoDumpOptions.hxx>
51 #include <V3d_UnMapped.hxx>
52 #include <V3d_Viewer.hxx>
54 IMPLEMENT_STANDARD_RTTIEXT(V3d_View,MMgt_TShared)
56 #define DEUXPI (2. * M_PI)
60 static const Standard_Integer THE_NB_BOUND_POINTS = 8;
63 //=============================================================================
64 //function : Constructor
66 //=============================================================================
67 V3d_View::V3d_View (const Handle(V3d_Viewer)& theViewer, const V3d_TypeOfView theType)
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 : MyViewer (theViewer.operator->()),
119 SwitchSetFront(Standard_False),
120 myZRotation (Standard_False),
123 myView = theViewer->Driver()->CreateView (theViewer->StructureManager());
125 myView->CopySettings (theView->View());
127 myDefaultCamera = new Graphic3d_Camera();
129 myImmediateUpdate = Standard_False;
130 SetAutoZFitMode (theView->AutoZFitMode(), theView->AutoZFitScaleFactor());
131 SetAxis (0.0, 0.0, 0.0, 1.0, 1.0, 1.0);
132 SetViewMappingDefault();
133 SetViewOrientationDefault();
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);
197 //=============================================================================
200 //=============================================================================
201 void V3d_View::Remove() const
203 MyViewer->DelView (this);
205 Handle(Aspect_Window)& aWin = const_cast<Handle(Aspect_Window)&> (MyWindow);
209 //=============================================================================
212 //=============================================================================
213 void V3d_View::Update() const
215 if (!myView->IsDefined()
216 || !myView->IsActive())
226 //=============================================================================
229 //=============================================================================
230 void V3d_View::Redraw() const
232 if (!myView->IsDefined()
233 || !myView->IsActive())
238 Handle(Graphic3d_GraphicDriver) aGraphicDriver = MyViewer->Driver();
239 Handle(Graphic3d_StructureManager) aStructureMgr = MyViewer->StructureManager();
240 for (Standard_Integer aRetryIter = 0; aRetryIter < 2; ++aRetryIter)
242 if (aGraphicDriver->IsDeviceLost())
244 aStructureMgr->RecomputeStructures();
245 aGraphicDriver->ResetDeviceLostFlag();
252 if (!aGraphicDriver->IsDeviceLost())
259 //=============================================================================
260 //function : RedrawImmediate
262 //=============================================================================
263 void V3d_View::RedrawImmediate() const
265 if (!myView->IsDefined()
266 || !myView->IsActive())
271 myView->RedrawImmediate();
274 //=============================================================================
275 //function : Invalidate
277 //=============================================================================
278 void V3d_View::Invalidate() const
280 if (!myView->IsDefined())
285 myView->Invalidate();
288 //=============================================================================
289 //function : IsInvalidated
291 //=============================================================================
292 Standard_Boolean V3d_View::IsInvalidated() const
294 return !myView->IsDefined()
295 || myView->IsInvalidated();
298 // ========================================================================
299 // function : SetAutoZFitMode
301 // ========================================================================
302 void V3d_View::SetAutoZFitMode (const Standard_Boolean theIsOn,
303 const Standard_Real theScaleFactor)
305 Standard_ASSERT_RAISE (theScaleFactor > 0.0, "Zero or negative scale factor is not allowed.");
306 myAutoZFitScaleFactor = theScaleFactor;
307 myAutoZFitIsOn = theIsOn;
310 // ========================================================================
311 // function : AutoZFitMode
313 // ========================================================================
314 Standard_Boolean V3d_View::AutoZFitMode() const
316 return myAutoZFitIsOn;
319 // ========================================================================
320 // function : AutoZFitScaleFactor
322 // ========================================================================
323 Standard_Real V3d_View::AutoZFitScaleFactor() const
325 return myAutoZFitScaleFactor;
328 //=============================================================================
329 //function : AutoZFit
331 //=============================================================================
332 void V3d_View::AutoZFit() const
339 ZFitAll (myAutoZFitScaleFactor);
342 //=============================================================================
345 //=============================================================================
346 void V3d_View::ZFitAll (const Standard_Real theScaleFactor) const
348 Bnd_Box aMinMaxBox = myView->MinMaxValues (Standard_False); // applicative min max boundaries
349 Bnd_Box aGraphicBox = myView->MinMaxValues (Standard_True); // real graphical boundaries (not accounting infinite flag).
351 myView->Camera()->ZFitAll (theScaleFactor, aMinMaxBox, aGraphicBox);
354 //=============================================================================
357 //=============================================================================
358 Standard_Boolean V3d_View::IsEmpty() const
360 Standard_Boolean TheStatus = Standard_True ;
361 if( myView->IsDefined() ) {
362 Standard_Integer Nstruct = myView->NumberOfDisplayedStructures() ;
363 if( Nstruct > 0 ) TheStatus = Standard_False ;
368 //=============================================================================
369 //function : UpdateLights
371 //=============================================================================
372 void V3d_View::UpdateLights() const
374 Graphic3d_ListOfCLight aLights;
375 for (V3d_ListOfLight::Iterator anActiveLightIter (myActiveLights); anActiveLightIter.More(); anActiveLightIter.Next())
377 aLights.Append (anActiveLightIter.Value()->Light());
379 myView->SetLights (aLights);
383 //=============================================================================
384 //function : DoMapping
386 //=============================================================================
387 void V3d_View::DoMapping()
389 if (!myView->IsDefined())
394 myView->Window()->DoMapping();
397 //=============================================================================
398 //function : MustBeResized
400 //=============================================================================
401 void V3d_View::MustBeResized()
403 if (!myView->IsDefined())
415 //=============================================================================
416 //function : SetBackgroundColor
418 //=============================================================================
419 void V3d_View::SetBackgroundColor (const Quantity_TypeOfColor theType,
420 const Standard_Real theV1,
421 const Standard_Real theV2,
422 const Standard_Real theV3)
424 Standard_Real aV1 = Max (Min (theV1, 1.0), 0.0);
425 Standard_Real aV2 = Max (Min (theV2, 1.0), 0.0);
426 Standard_Real aV3 = Max (Min (theV3, 1.0), 0.0);
428 SetBackgroundColor (Quantity_Color (aV1, aV2, aV3, theType));
431 //=============================================================================
432 //function : SetBackgroundColor
434 //=============================================================================
435 void V3d_View::SetBackgroundColor (const Quantity_Color& theColor)
437 myView->SetBackground (Aspect_Background (theColor));
439 if (myImmediateUpdate)
445 //=============================================================================
446 //function : SetBgGradientColors
448 //=============================================================================
449 void V3d_View::SetBgGradientColors (const Quantity_Color& theColor1,
450 const Quantity_Color& theColor2,
451 const Aspect_GradientFillMethod theFillStyle,
452 const Standard_Boolean theToUpdate)
454 Aspect_GradientBackground aGradientBg (theColor1, theColor2, theFillStyle);
456 myView->SetGradientBackground (aGradientBg);
458 if (myImmediateUpdate || theToUpdate)
464 //=============================================================================
465 //function : SetBgGradientStyle
467 //=============================================================================
468 void V3d_View::SetBgGradientStyle (const Aspect_GradientFillMethod theFillStyle, const Standard_Boolean theToUpdate)
470 Quantity_Color aColor1;
471 Quantity_Color aColor2;
472 GradientBackground().Colors (aColor1, aColor2);
474 SetBgGradientColors (aColor1, aColor2, theFillStyle, theToUpdate);
477 //=============================================================================
478 //function : SetBackgroundImage
480 //=============================================================================
481 void V3d_View::SetBackgroundImage (const Standard_CString theFileName,
482 const Aspect_FillMethod theFillStyle,
483 const Standard_Boolean theToUpdate)
485 myView->SetBackgroundImage (theFileName);
486 myView->SetBackgroundImageStyle (theFillStyle);
488 if (myImmediateUpdate || theToUpdate)
494 //=============================================================================
495 //function : SetBgImageStyle
497 //=============================================================================
498 void V3d_View::SetBgImageStyle (const Aspect_FillMethod theFillStyle, const Standard_Boolean theToUpdate)
500 myView->SetBackgroundImageStyle (theFillStyle);
502 if (myImmediateUpdate || theToUpdate)
508 //=============================================================================
511 //=============================================================================
512 void V3d_View::SetAxis(const Standard_Real X, const Standard_Real Y, const Standard_Real Z, const Standard_Real Vx, const Standard_Real Vy, const Standard_Real Vz)
514 Standard_Real D,Nx = Vx,Ny = Vy,Nz = Vz ;
516 D = Sqrt( Vx*Vx + Vy*Vy + Vz*Vz ) ;
517 V3d_BadValue_Raise_if ( D <= 0. , "V3d_View::SetAxis, bad axis");
518 Nx /= D ; Ny /= D ; Nz /= D ;
519 MyDefaultViewPoint.SetCoord(X,Y,Z) ;
520 MyDefaultViewAxis.SetCoord(Nx,Ny,Nz) ;
523 //=============================================================================
524 //function : SetShadingModel
526 //=============================================================================
527 void V3d_View::SetShadingModel (const V3d_TypeOfShadingModel theShadingModel)
529 myView->SetShadingModel (static_cast<Graphic3d_TypeOfShadingModel> (theShadingModel));
532 //=============================================================================
533 //function : SetTextureEnv
535 //=============================================================================
536 void V3d_View::SetTextureEnv (const Handle(Graphic3d_TextureEnv)& theTexture)
538 myView->SetTextureEnv (theTexture);
540 if (myImmediateUpdate)
546 //=============================================================================
547 //function : SetVisualization
549 //=============================================================================
550 void V3d_View::SetVisualization (const V3d_TypeOfVisualization theType)
552 myView->SetVisualizationType (static_cast <Graphic3d_TypeOfVisualization> (theType));
554 if (myImmediateUpdate)
560 //=============================================================================
561 //function : SetFront
563 //=============================================================================
564 void V3d_View::SetFront()
566 gp_Ax3 a = MyViewer->PrivilegedPlane();
567 Standard_Real xo, yo, zo, vx, vy, vz, xu, yu, zu;
569 a.Direction().Coord(vx,vy,vz);
570 a.YDirection().Coord(xu,yu,zu);
571 a.Location().Coord(xo,yo,zo);
573 Handle(Graphic3d_Camera) aCamera = Camera();
575 aCamera->SetCenter (gp_Pnt (xo, yo, zo));
579 aCamera->SetDirection (gp_Dir (vx, vy, vz));
583 aCamera->SetDirection (gp_Dir (vx, vy, vz).Reversed());
586 aCamera->SetUp (gp_Dir (xu, yu, zu));
590 SwitchSetFront = !SwitchSetFront;
595 //=============================================================================
598 //=============================================================================
599 void V3d_View::Rotate (const Standard_Real ax,
600 const Standard_Real ay,
601 const Standard_Real az,
602 const Standard_Boolean Start)
604 Standard_Real Ax = ax;
605 Standard_Real Ay = ay;
606 Standard_Real Az = az;
608 if( Ax > 0. ) while ( Ax > DEUXPI ) Ax -= DEUXPI;
609 else if( Ax < 0. ) while ( Ax < -DEUXPI ) Ax += DEUXPI;
610 if( Ay > 0. ) while ( Ay > DEUXPI ) Ay -= DEUXPI;
611 else if( Ay < 0. ) while ( Ay < -DEUXPI ) Ay += DEUXPI;
612 if( Az > 0. ) while ( Az > DEUXPI ) Az -= DEUXPI;
613 else if( Az < 0. ) while ( Az < -DEUXPI ) Az += DEUXPI;
615 Handle(Graphic3d_Camera) aCamera = Camera();
619 myCamStartOpUp = aCamera->Up();
620 myCamStartOpEye = aCamera->Eye();
621 myCamStartOpCenter = aCamera->Center();
624 aCamera->SetUp (myCamStartOpUp);
625 aCamera->SetEye (myCamStartOpEye);
626 aCamera->SetCenter (myCamStartOpCenter);
628 // rotate camera around 3 initial axes
629 gp_Dir aBackDir (gp_Vec (myCamStartOpCenter, myCamStartOpEye));
630 gp_Dir aXAxis (myCamStartOpUp.Crossed (aBackDir));
631 gp_Dir aYAxis (aBackDir.Crossed (aXAxis));
632 gp_Dir aZAxis (aXAxis.Crossed (aYAxis));
634 gp_Trsf aRot[3], aTrsf;
635 aRot[0].SetRotation (gp_Ax1 (myCamStartOpCenter, aYAxis), -Ax);
636 aRot[1].SetRotation (gp_Ax1 (myCamStartOpCenter, aXAxis), Ay);
637 aRot[2].SetRotation (gp_Ax1 (myCamStartOpCenter, aZAxis), Az);
638 aTrsf.Multiply (aRot[0]);
639 aTrsf.Multiply (aRot[1]);
640 aTrsf.Multiply (aRot[2]);
642 aCamera->Transform (aTrsf);
649 //=============================================================================
652 //=============================================================================
653 void V3d_View::Rotate(const Standard_Real ax, const Standard_Real ay, const Standard_Real az,
654 const Standard_Real X, const Standard_Real Y, const Standard_Real Z, const Standard_Boolean Start)
657 Standard_Real Ax = ax ;
658 Standard_Real Ay = ay ;
659 Standard_Real Az = az ;
661 if( Ax > 0. ) while ( Ax > DEUXPI ) Ax -= DEUXPI ;
662 else if( Ax < 0. ) while ( Ax < -DEUXPI ) Ax += DEUXPI ;
663 if( Ay > 0. ) while ( Ay > DEUXPI ) Ay -= DEUXPI ;
664 else if( Ay < 0. ) while ( Ay < -DEUXPI ) Ay += DEUXPI ;
665 if( Az > 0. ) while ( Az > DEUXPI ) Az -= DEUXPI ;
666 else if( Az < 0. ) while ( Az < -DEUXPI ) Az += DEUXPI ;
668 Handle(Graphic3d_Camera) aCamera = Camera();
672 myGravityReferencePoint.SetCoord (X, Y, Z);
673 myCamStartOpUp = aCamera->Up();
674 myCamStartOpEye = aCamera->Eye();
675 myCamStartOpCenter = aCamera->Center();
678 const Graphic3d_Vertex& aVref = myGravityReferencePoint;
680 aCamera->SetUp (myCamStartOpUp);
681 aCamera->SetEye (myCamStartOpEye);
682 aCamera->SetCenter (myCamStartOpCenter);
684 // rotate camera around 3 initial axes
685 gp_Pnt aRCenter (aVref.X(), aVref.Y(), aVref.Z());
687 gp_Dir aZAxis (aCamera->Direction().Reversed());
688 gp_Dir aYAxis (aCamera->Up());
689 gp_Dir aXAxis (aYAxis.Crossed (aZAxis));
691 gp_Trsf aRot[3], aTrsf;
692 aRot[0].SetRotation (gp_Ax1 (aRCenter, aYAxis), -Ax);
693 aRot[1].SetRotation (gp_Ax1 (aRCenter, aXAxis), Ay);
694 aRot[2].SetRotation (gp_Ax1 (aRCenter, aZAxis), Az);
695 aTrsf.Multiply (aRot[0]);
696 aTrsf.Multiply (aRot[1]);
697 aTrsf.Multiply (aRot[2]);
699 aCamera->Transform (aTrsf);
706 //=============================================================================
709 //=============================================================================
710 void V3d_View::Rotate(const V3d_TypeOfAxe Axe, const Standard_Real angle, const Standard_Boolean Start)
714 Rotate(angle,0.,0.,Start);
717 Rotate(0.,angle,0.,Start);
720 Rotate(0.,0.,angle,Start);
725 //=============================================================================
728 //=============================================================================
729 void V3d_View::Rotate(const V3d_TypeOfAxe Axe, const Standard_Real angle,
730 const Standard_Real X, const Standard_Real Y, const Standard_Real Z, const Standard_Boolean Start)
732 Standard_Real Angle = angle ;
734 if( Angle > 0. ) while ( Angle > DEUXPI ) Angle -= DEUXPI ;
735 else if( Angle < 0. ) while ( Angle < -DEUXPI ) Angle += DEUXPI ;
737 Handle(Graphic3d_Camera) aCamera = Camera();
741 myGravityReferencePoint.SetCoord (X, Y, Z);
742 myCamStartOpUp = aCamera->Up();
743 myCamStartOpEye = aCamera->Eye();
744 myCamStartOpCenter = aCamera->Center();
748 myViewAxis.SetCoord(1.,0.,0.) ;
751 myViewAxis.SetCoord(0.,1.,0.) ;
754 myViewAxis.SetCoord(0.,0.,1.) ;
758 myCamStartOpUp = aCamera->Up();
759 myCamStartOpEye = aCamera->Eye();
760 myCamStartOpCenter = aCamera->Center();
763 const Graphic3d_Vertex& aVref = myGravityReferencePoint;
765 aCamera->SetUp (myCamStartOpUp);
766 aCamera->SetEye (myCamStartOpEye);
767 aCamera->SetCenter (myCamStartOpCenter);
769 // rotate camera around passed axis
771 gp_Pnt aRCenter (aVref.X(), aVref.Y(), aVref.Z());
772 gp_Dir aRAxis ((Axe == V3d_X) ? 1.0 : 0.0,
773 (Axe == V3d_Y) ? 1.0 : 0.0,
774 (Axe == V3d_Z) ? 1.0 : 0.0);
776 aRotation.SetRotation (gp_Ax1 (aRCenter, aRAxis), Angle);
778 aCamera->Transform (aRotation);
785 //=============================================================================
788 //=============================================================================
789 void V3d_View::Rotate(const Standard_Real angle, const Standard_Boolean Start)
791 Standard_Real Angle = angle;
793 if( Angle > 0. ) while ( Angle > DEUXPI ) Angle -= DEUXPI ;
794 else if( Angle < 0. ) while ( Angle < -DEUXPI ) Angle += DEUXPI ;
796 Handle(Graphic3d_Camera) aCamera = Camera();
799 myCamStartOpUp = aCamera->Up();
800 myCamStartOpEye = aCamera->Eye();
801 myCamStartOpCenter = aCamera->Center();
804 const Graphic3d_Vertex& aPnt = MyDefaultViewPoint;
805 const Graphic3d_Vector& anAxis = MyDefaultViewAxis;
807 aCamera->SetUp (myCamStartOpUp);
808 aCamera->SetEye (myCamStartOpEye);
809 aCamera->SetCenter (myCamStartOpCenter);
812 gp_Pnt aRCenter (aPnt.X(), aPnt.Y(), aPnt.Z());
813 gp_Dir aRAxis (anAxis.X(), anAxis.Y(), anAxis.Z());
814 aRotation.SetRotation (gp_Ax1 (aRCenter, aRAxis), Angle);
816 aCamera->Transform (aRotation);
823 //=============================================================================
826 //=============================================================================
827 void V3d_View::Turn(const Standard_Real ax, const Standard_Real ay, const Standard_Real az, const Standard_Boolean Start)
829 Standard_Real Ax = ax;
830 Standard_Real Ay = ay;
831 Standard_Real Az = az;
833 if( Ax > 0. ) while ( Ax > DEUXPI ) Ax -= DEUXPI ;
834 else if( Ax < 0. ) while ( Ax < -DEUXPI ) Ax += DEUXPI ;
835 if( Ay > 0. ) while ( Ay > DEUXPI ) Ay -= DEUXPI ;
836 else if( Ay < 0. ) while ( Ay < -DEUXPI ) Ay += DEUXPI ;
837 if( Az > 0. ) while ( Az > DEUXPI ) Az -= DEUXPI ;
838 else if( Az < 0. ) while ( Az < -DEUXPI ) Az += DEUXPI ;
840 Handle(Graphic3d_Camera) aCamera = Camera();
843 myCamStartOpUp = aCamera->Up();
844 myCamStartOpEye = aCamera->Eye();
845 myCamStartOpCenter = aCamera->Center();
848 aCamera->SetUp (myCamStartOpUp);
849 aCamera->SetEye (myCamStartOpEye);
850 aCamera->SetCenter (myCamStartOpCenter);
852 // rotate camera around 3 initial axes
853 gp_Pnt aRCenter = aCamera->Eye();
854 gp_Dir aZAxis (aCamera->Direction().Reversed());
855 gp_Dir aYAxis (aCamera->Up());
856 gp_Dir aXAxis (aYAxis.Crossed (aZAxis));
858 gp_Trsf aRot[3], aTrsf;
859 aRot[0].SetRotation (gp_Ax1 (aRCenter, aYAxis), -Ax);
860 aRot[1].SetRotation (gp_Ax1 (aRCenter, aXAxis), Ay);
861 aRot[2].SetRotation (gp_Ax1 (aRCenter, aZAxis), Az);
862 aTrsf.Multiply (aRot[0]);
863 aTrsf.Multiply (aRot[1]);
864 aTrsf.Multiply (aRot[2]);
866 aCamera->Transform (aTrsf);
873 //=============================================================================
876 //=============================================================================
877 void V3d_View::Turn(const V3d_TypeOfAxe Axe, const Standard_Real angle, const Standard_Boolean Start)
881 Turn(angle,0.,0.,Start);
884 Turn(0.,angle,0.,Start);
887 Turn(0.,0.,angle,Start);
892 //=============================================================================
895 //=============================================================================
896 void V3d_View::Turn(const Standard_Real angle, const Standard_Boolean Start)
898 Standard_Real Angle = angle ;
900 if( Angle > 0. ) while ( Angle > DEUXPI ) Angle -= DEUXPI ;
901 else if( Angle < 0. ) while ( Angle < -DEUXPI ) Angle += DEUXPI ;
903 Handle(Graphic3d_Camera) aCamera = Camera();
906 myCamStartOpUp = aCamera->Up();
907 myCamStartOpEye = aCamera->Eye();
908 myCamStartOpCenter = aCamera->Center();
911 aCamera->SetUp (myCamStartOpUp);
912 aCamera->SetEye (myCamStartOpEye);
913 aCamera->SetCenter (myCamStartOpCenter);
915 const Graphic3d_Vector& anAxis = MyDefaultViewAxis;
918 gp_Pnt aRCenter = aCamera->Eye();
919 gp_Dir aRAxis (anAxis.X(), anAxis.Y(), anAxis.Z());
920 aRotation.SetRotation (gp_Ax1 (aRCenter, aRAxis), Angle);
922 aCamera->Transform (aRotation);
929 //=============================================================================
930 //function : SetTwist
932 //=============================================================================
933 void V3d_View::SetTwist(const Standard_Real angle)
935 Standard_Real Angle = angle ;
936 Standard_Boolean TheStatus;
938 if( Angle > 0. ) while ( Angle > DEUXPI ) Angle -= DEUXPI ;
939 else if( Angle < 0. ) while ( Angle < -DEUXPI ) Angle += DEUXPI ;
941 Handle(Graphic3d_Camera) aCamera = Camera();
943 gp_Dir aReferencePlane (aCamera->Direction().Reversed());
946 anUp = gp_Dir (0.0, 0.0, 1.0);
948 TheStatus = ScreenAxis(aReferencePlane, anUp,
949 myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
951 anUp = gp_Dir (0.0, 1.0, 0.0);
952 TheStatus = ScreenAxis(aReferencePlane, anUp,
953 myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
956 anUp = gp_Dir (1.0, 0.0, 0.0);
957 TheStatus = ScreenAxis(aReferencePlane, anUp,
958 myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
961 V3d_BadValue_Raise_if( !TheStatus,"V3d_ViewSetTwist, alignment of Eye,At,Up,");
963 gp_Pnt aRCenter = aCamera->Center();
964 gp_Dir aZAxis (aCamera->Direction().Reversed());
967 aTrsf.SetRotation (gp_Ax1 (aRCenter, aZAxis), Angle);
969 Standard_Real myYscreenAxisX, myYscreenAxisY, myYscreenAxisZ;
970 myYscreenAxis.Coord (myYscreenAxisX, myYscreenAxisY, myYscreenAxisZ);
972 aCamera->SetUp (gp_Dir (myYscreenAxisX, myYscreenAxisY, myYscreenAxisZ));
973 aCamera->Transform (aTrsf);
980 //=============================================================================
983 //=============================================================================
984 void V3d_View::SetEye(const Standard_Real X,const Standard_Real Y,const Standard_Real Z)
986 Standard_Real aTwistBefore = Twist();
988 Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
990 Handle(Graphic3d_Camera) aCamera = Camera();
992 aCamera->SetEye (gp_Pnt (X, Y, Z));
994 SetTwist (aTwistBefore);
998 SetImmediateUpdate (wasUpdateEnabled);
1003 //=============================================================================
1004 //function : SetDepth
1006 //=============================================================================
1007 void V3d_View::SetDepth(const Standard_Real Depth)
1009 V3d_BadValue_Raise_if (Depth == 0. ,"V3d_View::SetDepth, bad depth");
1011 Handle(Graphic3d_Camera) aCamera = Camera();
1015 // Move eye using center (target) as anchor.
1016 aCamera->SetDistance (Depth);
1020 // Move the view ref point instead of the eye.
1021 gp_Vec aDir (aCamera->Direction());
1022 gp_Pnt aCameraEye = aCamera->Eye();
1023 gp_Pnt aCameraCenter = aCameraEye.Translated (aDir.Multiplied (Abs (Depth)));
1025 aCamera->SetCenter (aCameraCenter);
1033 //=============================================================================
1034 //function : SetProj
1036 //=============================================================================
1037 void V3d_View::SetProj( const Standard_Real Vx,const Standard_Real Vy, const Standard_Real Vz )
1039 V3d_BadValue_Raise_if( Sqrt(Vx*Vx + Vy*Vy + Vz*Vz) <= 0.,
1040 "V3d_View::SetProj, null projection vector");
1042 Standard_Real aTwistBefore = Twist();
1044 Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
1046 Camera()->SetDirection (gp_Dir (Vx, Vy, Vz).Reversed());
1048 SetTwist(aTwistBefore);
1052 SetImmediateUpdate (wasUpdateEnabled);
1057 //=============================================================================
1058 //function : SetProj
1060 //=============================================================================
1061 void V3d_View::SetProj( const V3d_TypeOfOrientation Orientation )
1063 Standard_Real Xpn=0;
1064 Standard_Real Ypn=0;
1065 Standard_Real Zpn=0;
1067 switch (Orientation) {
1078 const Graphic3d_Vector& aBck = V3d::GetProjAxis (Orientation);
1080 // retain camera panning from origin when switching projection
1081 Handle(Graphic3d_Camera) aCamera = Camera();
1083 gp_Pnt anOriginVCS = aCamera->ConvertWorld2View (gp::Origin());
1084 Standard_Real aPanX = anOriginVCS.X();
1085 Standard_Real aPanY = anOriginVCS.Y();
1087 aCamera->SetCenter (gp_Pnt (0, 0, 0));
1088 aCamera->SetDirection (gp_Dir (aBck.X(), aBck.Y(), aBck.Z()).Reversed());
1089 aCamera->SetUp (gp_Dir (Xpn, Ypn, Zpn));
1090 aCamera->OrthogonalizeUp();
1092 Panning (aPanX, aPanY);
1099 //=============================================================================
1102 //=============================================================================
1103 void V3d_View::SetAt(const Standard_Real X,const Standard_Real Y,const Standard_Real Z)
1105 Standard_Real aTwistBefore = Twist();
1107 Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
1109 Camera()->SetCenter (gp_Pnt (X, Y, Z));
1111 SetTwist (aTwistBefore);
1115 SetImmediateUpdate (wasUpdateEnabled);
1120 //=============================================================================
1123 //=============================================================================
1124 void V3d_View::SetUp(const Standard_Real Vx,const Standard_Real Vy,const Standard_Real Vz)
1126 Standard_Boolean TheStatus ;
1127 V3d_BadValue_Raise_if( Sqrt(Vx*Vx + Vy*Vy + Vz*Vz) <= 0. ,
1128 "V3d_View::SetUp, nullUp vector");
1130 Handle(Graphic3d_Camera) aCamera = Camera();
1132 gp_Dir aReferencePlane (aCamera->Direction().Reversed());
1133 gp_Dir anUp (Vx, Vy, Vz);
1135 TheStatus = ScreenAxis(aReferencePlane,anUp,
1136 myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
1138 anUp = gp_Dir (0.0, 0.0, 1.0);
1139 TheStatus = ScreenAxis(aReferencePlane,anUp,
1140 myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
1143 anUp = gp_Dir (0.0, 1.0, 0.0);
1144 TheStatus = ScreenAxis(aReferencePlane,anUp,
1145 myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
1148 anUp = gp_Dir (1.0, 0.0, 0.0);
1149 TheStatus = ScreenAxis(aReferencePlane,anUp,
1150 myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
1152 V3d_BadValue_Raise_if( !TheStatus,"V3d_View::Setup, alignment of Eye,At,Up");
1154 Standard_Real myYscreenAxisX, myYscreenAxisY, myYscreenAxisZ;
1155 myYscreenAxis.Coord (myYscreenAxisX, myYscreenAxisY, myYscreenAxisZ);
1157 aCamera->SetUp (gp_Dir (myYscreenAxisX, myYscreenAxisY, myYscreenAxisZ));
1164 //=============================================================================
1167 //=============================================================================
1168 void V3d_View::SetUp( const V3d_TypeOfOrientation Orientation )
1170 Standard_Boolean TheStatus ;
1172 Handle(Graphic3d_Camera) aCamera = Camera();
1174 gp_Dir aReferencePlane (aCamera->Direction().Reversed());
1177 const Graphic3d_Vector& aViewReferenceUp = V3d::GetProjAxis(Orientation) ;
1178 anUp = gp_Dir (aViewReferenceUp.X(), aViewReferenceUp.Y(), aViewReferenceUp.Z());
1180 TheStatus = ScreenAxis(aReferencePlane,anUp,
1181 myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
1183 anUp = gp_Dir (0.,0.,1.);
1184 TheStatus = ScreenAxis(aReferencePlane,anUp,
1185 myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
1188 anUp = gp_Dir (0.,1.,0.);
1189 TheStatus = ScreenAxis(aReferencePlane,anUp,
1190 myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
1193 anUp = gp_Dir (1.,0.,0.);
1194 TheStatus = ScreenAxis(aReferencePlane,anUp,
1195 myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
1197 V3d_BadValue_Raise_if( !TheStatus, "V3d_View::SetUp, alignment of Eye,At,Up");
1199 Standard_Real myYscreenAxisX, myYscreenAxisY, myYscreenAxisZ;
1200 myYscreenAxis.Coord (myYscreenAxisX, myYscreenAxisY, myYscreenAxisZ);
1202 aCamera->SetUp (gp_Dir (myYscreenAxisX, myYscreenAxisY, myYscreenAxisZ));
1209 //=============================================================================
1210 //function : SetViewOrientationDefault
1212 //=============================================================================
1213 void V3d_View::SetViewOrientationDefault()
1215 myDefaultCamera->CopyOrientationData (Camera());
1218 //=======================================================================
1219 //function : SetViewMappingDefault
1221 //=======================================================================
1222 void V3d_View::SetViewMappingDefault()
1224 myDefaultCamera->CopyMappingData (Camera());
1227 //=============================================================================
1228 //function : ResetViewOrientation
1230 //=============================================================================
1231 void V3d_View::ResetViewOrientation()
1233 Camera()->CopyOrientationData (myDefaultCamera);
1240 //=======================================================================
1241 //function : ResetViewMapping
1243 //=======================================================================
1244 void V3d_View::ResetViewMapping()
1246 Camera()->CopyMappingData (myDefaultCamera);
1253 //=============================================================================
1256 //=============================================================================
1257 void V3d_View::Reset (const Standard_Boolean theToUpdate)
1259 Camera()->Copy (myDefaultCamera);
1263 SwitchSetFront = Standard_False;
1265 if (myImmediateUpdate || theToUpdate)
1271 //=======================================================================
1272 //function : SetCenter
1274 //=======================================================================
1275 void V3d_View::SetCenter (const Standard_Integer theXp,
1276 const Standard_Integer theYp)
1278 Standard_Real aXv, aYv;
1279 Convert (theXp, theYp, aXv, aYv);
1280 Translate (Camera(), aXv, aYv);
1285 //=============================================================================
1286 //function : SetSize
1288 //=============================================================================
1289 void V3d_View::SetSize (const Standard_Real theSize)
1291 V3d_BadValue_Raise_if (theSize <= 0.0, "V3d_View::SetSize, Window Size is NULL");
1293 Handle(Graphic3d_Camera) aCamera = Camera();
1295 aCamera->SetScale (aCamera->Aspect() >= 1.0 ? theSize / aCamera->Aspect() : theSize);
1302 //=============================================================================
1303 //function : SetZSize
1305 //=============================================================================
1306 void V3d_View::SetZSize (const Standard_Real theSize)
1308 Handle(Graphic3d_Camera) aCamera = Camera();
1310 Standard_Real Zmax = theSize / 2.;
1312 Standard_Real aDistance = aCamera->Distance();
1319 // ShortReal precision factor used to add meaningful tolerance to
1320 // ZNear, ZFar values in order to avoid equality after type conversion
1321 // to ShortReal matrices type.
1322 const Standard_Real aPrecision = 1.0 / Pow (10.0, ShortRealDigits() - 1);
1324 Standard_Real aZFar = Zmax + aDistance * 2.0;
1325 Standard_Real aZNear = -Zmax + aDistance;
1326 aZNear -= Abs (aZNear) * aPrecision;
1327 aZFar += Abs (aZFar) * aPrecision;
1329 if (!aCamera->IsOrthographic())
1331 if (aZFar < aPrecision)
1333 // Invalid case when both values are negative
1334 aZNear = aPrecision;
1335 aZFar = aPrecision * 2.0;
1337 else if (aZNear < Abs (aZFar) * aPrecision)
1339 // Z is less than 0.0, try to fix it using any appropriate z-scale
1340 aZNear = Abs (aZFar) * aPrecision;
1344 // If range is too small
1345 if (aZFar < (aZNear + Abs (aZFar) * aPrecision))
1347 aZFar = aZNear + Abs (aZFar) * aPrecision;
1350 aCamera->SetZRange (aZNear, aZFar);
1352 if (myImmediateUpdate)
1358 //=============================================================================
1359 //function : SetZoom
1361 //=============================================================================
1362 void V3d_View::SetZoom (const Standard_Real theCoef,const Standard_Boolean theToStart)
1364 V3d_BadValue_Raise_if (theCoef <= 0., "V3d_View::SetZoom, bad coefficient");
1366 Handle(Graphic3d_Camera) aCamera = Camera();
1370 myCamStartOpEye = aCamera->Eye();
1371 myCamStartOpCenter = aCamera->Center();
1374 Standard_Real aViewWidth = aCamera->ViewDimensions().X();
1375 Standard_Real aViewHeight = aCamera->ViewDimensions().Y();
1377 // ensure that zoom will not be too small or too big
1378 Standard_Real aCoef = theCoef;
1379 if (aViewWidth < aCoef * Precision::Confusion())
1381 aCoef = aViewWidth / Precision::Confusion();
1383 else if (aViewWidth > aCoef * 1e12)
1385 aCoef = aViewWidth / 1e12;
1387 if (aViewHeight < aCoef * Precision::Confusion())
1389 aCoef = aViewHeight / Precision::Confusion();
1391 else if (aViewHeight > aCoef * 1e12)
1393 aCoef = aViewHeight / 1e12;
1396 aCamera->SetEye (myCamStartOpEye);
1397 aCamera->SetCenter (myCamStartOpCenter);
1398 aCamera->SetScale (aCamera->Scale() / aCoef);
1405 //=============================================================================
1406 //function : SetScale
1408 //=============================================================================
1409 void V3d_View::SetScale( const Standard_Real Coef )
1411 V3d_BadValue_Raise_if( Coef <= 0. ,"V3d_View::SetScale, bad coefficient");
1413 Handle(Graphic3d_Camera) aCamera = Camera();
1415 Standard_Real aDefaultScale = myDefaultCamera->Scale();
1416 aCamera->SetAspect (myDefaultCamera->Aspect());
1417 aCamera->SetScale (aDefaultScale / Coef);
1424 //=============================================================================
1425 //function : SetAxialScale
1427 //=============================================================================
1428 void V3d_View::SetAxialScale( const Standard_Real Sx, const Standard_Real Sy, const Standard_Real Sz )
1430 V3d_BadValue_Raise_if( Sx <= 0. || Sy <= 0. || Sz <= 0.,"V3d_View::SetAxialScale, bad coefficient");
1432 Camera()->SetAxialScale (gp_XYZ (Sx, Sy, Sz));
1437 //=============================================================================
1438 //function : SetRatio
1440 //=============================================================================
1441 void V3d_View::SetRatio()
1443 if (MyWindow.IsNull())
1448 Standard_Integer aWidth = 0;
1449 Standard_Integer aHeight = 0;
1450 MyWindow->Size (aWidth, aHeight);
1451 if (aWidth > 0 && aHeight > 0)
1453 Standard_Real aRatio = static_cast<Standard_Real> (aWidth) /
1454 static_cast<Standard_Real> (aHeight);
1456 Camera() ->SetAspect (aRatio);
1457 myDefaultCamera->SetAspect (aRatio);
1461 //=============================================================================
1464 //=============================================================================
1465 void V3d_View::FitAll (const Quantity_Coefficient theMargin, const Standard_Boolean theToUpdate)
1467 FitAll (myView->MinMaxValues(), theMargin, theToUpdate);
1470 //=============================================================================
1473 //=============================================================================
1474 void V3d_View::FitAll (const Bnd_Box& theBox, const Quantity_Coefficient theMargin, const Standard_Boolean theToUpdate)
1476 Standard_ASSERT_RAISE(theMargin >= 0.0 && theMargin < 1.0, "Invalid margin coefficient");
1478 if (myView->NumberOfDisplayedStructures() == 0)
1483 if (!FitMinMax (Camera(), theBox, theMargin, 10.0 * Precision::Confusion()))
1490 if (myImmediateUpdate || theToUpdate)
1496 //=============================================================================
1497 //function : DepthFitAll
1499 //=============================================================================
1500 void V3d_View::DepthFitAll(const Quantity_Coefficient Aspect,
1501 const Quantity_Coefficient Margin)
1503 Standard_Real Xmin,Ymin,Zmin,Xmax,Ymax,Zmax,U,V,W,U1,V1,W1 ;
1504 Standard_Real Umin,Vmin,Wmin,Umax,Vmax,Wmax ;
1505 Standard_Real Dx,Dy,Dz,Size;
1507 Standard_Integer Nstruct = myView->NumberOfDisplayedStructures() ;
1509 if((Nstruct <= 0) || (Aspect < 0.) || (Margin < 0.) || (Margin > 1.)) {
1514 Bnd_Box aBox = myView->MinMaxValues();
1520 aBox.Get (Xmin,Ymin,Zmin,Xmax,Ymax,Zmax);
1521 Project (Xmin,Ymin,Zmin,U,V,W) ;
1522 Project (Xmax,Ymax,Zmax,U1,V1,W1) ;
1523 Umin = Min(U,U1) ; Umax = Max(U,U1) ;
1524 Vmin = Min(V,V1) ; Vmax = Max(V,V1) ;
1525 Wmin = Min(W,W1) ; Wmax = Max(W,W1) ;
1526 Project (Xmin,Ymin,Zmax,U,V,W) ;
1527 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1528 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1529 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1530 Project (Xmax,Ymin,Zmax,U,V,W) ;
1531 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1532 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1533 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1534 Project (Xmax,Ymin,Zmin,U,V,W) ;
1535 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1536 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1537 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1538 Project (Xmax,Ymax,Zmin,U,V,W) ;
1539 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1540 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1541 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1542 Project (Xmin,Ymax,Zmax,U,V,W) ;
1543 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1544 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1545 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1546 Project (Xmin,Ymax,Zmin,U,V,W) ;
1547 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1548 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1549 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1552 Wmax = Max(Abs(Wmin),Abs(Wmax)) ;
1553 Dz = 2.*Wmax + Margin * Wmax;
1555 // Compute depth value
1556 Dx = Abs(Umax - Umin) ; Dy = Abs(Vmax - Vmin) ; // Dz = Abs(Wmax - Wmin);
1557 Dx += Margin * Dx; Dy += Margin * Dy;
1558 Size = Sqrt(Dx*Dx + Dy*Dy + Dz*Dz);
1561 SetDepth( Aspect * Size / 2.);
1567 //=============================================================================
1568 //function : WindowFitAll
1570 //=============================================================================
1571 void V3d_View::WindowFitAll(const Standard_Integer Xmin,
1572 const Standard_Integer Ymin,
1573 const Standard_Integer Xmax,
1574 const Standard_Integer Ymax)
1576 WindowFit(Xmin,Ymin,Xmax,Ymax);
1579 //=======================================================================
1580 //function : WindowFit
1582 //=======================================================================
1583 void V3d_View::WindowFit (const Standard_Integer theMinXp,
1584 const Standard_Integer theMinYp,
1585 const Standard_Integer theMaxXp,
1586 const Standard_Integer theMaxYp)
1588 Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
1590 Handle(Graphic3d_Camera) aCamera = Camera();
1592 if (!aCamera->IsOrthographic())
1594 // normalize view coordinates
1595 Standard_Integer aWinWidth, aWinHeight;
1596 MyWindow->Size (aWinWidth, aWinHeight);
1598 // z coordinate of camera center
1599 Standard_Real aDepth = aCamera->Project (aCamera->Center()).Z();
1601 // camera projection coordinate are in NDC which are normalized [-1, 1]
1602 Standard_Real aUMin = (2.0 / aWinWidth) * theMinXp - 1.0;
1603 Standard_Real aUMax = (2.0 / aWinWidth) * theMaxXp - 1.0;
1604 Standard_Real aVMin = (2.0 / aWinHeight) * theMinYp - 1.0;
1605 Standard_Real aVMax = (2.0 / aWinHeight) * theMaxYp - 1.0;
1607 // compute camera panning
1608 gp_Pnt aScreenCenter (0.0, 0.0, aDepth);
1609 gp_Pnt aFitCenter ((aUMin + aUMax) * 0.5, (aVMin + aVMax) * 0.5, aDepth);
1610 gp_Pnt aPanTo = aCamera->ConvertProj2View (aFitCenter);
1611 gp_Pnt aPanFrom = aCamera->ConvertProj2View (aScreenCenter);
1612 gp_Vec aPanVec (aPanFrom, aPanTo);
1614 // compute section size
1615 gp_Pnt aFitTopRight (aUMax, aVMax, aDepth);
1616 gp_Pnt aFitBotLeft (aUMin, aVMin, aDepth);
1617 gp_Pnt aViewBotLeft = aCamera->ConvertProj2View (aFitBotLeft);
1618 gp_Pnt aViewTopRight = aCamera->ConvertProj2View (aFitTopRight);
1620 Standard_Real aUSize = aViewTopRight.X() - aViewBotLeft.X();
1621 Standard_Real aVSize = aViewTopRight.Y() - aViewBotLeft.Y();
1623 Translate (aCamera, aPanVec.X(), -aPanVec.Y());
1624 Scale (aCamera, aUSize, aVSize);
1629 Standard_Real aX1, aY1, aX2, aY2;
1630 Convert (theMinXp, theMinYp, aX1, aY1);
1631 Convert (theMaxXp, theMaxYp, aX2, aY2);
1632 FitAll (aX1, aY1, aX2, aY2);
1635 SetImmediateUpdate (wasUpdateEnabled);
1640 //=======================================================================
1641 //function : ConvertToGrid
1643 //=======================================================================
1644 void V3d_View::ConvertToGrid(const Standard_Integer Xp,
1645 const Standard_Integer Yp,
1648 Standard_Real& Zg) const
1650 Graphic3d_Vertex aVrp;
1651 Standard_Real anX, anY, aZ;
1652 Convert (Xp, Yp, anX, anY, aZ);
1653 aVrp.SetCoord (anX, anY, aZ);
1655 if( MyViewer->Grid()->IsActive() ) {
1656 Graphic3d_Vertex aNewVrp = Compute (aVrp) ;
1657 aNewVrp.Coord (Xg,Yg,Zg) ;
1659 aVrp.Coord (Xg,Yg,Zg) ;
1662 //=======================================================================
1663 //function : ConvertToGrid
1665 //=======================================================================
1666 void V3d_View::ConvertToGrid(const Standard_Real X,
1667 const Standard_Real Y,
1668 const Standard_Real Z,
1671 Standard_Real& Zg) const
1673 if( MyViewer->Grid()->IsActive() ) {
1674 Graphic3d_Vertex aVrp (X,Y,Z) ;
1675 Graphic3d_Vertex aNewVrp = Compute (aVrp) ;
1676 aNewVrp.Coord(Xg,Yg,Zg) ;
1678 Xg = X; Yg = Y; Zg = Z;
1682 //=======================================================================
1683 //function : Convert
1685 //=======================================================================
1686 Standard_Real V3d_View::Convert(const Standard_Integer Vp) const
1688 Standard_Integer aDxw, aDyw ;
1690 V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
1692 MyWindow->Size (aDxw, aDyw);
1693 Standard_Real aValue;
1695 gp_Pnt aViewDims = Camera()->ViewDimensions();
1696 aValue = aViewDims.X() * (Standard_Real)Vp / (Standard_Real)aDxw;
1701 //=======================================================================
1702 //function : Convert
1704 //=======================================================================
1705 void V3d_View::Convert(const Standard_Integer Xp,
1706 const Standard_Integer Yp,
1708 Standard_Real& Yv) const
1710 Standard_Integer aDxw, aDyw;
1712 V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
1714 MyWindow->Size (aDxw, aDyw);
1716 gp_Pnt aPoint (Xp * 2.0 / aDxw - 1.0, (aDyw - Yp) * 2.0 / aDyw - 1.0, 0.0);
1717 aPoint = Camera()->ConvertProj2View (aPoint);
1723 //=======================================================================
1724 //function : Convert
1726 //=======================================================================
1727 Standard_Integer V3d_View::Convert(const Standard_Real Vv) const
1729 V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
1731 Standard_Integer aDxw, aDyw;
1732 MyWindow->Size (aDxw, aDyw);
1734 gp_Pnt aViewDims = Camera()->ViewDimensions();
1735 Standard_Integer aValue = RealToInt (aDxw * Vv / (aViewDims.X()));
1740 //=======================================================================
1741 //function : Convert
1743 //=======================================================================
1744 void V3d_View::Convert(const Standard_Real Xv,
1745 const Standard_Real Yv,
1746 Standard_Integer& Xp,
1747 Standard_Integer& Yp) const
1749 V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
1751 Standard_Integer aDxw, aDyw;
1752 MyWindow->Size (aDxw, aDyw);
1754 gp_Pnt aPoint (Xv, Yv, 0.0);
1755 aPoint = Camera()->ConvertView2Proj (aPoint);
1756 aPoint = gp_Pnt ((aPoint.X() + 1.0) * aDxw / 2.0, aDyw - (aPoint.Y() + 1.0) * aDyw / 2.0, 0.0);
1758 Xp = RealToInt (aPoint.X());
1759 Yp = RealToInt (aPoint.Y());
1762 //=======================================================================
1763 //function : Convert
1765 //=======================================================================
1766 void V3d_View::Convert(const Standard_Integer Xp,
1767 const Standard_Integer Yp,
1770 Standard_Real& Z) const
1772 V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
1773 Standard_Integer aHeight, aWidth;
1774 MyWindow->Size (aWidth, aHeight);
1776 Standard_Real anX = 2.0 * Xp / aWidth - 1.0;
1777 Standard_Real anY = 2.0 * (aHeight - 1 - Yp) / aHeight - 1.0;
1778 Standard_Real aZ = 2.0 * 0.0 - 1.0;
1780 gp_Pnt aResult = Camera()->UnProject (gp_Pnt (anX, anY, aZ));
1787 //=======================================================================
1788 //function : ConvertWithProj
1790 //=======================================================================
1791 void V3d_View::ConvertWithProj(const Standard_Integer Xp,
1792 const Standard_Integer Yp,
1798 Standard_Real& Dz) const
1800 V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
1801 Standard_Integer aHeight, aWidth;
1802 MyWindow->Size (aWidth, aHeight);
1804 Standard_Real anX = 2.0 * Xp / aWidth - 1.0;
1805 Standard_Real anY = 2.0 * (aHeight - 1 - Yp) / aHeight - 1.0;
1806 Standard_Real aZ = 2.0 * 0.0 - 1.0;
1808 Handle(Graphic3d_Camera) aCamera = Camera();
1810 gp_Pnt aResult = aCamera->UnProject (gp_Pnt (anX, anY, aZ));
1816 Graphic3d_Vertex aVrp;
1817 aVrp.SetCoord (X, Y, Z);
1819 aResult = aCamera->UnProject (gp_Pnt (anX, anY, aZ - 10.0));
1821 Graphic3d_Vec3d aNormDir;
1822 aNormDir.x() = X - aResult.X();
1823 aNormDir.y() = Y - aResult.Y();
1824 aNormDir.z() = Z - aResult.Z();
1825 aNormDir.Normalize();
1832 //=======================================================================
1833 //function : Convert
1835 //=======================================================================
1836 void V3d_View::Convert(const Standard_Real X,
1837 const Standard_Real Y,
1838 const Standard_Real Z,
1839 Standard_Integer& Xp,
1840 Standard_Integer& Yp) const
1842 V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
1843 Standard_Integer aHeight, aWidth;
1844 MyWindow->Size (aWidth, aHeight);
1846 gp_Pnt aPoint = Camera()->Project (gp_Pnt (X, Y, Z));
1848 Xp = RealToInt ((aPoint.X() + 1) * 0.5 * aWidth);
1849 Yp = RealToInt (aHeight - 1 - (aPoint.Y() + 1) * 0.5 * aHeight);
1852 //=======================================================================
1853 //function : Project
1855 //=======================================================================
1856 void V3d_View::Project (const Standard_Real theX,
1857 const Standard_Real theY,
1858 const Standard_Real theZ,
1859 Standard_Real& theXp,
1860 Standard_Real& theYp) const
1863 Project (theX, theY, theZ, theXp, theYp, aZp);
1866 //=======================================================================
1867 //function : Project
1869 //=======================================================================
1870 void V3d_View::Project (const Standard_Real theX,
1871 const Standard_Real theY,
1872 const Standard_Real theZ,
1873 Standard_Real& theXp,
1874 Standard_Real& theYp,
1875 Standard_Real& theZp) const
1877 Handle(Graphic3d_Camera) aCamera = Camera();
1879 gp_XYZ aViewSpaceDimensions = aCamera->ViewDimensions();
1880 Standard_Real aXSize = aViewSpaceDimensions.X();
1881 Standard_Real aYSize = aViewSpaceDimensions.Y();
1882 Standard_Real aZSize = aViewSpaceDimensions.Z();
1884 gp_Pnt aPoint = aCamera->Project (gp_Pnt (theX, theY, theZ));
1886 // NDC [-1, 1] --> PROJ [ -size / 2, +size / 2 ]
1887 theXp = aPoint.X() * aXSize * 0.5;
1888 theYp = aPoint.Y() * aYSize * 0.5;
1889 theZp = aPoint.Z() * aZSize * 0.5;
1892 //=======================================================================
1893 //function : BackgroundColor
1895 //=======================================================================
1896 void V3d_View::BackgroundColor(const Quantity_TypeOfColor Type,
1899 Standard_Real& V3) const
1901 Quantity_Color C = BackgroundColor() ;
1902 C.Values(V1,V2,V3,Type) ;
1905 //=======================================================================
1906 //function : BackgroundColor
1908 //=======================================================================
1909 Quantity_Color V3d_View::BackgroundColor() const
1911 return myView->Background().Color() ;
1914 //=======================================================================
1915 //function : GradientBackgroundColors
1917 //=======================================================================
1918 void V3d_View::GradientBackgroundColors (Quantity_Color& theColor1, Quantity_Color& theColor2) const
1920 myView->GradientBackground().Colors (theColor1, theColor2);
1923 //=======================================================================
1924 //function : GradientBackground
1926 //=======================================================================
1927 Aspect_GradientBackground V3d_View::GradientBackground() const
1929 return myView->GradientBackground();
1932 //=======================================================================
1935 //=======================================================================
1936 Standard_Real V3d_View::Scale() const
1938 return myDefaultCamera->Scale() / Camera()->Scale();
1941 //=======================================================================
1942 //function : AxialScale
1944 //=======================================================================
1945 void V3d_View::AxialScale(Standard_Real& Sx, Standard_Real& Sy, Standard_Real& Sz) const
1947 gp_Pnt anAxialScale = Camera()->AxialScale();
1948 Sx = anAxialScale.X();
1949 Sy = anAxialScale.Y();
1950 Sz = anAxialScale.Z();
1953 //=======================================================================
1956 //=======================================================================
1957 void V3d_View::Size(Standard_Real& Width, Standard_Real& Height) const
1959 gp_Pnt aViewDims = Camera()->ViewDimensions();
1961 Width = aViewDims.X();
1962 Height = aViewDims.Y();
1965 //=======================================================================
1968 //=======================================================================
1969 Standard_Real V3d_View::ZSize() const
1971 gp_Pnt aViewDims = Camera()->ViewDimensions();
1973 return aViewDims.Z();
1976 //=======================================================================
1979 //=======================================================================
1980 Standard_Integer V3d_View::MinMax(Standard_Real& Umin,
1981 Standard_Real& Vmin,
1982 Standard_Real& Umax,
1983 Standard_Real& Vmax) const
1985 Standard_Real Wmin,Wmax,U,V,W ;
1986 Standard_Real Xmin,Ymin,Zmin,Xmax,Ymax,Zmax ;
1988 Standard_Integer Nstruct = myView->NumberOfDisplayedStructures() ;
1991 Bnd_Box aBox = myView->MinMaxValues();
1992 aBox.Get (Xmin,Ymin,Zmin,Xmax,Ymax,Zmax);
1993 Project (Xmin,Ymin,Zmin,Umin,Vmin,Wmin) ;
1994 Project (Xmax,Ymax,Zmax,Umax,Vmax,Wmax) ;
1995 Project (Xmin,Ymin,Zmax,U,V,W) ;
1996 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1997 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1998 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1999 Project (Xmax,Ymin,Zmax,U,V,W) ;
2000 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
2001 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
2002 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
2003 Project (Xmax,Ymin,Zmin,U,V,W) ;
2004 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
2005 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
2006 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
2007 Project (Xmax,Ymax,Zmin,U,V,W) ;
2008 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
2009 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
2010 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
2011 Project (Xmin,Ymax,Zmax,U,V,W) ;
2012 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
2013 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
2014 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
2015 Project (Xmin,Ymax,Zmin,U,V,W) ;
2016 Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
2017 Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
2018 Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
2023 //=======================================================================
2026 //=======================================================================
2027 Standard_Integer V3d_View::MinMax(Standard_Real& Xmin,
2028 Standard_Real& Ymin,
2029 Standard_Real& Zmin,
2030 Standard_Real& Xmax,
2031 Standard_Real& Ymax,
2032 Standard_Real& Zmax) const
2035 // Standard_Integer Nstruct = (MyView->DisplayedStructures())->Extent() ;
2036 Standard_Integer Nstruct = myView->NumberOfDisplayedStructures() ;
2039 Bnd_Box aBox = myView->MinMaxValues();
2040 aBox.Get (Xmin,Ymin,Zmin,Xmax,Ymax,Zmax);
2045 //=======================================================================
2046 //function : Gravity
2048 //=======================================================================
2049 void V3d_View::Gravity (Standard_Real& theX,
2050 Standard_Real& theY,
2051 Standard_Real& theZ) const
2053 Graphic3d_MapOfStructure aSetOfStructures;
2054 myView->DisplayedStructures (aSetOfStructures);
2056 Standard_Boolean hasSelection = Standard_False;
2057 for (Graphic3d_MapIteratorOfMapOfStructure aStructIter (aSetOfStructures);
2058 aStructIter.More(); aStructIter.Next())
2060 if (aStructIter.Key()->IsHighlighted()
2061 && aStructIter.Key()->IsVisible())
2063 hasSelection = Standard_True;
2068 Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
2069 Standard_Integer aNbPoints = 0;
2070 gp_XYZ aResult (0.0, 0.0, 0.0);
2071 for (Graphic3d_MapIteratorOfMapOfStructure aStructIter (aSetOfStructures);
2072 aStructIter.More(); aStructIter.Next())
2074 const Handle(Graphic3d_Structure)& aStruct = aStructIter.Key();
2075 if (!aStruct->IsVisible()
2076 || aStruct->IsInfinite()
2077 || (hasSelection && !aStruct->IsHighlighted()))
2082 const Graphic3d_BndBox3d& aBox = aStruct->CStructure()->BoundingBox();
2083 if (!aBox.IsValid())
2088 // skip transformation-persistent objects
2089 if (!aStruct->TransformPersistence().IsNull())
2094 // use camera projection to find gravity point
2095 Xmin = aBox.CornerMin().x();
2096 Ymin = aBox.CornerMin().y();
2097 Zmin = aBox.CornerMin().z();
2098 Xmax = aBox.CornerMax().x();
2099 Ymax = aBox.CornerMax().y();
2100 Zmax = aBox.CornerMax().z();
2101 gp_Pnt aPnts[THE_NB_BOUND_POINTS] =
2103 gp_Pnt (Xmin, Ymin, Zmin), gp_Pnt (Xmin, Ymin, Zmax),
2104 gp_Pnt (Xmin, Ymax, Zmin), gp_Pnt (Xmin, Ymax, Zmax),
2105 gp_Pnt (Xmax, Ymin, Zmin), gp_Pnt (Xmax, Ymin, Zmax),
2106 gp_Pnt (Xmax, Ymax, Zmin), gp_Pnt (Xmax, Ymax, Zmax)
2109 for (Standard_Integer aPntIt = 0; aPntIt < THE_NB_BOUND_POINTS; ++aPntIt)
2111 const gp_Pnt& aBndPnt = aPnts[aPntIt];
2112 const gp_Pnt aProjected = Camera()->Project (aBndPnt);
2113 if (Abs (aProjected.X()) <= 1.0
2114 && Abs (aProjected.Y()) <= 1.0)
2116 aResult += aBndPnt.XYZ();
2124 // fallback - just use bounding box of entire scene
2125 Bnd_Box aBox = myView->MinMaxValues();
2128 aBox.Get (Xmin, Ymin, Zmin,
2130 gp_Pnt aPnts[THE_NB_BOUND_POINTS] =
2132 gp_Pnt (Xmin, Ymin, Zmin), gp_Pnt (Xmin, Ymin, Zmax),
2133 gp_Pnt (Xmin, Ymax, Zmin), gp_Pnt (Xmin, Ymax, Zmax),
2134 gp_Pnt (Xmax, Ymin, Zmin), gp_Pnt (Xmax, Ymin, Zmax),
2135 gp_Pnt (Xmax, Ymax, Zmin), gp_Pnt (Xmax, Ymax, Zmax)
2138 for (Standard_Integer aPntIt = 0; aPntIt < THE_NB_BOUND_POINTS; ++aPntIt)
2140 const gp_Pnt& aBndPnt = aPnts[aPntIt];
2141 aResult += aBndPnt.XYZ();
2149 aResult /= aNbPoints;
2156 //=======================================================================
2159 //=======================================================================
2160 void V3d_View::Eye(Standard_Real& X, Standard_Real& Y, Standard_Real& Z) const
2162 gp_Pnt aCameraEye = Camera()->Eye();
2168 //=============================================================================
2169 //function : FocalReferencePoint
2171 //=============================================================================
2172 void V3d_View::FocalReferencePoint(Standard_Real& X, Standard_Real& Y,Standard_Real& Z) const
2177 //=============================================================================
2178 //function : ProjReferenceAxe
2180 //=============================================================================
2181 void V3d_View::ProjReferenceAxe(const Standard_Integer Xpix,
2182 const Standard_Integer Ypix,
2188 Standard_Real& VZ) const
2190 Standard_Real Xo,Yo,Zo;
2192 Convert (Xpix, Ypix, XP, YP, ZP);
2193 if ( Type() == V3d_PERSPECTIVE )
2195 FocalReferencePoint (Xo,Yo,Zo);
2206 //=============================================================================
2209 //=============================================================================
2210 Standard_Real V3d_View::Depth() const
2212 return Camera()->Distance();
2215 //=============================================================================
2218 //=============================================================================
2219 void V3d_View::Proj(Standard_Real& Dx, Standard_Real& Dy, Standard_Real& Dz) const
2221 gp_Dir aCameraDir = Camera()->Direction().Reversed();
2222 Dx = aCameraDir.X();
2223 Dy = aCameraDir.Y();
2224 Dz = aCameraDir.Z();
2227 //=============================================================================
2230 //=============================================================================
2231 void V3d_View::At(Standard_Real& X, Standard_Real& Y, Standard_Real& Z) const
2233 gp_Pnt aCameraCenter = Camera()->Center();
2234 X = aCameraCenter.X();
2235 Y = aCameraCenter.Y();
2236 Z = aCameraCenter.Z();
2239 //=============================================================================
2242 //=============================================================================
2243 void V3d_View::Up(Standard_Real& Vx, Standard_Real& Vy, Standard_Real& Vz) const
2245 gp_Dir aCameraUp = Camera()->Up();
2251 //=============================================================================
2254 //=============================================================================
2255 Standard_Real V3d_View::Twist() const
2257 Standard_Real Xup,Yup,Zup,Xpn,Ypn,Zpn,X0,Y0,Z0 ;
2258 Standard_Real pvx,pvy,pvz,pvn,sca,angle ;
2259 Graphic3d_Vector Xaxis,Yaxis,Zaxis ;
2260 Standard_Boolean TheStatus ;
2262 gp_Dir aReferencePlane (Camera()->Direction().Reversed());
2266 anUp = gp_Dir (0.,0.,1.) ;
2267 TheStatus = ScreenAxis (aReferencePlane, anUp,Xaxis,Yaxis,Zaxis) ;
2269 anUp = gp_Dir (0.,1.,0.) ;
2270 TheStatus = ScreenAxis (aReferencePlane, anUp,Xaxis,Yaxis,Zaxis) ;
2273 anUp = gp_Dir (1.,0.,0.) ;
2274 TheStatus = ScreenAxis (aReferencePlane, anUp,Xaxis,Yaxis,Zaxis) ;
2276 Yaxis.Coord(X0,Y0,Z0) ;
2279 /* Compute Cross Vector From Up & Origin */
2280 pvx = Y0*Zup - Z0*Yup ;
2281 pvy = Z0*Xup - X0*Zup ;
2282 pvz = X0*Yup - Y0*Xup ;
2283 pvn = pvx*pvx + pvy*pvy + pvz*pvz ;
2284 sca = X0*Xup + Y0*Yup + Z0*Zup ;
2287 if( angle > 1. ) angle = 1. ;
2288 else if( angle < -1. ) angle = -1. ;
2289 angle = asin(angle) ;
2290 if( sca < 0. ) angle = M_PI - angle ;
2291 if( angle > 0. && angle < M_PI ) {
2292 sca = pvx*Xpn + pvy*Ypn + pvz*Zpn ;
2293 if( sca < 0. ) angle = DEUXPI - angle ;
2298 //=============================================================================
2299 //function : ShadingModel
2301 //=============================================================================
2302 V3d_TypeOfShadingModel V3d_View::ShadingModel() const
2304 return static_cast<V3d_TypeOfShadingModel> (myView->ShadingModel());
2307 //=============================================================================
2308 //function : TextureEnv
2310 //=============================================================================
2311 Handle(Graphic3d_TextureEnv) V3d_View::TextureEnv() const
2313 return myView->TextureEnv();
2316 //=============================================================================
2317 //function : Visualization
2319 //=============================================================================
2320 V3d_TypeOfVisualization V3d_View::Visualization() const
2322 return static_cast<V3d_TypeOfVisualization> (myView->VisualizationType());
2325 //=============================================================================
2328 //=============================================================================
2329 Handle(V3d_Viewer) V3d_View::Viewer() const
2334 //=============================================================================
2335 //function : IfWindow
2337 //=============================================================================
2338 Standard_Boolean V3d_View::IfWindow() const
2340 return myView->IsDefined();
2343 //=============================================================================
2346 //=============================================================================
2347 Handle(Aspect_Window) V3d_View::Window() const
2352 //=============================================================================
2355 //=============================================================================
2356 V3d_TypeOfView V3d_View::Type() const
2358 return Camera()->IsOrthographic() ? V3d_ORTHOGRAPHIC : V3d_PERSPECTIVE;
2361 //=============================================================================
2362 //function : SetFocale
2364 //=============================================================================
2365 void V3d_View::SetFocale( const Standard_Real focale )
2367 Handle(Graphic3d_Camera) aCamera = Camera();
2369 if (aCamera->IsOrthographic())
2374 Standard_Real aFOVyRad = ATan (focale / (aCamera->Distance() * 2.0));
2376 aCamera->SetFOVy (aFOVyRad * (360 / M_PI));
2381 //=============================================================================
2384 //=============================================================================
2385 Standard_Real V3d_View::Focale() const
2387 Handle(Graphic3d_Camera) aCamera = Camera();
2389 if (aCamera->IsOrthographic())
2394 return aCamera->Distance() * 2.0 * Tan (aCamera->FOVy() * M_PI / 360.0);
2397 //=============================================================================
2400 //=============================================================================
2401 Handle(Graphic3d_CView) V3d_View::View() const
2406 //=============================================================================
2407 //function : ScreenAxis
2409 //=============================================================================
2410 Standard_Boolean V3d_View::ScreenAxis( const gp_Dir &Vpn, const gp_Dir &Vup, Graphic3d_Vector &Xaxe, Graphic3d_Vector &Yaxe, Graphic3d_Vector &Zaxe)
2412 Standard_Real Xpn, Ypn, Zpn, Xup, Yup, Zup;
2413 Standard_Real dx1, dy1, dz1, xx, yy, zz;
2415 Xpn = Vpn.X(); Ypn = Vpn.Y(); Zpn = Vpn.Z();
2416 Xup = Vup.X(); Yup = Vup.Y(); Zup = Vup.Z();
2417 xx = Yup*Zpn - Zup*Ypn;
2418 yy = Zup*Xpn - Xup*Zpn;
2419 zz = Xup*Ypn - Yup*Xpn;
2420 Xaxe.SetCoord (xx, yy, zz);
2421 if (Xaxe.LengthZero()) return Standard_False;
2423 Xaxe.Coord(dx1, dy1, dz1);
2424 xx = Ypn*dz1 - Zpn*dy1;
2425 yy = Zpn*dx1 - Xpn*dz1;
2426 zz = Xpn*dy1 - Ypn*dx1;
2427 Yaxe.SetCoord (xx, yy, zz) ;
2428 if (Yaxe.LengthZero()) return Standard_False;
2431 Zaxe.SetCoord (Xpn, Ypn, Zpn);
2433 return Standard_True;
2436 //=============================================================================
2437 //function : TrsPoint
2439 //=============================================================================
2440 Graphic3d_Vertex V3d_View::TrsPoint( const Graphic3d_Vertex &P, const TColStd_Array2OfReal &Matrix )
2442 Graphic3d_Vertex PP ;
2443 Standard_Real X,Y,Z,XX,YY,ZZ ;
2446 Standard_Integer lr, ur, lc, uc;
2447 lr = Matrix.LowerRow ();
2448 ur = Matrix.UpperRow ();
2449 lc = Matrix.LowerCol ();
2450 uc = Matrix.UpperCol ();
2451 if ((ur - lr + 1 != 4) || (uc - lc + 1 != 4) ) {
2453 PP.SetCoord(X,Y,Z) ;
2457 XX = (Matrix(lr,lc+3) + X*Matrix(lr,lc) + Y*Matrix(lr,lc+1)+
2458 Z*Matrix(lr,lc+2))/Matrix(lr+3,lc+3) ;
2460 YY = (Matrix(lr+1,lc+3) + X*Matrix(lr+1,lc) + Y*Matrix(lr+1,lc+1) +
2461 Z*Matrix(lr+1,lc+2))/Matrix(lr+3,lc+3) ;
2463 ZZ = (Matrix(lr+2,lc+3) + X*Matrix(lr+2,lc) + Y*Matrix(lr+2,lc+1) +
2464 Z*Matrix(lr+2,lc+2))/Matrix(lr+3,lc+3) ;
2465 PP.SetCoord(XX,YY,ZZ) ;
2469 //=======================================================================
2472 //=======================================================================
2473 void V3d_View::Pan (const Standard_Integer theDXp,
2474 const Standard_Integer theDYp,
2475 const Quantity_Factor theZoomFactor,
2476 const Standard_Boolean theToStart)
2478 Panning (Convert (theDXp), Convert (theDYp), theZoomFactor, theToStart);
2481 //=======================================================================
2482 //function : Panning
2484 //=======================================================================
2485 void V3d_View::Panning (const Standard_Real theDXv,
2486 const Standard_Real theDYv,
2487 const Quantity_Factor theZoomFactor,
2488 const Standard_Boolean theToStart)
2490 Standard_ASSERT_RAISE (theZoomFactor > 0.0, "Bad zoom factor");
2492 Handle(Graphic3d_Camera) aCamera = Camera();
2496 myCamStartOpEye = aCamera->Eye();
2497 myCamStartOpCenter = aCamera->Center();
2500 Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
2502 gp_Pnt aViewDims = aCamera->ViewDimensions();
2504 aCamera->SetEye (myCamStartOpEye);
2505 aCamera->SetCenter (myCamStartOpCenter);
2506 Translate (aCamera, -theDXv, -theDYv);
2507 Scale (aCamera, aViewDims.X() / theZoomFactor, aViewDims.Y() / theZoomFactor);
2509 SetImmediateUpdate (wasUpdateEnabled);
2514 //=======================================================================
2517 //=======================================================================
2518 void V3d_View::Zoom (const Standard_Integer theXp1,
2519 const Standard_Integer theYp1,
2520 const Standard_Integer theXp2,
2521 const Standard_Integer theYp2)
2523 Standard_Integer aDx = theXp2 - theXp1;
2524 Standard_Integer aDy = theYp2 - theYp1;
2525 if (aDx != 0 || aDy != 0)
2527 Standard_Real aCoeff = Sqrt( (Standard_Real)(aDx * aDx + aDy * aDy) ) / 100.0 + 1.0;
2528 aCoeff = (aDx > 0) ? aCoeff : 1.0 / aCoeff;
2529 SetZoom (aCoeff, Standard_True);
2533 //=======================================================================
2534 //function : StartZoomAtPoint
2536 //=======================================================================
2537 void V3d_View::StartZoomAtPoint (const Standard_Integer theXp,
2538 const Standard_Integer theYp)
2540 MyZoomAtPointX = theXp;
2541 MyZoomAtPointY = theYp;
2544 //=======================================================================
2545 //function : ZoomAtPoint
2547 //=======================================================================
2548 void V3d_View::ZoomAtPoint (const Standard_Integer theMouseStartX,
2549 const Standard_Integer theMouseStartY,
2550 const Standard_Integer theMouseEndX,
2551 const Standard_Integer theMouseEndY)
2553 Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
2556 Standard_Real aDxy = Standard_Real ((theMouseEndX + theMouseEndY) - (theMouseStartX + theMouseStartY));
2557 Standard_Real aDZoom = Abs (aDxy) / 100.0 + 1.0;
2558 aDZoom = (aDxy > 0.0) ? aDZoom : 1.0 / aDZoom;
2560 V3d_BadValue_Raise_if (aDZoom <= 0.0, "V3d_View::ZoomAtPoint, bad coefficient");
2562 Handle(Graphic3d_Camera) aCamera = Camera();
2564 Standard_Real aViewWidth = aCamera->ViewDimensions().X();
2565 Standard_Real aViewHeight = aCamera->ViewDimensions().Y();
2567 // ensure that zoom will not be too small or too big.
2568 Standard_Real aCoef = aDZoom;
2569 if (aViewWidth < aCoef * Precision::Confusion())
2571 aCoef = aViewWidth / Precision::Confusion();
2573 else if (aViewWidth > aCoef * 1e12)
2575 aCoef = aViewWidth / 1e12;
2577 if (aViewHeight < aCoef * Precision::Confusion())
2579 aCoef = aViewHeight / Precision::Confusion();
2581 else if (aViewHeight > aCoef * 1e12)
2583 aCoef = aViewHeight / 1e12;
2586 Standard_Real aZoomAtPointXv = 0.0;
2587 Standard_Real aZoomAtPointYv = 0.0;
2588 Convert (MyZoomAtPointX, MyZoomAtPointY, aZoomAtPointXv, aZoomAtPointYv);
2590 V3d_Coordinate aDxv = aZoomAtPointXv / aCoef;
2591 V3d_Coordinate aDyv = aZoomAtPointYv / aCoef;
2593 aCamera->SetScale (aCamera->Scale() / aCoef);
2594 Translate (aCamera, aZoomAtPointXv - aDxv, aZoomAtPointYv - aDyv);
2598 SetImmediateUpdate (wasUpdateEnabled);
2603 //=============================================================================
2604 //function : AxialScale
2606 //=============================================================================
2607 void V3d_View::AxialScale (const Standard_Integer Dx,
2608 const Standard_Integer Dy,
2609 const V3d_TypeOfAxe Axis)
2611 if( Dx != 0. || Dy != 0. ) {
2612 Standard_Real Sx, Sy, Sz;
2613 AxialScale( Sx, Sy, Sz );
2614 Standard_Real dscale = Sqrt(Dx*Dx + Dy*Dy) / 100. + 1;
2615 dscale = (Dx > 0) ? dscale : 1./dscale;
2616 if( Axis == V3d_X ) Sx = dscale;
2617 if( Axis == V3d_Y ) Sy = dscale;
2618 if( Axis == V3d_Z ) Sz = dscale;
2619 SetAxialScale( Sx, Sy, Sz );
2623 //=============================================================================
2626 //=============================================================================
2627 void V3d_View::FitAll(const Standard_Real theXmin,
2628 const Standard_Real theYmin,
2629 const Standard_Real theXmax,
2630 const Standard_Real theYmax)
2632 Handle(Graphic3d_Camera) aCamera = Camera();
2633 Standard_Real anAspect = aCamera->Aspect();
2635 Standard_Real aFitSizeU = Abs (theXmax - theXmin);
2636 Standard_Real aFitSizeV = Abs (theYmax - theYmin);
2637 Standard_Real aFitAspect = aFitSizeU / aFitSizeV;
2638 if (aFitAspect >= anAspect)
2640 aFitSizeV = aFitSizeU / anAspect;
2644 aFitSizeU = aFitSizeV * anAspect;
2647 Translate (aCamera, (theXmin + theXmax) * 0.5, (theYmin + theYmax) * 0.5);
2648 Scale (aCamera, aFitSizeU, aFitSizeV);
2655 //=============================================================================
2656 //function : StartRotation
2658 //=============================================================================
2659 void V3d_View::StartRotation(const Standard_Integer X,
2660 const Standard_Integer Y,
2661 const Quantity_Ratio zRotationThreshold)
2666 rx = Standard_Real(Convert(x));
2667 ry = Standard_Real(Convert(y));
2669 Rotate(0.,0.,0.,gx,gy,gz,Standard_True);
2670 myZRotation = Standard_False;
2671 if( zRotationThreshold > 0. ) {
2672 Standard_Real dx = Abs(sx - rx/2.);
2673 Standard_Real dy = Abs(sy - ry/2.);
2674 // if( dx > rx/3. || dy > ry/3. ) myZRotation = Standard_True;
2675 Standard_Real dd = zRotationThreshold * (rx + ry)/2.;
2676 if( dx > dd || dy > dd ) myZRotation = Standard_True;
2681 //=============================================================================
2682 //function : Rotation
2684 //=============================================================================
2685 void V3d_View::Rotation(const Standard_Integer X,
2686 const Standard_Integer Y)
2688 if( rx == 0. || ry == 0. ) {
2692 Standard_Real dx=0.,dy=0.,dz=0.;
2694 dz = atan2(Standard_Real(X)-rx/2., ry/2.-Standard_Real(Y)) -
2695 atan2(sx-rx/2.,ry/2.-sy);
2697 dx = (Standard_Real(X) - sx) * M_PI / rx;
2698 dy = (sy - Standard_Real(Y)) * M_PI / ry;
2701 Rotate(dx, dy, dz, gx, gy, gz, Standard_False);
2704 //=============================================================================
2705 //function : SetComputedMode
2707 //=============================================================================
2708 void V3d_View::SetComputedMode (const Standard_Boolean theMode)
2714 myView->SetComputedMode (Standard_True);
2720 myView->SetComputedMode (Standard_False);
2725 //=============================================================================
2726 //function : ComputedMode
2728 //=============================================================================
2729 Standard_Boolean V3d_View::ComputedMode() const
2731 return myView->ComputedMode();
2734 //=============================================================================
2735 //function : SetBackFacingModel
2737 //=============================================================================
2738 void V3d_View::SetBackFacingModel (const V3d_TypeOfBackfacingModel theModel)
2740 myView->SetBackfacingModel (static_cast<Graphic3d_TypeOfBackfacingModel> (theModel));
2744 //=============================================================================
2745 //function : BackFacingModel
2747 //=============================================================================
2748 V3d_TypeOfBackfacingModel V3d_View::BackFacingModel() const
2750 return static_cast<V3d_TypeOfBackfacingModel> (myView->BackfacingModel());
2753 //=============================================================================
2756 //=============================================================================
2757 void V3d_View::Init()
2759 myComputedMode = MyViewer->ComputedMode();
2760 if (!myComputedMode || !MyViewer->DefaultComputedMode())
2762 SetComputedMode (Standard_False);
2766 //=============================================================================
2769 //=============================================================================
2770 Standard_Boolean V3d_View::Export (const Standard_CString theFileName,
2771 const Graphic3d_ExportFormat theFormat,
2772 const Graphic3d_SortType theSortType)
2774 return myView->Export (theFileName, theFormat, theSortType);
2777 //=============================================================================
2780 //=============================================================================
2781 Standard_Boolean V3d_View::Dump (const Standard_CString theFile,
2782 const Graphic3d_BufferType& theBufferType)
2784 Standard_Integer aWinWidth, aWinHeight;
2785 MyWindow->Size (aWinWidth, aWinHeight);
2786 Image_AlienPixMap anImage;
2788 return ToPixMap (anImage, aWinWidth, aWinHeight, theBufferType) && anImage.Save (theFile);
2791 //=============================================================================
2792 //function : ToPixMap
2794 //=============================================================================
2795 Standard_Boolean V3d_View::ToPixMap (Image_PixMap& theImage,
2796 const V3d_ImageDumpOptions& theParams)
2798 Graphic3d_Vec2i aTargetSize (theParams.Width, theParams.Height);
2799 if (aTargetSize.x() != 0
2800 && aTargetSize.y() != 0)
2802 // allocate image buffer for dumping
2803 if (theImage.IsEmpty()
2804 || theImage.SizeX() != Standard_Size(aTargetSize.x())
2805 || theImage.SizeY() != Standard_Size(aTargetSize.y()))
2807 Image_Format aFormat = Image_Format_UNKNOWN;
2808 switch (theParams.BufferType)
2810 case Graphic3d_BT_RGB: aFormat = Image_Format_RGB; break;
2811 case Graphic3d_BT_RGBA: aFormat = Image_Format_RGBA; break;
2812 case Graphic3d_BT_Depth: aFormat = Image_Format_GrayF; break;
2813 case Graphic3d_BT_RGB_RayTraceHdrLeft: aFormat = Image_Format_RGBF; break;
2816 if (!theImage.InitZero (aFormat, Standard_Size(aTargetSize.x()), Standard_Size(aTargetSize.y())))
2818 Message::DefaultMessenger()->Send (TCollection_AsciiString ("Fail to allocate an image ") + aTargetSize.x() + "x" + aTargetSize.y()
2819 + " for view dump", Message_Fail);
2820 return Standard_False;
2824 if (theImage.IsEmpty())
2826 Message::DefaultMessenger()->Send (TCollection_AsciiString ("V3d_View::ToPixMap() has been called without image dimensions"), Message_Fail);
2827 return Standard_False;
2829 aTargetSize.x() = (Standard_Integer )theImage.SizeX();
2830 aTargetSize.y() = (Standard_Integer )theImage.SizeY();
2832 Handle(Standard_Transient) aFBOPtr;
2833 Handle(Standard_Transient) aPrevFBOPtr = myView->FBO();
2834 Graphic3d_Vec2i aFBOVPSize = aTargetSize;
2836 bool isTiling = false;
2837 if (theParams.TileSize > 0)
2839 if (aFBOVPSize.x() > theParams.TileSize
2840 || aFBOVPSize.y() > theParams.TileSize)
2842 aFBOVPSize.x() = Min (aFBOVPSize.x(), theParams.TileSize);
2843 aFBOVPSize.y() = Min (aFBOVPSize.y(), theParams.TileSize);
2848 Graphic3d_Vec2i aPrevFBOVPSize;
2849 if (!aPrevFBOPtr.IsNull())
2851 Graphic3d_Vec2i aPrevFBOSizeMax;
2852 myView->FBOGetDimensions (aPrevFBOPtr,
2853 aPrevFBOVPSize.x(), aPrevFBOVPSize.y(),
2854 aPrevFBOSizeMax.x(), aPrevFBOSizeMax.y());
2855 if (aFBOVPSize.x() <= aPrevFBOSizeMax.x()
2856 && aFBOVPSize.y() <= aPrevFBOSizeMax.y())
2858 aFBOPtr = aPrevFBOPtr;
2862 if (aFBOPtr.IsNull())
2864 Standard_Integer aMaxTexSize = MyViewer->Driver()->InquireLimit (Graphic3d_TypeOfLimit_MaxTextureSize);
2865 if (theParams.TileSize > aMaxTexSize)
2867 Message::DefaultMessenger()->Send (TCollection_AsciiString ("Image dump can not be performed - specified tile size (")
2868 + theParams.TileSize + ") exceeds hardware limits (" + aMaxTexSize + ")", Message_Fail);
2869 return Standard_False;
2872 if (aFBOVPSize.x() > aMaxTexSize
2873 || aFBOVPSize.y() > aMaxTexSize)
2875 aFBOVPSize.x() = Min (aFBOVPSize.x(), aMaxTexSize);
2876 aFBOVPSize.y() = Min (aFBOVPSize.y(), aMaxTexSize);
2880 // Try to create hardware accelerated buffer
2881 aFBOPtr = myView->FBOCreate (aFBOVPSize.x(), aFBOVPSize.y());
2883 myView->SetFBO (aFBOPtr);
2885 if (aFBOPtr.IsNull())
2887 // try to use on-screen buffer
2888 Graphic3d_Vec2i aWinSize;
2889 MyWindow->Size (aWinSize.x(), aWinSize.y());
2890 if (aFBOVPSize.x() != aWinSize.x()
2891 || aFBOVPSize.y() != aWinSize.y())
2895 aFBOVPSize = aWinSize;
2897 Message::DefaultMessenger()->Send (TCollection_AsciiString ("Warning, on screen buffer is used for image dump - content might be invalid"), Message_Warning);
2900 // backup camera parameters
2901 Handle(Graphic3d_Camera) aStoreMapping = new Graphic3d_Camera();
2902 Handle(Graphic3d_Camera) aCamera = Camera();
2903 aStoreMapping->Copy (aCamera);
2904 if (aCamera->IsStereo())
2906 switch (theParams.StereoOptions)
2910 aCamera->SetProjectionType (Graphic3d_Camera::Projection_Perspective);
2913 case V3d_SDO_LEFT_EYE:
2915 aCamera->SetProjectionType (Graphic3d_Camera::Projection_MonoLeftEye);
2918 case V3d_SDO_RIGHT_EYE:
2920 aCamera->SetProjectionType (Graphic3d_Camera::Projection_MonoRightEye);
2923 case V3d_SDO_BLENDED:
2925 break; // dump as is
2929 if (theParams.ToAdjustAspect)
2931 aCamera->SetAspect (Standard_Real(aTargetSize.x()) / Standard_Real(aTargetSize.y()));
2935 // render immediate structures into back buffer rather than front
2936 const Standard_Boolean aPrevImmediateMode = myView->SetImmediateModeDrawToFront (Standard_False);
2938 Standard_Boolean isSuccess = Standard_True;
2941 if (!aFBOPtr.IsNull())
2943 myView->FBOChangeViewport (aFBOPtr, aTargetSize.x(), aTargetSize.y());
2946 isSuccess = isSuccess && myView->BufferDump (theImage, theParams.BufferType);
2950 Image_PixMap aTilePixMap;
2951 aTilePixMap.SetTopDown (theImage.IsTopDown());
2953 Graphic3d_Vec2i anOffset (0, 0);
2954 for (; anOffset.y() < aTargetSize.y(); anOffset.y() += aFBOVPSize.y())
2957 for (; anOffset.x() < aTargetSize.x(); anOffset.x() += aFBOVPSize.x())
2959 Graphic3d_CameraTile aTile;
2960 aTile.Offset = anOffset;
2961 aTile.TotalSize = aTargetSize;
2962 aTile.TileSize = aFBOVPSize;
2963 if (!aFBOPtr.IsNull())
2965 // crop corners in case of FBO
2966 // (no API to resize viewport of on-screen buffer - keep uncropped in this case)
2967 aTile = aTile.Cropped();
2969 if (aTile.TileSize.x() < 1
2970 || aTile.TileSize.y() < 1)
2975 const Standard_Integer aLeft = aTile.Offset.x();
2976 Standard_Integer aBottom = aTile.Offset.y();
2977 if (theImage.IsTopDown())
2979 const Standard_Integer aTop = aTile.Offset.y() + aTile.TileSize.y();
2980 aBottom = aTargetSize.y() - aTop;
2982 aTilePixMap.InitWrapper (theImage.Format(), theImage.ChangeData()
2983 + theImage.SizeRowBytes() * aBottom + theImage.SizePixelBytes() * aLeft,
2984 aTile.TileSize.x(), aTile.TileSize.y(),
2985 theImage.SizeRowBytes());
2987 aCamera->SetTile (aTile);
2988 if (!aFBOPtr.IsNull())
2990 myView->FBOChangeViewport (aFBOPtr, aTile.TileSize.x(), aTile.TileSize.y());
2993 isSuccess = isSuccess && myView->BufferDump (aTilePixMap, theParams.BufferType);
3007 myView->SetImmediateModeDrawToFront (aPrevImmediateMode);
3008 aCamera->Copy (aStoreMapping);
3009 if (aFBOPtr != aPrevFBOPtr)
3011 myView->FBORelease (aFBOPtr);
3013 else if (!aPrevFBOPtr.IsNull())
3015 myView->FBOChangeViewport (aPrevFBOPtr, aPrevFBOVPSize.x(), aPrevFBOVPSize.y());
3017 myView->SetFBO (aPrevFBOPtr);
3021 //=============================================================================
3022 //function : ImmediateUpdate
3024 //=============================================================================
3025 void V3d_View::ImmediateUpdate() const
3027 if (myImmediateUpdate)
3033 //=============================================================================
3034 //function : SetImmediateUpdate
3036 //=============================================================================
3037 Standard_Boolean V3d_View::SetImmediateUpdate (const Standard_Boolean theImmediateUpdate)
3039 Standard_Boolean aPreviousMode = myImmediateUpdate;
3040 myImmediateUpdate = theImmediateUpdate;
3041 return aPreviousMode;
3044 // =======================================================================
3045 // function : SetCamera
3047 // =======================================================================
3048 void V3d_View::SetCamera (const Handle(Graphic3d_Camera)& theCamera)
3050 myView->SetCamera (theCamera);
3055 // =======================================================================
3056 // function : GetCamera
3058 // =======================================================================
3059 const Handle(Graphic3d_Camera)& V3d_View::Camera() const
3061 return myView->Camera();
3064 // =======================================================================
3065 // function : FitMinMax
3066 // purpose : Internal
3067 // =======================================================================
3068 Standard_Boolean V3d_View::FitMinMax (const Handle(Graphic3d_Camera)& theCamera,
3069 const Bnd_Box& theBox,
3070 const Standard_Real theMargin,
3071 const Standard_Real theResolution,
3072 const Standard_Boolean theToEnlargeIfLine) const
3074 // Check bounding box for validness
3075 if (theBox.IsVoid())
3077 return Standard_False; // bounding box is out of bounds...
3080 // Apply "axial scaling" to the bounding points.
3081 // It is not the best approach to make this scaling as a part of fit all operation,
3082 // but the axial scale is integrated into camera orientation matrix and the other
3083 // option is to perform frustum plane adjustment algorithm in view camera space,
3084 // which will lead to a number of additional world-view space conversions and
3085 // loosing precision as well.
3086 gp_Pnt aBndMin = theBox.CornerMin().XYZ().Multiplied (theCamera->AxialScale());
3087 gp_Pnt aBndMax = theBox.CornerMax().XYZ().Multiplied (theCamera->AxialScale());
3089 if (aBndMax.IsEqual (aBndMin, RealEpsilon()))
3091 return Standard_False; // nothing to fit all
3094 // Prepare camera frustum planes.
3095 NCollection_Array1<gp_Pln> aFrustumPlane (1, 6);
3096 theCamera->Frustum (aFrustumPlane.ChangeValue (1),
3097 aFrustumPlane.ChangeValue (2),
3098 aFrustumPlane.ChangeValue (3),
3099 aFrustumPlane.ChangeValue (4),
3100 aFrustumPlane.ChangeValue (5),
3101 aFrustumPlane.ChangeValue (6));
3103 // Prepare camera up, side, direction vectors.
3104 gp_Dir aCamUp = theCamera->OrthogonalizedUp();
3105 gp_Dir aCamDir = theCamera->Direction();
3106 gp_Dir aCamSide = aCamDir ^ aCamUp;
3108 // Prepare scene bounding box parameters.
3109 gp_Pnt aBndCenter = (aBndMin.XYZ() + aBndMax.XYZ()) / 2.0;
3111 NCollection_Array1<gp_Pnt> aBndCorner (1, 8);
3112 aBndCorner.ChangeValue (1) = gp_Pnt (aBndMin.X(), aBndMin.Y(), aBndMin.Z());
3113 aBndCorner.ChangeValue (2) = gp_Pnt (aBndMin.X(), aBndMin.Y(), aBndMax.Z());
3114 aBndCorner.ChangeValue (3) = gp_Pnt (aBndMin.X(), aBndMax.Y(), aBndMin.Z());
3115 aBndCorner.ChangeValue (4) = gp_Pnt (aBndMin.X(), aBndMax.Y(), aBndMax.Z());
3116 aBndCorner.ChangeValue (5) = gp_Pnt (aBndMax.X(), aBndMin.Y(), aBndMin.Z());
3117 aBndCorner.ChangeValue (6) = gp_Pnt (aBndMax.X(), aBndMin.Y(), aBndMax.Z());
3118 aBndCorner.ChangeValue (7) = gp_Pnt (aBndMax.X(), aBndMax.Y(), aBndMin.Z());
3119 aBndCorner.ChangeValue (8) = gp_Pnt (aBndMax.X(), aBndMax.Y(), aBndMax.Z());
3121 // Perspective-correct camera projection vector, matching the bounding box is determined geometrically.
3122 // Knowing the initial shape of a frustum it is possible to match it to a bounding box.
3123 // Then, knowing the relation of camera projection vector to the frustum shape it is possible to
3124 // set up perspective-correct camera projection matching the bounding box.
3125 // These steps support non-asymmetric transformations of view-projection space provided by camera.
3126 // The zooming can be done by calculating view plane size matching the bounding box at center of
3127 // the bounding box. The only limitation here is that the scale of camera should define size of
3128 // its view plane passing through the camera center, and the center of camera should be on the
3129 // same line with the center of bounding box.
3131 // The following method is applied:
3132 // 1) Determine normalized asymmetry of camera projection vector by frustum planes.
3133 // 2) Determine new location of frustum planes, "matching" the bounding box.
3134 // 3) Determine new camera projection vector using the normalized asymmetry.
3135 // 4) Determine new zooming in view space.
3137 // 1. Determine normalized projection asymmetry (if any).
3138 Standard_Real anAssymX = Tan (( aCamSide).Angle (aFrustumPlane (1).Axis().Direction()))
3139 - Tan ((-aCamSide).Angle (aFrustumPlane (2).Axis().Direction()));
3140 Standard_Real anAssymY = Tan (( aCamUp) .Angle (aFrustumPlane (3).Axis().Direction()))
3141 - Tan ((-aCamUp) .Angle (aFrustumPlane (4).Axis().Direction()));
3143 // 2. Determine how far should be the frustum planes placed from center
3144 // of bounding box, in order to match the bounding box closely.
3145 NCollection_Array1<Standard_Real> aFitDistance (1, 6);
3146 aFitDistance.ChangeValue (1) = 0.0;
3147 aFitDistance.ChangeValue (2) = 0.0;
3148 aFitDistance.ChangeValue (3) = 0.0;
3149 aFitDistance.ChangeValue (4) = 0.0;
3150 aFitDistance.ChangeValue (5) = 0.0;
3151 aFitDistance.ChangeValue (6) = 0.0;
3153 for (Standard_Integer anI = aFrustumPlane.Lower(); anI <= aFrustumPlane.Upper(); ++anI)
3155 // Measure distances from center of bounding box to its corners towards the frustum plane.
3156 const gp_Dir& aPlaneN = aFrustumPlane.ChangeValue (anI).Axis().Direction();
3158 Standard_Real& aFitDist = aFitDistance.ChangeValue (anI);
3160 for (Standard_Integer aJ = aBndCorner.Lower(); aJ <= aBndCorner.Upper(); ++aJ)
3162 aFitDist = Max (aFitDist, gp_Vec (aBndCenter, aBndCorner (aJ)).Dot (aPlaneN));
3165 // The center of camera is placed on the same line with center of bounding box.
3166 // The view plane section crosses the bounding box at its center.
3167 // To compute view plane size, evaluate coefficients converting "point -> plane distance"
3168 // into view section size between the point and the frustum plane.
3170 // /|\ right half of frame //
3172 // point o<-- distance * coeff -->//---- (view plane section)
3181 aFitDistance.ChangeValue (1) *= Sqrt(1 + Pow (Tan ( aCamSide .Angle (aFrustumPlane (1).Axis().Direction())), 2.0));
3182 aFitDistance.ChangeValue (2) *= Sqrt(1 + Pow (Tan ((-aCamSide).Angle (aFrustumPlane (2).Axis().Direction())), 2.0));
3183 aFitDistance.ChangeValue (3) *= Sqrt(1 + Pow (Tan ( aCamUp .Angle (aFrustumPlane (3).Axis().Direction())), 2.0));
3184 aFitDistance.ChangeValue (4) *= Sqrt(1 + Pow (Tan ((-aCamUp) .Angle (aFrustumPlane (4).Axis().Direction())), 2.0));
3185 aFitDistance.ChangeValue (5) *= Sqrt(1 + Pow (Tan ( aCamDir .Angle (aFrustumPlane (5).Axis().Direction())), 2.0));
3186 aFitDistance.ChangeValue (6) *= Sqrt(1 + Pow (Tan ((-aCamDir) .Angle (aFrustumPlane (6).Axis().Direction())), 2.0));
3188 Standard_Real aViewSizeXv = aFitDistance (1) + aFitDistance (2);
3189 Standard_Real aViewSizeYv = aFitDistance (3) + aFitDistance (4);
3190 Standard_Real aViewSizeZv = aFitDistance (5) + aFitDistance (6);
3192 // 3. Place center of camera on the same line with center of bounding
3193 // box applying corresponding projection asymmetry (if any).
3194 Standard_Real anAssymXv = anAssymX * aViewSizeXv * 0.5;
3195 Standard_Real anAssymYv = anAssymY * aViewSizeYv * 0.5;
3196 Standard_Real anOffsetXv = (aFitDistance (2) - aFitDistance (1)) * 0.5 + anAssymXv;
3197 Standard_Real anOffsetYv = (aFitDistance (4) - aFitDistance (3)) * 0.5 + anAssymYv;
3198 gp_Vec aTranslateSide = gp_Vec (aCamSide) * anOffsetXv;
3199 gp_Vec aTranslateUp = gp_Vec (aCamUp) * anOffsetYv;
3200 gp_Pnt aCamNewCenter = aBndCenter.Translated (aTranslateSide).Translated (aTranslateUp);
3202 gp_Trsf aCenterTrsf;
3203 aCenterTrsf.SetTranslation (theCamera->Center(), aCamNewCenter);
3204 theCamera->Transform (aCenterTrsf);
3205 theCamera->SetDistance (aFitDistance (6) + aFitDistance (5));
3207 // Bounding box collapses to a point or thin line going in depth of the screen
3208 if (aViewSizeXv < theResolution && aViewSizeYv < theResolution)
3210 if (aViewSizeXv < theResolution || !theToEnlargeIfLine)
3212 return Standard_True; // This is just one point or line and zooming has no effect.
3215 // Looking along line and "theToEnlargeIfLine" is requested.
3216 // Fit view to see whole scene on rotation.
3217 aViewSizeXv = aViewSizeZv;
3218 aViewSizeYv = aViewSizeZv;
3221 Scale (theCamera, aViewSizeXv, aViewSizeYv);
3223 const Standard_Real aZoomCoef = myView->ConsiderZoomPersistenceObjects();
3225 Scale (theCamera, theCamera->ViewDimensions().X() * (aZoomCoef + theMargin), theCamera->ViewDimensions().Y() * (aZoomCoef + theMargin));
3227 return Standard_True;
3230 // =======================================================================
3232 // purpose : Internal
3233 // =======================================================================
3234 void V3d_View::Scale (const Handle(Graphic3d_Camera)& theCamera,
3235 const Standard_Real theSizeXv,
3236 const Standard_Real theSizeYv) const
3238 Standard_Real anAspect = theCamera->Aspect();
3241 theCamera->SetScale (Max (theSizeXv / anAspect, theSizeYv));
3245 theCamera->SetScale (Max (theSizeXv, theSizeYv * anAspect));
3249 // =======================================================================
3250 // function : Translate
3251 // purpose : Internal
3252 // =======================================================================
3253 void V3d_View::Translate (const Handle(Graphic3d_Camera)& theCamera,
3254 const Standard_Real theDXv,
3255 const Standard_Real theDYv) const
3257 const gp_Pnt& aCenter = theCamera->Center();
3258 const gp_Dir& aDir = theCamera->Direction();
3259 const gp_Dir& anUp = theCamera->Up();
3260 gp_Ax3 aCameraCS (aCenter, aDir.Reversed(), aDir ^ anUp);
3262 gp_Vec aCameraPanXv = gp_Vec (aCameraCS.XDirection()) * theDXv;
3263 gp_Vec aCameraPanYv = gp_Vec (aCameraCS.YDirection()) * theDYv;
3264 gp_Vec aCameraPan = aCameraPanXv + aCameraPanYv;
3266 aPanTrsf.SetTranslation (aCameraPan);
3268 theCamera->Transform (aPanTrsf);
3271 // =======================================================================
3272 // function : IsCullingEnabled
3274 // =======================================================================
3275 Standard_Boolean V3d_View::IsCullingEnabled() const
3277 return myView->IsCullingEnabled();
3280 // =======================================================================
3281 // function : SetFrustumCulling
3283 // =======================================================================
3284 void V3d_View::SetFrustumCulling (const Standard_Boolean theToClip)
3286 myView->SetCullingEnabled (theToClip);
3289 // =======================================================================
3290 // function : DiagnosticInformation
3292 // =======================================================================
3293 void V3d_View::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
3294 Graphic3d_DiagnosticInfo theFlags) const
3296 myView->DiagnosticInformation (theDict, theFlags);
3299 //=============================================================================
3300 //function : RenderingParams
3302 //=============================================================================
3303 const Graphic3d_RenderingParams& V3d_View::RenderingParams() const
3305 return myView->RenderingParams();
3308 //=============================================================================
3309 //function : ChangeRenderingParams
3311 //=============================================================================
3312 Graphic3d_RenderingParams& V3d_View::ChangeRenderingParams()
3314 return myView->ChangeRenderingParams();