Salome HOME
Clean-up deprecated OCCT-related code
[modules/smesh.git] / src / SMESH / SMESH_Algo.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SMESH SMESH : implementaion of SMESH idl descriptions
24 //  File   : SMESH_Algo.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27
28 #include "SMESH_Algo.hxx"
29
30 #include "SMDS_EdgePosition.hxx"
31 #include "SMDS_FacePosition.hxx"
32 #include "SMDS_MeshElement.hxx"
33 #include "SMDS_MeshNode.hxx"
34 #include "SMDS_VolumeTool.hxx"
35 #include "SMESHDS_Mesh.hxx"
36 #include "SMESHDS_SubMesh.hxx"
37 #include "SMESH_Comment.hxx"
38 #include "SMESH_Gen.hxx"
39 #include "SMESH_HypoFilter.hxx"
40 #include "SMESH_Mesh.hxx"
41 #include "SMESH_MeshAlgos.hxx"
42 #include "SMESH_TypeDefs.hxx"
43 #include "SMESH_subMesh.hxx"
44
45 #include <BRepAdaptor_Curve.hxx>
46 #include <BRepLProp.hxx>
47 #include <BRep_Tool.hxx>
48 #include <GCPnts_AbscissaPoint.hxx>
49 #include <GeomAdaptor_Curve.hxx>
50 #include <Geom_Surface.hxx>
51 #include <LDOMParser.hxx>
52 #include <TopExp.hxx>
53 #include <TopExp_Explorer.hxx>
54 #include <TopLoc_Location.hxx>
55 #include <TopTools_ListIteratorOfListOfShape.hxx>
56 #include <TopTools_ListOfShape.hxx>
57 #include <TopoDS.hxx>
58 #include <TopoDS_Edge.hxx>
59 #include <TopoDS_Face.hxx>
60 #include <TopoDS_Vertex.hxx>
61 #include <TopoDS_Wire.hxx>
62 #include <gp_Pnt.hxx>
63 #include <gp_Pnt2d.hxx>
64 #include <gp_Vec.hxx>
65
66 #include <Standard_ErrorHandler.hxx>
67 #include <Standard_Failure.hxx>
68
69 #include "utilities.h"
70
71 #include <algorithm>
72 #include <limits>
73 #include "SMESH_ProxyMesh.hxx"
74 #include "SMESH_MesherHelper.hxx"
75
76 using namespace std;
77
78 //================================================================================
79 /*!
80  * \brief Returns \a true if two algorithms (described by \a this and the given
81  *        algo data) are compatible by their output and input types of elements.
82  */
83 //================================================================================
84
85 bool SMESH_Algo::Features::IsCompatible( const SMESH_Algo::Features& algo2 ) const
86 {
87   if ( _dim > algo2._dim ) return algo2.IsCompatible( *this );
88   // algo2 is of highter dimension
89   if ( _outElemTypes.empty() || algo2._inElemTypes.empty() )
90     return false;
91   bool compatible = true;
92   set<SMDSAbs_GeometryType>::const_iterator myOutType = _outElemTypes.begin();
93   for ( ; myOutType != _outElemTypes.end() && compatible; ++myOutType )
94     compatible = algo2._inElemTypes.count( *myOutType );
95   return compatible;
96 }
97
98 //================================================================================
99 /*!
100  * \brief Return Data of the algorithm
101  */
102 //================================================================================
103
104 const SMESH_Algo::Features& SMESH_Algo::GetFeatures( const std::string& algoType )
105 {
106   static map< string, SMESH_Algo::Features > theFeaturesByName;
107   if ( theFeaturesByName.empty() )
108   {
109     // Read Plugin.xml files
110     vector< string > xmlPaths = SMESH_Gen::GetPluginXMLPaths();
111     LDOMParser xmlParser;
112     for ( size_t iXML = 0; iXML < xmlPaths.size(); ++iXML )
113     {
114       bool error = xmlParser.parse( xmlPaths[iXML].c_str() );
115       if ( error )
116       {
117         TCollection_AsciiString data;
118         INFOS( xmlParser.GetError(data) );
119         continue;
120       }
121       // <algorithm type="Regular_1D"
122       //            ...
123       //            input="EDGE"
124       //            output="QUAD,TRIA">
125       //
126       LDOM_Document xmlDoc = xmlParser.getDocument();
127       LDOM_NodeList algoNodeList = xmlDoc.getElementsByTagName( "algorithm" );
128       for ( int i = 0; i < algoNodeList.getLength(); ++i )
129       {
130         LDOM_Node     algoNode           = algoNodeList.item( i );
131         LDOM_Element& algoElem           = (LDOM_Element&) algoNode;
132         TCollection_AsciiString algoType = algoElem.getAttribute("type");
133         TCollection_AsciiString input    = algoElem.getAttribute("input");
134         TCollection_AsciiString output   = algoElem.getAttribute("output");
135         TCollection_AsciiString dim      = algoElem.getAttribute("dim");
136         TCollection_AsciiString label    = algoElem.getAttribute("label-id");
137         if ( algoType.IsEmpty() ) continue;
138
139         Features & data = theFeaturesByName[ algoType.ToCString() ];
140         data._dim   = dim.IntegerValue();
141         data._label = label.ToCString();
142         for ( int isInput = 0; isInput < 2; ++isInput )
143         {
144           TCollection_AsciiString&   typeStr = isInput ? input : output;
145           set<SMDSAbs_GeometryType>& typeSet = isInput ? data._inElemTypes : data._outElemTypes;
146           int beg = 1, end;
147           while ( beg <= typeStr.Length() )
148           {
149             while ( beg < typeStr.Length() && !isalpha( typeStr.Value( beg ) ))
150               ++beg;
151             end = beg;
152             while ( end < typeStr.Length() && isalpha( typeStr.Value( end + 1 ) ))
153               ++end;
154             if ( end > beg )
155             {
156               TCollection_AsciiString typeName = typeStr.SubString( beg, end );
157               if      ( typeName == "EDGE" ) typeSet.insert( SMDSGeom_EDGE );
158               else if ( typeName == "TRIA" ) typeSet.insert( SMDSGeom_TRIANGLE );
159               else if ( typeName == "QUAD" ) typeSet.insert( SMDSGeom_QUADRANGLE );
160             }
161             beg = end + 1;
162           }
163         }
164       }
165     }
166   }
167   return theFeaturesByName[ algoType ];
168 }
169
170 //=============================================================================
171 /*!
172  *  
173  */
174 //=============================================================================
175
176 SMESH_Algo::SMESH_Algo (int hypId, int studyId, SMESH_Gen * gen)
177   : SMESH_Hypothesis(hypId, studyId, gen)
178 {
179   _compatibleAllHypFilter = _compatibleNoAuxHypFilter = NULL;
180   _onlyUnaryInput = _requireDiscreteBoundary = _requireShape = true;
181   _quadraticMesh = _supportSubmeshes = false;
182   _error = COMPERR_OK;
183   for ( int i = 0; i < 4; ++i )
184     _neededLowerHyps[ i ] = false;
185 }
186
187 //=============================================================================
188 /*!
189  *  
190  */
191 //=============================================================================
192
193 SMESH_Algo::~SMESH_Algo()
194 {
195   delete _compatibleNoAuxHypFilter;
196   // delete _compatibleAllHypFilter; -- _compatibleNoAuxHypFilter does it!!!
197 }
198
199 //=============================================================================
200 /*!
201  *  
202  */
203 //=============================================================================
204
205 SMESH_0D_Algo::SMESH_0D_Algo(int hypId, int studyId, SMESH_Gen* gen)
206   : SMESH_Algo(hypId, studyId, gen)
207 {
208   _shapeType = (1 << TopAbs_VERTEX);
209   _type = ALGO_0D;
210 }
211 SMESH_1D_Algo::SMESH_1D_Algo(int hypId, int studyId, SMESH_Gen* gen)
212   : SMESH_Algo(hypId, studyId, gen)
213 {
214   _shapeType = (1 << TopAbs_EDGE);
215   _type = ALGO_1D;
216 }
217 SMESH_2D_Algo::SMESH_2D_Algo(int hypId, int studyId, SMESH_Gen* gen)
218   : SMESH_Algo(hypId, studyId, gen)
219 {
220   _shapeType = (1 << TopAbs_FACE);
221   _type = ALGO_2D;
222 }
223 SMESH_3D_Algo::SMESH_3D_Algo(int hypId, int studyId, SMESH_Gen* gen)
224   : SMESH_Algo(hypId, studyId, gen)
225 {
226   _shapeType = (1 << TopAbs_SOLID);
227   _type = ALGO_3D;
228 }
229
230 //=============================================================================
231 /*!
232  * Usually an algorithm has nothing to save
233  */
234 //=============================================================================
235
236 ostream & SMESH_Algo::SaveTo(ostream & save) { return save; }
237 istream & SMESH_Algo::LoadFrom(istream & load) { return load; }
238
239 //=============================================================================
240 /*!
241  *  
242  */
243 //=============================================================================
244
245 const vector < string > &SMESH_Algo::GetCompatibleHypothesis()
246 {
247   return _compatibleHypothesis;
248 }
249
250 //=============================================================================
251 /*!
252  *  List the hypothesis used by the algorithm associated to the shape.
253  *  Hypothesis associated to father shape -are- taken into account (see
254  *  GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
255  *  the algorithm. This method could be surcharged by specific algorithms, in 
256  *  case of several hypothesis simultaneously applicable.
257  */
258 //=============================================================================
259
260 const list <const SMESHDS_Hypothesis *> &
261 SMESH_Algo::GetUsedHypothesis(SMESH_Mesh &         aMesh,
262                               const TopoDS_Shape & aShape,
263                               const bool           ignoreAuxiliary) const
264 {
265   SMESH_Algo* me = const_cast< SMESH_Algo* >( this );
266   me->_usedHypList.clear();
267   if ( const SMESH_HypoFilter* filter = GetCompatibleHypoFilter( ignoreAuxiliary ))
268   {
269     aMesh.GetHypotheses( aShape, *filter, me->_usedHypList, true );
270     if ( ignoreAuxiliary && _usedHypList.size() > 1 )
271       me->_usedHypList.clear(); //only one compatible hypothesis allowed
272   }
273   return _usedHypList;
274 }
275
276 //=============================================================================
277 /*!
278  *  List the relevant hypothesis associated to the shape. Relevant hypothesis
279  *  have a name (type) listed in the algorithm. Hypothesis associated to
280  *  father shape -are not- taken into account (see GetUsedHypothesis)
281  */
282 //=============================================================================
283
284 const list<const SMESHDS_Hypothesis *> &
285 SMESH_Algo::GetAppliedHypothesis(SMESH_Mesh &         aMesh,
286                                  const TopoDS_Shape & aShape,
287                                  const bool           ignoreAuxiliary) const
288 {
289   SMESH_Algo* me = const_cast< SMESH_Algo* >( this );
290   me->_appliedHypList.clear();
291   if ( const SMESH_HypoFilter* filter = GetCompatibleHypoFilter( ignoreAuxiliary ))
292     aMesh.GetHypotheses( aShape, *filter, me->_appliedHypList, false );
293
294   return _appliedHypList;
295 }
296
297 //=============================================================================
298 /*!
299  *  Compute length of an edge
300  */
301 //=============================================================================
302
303 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
304 {
305   double UMin = 0, UMax = 0;
306   TopLoc_Location L;
307   Handle(Geom_Curve) C = BRep_Tool::Curve(E, L, UMin, UMax);
308   if ( C.IsNull() )
309     return 0.;
310   GeomAdaptor_Curve AdaptCurve(C, UMin, UMax); //range is important for periodic curves
311   double length = GCPnts_AbscissaPoint::Length(AdaptCurve, UMin, UMax);
312   return length;
313 }
314
315 //================================================================================
316 /*!
317  * \brief Just return false as the algorithm does not hold parameters values
318  */
319 //================================================================================
320
321 bool SMESH_Algo::SetParametersByMesh(const SMESH_Mesh* /*theMesh*/,
322                                      const TopoDS_Shape& /*theShape*/)
323 {
324   return false;
325 }
326 bool SMESH_Algo::SetParametersByDefaults(const TDefaults& , const SMESH_Mesh*)
327 {
328   return false;
329 }
330 //================================================================================
331 /*!
332  * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
333  * \param theMesh - The mesh containing nodes
334  * \param theEdge - The geometrical edge of interest
335  * \param theParams - The resulting vector of sorted node parameters
336  * \retval bool - false if not all parameters are OK
337  */
338 //================================================================================
339
340 bool SMESH_Algo::GetNodeParamOnEdge(const SMESHDS_Mesh* theMesh,
341                                     const TopoDS_Edge&  theEdge,
342                                     vector< double > &  theParams)
343 {
344   theParams.clear();
345
346   if ( !theMesh || theEdge.IsNull() )
347     return false;
348
349   SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
350   if ( !eSubMesh || !eSubMesh->GetElements()->more() )
351     return false; // edge is not meshed
352
353   //int nbEdgeNodes = 0;
354   set < double > paramSet;
355   if ( eSubMesh )
356   {
357     // loop on nodes of an edge: sort them by param on edge
358     SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
359     while ( nIt->more() )
360     {
361       const SMDS_MeshNode* node = nIt->next();
362       const SMDS_PositionPtr& pos = node->GetPosition();
363       if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
364         return false;
365       const SMDS_EdgePosition* epos =
366         static_cast<const SMDS_EdgePosition*>(node->GetPosition());
367       if ( !paramSet.insert( epos->GetUParameter() ).second )
368         return false; // equal parameters
369     }
370   }
371   // add vertex nodes params
372   TopoDS_Vertex V1,V2;
373   TopExp::Vertices( theEdge, V1, V2);
374   if ( VertexNode( V1, theMesh ) &&
375        !paramSet.insert( BRep_Tool::Parameter(V1,theEdge) ).second )
376     return false; // there are equal parameters
377   if ( VertexNode( V2, theMesh ) &&
378        !paramSet.insert( BRep_Tool::Parameter(V2,theEdge) ).second )
379     return false; // there are equal parameters
380
381   // fill the vector
382   theParams.resize( paramSet.size() );
383   set < double >::iterator   par    = paramSet.begin();
384   vector< double >::iterator vecPar = theParams.begin();
385   for ( ; par != paramSet.end(); ++par, ++vecPar )
386     *vecPar = *par;
387
388   return theParams.size() > 1;
389 }
390
391 //================================================================================
392 /*!
393  * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
394  * \param theMesh - The mesh containing nodes
395  * \param theEdge - The geometrical edge of interest
396  * \param theParams - The resulting vector of sorted node parameters
397  * \retval bool - false if not all parameters are OK
398  */
399 //================================================================================
400
401 bool SMESH_Algo::GetSortedNodesOnEdge(const SMESHDS_Mesh*                   theMesh,
402                                       const TopoDS_Edge&                    theEdge,
403                                       const bool                            ignoreMediumNodes,
404                                       map< double, const SMDS_MeshNode* > & theNodes,
405                                       const SMDSAbs_ElementType             typeToCheck)
406 {
407   theNodes.clear();
408
409   if ( !theMesh || theEdge.IsNull() )
410     return false;
411
412   SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
413   if ( !eSubMesh || ( eSubMesh->NbElements() == 0 && eSubMesh->NbNodes() == 0))
414     return false; // edge is not meshed
415
416   int nbNodes = 0;
417   set < double > paramSet;
418   if ( eSubMesh )
419   {
420     // loop on nodes of an edge: sort them by param on edge
421     SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
422     while ( nIt->more() )
423     {
424       const SMDS_MeshNode* node = nIt->next();
425       if ( ignoreMediumNodes && SMESH_MesherHelper::IsMedium( node, typeToCheck ))
426         continue;
427       const SMDS_PositionPtr& pos = node->GetPosition();
428       if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
429         return false;
430       const SMDS_EdgePosition* epos =
431         static_cast<const SMDS_EdgePosition*>(node->GetPosition());
432       theNodes.insert( theNodes.end(), make_pair( epos->GetUParameter(), node ));
433       ++nbNodes;
434     }
435   }
436   // add vertex nodes
437   TopoDS_Vertex v1, v2;
438   TopExp::Vertices(theEdge, v1, v2);
439   const SMDS_MeshNode* n1 = VertexNode( v1, eSubMesh, 0 );
440   const SMDS_MeshNode* n2 = VertexNode( v2, eSubMesh, 0 );
441   const SMDS_MeshNode* nEnd[2] = { nbNodes ? theNodes.begin()->second  : 0,
442                                    nbNodes ? theNodes.rbegin()->second : 0 };
443   Standard_Real f, l;
444   BRep_Tool::Range(theEdge, f, l);
445   if ( v1.Orientation() != TopAbs_FORWARD )
446     std::swap( f, l );
447   if ( n1 && n1 != nEnd[0] && n1 != nEnd[1] && ++nbNodes )
448     theNodes.insert( make_pair( f, n1 ));
449   if ( n2 && n2 != nEnd[0] && n2 != nEnd[1] && ++nbNodes )
450     theNodes.insert( make_pair( l, n2 ));
451
452   return (int)theNodes.size() == nbNodes;
453 }
454
455 //================================================================================
456 /*!
457  * \brief Returns the filter recognizing only compatible hypotheses
458  *  \param ignoreAuxiliary - make filter ignore auxiliary hypotheses
459  *  \retval SMESH_HypoFilter* - the filter that can be NULL
460  */
461 //================================================================================
462
463 const SMESH_HypoFilter*
464 SMESH_Algo::GetCompatibleHypoFilter(const bool ignoreAuxiliary) const
465 {
466   if ( !_compatibleHypothesis.empty() )
467   {
468     if ( !_compatibleAllHypFilter )
469     {
470       SMESH_HypoFilter* filter = new SMESH_HypoFilter();
471       filter->Init( filter->HasName( _compatibleHypothesis[0] ));
472       for ( size_t i = 1; i < _compatibleHypothesis.size(); ++i )
473         filter->Or( filter->HasName( _compatibleHypothesis[ i ] ));
474
475       SMESH_HypoFilter* filterNoAux = new SMESH_HypoFilter( filter );
476       filterNoAux->AndNot( filterNoAux->IsAuxiliary() );
477
478       // _compatibleNoAuxHypFilter will detele _compatibleAllHypFilter!!!
479       SMESH_Algo* me = const_cast< SMESH_Algo* >( this );
480       me->_compatibleAllHypFilter   = filter;
481       me->_compatibleNoAuxHypFilter = filterNoAux;
482     }
483     return ignoreAuxiliary ? _compatibleNoAuxHypFilter : _compatibleAllHypFilter;
484   }
485   return 0;
486 }
487
488 //================================================================================
489 /*!
490  * \brief Return continuity of two edges
491  * \param E1 - the 1st edge
492  * \param E2 - the 2nd edge
493  * \retval GeomAbs_Shape - regularity at the junction between E1 and E2
494  */
495 //================================================================================
496
497 GeomAbs_Shape SMESH_Algo::Continuity(const TopoDS_Edge& theE1,
498                                      const TopoDS_Edge& theE2)
499 {
500   // avoid pb with internal edges
501   TopoDS_Edge E1 = theE1, E2 = theE2;
502   if (E1.Orientation() > TopAbs_REVERSED) // INTERNAL
503     E1.Orientation( TopAbs_FORWARD );
504   if (E2.Orientation() > TopAbs_REVERSED) // INTERNAL
505     E2.Orientation( TopAbs_FORWARD );
506
507   TopoDS_Vertex V, VV1[2], VV2[2];
508   TopExp::Vertices( E1, VV1[0], VV1[1], true );
509   TopExp::Vertices( E2, VV2[0], VV2[1], true );
510   if      ( VV1[1].IsSame( VV2[0] ))  { V = VV1[1]; }
511   else if ( VV1[0].IsSame( VV2[1] ))  { V = VV1[0]; }
512   else if ( VV1[1].IsSame( VV2[1] ))  { V = VV1[1]; E1.Reverse(); }
513   else if ( VV1[0].IsSame( VV2[0] ))  { V = VV1[0]; E1.Reverse(); }
514   else { return GeomAbs_C0; }
515
516   Standard_Real u1 = BRep_Tool::Parameter( V, E1 );
517   Standard_Real u2 = BRep_Tool::Parameter( V, E2 );
518   BRepAdaptor_Curve C1( E1 ), C2( E2 );
519   Standard_Real tol = BRep_Tool::Tolerance( V );
520   Standard_Real angTol = 2e-3;
521   try {
522     OCC_CATCH_SIGNALS;
523     return BRepLProp::Continuity(C1, C2, u1, u2, tol, angTol);
524   }
525   catch (Standard_Failure) {
526   }
527   return GeomAbs_C0;
528 }
529
530 //================================================================================
531 /*!
532  * \brief Return true if an edge can be considered straight
533  */
534 //================================================================================
535
536 bool SMESH_Algo::IsStraight( const TopoDS_Edge & E,
537                              const bool          degenResult)
538 {
539   {
540     double f,l;
541     if ( BRep_Tool::Curve( E, f, l ).IsNull())
542       return degenResult;
543   }
544   BRepAdaptor_Curve curve( E );
545   switch( curve.GetType() )
546   {
547   case GeomAbs_Line:
548     return true;
549   case GeomAbs_Circle:
550   case GeomAbs_Ellipse:
551   case GeomAbs_Hyperbola:
552   case GeomAbs_Parabola:
553     return false;
554     // case GeomAbs_BezierCurve:
555     // case GeomAbs_BSplineCurve:
556     // case GeomAbs_OtherCurve:
557   default:;
558   }
559
560   // evaluate how far from a straight line connecting the curve ends
561   // stand internal points of the curve
562   double  f = curve.FirstParameter();
563   double  l = curve.LastParameter();
564   gp_Pnt pf = curve.Value( f );
565   gp_Pnt pl = curve.Value( l );
566   gp_Vec lineVec( pf, pl );
567   double lineLen2 = lineVec.SquareMagnitude();
568   if ( lineLen2 < std::numeric_limits< double >::min() )
569     return false; // E seems closed
570
571   double edgeTol = 10 * curve.Tolerance();
572   double lenTol2 = lineLen2 * 1e-4; 
573   double tol2 = Min( edgeTol * edgeTol, lenTol2 );
574
575   const double nbSamples = 7;
576   for ( int i = 0; i < nbSamples; ++i )
577   {
578     double  r = ( i + 1 ) / nbSamples;
579     gp_Pnt pi = curve.Value( f * r + l * ( 1 - r ));
580     gp_Vec vi( pf, pi );
581     double h2 = lineVec.Crossed( vi ).SquareMagnitude() / lineLen2;
582     if ( h2 > tol2 )
583       return false;
584   }
585   return true;
586 }
587
588 //================================================================================
589 /*!
590  * \brief Return true if an edge has no 3D curve
591  */
592 //================================================================================
593
594 bool SMESH_Algo::isDegenerated( const TopoDS_Edge & E, const bool checkLength )
595 {
596   if ( checkLength )
597     return EdgeLength( E ) == 0;
598   double f,l;
599   TopLoc_Location loc;
600   Handle(Geom_Curve) C = BRep_Tool::Curve( E, loc, f,l );
601   return C.IsNull();
602 }
603
604 //================================================================================
605 /*!
606  * \brief Return the node built on a vertex
607  * \param V - the vertex
608  * \param meshDS - mesh
609  * \retval const SMDS_MeshNode* - found node or NULL
610  * \sa SMESH_MesherHelper::GetSubShapeByNode( const SMDS_MeshNode*, SMESHDS_Mesh* )
611  */
612 //================================================================================
613
614 const SMDS_MeshNode* SMESH_Algo::VertexNode(const TopoDS_Vertex& V,
615                                             const SMESHDS_Mesh*  meshDS)
616 {
617   if ( SMESHDS_SubMesh* sm = meshDS->MeshElements(V) ) {
618     SMDS_NodeIteratorPtr nIt= sm->GetNodes();
619     if (nIt->more())
620       return nIt->next();
621   }
622   return 0;
623 }
624
625 //=======================================================================
626 /*!
627  * \brief Return the node built on a vertex.
628  *        A node moved to other geometry by MergeNodes() is also returned.
629  * \param V - the vertex
630  * \param mesh - mesh
631  * \retval const SMDS_MeshNode* - found node or NULL
632  */
633 //=======================================================================
634
635 const SMDS_MeshNode* SMESH_Algo::VertexNode(const TopoDS_Vertex& V,
636                                             const SMESH_Mesh*    mesh)
637 {
638   const SMDS_MeshNode* node = VertexNode( V, mesh->GetMeshDS() );
639
640   if ( !node && mesh->HasModificationsToDiscard() )
641   {
642     PShapeIteratorPtr edgeIt = SMESH_MesherHelper::GetAncestors( V, *mesh, TopAbs_EDGE );
643     while ( const TopoDS_Shape* edge = edgeIt->next() )
644       if ( SMESHDS_SubMesh* edgeSM = mesh->GetMeshDS()->MeshElements( *edge ))
645         if ( edgeSM->NbElements() > 0 )
646           return VertexNode( V, edgeSM, mesh, /*checkV=*/false );
647   }
648   return node;
649 }
650
651 //=======================================================================
652 /*!
653  * \brief Return the node built on a vertex.
654  *        A node moved to other geometry by MergeNodes() is also returned.
655  * \param V - the vertex
656  * \param edgeSM - sub-mesh of a meshed EDGE sharing the vertex
657  * \param checkV - if \c true, presence of a node on the vertex is checked
658  * \retval const SMDS_MeshNode* - found node or NULL
659  */
660 //=======================================================================
661
662 const SMDS_MeshNode* SMESH_Algo::VertexNode(const TopoDS_Vertex&   V,
663                                             const SMESHDS_SubMesh* edgeSM,
664                                             const SMESH_Mesh*      mesh,
665                                             const bool             checkV)
666 {
667   const SMDS_MeshNode* node = checkV ? VertexNode( V, edgeSM->GetParent() ) : 0;
668
669   if ( !node && edgeSM )
670   {
671     // find nodes not shared by mesh segments
672     typedef set< const SMDS_MeshNode* >                       TNodeSet;
673     typedef map< const SMDS_MeshNode*, const SMDS_MeshNode* > TNodeMap;
674     TNodeMap notSharedNodes;
675     TNodeSet otherShapeNodes;
676     vector< const SMDS_MeshNode* > segNodes(3);
677     SMDS_ElemIteratorPtr segIt = edgeSM->GetElements();
678     while ( segIt->more() )
679     {
680       const SMDS_MeshElement* seg = segIt->next();
681       if ( seg->GetType() != SMDSAbs_Edge )
682         return node;
683       segNodes.assign( seg->begin_nodes(), seg->end_nodes() );
684       for ( int i = 0; i < 2; ++i )
685       {
686         const SMDS_MeshNode* n1 = segNodes[i];
687         const SMDS_MeshNode* n2 = segNodes[1-i];
688         pair<TNodeMap::iterator, bool> it2new = notSharedNodes.insert( make_pair( n1, n2 ));
689         if ( !it2new.second ) // n encounters twice
690           notSharedNodes.erase( it2new.first );
691         if ( n1->getshapeId() != edgeSM->GetID() )
692           otherShapeNodes.insert( n1 );
693       }
694     }
695     if ( otherShapeNodes.size() == 1 && notSharedNodes.empty() ) // a closed EDGE
696       return *otherShapeNodes.begin();
697
698     if ( notSharedNodes.size() == 2 ) // two end nodes found
699     {
700       SMESHDS_Mesh*  meshDS = edgeSM->GetParent();
701       const TopoDS_Shape& E = meshDS->IndexToShape( edgeSM->GetID() );
702       if ( E.IsNull() || E.ShapeType() != TopAbs_EDGE )
703         return node;
704       const SMDS_MeshNode* n1 = notSharedNodes.begin ()->first;
705       const SMDS_MeshNode* n2 = notSharedNodes.rbegin()->first;
706       TopoDS_Shape S1 = SMESH_MesherHelper::GetSubShapeByNode( n1, meshDS );
707       if ( S1.ShapeType() == TopAbs_VERTEX && SMESH_MesherHelper::IsSubShape( S1, E ))
708         return n2;
709       TopoDS_Shape S2 = SMESH_MesherHelper::GetSubShapeByNode( n2, meshDS );
710       if ( S2.ShapeType() == TopAbs_VERTEX && SMESH_MesherHelper::IsSubShape( S2, E ))
711         return n1;
712       if ( edgeSM->NbElements() <= 2 || !mesh ) // one-two segments
713       {
714         gp_Pnt pV = BRep_Tool::Pnt( V );
715         double dist1 = pV.SquareDistance( SMESH_TNodeXYZ( n1 ));
716         double dist2 = pV.SquareDistance( SMESH_TNodeXYZ( n2 ));
717         return dist1 < dist2 ? n1 : n2;
718       }
719       if ( mesh )
720       {
721         SMESH_MesherHelper helper( const_cast<SMESH_Mesh&>( *mesh ));
722         const SMDS_MeshNode* n1i = notSharedNodes.begin ()->second;
723         const SMDS_MeshNode* n2i = notSharedNodes.rbegin()->second;
724         const TopoDS_Edge&  edge = TopoDS::Edge( E );
725         bool  posOK = true;
726         double pos1 = helper.GetNodeU( edge, n1i, n2i, &posOK );
727         double pos2 = helper.GetNodeU( edge, n2i, n1i, &posOK );
728         double posV = BRep_Tool::Parameter( V, edge );
729         if ( Abs( pos1 - posV ) < Abs( pos2 - posV )) return n1;
730         else                                          return n2;
731       }
732     }
733   }
734   return node;
735 }
736
737 //=======================================================================
738 //function : GetMeshError
739 //purpose  : Finds topological errors of a sub-mesh
740 //WARNING  : 1D check is NOT implemented so far
741 //=======================================================================
742
743 SMESH_Algo::EMeshError SMESH_Algo::GetMeshError(SMESH_subMesh* subMesh)
744 {
745   EMeshError err = MEr_OK;
746
747   SMESHDS_SubMesh* smDS = subMesh->GetSubMeshDS();
748   if ( !smDS )
749     return MEr_EMPTY;
750
751   switch ( subMesh->GetSubShape().ShapeType() )
752   {
753   case TopAbs_FACE: { // ====================== 2D =====================
754
755     SMDS_ElemIteratorPtr fIt = smDS->GetElements();
756     if ( !fIt->more() )
757       return MEr_EMPTY;
758
759     // We check that only links on EDGEs encouter once, the rest links, twice
760     set< SMESH_TLink > links;
761     while ( fIt->more() )
762     {
763       const SMDS_MeshElement* f = fIt->next();
764       int nbNodes = f->NbCornerNodes(); // ignore medium nodes
765       for ( int i = 0; i < nbNodes; ++i )
766       {
767         const SMDS_MeshNode* n1 = f->GetNode( i );
768         const SMDS_MeshNode* n2 = f->GetNode(( i+1 ) % nbNodes);
769         std::pair< set< SMESH_TLink >::iterator, bool > it_added =
770           links.insert( SMESH_TLink( n1, n2 ));
771         if ( !it_added.second )
772           // As we do NOT(!) check if mesh is manifold, we believe that a link can
773           // encounter once or twice only (not three times), we erase a link as soon
774           // as it encounters twice to speed up search in the <links> map.
775           links.erase( it_added.first );
776       }
777     }
778     // the links remaining in the <links> should all be on EDGE
779     set< SMESH_TLink >::iterator linkIt = links.begin();
780     for ( ; linkIt != links.end(); ++linkIt )
781     {
782       const SMESH_TLink& link = *linkIt;
783       if ( link.node1()->GetPosition()->GetTypeOfPosition() > SMDS_TOP_EDGE ||
784            link.node2()->GetPosition()->GetTypeOfPosition() > SMDS_TOP_EDGE )
785         return MEr_HOLES;
786     }
787     // TODO: to check orientation
788     break;
789   }
790   case TopAbs_SOLID: { // ====================== 3D =====================
791
792     SMDS_ElemIteratorPtr vIt = smDS->GetElements();
793     if ( !vIt->more() )
794       return MEr_EMPTY;
795
796     SMDS_VolumeTool vTool;
797     while ( !vIt->more() )
798     {
799       if (!vTool.Set( vIt->next() ))
800         continue; // strange
801
802       for ( int iF = 0; iF < vTool.NbFaces(); ++iF )
803         if ( vTool.IsFreeFace( iF ))
804         {
805           int nbN = vTool.NbFaceNodes( iF );
806           const SMDS_MeshNode** nodes =  vTool.GetFaceNodes( iF );
807           for ( int i = 0; i < nbN; ++i )
808             if ( nodes[i]->GetPosition()->GetTypeOfPosition() > SMDS_TOP_FACE )
809               return MEr_HOLES;
810         }
811     }
812     break;
813   }
814   default:;
815   }
816   return err;
817 }
818
819 //================================================================================
820 /*!
821  * \brief Sets event listener to submeshes if necessary
822  * \param subMesh - submesh where algo is set
823  * 
824  * After being set, event listener is notified on each event of a submesh.
825  * By default non listener is set
826  */
827 //================================================================================
828
829 void SMESH_Algo::SetEventListener(SMESH_subMesh* /*subMesh*/)
830 {
831 }
832
833 //================================================================================
834 /*!
835  * \brief Allow algo to do something after persistent restoration
836  * \param subMesh - restored submesh
837  *
838  * This method is called only if a submesh has HYP_OK algo_state.
839  */
840 //================================================================================
841
842 void SMESH_Algo::SubmeshRestored(SMESH_subMesh* /*subMesh*/)
843 {
844 }
845
846 //================================================================================
847 /*!
848  * \brief Computes mesh without geometry
849  * \param aMesh - the mesh
850  * \param aHelper - helper that must be used for adding elements to \aaMesh
851  * \retval bool - is a success
852  */
853 //================================================================================
854
855 bool SMESH_Algo::Compute(SMESH_Mesh & /*aMesh*/, SMESH_MesherHelper* /*aHelper*/)
856 {
857   return error( COMPERR_BAD_INPUT_MESH, "Mesh built on shape expected");
858 }
859
860 //=======================================================================
861 //function : IsApplicableToShape
862 //purpose  : Return true if the algorithm can mesh a given shape
863 //=======================================================================
864
865 bool SMESH_Algo::IsApplicableToShape(const TopoDS_Shape & shape, bool toCheckAll) const
866 {
867   return true;
868 }
869
870 //=======================================================================
871 //function : CancelCompute
872 //purpose  : Sets _computeCanceled to true. It's usage depends on
873 //  *        implementation of a particular mesher.
874 //=======================================================================
875
876 void SMESH_Algo::CancelCompute()
877 {
878   _computeCanceled = true;
879   _error = COMPERR_CANCELED;
880 }
881
882 //================================================================================
883 /*
884  * If possible, returns progress of computation [0.,1.]
885  */
886 //================================================================================
887
888 double SMESH_Algo::GetProgress() const
889 {
890   return _progress;
891 }
892
893 //================================================================================
894 /*!
895  * \brief store error and comment and then return ( error == COMPERR_OK )
896  */
897 //================================================================================
898
899 bool SMESH_Algo::error(int error, const SMESH_Comment& comment)
900 {
901   _error   = error;
902   _comment = comment;
903   return ( error == COMPERR_OK );
904 }
905
906 //================================================================================
907 /*!
908  * \brief store error and return ( error == COMPERR_OK )
909  */
910 //================================================================================
911
912 bool SMESH_Algo::error(SMESH_ComputeErrorPtr error)
913 {
914   if ( error ) {
915     _error   = error->myName;
916     _comment = error->myComment;
917     _badInputElements = error->myBadElements;
918     return error->IsOK();
919   }
920   return true;
921 }
922
923 //================================================================================
924 /*!
925  * \brief return compute error
926  */
927 //================================================================================
928
929 SMESH_ComputeErrorPtr SMESH_Algo::GetComputeError() const
930 {
931   SMESH_ComputeErrorPtr err = SMESH_ComputeError::New( _error, _comment, this );
932   // hope this method is called by only SMESH_subMesh after this->Compute()
933   err->myBadElements.splice( err->myBadElements.end(),
934                              (list<const SMDS_MeshElement*>&) _badInputElements );
935   return err;
936 }
937
938 //================================================================================
939 /*!
940  * \brief initialize compute error before call of Compute()
941  */
942 //================================================================================
943
944 void SMESH_Algo::InitComputeError()
945 {
946   _error = COMPERR_OK;
947   _comment.clear();
948   list<const SMDS_MeshElement*>::iterator elem = _badInputElements.begin();
949   for ( ; elem != _badInputElements.end(); ++elem )
950     if ( (*elem)->GetID() < 1 )
951       delete *elem;
952   _badInputElements.clear();
953
954   _computeCanceled = false;
955   _progressTic     = 0;
956   _progress        = 0.;
957 }
958
959 //================================================================================
960 /*!
961  * \brief Return compute progress by nb of calls of this method
962  */
963 //================================================================================
964
965 double SMESH_Algo::GetProgressByTic() const
966 {
967   int computeCost = 0;
968   for ( size_t i = 0; i < _smToCompute.size(); ++i )
969     computeCost += _smToCompute[i]->GetComputeCost();
970
971   const_cast<SMESH_Algo*>( this )->_progressTic++;
972
973   double x = 5 * _progressTic;
974   x = ( x < computeCost ) ? ( x / computeCost ) : 1.;
975   return 0.9 * sin( x * M_PI / 2 );
976 }
977
978 //================================================================================
979 /*!
980  * \brief store a bad input element preventing computation,
981  *        which may be a temporary one i.e. not residing the mesh,
982  *        then it will be deleted by InitComputeError()
983  */
984 //================================================================================
985
986 void SMESH_Algo::addBadInputElement(const SMDS_MeshElement* elem)
987 {
988   if ( elem )
989     _badInputElements.push_back( elem );
990 }
991
992 //=======================================================================
993 //function : addBadInputElements
994 //purpose  : store a bad input elements or nodes preventing computation
995 //=======================================================================
996
997 void SMESH_Algo::addBadInputElements(const SMESHDS_SubMesh* sm,
998                                      const bool             addNodes)
999 {
1000   if ( sm )
1001   {
1002     if ( addNodes )
1003     {
1004       SMDS_NodeIteratorPtr nIt = sm->GetNodes();
1005       while ( nIt->more() ) addBadInputElement( nIt->next() );
1006     }
1007     else
1008     {
1009       SMDS_ElemIteratorPtr eIt = sm->GetElements();
1010       while ( eIt->more() ) addBadInputElement( eIt->next() );
1011     }
1012   }
1013 }
1014
1015 //=============================================================================
1016 /*!
1017  *  
1018  */
1019 //=============================================================================
1020
1021 // int SMESH_Algo::NumberOfWires(const TopoDS_Shape& S)
1022 // {
1023 //   int i = 0;
1024 //   for (TopExp_Explorer exp(S,TopAbs_WIRE); exp.More(); exp.Next())
1025 //     i++;
1026 //   return i;
1027 // }
1028
1029 //=============================================================================
1030 /*!
1031  *  
1032  */
1033 //=============================================================================
1034
1035 int SMESH_Algo::NumberOfPoints(SMESH_Mesh& aMesh, const TopoDS_Wire& W)
1036 {
1037   int nbPoints = 0;
1038   for (TopExp_Explorer exp(W,TopAbs_EDGE); exp.More(); exp.Next()) {
1039     const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
1040     int nb = aMesh.GetSubMesh(E)->GetSubMeshDS()->NbNodes();
1041     if(_quadraticMesh)
1042       nb = nb/2;
1043     nbPoints += nb + 1; // internal points plus 1 vertex of 2 (last point ?)
1044   }
1045   return nbPoints;
1046 }
1047
1048
1049 //================================================================================
1050 /*!
1051  * Method in which an algorithm generating a structured mesh
1052  * fixes positions of in-face nodes after there movement
1053  * due to insertion of viscous layers.
1054  */
1055 //================================================================================
1056
1057 bool SMESH_2D_Algo::FixInternalNodes(const SMESH_ProxyMesh& mesh,
1058                                      const TopoDS_Face&     face)
1059 {
1060   const SMESHDS_SubMesh* smDS = mesh.GetSubMesh(face);
1061   if ( !smDS || smDS->NbElements() < 1 )
1062     return false;
1063
1064   SMESH_MesherHelper helper( *mesh.GetMesh() );
1065
1066   // get all faces from a proxy sub-mesh
1067   typedef SMDS_StdIterator< const SMDS_MeshElement*, SMDS_ElemIteratorPtr > TIterator;
1068   TIDSortedElemSet allFaces( TIterator( smDS->GetElements() ), TIterator() );
1069   TIDSortedElemSet avoidSet, firstRowQuads;
1070
1071   // indices of nodes to pass to a neighbour quad using SMESH_MeshAlgos::FindFaceInSet()
1072   int iN1, iN2;
1073
1074   // get two first rows of nodes by passing through the first row of faces
1075   vector< vector< const SMDS_MeshNode* > > nodeRows;
1076   int iRow1 = 0, iRow2 = 1;
1077   const SMDS_MeshElement* quad;
1078   {
1079     // look for a corner quadrangle and it's corner node
1080     const SMDS_MeshElement* cornerQuad = 0;
1081     int                     cornerNodeInd = -1;
1082     SMDS_ElemIteratorPtr fIt = smDS->GetElements();
1083     while ( !cornerQuad && fIt->more() )
1084     {
1085       cornerQuad = fIt->next();
1086       if ( cornerQuad->NbCornerNodes() != 4 )
1087         return false;
1088       SMDS_NodeIteratorPtr nIt = cornerQuad->nodeIterator();
1089       for ( int i = 0; i < 4; ++i )
1090       {
1091         int nbInverseQuads = 0;
1092         SMDS_ElemIteratorPtr fIt = nIt->next()->GetInverseElementIterator(SMDSAbs_Face);
1093         while ( fIt->more() )
1094           nbInverseQuads += allFaces.count( fIt->next() );
1095         if ( nbInverseQuads == 1 )
1096           cornerNodeInd = i, i = 4;
1097       }
1098       if ( cornerNodeInd < 0 )
1099         cornerQuad = 0;
1100     }
1101     if ( !cornerQuad || cornerNodeInd < 0 )
1102       return false;
1103
1104     iN1     = helper.WrapIndex( cornerNodeInd + 1, 4 );
1105     iN2     = helper.WrapIndex( cornerNodeInd + 2, 4 );
1106     int iN3 = helper.WrapIndex( cornerNodeInd + 3, 4 );
1107     nodeRows.resize(2);
1108     nodeRows[iRow1].push_back( cornerQuad->GetNode( cornerNodeInd ));
1109     nodeRows[iRow1].push_back( cornerQuad->GetNode( iN1 ));
1110     nodeRows[iRow2].push_back( cornerQuad->GetNode( iN3 ));
1111     nodeRows[iRow2].push_back( cornerQuad->GetNode( iN2 ));
1112     firstRowQuads.insert( cornerQuad );
1113
1114     // pass through the rest quads in a face row
1115     quad = cornerQuad;
1116     while ( quad )
1117     {
1118       avoidSet.clear();
1119       avoidSet.insert( quad );
1120       if (( quad = SMESH_MeshAlgos::FindFaceInSet( nodeRows[iRow1].back(),
1121                                                    nodeRows[iRow2].back(),
1122                                                    allFaces, avoidSet, &iN1, &iN2)))
1123       {
1124         nodeRows[iRow1].push_back( quad->GetNode( helper.WrapIndex( iN2 + 2, 4 )));
1125         nodeRows[iRow2].push_back( quad->GetNode( helper.WrapIndex( iN1 + 2, 4 )));
1126         if ( quad->NbCornerNodes() != 4 )
1127           return false;
1128       }
1129     }
1130     if ( nodeRows[iRow1].size() < 3 )
1131       return true; // there is nothing to fix
1132   }
1133
1134   nodeRows.reserve( smDS->NbElements() / nodeRows[iRow1].size() );
1135
1136   // get the rest node rows
1137   while ( true )
1138   {
1139     ++iRow1, ++iRow2;
1140
1141     // get the first quad in the next face row 
1142     if (( quad = SMESH_MeshAlgos::FindFaceInSet( nodeRows[iRow1][0],
1143                                                  nodeRows[iRow1][1],
1144                                                  allFaces, /*avoid=*/firstRowQuads,
1145                                                  &iN1, &iN2)))
1146     {
1147       if ( quad->NbCornerNodes() != 4 )
1148         return false;
1149       nodeRows.resize( iRow2+1 );
1150       nodeRows[iRow2].push_back( quad->GetNode( helper.WrapIndex( iN2 + 2, 4 )));
1151       nodeRows[iRow2].push_back( quad->GetNode( helper.WrapIndex( iN1 + 2, 4 )));
1152       firstRowQuads.insert( quad );
1153     }
1154     else
1155     {
1156       break; // no more rows
1157     }
1158
1159     // pass through the rest quads in a face row
1160     while ( quad )
1161     {
1162       avoidSet.clear();
1163       avoidSet.insert( quad );
1164       if (( quad = SMESH_MeshAlgos::FindFaceInSet( nodeRows[iRow1][ nodeRows[iRow2].size()-1 ],
1165                                                    nodeRows[iRow2].back(),
1166                                                    allFaces, avoidSet, &iN1, &iN2)))
1167       {
1168         if ( quad->NbCornerNodes() != 4 )
1169           return false;
1170         nodeRows[iRow2].push_back( quad->GetNode( helper.WrapIndex( iN1 + 2, 4 )));
1171       }
1172     }
1173     if ( nodeRows[iRow1].size() != nodeRows[iRow2].size() )
1174       return false;
1175   }
1176   if ( nodeRows.size() < 3 )
1177     return true; // there is nothing to fix
1178
1179   // get params of the first (bottom) and last (top) node rows
1180   UVPtStructVec uvB( nodeRows[0].size() ), uvT( nodeRows[0].size() );
1181   for ( int isBot = 0; isBot < 2; ++isBot )
1182   {
1183     UVPtStructVec &                  uvps = isBot ? uvB : uvT;
1184     vector< const SMDS_MeshNode* >& nodes = nodeRows[ isBot ? 0 : nodeRows.size()-1 ];
1185     for ( size_t i = 0; i < nodes.size(); ++i )
1186     {
1187       uvps[i].node = nodes[i];
1188       gp_XY uv = helper.GetNodeUV( face, uvps[i].node );
1189       uvps[i].u = uv.Coord(1);
1190       uvps[i].v = uv.Coord(2);
1191       uvps[i].x = 0;
1192     }
1193     // calculate x (normalized param)
1194     for ( size_t i = 1; i < nodes.size(); ++i )
1195       uvps[i].x = uvps[i-1].x + SMESH_TNodeXYZ( uvps[i-1].node ).Distance( uvps[i].node );
1196     for ( size_t i = 1; i < nodes.size(); ++i )
1197       uvps[i].x /= uvps.back().x;
1198   }
1199
1200   // get params of the left and right node rows
1201   UVPtStructVec uvL( nodeRows.size() ), uvR( nodeRows.size() );
1202   for ( int isLeft = 0; isLeft < 2; ++isLeft )
1203   {
1204     UVPtStructVec & uvps = isLeft ? uvL : uvR;
1205     const int       iCol = isLeft ? 0 : nodeRows[0].size() - 1;
1206     for ( size_t i = 0; i < nodeRows.size(); ++i )
1207     {
1208       uvps[i].node = nodeRows[i][iCol];
1209       gp_XY uv = helper.GetNodeUV( face, uvps[i].node );
1210       uvps[i].u = uv.Coord(1);
1211       uvps[i].v = uv.Coord(2);
1212       uvps[i].y = 0;
1213     }
1214     // calculate y (normalized param)
1215     for ( size_t i = 1; i < nodeRows.size(); ++i )
1216       uvps[i].y = uvps[i-1].y + SMESH_TNodeXYZ( uvps[i-1].node ).Distance( uvps[i].node );
1217     for ( size_t i = 1; i < nodeRows.size(); ++i )
1218       uvps[i].y /= uvps.back().y;
1219   }
1220
1221   // update node coordinates
1222   SMESHDS_Mesh*   meshDS = mesh.GetMeshDS();
1223   Handle(Geom_Surface) S = BRep_Tool::Surface( face );
1224   gp_XY a0 ( uvB.front().u, uvB.front().v );
1225   gp_XY a1 ( uvB.back().u,  uvB.back().v );
1226   gp_XY a2 ( uvT.back().u,  uvT.back().v );
1227   gp_XY a3 ( uvT.front().u, uvT.front().v );
1228   for ( size_t iRow = 1; iRow < nodeRows.size()-1; ++iRow )
1229   {
1230     gp_XY p1 ( uvR[ iRow ].u, uvR[ iRow ].v );
1231     gp_XY p3 ( uvL[ iRow ].u, uvL[ iRow ].v );
1232     const double y0 = uvL[ iRow ].y;
1233     const double y1 = uvR[ iRow ].y;
1234     for ( size_t iCol = 1; iCol < nodeRows[0].size()-1; ++iCol )
1235     {
1236       gp_XY p0 ( uvB[ iCol ].u, uvB[ iCol ].v );
1237       gp_XY p2 ( uvT[ iCol ].u, uvT[ iCol ].v );
1238       const double x0 = uvB[ iCol ].x;
1239       const double x1 = uvT[ iCol ].x;
1240       double x = (x0 + y0 * (x1 - x0)) / (1 - (y1 - y0) * (x1 - x0));
1241       double y = y0 + x * (y1 - y0);
1242       gp_XY uv = helper.calcTFI( x, y, a0,a1,a2,a3, p0,p1,p2,p3 );
1243       gp_Pnt p = S->Value( uv.Coord(1), uv.Coord(2));
1244       const SMDS_MeshNode* n = nodeRows[iRow][iCol];
1245       meshDS->MoveNode( n, p.X(), p.Y(), p.Z() );
1246       if ( SMDS_FacePosition* pos = dynamic_cast< SMDS_FacePosition*>( n->GetPosition() ))
1247         pos->SetParameters( uv.Coord(1), uv.Coord(2) );
1248     }
1249   }
1250   return true;
1251 }