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 <BRep_Tool.hxx>
41 #include <GCPnts_AbscissaPoint.hxx>
42 #include <GeomAdaptor_Curve.hxx>
43 #include <Geom_Surface.hxx>
44 #include <TopLoc_Location.hxx>
45 #include <TopTools_ListIteratorOfListOfShape.hxx>
46 #include <TopTools_ListOfShape.hxx>
48 #include <TopoDS_Face.hxx>
50 #include <gp_Pnt2d.hxx>
53 #include "utilities.h"
59 //=============================================================================
63 //=============================================================================
65 SMESH_Algo::SMESH_Algo(int hypId, int studyId,
66 SMESH_Gen * gen):SMESH_Hypothesis(hypId, studyId, gen)
68 // _compatibleHypothesis.push_back("hypothese_bidon");
70 gen->_mapAlgo[hypId] = this;
72 _onlyUnaryInput = _requireDescretBoundary = true;
73 _quadraticMesh = false;
76 //=============================================================================
80 //=============================================================================
82 SMESH_Algo::~SMESH_Algo()
86 //=============================================================================
88 * Usually an algoritm has nothing to save
90 //=============================================================================
92 ostream & SMESH_Algo::SaveTo(ostream & save) { return save; }
93 istream & SMESH_Algo::LoadFrom(istream & load) { return load; }
95 //=============================================================================
99 //=============================================================================
101 const vector < string > &SMESH_Algo::GetCompatibleHypothesis()
103 return _compatibleHypothesis;
106 //=============================================================================
108 * List the hypothesis used by the algorithm associated to the shape.
109 * Hypothesis associated to father shape -are- taken into account (see
110 * GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
111 * the algorithm. This method could be surcharged by specific algorithms, in
112 * case of several hypothesis simultaneously applicable.
114 //=============================================================================
116 const list <const SMESHDS_Hypothesis *> &
117 SMESH_Algo::GetUsedHypothesis(SMESH_Mesh & aMesh,
118 const TopoDS_Shape & aShape,
119 const bool ignoreAuxiliary)
121 _usedHypList.clear();
122 SMESH_HypoFilter filter;
123 if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
125 aMesh.GetHypotheses( aShape, filter, _usedHypList, true );
126 if ( ignoreAuxiliary && _usedHypList.size() > 1 )
127 _usedHypList.clear(); //only one compatible hypothesis allowed
132 //=============================================================================
134 * List the relevant hypothesis associated to the shape. Relevant hypothesis
135 * have a name (type) listed in the algorithm. Hypothesis associated to
136 * father shape -are not- taken into account (see GetUsedHypothesis)
138 //=============================================================================
140 const list<const SMESHDS_Hypothesis *> &
141 SMESH_Algo::GetAppliedHypothesis(SMESH_Mesh & aMesh,
142 const TopoDS_Shape & aShape,
143 const bool ignoreAuxiliary)
145 _appliedHypList.clear();
146 SMESH_HypoFilter filter;
147 if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
148 aMesh.GetHypotheses( aShape, filter, _appliedHypList, false );
150 return _appliedHypList;
153 //=============================================================================
155 * Compute length of an edge
157 //=============================================================================
159 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
161 double UMin = 0, UMax = 0;
162 if (BRep_Tool::Degenerated(E))
165 Handle(Geom_Curve) C = BRep_Tool::Curve(E, L, UMin, UMax);
166 GeomAdaptor_Curve AdaptCurve(C);
167 GCPnts_AbscissaPoint gabs;
168 double length = gabs.Length(AdaptCurve, UMin, UMax);
172 //================================================================================
174 * \brief Find out elements orientation on a geometrical face
175 * \param theFace - The face correctly oriented in the shape being meshed
176 * \param theMeshDS - The mesh data structure
177 * \retval bool - true if the face normal and the normal of first element
178 * in the correspoding submesh point in different directions
180 //================================================================================
182 bool SMESH_Algo::IsReversedSubMesh (const TopoDS_Face& theFace,
183 SMESHDS_Mesh* theMeshDS)
185 if ( theFace.IsNull() || !theMeshDS )
188 // find out orientation of a meshed face
189 int faceID = theMeshDS->ShapeToIndex( theFace );
190 TopoDS_Shape aMeshedFace = theMeshDS->IndexToShape( faceID );
191 bool isReversed = ( theFace.Orientation() != aMeshedFace.Orientation() );
193 const SMESHDS_SubMesh * aSubMeshDSFace = theMeshDS->MeshElements( faceID );
194 if ( !aSubMeshDSFace )
197 // find element with node located on face and get its normal
198 const SMDS_FacePosition* facePos = 0;
202 bool normalOK = false;
203 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
204 while ( iteratorElem->more() ) // loop on elements on theFace
206 const SMDS_MeshElement* elem = iteratorElem->next();
207 if ( elem && elem->NbNodes() > 2 ) {
208 SMDS_ElemIteratorPtr nodesIt = elem->nodesIterator();
209 const SMDS_FacePosition* fPos = 0;
211 while ( nodesIt->more() ) { // loop on nodes
212 const SMDS_MeshNode* node
213 = static_cast<const SMDS_MeshNode *>(nodesIt->next());
215 nPnt[ i++ ].SetCoord( node->X(), node->Y(), node->Z() );
217 const SMDS_PositionPtr& pos = node->GetPosition();
218 if ( !pos ) continue;
219 if ( pos->GetTypeOfPosition() == SMDS_TOP_FACE ) {
220 fPos = dynamic_cast< const SMDS_FacePosition* >( pos.get() );
222 else if ( pos->GetTypeOfPosition() == SMDS_TOP_VERTEX ) {
223 vID = pos->GetShapeId();
226 if ( fPos || ( !normalOK && vID )) {
228 gp_Vec v01( nPnt[0], nPnt[1] ), v02( nPnt[0], nPnt[2] );
229 if ( v01.SquareMagnitude() > RealSmall() &&
230 v02.SquareMagnitude() > RealSmall() )
233 normalOK = ( Ne.SquareMagnitude() > RealSmall() );
235 // we need position on theFace or at least on vertex
238 if ((facePos = fPos))
247 // node position on face
250 u = facePos->GetUParameter();
251 v = facePos->GetVParameter();
253 else if ( vertexID ) {
254 TopoDS_Shape V = theMeshDS->IndexToShape( vertexID );
255 if ( V.IsNull() || V.ShapeType() != TopAbs_VERTEX )
257 gp_Pnt2d uv = BRep_Tool::Parameters( TopoDS::Vertex( V ), theFace );
266 // face normal at node position
268 Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
269 if ( surf.IsNull() || surf->Continuity() < GeomAbs_C1 ) return isReversed;
271 surf->D1( u, v, nPnt[0], d1u, d1v );
272 gp_Vec Nf = (d1u ^ d1v).Transformed( loc );
274 if ( theFace.Orientation() == TopAbs_REVERSED )
280 //================================================================================
282 * \brief Initialize my parameter values by the mesh built on the geometry
283 * \param theMesh - the built mesh
284 * \param theShape - the geometry of interest
285 * \retval bool - true if parameter values have been successfully defined
287 * Just return false as the algorithm does not hold parameters values
289 //================================================================================
291 bool SMESH_Algo::SetParametersByMesh(const SMESH_Mesh* /*theMesh*/,
292 const TopoDS_Shape& /*theShape*/)
297 //================================================================================
299 * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
300 * \param theMesh - The mesh containing nodes
301 * \param theEdge - The geometrical edge of interest
302 * \param theParams - The resulting vector of sorted node parameters
303 * \retval bool - false if not all parameters are OK
305 //================================================================================
307 bool SMESH_Algo::GetNodeParamOnEdge(const SMESHDS_Mesh* theMesh,
308 const TopoDS_Edge& theEdge,
309 vector< double > & theParams)
313 if ( !theMesh || theEdge.IsNull() )
316 SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
317 if ( !eSubMesh || !eSubMesh->GetElements()->more() )
318 return false; // edge is not meshed
321 set < double > paramSet;
324 // loop on nodes of an edge: sort them by param on edge
325 SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
326 while ( nIt->more() )
328 const SMDS_MeshNode* node = nIt->next();
329 const SMDS_PositionPtr& pos = node->GetPosition();
330 if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
332 const SMDS_EdgePosition* epos =
333 static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
334 paramSet.insert( epos->GetUParameter() );
338 // add vertex nodes params
340 BRep_Tool::Range(theEdge, f, l);
341 paramSet.insert( f );
342 paramSet.insert( l );
343 if ( paramSet.size() != nbEdgeNodes + 2 )
344 return false; // there are equal parameters
347 theParams.resize( paramSet.size() );
348 set < double >::iterator par = paramSet.begin();
349 vector< double >::iterator vecPar = theParams.begin();
350 for ( ; par != paramSet.end(); ++par, ++vecPar )
353 return theParams.size() > 1;
356 //================================================================================
358 * \brief Make filter recognize only compatible hypotheses
359 * \param theFilter - the filter to initialize
360 * \param ignoreAuxiliary - make filter ignore compatible auxiliary hypotheses
362 //================================================================================
364 bool SMESH_Algo::InitCompatibleHypoFilter( SMESH_HypoFilter & theFilter,
365 const bool ignoreAuxiliary) const
367 if ( !_compatibleHypothesis.empty() )
369 theFilter.Init( theFilter.HasName( _compatibleHypothesis[0] ));
370 for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
371 theFilter.Or( theFilter.HasName( _compatibleHypothesis[ i ] ));
373 if ( ignoreAuxiliary )
374 theFilter.AndNot( theFilter.IsAuxiliary() );
381 //================================================================================
383 * \brief Sets event listener to submeshes if necessary
384 * \param subMesh - submesh where algo is set
386 * After being set, event listener is notified on each event of a submesh.
387 * By default non listener is set
389 //================================================================================
391 void SMESH_Algo::SetEventListener(SMESH_subMesh* /*subMesh*/)