1 // Copyright (C) 2007-2010 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"
29 #include "SMESH_Comment.hxx"
30 #include "SMESH_Gen.hxx"
31 #include "SMESH_Mesh.hxx"
32 #include "SMESH_HypoFilter.hxx"
33 #include "SMDS_FacePosition.hxx"
34 #include "SMDS_EdgePosition.hxx"
35 #include "SMDS_MeshElement.hxx"
36 #include "SMDS_MeshNode.hxx"
37 #include "SMESHDS_Mesh.hxx"
38 #include "SMESHDS_SubMesh.hxx"
40 #include <BRepAdaptor_Curve.hxx>
41 #include <BRepLProp.hxx>
42 #include <BRep_Tool.hxx>
43 #include <GCPnts_AbscissaPoint.hxx>
44 #include <GeomAdaptor_Curve.hxx>
45 #include <Geom_Surface.hxx>
47 #include <TopLoc_Location.hxx>
48 #include <TopTools_ListIteratorOfListOfShape.hxx>
49 #include <TopTools_ListOfShape.hxx>
51 #include <TopoDS_Edge.hxx>
52 #include <TopoDS_Face.hxx>
53 #include <TopoDS_Vertex.hxx>
55 #include <gp_Pnt2d.hxx>
58 #include <Standard_ErrorHandler.hxx>
59 #include <Standard_Failure.hxx>
61 #include "utilities.h"
68 //=============================================================================
72 //=============================================================================
74 SMESH_Algo::SMESH_Algo (int hypId, int studyId, SMESH_Gen * gen)
75 : SMESH_Hypothesis(hypId, studyId, gen)
77 gen->_mapAlgo[hypId] = this;
79 _onlyUnaryInput = _requireDescretBoundary = _requireShape = true;
80 _quadraticMesh = _supportSubmeshes = false;
84 //=============================================================================
88 //=============================================================================
90 SMESH_Algo::~SMESH_Algo()
94 //=============================================================================
96 * Usually an algoritm has nothing to save
98 //=============================================================================
100 ostream & SMESH_Algo::SaveTo(ostream & save) { return save; }
101 istream & SMESH_Algo::LoadFrom(istream & load) { return load; }
103 //=============================================================================
107 //=============================================================================
109 const vector < string > &SMESH_Algo::GetCompatibleHypothesis()
111 return _compatibleHypothesis;
114 //=============================================================================
116 * List the hypothesis used by the algorithm associated to the shape.
117 * Hypothesis associated to father shape -are- taken into account (see
118 * GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
119 * the algorithm. This method could be surcharged by specific algorithms, in
120 * case of several hypothesis simultaneously applicable.
122 //=============================================================================
124 const list <const SMESHDS_Hypothesis *> &
125 SMESH_Algo::GetUsedHypothesis(SMESH_Mesh & aMesh,
126 const TopoDS_Shape & aShape,
127 const bool ignoreAuxiliary)
129 _usedHypList.clear();
130 SMESH_HypoFilter filter;
131 if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
133 aMesh.GetHypotheses( aShape, filter, _usedHypList, true );
134 if ( ignoreAuxiliary && _usedHypList.size() > 1 )
135 _usedHypList.clear(); //only one compatible hypothesis allowed
140 //=============================================================================
142 * List the relevant hypothesis associated to the shape. Relevant hypothesis
143 * have a name (type) listed in the algorithm. Hypothesis associated to
144 * father shape -are not- taken into account (see GetUsedHypothesis)
146 //=============================================================================
148 const list<const SMESHDS_Hypothesis *> &
149 SMESH_Algo::GetAppliedHypothesis(SMESH_Mesh & aMesh,
150 const TopoDS_Shape & aShape,
151 const bool ignoreAuxiliary)
153 _appliedHypList.clear();
154 SMESH_HypoFilter filter;
155 if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
156 aMesh.GetHypotheses( aShape, filter, _appliedHypList, false );
158 return _appliedHypList;
161 //=============================================================================
163 * Compute length of an edge
165 //=============================================================================
167 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
169 double UMin = 0, UMax = 0;
170 if (BRep_Tool::Degenerated(E))
173 Handle(Geom_Curve) C = BRep_Tool::Curve(E, L, UMin, UMax);
174 GeomAdaptor_Curve AdaptCurve(C);
175 double length = GCPnts_AbscissaPoint::Length(AdaptCurve, UMin, UMax);
179 //================================================================================
181 * \brief Calculate normal of a mesh face
183 //================================================================================
185 bool SMESH_Algo::FaceNormal(const SMDS_MeshElement* F, gp_XYZ& normal, bool normalized)
187 if ( !F || F->GetType() != SMDSAbs_Face )
190 normal.SetCoord(0,0,0);
191 int nbNodes = F->IsQuadratic() ? F->NbNodes()/2 : F->NbNodes();
192 for ( int i = 0; i < nbNodes-2; ++i )
195 for ( int n = 0; n < 3; ++n )
197 const SMDS_MeshNode* node = F->GetNode( i + n );
198 p[n].SetCoord( node->X(), node->Y(), node->Z() );
200 normal += ( p[2] - p[1] ) ^ ( p[0] - p[1] );
202 double size2 = normal.SquareModulus();
203 bool ok = ( size2 > numeric_limits<double>::min() * numeric_limits<double>::min());
204 if ( normalized && ok )
205 normal /= sqrt( size2 );
210 //================================================================================
212 * \brief Find out elements orientation on a geometrical face
213 * \param theFace - The face correctly oriented in the shape being meshed
214 * \param theMeshDS - The mesh data structure
215 * \retval bool - true if the face normal and the normal of first element
216 * in the correspoding submesh point in different directions
218 //================================================================================
220 bool SMESH_Algo::IsReversedSubMesh (const TopoDS_Face& theFace,
221 SMESHDS_Mesh* theMeshDS)
223 if ( theFace.IsNull() || !theMeshDS )
226 // find out orientation of a meshed face
227 int faceID = theMeshDS->ShapeToIndex( theFace );
228 TopoDS_Shape aMeshedFace = theMeshDS->IndexToShape( faceID );
229 bool isReversed = ( theFace.Orientation() != aMeshedFace.Orientation() );
231 const SMESHDS_SubMesh * aSubMeshDSFace = theMeshDS->MeshElements( faceID );
232 if ( !aSubMeshDSFace )
235 // find element with node located on face and get its normal
236 const SMDS_FacePosition* facePos = 0;
240 bool normalOK = false;
241 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
242 while ( iteratorElem->more() ) // loop on elements on theFace
244 const SMDS_MeshElement* elem = iteratorElem->next();
245 if ( elem && elem->NbNodes() > 2 ) {
246 SMDS_ElemIteratorPtr nodesIt = elem->nodesIterator();
247 const SMDS_FacePosition* fPos = 0;
249 while ( nodesIt->more() ) { // loop on nodes
250 const SMDS_MeshNode* node
251 = static_cast<const SMDS_MeshNode *>(nodesIt->next());
253 nPnt[ i++ ].SetCoord( node->X(), node->Y(), node->Z() );
255 const SMDS_PositionPtr& pos = node->GetPosition();
256 if ( !pos ) continue;
257 if ( pos->GetTypeOfPosition() == SMDS_TOP_FACE ) {
258 fPos = dynamic_cast< const SMDS_FacePosition* >( pos.get() );
260 else if ( pos->GetTypeOfPosition() == SMDS_TOP_VERTEX ) {
261 vID = pos->GetShapeId();
264 if ( fPos || ( !normalOK && vID )) {
266 gp_Vec v01( nPnt[0], nPnt[1] ), v02( nPnt[0], nPnt[2] );
267 if ( v01.SquareMagnitude() > RealSmall() &&
268 v02.SquareMagnitude() > RealSmall() )
271 normalOK = ( Ne.SquareMagnitude() > RealSmall() );
273 // we need position on theFace or at least on vertex
276 if ((facePos = fPos))
285 // node position on face
288 u = facePos->GetUParameter();
289 v = facePos->GetVParameter();
291 else if ( vertexID ) {
292 TopoDS_Shape V = theMeshDS->IndexToShape( vertexID );
293 if ( V.IsNull() || V.ShapeType() != TopAbs_VERTEX )
295 gp_Pnt2d uv = BRep_Tool::Parameters( TopoDS::Vertex( V ), theFace );
304 // face normal at node position
306 Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
307 if ( surf.IsNull() || surf->Continuity() < GeomAbs_C1 ) return isReversed;
309 surf->D1( u, v, nPnt[0], d1u, d1v );
310 gp_Vec Nf = (d1u ^ d1v).Transformed( loc );
312 if ( theFace.Orientation() == TopAbs_REVERSED )
318 //================================================================================
320 * \brief Just return false as the algorithm does not hold parameters values
322 //================================================================================
324 bool SMESH_Algo::SetParametersByMesh(const SMESH_Mesh* /*theMesh*/,
325 const TopoDS_Shape& /*theShape*/)
329 bool SMESH_Algo::SetParametersByDefaults(const TDefaults& , const SMESH_Mesh*)
333 //================================================================================
335 * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
336 * \param theMesh - The mesh containing nodes
337 * \param theEdge - The geometrical edge of interest
338 * \param theParams - The resulting vector of sorted node parameters
339 * \retval bool - false if not all parameters are OK
341 //================================================================================
343 bool SMESH_Algo::GetNodeParamOnEdge(const SMESHDS_Mesh* theMesh,
344 const TopoDS_Edge& theEdge,
345 vector< double > & theParams)
349 if ( !theMesh || theEdge.IsNull() )
352 SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
353 if ( !eSubMesh || !eSubMesh->GetElements()->more() )
354 return false; // edge is not meshed
356 //int nbEdgeNodes = 0;
357 set < double > paramSet;
360 // loop on nodes of an edge: sort them by param on edge
361 SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
362 while ( nIt->more() )
364 const SMDS_MeshNode* node = nIt->next();
365 const SMDS_PositionPtr& pos = node->GetPosition();
366 if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
368 const SMDS_EdgePosition* epos =
369 static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
370 if ( !paramSet.insert( epos->GetUParameter() ).second )
371 return false; // equal parameters
374 // add vertex nodes params
376 TopExp::Vertices( theEdge, V1, V2);
377 if ( VertexNode( V1, theMesh ) &&
378 !paramSet.insert( BRep_Tool::Parameter(V1,theEdge) ).second )
379 return false; // there are equal parameters
380 if ( VertexNode( V2, theMesh ) &&
381 !paramSet.insert( BRep_Tool::Parameter(V2,theEdge) ).second )
382 return false; // there are equal parameters
385 theParams.resize( paramSet.size() );
386 set < double >::iterator par = paramSet.begin();
387 vector< double >::iterator vecPar = theParams.begin();
388 for ( ; par != paramSet.end(); ++par, ++vecPar )
391 return theParams.size() > 1;
394 //================================================================================
396 * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
397 * \param theMesh - The mesh containing nodes
398 * \param theEdge - The geometrical edge of interest
399 * \param theParams - The resulting vector of sorted node parameters
400 * \retval bool - false if not all parameters are OK
402 //================================================================================
404 bool SMESH_Algo::GetSortedNodesOnEdge(const SMESHDS_Mesh* theMesh,
405 const TopoDS_Edge& theEdge,
406 const bool ignoreMediumNodes,
407 map< double, const SMDS_MeshNode* > & theNodes)
411 if ( !theMesh || theEdge.IsNull() )
414 SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
415 if ( !eSubMesh || !eSubMesh->GetElements()->more() )
416 return false; // edge is not meshed
419 set < double > paramSet;
422 // loop on nodes of an edge: sort them by param on edge
423 SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
424 while ( nIt->more() )
426 const SMDS_MeshNode* node = nIt->next();
427 if ( ignoreMediumNodes ) {
428 SMDS_ElemIteratorPtr elemIt = node->GetInverseElementIterator();
429 if ( elemIt->more() && elemIt->next()->IsMediumNode( node ))
432 const SMDS_PositionPtr& pos = node->GetPosition();
433 if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
435 const SMDS_EdgePosition* epos =
436 static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
437 theNodes.insert( make_pair( epos->GetUParameter(), node ));
442 TopoDS_Vertex v1, v2;
443 TopExp::Vertices(theEdge, v1, v2);
444 const SMDS_MeshNode* n1 = VertexNode( v1, (SMESHDS_Mesh*) theMesh );
445 const SMDS_MeshNode* n2 = VertexNode( v2, (SMESHDS_Mesh*) theMesh );
447 BRep_Tool::Range(theEdge, f, l);
448 if ( v1.Orientation() != TopAbs_FORWARD )
450 if ( n1 && ++nbNodes )
451 theNodes.insert( make_pair( f, n1 ));
452 if ( n2 && ++nbNodes )
453 theNodes.insert( make_pair( l, n2 ));
455 return theNodes.size() == nbNodes;
458 //================================================================================
460 * \brief Make filter recognize only compatible hypotheses
461 * \param theFilter - the filter to initialize
462 * \param ignoreAuxiliary - make filter ignore compatible auxiliary hypotheses
464 //================================================================================
466 bool SMESH_Algo::InitCompatibleHypoFilter( SMESH_HypoFilter & theFilter,
467 const bool ignoreAuxiliary) const
469 if ( !_compatibleHypothesis.empty() )
471 theFilter.Init( theFilter.HasName( _compatibleHypothesis[0] ));
472 for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
473 theFilter.Or( theFilter.HasName( _compatibleHypothesis[ i ] ));
475 if ( ignoreAuxiliary )
476 theFilter.AndNot( theFilter.IsAuxiliary() );
483 //================================================================================
485 * \brief Return continuity of two edges
486 * \param E1 - the 1st edge
487 * \param E2 - the 2nd edge
488 * \retval GeomAbs_Shape - regularity at the junction between E1 and E2
490 //================================================================================
492 GeomAbs_Shape SMESH_Algo::Continuity(TopoDS_Edge E1,
495 //E1.Orientation(TopAbs_FORWARD), E2.Orientation(TopAbs_FORWARD); // avoid pb with internal edges
496 if (E1.Orientation() > TopAbs_REVERSED) // INTERNAL
497 E1.Orientation( TopAbs_FORWARD );
498 if (E2.Orientation() > TopAbs_REVERSED) // INTERNAL
499 E2.Orientation( TopAbs_FORWARD );
500 TopoDS_Vertex V = TopExp::LastVertex (E1, true);
501 if ( !V.IsSame( TopExp::FirstVertex(E2, true )))
502 if ( !TopExp::CommonVertex( E1, E2, V ))
504 Standard_Real u1 = BRep_Tool::Parameter( V, E1 );
505 Standard_Real u2 = BRep_Tool::Parameter( V, E2 );
506 BRepAdaptor_Curve C1( E1 ), C2( E2 );
507 Standard_Real tol = BRep_Tool::Tolerance( V );
508 Standard_Real angTol = 2e-3;
510 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
513 return BRepLProp::Continuity(C1, C2, u1, u2, tol, angTol);
515 catch (Standard_Failure) {
520 //================================================================================
522 * \brief Return the node built on a vertex
523 * \param V - the vertex
524 * \param meshDS - mesh
525 * \retval const SMDS_MeshNode* - found node or NULL
527 //================================================================================
529 const SMDS_MeshNode* SMESH_Algo::VertexNode(const TopoDS_Vertex& V,
530 const SMESHDS_Mesh* meshDS)
532 if ( SMESHDS_SubMesh* sm = meshDS->MeshElements(V) ) {
533 SMDS_NodeIteratorPtr nIt= sm->GetNodes();
540 //=======================================================================
541 //function : GetCommonNodes
542 //purpose : Return nodes common to two elements
543 //=======================================================================
545 vector< const SMDS_MeshNode*> SMESH_Algo::GetCommonNodes(const SMDS_MeshElement* e1,
546 const SMDS_MeshElement* e2)
548 vector< const SMDS_MeshNode*> common;
549 for ( int i = 0 ; i < e1->NbNodes(); ++i )
550 if ( e2->GetNodeIndex( e1->GetNode( i )) >= 0 )
551 common.push_back( e1->GetNode( i ));
555 //================================================================================
557 * \brief Sets event listener to submeshes if necessary
558 * \param subMesh - submesh where algo is set
560 * After being set, event listener is notified on each event of a submesh.
561 * By default non listener is set
563 //================================================================================
565 void SMESH_Algo::SetEventListener(SMESH_subMesh* /*subMesh*/)
569 //================================================================================
571 * \brief Allow algo to do something after persistent restoration
572 * \param subMesh - restored submesh
574 * This method is called only if a submesh has HYP_OK algo_state.
576 //================================================================================
578 void SMESH_Algo::SubmeshRestored(SMESH_subMesh* /*subMesh*/)
582 //================================================================================
584 * \brief Computes mesh without geometry
585 * \param aMesh - the mesh
586 * \param aHelper - helper that must be used for adding elements to \aaMesh
587 * \retval bool - is a success
589 //================================================================================
591 bool SMESH_Algo::Compute(SMESH_Mesh & /*aMesh*/, SMESH_MesherHelper* /*aHelper*/)
593 return error( COMPERR_BAD_INPUT_MESH, "Mesh built on shape expected");
596 //================================================================================
598 * \brief store error and comment and then return ( error == COMPERR_OK )
600 //================================================================================
602 bool SMESH_Algo::error(int error, const SMESH_Comment& comment)
606 return ( error == COMPERR_OK );
609 //================================================================================
611 * \brief store error and return ( error == COMPERR_OK )
613 //================================================================================
615 bool SMESH_Algo::error(SMESH_ComputeErrorPtr error)
618 _error = error->myName;
619 _comment = error->myComment;
620 _badInputElements = error->myBadElements;
621 return error->IsOK();
626 //================================================================================
628 * \brief return compute error
630 //================================================================================
632 SMESH_ComputeErrorPtr SMESH_Algo::GetComputeError() const
634 SMESH_ComputeErrorPtr err = SMESH_ComputeError::New( _error, _comment, this );
635 // hope this method is called by only SMESH_subMesh after this->Compute()
636 err->myBadElements.splice( err->myBadElements.end(),
637 (list<const SMDS_MeshElement*>&) _badInputElements );
641 //================================================================================
643 * \brief initialize compute error
645 //================================================================================
647 void SMESH_Algo::InitComputeError()
651 list<const SMDS_MeshElement*>::iterator elem = _badInputElements.begin();
652 for ( ; elem != _badInputElements.end(); ++elem )
653 if ( (*elem)->GetID() < 1 )
655 _badInputElements.clear();
658 //================================================================================
660 * \brief store a bad input element preventing computation,
661 * which may be a temporary one i.e. not residing the mesh,
662 * then it will be deleted by InitComputeError()
664 //================================================================================
666 void SMESH_Algo::addBadInputElement(const SMDS_MeshElement* elem)
669 _badInputElements.push_back( elem );