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