Salome HOME
Freshly added file
[modules/smesh.git] / src / SMESH / SMESH_Algo.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   : SMESH_Algo.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //  $Header$
28
29 using namespace std;
30 using namespace std;
31 #include "SMESH_Algo.hxx"
32 #include "SMESH_Gen.hxx"
33 #include "SMESH_Mesh.hxx"
34
35 #include <GeomAdaptor_Curve.hxx>
36 #include <BRep_Tool.hxx>
37 #include <GCPnts_AbscissaPoint.hxx>
38
39 #include "utilities.h"
40
41 #include <algorithm>
42
43 //=============================================================================
44 /*!
45  *  
46  */
47 //=============================================================================
48
49 SMESH_Algo::SMESH_Algo(int hypId, int studyId,
50         SMESH_Gen * gen):SMESH_Hypothesis(hypId, studyId, gen)
51 {
52 //   _compatibleHypothesis.push_back("hypothese_bidon");
53         _type = ALGO;
54         gen->_mapAlgo[hypId] = this;
55 }
56
57 //=============================================================================
58 /*!
59  *  
60  */
61 //=============================================================================
62
63 SMESH_Algo::~SMESH_Algo()
64 {
65 }
66
67 //=============================================================================
68 /*!
69  *  
70  */
71 //=============================================================================
72
73 const vector < string > &SMESH_Algo::GetCompatibleHypothesis()
74 {
75         return _compatibleHypothesis;
76 }
77
78 //=============================================================================
79 /*!
80  *  
81  */
82 //=============================================================================
83
84 ostream & SMESH_Algo::SaveTo(ostream & save)
85 {
86         return save << this;
87 }
88
89 //=============================================================================
90 /*!
91  *  
92  */
93 //=============================================================================
94
95 istream & SMESH_Algo::LoadFrom(istream & load)
96 {
97         return load >> (*this);
98 }
99
100 //=============================================================================
101 /*!
102  *  
103  */
104 //=============================================================================
105
106 ostream & operator <<(ostream & save, SMESH_Algo & hyp)
107 {
108         return save;
109 }
110
111 //=============================================================================
112 /*!
113  *  
114  */
115 //=============================================================================
116
117 istream & operator >>(istream & load, SMESH_Algo & hyp)
118 {
119         return load;
120 }
121
122 //=============================================================================
123 /*!
124  *  
125  */
126 //=============================================================================
127
128 bool SMESH_Algo::CheckHypothesis(SMESH_Mesh & aMesh,
129         const TopoDS_Shape & aShape)
130 {
131         MESSAGE("SMESH_Algo::CheckHypothesis");
132         ASSERT(0);                                      // use method from derived classes
133         return false;
134 }
135
136 //=============================================================================
137 /*!
138  *  
139  */
140 //=============================================================================
141
142 bool SMESH_Algo::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
143 {
144         MESSAGE("SMESH_Algo::Compute");
145         ASSERT(0);                                      // use method from derived classes
146         return false;
147 }
148
149 //=============================================================================
150 /*!
151  *  List the hypothesis used by the algorithm associated to the shape.
152  *  Hypothesis associated to father shape -are- taken into account (see
153  *  GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
154  *  the algorithm. This method could be surcharged by specific algorithms, in 
155  *  case of several hypothesis simultaneously applicable.
156  */
157 //=============================================================================
158
159 const list <const SMESHDS_Hypothesis *> & SMESH_Algo::GetUsedHypothesis(
160         SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
161 {
162         _usedHypList.clear();
163         _usedHypList = GetAppliedHypothesis(aMesh, aShape);     // copy
164         int nbHyp = _usedHypList.size();
165         if (nbHyp == 0)
166         {
167                 TopoDS_Shape mainShape = aMesh.GetMeshDS()->ShapeToMesh();
168                 if (!mainShape.IsSame(aShape))
169                 {
170                         _usedHypList = GetAppliedHypothesis(aMesh, mainShape);  // copy
171                         nbHyp = _usedHypList.size();
172                 }
173         }
174         if (nbHyp > 1)
175                 _usedHypList.clear();   //only one compatible hypothesis allowed
176         return _usedHypList;
177 }
178
179 //=============================================================================
180 /*!
181  *  List the relevant hypothesis associated to the shape. Relevant hypothesis
182  *  have a name (type) listed in the algorithm. Hypothesis associated to
183  *  father shape -are not- taken into account (see GetUsedHypothesis)
184  */
185 //=============================================================================
186
187 const list<const SMESHDS_Hypothesis *> & SMESH_Algo::GetAppliedHypothesis(
188         SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
189 {
190         const SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
191         const list<const SMESHDS_Hypothesis*> & listHyp = meshDS->GetHypothesis(aShape);
192         list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
193
194         int hypType;
195         string hypName;
196
197         _appliedHypList.clear();
198         while (it!=listHyp.end())
199         {
200                 const SMESHDS_Hypothesis *anHyp = *it;
201                 hypType = anHyp->GetType();
202                 //SCRUTE(hypType);
203                 if (hypType == SMESHDS_Hypothesis::PARAM_ALGO)
204                 {
205                         hypName = anHyp->GetName();
206                         vector < string >::iterator ith =
207                                 find(_compatibleHypothesis.begin(), _compatibleHypothesis.end(),
208                                 hypName);
209                         if (ith != _compatibleHypothesis.end()) // count only relevant 
210                         {
211                                 _appliedHypList.push_back(anHyp);
212                                 //SCRUTE(hypName);
213                         }
214                 }
215                 it++;
216         }
217         return _appliedHypList;
218 }
219
220 //=============================================================================
221 /*!
222  *  Compute length of an edge
223  */
224 //=============================================================================
225
226 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
227 {
228         double UMin = 0, UMax = 0;
229         TopLoc_Location L;
230         if (BRep_Tool::Degenerated(E))
231                 return 0;
232         Handle(Geom_Curve) C = BRep_Tool::Curve(E, L, UMin, UMax);
233         GeomAdaptor_Curve AdaptCurve(C);
234         GCPnts_AbscissaPoint gabs;
235         double length = gabs.Length(AdaptCurve, UMin, UMax);
236         return length;
237 }