0026154: Visualization - v3d viewer grid disappears due to automatic z-fit
[occt.git] / src / V3d / V3d_RectangularGrid.cxx
index a95be25..4a24e55 100644 (file)
 // Alternatively, this file may be used under the terms of Open CASCADE
 // commercial license or contractual agreement.
 
-/***********************************************************************
-     FONCTION :
-     ----------
-        Classe V3d_CircularGrid :
-     HISTORIQUE DES MODIFICATIONS   :
-     --------------------------------
-      16-08-98 : CAL ; S3892. Ajout grilles 3d.
-      13-10-98 : CAL ; S3892. Ajout de la gestion de la taille des grilles 3d.
-      23-10-98 : CAL ; PRO 15885. Patch K4403 et K4404
-      03-11-98 : CAL ; PRO 16161. Patch K4418 et K4419
-      IMP230300: GG The color fields and methods have moved in Aspect_Grid
-
-
-************************************************************************/
-
-#define IMP200100      //GG 
-//                     -> Compute the case Aspect_GDM_None
-//                     -> Initialize the grid size according to the
-//                       viewer default size.
-
-#define IMP200300      //GG
-//                     -> Recompute the grid when any graphic parameter is
-//                       modified.
+#include <V3d_RectangularGrid.hxx>
 
-/*----------------------------------------------------------------------*/
-/*
- * Includes
- */
-
-#include <V3d_RectangularGrid.ixx>
-
-#include <TColStd_Array2OfReal.hxx>
+#include <Graphic3d_ArrayOfPoints.hxx>
+#include <Graphic3d_ArrayOfSegments.hxx>
 #include <Graphic3d_AspectLine3d.hxx>
 #include <Graphic3d_AspectMarker3d.hxx>
 #include <Graphic3d_AspectText3d.hxx>
-#include <Graphic3d_ArrayOfPoints.hxx>
-#include <Visual3d_ViewManager.hxx>
-#include <V3d_Viewer.hxx>
+#include <Graphic3d_Group.hxx>
+#include <Graphic3d_Structure.hxx>
+#include <Quantity_Color.hxx>
+#include <Standard_Type.hxx>
 #include <TColgp_SequenceOfPnt.hxx>
-#include <Graphic3d_ArrayOfSegments.hxx>
+#include <TColStd_Array2OfReal.hxx>
+#include <V3d_Viewer.hxx>
 
-/*----------------------------------------------------------------------*/
-/*
- * Constant
- */
+IMPLEMENT_STANDARD_RTTIEXT(V3d_RectangularGrid,Aspect_RectangularGrid)
+
+namespace
+{
+  static const Standard_Real THE_DEFAULT_GRID_STEP = 10.0;
+  #define MYFACTOR 50.
+}
+
+//! Dummy implementation of Graphic3d_Structure overriding ::Compute() method for handling Device Lost.
+class V3d_RectangularGrid::RectangularGridStructure : public Graphic3d_Structure
+{
+public:
+  //! Main constructor.
+  RectangularGridStructure (const Handle(Graphic3d_StructureManager)& theManager, V3d_RectangularGrid* theGrid)
+  : Graphic3d_Structure (theManager), myGrid (theGrid) {}
+
+  //! Override method initiating recomputing in V3d_RectangularGrid.
+  virtual void Compute() Standard_OVERRIDE
+  {
+    GraphicClear (Standard_False);
+    myGrid->myGroup = NewGroup();
+    myGrid->myCurAreDefined = Standard_False;
+    myGrid->UpdateDisplay();
+  }
 
-#define MYMINMAX 25.
-#define MYFACTOR 50.
+private:
+  V3d_RectangularGrid* myGrid;
+};
 
 /*----------------------------------------------------------------------*/
 
 V3d_RectangularGrid::V3d_RectangularGrid (const V3d_ViewerPointer& aViewer, const Quantity_Color& aColor, const Quantity_Color& aTenthColor)
 : Aspect_RectangularGrid (1.,1.),
-  myStructure (new Graphic3d_Structure (aViewer->Viewer ())),
-  myGroup (new Graphic3d_Group (myStructure)),
   myViewer (aViewer),
