Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/smesh.git] / src / SMESH / SMESH_Algo.cxx
1 // Copyright (C) 2007-2012  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.
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_TypeDefs.hxx"
42
43 #include <Basics_OCCTVersion.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 <TopExp.hxx>
52 #include <TopLoc_Location.hxx>
53 #include <TopTools_ListIteratorOfListOfShape.hxx>
54 #include <TopTools_ListOfShape.hxx>
55 #include <TopoDS.hxx>
56 #include <TopoDS_Edge.hxx>
57 #include <TopoDS_Face.hxx>
58 #include <TopoDS_Vertex.hxx>
59 #include <gp_Pnt.hxx>
60 #include <gp_Pnt2d.hxx>
61 #include <gp_Vec.hxx>
62
63 #include <Standard_ErrorHandler.hxx>
64 #include <Standard_Failure.hxx>
65
66 #include "utilities.h"
67
68 #include <algorithm>
69 #include <limits>
70
71 using namespace std;
72
73 //=============================================================================
74 /*!
75  *  
76  */
77 //=============================================================================
78
79 SMESH_Algo::SMESH_Algo (int hypId, int studyId, SMESH_Gen * gen)
80   : SMESH_Hypothesis(hypId, studyId, gen)
81 {
82   gen->_mapAlgo[hypId] = this;
83
84   _onlyUnaryInput = _requireDiscreteBoundary = _requireShape = true;
85   _quadraticMesh = _supportSubmeshes = false;
86   _error = COMPERR_OK;
87 }
88
89 //=============================================================================
90 /*!
91  *  
92  */
93 //=============================================================================
94
95 SMESH_Algo::~SMESH_Algo()
96 {
97 }
98
99 //=============================================================================
100 /*!
101  * Usually an algoritm has nothing to save
102  */
103 //=============================================================================
104
105 ostream & SMESH_Algo::SaveTo(ostream & save) { return save; }
106 istream & SMESH_Algo::LoadFrom(istream & load) { return load; }
107
108 //=============================================================================
109 /*!
110  *  
111  */
112 //=============================================================================
113
114 const vector < string > &SMESH_Algo::GetCompatibleHypothesis()
115 {
116   return _compatibleHypothesis;
117 }
118
119 //=============================================================================
120 /*!
121  *  List the hypothesis used by the algorithm associated to the shape.
122  *  Hypothesis associated to father shape -are- taken into account (see
123  *  GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
124  *  the algorithm. This method could be surcharged by specific algorithms, in 
125  *  case of several hypothesis simultaneously applicable.
126  */
127 //=============================================================================
128
129 const list <const SMESHDS_Hypothesis *> &
130 SMESH_Algo::GetUsedHypothesis(SMESH_Mesh &         aMesh,
131                               const TopoDS_Shape & aShape,
132                               const bool           ignoreAuxiliary)
133 {
134   _usedHypList.clear();
135   SMESH_HypoFilter filter;
136   if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
137   {
138     aMesh.GetHypotheses( aShape, filter, _usedHypList, true );
139     if ( ignoreAuxiliary && _usedHypList.size() > 1 )
140       _usedHypList.clear(); //only one compatible hypothesis allowed
141   }
142   return _usedHypList;
143 }
144
145 //=============================================================================
146 /*!
147  *  List the relevant hypothesis associated to the shape. Relevant hypothesis
148  *  have a name (type) listed in the algorithm. Hypothesis associated to
149  *  father shape -are not- taken into account (see GetUsedHypothesis)
150  */
151 //=============================================================================
152
153 const list<const SMESHDS_Hypothesis *> &
154 SMESH_Algo::GetAppliedHypothesis(SMESH_Mesh &         aMesh,
155                                  const TopoDS_Shape & aShape,
156                                  const bool           ignoreAuxiliary)
157 {
158   _appliedHypList.clear();
159   SMESH_HypoFilter filter;
160   if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
161     aMesh.GetHypotheses( aShape, filter, _appliedHypList, false );
162
163   return _appliedHypList;
164 }
165
166 //=============================================================================
167 /*!
168  *  Compute length of an edge
169  */
170 //=============================================================================
171
172 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
173 {
174   double UMin = 0, UMax = 0;
175   if (BRep_Tool::Degenerated(E))
176     return 0;
177   TopLoc_Location L;
178   Handle(Geom_Curve) C = BRep_Tool::Curve(E, L, UMin, UMax);
179   GeomAdaptor_Curve AdaptCurve(C, UMin, UMax); //range is important for periodic curves
180   double length = GCPnts_AbscissaPoint::Length(AdaptCurve, UMin, UMax);
181   return length;
182 }
183
184 //================================================================================
185 /*!
186  * \brief Calculate normal of a mesh face
187  */
188 //================================================================================
189
190 bool SMESH_Algo::FaceNormal(const SMDS_MeshElement* F, gp_XYZ& normal, bool normalized)
191 {
192   if ( !F || F->GetType() != SMDSAbs_Face )
193     return false;
194
195   normal.SetCoord(0,0,0);
196   int nbNodes = F->IsQuadratic() ? F->NbNodes()/2 : F->NbNodes();
197   for ( int i = 0; i < nbNodes-2; ++i )
198   {
199     gp_XYZ p[3];
200     for ( int n = 0; n < 3; ++n )
201     {
202       const SMDS_MeshNode* node = F->GetNode( i + n );
203       p[n].SetCoord( node->X(), node->Y(), node->Z() );
204     }
205     normal += ( p[2] - p[1] ) ^ ( p[0] - p[1] );
206   }
207   double size2 = normal.SquareModulus();
208   bool ok = ( size2 > numeric_limits<double>::min() * numeric_limits<double>::min());
209   if ( normalized && ok )
210     normal /= sqrt( size2 );
211
212   return ok;
213 }
214
215 //================================================================================
216 /*!
217  * \brief Find out elements orientation on a geometrical face
218  * \param theFace - The face correctly oriented in the shape being meshed
219  * \param theMeshDS - The mesh data structure
220  * \retval bool - true if the face normal and the normal of first element
221  *                in the correspoding submesh point in different directions
222  */
223 //================================================================================
224
225 bool SMESH_Algo::IsReversedSubMesh (const TopoDS_Face&  theFace,
226                                     SMESHDS_Mesh*       theMeshDS)
227 {
228   if ( theFace.IsNull() || !theMeshDS )
229     return false;
230
231   // find out orientation of a meshed face
232   int faceID = theMeshDS->ShapeToIndex( theFace );
233   TopoDS_Shape aMeshedFace = theMeshDS->IndexToShape( faceID );
234   bool isReversed = ( theFace.Orientation() != aMeshedFace.Orientation() );
235
236   const SMESHDS_SubMesh * aSubMeshDSFace = theMeshDS->MeshElements( faceID );
237   if ( !aSubMeshDSFace )
238     return isReversed;
239
240   // find element with node located on face and get its normal
241   const SMDS_FacePosition* facePos = 0;
242   int vertexID = 0;
243   gp_Pnt nPnt[3];
244   gp_Vec Ne;
245   bool normalOK = false;
246   SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
247   while ( iteratorElem->more() ) // loop on elements on theFace
248   {
249     const SMDS_MeshElement* elem = iteratorElem->next();
250     if ( elem && elem->NbNodes() > 2 ) {
251       SMDS_ElemIteratorPtr nodesIt = elem->nodesIterator();
252       const SMDS_FacePosition* fPos = 0;
253       int i = 0, vID = 0;
254       while ( nodesIt->more() ) { // loop on nodes
255         const SMDS_MeshNode* node
256           = static_cast<const SMDS_MeshNode *>(nodesIt->next());
257         if ( i == 3 ) i = 2;
258         nPnt[ i++ ].SetCoord( node->X(), node->Y(), node->Z() );
259         // check position
260         const SMDS_PositionPtr& pos = node->GetPosition();
261         if ( !pos ) continue;
262         if ( pos->GetTypeOfPosition() == SMDS_TOP_FACE ) {
263           fPos = dynamic_cast< const SMDS_FacePosition* >( pos );
264         }
265         else if ( pos->GetTypeOfPosition() == SMDS_TOP_VERTEX ) {
266           vID = node->getshapeId();
267         }
268       }
269       if ( fPos || ( !normalOK && vID )) {
270         // compute normal
271         gp_Vec v01( nPnt[0], nPnt[1] ), v02( nPnt[0], nPnt[2] );
272         if ( v01.SquareMagnitude() > RealSmall() &&
273              v02.SquareMagnitude() > RealSmall() )
274         {
275           Ne = v01 ^ v02;
276           normalOK = ( Ne.SquareMagnitude() > RealSmall() );
277         }
278         // we need position on theFace or at least on vertex
279         if ( normalOK ) {
280           vertexID = vID;
281           if ((facePos = fPos))
282             break;
283         }
284       }
285     }
286   }
287   if ( !normalOK )
288     return isReversed;
289
290   // node position on face
291   double u,v;
292   if ( facePos ) {
293     u = facePos->GetUParameter();
294     v = facePos->GetVParameter();
295   }
296   else if ( vertexID ) {
297     TopoDS_Shape V = theMeshDS->IndexToShape( vertexID );
298     if ( V.IsNull() || V.ShapeType() != TopAbs_VERTEX )
299       return isReversed;
300     gp_Pnt2d uv = BRep_Tool::Parameters( TopoDS::Vertex( V ), theFace );
301     u = uv.X();
302     v = uv.Y();
303   }
304   else
305   {
306     return isReversed;
307   }
308
309   // face normal at node position
310   TopLoc_Location loc;
311   Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
312   if ( surf.IsNull() || surf->Continuity() < GeomAbs_C1 ) return isReversed;
313   gp_Vec d1u, d1v;
314   surf->D1( u, v, nPnt[0], d1u, d1v );
315   gp_Vec Nf = (d1u ^ d1v).Transformed( loc );
316
317   if ( theFace.Orientation() == TopAbs_REVERSED )
318     Nf.Reverse();
319
320   return Ne * Nf < 0.;
321 }
322
323 //================================================================================
324 /*!
325  * \brief Just return false as the algorithm does not hold parameters values
326  */
327 //================================================================================
328
329 bool SMESH_Algo::SetParametersByMesh(const SMESH_Mesh* /*theMesh*/,
330                                      const TopoDS_Shape& /*theShape*/)
331 {
332   return false;
333 }
334 bool SMESH_Algo::SetParametersByDefaults(const TDefaults& , const SMESH_Mesh*)
335 {
336   return false;
337 }
338 //================================================================================
339 /*!
340  * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
341  * \param theMesh - The mesh containing nodes
342  * \param theEdge - The geometrical edge of interest
343  * \param theParams - The resulting vector of sorted node parameters
344  * \retval bool - false if not all parameters are OK
345  */
346 //================================================================================
347
348 bool SMESH_Algo::GetNodeParamOnEdge(const SMESHDS_Mesh* theMesh,
349                                     const TopoDS_Edge&  theEdge,
350                                     vector< double > &  theParams)
351 {
352   theParams.clear();
353
354   if ( !theMesh || theEdge.IsNull() )
355     return false;
356
357   SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
358   if ( !eSubMesh || !eSubMesh->GetElements()->more() )
359     return false; // edge is not meshed
360
361   //int nbEdgeNodes = 0;
362   set < double > paramSet;
363   if ( eSubMesh )
364   {
365     // loop on nodes of an edge: sort them by param on edge
366     SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
367     while ( nIt->more() )
368     {
369       const SMDS_MeshNode* node = nIt->next();
370       const SMDS_PositionPtr& pos = node->GetPosition();
371       if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
372         return false;
373       const SMDS_EdgePosition* epos =
374         static_cast<const SMDS_EdgePosition*>(node->GetPosition());
375       if ( !paramSet.insert( epos->GetUParameter() ).second )
376         return false; // equal parameters
377     }
378   }
379   // add vertex nodes params
380   TopoDS_Vertex V1,V2;
381   TopExp::Vertices( theEdge, V1, V2);
382   if ( VertexNode( V1, theMesh ) &&
383        !paramSet.insert( BRep_Tool::Parameter(V1,theEdge) ).second )
384     return false; // there are equal parameters
385   if ( VertexNode( V2, theMesh ) &&
386        !paramSet.insert( BRep_Tool::Parameter(V2,theEdge) ).second )
387     return false; // there are equal parameters
388
389   // fill the vector
390   theParams.resize( paramSet.size() );
391   set < double >::iterator   par    = paramSet.begin();
392   vector< double >::iterator vecPar = theParams.begin();
393   for ( ; par != paramSet.end(); ++par, ++vecPar )
394     *vecPar = *par;
395
396   return theParams.size() > 1;
397 }
398
399 //================================================================================
400 /*!
401  * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
402  * \param theMesh - The mesh containing nodes
403  * \param theEdge - The geometrical edge of interest
404  * \param theParams - The resulting vector of sorted node parameters
405  * \retval bool - false if not all parameters are OK
406  */
407 //================================================================================
408
409 bool SMESH_Algo::GetSortedNodesOnEdge(const SMESHDS_Mesh*                   theMesh,
410                                       const TopoDS_Edge&                    theEdge,
411                                       const bool                            ignoreMediumNodes,
412                                       map< double, const SMDS_MeshNode* > & theNodes)
413 {
414   theNodes.clear();
415
416   if ( !theMesh || theEdge.IsNull() )
417     return false;
418
419   SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
420   if ( !eSubMesh || !eSubMesh->GetElements()->more() )
421     return false; // edge is not meshed
422
423   int nbNodes = 0;
424   set < double > paramSet;
425   if ( eSubMesh )
426   {
427     // loop on nodes of an edge: sort them by param on edge
428     SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
429     while ( nIt->more() )
430     {
431       const SMDS_MeshNode* node = nIt->next();
432       if ( ignoreMediumNodes ) {
433         SMDS_ElemIteratorPtr elemIt = node->GetInverseElementIterator();
434         if ( elemIt->more() && elemIt->next()->IsMediumNode( node ))
435           continue;
436       }
437       const SMDS_PositionPtr& pos = node->GetPosition();
438       if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
439         return false;
440       const SMDS_EdgePosition* epos =
441         static_cast<const SMDS_EdgePosition*>(node->GetPosition());
442       theNodes.insert( theNodes.end(), make_pair( epos->GetUParameter(), node ));
443       //MESSAGE("U " << epos->GetUParameter() << " ID " << node->GetID());
444       ++nbNodes;
445     }
446   }
447   // add vertex nodes
448   TopoDS_Vertex v1, v2;
449   TopExp::Vertices(theEdge, v1, v2);
450   const SMDS_MeshNode* n1 = VertexNode( v1, (SMESHDS_Mesh*) theMesh );
451   const SMDS_MeshNode* n2 = VertexNode( v2, (SMESHDS_Mesh*) theMesh );
452   //MESSAGE("Vertices ID " << n1->GetID() << " " << n2->GetID());
453   Standard_Real f, l;
454   BRep_Tool::Range(theEdge, f, l);
455   if ( v1.Orientation() != TopAbs_FORWARD )
456     std::swap( f, l );
457   if ( n1 && ++nbNodes )
458     theNodes.insert( make_pair( f, n1 ));
459   if ( n2 && ++nbNodes )
460     theNodes.insert( make_pair( l, n2 ));
461
462   return theNodes.size() == nbNodes;
463 }
464
465 //================================================================================
466 /*!
467  * \brief Make filter recognize only compatible hypotheses
468  * \param theFilter - the filter to initialize
469  * \param ignoreAuxiliary - make filter ignore compatible auxiliary hypotheses
470  */
471 //================================================================================
472
473 bool SMESH_Algo::InitCompatibleHypoFilter( SMESH_HypoFilter & theFilter,
474                                            const bool         ignoreAuxiliary) const
475 {
476   if ( !_compatibleHypothesis.empty() )
477   {
478     theFilter.Init( theFilter.HasName( _compatibleHypothesis[0] ));
479     for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
480       theFilter.Or( theFilter.HasName( _compatibleHypothesis[ i ] ));
481
482     if ( ignoreAuxiliary )
483       theFilter.AndNot( theFilter.IsAuxiliary() );
484
485     return true;
486   }
487   return false;
488 }
489
490 //================================================================================
491 /*!
492  * \brief Return continuity of two edges
493  * \param E1 - the 1st edge
494  * \param E2 - the 2nd edge
495  * \retval GeomAbs_Shape - regularity at the junction between E1 and E2
496  */
497 //================================================================================
498
499 GeomAbs_Shape SMESH_Algo::Continuity(TopoDS_Edge E1,
500                                      TopoDS_Edge E2)
501 {
502   //E1.Orientation(TopAbs_FORWARD), E2.Orientation(TopAbs_FORWARD); // avoid pb with internal edges
503   if (E1.Orientation() > TopAbs_REVERSED) // INTERNAL
504     E1.Orientation( TopAbs_FORWARD );
505   if (E2.Orientation() > TopAbs_REVERSED) // INTERNAL
506     E2.Orientation( TopAbs_FORWARD );
507
508   TopoDS_Vertex V, VV1[2], VV2[2];
509   TopExp::Vertices( E1, VV1[0], VV1[1], true );
510   TopExp::Vertices( E2, VV2[0], VV2[1], true );
511   if      ( VV1[1].IsSame( VV2[0] ))  { V = VV1[1]; }
512   else if ( VV1[0].IsSame( VV2[1] ))  { V = VV1[0]; }
513   else if ( VV1[1].IsSame( VV2[1] ))  { V = VV1[1]; E1.Reverse(); }
514   else if ( VV1[0].IsSame( VV2[0] ))  { V = VV1[0]; E1.Reverse(); }
515   else { return GeomAbs_C0; }
516
517   Standard_Real u1 = BRep_Tool::Parameter( V, E1 );
518   Standard_Real u2 = BRep_Tool::Parameter( V, E2 );
519   BRepAdaptor_Curve C1( E1 ), C2( E2 );
520   Standard_Real tol = BRep_Tool::Tolerance( V );
521   Standard_Real angTol = 2e-3;
522   try {
523 #if OCC_VERSION_LARGE > 0x06010000
524     OCC_CATCH_SIGNALS;
525 #endif
526     return BRepLProp::Continuity(C1, C2, u1, u2, tol, angTol);
527   }
528   catch (Standard_Failure) {
529   }
530   return GeomAbs_C0;
531 }
532
533 //================================================================================
534 /*!
535  * \brief Return the node built on a vertex
536  * \param V - the vertex
537  * \param meshDS - mesh
538  * \retval const SMDS_MeshNode* - found node or NULL
539  */
540 //================================================================================
541
542 const SMDS_MeshNode* SMESH_Algo::VertexNode(const TopoDS_Vertex& V,
543                                             const SMESHDS_Mesh*  meshDS)
544 {
545   if ( SMESHDS_SubMesh* sm = meshDS->MeshElements(V) ) {
546     SMDS_NodeIteratorPtr nIt= sm->GetNodes();
547     if (nIt->more())
548       return nIt->next();
549   }
550   return 0;
551 }
552
553 //=======================================================================
554 //function : GetCommonNodes
555 //purpose  : Return nodes common to two elements
556 //=======================================================================
557
558 vector< const SMDS_MeshNode*> SMESH_Algo::GetCommonNodes(const SMDS_MeshElement* e1,
559                                                          const SMDS_MeshElement* e2)
560 {
561   vector< const SMDS_MeshNode*> common;
562   for ( int i = 0 ; i < e1->NbNodes(); ++i )
563     if ( e2->GetNodeIndex( e1->GetNode( i )) >= 0 )
564       common.push_back( e1->GetNode( i ));
565   return common;
566 }
567
568 //=======================================================================
569 //function : GetMeshError
570 //purpose  : Finds topological errors of a sub-mesh
571 //WARNING  : 1D check is NOT implemented so far
572 //=======================================================================
573
574 SMESH_Algo::EMeshError SMESH_Algo::GetMeshError(SMESH_subMesh* subMesh)
575 {
576   EMeshError err = MEr_OK;
577
578   SMESHDS_SubMesh* smDS = subMesh->GetSubMeshDS();
579   if ( !smDS )
580     return MEr_EMPTY;
581
582   switch ( subMesh->GetSubShape().ShapeType() )
583   {
584   case TopAbs_FACE: { // ====================== 2D =====================
585
586     SMDS_ElemIteratorPtr fIt = smDS->GetElements();
587     if ( !fIt->more() )
588       return MEr_EMPTY;
589
590     // We check that olny links on EDGEs encouter once, the rest links, twice
591     set< SMESH_TLink > links;
592     while ( fIt->more() )
593     {
594       const SMDS_MeshElement* f = fIt->next();
595       int nbNodes = f->NbCornerNodes(); // ignore medium nodes
596       for ( int i = 0; i < nbNodes; ++i )
597       {
598         const SMDS_MeshNode* n1 = f->GetNode( i );
599         const SMDS_MeshNode* n2 = f->GetNode(( i+1 ) % nbNodes);
600         std::pair< set< SMESH_TLink >::iterator, bool > it_added =
601           links.insert( SMESH_TLink( n1, n2 ));
602         if ( !it_added.second )
603           // As we do NOT(!) check if mesh is manifold, we believe that a link can
604           // encounter once or twice only (not three times), we erase a link as soon
605           // as it encounters twice to speed up search in the <links> map.
606           links.erase( it_added.first );
607       }
608     }
609     // the links remaining in the <links> should all be on EDGE
610     set< SMESH_TLink >::iterator linkIt = links.begin();
611     for ( ; linkIt != links.end(); ++linkIt )
612     {
613       const SMESH_TLink& link = *linkIt;
614       if ( link.node1()->GetPosition()->GetTypeOfPosition() > SMDS_TOP_EDGE ||
615            link.node2()->GetPosition()->GetTypeOfPosition() > SMDS_TOP_EDGE )
616         return MEr_HOLES;
617     }
618     // TODO: to check orientation
619     break;
620   }
621   case TopAbs_SOLID: { // ====================== 3D =====================
622
623     SMDS_ElemIteratorPtr vIt = smDS->GetElements();
624     if ( !vIt->more() )
625       return MEr_EMPTY;
626
627     SMDS_VolumeTool vTool;
628     while ( !vIt->more() )
629     {
630       if (!vTool.Set( vIt->next() ))
631         continue; // strange
632
633       for ( int iF = 0; iF < vTool.NbFaces(); ++iF )
634         if ( vTool.IsFreeFace( iF ))
635         {
636           int nbN = vTool.NbFaceNodes( iF );
637           const SMDS_MeshNode** nodes =  vTool.GetFaceNodes( iF );
638           for ( int i = 0; i < nbN; ++i )
639             if ( nodes[i]->GetPosition()->GetTypeOfPosition() > SMDS_TOP_FACE )
640               return MEr_HOLES;
641         }
642     }
643     break;
644   }
645   default:;
646   }
647   return err;
648 }
649
650 //================================================================================
651 /*!
652  * \brief Sets event listener to submeshes if necessary
653  * \param subMesh - submesh where algo is set
654  * 
655  * After being set, event listener is notified on each event of a submesh.
656  * By default non listener is set
657  */
658 //================================================================================
659
660 void SMESH_Algo::SetEventListener(SMESH_subMesh* /*subMesh*/)
661 {
662 }
663
664 //================================================================================
665 /*!
666  * \brief Allow algo to do something after persistent restoration
667  * \param subMesh - restored submesh
668  *
669  * This method is called only if a submesh has HYP_OK algo_state.
670  */
671 //================================================================================
672
673 void SMESH_Algo::SubmeshRestored(SMESH_subMesh* /*subMesh*/)
674 {
675 }
676
677 //================================================================================
678 /*!
679  * \brief Computes mesh without geometry
680  * \param aMesh - the mesh
681  * \param aHelper - helper that must be used for adding elements to \aaMesh
682  * \retval bool - is a success
683  */
684 //================================================================================
685
686 bool SMESH_Algo::Compute(SMESH_Mesh & /*aMesh*/, SMESH_MesherHelper* /*aHelper*/)
687 {
688   return error( COMPERR_BAD_INPUT_MESH, "Mesh built on shape expected");
689 }
690
691 //=======================================================================
692 //function : CancelCompute
693 //purpose  : Sets _computeCanceled to true. It's usage depends on
694 //  *        implementation of a particular mesher.
695 //=======================================================================
696
697 void SMESH_Algo::CancelCompute()
698 {
699   _computeCanceled = true;
700   _error = COMPERR_CANCELED;
701 }
702
703 //================================================================================
704 /*!
705  * \brief store error and comment and then return ( error == COMPERR_OK )
706  */
707 //================================================================================
708
709 bool SMESH_Algo::error(int error, const SMESH_Comment& comment)
710 {
711   _error   = error;
712   _comment = comment;
713   return ( error == COMPERR_OK );
714 }
715
716 //================================================================================
717 /*!
718  * \brief store error and return ( error == COMPERR_OK )
719  */
720 //================================================================================
721
722 bool SMESH_Algo::error(SMESH_ComputeErrorPtr error)
723 {
724   if ( error ) {
725     _error   = error->myName;
726     _comment = error->myComment;
727     _badInputElements = error->myBadElements;
728     return error->IsOK();
729   }
730   return true;
731 }
732
733 //================================================================================
734 /*!
735  * \brief return compute error
736  */
737 //================================================================================
738
739 SMESH_ComputeErrorPtr SMESH_Algo::GetComputeError() const
740 {
741   SMESH_ComputeErrorPtr err = SMESH_ComputeError::New( _error, _comment, this );
742   // hope this method is called by only SMESH_subMesh after this->Compute()
743   err->myBadElements.splice( err->myBadElements.end(),
744                              (list<const SMDS_MeshElement*>&) _badInputElements );
745   return err;
746 }
747
748 //================================================================================
749 /*!
750  * \brief initialize compute error
751  */
752 //================================================================================
753
754 void SMESH_Algo::InitComputeError()
755 {
756   _error = COMPERR_OK;
757   _comment.clear();
758   list<const SMDS_MeshElement*>::iterator elem = _badInputElements.begin();
759   for ( ; elem != _badInputElements.end(); ++elem )
760     if ( (*elem)->GetID() < 1 )
761       delete *elem;
762   _badInputElements.clear();
763
764   _computeCanceled = false;
765 }
766
767 //================================================================================
768 /*!
769  * \brief store a bad input element preventing computation,
770  *        which may be a temporary one i.e. not residing the mesh,
771  *        then it will be deleted by InitComputeError()
772  */
773 //================================================================================
774
775 void SMESH_Algo::addBadInputElement(const SMDS_MeshElement* elem)
776 {
777   if ( elem )
778     _badInputElements.push_back( elem );
779 }