Salome HOME
Correct some memory leaks
[modules/smesh.git] / src / SMESH / SMESH_MEFISTO_2D.cxx
index f9d4fde0a6f11aa2e06ebb08c9d891314ba6980a..ffd212d06913aea4b8a9ec3f10b66b5933e79ca3 100644 (file)
@@ -1,14 +1,32 @@
-using namespace std;
-//=============================================================================
-// File      : SMESH_MEFISTO_2D.cxx
-// Created   : sam mai 18 08:10:55 CEST 2002
-// Author    : Paul RASCLE, EDF
-// Project   : SALOME
-// Copyright : EDF 2002
-// $Header$
-//=============================================================================
-using namespace std;
+//  SMESH SMESH : implementaion of SMESH idl descriptions
+//
+//  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
+// 
+//  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. 
+// 
+//  This library is distributed in the hope that it will be useful, 
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
+//  Lesser General Public License for more details. 
+// 
+//  You should have received a copy of the GNU Lesser General Public 
+//  License along with this library; if not, write to the Free Software 
+//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
+// 
+//  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
+//
+//
+//
+//  File   : SMESH_MEFISTO_2D.cxx
+//  Author : Paul RASCLE, EDF
+//  Module : SMESH
+//  $Header$
 
+using namespace std;
 #include "SMESH_MEFISTO_2D.hxx"
 #include "SMESH_Gen.hxx"
 #include "SMESH_Mesh.hxx"
@@ -19,8 +37,6 @@ using namespace std;
 #include "Rn.h"
 #include "aptrte.h"
 
-#include "SMESHDS_ListOfPtrHypothesis.hxx"
-#include "SMESHDS_ListIteratorOfListOfPtrHypothesis.hxx"
 #include "SMDS_MeshElement.hxx"
 #include "SMDS_MeshNode.hxx"
 #include "SMDS_EdgePosition.hxx"
@@ -51,20 +67,20 @@ using namespace std;
  */
 //=============================================================================
 
-SMESH_MEFISTO_2D::SMESH_MEFISTO_2D(int hypId, int studyId, SMESH_Gen* gen)
-  SMESH_2D_Algo(hypId, studyId, gen)
+SMESH_MEFISTO_2D::SMESH_MEFISTO_2D(int hypId, int studyId,
+       SMESH_Gen * gen):SMESH_2D_Algo(hypId, studyId, gen)
 {
-  MESSAGE("SMESH_MEFISTO_2D::SMESH_MEFISTO_2D");
-  _name = "MEFISTO_2D";
+       MESSAGE("SMESH_MEFISTO_2D::SMESH_MEFISTO_2D");
+       _name = "MEFISTO_2D";
 //   _shapeType = TopAbs_FACE;
-  _shapeType = (1<<TopAbs_FACE);
-  _compatibleHypothesis.push_back("MaxElementArea");
-  _compatibleHypothesis.push_back("LengthFromEdges");
-
-  _edgeLength = 0;
-  _maxElementArea = 0;
-  _hypMaxElementArea = NULL;
-  _hypLengthFromEdges = NULL;
+       _shapeType = (1 << TopAbs_FACE);
+       _compatibleHypothesis.push_back("MaxElementArea");
+       _compatibleHypothesis.push_back("LengthFromEdges");
+
+       _edgeLength = 0;
+       _maxElementArea = 0;
+       _hypMaxElementArea = NULL;
+       _hypLengthFromEdges = NULL;
 }
 
 //=============================================================================
@@ -75,7 +91,7 @@ SMESH_MEFISTO_2D::SMESH_MEFISTO_2D(int hypId, int studyId, SMESH_Gen* gen)
 
 SMESH_MEFISTO_2D::~SMESH_MEFISTO_2D()
 {
-  MESSAGE("SMESH_MEFISTO_2D::~SMESH_MEFISTO_2D");
+       MESSAGE("SMESH_MEFISTO_2D::~SMESH_MEFISTO_2D");
 }
 
 //=============================================================================
@@ -84,65 +100,64 @@ SMESH_MEFISTO_2D::~SMESH_MEFISTO_2D()
  */
 //=============================================================================
 
