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