Salome HOME
36ae6bec58e3668e2b5fd495ecae92302421b149
[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     Standard_Real L = SMESH_Algo::EdgeLength( edge );
105     if ( L > Lmax )
106       Lmax = L;
107     if ( L < Lmin )
108       Lmin = L;
109     // remember i-th edge length
110     theTShapeToLengthMap.insert( theTShapeToLengthMap.end(),
111                                  make_pair( getTShape( edge ), L ));
112   }
113
114   // Compute S0
115
116   // image attached to PAL10237
117
118 //   NbSeg
119 //     ^
120 //     |
121 //   10|\
122 //     | \
123 //     |  \
124 //     |   \
125 //    5|    --------
126 //     |
127 //     +------------>
128 //     1    10       Lmax/Lmin
129
130   const int NbSegMin = 5, NbSegMax = 10; //  on axis NbSeg
131   const double Lrat1 = 1., Lrat2 = 10.;  //  on axis Lmax/Lmin
132
133   double Lratio = Lmax/Lmin;
134   double NbSeg = NbSegMin;
135   if ( Lratio < Lrat2 )
136     NbSeg += ( Lrat2 - Lratio ) / ( Lrat2 - Lrat1 )  * ( NbSegMax - NbSegMin );
137
138   double S0 = Lmin / (int) NbSeg;
139   MESSAGE( "S0 = " << S0 << ", Lmin = " << Lmin << ", Nbseg = " << (int) NbSeg);
140
141   // Compute segments length for all edges
142
143   // S = S0 * f(L/Lmin) where f(x) = 1 + (2/Pi * 7 * atan(x/5) )
144   // =>
145   // S = S0 * ( 1 + 14/PI * atan( L / ( 5 * Lmin )))
146
147   const double a14divPI = 14. / PI, a5xLmin = 5 * Lmin;
148   map<const TopoDS_TShape*, double>::iterator tshape_length = theTShapeToLengthMap.begin();
149   for ( ; tshape_length != theTShapeToLengthMap.end(); ++tshape_length )
150   {
151     double & L = tshape_length->second;
152     L = S0 * ( 1. + a14divPI * atan( L / a5xLmin ));
153   }
154 }
155
156 //=============================================================================
157 /*!
158  *  
159  */
160 //=============================================================================
161
162 double StdMeshers_AutomaticLength::GetLength(const SMESH_Mesh*   theMesh,
163                                              const TopoDS_Shape& anEdge)
164   throw(SALOME_Exception)
165 {
166   if ( !theMesh ) throw SALOME_Exception(LOCALIZED("NULL Mesh"));
167
168   if ( anEdge.IsNull() || anEdge.ShapeType() != TopAbs_EDGE )
169     throw SALOME_Exception(LOCALIZED("Bad edge shape"));
170
171   if ( theMesh != _mesh ) {
172     computeLengths( theMesh, _TShapeToLength );
173     _mesh = theMesh;
174   }
175
176   map<const TopoDS_TShape*, double>::iterator tshape_length =
177     _TShapeToLength.find( getTShape( anEdge ));
178   ASSERT( tshape_length != _TShapeToLength.end() );
179   return tshape_length->second;
180 }
181
182 //=============================================================================
183 /*!
184  *  
185  */
186 //=============================================================================
187
188 ostream & StdMeshers_AutomaticLength::SaveTo(ostream & save)
189 {
190   return save;
191 }
192
193 //=============================================================================
194 /*!
195  *  
196  */
197 //=============================================================================
198
199 istream & StdMeshers_AutomaticLength::LoadFrom(istream & load)
200 {
201   return load;
202 }
203
204 //=============================================================================
205 /*!
206  *  
207  */
208 //=============================================================================
209
210 ostream & operator <<(ostream & save, StdMeshers_AutomaticLength & hyp)
211 {
212   return hyp.SaveTo( save );
213 }
214
215 //=============================================================================
216 /*!
217  *  
218  */
219 //=============================================================================
220
221 istream & operator >>(istream & load, StdMeshers_AutomaticLength & hyp)
222 {
223   return hyp.LoadFrom( load );
224 }