Salome HOME
#16843 EDF 19340 - wrong quadratic mesh
[modules/smesh.git] / src / SMESH / SMESH_Algo.cxx
1 // Copyright (C) 2007-2019  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 : implementation 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 higher 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, SMESH_Gen * gen)
177   : SMESH_Hypothesis(hypId, 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, SMESH_Gen* gen)
206   : SMESH_Algo(hypId, gen)
207 {
208   _shapeType = (1 << TopAbs_VERTEX);
209   _type = ALGO_0D;
210 }
211 SMESH_1D_Algo::SMESH_1D_Algo(int hypId, SMESH_Gen* gen)
212   : SMESH_Algo(hypId, gen)
213 {
214   _shapeType = (1 << TopAbs_EDGE);
215   _type = ALGO_1D;
216 }
217 SMESH_2D_Algo::SMESH_2D_Algo(int hypId, SMESH_Gen* gen)
218   : SMESH_Algo(hypId, gen)
219 {
220   _shapeType = (1 << TopAbs_FACE);
221   _type = ALGO_2D;
222 }
223 SMESH_3D_Algo::SMESH_3D_Algo(int hypId, SMESH_Gen* gen)
224   : SMESH_Algo(hypId, 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
267   std::list<const SMESHDS_Hypothesis *> savedHyps; // don't delete the list if 
268   savedHyps.swap( me->_usedHypList );              // it does not change (#16578)
269
270   me->_usedHypList.clear();
271   if ( const SMESH_HypoFilter* filter = GetCompatibleHypoFilter( ignoreAuxiliary ))
272   {
273     aMesh.GetHypotheses( aShape, *filter, me->_usedHypList, true );
274     if ( ignoreAuxiliary && _usedHypList.size() > 1 )
275       me->_usedHypList.clear(); //only one compatible hypothesis allowed
276   }
277   if ( _usedHypList == savedHyps )
278     savedHyps.swap( me->_usedHypList );
279
280   return _usedHypList;
281 }
282
283 //=============================================================================
284 /*!
285  *  List the relevant hypothesis associated to the shape. Relevant hypothesis
286  *  have a name (type) listed in the algorithm. Hypothesis associated to
287  *  father shape -are not- taken into account (see GetUsedHypothesis)
288  */
289 //=============================================================================
290
291 const list<const SMESHDS_Hypothesis *> &
292 SMESH_Algo::GetAppliedHypothesis(SMESH_Mesh &         aMesh,
293                                  const TopoDS_Shape & aShape,
294                                  const bool           ignoreAuxiliary) const
295 {
296   SMESH_Algo* me = const_cast< SMESH_Algo* >( this );
297
298   std::list<const SMESHDS_Hypothesis *> savedHyps; // don't delete the list if 
299   savedHyps.swap( me->_appliedHypList );           // it does not change (#16578)
300
301   me->_appliedHypList.clear();
302   if ( const SMESH_HypoFilter* filter = GetCompatibleHypoFilter( ignoreAuxiliary ))
303     aMesh.GetHypotheses( aShape, *filter, me->_appliedHypList, false );
304
305   if ( _appliedHypList == savedHyps )
306     savedHyps.swap( me->_appliedHypList );
307
308   return _appliedHypList;
309 }
310
311 //=============================================================================
312 /*!
313  *  Compute length of an edge
314  */
315 //=============================================================================
316
317 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
318 {
319   double UMin = 0, UMax = 0;
320   TopLoc_Location L;
321   Handle(Geom_Curve) C = BRep_Tool::Curve(E, L, UMin, UMax);
322   if ( C.IsNull() )
323     return 0.;
324   GeomAdaptor_Curve AdaptCurve(C, UMin, UMax); //range is important for periodic curves
325   double length = GCPnts_AbscissaPoint::Length(AdaptCurve, UMin, UMax);
326   return length;
327 }
328
329 //================================================================================
330 /*!
331  * \brief Just return false as the algorithm does not hold parameters values
332  */
333 //================================================================================
334
335 bool SMESH_Algo::SetParametersByMesh(const SMESH_Mesh* /*theMesh*/,
336                                      const TopoDS_Shape& /*theShape*/)
337 {
338   return false;
339 }
340 bool SMESH_Algo::SetParametersByDefaults(const TDefaults& , const SMESH_Mesh*)
341 {
342   return false;
343 }
344 //================================================================================
345 /*!
346  * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
347  * \param theMesh - The mesh containing nodes
348  * \param theEdge - The geometrical edge of interest
349  * \param theParams - The resulting vector of sorted node parameters
350  * \retval bool - false if not all parameters are OK
351  */
352 //================================================================================
353
354 bool SMESH_Algo::GetNodeParamOnEdge(const SMESHDS_Mesh* theMesh,
355                                     const TopoDS_Edge&  theEdge,
356                                     vector< double > &  theParams)
357 {
358   theParams.clear();
359
360   if ( !theMesh || theEdge.IsNull() )
361     return false;
362
363   SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
364   if ( !eSubMesh || !eSubMesh->GetElements()->more() )
365     return false; // edge is not meshed
366
367   //int nbEdgeNodes = 0;
368   set < double > paramSet;
369   if ( eSubMesh )
370   {
371     // loop on nodes of an edge: sort them by param on edge
372     SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
373     while ( nIt->more() )
374     {
375       SMDS_EdgePositionPtr epos = nIt->next()->GetPosition();
376       if ( !epos )
377         return false;
378       if ( !paramSet.insert( epos->GetUParameter() ).second )
379         return false; // equal parameters
380     }
381   }
382   // add vertex nodes params
383   TopoDS_Vertex V1,V2;
384   TopExp::Vertices( theEdge, V1, V2);
385   if ( VertexNode( V1, theMesh ) &&
386        !paramSet.insert( BRep_Tool::Parameter(V1,theEdge) ).second )
387     return false; // there are equal parameters
388   if ( VertexNode( V2, theMesh ) &&
389        !paramSet.insert( BRep_Tool::Parameter(V2,theEdge) ).second )
390     return false; // there are equal parameters
391
392   // fill the vector
393   theParams.resize( paramSet.size() );
394   set < double >::iterator   par    = paramSet.begin();
395   vector< double >::iterator vecPar = theParams.begin();
396   for ( ; par != paramSet.end(); ++par, ++vecPar )
397     *vecPar = *par;
398
399   return theParams.size() > 1;
400 }
401
402 //================================================================================
403 /*!
404  * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
405  * \param theMesh - The mesh containing nodes
406  * \param theEdge - The geometrical edge of interest
407  * \param theParams - The resulting vector of sorted node parameters
408  * \retval bool - false if not all parameters are OK
409  */
410 //================================================================================
411
412 bool SMESH_Algo::GetSortedNodesOnEdge(const SMESHDS_Mesh*                   theMesh,
413                                       const TopoDS_Edge&                    theEdge,
414                                       const bool                            ignoreMediumNodes,
415                                       map< double, const SMDS_MeshNode* > & theNodes,
416                                       const SMDSAbs_ElementType             typeToCheck)
417 {
418   theNodes.clear();
419
420   if ( !theMesh || theEdge.IsNull() )
421     return false;
422
423   SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
424   if ( !eSubMesh || ( eSubMesh->NbElements() == 0 && eSubMesh->NbNodes() == 0))
425     return false; // edge is not meshed
426
427   int nbNodes = 0;
428   set < double > paramSet;
429   if ( eSubMesh )
430   {
431     // loop on nodes of an edge: sort them by param on edge
432     SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
433     while ( nIt->more() )
434     {
435       const SMDS_MeshNode* node = nIt->next();
436       if ( ignoreMediumNodes && SMESH_MesherHelper::IsMedium( node, typeToCheck ))
437         continue;
438       SMDS_EdgePositionPtr epos = node->GetPosition();
439       if ( ! epos )
440         return false;
441       theNodes.insert( theNodes.end(), make_pair( epos->GetUParameter(), node ));
442       ++nbNodes;
443     }
444   }
445   // add vertex nodes
446   TopoDS_Vertex v1, v2;
447   TopExp::Vertices(theEdge, v1, v2);
448   const SMDS_MeshNode* n1 = VertexNode( v1, eSubMesh, 0 );
449   const SMDS_MeshNode* n2 = VertexNode( v2, eSubMesh, 0 );
450   const SMDS_MeshNode* nEnd[2] = { nbNodes ? theNodes.begin()->second  : 0,
451                                    nbNodes ? theNodes.rbegin()->second : 0 };
452   Standard_Real f, l;
453   BRep_Tool::Range(theEdge, f, l);
454   if ( v1.Orientation() != TopAbs_FORWARD )
455     std::swap( f, l );
456   if ( n1 && n1 != nEnd[0] && n1 != nEnd[1] && ++nbNodes )
457     theNodes.insert( make_pair( f, n1 ));
458   if ( n2 && n2 != nEnd[0] && n2 != nEnd[1] && ++nbNodes )
459     theNodes.insert( make_pair( l, n2 ));
460
461   return (int)theNodes.size() == nbNodes;
462 }
463
464 //================================================================================
465 /*!
466  * \brief Returns the filter recognizing only compatible hypotheses
467  *  \param ignoreAuxiliary - make filter ignore auxiliary hypotheses
468  *  \retval SMESH_HypoFilter* - the filter that can be NULL
469  */
470 //================================================================================
471
472 const SMESH_HypoFilter*
473 SMESH_Algo::GetCompatibleHypoFilter(const bool ignoreAuxiliary) const
474 {
475   if ( !_compatibleHypothesis.empty() )
476   {
477     if ( !_compatibleAllHypFilter )
478     {
479       SMESH_HypoFilter* filter = new SMESH_HypoFilter();
480       filter->Init( filter->HasName( _compatibleHypothesis[0] ));
481       for ( size_t i = 1; i < _compatibleHypothesis.size(); ++i )
482         filter->Or( filter->HasName( _compatibleHypothesis[ i ] ));
483
484       SMESH_HypoFilter* filterNoAux = new SMESH_HypoFilter( filter );
485       filterNoAux->AndNot( filterNoAux->IsAuxiliary() );
486
487       // _compatibleNoAuxHypFilter will detele _compatibleAllHypFilter!!!
488       SMESH_Algo* me = const_cast< SMESH_Algo* >( this );
489       me->_compatibleAllHypFilter   = filter;
490       me->_compatibleNoAuxHypFilter = filterNoAux;
491     }
492     return ignoreAuxiliary ? _compatibleNoAuxHypFilter : _compatibleAllHypFilter;
493   }
494   return 0;
495 }
496
497 //================================================================================
498 /*!
499  * \brief Return continuity of two edges
500  * \param E1 - the 1st edge
501  * \param E2 - the 2nd edge
502  * \retval GeomAbs_Shape - regularity at the junction between E1 and E2
503  */
504 //================================================================================
505
506 GeomAbs_Shape SMESH_Algo::Continuity(const TopoDS_Edge& theE1,
507                                      const TopoDS_Edge& theE2)
508 {
509   // avoid pb with internal edges
510   TopoDS_Edge E1 = theE1, E2 = theE2;
511   if (E1.Orientation() > TopAbs_REVERSED) // INTERNAL
512     E1.Orientation( TopAbs_FORWARD );
513   if (E2.Orientation() > TopAbs_REVERSED) // INTERNAL
514     E2.Orientation( TopAbs_FORWARD );
515
516   TopoDS_Vertex V, VV1[2], VV2[2];
517   TopExp::Vertices( E1, VV1[0], VV1[1], true );
518   TopExp::Vertices( E2, VV2[0], VV2[1], true );
519   if      ( VV1[1].IsSame( VV2[0] ))  { V = VV1[1]; }
520   else if ( VV1[0].IsSame( VV2[1] ))  { V = VV1[0]; }
521   else if ( VV1[1].IsSame( VV2[1] ))  { V = VV1[1]; E1.Reverse(); }
522   else if ( VV1[0].IsSame( VV2[0] ))  { V = VV1[0]; E1.Reverse(); }
523   else { return GeomAbs_C0; }
524
525   Standard_Real u1 = BRep_Tool::Parameter( V, E1 );
526   Standard_Real u2 = BRep_Tool::Parameter( V, E2 );
527   BRepAdaptor_Curve C1( E1 ), C2( E2 );
528   Standard_Real tol = BRep_Tool::Tolerance( V );
529   Standard_Real angTol = 2e-3;
530   try {
531     OCC_CATCH_SIGNALS;
532     return BRepLProp::Continuity(C1, C2, u1, u2, tol, angTol);
533   }
534   catch (Standard_Failure) {
535   }
536   return GeomAbs_C0;
537 }
538
539 //================================================================================
540 /*!
541  * \brief Return true if an edge can be considered straight
542  */
543 //================================================================================
544
545 bool SMESH_Algo::IsStraight( const TopoDS_Edge & E,
546                              const bool          degenResult)
547 {
548   {
549     double f,l;
550     if ( BRep_Tool::Curve( E, f, l ).IsNull())
551       return degenResult;
552   }
553   BRepAdaptor_Curve curve( E );
554   switch( curve.GetType() )
555   {
556   case GeomAbs_Line:
557     return true;
558   case GeomAbs_Circle:
559   case GeomAbs_Ellipse:
560   case GeomAbs_Hyperbola:
561   case GeomAbs_Parabola:
562     return false;
563     // case GeomAbs_BezierCurve:
564     // case GeomAbs_BSplineCurve:
565     // case GeomAbs_OtherCurve:
566   default:;
567   }
568
569   // evaluate how far from a straight line connecting the curve ends
570   // stand internal points of the curve
571   double  f = curve.FirstParameter();
572   double  l = curve.LastParameter();
573   gp_Pnt pf = curve.Value( f );
574   gp_Pnt pl = curve.Value( l );
575   gp_Vec lineVec( pf, pl );
576   double lineLen2 = lineVec.SquareMagnitude();
577   if ( lineLen2 < std::numeric_limits< double >::min() )
578     return false; // E seems closed
579
580   double edgeTol = 10 * curve.Tolerance();
581   double lenTol2 = lineLen2 * 1e-4; 
582   double tol2 = Min( edgeTol * edgeTol, lenTol2 );
583
584   const double nbSamples = 7;
585   for ( int i = 0; i < nbSamples; ++i )
586   {
587     double  r = ( i + 1 ) / nbSamples;
588     gp_Pnt pi = curve.Value( f * r + l * ( 1 - r ));
589     gp_Vec vi( pf, pi );
590     double h2 = lineVec.Crossed( vi ).SquareMagnitude() / lineLen2;
591     if ( h2 > tol2 )
592       return false;
593   }
594   return true;
595 }
596
597 //================================================================================
598 /*!
599  * \brief Return true if an edge has no 3D curve
600  */
601 //================================================================================
602
603 bool SMESH_Algo::isDegenerated( const TopoDS_Edge & E, const bool checkLength )
604 {
605   if ( checkLength )
606     return EdgeLength( E ) == 0;
607   double f,l;
608   TopLoc_Location loc;
609   Handle(Geom_Curve) C = BRep_Tool::Curve( E, loc, f,l );
610   return C.IsNull();
611 }
612
613 //================================================================================
614 /*!
615  * \brief Return the node built on a vertex
616  * \param V - the vertex
617  * \param meshDS - mesh
618  * \retval const SMDS_MeshNode* - found node or NULL
619  * \sa SMESH_MesherHelper::GetSubShapeByNode( const SMDS_MeshNode*, SMESHDS_Mesh* )
620  */
621 //================================================================================
622
623 const SMDS_MeshNode* SMESH_Algo::VertexNode(const TopoDS_Vertex& V,
624                                             const SMESHDS_Mesh*  meshDS)
625 {
626   if ( SMESHDS_SubMesh* sm = meshDS->MeshElements(V) ) {
627     SMDS_NodeIteratorPtr nIt= sm->GetNodes();
628     if (nIt->more())
629       return nIt->next();
630   }
631   return 0;
632 }
633
634 //=======================================================================
635 /*!
636  * \brief Return the node built on a vertex.
637  *        A node moved to other geometry by MergeNodes() is also returned.
638  * \param V - the vertex
639  * \param mesh - mesh
640  * \retval const SMDS_MeshNode* - found node or NULL
641  */
642 //=======================================================================
643
644 const SMDS_MeshNode* SMESH_Algo::VertexNode(const TopoDS_Vertex& V,
645                                             const SMESH_Mesh*    mesh)
646 {
647   const SMDS_MeshNode* node = VertexNode( V, mesh->GetMeshDS() );
648
649   if ( !node && mesh->HasModificationsToDiscard() )
650   {
651     PShapeIteratorPtr edgeIt = SMESH_MesherHelper::GetAncestors( V, *mesh, TopAbs_EDGE );
652     while ( const TopoDS_Shape* edge = edgeIt->next() )
653       if ( SMESHDS_SubMesh* edgeSM = mesh->GetMeshDS()->MeshElements( *edge ))
654         if ( edgeSM->NbElements() > 0 )
655           return VertexNode( V, edgeSM, mesh, /*checkV=*/false );
656   }
657   return node;
658 }
659
660 //=======================================================================
661 /*!
662  * \brief Return the node built on a vertex.
663  *        A node moved to other geometry by MergeNodes() is also returned.
664  * \param V - the vertex
665  * \param edgeSM - sub-mesh of a meshed EDGE sharing the vertex
666  * \param checkV - if \c true, presence of a node on the vertex is checked
667  * \retval const SMDS_MeshNode* - found node or NULL
668  */
669 //=======================================================================
670
671 const SMDS_MeshNode* SMESH_Algo::VertexNode(const TopoDS_Vertex&   V,
672                                             const SMESHDS_SubMesh* edgeSM,
673                                             const SMESH_Mesh*      mesh,
674                                             const bool             checkV)
675 {
676   const SMDS_MeshNode* node = checkV ? VertexNode( V, edgeSM->GetParent() ) : 0;
677
678   if ( !node && edgeSM )
679   {
680     // find nodes not shared by mesh segments
681     typedef set< const SMDS_MeshNode* >                       TNodeSet;
682     typedef map< const SMDS_MeshNode*, const SMDS_MeshNode* > TNodeMap;
683     TNodeMap notSharedNodes;
684     TNodeSet otherShapeNodes;
685     vector< const SMDS_MeshNode* > segNodes(3);
686     SMDS_ElemIteratorPtr segIt = edgeSM->GetElements();
687     while ( segIt->more() )
688     {
689       const SMDS_MeshElement* seg = segIt->next();
690       if ( seg->GetType() != SMDSAbs_Edge )
691         return node;
692       segNodes.assign( seg->begin_nodes(), seg->end_nodes() );
693       for ( int i = 0; i < 2; ++i )
694       {
695         const SMDS_MeshNode* n1 = segNodes[i];
696         const SMDS_MeshNode* n2 = segNodes[1-i];
697         pair<TNodeMap::iterator, bool> it2new = notSharedNodes.insert( make_pair( n1, n2 ));
698         if ( !it2new.second ) // n encounters twice
699           notSharedNodes.erase( it2new.first );
700         if ( n1->getshapeId() != edgeSM->GetID() )
701           otherShapeNodes.insert( n1 );
702       }
703     }
704     if ( otherShapeNodes.size() == 1 && notSharedNodes.empty() ) // a closed EDGE
705       return *otherShapeNodes.begin();
706
707     if ( notSharedNodes.size() == 2 ) // two end nodes found
708     {
709       SMESHDS_Mesh*  meshDS = edgeSM->GetParent();
710       const TopoDS_Shape& E = meshDS->IndexToShape( edgeSM->GetID() );
711       if ( E.IsNull() || E.ShapeType() != TopAbs_EDGE )
712         return node;
713       const SMDS_MeshNode* n1 = notSharedNodes.begin ()->first;
714       const SMDS_MeshNode* n2 = notSharedNodes.rbegin()->first;
715       TopoDS_Shape S1 = SMESH_MesherHelper::GetSubShapeByNode( n1, meshDS );
716       if ( S1.ShapeType() == TopAbs_VERTEX && SMESH_MesherHelper::IsSubShape( S1, E ))
717         return n2;
718       TopoDS_Shape S2 = SMESH_MesherHelper::GetSubShapeByNode( n2, meshDS );
719       if ( S2.ShapeType() == TopAbs_VERTEX && SMESH_MesherHelper::IsSubShape( S2, E ))
720         return n1;
721       if ( edgeSM->NbElements() <= 2 || !mesh ) // one-two segments
722       {
723         gp_Pnt pV = BRep_Tool::Pnt( V );
724         double dist1 = pV.SquareDistance( SMESH_TNodeXYZ( n1 ));
725         double dist2 = pV.SquareDistance( SMESH_TNodeXYZ( n2 ));
726         return dist1 < dist2 ? n1 : n2;
727       }
728       if ( mesh )
729       {
730         SMESH_MesherHelper helper( const_cast<SMESH_Mesh&>( *mesh ));
731         const SMDS_MeshNode* n1i = notSharedNodes.begin ()->second;
732         const SMDS_MeshNode* n2i = notSharedNodes.rbegin()->second;
733         const TopoDS_Edge&  edge = TopoDS::Edge( E );
734         bool  posOK = true;
735         double pos1 = helper.GetNodeU( edge, n1i, n2i, &posOK );
736         double pos2 = helper.GetNodeU( edge, n2i, n1i, &posOK );
737         double posV = BRep_Tool::Parameter( V, edge );
738         if ( Abs( pos1 - posV ) < Abs( pos2 - posV )) return n1;
739         else                                          return n2;
740       }
741     }
742   }
743   return node;
744 }
745
746 //=======================================================================
747 //function : GetMeshError
748 //purpose  : Finds topological errors of a sub-mesh
749 //WARNING  : 1D check is NOT implemented so far
750 //=======================================================================
751
752 SMESH_Algo::EMeshError SMESH_Algo::GetMeshError(SMESH_subMesh* subMesh)
753 {
754   EMeshError err = MEr_OK;
755
756   SMESHDS_SubMesh* smDS = subMesh->GetSubMeshDS();
757   if ( !smDS )
758     return MEr_EMPTY;
759
760   switch ( subMesh->GetSubShape().ShapeType() )
761   {
762   case TopAbs_FACE: { // ====================== 2D =====================
763
764     SMDS_ElemIteratorPtr fIt = smDS->GetElements();
765     if ( !fIt->more() )
766       return MEr_EMPTY;
767
768     // We check that only links on EDGEs encounter once, the rest links, twice
769     set< SMESH_TLink > links;
770     while ( fIt->more() )
771     {
772       const SMDS_MeshElement* f = fIt->next();
773       int nbNodes = f->NbCornerNodes(); // ignore medium nodes
774       for ( int i = 0; i < nbNodes; ++i )
775       {
776         const SMDS_MeshNode* n1 = f->GetNode( i );
777         const SMDS_MeshNode* n2 = f->GetNode(( i+1 ) % nbNodes);
778         std::pair< set< SMESH_TLink >::iterator, bool > it_added =
779           links.insert( SMESH_TLink( n1, n2 ));
780         if ( !it_added.second )
781           // As we do NOT(!) check if mesh is manifold, we believe that a link can
782           // encounter once or twice only (not three times), we erase a link as soon
783           // as it encounters twice to speed up search in the <links> map.
784           links.erase( it_added.first );
785       }
786     }
787     // the links remaining in the <links> should all be on EDGE
788     set< SMESH_TLink >::iterator linkIt = links.begin();
789     for ( ; linkIt != links.end(); ++linkIt )
790     {
791       const SMESH_TLink& link = *linkIt;
792       if ( link.node1()->GetPosition()->GetTypeOfPosition() > SMDS_TOP_EDGE ||
793            link.node2()->GetPosition()->GetTypeOfPosition() > SMDS_TOP_EDGE )
794         return MEr_HOLES;
795     }
796     // TODO: to check orientation
797     break;
798   }
799   case TopAbs_SOLID: { // ====================== 3D =====================
800
801     SMDS_ElemIteratorPtr vIt = smDS->GetElements();
802     if ( !vIt->more() )
803       return MEr_EMPTY;
804
805     SMDS_VolumeTool vTool;
806     while ( !vIt->more() )
807     {
808       if (!vTool.Set( vIt->next() ))
809         continue; // strange
810
811       for ( int iF = 0; iF < vTool.NbFaces(); ++iF )
812         if ( vTool.IsFreeFace( iF ))
813         {
814           int nbN = vTool.NbFaceNodes( iF );
815           const SMDS_MeshNode** nodes =  vTool.GetFaceNodes( iF );
816           for ( int i = 0; i < nbN; ++i )
817             if ( nodes[i]->GetPosition()->GetTypeOfPosition() > SMDS_TOP_FACE )
818               return MEr_HOLES;
819         }
820     }
821     break;
822   }
823   default:;
824   }
825   return err;
826 }
827
828 //================================================================================
829 /*!
830  * \brief Sets event listener to submeshes if necessary
831  * \param subMesh - submesh where algo is set
832  * 
833  * After being set, event listener is notified on each event of a submesh.
834  * By default non listener is set
835  */
836 //================================================================================
837
838 void SMESH_Algo::SetEventListener(SMESH_subMesh* /*subMesh*/)
839 {
840 }
841
842 //================================================================================
843 /*!
844  * \brief Allow algo to do something after persistent restoration
845  * \param subMesh - restored submesh
846  *
847  * This method is called only if a submesh has HYP_OK algo_state.
848  */
849 //================================================================================
850
851 void SMESH_Algo::SubmeshRestored(SMESH_subMesh* /*subMesh*/)
852 {
853 }
854
855 //================================================================================
856 /*!
857  * \brief Computes mesh without geometry
858  * \param aMesh - the mesh
859  * \param aHelper - helper that must be used for adding elements to \aaMesh
860  * \retval bool - is a success
861  */
862 //================================================================================
863
864 bool SMESH_Algo::Compute(SMESH_Mesh & /*aMesh*/, SMESH_MesherHelper* /*aHelper*/)
865 {
866   return error( COMPERR_BAD_INPUT_MESH, "Mesh built on shape expected");
867 }
868
869 //=======================================================================
870 //function : IsApplicableToShape
871 //purpose  : Return true if the algorithm can mesh a given shape
872 //=======================================================================
873
874 bool SMESH_Algo::IsApplicableToShape(const TopoDS_Shape & shape, bool toCheckAll) const
875 {
876   return true;
877 }
878
879 //=======================================================================
880 //function : CancelCompute
881 //purpose  : Sets _computeCanceled to true. It's usage depends on
882 //  *        implementation of a particular mesher.
883 //=======================================================================
884
885 void SMESH_Algo::CancelCompute()
886 {
887   _computeCanceled = true;
888   _error = COMPERR_CANCELED;
889 }
890
891 //================================================================================
892 /*
893  * If possible, returns progress of computation [0.,1.]
894  */
895 //================================================================================
896
897 double SMESH_Algo::GetProgress() const
898 {
899   return _progress;
900 }
901
902 //================================================================================
903 /*!
904  * \brief store error and comment and then return ( error == COMPERR_OK )
905  */
906 //================================================================================
907
908 bool SMESH_Algo::error(int error, const SMESH_Comment& comment)
909 {
910   _error   = error;
911   _comment = comment;
912   return ( error == COMPERR_OK );
913 }
914
915 //================================================================================
916 /*!
917  * \brief store error and return ( error == COMPERR_OK )
918  */
919 //================================================================================
920
921 bool SMESH_Algo::error(SMESH_ComputeErrorPtr error)
922 {
923   if ( error ) {
924     _error   = error->myName;
925     _comment = error->myComment;
926     if ( error->HasBadElems() )
927     {
928       SMESH_BadInputElements* badElems = static_cast<SMESH_BadInputElements*>( error.get() );
929       _badInputElements = badElems->GetElements();
930       _mesh             = badElems->GetMesh();
931     }
932     return error->IsOK();
933   }
934   return true;
935 }
936
937 //================================================================================
938 /*!
939  * \brief return compute error
940  */
941 //================================================================================
942
943 SMESH_ComputeErrorPtr SMESH_Algo::GetComputeError() const
944 {
945   if ( !_badInputElements.empty() && _mesh )
946   {
947     SMESH_BadInputElements* err = new SMESH_BadInputElements( _mesh, _error, _comment, this );
948     // hope this method is called by only SMESH_subMesh after this->Compute()
949     err->myBadElements.splice( err->myBadElements.end(),
950                                (list<const SMDS_MeshElement*>&) _badInputElements );
951     return SMESH_ComputeErrorPtr( err );
952   }
953   return SMESH_ComputeError::New( _error, _comment, this );
954 }
955
956 //================================================================================
957 /*!
958  * \brief initialize compute error before call of Compute()
959  */
960 //================================================================================
961
962 void SMESH_Algo::InitComputeError()
963 {
964   _error = COMPERR_OK;
965   _comment.clear();
966   list<const SMDS_MeshElement*>::iterator elem = _badInputElements.begin();
967   for ( ; elem != _badInputElements.end(); ++elem )
968     if ( (*elem)->GetID() < 1 )
969       delete *elem;
970   _badInputElements.clear();
971   _mesh = 0;
972
973   _computeCanceled = false;
974   _progressTic     = 0;
975   _progress        = 0.;
976 }
977
978 //================================================================================
979 /*!
980  * \brief Return compute progress by nb of calls of this method
981  */
982 //================================================================================
983
984 double SMESH_Algo::GetProgressByTic() const
985 {
986   int computeCost = 0;
987   for ( size_t i = 0; i < _smToCompute.size(); ++i )
988     computeCost += _smToCompute[i]->GetComputeCost();
989
990   const_cast<SMESH_Algo*>( this )->_progressTic++;
991
992   double x = 5 * _progressTic;
993   x = ( x < computeCost ) ? ( x / computeCost ) : 1.;
994   return 0.9 * sin( x * M_PI / 2 );
995 }
996
997 //================================================================================
998 /*!
999  * \brief store a bad input element preventing computation,
1000  *        which may be a temporary one i.e. not residing the mesh,
1001  *        then it will be deleted by InitComputeError()
1002  */
1003 //================================================================================
1004
1005 void SMESH_Algo::addBadInputElement(const SMDS_MeshElement* elem)
1006 {
1007   if ( elem )
1008     _badInputElements.push_back( elem );
1009 }
1010
1011 //=======================================================================
1012 //function : addBadInputElements
1013 //purpose  : store a bad input elements or nodes preventing computation
1014 //=======================================================================
1015
1016 void SMESH_Algo::addBadInputElements(const SMESHDS_SubMesh* sm,
1017                                      const bool             addNodes)
1018 {
1019   if ( sm )
1020   {
1021     if ( addNodes )
1022     {
1023       SMDS_NodeIteratorPtr nIt = sm->GetNodes();
1024       while ( nIt->more() ) addBadInputElement( nIt->next() );
1025     }
1026     else
1027     {
1028       SMDS_ElemIteratorPtr eIt = sm->GetElements();
1029       while ( eIt->more() ) addBadInputElement( eIt->next() );
1030     }
1031   }
1032 }
1033
1034 //=============================================================================
1035 /*!
1036  *  
1037  */
1038 //=============================================================================
1039
1040 // int SMESH_Algo::NumberOfWires(const TopoDS_Shape& S)
1041 // {
1042 //   int i = 0;
1043 //   for (TopExp_Explorer exp(S,TopAbs_WIRE); exp.More(); exp.Next())
1044 //     i++;
1045 //   return i;
1046 // }
1047
1048 //=============================================================================
1049 /*!
1050  *  
1051  */
1052 //=============================================================================
1053
1054 int SMESH_Algo::NumberOfPoints(SMESH_Mesh& aMesh, const TopoDS_Wire& W)
1055 {
1056   int nbPoints = 0;
1057   for (TopExp_Explorer exp(W,TopAbs_EDGE); exp.More(); exp.Next()) {
1058     const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
1059     int nb = aMesh.GetSubMesh(E)->GetSubMeshDS()->NbNodes();
1060     if(_quadraticMesh)
1061       nb = nb/2;
1062     nbPoints += nb + 1; // internal points plus 1 vertex of 2 (last point ?)
1063   }
1064   return nbPoints;
1065 }
1066
1067
1068 //================================================================================
1069 /*!
1070  * Method in which an algorithm generating a structured mesh
1071  * fixes positions of in-face nodes after there movement
1072  * due to insertion of viscous layers.
1073  */
1074 //================================================================================
1075
1076 bool SMESH_2D_Algo::FixInternalNodes(const SMESH_ProxyMesh& mesh,
1077                                      const TopoDS_Face&     face)
1078 {
1079   const SMESHDS_SubMesh* smDS = mesh.GetSubMesh(face);
1080   if ( !smDS || smDS->NbElements() < 1 )
1081     return false;
1082
1083   SMESH_MesherHelper helper( *mesh.GetMesh() );
1084
1085   // get all faces from a proxy sub-mesh
1086   typedef SMDS_StdIterator< const SMDS_MeshElement*, SMDS_ElemIteratorPtr > TIterator;
1087   TIDSortedElemSet allFaces( TIterator( smDS->GetElements() ), TIterator() );
1088   TIDSortedElemSet avoidSet, firstRowQuads;
1089
1090   // indices of nodes to pass to a neighbour quad using SMESH_MeshAlgos::FindFaceInSet()
1091   int iN1, iN2;
1092
1093   // get two first rows of nodes by passing through the first row of faces
1094   vector< vector< const SMDS_MeshNode* > > nodeRows;
1095   int iRow1 = 0, iRow2 = 1;
1096   const SMDS_MeshElement* quad;
1097   {
1098     // look for a corner quadrangle and it's corner node
1099     const SMDS_MeshElement* cornerQuad = 0;
1100     int                     cornerNodeInd = -1;
1101     SMDS_ElemIteratorPtr fIt = smDS->GetElements();
1102     while ( !cornerQuad && fIt->more() )
1103     {
1104       cornerQuad = fIt->next();
1105       if ( cornerQuad->NbCornerNodes() != 4 )
1106         return false;
1107       SMDS_NodeIteratorPtr nIt = cornerQuad->nodeIterator();
1108       for ( int i = 0; i < 4; ++i )
1109       {
1110         int nbInverseQuads = 0;
1111         SMDS_ElemIteratorPtr fIt = nIt->next()->GetInverseElementIterator(SMDSAbs_Face);
1112         while ( fIt->more() )
1113           nbInverseQuads += allFaces.count( fIt->next() );
1114         if ( nbInverseQuads == 1 )
1115           cornerNodeInd = i, i = 4;
1116       }
1117       if ( cornerNodeInd < 0 )
1118         cornerQuad = 0;
1119     }
1120     if ( !cornerQuad || cornerNodeInd < 0 )
1121       return false;
1122
1123     iN1     = helper.WrapIndex( cornerNodeInd + 1, 4 );
1124     iN2     = helper.WrapIndex( cornerNodeInd + 2, 4 );
1125     int iN3 = helper.WrapIndex( cornerNodeInd + 3, 4 );
1126     nodeRows.resize(2);
1127     nodeRows[iRow1].push_back( cornerQuad->GetNode( cornerNodeInd ));
1128     nodeRows[iRow1].push_back( cornerQuad->GetNode( iN1 ));
1129     nodeRows[iRow2].push_back( cornerQuad->GetNode( iN3 ));
1130     nodeRows[iRow2].push_back( cornerQuad->GetNode( iN2 ));
1131     firstRowQuads.insert( cornerQuad );
1132
1133     // pass through the rest quads in a face row
1134     quad = cornerQuad;
1135     while ( quad )
1136     {
1137       avoidSet.clear();
1138       avoidSet.insert( quad );
1139       if (( quad = SMESH_MeshAlgos::FindFaceInSet( nodeRows[iRow1].back(),
1140                                                    nodeRows[iRow2].back(),
1141                                                    allFaces, avoidSet, &iN1, &iN2)))
1142       {
1143         nodeRows[iRow1].push_back( quad->GetNode( helper.WrapIndex( iN2 + 2, 4 )));
1144         nodeRows[iRow2].push_back( quad->GetNode( helper.WrapIndex( iN1 + 2, 4 )));
1145         if ( quad->NbCornerNodes() != 4 )
1146           return false;
1147       }
1148     }
1149     if ( nodeRows[iRow1].size() < 3 )
1150       return true; // there is nothing to fix
1151   }
1152
1153   nodeRows.reserve( smDS->NbElements() / nodeRows[iRow1].size() );
1154
1155   // get the rest node rows
1156   while ( true )
1157   {
1158     ++iRow1, ++iRow2;
1159
1160     // get the first quad in the next face row 
1161     if (( quad = SMESH_MeshAlgos::FindFaceInSet( nodeRows[iRow1][0],
1162                                                  nodeRows[iRow1][1],
1163                                                  allFaces, /*avoid=*/firstRowQuads,
1164                                                  &iN1, &iN2)))
1165     {
1166       if ( quad->NbCornerNodes() != 4 )
1167         return false;
1168       nodeRows.resize( iRow2+1 );
1169       nodeRows[iRow2].push_back( quad->GetNode( helper.WrapIndex( iN2 + 2, 4 )));
1170       nodeRows[iRow2].push_back( quad->GetNode( helper.WrapIndex( iN1 + 2, 4 )));
1171       firstRowQuads.insert( quad );
1172     }
1173     else
1174     {
1175       break; // no more rows
1176     }
1177
1178     // pass through the rest quads in a face row
1179     while ( quad )
1180     {
1181       avoidSet.clear();
1182       avoidSet.insert( quad );
1183       if (( quad = SMESH_MeshAlgos::FindFaceInSet( nodeRows[iRow1][ nodeRows[iRow2].size()-1 ],
1184                                                    nodeRows[iRow2].back(),
1185                                                    allFaces, avoidSet, &iN1, &iN2)))
1186       {
1187         if ( quad->NbCornerNodes() != 4 )
1188           return false;
1189         nodeRows[iRow2].push_back( quad->GetNode( helper.WrapIndex( iN1 + 2, 4 )));
1190       }
1191     }
1192     if ( nodeRows[iRow1].size() != nodeRows[iRow2].size() )
1193       return false;
1194   }
1195   if ( nodeRows.size() < 3 )
1196     return true; // there is nothing to fix
1197
1198   // get params of the first (bottom) and last (top) node rows
1199   UVPtStructVec uvB( nodeRows[0].size() ), uvT( nodeRows[0].size() );
1200   for ( int isBot = 0; isBot < 2; ++isBot )
1201   {
1202     UVPtStructVec &                  uvps = isBot ? uvB : uvT;
1203     vector< const SMDS_MeshNode* >& nodes = nodeRows[ isBot ? 0 : nodeRows.size()-1 ];
1204     for ( size_t i = 0; i < nodes.size(); ++i )
1205     {
1206       uvps[i].node = nodes[i];
1207       gp_XY uv = helper.GetNodeUV( face, uvps[i].node );
1208       uvps[i].u = uv.Coord(1);
1209       uvps[i].v = uv.Coord(2);
1210       uvps[i].x = 0;
1211     }
1212     // calculate x (normalized param)
1213     for ( size_t i = 1; i < nodes.size(); ++i )
1214       uvps[i].x = uvps[i-1].x + SMESH_TNodeXYZ( uvps[i-1].node ).Distance( uvps[i].node );
1215     for ( size_t i = 1; i < nodes.size(); ++i )
1216       uvps[i].x /= uvps.back().x;
1217   }
1218
1219   // get params of the left and right node rows
1220   UVPtStructVec uvL( nodeRows.size() ), uvR( nodeRows.size() );
1221   for ( int isLeft = 0; isLeft < 2; ++isLeft )
1222   {
1223     UVPtStructVec & uvps = isLeft ? uvL : uvR;
1224     const int       iCol = isLeft ? 0 : nodeRows[0].size() - 1;
1225     for ( size_t i = 0; i < nodeRows.size(); ++i )
1226     {
1227       uvps[i].node = nodeRows[i][iCol];
1228       gp_XY uv = helper.GetNodeUV( face, uvps[i].node );
1229       uvps[i].u = uv.Coord(1);
1230       uvps[i].v = uv.Coord(2);
1231       uvps[i].y = 0;
1232     }
1233     // calculate y (normalized param)
1234     for ( size_t i = 1; i < nodeRows.size(); ++i )
1235       uvps[i].y = uvps[i-1].y + SMESH_TNodeXYZ( uvps[i-1].node ).Distance( uvps[i].node );
1236     for ( size_t i = 1; i < nodeRows.size(); ++i )
1237       uvps[i].y /= uvps.back().y;
1238   }
1239
1240   // update node coordinates
1241   SMESHDS_Mesh*   meshDS = mesh.GetMeshDS();
1242   Handle(Geom_Surface) S = BRep_Tool::Surface( face );
1243   gp_XY a0 ( uvB.front().u, uvB.front().v );
1244   gp_XY a1 ( uvB.back().u,  uvB.back().v );
1245   gp_XY a2 ( uvT.back().u,  uvT.back().v );
1246   gp_XY a3 ( uvT.front().u, uvT.front().v );
1247   for ( size_t iRow = 1; iRow < nodeRows.size()-1; ++iRow )
1248   {
1249     gp_XY p1 ( uvR[ iRow ].u, uvR[ iRow ].v );
1250     gp_XY p3 ( uvL[ iRow ].u, uvL[ iRow ].v );
1251     const double y0 = uvL[ iRow ].y;
1252     const double y1 = uvR[ iRow ].y;
1253     for ( size_t iCol = 1; iCol < nodeRows[0].size()-1; ++iCol )
1254     {
1255       gp_XY p0 ( uvB[ iCol ].u, uvB[ iCol ].v );
1256       gp_XY p2 ( uvT[ iCol ].u, uvT[ iCol ].v );
1257       const double x0 = uvB[ iCol ].x;
1258       const double x1 = uvT[ iCol ].x;
1259       double x = (x0 + y0 * (x1 - x0)) / (1 - (y1 - y0) * (x1 - x0));
1260       double y = y0 + x * (y1 - y0);
1261       gp_XY uv = helper.calcTFI( x, y, a0,a1,a2,a3, p0,p1,p2,p3 );
1262       gp_Pnt p = S->Value( uv.Coord(1), uv.Coord(2));
1263       const SMDS_MeshNode* n = nodeRows[iRow][iCol];
1264       meshDS->MoveNode( n, p.X(), p.Y(), p.Z() );
1265       if ( SMDS_FacePositionPtr pos = n->GetPosition() )
1266         pos->SetParameters( uv.Coord(1), uv.Coord(2) );
1267     }
1268   }
1269   return true;
1270 }
1271
1272 //=======================================================================
1273 //function : IsApplicableToShape
1274 //purpose  : Return true if the algorithm can mesh a given shape
1275 //=======================================================================
1276
1277 bool SMESH_1D_Algo::IsApplicableToShape(const TopoDS_Shape & shape, bool toCheckAll) const
1278 {
1279   return ( !shape.IsNull() && TopExp_Explorer( shape, TopAbs_EDGE ).More() );
1280 }
1281
1282 //=======================================================================
1283 //function : IsApplicableToShape
1284 //purpose  : Return true if the algorithm can mesh a given shape
1285 //=======================================================================
1286
1287 bool SMESH_2D_Algo::IsApplicableToShape(const TopoDS_Shape & shape, bool toCheckAll) const
1288 {
1289   return ( !shape.IsNull() && TopExp_Explorer( shape, TopAbs_FACE ).More() );
1290 }
1291
1292 //=======================================================================
1293 //function : IsApplicableToShape
1294 //purpose  : Return true if the algorithm can mesh a given shape
1295 //=======================================================================
1296
1297 bool SMESH_3D_Algo::IsApplicableToShape(const TopoDS_Shape & shape, bool toCheckAll) const
1298 {
1299   return ( !shape.IsNull() && TopExp_Explorer( shape, TopAbs_SOLID ).More() );
1300 }