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