Salome HOME
#18963 Minimize compiler warnings
[modules/smesh.git] / src / StdMeshers / StdMeshers_StartEndLength.cxx
1 // Copyright (C) 2007-2020  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_StartEndLength : implementation of SMESH idl descriptions
24 //  File   : StdMeshers_StartEndLength.cxx
25 //  Module : SMESH
26 //
27 #include "StdMeshers_StartEndLength.hxx"
28
29 #include "SMESH_Algo.hxx"
30 #include "SMESH_Mesh.hxx"
31
32 #include <BRep_Tool.hxx>
33 #include <GCPnts_AbscissaPoint.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
42 using namespace std;
43
44 //=============================================================================
45 /*!
46  *  
47  */
48 //=============================================================================
49
50 StdMeshers_StartEndLength::StdMeshers_StartEndLength( int hypId, SMESH_Gen* gen )
51   :StdMeshers_Reversible1D(hypId, gen)
52 {
53   _begLength = 1.;
54   _endLength = 10.;
55   _name = "StartEndLength";
56   _param_algo_dim = 1; // is used by SMESH_Regular_1D
57 }
58
59 //=============================================================================
60 /*!
61  *  
62  */
63 //=============================================================================
64
65 StdMeshers_StartEndLength::~StdMeshers_StartEndLength()
66 {
67 }
68
69 //=============================================================================
70 /*!
71  *  
72  */
73 //=============================================================================
74
75 void StdMeshers_StartEndLength::SetLength(double length, bool isStartLength)
76 {
77   if ( (isStartLength ? _begLength : _endLength) != length ) {
78     if (length <= 0)
79       throw SALOME_Exception(LOCALIZED("length must be positive"));
80     if ( isStartLength )
81       _begLength = length;
82     else
83       _endLength = length;
84
85     NotifySubMeshesHypothesisModification();
86   }
87 }
88
89 //=============================================================================
90 /*!
91  *  
92  */
93 //=============================================================================
94
95 double StdMeshers_StartEndLength::GetLength(bool isStartLength) const
96 {
97   return isStartLength ? _begLength : _endLength;
98 }
99
100 //=============================================================================
101 /*!
102  *  
103  */
104 //=============================================================================
105
106 ostream & StdMeshers_StartEndLength::SaveTo(ostream & save)
107 {
108   int listSize = _edgeIDs.size();
109   save << _begLength << " " << _endLength << " " << listSize;
110
111   if ( listSize > 0 ) {
112     for ( int i = 0; i < listSize; i++) {
113       save << " " << _edgeIDs[i];
114     }
115     save << " " << _objEntry;
116   }
117
118   return save;
119 }
120
121 //=============================================================================
122 /*!
123  *  
124  */
125 //=============================================================================
126
127 istream & StdMeshers_StartEndLength::LoadFrom(istream & load)
128 {
129   bool isOK = true;
130   int intVal;
131   isOK = static_cast<bool>(load >> _begLength);
132   if (!isOK)
133     load.clear(ios::badbit | load.rdstate());
134   isOK = static_cast<bool>(load >> _endLength);
135
136   if (!isOK)
137     load.clear(ios::badbit | load.rdstate());
138
139   isOK = static_cast<bool>(load >> intVal);
140   if (isOK && intVal > 0) {
141     _edgeIDs.reserve( intVal );
142     for ( size_t i = 0; i < _edgeIDs.capacity() && isOK; i++) {
143       isOK = static_cast<bool>(load >> intVal);
144       if ( isOK ) _edgeIDs.push_back( intVal );
145     }
146     isOK = static_cast<bool>(load >> _objEntry);
147   }
148
149   return load;
150 }
151
152 //================================================================================
153 /*!
154  * \brief Initialize start and end length by the mesh built on the geometry
155  * \param theMesh - the built mesh
156  * \param theShape - the geometry of interest
157  * \retval bool - true if parameter values have been successfully defined
158  */
159 //================================================================================
160
161 bool StdMeshers_StartEndLength::SetParametersByMesh(const SMESH_Mesh*   theMesh,
162                                                     const TopoDS_Shape& theShape)
163 {
164   if ( !theMesh || theShape.IsNull() )
165     return false;
166
167   _begLength = _endLength = 0.;
168
169   Standard_Real UMin, UMax;
170   TopLoc_Location L;
171
172   int nbEdges = 0;
173   TopTools_IndexedMapOfShape edgeMap;
174   TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
175   for ( int i = 1; i <= edgeMap.Extent(); ++i )
176   {
177     const TopoDS_Edge& edge = TopoDS::Edge( edgeMap( i ));
178     Handle(Geom_Curve) C = BRep_Tool::Curve(edge, L, UMin, UMax);
179     GeomAdaptor_Curve AdaptCurve(C, UMin, UMax);
180
181     vector< double > params;
182     SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
183     if ( SMESH_Algo::GetNodeParamOnEdge( aMeshDS, edge, params ))
184     {
185       nbEdges++;
186       _begLength += GCPnts_AbscissaPoint::Length( AdaptCurve, params[0], params[1]);
187       int nb = params.size();
188       _endLength += GCPnts_AbscissaPoint::Length( AdaptCurve, params[nb-2], params[nb-1]);
189     }
190   }
191   if ( nbEdges ) {
192     _begLength /= nbEdges;
193     _endLength /= nbEdges;
194   }
195   return nbEdges;
196 }
197
198 //================================================================================
199 /*!
200  * \brief Initialize my parameter values by default parameters.
201  *  \retval bool - true if parameter values have been successfully defined
202  */
203 //================================================================================
204
205 bool StdMeshers_StartEndLength::SetParametersByDefaults(const TDefaults&  dflts,
206                                                         const SMESH_Mesh* /*theMesh*/)
207 {
208   return (_begLength = _endLength = dflts._elemLength );
209 }
210