Salome HOME
Merging with WPdev
[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  * Usually an algoritm has nothing to save
89  */
90 //=============================================================================
91
92 ostream & SMESH_Algo::SaveTo(ostream & save) { return save; }
93 istream & SMESH_Algo::LoadFrom(istream & load) { return load; }
94
95 //=============================================================================
96 /*!
97  *  
98  */
99 //=============================================================================
100
101 const vector < string > &SMESH_Algo::GetCompatibleHypothesis()
102 {
103         return _compatibleHypothesis;
104 }
105
106 //=============================================================================
107 /*!
108  *  List the hypothesis used by the algorithm associated to the shape.
109  *  Hypothesis associated to father shape -are- taken into account (see
110  *  GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
111  *  the algorithm. This method could be surcharged by specific algorithms, in 
112  *  case of several hypothesis simultaneously applicable.
113  */
114 //=============================================================================
115
116 const list <const SMESHDS_Hypothesis *> &
117 SMESH_Algo::GetUsedHypothesis(SMESH_Mesh &         aMesh,
118                               const TopoDS_Shape & aShape,
119                               const bool           ignoreAuxiliary)
120 {
121   _usedHypList.clear();
122   SMESH_HypoFilter filter;
123   if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
124   {
125     aMesh.GetHypotheses( aShape, filter, _usedHypList, true );
126     if ( ignoreAuxiliary && _usedHypList.size() > 1 )
127       _usedHypList.clear();     //only one compatible hypothesis allowed
128   }
129   return _usedHypList;
130 }
131
132 //=============================================================================
133 /*!
134  *  List the relevant hypothesis associated to the shape. Relevant hypothesis
135  *  have a name (type) listed in the algorithm. Hypothesis associated to
136  *  father shape -are not- taken into account (see GetUsedHypothesis)
137  */
138 //=============================================================================
139
140 const list<const SMESHDS_Hypothesis *> &
141 SMESH_Algo::GetAppliedHypothesis(SMESH_Mesh &         aMesh,
142                                  const TopoDS_Shape & aShape,
143                                  const bool           ignoreAuxiliary)
144 {
145   _appliedHypList.clear();
146   SMESH_HypoFilter filter;
147   if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
148     aMesh.GetHypotheses( aShape, filter, _appliedHypList, false );
149
150   return _appliedHypList;
151 }
152
153 //=============================================================================
154 /*!
155  *  Compute length of an edge
156  */
157 //=============================================================================
158
159 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
160 {
161   double UMin = 0, UMax = 0;
162   if (BRep_Tool::Degenerated(E))
163     return 0;
164   TopLoc_Location L;
165   Handle(Geom_Curve) C = BRep_Tool::Curve(E, L, UMin, UMax);
166   GeomAdaptor_Curve AdaptCurve(C);
167   GCPnts_AbscissaPoint gabs;
168   double length = gabs.Length(AdaptCurve, UMin, UMax);
169   return length;
170 }
171
172 //================================================================================
173 /*!
174  * \brief Find out elements orientation on a geometrical face
175  * \param theFace - The face correctly oriented in the shape being meshed
176  * \param theMeshDS - The mesh data structure
177  * \retval bool - true if the face normal and the normal of first element
178  *                in the correspoding submesh point in different directions
179  */
180 //================================================================================
181
182 bool SMESH_Algo::IsReversedSubMesh (const TopoDS_Face&  theFace,
183                                     SMESHDS_Mesh*       theMeshDS)
184 {
185   if ( theFace.IsNull() || !theMeshDS )
186     return false;
187
188   // find out orientation of a meshed face
189   int faceID = theMeshDS->ShapeToIndex( theFace );
190   TopoDS_Shape aMeshedFace = theMeshDS->IndexToShape( faceID );
191   bool isReversed = ( theFace.Orientation() != aMeshedFace.Orientation() );
192
193   const SMESHDS_SubMesh * aSubMeshDSFace = theMeshDS->MeshElements( faceID );
194   if ( !aSubMeshDSFace )
195     return isReversed;
196
197   // find element with node located on face and get its normal
198   const SMDS_FacePosition* facePos = 0;
199   int vertexID = 0;
200   gp_Pnt nPnt[3];
201   gp_Vec Ne;
202   bool normalOK = false;
203   SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
204   while ( iteratorElem->more() ) // loop on elements on theFace
205   {
206     const SMDS_MeshElement* elem = iteratorElem->next();
207     if ( elem && elem->NbNodes() > 2 ) {
208       SMDS_ElemIteratorPtr nodesIt = elem->nodesIterator();
209       const SMDS_FacePosition* fPos = 0;
210       int i = 0, vID = 0;
211       while ( nodesIt->more() ) { // loop on nodes
212         const SMDS_MeshNode* node
213           = static_cast<const SMDS_MeshNode *>(nodesIt->next());
214         if ( i == 3 ) i = 2;
215         nPnt[ i++ ].SetCoord( node->X(), node->Y(), node->Z() );
216         // check position
217         const SMDS_PositionPtr& pos = node->GetPosition();
218         if ( !pos ) continue;
219         if ( pos->GetTypeOfPosition() == SMDS_TOP_FACE ) {
220           fPos = dynamic_cast< const SMDS_FacePosition* >( pos.get() );
221         }
222         else if ( pos->GetTypeOfPosition() == SMDS_TOP_VERTEX ) {
223           vID = pos->GetShapeId();
224         }
225       }
226       if ( fPos || ( !normalOK && vID )) {
227         // compute normal
228         gp_Vec v01( nPnt[0], nPnt[1] ), v02( nPnt[0], nPnt[2] );
229         if ( v01.SquareMagnitude() > RealSmall() &&
230              v02.SquareMagnitude() > RealSmall() )
231         {
232           Ne = v01 ^ v02;
233           normalOK = ( Ne.SquareMagnitude() > RealSmall() );
234         }
235         // we need position on theFace or at least on vertex
236         if ( normalOK ) {
237           vertexID = vID;
238           if ((facePos = fPos))
239             break;
240         }
241       }
242     }
243   }
244   if ( !normalOK )
245     return isReversed;
246
247   // node position on face
248   double u,v;
249   if ( facePos ) {
250     u = facePos->GetUParameter();
251     v = facePos->GetVParameter();
252   }
253   else if ( vertexID ) {
254     TopoDS_Shape V = theMeshDS->IndexToShape( vertexID );
255     if ( V.IsNull() || V.ShapeType() != TopAbs_VERTEX )
256       return isReversed;
257     gp_Pnt2d uv = BRep_Tool::Parameters( TopoDS::Vertex( V ), theFace );
258     u = uv.X();
259     v = uv.Y();
260   }
261   else
262   {
263     return isReversed;
264   }
265
266   // face normal at node position
267   TopLoc_Location loc;
268   Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
269   if ( surf.IsNull() || surf->Continuity() < GeomAbs_C1 ) return isReversed;
270   gp_Vec d1u, d1v;
271   surf->D1( u, v, nPnt[0], d1u, d1v );
272   gp_Vec Nf = (d1u ^ d1v).Transformed( loc );
273
274   if ( theFace.Orientation() == TopAbs_REVERSED )
275     Nf.Reverse();
276
277   return Ne * Nf < 0.;
278 }
279
280 //================================================================================
281 /*!
282  * \brief Initialize my parameter values by the mesh built on the geometry
283  * \param theMesh - the built mesh
284  * \param theShape - the geometry of interest
285  * \retval bool - true if parameter values have been successfully defined
286  *
287  * Just return false as the algorithm does not hold parameters values
288  */
289 //================================================================================
290
291 bool SMESH_Algo::SetParametersByMesh(const SMESH_Mesh* /*theMesh*/,
292                                      const TopoDS_Shape& /*theShape*/)
293 {
294   return false;
295 }
296
297 //================================================================================
298 /*!
299  * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
300  * \param theMesh - The mesh containing nodes
301  * \param theEdge - The geometrical edge of interest
302  * \param theParams - The resulting vector of sorted node parameters
303  * \retval bool - false if not all parameters are OK
304  */
305 //================================================================================
306
307 bool SMESH_Algo::GetNodeParamOnEdge(const SMESHDS_Mesh* theMesh,
308                                     const TopoDS_Edge&  theEdge,
309                                     vector< double > &  theParams)
310 {
311   theParams.clear();
312
313   if ( !theMesh || theEdge.IsNull() )
314     return false;
315
316   SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
317   if ( !eSubMesh || !eSubMesh->GetElements()->more() )
318     return false; // edge is not meshed
319
320   int nbEdgeNodes = 0;
321   set < double > paramSet;
322   if ( eSubMesh )
323   {
324     // loop on nodes of an edge: sort them by param on edge
325     SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
326     while ( nIt->more() )
327     {
328       const SMDS_MeshNode* node = nIt->next();
329       const SMDS_PositionPtr& pos = node->GetPosition();
330       if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
331         return false;
332       const SMDS_EdgePosition* epos =
333         static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
334       paramSet.insert( epos->GetUParameter() );
335       ++nbEdgeNodes;
336     }
337   }
338   // add vertex nodes params
339   Standard_Real f, l;
340   BRep_Tool::Range(theEdge, f, l);
341   paramSet.insert( f );
342   paramSet.insert( l );
343   if ( paramSet.size() != nbEdgeNodes + 2 )
344     return false; // there are equal parameters
345
346   // fill the vector
347   theParams.resize( paramSet.size() );
348   set < double >::iterator   par    = paramSet.begin();
349   vector< double >::iterator vecPar = theParams.begin();
350   for ( ; par != paramSet.end(); ++par, ++vecPar )
351     *vecPar = *par;
352
353   return theParams.size() > 1;
354 }
355
356 //================================================================================
357 /*!
358  * \brief Make filter recognize only compatible hypotheses
359  * \param theFilter - the filter to initialize
360  * \param ignoreAuxiliary - make filter ignore compatible auxiliary hypotheses
361  */
362 //================================================================================
363
364 bool SMESH_Algo::InitCompatibleHypoFilter( SMESH_HypoFilter & theFilter,
365                                            const bool         ignoreAuxiliary) const
366 {
367   if ( !_compatibleHypothesis.empty() )
368   {
369     theFilter.Init( theFilter.HasName( _compatibleHypothesis[0] ));
370     for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
371       theFilter.Or( theFilter.HasName( _compatibleHypothesis[ i ] ));
372
373     if ( ignoreAuxiliary )
374       theFilter.AndNot( theFilter.IsAuxiliary() );
375
376     return true;
377   }
378   return false;
379 }
380
381 //================================================================================
382 /*!
383  * \brief Sets event listener to submeshes if necessary
384   * \param subMesh - submesh where algo is set
385  * 
386  * After being set, event listener is notified on each event of a submesh.
387  * By default non listener is set
388  */
389 //================================================================================
390
391 void SMESH_Algo::SetEventListener(SMESH_subMesh* /*subMesh*/)
392 {
393 }