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