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