]> SALOME platform Git repositories - plugins/blsurfplugin.git/blobdiff - src/BLSURFPlugin/BLSURFPlugin_Attractor.cxx
Salome HOME
Clean-up deprecated OCCT-related code
[plugins/blsurfplugin.git] / src / BLSURFPlugin / BLSURFPlugin_Attractor.cxx
index e5366dc047a222a224b75b4364f11351034efdc2..544401010857d0871a736b8926512637ee5e1872 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2014  CEA/DEN, EDF R&D
+// Copyright (C) 2007-2016  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -44,13 +44,14 @@ BLSURFPlugin_Attractor::BLSURFPlugin_Attractor ()
   : _face(),
   _attractorShape(),
   _attEntry(),
-  _gridU(0),
-  _gridV(0),
   _vectU(),
   _vectV(),
   _DMap(),
   _known(),
   _trial(),
+  _type(-1),
+  _gridU(0),
+  _gridV(0),
   _u1 (0.),
   _u2 (0.),
   _v1 (0.),
@@ -59,7 +60,6 @@ BLSURFPlugin_Attractor::BLSURFPlugin_Attractor ()
   _endSize(-1),
   _actionRadius(-1),
   _constantRadius(-1),
-  _type(-1),
   _isMapBuilt(false),
   _isEmpty(true){ MESSAGE("construction of a void attractor"); }
 
@@ -67,13 +67,14 @@ BLSURFPlugin_Attractor::BLSURFPlugin_Attractor (const TopoDS_Face& Face, const T
   : _face(),
   _attractorShape(),
   _attEntry(attEntry),
-  _gridU(),
-  _gridV(),
   _vectU(),
   _vectV(),
   _DMap(),
   _known(),
   _trial(),
+  _type(0),
+  _gridU(),
+  _gridV(),
   _u1 (0.),
   _u2 (0.),
   _v1 (0.),
@@ -82,7 +83,6 @@ BLSURFPlugin_Attractor::BLSURFPlugin_Attractor (const TopoDS_Face& Face, const T
   _endSize(-1),
   _actionRadius(-1),
   _constantRadius(-1),
-  _type(0),
   _isMapBuilt(false),
   _isEmpty(false)
 {
@@ -171,23 +171,40 @@ bool BLSURFPlugin_Attractor::init(){
   return true;
 }
 
+// check that i and j are inside the bounds of the grid to avoid out of bounds errors
+// in affectation of the grid's vectors
+void BLSURFPlugin_Attractor::avoidOutOfBounds(int& i, int& j){
+  if (i > _gridU)
+    i = _gridU;
+  if (i < 0)
+    i = 0;
+  if (j > _gridV)
+    j = _gridV;
+  if (j < 0)
+    j = 0;
+}
+
 void BLSURFPlugin_Attractor::edgeInit(Handle(Geom_Surface) theSurf, const TopoDS_Edge& anEdge){
   gp_Pnt2d P2;
   double first;
   double last;
-  int i,j,i0,j0;
+  int i,i0,j0;
   Trial_Pnt TPnt(3,0);
   Handle(Geom2d_Curve) aCurve2d; 
   Handle(Geom_Curve) aCurve3d = BRep_Tool::Curve (anEdge, first, last);
   ShapeConstruct_ProjectCurveOnSurface curveProjector;
   curveProjector.Init(theSurf, Precision::Confusion());
-  curveProjector.PerformAdvanced (aCurve3d, first, last, aCurve2d);
-  
+  curveProjector.Perform (aCurve3d, first, last, aCurve2d);
+
   int N = 1200;
   for (i=0; i<=N; i++){
     P2 = aCurve2d->Value(first + i * (last-first) / N);
     i0 = floor( (P2.X() - _u1) * _gridU / (_u2 - _u1) + 0.5 );
     j0 = floor( (P2.Y() - _v1) * _gridV / (_v2 - _v1) + 0.5 );
+
+    // Avoid out of bounds errors when the ends of the edge are outside the face
+    avoidOutOfBounds(i0, j0);
+
     TPnt[0] = 0.;
     TPnt[1] = i0;
     TPnt[2] = j0;
@@ -211,10 +228,13 @@ double BLSURFPlugin_Attractor::_distanceFromPoint(double u, double v)
 
 double BLSURFPlugin_Attractor::_distanceFromMap(double u, double v){
   
-  //   BLSURF seems to perform a linear interpolation so it's sufficient to give it a non-continuous distance map
+  //   MG-CADSurf seems to perform a linear interpolation so it's sufficient to give it a non-continuous distance map
   int i = floor ( (u - _u1) * _gridU / (_u2 - _u1) + 0.5 );
   int j = floor ( (v - _v1) * _gridV / (_v2 - _v1) + 0.5 );
   
+  // Avoid out of bounds errors in _DMap
+  avoidOutOfBounds(i, j);
+
   return _DMap[i][j];
 }
 
@@ -238,9 +258,10 @@ double BLSURFPlugin_Attractor::GetSize(double u, double v)
       }
       break;
     case TYPE_LIN:
-        return _startSize + ( 0.5 * (attrDist - _constantRadius + abs(attrDist - _constantRadius)) ) ;
+        return _startSize + ( 0.5 * (attrDist - _constantRadius + fabs(attrDist - _constantRadius)) ) ;
       break;
   }
+  return -1;
 }
 
 
@@ -248,7 +269,7 @@ void BLSURFPlugin_Attractor::BuildMap() {
   
   MESSAGE("building the map");
   int i, j, k, n;  
-  int count = 0;
+  //int count = 0;
   int ip, jp, kp, np;
   int i0, j0;
   gp_Pnt P;
@@ -263,15 +284,17 @@ void BLSURFPlugin_Attractor::BuildMap() {
   TTrialSet::iterator min;
   TTrialSet::iterator found;
   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(_face);
-  
+
   // While there are points in "Trial" (representing a kind of advancing front), loop on them -----------------------------------------------------------
   while (_trial.size() > 0 ) {
     min = _trial.begin();                        // Get trial point with min distance from start
     i0 = (*min)[1];
     j0 = (*min)[2];
+    // Avoid out of bounds errors in _known affectations
+    avoidOutOfBounds(i0, j0);
     _known[i0][j0] = true;                       // Move it to "Known"
     _trial.erase(min);                           // Remove it from "Trial"
-    
+
     // Loop on neighbours of the trial min --------------------------------------------------------------------------------------------------------------
     for (i=i0 - 1 ; i <= i0 + 1 ; i++){ 
       if (!aSurf->IsUPeriodic()){                          // Periodic conditions in U  
@@ -282,7 +305,7 @@ void BLSURFPlugin_Attractor::BuildMap() {
       }
       ip = (i + _gridU + 1) % (_gridU+1);                  // We get a periodic index :
       for (j=j0 - 1 ; j <= j0 + 1 ; j++){                  //    ip=modulo(i,N+2) so that  i=-1->ip=N; i=0 -> ip=0 ; ... ; i=N+1 -> ip=0;  
-        if (!aSurf->IsVPeriodic()){                        // Periodic conditions in V . 
+          if (!aSurf->IsVPeriodic()){                        // Periodic conditions in V .
           if (j > _gridV ){
             break; }
           else if (j < 0){