1 // SMESH SMESH : implementaion of SMESH idl descriptions
3 // Copyright (C) 2003 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
24 // File : StdMeshers_AutomaticLength.cxx
25 // Author : Edward AGAPOV, OCC
29 #include "StdMeshers_AutomaticLength.hxx"
31 #include "SMESH_Mesh.hxx"
32 #include "SMESHDS_Mesh.hxx"
33 #include "SMESH_Algo.hxx"
34 #include "SMESHDS_SubMesh.hxx"
36 #include "utilities.h"
38 #include <TopTools_IndexedMapOfShape.hxx>
41 #include <TopoDS_Edge.hxx>
45 //=============================================================================
49 //=============================================================================
51 StdMeshers_AutomaticLength::StdMeshers_AutomaticLength(int hypId, int studyId, SMESH_Gen * gen)
52 :SMESH_Hypothesis(hypId, studyId, gen)
54 _name = "AutomaticLength";
55 _param_algo_dim = 1; // is used by SMESH_Regular_1D
61 //=============================================================================
65 //=============================================================================
67 StdMeshers_AutomaticLength::~StdMeshers_AutomaticLength()
71 //================================================================================
74 * \param theFineness - The Fineness value [0.0-1.0],
78 * Raise if theFineness is out of range
79 * The "Initial Number of Elements on the Shortest Edge" (S0)
80 * is divided by (0.5 + 4.5 x theFineness)
82 //================================================================================
84 const double theCoarseConst = 0.5;
85 const double theFineConst = 4.5;
87 void StdMeshers_AutomaticLength::SetFineness(double theFineness)
88 throw(SALOME_Exception)
90 if ( theFineness < 0.0 || theFineness > 1.0 )
91 throw SALOME_Exception(LOCALIZED("theFineness is out of range [0.0-1.0]"));
93 if ( _fineness != theFineness )
95 NotifySubMeshesHypothesisModification();
96 _fineness = theFineness;
100 //================================================================================
102 * \brief Return pointer to TopoDS_TShape
103 * \param theShape - The TopoDS_Shape
104 * \retval inline const TopoDS_TShape* - result
106 //================================================================================
108 inline const TopoDS_TShape* getTShape(const TopoDS_Shape& theShape)
110 return theShape.TShape().operator->();
112 //================================================================================
114 * \brief Compute segment length for all edges
115 * \param theMesh - The mesh
116 * \param theTShapeToLengthMap - The map of edge to segment length
118 //================================================================================
120 static void computeLengths( SMESHDS_Mesh* aMesh,
121 map<const TopoDS_TShape*, double> & theTShapeToLengthMap)
123 theTShapeToLengthMap.clear();
125 TopoDS_Shape aMainShape = aMesh->ShapeToMesh();
127 // Find length of longest and shortest edge
128 double Lmin = DBL_MAX, Lmax = -DBL_MAX;
129 TopTools_IndexedMapOfShape edgeMap;
130 TopExp::MapShapes( aMainShape, TopAbs_EDGE, edgeMap);
131 for ( int i = 1; i <= edgeMap.Extent(); ++i )
133 TopoDS_Edge edge = TopoDS::Edge( edgeMap(i) );
134 //if ( BRep_Tool::Degenerated( edge )) continue;
136 Standard_Real L = SMESH_Algo::EdgeLength( edge );
137 if ( L < DBL_MIN ) continue;
139 if ( L > Lmax ) Lmax = L;
140 if ( L < Lmin ) Lmin = L;
142 // remember i-th edge length
143 theTShapeToLengthMap.insert( make_pair( getTShape( edge ), L ));
148 // image attached to PAL10237
162 const int NbSegMin = 5, NbSegMax = 10; // on axis NbSeg
163 const double Lrat1 = 1., Lrat2 = 10.; // on axis Lmax/Lmin
165 double Lratio = Lmax/Lmin;
166 double NbSeg = NbSegMin;
167 if ( Lratio < Lrat2 )
168 NbSeg += ( Lrat2 - Lratio ) / ( Lrat2 - Lrat1 ) * ( NbSegMax - NbSegMin );
170 double S0 = Lmin / (int) NbSeg;
171 MESSAGE( "S0 = " << S0 << ", Lmin = " << Lmin << ", Nbseg = " << (int) NbSeg);
173 // Compute segments length for all edges
175 // S = S0 * f(L/Lmin) where f(x) = 1 + (2/Pi * 7 * atan(x/5) )
177 // S = S0 * ( 1 + 14/PI * atan( L / ( 5 * Lmin )))
179 const double a14divPI = 14. / PI, a5xLmin = 5 * Lmin;
180 map<const TopoDS_TShape*, double>::iterator tshape_length = theTShapeToLengthMap.begin();
181 for ( ; tshape_length != theTShapeToLengthMap.end(); ++tshape_length )
183 double & L = tshape_length->second;
184 L = S0 * ( 1. + a14divPI * atan( L / a5xLmin ));
188 //=============================================================================
192 //=============================================================================
194 double StdMeshers_AutomaticLength::GetLength(const SMESH_Mesh* theMesh,
195 const TopoDS_Shape& anEdge)
196 throw(SALOME_Exception)
198 if ( !theMesh ) throw SALOME_Exception(LOCALIZED("NULL Mesh"));
200 if ( anEdge.IsNull() || anEdge.ShapeType() != TopAbs_EDGE )
201 throw SALOME_Exception(LOCALIZED("Bad edge shape"));
203 if ( theMesh != _mesh )
205 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* > ( theMesh )->GetMeshDS();
206 computeLengths( aMeshDS, _TShapeToLength );
210 map<const TopoDS_TShape*, double>::iterator tshape_length =
211 _TShapeToLength.find( getTShape( anEdge ));
213 if ( tshape_length == _TShapeToLength.end() )
214 return 1; // it is a dgenerated edge
216 return tshape_length->second / (theCoarseConst + theFineConst * _fineness);
219 //=============================================================================
223 //=============================================================================
225 ostream & StdMeshers_AutomaticLength::SaveTo(ostream & save)
231 //=============================================================================
235 //=============================================================================
237 istream & StdMeshers_AutomaticLength::LoadFrom(istream & load)
239 if ( ! ( load >> _fineness ))
240 load.clear(ios::badbit | load.rdstate());
244 //=============================================================================
248 //=============================================================================
250 ostream & operator <<(ostream & save, StdMeshers_AutomaticLength & hyp)
252 return hyp.SaveTo( save );
255 //=============================================================================
259 //=============================================================================
261 istream & operator >>(istream & load, StdMeshers_AutomaticLength & hyp)
263 return hyp.LoadFrom( load );
266 //================================================================================
268 * \brief Initialize Fineness by the mesh built on the geometry
269 * \param theMesh - the built mesh
270 * \param theShape - the geometry of interest
271 * \retval bool - true if parameter values have been successfully defined
273 //================================================================================
275 bool StdMeshers_AutomaticLength::SetParametersByMesh(const SMESH_Mesh* theMesh,
276 const TopoDS_Shape& theShape)
278 if ( !theMesh || theShape.IsNull() )
283 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
286 TopTools_IndexedMapOfShape edgeMap;
287 TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
288 for ( int i = 1; i <= edgeMap.Extent(); ++i )
290 const TopoDS_Edge& edge = TopoDS::Edge( edgeMap( i ));
292 // assure the base automatic length is stored in _TShapeToLength
294 GetLength( theMesh, edge );
296 // get current segment length
297 double L = SMESH_Algo::EdgeLength( edge );
300 SMESHDS_SubMesh * eSubMesh = aMeshDS->MeshElements( edge );
303 int nbSeg = eSubMesh->NbElements();
306 double segLen = L / nbSeg;
308 // get segment length from _TShapeToLength
309 map<const TopoDS_TShape*, double>::iterator tshape_length =
310 _TShapeToLength.find( getTShape( edge ));
311 if ( tshape_length == _TShapeToLength.end() )
313 double autoLen = tshape_length->second;
315 // segLen = autoLen / (theCoarseConst + theFineConst * _fineness) -->
316 _fineness += ( autoLen / segLen - theCoarseConst ) / theFineConst;
321 _fineness /= nbEdges;
325 else if (_fineness < 0.0)