0026348: Visualization, TKOpenGl - eliminate invalid NULL checks for transformation...
authorosa <osa@opencascade.com>
Tue, 24 Nov 2015 12:26:34 +0000 (15:26 +0300)
committerbugmaster <bugmaster@opencascade.com>
Fri, 27 Nov 2015 07:08:30 +0000 (10:08 +0300)
Graphic3d_CStructure::Transformation - use Graphic3d_Mat4 instead of plain arrays.
Drop duplicating field OpenGl_Structure::myTransformation (copy of Graphic3d_CStructure::Transformation).
Drop unused property Graphic3d_CStructure::Composition and Graphic3d_Structure::Composition().

12 files changed:
src/Graphic3d/Graphic3d_CStructure.cxx
src/Graphic3d/Graphic3d_CStructure.hxx
src/Graphic3d/Graphic3d_Structure.cxx
src/Graphic3d/Graphic3d_Structure.hxx
src/OpenGl/FILES
src/OpenGl/OpenGl_Matrix.cxx [deleted file]
src/OpenGl/OpenGl_Matrix.hxx
src/OpenGl/OpenGl_Structure.cxx
src/OpenGl/OpenGl_Structure.hxx
src/OpenGl/OpenGl_StructureShadow.cxx
src/OpenGl/OpenGl_View.hxx
src/OpenGl/OpenGl_View_Raytrace.cxx

