Salome HOME
1176ebe185ad5b6c3d2e49f49f2a26ee8402c1b5
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
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
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,
51         SMESH_Gen * gen):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 void StdMeshers_AutomaticLength::SetFineness(double theFineness)
84   throw(SALOME_Exception)
85 {
86   if ( theFineness < 0.0 || theFineness > 1.0 )
87     throw SALOME_Exception(LOCALIZED("theFineness is out of range [0.0-1.0]"));
88
89   if ( _fineness != theFineness )
90   {
91     NotifySubMeshesHypothesisModification();
92     _fineness = theFineness;
93   }
94 }
95
96 //================================================================================
97 /*!
98  * \brief Return pointer to TopoDS_TShape
99   * \param theShape - The TopoDS_Shape
100   * \retval inline const TopoDS_TShape* - result
101  */
102 //================================================================================
103
104 inline const TopoDS_TShape* getTShape(const TopoDS_Shape& theShape)
105 {
106   return theShape.TShape().operator->();
107 }
108 //================================================================================
109 /*!
110  * \brief Compute segment length for all edges
111   * \param theMesh - The mesh
112   * \param theTShapeToLengthMap - The map of edge to segment length
113  */
114 //================================================================================
115
116 static void computeLengths( const SMESH_Mesh*                   theMesh,
117                             map<const TopoDS_TShape*, double> & theTShapeToLengthMap)
118 {
119   theTShapeToLengthMap.clear();
120
121   SMESHDS_Mesh* aMesh = const_cast< SMESH_Mesh* > ( theMesh )->GetMeshDS();
122   TopoDS_Shape aMainShape = aMesh->ShapeToMesh();
123
124   // Find length of longest and shortest edge
125   double Lmin = DBL_MAX, Lmax = -DBL_MAX;
126   TopTools_IndexedMapOfShape edgeMap;
127   TopExp::MapShapes( aMainShape, TopAbs_EDGE, edgeMap);
128   for ( int i = 1; i <= edgeMap.Extent(); ++i )
129   {
130     TopoDS_Edge edge = TopoDS::Edge( edgeMap(i) );
131     //if ( BRep_Tool::Degenerated( edge )) continue;
132
133     Standard_Real L = SMESH_Algo::EdgeLength( edge );
134     if ( L < DBL_MIN ) continue;
135
136     if ( L > Lmax ) Lmax = L;
137     if ( L < Lmin ) Lmin = L;
138
139     // remember i-th edge length
140     theTShapeToLengthMap.insert( make_pair( getTShape( edge ), L ));
141   }
142
143   // Compute S0
144
145   // image attached to PAL10237
146
147 //   NbSeg
148 //     ^
149 //     |
150 //   10|\
151 //     | \
152 //     |  \
153 //     |   \
154 //    5|    --------
155 //     |
156 //     +------------>
157 //     1    10       Lmax/Lmin
158
159   const int NbSegMin = 5, NbSegMax = 10; //  on axis NbSeg
160   const double Lrat1 = 1., Lrat2 = 10.;  //  on axis Lmax/Lmin
161
162   double Lratio = Lmax/Lmin;
163   double NbSeg = NbSegMin;
164   if ( Lratio < Lrat2 )
165     NbSeg += ( Lrat2 - Lratio ) / ( Lrat2 - Lrat1 )  * ( NbSegMax - NbSegMin );
166
167   double S0 = Lmin / (int) NbSeg;
168   MESSAGE( "S0 = " << S0 << ", Lmin = " << Lmin << ", Nbseg = " << (int) NbSeg);
169
170   // Compute segments length for all edges
171
172   // S = S0 * f(L/Lmin) where f(x) = 1 + (2/Pi * 7 * atan(x/5) )
173   // =>
174   // S = S0 * ( 1 + 14/PI * atan( L / ( 5 * Lmin )))
175
176   const double a14divPI = 14. / PI, a5xLmin = 5 * Lmin;
177   map<const TopoDS_TShape*, double>::iterator tshape_length = theTShapeToLengthMap.begin();
178   for ( ; tshape_length != theTShapeToLengthMap.end(); ++tshape_length )
179   {
180     double & L = tshape_length->second;
181     L = S0 * ( 1. + a14divPI * atan( L / a5xLmin ));
182   }
183 }
184
185 //=============================================================================
186 /*!
187  *  
188  */
189 //=============================================================================
190
191 double StdMeshers_AutomaticLength::GetLength(const SMESH_Mesh*   theMesh,
192                                              const TopoDS_Shape& anEdge)
193   throw(SALOME_Exception)
194 {
195   if ( !theMesh ) throw SALOME_Exception(LOCALIZED("NULL Mesh"));
196
197   if ( anEdge.IsNull() || anEdge.ShapeType() != TopAbs_EDGE )
198     throw SALOME_Exception(LOCALIZED("Bad edge shape"));
199
200   if ( theMesh != _mesh ) {
201     computeLengths( theMesh, _TShapeToLength );
202     _mesh = theMesh;
203   }
204
205   map<const TopoDS_TShape*, double>::iterator tshape_length =
206     _TShapeToLength.find( getTShape( anEdge ));
207
208   if ( tshape_length == _TShapeToLength.end() )
209     return 1; // it is a dgenerated edge
210
211   return tshape_length->second / (0.5 + 4.5 * _fineness);
212 }
213
214 //=============================================================================
215 /*!
216  *  
217  */
218 //=============================================================================
219
220 ostream & StdMeshers_AutomaticLength::SaveTo(ostream & save)
221 {
222   save << _fineness;
223   return save;
224 }
225
226 //=============================================================================
227 /*!
228  *  
229  */
230 //=============================================================================
231
232 istream & StdMeshers_AutomaticLength::LoadFrom(istream & load)
233 {
234   if ( ! ( load >> _fineness ))
235     load.clear(ios::badbit | load.rdstate());
236   return load;
237 }
238
239 //=============================================================================
240 /*!
241  *  
242  */
243 //=============================================================================
244
245 ostream & operator <<(ostream & save, StdMeshers_AutomaticLength & hyp)
246 {
247   return hyp.SaveTo( save );
248 }
249
250 //=============================================================================
251 /*!
252  *  
253  */
254 //=============================================================================
255
256 istream & operator >>(istream & load, StdMeshers_AutomaticLength & hyp)
257 {
258   return hyp.LoadFrom( load );
259 }