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