0030692: Data Exchange - introduce base framework RWMesh for importing mesh data...
[occt.git] / src / V3d / V3d_View.cxx
index cd35478..b67123a 100644 (file)
 // Alternatively, this file may be used under the terms of Open CASCADE
 // commercial license or contractual agreement.
 
-/***********************************************************************
-
-FONCTION :
-----------
-Classe V3d_View :
-
-HISTORIQUE DES MODIFICATIONS   :
---------------------------------
-00-09-92 : GG  ; Creation.
-02-10-96 : FMN ; Suppression appel Redraw sans MustBeResized()
-05-06-97 : FMN ; Correction FitAll()
-30-06-97 : GG ; Correction + Optimisation de Panning(...)
-On fait la translation + le zoom en une seule
-operation au lieu de 2 precedemment qui etait buggee.
-09-07-97 : FMN ; Correction FitAll() sur le Ratio
-16-07-97 : FMN ; Correction FitAll() sur le calcul de la Box
-22-07-97 : FMN ; Ajout mode RetainMode pour le Transient
-15-12-97 : FMN ; Ajout texture mapping
-17-12-97 : FMN ; CTS19129 Correction FitAll() multiple
-18-12-97 : FMN ; Ajout mode Ajout
-24-12-97 : FMN ; Remplacement de math par MathGra
-24-12-97 : CQO ; BUC50037 Xw_Window -> Aspect_Window
-31-12-97 : CAL ; Remplacement de MathGra par Array2OfReal
-07-01-98 : CAL ; Ajout de la methode DoMapping.
-07-01-98 : CAL ; Retrait de tous les "this->" inutiles
-21-01-98 : CAL ; Remplacement des Window->Position () par Window->Size ()
-27-01-98 : FMN ; PERF: OPTIMISATION LOADER (LOPTIM)
-12-02-98 : GG  ; Reactivation du Redraw dans MustBeResized()
-23-02-98 : FMN ; Remplacement PI par Standard_PI
-25-02-98 : FMN ; PERF.27: Optimisation of view creation from existing view
-11-03-98 : STT ; S3558
-19-03-98 : FMN ; Probleme dans FitAll car la methode WNT_Window::Size(Real,Real)
-ne marche pas.
-08-04-98 : STT ; suppr. S3558
-10-04-98 : CAL ; Ajout des methodes RefToPix et PixToRef
-13-06-98 : FMN ; Probleme dans FitAll car la methode WNT_Window::Size(Real,Real)
-ne marche pas. Contournement en appelant WNT_Window::Size(Int,Int).
-16-08-98 : CAL ; S3892. Ajout grilles 3d.
-09-09-98 : CAL ; S3892. Generalisation de TrsPoint.
-06-10-98 : CAL ; Ajout d'un TIMER si CSF_GraphicTimer est definie.
-16-10-98 : CAL ; Retrait d'un TIMER si CSF_GraphicTimer est definie.
-06-11-98 : CAL ; PRO ?????. Probleme dans ZFitAll si un point dans la vue.
-13-06-98 : FMN ; PRO14896: Correction sur la gestion de la perspective (cf Programming Guinde)
-29-OCT-98 : DCB : Adding ScreenCopy () method.
-10-11-99 : GG ; PRO19603 Add Redraw( area ) method
-IMP130100 : GG
--> Don't increase too much the ZSize.
--> Initialize correctly the Z clipping and D cueing
-planes.
-IMP100701 : SZV ; Add ToPixMap() method
-
-REMARQUES :
------------
-About  FitAll() multiple. This probleme is caused by missing
-precision of transformation matrices. If it is supposed that
-projection is made in the plane (U,V), there is a difference
-after several Zoom - compared to the exact value (cf ZoomX).
-Don't forget that the matrices work in float and not in double.
-To solve the problem (for lack of a better solution) I make 2 passes.
-
-************************************************************************/
-
-//GER61351  //GG_15/12/99 Add SetBackgroundColor() and BackgroundColor() methods
-
-
-#define IMP020300 //GG Don't use ZFitAll in during Rotation
-//      for perf improvment
-
-#define IMP210600 //GG Avoid to have infinite loop when call Rotation() method
-//      without call before StartRotation().
-//      This problem occurs when CTRL MB3 is maintain press betwwen 2 views.
-
-#define IMP250900 //GG Enable rotation around screen Z axis when
-//      rotation begin far the center of the screen.
-//      Thanks to Patrick REGINSTER (SAMTECH)
-//      GG 21/12/00 Due to a regression on the previous specifications
-//      this new functionnality is right now deactivated
-//      by default (see StartRotation(...,zRotationThreshold)
-//      method.
-
-#define BUC60952  //GG Enable to rotate around the view axis
-//      and the required view point
-
-#define RIC120302 //GG Add a NEW SetWindow method which enable
-//      to connect a graphic widget and context to OGL.
-
-#define IMP260302 //GG To avoid conflicting in Window destructor
-//      nullify this handle in Remove method
-
-#define OCC280          //SAV fix for FitAll problem in the perspective view.
-
-#define OCC1188         //SAV Added methods to set background image
-
-/*----------------------------------------------------------------------*/
-/*
-* Includes
-*/
-
-#include <Standard_TypeMismatch.hxx>
-#include <Standard_ShortReal.hxx>
-#include <Standard_Assert.hxx>
-#include <Standard_ErrorHandler.hxx>
-#include <Standard_DivideByZero.hxx>
-
-#include <Visual3d_ViewManager.hxx>
-#include <Visual3d_Light.hxx>
-#include <Visual3d_Layer.hxx>
-
-#include <V3d.hxx>
-#include <V3d_View.ixx>
-#include <V3d_BadValue.hxx>
-#include <V3d_StereoDumpOptions.hxx>
-
-#include <Image_AlienPixMap.hxx>
+#include <V3d_View.hxx>
 
+#include <Aspect_GradientBackground.hxx>
+#include <Aspect_Grid.hxx>
+#include <Aspect_Window.hxx>
+#include <Bnd_Box.hxx>
+#include <gp_Ax3.hxx>
 #include <gp_Dir.hxx>
 #include <gp_Pln.hxx>
-#include <TColStd_Array2OfReal.hxx>
-#include <TColStd_HSequenceOfInteger.hxx>
-
-#include <Bnd_Box.hxx>
-
-#include <Precision.hxx>
-
-#include <Graphic3d_Structure.hxx>
+#include <Graphic3d_AspectMarker3d.hxx>
+#include <Graphic3d_GraphicDriver.hxx>
+#include <Graphic3d_Group.hxx>
 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
 #include <Graphic3d_MapOfStructure.hxx>
+#include <Graphic3d_Structure.hxx>
 #include <Graphic3d_TextureEnv.hxx>
-#include <Graphic3d_AspectMarker3d.hxx>
-#include <Graphic3d_GraphicDriver.hxx>
-
-#define V3d_FLAG_COMPUTATION   0x00000004
-
-// Perspective
-#include <OSD_Environment.hxx>
+#include <Image_AlienPixMap.hxx>
+#include <Message.hxx>
+#include <Message_Messenger.hxx>
+#include <NCollection_Array1.hxx>
+#include <Precision.hxx>
+#include <Quantity_Color.hxx>
+#include <Standard_Assert.hxx>
+#include <Standard_DivideByZero.hxx>
+#include <Standard_ErrorHandler.hxx>
+#include <Standard_MultiplyDefined.hxx>
+#include <Standard_ShortReal.hxx>
+#include <Standard_Type.hxx>
+#include <Standard_TypeMismatch.hxx>
+#include <TColgp_Array1OfPnt.hxx>
+#include <TColStd_Array2OfReal.hxx>
+#include <TColStd_HSequenceOfInteger.hxx>
+#include <V3d.hxx>
+#include <V3d_BadValue.hxx>
+#include <V3d_Light.hxx>
+#include <V3d_StereoDumpOptions.hxx>
+#include <V3d_UnMapped.hxx>
+#include <V3d_Viewer.hxx>
 
-/*----------------------------------------------------------------------*/
-/*
-* Constant
-*/
+IMPLEMENT_STANDARD_RTTIEXT(V3d_View,Standard_Transient)
 
 #define DEUXPI (2. * M_PI)
 
+namespace
+{
+  static const Standard_Integer THE_NB_BOUND_POINTS = 8;
+}
+
 //=============================================================================
 //function : Constructor
 //purpose  :
 //=============================================================================
-V3d_View::V3d_View(const Handle(V3d_Viewer)& VM, const V3d_TypeOfView Type ) :
-  MyProjModel(V3d_TPM_SCREEN),
-  MyViewer(VM.operator->()),
-  MyActiveLights(),
-  MyViewContext (),
-  myActiveLightsIterator(),
-  SwitchSetFront(Standard_False),
-  MyTrsf (1, 4, 1, 4),
-  myAutoZFitIsOn (Standard_True),
-  myAutoZFitScaleFactor (1.0)
+V3d_View::V3d_View (const Handle(V3d_Viewer)& theViewer, const V3d_TypeOfView theType)
+: MyViewer (theViewer.operator->()),
+  SwitchSetFront (Standard_False),
+  myZRotation (Standard_False),
+  myTrihedron (new V3d_Trihedron()),
+  MyTrsf (1, 4, 1, 4)
 {
-  myImmediateUpdate = Standard_False;
-  MyView = new Visual3d_View(MyViewer->Viewer());
-
-  // { Begin to retrieve the definition from ViewContext.
-  // Step MyViewContext = MyView->Context() ;
-  // to permit MyView->SetContext to compare
-  // the old and the new context.
-  // No problem for MyViewMapping, MyViewOrientation
-  // as MyView->SetViewMapping and MyView->SetViewOrientation
-  // don't try to optimize the modifications introduced to
-  // viewmapping and vieworientation.
-
-  // Aliasing
-  if ((MyView->Context ()).AliasingIsOn ())
-    MyViewContext.SetAliasingOn ();
-  else
-    MyViewContext.SetAliasingOff ();
-
-  // DepthCueing
-  MyViewContext.SetDepthCueingBackPlane
-    ((MyView->Context ()).DepthCueingBackPlane ());
-  MyViewContext.SetDepthCueingFrontPlane
-    ((MyView->Context ()).DepthCueingFrontPlane ());
-
-  if ((MyView->Context ()).DepthCueingIsOn ())
-    MyViewContext.SetDepthCueingOn ();
-  else
-    MyViewContext.SetDepthCueingOff ();
-
-  // ZClipping
-  MyViewContext.SetZClippingBackPlane
-    ((MyView->Context ()).ZClippingBackPlane ());
-  MyViewContext.SetZClippingFrontPlane
-    ((MyView->Context ()).ZClippingFrontPlane ());
+  myView = theViewer->Driver()->CreateView (theViewer->StructureManager());
 
-  if ((MyView->Context ()).FrontZClippingIsOn ())
-    MyViewContext.SetFrontZClippingOn ();
-  else
-    MyViewContext.SetFrontZClippingOff ();
-
-  if ((MyView->Context ()).BackZClippingIsOn ())
-    MyViewContext.SetBackZClippingOn ();
-  else
-    MyViewContext.SetBackZClippingOff ();
-
-  // Visualization and Shading Model
-  MyViewContext.SetModel ((MyView->Context ()).Model ());
-  MyViewContext.SetVisualization ((MyView->Context ()).Visualization ());
+  myView->SetBackground         (theViewer->GetBackgroundColor());
+  myView->SetGradientBackground (theViewer->GetGradientBackground());
 
-  // Texture Mapping
-  MyViewContext.SetSurfaceDetail (MyView->Context ().SurfaceDetail ());
-  MyViewContext.SetTextureEnv (MyView->Context ().TextureEnv ());
-  // } End of retrieval of the definition of ViewContext.
-
-  MyBackground = VM->GetBackgroundColor() ;
-  MyGradientBackground = VM->GetGradientBackground() ;
+  ChangeRenderingParams() = theViewer->DefaultRenderingParams();
 
   // camera init
   Handle(Graphic3d_Camera) aCamera = new Graphic3d_Camera();
   aCamera->SetFOVy (45.0);
   aCamera->SetIOD (Graphic3d_Camera::IODType_Relative, 0.05);
   aCamera->SetZFocus (Graphic3d_Camera::FocusType_Relative, 1.0);
-  SetCamera (aCamera);
+  aCamera->SetProjectionType ((theType == V3d_ORTHOGRAPHIC)
+    ? Graphic3d_Camera::Projection_Orthographic
+    : Graphic3d_Camera::Projection_Perspective);
+
+  myDefaultCamera = new Graphic3d_Camera();
 
+  myImmediateUpdate = Standard_False;
+  SetAutoZFitMode (Standard_True, 1.0);
+  SetBackFacingModel (V3d_TOBM_AUTOMATIC);
+  SetCamera (aCamera);
   SetAxis (0.,0.,0.,1.,1.,1.);
-  SetVisualization (VM->DefaultVisualization());
-  SetShadingModel (VM->DefaultShadingModel());
-  SetSurfaceDetail (VM->DefaultSurfaceDetail());
+  SetVisualization (theViewer->DefaultVisualization());
+  SetShadingModel (theViewer->DefaultShadingModel());
   SetTwist (0.);
   SetAt (0.,0.,0.);
-  SetProj (VM->DefaultViewProj());
-  SetSize (VM->DefaultViewSize());
-  Standard_Real zsize = VM->DefaultViewSize();
+  SetProj (theViewer->DefaultViewProj());
+  SetSize (theViewer->DefaultViewSize());
+  Standard_Real zsize = theViewer->DefaultViewSize();
   SetZSize (2.*zsize);
-  SetZClippingDepth (0.);
-  SetZClippingWidth (zsize);
-  SetZCueingDepth (0.);
-  SetZCueingWidth (zsize);
-  SetDepth (VM->DefaultViewSize()/2.0);
+  SetDepth (theViewer->DefaultViewSize() / 2.0);
   SetViewMappingDefault();
   SetViewOrientationDefault();
-  VM->AddView (this);
+  theViewer->AddView (this);
   Init();
   myImmediateUpdate = Standard_True;
-
-  aCamera->SetProjectionType ((Type == V3d_ORTHOGRAPHIC)
-    ? Graphic3d_Camera::Projection_Orthographic
-    : Graphic3d_Camera::Projection_Perspective);
-
-  MyTransparencyFlag = Standard_False;
 }
 
 //=============================================================================
 //function : Constructor
 //purpose  :
 //=============================================================================
-V3d_View::V3d_View(const Handle(V3d_Viewer)& theVM,const Handle(V3d_View)& theView) :
-  MyProjModel(V3d_TPM_SCREEN),
-  MyViewer(theVM.operator->()),
-  MyActiveLights(),
-  MyViewContext (),
-  myActiveLightsIterator(),
+V3d_View::V3d_View (const Handle(V3d_Viewer)& theViewer, const Handle(V3d_View)& theView)
+: MyViewer (theViewer.operator->()),
   SwitchSetFront(Standard_False),
+  myZRotation (Standard_False),
   MyTrsf (1, 4, 1, 4)
 {
-  Handle(Visual3d_View) aFromView = theView->View();
-
-  myImmediateUpdate = Standard_False;
-  MyView = new Visual3d_View (MyViewer->Viewer());
-
-  for (theView->InitActiveLights(); theView->MoreActiveLights(); theView->NextActiveLights())
-  {
-    MyActiveLights.Append (theView->ActiveLight());
-  }
-
-  MyViewContext = aFromView->Context() ;
-
-  SetCamera (new Graphic3d_Camera (theView->Camera()));
-  myAutoZFitIsOn        = theView->AutoZFitMode();
-  myAutoZFitScaleFactor = theView->AutoZFitScaleFactor();
+  myView = theViewer->Driver()->CreateView (theViewer->StructureManager());
 
-  MyBackground = aFromView->Background() ;
-  MyGradientBackground = aFromView->GradientBackground();
+  myView->CopySettings (theView->View());
+  myDefaultViewPoint = theView->myDefaultViewPoint;
+  myDefaultViewAxis  = theView->myDefaultViewAxis;
 
-  MyView->SetContext (MyViewContext) ;
-
-  SetAxis (0.0, 0.0, 0.0, 1.0, 1.0, 1.0);
-
-  SetViewMappingDefault();
-  SetViewOrientationDefault();
-  theVM->AddView (this);
+  myDefaultCamera = new Graphic3d_Camera (theView->DefaultCamera());
 
+  myImmediateUpdate = Standard_False;
+  SetAutoZFitMode (theView->AutoZFitMode(), theView->AutoZFitScaleFactor());
+  theViewer->AddView (this);
   Init();
-
   myImmediateUpdate = Standard_True;
 }
 
 //=============================================================================
-//function : SetMagnify
+//function : Destructor
 //purpose  :
 //=============================================================================
