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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
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"
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,
51 SMESH_Gen * gen):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 void StdMeshers_AutomaticLength::SetFineness(double theFineness)
84 throw(SALOME_Exception)
86 if ( theFineness < 0.0 || theFineness > 1.0 )
87 throw SALOME_Exception(LOCALIZED("theFineness is out of range [0.0-1.0]"));
89 if ( _fineness != theFineness )
91 NotifySubMeshesHypothesisModification();
92 _fineness = theFineness;
96 //================================================================================
98 * \brief Return pointer to TopoDS_TShape
99 * \param theShape - The TopoDS_Shape
100 * \retval inline const TopoDS_TShape* - result
102 //================================================================================
104 inline const TopoDS_TShape* getTShape(const TopoDS_Shape& theShape)
106 return theShape.TShape().operator->();
108 //================================================================================
110 * \brief Compute segment length for all edges
111 * \param theMesh - The mesh
112 * \param theTShapeToLengthMap - The map of edge to segment length
114 //================================================================================
116 static void computeLengths( const SMESH_Mesh* theMesh,
117 map<const TopoDS_TShape*, double> & theTShapeToLengthMap)
119 theTShapeToLengthMap.clear();
121 SMESHDS_Mesh* aMesh = const_cast< SMESH_Mesh* > ( theMesh )->GetMeshDS();
122 TopoDS_Shape aMainShape = aMesh->ShapeToMesh();
124 // Find length of longest and shortest edge
125 double Lmin = DBL_MAX, Lmax = -DBL_MAX;
126 TopTools_IndexedMapOfShape edgeMap;
127 TopExp::MapShapes( aMainShape, TopAbs_EDGE, edgeMap);
128 for ( int i = 1; i <= edgeMap.Extent(); ++i )
130 TopoDS_Edge edge = TopoDS::Edge( edgeMap(i) );
131 //if ( BRep_Tool::Degenerated( edge )) continue;
133 Standard_Real L = SMESH_Algo::EdgeLength( edge );
134 if ( L < DBL_MIN ) continue;
136 if ( L > Lmax ) Lmax = L;
137 if ( L < Lmin ) Lmin = L;
139 // remember i-th edge length
140 theTShapeToLengthMap.insert( make_pair( getTShape( edge ), L ));
145 // image attached to PAL10237
159 const int NbSegMin = 5, NbSegMax = 10; // on axis NbSeg
160 const double Lrat1 = 1., Lrat2 = 10.; // on axis Lmax/Lmin
162 double Lratio = Lmax/Lmin;
163 double NbSeg = NbSegMin;
164 if ( Lratio < Lrat2 )
165 NbSeg += ( Lrat2 - Lratio ) / ( Lrat2 - Lrat1 ) * ( NbSegMax - NbSegMin );
167 double S0 = Lmin / (int) NbSeg;
168 MESSAGE( "S0 = " << S0 << ", Lmin = " << Lmin << ", Nbseg = " << (int) NbSeg);
170 // Compute segments length for all edges
172 // S = S0 * f(L/Lmin) where f(x) = 1 + (2/Pi * 7 * atan(x/5) )
174 // S = S0 * ( 1 + 14/PI * atan( L / ( 5 * Lmin )))
176 const double a14divPI = 14. / PI, a5xLmin = 5 * Lmin;
177 map<const TopoDS_TShape*, double>::iterator tshape_length = theTShapeToLengthMap.begin();
178 for ( ; tshape_length != theTShapeToLengthMap.end(); ++tshape_length )
180 double & L = tshape_length->second;
181 L = S0 * ( 1. + a14divPI * atan( L / a5xLmin ));
185 //=============================================================================
189 //=============================================================================
191 double StdMeshers_AutomaticLength::GetLength(const SMESH_Mesh* theMesh,
192 const TopoDS_Shape& anEdge)
193 throw(SALOME_Exception)
195 if ( !theMesh ) throw SALOME_Exception(LOCALIZED("NULL Mesh"));
197 if ( anEdge.IsNull() || anEdge.ShapeType() != TopAbs_EDGE )
198 throw SALOME_Exception(LOCALIZED("Bad edge shape"));
200 if ( theMesh != _mesh ) {
201 computeLengths( theMesh, _TShapeToLength );
205 map<const TopoDS_TShape*, double>::iterator tshape_length =
206 _TShapeToLength.find( getTShape( anEdge ));
208 if ( tshape_length == _TShapeToLength.end() )
209 return 1; // it is a dgenerated edge
211 return tshape_length->second / (0.5 + 4.5 * _fineness);
214 //=============================================================================
218 //=============================================================================
220 ostream & StdMeshers_AutomaticLength::SaveTo(ostream & save)
226 //=============================================================================
230 //=============================================================================
232 istream & StdMeshers_AutomaticLength::LoadFrom(istream & load)
234 if ( ! ( load >> _fineness ))
235 load.clear(ios::badbit | load.rdstate());
239 //=============================================================================
243 //=============================================================================
245 ostream & operator <<(ostream & save, StdMeshers_AutomaticLength & hyp)
247 return hyp.SaveTo( save );
250 //=============================================================================
254 //=============================================================================
256 istream & operator >>(istream & load, StdMeshers_AutomaticLength & hyp)
258 return hyp.LoadFrom( load );