Salome HOME
Merge from V6_main (04/10/2012)
[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 )
351   // some surfaces not detected as GeomAbs_C1 are nevertheless correct for meshing
352   if ( surf.IsNull() || surf->Continuity() < GeomAbs_C0 )
353     {
354       if (!surf.IsNull())
355         MESSAGE("surf->Continuity() < GeomAbs_C1 " << (surf->Continuity() < GeomAbs_C1));
356       return isReversed;
357     }
358   gp_Vec d1u, d1v;
359   surf->D1( u, v, nPnt[0], d1u, d1v );
360   gp_Vec Nf = (d1u ^ d1v).Transformed( loc );
361
362   if ( theFace.Orientation() == TopAbs_REVERSED )
363     Nf.Reverse();
364
365   return Ne * Nf < 0.;
366 }
367
368 //================================================================================
369 /*!
370  * \brief Just return false as the algorithm does not hold parameters values
371  */
372 //================================================================================
373
374 bool SMESH_Algo::SetParametersByMesh(const SMESH_Mesh* /*theMesh*/,
375                                      const TopoDS_Shape& /*theShape*/)
376 {
377   return false;
378 }
379 bool SMESH_Algo::SetParametersByDefaults(const TDefaults& , const SMESH_Mesh*)
380 {
381   return false;
382 }
383 //================================================================================
384 /*!
385  * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
386  * \param theMesh - The mesh containing nodes
387  * \param theEdge - The geometrical edge of interest
388  * \param theParams - The resulting vector of sorted node parameters
389  * \retval bool - false if not all parameters are OK
390  */
391 //================================================================================
392
393 bool SMESH_Algo::GetNodeParamOnEdge(const SMESHDS_Mesh* theMesh,
394                                     const TopoDS_Edge&  theEdge,
395                                     vector< double > &  theParams)
396 {
397   theParams.clear();
398
399   if ( !theMesh || theEdge.IsNull() )
400     return false;
401
402   SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
403   if ( !eSubMesh || !eSubMesh->GetElements()->more() )
404     return false; // edge is not meshed
405
406   //int nbEdgeNodes = 0;
407   set < double > paramSet;
408   if ( eSubMesh )
409   {
410     // loop on nodes of an edge: sort them by param on edge
411     SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
412     while ( nIt->more() )
413     {
414       const SMDS_MeshNode* node = nIt->next();
415       const SMDS_PositionPtr& pos = node->GetPosition();
416       if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
417         return false;
418       const SMDS_EdgePosition* epos =
419         static_cast<const SMDS_EdgePosition*>(node->GetPosition());
420       if ( !paramSet.insert( epos->GetUParameter() ).second )
421         return false; // equal parameters
422     }
423   }
424   // add vertex nodes params
425   TopoDS_Vertex V1,V2;
426   TopExp::Vertices( theEdge, V1, V2);
427   if ( VertexNode( V1, theMesh ) &&
428        !paramSet.insert( BRep_Tool::Parameter(V1,theEdge) ).second )
429     return false; // there are equal parameters
430   if ( VertexNode( V2, theMesh ) &&
431        !paramSet.insert( BRep_Tool::Parameter(V2,theEdge) ).second )
432     return false; // there are equal parameters
433
434   // fill the vector
435   theParams.resize( paramSet.size() );
436   set < double >::iterator   par    = paramSet.begin();
437   vector< double >::iterator vecPar = theParams.begin();
438   for ( ; par != paramSet.end(); ++par, ++vecPar )
439     *vecPar = *par;
440
441   return theParams.size() > 1;
442 }
443
444 //================================================================================
445 /*!
446  * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
447  * \param theMesh - The mesh containing nodes
448  * \param theEdge - The geometrical edge of interest
449  * \param theParams - The resulting vector of sorted node parameters
450  * \retval bool - false if not all parameters are OK
451  */
452 //================================================================================
453
454 bool SMESH_Algo::GetSortedNodesOnEdge(const SMESHDS_Mesh*                   theMesh,
455                                       const TopoDS_Edge&                    theEdge,
456                                       const bool                            ignoreMediumNodes,
457                                       map< double, const SMDS_MeshNode* > & theNodes)
458 {
459   theNodes.clear();
460
461   if ( !theMesh || theEdge.IsNull() )
462     return false;
463
464   SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
465   if ( !eSubMesh || !eSubMesh->GetElements()->more() )
466     return false; // edge is not meshed
467
468   int nbNodes = 0;
469   set < double > paramSet;
470   if ( eSubMesh )
471   {
472     // loop on nodes of an edge: sort them by param on edge
473     SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
474     while ( nIt->more() )
475     {
476       const SMDS_MeshNode* node = nIt->next();
477       if ( ignoreMediumNodes ) {
478         SMDS_ElemIteratorPtr elemIt = node->GetInverseElementIterator();
479         if ( elemIt->more() && elemIt->next()->IsMediumNode( node ))
480           continue;
481       }
482       const SMDS_PositionPtr& pos = node->GetPosition();
483       if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
484         return false;
485       const SMDS_EdgePosition* epos =
486         static_cast<const SMDS_EdgePosition*>(node->GetPosition());
487       theNodes.insert( theNodes.end(), make_pair( epos->GetUParameter(), node ));
488       //MESSAGE("U " << epos->GetUParameter() << " ID " << node->GetID());
489       ++nbNodes;
490     }
491   }
492   // add vertex nodes
493   TopoDS_Vertex v1, v2;
494   TopExp::Vertices(theEdge, v1, v2);
495   const SMDS_MeshNode* n1 = VertexNode( v1, (SMESHDS_Mesh*) theMesh );
496   const SMDS_MeshNode* n2 = VertexNode( v2, (SMESHDS_Mesh*) theMesh );
497   //MESSAGE("Vertices ID " << n1->GetID() << " " << n2->GetID());
498   Standard_Real f, l;
499   BRep_Tool::Range(theEdge, f, l);
500   if ( v1.Orientation() != TopAbs_FORWARD )
501     std::swap( f, l );
502   if ( n1 && ++nbNodes )
503     theNodes.insert( make_pair( f, n1 ));
504   if ( n2 && ++nbNodes )
505     theNodes.insert( make_pair( l, n2 ));
506
507   return theNodes.size() == nbNodes;
508 }
509
510 //================================================================================
511 /*!
512  * \brief Make filter recognize only compatible hypotheses
513  * \param theFilter - the filter to initialize
514  * \param ignoreAuxiliary - make filter ignore compatible auxiliary hypotheses
515  */
516 //================================================================================
517
518 bool SMESH_Algo::InitCompatibleHypoFilter( SMESH_HypoFilter & theFilter,
519                                            const bool         ignoreAuxiliary) const
520 {
521   if ( !_compatibleHypothesis.empty() )
522   {
523     theFilter.Init( theFilter.HasName( _compatibleHypothesis[0] ));
524     for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
525       theFilter.Or( theFilter.HasName( _compatibleHypothesis[ i ] ));
526
527     if ( ignoreAuxiliary )
528       theFilter.AndNot( theFilter.IsAuxiliary() );
529
530     return true;
531   }
532   return false;
533 }
534
535 //================================================================================
536 /*!
537  * \brief Return continuity of two edges
538  * \param E1 - the 1st edge
539  * \param E2 - the 2nd edge
540  * \retval GeomAbs_Shape - regularity at the junction between E1 and E2
541  */
542 //================================================================================
543
544 GeomAbs_Shape SMESH_Algo::Continuity(TopoDS_Edge E1,
545                                      TopoDS_Edge E2)
546 {
547   //E1.Orientation(TopAbs_FORWARD), E2.Orientation(TopAbs_FORWARD); // avoid pb with internal edges
548   if (E1.Orientation() > TopAbs_REVERSED) // INTERNAL
549     E1.Orientation( TopAbs_FORWARD );
550   if (E2.Orientation() > TopAbs_REVERSED) // INTERNAL
551     E2.Orientation( TopAbs_FORWARD );
552
553   TopoDS_Vertex V, VV1[2], VV2[2];
554   TopExp::Vertices( E1, VV1[0], VV1[1], true );
555   TopExp::Vertices( E2, VV2[0], VV2[1], true );
556   if      ( VV1[1].IsSame( VV2[0] ))  { V = VV1[1]; }
557   else if ( VV1[0].IsSame( VV2[1] ))  { V = VV1[0]; }
558   else if ( VV1[1].IsSame( VV2[1] ))  { V = VV1[1]; E1.Reverse(); }
559   else if ( VV1[0].IsSame( VV2[0] ))  { V = VV1[0]; E1.Reverse(); }
560   else { return GeomAbs_C0; }
561
562   Standard_Real u1 = BRep_Tool::Parameter( V, E1 );
563   Standard_Real u2 = BRep_Tool::Parameter( V, E2 );
564   BRepAdaptor_Curve C1( E1 ), C2( E2 );
565   Standard_Real tol = BRep_Tool::Tolerance( V );
566   Standard_Real angTol = 2e-3;
567   try {
568 #if OCC_VERSION_LARGE > 0x06010000
569     OCC_CATCH_SIGNALS;
570 #endif
571     return BRepLProp::Continuity(C1, C2, u1, u2, tol, angTol);
572   }
573   catch (Standard_Failure) {
574   }
575   return GeomAbs_C0;
576 }
577
578 //================================================================================
579 /*!
580  * \brief Return the node built on a vertex
581  * \param V - the vertex
582  * \param meshDS - mesh
583  * \retval const SMDS_MeshNode* - found node or NULL
584  */
585 //================================================================================
586
587 const SMDS_MeshNode* SMESH_Algo::VertexNode(const TopoDS_Vertex& V,
588                                             const SMESHDS_Mesh*  meshDS)
589 {
590   if ( SMESHDS_SubMesh* sm = meshDS->MeshElements(V) ) {
591     SMDS_NodeIteratorPtr nIt= sm->GetNodes();
592     if (nIt->more())
593       return nIt->next();
594   }
595   return 0;
596 }
597
598 //=======================================================================
599 //function : GetCommonNodes
600 //purpose  : Return nodes common to two elements
601 //=======================================================================
602
603 vector< const SMDS_MeshNode*> SMESH_Algo::GetCommonNodes(const SMDS_MeshElement* e1,
604                                                          const SMDS_MeshElement* e2)
605 {
606   vector< const SMDS_MeshNode*> common;
607   for ( int i = 0 ; i < e1->NbNodes(); ++i )
608     if ( e2->GetNodeIndex( e1->GetNode( i )) >= 0 )
609       common.push_back( e1->GetNode( i ));
610   return common;
611 }
612
613 //=======================================================================
614 //function : GetMeshError
615 //purpose  : Finds topological errors of a sub-mesh
616 //WARNING  : 1D check is NOT implemented so far
617 //=======================================================================
618
619 SMESH_Algo::EMeshError SMESH_Algo::GetMeshError(SMESH_subMesh* subMesh)
620 {
621   EMeshError err = MEr_OK;
622
623   SMESHDS_SubMesh* smDS = subMesh->GetSubMeshDS();
624   if ( !smDS )
625     return MEr_EMPTY;
626
627   switch ( subMesh->GetSubShape().ShapeType() )
628   {
629   case TopAbs_FACE: { // ====================== 2D =====================
630
631     SMDS_ElemIteratorPtr fIt = smDS->GetElements();
632     if ( !fIt->more() )
633       return MEr_EMPTY;
634
635     // We check that olny links on EDGEs encouter once, the rest links, twice
636     set< SMESH_TLink > links;
637     while ( fIt->more() )
638     {
639       const SMDS_MeshElement* f = fIt->next();
640       int nbNodes = f->NbCornerNodes(); // ignore medium nodes
641       for ( int i = 0; i < nbNodes; ++i )
642       {
643         const SMDS_MeshNode* n1 = f->GetNode( i );
644         const SMDS_MeshNode* n2 = f->GetNode(( i+1 ) % nbNodes);
645         std::pair< set< SMESH_TLink >::iterator, bool > it_added =
646           links.insert( SMESH_TLink( n1, n2 ));
647         if ( !it_added.second )
648           // As we do NOT(!) check if mesh is manifold, we believe that a link can
649           // encounter once or twice only (not three times), we erase a link as soon
650           // as it encounters twice to speed up search in the <links> map.
651           links.erase( it_added.first );
652       }
653     }
654     // the links remaining in the <links> should all be on EDGE
655     set< SMESH_TLink >::iterator linkIt = links.begin();
656     for ( ; linkIt != links.end(); ++linkIt )
657     {
658       const SMESH_TLink& link = *linkIt;
659       if ( link.node1()->GetPosition()->GetTypeOfPosition() > SMDS_TOP_EDGE ||
660            link.node2()->GetPosition()->GetTypeOfPosition() > SMDS_TOP_EDGE )
661         return MEr_HOLES;
662     }
663     // TODO: to check orientation
664     break;
665   }
666   case TopAbs_SOLID: { // ====================== 3D =====================
667
668     SMDS_ElemIteratorPtr vIt = smDS->GetElements();
669     if ( !vIt->more() )
670       return MEr_EMPTY;
671
672     SMDS_VolumeTool vTool;
673     while ( !vIt->more() )
674     {
675       if (!vTool.Set( vIt->next() ))
676         continue; // strange
677
678       for ( int iF = 0; iF < vTool.NbFaces(); ++iF )
679         if ( vTool.IsFreeFace( iF ))
680         {
681           int nbN = vTool.NbFaceNodes( iF );
682           const SMDS_MeshNode** nodes =  vTool.GetFaceNodes( iF );
683           for ( int i = 0; i < nbN; ++i )
684             if ( nodes[i]->GetPosition()->GetTypeOfPosition() > SMDS_TOP_FACE )
685               return MEr_HOLES;
686         }
687     }
688     break;
689   }
690   default:;
691   }
692   return err;
693 }
694
695 //================================================================================
696 /*!
697  * \brief Sets event listener to submeshes if necessary
698  * \param subMesh - submesh where algo is set
699  * 
700  * After being set, event listener is notified on each event of a submesh.
701  * By default non listener is set
702  */
703 //================================================================================
704
705 void SMESH_Algo::SetEventListener(SMESH_subMesh* /*subMesh*/)
706 {
707 }
708
709 //================================================================================
710 /*!
711  * \brief Allow algo to do something after persistent restoration
712  * \param subMesh - restored submesh
713  *
714  * This method is called only if a submesh has HYP_OK algo_state.
715  */
716 //================================================================================
717
718 void SMESH_Algo::SubmeshRestored(SMESH_subMesh* /*subMesh*/)
719 {
720 }
721
722 //================================================================================
723 /*!
724  * \brief Computes mesh without geometry
725  * \param aMesh - the mesh
726  * \param aHelper - helper that must be used for adding elements to \aaMesh
727  * \retval bool - is a success
728  */
729 //================================================================================
730
731 bool SMESH_Algo::Compute(SMESH_Mesh & /*aMesh*/, SMESH_MesherHelper* /*aHelper*/)
732 {
733   return error( COMPERR_BAD_INPUT_MESH, "Mesh built on shape expected");
734 }
735
736 //=======================================================================
737 //function : CancelCompute
738 //purpose  : Sets _computeCanceled to true. It's usage depends on
739 //  *        implementation of a particular mesher.
740 //=======================================================================
741
742 void SMESH_Algo::CancelCompute()
743 {
744   _computeCanceled = true;
745   _error = COMPERR_CANCELED;
746 }
747
748 //================================================================================
749 /*!
750  * \brief store error and comment and then return ( error == COMPERR_OK )
751  */
752 //================================================================================
753
754 bool SMESH_Algo::error(int error, const SMESH_Comment& comment)
755 {
756   _error   = error;
757   _comment = comment;
758   return ( error == COMPERR_OK );
759 }
760
761 //================================================================================
762 /*!
763  * \brief store error and return ( error == COMPERR_OK )
764  */
765 //================================================================================
766
767 bool SMESH_Algo::error(SMESH_ComputeErrorPtr error)
768 {
769   if ( error ) {
770     _error   = error->myName;
771     _comment = error->myComment;
772     _badInputElements = error->myBadElements;
773     return error->IsOK();
774   }
775   return true;
776 }
777
778 //================================================================================
779 /*!
780  * \brief return compute error
781  */
782 //================================================================================
783
784 SMESH_ComputeErrorPtr SMESH_Algo::GetComputeError() const
785 {
786   SMESH_ComputeErrorPtr err = SMESH_ComputeError::New( _error, _comment, this );
787   // hope this method is called by only SMESH_subMesh after this->Compute()
788   err->myBadElements.splice( err->myBadElements.end(),
789                              (list<const SMDS_MeshElement*>&) _badInputElements );
790   return err;
791 }
792
793 //================================================================================
794 /*!
795  * \brief initialize compute error
796  */
797 //================================================================================
798
799 void SMESH_Algo::InitComputeError()
800 {
801   _error = COMPERR_OK;
802   _comment.clear();
803   list<const SMDS_MeshElement*>::iterator elem = _badInputElements.begin();
804   for ( ; elem != _badInputElements.end(); ++elem )
805     if ( (*elem)->GetID() < 1 )
806       delete *elem;
807   _badInputElements.clear();
808
809   _computeCanceled = false;
810 }
811
812 //================================================================================
813 /*!
814  * \brief store a bad input element preventing computation,
815  *        which may be a temporary one i.e. not residing the mesh,
816  *        then it will be deleted by InitComputeError()
817  */
818 //================================================================================
819
820 void SMESH_Algo::addBadInputElement(const SMDS_MeshElement* elem)
821 {
822   if ( elem )
823     _badInputElements.push_back( elem );
824 }
825
826 //=============================================================================
827 /*!
828  *  
829  */
830 //=============================================================================
831
832 int SMESH_Algo::NumberOfWires(const TopoDS_Shape& S)
833 {
834   int i = 0;
835   for (TopExp_Explorer exp(S,TopAbs_WIRE); exp.More(); exp.Next())
836     i++;
837   return i;
838 }
839
840 //=============================================================================
841 /*!
842  *  
843  */
844 //=============================================================================
845
846 int SMESH_Algo::NumberOfPoints(SMESH_Mesh& aMesh, const TopoDS_Wire& W)
847 {
848   int nbPoints = 0;
849   for (TopExp_Explorer exp(W,TopAbs_EDGE); exp.More(); exp.Next()) {
850     const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
851     int nb = aMesh.GetSubMesh(E)->GetSubMeshDS()->NbNodes();
852     if(_quadraticMesh)
853       nb = nb/2;
854     nbPoints += nb + 1; // internal points plus 1 vertex of 2 (last point ?)
855   }
856   return nbPoints;
857 }
858
859