Salome HOME
01e8398abdf8d0640834d55b255dafe69d31ad74
[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 }
58
59 //=============================================================================
60 /*!
61  *  
62  */
63 //=============================================================================
64
65 StdMeshers_AutomaticLength::~StdMeshers_AutomaticLength()
66 {
67 }
68
69 //================================================================================
70 /*!
71  * \brief Return pointer to TopoDS_TShape
72   * \param theShape - The TopoDS_Shape
73   * \retval inline const TopoDS_TShape* - result
74  */
75 //================================================================================
76
77 inline const TopoDS_TShape* getTShape(const TopoDS_Shape& theShape)
78 {
79   return theShape.TShape().operator->();
80 }
81 //================================================================================
82 /*!
83  * \brief Compute segment length for all edges
84   * \param theMesh - The mesh
85   * \param theTShapeToLengthMap - The map of edge to segment length
86  */
87 //================================================================================
88
89 static void computeLengths( const SMESH_Mesh*                   theMesh,
90                             map<const TopoDS_TShape*, double> & theTShapeToLengthMap)
91 {
92   theTShapeToLengthMap.clear();
93
94   SMESHDS_Mesh* aMesh = const_cast< SMESH_Mesh* > ( theMesh )->GetMeshDS();
95   TopoDS_Shape aMainShape = aMesh->ShapeToMesh();
96
97   // Find length of longest and shortest edge
98   double Lmin = DBL_MAX, Lmax = -DBL_MAX;
99   TopTools_IndexedMapOfShape edgeMap;
100   TopExp::MapShapes( aMainShape, TopAbs_EDGE, edgeMap);
101   for ( int i = 1; i <= edgeMap.Extent(); ++i )
102   {
103     TopoDS_Edge edge = TopoDS::Edge( edgeMap(i) );
104     //if ( BRep_Tool::Degenerated( edge )) continue;
105
106     Standard_Real L = SMESH_Algo::EdgeLength( edge );
107     if ( L < DBL_MIN ) continue;
108
109     if ( L > Lmax ) Lmax = L;
110     if ( L < Lmin ) Lmin = L;
111
112     // remember i-th edge length
113     theTShapeToLengthMap.insert( make_pair( getTShape( edge ), L ));
114   }
115
116   // Compute S0
117
118   // image attached to PAL10237
119
120 //   NbSeg
121 //     ^
122 //     |
123 //   10|\
124 //     | \
125 //     |  \
126 //     |   \
127 //    5|    --------
128 //     |
129 //     +------------>
130 //     1    10       Lmax/Lmin
131
132   const int NbSegMin = 5, NbSegMax = 10; //  on axis NbSeg
133   const double Lrat1 = 1., Lrat2 = 10.;  //  on axis Lmax/Lmin
134
135   double Lratio = Lmax/Lmin;
136   double NbSeg = NbSegMin;
137   if ( Lratio < Lrat2 )
138     NbSeg += ( Lrat2 - Lratio ) / ( Lrat2 - Lrat1 )  * ( NbSegMax - NbSegMin );
139
140   double S0 = Lmin / (int) NbSeg;
141   MESSAGE( "S0 = " << S0 << ", Lmin = " << Lmin << ", Nbseg = " << (int) NbSeg);
142
143   // Compute segments length for all edges
144
145   // S = S0 * f(L/Lmin) where f(x) = 1 + (2/Pi * 7 * atan(x/5) )
146   // =>
147   // S = S0 * ( 1 + 14/PI * atan( L / ( 5 * Lmin )))
148
149   const double a14divPI = 14. / PI, a5xLmin = 5 * Lmin;
150   map<const TopoDS_TShape*, double>::iterator tshape_length = theTShapeToLengthMap.begin();
151   for ( ; tshape_length != theTShapeToLengthMap.end(); ++tshape_length )
152   {
153     double & L = tshape_length->second;
154     L = S0 * ( 1. + a14divPI * atan( L / a5xLmin ));
155   }
156 }
157
158 //=============================================================================
159 /*!
160  *  
161  */
162 //=============================================================================
163
164 double StdMeshers_AutomaticLength::GetLength(const SMESH_Mesh*   theMesh,
165                                              const TopoDS_Shape& anEdge)
166   throw(SALOME_Exception)
167 {
168   if ( !theMesh ) throw SALOME_Exception(LOCALIZED("NULL Mesh"));
169
170   if ( anEdge.IsNull() || anEdge.ShapeType() != TopAbs_EDGE )
171     throw SALOME_Exception(LOCALIZED("Bad edge shape"));
172
173   if ( theMesh != _mesh ) {
174     computeLengths( theMesh, _TShapeToLength );
175     _mesh = theMesh;
176   }
177
178   map<const TopoDS_TShape*, double>::iterator tshape_length =
179     _TShapeToLength.find( getTShape( anEdge ));
180
181   if ( tshape_length == _TShapeToLength.end() )
182     return 1; // it is a dgenerated edge
183
184   return tshape_length->second;
185 }
186
187 //=============================================================================
188 /*!
189  *  
190  */
191 //=============================================================================
192
193 ostream & StdMeshers_AutomaticLength::SaveTo(ostream & save)
194 {
195   return save;
196 }
197
198 //=============================================================================
199 /*!
200  *  
201  */
202 //=============================================================================
203
204 istream & StdMeshers_AutomaticLength::LoadFrom(istream & load)
205 {
206   return load;
207 }
208
209 //=============================================================================
210 /*!
211  *  
212  */
213 //=============================================================================
214
215 ostream & operator <<(ostream & save, StdMeshers_AutomaticLength & hyp)
216 {
217   return hyp.SaveTo( save );
218 }
219
220 //=============================================================================
221 /*!
222  *  
223  */
224 //=============================================================================
225
226 istream & operator >>(istream & load, StdMeshers_AutomaticLength & hyp)
227 {
228   return hyp.LoadFrom( load );
229 }