Salome HOME
Merge remote branch 'origin/V8_5_asterstudy'
[modules/smesh.git] / src / StdMeshers / StdMeshers_Geometric1D.cxx
1 // Copyright (C) 2007-2016  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 SMESH : implementation of SMESH idl descriptions
24 //  File   : StdMeshers_Geometric1D.cxx
25 //  Module : SMESH
26 //
27 #include "StdMeshers_Geometric1D.hxx"
28
29 #include "SMESH_Mesh.hxx"
30
31 #include <BRepAdaptor_Curve.hxx>
32 #include <GCPnts_AbscissaPoint.hxx>
33 #include <SMESH_Algo.hxx>
34 #include <TopExp.hxx>
35 #include <TopTools_IndexedMapOfShape.hxx>
36 #include <TopoDS.hxx>
37 #include <TopoDS_Edge.hxx>
38
39 //=============================================================================
40 /*!
41  * Constructor
42  */
43 //=============================================================================
44
45 StdMeshers_Geometric1D::StdMeshers_Geometric1D(int hypId, SMESH_Gen * gen)
46   :StdMeshers_Reversible1D(hypId, gen)
47 {
48   _begLength = 1.;
49   _ratio = 1.;
50   _name = "GeometricProgression";
51 }
52
53 //=============================================================================
54 /*!
55  * Sets length of the first segment
56  */
57 //=============================================================================
58
59 void StdMeshers_Geometric1D::SetStartLength(double length)
60   throw(SALOME_Exception)
61 {
62   if ( _begLength != length )
63   {
64     if (length <= 0)
65       throw SALOME_Exception(LOCALIZED("length must be positive"));
66     _begLength = length;
67     NotifySubMeshesHypothesisModification();
68   }
69 }
70
71 //=============================================================================
72 /*!
73  * Sets value of Common Ratio
74  */
75 //=============================================================================
76
77 void StdMeshers_Geometric1D::SetCommonRatio(double factor)
78   throw(SALOME_Exception)
79 {
80   if ( _ratio != factor )
81   {
82     if (factor == 0)
83       throw SALOME_Exception(LOCALIZED("Zero factor is not allowed"));
84     _ratio = factor;
85     NotifySubMeshesHypothesisModification();
86   }
87 }
88
89 //=============================================================================
90 /*!
91  * Returns length of the first segment 
92  */
93 //=============================================================================
94
95 double StdMeshers_Geometric1D::GetStartLength() const
96 {
97   return _begLength;
98 }
99
100 //=============================================================================
101 /*!
102  * Returns value of Common Ratio
103  */
104 //=============================================================================
105
106 double StdMeshers_Geometric1D::GetCommonRatio() const
107 {
108   return _ratio;
109 }
110
111 //=============================================================================
112 /*!
113  *  
114  */
115 //=============================================================================
116
117 ostream & StdMeshers_Geometric1D::SaveTo(ostream & save)
118 {
119   save << _begLength << " " << _ratio << " ";
120
121   StdMeshers_Reversible1D::SaveTo( save );
122
123   return save;
124 }
125
126 //=============================================================================
127 /*!
128  *  
129  */
130 //=============================================================================
131
132 istream & StdMeshers_Geometric1D::LoadFrom(istream & load)
133 {
134   bool isOK = true;
135   isOK = static_cast<bool>(load >> _begLength);
136   isOK = static_cast<bool>(load >> _ratio);
137
138   if (isOK)
139     StdMeshers_Reversible1D::LoadFrom( load );
140
141   return load;
142 }
143
144 //================================================================================
145 /*!
146  * \brief Initialize start and end length by the mesh built on the geometry
147  * \param theMesh - the built mesh
148  * \param theShape - the geometry of interest
149  * \retval bool - true if parameter values have been successfully defined
150  */
151 //================================================================================
152
153 bool StdMeshers_Geometric1D::SetParametersByMesh(const SMESH_Mesh*   theMesh,
154                                                  const TopoDS_Shape& theShape)
155 {
156   if ( !theMesh || theShape.IsNull() )
157     return false;
158
159   _begLength = _ratio = 0.;
160
161   int nbEdges = 0;
162   TopTools_IndexedMapOfShape edgeMap;
163   TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
164   for ( int i = 1; i <= edgeMap.Extent(); ++i )
165   {
166     const TopoDS_Edge& edge = TopoDS::Edge( edgeMap( i ));
167     BRepAdaptor_Curve C( edge );
168
169     std::vector< double > params;
170     if ( SMESH_Algo::GetNodeParamOnEdge( theMesh->GetMeshDS(), edge, params ))
171     {
172       nbEdges++;
173       double l1 = GCPnts_AbscissaPoint::Length( C, params[0], params[1] );
174       _begLength += l1;
175       if ( params.size() > 2 && l1 > 1e-100 )
176         _ratio += GCPnts_AbscissaPoint::Length( C, params[1], params[2]) / l1;
177       else
178         _ratio += 1;
179     }
180   }
181   if ( nbEdges ) {
182     _begLength /= nbEdges;
183     _ratio     /= nbEdges;
184   }
185   else {
186     _begLength = 1;
187     _ratio     = 1;
188   }
189   return nbEdges;
190 }
191
192 //================================================================================
193 /*!
194  * \brief Initialize my parameter values by default parameters.
195  *  \retval bool - true if parameter values have been successfully defined
196  */
197 //================================================================================
198
199 bool StdMeshers_Geometric1D::SetParametersByDefaults(const TDefaults&  dflts,
200                                                      const SMESH_Mesh* /*mesh*/)
201 {
202   return ( _begLength = dflts._elemLength );
203 }
204