Salome HOME
PAL13615 (EDF PAL 315/31 GEOM SMESH : meshing of a "5 edges quadrangle")
[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 <BRepAdaptor_Curve.hxx>
41 #include <BRepLProp.hxx>
42 #include <BRep_Tool.hxx>
43 #include <GCPnts_AbscissaPoint.hxx>
44 #include <GeomAdaptor_Curve.hxx>
45 #include <Geom_Surface.hxx>
46 #include <TopExp.hxx>
47 #include <TopLoc_Location.hxx>
48 #include <TopTools_ListIteratorOfListOfShape.hxx>
49 #include <TopTools_ListOfShape.hxx>
50 #include <TopoDS.hxx>
51 #include <TopoDS_Edge.hxx>
52 #include <TopoDS_Face.hxx>
53 #include <TopoDS_Vertex.hxx>
54 #include <gp_Pnt.hxx>
55 #include <gp_Pnt2d.hxx>
56 #include <gp_Vec.hxx>
57
58 #include <Standard_ErrorHandler.hxx>
59 #include <Standard_Failure.hxx>
60
61 #include "utilities.h"
62
63 #include <algorithm>
64
65 using namespace std;
66
67 //=============================================================================
68 /*!
69  *  
70  */
71 //=============================================================================
72
73 SMESH_Algo::SMESH_Algo(int hypId, int studyId,
74         SMESH_Gen * gen):SMESH_Hypothesis(hypId, studyId, gen)
75 {
76   gen->_mapAlgo[hypId] = this;
77
78   _onlyUnaryInput = _requireDescretBoundary = _requireShape = true;
79   _quadraticMesh = false;
80 }
81
82 //=============================================================================
83 /*!
84  *  
85  */
86 //=============================================================================
87
88 SMESH_Algo::~SMESH_Algo()
89 {
90 }
91
92 //=============================================================================
93 /*!
94  * Usually an algoritm has nothing to save
95  */
96 //=============================================================================
97
98 ostream & SMESH_Algo::SaveTo(ostream & save) { return save; }
99 istream & SMESH_Algo::LoadFrom(istream & load) { return load; }
100
101 //=============================================================================
102 /*!
103  *  
104  */
105 //=============================================================================
106
107 const vector < string > &SMESH_Algo::GetCompatibleHypothesis()
108 {
109         return _compatibleHypothesis;
110 }
111
112 //=============================================================================
113 /*!
114  *  List the hypothesis used by the algorithm associated to the shape.
115  *  Hypothesis associated to father shape -are- taken into account (see
116  *  GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
117  *  the algorithm. This method could be surcharged by specific algorithms, in 
118  *  case of several hypothesis simultaneously applicable.
119  */
120 //=============================================================================
121
122 const list <const SMESHDS_Hypothesis *> &
123 SMESH_Algo::GetUsedHypothesis(SMESH_Mesh &         aMesh,
124                               const TopoDS_Shape & aShape,
125                               const bool           ignoreAuxiliary)
126 {
127   _usedHypList.clear();
128   SMESH_HypoFilter filter;
129   if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
130   {
131     aMesh.GetHypotheses( aShape, filter, _usedHypList, true );
132     if ( ignoreAuxiliary && _usedHypList.size() > 1 )
133       _usedHypList.clear();     //only one compatible hypothesis allowed
134   }
135   return _usedHypList;
136 }
137
138 //=============================================================================
139 /*!
140  *  List the relevant hypothesis associated to the shape. Relevant hypothesis
141  *  have a name (type) listed in the algorithm. Hypothesis associated to
142  *  father shape -are not- taken into account (see GetUsedHypothesis)
143  */
144 //=============================================================================
145
146 const list<const SMESHDS_Hypothesis *> &
147 SMESH_Algo::GetAppliedHypothesis(SMESH_Mesh &         aMesh,
148                                  const TopoDS_Shape & aShape,
149                                  const bool           ignoreAuxiliary)
150 {
151   _appliedHypList.clear();
152   SMESH_HypoFilter filter;
153   if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
154     aMesh.GetHypotheses( aShape, filter, _appliedHypList, false );
155
156   return _appliedHypList;
157 }
158
159 //=============================================================================
160 /*!
161  *  Compute length of an edge
162  */
163 //=============================================================================
164
165 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
166 {
167   double UMin = 0, UMax = 0;
168   if (BRep_Tool::Degenerated(E))
169     return 0;
170   TopLoc_Location L;
171   Handle(Geom_Curve) C = BRep_Tool::Curve(E, L, UMin, UMax);
172   GeomAdaptor_Curve AdaptCurve(C);
173   double length = GCPnts_AbscissaPoint::Length(AdaptCurve, UMin, UMax);
174   return length;
175 }
176
177 //================================================================================
178 /*!
179  * \brief Find out elements orientation on a geometrical face
180  * \param theFace - The face correctly oriented in the shape being meshed
181  * \param theMeshDS - The mesh data structure
182  * \retval bool - true if the face normal and the normal of first element
183  *                in the correspoding submesh point in different directions
184  */
185 //================================================================================
186
187 bool SMESH_Algo::IsReversedSubMesh (const TopoDS_Face&  theFace,
188                                     SMESHDS_Mesh*       theMeshDS)
189 {
190   if ( theFace.IsNull() || !theMeshDS )
191     return false;
192
193   // find out orientation of a meshed face
194   int faceID = theMeshDS->ShapeToIndex( theFace );
195   TopoDS_Shape aMeshedFace = theMeshDS->IndexToShape( faceID );
196   bool isReversed = ( theFace.Orientation() != aMeshedFace.Orientation() );
197
198   const SMESHDS_SubMesh * aSubMeshDSFace = theMeshDS->MeshElements( faceID );
199   if ( !aSubMeshDSFace )
200     return isReversed;
201
202   // find element with node located on face and get its normal
203   const SMDS_FacePosition* facePos = 0;
204   int vertexID = 0;
205   gp_Pnt nPnt[3];
206   gp_Vec Ne;
207   bool normalOK = false;
208   SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
209   while ( iteratorElem->more() ) // loop on elements on theFace
210   {
211     const SMDS_MeshElement* elem = iteratorElem->next();
212     if ( elem && elem->NbNodes() > 2 ) {
213       SMDS_ElemIteratorPtr nodesIt = elem->nodesIterator();
214       const SMDS_FacePosition* fPos = 0;
215       int i = 0, vID = 0;
216       while ( nodesIt->more() ) { // loop on nodes
217         const SMDS_MeshNode* node
218           = static_cast<const SMDS_MeshNode *>(nodesIt->next());
219         if ( i == 3 ) i = 2;
220         nPnt[ i++ ].SetCoord( node->X(), node->Y(), node->Z() );
221         // check position
222         const SMDS_PositionPtr& pos = node->GetPosition();
223         if ( !pos ) continue;
224         if ( pos->GetTypeOfPosition() == SMDS_TOP_FACE ) {
225           fPos = dynamic_cast< const SMDS_FacePosition* >( pos.get() );
226         }
227         else if ( pos->GetTypeOfPosition() == SMDS_TOP_VERTEX ) {
228           vID = pos->GetShapeId();
229         }
230       }
231       if ( fPos || ( !normalOK && vID )) {
232         // compute normal
233         gp_Vec v01( nPnt[0], nPnt[1] ), v02( nPnt[0], nPnt[2] );
234         if ( v01.SquareMagnitude() > RealSmall() &&
235              v02.SquareMagnitude() > RealSmall() )
236         {
237           Ne = v01 ^ v02;
238           normalOK = ( Ne.SquareMagnitude() > RealSmall() );
239         }
240         // we need position on theFace or at least on vertex
241         if ( normalOK ) {
242           vertexID = vID;
243           if ((facePos = fPos))
244             break;
245         }
246       }
247     }
248   }
249   if ( !normalOK )
250     return isReversed;
251
252   // node position on face
253   double u,v;
254   if ( facePos ) {
255     u = facePos->GetUParameter();
256     v = facePos->GetVParameter();
257   }
258   else if ( vertexID ) {
259     TopoDS_Shape V = theMeshDS->IndexToShape( vertexID );
260     if ( V.IsNull() || V.ShapeType() != TopAbs_VERTEX )
261       return isReversed;
262     gp_Pnt2d uv = BRep_Tool::Parameters( TopoDS::Vertex( V ), theFace );
263     u = uv.X();
264     v = uv.Y();
265   }
266   else
267   {
268     return isReversed;
269   }
270
271   // face normal at node position
272   TopLoc_Location loc;
273   Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
274   if ( surf.IsNull() || surf->Continuity() < GeomAbs_C1 ) return isReversed;
275   gp_Vec d1u, d1v;
276   surf->D1( u, v, nPnt[0], d1u, d1v );
277   gp_Vec Nf = (d1u ^ d1v).Transformed( loc );
278
279   if ( theFace.Orientation() == TopAbs_REVERSED )
280     Nf.Reverse();
281
282   return Ne * Nf < 0.;
283 }
284
285 //================================================================================
286 /*!
287  * \brief Initialize my parameter values by the mesh built on the geometry
288  * \param theMesh - the built mesh
289  * \param theShape - the geometry of interest
290  * \retval bool - true if parameter values have been successfully defined
291  *
292  * Just return false as the algorithm does not hold parameters values
293  */
294 //================================================================================
295
296 bool SMESH_Algo::SetParametersByMesh(const SMESH_Mesh* /*theMesh*/,
297                                      const TopoDS_Shape& /*theShape*/)
298 {
299   return false;
300 }
301
302 //================================================================================
303 /*!
304  * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
305  * \param theMesh - The mesh containing nodes
306  * \param theEdge - The geometrical edge of interest
307  * \param theParams - The resulting vector of sorted node parameters
308  * \retval bool - false if not all parameters are OK
309  */
310 //================================================================================
311
312 bool SMESH_Algo::GetNodeParamOnEdge(const SMESHDS_Mesh* theMesh,
313                                     const TopoDS_Edge&  theEdge,
314                                     vector< double > &  theParams)
315 {
316   theParams.clear();
317
318   if ( !theMesh || theEdge.IsNull() )
319     return false;
320
321   SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
322   if ( !eSubMesh || !eSubMesh->GetElements()->more() )
323     return false; // edge is not meshed
324
325   int nbEdgeNodes = 0;
326   set < double > paramSet;
327   if ( eSubMesh )
328   {
329     // loop on nodes of an edge: sort them by param on edge
330     SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
331     while ( nIt->more() )
332     {
333       const SMDS_MeshNode* node = nIt->next();
334       const SMDS_PositionPtr& pos = node->GetPosition();
335       if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
336         return false;
337       const SMDS_EdgePosition* epos =
338         static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
339       paramSet.insert( epos->GetUParameter() );
340       ++nbEdgeNodes;
341     }
342   }
343   // add vertex nodes params
344   Standard_Real f, l;
345   BRep_Tool::Range(theEdge, f, l);
346   paramSet.insert( f );
347   paramSet.insert( l );
348   if ( paramSet.size() != nbEdgeNodes + 2 )
349     return false; // there are equal parameters
350
351   // fill the vector
352   theParams.resize( paramSet.size() );
353   set < double >::iterator   par    = paramSet.begin();
354   vector< double >::iterator vecPar = theParams.begin();
355   for ( ; par != paramSet.end(); ++par, ++vecPar )
356     *vecPar = *par;
357
358   return theParams.size() > 1;
359 }
360
361 //================================================================================
362 /*!
363  * \brief Make filter recognize only compatible hypotheses
364  * \param theFilter - the filter to initialize
365  * \param ignoreAuxiliary - make filter ignore compatible auxiliary hypotheses
366  */
367 //================================================================================
368
369 bool SMESH_Algo::InitCompatibleHypoFilter( SMESH_HypoFilter & theFilter,
370                                            const bool         ignoreAuxiliary) const
371 {
372   if ( !_compatibleHypothesis.empty() )
373   {
374     theFilter.Init( theFilter.HasName( _compatibleHypothesis[0] ));
375     for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
376       theFilter.Or( theFilter.HasName( _compatibleHypothesis[ i ] ));
377
378     if ( ignoreAuxiliary )
379       theFilter.AndNot( theFilter.IsAuxiliary() );
380
381     return true;
382   }
383   return false;
384 }
385
386 //================================================================================
387 /*!
388  * \brief Return continuity of two edges
389  * \param E1 - the 1st edge
390  * \param E2 - the 2nd edge
391  * \retval GeomAbs_Shape - regularity at the junction between E1 and E2
392  */
393 //================================================================================
394
395 GeomAbs_Shape SMESH_Algo::Continuity(const TopoDS_Edge & E1,
396                                      const TopoDS_Edge & E2)
397 {
398   TopoDS_Vertex V = TopExp::LastVertex (E1, true);
399   if ( !V.IsSame( TopExp::FirstVertex(E2, true )))
400     if ( !TopExp::CommonVertex( E1, E2, V ))
401       return GeomAbs_C0;
402   Standard_Real u1 = BRep_Tool::Parameter( V, E1 );
403   Standard_Real u2 = BRep_Tool::Parameter( V, E2 );
404   BRepAdaptor_Curve C1( E1 ), C2( E2 );
405   try {
406 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
407     OCC_CATCH_SIGNALS;
408 #endif
409     return BRepLProp::Continuity(C1, C2, u1, u2);
410   }
411   catch (Standard_Failure) {
412   }
413   return GeomAbs_C0;
414 }
415
416 //================================================================================
417 /*!
418  * \brief Return the node built on a vertex
419  * \param V - the vertex
420  * \param meshDS - mesh
421  * \retval const SMDS_MeshNode* - found node or NULL
422  */
423 //================================================================================
424
425 const SMDS_MeshNode* SMESH_Algo::VertexNode(const TopoDS_Vertex& V,
426                                             SMESHDS_Mesh*        meshDS)
427 {
428   if ( SMESHDS_SubMesh* sm = meshDS->MeshElements(V) ) {
429     SMDS_NodeIteratorPtr nIt= sm->GetNodes();
430     if (nIt->more())
431       return nIt->next();
432   }
433   return 0;
434 }
435
436 //================================================================================
437 /*!
438  * \brief Sets event listener to submeshes if necessary
439  * \param subMesh - submesh where algo is set
440  * 
441  * After being set, event listener is notified on each event of a submesh.
442  * By default non listener is set
443  */
444 //================================================================================
445
446 void SMESH_Algo::SetEventListener(SMESH_subMesh* /*subMesh*/)
447 {
448 }
449
450 //================================================================================
451 /*!
452  * \brief Allow algo to do something after persistent restoration
453  * \param subMesh - restored submesh
454  *
455  * This method is called only if a submesh has HYP_OK algo_state.
456  */
457 //================================================================================
458
459 void SMESH_Algo::SubmeshRestored(SMESH_subMesh* /*subMesh*/)
460 {
461 }
462
463 //================================================================================
464 /*!
465  * \brief Computes mesh without geometry
466  * \param aMesh - the mesh
467  * \param aHelper - helper that must be used for adding elements to \aaMesh
468  * \retval bool - is a success
469  */
470 //================================================================================
471
472 bool SMESH_Algo::Compute(SMESH_Mesh & /*aMesh*/, SMESH_MesherHelper* aHelper)
473 {
474   return false;
475 }