1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH : implementaion of SMESH idl descriptions
24 // File : SMESH_Algo.cxx
25 // Author : Paul RASCLE, EDF
28 #include "SMESH_Algo.hxx"
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"
44 #include <Basics_OCCTVersion.hxx>
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>
53 #include <TopExp_Explorer.hxx>
54 #include <TopLoc_Location.hxx>
55 #include <TopTools_ListIteratorOfListOfShape.hxx>
56 #include <TopTools_ListOfShape.hxx>
58 #include <TopoDS_Edge.hxx>
59 #include <TopoDS_Face.hxx>
60 #include <TopoDS_Vertex.hxx>
61 #include <TopoDS_Wire.hxx>
63 #include <gp_Pnt2d.hxx>
66 #include <Standard_ErrorHandler.hxx>
67 #include <Standard_Failure.hxx>
69 #include "utilities.h"
76 //=============================================================================
80 //=============================================================================
82 SMESH_Algo::SMESH_Algo (int hypId, int studyId, SMESH_Gen * gen)
83 : SMESH_Hypothesis(hypId, studyId, gen)
85 gen->_mapAlgo[hypId] = this;
87 _onlyUnaryInput = _requireDiscreteBoundary = _requireShape = true;
88 _quadraticMesh = _supportSubmeshes = false;
90 for ( int i = 0; i < 4; ++i )
91 _neededLowerHyps[ i ] = false;
94 //=============================================================================
98 //=============================================================================
100 SMESH_Algo::~SMESH_Algo()
104 //=============================================================================
108 //=============================================================================
110 SMESH_0D_Algo::SMESH_0D_Algo(int hypId, int studyId, SMESH_Gen* gen)
111 : SMESH_Algo(hypId, studyId, gen)
113 _shapeType = (1 << TopAbs_VERTEX);
115 gen->_map0D_Algo[hypId] = this;
117 SMESH_1D_Algo::SMESH_1D_Algo(int hypId, int studyId, SMESH_Gen* gen)
118 : SMESH_Algo(hypId, studyId, gen)
120 _shapeType = (1 << TopAbs_EDGE);
122 gen->_map1D_Algo[hypId] = this;
124 SMESH_2D_Algo::SMESH_2D_Algo(int hypId, int studyId, SMESH_Gen* gen)
125 : SMESH_Algo(hypId, studyId, gen)
127 _shapeType = (1 << TopAbs_FACE);
129 gen->_map2D_Algo[hypId] = this;
131 SMESH_3D_Algo::SMESH_3D_Algo(int hypId, int studyId, SMESH_Gen* gen)
132 : SMESH_Algo(hypId, studyId, gen)
134 _shapeType = (1 << TopAbs_SOLID);
136 gen->_map3D_Algo[hypId] = this;
139 //=============================================================================
141 * Usually an algoritm has nothing to save
143 //=============================================================================
145 ostream & SMESH_Algo::SaveTo(ostream & save) { return save; }
146 istream & SMESH_Algo::LoadFrom(istream & load) { return load; }
148 //=============================================================================
152 //=============================================================================
154 const vector < string > &SMESH_Algo::GetCompatibleHypothesis()
156 return _compatibleHypothesis;
159 //=============================================================================
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.
167 //=============================================================================
169 const list <const SMESHDS_Hypothesis *> &
170 SMESH_Algo::GetUsedHypothesis(SMESH_Mesh & aMesh,
171 const TopoDS_Shape & aShape,
172 const bool ignoreAuxiliary)
174 _usedHypList.clear();
175 SMESH_HypoFilter filter;
176 if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
178 aMesh.GetHypotheses( aShape, filter, _usedHypList, true );
179 if ( ignoreAuxiliary && _usedHypList.size() > 1 )
180 _usedHypList.clear(); //only one compatible hypothesis allowed
185 //=============================================================================
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)
191 //=============================================================================
193 const list<const SMESHDS_Hypothesis *> &
194 SMESH_Algo::GetAppliedHypothesis(SMESH_Mesh & aMesh,
195 const TopoDS_Shape & aShape,
196 const bool ignoreAuxiliary)
198 _appliedHypList.clear();
199 SMESH_HypoFilter filter;
200 if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
201 aMesh.GetHypotheses( aShape, filter, _appliedHypList, false );
203 return _appliedHypList;
206 //=============================================================================
208 * Compute length of an edge
210 //=============================================================================
212 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
214 double UMin = 0, UMax = 0;
215 if (BRep_Tool::Degenerated(E))
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);
224 //================================================================================
226 * \brief Calculate normal of a mesh face
228 //================================================================================
230 bool SMESH_Algo::FaceNormal(const SMDS_MeshElement* F, gp_XYZ& normal, bool normalized)
232 if ( !F || F->GetType() != SMDSAbs_Face )
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 )
240 for ( int n = 0; n < 3; ++n )
242 const SMDS_MeshNode* node = F->GetNode( i + n );
243 p[n].SetCoord( node->X(), node->Y(), node->Z() );
245 normal += ( p[2] - p[1] ) ^ ( p[0] - p[1] );
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 );
256 * Moved to SMESH_MesherHelper
258 // bool SMESH_Algo::IsReversedSubMesh (const TopoDS_Face& theFace,
259 // SMESHDS_Mesh* theMeshDS)
263 //================================================================================
265 * \brief Just return false as the algorithm does not hold parameters values
267 //================================================================================
269 bool SMESH_Algo::SetParametersByMesh(const SMESH_Mesh* /*theMesh*/,
270 const TopoDS_Shape& /*theShape*/)
274 bool SMESH_Algo::SetParametersByDefaults(const TDefaults& , const SMESH_Mesh*)
278 //================================================================================
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
286 //================================================================================
288 bool SMESH_Algo::GetNodeParamOnEdge(const SMESHDS_Mesh* theMesh,
289 const TopoDS_Edge& theEdge,
290 vector< double > & theParams)
294 if ( !theMesh || theEdge.IsNull() )
297 SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
298 if ( !eSubMesh || !eSubMesh->GetElements()->more() )
299 return false; // edge is not meshed
301 //int nbEdgeNodes = 0;
302 set < double > paramSet;
305 // loop on nodes of an edge: sort them by param on edge
306 SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
307 while ( nIt->more() )
309 const SMDS_MeshNode* node = nIt->next();
310 const SMDS_PositionPtr& pos = node->GetPosition();
311 if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
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
319 // add vertex nodes params
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
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 )
336 return theParams.size() > 1;
339 //================================================================================
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
347 //================================================================================
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)
356 if ( !theMesh || theEdge.IsNull() )
359 SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
360 if ( !eSubMesh || !eSubMesh->GetElements()->more() )
361 return false; // edge is not meshed
364 set < double > paramSet;
367 // loop on nodes of an edge: sort them by param on edge
368 SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
369 while ( nIt->more() )
371 const SMDS_MeshNode* node = nIt->next();
372 if ( ignoreMediumNodes ) {
373 SMDS_ElemIteratorPtr elemIt = node->GetInverseElementIterator();
374 if ( elemIt->more() && elemIt->next()->IsMediumNode( node ))
377 const SMDS_PositionPtr& pos = node->GetPosition();
378 if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
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());
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());
394 BRep_Tool::Range(theEdge, f, l);
395 if ( v1.Orientation() != TopAbs_FORWARD )
397 if ( n1 && ++nbNodes )
398 theNodes.insert( make_pair( f, n1 ));
399 if ( n2 && ++nbNodes )
400 theNodes.insert( make_pair( l, n2 ));
402 return theNodes.size() == nbNodes;
405 //================================================================================
407 * \brief Make filter recognize only compatible hypotheses
408 * \param theFilter - the filter to initialize
409 * \param ignoreAuxiliary - make filter ignore compatible auxiliary hypotheses
411 //================================================================================
413 bool SMESH_Algo::InitCompatibleHypoFilter( SMESH_HypoFilter & theFilter,
414 const bool ignoreAuxiliary) const
416 if ( !_compatibleHypothesis.empty() )
418 theFilter.Init( theFilter.HasName( _compatibleHypothesis[0] ));
419 for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
420 theFilter.Or( theFilter.HasName( _compatibleHypothesis[ i ] ));
422 if ( ignoreAuxiliary )
423 theFilter.AndNot( theFilter.IsAuxiliary() );
430 //================================================================================
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
437 //================================================================================
439 GeomAbs_Shape SMESH_Algo::Continuity(TopoDS_Edge E1,
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 );
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; }
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;
463 #if OCC_VERSION_LARGE > 0x06010000
466 return BRepLProp::Continuity(C1, C2, u1, u2, tol, angTol);
468 catch (Standard_Failure) {
473 //================================================================================
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
480 //================================================================================
482 const SMDS_MeshNode* SMESH_Algo::VertexNode(const TopoDS_Vertex& V,
483 const SMESHDS_Mesh* meshDS)
485 if ( SMESHDS_SubMesh* sm = meshDS->MeshElements(V) ) {
486 SMDS_NodeIteratorPtr nIt= sm->GetNodes();
493 //=======================================================================
494 //function : GetCommonNodes
495 //purpose : Return nodes common to two elements
496 //=======================================================================
498 vector< const SMDS_MeshNode*> SMESH_Algo::GetCommonNodes(const SMDS_MeshElement* e1,
499 const SMDS_MeshElement* e2)
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 ));
508 //=======================================================================
509 //function : GetMeshError
510 //purpose : Finds topological errors of a sub-mesh
511 //WARNING : 1D check is NOT implemented so far
512 //=======================================================================
514 SMESH_Algo::EMeshError SMESH_Algo::GetMeshError(SMESH_subMesh* subMesh)
516 EMeshError err = MEr_OK;
518 SMESHDS_SubMesh* smDS = subMesh->GetSubMeshDS();
522 switch ( subMesh->GetSubShape().ShapeType() )
524 case TopAbs_FACE: { // ====================== 2D =====================
526 SMDS_ElemIteratorPtr fIt = smDS->GetElements();
530 // We check that olny links on EDGEs encouter once, the rest links, twice
531 set< SMESH_TLink > links;
532 while ( fIt->more() )
534 const SMDS_MeshElement* f = fIt->next();
535 int nbNodes = f->NbCornerNodes(); // ignore medium nodes
536 for ( int i = 0; i < nbNodes; ++i )
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 );
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 )
553 const SMESH_TLink& link = *linkIt;
554 if ( link.node1()->GetPosition()->GetTypeOfPosition() > SMDS_TOP_EDGE ||
555 link.node2()->GetPosition()->GetTypeOfPosition() > SMDS_TOP_EDGE )
558 // TODO: to check orientation
561 case TopAbs_SOLID: { // ====================== 3D =====================
563 SMDS_ElemIteratorPtr vIt = smDS->GetElements();
567 SMDS_VolumeTool vTool;
568 while ( !vIt->more() )
570 if (!vTool.Set( vIt->next() ))
573 for ( int iF = 0; iF < vTool.NbFaces(); ++iF )
574 if ( vTool.IsFreeFace( iF ))
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 )
590 //================================================================================
592 * \brief Sets event listener to submeshes if necessary
593 * \param subMesh - submesh where algo is set
595 * After being set, event listener is notified on each event of a submesh.
596 * By default non listener is set
598 //================================================================================
600 void SMESH_Algo::SetEventListener(SMESH_subMesh* /*subMesh*/)
604 //================================================================================
606 * \brief Allow algo to do something after persistent restoration
607 * \param subMesh - restored submesh
609 * This method is called only if a submesh has HYP_OK algo_state.
611 //================================================================================
613 void SMESH_Algo::SubmeshRestored(SMESH_subMesh* /*subMesh*/)
617 //================================================================================
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
624 //================================================================================
626 bool SMESH_Algo::Compute(SMESH_Mesh & /*aMesh*/, SMESH_MesherHelper* /*aHelper*/)
628 return error( COMPERR_BAD_INPUT_MESH, "Mesh built on shape expected");
631 //=======================================================================
632 //function : CancelCompute
633 //purpose : Sets _computeCanceled to true. It's usage depends on
634 // * implementation of a particular mesher.
635 //=======================================================================
637 void SMESH_Algo::CancelCompute()
639 _computeCanceled = true;
640 _error = COMPERR_CANCELED;
643 //================================================================================
645 * \brief store error and comment and then return ( error == COMPERR_OK )
647 //================================================================================
649 bool SMESH_Algo::error(int error, const SMESH_Comment& comment)
653 return ( error == COMPERR_OK );
656 //================================================================================
658 * \brief store error and return ( error == COMPERR_OK )
660 //================================================================================
662 bool SMESH_Algo::error(SMESH_ComputeErrorPtr error)
665 _error = error->myName;
666 _comment = error->myComment;
667 _badInputElements = error->myBadElements;
668 return error->IsOK();
673 //================================================================================
675 * \brief return compute error
677 //================================================================================
679 SMESH_ComputeErrorPtr SMESH_Algo::GetComputeError() const
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 );
688 //================================================================================
690 * \brief initialize compute error
692 //================================================================================
694 void SMESH_Algo::InitComputeError()
698 list<const SMDS_MeshElement*>::iterator elem = _badInputElements.begin();
699 for ( ; elem != _badInputElements.end(); ++elem )
700 if ( (*elem)->GetID() < 1 )
702 _badInputElements.clear();
704 _computeCanceled = false;
707 //================================================================================
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()
713 //================================================================================
715 void SMESH_Algo::addBadInputElement(const SMDS_MeshElement* elem)
718 _badInputElements.push_back( elem );
721 //=======================================================================
722 //function : addBadInputElements
723 //purpose : store a bad input elements or nodes preventing computation
724 //=======================================================================
726 void SMESH_Algo::addBadInputElements(const SMESHDS_SubMesh* sm,
733 SMDS_NodeIteratorPtr nIt = sm->GetNodes();
734 while ( nIt->more() ) addBadInputElement( nIt->next() );
738 SMDS_ElemIteratorPtr eIt = sm->GetElements();
739 while ( eIt->more() ) addBadInputElement( eIt->next() );
744 //=============================================================================
748 //=============================================================================
750 // int SMESH_Algo::NumberOfWires(const TopoDS_Shape& S)
753 // for (TopExp_Explorer exp(S,TopAbs_WIRE); exp.More(); exp.Next())
758 //=============================================================================
762 //=============================================================================
764 int SMESH_Algo::NumberOfPoints(SMESH_Mesh& aMesh, const TopoDS_Wire& W)
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();
772 nbPoints += nb + 1; // internal points plus 1 vertex of 2 (last point ?)