-  myCurAreDefined (Standard_False)
+  myCurAreDefined (Standard_False),
+  myToComputePrs (Standard_True),
+  myCurDrawMode (Aspect_GDM_Lines),
+  myCurXo (0.0),
+  myCurYo (0.0),
+  myCurAngle (0.0),
+  myCurXStep (0.0),
+  myCurYStep (0.0),
+  myXSize  (0.5 * aViewer->DefaultViewSize()),
+  myYSize  (0.5 * aViewer->DefaultViewSize()),
+  myOffSet (THE_DEFAULT_GRID_STEP / MYFACTOR)
 {
   myColor = aColor;
   myTenthColor = aTenthColor;
 
+  myStructure = new RectangularGridStructure (aViewer->StructureManager(), this);
+  myGroup = myStructure->NewGroup();
   myStructure->SetInfiniteState (Standard_True);
 
-  const Standard_Real step = 10.;
-  const Standard_Real gstep = step/MYFACTOR;
-  const Standard_Real size = 0.5*myViewer->DefaultViewSize();
-  SetGraphicValues (size, size, gstep);
-  SetXStep (step);
-  SetYStep (step);
+  SetXStep (THE_DEFAULT_GRID_STEP);
+  SetYStep (THE_DEFAULT_GRID_STEP);
+}
+
+V3d_RectangularGrid::~V3d_RectangularGrid()
+{
+  myGroup.Nullify();
+  if (!myStructure.IsNull())
+  {
+    myStructure->Erase();
+  }
 }
 
 void V3d_RectangularGrid::SetColors (const Quantity_Color& aColor, const Quantity_Color& aTenthColor)
@@ -96,7 +104,9 @@ void V3d_RectangularGrid::SetColors (const Quantity_Color& aColor, const Quantit
 
 void V3d_RectangularGrid::Display ()
 {
-  myStructure->Display (1);
+  myStructure->SetDisplayPriority (1);
+  myStructure->Display();
+  UpdateDisplay();
 }
 
 void V3d_RectangularGrid::Erase () const
@@ -147,50 +157,31 @@ void V3d_RectangularGrid::UpdateDisplay ()
   if (MakeTransform) {
     const Standard_Real CosAlpha = Cos (RotationAngle ());
     const Standard_Real SinAlpha = Sin (RotationAngle ());
-    TColStd_Array2OfReal Trsf (1, 4, 1, 4);
-    Trsf (4, 4) = 1.0;
-    Trsf (4, 1) = Trsf (4, 2) = Trsf (4, 3) = 0.0;
+
+    gp_Trsf aTrsf;
     // Translation
-    Trsf (1, 4) = xl,
-    Trsf (2, 4) = yl,
-    Trsf (3, 4) = zl;
     // Transformation of change of marker
-    Trsf (1, 1) = xdx,
-    Trsf (2, 1) = xdy,
-    Trsf (3, 1) = xdz,
-    Trsf (1, 2) = ydx,
-    Trsf (2, 2) = ydy,
-    Trsf (3, 2) = ydz,
-    Trsf (1, 3) = dx,
-    Trsf (2, 3) = dy,
-    Trsf (3, 3) = dz;
-    myStructure->SetTransform (Trsf, Graphic3d_TOC_REPLACE);
+    aTrsf.SetValues (xdx, ydx, dx, xl,
+                     xdy, ydy, dy, yl,
+                     xdz, ydz, dz, zl);
 
     // Translation of the origin
-    Trsf (1, 4) = -XOrigin (),
-    Trsf (2, 4) = -YOrigin (),
-    Trsf (3, 4) = 0.0;
     // Rotation Alpha around axis -Z
-    Trsf (1, 1) = CosAlpha,
-    Trsf (2, 1) = -SinAlpha,
-    Trsf (3, 1) = 0.0,
-    Trsf (1, 2) = SinAlpha,
-    Trsf (2, 2) = CosAlpha,
-    Trsf (3, 2) = 0.0,
-    Trsf (1, 3) = 0.0,
-    Trsf (2, 3) = 0.0,
-    Trsf (3, 3) = 1.0;
-    myStructure->SetTransform (Trsf,Graphic3d_TOC_POSTCONCATENATE);
+    gp_Trsf aTrsf2;
+    aTrsf2.SetValues ( CosAlpha, SinAlpha, 0.0, -XOrigin(),
+                      -SinAlpha, CosAlpha, 0.0, -YOrigin(),
+                            0.0,      0.0, 1.0, 0.0);
+    aTrsf.Multiply (aTrsf2);
+    myStructure->SetTransformation (new Geom_Transformation (aTrsf));
 
     myCurAngle = RotationAngle ();
     myCurXo = XOrigin (), myCurYo = YOrigin ();
     myCurViewPlane = ThePlane;
   }
 
-  switch (DrawMode ())
+  switch (myDrawMode)
   {
-    default:
-    //case Aspect_GDM_Points:
+    case Aspect_GDM_Points:
       DefinePoints ();
       myCurDrawMode = Aspect_GDM_Points;
       break;
@@ -198,11 +189,9 @@ void V3d_RectangularGrid::UpdateDisplay ()
       DefineLines ();
       myCurDrawMode = Aspect_GDM_Lines;
       break;
-#ifdef IMP210100
     case Aspect_GDM_None:
       myCurDrawMode = Aspect_GDM_None;
       break;
-#endif
        }
        myCurAreDefined = Standard_True;
 }
@@ -215,18 +204,20 @@ void V3d_RectangularGrid::DefineLines ()
                                  || myCurDrawMode != Aspect_GDM_Lines
                                  || aXStep != myCurXStep
                                  || aYStep != myCurYStep;
