Salome HOME
correct previous integration (Porting to Python 2.6)
[modules/smesh.git] / src / StdMeshers / StdMeshers_MaxLength.cxx
1 //  SMESH SMESH : implementaion of SMESH idl descriptions
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : StdMeshers_MaxLength.cxx
23 //  Module : SMESH
24
25 #include "StdMeshers_MaxLength.hxx"
26
27 #include "SMESH_Mesh.hxx"
28 #include "SMESH_Algo.hxx"
29
30 #include "utilities.h"
31
32 #include <BRep_Tool.hxx>
33 #include <GCPnts_AbscissaPoint.hxx>
34 #include <GeomAdaptor_Curve.hxx>
35 #include <Geom_Curve.hxx>
36 #include <TopExp.hxx>
37 #include <TopLoc_Location.hxx>
38 #include <TopTools_IndexedMapOfShape.hxx>
39 #include <TopoDS.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <Precision.hxx>
42
43 using namespace std;
44
45 //=============================================================================
46 /*!
47  *  
48  */
49 //=============================================================================
50
51 StdMeshers_MaxLength::StdMeshers_MaxLength(int hypId, int studyId, SMESH_Gen * gen)
52   :SMESH_Hypothesis(hypId, studyId, gen)
53 {
54   _length = 1.;
55   _preestimated = 0.;
56   _preestimation = false;
57   _name = "MaxLength";
58   _param_algo_dim = 1; // is used by SMESH_Regular_1D
59 }
60
61 //=============================================================================
62 /*!
63  *  
64  */
65 //=============================================================================
66
67 StdMeshers_MaxLength::~StdMeshers_MaxLength()
68 {
69 }
70
71 //=============================================================================
72 /*!
73  *  
74  */
75 //=============================================================================
76
77 void StdMeshers_MaxLength::SetLength(double length) throw(SALOME_Exception)
78 {
79   if (length <= 0)
80     throw SALOME_Exception(LOCALIZED("length must be positive"));
81   if ( _length != length ) {
82     _length = length;
83     NotifySubMeshesHypothesisModification();
84   }
85 }
86
87 //=============================================================================
88 /*!
89  *  
90  */
91 //=============================================================================
92
93 double StdMeshers_MaxLength::GetLength() const
94 {
95   return ( _preestimation && _preestimated > 0. ) ? _preestimated : _length;
96 }
97
98 //================================================================================
99 /*!
100  * \brief Sets boolean parameter enabling/desabling usage of length computed
101  * basing on size of bounding box of shape to mesh
102  */
103 //================================================================================
104
105 void StdMeshers_MaxLength::SetUsePreestimatedLength(bool toUse)
106 {
107   if ( toUse != _preestimation )
108   {
109     _preestimation = toUse;
110     // this parameter is just to help the user
111     //NotifySubMeshesHypothesisModification();
112   }
113 }
114
115 //================================================================================
116 /*!
117  * \brief Store preestemated length
118  */
119 //================================================================================
120
121 void StdMeshers_MaxLength::SetPreestimatedLength(double length)
122 {
123   if ( length > 0 )
124     _preestimated = length;
125 }
126
127 //================================================================================
128 /*!
129  * \brief Returns value of boolean parameter enabling/desabling usage of length computed
130  * basing on size of bounding box of shape to mesh
131  */
132 //================================================================================
133
134 bool StdMeshers_MaxLength::GetUsePreestimatedLength() const
135 {
136   return _preestimation;
137 }
138
139 //=============================================================================
140 /*!
141  *  
142  */
143 //=============================================================================
144
145 ostream & StdMeshers_MaxLength::SaveTo(ostream & save)
146 {
147   save << _length << " " << _preestimated << " " << _preestimation;
148   return save;
149 }
150
151 //=============================================================================
152 /*!
153  *  
154  */
155 //=============================================================================
156
157 istream & StdMeshers_MaxLength::LoadFrom(istream & load)
158 {
159   bool isOK = true;
160   double a;
161
162   isOK = (load >> a);
163   if (isOK)
164     _length = a;
165   else
166     load.clear(ios::badbit | load.rdstate());
167
168   isOK = (load >> a);
169   if (isOK)
170     _preestimated = a;
171   else
172     load.clear(ios::badbit | load.rdstate());
173
174   bool pre;
175   isOK = (load >> pre);
176   if ( isOK )
177     _preestimation = pre;
178   else
179     load.clear(ios::badbit | load.rdstate());
180
181   return load;
182 }
183
184 //================================================================================
185 /*!
186  * \brief Initialize segment length by the mesh built on the geometry
187  * \param theMesh - the built mesh
188  * \param theShape - the geometry of interest
189  * \retval bool - true if parameter values have been successfully defined
190  */
191 //================================================================================
192
193 bool StdMeshers_MaxLength::SetParametersByMesh(const SMESH_Mesh*   theMesh,
194                                                const TopoDS_Shape& theShape)
195 {
196   if ( !theMesh || theShape.IsNull() )
197     return false;
198
199   _length = 0.;
200
201   Standard_Real UMin, UMax;
202   TopLoc_Location L;
203
204   int nbEdges = 0;
205   TopTools_IndexedMapOfShape edgeMap;
206   TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
207   for ( int iE = 1; iE <= edgeMap.Extent(); ++iE )
208   {
209     const TopoDS_Edge& edge = TopoDS::Edge( edgeMap( iE ));
210     Handle(Geom_Curve) C = BRep_Tool::Curve( edge, L, UMin, UMax );
211     GeomAdaptor_Curve AdaptCurve(C);
212
213     vector< double > params;
214     SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
215     if ( SMESH_Algo::GetNodeParamOnEdge( aMeshDS, edge, params ))
216     {
217       for ( int i = 1; i < params.size(); ++i )
218         _length += GCPnts_AbscissaPoint::Length( AdaptCurve, params[ i-1 ], params[ i ]);
219       nbEdges += params.size() - 1;
220     }
221   }
222   if ( nbEdges )
223     _length /= nbEdges;
224
225   return nbEdges;
226 }
227 //================================================================================
228 /*!
229  * \brief Initialize my parameter values by default parameters.
230  *  \retval bool - true if parameter values have been successfully defined
231  */
232 //================================================================================
233
234 bool StdMeshers_MaxLength::SetParametersByDefaults(const TDefaults&  dflts,
235                                                    const SMESH_Mesh* /*theMesh*/)
236 {
237   //_preestimation = ( dflts._elemLength > 0.);
238   if ( dflts._elemLength > 0. )
239     _preestimated = dflts._elemLength;
240   return ( _length = dflts._elemLength );
241 }
242