1 // Copyright (C) 2007-2008 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
22 // SMESH SMESH : implementaion of SMESH idl descriptions
23 // File : SMESH_Algo.cxx
24 // 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"
67 //=============================================================================
71 //=============================================================================
73 SMESH_Algo::SMESH_Algo (int hypId, int studyId, SMESH_Gen * gen)
74 : SMESH_Hypothesis(hypId, studyId, gen)
76 gen->_mapAlgo[hypId] = this;
78 _onlyUnaryInput = _requireDescretBoundary = _requireShape = true;
79 _quadraticMesh = false;
83 //=============================================================================
87 //=============================================================================
89 SMESH_Algo::~SMESH_Algo()
93 //=============================================================================
95 * Usually an algoritm has nothing to save
97 //=============================================================================
99 ostream & SMESH_Algo::SaveTo(ostream & save) { return save; }
100 istream & SMESH_Algo::LoadFrom(istream & load) { return load; }
102 //=============================================================================
106 //=============================================================================
108 const vector < string > &SMESH_Algo::GetCompatibleHypothesis()
110 return _compatibleHypothesis;
113 //=============================================================================
115 * List the hypothesis used by the algorithm associated to the shape.
116 * Hypothesis associated to father shape -are- taken into account (see
117 * GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
118 * the algorithm. This method could be surcharged by specific algorithms, in
119 * case of several hypothesis simultaneously applicable.
121 //=============================================================================
123 const list <const SMESHDS_Hypothesis *> &
124 SMESH_Algo::GetUsedHypothesis(SMESH_Mesh & aMesh,
125 const TopoDS_Shape & aShape,
126 const bool ignoreAuxiliary)
128 _usedHypList.clear();
129 SMESH_HypoFilter filter;
130 if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
132 aMesh.GetHypotheses( aShape, filter, _usedHypList, true );
133 if ( ignoreAuxiliary && _usedHypList.size() > 1 )
134 _usedHypList.clear(); //only one compatible hypothesis allowed
139 //=============================================================================
141 * List the relevant hypothesis associated to the shape. Relevant hypothesis
142 * have a name (type) listed in the algorithm. Hypothesis associated to
143 * father shape -are not- taken into account (see GetUsedHypothesis)
145 //=============================================================================
147 const list<const SMESHDS_Hypothesis *> &
148 SMESH_Algo::GetAppliedHypothesis(SMESH_Mesh & aMesh,
149 const TopoDS_Shape & aShape,
150 const bool ignoreAuxiliary)
152 _appliedHypList.clear();
153 SMESH_HypoFilter filter;
154 if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
155 aMesh.GetHypotheses( aShape, filter, _appliedHypList, false );
157 return _appliedHypList;
160 //=============================================================================
162 * Compute length of an edge
164 //=============================================================================
166 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
168 double UMin = 0, UMax = 0;
169 if (BRep_Tool::Degenerated(E))
172 Handle(Geom_Curve) C = BRep_Tool::Curve(E, L, UMin, UMax);
173 GeomAdaptor_Curve AdaptCurve(C);
174 double length = GCPnts_AbscissaPoint::Length(AdaptCurve, UMin, UMax);
178 //================================================================================
180 * \brief Find out elements orientation on a geometrical face
181 * \param theFace - The face correctly oriented in the shape being meshed
182 * \param theMeshDS - The mesh data structure
183 * \retval bool - true if the face normal and the normal of first element
184 * in the correspoding submesh point in different directions
186 //================================================================================
188 bool SMESH_Algo::IsReversedSubMesh (const TopoDS_Face& theFace,
189 SMESHDS_Mesh* theMeshDS)
191 if ( theFace.IsNull() || !theMeshDS )
194 // find out orientation of a meshed face
195 int faceID = theMeshDS->ShapeToIndex( theFace );
196 TopoDS_Shape aMeshedFace = theMeshDS->IndexToShape( faceID );
197 bool isReversed = ( theFace.Orientation() != aMeshedFace.Orientation() );
199 const SMESHDS_SubMesh * aSubMeshDSFace = theMeshDS->MeshElements( faceID );
200 if ( !aSubMeshDSFace )
203 // find element with node located on face and get its normal
204 const SMDS_FacePosition* facePos = 0;
208 bool normalOK = false;
209 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
210 while ( iteratorElem->more() ) // loop on elements on theFace
212 const SMDS_MeshElement* elem = iteratorElem->next();
213 if ( elem && elem->NbNodes() > 2 ) {
214 SMDS_ElemIteratorPtr nodesIt = elem->nodesIterator();
215 const SMDS_FacePosition* fPos = 0;
217 while ( nodesIt->more() ) { // loop on nodes
218 const SMDS_MeshNode* node
219 = static_cast<const SMDS_MeshNode *>(nodesIt->next());
221 nPnt[ i++ ].SetCoord( node->X(), node->Y(), node->Z() );
223 const SMDS_PositionPtr& pos = node->GetPosition();
224 if ( !pos ) continue;
225 if ( pos->GetTypeOfPosition() == SMDS_TOP_FACE ) {
226 fPos = dynamic_cast< const SMDS_FacePosition* >( pos.get() );
228 else if ( pos->GetTypeOfPosition() == SMDS_TOP_VERTEX ) {
229 vID = pos->GetShapeId();
232 if ( fPos || ( !normalOK && vID )) {
234 gp_Vec v01( nPnt[0], nPnt[1] ), v02( nPnt[0], nPnt[2] );
235 if ( v01.SquareMagnitude() > RealSmall() &&
236 v02.SquareMagnitude() > RealSmall() )
239 normalOK = ( Ne.SquareMagnitude() > RealSmall() );
241 // we need position on theFace or at least on vertex
244 if ((facePos = fPos))
253 // node position on face
256 u = facePos->GetUParameter();
257 v = facePos->GetVParameter();
259 else if ( vertexID ) {
260 TopoDS_Shape V = theMeshDS->IndexToShape( vertexID );
261 if ( V.IsNull() || V.ShapeType() != TopAbs_VERTEX )
263 gp_Pnt2d uv = BRep_Tool::Parameters( TopoDS::Vertex( V ), theFace );
272 // face normal at node position
274 Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
275 if ( surf.IsNull() || surf->Continuity() < GeomAbs_C1 ) return isReversed;
277 surf->D1( u, v, nPnt[0], d1u, d1v );
278 gp_Vec Nf = (d1u ^ d1v).Transformed( loc );
280 if ( theFace.Orientation() == TopAbs_REVERSED )
286 //================================================================================
288 * \brief Just return false as the algorithm does not hold parameters values
290 //================================================================================
292 bool SMESH_Algo::SetParametersByMesh(const SMESH_Mesh* /*theMesh*/,
293 const TopoDS_Shape& /*theShape*/)
298 //================================================================================
300 * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
301 * \param theMesh - The mesh containing nodes
302 * \param theEdge - The geometrical edge of interest
303 * \param theParams - The resulting vector of sorted node parameters
304 * \retval bool - false if not all parameters are OK
306 //================================================================================
308 bool SMESH_Algo::GetNodeParamOnEdge(const SMESHDS_Mesh* theMesh,
309 const TopoDS_Edge& theEdge,
310 vector< double > & theParams)
314 if ( !theMesh || theEdge.IsNull() )
317 SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
318 if ( !eSubMesh || !eSubMesh->GetElements()->more() )
319 return false; // edge is not meshed
322 set < double > paramSet;
325 // loop on nodes of an edge: sort them by param on edge
326 SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
327 while ( nIt->more() )
329 const SMDS_MeshNode* node = nIt->next();
330 const SMDS_PositionPtr& pos = node->GetPosition();
331 if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
333 const SMDS_EdgePosition* epos =
334 static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
335 if ( !paramSet.insert( epos->GetUParameter() ).second )
336 return false; // equal parameters
339 // add vertex nodes params
341 TopExp::Vertices( theEdge, V1, V2);
342 if ( VertexNode( V1, theMesh ) &&
343 !paramSet.insert( BRep_Tool::Parameter(V1,theEdge) ).second )
344 return false; // there are equal parameters
345 if ( VertexNode( V2, theMesh ) &&
346 !paramSet.insert( BRep_Tool::Parameter(V2,theEdge) ).second )
347 return false; // there are equal parameters
350 theParams.resize( paramSet.size() );
351 set < double >::iterator par = paramSet.begin();
352 vector< double >::iterator vecPar = theParams.begin();
353 for ( ; par != paramSet.end(); ++par, ++vecPar )
356 return theParams.size() > 1;
359 //================================================================================
361 * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
362 * \param theMesh - The mesh containing nodes
363 * \param theEdge - The geometrical edge of interest
364 * \param theParams - The resulting vector of sorted node parameters
365 * \retval bool - false if not all parameters are OK
367 //================================================================================
369 bool SMESH_Algo::GetSortedNodesOnEdge(const SMESHDS_Mesh* theMesh,
370 const TopoDS_Edge& theEdge,
371 const bool ignoreMediumNodes,
372 map< double, const SMDS_MeshNode* > & theNodes)
376 if ( !theMesh || theEdge.IsNull() )
379 SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
380 if ( !eSubMesh || !eSubMesh->GetElements()->more() )
381 return false; // edge is not meshed
384 set < double > paramSet;
387 // loop on nodes of an edge: sort them by param on edge
388 SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
389 while ( nIt->more() )
391 const SMDS_MeshNode* node = nIt->next();
392 if ( ignoreMediumNodes ) {
393 SMDS_ElemIteratorPtr elemIt = node->GetInverseElementIterator();
394 if ( elemIt->more() && elemIt->next()->IsMediumNode( node ))
397 const SMDS_PositionPtr& pos = node->GetPosition();
398 if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
400 const SMDS_EdgePosition* epos =
401 static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
402 theNodes.insert( make_pair( epos->GetUParameter(), node ));
407 TopoDS_Vertex v1, v2;
408 TopExp::Vertices(theEdge, v1, v2);
409 const SMDS_MeshNode* n1 = VertexNode( v1, (SMESHDS_Mesh*) theMesh );
410 const SMDS_MeshNode* n2 = VertexNode( v2, (SMESHDS_Mesh*) theMesh );
412 BRep_Tool::Range(theEdge, f, l);
413 if ( v1.Orientation() != TopAbs_FORWARD )
415 if ( n1 && ++nbNodes )
416 theNodes.insert( make_pair( f, n1 ));
417 if ( n2 && ++nbNodes )
418 theNodes.insert( make_pair( l, n2 ));
420 return theNodes.size() == nbNodes;
423 //================================================================================
425 * \brief Make filter recognize only compatible hypotheses
426 * \param theFilter - the filter to initialize
427 * \param ignoreAuxiliary - make filter ignore compatible auxiliary hypotheses
429 //================================================================================
431 bool SMESH_Algo::InitCompatibleHypoFilter( SMESH_HypoFilter & theFilter,
432 const bool ignoreAuxiliary) const
434 if ( !_compatibleHypothesis.empty() )
436 theFilter.Init( theFilter.HasName( _compatibleHypothesis[0] ));
437 for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
438 theFilter.Or( theFilter.HasName( _compatibleHypothesis[ i ] ));
440 if ( ignoreAuxiliary )
441 theFilter.AndNot( theFilter.IsAuxiliary() );
448 //================================================================================
450 * \brief Return continuity of two edges
451 * \param E1 - the 1st edge
452 * \param E2 - the 2nd edge
453 * \retval GeomAbs_Shape - regularity at the junction between E1 and E2
455 //================================================================================
457 GeomAbs_Shape SMESH_Algo::Continuity(const TopoDS_Edge & E1,
458 const TopoDS_Edge & E2)
460 TopoDS_Vertex V = TopExp::LastVertex (E1, true);
461 if ( !V.IsSame( TopExp::FirstVertex(E2, true )))
462 if ( !TopExp::CommonVertex( E1, E2, V ))
464 Standard_Real u1 = BRep_Tool::Parameter( V, E1 );
465 Standard_Real u2 = BRep_Tool::Parameter( V, E2 );
466 BRepAdaptor_Curve C1( E1 ), C2( E2 );
467 Standard_Real tol = BRep_Tool::Tolerance( V );
468 Standard_Real angTol = 2e-3;
470 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
473 return BRepLProp::Continuity(C1, C2, u1, u2, tol, angTol);
475 catch (Standard_Failure) {
480 //================================================================================
482 * \brief Return the node built on a vertex
483 * \param V - the vertex
484 * \param meshDS - mesh
485 * \retval const SMDS_MeshNode* - found node or NULL
487 //================================================================================
489 const SMDS_MeshNode* SMESH_Algo::VertexNode(const TopoDS_Vertex& V,
490 const SMESHDS_Mesh* meshDS)
492 if ( SMESHDS_SubMesh* sm = meshDS->MeshElements(V) ) {
493 SMDS_NodeIteratorPtr nIt= sm->GetNodes();
500 //================================================================================
502 * \brief Sets event listener to submeshes if necessary
503 * \param subMesh - submesh where algo is set
505 * After being set, event listener is notified on each event of a submesh.
506 * By default non listener is set
508 //================================================================================
510 void SMESH_Algo::SetEventListener(SMESH_subMesh* /*subMesh*/)
514 //================================================================================
516 * \brief Allow algo to do something after persistent restoration
517 * \param subMesh - restored submesh
519 * This method is called only if a submesh has HYP_OK algo_state.
521 //================================================================================
523 void SMESH_Algo::SubmeshRestored(SMESH_subMesh* /*subMesh*/)
527 //================================================================================
529 * \brief Computes mesh without geometry
530 * \param aMesh - the mesh
531 * \param aHelper - helper that must be used for adding elements to \aaMesh
532 * \retval bool - is a success
534 //================================================================================
536 bool SMESH_Algo::Compute(SMESH_Mesh & /*aMesh*/, SMESH_MesherHelper* /*aHelper*/)
538 return error( COMPERR_BAD_INPUT_MESH, "Mesh built on shape expected");
541 //================================================================================
543 * \brief store error and comment and then return ( error == COMPERR_OK )
545 //================================================================================
547 bool SMESH_Algo::error(int error, const SMESH_Comment& comment)
551 return ( error == COMPERR_OK );
554 //================================================================================
556 * \brief store error and return ( error == COMPERR_OK )
558 //================================================================================
560 bool SMESH_Algo::error(SMESH_ComputeErrorPtr error)
563 _error = error->myName;
564 _comment = error->myComment;
565 _badInputElements = error->myBadElements;
566 return error->IsOK();
571 //================================================================================
573 * \brief return compute error
575 //================================================================================
577 SMESH_ComputeErrorPtr SMESH_Algo::GetComputeError() const
579 SMESH_ComputeErrorPtr err = SMESH_ComputeError::New( _error, _comment, this );
580 // hope this method is called by only SMESH_subMesh after this->Compute()
581 err->myBadElements.splice( err->myBadElements.end(),
582 (list<const SMDS_MeshElement*>&) _badInputElements );
586 //================================================================================
588 * \brief initialize compute error
590 //================================================================================
592 void SMESH_Algo::InitComputeError()
596 list<const SMDS_MeshElement*>::iterator elem = _badInputElements.begin();
597 for ( ; elem != _badInputElements.end(); ++elem )
598 if ( (*elem)->GetID() < 1 )
600 _badInputElements.clear();
603 //================================================================================
605 * \brief store a bad input element preventing computation,
606 * which may be a temporary one i.e. not residing the mesh,
607 * then it will be deleted by InitComputeError()
609 //================================================================================
611 void SMESH_Algo::addBadInputElement(const SMDS_MeshElement* elem)
614 _badInputElements.push_back( elem );