1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH : implementaion of SMESH idl descriptions
24 // File : StdMeshers_AutomaticLength.cxx
25 // Author : Edward AGAPOV, OCC
28 #include "StdMeshers_AutomaticLength.hxx"
30 #include "SMESH_Mesh.hxx"
31 #include "SMESHDS_Mesh.hxx"
32 #include "SMESH_Algo.hxx"
33 #include "SMESHDS_SubMesh.hxx"
35 #include "utilities.h"
37 #include <TopTools_IndexedMapOfShape.hxx>
40 #include <TopoDS_Edge.hxx>
44 //=============================================================================
48 //=============================================================================
50 StdMeshers_AutomaticLength::StdMeshers_AutomaticLength(int hypId, int studyId, SMESH_Gen * gen)
51 :SMESH_Hypothesis(hypId, studyId, gen)
53 _name = "AutomaticLength";
54 _param_algo_dim = 1; // is used by SMESH_Regular_1D
60 //=============================================================================
64 //=============================================================================
66 StdMeshers_AutomaticLength::~StdMeshers_AutomaticLength()
70 //================================================================================
73 * \param theFineness - The Fineness value [0.0-1.0],
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)
81 //================================================================================
83 const double theCoarseConst = 0.5;
84 const double theFineConst = 4.5;
86 void StdMeshers_AutomaticLength::SetFineness(double theFineness)
87 throw(SALOME_Exception)
89 if ( theFineness < 0.0 || theFineness > 1.0 )
90 throw SALOME_Exception(LOCALIZED("theFineness is out of range [0.0-1.0]"));
92 if ( _fineness != theFineness )
94 NotifySubMeshesHypothesisModification();
95 _fineness = theFineness;
101 //================================================================================
103 * \brief Return pointer to TopoDS_TShape
104 * \param theShape - The TopoDS_Shape
105 * \retval inline const TopoDS_TShape* - result
107 //================================================================================
109 inline const TopoDS_TShape* getTShape(const TopoDS_Shape& theShape)
111 return theShape.TShape().operator->();
114 //================================================================================
116 * \brief computes segment length by S0 and edge length
118 //================================================================================
120 //const double a14divPI = 14. / M_PI;
121 const double a2div7divPI = 2. / 7. / M_PI;
123 inline double segLength(double S0, double edgeLen, double minLen )
126 // S = S0 * f(L/Lmin) where
127 // f(x) = 1 + (7 * 2/Pi * atan(x/5))
129 // S = S0 * ( 1 + 14/PI * atan( L / ( 5 * Lmin )))
131 // return S0 * ( 1. + a14divPI * atan( edgeLen / ( 5 * minLen )));
133 // The above formular gives too short segments when Lmax/Lmin is too high
134 // because by this formular the largest segment is only 8 times longer than the
135 // shortest one ( 2/Pi * atan(x/5) varies within [0,1] ). So a new formular is:
137 // f(x) = 1 + (x/7 * 2/Pi * atan(x/5))
139 // S = S0 * ( 1 + 2/7/PI * L/Lmin * atan( 5 * L/Lmin ))
141 const double Lratio = edgeLen / minLen;
142 return S0 * ( 1. + a2div7divPI * Lratio * atan( 5 * Lratio ));
145 //================================================================================
147 * \brief Compute segment length for all edges
148 * \param theMesh - The mesh
149 * \param theTShapeToLengthMap - The map of edge to segment length
151 //================================================================================
153 void computeLengths( SMESHDS_Mesh* aMesh,
154 map<const TopoDS_TShape*, double> & theTShapeToLengthMap,
158 theTShapeToLengthMap.clear();
160 TopoDS_Shape aMainShape = aMesh->ShapeToMesh();
162 // Find length of longest and shortest edge
163 double Lmin = DBL_MAX, Lmax = -DBL_MAX;
164 TopTools_IndexedMapOfShape edgeMap;
165 TopExp::MapShapes( aMainShape, TopAbs_EDGE, edgeMap);
166 for ( int i = 1; i <= edgeMap.Extent(); ++i )
168 TopoDS_Edge edge = TopoDS::Edge( edgeMap(i) );
169 //if ( BRep_Tool::Degenerated( edge )) continue;
171 Standard_Real L = SMESH_Algo::EdgeLength( edge );
172 if ( L < DBL_MIN ) continue;
174 if ( L > Lmax ) Lmax = L;
175 if ( L < Lmin ) Lmin = L;
177 // remember i-th edge length
178 theTShapeToLengthMap.insert( make_pair( getTShape( edge ), L ));
181 // Compute S0 - minimal segement length, is computed by the shortest EDGE
183 /* image attached to PAL10237
185 NbSeg (on the shortest EDGE)
197 const int NbSegMin = 5, NbSegMax = 10; // on axis NbSeg
198 const double Lrat1 = 1., Lrat2 = 10.; // on axis Lmax/Lmin
200 double Lratio = Lmax/Lmin;
201 double NbSeg = NbSegMin;
202 if ( Lratio < Lrat2 )
203 NbSeg += ( Lrat2 - Lratio ) / ( Lrat2 - Lrat1 ) * ( NbSegMax - NbSegMin );
205 double S0 = Lmin / (int) NbSeg;
206 MESSAGE( "S0 = " << S0 << ", Lmin = " << Lmin << ", Nbseg = " << (int) NbSeg);
208 // Compute segments length for all edges
210 map<const TopoDS_TShape*, double>::iterator tshape_length = theTShapeToLengthMap.begin();
211 for ( ; tshape_length != theTShapeToLengthMap.end(); ++tshape_length )
213 double & L = tshape_length->second;
214 L = segLength( S0, L, Lmin );
221 //=============================================================================
223 * \brief Computes segment length for an edge of given length
225 //=============================================================================
227 double StdMeshers_AutomaticLength::GetLength(const SMESH_Mesh* theMesh,
228 const double theEdgeLength)
229 throw(SALOME_Exception)
231 if ( !theMesh ) throw SALOME_Exception(LOCALIZED("NULL Mesh"));
233 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* > ( theMesh )->GetMeshDS();
234 if ( theMesh != _mesh )
236 computeLengths( aMeshDS, _TShapeToLength, _S0, _minLen );
239 double L = segLength( _S0, theEdgeLength, _minLen );
240 return L / (theCoarseConst + theFineConst * _fineness);
243 //=============================================================================
247 //=============================================================================
249 double StdMeshers_AutomaticLength::GetLength(const SMESH_Mesh* theMesh,
250 const TopoDS_Shape& anEdge)
251 throw(SALOME_Exception)
253 if ( !theMesh ) throw SALOME_Exception(LOCALIZED("NULL Mesh"));
255 if ( anEdge.IsNull() || anEdge.ShapeType() != TopAbs_EDGE )
256 throw SALOME_Exception(LOCALIZED("Bad edge shape"));
258 if ( theMesh != _mesh )
260 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* > ( theMesh )->GetMeshDS();
261 computeLengths( aMeshDS, _TShapeToLength, _S0, _minLen );
265 map<const TopoDS_TShape*, double>::iterator tshape_length =
266 _TShapeToLength.find( getTShape( anEdge ));
268 if ( tshape_length == _TShapeToLength.end() )
269 return 1; // it is a dgenerated edge
271 return tshape_length->second / (theCoarseConst + theFineConst * _fineness);
274 //=============================================================================
278 //=============================================================================
280 ostream & StdMeshers_AutomaticLength::SaveTo(ostream & save)
286 //=============================================================================
290 //=============================================================================
292 istream & StdMeshers_AutomaticLength::LoadFrom(istream & load)
294 if ( ! ( load >> _fineness ))
295 load.clear(ios::badbit | load.rdstate());
299 //=============================================================================
303 //=============================================================================
305 ostream & operator <<(ostream & save, StdMeshers_AutomaticLength & hyp)
307 return hyp.SaveTo( save );
310 //=============================================================================
314 //=============================================================================
316 istream & operator >>(istream & load, StdMeshers_AutomaticLength & hyp)
318 return hyp.LoadFrom( load );
321 //================================================================================
323 * \brief Initialize Fineness by the mesh built on the geometry
324 * \param theMesh - the built mesh
325 * \param theShape - the geometry of interest
326 * \retval bool - true if parameter values have been successfully defined
328 //================================================================================
330 bool StdMeshers_AutomaticLength::SetParametersByMesh(const SMESH_Mesh* theMesh,
331 const TopoDS_Shape& theShape)
333 if ( !theMesh || theShape.IsNull() )
338 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
341 TopTools_IndexedMapOfShape edgeMap;
342 TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
343 for ( int i = 1; i <= edgeMap.Extent(); ++i )
345 const TopoDS_Edge& edge = TopoDS::Edge( edgeMap( i ));
347 // assure the base automatic length is stored in _TShapeToLength
349 GetLength( theMesh, edge );
351 // get current segment length
352 double L = SMESH_Algo::EdgeLength( edge );
355 SMESHDS_SubMesh * eSubMesh = aMeshDS->MeshElements( edge );
358 int nbSeg = eSubMesh->NbElements();
361 double segLen = L / nbSeg;
363 // get segment length from _TShapeToLength
364 map<const TopoDS_TShape*, double>::iterator tshape_length =
365 _TShapeToLength.find( getTShape( edge ));
366 if ( tshape_length == _TShapeToLength.end() )
368 double autoLen = tshape_length->second;
370 // segLen = autoLen / (theCoarseConst + theFineConst * _fineness) -->
371 _fineness += ( autoLen / segLen - theCoarseConst ) / theFineConst;
376 _fineness /= nbEdges;
380 else if (_fineness < 0.0)
386 //================================================================================
388 * \brief Initialize my parameter values by default parameters.
389 * \retval bool - true if parameter values have been successfully defined
391 //================================================================================
393 bool StdMeshers_AutomaticLength::SetParametersByDefaults(const TDefaults& /*theDflts*/,
394 const SMESH_Mesh* /*theMesh*/)
398 // assure the base automatic length is stored in _TShapeToLength
399 // GetLength( theMesh, elemLenght );
401 // // find maximal edge length
402 // double maxLen = 0;
403 // map<const TopoDS_TShape*, double>::iterator
404 // tshape_length = _TShapeToLength.begin(), slEnd = _TShapeToLength.end();
405 // for ( ; tshape_length != slEnd; ++tshape_length )
406 // if ( tshape_length->second > maxLen )
407 // maxLen = tshape_length->second;
409 // // automatic length for longest element
410 // double autoLen = GetLength( theMesh, maxLen );
412 // // elemLenght = autoLen / (theCoarseConst + theFineConst * _fineness) -->
413 // _fineness = ( autoLen / elemLenght - theCoarseConst ) / theFineConst;