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
30 #include "SMESH_Algo.hxx"
31 #include "SMESH_Gen.hxx"
32 #include "SMESH_Mesh.hxx"
33 #include "SMESH_HypoFilter.hxx"
34 #include "SMDS_FacePosition.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"
57 //=============================================================================
61 //=============================================================================
63 SMESH_Algo::SMESH_Algo(int hypId, int studyId,
64 SMESH_Gen * gen):SMESH_Hypothesis(hypId, studyId, gen)
66 // _compatibleHypothesis.push_back("hypothese_bidon");
68 gen->_mapAlgo[hypId] = this;
70 _onlyUnaryInput = _requireDescretBoundary = true;
73 //=============================================================================
77 //=============================================================================
79 SMESH_Algo::~SMESH_Algo()
83 //=============================================================================
87 //=============================================================================
89 const vector < string > &SMESH_Algo::GetCompatibleHypothesis()
91 return _compatibleHypothesis;
94 //=============================================================================
96 * List the hypothesis used by the algorithm associated to the shape.
97 * Hypothesis associated to father shape -are- taken into account (see
98 * GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
99 * the algorithm. This method could be surcharged by specific algorithms, in
100 * case of several hypothesis simultaneously applicable.
102 //=============================================================================
104 const list <const SMESHDS_Hypothesis *> & SMESH_Algo::GetUsedHypothesis(
105 SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
107 _usedHypList.clear();
108 if ( !_compatibleHypothesis.empty() )
110 SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( _compatibleHypothesis[0] ));
111 for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
112 filter.Or( filter.HasName( _compatibleHypothesis[ i ] ));
114 aMesh.GetHypotheses( aShape, filter, _usedHypList, true );
115 if ( _usedHypList.size() > 1 )
116 _usedHypList.clear(); //only one compatible hypothesis allowed
121 //=============================================================================
123 * List the relevant hypothesis associated to the shape. Relevant hypothesis
124 * have a name (type) listed in the algorithm. Hypothesis associated to
125 * father shape -are not- taken into account (see GetUsedHypothesis)
127 //=============================================================================
129 const list<const SMESHDS_Hypothesis *> & SMESH_Algo::GetAppliedHypothesis(
130 SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
132 _appliedHypList.clear();
133 if ( !_compatibleHypothesis.empty() )
135 SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( _compatibleHypothesis[0] ));
136 for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
137 filter.Or( filter.HasName( _compatibleHypothesis[ i ] ));
139 aMesh.GetHypotheses( aShape, filter, _appliedHypList, false );
141 return _appliedHypList;
144 //=============================================================================
146 * Compute length of an edge
148 //=============================================================================
150 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
152 double UMin = 0, UMax = 0;
154 if (BRep_Tool::Degenerated(E))
156 Handle(Geom_Curve) C = BRep_Tool::Curve(E, L, UMin, UMax);
157 GeomAdaptor_Curve AdaptCurve(C);
158 GCPnts_AbscissaPoint gabs;
159 double length = gabs.Length(AdaptCurve, UMin, UMax);
163 //================================================================================
165 * \brief Find out elements orientation on a geometrical face
166 * \param theFace - The face correctly oriented in the shape being meshed
167 * \param theMeshDS - The mesh data structure
168 * \retval bool - true if the face normal and the normal of first element
169 * in the correspoding submesh point in different directions
171 //================================================================================
173 bool SMESH_Algo::IsReversedSubMesh (const TopoDS_Face& theFace,
174 SMESHDS_Mesh* theMeshDS)
176 if ( theFace.IsNull() || !theMeshDS )
179 // find out orientation of a meshed face
180 int faceID = theMeshDS->ShapeToIndex( theFace );
181 TopoDS_Shape aMeshedFace = theMeshDS->IndexToShape( faceID );
182 bool isReversed = ( theFace.Orientation() != aMeshedFace.Orientation() );
184 const SMESHDS_SubMesh * aSubMeshDSFace = theMeshDS->MeshElements( faceID );
185 if ( !aSubMeshDSFace )
188 // find element with node located on face and get its normal
189 const SMDS_FacePosition* facePos = 0;
193 bool normalOK = false;
194 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
195 while ( iteratorElem->more() ) // loop on elements on theFace
197 const SMDS_MeshElement* elem = iteratorElem->next();
198 if ( elem && elem->NbNodes() > 2 ) {
199 SMDS_ElemIteratorPtr nodesIt = elem->nodesIterator();
200 const SMDS_FacePosition* fPos = 0;
202 while ( nodesIt->more() ) { // loop on nodes
203 const SMDS_MeshNode* node
204 = static_cast<const SMDS_MeshNode *>(nodesIt->next());
206 nPnt[ i++ ].SetCoord( node->X(), node->Y(), node->Z() );
208 const SMDS_PositionPtr& pos = node->GetPosition();
209 if ( !pos ) continue;
210 if ( pos->GetTypeOfPosition() == SMDS_TOP_FACE ) {
211 fPos = dynamic_cast< const SMDS_FacePosition* >( pos.get() );
213 else if ( pos->GetTypeOfPosition() == SMDS_TOP_VERTEX ) {
214 vID = pos->GetShapeId();
217 if ( fPos || ( !normalOK && vID )) {
219 gp_Vec v01( nPnt[0], nPnt[1] ), v02( nPnt[0], nPnt[2] );
220 if ( v01.SquareMagnitude() > RealSmall() &&
221 v02.SquareMagnitude() > RealSmall() )
224 normalOK = ( Ne.SquareMagnitude() > RealSmall() );
226 // we need position on theFace or at least on vertex
229 if ((facePos = fPos))
238 // node position on face
241 u = facePos->GetUParameter();
242 v = facePos->GetVParameter();
244 else if ( vertexID ) {
245 TopoDS_Shape V = theMeshDS->IndexToShape( vertexID );
246 if ( V.IsNull() || V.ShapeType() != TopAbs_VERTEX )
248 gp_Pnt2d uv = BRep_Tool::Parameters( TopoDS::Vertex( V ), theFace );
257 // face normal at node position
259 Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
260 if ( surf.IsNull() || surf->Continuity() < GeomAbs_C1 ) return isReversed;
262 surf->D1( u, v, nPnt[0], d1u, d1v );
263 gp_Vec Nf = (d1u ^ d1v).Transformed( loc );
265 if ( theFace.Orientation() == TopAbs_REVERSED )