-bool SMESH_MEFISTO_2D::CheckHypothesis(SMESH_Mesh& aMesh,
-                              const TopoDS_Shape& aShape)
+bool SMESH_MEFISTO_2D::CheckHypothesis(SMESH_Mesh & aMesh,
+       const TopoDS_Shape & aShape)
 {
-  //MESSAGE("SMESH_MEFISTO_2D::CheckHypothesis");
-
-  _hypMaxElementArea = NULL;
-  _hypLengthFromEdges = NULL;
-
-  list<SMESHDS_Hypothesis*>::const_iterator itl;
-  SMESHDS_Hypothesis* theHyp;
-
-  const list<SMESHDS_Hypothesis*>& hyps = GetUsedHypothesis(aMesh, aShape);
-  int nbHyp = hyps.size();
-  if (nbHyp != 1) return false;  // only one compatible hypothesis allowed
-
-  itl = hyps.begin();
-  theHyp = (*itl);
-
-  string hypName = theHyp->GetName();
-  int hypId = theHyp->GetID();
-  //SCRUTE(hypName);
-
-  bool isOk = false;
-
-  if (hypName == "MaxElementArea")
-    {
-      _hypMaxElementArea = dynamic_cast<SMESH_MaxElementArea*> (theHyp);
-      ASSERT(_hypMaxElementArea);
-      _maxElementArea = _hypMaxElementArea->GetMaxArea();
-      _edgeLength = 0;
-      isOk =true;
-    }
-
-  if (hypName == "LengthFromEdges")
-    {
-      _hypLengthFromEdges = dynamic_cast<SMESH_LengthFromEdges*> (theHyp);
-      ASSERT(_hypLengthFromEdges);
-      _edgeLength = 0;
-      _maxElementArea = 0;
-      isOk =true;
-    }
-
-
-  if (isOk)
-    {
-      isOk = false;
-      if (_maxElementArea > 0)
+       //MESSAGE("SMESH_MEFISTO_2D::CheckHypothesis");
+
+       _hypMaxElementArea = NULL;
+       _hypLengthFromEdges = NULL;
+
+       list <const SMESHDS_Hypothesis * >::const_iterator itl;
+       const SMESHDS_Hypothesis *theHyp;
+
+       const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
+       int nbHyp = hyps.size();
+       if (nbHyp != 1) return false;// only one compatible hypothesis allowed
+
+       itl = hyps.begin();
+       theHyp = (*itl);
+
+       string hypName = theHyp->GetName();
+       int hypId = theHyp->GetID();
+       //SCRUTE(hypName);
+
+       bool isOk = false;
+
+       if (hypName == "MaxElementArea")
        {
-         _edgeLength = 2*sqrt(_maxElementArea);  // triangles : minorant
-         isOk = true;
+               _hypMaxElementArea = static_cast<const SMESH_MaxElementArea *>(theHyp);
+               ASSERT(_hypMaxElementArea);
+               _maxElementArea = _hypMaxElementArea->GetMaxArea();
+               _edgeLength = 0;
+               isOk = true;
        }
-      else isOk = (_hypLengthFromEdges != NULL); // **** check mode
-    }
 
-  //SCRUTE(_edgeLength);
-  //SCRUTE(_maxElementArea);
-  return isOk;
-}
+       if (hypName == "LengthFromEdges")
+       {
+               _hypLengthFromEdges = static_cast<const SMESH_LengthFromEdges *>(theHyp);
+               ASSERT(_hypLengthFromEdges);
+               _edgeLength = 0;
+               _maxElementArea = 0;
+               isOk = true;
+       }
 
+       if (isOk)
+       {
+               isOk = false;
+               if (_maxElementArea > 0)
+               {
+                       _edgeLength = 2 * sqrt(_maxElementArea);        // triangles : minorant
+                       isOk = true;
+               }
+               else
+                       isOk = (_hypLengthFromEdges != NULL);   // **** check mode
+       }
+
+       //SCRUTE(_edgeLength);
+       //SCRUTE(_maxElementArea);
+       return isOk;
+}
 
 //=============================================================================
 /*!
@@ -150,81 +165,80 @@ bool SMESH_MEFISTO_2D::CheckHypothesis(SMESH_Mesh& aMesh,
  */
 //=============================================================================
 