-  if (!toUpdate)
+  if (!toUpdate
+   && !myToComputePrs)
+  {
+    return;
+  }
+  else if (!myStructure->IsDisplayed())
   {
+    myToComputePrs = Standard_True;
     return;
   }
 
+  myToComputePrs = Standard_False;
   myGroup->Clear();
 
-  Handle(Graphic3d_AspectLine3d) LineAttrib = new Graphic3d_AspectLine3d ();
-  LineAttrib->SetColor (myColor);
-  LineAttrib->SetType (Aspect_TOL_SOLID);
-  LineAttrib->SetWidth (1.0);
-
   Standard_Integer nblines;
   Standard_Real xl, yl, zl = myOffSet;
 
@@ -258,8 +249,8 @@ void V3d_RectangularGrid::DefineLines ()
 
   if (aSeqLines.Length())
   {
-    LineAttrib->SetColor (myColor);
-    myGroup->SetPrimitivesAspect (LineAttrib);
+    Handle(Graphic3d_AspectLine3d) aLineAspect = new Graphic3d_AspectLine3d (myColor, Aspect_TOL_SOLID, 1.0);
+    myGroup->SetPrimitivesAspect (aLineAspect);
     const Standard_Integer nbv = aSeqLines.Length();
     Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(nbv);
     Standard_Integer n = 1;
@@ -269,8 +260,8 @@ void V3d_RectangularGrid::DefineLines ()
   }
   if (aSeqTenth.Length())
   {
-    LineAttrib->SetColor (myTenthColor);
-    myGroup->SetPrimitivesAspect (LineAttrib);
+    Handle(Graphic3d_AspectLine3d) aLineAspect = new Graphic3d_AspectLine3d (myTenthColor, Aspect_TOL_SOLID, 1.0);
+    myGroup->SetPrimitivesAspect (aLineAspect);
     const Standard_Integer nbv = aSeqTenth.Length();
     Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(nbv);
     Standard_Integer n = 1;
@@ -279,8 +270,12 @@ void V3d_RectangularGrid::DefineLines ()
     myGroup->AddPrimitiveArray(aPrims, Standard_False);
   }
 
-  myGroup->SetMinMaxValues(-myXSize, -myYSize, 0.0, myXSize, myYSize, 0.0);
+  myGroup->SetMinMaxValues(-myXSize, -myYSize, -myOffSet, myXSize, myYSize, -myOffSet);
   myCurXStep = aXStep, myCurYStep = aYStep;
+
+  // update bounding box
+  myStructure->CalculateBoundBox();
+  myViewer->StructureManager()->Update (myStructure->GetZLayer());
 }
 
 void V3d_RectangularGrid::DefinePoints ()
@@ -291,17 +286,19 @@ void V3d_RectangularGrid::DefinePoints ()
                                   || myCurDrawMode != Aspect_GDM_Points
                                   || aXStep != myCurXStep
                                   || aYStep != myCurYStep;
-  if (!toUpdate)
+  if (!toUpdate
+   && !myToComputePrs)
   {
     return;
   }
+  else if (!myStructure->IsDisplayed())
+  {
+    myToComputePrs = Standard_True;
+    return;
+  }
 
-  myGroup->Clear ();
-
-  Handle(Graphic3d_AspectMarker3d) MarkerAttrib = new Graphic3d_AspectMarker3d ();
-  MarkerAttrib->SetColor (myColor);
-  MarkerAttrib->SetType (Aspect_TOM_POINT);
-  MarkerAttrib->SetScale (3.);
+  myToComputePrs = Standard_False;
+  myGroup->Clear();
 
   // horizontals
   Standard_Real xl, yl;
@@ -327,12 +324,18 @@ void V3d_RectangularGrid::DefinePoints ()
       aSeqPnts(i).Coord(X,Y,Z);
       Vertical->AddVertex (X,Y,Z);
     }
-    myGroup->SetGroupPrimitivesAspect (MarkerAttrib);
+
+    Handle(Graphic3d_AspectMarker3d) aMarkerAspect = new Graphic3d_AspectMarker3d (Aspect_TOM_POINT, myColor, 3.0);
+    myGroup->SetGroupPrimitivesAspect (aMarkerAspect);
     myGroup->AddPrimitiveArray (Vertical, Standard_False);
   }
 
-  myGroup->SetMinMaxValues(-myXSize, -myYSize, 0.0, myXSize, myYSize, 0.0);
+  myGroup->SetMinMaxValues(-myXSize, -myYSize, -myOffSet, myXSize, myYSize, -myOffSet);
   myCurXStep = aXStep, myCurYStep = aYStep;
+
+  // update bounding box
+  myStructure->CalculateBoundBox();
+  myViewer->StructureManager()->Update (myStructure->GetZLayer());
 }
 
 void V3d_RectangularGrid::GraphicValues (Standard_Real& theXSize, Standard_Real& theYSize, Standard_Real& theOffSet) const