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