-bool SMESH_MEFISTO_2D::Compute(SMESH_Mesh& aMesh,
-                      const TopoDS_Shape& aShape)
+bool SMESH_MEFISTO_2D::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
 {
-  MESSAGE("SMESH_MEFISTO_2D::Compute");
-
-  if (_hypLengthFromEdges)
-    _edgeLength = ComputeEdgeElementLength(aMesh, aShape);
-
-  bool isOk = false;
-  const Handle(SMESHDS_Mesh)& meshDS = aMesh.GetMeshDS();
-  SMESH_subMesh* theSubMesh = aMesh.GetSubMesh(aShape);
-
-  const TopoDS_Face& FF = TopoDS::Face(aShape);
-  bool faceIsForward = (FF.Orientation() == TopAbs_FORWARD);
-  TopoDS_Face F = TopoDS::Face(FF.Oriented(TopAbs_FORWARD));
-
-  Z   nblf;        //nombre de lignes fermees (enveloppe en tete)
-  Z  *nudslf=NULL; //numero du dernier sommet de chaque ligne fermee
-  R2 *uvslf=NULL;
-  Z   nbpti=0;     //nombre points internes futurs sommets de la triangulation
-  R2 *uvpti=NULL;
-  
-  Z   nbst;
-  R2 *uvst=NULL;
-  Z   nbt;
-  Z  *nust=NULL;
-  Z  ierr=0;
-  
-  Z  nutysu=1;                 // 1: il existe un fonction areteideale_()
-  // Z  nutysu=0;              // 0: on utilise aretmx
-  R  aretmx=_edgeLength;       // longueur max aretes future triangulation
-  //SCRUTE(aretmx);
-
-  nblf =  NumberOfWires(F);
-  //SCRUTE(nblf);
-
-  nudslf = new Z[1+nblf];
-  nudslf[0] = 0;
-  int iw = 1;
-  int nbpnt = 0;
-
-  const TopoDS_Wire OW1 = BRepTools::OuterWire(F);
-  nbpnt += NumberOfPoints (aMesh, OW1);
-  nudslf [iw++] = nbpnt;
-  //SCRUTE(nbpnt);
-
-  for (TopExp_Explorer exp (F, TopAbs_WIRE); exp.More(); exp.Next())
-    {
-      const TopoDS_Wire& W = TopoDS::Wire(exp.Current());
-      if (!OW1.IsSame(W))
+       MESSAGE("SMESH_MEFISTO_2D::Compute");
+
+       if (_hypLengthFromEdges)
+               _edgeLength = ComputeEdgeElementLength(aMesh, aShape);
+
+       bool isOk = false;
+       const SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
+       SMESH_subMesh *theSubMesh = aMesh.GetSubMesh(aShape);
+
+       const TopoDS_Face & FF = TopoDS::Face(aShape);
+       bool faceIsForward = (FF.Orientation() == TopAbs_FORWARD);
+       TopoDS_Face F = TopoDS::Face(FF.Oriented(TopAbs_FORWARD));
+
+       Z nblf;                                         //nombre de lignes fermees (enveloppe en tete)
+       Z *nudslf = NULL;                       //numero du dernier sommet de chaque ligne fermee
+       R2 *uvslf = NULL;
+       Z nbpti = 0;                            //nombre points internes futurs sommets de la triangulation
+       R2 *uvpti = NULL;
+
+       Z nbst;
+       R2 *uvst = NULL;
+       Z nbt;
+       Z *nust = NULL;
+       Z ierr = 0;
+
+       Z nutysu = 1;                           // 1: il existe un fonction areteideale_()
+       // Z  nutysu=0;              // 0: on utilise aretmx
+       R aretmx = _edgeLength;         // longueur max aretes future triangulation
+       //SCRUTE(aretmx);
+
+       nblf = NumberOfWires(F);
+       //SCRUTE(nblf);
+
+       nudslf = new Z[1 + nblf];
+       nudslf[0] = 0;
+       int iw = 1;
+       int nbpnt = 0;
+
+       const TopoDS_Wire OW1 = BRepTools::OuterWire(F);
+       nbpnt += NumberOfPoints(aMesh, OW1);
+       nudslf[iw++] = nbpnt;
+       //SCRUTE(nbpnt);
+
+       for (TopExp_Explorer exp(F, TopAbs_WIRE); exp.More(); exp.Next())
        {
-         nbpnt += NumberOfPoints (aMesh, W);
-         nudslf [iw++] = nbpnt;
-         //SCRUTE(nbpnt);
+               const TopoDS_Wire & W = TopoDS::Wire(exp.Current());
+               if (!OW1.IsSame(W))
+               {
+                       nbpnt += NumberOfPoints(aMesh, W);
+                       nudslf[iw++] = nbpnt;
+                       //SCRUTE(nbpnt);
+               }
        }
-    }
-
-  uvslf = new R2[nudslf[nblf]];
-  //SCRUTE(nudslf[nblf]);
-  int m = 0;
-
-  map<int,int> mefistoToDS;  // correspondence mefisto index--> points IDNodes
-  TopoDS_Wire OW = BRepTools::OuterWire(F);
-  LoadPoints (aMesh, F, OW, uvslf, m, mefistoToDS);
-  //SCRUTE(m);
-  for (TopExp_Explorer exp (F, TopAbs_WIRE); exp.More(); exp.Next())
-    {
-      const TopoDS_Wire& W = TopoDS::Wire(exp.Current());
-      if (!OW.IsSame(W))
+
+       uvslf = new R2[nudslf[nblf]];
+       //SCRUTE(nudslf[nblf]);
+       int m = 0;
+
+       map<int, const SMDS_MeshNode*> mefistoToDS;     // correspondence mefisto index--> points IDNodes
+       TopoDS_Wire OW = BRepTools::OuterWire(F);
+       LoadPoints(aMesh, F, OW, uvslf, m, mefistoToDS);
+       //SCRUTE(m);
+
+       for (TopExp_Explorer exp(F, TopAbs_WIRE); exp.More(); exp.Next())
        {
-         LoadPoints (aMesh, F, W, uvslf, m, mefistoToDS);
-         //SCRUTE(m);
+               const TopoDS_Wire & W = TopoDS::Wire(exp.Current());
+               if (!OW.IsSame(W))
+               {
+                       LoadPoints(aMesh, F, W, uvslf, m, mefistoToDS);
+                       //SCRUTE(m);
+               }
        }
-    }
 //   SCRUTE(nudslf[nblf]);
 //   for (int i=0; i<=nblf; i++)
 //     {
@@ -238,34 +252,35 @@ bool SMESH_MEFISTO_2D::Compute(SMESH_Mesh& aMesh,
 //   SCRUTE(aretmx);
 //   SCRUTE(nblf);
 
-  MESSAGE("MEFISTO triangulation ..."); 
-  uvst = NULL;
-  nust = NULL;
-  aptrte( nutysu, aretmx,
-         nblf, nudslf, uvslf,
-         nbpti, uvpti,
-         nbst, uvst, nbt, nust,
-         ierr );
-
-  if( ierr == 0 )
-    {
-      MESSAGE("... End Triangulation");
-      //SCRUTE(nbst);
-      //SCRUTE(nbt);
-      StoreResult (aMesh, nbst, uvst, nbt, nust, F,
-                  faceIsForward, mefistoToDS);
-      isOk = true;
-    }
-  else
-    {
-      MESSAGE("Error in Triangulation");
-      isOk = false;
-    }
-  if (nudslf != NULL) delete [] nudslf;
-  if (uvslf  != NULL) delete [] uvslf;
-  if (uvst   != NULL) delete [] uvst;
-  if (nust   != NULL) delete [] nust;
-  return isOk;
+       MESSAGE("MEFISTO triangulation ...");
+       uvst = NULL;
+       nust = NULL;
+       aptrte(nutysu, aretmx,
+               nblf, nudslf, uvslf, nbpti, uvpti, nbst, uvst, nbt, nust, ierr);
+
+       if (ierr == 0)
+       {
+               MESSAGE("... End Triangulation");
+               //SCRUTE(nbst);
+               //SCRUTE(nbt);
+               StoreResult(aMesh, nbst, uvst, nbt, nust, F,
+                       faceIsForward, mefistoToDS);
+               isOk = true;
+       }
+       else
+       {
+               MESSAGE("Error in Triangulation");
+               isOk = false;
+       }
+       if (nudslf != NULL)
+               delete[]nudslf;
+       if (uvslf != NULL)
+               delete[]uvslf;
+       if (uvst != NULL)
+               delete[]uvst;
+       if (nust != NULL)
+               delete[]nust;
+       return isOk;
 }
 
 //=============================================================================
@@ -274,125 +289,115 @@ bool SMESH_MEFISTO_2D::Compute(SMESH_Mesh& aMesh,
  */
 //=============================================================================
 
-void SMESH_MEFISTO_2D::LoadPoints(SMESH_Mesh& aMesh,
-                                 const TopoDS_Face& FF, 
-                                 const TopoDS_Wire& WW,
-                                 R2* uvslf, 
-                                 int& m,
-                                 map<int,int>& mefistoToDS)
+void SMESH_MEFISTO_2D::LoadPoints(SMESH_Mesh & aMesh,
+       const TopoDS_Face & FF,
+       const TopoDS_Wire & WW, R2 * uvslf, int &m,
+       map<int, const SMDS_MeshNode*>&mefistoToDS)
 {
-  MESSAGE("SMESH_MEFISTO_2D::LoadPoints");
-
-  Handle (SMDS_Mesh) meshDS = aMesh.GetMeshDS();
-
-  double scalex;
-  double scaley;
-  TopoDS_Face F = TopoDS::Face(FF.Oriented(TopAbs_FORWARD));
-  ComputeScaleOnFace(aMesh, F, scalex, scaley);
-
-  TopoDS_Wire W = TopoDS::Wire(WW.Oriented(TopAbs_FORWARD));
-  BRepTools_WireExplorer wexp(W,F);    
-  for (wexp.Init(W,F);wexp.More(); wexp.Next())
-    {
-      const TopoDS_Edge& E = wexp.Current();
-
-      // --- IDNodes of first and last Vertex
-
-      TopoDS_Vertex VFirst, VLast;
-      TopExp::Vertices(E, VFirst, VLast); // corresponds to f and l
-
-      ASSERT(!VFirst.IsNull());
-      SMESH_subMesh* firstSubMesh = aMesh.GetSubMesh(VFirst);
-      const TColStd_ListOfInteger& lidf
-       = firstSubMesh->GetSubMeshDS()->GetIDNodes();
-      int idFirst= lidf.First();
-//       SCRUTE(idFirst);
-
-      ASSERT(!VLast.IsNull());
-      SMESH_subMesh* lastSubMesh = aMesh.GetSubMesh(VLast);
-      const TColStd_ListOfInteger& lidl
-       = lastSubMesh->GetSubMeshDS()->GetIDNodes();
-      int idLast= lidl.First();
-//       SCRUTE(idLast);
-
-      // --- edge internal IDNodes (relies on good order storage, not checked)
-
-      int nbPoints = aMesh.GetSubMesh(E)->GetSubMeshDS()->NbNodes();
-      //SCRUTE(nbPoints);
-      
-      Standard_Real f,l;
-      Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E,F,f,l);
-    
-      const TColStd_ListOfInteger& indElt
-       = aMesh.GetSubMesh(E)->GetSubMeshDS()->GetIDNodes();
-      TColStd_ListIteratorOfListOfInteger ite(indElt);
-      //SCRUTE(nbPoints);
-      //SCRUTE(indElt.Extent());
-      ASSERT(nbPoints == indElt.Extent());
-      bool isForward = (E.Orientation() == TopAbs_FORWARD);
-      map<double,int> params;
-      for (; ite.More(); ite.Next())
-       {
-         int nodeId = ite.Value();
-         Handle (SMDS_MeshElement) elt = meshDS->FindNode(nodeId);
-         Handle (SMDS_MeshNode) node = meshDS->GetNode(1, elt);
-         Handle (SMDS_EdgePosition) epos
-           = Handle (SMDS_EdgePosition)::DownCast(node->GetPosition());
-         double param = epos->GetUParameter();
-         params[param] = nodeId;
-//       MESSAGE(" " << param << " " << params[param]);
-       }
+       MESSAGE("SMESH_MEFISTO_2D::LoadPoints");
 
-      // --- load 2D values into MEFISTO structure,
-      //     add IDNodes in mefistoToDS map
+       SMDS_Mesh * meshDS = aMesh.GetMeshDS();
 
-      if (E.Orientation() == TopAbs_FORWARD)
-       {
-         gp_Pnt2d p = C2d->Value(f); // first point = Vertex Forward
-         uvslf [m].x = scalex * p.X();
-         uvslf [m].y = scaley * p.Y();
-         mefistoToDS[m+1] = idFirst;
-         //MESSAGE(" "<<m<<" "<<mefistoToDS[m+1]);
-         //MESSAGE("__ f "<<f<<" "<<uvslf[m].x <<" "<<uvslf[m].y);
-         m++;
-         map<double,int>::iterator itp = params.begin();
-         for (Standard_Integer i = 1; i<=nbPoints; i++) // nbPoints internal
-           {
-             double param = (*itp).first;
-             gp_Pnt2d p = C2d->Value(param);
-             uvslf [m].x = scalex * p.X();
-             uvslf [m].y = scaley * p.Y();
-             mefistoToDS[m+1] = (*itp).second;
-//           MESSAGE(" "<<m<<" "<<mefistoToDS[m+1]);
-//           MESSAGE("__ "<<i<<" "<<param<<" "<<uvslf[m].x <<" "<<uvslf[m].y);
-             m++;
-             itp++;
-           }
-       }
-      else 
+       double scalex;
+       double scaley;
+       TopoDS_Face F = TopoDS::Face(FF.Oriented(TopAbs_FORWARD));
+       ComputeScaleOnFace(aMesh, F, scalex, scaley);
+
+       TopoDS_Wire W = TopoDS::Wire(WW.Oriented(TopAbs_FORWARD));
+       BRepTools_WireExplorer wexp(W, F);
+       for (wexp.Init(W, F); wexp.More(); wexp.Next())
        {
-         gp_Pnt2d p = C2d->Value(l); // last point = Vertex Reversed
-         uvslf [m].x = scalex * p.X();
-         uvslf [m].y = scaley * p.Y();
-         mefistoToDS[m+1] = idLast;
-//       MESSAGE(" "<<m<<" "<<mefistoToDS[m+1]);
-//       MESSAGE("__ l "<<l<<" "<<uvslf[m].x <<" "<<uvslf[m].y);
-         m++;
-         map<double,int>::reverse_iterator itp = params.rbegin();
-         for (Standard_Integer i = nbPoints ; i >= 1; i--)
-           {
-             double param = (*itp).first;
-             gp_Pnt2d p = C2d->Value(param);
-             uvslf [m].x = scalex * p.X();
-             uvslf [m].y = scaley * p.Y();
-             mefistoToDS[m+1] = (*itp).second;
-//           MESSAGE(" "<<m<<" "<<mefistoToDS[m+1]);
-//           MESSAGE("__ "<<i<<" "<<param<<" "<<uvslf[m].x <<" "<<uvslf[m].y);
-             m++;
-             itp++;
-           }
+               const TopoDS_Edge & E = wexp.Current();
+
+               // --- IDNodes of first and last Vertex
+
+               TopoDS_Vertex VFirst, VLast;
+               TopExp::Vertices(E, VFirst, VLast);     // corresponds to f and l
+
+           ASSERT(!VFirst.IsNull());
+               SMDS_Iterator<const SMDS_MeshNode *> * lid=
+               aMesh.GetSubMesh(VFirst)->GetSubMeshDS()->GetNodes();
+               const SMDS_MeshNode* idFirst = lid->next();
+               delete lid;
+
+               ASSERT(!VLast.IsNull());
+               lid=aMesh.GetSubMesh(VLast)->GetSubMeshDS()->GetNodes();
+               const SMDS_MeshNode* idLast = lid->next();
+               delete lid;
+
+               // --- edge internal IDNodes (relies on good order storage, not checked)
+
+               int nbPoints = aMesh.GetSubMesh(E)->GetSubMeshDS()->NbNodes();
+               //SCRUTE(nbPoints);
+
+               double f, l;
+               Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, F, f, l);
+
+               SMDS_Iterator<const SMDS_MeshNode *> * ite=
+                       aMesh.GetSubMesh(E)->GetSubMeshDS()->GetNodes();
+
+               bool isForward = (E.Orientation() == TopAbs_FORWARD);
+               map<double, const SMDS_MeshNode*> params;
+
+               while(ite->more())
+               {
+                       const SMDS_MeshNode * node = ite->next();
+                       const SMDS_EdgePosition* epos
+                               = static_cast<const SMDS_EdgePosition*>(node->GetPosition());
+                       double param = epos->GetUParameter();
+                       params[param] = node;
+               }
+               delete ite;
+               // --- load 2D values into MEFISTO structure,
+               //     add IDNodes in mefistoToDS map
+
+               if (E.Orientation() == TopAbs_FORWARD)
+               {
+                       gp_Pnt2d p = C2d->Value(f);     // first point = Vertex Forward
+                       uvslf[m].x = scalex * p.X();
+                       uvslf[m].y = scaley * p.Y();
+                       mefistoToDS[m + 1] = idFirst;
+                       //MESSAGE(" "<<m<<" "<<mefistoToDS[m+1]);
+                       //MESSAGE("__ f "<<f<<" "<<uvslf[m].x <<" "<<uvslf[m].y);
+                       m++;
+                       map<double, const SMDS_MeshNode*>::iterator itp = params.begin();
+                       for (int i = 1; i <= nbPoints; i++)     // nbPoints internal
+                       {
+                               double param = (*itp).first;
+                               gp_Pnt2d p = C2d->Value(param);
+                               uvslf[m].x = scalex * p.X();
+                               uvslf[m].y = scaley * p.Y();
+                               mefistoToDS[m + 1] = (*itp).second;
+//        MESSAGE(" "<<m<<" "<<mefistoToDS[m+1]);
+//        MESSAGE("__ "<<i<<" "<<param<<" "<<uvslf[m].x <<" "<<uvslf[m].y);
+                               m++;
+                               itp++;
+                       }
+               }
+               else
+               {
+                       gp_Pnt2d p = C2d->Value(l);     // last point = Vertex Reversed
+                       uvslf[m].x = scalex * p.X();
+                       uvslf[m].y = scaley * p.Y();
+                       mefistoToDS[m + 1] = idLast;
+//    MESSAGE(" "<<m<<" "<<mefistoToDS[m+1]);
+//    MESSAGE("__ l "<<l<<" "<<uvslf[m].x <<" "<<uvslf[m].y);
+                       m++;
+                       map<double, const SMDS_MeshNode*>::reverse_iterator itp = params.rbegin();
+                       for (int i = nbPoints; i >= 1; i--)
+                       {
+                               double param = (*itp).first;
+                               gp_Pnt2d p = C2d->Value(param);
+                               uvslf[m].x = scalex * p.X();
+                               uvslf[m].y = scaley * p.Y();
+                               mefistoToDS[m + 1] = (*itp).second;
+//        MESSAGE(" "<<m<<" "<<mefistoToDS[m+1]);
+//            MESSAGE("__ "<<i<<" "<<param<<" "<<uvslf[m].x <<" "<<uvslf[m].y);
+                               m++;
+                               itp++;
+                       }
+               }
        }
-    }  
 }
 
 //=============================================================================