index 2097704..6cd5009 100644 (file)
@@ -28,7 +28,6 @@ Graphic3d_CStructure::Graphic3d_CStructure (const Handle(Graphic3d_StructureMana
   myZLayer         (Graphic3d_ZLayerId_Default),
   Priority         (Structure_MAX_PRIORITY / 2),
   PreviousPriority (Structure_MAX_PRIORITY / 2),
-  Composition      (Graphic3d_TOC_REPLACE),
   ContainsFacet    (0),
   IsInfinite       (0),
   stick            (0),
@@ -40,14 +39,6 @@ Graphic3d_CStructure::Graphic3d_CStructure (const Handle(Graphic3d_StructureMana
   Is2dText         (Standard_False),
   myGraphicDriver  (theManager->GraphicDriver())
 {
-  for (Standard_Integer i = 0; i <= 3; ++i)
-  {
-    for (Standard_Integer j = 0; j <= 3; ++j)
-    {
-      Transformation[i][j] = (i == j) ? 1.0f : 0.0f;
-    }
-  }
-
   ContextLine.IsDef     = 1,
   ContextFillArea.IsDef = 1,
   ContextMarker.IsDef   = 1,
index 126835e..18d30ab 100644 (file)
@@ -129,8 +129,7 @@ public:
 
   CALL_DEF_COLOR HighlightColor;
 
-  float Transformation[4][4];
-  Graphic3d_TypeOfComposition Composition;
+  Graphic3d_Mat4           Transformation;
 
   int   ContainsFacet;
 
index 655499a..2740194 100644 (file)
@@ -435,39 +435,13 @@ Standard_Boolean Graphic3d_Structure::IsVisible() const
   return myCStructure->visible ? Standard_True : Standard_False;
 }
 
-//=============================================================================
-//function : IsRotated
-//purpose  :
-//=============================================================================
-Standard_Boolean Graphic3d_Structure::IsRotated() const
-{
-  // A somewhat light test !
-  return myCStructure->Transformation[0][1] != 0.0
-      || myCStructure->Transformation[0][2] != 0.0
-      || myCStructure->Transformation[1][0] != 0.0
-      || myCStructure->Transformation[1][2] != 0.0
-      || myCStructure->Transformation[2][0] != 0.0
-      || myCStructure->Transformation[2][1] != 0.0;
-}
-
 //=============================================================================
 //function : IsTransformed
 //purpose  :
 //=============================================================================
 Standard_Boolean Graphic3d_Structure::IsTransformed() const
 {
-  Standard_Boolean aResult = Standard_False;
-  for (Standard_Integer i = 0; i <= 3 && !aResult; ++i)
-  {
-    for (Standard_Integer j = 0; j <= 3 && !aResult; ++j)
-    {
-      if (i == j)
-        aResult = myCStructure->Transformation[i][j] != 1.0;
-      else
-        aResult = myCStructure->Transformation[i][j] != 0.0;
-    }
-  }
-  return aResult;
+  return !myCStructure->Transformation.IsIdentity();
 }
 
 //=============================================================================
@@ -1500,15 +1474,6 @@ void Graphic3d_Structure::DisconnectAll (const Graphic3d_TypeOfConnection theTyp
   }
 }
 
-//=============================================================================
-//function : Composition
-//purpose  :
-//=============================================================================
-Graphic3d_TypeOfComposition Graphic3d_Structure::Composition() const
-{
-  return myCStructure->Composition;
-}
-
 //=============================================================================
 //function : SetTransform
 //purpose  :
@@ -1537,17 +1502,17 @@ void Graphic3d_Structure::SetTransform (const TColStd_Array2OfReal&       theMat
     Graphic3d_TransformError::Raise ("Transform : not a 4x4 matrix");
   }
 
+  const Standard_Boolean wasTransformed = IsTransformed();
   switch (theType)
   {
     case Graphic3d_TOC_REPLACE:
     {
-      myCStructure->Composition = Graphic3d_TOC_REPLACE;
       // Update of CStructure
       for (Standard_Integer i = 0; i <= 3; ++i)
       {
         for (Standard_Integer j = 0; j <= 3; ++j)
         {
-          myCStructure->Transformation[i][j] = float (theMatrix (lr + i, lc + j));
+          myCStructure->Transformation.ChangeValue (i, j) = float (theMatrix (lr + i, lc + j));
           aNewTrsf (i, j) = theMatrix (lr + i, lc + j);
         }
       }
@@ -1555,7 +1520,6 @@ void Graphic3d_Structure::SetTransform (const TColStd_Array2OfReal&       theMat
     }
     case Graphic3d_TOC_POSTCONCATENATE:
     {
-      myCStructure->Composition = Graphic3d_TOC_POSTCONCATENATE;
       // To simplify management of indices
       for (Standard_Integer i = 0; i <= 3; ++i)
       {
@@ -1573,7 +1537,7 @@ void Graphic3d_Structure::SetTransform (const TColStd_Array2OfReal&       theMat
           aNewTrsf (i, j) = 0.0;
           for (Standard_Integer k = 0; k <= 3; ++k)
           {
-            valueoldtrsf = myCStructure->Transformation[i][k];
+            valueoldtrsf = myCStructure->Transformation.GetValue (i, k);
             valuetrsf    = aMatrix44 (k, j);
             valuenewtrsf = aNewTrsf (i, j) + valueoldtrsf * valuetrsf;
             aNewTrsf (i, j) = valuenewtrsf;
@@ -1586,7 +1550,7 @@ void Graphic3d_Structure::SetTransform (const TColStd_Array2OfReal&       theMat
       {
         for (Standard_Integer j = 0; j <= 3; ++j)
         {
-          myCStructure->Transformation[i][j] = float (aNewTrsf (i, j));
+          myCStructure->Transformation.ChangeValue (i, j) = float (aNewTrsf (i, j));
         }
       }
       break;
@@ -1594,7 +1558,7 @@ void Graphic3d_Structure::SetTransform (const TColStd_Array2OfReal&       theMat
   }
 
   // If transformation, no validation of hidden already calculated parts
-  if (IsRotated())
+  if (IsTransformed() || (!IsTransformed() && wasTransformed))
   {
     ReCompute();
   }
@@ -1624,7 +1588,7 @@ void Graphic3d_Structure::Transform (TColStd_Array2OfReal& theMatrix) const
   {
     for (Standard_Integer j = 0; j <= 3; ++j)
     {
-      theMatrix (lr + i, lc + j) = myCStructure->Transformation[i][j];
+      theMatrix (lr + i, lc + j) = myCStructure->Transformation.GetValue (i, j);
     }
   }
 }
@@ -2251,7 +2215,7 @@ void Graphic3d_Structure::GraphicTransform (const TColStd_Array2OfReal& theMatri
   {
     for (Standard_Integer j = 0; j <= 3; ++j)
     {
-      myCStructure->Transformation[i][j] = float (theMatrix (i, j));
+      myCStructure->Transformation.ChangeValue (i, j) = float (theMatrix (i, j));
     }
   }
   myCStructure->UpdateTransformation();
index b86f007..a7cc892 100644 (file)
@@ -284,10 +284,6 @@ public:
   //! Returns the highlight indicator for the structure <me>.
   Standard_EXPORT virtual Standard_Boolean IsHighlighted() const;
   
-  //! Returns Standard_True if the structure <me> is rotated.
-  //! <=> The transformation != Identity, != Scale, != Translation.
-  Standard_EXPORT Standard_Boolean IsRotated() const;
-  
   //! Returns Standard_True if the structure <me> is transformed.
   //! <=> The transformation != Identity.
   Standard_EXPORT Standard_Boolean IsTransformed() const;
@@ -376,11 +372,7 @@ public:
   Standard_EXPORT void SetHLRValidation (const Standard_Boolean AFlag);
   
   Standard_EXPORT Standard_Boolean HLRValidation() const;
-  
-  //! Returns the type of composition applied to matrices
-  //! of transformation of <me>.
-  Standard_EXPORT Graphic3d_TypeOfComposition Composition() const;
-  
+
   //! Modifies the current local modelling transformation
   //! in the structure <me>.
   //!
index 650941a..67af2f8 100755 (executable)
@@ -40,7 +40,6 @@ OpenGl_GraduatedTrihedron.hxx
 OpenGl_GraduatedTrihedron.cxx
 OpenGl_MapOfZLayerSettings.hxx
 OpenGl_Matrix.hxx
-OpenGl_Matrix.cxx
 OpenGl_MatrixState.hxx
 OpenGl_NamedStatus.hxx
 OpenGl_TextParam.hxx
diff --git a/src/OpenGl/OpenGl_Matrix.cxx b/src/OpenGl/OpenGl_Matrix.cxx
deleted file mode 100644 (file)
index 877b722..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-// Created on: 2011-07-13
-// Created by: Sergey ZERCHANINOV
-// Copyright (c) 2011-2014 OPEN CASCADE SAS
-//
-// This file is part of Open CASCADE Technology software library.
-//
-// This library is free software; you can redistribute it and/or modify it under
-// the terms of the GNU Lesser General Public License version 2.1 as published
-// by the Free Software Foundation, with special exception defined in the file
-// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
-// distribution for complete text of the license and disclaimer of any warranty.
-//
-// Alternatively, this file may be used under the terms of Open CASCADE
-// commercial license or contractual agreement.
-
-#include <OpenGl_Matrix.hxx>
-
-/*----------------------------------------------------------------------*/
-
-void OpenGl_Transposemat3 (OpenGl_Matrix *c, const OpenGl_Matrix *a)
-{
-  Tint row, col;
-  for (row = 0; row < 4; row++)
-    for (col = 0; col < 4; col++)
-      c->mat[row][col] = a->mat[col][row];
-}
index e92da57..a81a366 100644 (file)
@@ -24,6 +24,4 @@ struct OpenGl_Matrix
   DEFINE_STANDARD_ALLOC
 };
 
-Standard_EXPORT void OpenGl_Transposemat3 (OpenGl_Matrix *c, const OpenGl_Matrix *a);
-
 #endif //OpenGl_Matrix_Header
index 69d2e4d..1ede9f5 100644 (file)
@@ -117,7 +117,6 @@ public:
 // =======================================================================
 OpenGl_Structure::OpenGl_Structure (const Handle(Graphic3d_StructureManager)& theManager)
 : Graphic3d_CStructure (theManager),
-  myTransformation     (NULL),
   myAspectLine         (NULL),
   myAspectFace         (NULL),
   myAspectMarker       (NULL),
@@ -139,7 +138,6 @@ OpenGl_Structure::OpenGl_Structure (const Handle(Graphic3d_StructureManager)& th
 OpenGl_Structure::~OpenGl_Structure()
 {
   Release (Handle(OpenGl_Context)());
-  delete myTransformation;  myTransformation  = NULL;
 }
 
 // =======================================================================
@@ -167,23 +165,15 @@ void OpenGl_Structure::UpdateAspects()
 // =======================================================================
 void OpenGl_Structure::UpdateTransformation()
 {
-  if (myTransformation == NULL)
-  {
-    myTransformation = new OpenGl_Matrix();
-  }
-
-  Standard_ShortReal (*aMat)[4] = Graphic3d_CStructure::Transformation;
-
+  const OpenGl_Mat4& aMat = Graphic3d_CStructure::Transformation;
   Standard_ShortReal aDet =
-    aMat[0][0] * (aMat[1][1] * aMat[2][2] - aMat[2][1] * aMat[1][2]) -
-    aMat[0][1] * (aMat[1][0] * aMat[2][2] - aMat[2][0] * aMat[1][2]) +
-    aMat[0][2] * (aMat[1][0] * aMat[2][1] - aMat[2][0] * aMat[1][1]);
+    aMat.GetValue(0, 0) * (aMat.GetValue(1, 1) * aMat.GetValue(2, 2) - aMat.GetValue(2, 1) * aMat.GetValue(1, 2)) -
+    aMat.GetValue(0, 1) * (aMat.GetValue(1, 0) * aMat.GetValue(2, 2) - aMat.GetValue(2, 0) * aMat.GetValue(1, 2)) +
+    aMat.GetValue(0, 2) * (aMat.GetValue(1, 0) * aMat.GetValue(2, 1) - aMat.GetValue(2, 0) * aMat.GetValue(1, 1));
 
   // Determinant of transform matrix less then 0 means that mirror transform applied.
   myIsMirrored = aDet < 0.0f;
 
-  matcpy (myTransformation->mat, &Graphic3d_CStructure::Transformation[0][0]);
-
   if (IsRaytracable())
   {
     ++myModificationState;
@@ -534,26 +524,18 @@ void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) con
     theWorkspace->NamedStatus |= OPENGL_NS_HIGHLIGHT;
   }
 
-  // Do we need to restore GL_NORMALIZE?
-  const Standard_Boolean anOldGlNormalize = aCtx->IsGlNormalizeEnabled();
-
   // Apply local transformation
-  if (myTransformation)
-  {
-    OpenGl_Matrix aModelWorld;
-    OpenGl_Transposemat3 (&aModelWorld, myTransformation);
-    aCtx->ModelWorldState.Push();
-    aCtx->ModelWorldState.SetCurrent (OpenGl_Mat4::Map ((Standard_ShortReal* )aModelWorld.mat));
-
-    Standard_ShortReal aScaleX = OpenGl_Vec3 (myTransformation->mat[0][0],
-                                              myTransformation->mat[0][1],
-                                              myTransformation->mat[0][2]).SquareModulus();
-    // Scale transform detected.
-    if (Abs (aScaleX - 1.f) > Precision::Confusion())
-    {
-      aCtx->SetGlNormalizeEnabled (Standard_True);
-    }
+  aCtx->ModelWorldState.Push();
+  aCtx->ModelWorldState.SetCurrent (Transformation);
+
+  // detect scale transform
+  const Standard_Boolean   anOldGlNormalize = aCtx->IsGlNormalizeEnabled();
+  const Standard_ShortReal aScaleX          = Transformation.GetRow (0).xyz().SquareModulus();
+  if (Abs (aScaleX - 1.f) > Precision::Confusion())
+  {
+    aCtx->SetGlNormalizeEnabled (Standard_True);
   }
+
   if (TransformPersistence.Flags)
   {
     OpenGl_Mat4 aProjection = aCtx->ProjectionState.Current();
@@ -678,11 +660,8 @@ void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) con
   }
 
   // Restore local transformation
-  if (myTransformation)
-  {
-    aCtx->ModelWorldState.Pop();
-    aCtx->SetGlNormalizeEnabled (anOldGlNormalize);
-  }
+  aCtx->ModelWorldState.Pop();
+  aCtx->SetGlNormalizeEnabled (anOldGlNormalize);
   if (TransformPersistence.Flags)
   {
     aCtx->ProjectionState.Pop();
index e4f47da..b4a1372 100644 (file)
@@ -180,9 +180,6 @@ public:
   //! Returns OpenGL face aspect.
   const OpenGl_AspectFace* AspectFace() const { return myAspectFace; }
 
-  //! Returns OpenGL transformation matrix.
-  const OpenGl_Matrix* Transformation() const { return myTransformation; }
-
   //! Returns structure modification state (for ray-tracing).
   Standard_Size ModificationState() const { return myModificationState; }
 
@@ -201,7 +198,6 @@ protected:
 
 protected:
 
-  OpenGl_Matrix*             myTransformation;
   OpenGl_AspectLine*         myAspectLine;
   OpenGl_AspectFace*         myAspectFace;
   OpenGl_AspectMarker*       myAspectMarker;
index 5e5ed8f..100dca0 100644 (file)
@@ -28,17 +28,9 @@ OpenGl_StructureShadow::OpenGl_StructureShadow (const Handle(Graphic3d_Structure
   Handle(OpenGl_StructureShadow) aShadow = Handle(OpenGl_StructureShadow)::DownCast (theStructure);
   myParent = aShadow.IsNull() ? theStructure : aShadow->myParent;
 
-
-  Composition   = myParent->Composition;
   ContainsFacet = myParent->ContainsFacet;
   IsInfinite    = myParent->IsInfinite;
-  for (Standard_Integer i = 0; i <= 3; ++i)
-  {
-    for (Standard_Integer j = 0; j <= 3; ++j)
-    {
-      Graphic3d_CStructure::Transformation[i][j] = myParent->Graphic3d_CStructure::Transformation[i][j];
-    }
-  }
+  Transformation = myParent->Transformation;
 
   UpdateTransformation();
   myInstancedStructure = const_cast<OpenGl_Structure*> (myParent->InstancedStructure());
index 774ccfb..6a058aa 100644 (file)
@@ -842,7 +842,7 @@ protected: //! @name methods related to ray-tracing
   //! Adds OpenGL groups to ray-traced scene geometry.
   Standard_Boolean addRaytraceGroups (const OpenGl_Structure*        theStructure,
                                       const OpenGl_RaytraceMaterial& theStructMat,
-                                      const Standard_ShortReal*      theTransform,
+                                      const Graphic3d_Mat4*          theTransform,
                                       const Handle(OpenGl_Context)&  theGlContext);
 
   //! Creates ray-tracing material properties.
index 8b5468b..45f9e79 100644 (file)
@@ -411,29 +411,14 @@ Standard_Boolean OpenGl_View::addRaytraceStructure (const OpenGl_Structure*
     aStructMaterial = convertMaterial (theStructure->AspectFace(), theGlContext);
   }
 
-  Standard_ShortReal aStructTransform[16];
-
-  if (theStructure->Transformation()->mat != NULL)
-  {
-    for (Standard_Integer i = 0; i < 4; ++i)
-    {
-      for (Standard_Integer j = 0; j < 4; ++j)
-      {
-        aStructTransform[j * 4 + i] = theStructure->Transformation()->mat[i][j];
-      }
-    }
-  }
-
-  Standard_Boolean aResult = addRaytraceGroups (theStructure, aStructMaterial,
-    theStructure->Transformation()->mat ? aStructTransform : NULL, theGlContext);
+  Standard_Boolean aResult = addRaytraceGroups (theStructure, aStructMaterial, &theStructure->Transformation, theGlContext);
 
   // Process all connected OpenGL structures
   const OpenGl_Structure* anInstanced = theStructure->InstancedStructure();
 
   if (anInstanced != NULL && anInstanced->IsRaytracable())
   {
-    aResult &= addRaytraceGroups (anInstanced, aStructMaterial,
-      theStructure->Transformation()->mat ? aStructTransform : NULL, theGlContext);
+    aResult &= addRaytraceGroups (anInstanced, aStructMaterial, &theStructure->Transformation, theGlContext);
   }
 
   myStructureStates[theStructure] = StructState (theStructure);
@@ -447,7 +432,7 @@ Standard_Boolean OpenGl_View::addRaytraceStructure (const OpenGl_Structure*
 // =======================================================================
 Standard_Boolean OpenGl_View::addRaytraceGroups (const OpenGl_Structure*        theStructure,
                                                  const OpenGl_RaytraceMaterial& theStructMat,
-                                                 const Standard_ShortReal*      theTransform,
+                                                 const Graphic3d_Mat4*          theTransform,
                                                  const Handle(OpenGl_Context)&  theGlContext)
 {
   for (OpenGl_Structure::GroupIterator aGroupIter (theStructure->DrawGroups()); aGroupIter.More(); aGroupIter.Next())
@@ -495,7 +480,7 @@ Standard_Boolean OpenGl_View::addRaytraceGroups (const OpenGl_Structure*
 
             if (theTransform != NULL)
             {
-              aTransform->SetTransform (*(reinterpret_cast<const BVH_Mat4f*> (theTransform)));
+              aTransform->SetTransform (*theTransform);
             }
 
             aSet->SetProperties (aTransform);
@@ -516,7 +501,7 @@ Standard_Boolean OpenGl_View::addRaytraceGroups (const OpenGl_Structure*
 
               if (theTransform != NULL)
               {
-                aTransform->SetTransform (*(reinterpret_cast<const BVH_Mat4f*> (theTransform)));
+                aTransform->SetTransform (*theTransform);
               }
 
               aSet->SetProperties (aTransform);