Salome HOME
BUG SWP13024
[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 #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         _quadraticMesh = false;
74 }
75
76 //=============================================================================
77 /*!
78  *  
79  */
80 //=============================================================================
81
82 SMESH_Algo::~SMESH_Algo()
83 {
84 }
85
86 //=============================================================================
87 /*!
88  *  
89  */
90 //=============================================================================
91
92 const vector < string > &SMESH_Algo::GetCompatibleHypothesis()
93 {
94         return _compatibleHypothesis;
95 }
96
97 //=============================================================================
98 /*!
99  *  List the hypothesis used by the algorithm associated to the shape.
100  *  Hypothesis associated to father shape -are- taken into account (see
101  *  GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
102  *  the algorithm. This method could be surcharged by specific algorithms, in 
103  *  case of several hypothesis simultaneously applicable.
104  */
105 //=============================================================================
106
107 const list <const SMESHDS_Hypothesis *> &
108 SMESH_Algo::GetUsedHypothesis(SMESH_Mesh &         aMesh,
109                               const TopoDS_Shape & aShape,
110                               const bool           ignoreAuxiliary)
111 {
112   _usedHypList.clear();
113   SMESH_HypoFilter filter;
114   if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
115   {
116     aMesh.GetHypotheses( aShape, filter, _usedHypList, true );
117     if ( ignoreAuxiliary && _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 *> &
132 SMESH_Algo::GetAppliedHypothesis(SMESH_Mesh &         aMesh,
133                                  const TopoDS_Shape & aShape,
134                                  const bool           ignoreAuxiliary)
135 {
136   _appliedHypList.clear();
137   SMESH_HypoFilter filter;
138   if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
139     aMesh.GetHypotheses( aShape, filter, _appliedHypList, false );
140
141   return _appliedHypList;
142 }
143
144 //=============================================================================
145 /*!
146  *  Compute length of an edge
147  */
148 //=============================================================================
149
150 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
151 {
152   double UMin = 0, UMax = 0;
153   if (BRep_Tool::Degenerated(E))
154     return 0;
155   TopLoc_Location L;
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);
160   return length;
161 }
162
163 //================================================================================
164 /*!
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
170  */
171 //================================================================================
172
173 bool SMESH_Algo::IsReversedSubMesh (const TopoDS_Face&  theFace,
174                                     SMESHDS_Mesh*       theMeshDS)
175 {
176   if ( theFace.IsNull() || !theMeshDS )
177     return false;
178
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() );
183
184   const SMESHDS_SubMesh * aSubMeshDSFace = theMeshDS->MeshElements( faceID );
185   if ( !aSubMeshDSFace )
186     return isReversed;
187
188   // find element with node located on face and get its normal
189   const SMDS_FacePosition* facePos = 0;
190   int vertexID = 0;
191   gp_Pnt nPnt[3];
192   gp_Vec Ne;
193   bool normalOK = false;
194   SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
195   while ( iteratorElem->more() ) // loop on elements on theFace
196   {
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;
201       int i = 0, vID = 0;
202       while ( nodesIt->more() ) { // loop on nodes
203         const SMDS_MeshNode* node
204           = static_cast<const SMDS_MeshNode *>(nodesIt->next());
205         if ( i == 3 ) i = 2;
206         nPnt[ i++ ].SetCoord( node->X(), node->Y(), node->Z() );
207         // check position
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() );
212         }
213         else if ( pos->GetTypeOfPosition() == SMDS_TOP_VERTEX ) {
214           vID = pos->GetShapeId();
215         }
216       }
217       if ( fPos || ( !normalOK && vID )) {
218         // compute normal
219         gp_Vec v01( nPnt[0], nPnt[1] ), v02( nPnt[0], nPnt[2] );
220         if ( v01.SquareMagnitude() > RealSmall() &&
221              v02.SquareMagnitude() > RealSmall() )
222         {
223           Ne = v01 ^ v02;
224           normalOK = ( Ne.SquareMagnitude() > RealSmall() );
225         }
226         // we need position on theFace or at least on vertex
227         if ( normalOK ) {
228           vertexID = vID;
229           if ((facePos = fPos))
230             break;
231         }
232       }
233     }
234   }
235   if ( !normalOK )
236     return isReversed;
237
238   // node position on face
239   double u,v;
240   if ( facePos ) {
241     u = facePos->GetUParameter();
242     v = facePos->GetVParameter();
243   }
244   else if ( vertexID ) {
245     TopoDS_Shape V = theMeshDS->IndexToShape( vertexID );
246     if ( V.IsNull() || V.ShapeType() != TopAbs_VERTEX )
247       return isReversed;
248     gp_Pnt2d uv = BRep_Tool::Parameters( TopoDS::Vertex( V ), theFace );
249     u = uv.X();
250     v = uv.Y();
251   }
252   else
253   {
254     return isReversed;
255   }
256
257   // face normal at node position
258   TopLoc_Location loc;
259   Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
260   if ( surf.IsNull() || surf->Continuity() < GeomAbs_C1 ) return isReversed;
261   gp_Vec d1u, d1v;
262   surf->D1( u, v, nPnt[0], d1u, d1v );
263   gp_Vec Nf = (d1u ^ d1v).Transformed( loc );
264
265   if ( theFace.Orientation() == TopAbs_REVERSED )
266     Nf.Reverse();
267
268   return Ne * Nf < 0.;
269 }
270
271 //================================================================================
272 /*!
273  * \brief Initialize my parameter values by the mesh built on the geometry
274  * \param theMesh - the built mesh
275  * \param theShape - the geometry of interest
276  * \retval bool - true if parameter values have been successfully defined
277  *
278  * Just return false as the algorithm does not hold parameters values
279  */
280 //================================================================================
281
282 bool SMESH_Algo::SetParametersByMesh(const SMESH_Mesh* /*theMesh*/,
283                                      const TopoDS_Shape& /*theShape*/)
284 {
285   return false;
286 }
287
288 //================================================================================
289 /*!
290  * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
291  * \param theMesh - The mesh containing nodes
292  * \param theEdge - The geometrical edge of interest
293  * \param theParams - The resulting vector of sorted node parameters
294  * \retval bool - false if not all parameters are OK
295  */
296 //================================================================================
297
298 bool SMESH_Algo::GetNodeParamOnEdge(const SMESHDS_Mesh* theMesh,
299                                     const TopoDS_Edge&  theEdge,
300                                     vector< double > &  theParams)
301 {
302   theParams.clear();
303
304   if ( !theMesh || theEdge.IsNull() )
305     return false;
306
307   SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
308   if ( !eSubMesh || !eSubMesh->GetElements()->more() )
309     return false; // edge is not meshed
310
311   int nbEdgeNodes = 0;
312   set < double > paramSet;
313   if ( eSubMesh )
314   {
315     // loop on nodes of an edge: sort them by param on edge
316     SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
317     while ( nIt->more() )
318     {
319       const SMDS_MeshNode* node = nIt->next();
320       const SMDS_PositionPtr& pos = node->GetPosition();
321       if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
322         return false;
323       const SMDS_EdgePosition* epos =
324         static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
325       paramSet.insert( epos->GetUParameter() );
326       ++nbEdgeNodes;
327     }
328   }
329   // add vertex nodes params
330   Standard_Real f, l;
331   BRep_Tool::Range(theEdge, f, l);
332   paramSet.insert( f );
333   paramSet.insert( l );
334   if ( paramSet.size() != nbEdgeNodes + 2 )
335     return false; // there are equal parameters
336
337   // fill the vector
338   theParams.resize( paramSet.size() );
339   set < double >::iterator   par    = paramSet.begin();
340   vector< double >::iterator vecPar = theParams.begin();
341   for ( ; par != paramSet.end(); ++par, ++vecPar )
342     *vecPar = *par;
343
344   return theParams.size() > 1;
345 }
346
347 //================================================================================
348 /*!
349  * \brief Make filter recognize only compatible hypotheses
350  * \param theFilter - the filter to initialize
351  * \param ignoreAuxiliary - make filter ignore compatible auxiliary hypotheses
352  */
353 //================================================================================
354
355 bool SMESH_Algo::InitCompatibleHypoFilter( SMESH_HypoFilter & theFilter,
356                                            const bool         ignoreAuxiliary) const
357 {
358   if ( !_compatibleHypothesis.empty() )
359   {
360     theFilter.Init( theFilter.HasName( _compatibleHypothesis[0] ));
361     for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
362       theFilter.Or( theFilter.HasName( _compatibleHypothesis[ i ] ));
363
364     if ( ignoreAuxiliary )
365       theFilter.AndNot( theFilter.IsAuxiliary() );
366
367     return true;
368   }
369   return false;
370 }