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
59 //=============================================================================
63 //=============================================================================
65 StdMeshers_AutomaticLength::~StdMeshers_AutomaticLength()
69 //================================================================================
71 * \brief Return pointer to TopoDS_TShape
72 * \param theShape - The TopoDS_Shape
73 * \retval inline const TopoDS_TShape* - result
75 //================================================================================
77 inline const TopoDS_TShape* getTShape(const TopoDS_Shape& theShape)
79 return theShape.TShape().operator->();
81 //================================================================================
83 * \brief Compute segment length for all edges
84 * \param theMesh - The mesh
85 * \param theTShapeToLengthMap - The map of edge to segment length
87 //================================================================================
89 static void computeLengths( const SMESH_Mesh* theMesh,
90 map<const TopoDS_TShape*, double> & theTShapeToLengthMap)
92 theTShapeToLengthMap.clear();
94 SMESHDS_Mesh* aMesh = const_cast< SMESH_Mesh* > ( theMesh )->GetMeshDS();
95 TopoDS_Shape aMainShape = aMesh->ShapeToMesh();
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 )
103 TopoDS_Edge edge = TopoDS::Edge( edgeMap(i) );
104 //if ( BRep_Tool::Degenerated( edge )) continue;
106 Standard_Real L = SMESH_Algo::EdgeLength( edge );
107 if ( L < DBL_MIN ) continue;
109 if ( L > Lmax ) Lmax = L;
110 if ( L < Lmin ) Lmin = L;
112 // remember i-th edge length
113 theTShapeToLengthMap.insert( make_pair( getTShape( edge ), L ));
118 // image attached to PAL10237
132 const int NbSegMin = 5, NbSegMax = 10; // on axis NbSeg
133 const double Lrat1 = 1., Lrat2 = 10.; // on axis Lmax/Lmin
135 double Lratio = Lmax/Lmin;
136 double NbSeg = NbSegMin;
137 if ( Lratio < Lrat2 )
138 NbSeg += ( Lrat2 - Lratio ) / ( Lrat2 - Lrat1 ) * ( NbSegMax - NbSegMin );
140 double S0 = Lmin / (int) NbSeg;
141 MESSAGE( "S0 = " << S0 << ", Lmin = " << Lmin << ", Nbseg = " << (int) NbSeg);
143 // Compute segments length for all edges
145 // S = S0 * f(L/Lmin) where f(x) = 1 + (2/Pi * 7 * atan(x/5) )
147 // S = S0 * ( 1 + 14/PI * atan( L / ( 5 * Lmin )))
149 const double a14divPI = 14. / PI, a5xLmin = 5 * Lmin;
150 map<const TopoDS_TShape*, double>::iterator tshape_length = theTShapeToLengthMap.begin();
151 for ( ; tshape_length != theTShapeToLengthMap.end(); ++tshape_length )
153 double & L = tshape_length->second;
154 L = S0 * ( 1. + a14divPI * atan( L / a5xLmin ));
158 //=============================================================================
162 //=============================================================================
164 double StdMeshers_AutomaticLength::GetLength(const SMESH_Mesh* theMesh,
165 const TopoDS_Shape& anEdge)
166 throw(SALOME_Exception)
168 if ( !theMesh ) throw SALOME_Exception(LOCALIZED("NULL Mesh"));
170 if ( anEdge.IsNull() || anEdge.ShapeType() != TopAbs_EDGE )
171 throw SALOME_Exception(LOCALIZED("Bad edge shape"));
173 if ( theMesh != _mesh ) {
174 computeLengths( theMesh, _TShapeToLength );
178 map<const TopoDS_TShape*, double>::iterator tshape_length =
179 _TShapeToLength.find( getTShape( anEdge ));
181 if ( tshape_length == _TShapeToLength.end() )
182 return 1; // it is a dgenerated edge
184 return tshape_length->second;
187 //=============================================================================
191 //=============================================================================
193 ostream & StdMeshers_AutomaticLength::SaveTo(ostream & save)
198 //=============================================================================
202 //=============================================================================
204 istream & StdMeshers_AutomaticLength::LoadFrom(istream & load)
209 //=============================================================================
213 //=============================================================================
215 ostream & operator <<(ostream & save, StdMeshers_AutomaticLength & hyp)
217 return hyp.SaveTo( save );
220 //=============================================================================
224 //=============================================================================
226 istream & operator >>(istream & load, StdMeshers_AutomaticLength & hyp)
228 return hyp.LoadFrom( load );