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"
36 #include <BRep_Tool.hxx>
37 #include <GCPnts_AbscissaPoint.hxx>
38 #include <GeomAdaptor_Curve.hxx>
39 #include <Geom_Surface.hxx>
40 #include <TopLoc_Location.hxx>
41 #include <TopTools_ListIteratorOfListOfShape.hxx>
42 #include <TopTools_ListOfShape.hxx>
44 #include <TopoDS_Face.hxx>
46 #include <gp_Pnt2d.hxx>
49 #include "utilities.h"
52 #include <TopTools_ListOfShape.hxx>
53 #include <TopTools_ListIteratorOfListOfShape.hxx>
55 //=============================================================================
59 //=============================================================================
61 SMESH_Algo::SMESH_Algo(int hypId, int studyId,
62 SMESH_Gen * gen):SMESH_Hypothesis(hypId, studyId, gen)
64 // _compatibleHypothesis.push_back("hypothese_bidon");
66 gen->_mapAlgo[hypId] = this;
68 _onlyUnaryInput = _requireDescretBoundary = true;
71 //=============================================================================
75 //=============================================================================
77 SMESH_Algo::~SMESH_Algo()
81 //=============================================================================
85 //=============================================================================
87 const vector < string > &SMESH_Algo::GetCompatibleHypothesis()
89 return _compatibleHypothesis;
92 //=============================================================================
94 * List the hypothesis used by the algorithm associated to the shape.
95 * Hypothesis associated to father shape -are- taken into account (see
96 * GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
97 * the algorithm. This method could be surcharged by specific algorithms, in
98 * case of several hypothesis simultaneously applicable.
100 //=============================================================================
102 const list <const SMESHDS_Hypothesis *> & SMESH_Algo::GetUsedHypothesis(
103 SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
105 _usedHypList.clear();
106 if ( !_compatibleHypothesis.empty() )
108 SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( _compatibleHypothesis[0] ));
109 for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
110 filter.Or( filter.HasName( _compatibleHypothesis[ i ] ));
112 aMesh.GetHypotheses( aShape, filter, _usedHypList, true );
113 if ( _usedHypList.size() > 1 )
114 _usedHypList.clear(); //only one compatible hypothesis allowed
119 //=============================================================================
121 * List the relevant hypothesis associated to the shape. Relevant hypothesis
122 * have a name (type) listed in the algorithm. Hypothesis associated to
123 * father shape -are not- taken into account (see GetUsedHypothesis)
125 //=============================================================================
127 const list<const SMESHDS_Hypothesis *> & SMESH_Algo::GetAppliedHypothesis(
128 SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
130 _appliedHypList.clear();
131 if ( !_compatibleHypothesis.empty() )
133 SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( _compatibleHypothesis[0] ));
134 for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
135 filter.Or( filter.HasName( _compatibleHypothesis[ i ] ));
137 aMesh.GetHypotheses( aShape, filter, _appliedHypList, false );
139 return _appliedHypList;
142 //=============================================================================
144 * Compute length of an edge
146 //=============================================================================
148 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
150 double UMin = 0, UMax = 0;
152 if (BRep_Tool::Degenerated(E))
154 Handle(Geom_Curve) C = BRep_Tool::Curve(E, L, UMin, UMax);
155 GeomAdaptor_Curve AdaptCurve(C);
156 GCPnts_AbscissaPoint gabs;
157 double length = gabs.Length(AdaptCurve, UMin, UMax);
161 //================================================================================
163 * \brief Find out elements orientation on a geometrical face
164 * \param theFace - The face correctly oriented in the shape being meshed
165 * \param theMeshDS - The mesh data structure
166 * \retval bool - true if the face normal and the normal of first element
167 * in the correspoding submesh point in different directions
169 //================================================================================
171 bool SMESH_Algo::IsReversedSubMesh (const TopoDS_Face& theFace,
172 SMESHDS_Mesh* theMeshDS)
174 if ( theFace.IsNull() || !theMeshDS )
177 // find out orientation of a meshed face
178 int faceID = theMeshDS->ShapeToIndex( theFace );
179 TopoDS_Shape aMeshedFace = theMeshDS->IndexToShape( faceID );
180 bool isReversed = ( theFace.Orientation() != aMeshedFace.Orientation() );
182 const SMESHDS_SubMesh * aSubMeshDSFace = theMeshDS->MeshElements( faceID );
183 if ( !aSubMeshDSFace )
186 // find element with node located on face and get its normal
187 const SMDS_FacePosition* facePos = 0;
191 bool normalOK = false;
192 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
193 while ( iteratorElem->more() ) // loop on elements on theFace
195 const SMDS_MeshElement* elem = iteratorElem->next();
196 if ( elem && elem->NbNodes() > 2 ) {
197 SMDS_ElemIteratorPtr nodesIt = elem->nodesIterator();
198 const SMDS_FacePosition* fPos = 0;
200 while ( nodesIt->more() ) { // loop on nodes
201 const SMDS_MeshNode* node
202 = static_cast<const SMDS_MeshNode *>(nodesIt->next());
204 nPnt[ i++ ].SetCoord( node->X(), node->Y(), node->Z() );
206 const SMDS_PositionPtr& pos = node->GetPosition();
207 if ( !pos ) continue;
208 if ( pos->GetTypeOfPosition() == SMDS_TOP_FACE ) {
209 fPos = dynamic_cast< const SMDS_FacePosition* >( pos.get() );
211 else if ( pos->GetTypeOfPosition() == SMDS_TOP_VERTEX ) {
212 vID = pos->GetShapeId();
215 if ( fPos || ( !normalOK && vID )) {
217 gp_Vec v01( nPnt[0], nPnt[1] ), v02( nPnt[0], nPnt[2] );
218 if ( v01.SquareMagnitude() > RealSmall() &&
219 v02.SquareMagnitude() > RealSmall() )
222 normalOK = ( Ne.SquareMagnitude() > RealSmall() );
224 // we need position on theFace or at least on vertex
227 if ((facePos = fPos))
236 // node position on face
239 u = facePos->GetUParameter();
240 v = facePos->GetVParameter();
242 else if ( vertexID ) {
243 TopoDS_Shape V = theMeshDS->IndexToShape( vertexID );
244 if ( V.IsNull() || V.ShapeType() != TopAbs_VERTEX )
246 gp_Pnt2d uv = BRep_Tool::Parameters( TopoDS::Vertex( V ), theFace );
255 // face normal at node position
257 Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
258 if ( surf.IsNull() || surf->Continuity() < GeomAbs_C1 ) return isReversed;
260 surf->D1( u, v, nPnt[0], d1u, d1v );
261 gp_Vec Nf = (d1u ^ d1v).Transformed( loc );
263 if ( theFace.Orientation() == TopAbs_REVERSED )