Salome HOME
Updated copyright comment
[modules/gui.git] / src / VTKViewer / VTKViewer_Utilities.cxx
old mode 100755 (executable)
new mode 100644 (file)
index 8166845..4c38e81
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -6,7 +6,7 @@
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -48,7 +48,7 @@ ResetCamera(vtkRenderer* theRenderer,
   int aCount = ComputeVisiblePropBounds(theRenderer,aBounds);
 
   if(theUsingZeroFocalPoint || aCount){
-    static double MIN_DISTANCE = 1.0 / VTK_LARGE_FLOAT;
+    static double MIN_DISTANCE = 1.0 / VTK_FLOAT_MAX;
 
     double aLength = aBounds[1]-aBounds[0];
     aLength = std::max((aBounds[3]-aBounds[2]),aLength);
@@ -109,36 +109,43 @@ int
 ComputeVisiblePropBounds(vtkRenderer* theRenderer, 
                          double theBounds[6])
 {
-  int aCount = 0;
-  
-  theBounds[0] = theBounds[2] = theBounds[4] = VTK_LARGE_FLOAT;
-  theBounds[1] = theBounds[3] = theBounds[5] = -VTK_LARGE_FLOAT;
-  
-  // loop through all props
   VTK::ActorCollectionCopy aCopy(theRenderer->GetActors());
   vtkActorCollection* aCollection = aCopy.GetActors();
-  aCollection->InitTraversal();
-  while (vtkActor* aProp = aCollection->GetNextActor()) {
-    // if it's invisible, or has no geometry, we can skip the rest 
+  return ComputeBounds( aCollection, theBounds );
+}
+
+/*! Compute the bounds of actors*/
+int
+ComputeBounds(vtkActorCollection* theCollection, double theBounds[6])
+{
+  int aCount = 0;
+
+  theBounds[0] = theBounds[2] = theBounds[4] = VTK_FLOAT_MAX;
+  theBounds[1] = theBounds[3] = theBounds[5] = -VTK_FLOAT_MAX;
+
+  // loop through all props
+  theCollection->InitTraversal();
+  while (vtkActor* aProp = theCollection->GetNextActor()) {
+    // if it's invisible, or has no geometry, we can skip the rest
     if(aProp->GetVisibility() && aProp->GetMapper() && vtkMath::AreBoundsInitialized(aProp->GetBounds())){
       if(VTKViewer_Actor* anActor = VTKViewer_Actor::SafeDownCast(aProp))
         if(anActor->IsInfinitive())
           continue;
       double *aBounds = aProp->GetBounds();
-      static double MIN_DISTANCE = 1./VTK_LARGE_FLOAT;
-      static double MAX_DISTANCE = 0.9*VTK_LARGE_FLOAT;
+      static double MIN_DISTANCE = 1./VTK_FLOAT_MAX;
+      static double MAX_DISTANCE = 0.9*VTK_FLOAT_MAX;
 
-      if(abs(aBounds[1] - aBounds[0]) < MIN_DISTANCE) {
+      if(fabs(aBounds[1] - aBounds[0]) < MIN_DISTANCE) {
         aBounds[0]-=0.001;
         aBounds[1]+=0.001;
       }
 
-      if(abs(aBounds[3] - aBounds[2]) < MIN_DISTANCE) {
+      if(fabs(aBounds[3] - aBounds[2]) < MIN_DISTANCE) {
         aBounds[2]-=0.001;
         aBounds[3]+=0.001;
       }
 
-      if(abs(aBounds[5] - aBounds[4]) < MIN_DISTANCE) {
+      if(fabs(aBounds[5] - aBounds[4]) < MIN_DISTANCE) {
         aBounds[4]-=0.001;
         aBounds[5]+=0.001;
       }
@@ -241,8 +248,8 @@ bool IsBBEmpty(vtkRenderer* theRenderer)
     return false;
 
   double aNewBndBox[6];
-  aNewBndBox[ 0 ] = aNewBndBox[ 2 ] = aNewBndBox[ 4 ] = VTK_LARGE_FLOAT;
-  aNewBndBox[ 1 ] = aNewBndBox[ 3 ] = aNewBndBox[ 5 ] = -VTK_LARGE_FLOAT;
+  aNewBndBox[ 0 ] = aNewBndBox[ 2 ] = aNewBndBox[ 4 ] = VTK_FLOAT_MAX;
+  aNewBndBox[ 1 ] = aNewBndBox[ 3 ] = aNewBndBox[ 5 ] = -VTK_FLOAT_MAX;
   
   // iterate through displayed objects and set size if necessary
   VTK::ActorCollectionCopy aCopy(theRenderer->GetActors());
@@ -255,15 +262,28 @@ bool IsBBEmpty(vtkRenderer* theRenderer)
       if(anActor->GetVisibility() && !anActor->IsInfinitive())
       {
         double *aBounds = anActor->GetBounds();
-        if(aBounds[0] > -VTK_LARGE_FLOAT && aBounds[1] < VTK_LARGE_FLOAT &&
-           aBounds[2] > -VTK_LARGE_FLOAT && aBounds[3] < VTK_LARGE_FLOAT &&
-           aBounds[4] > -VTK_LARGE_FLOAT && aBounds[5] < VTK_LARGE_FLOAT)
+        if(aBounds[0] > -VTK_FLOAT_MAX && aBounds[1] < VTK_FLOAT_MAX &&
+           aBounds[2] > -VTK_FLOAT_MAX && aBounds[3] < VTK_FLOAT_MAX &&
+           aBounds[4] > -VTK_FLOAT_MAX && aBounds[5] < VTK_FLOAT_MAX)
           isAny = true;
       }
   
   return !isAny;
 }
 
+/*!
+  Check that the given bounding box is valid, i.e each min bound < each max bound
+*/
+
+bool isBoundValid(double* theBounds) {
+  if(theBounds[0] > theBounds[1] ||
+     theBounds[2] > theBounds[3] ||
+     theBounds[4] > theBounds[5])
+    return false;
+  else 
+    return true;
+}
+
 bool ComputeBBCenter(vtkRenderer* theRenderer, double theCenter[3])
 {  
   theCenter[0] = theCenter[1] = theCenter[2] = 0.0;
@@ -272,8 +292,8 @@ bool ComputeBBCenter(vtkRenderer* theRenderer, double theCenter[3])
     return false;
 
   double aNewBndBox[6];
-  aNewBndBox[ 0 ] = aNewBndBox[ 2 ] = aNewBndBox[ 4 ] = VTK_LARGE_FLOAT;
-  aNewBndBox[ 1 ] = aNewBndBox[ 3 ] = aNewBndBox[ 5 ] = -VTK_LARGE_FLOAT;
+  aNewBndBox[ 0 ] = aNewBndBox[ 2 ] = aNewBndBox[ 4 ] = VTK_FLOAT_MAX;
+  aNewBndBox[ 1 ] = aNewBndBox[ 3 ] = aNewBndBox[ 5 ] = -VTK_FLOAT_MAX;
 
   // iterate through displayed objects and set size if necessary
   VTK::ActorCollectionCopy aCopy(theRenderer->GetActors());
@@ -288,9 +308,13 @@ bool ComputeBBCenter(vtkRenderer* theRenderer, double theCenter[3])
       if(anActor->GetVisibility() && !anActor->IsInfinitive())
       {
         double *aBounds = anActor->GetBounds();
-        if(aBounds[0] > -VTK_LARGE_FLOAT && aBounds[1] < VTK_LARGE_FLOAT &&
-           aBounds[2] > -VTK_LARGE_FLOAT && aBounds[3] < VTK_LARGE_FLOAT &&
-           aBounds[4] > -VTK_LARGE_FLOAT && aBounds[5] < VTK_LARGE_FLOAT)
+       
+       //Ignore invalid bounds
+       if(!isBoundValid(aBounds)) continue;
+
+        if(aBounds[0] > -VTK_FLOAT_MAX && aBounds[1] < VTK_FLOAT_MAX &&
+           aBounds[2] > -VTK_FLOAT_MAX && aBounds[3] < VTK_FLOAT_MAX &&
+           aBounds[4] > -VTK_FLOAT_MAX && aBounds[5] < VTK_FLOAT_MAX)
         {
           for(int i = 0; i < 5; i = i + 2){
             if(aBounds[i] < aNewBndBox[i]) 
@@ -310,11 +334,11 @@ bool ComputeBBCenter(vtkRenderer* theRenderer, double theCenter[3])
     return true;
   }
 
-  if(aNewBndBox[0] > -VTK_LARGE_FLOAT && aNewBndBox[1] < VTK_LARGE_FLOAT &&
-     aNewBndBox[2] > -VTK_LARGE_FLOAT && aNewBndBox[3] < VTK_LARGE_FLOAT &&
-     aNewBndBox[4] > -VTK_LARGE_FLOAT && aNewBndBox[5] < VTK_LARGE_FLOAT)
+  if(aNewBndBox[0] > -VTK_FLOAT_MAX && aNewBndBox[1] < VTK_FLOAT_MAX &&
+     aNewBndBox[2] > -VTK_FLOAT_MAX && aNewBndBox[3] < VTK_FLOAT_MAX &&
+     aNewBndBox[4] > -VTK_FLOAT_MAX && aNewBndBox[5] < VTK_FLOAT_MAX)
   {
-    static double MIN_DISTANCE = 1.0 / VTK_LARGE_FLOAT;
+    static double MIN_DISTANCE = 1.0 / VTK_FLOAT_MAX;
     
     double aLength = aNewBndBox[1]-aNewBndBox[0];
     aLength = std::max((aNewBndBox[3]-aNewBndBox[2]),aLength);
@@ -347,7 +371,7 @@ bool ComputeBBCenter(vtkRenderer* theRenderer, double theCenter[3])
   printf("aCount = %d\n",aCount);
 
   if(aCount){
-    static double MIN_DISTANCE = 1.0 / VTK_LARGE_FLOAT;
+    static double MIN_DISTANCE = 1.0 / VTK_FLOAT_MAX;
 
     double aLength = aBounds[1]-aBounds[0];
     aLength = max((aBounds[3]-aBounds[2]),aLength);