@@ -403,72 +408,74 @@ void SMESH_MEFISTO_2D::LoadPoints(SMESH_Mesh& aMesh,
 
 // **** a mettre dans SMESH_Algo ou SMESH_2D_Algo
 
-void SMESH_MEFISTO_2D::ComputeScaleOnFace(SMESH_Mesh& aMesh,
-                                         const TopoDS_Face& aFace,
-                                         double& scalex,
-                                         double& scaley)
+void SMESH_MEFISTO_2D::ComputeScaleOnFace(SMESH_Mesh & aMesh,
+       const TopoDS_Face & aFace, double &scalex, double &scaley)
 {
-  //MESSAGE("SMESH_MEFISTO_2D::ComputeScaleOnFace");
-  TopoDS_Face F = TopoDS::Face(aFace.Oriented(TopAbs_FORWARD));  
-  TopoDS_Wire W = BRepTools::OuterWire(F);
-
-  BRepTools_WireExplorer wexp(W,F);    
-
-  double xmin =  1.e300; // min & max of face 2D parametric coord.
-  double xmax = -1.e300;
-  double ymin =  1.e300;
-  double ymax = -1.e300;
-  int nbp = 50;
-  scalex = 1;
-  scaley = 1;
-  for (wexp.Init(W,F);wexp.More(); wexp.Next())
-    {
-      const TopoDS_Edge& E = wexp.Current();
-      double f,l;
-      Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E,F,f,l);
-      for (int i = 0; i<= nbp; i++)
+       //MESSAGE("SMESH_MEFISTO_2D::ComputeScaleOnFace");
+       TopoDS_Face F = TopoDS::Face(aFace.Oriented(TopAbs_FORWARD));
+       TopoDS_Wire W = BRepTools::OuterWire(F);
+
+       BRepTools_WireExplorer wexp(W, F);
+
+       double xmin = 1.e300;           // min & max of face 2D parametric coord.
+       double xmax = -1.e300;
+       double ymin = 1.e300;
+       double ymax = -1.e300;
+       int nbp = 50;
+       scalex = 1;
+       scaley = 1;
+       for (wexp.Init(W, F); wexp.More(); wexp.Next())
        {
-         double param = f + (double(i)/double(nbp))*(l-f);
-         gp_Pnt2d p = C2d->Value(param);
-         if (p.X() < xmin) xmin = p.X();
-         if (p.X() > xmax) xmax = p.X();
-         if (p.Y() < ymin) ymin = p.Y();
-         if (p.Y() > ymax) ymax = p.Y();
-//       MESSAGE(" "<< f<<" "<<l<<" "<<param<<" "<<xmin<<" "<<xmax<<" "<<ymin<<" "<<ymax);
+               const TopoDS_Edge & E = wexp.Current();
+               double f, l;
+               Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, F, f, l);
+               for (int i = 0; i <= nbp; i++)
+               {
+                       double param = f + (double (i) / double (nbp))*(l - f);
+                       gp_Pnt2d p = C2d->Value(param);
+                       if (p.X() < xmin)
+                               xmin = p.X();
+                       if (p.X() > xmax)
+                               xmax = p.X();
+                       if (p.Y() < ymin)
+                               ymin = p.Y();
+                       if (p.Y() > ymax)
+                               ymax = p.Y();
+//    MESSAGE(" "<< f<<" "<<l<<" "<<param<<" "<<xmin<<" "<<xmax<<" "<<ymin<<" "<<ymax);
+               }
        }
-    }
 //   SCRUTE(xmin);
 //   SCRUTE(xmax);
 //   SCRUTE(ymin);
 //   SCRUTE(ymax);
