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