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