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