1 // SMESH SMESH : implementaion of SMESH idl descriptions
3 // Copyright (C) 2003 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
24 // File : SMESH_Algo.cxx
25 // Author : Paul RASCLE, EDF
29 #include "SMESH_Algo.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,
74 SMESH_Gen * gen):SMESH_Hypothesis(hypId, studyId, gen)
76 gen->_mapAlgo[hypId] = this;
78 _onlyUnaryInput = _requireDescretBoundary = _requireShape = true;
79 _quadraticMesh = false;
82 //=============================================================================
86 //=============================================================================
88 SMESH_Algo::~SMESH_Algo()
92 //=============================================================================
94 * Usually an algoritm has nothing to save
96 //=============================================================================
98 ostream & SMESH_Algo::SaveTo(ostream & save) { return save; }
99 istream & SMESH_Algo::LoadFrom(istream & load) { return load; }
101 //=============================================================================
105 //=============================================================================
107 const vector < string > &SMESH_Algo::GetCompatibleHypothesis()
109 return _compatibleHypothesis;
112 //=============================================================================
114 * List the hypothesis used by the algorithm associated to the shape.
115 * Hypothesis associated to father shape -are- taken into account (see
116 * GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
117 * the algorithm. This method could be surcharged by specific algorithms, in
118 * case of several hypothesis simultaneously applicable.
120 //=============================================================================
122 const list <const SMESHDS_Hypothesis *> &
123 SMESH_Algo::GetUsedHypothesis(SMESH_Mesh & aMesh,
124 const TopoDS_Shape & aShape,
125 const bool ignoreAuxiliary)
127 _usedHypList.clear();
128 SMESH_HypoFilter filter;
129 if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
131 aMesh.GetHypotheses( aShape, filter, _usedHypList, true );
132 if ( ignoreAuxiliary && _usedHypList.size() > 1 )
133 _usedHypList.clear(); //only one compatible hypothesis allowed
138 //=============================================================================
140 * List the relevant hypothesis associated to the shape. Relevant hypothesis
141 * have a name (type) listed in the algorithm. Hypothesis associated to
142 * father shape -are not- taken into account (see GetUsedHypothesis)
144 //=============================================================================
146 const list<const SMESHDS_Hypothesis *> &
147 SMESH_Algo::GetAppliedHypothesis(SMESH_Mesh & aMesh,
148 const TopoDS_Shape & aShape,
149 const bool ignoreAuxiliary)
151 _appliedHypList.clear();
152 SMESH_HypoFilter filter;
153 if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
154 aMesh.GetHypotheses( aShape, filter, _appliedHypList, false );
156 return _appliedHypList;
159 //=============================================================================
161 * Compute length of an edge
163 //=============================================================================
165 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
167 double UMin = 0, UMax = 0;
168 if (BRep_Tool::Degenerated(E))
171 Handle(Geom_Curve) C = BRep_Tool::Curve(E, L, UMin, UMax);
172 GeomAdaptor_Curve AdaptCurve(C);
173 double length = GCPnts_AbscissaPoint::Length(AdaptCurve, UMin, UMax);
177 //================================================================================
179 * \brief Find out elements orientation on a geometrical face
180 * \param theFace - The face correctly oriented in the shape being meshed
181 * \param theMeshDS - The mesh data structure
182 * \retval bool - true if the face normal and the normal of first element
183 * in the correspoding submesh point in different directions
185 //================================================================================
187 bool SMESH_Algo::IsReversedSubMesh (const TopoDS_Face& theFace,
188 SMESHDS_Mesh* theMeshDS)
190 if ( theFace.IsNull() || !theMeshDS )
193 // find out orientation of a meshed face
194 int faceID = theMeshDS->ShapeToIndex( theFace );
195 TopoDS_Shape aMeshedFace = theMeshDS->IndexToShape( faceID );
196 bool isReversed = ( theFace.Orientation() != aMeshedFace.Orientation() );
198 const SMESHDS_SubMesh * aSubMeshDSFace = theMeshDS->MeshElements( faceID );
199 if ( !aSubMeshDSFace )
202 // find element with node located on face and get its normal
203 const SMDS_FacePosition* facePos = 0;
207 bool normalOK = false;
208 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
209 while ( iteratorElem->more() ) // loop on elements on theFace
211 const SMDS_MeshElement* elem = iteratorElem->next();
212 if ( elem && elem->NbNodes() > 2 ) {
213 SMDS_ElemIteratorPtr nodesIt = elem->nodesIterator();
214 const SMDS_FacePosition* fPos = 0;
216 while ( nodesIt->more() ) { // loop on nodes
217 const SMDS_MeshNode* node
218 = static_cast<const SMDS_MeshNode *>(nodesIt->next());
220 nPnt[ i++ ].SetCoord( node->X(), node->Y(), node->Z() );
222 const SMDS_PositionPtr& pos = node->GetPosition();
223 if ( !pos ) continue;
224 if ( pos->GetTypeOfPosition() == SMDS_TOP_FACE ) {
225 fPos = dynamic_cast< const SMDS_FacePosition* >( pos.get() );
227 else if ( pos->GetTypeOfPosition() == SMDS_TOP_VERTEX ) {
228 vID = pos->GetShapeId();
231 if ( fPos || ( !normalOK && vID )) {
233 gp_Vec v01( nPnt[0], nPnt[1] ), v02( nPnt[0], nPnt[2] );
234 if ( v01.SquareMagnitude() > RealSmall() &&
235 v02.SquareMagnitude() > RealSmall() )
238 normalOK = ( Ne.SquareMagnitude() > RealSmall() );
240 // we need position on theFace or at least on vertex
243 if ((facePos = fPos))
252 // node position on face
255 u = facePos->GetUParameter();
256 v = facePos->GetVParameter();
258 else if ( vertexID ) {
259 TopoDS_Shape V = theMeshDS->IndexToShape( vertexID );
260 if ( V.IsNull() || V.ShapeType() != TopAbs_VERTEX )
262 gp_Pnt2d uv = BRep_Tool::Parameters( TopoDS::Vertex( V ), theFace );
271 // face normal at node position
273 Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
274 if ( surf.IsNull() || surf->Continuity() < GeomAbs_C1 ) return isReversed;
276 surf->D1( u, v, nPnt[0], d1u, d1v );
277 gp_Vec Nf = (d1u ^ d1v).Transformed( loc );
279 if ( theFace.Orientation() == TopAbs_REVERSED )
285 //================================================================================
287 * \brief Initialize my parameter values by the mesh built on the geometry
288 * \param theMesh - the built mesh
289 * \param theShape - the geometry of interest
290 * \retval bool - true if parameter values have been successfully defined
292 * Just return false as the algorithm does not hold parameters values
294 //================================================================================
296 bool SMESH_Algo::SetParametersByMesh(const SMESH_Mesh* /*theMesh*/,
297 const TopoDS_Shape& /*theShape*/)
302 //================================================================================
304 * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
305 * \param theMesh - The mesh containing nodes
306 * \param theEdge - The geometrical edge of interest
307 * \param theParams - The resulting vector of sorted node parameters
308 * \retval bool - false if not all parameters are OK
310 //================================================================================
312 bool SMESH_Algo::GetNodeParamOnEdge(const SMESHDS_Mesh* theMesh,
313 const TopoDS_Edge& theEdge,
314 vector< double > & theParams)
318 if ( !theMesh || theEdge.IsNull() )
321 SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
322 if ( !eSubMesh || !eSubMesh->GetElements()->more() )
323 return false; // edge is not meshed
326 set < double > paramSet;
329 // loop on nodes of an edge: sort them by param on edge
330 SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
331 while ( nIt->more() )
333 const SMDS_MeshNode* node = nIt->next();
334 const SMDS_PositionPtr& pos = node->GetPosition();
335 if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
337 const SMDS_EdgePosition* epos =
338 static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
339 paramSet.insert( epos->GetUParameter() );
343 // add vertex nodes params
345 BRep_Tool::Range(theEdge, f, l);
346 paramSet.insert( f );
347 paramSet.insert( l );
348 if ( paramSet.size() != nbEdgeNodes + 2 )
349 return false; // there are equal parameters
352 theParams.resize( paramSet.size() );
353 set < double >::iterator par = paramSet.begin();
354 vector< double >::iterator vecPar = theParams.begin();
355 for ( ; par != paramSet.end(); ++par, ++vecPar )
358 return theParams.size() > 1;
361 //================================================================================
363 * \brief Make filter recognize only compatible hypotheses
364 * \param theFilter - the filter to initialize
365 * \param ignoreAuxiliary - make filter ignore compatible auxiliary hypotheses
367 //================================================================================
369 bool SMESH_Algo::InitCompatibleHypoFilter( SMESH_HypoFilter & theFilter,
370 const bool ignoreAuxiliary) const
372 if ( !_compatibleHypothesis.empty() )
374 theFilter.Init( theFilter.HasName( _compatibleHypothesis[0] ));
375 for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
376 theFilter.Or( theFilter.HasName( _compatibleHypothesis[ i ] ));
378 if ( ignoreAuxiliary )
379 theFilter.AndNot( theFilter.IsAuxiliary() );
386 //================================================================================
388 * \brief Return continuity of two edges
389 * \param E1 - the 1st edge
390 * \param E2 - the 2nd edge
391 * \retval GeomAbs_Shape - regularity at the junction between E1 and E2
393 //================================================================================
395 GeomAbs_Shape SMESH_Algo::Continuity(const TopoDS_Edge & E1,
396 const TopoDS_Edge & E2)
398 TopoDS_Vertex V = TopExp::LastVertex (E1, true);
399 if ( !V.IsSame( TopExp::FirstVertex(E2, true )))
400 if ( !TopExp::CommonVertex( E1, E2, V ))
402 Standard_Real u1 = BRep_Tool::Parameter( V, E1 );
403 Standard_Real u2 = BRep_Tool::Parameter( V, E2 );
404 BRepAdaptor_Curve C1( E1 ), C2( E2 );
406 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
409 return BRepLProp::Continuity(C1, C2, u1, u2);
411 catch (Standard_Failure) {
416 //================================================================================
418 * \brief Return the node built on a vertex
419 * \param V - the vertex
420 * \param meshDS - mesh
421 * \retval const SMDS_MeshNode* - found node or NULL
423 //================================================================================
425 const SMDS_MeshNode* SMESH_Algo::VertexNode(const TopoDS_Vertex& V,
426 SMESHDS_Mesh* meshDS)
428 if ( SMESHDS_SubMesh* sm = meshDS->MeshElements(V) ) {
429 SMDS_NodeIteratorPtr nIt= sm->GetNodes();
436 //================================================================================
438 * \brief Sets event listener to submeshes if necessary
439 * \param subMesh - submesh where algo is set
441 * After being set, event listener is notified on each event of a submesh.
442 * By default non listener is set
444 //================================================================================
446 void SMESH_Algo::SetEventListener(SMESH_subMesh* /*subMesh*/)
450 //================================================================================
452 * \brief Allow algo to do something after persistent restoration
453 * \param subMesh - restored submesh
455 * This method is called only if a submesh has HYP_OK algo_state.
457 //================================================================================
459 void SMESH_Algo::SubmeshRestored(SMESH_subMesh* /*subMesh*/)
463 //================================================================================
465 * \brief Computes mesh without geometry
466 * \param aMesh - the mesh
467 * \param aHelper - helper that must be used for adding elements to \aaMesh
468 * \retval bool - is a success
470 //================================================================================
472 bool SMESH_Algo::Compute(SMESH_Mesh & /*aMesh*/, SMESH_MesherHelper* aHelper)