-void V3d_View::SetMagnify(const Handle(Aspect_Window)& TheWindow,
-                          const Handle(V3d_View)& aPreviousView,
-                          const Standard_Integer x1,
-                          const Standard_Integer y1,
-                          const Standard_Integer x2,
-                          const Standard_Integer y2)
-{
-  if( !MyView->IsDefined() ) {
-    Standard_Real a,b,c,d;
-    aPreviousView->Convert(x1,y1,a,b);
-    aPreviousView->Convert(x2,y2,c,d);
-    MyView->SetWindow(TheWindow) ;
-    FitAll(TheWindow,a,b,c,d);
-    MyView->SetContext(MyViewContext) ;
-    MyView->SetBackground(MyBackground) ;
-    MyViewer->SetViewOn(this) ;
-    MyWindow = TheWindow;
-    MyView->Redraw() ;
-    SetViewMappingDefault();
+V3d_View::~V3d_View()
+{
+  if (!myView->IsRemoved())
+  {
+    myView->Remove();
   }
 }
 
 //=============================================================================
-//function : SetWindow
+//function : SetMagnify
 //purpose  :
 //=============================================================================
-void V3d_View::SetWindow(const Handle(Aspect_Window)& TheWindow)
+void V3d_View::SetMagnify (const Handle(Aspect_Window)& theWindow,
+                           const Handle(V3d_View)& thePreviousView,
+                           const Standard_Integer theX1,
+                           const Standard_Integer theY1,
+                           const Standard_Integer theX2,
+                           const Standard_Integer theY2)
 {
-  Standard_MultiplyDefined_Raise_if( MyView->IsDefined(),
-    "V3d_View::SetWindow, window of view already defined");
-
-  MyView->SetWindow(TheWindow) ;
-  // AGV: Method V3d_View::SetWindow() should assign the field MyWindow before
-  // calling Redraw(). Otherwise it is impossible to call certain methods of
-  // V3d_View like Convert() inside the context of Redraw(),
-  // particularly in class NIS_View.
-  MyWindow = TheWindow;
-  // SetWindow carries out SetRatio and modifies
-  MyView->SetContext(MyViewContext) ;
-  MyView->SetBackground(MyBackground) ;
-  MyViewer->SetViewOn(this) ;
-  MyView->Redraw() ;
+  if (!myView->IsRemoved() && !myView->IsDefined())
+  {
+    Standard_Real aU1, aV1, aU2, aV2;
+    thePreviousView->Convert (theX1, theY1, aU1, aV1);
+    thePreviousView->Convert (theX2, theY2, aU2, aV2);
+    myView->SetWindow (theWindow);
+    FitAll (aU1, aV1, aU2, aV2);
+    MyViewer->SetViewOn (this);
+    MyWindow = theWindow;
+    SetRatio();
+    Redraw();
+    SetViewMappingDefault();
+  }
 }
 
 //=============================================================================
 //function : SetWindow
 //purpose  :
 //=============================================================================
-void V3d_View::SetWindow(const Handle(Aspect_Window)&      aWindow,
-                         const Aspect_RenderingContext     aContext,
-                         const Aspect_GraphicCallbackProc& aDisplayCB,
-                         const Standard_Address            aClientData)
+void V3d_View::SetWindow (const Handle(Aspect_Window)&  theWindow,
+                          const Aspect_RenderingContext theContext)
 {
-  Standard_MultiplyDefined_Raise_if( MyView->IsDefined(),
-    "V3d_View::SetWindow, "
-    "window of view already defined");
-  // AGV: Method V3d_View::SetWindow() should assign the field MyWindow before
-  // calling Redraw(). Otherwise it is impossible to call certain methods of
-  // V3d_View like Convert() inside the context of Redraw(),
-  // particularly in class NIS_View.
-  MyWindow = aWindow;
-  MyView->SetWindow(aWindow, aContext, aDisplayCB, aClientData) ;
-  MyView->SetContext(MyViewContext) ;
-  MyView->SetBackground(MyBackground) ;
-  MyViewer->SetViewOn(this) ;
-  MyView->Redraw() ;
+  if (myView->IsRemoved())
+  {
+    return;
+  }
+
+  // method V3d_View::SetWindow() should assign the field MyWindow before calling Redraw()
+  MyWindow = theWindow;
+  myView->SetWindow (theWindow, theContext);
+  MyViewer->SetViewOn (this);
+  SetRatio();
+  if (myImmediateUpdate)
+  {
+    Redraw();
+  }
 }
 
 //=============================================================================
@@ -384,8 +201,17 @@ void V3d_View::SetWindow(const Handle(Aspect_Window)&      aWindow,
 //=============================================================================
 void V3d_View::Remove() const
 {
+  if (!MyGrid.IsNull())
+  {
+    MyGrid->Erase();
+  }
+  if (!myTrihedron.IsNull())
+  {
+    myTrihedron->Erase();
+  }
+
   MyViewer->DelView (this);
-  MyView->Remove();
+  myView->Remove();
   Handle(Aspect_Window)& aWin = const_cast<Handle(Aspect_Window)&> (MyWindow);
   aWin.Nullify();
 }
@@ -396,7 +222,15 @@ void V3d_View::Remove() const
 //=============================================================================
 void V3d_View::Update() const
 {
-  if( MyView->IsDefined() )  MyView->Update() ;
+  if (!myView->IsDefined()
+   || !myView->IsActive())
+  {
+    return;
+  }
+
+  myView->Update();
+  myView->Compute();
+  myView->Redraw();
 }
 
 //=============================================================================
@@ -405,7 +239,29 @@ void V3d_View::Update() const
 //=============================================================================
 void V3d_View::Redraw() const
 {
-  if( MyView->IsDefined() ) MyView->Redraw() ;
+  if (!myView->IsDefined()
+   || !myView->IsActive())
+  {
+    return;
+  }
+
+  Handle(Graphic3d_StructureManager) aStructureMgr  = MyViewer->StructureManager();
+  for (Standard_Integer aRetryIter = 0; aRetryIter < 2; ++aRetryIter)
+  {
+    if (aStructureMgr->IsDeviceLost())
+    {
+      aStructureMgr->RecomputeStructures();
+    }
+
+    AutoZFit();
+
+    myView->Redraw();
+
+    if (!aStructureMgr->IsDeviceLost())
+    {
+      return;
+    }
+  }
 }
 
 //=============================================================================
@@ -414,10 +270,13 @@ void V3d_View::Redraw() const
 //=============================================================================
 void V3d_View::RedrawImmediate() const
 {
-  if (MyView->IsDefined())
+  if (!myView->IsDefined()
+   || !myView->IsActive())
   {
-    MyView->RedrawImmediate();
+    return;
   }
+
+  myView->RedrawImmediate();
 }
 
 //=============================================================================
@@ -426,20 +285,60 @@ void V3d_View::RedrawImmediate() const
 //=============================================================================
 void V3d_View::Invalidate() const
 {
-  if (MyView->IsDefined())
+  if (!myView->IsDefined())
   {
-    MyView->Invalidate();
+    return;
   }
+
+  myView->Invalidate();
 }
 
 //=============================================================================
-//function : Redraw
+//function : IsInvalidated
+//purpose  :
+//=============================================================================
+Standard_Boolean V3d_View::IsInvalidated() const
+{
+  return !myView->IsDefined()
+       || myView->IsInvalidated();
+}
+
+// ========================================================================
+// function : SetAutoZFitMode
+// purpose  :
+// ========================================================================
+void V3d_View::SetAutoZFitMode (const Standard_Boolean theIsOn,
+                                const Standard_Real    theScaleFactor)
+{
+  Standard_ASSERT_RAISE (theScaleFactor > 0.0, "Zero or negative scale factor is not allowed.");
+  myAutoZFitScaleFactor = theScaleFactor;
+  myAutoZFitIsOn = theIsOn;
+}
+
+//=============================================================================
+//function : AutoZFit
 //purpose  :
 //=============================================================================
-void V3d_View::Redraw(const Standard_Integer xc,const Standard_Integer yc,
-                      const Standard_Integer width,const Standard_Integer height) const
+void V3d_View::AutoZFit() const
 {
-  if( MyView->IsDefined() ) MyView->Redraw(xc,yc,width,height) ;
+  if (!AutoZFitMode())
+  {
+    return;
+  }
+
+  ZFitAll (myAutoZFitScaleFactor);
+}
+
+//=============================================================================
+//function : ZFitAll
+//purpose  :
+//=============================================================================
+void V3d_View::ZFitAll (const Standard_Real theScaleFactor) const
+{
+  Bnd_Box aMinMaxBox   = myView->MinMaxValues (Standard_False); // applicative min max boundaries
+  Bnd_Box aGraphicBox  = myView->MinMaxValues (Standard_True);  // real graphical boundaries (not accounting infinite flag).
+
+  myView->Camera()->ZFitAll (theScaleFactor, aMinMaxBox, aGraphicBox);
 }
 
 //=============================================================================
@@ -449,8 +348,8 @@ void V3d_View::Redraw(const Standard_Integer xc,const Standard_Integer yc,
 Standard_Boolean V3d_View::IsEmpty() const
 {
   Standard_Boolean TheStatus = Standard_True ;
-  if( MyView->IsDefined() ) {
-    Standard_Integer Nstruct = MyView->NumberOfDisplayedStructures() ;
+  if( myView->IsDefined() ) {
+    Standard_Integer Nstruct = myView->NumberOfDisplayedStructures() ;
     if( Nstruct > 0 ) TheStatus = Standard_False ;
   }
   return (TheStatus) ;
@@ -462,8 +361,12 @@ Standard_Boolean V3d_View::IsEmpty() const
 //=============================================================================
 void V3d_View::UpdateLights() const
 {
-  MyView->SetContext(MyViewContext);
-  Update();
+  Handle(Graphic3d_LightSet) aLights = new Graphic3d_LightSet();
+  for (V3d_ListOfLight::Iterator anActiveLightIter (myActiveLights); anActiveLightIter.More(); anActiveLightIter.Next())
+  {
+    aLights->Add (anActiveLightIter.Value());
+  }
+  myView->SetLights (aLights);
 }
 
 //=============================================================================
@@ -472,9 +375,12 @@ void V3d_View::UpdateLights() const
 //=============================================================================
 void V3d_View::DoMapping()
 {
-  if( MyView->IsDefined() ) {
-    (MyView->Window())->DoMapping() ;
+  if (!myView->IsDefined())
+  {
+    return;
   }
+
+  myView->Window()->DoMapping();
 }
 
 //=============================================================================
@@ -483,12 +389,17 @@ void V3d_View::DoMapping()
 //=============================================================================
 void V3d_View::MustBeResized()
 {
-  if ( !MyLayerMgr.IsNull() )
-    MyLayerMgr->Resized();
+  if (!myView->IsDefined())
+  {
+    return;
+  }
 
-  if( MyView->IsDefined() ) {
-    MyView->Resized() ;
-    MyView->Redraw();
+  myView->Resized();
+
+  SetRatio();
+  if (myImmediateUpdate)
+  {
+    Redraw();
   }
 }
 
@@ -496,164 +407,141 @@ void V3d_View::MustBeResized()
 //function : SetBackgroundColor
 //purpose  :
 //=============================================================================
-void V3d_View::SetBackgroundColor(const Quantity_TypeOfColor Type, const Standard_Real v1, const Standard_Real v2, const Standard_Real v3)
+void V3d_View::SetBackgroundColor (const Quantity_TypeOfColor theType,
+                                   const Standard_Real theV1,
+                                   const Standard_Real theV2,
+                                   const Standard_Real theV3)
 {
-  Standard_Real V1 = Max( Min( v1, 1.0 ), 0.0 );
-  Standard_Real V2 = Max( Min( v2, 1.0 ), 0.0 );
-  Standard_Real V3 = Max( Min( v3, 1.0 ), 0.0 );
+  Standard_Real aV1 = Max (Min (theV1, 1.0), 0.0);
+  Standard_Real aV2 = Max (Min (theV2, 1.0), 0.0);
+  Standard_Real aV3 = Max (Min (theV3, 1.0), 0.0);
 
-  Quantity_Color C( V1, V2, V3, Type );
-  SetBackgroundColor( C );
+  SetBackgroundColor (Quantity_Color (aV1, aV2, aV3, theType));
 }
 
 //=============================================================================
 //function : SetBackgroundColor
 //purpose  :
 //=============================================================================
-void V3d_View::SetBackgroundColor(const Quantity_Color &Color)
+void V3d_View::SetBackgroundColor (const Quantity_Color& theColor)
 {
-  MyBackground.SetColor( Color );
-  if ( MyView->IsDefined() )
-    MyView->SetBackground( MyBackground );
-  //szv: Why?
-  if ( !MyLayerMgr.IsNull() )
-    MyLayerMgr->Resized();
-}
+  myView->SetBackground (Aspect_Background (theColor));
 
-//=============================================================================
-//function : SetBackgroundColor
-//purpose  :
-//=============================================================================
-void V3d_View::SetBackgroundColor(const Quantity_NameOfColor Name)
-{
-  Quantity_Color C( Name );
-  SetBackgroundColor( C );
+  if (myImmediateUpdate)
+  {
+    Redraw();
+  }
 }
 
 //=============================================================================
 //function : SetBgGradientColors
 //purpose  :
 //=============================================================================
-void V3d_View::SetBgGradientColors( const Quantity_Color& Color1,
-                                    const Quantity_Color& Color2,
-                                    const Aspect_GradientFillMethod FillStyle,
-                                    const Standard_Boolean status)
+void V3d_View::SetBgGradientColors (const Quantity_Color& theColor1,
+                                    const Quantity_Color& theColor2,
+                                    const Aspect_GradientFillMethod theFillStyle,
+                                    const Standard_Boolean theToUpdate)
 {
-  MyGradientBackground.SetColors(Color1, Color2, FillStyle);
-  if ( MyView->IsDefined() )
-    MyView->SetGradientBackground( MyGradientBackground, status );
-}
+  Aspect_GradientBackground aGradientBg (theColor1, theColor2, theFillStyle);
 
-//=============================================================================
-//function : SetBgGradientColors
-//purpose  :
-//=============================================================================
-void V3d_View::SetBgGradientColors( const Quantity_NameOfColor Color1,
-                                    const Quantity_NameOfColor Color2,
-                                    const Aspect_GradientFillMethod FillStyle,
-                                    const Standard_Boolean status )
-{
-  Quantity_Color C1( Color1 );
-  Quantity_Color C2( Color2 );
-  MyGradientBackground.SetColors( C1, C2, FillStyle );
-  if ( MyView->IsDefined() )
-    MyView->SetGradientBackground( MyGradientBackground, status );
+  myView->SetGradientBackground (aGradientBg);
+
+  if (myImmediateUpdate || theToUpdate)
+  {
+    Redraw();
+  }
 }
 
 //=============================================================================
 //function : SetBgGradientStyle
 //purpose  :
 //=============================================================================
-void V3d_View::SetBgGradientStyle( const Aspect_GradientFillMethod FillStyle,
-                                   const Standard_Boolean update)
+void V3d_View::SetBgGradientStyle (const Aspect_GradientFillMethod theFillStyle, const Standard_Boolean theToUpdate)
 {
-  Quantity_Color Color1, Color2;
-  MyGradientBackground.Colors( Color1, Color2 );
-  MyGradientBackground.SetColors( Color1, Color2, FillStyle );
-  if( MyView->IsDefined() )
-    MyView->SetBgGradientStyle( FillStyle, update ) ;
+  Quantity_Color aColor1;
+  Quantity_Color aColor2;
+  GradientBackground().Colors (aColor1, aColor2);
+
+  SetBgGradientColors (aColor1, aColor2, theFillStyle, theToUpdate);
 }
 
 //=============================================================================
 //function : SetBackgroundImage
 //purpose  :
 //=============================================================================
-void V3d_View::SetBackgroundImage( const Standard_CString FileName,
-                                   const Aspect_FillMethod FillStyle,
-                                   const Standard_Boolean update )
+void V3d_View::SetBackgroundImage (const Standard_CString theFileName,
+                                   const Aspect_FillMethod theFillStyle,
+                                   const Standard_Boolean theToUpdate)
 {
-#ifdef OCC1188
-  if( MyView->IsDefined() )
-    MyView->SetBackgroundImage( FileName, FillStyle, update ) ;
-#endif
+  myView->SetBackgroundImage (theFileName);
+  myView->SetBackgroundImageStyle (theFillStyle);
+
+  if (myImmediateUpdate || theToUpdate)
+  {
+    Redraw();
+  }
 }
 
 //=============================================================================
 //function : SetBgImageStyle
 //purpose  :
 //=============================================================================
-void V3d_View::SetBgImageStyle( const Aspect_FillMethod FillStyle,
-                                const Standard_Boolean update )
+void V3d_View::SetBgImageStyle (const Aspect_FillMethod theFillStyle, const Standard_Boolean theToUpdate)
 {
-#ifdef OCC1188
-  if( MyView->IsDefined() )
-    MyView->SetBgImageStyle( FillStyle, update ) ;
-#endif
+  myView->SetBackgroundImageStyle (theFillStyle);
+
+  if (myImmediateUpdate || theToUpdate)
+  {
+    Redraw();
+  }
 }
 
 //=============================================================================
 //function : SetAxis
 //purpose  :
 //=============================================================================
-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)
+void V3d_View::SetAxis (const Standard_Real theX,  const Standard_Real theY,  const Standard_Real theZ,
+                        const Standard_Real theVx, const Standard_Real theVy, const Standard_Real theVz)
 {
-  Standard_Real D,Nx = Vx,Ny = Vy,Nz = Vz ;
-
-  D = Sqrt( Vx*Vx + Vy*Vy + Vz*Vz ) ;
-  V3d_BadValue_Raise_if ( D <= 0. , "V3d_View::SetAxis, bad axis");
-  Nx /= D ; Ny /= D ; Nz /= D ;
-  MyDefaultViewPoint.SetCoord(X,Y,Z) ;
-  MyDefaultViewAxis.SetCoord(Nx,Ny,Nz) ;
+  myDefaultViewPoint.SetCoord (theX, theY, theZ);
+  myDefaultViewAxis.SetCoord (theVx, theVy, theVz);
 }
 
 //=============================================================================
 //function : SetShadingModel
 //purpose  :
 //=============================================================================
-void V3d_View::SetShadingModel(const V3d_TypeOfShadingModel Model)
-{
-  MyViewContext.SetModel((Visual3d_TypeOfModel) Model) ;
-  MyView->SetContext(MyViewContext) ;
-}
-
-//=============================================================================
-//function : SetSurfaceDetail
-//purpose  :
-//=============================================================================
-void V3d_View::SetSurfaceDetail(const V3d_TypeOfSurfaceDetail Model)
+void V3d_View::SetShadingModel (const Graphic3d_TypeOfShadingModel theShadingModel)
 {
-  MyViewContext.SetSurfaceDetail((Visual3d_TypeOfSurfaceDetail) Model) ;
-  MyView->SetContext(MyViewContext) ;
+  myView->SetShadingModel (theShadingModel);
 }
 
 //=============================================================================
 //function : SetTextureEnv
 //purpose  :
 //=============================================================================
-void V3d_View::SetTextureEnv(const Handle(Graphic3d_TextureEnv)& ATexture)
+void V3d_View::SetTextureEnv (const Handle(Graphic3d_TextureEnv)& theTexture)
 {
-  MyViewContext.SetTextureEnv(ATexture) ;
-  MyView->SetContext(MyViewContext) ;
+  myView->SetTextureEnv (theTexture);
+
+  if (myImmediateUpdate)
+  {
+    Redraw();
+  }
 }
 
 //=============================================================================
 //function : SetVisualization
 //purpose  :
 //=============================================================================
-void V3d_View::SetVisualization(const V3d_TypeOfVisualization Mode)
+void V3d_View::SetVisualization (const V3d_TypeOfVisualization theType)
 {
-  MyViewContext.SetVisualization((Visual3d_TypeOfVisualization) Mode);
-  MyView->SetContext(MyViewContext) ;
+  myView->SetVisualizationType (static_cast <Graphic3d_TypeOfVisualization> (theType));
+
+  if (myImmediateUpdate)
+  {
+    Redraw();
+  }
 }
 
 //=============================================================================
@@ -669,12 +557,20 @@ void V3d_View::SetFront()
   a.YDirection().Coord(xu,yu,zu);
   a.Location().Coord(xo,yo,zo);
 
-  myCamera->SetCenter (gp_Pnt (xo, yo, zo));
+  Handle(Graphic3d_Camera) aCamera = Camera();
+
+  aCamera->SetCenter (gp_Pnt (xo, yo, zo));
+
   if(SwitchSetFront)
-    myCamera->SetDirection (gp_Dir (vx, vy, vz));
+  {
+    aCamera->SetDirection (gp_Dir (vx, vy, vz));
+  }
   else
-    myCamera->SetDirection (gp_Dir (vx, vy, vz).Reversed());
-  myCamera->SetUp (gp_Dir (xu, yu, zu));
+  {
+    aCamera->SetDirection (gp_Dir (vx, vy, vz).Reversed());
+  }
+
+  aCamera->SetUp (gp_Dir (xu, yu, zu));
 
   AutoZFit();
 
@@ -703,16 +599,18 @@ void V3d_View::Rotate (const Standard_Real ax,
   if( Az > 0. ) while ( Az > DEUXPI ) Az -= DEUXPI;
   else if( Az < 0. ) while ( Az < -DEUXPI ) Az += DEUXPI;
 
+  Handle(Graphic3d_Camera) aCamera = Camera();
+
   if (Start)
   {
-    myCamStartOpUp = myCamera->Up();
-    myCamStartOpEye = myCamera->Eye();
-    myCamStartOpCenter = myCamera->Center();
+    myCamStartOpUp     = aCamera->Up();
+    myCamStartOpEye    = aCamera->Eye();
+    myCamStartOpCenter = aCamera->Center();
   }
 
-  myCamera->SetUp (myCamStartOpUp);
-  myCamera->SetEye (myCamStartOpEye);
-  myCamera->SetCenter (myCamStartOpCenter);
+  aCamera->SetUp     (myCamStartOpUp);
+  aCamera->SetEye    (myCamStartOpEye);
+  aCamera->SetCenter (myCamStartOpCenter);
 
   // rotate camera around 3 initial axes
   gp_Dir aBackDir (gp_Vec (myCamStartOpCenter, myCamStartOpEye));
@@ -728,7 +626,7 @@ void V3d_View::Rotate (const Standard_Real ax,
   aTrsf.Multiply (aRot[1]);
   aTrsf.Multiply (aRot[2]);
 
-  myCamera->Transform (aTrsf);
+  aCamera->Transform (aTrsf);
 
   AutoZFit();
 
@@ -754,25 +652,27 @@ void V3d_View::Rotate(const Standard_Real ax, const Standard_Real ay, const Stan
   if( Az > 0. ) while ( Az > DEUXPI ) Az -= DEUXPI ;
   else if( Az < 0. ) while ( Az < -DEUXPI ) Az += DEUXPI ;
 
+  Handle(Graphic3d_Camera) aCamera = Camera();
+
   if (Start)
   {
     myGravityReferencePoint.SetCoord (X, Y, Z);
-    myCamStartOpUp = myCamera->Up();
-    myCamStartOpEye = myCamera->Eye();
-    myCamStartOpCenter = myCamera->Center();
+    myCamStartOpUp     = aCamera->Up();
+    myCamStartOpEye    = aCamera->Eye();
+    myCamStartOpCenter = aCamera->Center();
   }
 
   const Graphic3d_Vertex& aVref = myGravityReferencePoint;
 
-  myCamera->SetUp (myCamStartOpUp);
-  myCamera->SetEye (myCamStartOpEye);
-  myCamera->SetCenter (myCamStartOpCenter);
+  aCamera->SetUp     (myCamStartOpUp);
+  aCamera->SetEye    (myCamStartOpEye);
+  aCamera->SetCenter (myCamStartOpCenter);
 
   // rotate camera around 3 initial axes
   gp_Pnt aRCenter (aVref.X(), aVref.Y(), aVref.Z());
 
-  gp_Dir aZAxis (myCamera->Direction().Reversed());
-  gp_Dir aYAxis (myCamera->Up());
+  gp_Dir aZAxis (aCamera->Direction().Reversed());
+  gp_Dir aYAxis (aCamera->Up());
   gp_Dir aXAxis (aYAxis.Crossed (aZAxis)); 
 
   gp_Trsf aRot[3], aTrsf;
@@ -783,7 +683,7 @@ void V3d_View::Rotate(const Standard_Real ax, const Standard_Real ay, const Stan
   aTrsf.Multiply (aRot[1]);
   aTrsf.Multiply (aRot[2]);
 
-  myCamera->Transform (aTrsf);
+  aCamera->Transform (aTrsf);
 
   AutoZFit();
 
@@ -813,53 +713,51 @@ void V3d_View::Rotate(const V3d_TypeOfAxe Axe, const Standard_Real angle, const
 //function : Rotate
 //purpose  :
 //=============================================================================
-void V3d_View::Rotate(const V3d_TypeOfAxe Axe, const Standard_Real angle,
-                      const Standard_Real X, const Standard_Real Y, const Standard_Real Z, const Standard_Boolean Start)
+void V3d_View::Rotate (const V3d_TypeOfAxe theAxe, const Standard_Real theAngle,
+                       const Standard_Real theX, const Standard_Real theY, const Standard_Real theZ, const Standard_Boolean theStart)
 {
-  Standard_Real Angle = angle ;
+  Standard_Real anAngle = theAngle;
 
-  if( Angle > 0. ) while ( Angle > DEUXPI ) Angle -= DEUXPI ;
-  else if( Angle < 0. ) while ( Angle < -DEUXPI ) Angle += DEUXPI ;
+  if (anAngle > 0.0) while (anAngle > DEUXPI) anAngle -= DEUXPI;
+  else if (anAngle < 0.0) while (anAngle < -DEUXPI) anAngle += DEUXPI;
 
-  if (Start)
+  Handle(Graphic3d_Camera) aCamera = Camera();
+
+  if (theStart)
   {
-    myGravityReferencePoint.SetCoord (X, Y, Z);
-    myCamStartOpUp = myCamera->Up();
-    myCamStartOpEye = myCamera->Eye();
-    myCamStartOpCenter = myCamera->Center();
-
-    switch (Axe) {
-    case V3d_X :
-      myViewAxis.SetCoord(1.,0.,0.) ;
-      break ;
-    case V3d_Y :
-      myViewAxis.SetCoord(0.,1.,0.) ;
-      break ;
-    case V3d_Z :
-      myViewAxis.SetCoord(0.,0.,1.) ;
-      break ;
+    myGravityReferencePoint.SetCoord (theX, theY, theZ);
+    myCamStartOpUp     = aCamera->Up();
+    myCamStartOpEye    = aCamera->Eye();
+    myCamStartOpCenter = aCamera->Center();
+
+    switch (theAxe)
+    {
+      case V3d_X: myViewAxis = gp::DX(); break;
+      case V3d_Y: myViewAxis = gp::DY(); break;
+      case V3d_Z: myViewAxis = gp::DZ(); break;
     }
 
-    myCamStartOpUp = myCamera->Up();
-    myCamStartOpEye = myCamera->Eye();
-    myCamStartOpCenter = myCamera->Center();
+    myCamStartOpUp     = aCamera->Up();
+    myCamStartOpEye    = aCamera->Eye();
+    myCamStartOpCenter = aCamera->Center();
   }
 
   const Graphic3d_Vertex& aVref = myGravityReferencePoint;
 
-  myCamera->SetUp (myCamStartOpUp);
-  myCamera->SetEye (myCamStartOpEye);
-  myCamera->SetCenter (myCamStartOpCenter);
+  aCamera->SetUp     (myCamStartOpUp);
+  aCamera->SetEye    (myCamStartOpEye);
+  aCamera->SetCenter (myCamStartOpCenter);
 
   // rotate camera around passed axis
   gp_Trsf aRotation;
   gp_Pnt aRCenter (aVref.X(), aVref.Y(), aVref.Z());
-  gp_Dir aRAxis ((Axe == V3d_X) ? 1.0 : 0.0,
-                  (Axe == V3d_Y) ? 1.0 : 0.0,
-                  (Axe == V3d_Z) ? 1.0 : 0.0);
+  gp_Dir aRAxis ((theAxe == V3d_X) ? 1.0 : 0.0,
+                 (theAxe == V3d_Y) ? 1.0 : 0.0,
+                 (theAxe == V3d_Z) ? 1.0 : 0.0);
 
-  aRotation.SetRotation (gp_Ax1 (aRCenter, aRAxis), Angle);
-  myCamera->Transform (aRotation);
+  aRotation.SetRotation (gp_Ax1 (aRCenter, aRAxis), anAngle);
+
+  aCamera->Transform (aRotation);
 
   AutoZFit();
 
@@ -877,24 +775,24 @@ void V3d_View::Rotate(const Standard_Real angle, const Standard_Boolean Start)
   if( Angle > 0. ) while ( Angle > DEUXPI ) Angle -= DEUXPI ;
   else if( Angle < 0. ) while ( Angle < -DEUXPI ) Angle += DEUXPI ;
 
+  Handle(Graphic3d_Camera) aCamera = Camera();
+
   if( Start ) {
-    myCamStartOpUp = myCamera->Up();
-    myCamStartOpEye = myCamera->Eye();
-    myCamStartOpCenter = myCamera->Center();
+    myCamStartOpUp     = aCamera->Up();
+    myCamStartOpEye    = aCamera->Eye();
+    myCamStartOpCenter = aCamera->Center();
   }
 
-  const Graphic3d_Vertex& aPnt = MyDefaultViewPoint;
-  const Graphic3d_Vector& anAxis = MyDefaultViewAxis;
-
-  myCamera->SetUp (myCamStartOpUp);
-  myCamera->SetEye (myCamStartOpEye);
-  myCamera->SetCenter (myCamStartOpCenter);
+  aCamera->SetUp     (myCamStartOpUp);
+  aCamera->SetEye    (myCamStartOpEye);
+  aCamera->SetCenter (myCamStartOpCenter);
 
   gp_Trsf aRotation;
-  gp_Pnt aRCenter (aPnt.X(), aPnt.Y(), aPnt.Z());
-  gp_Dir aRAxis (anAxis.X(), anAxis.Y(), anAxis.Z());
+  gp_Pnt aRCenter (myDefaultViewPoint);
+  gp_Dir aRAxis (myDefaultViewAxis);
   aRotation.SetRotation (gp_Ax1 (aRCenter, aRAxis), Angle);
-  myCamera->Transform (aRotation);
+
+  aCamera->Transform (aRotation);
 
   AutoZFit();
 
@@ -918,20 +816,22 @@ void V3d_View::Turn(const Standard_Real ax, const Standard_Real ay, const Standa
   if( Az > 0. ) while ( Az > DEUXPI ) Az -= DEUXPI ;
   else if( Az < 0. ) while ( Az < -DEUXPI ) Az += DEUXPI ;
 
+  Handle(Graphic3d_Camera) aCamera = Camera();
+
   if( Start ) {
-    myCamStartOpUp = myCamera->Up();
-    myCamStartOpEye = myCamera->Eye();
-    myCamStartOpCenter = myCamera->Center();
+    myCamStartOpUp     = aCamera->Up();
+    myCamStartOpEye    = aCamera->Eye();
+    myCamStartOpCenter = aCamera->Center();
   }
 
-  myCamera->SetUp (myCamStartOpUp);
-  myCamera->SetEye (myCamStartOpEye);
-  myCamera->SetCenter (myCamStartOpCenter);
+  aCamera->SetUp     (myCamStartOpUp);
+  aCamera->SetEye    (myCamStartOpEye);
+  aCamera->SetCenter (myCamStartOpCenter);
 
   // rotate camera around 3 initial axes
-  gp_Pnt aRCenter = myCamera->Eye();
-  gp_Dir aZAxis (myCamera->Direction().Reversed());
-  gp_Dir aYAxis (myCamera->Up());
+  gp_Pnt aRCenter = aCamera->Eye();
+  gp_Dir aZAxis (aCamera->Direction().Reversed());
+  gp_Dir aYAxis (aCamera->Up());
   gp_Dir aXAxis (aYAxis.Crossed (aZAxis)); 
 
   gp_Trsf aRot[3], aTrsf;
@@ -942,7 +842,7 @@ void V3d_View::Turn(const Standard_Real ax, const Standard_Real ay, const Standa
   aTrsf.Multiply (aRot[1]);
   aTrsf.Multiply (aRot[2]);
 
-  myCamera->Transform (aTrsf);
+  aCamera->Transform (aTrsf);
 
   AutoZFit();
 
@@ -979,23 +879,24 @@ void V3d_View::Turn(const Standard_Real angle, const Standard_Boolean Start)
   if( Angle > 0. ) while ( Angle > DEUXPI ) Angle -= DEUXPI ;
   else if( Angle < 0. ) while ( Angle < -DEUXPI ) Angle += DEUXPI ;
 
+  Handle(Graphic3d_Camera) aCamera = Camera();
+
   if( Start ) {
-    myCamStartOpUp = myCamera->Up();
-    myCamStartOpEye = myCamera->Eye();
-    myCamStartOpCenter = myCamera->Center();
+    myCamStartOpUp     = aCamera->Up();
+    myCamStartOpEye    = aCamera->Eye();
+    myCamStartOpCenter = aCamera->Center();
   }
 
-  myCamera->SetUp (myCamStartOpUp);
-  myCamera->SetEye (myCamStartOpEye);
-  myCamera->SetCenter (myCamStartOpCenter);
-
-  const Graphic3d_Vector& anAxis = MyDefaultViewAxis;
+  aCamera->SetUp     (myCamStartOpUp);
+  aCamera->SetEye    (myCamStartOpEye);
+  aCamera->SetCenter (myCamStartOpCenter);
 
   gp_Trsf aRotation;
-  gp_Pnt aRCenter = myCamera->Eye();
-  gp_Dir aRAxis (anAxis.X(), anAxis.Y(), anAxis.Z());
+  gp_Pnt aRCenter = aCamera->Eye();
+  gp_Dir aRAxis (myDefaultViewAxis);
   aRotation.SetRotation (gp_Ax1 (aRCenter, aRAxis), Angle);
-  myCamera->Transform (aRotation);
+
+  aCamera->Transform (aRotation);
 
   AutoZFit();
 
@@ -1009,77 +910,34 @@ void V3d_View::Turn(const Standard_Real angle, const Standard_Boolean Start)
 void V3d_View::SetTwist(const Standard_Real angle)
 {
   Standard_Real Angle = angle ;
-  Standard_Boolean TheStatus;
 
   if( Angle > 0. ) while ( Angle > DEUXPI ) Angle -= DEUXPI ;
   else if( Angle < 0. ) while ( Angle < -DEUXPI ) Angle += DEUXPI ;
 
-  gp_Dir aReferencePlane (myCamera->Direction().Reversed());
-  gp_Dir anUp;
+  Handle(Graphic3d_Camera) aCamera = Camera();
 
-  anUp = gp_Dir (0.0, 0.0, 1.0);
-
-  TheStatus = ScreenAxis(aReferencePlane, anUp,
-    myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
-  if( !TheStatus ) {
-    anUp = gp_Dir (0.0, 1.0, 0.0);
-    TheStatus = ScreenAxis(aReferencePlane, anUp,
-      myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
-  }
-  if( !TheStatus ) {
-    anUp = gp_Dir (1.0, 0.0, 0.0);
-    TheStatus = ScreenAxis(aReferencePlane, anUp,
-      myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
+  const gp_Dir aReferencePlane (aCamera->Direction().Reversed());
+  if (!screenAxis (aReferencePlane, gp::DZ(), myXscreenAxis, myYscreenAxis, myZscreenAxis)
+   && !screenAxis (aReferencePlane, gp::DY(), myXscreenAxis, myYscreenAxis, myZscreenAxis)
+   && !screenAxis (aReferencePlane, gp::DZ(), myXscreenAxis, myYscreenAxis, myZscreenAxis))
+  {
+    throw V3d_BadValue ("V3d_ViewSetTwist, alignment of Eye,At,Up,");
   }
-
-  V3d_BadValue_Raise_if( !TheStatus,"V3d_ViewSetTwist, alignment of Eye,At,Up,");
   
-  gp_Pnt aRCenter = myCamera->Center();
-  gp_Dir aZAxis (myCamera->Direction().Reversed());
+  gp_Pnt aRCenter = aCamera->Center();
+  gp_Dir aZAxis (aCamera->Direction().Reversed());
 
   gp_Trsf aTrsf;
   aTrsf.SetRotation (gp_Ax1 (aRCenter, aZAxis), Angle);
 
-  Standard_Real myYscreenAxisX, myYscreenAxisY, myYscreenAxisZ;
-  myYscreenAxis.Coord (myYscreenAxisX, myYscreenAxisY, myYscreenAxisZ);
-
-  myCamera->SetUp (gp_Dir (myYscreenAxisX, myYscreenAxisY, myYscreenAxisZ));
-  myCamera->Transform (aTrsf);
+  aCamera->SetUp (gp_Dir (myYscreenAxis));
+  aCamera->Transform (aTrsf);
 
   AutoZFit();
 
   ImmediateUpdate();
 }
 
-//=============================================================================
-//function : SetAutoZFitMode
-//purpose  :
-//=============================================================================
-void V3d_View::SetAutoZFitMode (const Standard_Boolean theIsOn, const Standard_Real theScaleFactor)
-{
-  Standard_ASSERT_RAISE (theScaleFactor > 0.0, "Zero or negative scale factor is not allowed.");
-  myAutoZFitScaleFactor = theScaleFactor;
-  myAutoZFitIsOn = theIsOn;
-}
-
-//=============================================================================
-//function : AutoZFitMode
-//purpose  :
-//=============================================================================
-Standard_Boolean V3d_View::AutoZFitMode() const
-{
-  return myAutoZFitIsOn;
-}
-
-//=============================================================================
-//function : AutoZFitScaleFactor
-//purpose  :
-//=============================================================================
-Standard_Real V3d_View::AutoZFitScaleFactor () const
-{
-  return myAutoZFitScaleFactor;
-}
-
 //=============================================================================
 //function : SetEye
 //purpose  :
@@ -1090,7 +948,10 @@ void V3d_View::SetEye(const Standard_Real X,const Standard_Real Y,const Standard
 
   Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
 
-  myCamera->SetEye (gp_Pnt (X, Y, Z));
+  Handle(Graphic3d_Camera) aCamera = Camera();
+
+  aCamera->SetEye (gp_Pnt (X, Y, Z));
+
   SetTwist (aTwistBefore);
 
   AutoZFit();
@@ -1108,19 +969,21 @@ void V3d_View::SetDepth(const Standard_Real Depth)
 {
   V3d_BadValue_Raise_if (Depth == 0. ,"V3d_View::SetDepth, bad depth");
 
+  Handle(Graphic3d_Camera) aCamera = Camera();
+
   if( Depth > 0. )
   {
     // Move eye using center (target) as anchor.
-    myCamera->SetDistance (Depth);
+    aCamera->SetDistance (Depth);
   }
   else
   {
     // Move the view ref point instead of the eye.
-    gp_Vec aDir (myCamera->Direction());
-    gp_Pnt aCameraEye = myCamera->Eye();
+    gp_Vec aDir (aCamera->Direction());
+    gp_Pnt aCameraEye = aCamera->Eye();
     gp_Pnt aCameraCenter = aCameraEye.Translated (aDir.Multiplied (Abs (Depth)));
 
-    myCamera->SetCenter (aCameraCenter);
+    aCamera->SetCenter (aCameraCenter);
   }
 
   AutoZFit();
@@ -1141,12 +1004,9 @@ void V3d_View::SetProj( const Standard_Real Vx,const Standard_Real Vy, const Sta
 
   Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
 
-  myCamera->SetDirection (gp_Dir (Vx, Vy, Vz).Reversed());
+  Camera()->SetDirection (gp_Dir (Vx, Vy, Vz).Reversed());
 
-  if (MyProjModel == V3d_TPM_SCREEN)
-  {
-    SetTwist(aTwistBefore);
-  }
+  SetTwist(aTwistBefore);
 
   AutoZFit();
 
@@ -1159,36 +1019,42 @@ void V3d_View::SetProj( const Standard_Real Vx,const Standard_Real Vy, const Sta
 //function : SetProj
 //purpose  :
 //=============================================================================
-void V3d_View::SetProj( const V3d_TypeOfOrientation Orientation )
+void V3d_View::SetProj (const V3d_TypeOfOrientation theOrientation,
+                        const Standard_Boolean theIsYup)
 {
-  Standard_Real Xpn=0;
-  Standard_Real Ypn=0;
-  Standard_Real Zpn=0;
-
-  switch (Orientation) {
-  case V3d_Zpos :
-    Ypn = 1.;
-    break;
-  case V3d_Zneg :
-    Ypn = -1.;
-    break;
-  default:
-    Zpn = 1.;
+  Graphic3d_Vec3d anUp = theIsYup ? Graphic3d_Vec3d (0.0, 1.0, 0.0) : Graphic3d_Vec3d (0.0, 0.0, 1.0);
+  if (theIsYup)
+  {
+    if (theOrientation == V3d_Ypos
+     || theOrientation == V3d_Yneg)
+    {
+      anUp.SetValues (0.0, 0.0, -1.0);
+    }
+  }
+  else
+  {
+    if (theOrientation == V3d_Zpos)
+    {
+      anUp.SetValues (0.0, 1.0, 0.0);
+    }
+    else if (theOrientation == V3d_Zneg)
+    {
+      anUp.SetValues (0.0, -1.0, 0.0);
+    }
   }
 
-  const Graphic3d_Vector& aBck = V3d::GetProjAxis (Orientation);
+  const gp_Dir aBck = V3d::GetProjAxis (theOrientation);
 
   // retain camera panning from origin when switching projection
-  gp_Pnt anOriginVCS = myCamera->ConvertWorld2View (gp::Origin());
-  Standard_Real aPanX = anOriginVCS.X();
-  Standard_Real aPanY = anOriginVCS.Y();
+  const Handle(Graphic3d_Camera)& aCamera = Camera();
+  const gp_Pnt anOriginVCS = aCamera->ConvertWorld2View (gp::Origin());
 
-  myCamera->SetCenter (gp_Pnt (0, 0, 0));
-  myCamera->SetDirection (gp_Dir (aBck.X(), aBck.Y(), aBck.Z()).Reversed());
-  myCamera->SetUp (gp_Dir (Xpn, Ypn, Zpn));
-  myCamera->OrthogonalizeUp();
+  aCamera->SetCenter (gp_Pnt (0, 0, 0));
+  aCamera->SetDirection (gp_Dir (aBck.X(), aBck.Y(), aBck.Z()).Reversed());
+  aCamera->SetUp (gp_Dir (anUp.x(), anUp.y(), anUp.z()));
+  aCamera->OrthogonalizeUp();
 
-  Panning (aPanX, aPanY);
+  Panning (anOriginVCS.X(), anOriginVCS.Y());
 
   AutoZFit();
 
@@ -1205,7 +1071,7 @@ void V3d_View::SetAt(const Standard_Real X,const Standard_Real Y,const Standard_
 
   Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
 
-  myCamera->SetCenter (gp_Pnt (X, Y, Z));
+  Camera()->SetCenter (gp_Pnt (X, Y, Z));
 
   SetTwist (aTwistBefore);
 
@@ -1220,38 +1086,21 @@ void V3d_View::SetAt(const Standard_Real X,const Standard_Real Y,const Standard_
 //function : SetUp
 //purpose  :
 //=============================================================================
-void V3d_View::SetUp(const Standard_Real Vx,const Standard_Real Vy,const Standard_Real Vz)
+void V3d_View::SetUp (const Standard_Real theVx, const Standard_Real theVy, const Standard_Real theVz)
 {
-  Standard_Boolean TheStatus ;
-  V3d_BadValue_Raise_if( Sqrt(Vx*Vx + Vy*Vy + Vz*Vz) <= 0. ,
-    "V3d_View::SetUp, nullUp vector");
-
-  gp_Dir aReferencePlane (myCamera->Direction().Reversed());
-  gp_Dir anUp (Vx, Vy, Vz);
+  Handle(Graphic3d_Camera) aCamera = Camera();
 
-  TheStatus = ScreenAxis(aReferencePlane,anUp,
-    myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
-  if( !TheStatus ) {
-    anUp = gp_Dir (0.0, 0.0, 1.0);
-    TheStatus = ScreenAxis(aReferencePlane,anUp,
-      myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
-  }
-  if( !TheStatus ) {
-    anUp = gp_Dir (0.0, 1.0, 0.0);
-    TheStatus = ScreenAxis(aReferencePlane,anUp,
-      myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
-  }
-  if( !TheStatus ) {
-    anUp = gp_Dir (1.0, 0.0, 0.0);
-    TheStatus = ScreenAxis(aReferencePlane,anUp,
-      myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
+  const gp_Dir aReferencePlane (aCamera->Direction().Reversed());
+  const gp_Dir anUp (theVx, theVy, theVz);
+  if (!screenAxis (aReferencePlane, anUp,     myXscreenAxis, myYscreenAxis, myZscreenAxis)
+   && !screenAxis (aReferencePlane, gp::DZ(), myXscreenAxis, myYscreenAxis, myZscreenAxis)
+   && !screenAxis (aReferencePlane, gp::DY(), myXscreenAxis, myYscreenAxis, myZscreenAxis)
+   && !screenAxis (aReferencePlane, gp::DX(), myXscreenAxis, myYscreenAxis, myZscreenAxis))
+  {
+    throw V3d_BadValue ("V3d_View::Setup, alignment of Eye,At,Up");
   }
-  V3d_BadValue_Raise_if( !TheStatus,"V3d_View::Setup, alignment of Eye,At,Up");
-
-  Standard_Real myYscreenAxisX, myYscreenAxisY, myYscreenAxisZ;
-  myYscreenAxis.Coord (myYscreenAxisX, myYscreenAxisY, myYscreenAxisZ);
 
-  myCamera->SetUp (gp_Dir (myYscreenAxisX, myYscreenAxisY, myYscreenAxisZ));
+  aCamera->SetUp (gp_Dir (myYscreenAxis));
 
   AutoZFit();
 
@@ -1262,39 +1111,21 @@ void V3d_View::SetUp(const Standard_Real Vx,const Standard_Real Vy,const Standar
 //function : SetUp
 //purpose  :
 //=============================================================================
-void V3d_View::SetUp( const V3d_TypeOfOrientation Orientation )
+void V3d_View::SetUp (const V3d_TypeOfOrientation theOrientation)
 {
-  Standard_Boolean TheStatus ;
-
-  gp_Dir aReferencePlane (myCamera->Direction().Reversed());
-  gp_Dir anUp;
-
-  const Graphic3d_Vector& aViewReferenceUp = V3d::GetProjAxis(Orientation) ;
-  anUp = gp_Dir (aViewReferenceUp.X(), aViewReferenceUp.Y(), aViewReferenceUp.Z());
+  Handle(Graphic3d_Camera) aCamera = Camera();
 
-  TheStatus = ScreenAxis(aReferencePlane,anUp,
-    myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
-  if( !TheStatus ) {
-    anUp = gp_Dir (0.,0.,1.);
-    TheStatus = ScreenAxis(aReferencePlane,anUp,
-      myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
-  }
-  if( !TheStatus ) {
-    anUp = gp_Dir (0.,1.,0.);
-    TheStatus = ScreenAxis(aReferencePlane,anUp,
-      myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
-  }
-  if( !TheStatus ) {
-    anUp = gp_Dir (1.,0.,0.);
-    TheStatus = ScreenAxis(aReferencePlane,anUp,
-      myXscreenAxis,myYscreenAxis,myZscreenAxis) ;
+  const gp_Dir aReferencePlane (aCamera->Direction().Reversed());
+  const gp_Dir anUp = V3d::GetProjAxis (theOrientation);
+  if (!screenAxis (aReferencePlane, anUp,     myXscreenAxis, myYscreenAxis, myZscreenAxis)
+   && !screenAxis (aReferencePlane, gp::DZ(), myXscreenAxis, myYscreenAxis, myZscreenAxis)
+   && !screenAxis (aReferencePlane, gp::DY(), myXscreenAxis, myYscreenAxis, myZscreenAxis)
+   && !screenAxis (aReferencePlane, gp::DX(), myXscreenAxis, myYscreenAxis, myZscreenAxis))
+  {
+    throw V3d_BadValue ("V3d_View::SetUp, alignment of Eye,At,Up");
   }
-  V3d_BadValue_Raise_if( !TheStatus, "V3d_View::SetUp, alignment of Eye,At,Up");
 
-  Standard_Real myYscreenAxisX, myYscreenAxisY, myYscreenAxisZ;
-  myYscreenAxis.Coord (myYscreenAxisX, myYscreenAxisY, myYscreenAxisZ);
-
-  myCamera->SetUp (gp_Dir (myYscreenAxisX, myYscreenAxisY, myYscreenAxisZ));
+  aCamera->SetUp (gp_Dir (myYscreenAxis));
 
   AutoZFit();
 
@@ -1307,9 +1138,16 @@ void V3d_View::SetUp( const V3d_TypeOfOrientation Orientation )
 //=============================================================================
 void V3d_View::SetViewOrientationDefault()
 {
-  MyView->SetViewOrientationDefault() ;
+  myDefaultCamera->CopyOrientationData (Camera());
+}
 
-  ImmediateUpdate();
+//=======================================================================
+//function : SetViewMappingDefault
+//purpose  :
+//=======================================================================
+void V3d_View::SetViewMappingDefault()
+{
+  myDefaultCamera->CopyMappingData (Camera());
 }
 
 //=============================================================================
@@ -1318,7 +1156,22 @@ void V3d_View::SetViewOrientationDefault()
 //=============================================================================
 void V3d_View::ResetViewOrientation()
 {
-  MyView->ViewOrientationReset() ;
+  Camera()->CopyOrientationData (myDefaultCamera);
+
+  AutoZFit();
+
+  ImmediateUpdate();
+}
+
+//=======================================================================
+//function : ResetViewMapping
+//purpose  :
+//=======================================================================
+void V3d_View::ResetViewMapping()
+{
+  Camera()->CopyMappingData (myDefaultCamera);
+
+  AutoZFit();
 
   ImmediateUpdate();
 }
@@ -1327,21 +1180,18 @@ void V3d_View::ResetViewOrientation()
 //function : Reset
 //purpose  :
 //=============================================================================
-void V3d_View::Reset( const Standard_Boolean update )
+void V3d_View::Reset (const Standard_Boolean theToUpdate)
 {
-  Handle(Graphic3d_Camera) aDefaultCamera = MyView->DefaultCamera();
-
-  if (!aDefaultCamera.IsNull())
-  {
-    myCamera->CopyMappingData (aDefaultCamera);
-    myCamera->CopyOrientationData (aDefaultCamera);
+  Camera()->Copy (myDefaultCamera);
 
-    AutoZFit();
-  }
+  AutoZFit();
 
   SwitchSetFront = Standard_False;
 
-  if( !myImmediateUpdate && update ) Update();
+  if (myImmediateUpdate || theToUpdate)
+  {
+    Update();
+  }
 }
 
 //=======================================================================
@@ -1353,7 +1203,7 @@ void V3d_View::SetCenter (const Standard_Integer theXp,
 {
   Standard_Real aXv, aYv;
   Convert (theXp, theYp, aXv, aYv);
-  Translate (myCamera, aXv, aYv);
+  Translate (Camera(), aXv, aYv);
 
   ImmediateUpdate();
 }
@@ -1366,7 +1216,9 @@ void V3d_View::SetSize (const Standard_Real theSize)
 {
   V3d_BadValue_Raise_if (theSize <= 0.0, "V3d_View::SetSize, Window Size is NULL");
 
-  myCamera->SetScale (myCamera->Aspect() >= 1.0 ? theSize / myCamera->Aspect() : theSize);
+  Handle(Graphic3d_Camera) aCamera = Camera();
+
+  aCamera->SetScale (aCamera->Aspect() >= 1.0 ? theSize / aCamera->Aspect() : theSize);
 
   AutoZFit();
 
@@ -1377,19 +1229,19 @@ void V3d_View::SetSize (const Standard_Real theSize)
 //function : SetZSize
 //purpose  :
 //=============================================================================
-void V3d_View::SetZSize(const Standard_Real Size)
+void V3d_View::SetZSize (const Standard_Real theSize)
 {
-  Standard_Real Zmax = Size/2.;
+  Handle(Graphic3d_Camera) aCamera = Camera();
 
-  Standard_Real aDistance = myCamera->Distance();
+  Standard_Real Zmax = theSize / 2.;
 
-  if( Size <= 0. ) {
+  Standard_Real aDistance = aCamera->Distance();
+
+  if (theSize <= 0.)
+  {
     Zmax = aDistance;
   }
 
-  Standard_Real Front = MyViewContext.ZClippingFrontPlane();
-  Standard_Real Back  = MyViewContext.ZClippingBackPlane();
-
   // ShortReal precision factor used to add meaningful tolerance to
   // ZNear, ZFar values in order to avoid equality after type conversion
   // to ShortReal matrices type.
@@ -1400,7 +1252,7 @@ void V3d_View::SetZSize(const Standard_Real Size)
   aZNear              -= Abs (aZNear) * aPrecision;
   aZFar               += Abs (aZFar)  * aPrecision;
 
-  if (!myCamera->IsOrthographic())
+  if (!aCamera->IsOrthographic())
   {
     if (aZFar < aPrecision)
     {
@@ -1421,14 +1273,11 @@ void V3d_View::SetZSize(const Standard_Real Size)
     aZFar = aZNear + Abs (aZFar) * aPrecision;
   }
 
-  myCamera->SetZRange (aZNear, aZFar);
+  aCamera->SetZRange (aZNear, aZFar);
 
-  if (MyViewContext.FrontZClippingIsOn()  ||
-      MyViewContext.BackZClippingIsOn())
+  if (myImmediateUpdate)
   {
-    MyViewContext.SetZClippingFrontPlane (Front);
-    MyViewContext.SetZClippingBackPlane (Back);
-    MyView->SetContext (MyViewContext);
+    Redraw();
   }
 }
 
@@ -1436,41 +1285,44 @@ void V3d_View::SetZSize(const Standard_Real Size)
 //function : SetZoom
 //purpose  :
 //=============================================================================
-void V3d_View::SetZoom(const Standard_Real Coef,const Standard_Boolean Start)
+void V3d_View::SetZoom (const Standard_Real theCoef,const Standard_Boolean theToStart)
 {
-  V3d_BadValue_Raise_if( Coef <= 0.,"V3d_View::SetZoom, bad coefficient");
+  V3d_BadValue_Raise_if (theCoef <= 0., "V3d_View::SetZoom, bad coefficient");
 
-  if (Start)
+  Handle(Graphic3d_Camera) aCamera = Camera();
+
+  if (theToStart)
   {
-    myCamStartOpEye    = myCamera->Eye();
-    myCamStartOpCenter = myCamera->Center();
+    myCamStartOpEye    = aCamera->Eye();
+    myCamStartOpCenter = aCamera->Center();
   }
 
-  Standard_Real aViewWidth  = myCamera->ViewDimensions().X();
-  Standard_Real aViewHeight = myCamera->ViewDimensions().Y();
+  Standard_Real aViewWidth  = aCamera->ViewDimensions().X();
+  Standard_Real aViewHeight = aCamera->ViewDimensions().Y();
 
   // ensure that zoom will not be too small or too big
-  Standard_Real coef = Coef;
-  if (aViewWidth < coef * Precision::Confusion())
+  Standard_Real aCoef = theCoef;
+  if (aViewWidth < aCoef * Precision::Confusion())
   {
-    coef = aViewWidth / Precision::Confusion();
+    aCoef = aViewWidth / Precision::Confusion();
   }
-  else if (aViewWidth > coef * 1e12)
+  else if (aViewWidth > aCoef * 1e12)
   {
-    coef = aViewWidth / 1e12;
+    aCoef = aViewWidth / 1e12;
   }
-  if (aViewHeight < coef * Precision::Confusion())
+  if (aViewHeight < aCoef * Precision::Confusion())
   {
-    coef = aViewHeight / Precision::Confusion();
+    aCoef = aViewHeight / Precision::Confusion();
   }
-  else if (aViewHeight > coef * 1e12)
+  else if (aViewHeight > aCoef * 1e12)
   {
-    coef = aViewHeight / 1e12;
+    aCoef = aViewHeight / 1e12;
   }
 
-  myCamera->SetEye (myCamStartOpEye);
-  myCamera->SetCenter (myCamStartOpCenter);
-  myCamera->SetScale (myCamera->Scale() / Coef);
+  aCamera->SetEye (myCamStartOpEye);
+  aCamera->SetCenter (myCamStartOpCenter);
+  aCamera->SetScale (aCamera->Scale() / aCoef);
+
   AutoZFit();
 
   ImmediateUpdate();
@@ -1484,19 +1336,11 @@ void V3d_View::SetScale( const Standard_Real Coef )
 {
   V3d_BadValue_Raise_if( Coef <= 0. ,"V3d_View::SetScale, bad coefficient");
 
-  Handle(Graphic3d_Camera) aDefaultCamera = MyView->DefaultCamera();
+  Handle(Graphic3d_Camera) aCamera = Camera();
 
-  // Strange behavior for the sake of compatibility.
-  if (!aDefaultCamera.IsNull())
-  {
-    myCamera->SetAspect (aDefaultCamera->Aspect());
-    Standard_Real aDefaultScale = aDefaultCamera->Scale();
-    myCamera->SetScale (aDefaultScale / Coef);
-  } 
-  else
-  {
-    myCamera->SetScale (myCamera->Scale() / Coef);
-  }
+  Standard_Real aDefaultScale = myDefaultCamera->Scale();
+  aCamera->SetAspect (myDefaultCamera->Aspect());
+  aCamera->SetScale (aDefaultScale / Coef);
 
   AutoZFit();
 
@@ -1511,265 +1355,121 @@ void V3d_View::SetAxialScale( const Standard_Real Sx, const Standard_Real Sy, co
 {
   V3d_BadValue_Raise_if( Sx <= 0. || Sy <= 0. || Sz <= 0.,"V3d_View::SetAxialScale, bad coefficient");
 
-  myCamera->SetAxialScale (gp_XYZ (Sx, Sy, Sz));
+  Camera()->SetAxialScale (gp_XYZ (Sx, Sy, Sz));
+
   AutoZFit();
 }
 
 //=============================================================================
-//function : FitAll
+//function : SetRatio
 //purpose  :
 //=============================================================================
-void V3d_View::FitAll (const Standard_Real theMargin, const Standard_Boolean theToUpdate)
+void V3d_View::SetRatio()
 {
-  Standard_ASSERT_RAISE (theMargin >= 0.0 && theMargin < 1.0, "Invalid margin coefficient");
-
-  if (MyView->NumberOfDisplayedStructures() == 0)
+  if (MyWindow.IsNull())
   {
     return;
   }
 
-  Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
-  MyView->MinMaxValues (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
-  gp_XYZ aMin (aXmin, aYmin, aZmin);
-  gp_XYZ aMax (aXmax, aYmax, aZmax);
-
-  if (!FitMinMax (myCamera, aMin, aMax, theMargin, 10.0 * Precision::Confusion()))
+  Standard_Integer aWidth  = 0;
+  Standard_Integer aHeight = 0;
+  MyWindow->Size (aWidth, aHeight);
+  if (aWidth > 0 && aHeight > 0)
   {
-    return;
-  }
-
-  AutoZFit();
+    Standard_Real aRatio = static_cast<Standard_Real> (aWidth) /
+                           static_cast<Standard_Real> (aHeight);
 
-  if (myImmediateUpdate || theToUpdate)
-  {
-    Update();
+    Camera()       ->SetAspect (aRatio);
+    myDefaultCamera->SetAspect (aRatio);
   }
 }
 
 //=============================================================================
-//function : AutoZFit
+//function : FitAll
 //purpose  :
 //=============================================================================
-void V3d_View::AutoZFit()
+void V3d_View::FitAll (const Standard_Real theMargin, const Standard_Boolean theToUpdate)
 {
-  if (!AutoZFitMode())
-  {
-    return;
-  }
-
-  ZFitAll (myAutoZFitScaleFactor);
+  FitAll (myView->MinMaxValues(), theMargin, theToUpdate);
 }
 
 //=============================================================================
-//function : ZFitAll
+//function : FitAll
 //purpose  :
 //=============================================================================
-void V3d_View::ZFitAll (const Standard_Real theScaleFactor)
+void V3d_View::FitAll (const Bnd_Box& theBox, const Standard_Real theMargin, const Standard_Boolean theToUpdate)
 {
-  Standard_ASSERT_RAISE (theScaleFactor > 0.0, "Zero or negative scale factor is not allowed.");
+  Standard_ASSERT_RAISE(theMargin >= 0.0 && theMargin < 1.0, "Invalid margin coefficient");
 
-  // Method changes ZNear and ZFar planes of camera so as to fit the graphical structures
-  // by their real boundaries (computed ignoring infinite flag) into the viewing volume.
-  // In addition to the graphical boundaries, the usual min max used for fitting perspective
-  // camera. To avoid numeric errors for perspective camera the negative ZNear values are
-  // fixed using tolerance distance, relative to boundaries size. The tolerance distance
-  // should be computed using information on boundaries of primary application actors,
-  // (e.g. representing the displayed model) - to ensure that they are not unreasonably clipped.
-
-  Standard_Real aMinMax[6];    // applicative min max boundaries
-  View()->MinMaxValues (aMinMax[0], aMinMax[1], aMinMax[2],
-                        aMinMax[3], aMinMax[4], aMinMax[5],
-                        Standard_False);
-
-  Standard_Real aGraphicBB[6]; // real graphical boundaries (not accounting infinite flag).
-  View()->MinMaxValues (aGraphicBB[0], aGraphicBB[1], aGraphicBB[2],
-                        aGraphicBB[3], aGraphicBB[4], aGraphicBB[5],
-                        Standard_True);
-
-  // Check if anything can be adjusted
-  Standard_Real aLim = (ShortRealLast() - 1.0);
-  if (Abs (aGraphicBB[0]) > aLim || Abs (aGraphicBB[1]) > aLim || Abs (aGraphicBB[2]) > aLim ||
-      Abs (aGraphicBB[3]) > aLim || Abs (aGraphicBB[4]) > aLim || Abs (aGraphicBB[5]) > aLim)
-  {
-    SetZSize (0.0);
-    ImmediateUpdate();
+  if (myView->NumberOfDisplayedStructures() == 0)
+  {
     return;
   }
 
-  // Measure depth of boundary points from camera eye
-  gp_Pnt aPntsToMeasure[16] =
+  if (!FitMinMax (Camera(), theBox, theMargin, 10.0 * Precision::Confusion()))
   {
-    gp_Pnt (aMinMax[0], aMinMax[1], aMinMax[2]),
-    gp_Pnt (aMinMax[0], aMinMax[1], aMinMax[5]),
-    gp_Pnt (aMinMax[0], aMinMax[4], aMinMax[2]),
-    gp_Pnt (aMinMax[0], aMinMax[4], aMinMax[5]),
-    gp_Pnt (aMinMax[3], aMinMax[1], aMinMax[2]),
-    gp_Pnt (aMinMax[3], aMinMax[1], aMinMax[5]),
-    gp_Pnt (aMinMax[3], aMinMax[4], aMinMax[2]),
-    gp_Pnt (aMinMax[3], aMinMax[4], aMinMax[5]),
-
-    gp_Pnt (aGraphicBB[0], aGraphicBB[1], aGraphicBB[2]),
-    gp_Pnt (aGraphicBB[0], aGraphicBB[1], aGraphicBB[5]),
-    gp_Pnt (aGraphicBB[0], aGraphicBB[4], aGraphicBB[2]),
-    gp_Pnt (aGraphicBB[0], aGraphicBB[4], aGraphicBB[5]),
-    gp_Pnt (aGraphicBB[3], aGraphicBB[1], aGraphicBB[2]),
-    gp_Pnt (aGraphicBB[3], aGraphicBB[1], aGraphicBB[5]),
-    gp_Pnt (aGraphicBB[3], aGraphicBB[4], aGraphicBB[2]),
-    gp_Pnt (aGraphicBB[3], aGraphicBB[4], aGraphicBB[5])
-  };
-
-  // Camera eye plane
-  gp_Dir aCamDir = myCamera->Direction();
-  gp_Pnt aCamEye = myCamera->Eye();
-  gp_Pln aCamPln (aCamEye, aCamDir);
-
-  Standard_Real aModelMinDist   = RealLast();
-  Standard_Real aModelMaxDist   = RealFirst();
-  Standard_Real aGraphicMinDist = RealLast();
-  Standard_Real aGraphicMaxDist = RealFirst();
-
-  const gp_XYZ& anAxialScale = myCamera->AxialScale();
-
-  // Get minimum and maximum distances to the eye plane
-  for (Standard_Integer aPntIt = 0; aPntIt < 16; ++aPntIt)
-  {
-    gp_Pnt aMeasurePnt = aPntsToMeasure[aPntIt];
-
-    if (Abs (aMeasurePnt.X()) > aLim || Abs (aMeasurePnt.Y()) > aLim || Abs (aMeasurePnt.Z()) > aLim)
-    {
-      continue;
-    }
-
-    aMeasurePnt = gp_Pnt (aMeasurePnt.X() * anAxialScale.X(),
-                          aMeasurePnt.Y() * anAxialScale.Y(),
-                          aMeasurePnt.Z() * anAxialScale.Z());
-
-    Standard_Real aDistance = aCamPln.Distance (aMeasurePnt);
-
-    // Check if the camera is intruded into the scene
-    if (aCamDir.IsOpposite (gp_Vec (aCamEye, aMeasurePnt), M_PI * 0.5))
-    {
-      aDistance *= -1;
-    }
-
-    Standard_Real& aChangeMinDist = aPntIt >= 8 ? aGraphicMinDist : aModelMinDist;
-    Standard_Real& aChangeMaxDist = aPntIt >= 8 ? aGraphicMaxDist : aModelMaxDist;
-    aChangeMinDist = Min (aDistance, aChangeMinDist);
-    aChangeMaxDist = Max (aDistance, aChangeMaxDist);
+    return;
   }
 
-  // Compute depth of bounding box center
-  Standard_Real aMidDepth  = (aGraphicMinDist + aGraphicMaxDist) * 0.5;
-  Standard_Real aHalfDepth = (aGraphicMaxDist - aGraphicMinDist) * 0.5;
-
-  // ShortReal precision factor used to add meaningful tolerance to
-  // ZNear, ZFar values in order to avoid equality after type conversion
-  // to ShortReal matrices type.
-  const Standard_Real aPrecision = Pow (0.1, ShortRealDigits() - 2);
-
-  // Compute enlarged or shrank near and far z ranges
-  Standard_Real aZNear = aMidDepth - aHalfDepth * theScaleFactor;
-  Standard_Real aZFar  = aMidDepth + aHalfDepth * theScaleFactor;
-  aZNear              -= aPrecision * 0.5;
-  aZFar               += aPrecision * 0.5;
-
-  if (!myCamera->IsOrthographic())
-  {
-    if (aZFar >= aPrecision)
-    {
-      // To avoid numeric errors... (See comments in the beginning of the method).
-      // Choose between model distance and graphical distance, as the model boundaries
-      // might be infinite if all structures have infinite flag.
-      const Standard_Real aGraphicDepth = aGraphicMaxDist >= aGraphicMinDist
-        ? aGraphicMaxDist - aGraphicMinDist : RealLast();
-
-      const Standard_Real aModelDepth = aModelMaxDist >= aModelMinDist
-        ? aModelMaxDist - aModelMinDist : RealLast();
-
-      const Standard_Real aMinDepth = Min (aModelDepth, aGraphicDepth);
-      const Standard_Real aZTolerance =
-        Max (Abs (aMinDepth) * aPrecision, aPrecision);
-
-      if (aZNear < aZTolerance)
-      {
-        aZNear = aZTolerance;
-      }
-    }
-    else // aZFar < aPrecision - Invalid case when both ZNear and ZFar are negative
-    {
-      aZNear = aPrecision;
-      aZFar  = aPrecision * 2.0;
-    }
-  }
+  AutoZFit();
 
-  // If range is too small
-  if (aZFar < (aZNear + Abs (aZFar) * aPrecision))
+  if (myImmediateUpdate || theToUpdate)
   {
-    aZFar = aZNear + Abs (aZFar) * aPrecision;
+    Update();
   }
-
-  myCamera->SetZRange (aZNear, aZFar);
-
-  ImmediateUpdate();
 }
 
 //=============================================================================
 //function : DepthFitAll
 //purpose  :
 //=============================================================================
-void V3d_View::DepthFitAll(const Quantity_Coefficient Aspect,
-                           const Quantity_Coefficient Margin)
+void V3d_View::DepthFitAll(const Standard_Real Aspect,
+                           const Standard_Real Margin)
 {
   Standard_Real Xmin,Ymin,Zmin,Xmax,Ymax,Zmax,U,V,W,U1,V1,W1 ;
   Standard_Real Umin,Vmin,Wmin,Umax,Vmax,Wmax ;
   Standard_Real Dx,Dy,Dz,Size;
 
-  Standard_Integer Nstruct = MyView->NumberOfDisplayedStructures() ;
+  Standard_Integer Nstruct = myView->NumberOfDisplayedStructures() ;
 
   if((Nstruct <= 0) || (Aspect < 0.) || (Margin < 0.) || (Margin > 1.)) {
     ImmediateUpdate();
     return ;
   }
 
-  MyView->MinMaxValues(Xmin,Ymin,Zmin,Xmax,Ymax,Zmax) ;
-
-  Standard_Real LIM = ShortRealLast() -1.;
-  if     (Abs(Xmin) > LIM || Abs(Ymin) > LIM || Abs(Zmin) > LIM
-    ||  Abs(Xmax) > LIM || Abs(Ymax) > LIM || Abs(Zmax) > LIM ) {
-      ImmediateUpdate();
-      return ;
-    }
-
-    if (Xmin == Xmax && Ymin == Ymax && Zmin == Zmax) {
-      ImmediateUpdate();
-      return ;
-    }
-    MyView->Projects(Xmin,Ymin,Zmin,U,V,W) ;
-    MyView->Projects(Xmax,Ymax,Zmax,U1,V1,W1) ;
+  Bnd_Box aBox = myView->MinMaxValues();
+  if (aBox.IsVoid())
+  {
+    ImmediateUpdate();
+    return ;
+  }
+    aBox.Get (Xmin,Ymin,Zmin,Xmax,Ymax,Zmax);
+    Project (Xmin,Ymin,Zmin,U,V,W) ;
+    Project (Xmax,Ymax,Zmax,U1,V1,W1) ;
     Umin = Min(U,U1) ; Umax = Max(U,U1) ;
     Vmin = Min(V,V1) ; Vmax = Max(V,V1) ;
     Wmin = Min(W,W1) ; Wmax = Max(W,W1) ;
-    MyView->Projects(Xmin,Ymin,Zmax,U,V,W) ;
+    Project (Xmin,Ymin,Zmax,U,V,W) ;
     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
-    MyView->Projects(Xmax,Ymin,Zmax,U,V,W) ;
+    Project (Xmax,Ymin,Zmax,U,V,W) ;
     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
-    MyView->Projects(Xmax,Ymin,Zmin,U,V,W) ;
+    Project (Xmax,Ymin,Zmin,U,V,W) ;
     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
-    MyView->Projects(Xmax,Ymax,Zmin,U,V,W) ;
+    Project (Xmax,Ymax,Zmin,U,V,W) ;
     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
-    MyView->Projects(Xmin,Ymax,Zmax,U,V,W) ;
+    Project (Xmin,Ymax,Zmax,U,V,W) ;
     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
-    MyView->Projects(Xmin,Ymax,Zmin,U,V,W) ;
+    Project (Xmin,Ymax,Zmin,U,V,W) ;
     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
@@ -1790,30 +1490,6 @@ void V3d_View::DepthFitAll(const Quantity_Coefficient Aspect,
     ImmediateUpdate();
 }
 
-//=============================================================================
-//function : FitAll
-//purpose  :
-//=============================================================================
-void V3d_View::FitAll(const Standard_Real theMinXv,
-                      const Standard_Real theMinYv,
-                      const Standard_Real theMaxXv,
-                      const Standard_Real theMaxYv)
-{
-  FitAll (MyWindow, theMinXv, theMinYv, theMaxXv, theMaxYv);
-}
-
-//=============================================================================
-//function : WindowFitAll
-//purpose  :
-//=============================================================================
-void V3d_View::WindowFitAll(const Standard_Integer Xmin,
-                            const Standard_Integer Ymin,
-                            const Standard_Integer Xmax,
-                            const Standard_Integer Ymax)
-{
-  WindowFit(Xmin,Ymin,Xmax,Ymax);
-}
-
 //=======================================================================
 //function : WindowFit
 //purpose  :
@@ -1825,14 +1501,16 @@ void V3d_View::WindowFit (const Standard_Integer theMinXp,
 {
   Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
 
-  if (!myCamera->IsOrthographic())
+  Handle(Graphic3d_Camera) aCamera = Camera();
+
+  if (!aCamera->IsOrthographic())
   {
-    // normalize view coordiantes
+    // normalize view coordinates
     Standard_Integer aWinWidth, aWinHeight;
     MyWindow->Size (aWinWidth, aWinHeight);
 
     // z coordinate of camera center
-    Standard_Real aDepth = myCamera->Project (myCamera->Center()).Z();
+    Standard_Real aDepth = aCamera->Project (aCamera->Center()).Z();
 
     // camera projection coordinate are in NDC which are normalized [-1, 1]
     Standard_Real aUMin = (2.0 / aWinWidth) * theMinXp  - 1.0;
@@ -1843,21 +1521,21 @@ void V3d_View::WindowFit (const Standard_Integer theMinXp,
     // compute camera panning
     gp_Pnt aScreenCenter (0.0, 0.0, aDepth);
     gp_Pnt aFitCenter ((aUMin + aUMax) * 0.5, (aVMin + aVMax) * 0.5, aDepth);
-    gp_Pnt aPanTo = myCamera->ConvertProj2View (aFitCenter);
-    gp_Pnt aPanFrom = myCamera->ConvertProj2View (aScreenCenter);
+    gp_Pnt aPanTo = aCamera->ConvertProj2View (aFitCenter);
+    gp_Pnt aPanFrom = aCamera->ConvertProj2View (aScreenCenter);
     gp_Vec aPanVec (aPanFrom, aPanTo);
 
     // compute section size
     gp_Pnt aFitTopRight (aUMax, aVMax, aDepth);
     gp_Pnt aFitBotLeft (aUMin, aVMin, aDepth);
-    gp_Pnt aViewBotLeft = myCamera->ConvertProj2View (aFitBotLeft);
-    gp_Pnt aViewTopRight = myCamera->ConvertProj2View (aFitTopRight);
+    gp_Pnt aViewBotLeft = aCamera->ConvertProj2View (aFitBotLeft);
+    gp_Pnt aViewTopRight = aCamera->ConvertProj2View (aFitTopRight);
 
     Standard_Real aUSize = aViewTopRight.X() - aViewBotLeft.X();
     Standard_Real aVSize = aViewTopRight.Y() - aViewBotLeft.Y();
 
-    Translate (myCamera, aPanVec.X(), -aPanVec.Y());
-    Scale (myCamera, aUSize, aVSize);
+    Translate (aCamera, aPanVec.X(), -aPanVec.Y());
+    Scale (aCamera, aUSize, aVSize);
     AutoZFit();
   }
   else
@@ -1873,28 +1551,6 @@ void V3d_View::WindowFit (const Standard_Integer theMinXp,
   ImmediateUpdate();
 }
 
-//=======================================================================
-//function : SetViewMappingDefault
-//purpose  :
-//=======================================================================
-void V3d_View::SetViewMappingDefault()
-{
-  MyView->SetViewMappingDefault();
-
-  ImmediateUpdate();
-}
-
-//=======================================================================
-//function : ResetViewMapping
-//purpose  :
-//=======================================================================
-void V3d_View::ResetViewMapping()
-{
-  MyView->ViewMappingReset();
-
-  Update();
-}
-
 //=======================================================================
 //function : ConvertToGrid
 //purpose  :
@@ -1945,12 +1601,12 @@ Standard_Real V3d_View::Convert(const Standard_Integer Vp) const
 {
   Standard_Integer aDxw, aDyw ;
 
-  V3d_UnMapped_Raise_if (!MyView->IsDefined(), "view has no window");
+  V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
 
   MyWindow->Size (aDxw, aDyw);
   Standard_Real aValue;
 
-  gp_Pnt aViewDims = myCamera->ViewDimensions();
+  gp_Pnt aViewDims = Camera()->ViewDimensions();
   aValue = aViewDims.X() * (Standard_Real)Vp / (Standard_Real)aDxw;
 
   return aValue;
@@ -1967,12 +1623,12 @@ void V3d_View::Convert(const Standard_Integer Xp,
 {
   Standard_Integer aDxw, aDyw;
 
-  V3d_UnMapped_Raise_if (!MyView->IsDefined(), "view has no window");
+  V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
 
   MyWindow->Size (aDxw, aDyw);
 
   gp_Pnt aPoint (Xp * 2.0 / aDxw - 1.0, (aDyw - Yp) * 2.0 / aDyw - 1.0, 0.0);
-  aPoint = myCamera->ConvertProj2View (aPoint);
+  aPoint = Camera()->ConvertProj2View (aPoint);
 
   Xv = aPoint.X();
   Yv = aPoint.Y();
@@ -1984,12 +1640,12 @@ void V3d_View::Convert(const Standard_Integer Xp,
 //=======================================================================
 Standard_Integer V3d_View::Convert(const Standard_Real Vv) const
 {
-  V3d_UnMapped_Raise_if (!MyView->IsDefined(), "view has no window");
+  V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
 
   Standard_Integer aDxw, aDyw;
   MyWindow->Size (aDxw, aDyw);
 
-  gp_Pnt aViewDims = myCamera->ViewDimensions();
+  gp_Pnt aViewDims = Camera()->ViewDimensions();
   Standard_Integer aValue = RealToInt (aDxw * Vv / (aViewDims.X()));
 
   return aValue;
@@ -2004,13 +1660,13 @@ void V3d_View::Convert(const Standard_Real Xv,
                        Standard_Integer& Xp,
                        Standard_Integer& Yp) const
 {
-  V3d_UnMapped_Raise_if (!MyView->IsDefined(), "view has no window");
+  V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
 
   Standard_Integer aDxw, aDyw;
   MyWindow->Size (aDxw, aDyw);
 
   gp_Pnt aPoint (Xv, Yv, 0.0);
-  aPoint = myCamera->ConvertView2Proj (aPoint);
+  aPoint = Camera()->ConvertView2Proj (aPoint);
   aPoint = gp_Pnt ((aPoint.X() + 1.0) * aDxw / 2.0, aDyw - (aPoint.Y() + 1.0) * aDyw / 2.0, 0.0);
 
   Xp = RealToInt (aPoint.X());
@@ -2024,46 +1680,10 @@ void V3d_View::Convert(const Standard_Real Xv,
 void V3d_View::Convert(const Standard_Integer Xp,
                        const Standard_Integer Yp,
                        Standard_Real& X,
-                       Standard_Real& Y,
-                       Standard_Real& Z) const
-{
-  V3d_UnMapped_Raise_if (!MyView->IsDefined(), "view has no window");
-  Standard_Integer aHeight, aWidth;
-  MyWindow->Size (aWidth, aHeight);
-
-  Standard_Real anX = 2.0 * Xp / aWidth - 1.0;
-  Standard_Real anY = 2.0 * (aHeight - 1 - Yp) / aHeight - 1.0;
-  Standard_Real  aZ = 2.0 * 0.0 - 1.0;
-
-  gp_Pnt aResult = myCamera->UnProject (gp_Pnt (anX, anY, aZ));
-
-  X = aResult.X();
-  Y = aResult.Y();
-  Z = aResult.Z();
-
-  Graphic3d_Vertex aVrp;
-  aVrp.SetCoord (X, Y, Z);
-
-  if( MyViewer->Grid()->IsActive() ) {
-    Graphic3d_Vertex aNewVrp = Compute (aVrp) ;
-    aNewVrp.Coord (X, Y, Z) ;
-  }
-}
-
-//=======================================================================
-//function : ConvertWithProj
-//purpose  :
-//=======================================================================
-void V3d_View::ConvertWithProj(const Standard_Integer Xp,
-                               const Standard_Integer Yp,
-                               Standard_Real& X,
-                               Standard_Real& Y,
-                               Standard_Real& Z,
-                               Standard_Real& Dx,
-                               Standard_Real& Dy,
-                               Standard_Real& Dz) const
+                       Standard_Real& Y,
+                       Standard_Real& Z) const
 {
-  V3d_UnMapped_Raise_if( !MyView->IsDefined(), "view has no window");
+  V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
   Standard_Integer aHeight, aWidth;
   MyWindow->Size (aWidth, aHeight);
 
@@ -2071,25 +1691,49 @@ void V3d_View::ConvertWithProj(const Standard_Integer Xp,
   Standard_Real anY = 2.0 * (aHeight - 1 - Yp) / aHeight - 1.0;
   Standard_Real  aZ = 2.0 * 0.0 - 1.0;
 
-  gp_Pnt aResult = myCamera->UnProject (gp_Pnt (anX, anY, aZ));
+  gp_Pnt aResult = Camera()->UnProject (gp_Pnt (anX, anY, aZ));
 
   X = aResult.X();
   Y = aResult.Y();
   Z = aResult.Z();
+}
 
-  Graphic3d_Vertex aVrp;
-  aVrp.SetCoord (X, Y, Z);
+//=======================================================================
+//function : ConvertWithProj
+//purpose  :
+//=======================================================================
+void V3d_View::ConvertWithProj(const Standard_Integer theXp,
+                               const Standard_Integer theYp,
+                               Standard_Real& theX,
+                               Standard_Real& theY,
+                               Standard_Real& theZ,
+                               Standard_Real& theDx,
+                               Standard_Real& theDy,
+                               Standard_Real& theDz) const
+{
+  V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
+  Standard_Integer aHeight = 0, aWidth = 0;
+  MyWindow->Size (aWidth, aHeight);
 
-  aResult = myCamera->UnProject (gp_Pnt (anX, anY, aZ - 10.0));
+  const Standard_Real anX = 2.0 * theXp / aWidth - 1.0;
+  const Standard_Real anY = 2.0 * (aHeight - 1 - theYp) / aHeight - 1.0;
+  const Standard_Real  aZ = 2.0 * 0.0 - 1.0;
 
-  Dx = X - aResult.X();
-  Dy = Y - aResult.Y();
-  Dz = Z - aResult.Z();
+  const Handle(Graphic3d_Camera)& aCamera = Camera();
+  const gp_Pnt aResult1 = aCamera->UnProject (gp_Pnt (anX, anY, aZ));
+  const gp_Pnt aResult2 = aCamera->UnProject (gp_Pnt (anX, anY, aZ - 10.0));
 
-  if( MyViewer->Grid()->IsActive() ) {
-    Graphic3d_Vertex aNewVrp = Compute (aVrp) ;
-    aNewVrp.Coord (X, Y, Z) ;
-  }
+  theX = aResult1.X();
+  theY = aResult1.Y();
+  theZ = aResult1.Z();
+  Graphic3d_Vec3d aNormDir (theX - aResult2.X(),
+                            theY - aResult2.Y(),
+                            theZ - aResult2.Z());
+  aNormDir.Normalize();
+
+  theDx = aNormDir.x();
+  theDy = aNormDir.y();
+  theDz = aNormDir.z();
 }
 
 //=======================================================================
@@ -2102,28 +1746,54 @@ void V3d_View::Convert(const Standard_Real X,
                        Standard_Integer& Xp,
                        Standard_Integer& Yp) const
 {
-  V3d_UnMapped_Raise_if( !MyView->IsDefined(), "view has no window");
+  V3d_UnMapped_Raise_if (!myView->IsDefined(), "view has no window");
   Standard_Integer aHeight, aWidth;
   MyWindow->Size (aWidth, aHeight);
 
-  gp_Pnt aPoint = myCamera->Project (gp_Pnt (X, Y, Z));
+  gp_Pnt aPoint = Camera()->Project (gp_Pnt (X, Y, Z));
 
   Xp = RealToInt ((aPoint.X() + 1) * 0.5 * aWidth);
-  Yp = RealToInt ((aPoint.Y() + 1) * 0.5 * aHeight);
+  Yp = RealToInt (aHeight - 1 - (aPoint.Y() + 1) * 0.5 * aHeight);
 }
 
 //=======================================================================
 //function : Project
 //purpose  :
 //=======================================================================
-void V3d_View::Project(const Standard_Real X,
-                       const Standard_Real Y,
-                       const Standard_Real Z,
-                       Standard_Real &Xp,
-                       Standard_Real &Yp) const
+void V3d_View::Project (const Standard_Real theX,
+                        const Standard_Real theY,
+                        const Standard_Real theZ,
+                        Standard_Real& theXp,
+                        Standard_Real& theYp) const
+{
+  Standard_Real aZp;
+  Project (theX, theY, theZ, theXp, theYp, aZp);
+}
+
+//=======================================================================
+//function : Project
+//purpose  :
+//=======================================================================
+void V3d_View::Project (const Standard_Real theX,
+                        const Standard_Real theY,
+                        const Standard_Real theZ,
+                        Standard_Real& theXp,
+                        Standard_Real& theYp,
+                        Standard_Real& theZp) const
 {
-  Standard_Real Zp;
-  MyView->Projects (X, Y, Z, Xp, Yp, Zp);
+  Handle(Graphic3d_Camera) aCamera = Camera();
+
+  gp_XYZ aViewSpaceDimensions = aCamera->ViewDimensions();
+  Standard_Real aXSize = aViewSpaceDimensions.X();
+  Standard_Real aYSize = aViewSpaceDimensions.Y();
+  Standard_Real aZSize = aViewSpaceDimensions.Z();
+
+  gp_Pnt aPoint = aCamera->Project (gp_Pnt (theX, theY, theZ));
+
+  // NDC [-1, 1] --> PROJ [ -size / 2, +size / 2 ]
+  theXp = aPoint.X() * aXSize * 0.5;
+  theYp = aPoint.Y() * aYSize * 0.5;
+  theZp = aPoint.Z() * aZSize * 0.5;
 }
 
 //=======================================================================
@@ -2145,16 +1815,16 @@ void V3d_View::BackgroundColor(const Quantity_TypeOfColor Type,
 //=======================================================================
 Quantity_Color V3d_View::BackgroundColor() const
 {
-  return MyBackground.Color() ;
+  return myView->Background().Color() ;
 }
 
 //=======================================================================
 //function : GradientBackgroundColors
 //purpose  :
 //=======================================================================
-void V3d_View::GradientBackgroundColors(Quantity_Color& Color1,Quantity_Color& Color2) const
+void V3d_View::GradientBackgroundColors (Quantity_Color& theColor1, Quantity_Color& theColor2) const
 {
-  MyGradientBackground.Colors(Color1, Color2);
+  myView->GradientBackground().Colors (theColor1, theColor2);
 }
 
 //=======================================================================
@@ -2163,7 +1833,7 @@ void V3d_View::GradientBackgroundColors(Quantity_Color& Color1,Quantity_Color& C
 //=======================================================================
 Aspect_GradientBackground V3d_View::GradientBackground() const
 {
-   return MyGradientBackground;
+  return myView->GradientBackground();
 }
 
 //=======================================================================
@@ -2172,22 +1842,7 @@ Aspect_GradientBackground V3d_View::GradientBackground() const
 //=======================================================================
 Standard_Real V3d_View::Scale() const
 {
-  Handle(Graphic3d_Camera) aDefaultCamera = MyView->DefaultCamera();
-
-  Standard_Real aCameraScale;
-
-  // Strange behavior for the sake of compatibility.
-  if (!aDefaultCamera.IsNull())
-  {
-    Standard_Real aDefaultScale = aDefaultCamera->Scale();
-    aCameraScale = aDefaultScale / myCamera->Scale();
-  } 
-  else
-  {
-    aCameraScale = myCamera->Scale();
-  }
-
-  return aCameraScale;
+  return myDefaultCamera->Scale() / Camera()->Scale();
 }
 
 //=======================================================================
@@ -2196,7 +1851,7 @@ Standard_Real V3d_View::Scale() const
 //=======================================================================
 void V3d_View::AxialScale(Standard_Real& Sx, Standard_Real& Sy, Standard_Real& Sz) const
 {
-  gp_Pnt anAxialScale = myCamera->AxialScale();
+  gp_Pnt anAxialScale = Camera()->AxialScale();
   Sx = anAxialScale.X();
   Sy = anAxialScale.Y();
   Sz = anAxialScale.Z();
@@ -2208,7 +1863,7 @@ void V3d_View::AxialScale(Standard_Real& Sx, Standard_Real& Sy, Standard_Real& S
 //=======================================================================
 void V3d_View::Size(Standard_Real& Width, Standard_Real& Height) const
 {
-  gp_Pnt aViewDims = myCamera->ViewDimensions();
+  gp_Pnt aViewDims = Camera()->ViewDimensions();
 
   Width = aViewDims.X();
   Height = aViewDims.Y();
@@ -2220,7 +1875,7 @@ void V3d_View::Size(Standard_Real& Width, Standard_Real& Height) const
 //=======================================================================
 Standard_Real V3d_View::ZSize() const
 {
-  gp_Pnt aViewDims = myCamera->ViewDimensions();
+  gp_Pnt aViewDims = Camera()->ViewDimensions();
 
   return aViewDims.Z();
 }
@@ -2237,33 +1892,34 @@ Standard_Integer V3d_View::MinMax(Standard_Real& Umin,
   Standard_Real Wmin,Wmax,U,V,W ;
   Standard_Real Xmin,Ymin,Zmin,Xmax,Ymax,Zmax ;
   // CAL 6/11/98
-  Standard_Integer Nstruct = MyView->NumberOfDisplayedStructures() ;
+  Standard_Integer Nstruct = myView->NumberOfDisplayedStructures() ;
 
   if( Nstruct ) {
-    MyView->MinMaxValues(Xmin,Ymin,Zmin,Xmax,Ymax,Zmax) ;
-    MyView->Projects(Xmin,Ymin,Zmin,Umin,Vmin,Wmin) ;
-    MyView->Projects(Xmax,Ymax,Zmax,Umax,Vmax,Wmax) ;
-    MyView->Projects(Xmin,Ymin,Zmax,U,V,W) ;
+    Bnd_Box aBox = myView->MinMaxValues();
+    aBox.Get (Xmin,Ymin,Zmin,Xmax,Ymax,Zmax);
+    Project (Xmin,Ymin,Zmin,Umin,Vmin,Wmin) ;
+    Project (Xmax,Ymax,Zmax,Umax,Vmax,Wmax) ;
+    Project (Xmin,Ymin,Zmax,U,V,W) ;
     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
-    MyView->Projects(Xmax,Ymin,Zmax,U,V,W) ;
+    Project (Xmax,Ymin,Zmax,U,V,W) ;
     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
-    MyView->Projects(Xmax,Ymin,Zmin,U,V,W) ;
+    Project (Xmax,Ymin,Zmin,U,V,W) ;
     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
-    MyView->Projects(Xmax,Ymax,Zmin,U,V,W) ;
+    Project (Xmax,Ymax,Zmin,U,V,W) ;
     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
-    MyView->Projects(Xmin,Ymax,Zmax,U,V,W) ;
+    Project (Xmin,Ymax,Zmax,U,V,W) ;
     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
-    MyView->Projects(Xmin,Ymax,Zmin,U,V,W) ;
+    Project (Xmin,Ymax,Zmin,U,V,W) ;
     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
@@ -2284,10 +1940,11 @@ Standard_Integer V3d_View::MinMax(Standard_Real& Xmin,
 {
   // CAL 6/11/98
   // Standard_Integer Nstruct = (MyView->DisplayedStructures())->Extent() ;
-  Standard_Integer Nstruct = MyView->NumberOfDisplayedStructures() ;
+  Standard_Integer Nstruct = myView->NumberOfDisplayedStructures() ;
 
   if( Nstruct ) {
-    MyView->MinMaxValues(Xmin,Ymin,Zmin,Xmax,Ymax,Zmax) ;
+    Bnd_Box aBox = myView->MinMaxValues();
+    aBox.Get (Xmin,Ymin,Zmin,Xmax,Ymax,Zmax);
   }
   return Nstruct ;
 }
@@ -2296,62 +1953,111 @@ Standard_Integer V3d_View::MinMax(Standard_Real& Xmin,
 //function : Gravity
 //purpose  :
 //=======================================================================
-Standard_Integer V3d_View::Gravity(Standard_Real& X, Standard_Real& Y, Standard_Real& Z) const
+void V3d_View::Gravity (Standard_Real& theX,
+                        Standard_Real& theY,
+                        Standard_Real& theZ) const
 {
-  Standard_Real Xmin,Ymin,Zmin,Xmax,Ymax,Zmax;
-  Standard_Integer Nstruct,Npoint ;
-  Graphic3d_MapOfStructure MySetOfStructures;
+  Graphic3d_MapOfStructure aSetOfStructures;
+  myView->DisplayedStructures (aSetOfStructures);
 
-  MyView->DisplayedStructures (MySetOfStructures);
-  Nstruct = MySetOfStructures.Extent() ;
-
-  Graphic3d_MapIteratorOfMapOfStructure MyIterator(MySetOfStructures) ;
+  Standard_Boolean hasSelection = Standard_False;
+  for (Graphic3d_MapIteratorOfMapOfStructure aStructIter (aSetOfStructures);
+       aStructIter.More(); aStructIter.Next())
+  {
+    if (aStructIter.Key()->IsHighlighted()
+     && aStructIter.Key()->IsVisible())
+    {
+      hasSelection = Standard_True;
+      break;
+    }
+  }
 
-  Npoint = 0 ; X = Y = Z = 0. ;
-  for (; MyIterator.More(); MyIterator.Next())
+  Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
+  Standard_Integer aNbPoints = 0;
+  gp_XYZ aResult (0.0, 0.0, 0.0);
+  for (Graphic3d_MapIteratorOfMapOfStructure aStructIter (aSetOfStructures);
+       aStructIter.More(); aStructIter.Next())
   {
-    const Handle(Graphic3d_Structure)& aStruct = MyIterator.Key();
-    if (!aStruct->IsEmpty())
+    const Handle(Graphic3d_Structure)& aStruct = aStructIter.Key();
+    if (!aStruct->IsVisible()
+      || aStruct->IsInfinite()
+      || (hasSelection && !aStruct->IsHighlighted()))
+    {
+      continue;
+    }
+
+    const Graphic3d_BndBox3d& aBox = aStruct->CStructure()->BoundingBox();
+    if (!aBox.IsValid())
+    {
+      continue;
+    }
+
+    // skip transformation-persistent objects
+    if (!aStruct->TransformPersistence().IsNull())
+    {
+      continue;
+    }
+
+    // use camera projection to find gravity point
+    Xmin = aBox.CornerMin().x();
+    Ymin = aBox.CornerMin().y();
+    Zmin = aBox.CornerMin().z();
+    Xmax = aBox.CornerMax().x();
+    Ymax = aBox.CornerMax().y();
+    Zmax = aBox.CornerMax().z();
+    gp_Pnt aPnts[THE_NB_BOUND_POINTS] =
     {
-      aStruct->MinMaxValues (Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
+      gp_Pnt (Xmin, Ymin, Zmin), gp_Pnt (Xmin, Ymin, Zmax),
+      gp_Pnt (Xmin, Ymax, Zmin), gp_Pnt (Xmin, Ymax, Zmax),
+      gp_Pnt (Xmax, Ymin, Zmin), gp_Pnt (Xmax, Ymin, Zmax),
+      gp_Pnt (Xmax, Ymax, Zmin), gp_Pnt (Xmax, Ymax, Zmax)
+    };
 
-      // Check bounding box for validness
-      Standard_Real aLim = (ShortRealLast() - 1.0);
-      if (Abs (Xmin) > aLim || Abs (Ymin) > aLim || Abs (Zmin) > aLim ||
-          Abs (Xmax) > aLim || Abs (Ymax) > aLim || Abs (Zmax) > aLim)
+    for (Standard_Integer aPntIt = 0; aPntIt < THE_NB_BOUND_POINTS; ++aPntIt)
+    {
+      const gp_Pnt& aBndPnt    = aPnts[aPntIt];
+      const gp_Pnt  aProjected = Camera()->Project (aBndPnt);
+      if (Abs (aProjected.X()) <= 1.0
+       && Abs (aProjected.Y()) <= 1.0)
       {
-        continue;
+        aResult += aBndPnt.XYZ();
+        ++aNbPoints;
       }
+    }
+  }
 
-      // use camera projection to find gravity point
-      gp_Pnt aPnts[8] = { 
+  if (aNbPoints == 0)
+  {
+    // fallback - just use bounding box of entire scene
+    Bnd_Box aBox = myView->MinMaxValues();
+    if (!aBox.IsVoid())
+    {
+      aBox.Get (Xmin, Ymin, Zmin,
+                Xmax, Ymax, Zmax);
+      gp_Pnt aPnts[THE_NB_BOUND_POINTS] =
+      {
         gp_Pnt (Xmin, Ymin, Zmin), gp_Pnt (Xmin, Ymin, Zmax),
         gp_Pnt (Xmin, Ymax, Zmin), gp_Pnt (Xmin, Ymax, Zmax),
         gp_Pnt (Xmax, Ymin, Zmin), gp_Pnt (Xmax, Ymin, Zmax),
-        gp_Pnt (Xmax, Ymax, Zmin), gp_Pnt (Xmax, Ymax, Zmax) };
+        gp_Pnt (Xmax, Ymax, Zmin), gp_Pnt (Xmax, Ymax, Zmax)
+      };
 
-      for (Standard_Integer aPntIt = 0; aPntIt < 8; ++aPntIt)
+      for (Standard_Integer aPntIt = 0; aPntIt < THE_NB_BOUND_POINTS; ++aPntIt)
       {
         const gp_Pnt& aBndPnt = aPnts[aPntIt];
-
-        gp_Pnt aProjected = myCamera->Project (aBndPnt);
-        const Standard_Real& U = aProjected.X();
-        const Standard_Real& V = aProjected.Y();
-        if (Abs(U) <= 1.0 && Abs(V) <= 1.0)
-        {
-          Npoint++;
-          X += aBndPnt.X();
-          Y += aBndPnt.Y();
-          Z += aBndPnt.Z();
-        }
+        aResult += aBndPnt.XYZ();
+        ++aNbPoints;
       }
     }
   }
-  if( Npoint > 0 ) {
-    X /= Npoint ; Y /= Npoint ; Z /= Npoint ;
-  }
 
-  return Nstruct ;
+  if (aNbPoints > 0)
+  {
+    aResult /= aNbPoints;
+  }
+  theX = aResult.X();
+  theY = aResult.Y();
+  theZ = aResult.Z();
 }
 
 //=======================================================================
@@ -2360,21 +2066,12 @@ Standard_Integer V3d_View::Gravity(Standard_Real& X, Standard_Real& Y, Standard_
 //=======================================================================
 void V3d_View::Eye(Standard_Real& X, Standard_Real& Y, Standard_Real& Z) const
 {
-  gp_Pnt aCameraEye = myCamera->Eye();
+  gp_Pnt aCameraEye = Camera()->Eye();
   X = aCameraEye.X();
   Y = aCameraEye.Y();
   Z = aCameraEye.Z();
 }
 
-//=============================================================================
-//function : FocalReferencePoint
-//purpose  :
-//=============================================================================
-void V3d_View::FocalReferencePoint(Standard_Real& X, Standard_Real& Y,Standard_Real& Z) const
-{
-  Eye (X,Y,Z);
-}
-
 //=============================================================================
 //function : ProjReferenceAxe
 //purpose  :
@@ -2410,7 +2107,7 @@ void V3d_View::ProjReferenceAxe(const Standard_Integer Xpix,
 //=============================================================================
 Standard_Real V3d_View::Depth() const
 {
-  return myCamera->Distance();
+  return Camera()->Distance();
 }
 
 //=============================================================================
@@ -2419,7 +2116,7 @@ Standard_Real V3d_View::Depth() const
 //=============================================================================
 void V3d_View::Proj(Standard_Real& Dx, Standard_Real& Dy, Standard_Real& Dz) const
 {
-  gp_Dir aCameraDir = myCamera->Direction().Reversed();
+  gp_Dir aCameraDir = Camera()->Direction().Reversed();
   Dx = aCameraDir.X();
   Dy = aCameraDir.Y();
   Dz = aCameraDir.Z();
@@ -2431,7 +2128,7 @@ void V3d_View::Proj(Standard_Real& Dx, Standard_Real& Dy, Standard_Real& Dz) con
 //=============================================================================
 void V3d_View::At(Standard_Real& X, Standard_Real& Y, Standard_Real& Z) const
 {
-  gp_Pnt aCameraCenter = myCamera->Center();
+  gp_Pnt aCameraCenter = Camera()->Center();
   X = aCameraCenter.X();
   Y = aCameraCenter.Y();
   Z = aCameraCenter.Z();
@@ -2443,7 +2140,7 @@ void V3d_View::At(Standard_Real& X, Standard_Real& Y, Standard_Real& Z) const
 //=============================================================================
 void V3d_View::Up(Standard_Real& Vx, Standard_Real& Vy, Standard_Real& Vz) const
 {
-  gp_Dir aCameraUp = myCamera->Up();
+  gp_Dir aCameraUp = Camera()->Up();
   Vx = aCameraUp.X();
   Vy = aCameraUp.Y();
   Vz = aCameraUp.Z();
@@ -2455,65 +2152,44 @@ void V3d_View::Up(Standard_Real& Vx, Standard_Real& Vy, Standard_Real& Vz) const
 //=============================================================================
 Standard_Real V3d_View::Twist() const
 {
-  Standard_Real Xup,Yup,Zup,Xpn,Ypn,Zpn,X0,Y0,Z0 ;
-  Standard_Real pvx,pvy,pvz,pvn,sca,angle ;
-  Graphic3d_Vector Xaxis,Yaxis,Zaxis ;
-  Standard_Boolean TheStatus ;
+  gp_Vec Xaxis, Yaxis, Zaxis;
+  const gp_Dir aReferencePlane (Camera()->Direction().Reversed());
+  if (!screenAxis (aReferencePlane, gp::DZ(), Xaxis, Yaxis, Zaxis)
+   && !screenAxis (aReferencePlane, gp::DY(), Xaxis, Yaxis, Zaxis)
+   && !screenAxis (aReferencePlane, gp::DX(), Xaxis, Yaxis, Zaxis))
+  {
+    //
+  }
 
-  gp_Dir aReferencePlane (myCamera->Direction().Reversed());
-  gp_Dir anUp;
+  // Compute Cross Vector From Up & Origin
+  const gp_Dir aCameraUp = Camera()->Up();
+  const gp_XYZ aP = Yaxis.XYZ().Crossed (aCameraUp.XYZ());
 
-  Proj(Xpn,Ypn,Zpn);
-  anUp = gp_Dir (0.,0.,1.) ;
-  TheStatus = ScreenAxis (aReferencePlane, anUp,Xaxis,Yaxis,Zaxis) ;
-  if( !TheStatus ) {
-    anUp = gp_Dir (0.,1.,0.) ;
-    TheStatus = ScreenAxis (aReferencePlane, anUp,Xaxis,Yaxis,Zaxis) ;
-  }
-  if( !TheStatus ) {
-    anUp = gp_Dir (1.,0.,0.) ;
-    TheStatus = ScreenAxis (aReferencePlane, anUp,Xaxis,Yaxis,Zaxis) ;
+  // compute Angle
+  Standard_Real anAngle = ASin (Max (Min (aP.Modulus(), 1.0), -1.0));
+  if (Yaxis.Dot (aCameraUp.XYZ()) < 0.0)
+  {
+    anAngle = M_PI - anAngle;
   }
-  Yaxis.Coord(X0,Y0,Z0) ;
-
-  Up(Xup,Yup,Zup) ;
-  /* Compute Cross Vector From Up & Origin */
-  pvx = Y0*Zup - Z0*Yup ;
-  pvy = Z0*Xup - X0*Zup ;
-  pvz = X0*Yup - Y0*Xup ;
-  pvn = pvx*pvx + pvy*pvy + pvz*pvz ;
-  sca = X0*Xup + Y0*Yup + Z0*Zup ;
-  /* Compute Angle */
-  angle = Sqrt(pvn) ;
-  if( angle > 1. ) angle = 1. ;
-  else if( angle < -1. ) angle = -1. ;
-  angle = asin(angle) ;
-  if( sca < 0. ) angle = M_PI - angle ;
-  if( angle > 0. && angle < M_PI ) {
-    sca = pvx*Xpn + pvy*Ypn + pvz*Zpn ;
-    if( sca < 0. ) angle = DEUXPI - angle ;
+  if (anAngle > 0.0
+   && anAngle < M_PI)
+  {
+    const gp_Dir aProjDir = Camera()->Direction().Reversed();
+    if (aP.Dot (aProjDir.XYZ()) < 0.0)
+    {
+      anAngle = DEUXPI - anAngle;
+    }
   }
-  return angle ;
+  return anAngle;
 }
 
 //=============================================================================
 //function : ShadingModel
 //purpose  :
 //=============================================================================
-V3d_TypeOfShadingModel V3d_View::ShadingModel() const
-{
-  V3d_TypeOfShadingModel SM = (V3d_TypeOfShadingModel)MyViewContext.Model() ;
-  return SM ;
-}
-
-//=============================================================================
-//function : SurfaceDetail
-//purpose  :
-//=============================================================================
-V3d_TypeOfSurfaceDetail V3d_View::SurfaceDetail() const
+Graphic3d_TypeOfShadingModel V3d_View::ShadingModel() const
 {
-  V3d_TypeOfSurfaceDetail SM = (V3d_TypeOfSurfaceDetail)MyViewContext.SurfaceDetail() ;
-  return SM ;
+  return myView->ShadingModel();
 }
 
 //=============================================================================
@@ -2522,8 +2198,7 @@ V3d_TypeOfSurfaceDetail V3d_View::SurfaceDetail() const
 //=============================================================================
 Handle(Graphic3d_TextureEnv) V3d_View::TextureEnv() const
 {
-  Handle(Graphic3d_TextureEnv) SM = MyViewContext.TextureEnv() ;
-  return SM ;
+  return myView->TextureEnv();
 }
 
 //=============================================================================
@@ -2532,28 +2207,7 @@ Handle(Graphic3d_TextureEnv) V3d_View::TextureEnv() const
 //=============================================================================
 V3d_TypeOfVisualization V3d_View::Visualization() const
 {
-  V3d_TypeOfVisualization V =
-    (V3d_TypeOfVisualization)MyViewContext.Visualization() ;
-  return V ;
-}
-
-//=============================================================================
-//function : Antialiasing
-//purpose  :
-//=============================================================================
-Standard_Boolean V3d_View::Antialiasing() const
-{
-  Standard_Boolean A = MyViewContext.AliasingIsOn() ;
-  return A ;
-}
-
-//=============================================================================
-//function : Viewer
-//purpose  :
-//=============================================================================
-Handle(V3d_Viewer) V3d_View::Viewer() const
-{
-  return MyViewer ;
+  return static_cast<V3d_TypeOfVisualization> (myView->VisualizationType());
 }
 
 //=============================================================================
@@ -2562,17 +2216,7 @@ Handle(V3d_Viewer) V3d_View::Viewer() const
 //=============================================================================
 Standard_Boolean V3d_View::IfWindow() const
 {
-  Standard_Boolean TheStatus = MyView->IsDefined() ;
-  return TheStatus ;
-}
-
-//=============================================================================
-//function : Window
-//purpose  :
-//=============================================================================
-Handle(Aspect_Window) V3d_View::Window() const
-{
-  return MyWindow;
+  return myView->IsDefined();
 }
 
 //=============================================================================
@@ -2581,7 +2225,7 @@ Handle(Aspect_Window) V3d_View::Window() const
 //=============================================================================
 V3d_TypeOfView V3d_View::Type() const
 {
-  return myCamera->IsOrthographic() ? V3d_ORTHOGRAPHIC : V3d_PERSPECTIVE;
+  return Camera()->IsOrthographic() ? V3d_ORTHOGRAPHIC : V3d_PERSPECTIVE;
 }
 
 //=============================================================================
@@ -2590,14 +2234,16 @@ V3d_TypeOfView V3d_View::Type() const
 //=============================================================================
 void V3d_View::SetFocale( const Standard_Real focale )
 {
-  if (myCamera->IsOrthographic())
+  Handle(Graphic3d_Camera) aCamera = Camera();
+
+  if (aCamera->IsOrthographic())
   {
     return;
   }
 
-  Standard_Real aFOVyRad = ATan (focale / (myCamera->Distance() * 2.0));
+  Standard_Real aFOVyRad = ATan (focale / (aCamera->Distance() * 2.0));
 
-  myCamera->SetFOVy (aFOVyRad * (360 / M_PI));
+  aCamera->SetFOVy (aFOVyRad * (360 / M_PI));
 
   ImmediateUpdate();
 }
@@ -2608,50 +2254,39 @@ void V3d_View::SetFocale( const Standard_Real focale )
 //=============================================================================
 Standard_Real V3d_View::Focale() const
 {
-  if (myCamera->IsOrthographic())
+  Handle(Graphic3d_Camera) aCamera = Camera();
+
+  if (aCamera->IsOrthographic())
   {
     return 0.0;
   }
 
-  return myCamera->Distance() * 2.0 * Tan(myCamera->FOVy() * M_PI / 360.0);
-}
-
-//=============================================================================
-//function : View
-//purpose  :
-//=============================================================================
-Handle(Visual3d_View) V3d_View::View() const
-{
-  return MyView ;
+  return aCamera->Distance() * 2.0 * Tan (aCamera->FOVy() * M_PI / 360.0);
 }
 
 //=============================================================================
-//function : ScreenAxis
+//function : screenAxis
 //purpose  :
 //=============================================================================
-Standard_Boolean V3d_View::ScreenAxis( const gp_Dir &Vpn, const gp_Dir &Vup, Graphic3d_Vector &Xaxe, Graphic3d_Vector &Yaxe, Graphic3d_Vector &Zaxe)
+Standard_Boolean V3d_View::screenAxis (const gp_Dir& theVpn, const gp_Dir& theVup,
+                                       gp_Vec& theXaxe, gp_Vec& theYaxe, gp_Vec& theZaxe)
 {
-  Standard_Real Xpn, Ypn, Zpn, Xup, Yup, Zup;
-  Standard_Real dx1, dy1, dz1, xx, yy, zz;
+  theXaxe = theVup.XYZ().Crossed (theVpn.XYZ());
+  if (theXaxe.Magnitude() <= gp::Resolution())
+  {
+    return Standard_False;
+  }
+  theXaxe.Normalize();
 
-  Xpn = Vpn.X(); Ypn = Vpn.Y(); Zpn = Vpn.Z();
-  Xup = Vup.X(); Yup = Vup.Y(); Zup = Vup.Z();
-  xx = Yup*Zpn - Zup*Ypn;
-  yy = Zup*Xpn - Xup*Zpn;
-  zz = Xup*Ypn - Yup*Xpn;
-  Xaxe.SetCoord (xx, yy, zz);
-  if (Xaxe.LengthZero()) return Standard_False;
-  Xaxe.Normalize(); 
-  Xaxe.Coord(dx1, dy1, dz1);
-  xx = Ypn*dz1 - Zpn*dy1;
-  yy = Zpn*dx1 - Xpn*dz1;
-  zz = Xpn*dy1 - Ypn*dx1;
-  Yaxe.SetCoord (xx, yy, zz) ;
-  if (Yaxe.LengthZero()) return Standard_False;
-  Yaxe.Normalize(); 
+  theYaxe = theVpn.XYZ().Crossed (theXaxe.XYZ());
+  if (theYaxe.Magnitude() <= gp::Resolution())
+  {
+    return Standard_False;
+  }
+  theYaxe.Normalize();
 
-  Zaxe.SetCoord (Xpn, Ypn, Zpn);
-  Zaxe.Normalize();
+  theZaxe = theVpn.XYZ();
+  theZaxe.Normalize();
   return Standard_True;
 }
 
@@ -2659,33 +2294,24 @@ Standard_Boolean V3d_View::ScreenAxis( const gp_Dir &Vpn, const gp_Dir &Vup, Gra
 //function : TrsPoint
 //purpose  :
 //=============================================================================
-Graphic3d_Vertex V3d_View::TrsPoint( const Graphic3d_Vertex &P, const TColStd_Array2OfReal &Matrix )
+gp_XYZ V3d_View::TrsPoint (const Graphic3d_Vertex& thePnt, const TColStd_Array2OfReal& theMat)
 {
-  Graphic3d_Vertex PP ;
-  Standard_Real X,Y,Z,XX,YY,ZZ ;
-
   // CAL. S3892
-  Standard_Integer lr, ur, lc, uc;
-  lr    = Matrix.LowerRow ();
-  ur    = Matrix.UpperRow ();
-  lc    = Matrix.LowerCol ();
-  uc    = Matrix.UpperCol ();
-  if ((ur - lr + 1 != 4) || (uc - lc + 1 != 4) ) {
-    P.Coord(X,Y,Z) ;
-    PP.SetCoord(X,Y,Z) ;
-    return PP ;
+  const Standard_Integer lr = theMat.LowerRow();
+  const Standard_Integer ur = theMat.UpperRow();
+  const Standard_Integer lc = theMat.LowerCol();
+  const Standard_Integer uc = theMat.UpperCol();
+  if ((ur - lr + 1 != 4) || (uc - lc + 1 != 4))
+  {
+    return gp_XYZ (thePnt.X(), thePnt.Y(), thePnt.Z());
   }
-  P.Coord(X,Y,Z) ;
-  XX = (Matrix(lr,lc+3) + X*Matrix(lr,lc) + Y*Matrix(lr,lc+1)+
-    Z*Matrix(lr,lc+2))/Matrix(lr+3,lc+3) ;
 
-  YY = (Matrix(lr+1,lc+3) + X*Matrix(lr+1,lc) + Y*Matrix(lr+1,lc+1) +
-    Z*Matrix(lr+1,lc+2))/Matrix(lr+3,lc+3) ;
-
-  ZZ = (Matrix(lr+2,lc+3) + X*Matrix(lr+2,lc) + Y*Matrix(lr+2,lc+1) +
-    Z*Matrix(lr+2,lc+2))/Matrix(lr+3,lc+3) ;
-  PP.SetCoord(XX,YY,ZZ) ;
-  return PP ;
+  Standard_Real X, Y, Z;
+  thePnt.Coord (X,Y,Z);
+  const Standard_Real XX = (theMat(lr,lc+3)   + X*theMat(lr,lc)   + Y*theMat(lr,lc+1)   + Z*theMat(lr,lc+2)) / theMat(lr+3,lc+3);
+  const Standard_Real YY = (theMat(lr+1,lc+3) + X*theMat(lr+1,lc) + Y*theMat(lr+1,lc+1) + Z*theMat(lr+1,lc+2))/theMat(lr+3,lc+3);
+  const Standard_Real ZZ = (theMat(lr+2,lc+3) + X*theMat(lr+2,lc) + Y*theMat(lr+2,lc+1) + Z*theMat(lr+2,lc+2))/theMat(lr+3,lc+3);
+  return gp_XYZ (XX, YY, ZZ);
 }
 
 //=======================================================================
@@ -2694,7 +2320,7 @@ Graphic3d_Vertex V3d_View::TrsPoint( const Graphic3d_Vertex &P, const TColStd_Ar
 //=======================================================================
 void V3d_View::Pan (const Standard_Integer theDXp,
                     const Standard_Integer theDYp,
-                    const Quantity_Factor  theZoomFactor,
+                    const Standard_Real    theZoomFactor,
                     const Standard_Boolean theToStart)
 {
   Panning (Convert (theDXp), Convert (theDYp), theZoomFactor, theToStart);
@@ -2706,25 +2332,27 @@ void V3d_View::Pan (const Standard_Integer theDXp,
 //=======================================================================
 void V3d_View::Panning (const Standard_Real theDXv,
                         const Standard_Real theDYv,
-                        const Quantity_Factor theZoomFactor,
+                        const Standard_Real theZoomFactor,
                         const Standard_Boolean theToStart)
 {
   Standard_ASSERT_RAISE (theZoomFactor > 0.0, "Bad zoom factor");
 
+  Handle(Graphic3d_Camera) aCamera = Camera();
+
   if (theToStart)
   {
-    myCamStartOpEye = myCamera->Eye();
-    myCamStartOpCenter = myCamera->Center();
+    myCamStartOpEye    = aCamera->Eye();
+    myCamStartOpCenter = aCamera->Center();
   }
 
   Standard_Boolean wasUpdateEnabled = SetImmediateUpdate (Standard_False);
 
-  gp_Pnt aViewDims = myCamera->ViewDimensions();
+  gp_Pnt aViewDims = aCamera->ViewDimensions();
 
-  myCamera->SetEye (myCamStartOpEye);
-  myCamera->SetCenter (myCamStartOpCenter);
-  Translate (myCamera, -theDXv, -theDYv);
-  Scale (myCamera, aViewDims.X() / theZoomFactor, aViewDims.Y() / theZoomFactor);
+  aCamera->SetEye (myCamStartOpEye);
+  aCamera->SetCenter (myCamStartOpCenter);
+  Translate (aCamera, -theDXv, -theDYv);
+  Scale (aCamera, aViewDims.X() / theZoomFactor, aViewDims.Y() / theZoomFactor);
 
   SetImmediateUpdate (wasUpdateEnabled);
 
@@ -2779,8 +2407,10 @@ void V3d_View::ZoomAtPoint (const Standard_Integer theMouseStartX,
 
   V3d_BadValue_Raise_if (aDZoom <= 0.0, "V3d_View::ZoomAtPoint, bad coefficient");
 
-  Standard_Real aViewWidth  = myCamera->ViewDimensions().X();
-  Standard_Real aViewHeight = myCamera->ViewDimensions().Y();
+  Handle(Graphic3d_Camera) aCamera = Camera();
+
+  Standard_Real aViewWidth  = aCamera->ViewDimensions().X();
+  Standard_Real aViewHeight = aCamera->ViewDimensions().Y();
 
   // ensure that zoom will not be too small or too big.
   Standard_Real aCoef = aDZoom;
@@ -2805,11 +2435,11 @@ void V3d_View::ZoomAtPoint (const Standard_Integer theMouseStartX,
   Standard_Real aZoomAtPointYv = 0.0;
   Convert (MyZoomAtPointX, MyZoomAtPointY, aZoomAtPointXv, aZoomAtPointYv);
 
-  V3d_Coordinate aDxv = aZoomAtPointXv / aCoef;
-  V3d_Coordinate aDyv = aZoomAtPointYv / aCoef;
+  Standard_Real aDxv = aZoomAtPointXv / aCoef;
+  Standard_Real aDyv = aZoomAtPointYv / aCoef;
 
-  myCamera->SetScale (myCamera->Scale() / aCoef);
-  Translate (myCamera, aZoomAtPointXv - aDxv, aZoomAtPointYv - aDyv);
+  aCamera->SetScale (aCamera->Scale() / aCoef);
+  Translate (aCamera, aZoomAtPointXv - aDxv, aZoomAtPointYv - aDyv);
 
   AutoZFit();
 
@@ -2842,31 +2472,29 @@ void V3d_View::AxialScale (const Standard_Integer Dx,
 //function : FitAll
 //purpose  :
 //=============================================================================
-void V3d_View::FitAll(const Handle(Aspect_Window)& aWindow,
-                      const Standard_Real Xmin,
-                      const Standard_Real Ymin,
-                      const Standard_Real Xmax,
-                      const Standard_Real Ymax)
+void V3d_View::FitAll(const Standard_Real theXmin,
+                      const Standard_Real theYmin,
+                      const Standard_Real theXmax,
+                      const Standard_Real theYmax)
 {
-  Standard_Integer aWinWidth, aWinHeight;
-  aWindow->Size (aWinWidth, aWinHeight);
+  Handle(Graphic3d_Camera) aCamera = Camera();
+  Standard_Real anAspect = aCamera->Aspect();
 
-  Standard_Real aWinAspect = (Standard_Real)aWinWidth / aWinHeight;
-  Standard_Real aFitSizeU  = Abs (Xmax - Xmin);
-  Standard_Real aFitSizeV  = Abs (Ymax - Ymin);
+  Standard_Real aFitSizeU  = Abs (theXmax - theXmin);
+  Standard_Real aFitSizeV  = Abs (theYmax - theYmin);
   Standard_Real aFitAspect = aFitSizeU / aFitSizeV;
-  if (aFitAspect >= aWinAspect)
+  if (aFitAspect >= anAspect)
   {
-    aFitSizeV = aFitSizeU / aWinAspect;
+    aFitSizeV = aFitSizeU / anAspect;
   }
   else
   {
-    aFitSizeU = aFitSizeV * aWinAspect;
+    aFitSizeU = aFitSizeV * anAspect;
   }
 
-  myCamera->SetAspect (aWinAspect);
-  Translate (myCamera, (Xmin + Xmax) * 0.5, (Ymin + Ymax) * 0.5);
-  Scale (myCamera, aFitSizeU, aFitSizeV);
+  Translate (aCamera, (theXmin + theXmax) * 0.5, (theYmin + theYmax) * 0.5);
+  Scale (aCamera, aFitSizeU, aFitSizeV);
+
   AutoZFit();
 
   ImmediateUpdate();
@@ -2876,12 +2504,9 @@ void V3d_View::FitAll(const Handle(Aspect_Window)& aWindow,
 //function : StartRotation
 //purpose  :
 //=============================================================================
-#ifdef IMP250900
-static Standard_Boolean zRotation = Standard_False;
-#endif
 void V3d_View::StartRotation(const Standard_Integer X,
                              const Standard_Integer Y,
-                             const Quantity_Ratio zRotationThreshold)
+                             const Standard_Real zRotationThreshold)
 {
   sx = X; sy = Y;
   Standard_Real x,y;
@@ -2890,16 +2515,14 @@ void V3d_View::StartRotation(const Standard_Integer X,
   ry = Standard_Real(Convert(y));
   Gravity(gx,gy,gz);
   Rotate(0.,0.,0.,gx,gy,gz,Standard_True);
-#ifdef IMP250900
-  zRotation = Standard_False;
+  myZRotation = Standard_False;
   if( zRotationThreshold > 0. ) {
     Standard_Real dx = Abs(sx - rx/2.);
     Standard_Real dy = Abs(sy - ry/2.);
-    //  if( dx > rx/3. || dy > ry/3. ) zRotation = Standard_True;
+    //  if( dx > rx/3. || dy > ry/3. ) myZRotation = Standard_True;
     Standard_Real dd = zRotationThreshold * (rx + ry)/2.;
-    if( dx > dd || dy > dd ) zRotation = Standard_True;
+    if( dx > dd || dy > dd ) myZRotation = Standard_True;
   }
-#endif
 
 }
 
@@ -2910,56 +2533,38 @@ void V3d_View::StartRotation(const Standard_Integer X,
 void V3d_View::Rotation(const Standard_Integer X,
                         const Standard_Integer Y)
 {
-#ifdef IMP210600
   if( rx == 0. || ry == 0. ) {
     StartRotation(X,Y);
     return;
   }
-#endif
-#ifdef IMP250900
   Standard_Real dx=0.,dy=0.,dz=0.;
-  if( zRotation ) {
+  if( myZRotation ) {
     dz = atan2(Standard_Real(X)-rx/2., ry/2.-Standard_Real(Y)) -
       atan2(sx-rx/2.,ry/2.-sy);
   } else {
     dx = (Standard_Real(X) - sx) * M_PI / rx;
     dy = (sy - Standard_Real(Y)) * M_PI / ry;
   }
+
   Rotate(dx, dy, dz, gx, gy, gz, Standard_False);
-#else
-  Standard_Real dx = (Standard_Real(X - sx)) * M_PI;
-  Standard_Real dy = (Standard_Real(sy - Y)) * M_PI;
-  Rotate(dx/rx, dy/ry, 0., gx, gy, gz, Standard_False);
-#endif
-#ifdef IMP020300
-  if( !myImmediateUpdate ) Update();
-#else
-  myImmediateUpdate = Standard_False;
-  Rotate(dx/rx, dy/ry, 0., gx, gy, gz, Standard_False);
-  ZFitAll (Zmargin);    //Don't do that, perf improvment
-  myImmediateUpdate = Standard_True;
-  ImmediateUpdate();
-#endif
 }
 
 //=============================================================================
 //function : SetComputedMode
 //purpose  :
 //=============================================================================
-void V3d_View::SetComputedMode (const Standard_Boolean aMode)
+void V3d_View::SetComputedMode (const Standard_Boolean theMode)
 {
-  if (aMode)
+  if (theMode)
   {
     if (myComputedMode)
     {
-      MyView->SetComputedMode (Standard_True);
-      Update();
+      myView->SetComputedMode (Standard_True);
     }
-  } 
-  else 
+  }
+  else
   {
-    MyView->SetComputedMode (Standard_False);
-    Update();
+    myView->SetComputedMode (Standard_False);
   }
 }
 
@@ -2969,16 +2574,16 @@ void V3d_View::SetComputedMode (const Standard_Boolean aMode)
 //=============================================================================
 Standard_Boolean V3d_View::ComputedMode() const
 {
-  return MyView->ComputedMode();
+  return myView->ComputedMode();
 }
 
 //=============================================================================
 //function : SetBackFacingModel
 //purpose  :
 //=============================================================================
-void V3d_View::SetBackFacingModel (const V3d_TypeOfBackfacingModel aModel)
+void V3d_View::SetBackFacingModel (const V3d_TypeOfBackfacingModel theModel)
 {
-  MyView->SetBackFacingModel (Visual3d_TypeOfBackfacingModel(aModel));
+  myView->SetBackfacingModel (static_cast<Graphic3d_TypeOfBackfacingModel> (theModel));
   Redraw();
 }
 
@@ -2988,14 +2593,19 @@ void V3d_View::SetBackFacingModel (const V3d_TypeOfBackfacingModel aModel)
 //=============================================================================
 V3d_TypeOfBackfacingModel V3d_View::BackFacingModel() const
 {
-  return V3d_TypeOfBackfacingModel(MyView -> BackFacingModel ());
+  return static_cast<V3d_TypeOfBackfacingModel> (myView->BackfacingModel());
 }
 
+//=============================================================================
+//function : Init
+//purpose  :
+//=============================================================================
 void V3d_View::Init()
 {
   myComputedMode = MyViewer->ComputedMode();
-  if( !myComputedMode || !MyViewer->DefaultComputedMode() ) {
-    SetComputedMode(Standard_False);
+  if (!myComputedMode || !MyViewer->DefaultComputedMode())
+  {
+    SetComputedMode (Standard_False);
   }
 }
 
@@ -3018,153 +2628,255 @@ Standard_Boolean V3d_View::Dump (const Standard_CString      theFile,
 //purpose  :
 //=============================================================================
 Standard_Boolean V3d_View::ToPixMap (Image_PixMap&               theImage,
-                                     const Standard_Integer      theWidth,
-                                     const Standard_Integer      theHeight,
-                                     const Graphic3d_BufferType& theBufferType,
-                                     const Standard_Boolean      theToKeepAspect,
-                                     const V3d_StereoDumpOptions theStereoOptions)
-{
-  Graphic3d_CView* cView = (Graphic3d_CView* )MyView->CView();
-
-  // always prefer hardware accelerated offscreen buffer
-  Graphic3d_PtrFrameBuffer aFBOPtr = NULL;
-  Graphic3d_PtrFrameBuffer aPrevFBOPtr = (Graphic3d_PtrFrameBuffer )cView->ptrFBO;
-  Standard_Integer aFBOVPSizeX (theWidth), aFBOVPSizeY (theHeight), aFBOSizeXMax (0), aFBOSizeYMax (0);
-  Standard_Integer aPrevFBOVPSizeX (0), aPrevFBOVPSizeY (0), aPrevFBOSizeXMax (0), aPrevFBOSizeYMax (0);
-  if (aPrevFBOPtr != NULL)
-  {
-    MyView->FBOGetDimensions (aPrevFBOPtr,
-                              aPrevFBOVPSizeX, aPrevFBOVPSizeY,
-                              aPrevFBOSizeXMax, aPrevFBOSizeYMax);
-    if (aFBOVPSizeX <= aPrevFBOSizeXMax && aFBOVPSizeY <= aPrevFBOSizeYMax)
+                                     const V3d_ImageDumpOptions& theParams)
+{
+  Graphic3d_Vec2i aTargetSize (theParams.Width, theParams.Height);
+  if (aTargetSize.x() != 0
+   && aTargetSize.y() != 0)
+  {
+    // allocate image buffer for dumping
+    if (theImage.IsEmpty()
+     || theImage.SizeX() != Standard_Size(aTargetSize.x())
+     || theImage.SizeY() != Standard_Size(aTargetSize.y()))
     {
-      MyView->FBOChangeViewport (aPrevFBOPtr, aFBOVPSizeX, aFBOVPSizeY);
-      aFBOPtr = aPrevFBOPtr;
+      Image_Format aFormat = Image_Format_UNKNOWN;
+      switch (theParams.BufferType)
+      {
+        case Graphic3d_BT_RGB:                 aFormat = Image_Format_RGB;   break;
+        case Graphic3d_BT_RGBA:                aFormat = Image_Format_RGBA;  break;
+        case Graphic3d_BT_Depth:               aFormat = Image_Format_GrayF; break;
+        case Graphic3d_BT_RGB_RayTraceHdrLeft: aFormat = Image_Format_RGBF;  break;
+      }
+
+      if (!theImage.InitZero (aFormat, Standard_Size(aTargetSize.x()), Standard_Size(aTargetSize.y())))
+      {
+        Message::DefaultMessenger()->Send (TCollection_AsciiString ("Fail to allocate an image ") + aTargetSize.x() + "x" + aTargetSize.y()
+                                                                 + " for view dump", Message_Fail);
+        return Standard_False;
+      }
     }
   }
+  if (theImage.IsEmpty())
+  {
+    Message::DefaultMessenger()->Send (TCollection_AsciiString ("V3d_View::ToPixMap() has been called without image dimensions"), Message_Fail);
+    return Standard_False;
+  }
+  aTargetSize.x() = (Standard_Integer )theImage.SizeX();
+  aTargetSize.y() = (Standard_Integer )theImage.SizeY();
+
+  Handle(Standard_Transient) aFBOPtr;
+  Handle(Standard_Transient) aPrevFBOPtr = myView->FBO();
+  Graphic3d_Vec2i aFBOVPSize = aTargetSize;
 
-  if (aFBOPtr == NULL)
+  bool isTiling = false;
+  if (theParams.TileSize > 0)
   {
-    // Try to create hardware accelerated buffer
-    aFBOPtr = MyView->FBOCreate (aFBOVPSizeX, aFBOVPSizeY);
-    if (aFBOPtr != NULL)
+    if (aFBOVPSize.x() > theParams.TileSize
+     || aFBOVPSize.y() > theParams.TileSize)
     {
-      MyView->FBOGetDimensions (aFBOPtr,
-                                aFBOVPSizeX,  aFBOVPSizeY,
-                                aFBOSizeXMax, aFBOSizeYMax);
-      // reduce viewport in case of hardware limits
-      if (aFBOVPSizeX > aFBOSizeXMax) aFBOVPSizeX = aFBOSizeXMax;
-      if (aFBOVPSizeY > aFBOSizeYMax) aFBOVPSizeY = aFBOSizeYMax;
-      MyView->FBOChangeViewport (aFBOPtr, aFBOVPSizeX, aFBOVPSizeY);
+      aFBOVPSize.x() = Min (aFBOVPSize.x(), theParams.TileSize);
+      aFBOVPSize.y() = Min (aFBOVPSize.y(), theParams.TileSize);
+      isTiling = true;
     }
   }
-  cView->ptrFBO = aFBOPtr;
 
-  // If hardware accelerated buffer - try to use onscreen buffer
-  // Results may be bad!
-  if (aFBOPtr == NULL)
+  Graphic3d_Vec2i aPrevFBOVPSize;
+  if (!aPrevFBOPtr.IsNull())
   {
-    // retrieve window sizes
-    Standard_Integer aWinWidth, aWinHeight;
-    MyWindow->Size (aWinWidth, aWinHeight);
+    Graphic3d_Vec2i aPrevFBOSizeMax;
+    myView->FBOGetDimensions (aPrevFBOPtr,
+                              aPrevFBOVPSize.x(),  aPrevFBOVPSize.y(),
+                              aPrevFBOSizeMax.x(), aPrevFBOSizeMax.y());
+    if (aFBOVPSize.x() <= aPrevFBOSizeMax.x()
+     && aFBOVPSize.y() <= aPrevFBOSizeMax.y())
+    {
+      aFBOPtr = aPrevFBOPtr;
+    }
+  }
 
-    // technically we can reduce existing viewport...
-    // but currently allow only dumping the window itself
-    if (aFBOVPSizeX != aWinWidth || aFBOVPSizeY != aWinHeight)
+  if (aFBOPtr.IsNull())
+  {
+    Standard_Integer aMaxTexSizeX = MyViewer->Driver()->InquireLimit (Graphic3d_TypeOfLimit_MaxViewDumpSizeX);
+    Standard_Integer aMaxTexSizeY = MyViewer->Driver()->InquireLimit (Graphic3d_TypeOfLimit_MaxViewDumpSizeY);
+    if (theParams.TileSize > aMaxTexSizeX
+     || theParams.TileSize > aMaxTexSizeY)
     {
+      Message::DefaultMessenger()->Send (TCollection_AsciiString ("Image dump can not be performed - specified tile size (")
+                                                                 + theParams.TileSize + ") exceeds hardware limits (" + aMaxTexSizeX + "x" + aMaxTexSizeY + ")", Message_Fail);
       return Standard_False;
     }
+
+    if (aFBOVPSize.x() > aMaxTexSizeX
+     || aFBOVPSize.y() > aMaxTexSizeY)
+    {
+      if (MyViewer->Driver()->InquireLimit (Graphic3d_TypeOfLimit_IsWorkaroundFBO))
+      {
+        Message::DefaultMessenger ()->Send (TCollection_AsciiString ("Warning, workaround for Intel driver problem with empty FBO for images with big width is applyed."), Message_Warning);
+      }
+      Message::DefaultMessenger()->Send (TCollection_AsciiString ("Info, tiling image dump is used, image size (")
+                                                                 + aFBOVPSize.x() + "x" + aFBOVPSize.y() + ") exceeds hardware limits (" + aMaxTexSizeX + "x" + aMaxTexSizeY + ")", Message_Info);
+      aFBOVPSize.x() = Min (aFBOVPSize.x(), aMaxTexSizeX);
+      aFBOVPSize.y() = Min (aFBOVPSize.y(), aMaxTexSizeY);
+      isTiling = true;
+    }
+
+    // Try to create hardware accelerated buffer
+    aFBOPtr = myView->FBOCreate (aFBOVPSize.x(), aFBOVPSize.y());
   }
+  myView->SetFBO (aFBOPtr);
 
-  Handle(Graphic3d_Camera) aStoreMapping = new Graphic3d_Camera();
+  if (aFBOPtr.IsNull())
+  {
+    // try to use on-screen buffer
+    Graphic3d_Vec2i aWinSize;
+    MyWindow->Size (aWinSize.x(), aWinSize.y());
+    if (aFBOVPSize.x() != aWinSize.x()
+     || aFBOVPSize.y() != aWinSize.y())
+    {
+      isTiling = true;
+    }
+    aFBOVPSize = aWinSize;
 
-  aStoreMapping->Copy (myCamera);
+    Message::DefaultMessenger()->Send (TCollection_AsciiString ("Warning, on screen buffer is used for image dump - content might be invalid"), Message_Warning);
+  }
 
-  if (myCamera->IsStereo())
+  // backup camera parameters
+  Handle(Graphic3d_Camera) aStoreMapping = new Graphic3d_Camera();
+  Handle(Graphic3d_Camera) aCamera = Camera();
+  aStoreMapping->Copy (aCamera);
+  if (aCamera->IsStereo())
   {
-    switch (theStereoOptions)
+    switch (theParams.StereoOptions)
     {
-      case V3d_SDO_MONO :
-        myCamera->SetProjectionType (Graphic3d_Camera::Projection_Perspective);
+      case V3d_SDO_MONO:
+      {
+        aCamera->SetProjectionType (Graphic3d_Camera::Projection_Perspective);
         break;
-
-      case V3d_SDO_LEFT_EYE :
-        myCamera->SetProjectionType (Graphic3d_Camera::Projection_MonoLeftEye);
+      }
+      case V3d_SDO_LEFT_EYE:
+      {
+        aCamera->SetProjectionType (Graphic3d_Camera::Projection_MonoLeftEye);
         break;
-
-      case V3d_SDO_RIGHT_EYE :
-        myCamera->SetProjectionType (Graphic3d_Camera::Projection_MonoRightEye);
+      }
+      case V3d_SDO_RIGHT_EYE:
+      {
+        aCamera->SetProjectionType (Graphic3d_Camera::Projection_MonoRightEye);
         break;
+      }
+      case V3d_SDO_BLENDED:
+      {
+        break; // dump as is
+      }
     }
   }
-
-  // render immediate structures into back buffer rather than front
-  Handle(Graphic3d_GraphicDriver) aDriver = Handle(Graphic3d_GraphicDriver)::DownCast (MyView->GraphicDriver());
-  const Standard_Boolean aPrevImmediateMode = aDriver.IsNull() ? Standard_True : aDriver->SetImmediateModeDrawToFront (*cView, Standard_False);
-
-  const Standard_Boolean toAutoUpdate = myImmediateUpdate;
-  myImmediateUpdate = Standard_False;
-  AutoZFit();
-  myImmediateUpdate = toAutoUpdate;
-
-  if (theToKeepAspect)
-  {
-    myCamera->SetAspect ((Standard_Real) aFBOVPSizeX / aFBOVPSizeY);
-  }
-
-  //workaround for rendering list of Over and Under Layers
-  if (!MyLayerMgr.IsNull())
+  if (theParams.ToAdjustAspect)
   {
-    MyLayerMgr->Compute();
+    aCamera->SetAspect (Standard_Real(aTargetSize.x()) / Standard_Real(aTargetSize.y()));
   }
+  AutoZFit();
 
-  Redraw();
+  // render immediate structures into back buffer rather than front
+  const Standard_Boolean aPrevImmediateMode = myView->SetImmediateModeDrawToFront (Standard_False);
 
-  if (!aDriver.IsNull())
+  Standard_Boolean isSuccess = Standard_True;
+  if (!isTiling)
   {
-    aDriver->SetImmediateModeDrawToFront (*cView, aPrevImmediateMode);
+    if (!aFBOPtr.IsNull())
+    {
+      myView->FBOChangeViewport (aFBOPtr, aTargetSize.x(), aTargetSize.y());
+    }
+    Redraw();
+    isSuccess = isSuccess && myView->BufferDump (theImage, theParams.BufferType);
   }
+  else
+  {
+    Image_PixMap aTilePixMap;
+    aTilePixMap.SetTopDown (theImage.IsTopDown());
 
-  myCamera->Copy (aStoreMapping);
+    Graphic3d_Vec2i anOffset (0, 0);
+    for (; anOffset.y() < aTargetSize.y(); anOffset.y() += aFBOVPSize.y())
+    {
+      anOffset.x() = 0;
+      for (; anOffset.x() < aTargetSize.x(); anOffset.x() += aFBOVPSize.x())
+      {
+        Graphic3d_CameraTile aTileUncropped;
+        aTileUncropped.Offset    = anOffset;
+        aTileUncropped.TotalSize = aTargetSize;
+        aTileUncropped.TileSize  = aFBOVPSize;
+        const Graphic3d_CameraTile aTile = aTileUncropped.Cropped();
+        if (aTile.TileSize.x() < 1
+         || aTile.TileSize.y() < 1)
+        {
+          continue;
+        }
 
-  Standard_Boolean isSuccess = Standard_True;
+        const Standard_Integer aLeft   = aTile.Offset.x();
+        Standard_Integer       aBottom = aTile.Offset.y();
+        if (theImage.IsTopDown())
+        {
+          const Standard_Integer aTop = aTile.Offset.y() + aTile.TileSize.y();
+          aBottom = aTargetSize.y() - aTop;
+        }
+        aTilePixMap.InitWrapper (theImage.Format(), theImage.ChangeData()
+                               + theImage.SizeRowBytes() * aBottom + theImage.SizePixelBytes() * aLeft,
+                                 aTile.TileSize.x(), aTile.TileSize.y(),
+                                 theImage.SizeRowBytes());
 
-  // allocate image buffer for dumping
-  if (theImage.IsEmpty()
-   || (Standard_Size )aFBOVPSizeX != theImage.SizeX()
-   || (Standard_Size )aFBOVPSizeY != theImage.SizeY())
-  {
-    bool isBigEndian = Image_PixMap::IsBigEndianHost();
-    Image_PixMap::ImgFormat aFormat = Image_PixMap::ImgUNKNOWN;
-    switch (theBufferType)
-    {
-      case Graphic3d_BT_RGB:   aFormat = isBigEndian ? Image_PixMap::ImgRGB  : Image_PixMap::ImgBGR;  break;
-      case Graphic3d_BT_RGBA:  aFormat = isBigEndian ? Image_PixMap::ImgRGBA : Image_PixMap::ImgBGRA; break;
-      case Graphic3d_BT_Depth: aFormat = Image_PixMap::ImgGrayF; break;
+        if (!aFBOPtr.IsNull())
+        {
+          aCamera->SetTile (aTile);
+          myView->FBOChangeViewport (aFBOPtr, aTile.TileSize.x(), aTile.TileSize.y());
+        }
+        else
+        {
+          // no API to resize viewport of on-screen buffer - render uncropped
+          aCamera->SetTile (aTileUncropped);
+        }
+        Redraw();
+        isSuccess = isSuccess && myView->BufferDump (aTilePixMap, theParams.BufferType);
+        if (!isSuccess)
+        {
+          break;
+        }
+      }
+      if (!isSuccess)
+      {
+        break;
+      }
     }
-
-    isSuccess = isSuccess && theImage.InitZero (aFormat, aFBOVPSizeX, aFBOVPSizeY);
   }
-  isSuccess = isSuccess && MyView->BufferDump (theImage, theBufferType);
 
-  // FBO now useless, free resources
+  // restore state
+  myView->SetImmediateModeDrawToFront (aPrevImmediateMode);
+  aCamera->Copy (aStoreMapping);
   if (aFBOPtr != aPrevFBOPtr)
   {
-    MyView->FBORelease (aFBOPtr);
+    myView->FBORelease (aFBOPtr);
   }
-  else if (aPrevFBOPtr != NULL)
+  else if (!aPrevFBOPtr.IsNull())
   {
-    MyView->FBOChangeViewport (aPrevFBOPtr, aPrevFBOVPSizeX, aPrevFBOVPSizeY);
+    myView->FBOChangeViewport (aPrevFBOPtr, aPrevFBOVPSize.x(), aPrevFBOVPSize.y());
   }
-  cView->ptrFBO = aPrevFBOPtr;
+  myView->SetFBO (aPrevFBOPtr);
   return isSuccess;
 }
 
+//=============================================================================
+//function : ImmediateUpdate
+//purpose  :
+//=============================================================================
 void V3d_View::ImmediateUpdate() const
 {
-  if (myImmediateUpdate) Update();
+  if (myImmediateUpdate)
+  {
+    Update();
+  }
 }
 
+//=============================================================================
+//function : SetImmediateUpdate
+//purpose  :
+//=============================================================================
 Standard_Boolean V3d_View::SetImmediateUpdate (const Standard_Boolean theImmediateUpdate)
 {
   Standard_Boolean aPreviousMode = myImmediateUpdate;
@@ -3178,11 +2890,9 @@ Standard_Boolean V3d_View::SetImmediateUpdate (const Standard_Boolean theImmedia
 // =======================================================================
 void V3d_View::SetCamera (const Handle(Graphic3d_Camera)& theCamera)
 {
-  Standard_ASSERT_RAISE (!theCamera.IsNull(), "Void camera is not allowed");
-
-  myCamera = theCamera;
+  myView->SetCamera (theCamera);
 
-  MyView->SetCamera (theCamera);
+  ImmediateUpdate();
 }
 
 // =======================================================================
@@ -3191,7 +2901,7 @@ void V3d_View::SetCamera (const Handle(Graphic3d_Camera)& theCamera)
 // =======================================================================
 const Handle(Graphic3d_Camera)& V3d_View::Camera() const
 {
-  return myCamera;
+  return myView->Camera();
 }
 
 // =======================================================================
@@ -3199,16 +2909,13 @@ const Handle(Graphic3d_Camera)& V3d_View::Camera() const
 // purpose  : Internal
 // =======================================================================
 Standard_Boolean V3d_View::FitMinMax (const Handle(Graphic3d_Camera)& theCamera,
-                                      const gp_XYZ& theMinCorner,
-                                      const gp_XYZ& theMaxCorner,
+                                      const Bnd_Box& theBox,
                                       const Standard_Real theMargin,
                                       const Standard_Real theResolution,
                                       const Standard_Boolean theToEnlargeIfLine) const
 {
   // Check bounding box for validness
-  Standard_Real aLim = (ShortRealLast() - 1.0);
-  if (Abs (theMinCorner.X()) > aLim || Abs (theMinCorner.Y()) > aLim || Abs (theMinCorner.Z()) > aLim ||
-      Abs (theMaxCorner.X()) > aLim || Abs (theMaxCorner.Y()) > aLim || Abs (theMaxCorner.Z()) > aLim)
+  if (theBox.IsVoid())
   {
     return Standard_False; // bounding box is out of bounds...
   }
@@ -3219,34 +2926,41 @@ Standard_Boolean V3d_View::FitMinMax (const Handle(Graphic3d_Camera)& theCamera,
   // option is to perform frustum plane adjustment algorithm in view camera space,
   // which will lead to a number of additional world-view space conversions and
   // loosing precision as well.
-  Standard_Real aXmin = theMinCorner.X() * theCamera->AxialScale().X();
-  Standard_Real aXmax = theMaxCorner.X() * theCamera->AxialScale().X();
-  Standard_Real aYmin = theMinCorner.Y() * theCamera->AxialScale().Y();
-  Standard_Real aYmax = theMaxCorner.Y() * theCamera->AxialScale().Y();
-  Standard_Real aZmin = theMinCorner.Z() * theCamera->AxialScale().Z();
-  Standard_Real aZmax = theMaxCorner.Z() * theCamera->AxialScale().Z();
+  gp_Pnt aBndMin = theBox.CornerMin().XYZ().Multiplied (theCamera->AxialScale());
+  gp_Pnt aBndMax = theBox.CornerMax().XYZ().Multiplied (theCamera->AxialScale());
 
-  Bnd_Box aBBox;
-  aBBox.Update (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
-  if (aBBox.IsThin (RealEpsilon()))
+  if (aBndMax.IsEqual (aBndMin, RealEpsilon()))
   {
     return Standard_False; // nothing to fit all
   }
 
-  gp_Pnt aBBCenter ((aXmin + aXmax) * 0.5, (aYmin + aYmax) * 0.5, (aZmin + aZmax) * 0.5);
-
-  gp_Pln aFrustumLeft;
-  gp_Pln aFrustumRight;
-  gp_Pln aFrustumBottom;
-  gp_Pln aFrustumTop;
-  gp_Pln aFrustumNear;
-  gp_Pln aFrustumFar;
-  theCamera->Frustum (aFrustumLeft, aFrustumRight, aFrustumBottom, aFrustumTop, aFrustumNear, aFrustumFar);
+  // Prepare camera frustum planes.
+  NCollection_Array1<gp_Pln> aFrustumPlane (1, 6);
+  theCamera->Frustum (aFrustumPlane.ChangeValue (1),
+                      aFrustumPlane.ChangeValue (2),
+                      aFrustumPlane.ChangeValue (3),
+                      aFrustumPlane.ChangeValue (4),
+                      aFrustumPlane.ChangeValue (5),
+                      aFrustumPlane.ChangeValue (6));
 
+  // Prepare camera up, side, direction vectors.
   gp_Dir aCamUp  = theCamera->OrthogonalizedUp();
   gp_Dir aCamDir = theCamera->Direction();
   gp_Dir aCamSide = aCamDir ^ aCamUp;
 
+  // Prepare scene bounding box parameters.
+  gp_Pnt aBndCenter = (aBndMin.XYZ() + aBndMax.XYZ()) / 2.0;
+
+  NCollection_Array1<gp_Pnt> aBndCorner (1, 8);
+  aBndCorner.ChangeValue (1) = gp_Pnt (aBndMin.X(), aBndMin.Y(), aBndMin.Z());
+  aBndCorner.ChangeValue (2) = gp_Pnt (aBndMin.X(), aBndMin.Y(), aBndMax.Z());
+  aBndCorner.ChangeValue (3) = gp_Pnt (aBndMin.X(), aBndMax.Y(), aBndMin.Z());
+  aBndCorner.ChangeValue (4) = gp_Pnt (aBndMin.X(), aBndMax.Y(), aBndMax.Z());
+  aBndCorner.ChangeValue (5) = gp_Pnt (aBndMax.X(), aBndMin.Y(), aBndMin.Z());
+  aBndCorner.ChangeValue (6) = gp_Pnt (aBndMax.X(), aBndMin.Y(), aBndMax.Z());
+  aBndCorner.ChangeValue (7) = gp_Pnt (aBndMax.X(), aBndMax.Y(), aBndMin.Z());
+  aBndCorner.ChangeValue (8) = gp_Pnt (aBndMax.X(), aBndMax.Y(), aBndMax.Z());
+
   // Perspective-correct camera projection vector, matching the bounding box is determined geometrically.
   // Knowing the initial shape of a frustum it is possible to match it to a bounding box.
   // Then, knowing the relation of camera projection vector to the frustum shape it is possible to
@@ -3263,30 +2977,33 @@ Standard_Boolean V3d_View::FitMinMax (const Handle(Graphic3d_Camera)& theCamera,
   // 3) Determine new camera projection vector using the normalized asymmetry.
   // 4) Determine new zooming in view space.
 
-  // Determine normalized projection asymmetry (if any).
-
-  Standard_Real anAssymX = Tan ( aCamSide.Angle (aFrustumLeft.Axis().Direction()))
-                         - Tan (-aCamSide.Angle (aFrustumRight.Axis().Direction()));
-  Standard_Real anAssymY = Tan ( aCamUp.Angle   (aFrustumBottom.Axis().Direction()))
-                         - Tan (-aCamUp.Angle   (aFrustumTop.Axis().Direction()));
-
-  // Determine how far should be the frustum planes placed from center
-  // of bounding box, in order to match the bounding box closely.
-  gp_Pln aMatchSide[6] = {aFrustumLeft, aFrustumRight, aFrustumBottom, aFrustumTop, aFrustumNear, aFrustumFar};
-  Standard_Real aMatchDistance[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
-  for (Standard_Integer anIt = 0; anIt < 6; ++anIt)
+  // 1. Determine normalized projection asymmetry (if any).
+  Standard_Real anAssymX = Tan (( aCamSide).Angle (aFrustumPlane (1).Axis().Direction()))
+                         - Tan ((-aCamSide).Angle (aFrustumPlane (2).Axis().Direction()));
+  Standard_Real anAssymY = Tan (( aCamUp)  .Angle (aFrustumPlane (3).Axis().Direction()))
+                         - Tan ((-aCamUp)  .Angle (aFrustumPlane (4).Axis().Direction()));
+
+  // 2. Determine how far should be the frustum planes placed from center
+  //    of bounding box, in order to match the bounding box closely.
+  NCollection_Array1<Standard_Real> aFitDistance (1, 6);
+  aFitDistance.ChangeValue (1) = 0.0;
+  aFitDistance.ChangeValue (2) = 0.0;
+  aFitDistance.ChangeValue (3) = 0.0;
+  aFitDistance.ChangeValue (4) = 0.0;
+  aFitDistance.ChangeValue (5) = 0.0;
+  aFitDistance.ChangeValue (6) = 0.0;
+
+  for (Standard_Integer anI = aFrustumPlane.Lower(); anI <= aFrustumPlane.Upper(); ++anI)
   {
-    const gp_Dir& aPlaneN = aMatchSide[anIt].Axis().Direction();
+    // Measure distances from center of bounding box to its corners towards the frustum plane.
+    const gp_Dir& aPlaneN = aFrustumPlane.ChangeValue (anI).Axis().Direction();
 
-    gp_Trsf aPlaneTrsf;
-    aPlaneTrsf.SetTransformation (gp_Ax3(), gp_Ax3 (aBBCenter, aPlaneN));
-    Bnd_Box aRelativeBBox = aBBox.Transformed (aPlaneTrsf);
+    Standard_Real& aFitDist = aFitDistance.ChangeValue (anI);
 
-    Standard_Real aDummy = 0.0;
-    Standard_Real aZmin  = 0.0;
-    Standard_Real aZmax  = 0.0;
-    aRelativeBBox.Get (aDummy, aDummy, aZmin, aDummy, aDummy, aZmax);
-    aMatchDistance[anIt] = -aZmin;
+    for (Standard_Integer aJ = aBndCorner.Lower(); aJ <= aBndCorner.Upper(); ++aJ)
+    {
+      aFitDist = Max (aFitDist, gp_Vec (aBndCenter, aBndCorner (aJ)).Dot (aPlaneN));
+    }
   }
   // The center of camera is placed on the same line with center of bounding box.
   // The view plane section crosses the bounding box at its center.
@@ -3304,32 +3021,31 @@ Standard_Boolean V3d_View::FitMinMax (const Handle(Graphic3d_Camera)& theCamera,
   //                            \//
   //                            //
   //                      (frustum plane)
-
-  aMatchDistance[0] *= Sqrt(1 + Pow (Tan ( aCamSide.Angle (aFrustumLeft.Axis().Direction())),   2.0));
-  aMatchDistance[1] *= Sqrt(1 + Pow (Tan (-aCamSide.Angle (aFrustumRight.Axis().Direction())),  2.0));
-  aMatchDistance[2] *= Sqrt(1 + Pow (Tan ( aCamUp.Angle   (aFrustumBottom.Axis().Direction())), 2.0));
-  aMatchDistance[3] *= Sqrt(1 + Pow (Tan (-aCamUp.Angle   (aFrustumTop.Axis().Direction())),    2.0));
-  aMatchDistance[4] *= Sqrt(1 + Pow (Tan ( aCamDir.Angle  (aFrustumNear.Axis().Direction())),   2.0));
-  aMatchDistance[5] *= Sqrt(1 + Pow (Tan (-aCamDir.Angle  (aFrustumFar.Axis().Direction())),    2.0));
-
-  Standard_Real aViewSizeXv = aMatchDistance[0] + aMatchDistance[1];
-  Standard_Real aViewSizeYv = aMatchDistance[2] + aMatchDistance[3];
-  Standard_Real aViewSizeZv = aMatchDistance[4] + aMatchDistance[5];
-
-  // Place center of camera on the same line with center of bounding
-  // box applying corresponding projection asymmetry (if any).
+  aFitDistance.ChangeValue (1) *= Sqrt(1 + Pow (Tan (  aCamSide .Angle (aFrustumPlane (1).Axis().Direction())), 2.0));
+  aFitDistance.ChangeValue (2) *= Sqrt(1 + Pow (Tan ((-aCamSide).Angle (aFrustumPlane (2).Axis().Direction())), 2.0));
+  aFitDistance.ChangeValue (3) *= Sqrt(1 + Pow (Tan (  aCamUp   .Angle (aFrustumPlane (3).Axis().Direction())), 2.0));
+  aFitDistance.ChangeValue (4) *= Sqrt(1 + Pow (Tan ((-aCamUp)  .Angle (aFrustumPlane (4).Axis().Direction())), 2.0));
+  aFitDistance.ChangeValue (5) *= Sqrt(1 + Pow (Tan (  aCamDir  .Angle (aFrustumPlane (5).Axis().Direction())), 2.0));
+  aFitDistance.ChangeValue (6) *= Sqrt(1 + Pow (Tan ((-aCamDir) .Angle (aFrustumPlane (6).Axis().Direction())), 2.0));
+
+  Standard_Real aViewSizeXv = aFitDistance (1) + aFitDistance (2);
+  Standard_Real aViewSizeYv = aFitDistance (3) + aFitDistance (4);
+  Standard_Real aViewSizeZv = aFitDistance (5) + aFitDistance (6);
+
+  // 3. Place center of camera on the same line with center of bounding
+  //    box applying corresponding projection asymmetry (if any).
   Standard_Real anAssymXv = anAssymX * aViewSizeXv * 0.5;
   Standard_Real anAssymYv = anAssymY * aViewSizeYv * 0.5;
-  Standard_Real anOffsetXv = (aMatchDistance[1] - aMatchDistance[0]) * 0.5 + anAssymXv;
-  Standard_Real anOffsetYv = (aMatchDistance[3] - aMatchDistance[2]) * 0.5 + anAssymYv;
+  Standard_Real anOffsetXv = (aFitDistance (2) - aFitDistance (1)) * 0.5 + anAssymXv;
+  Standard_Real anOffsetYv = (aFitDistance (4) - aFitDistance (3)) * 0.5 + anAssymYv;
   gp_Vec aTranslateSide = gp_Vec (aCamSide) * anOffsetXv;
   gp_Vec aTranslateUp   = gp_Vec (aCamUp)   * anOffsetYv;
-  gp_Pnt aNewCenter     = aBBCenter.Translated (aTranslateSide).Translated (aTranslateUp);
+  gp_Pnt aCamNewCenter  = aBndCenter.Translated (aTranslateSide).Translated (aTranslateUp);
 
   gp_Trsf aCenterTrsf;
-  aCenterTrsf.SetTranslation (theCamera->Center(), aNewCenter);
+  aCenterTrsf.SetTranslation (theCamera->Center(), aCamNewCenter);
   theCamera->Transform (aCenterTrsf);
-  theCamera->SetDistance (aMatchDistance[5] + aMatchDistance[4]);
+  theCamera->SetDistance (aFitDistance (6) + aFitDistance (5));
 
   // Bounding box collapses to a point or thin line going in depth of the screen
   if (aViewSizeXv < theResolution && aViewSizeYv < theResolution)
@@ -3345,7 +3061,11 @@ Standard_Boolean V3d_View::FitMinMax (const Handle(Graphic3d_Camera)& theCamera,
     aViewSizeYv = aViewSizeZv;
   }
 
-  Scale (theCamera, aViewSizeXv * (1.0 + theMargin), aViewSizeYv * (1.0 + theMargin));
+  Scale (theCamera, aViewSizeXv, aViewSizeYv);
+
+  const Standard_Real aZoomCoef = myView->ConsiderZoomPersistenceObjects();
+
+  Scale (theCamera, theCamera->ViewDimensions().X() * (aZoomCoef + theMargin), theCamera->ViewDimensions().Y() * (aZoomCoef + theMargin));
 
   return Standard_True;
 }
@@ -3359,8 +3079,14 @@ void V3d_View::Scale (const Handle(Graphic3d_Camera)& theCamera,
                       const Standard_Real theSizeYv) const
 {
   Standard_Real anAspect = theCamera->Aspect();
-  Standard_Real aMaxSize = Max (theSizeXv / anAspect, theSizeYv);
-  theCamera->SetScale (aMaxSize);
+  if (anAspect > 1.0)
+  {
+    theCamera->SetScale (Max (theSizeXv / anAspect, theSizeYv));
+  }
+  else
+  {
+    theCamera->SetScale (Max (theSizeXv, theSizeYv * anAspect));
+  }
 }
 
 // =======================================================================
@@ -3386,21 +3112,47 @@ void V3d_View::Translate (const Handle(Graphic3d_Camera)& theCamera,
 }
 
 // =======================================================================
-// function : IsCullingEnabled
+// function : DiagnosticInformation
 // purpose  :
 // =======================================================================
-Standard_Boolean V3d_View::IsCullingEnabled() const
+void V3d_View::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
+                                      Graphic3d_DiagnosticInfo theFlags) const
+{
+  myView->DiagnosticInformation (theDict, theFlags);
+}
+
+//=======================================================================
+//function : StatisticInformation
+//purpose  :
+//=======================================================================
+void V3d_View::StatisticInformation (TColStd_IndexedDataMapOfStringString& theDict) const
 {
-  Graphic3d_CView* aView = (Graphic3d_CView* )MyView->CView();
-  return aView->IsCullingEnabled;
+  myView->StatisticInformation (theDict);
 }
 
 // =======================================================================
-// function : SetFrustumCulling
+// function : StatisticInformation
 // purpose  :
 // =======================================================================
-void V3d_View::SetFrustumCulling (const Standard_Boolean theToClip)
+TCollection_AsciiString V3d_View::StatisticInformation() const
+{
+  return myView->StatisticInformation();
+}
+
+//=============================================================================
+//function : RenderingParams
+//purpose  :
+//=============================================================================
+const Graphic3d_RenderingParams& V3d_View::RenderingParams() const
+{
+  return myView->RenderingParams();
+}
+
+//=============================================================================
+//function : ChangeRenderingParams
+//purpose  :
+//=============================================================================
+Graphic3d_RenderingParams& V3d_View::ChangeRenderingParams()
 {
-  Graphic3d_CView* aView = (Graphic3d_CView* )MyView->CView();
-  aView->IsCullingEnabled = theToClip;
+  return myView->ChangeRenderingParams();
 }