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