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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
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;
75 //=============================================================================
79 //=============================================================================
81 SMESH_Algo::~SMESH_Algo()
85 //=============================================================================
89 //=============================================================================
91 const vector < string > &SMESH_Algo::GetCompatibleHypothesis()
93 return _compatibleHypothesis;
96 //=============================================================================
98 * List the hypothesis used by the algorithm associated to the shape.
99 * Hypothesis associated to father shape -are- taken into account (see
100 * GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
101 * the algorithm. This method could be surcharged by specific algorithms, in
102 * case of several hypothesis simultaneously applicable.
104 //=============================================================================
106 const list <const SMESHDS_Hypothesis *> & SMESH_Algo::GetUsedHypothesis(
107 SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
109 _usedHypList.clear();
110 if ( !_compatibleHypothesis.empty() )
112 SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( _compatibleHypothesis[0] ));
113 for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
114 filter.Or( filter.HasName( _compatibleHypothesis[ i ] ));
116 aMesh.GetHypotheses( aShape, filter, _usedHypList, true );
117 if ( _usedHypList.size() > 1 )
118 _usedHypList.clear(); //only one compatible hypothesis allowed
123 //=============================================================================
125 * List the relevant hypothesis associated to the shape. Relevant hypothesis
126 * have a name (type) listed in the algorithm. Hypothesis associated to
127 * father shape -are not- taken into account (see GetUsedHypothesis)
129 //=============================================================================
131 const list<const SMESHDS_Hypothesis *> & SMESH_Algo::GetAppliedHypothesis(
132 SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
134 _appliedHypList.clear();
135 if ( !_compatibleHypothesis.empty() )
137 SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( _compatibleHypothesis[0] ));
138 for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
139 filter.Or( filter.HasName( _compatibleHypothesis[ i ] ));
141 aMesh.GetHypotheses( aShape, filter, _appliedHypList, false );
143 return _appliedHypList;
146 //=============================================================================
148 * Compute length of an edge
150 //=============================================================================
152 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
154 double UMin = 0, UMax = 0;
155 if (BRep_Tool::Degenerated(E))
158 Handle(Geom_Curve) C = BRep_Tool::Curve(E, L, UMin, UMax);
159 GeomAdaptor_Curve AdaptCurve(C);
160 GCPnts_AbscissaPoint gabs;
161 double length = gabs.Length(AdaptCurve, UMin, UMax);
165 //================================================================================
167 * \brief Find out elements orientation on a geometrical face
168 * \param theFace - The face correctly oriented in the shape being meshed
169 * \param theMeshDS - The mesh data structure
170 * \retval bool - true if the face normal and the normal of first element
171 * in the correspoding submesh point in different directions
173 //================================================================================
175 bool SMESH_Algo::IsReversedSubMesh (const TopoDS_Face& theFace,
176 SMESHDS_Mesh* theMeshDS)
178 if ( theFace.IsNull() || !theMeshDS )
181 // find out orientation of a meshed face
182 int faceID = theMeshDS->ShapeToIndex( theFace );
183 TopoDS_Shape aMeshedFace = theMeshDS->IndexToShape( faceID );
184 bool isReversed = ( theFace.Orientation() != aMeshedFace.Orientation() );
186 const SMESHDS_SubMesh * aSubMeshDSFace = theMeshDS->MeshElements( faceID );
187 if ( !aSubMeshDSFace )
190 // find element with node located on face and get its normal
191 const SMDS_FacePosition* facePos = 0;
195 bool normalOK = false;
196 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
197 while ( iteratorElem->more() ) // loop on elements on theFace
199 const SMDS_MeshElement* elem = iteratorElem->next();
200 if ( elem && elem->NbNodes() > 2 ) {
201 SMDS_ElemIteratorPtr nodesIt = elem->nodesIterator();
202 const SMDS_FacePosition* fPos = 0;
204 while ( nodesIt->more() ) { // loop on nodes
205 const SMDS_MeshNode* node
206 = static_cast<const SMDS_MeshNode *>(nodesIt->next());
208 nPnt[ i++ ].SetCoord( node->X(), node->Y(), node->Z() );
210 const SMDS_PositionPtr& pos = node->GetPosition();
211 if ( !pos ) continue;
212 if ( pos->GetTypeOfPosition() == SMDS_TOP_FACE ) {
213 fPos = dynamic_cast< const SMDS_FacePosition* >( pos.get() );
215 else if ( pos->GetTypeOfPosition() == SMDS_TOP_VERTEX ) {
216 vID = pos->GetShapeId();
219 if ( fPos || ( !normalOK && vID )) {
221 gp_Vec v01( nPnt[0], nPnt[1] ), v02( nPnt[0], nPnt[2] );
222 if ( v01.SquareMagnitude() > RealSmall() &&
223 v02.SquareMagnitude() > RealSmall() )
226 normalOK = ( Ne.SquareMagnitude() > RealSmall() );
228 // we need position on theFace or at least on vertex
231 if ((facePos = fPos))
240 // node position on face
243 u = facePos->GetUParameter();
244 v = facePos->GetVParameter();
246 else if ( vertexID ) {
247 TopoDS_Shape V = theMeshDS->IndexToShape( vertexID );
248 if ( V.IsNull() || V.ShapeType() != TopAbs_VERTEX )
250 gp_Pnt2d uv = BRep_Tool::Parameters( TopoDS::Vertex( V ), theFace );
259 // face normal at node position
261 Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
262 if ( surf.IsNull() || surf->Continuity() < GeomAbs_C1 ) return isReversed;
264 surf->D1( u, v, nPnt[0], d1u, d1v );
265 gp_Vec Nf = (d1u ^ d1v).Transformed( loc );
267 if ( theFace.Orientation() == TopAbs_REVERSED )
273 //================================================================================
275 * \brief Initialize my parameter values by the mesh built on the geometry
276 * \param theMesh - the built mesh
277 * \param theShape - the geometry of interest
278 * \retval bool - true if parameter values have been successfully defined
280 * Just return false as the algorithm does not hold parameters values
282 //================================================================================
284 bool SMESH_Algo::SetParametersByMesh(const SMESH_Mesh* /*theMesh*/,
285 const TopoDS_Shape& /*theShape*/)
290 //================================================================================
292 * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
293 * \param theMesh - The mesh containing nodes
294 * \param theEdge - The geometrical edge of interest
295 * \param theParams - The resulting vector of sorted node parameters
296 * \retval bool - false if not all parameters are OK
298 //================================================================================
300 bool SMESH_Algo::GetNodeParamOnEdge(const SMESHDS_Mesh* theMesh,
301 const TopoDS_Edge& theEdge,
302 vector< double > & theParams)
306 if ( !theMesh || theEdge.IsNull() )
309 SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
310 if ( !eSubMesh || !eSubMesh->GetElements()->more() )
311 return false; // edge is not meshed
314 set < double > paramSet;
317 // loop on nodes of an edge: sort them by param on edge
318 SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
319 while ( nIt->more() )
321 const SMDS_MeshNode* node = nIt->next();
322 const SMDS_PositionPtr& pos = node->GetPosition();
323 if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
325 const SMDS_EdgePosition* epos =
326 static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
327 paramSet.insert( epos->GetUParameter() );
331 // add vertex nodes params
333 BRep_Tool::Range(theEdge, f, l);
334 paramSet.insert( f );
335 paramSet.insert( l );
336 if ( paramSet.size() != nbEdgeNodes + 2 )
337 return false; // there are equal parameters
340 theParams.resize( paramSet.size() );
341 set < double >::iterator par = paramSet.begin();
342 vector< double >::iterator vecPar = theParams.begin();
343 for ( ; par != paramSet.end(); ++par, ++vecPar )
346 return theParams.size() > 1;