-  double xmoy = (xmax + xmin)/2.; 
-  double ymoy = (ymax + ymin)/2.;
-
-  Handle(Geom_Surface) S = BRep_Tool::Surface(F); // 3D surface
-
-  double length_x = 0;
-  double length_y = 0;
-  gp_Pnt PX0 = S->Value(xmin, ymoy);
-  gp_Pnt PY0 = S->Value(xmoy, ymin);
-  for (Standard_Integer i = 1; i<= nbp; i++)
-    {
-      double x = xmin + (double(i)/double(nbp))*(xmax-xmin);
-      gp_Pnt PX = S->Value(x,ymoy);
-      double y = ymin + (double(i)/double(nbp))*(ymax-ymin);
-      gp_Pnt PY = S->Value(xmoy,y);
-      length_x += PX.Distance(PX0);
-      length_y += PY.Distance(PY0);
-      PX0.SetCoord(PX.X(),PX.Y(),PX.Z());
-      PY0.SetCoord(PY.X(),PY.Y(),PY.Z());
-    }
+       double xmoy = (xmax + xmin) / 2.;
+       double ymoy = (ymax + ymin) / 2.;
+
+       Handle(Geom_Surface) S = BRep_Tool::Surface(F); // 3D surface
+
+       double length_x = 0;
+       double length_y = 0;
+       gp_Pnt PX0 = S->Value(xmin, ymoy);
+       gp_Pnt PY0 = S->Value(xmoy, ymin);
+       for (int i = 1; i <= nbp; i++)
+       {
+               double x = xmin + (double (i) / double (nbp))*(xmax - xmin);
+               gp_Pnt PX = S->Value(x, ymoy);
+               double y = ymin + (double (i) / double (nbp))*(ymax - ymin);
+               gp_Pnt PY = S->Value(xmoy, y);
+               length_x += PX.Distance(PX0);
+               length_y += PY.Distance(PY0);
+               PX0.SetCoord(PX.X(), PX.Y(), PX.Z());
+               PY0.SetCoord(PY.X(), PY.Y(), PY.Z());
+       }
 //   SCRUTE(length_x);
 //   SCRUTE(length_y);
