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