Salome HOME
Update mail address
[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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : SMESH_Algo.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //  $Header$
28
29 using namespace std;
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_EdgePosition.hxx"
36 #include "SMDS_MeshElement.hxx"
37 #include "SMDS_MeshNode.hxx"
38 #include "SMESHDS_Mesh.hxx"
39 #include "SMESHDS_SubMesh.hxx"
40
41 #include <BRep_Tool.hxx>
42 #include <GCPnts_AbscissaPoint.hxx>
43 #include <GeomAdaptor_Curve.hxx>
44 #include <Geom_Surface.hxx>
45 #include <TopLoc_Location.hxx>
46 #include <TopTools_ListIteratorOfListOfShape.hxx>
47 #include <TopTools_ListOfShape.hxx>
48 #include <TopoDS.hxx>
49 #include <TopoDS_Face.hxx>
50 #include <gp_Pnt.hxx>
51 #include <gp_Pnt2d.hxx>
52 #include <gp_Vec.hxx>
53
54 #include "utilities.h"
55
56 #include <algorithm>
57
58 //=============================================================================
59 /*!
60  *  
61  */
62 //=============================================================================
63
64 SMESH_Algo::SMESH_Algo(int hypId, int studyId,
65         SMESH_Gen * gen):SMESH_Hypothesis(hypId, studyId, gen)
66 {
67 //   _compatibleHypothesis.push_back("hypothese_bidon");
68         _type = ALGO;
69         gen->_mapAlgo[hypId] = this;
70
71         _onlyUnaryInput = _requireDescretBoundary = true;
72         _quadraticMesh = false;
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 *> &
107 SMESH_Algo::GetUsedHypothesis(SMESH_Mesh &         aMesh,
108                               const TopoDS_Shape & aShape,
109                               const bool           ignoreAuxiliary)
110 {
111   _usedHypList.clear();
112   SMESH_HypoFilter filter;
113   if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
114   {
115     aMesh.GetHypotheses( aShape, filter, _usedHypList, true );
116     if ( ignoreAuxiliary && _usedHypList.size() > 1 )
117       _usedHypList.clear();     //only one compatible hypothesis allowed
118   }
119   return _usedHypList;
120 }
121
122 //=============================================================================
123 /*!
124  *  List the relevant hypothesis associated to the shape. Relevant hypothesis
125  *  have a name (type) listed in the algorithm. Hypothesis associated to
126  *  father shape -are not- taken into account (see GetUsedHypothesis)
127  */
128 //=============================================================================
129
130 const list<const SMESHDS_Hypothesis *> &
131 SMESH_Algo::GetAppliedHypothesis(SMESH_Mesh &         aMesh,
132                                  const TopoDS_Shape & aShape,
133                                  const bool           ignoreAuxiliary)
134 {
135   _appliedHypList.clear();
136   SMESH_HypoFilter filter;
137   if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
138     aMesh.GetHypotheses( aShape, filter, _appliedHypList, false );
139
140   return _appliedHypList;
141 }
142
143 //=============================================================================
144 /*!
145  *  Compute length of an edge
146  */
147 //=============================================================================
148
149 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
150 {
151   double UMin = 0, UMax = 0;
152   if (BRep_Tool::Degenerated(E))
153     return 0;
154   TopLoc_Location L;
155   Handle(Geom_Curve) C = BRep_Tool::Curve(E, L, UMin, UMax);
156   GeomAdaptor_Curve AdaptCurve(C);
157   GCPnts_AbscissaPoint gabs;
158   double length = gabs.Length(AdaptCurve, UMin, UMax);
159   return length;
160 }
161
162 //================================================================================
163 /*!
164  * \brief Find out elements orientation on a geometrical face
165  * \param theFace - The face correctly oriented in the shape being meshed
166  * \param theMeshDS - The mesh data structure
167  * \retval bool - true if the face normal and the normal of first element
168  *                in the correspoding submesh point in different directions
169  */
170 //================================================================================
171
172 bool SMESH_Algo::IsReversedSubMesh (const TopoDS_Face&  theFace,
173                                     SMESHDS_Mesh*       theMeshDS)
174 {
175   if ( theFace.IsNull() || !theMeshDS )
176     return false;
177
178   // find out orientation of a meshed face
179   int faceID = theMeshDS->ShapeToIndex( theFace );
180   TopoDS_Shape aMeshedFace = theMeshDS->IndexToShape( faceID );
181   bool isReversed = ( theFace.Orientation() != aMeshedFace.Orientation() );
182
183   const SMESHDS_SubMesh * aSubMeshDSFace = theMeshDS->MeshElements( faceID );
184   if ( !aSubMeshDSFace )
185     return isReversed;
186
187   // find element with node located on face and get its normal
188   const SMDS_FacePosition* facePos = 0;
189   int vertexID = 0;
190   gp_Pnt nPnt[3];
191   gp_Vec Ne;
192   bool normalOK = false;
193   SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
194   while ( iteratorElem->more() ) // loop on elements on theFace
195   {
196     const SMDS_MeshElement* elem = iteratorElem->next();
197     if ( elem && elem->NbNodes() > 2 ) {
198       SMDS_ElemIteratorPtr nodesIt = elem->nodesIterator();
199       const SMDS_FacePosition* fPos = 0;
200       int i = 0, vID = 0;
201       while ( nodesIt->more() ) { // loop on nodes
202         const SMDS_MeshNode* node
203           = static_cast<const SMDS_MeshNode *>(nodesIt->next());
204         if ( i == 3 ) i = 2;
205         nPnt[ i++ ].SetCoord( node->X(), node->Y(), node->Z() );
206         // check position
207         const SMDS_PositionPtr& pos = node->GetPosition();
208         if ( !pos ) continue;
209         if ( pos->GetTypeOfPosition() == SMDS_TOP_FACE ) {
210           fPos = dynamic_cast< const SMDS_FacePosition* >( pos.get() );
211         }
212         else if ( pos->GetTypeOfPosition() == SMDS_TOP_VERTEX ) {
213           vID = pos->GetShapeId();
214         }
215       }
216       if ( fPos || ( !normalOK && vID )) {
217         // compute normal
218         gp_Vec v01( nPnt[0], nPnt[1] ), v02( nPnt[0], nPnt[2] );
219         if ( v01.SquareMagnitude() > RealSmall() &&
220              v02.SquareMagnitude() > RealSmall() )
221         {
222           Ne = v01 ^ v02;
223           normalOK = ( Ne.SquareMagnitude() > RealSmall() );
224         }
225         // we need position on theFace or at least on vertex
226         if ( normalOK ) {
227           vertexID = vID;
228           if ((facePos = fPos))
229             break;
230         }
231       }
232     }
233   }
234   if ( !normalOK )
235     return isReversed;
236
237   // node position on face
238   double u,v;
239   if ( facePos ) {
240     u = facePos->GetUParameter();
241     v = facePos->GetVParameter();
242   }
243   else if ( vertexID ) {
244     TopoDS_Shape V = theMeshDS->IndexToShape( vertexID );
245     if ( V.IsNull() || V.ShapeType() != TopAbs_VERTEX )
246       return isReversed;
247     gp_Pnt2d uv = BRep_Tool::Parameters( TopoDS::Vertex( V ), theFace );
248     u = uv.X();
249     v = uv.Y();
250   }
251   else
252   {
253     return isReversed;
254   }
255
256   // face normal at node position
257   TopLoc_Location loc;
258   Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
259   if ( surf.IsNull() || surf->Continuity() < GeomAbs_C1 ) return isReversed;
260   gp_Vec d1u, d1v;
261   surf->D1( u, v, nPnt[0], d1u, d1v );
262   gp_Vec Nf = (d1u ^ d1v).Transformed( loc );
263
264   if ( theFace.Orientation() == TopAbs_REVERSED )
265     Nf.Reverse();
266
267   return Ne * Nf < 0.;
268 }
269
270 //================================================================================
271 /*!
272  * \brief Initialize my parameter values by the mesh built on the geometry
273  * \param theMesh - the built mesh
274  * \param theShape - the geometry of interest
275  * \retval bool - true if parameter values have been successfully defined
276  *
277  * Just return false as the algorithm does not hold parameters values
278  */
279 //================================================================================
280
281 bool SMESH_Algo::SetParametersByMesh(const SMESH_Mesh* /*theMesh*/,
282                                      const TopoDS_Shape& /*theShape*/)
283 {
284   return false;
285 }
286
287 //================================================================================
288 /*!
289  * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
290  * \param theMesh - The mesh containing nodes
291  * \param theEdge - The geometrical edge of interest
292  * \param theParams - The resulting vector of sorted node parameters
293  * \retval bool - false if not all parameters are OK
294  */
295 //================================================================================
296
297 bool SMESH_Algo::GetNodeParamOnEdge(const SMESHDS_Mesh* theMesh,
298                                     const TopoDS_Edge&  theEdge,
299                                     vector< double > &  theParams)
300 {
301   theParams.clear();
302
303   if ( !theMesh || theEdge.IsNull() )
304     return false;
305
306   SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
307   if ( !eSubMesh || !eSubMesh->GetElements()->more() )
308     return false; // edge is not meshed
309
310   int nbEdgeNodes = 0;
311   set < double > paramSet;
312   if ( eSubMesh )
313   {
314     // loop on nodes of an edge: sort them by param on edge
315     SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
316     while ( nIt->more() )
317     {
318       const SMDS_MeshNode* node = nIt->next();
319       const SMDS_PositionPtr& pos = node->GetPosition();
320       if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
321         return false;
322       const SMDS_EdgePosition* epos =
323         static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
324       paramSet.insert( epos->GetUParameter() );
325       ++nbEdgeNodes;
326     }
327   }
328   // add vertex nodes params
329   Standard_Real f, l;
330   BRep_Tool::Range(theEdge, f, l);
331   paramSet.insert( f );
332   paramSet.insert( l );
333   if ( paramSet.size() != nbEdgeNodes + 2 )
334     return false; // there are equal parameters
335
336   // fill the vector
337   theParams.resize( paramSet.size() );
338   set < double >::iterator   par    = paramSet.begin();
339   vector< double >::iterator vecPar = theParams.begin();
340   for ( ; par != paramSet.end(); ++par, ++vecPar )
341     *vecPar = *par;
342
343   return theParams.size() > 1;
344 }
345
346 //================================================================================
347 /*!
348  * \brief Make filter recognize only compatible hypotheses
349  * \param theFilter - the filter to initialize
350  * \param ignoreAuxiliary - make filter ignore compatible auxiliary hypotheses
351  */
352 //================================================================================
353
354 bool SMESH_Algo::InitCompatibleHypoFilter( SMESH_HypoFilter & theFilter,
355                                            const bool         ignoreAuxiliary) const
356 {
357   if ( !_compatibleHypothesis.empty() )
358   {
359     theFilter.Init( theFilter.HasName( _compatibleHypothesis[0] ));
360     for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
361       theFilter.Or( theFilter.HasName( _compatibleHypothesis[ i ] ));
362
363     if ( ignoreAuxiliary )
364       theFilter.AndNot( theFilter.IsAuxiliary() );
365
366     return true;
367   }
368   return false;
369 }