-  scalex = length_x/(xmax - xmin);
-  scaley = length_y/(ymax - ymin);
+       scalex = length_x / (xmax - xmin);
+       scaley = length_y / (ymax - ymin);
 //   SCRUTE(scalex);
 //   SCRUTE(scaley);
-  ASSERT(scalex);
-  ASSERT(scaley);
+       ASSERT(scalex);
+       ASSERT(scaley);
 }
 
 //=============================================================================
@@ -477,75 +484,71 @@ void SMESH_MEFISTO_2D::ComputeScaleOnFace(SMESH_Mesh& aMesh,
  */
 //=============================================================================
 
-void SMESH_MEFISTO_2D::StoreResult (SMESH_Mesh& aMesh,
-                                   Z nbst, R2* uvst, Z nbt, Z* nust, 
-                                   const TopoDS_Face& F, bool faceIsForward,
-                                   map<int,int>& mefistoToDS)
+void SMESH_MEFISTO_2D::StoreResult(SMESH_Mesh & aMesh,
+       Z nbst, R2 * uvst, Z nbt, Z * nust,
+       const TopoDS_Face & F, bool faceIsForward,
+       map<int, const SMDS_MeshNode*>&mefistoToDS)
 {
-  double scalex;
-  double scaley;
-  ComputeScaleOnFace(aMesh, F, scalex, scaley);
-
-  Handle (SMESHDS_Mesh) meshDS = aMesh.GetMeshDS();
-
-  Z n,m;
-  Handle(Geom_Surface) S = BRep_Tool::Surface(F);
-  
-  for ( n=0; n<nbst; n++ )
-    {
-      double u = uvst[n][0]/scalex;
-      double v = uvst[n][1]/scaley;
-      gp_Pnt P = S->Value(u,v);
-    
-      if (mefistoToDS.find(n+1) == mefistoToDS.end())
-       {
-         int nodeId = meshDS->AddNode(P.X(), P.Y(), P.Z());
-         Handle (SMDS_MeshElement) elt = meshDS->FindNode(nodeId);
-         Handle (SMDS_MeshNode) node = meshDS->GetNode(1, elt);
-         meshDS->SetNodeOnFace(node, F);
-         
-         //MESSAGE(nodeId<<" "<<P.X()<<" "<<P.Y()<<" "<<P.Z());
-         mefistoToDS[n+1] = nodeId;
-         //MESSAGE(" "<<n<<" "<<mefistoToDS[n+1]);
-         Handle (SMDS_FacePosition) fpos
-           = Handle (SMDS_FacePosition)::DownCast(node->GetPosition());
-         fpos->SetUParameter(u);
-         fpos->SetVParameter(v);
-       }
-    }
-  
-  m=0;
-  int mt=0;
-
-  //SCRUTE(faceIsForward);
-  for ( n=1; n<=nbt; n++ )
-    {
-      int inode1 = nust[m++];
-      int inode2 = nust[m++];
-      int inode3 = nust[m++];
-
-      int nodeId1 = mefistoToDS[inode1];
-      int nodeId2 = mefistoToDS[inode2];
-      int nodeId3 = mefistoToDS[inode3];
-      //MESSAGE("-- "<<inode1<<" "<<inode2<<" "<<inode3<<" ++ "<<nodeId1<<" "<<nodeId2<<" "<<nodeId3);
-
-      // triangle points must be in trigonometric order if face is Forward
-      // else they must be put clockwise
-
-      bool triangleIsWellOriented = faceIsForward;
-      int faceId;
-      if (triangleIsWellOriented)
+       double scalex;
+       double scaley;
+       ComputeScaleOnFace(aMesh, F, scalex, scaley);
+
+       SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
+
+       Z n, m;
+       Handle(Geom_Surface) S = BRep_Tool::Surface(F);
+
+       for (n = 0; n < nbst; n++)
        {
-         faceId = meshDS->AddFace(nodeId1, nodeId2, nodeId3);
+               double u = uvst[n][0] / scalex;
+               double v = uvst[n][1] / scaley;
+               gp_Pnt P = S->Value(u, v);
+
+               if (mefistoToDS.find(n + 1) == mefistoToDS.end())
+               {
+                       SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
+                       meshDS->SetNodeOnFace(node, F);
+
+                       //MESSAGE(nodeId<<" "<<P.X()<<" "<<P.Y()<<" "<<P.Z());
+                       mefistoToDS[n + 1] = node;
+                       //MESSAGE(" "<<n<<" "<<mefistoToDS[n+1]);
+                       SMDS_FacePosition* fpos
+                               = static_cast<SMDS_FacePosition*>(node->GetPosition());
+                       fpos->SetUParameter(u);
+                       fpos->SetVParameter(v);
+               }
        }
-      else
+
+       m = 0;
+       int mt = 0;
+
+       //SCRUTE(faceIsForward);
+       for (n = 1; n <= nbt; n++)
        {
-         faceId = meshDS->AddFace(nodeId1, nodeId3, nodeId2);
+               int inode1 = nust[m++];
+               int inode2 = nust[m++];
+               int inode3 = nust[m++];
+
+               const SMDS_MeshNode *n1, *n2, *n3;
+               n1 = mefistoToDS[inode1];
+               n2 = mefistoToDS[inode2];
+               n3 = mefistoToDS[inode3];
+               //MESSAGE("-- "<<inode1<<" "<<inode2<<" "<<inode3<<" ++ "<<nodeId1<<" "<<nodeId2<<" "<<nodeId3);
+
+               // triangle points must be in trigonometric order if face is Forward
+               // else they must be put clockwise
+
+               bool triangleIsWellOriented = faceIsForward;
+
+        SMDS_MeshElement * elt;
+               if (triangleIsWellOriented)
+                       elt = meshDS->AddFace(n1, n2, n3);
+               else
+                       elt = meshDS->AddFace(n1, n3, n2);
+       
+               meshDS->SetMeshElementOnShape(elt, F);
+               m++;
        }
-      Handle (SMDS_MeshElement) elt = meshDS->FindElement(faceId);
-      meshDS->SetMeshElementOnShape(elt, F);
-      m++;
-    }
 }
 
 //=============================================================================
@@ -554,35 +557,35 @@ void SMESH_MEFISTO_2D::StoreResult (SMESH_Mesh& aMesh,
  */
 //=============================================================================
 
-double SMESH_MEFISTO_2D::ComputeEdgeElementLength(SMESH_Mesh& aMesh,
-                                                 const TopoDS_Shape& aShape)
+double SMESH_MEFISTO_2D::ComputeEdgeElementLength(SMESH_Mesh & aMesh,
+       const TopoDS_Shape & aShape)
 {
-  MESSAGE("SMESH_MEFISTO_2D::ComputeEdgeElementLength");
-  // **** a mettre dans SMESH_2D_Algo ?
-
-  const TopoDS_Face& FF = TopoDS::Face(aShape);
-  bool faceIsForward = (FF.Orientation() == TopAbs_FORWARD);
-  TopoDS_Face F = TopoDS::Face(FF.Oriented(TopAbs_FORWARD));
-
-  double meanElementLength = 100;
-  double wireLength =0;
-  int wireElementsNumber =0;
-  for (TopExp_Explorer exp (F, TopAbs_WIRE); exp.More(); exp.Next())
-    {
-      const TopoDS_Wire& W = TopoDS::Wire(exp.Current());
-      for (TopExp_Explorer expe(W,TopAbs_EDGE); expe.More(); expe.Next())
+       MESSAGE("SMESH_MEFISTO_2D::ComputeEdgeElementLength");
+       // **** a mettre dans SMESH_2D_Algo ?
+
+       const TopoDS_Face & FF = TopoDS::Face(aShape);
+       bool faceIsForward = (FF.Orientation() == TopAbs_FORWARD);
+       TopoDS_Face F = TopoDS::Face(FF.Oriented(TopAbs_FORWARD));
+
+       double meanElementLength = 100;
+       double wireLength = 0;
+       int wireElementsNumber = 0;
+       for (TopExp_Explorer exp(F, TopAbs_WIRE); exp.More(); exp.Next())
        {
-         const TopoDS_Edge& E = TopoDS::Edge(expe.Current());
-         int nb = aMesh.GetSubMesh(E)->GetSubMeshDS()->NbNodes();
-         double length = EdgeLength(E);
-         wireLength += length;
-         wireElementsNumber += nb;
+               const TopoDS_Wire & W = TopoDS::Wire(exp.Current());
+               for (TopExp_Explorer expe(W, TopAbs_EDGE); expe.More(); expe.Next())
+               {
+                       const TopoDS_Edge & E = TopoDS::Edge(expe.Current());
+                       int nb = aMesh.GetSubMesh(E)->GetSubMeshDS()->NbNodes();
+                       double length = EdgeLength(E);
+                       wireLength += length;
+                       wireElementsNumber += nb;
+               }
        }
-    }
-  if (wireElementsNumber)
-    meanElementLength = wireLength/wireElementsNumber;
-  //SCRUTE(meanElementLength);
-  return meanElementLength;
+       if (wireElementsNumber)
+               meanElementLength = wireLength / wireElementsNumber;
+       //SCRUTE(meanElementLength);
+       return meanElementLength;
 }
 
 //=============================================================================
@@ -593,7 +596,7 @@ double SMESH_MEFISTO_2D::ComputeEdgeElementLength(SMESH_Mesh& aMesh,
 
 ostream & SMESH_MEFISTO_2D::SaveTo(ostream & save)
 {
-  return save << this;
+       return save << this;
 }
 
 //=============================================================================
@@ -604,7 +607,7 @@ ostream & SMESH_MEFISTO_2D::SaveTo(ostream & save)
 
 istream & SMESH_MEFISTO_2D::LoadFrom(istream & load)
 {
-  return load >> (*this);
+       return load >> (*this);
 }
 
 //=============================================================================
@@ -613,9 +616,9 @@ istream & SMESH_MEFISTO_2D::LoadFrom(istream & load)
  */
 //=============================================================================
 
-ostream & operator << (ostream & save, SMESH_MEFISTO_2D & hyp)
+ostream & operator <<(ostream & save, SMESH_MEFISTO_2D & hyp)
 {
-  return save;
+       return save;
 }
 
 //=============================================================================
@@ -624,8 +627,7 @@ ostream & operator << (ostream & save, SMESH_MEFISTO_2D & hyp)
  */
 //=============================================================================
 
-istream & operator >> (istream & load, SMESH_MEFISTO_2D & hyp)
+istream & operator >>(istream & load, SMESH_MEFISTO_2D & hyp)
 {
-  return load;
+       return load;
 }
-