Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/smesh.git] / src / StdMeshers / StdMeshers_Deflection1D.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 //  SMESH StdMeshers_Deflection1D : implementaion of SMESH idl descriptions
23 //  File   : StdMeshers_Deflection1D.cxx
24 //  Module : SMESH
25 //
26 #include "StdMeshers_Deflection1D.hxx"
27 #include "utilities.h"
28
29 #include "SMESH_Mesh.hxx"
30 #include "SMESH_Algo.hxx"
31
32 #include <BRep_Tool.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 <gp_Lin.hxx>
41 #include <gp_Pnt.hxx>
42
43 using namespace std;
44
45 //=============================================================================
46 /*!
47  *  
48  */
49 //=============================================================================
50
51 StdMeshers_Deflection1D::StdMeshers_Deflection1D(int         hypId,
52                                                  int         studyId,
53                                                  SMESH_Gen * gen)
54      :SMESH_Hypothesis(hypId, studyId, gen)
55 {
56   _value = 1.;
57   _name = "Deflection1D";
58   _param_algo_dim = 1; // is used by SMESH_Regular_1D
59 }
60
61 //=============================================================================
62 /*!
63  *  
64  */
65 //=============================================================================
66
67 StdMeshers_Deflection1D::~StdMeshers_Deflection1D()
68 {
69 }
70
71 //=============================================================================
72 /*!
73  *  
74  */
75 //=============================================================================
76
77 void StdMeshers_Deflection1D::SetDeflection(double value)
78      throw(SALOME_Exception)
79 {
80   if (_value != value) {
81     if (value <= 0.)
82       throw SALOME_Exception(LOCALIZED("Value must be positive"));
83
84     NotifySubMeshesHypothesisModification();
85
86     _value = value;
87   }
88 }
89
90 //=============================================================================
91 /*!
92  *  
93  */
94 //=============================================================================
95
96 double StdMeshers_Deflection1D::GetDeflection() const
97 {
98   return _value;
99 }
100
101 //=============================================================================
102 /*!
103  *  
104  */
105 //=============================================================================
106
107 ostream & StdMeshers_Deflection1D::SaveTo(ostream & save)
108 {
109   save << _value;
110   return save;
111 }
112
113 //=============================================================================
114 /*!
115  *  
116  */
117 //=============================================================================
118
119 istream & StdMeshers_Deflection1D::LoadFrom(istream & load)
120 {
121   bool isOK = (load >> _value);
122   if (!isOK)
123     load.clear(ios::badbit | load.rdstate());
124   return load;
125 }
126
127 //=============================================================================
128 /*!
129  *  
130  */
131 //=============================================================================
132
133 ostream & operator <<(ostream & save, StdMeshers_Deflection1D & hyp)
134 {
135   return hyp.SaveTo( save );
136 }
137
138 //=============================================================================
139 /*!
140  *  
141  */
142 //=============================================================================
143
144 istream & operator >>(istream & load, StdMeshers_Deflection1D & hyp)
145 {
146   return hyp.LoadFrom( load );
147 }
148 //================================================================================
149 /*!
150  * \brief Evaluate curve deflection between two points
151   * \param theCurve - the curve
152   * \param theU1 - the parameter of the first point
153   * \param theU2 - the parameter of the second point
154   * \retval double - deflection value
155  */
156 //================================================================================
157
158 static double deflection(const GeomAdaptor_Curve & theCurve,
159                          double                    theU1,
160                          double                    theU2)
161 {
162   if ( theCurve.GetType() == GeomAbs_Line )
163     return 0;
164   // line between theU1 and theU2
165   gp_Pnt p1 = theCurve.Value( theU1 ), p2 = theCurve.Value( theU2 );
166   gp_Lin segment( p1, gp_Vec( p1, p2 ));
167
168   // evaluate square distance of theCurve from the segment
169   Standard_Real dist2 = 0;
170   const int nbPnt = 7;
171   const double step = ( theU2 - theU1 ) / nbPnt;
172   while (( theU1 += step ) < theU2 )
173     dist2 = Max( dist2, segment.SquareDistance( theCurve.Value( theU1 )));
174
175   return sqrt( dist2 );
176 }
177
178 //================================================================================
179 /*!
180  * \brief Initialize deflection value by the mesh built on the geometry
181  * \param theMesh - the built mesh
182  * \param theShape - the geometry of interest
183  * \retval bool - true if parameter values have been successfully defined
184  */
185 //================================================================================
186
187 bool StdMeshers_Deflection1D::SetParametersByMesh(const SMESH_Mesh*   theMesh,
188                                                   const TopoDS_Shape& theShape)
189 {
190   if ( !theMesh || theShape.IsNull() )
191     return false;
192
193   _value = 0.;
194
195   Standard_Real UMin, UMax;
196   TopLoc_Location L;
197
198   int nbEdges = 0;
199   TopTools_IndexedMapOfShape edgeMap;
200   TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
201
202   for ( int iE = 1; iE <= edgeMap.Extent(); ++iE )
203   {
204     const TopoDS_Edge& edge = TopoDS::Edge( edgeMap( iE ));
205     Handle(Geom_Curve) C = BRep_Tool::Curve( edge, L, UMin, UMax );
206     GeomAdaptor_Curve AdaptCurve(C);
207     if ( AdaptCurve.GetType() != GeomAbs_Line )
208     {
209       vector< double > params;
210       SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
211       if ( SMESH_Algo::GetNodeParamOnEdge( aMeshDS, edge, params ))
212       {
213         nbEdges++;
214         for ( int i = 1; i < params.size(); ++i )
215           _value = Max( _value, deflection( AdaptCurve, params[ i-1 ], params[ i ]));
216       }
217     }
218     else
219       nbEdges++;
220   }
221   return nbEdges;
222 }
223
224 //================================================================================
225 /*!
226  * \brief Initialize my parameter values by default parameters.
227  *  \retval bool - true if parameter values have been successfully defined
228  */
229 //================================================================================
230
231 bool StdMeshers_Deflection1D::SetParametersByDefaults(const TDefaults&  /*dflts*/,
232                                                       const SMESH_Mesh* /*theMesh*/)
233 {
234   return false;
235 }