Salome HOME
Merging with branch OCC_development_for_3_2_0b1
[modules/smesh.git] / src / SMESH / SMESH_Algo.cxx
1 //  SMESH SMESH : implementaion of SMESH idl descriptions
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SMESH_Algo.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //  $Header$
28
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"
39
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>
47 #include <TopoDS.hxx>
48 #include <TopoDS_Face.hxx>
49 #include <gp_Pnt.hxx>
50 #include <gp_Pnt2d.hxx>
51 #include <gp_Vec.hxx>
52
53 #include "utilities.h"
54
55 #include <algorithm>
56
57 using namespace std;
58
59 //=============================================================================
60 /*!
61  *  
62  */
63 //=============================================================================
64
65 SMESH_Algo::SMESH_Algo(int hypId, int studyId,
66         SMESH_Gen * gen):SMESH_Hypothesis(hypId, studyId, gen)
67 {
68 //   _compatibleHypothesis.push_back("hypothese_bidon");
69         _type = ALGO;
70         gen->_mapAlgo[hypId] = this;
71
72         _onlyUnaryInput = _requireDescretBoundary = true;
73 }
74
75 //=============================================================================
76 /*!
77  *  
78  */
79 //=============================================================================
80
81 SMESH_Algo::~SMESH_Algo()
82 {
83 }
84
85 //=============================================================================
86 /*!
87  *  
88  */
89 //=============================================================================
90
91 const vector < string > &SMESH_Algo::GetCompatibleHypothesis()
92 {
93         return _compatibleHypothesis;
94 }
95
96 //=============================================================================
97 /*!
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.
103  */
104 //=============================================================================
105
106 const list <const SMESHDS_Hypothesis *> & SMESH_Algo::GetUsedHypothesis(
107         SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
108 {
109   _usedHypList.clear();
110   if ( !_compatibleHypothesis.empty() )
111   {
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 ] ));
115
116     aMesh.GetHypotheses( aShape, filter, _usedHypList, true );
117     if ( _usedHypList.size() > 1 )
118       _usedHypList.clear();     //only one compatible hypothesis allowed
119   }
120   return _usedHypList;
121 }
122
123 //=============================================================================
124 /*!
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)
128  */
129 //=============================================================================
130
131 const list<const SMESHDS_Hypothesis *> & SMESH_Algo::GetAppliedHypothesis(
132         SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
133 {
134   _appliedHypList.clear();
135   if ( !_compatibleHypothesis.empty() )
136   {
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 ] ));
140     
141     aMesh.GetHypotheses( aShape, filter, _appliedHypList, false );
142   }
143   return _appliedHypList;
144 }
145
146 //=============================================================================
147 /*!
148  *  Compute length of an edge
149  */
150 //=============================================================================
151
152 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
153 {
154   double UMin = 0, UMax = 0;
155   if (BRep_Tool::Degenerated(E))
156     return 0;
157   TopLoc_Location L;
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);
162   return length;
163 }
164
165 //================================================================================
166 /*!
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
172  */
173 //================================================================================
174
175 bool SMESH_Algo::IsReversedSubMesh (const TopoDS_Face&  theFace,
176                                     SMESHDS_Mesh*       theMeshDS)
177 {
178   if ( theFace.IsNull() || !theMeshDS )
179     return false;
180
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() );
185
186   const SMESHDS_SubMesh * aSubMeshDSFace = theMeshDS->MeshElements( faceID );
187   if ( !aSubMeshDSFace )
188     return isReversed;
189
190   // find element with node located on face and get its normal
191   const SMDS_FacePosition* facePos = 0;
192   int vertexID = 0;
193   gp_Pnt nPnt[3];
194   gp_Vec Ne;
195   bool normalOK = false;
196   SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
197   while ( iteratorElem->more() ) // loop on elements on theFace
198   {
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;
203       int i = 0, vID = 0;
204       while ( nodesIt->more() ) { // loop on nodes
205         const SMDS_MeshNode* node
206           = static_cast<const SMDS_MeshNode *>(nodesIt->next());
207         if ( i == 3 ) i = 2;
208         nPnt[ i++ ].SetCoord( node->X(), node->Y(), node->Z() );
209         // check position
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() );
214         }
215         else if ( pos->GetTypeOfPosition() == SMDS_TOP_VERTEX ) {
216           vID = pos->GetShapeId();
217         }
218       }
219       if ( fPos || ( !normalOK && vID )) {
220         // compute normal
221         gp_Vec v01( nPnt[0], nPnt[1] ), v02( nPnt[0], nPnt[2] );
222         if ( v01.SquareMagnitude() > RealSmall() &&
223              v02.SquareMagnitude() > RealSmall() )
224         {
225           Ne = v01 ^ v02;
226           normalOK = ( Ne.SquareMagnitude() > RealSmall() );
227         }
228         // we need position on theFace or at least on vertex
229         if ( normalOK ) {
230           vertexID = vID;
231           if ((facePos = fPos))
232             break;
233         }
234       }
235     }
236   }
237   if ( !normalOK )
238     return isReversed;
239
240   // node position on face
241   double u,v;
242   if ( facePos ) {
243     u = facePos->GetUParameter();
244     v = facePos->GetVParameter();
245   }
246   else if ( vertexID ) {
247     TopoDS_Shape V = theMeshDS->IndexToShape( vertexID );
248     if ( V.IsNull() || V.ShapeType() != TopAbs_VERTEX )
249       return isReversed;
250     gp_Pnt2d uv = BRep_Tool::Parameters( TopoDS::Vertex( V ), theFace );
251     u = uv.X();
252     v = uv.Y();
253   }
254   else
255   {
256     return isReversed;
257   }
258
259   // face normal at node position
260   TopLoc_Location loc;
261   Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
262   if ( surf.IsNull() || surf->Continuity() < GeomAbs_C1 ) return isReversed;
263   gp_Vec d1u, d1v;
264   surf->D1( u, v, nPnt[0], d1u, d1v );
265   gp_Vec Nf = (d1u ^ d1v).Transformed( loc );
266
267   if ( theFace.Orientation() == TopAbs_REVERSED )
268     Nf.Reverse();
269
270   return Ne * Nf < 0.;
271 }
272
273 //================================================================================
274 /*!
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
279  *
280  * Just return false as the algorithm does not hold parameters values
281  */
282 //================================================================================
283
284 bool SMESH_Algo::SetParametersByMesh(const SMESH_Mesh* /*theMesh*/,
285                                      const TopoDS_Shape& /*theShape*/)
286 {
287   return false;
288 }
289
290 //================================================================================
291 /*!
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
297  */
298 //================================================================================
299
300 bool SMESH_Algo::GetNodeParamOnEdge(const SMESHDS_Mesh* theMesh,
301                                     const TopoDS_Edge&  theEdge,
302                                     vector< double > &  theParams)
303 {
304   theParams.clear();
305
306   if ( !theMesh || theEdge.IsNull() )
307     return false;
308
309   SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
310   if ( !eSubMesh || !eSubMesh->GetElements()->more() )
311     return false; // edge is not meshed
312
313   int nbEdgeNodes = 0;
314   set < double > paramSet;
315   if ( eSubMesh )
316   {
317     // loop on nodes of an edge: sort them by param on edge
318     SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
319     while ( nIt->more() )
320     {
321       const SMDS_MeshNode* node = nIt->next();
322       const SMDS_PositionPtr& pos = node->GetPosition();
323       if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
324         return false;
325       const SMDS_EdgePosition* epos =
326         static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
327       paramSet.insert( epos->GetUParameter() );
328       ++nbEdgeNodes;
329     }
330   }
331   // add vertex nodes params
332   Standard_Real f, l;
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
338
339   // fill the vector
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 )
344     *vecPar = *par;
345
346   return theParams.size() > 1;
347 }