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