1 // Copyright (C) 2007-2010 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. / PI;
122 inline double segLength(double S0, double edgeLen, double minLen )
125 // S = S0 * f(L/Lmin) where f(x) = 1 + (2/Pi * 7 * atan(x/5) )
127 // S = S0 * ( 1 + 14/PI * atan( L / ( 5 * Lmin )))
128 return S0 * ( 1. + a14divPI * atan( edgeLen / ( 5 * minLen )));
131 //================================================================================
133 * \brief Compute segment length for all edges
134 * \param theMesh - The mesh
135 * \param theTShapeToLengthMap - The map of edge to segment length
137 //================================================================================
139 void computeLengths( SMESHDS_Mesh* aMesh,
140 map<const TopoDS_TShape*, double> & theTShapeToLengthMap,
144 theTShapeToLengthMap.clear();
146 TopoDS_Shape aMainShape = aMesh->ShapeToMesh();
148 // Find length of longest and shortest edge
149 double Lmin = DBL_MAX, Lmax = -DBL_MAX;
150 TopTools_IndexedMapOfShape edgeMap;
151 TopExp::MapShapes( aMainShape, TopAbs_EDGE, edgeMap);
152 for ( int i = 1; i <= edgeMap.Extent(); ++i )
154 TopoDS_Edge edge = TopoDS::Edge( edgeMap(i) );
155 //if ( BRep_Tool::Degenerated( edge )) continue;
157 Standard_Real L = SMESH_Algo::EdgeLength( edge );
158 if ( L < DBL_MIN ) continue;
160 if ( L > Lmax ) Lmax = L;
161 if ( L < Lmin ) Lmin = L;
163 // remember i-th edge length
164 theTShapeToLengthMap.insert( make_pair( getTShape( edge ), L ));
169 // image attached to PAL10237
183 const int NbSegMin = 5, NbSegMax = 10; // on axis NbSeg
184 const double Lrat1 = 1., Lrat2 = 10.; // on axis Lmax/Lmin
186 double Lratio = Lmax/Lmin;
187 double NbSeg = NbSegMin;
188 if ( Lratio < Lrat2 )
189 NbSeg += ( Lrat2 - Lratio ) / ( Lrat2 - Lrat1 ) * ( NbSegMax - NbSegMin );
191 double S0 = Lmin / (int) NbSeg;
192 MESSAGE( "S0 = " << S0 << ", Lmin = " << Lmin << ", Nbseg = " << (int) NbSeg);
194 // Compute segments length for all edges
195 map<const TopoDS_TShape*, double>::iterator tshape_length = theTShapeToLengthMap.begin();
196 for ( ; tshape_length != theTShapeToLengthMap.end(); ++tshape_length )
198 double & L = tshape_length->second;
199 L = segLength( S0, L, Lmin );
206 //=============================================================================
208 * \brief Computes segment length for an edge of given length
210 //=============================================================================
212 double StdMeshers_AutomaticLength::GetLength(const SMESH_Mesh* theMesh,
213 const double theEdgeLength)
214 throw(SALOME_Exception)
216 if ( !theMesh ) throw SALOME_Exception(LOCALIZED("NULL Mesh"));
218 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* > ( theMesh )->GetMeshDS();
219 if ( theMesh != _mesh )
221 computeLengths( aMeshDS, _TShapeToLength, _S0, _minLen );
224 double L = segLength( _S0, theEdgeLength, _minLen );
225 return L / (theCoarseConst + theFineConst * _fineness);
228 //=============================================================================
232 //=============================================================================
234 double StdMeshers_AutomaticLength::GetLength(const SMESH_Mesh* theMesh,
235 const TopoDS_Shape& anEdge)
236 throw(SALOME_Exception)
238 if ( !theMesh ) throw SALOME_Exception(LOCALIZED("NULL Mesh"));
240 if ( anEdge.IsNull() || anEdge.ShapeType() != TopAbs_EDGE )
241 throw SALOME_Exception(LOCALIZED("Bad edge shape"));
243 if ( theMesh != _mesh )
245 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* > ( theMesh )->GetMeshDS();
246 computeLengths( aMeshDS, _TShapeToLength, _S0, _minLen );
250 map<const TopoDS_TShape*, double>::iterator tshape_length =
251 _TShapeToLength.find( getTShape( anEdge ));
253 if ( tshape_length == _TShapeToLength.end() )
254 return 1; // it is a dgenerated edge
256 return tshape_length->second / (theCoarseConst + theFineConst * _fineness);
259 //=============================================================================
263 //=============================================================================
265 ostream & StdMeshers_AutomaticLength::SaveTo(ostream & save)
271 //=============================================================================
275 //=============================================================================
277 istream & StdMeshers_AutomaticLength::LoadFrom(istream & load)
279 if ( ! ( load >> _fineness ))
280 load.clear(ios::badbit | load.rdstate());
284 //=============================================================================
288 //=============================================================================
290 ostream & operator <<(ostream & save, StdMeshers_AutomaticLength & hyp)
292 return hyp.SaveTo( save );
295 //=============================================================================
299 //=============================================================================
301 istream & operator >>(istream & load, StdMeshers_AutomaticLength & hyp)
303 return hyp.LoadFrom( load );
306 //================================================================================
308 * \brief Initialize Fineness by the mesh built on the geometry
309 * \param theMesh - the built mesh
310 * \param theShape - the geometry of interest
311 * \retval bool - true if parameter values have been successfully defined
313 //================================================================================
315 bool StdMeshers_AutomaticLength::SetParametersByMesh(const SMESH_Mesh* theMesh,
316 const TopoDS_Shape& theShape)
318 if ( !theMesh || theShape.IsNull() )
323 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
326 TopTools_IndexedMapOfShape edgeMap;
327 TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
328 for ( int i = 1; i <= edgeMap.Extent(); ++i )
330 const TopoDS_Edge& edge = TopoDS::Edge( edgeMap( i ));
332 // assure the base automatic length is stored in _TShapeToLength
334 GetLength( theMesh, edge );
336 // get current segment length
337 double L = SMESH_Algo::EdgeLength( edge );
340 SMESHDS_SubMesh * eSubMesh = aMeshDS->MeshElements( edge );
343 int nbSeg = eSubMesh->NbElements();
346 double segLen = L / nbSeg;
348 // get segment length from _TShapeToLength
349 map<const TopoDS_TShape*, double>::iterator tshape_length =
350 _TShapeToLength.find( getTShape( edge ));
351 if ( tshape_length == _TShapeToLength.end() )
353 double autoLen = tshape_length->second;
355 // segLen = autoLen / (theCoarseConst + theFineConst * _fineness) -->
356 _fineness += ( autoLen / segLen - theCoarseConst ) / theFineConst;
361 _fineness /= nbEdges;
365 else if (_fineness < 0.0)
371 //================================================================================
373 * \brief Initialize my parameter values by default parameters.
374 * \retval bool - true if parameter values have been successfully defined
376 //================================================================================
378 bool StdMeshers_AutomaticLength::SetParametersByDefaults(const TDefaults& /*theDflts*/,
379 const SMESH_Mesh* /*theMesh*/)
383 // assure the base automatic length is stored in _TShapeToLength
384 // GetLength( theMesh, elemLenght );
386 // // find maximal edge length
387 // double maxLen = 0;
388 // map<const TopoDS_TShape*, double>::iterator
389 // tshape_length = _TShapeToLength.begin(), slEnd = _TShapeToLength.end();
390 // for ( ; tshape_length != slEnd; ++tshape_length )
391 // if ( tshape_length->second > maxLen )
392 // maxLen = tshape_length->second;
394 // // automatic length for longest element
395 // double autoLen = GetLength( theMesh, maxLen );
397 // // elemLenght = autoLen / (theCoarseConst + theFineConst * _fineness) -->
398 // _fineness = ( autoLen / elemLenght - theCoarseConst ) / theFineConst;