Salome HOME
Update copyright information
[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 //  $Header$
27 //
28 #include "StdMeshers_Arithmetic1D.hxx"
29
30 #include "SMESH_Algo.hxx"
31 #include "SMESH_Mesh.hxx"
32
33 #include <BRep_Tool.hxx>
34 #include <GCPnts_AbscissaPoint.hxx>
35 #include <GeomAdaptor_Curve.hxx>
36 #include <Geom_Curve.hxx>
37 #include <TopExp.hxx>
38 #include <TopLoc_Location.hxx>
39 #include <TopTools_IndexedMapOfShape.hxx>
40 #include <TopoDS.hxx>
41 #include <TopoDS_Edge.hxx>
42
43 using namespace std;
44
45 //=============================================================================
46 /*!
47  *  
48  */
49 //=============================================================================
50
51 StdMeshers_Arithmetic1D::StdMeshers_Arithmetic1D(int hypId, int studyId, SMESH_Gen * gen)
52   :SMESH_Hypothesis(hypId, studyId, gen)
53 {
54   _begLength = 1.;
55   _endLength = 10.;
56   _name = "Arithmetic1D";
57   _param_algo_dim = 1; 
58 }
59
60 //=============================================================================
61 /*!
62  *  
63  */
64 //=============================================================================
65
66 StdMeshers_Arithmetic1D::~StdMeshers_Arithmetic1D()
67 {
68 }
69
70 //=============================================================================
71 /*!
72  *  
73  */
74 //=============================================================================
75
76 void StdMeshers_Arithmetic1D::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_Arithmetic1D::GetLength(bool isStartLength) const
98 {
99   return isStartLength ? _begLength : _endLength;
100 }
101
102 //=============================================================================
103 /*!
104  *  
105  */
106 //=============================================================================
107
108 ostream & StdMeshers_Arithmetic1D::SaveTo(ostream & save)
109 {
110   save << _begLength << " " << _endLength;
111   return save;
112 }
113
114 //=============================================================================
115 /*!
116  *  
117  */
118 //=============================================================================
119
120 istream & StdMeshers_Arithmetic1D::LoadFrom(istream & load)
121 {
122   bool isOK = true;
123   isOK = (load >> _begLength);
124   if (!isOK)
125     load.clear(ios::badbit | load.rdstate());
126   isOK = (load >> _endLength);
127   if (!isOK)
128     load.clear(ios::badbit | load.rdstate());
129   return load;
130 }
131
132 //=============================================================================
133 /*!
134  *  
135  */
136 //=============================================================================
137
138 ostream & operator <<(ostream & save, StdMeshers_Arithmetic1D & hyp)
139 {
140   return hyp.SaveTo( save );
141 }
142
143 //=============================================================================
144 /*!
145  *  
146  */
147 //=============================================================================
148
149 istream & operator >>(istream & load, StdMeshers_Arithmetic1D & hyp)
150 {
151   return hyp.LoadFrom( load );
152 }
153
154 //================================================================================
155 /*!
156  * \brief Initialize start and end length by the mesh built on the geometry
157  * \param theMesh - the built mesh
158  * \param theShape - the geometry of interest
159  * \retval bool - true if parameter values have been successfully defined
160  */
161 //================================================================================
162
163 bool StdMeshers_Arithmetic1D::SetParametersByMesh(const SMESH_Mesh*   theMesh,
164                                                   const TopoDS_Shape& theShape)
165 {
166   if ( !theMesh || theShape.IsNull() )
167     return false;
168
169   _begLength = _endLength = 0.;
170
171   Standard_Real UMin, UMax;
172   TopLoc_Location L;
173
174   int nbEdges = 0;
175   TopTools_IndexedMapOfShape edgeMap;
176   TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
177   for ( int i = 1; i <= edgeMap.Extent(); ++i )
178   {
179     const TopoDS_Edge& edge = TopoDS::Edge( edgeMap( i ));
180     Handle(Geom_Curve) C = BRep_Tool::Curve(edge, L, UMin, UMax);
181     GeomAdaptor_Curve AdaptCurve(C);
182
183     vector< double > params;
184     SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
185     if ( SMESH_Algo::GetNodeParamOnEdge( aMeshDS, edge, params ))
186     {
187       nbEdges++;
188       _begLength += GCPnts_AbscissaPoint::Length( AdaptCurve, params[0], params[1]);
189       int nb = params.size();
190       _endLength += GCPnts_AbscissaPoint::Length( AdaptCurve, params[nb-2], params[nb-1]);
191     }
192   }
193   if ( nbEdges ) {
194     _begLength /= nbEdges;
195     _endLength /= nbEdges;
196   }
197   return nbEdges;
198 }