Salome HOME
Merge branch 'V9_9_BR'
[modules/smesh.git] / src / StdMeshers / StdMeshers_AutomaticLength.cxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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, or (at your option) any later version.
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 //  SMESH SMESH : implementation of SMESH idl descriptions
24 //  File   : StdMeshers_AutomaticLength.cxx
25 //  Author : Edward AGAPOV, OCC
26 //  Module : SMESH
27
28 #include "StdMeshers_AutomaticLength.hxx"
29
30 #include "SMESH_Mesh.hxx"
31 #include "SMESHDS_Mesh.hxx"
32 #include "SMESH_Algo.hxx"
33 #include "SMESHDS_SubMesh.hxx"
34
35 #include "utilities.h"
36
37 #include <TopTools_IndexedMapOfShape.hxx>
38 #include <TopExp.hxx>
39 #include <TopoDS.hxx>
40 #include <TopoDS_Edge.hxx>
41
42 using namespace std;
43
44 //=============================================================================
45 /*!
46  *  
47  */
48 //=============================================================================
49
50 StdMeshers_AutomaticLength::StdMeshers_AutomaticLength(int hypId, SMESH_Gen * gen)
51   :SMESH_Hypothesis(hypId, gen)
52 {
53   _name = "AutomaticLength";
54   _param_algo_dim = 1; // is used by SMESH_Regular_1D
55
56   _mesh = 0;
57   _fineness = 0;
58 }
59
60 //=============================================================================
61 /*!
62  *  
63  */
64 //=============================================================================
65
66 StdMeshers_AutomaticLength::~StdMeshers_AutomaticLength()
67 {
68 }
69
70 //================================================================================
71 /*!
72  * \brief Set Fineness
73  * \param theFineness - The Fineness value [0.0-1.0],
74  *                        0 - coarse mesh
75  *                        1 - fine mesh
76  * 
77  * Raise if theFineness is out of range
78  * The "Initial Number of Elements on the Shortest Edge" (S0)
79  * is divided by (0.5 + 4.5 x theFineness)
80  */
81 //================================================================================
82
83 const double theCoarseConst = 0.5;
84 const double theFineConst   = 4.5;
85
86 void StdMeshers_AutomaticLength::SetFineness(double theFineness)
87 {
88   if ( theFineness < 0.0 || theFineness > 1.0 )
89     throw SALOME_Exception(LOCALIZED("theFineness is out of range [0.0-1.0]"));
90
91   if ( _fineness != theFineness )
92   {
93     NotifySubMeshesHypothesisModification();
94     _fineness = theFineness;
95   }
96 }
97
98 namespace {
99
100   //================================================================================
101   /*!
102    * \brief Return pointer to TopoDS_TShape
103    * \param theShape - The TopoDS_Shape
104    * \retval inline const TopoDS_TShape* - result
105    */
106   //================================================================================
107
108   inline const TopoDS_TShape* getTShape(const TopoDS_Shape& theShape)
109   {
110     return theShape.TShape().operator->();
111   }
112
113   //================================================================================
114   /*!
115    * \brief computes segment length by S0 and edge length
116    */
117   //================================================================================
118
119   const double a14divPI = 14. / M_PI;
120
121
122   inline double segLength(double S0, double edgeLen, double minLen )
123   {
124     // PAL10237
125     // S = S0 * f(L/Lmin) where f(x) = 1 + (2/Pi * 7 * atan(x/5) )
126
127     // =>
128     // S = S0 * ( 1 + 14/PI * atan( L / ( 5 * Lmin )))
129     return S0 * ( 1. + a14divPI * atan( edgeLen / ( 5 * minLen )));
130   }
131 #if 0
132   //const double a14divPI = 14. / M_PI;
133   const double a2div7divPI = 2. / 7. / M_PI;
134
135   inline double segLength(double S0, double edgeLen, double minLen )
136   {
137     // PAL10237
138     // S = S0 * f(L/Lmin) where
139     // f(x) = 1 + (7 * 2/Pi * atan(x/5))
140     // =>
141     // S = S0 * ( 1 + 14/PI * atan( L / ( 5 * Lmin )))
142     //
143     // return S0 * ( 1. + a14divPI * atan( edgeLen / ( 5 * minLen )));
144
145     // The above formular gives too short segments when Lmax/Lmin is too high
146     // because by this formular the largest segment is only 8 times longer than the
147     // shortest one ( 2/Pi * atan(x/5) varies within [0,1] ). So a new formular is:
148     //
149     // f(x) = 1 + (x/7 * 2/Pi * atan(x/5))
150     // =>
151     // S = S0 * ( 1 + 2/7/PI * L/Lmin * atan( 5 * L/Lmin ))
152     //
153     const double Lratio = edgeLen / minLen;
154     return S0 * ( 1. + a2div7divPI * Lratio * atan( 5 * Lratio ));
155   }
156 #endif
157   //================================================================================
158   /*!
159    * \brief Compute segment length for all edges
160    * \param theMesh - The mesh
161    * \param theTShapeToLengthMap - The map of edge to segment length
162    */
163   //================================================================================
164
165   void computeLengths( SMESHDS_Mesh*                       aMesh,
166                        map<const TopoDS_TShape*, double> & theTShapeToLengthMap,
167                        double &                            theS0,
168                        double &                            theMinLen)
169   {
170     theTShapeToLengthMap.clear();
171
172     TopoDS_Shape aMainShape = aMesh->ShapeToMesh();
173
174     // Find length of longest and shortest edge
175     double Lmin = DBL_MAX, Lmax = -DBL_MAX;
176     TopTools_IndexedMapOfShape edgeMap;
177     TopExp::MapShapes( aMainShape, TopAbs_EDGE, edgeMap);
178     for ( int i = 1; i <= edgeMap.Extent(); ++i )
179     {
180       TopoDS_Edge edge = TopoDS::Edge( edgeMap(i) );
181       //if ( BRep_Tool::Degenerated( edge )) continue;
182
183       Standard_Real L = SMESH_Algo::EdgeLength( edge );
184       if ( L < DBL_MIN ) continue;
185
186       if ( L > Lmax ) Lmax = L;
187       if ( L < Lmin ) Lmin = L;
188
189       // remember i-th edge length
190       theTShapeToLengthMap.insert( make_pair( getTShape( edge ), L ));
191     }
192
193     // Compute S0 - minimal segment length, is computed by the shortest EDGE
194
195     /* image attached to PAL10237
196
197        NbSeg (on the shortest EDGE)
198          ^
199          |
200        10|\
201          | \
202          |  \
203          |   \
204         5|    --------
205          |
206          +------------>
207          1    10       Lmax/Lmin
208     */
209     const int NbSegMin = 5, NbSegMax = 10; //  on axis NbSeg
210     const double Lrat1 = 1., Lrat2 = 10.;  //  on axis Lmax/Lmin
211
212     double Lratio = Lmax/Lmin;
213     double NbSeg = NbSegMin;
214     if ( Lratio < Lrat2 )
215       NbSeg += ( Lrat2 - Lratio ) / ( Lrat2 - Lrat1 )  * ( NbSegMax - NbSegMin );
216
217     double S0 = Lmin / (int) NbSeg;
218     //MESSAGE( "S0 = " << S0 << ", Lmin = " << Lmin << ", Nbseg = " << (int) NbSeg);
219
220     // Compute segments length for all edges
221
222     map<const TopoDS_TShape*, double>::iterator tshape_length = theTShapeToLengthMap.begin();
223     for ( ; tshape_length != theTShapeToLengthMap.end(); ++tshape_length )
224     {
225       double & L = tshape_length->second;
226       L = segLength( S0, L, Lmin );
227     }
228     theS0 = S0;
229     theMinLen = Lmin;
230   }
231 }
232
233 //=============================================================================
234 /*!
235  * \brief Computes segment length for an edge of given length
236  */
237 //=============================================================================
238
239 double StdMeshers_AutomaticLength::GetLength(const SMESH_Mesh* theMesh,
240                                              const double      theEdgeLength)
241 {
242   if ( !theMesh ) throw SALOME_Exception(LOCALIZED("NULL Mesh"));
243
244   SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* > ( theMesh )->GetMeshDS();
245   if ( theMesh != _mesh )
246   {
247     computeLengths( aMeshDS, _TShapeToLength, _S0, _minLen );
248     _mesh = theMesh;
249   }
250   double L = segLength( _S0, theEdgeLength, _minLen );
251   return L / (theCoarseConst + theFineConst * _fineness);
252 }
253
254 //=============================================================================
255 /*!
256  *  
257  */
258 //=============================================================================
259
260 double StdMeshers_AutomaticLength::GetLength(const SMESH_Mesh*   theMesh,
261                                              const TopoDS_Shape& anEdge)
262 {
263   if ( !theMesh ) throw SALOME_Exception(LOCALIZED("NULL Mesh"));
264
265   if ( anEdge.IsNull() || anEdge.ShapeType() != TopAbs_EDGE )
266     throw SALOME_Exception(LOCALIZED("Bad edge shape"));
267
268   if ( theMesh != _mesh )
269   {
270     SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* > ( theMesh )->GetMeshDS();
271     computeLengths( aMeshDS, _TShapeToLength, _S0, _minLen );
272     _mesh = theMesh;
273   }
274
275   map<const TopoDS_TShape*, double>::iterator tshape_length =
276     _TShapeToLength.find( getTShape( anEdge ));
277
278   if ( tshape_length == _TShapeToLength.end() )
279     return 1; // it is a dgenerated edge
280
281   return tshape_length->second / (theCoarseConst + theFineConst * _fineness);
282 }
283
284 //=============================================================================
285 /*!
286  *  
287  */
288 //=============================================================================
289
290 ostream & StdMeshers_AutomaticLength::SaveTo(ostream & save)
291 {
292   save << _fineness;
293   return save;
294 }
295
296 //=============================================================================
297 /*!
298  *  
299  */
300 //=============================================================================
301
302 istream & StdMeshers_AutomaticLength::LoadFrom(istream & load)
303 {
304   if ( ! ( load >> _fineness ))
305     load.clear(ios::badbit | load.rdstate());
306   return load;
307 }
308
309 //=============================================================================
310 /*!
311  *  
312  */
313 //=============================================================================
314
315 ostream & operator <<(ostream & save, StdMeshers_AutomaticLength & hyp)
316 {
317   return hyp.SaveTo( save );
318 }
319
320 //=============================================================================
321 /*!
322  *  
323  */
324 //=============================================================================
325
326 istream & operator >>(istream & load, StdMeshers_AutomaticLength & hyp)
327 {
328   return hyp.LoadFrom( load );
329 }
330
331 //================================================================================
332 /*!
333  * \brief Initialize Fineness by the mesh built on the geometry
334  * \param theMesh - the built mesh
335  * \param theShape - the geometry of interest
336  * \retval bool - true if parameter values have been successfully defined
337  */
338 //================================================================================
339
340 bool StdMeshers_AutomaticLength::SetParametersByMesh(const SMESH_Mesh*   theMesh,
341                                                      const TopoDS_Shape& theShape)
342 {
343   if ( !theMesh || theShape.IsNull() )
344     return false;
345
346   _fineness = 0;
347
348   SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
349
350   int nbEdges = 0;
351   TopTools_IndexedMapOfShape edgeMap;
352   TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
353   for ( int i = 1; i <= edgeMap.Extent(); ++i )
354   {
355     const TopoDS_Edge& edge = TopoDS::Edge( edgeMap( i ));
356
357     // assure the base automatic length is stored in _TShapeToLength
358     if ( i == 1 ) 
359       GetLength( theMesh, edge );
360
361     // get current segment length
362     double L = SMESH_Algo::EdgeLength( edge );
363     if ( L <= DBL_MIN )
364       continue;
365     SMESHDS_SubMesh * eSubMesh = aMeshDS->MeshElements( edge );
366     if ( !eSubMesh )
367       return false;
368     smIdType nbSeg = eSubMesh->NbElements();
369     if ( nbSeg < 1 )
370       continue;
371     double segLen = L / nbSeg;
372
373     // get segment length from _TShapeToLength
374     map<const TopoDS_TShape*, double>::iterator tshape_length =
375       _TShapeToLength.find( getTShape( edge ));
376     if ( tshape_length == _TShapeToLength.end() )
377       continue;
378     double autoLen = tshape_length->second;
379
380     // segLen = autoLen / (theCoarseConst + theFineConst * _fineness) -->
381     _fineness += ( autoLen / segLen - theCoarseConst ) / theFineConst;
382
383     ++nbEdges;
384   }
385   if ( nbEdges )
386     _fineness /= nbEdges;
387
388   if (_fineness > 1.0)
389     _fineness = 1.0;
390   else if (_fineness < 0.0)
391     _fineness = 0.0;
392
393   return nbEdges;
394 }
395
396 //================================================================================
397 /*!
398  * \brief Initialize my parameter values by default parameters.
399  *  \retval bool - true if parameter values have been successfully defined
400  */
401 //================================================================================
402
403 bool StdMeshers_AutomaticLength::SetParametersByDefaults(const TDefaults&  /*theDflts*/,
404                                                          const SMESH_Mesh* /*theMesh*/)
405 {
406   return false;
407
408   // assure the base automatic length is stored in _TShapeToLength
409 //   GetLength( theMesh, elemLenght );
410
411 //   // find maximal edge length
412 //   double maxLen = 0;
413 //   map<const TopoDS_TShape*, double>::iterator
414 //     tshape_length = _TShapeToLength.begin(), slEnd = _TShapeToLength.end();
415 //   for ( ; tshape_length != slEnd; ++tshape_length )
416 //     if ( tshape_length->second > maxLen )
417 //       maxLen = tshape_length->second;
418
419 //   // automatic length for longest element
420 //   double autoLen = GetLength( theMesh, maxLen );
421
422 //   // elemLenght = autoLen / (theCoarseConst + theFineConst * _fineness) -->
423 //   _fineness = ( autoLen / elemLenght - theCoarseConst ) / theFineConst;
424
425 //   return true;
426 }