Salome HOME
a2bf0f7e116cf39607ee72234659af1c60773e4d
[modules/smesh.git] / src / StdMeshers / StdMeshers_StartEndLength.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 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,
51                                                      SMESH_Gen * gen)
52      :SMESH_Hypothesis(hypId, gen)
53 {
54   _begLength = 1.;
55   _endLength = 10.;
56   _name = "StartEndLength";
57   _param_algo_dim = 1; // is used by SMESH_Regular_1D
58 }
59
60 //=============================================================================
61 /*!
62  *  
63  */
64 //=============================================================================
65
66 StdMeshers_StartEndLength::~StdMeshers_StartEndLength()
67 {
68 }
69
70 //=============================================================================
71 /*!
72  *  
73  */
74 //=============================================================================
75
76 void StdMeshers_StartEndLength::SetLength(double length, bool isStartLength)
77      throw(SALOME_Exception)
78 {
79   if ( (isStartLength ? _begLength : _endLength) != length ) {
80     if (length <= 0)
81       throw SALOME_Exception(LOCALIZED("length must be positive"));
82     if ( isStartLength )
83       _begLength = length;
84     else
85       _endLength = length;
86
87     NotifySubMeshesHypothesisModification();
88   }
89 }
90
91 //=============================================================================
92 /*!
93  *  
94  */
95 //=============================================================================
96
97 double StdMeshers_StartEndLength::GetLength(bool isStartLength) const
98 {
99   return isStartLength ? _begLength : _endLength;
100 }
101
102 //=============================================================================
103 /*!
104  *  
105  */
106 //=============================================================================
107
108 void StdMeshers_StartEndLength::SetReversedEdges( std::vector<int>& ids )
109 {
110   if ( ids != _edgeIDs ) {
111     _edgeIDs = ids;
112
113     NotifySubMeshesHypothesisModification();
114   }
115 }
116
117 //=============================================================================
118 /*!
119  *  
120  */
121 //=============================================================================
122
123 ostream & StdMeshers_StartEndLength::SaveTo(ostream & save)
124 {
125   int listSize = _edgeIDs.size();
126   save << _begLength << " " << _endLength << " " << listSize;
127
128   if ( listSize > 0 ) {
129     for ( int i = 0; i < listSize; i++) {
130       save << " " << _edgeIDs[i];
131     }
132     save << " " << _objEntry;
133   }
134
135   return save;
136 }
137
138 //=============================================================================
139 /*!
140  *  
141  */
142 //=============================================================================
143
144 istream & StdMeshers_StartEndLength::LoadFrom(istream & load)
145 {
146   bool isOK = true;
147   int intVal;
148   isOK = static_cast<bool>(load >> _begLength);
149   if (!isOK)
150     load.clear(ios::badbit | load.rdstate());
151   isOK = static_cast<bool>(load >> _endLength);
152
153   if (!isOK)
154     load.clear(ios::badbit | load.rdstate());
155
156   isOK = static_cast<bool>(load >> intVal);
157   if (isOK && intVal > 0) {
158     _edgeIDs.reserve( intVal );
159     for ( size_t i = 0; i < _edgeIDs.capacity() && isOK; i++) {
160       isOK = static_cast<bool>(load >> intVal);
161       if ( isOK ) _edgeIDs.push_back( intVal );
162     }
163     isOK = static_cast<bool>(load >> _objEntry);
164   }
165
166   return load;
167 }
168
169 //=============================================================================
170 /*!
171  *
172  */
173 //=============================================================================
174
175 ostream & operator <<(ostream & save, StdMeshers_StartEndLength & hyp)
176 {
177   return hyp.SaveTo( save );
178 }
179
180 //=============================================================================
181 /*!
182  *  
183  */
184 //=============================================================================
185
186 istream & operator >>(istream & load, StdMeshers_StartEndLength & hyp)
187 {
188   return hyp.LoadFrom( load );
189 }
190
191 //================================================================================
192 /*!
193  * \brief Initialize start and end length by the mesh built on the geometry
194  * \param theMesh - the built mesh
195  * \param theShape - the geometry of interest
196  * \retval bool - true if parameter values have been successfully defined
197  */
198 //================================================================================
199
200 bool StdMeshers_StartEndLength::SetParametersByMesh(const SMESH_Mesh*   theMesh,
201                                                     const TopoDS_Shape& theShape)
202 {
203   if ( !theMesh || theShape.IsNull() )
204     return false;
205
206   _begLength = _endLength = 0.;
207
208   Standard_Real UMin, UMax;
209   TopLoc_Location L;
210
211   int nbEdges = 0;
212   TopTools_IndexedMapOfShape edgeMap;
213   TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
214   for ( int i = 1; i <= edgeMap.Extent(); ++i )
215   {
216     const TopoDS_Edge& edge = TopoDS::Edge( edgeMap( i ));
217     Handle(Geom_Curve) C = BRep_Tool::Curve(edge, L, UMin, UMax);
218     GeomAdaptor_Curve AdaptCurve(C, UMin, UMax);
219
220     vector< double > params;
221     SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
222     if ( SMESH_Algo::GetNodeParamOnEdge( aMeshDS, edge, params ))
223     {
224       nbEdges++;
225       _begLength += GCPnts_AbscissaPoint::Length( AdaptCurve, params[0], params[1]);
226       int nb = params.size();
227       _endLength += GCPnts_AbscissaPoint::Length( AdaptCurve, params[nb-2], params[nb-1]);
228     }
229   }
230   if ( nbEdges ) {
231     _begLength /= nbEdges;
232     _endLength /= nbEdges;
233   }
234   return nbEdges;
235 }
236
237 //================================================================================
238 /*!
239  * \brief Initialize my parameter values by default parameters.
240  *  \retval bool - true if parameter values have been successfully defined
241  */
242 //================================================================================
243
244 bool StdMeshers_StartEndLength::SetParametersByDefaults(const TDefaults&  dflts,
245                                                         const SMESH_Mesh* /*theMesh*/)
246 {
247   return (_begLength = _endLength = dflts._elemLength );
248 }
249