Salome HOME
Update copyright info (2010->2011)
[plugins/netgenplugin.git] / src / NETGENPlugin / NETGENPlugin_Hypothesis.cxx
1 //  Copyright (C) 2007-2011  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
23 //  NETGENPlugin : C++ implementation
24 // File      : NETGENPlugin_Hypothesis.cxx
25 // Author    : Michael Sazonov (OCN)
26 // Date      : 28/03/2006
27 // Project   : SALOME
28 //
29 #include "NETGENPlugin_Hypothesis.hxx"
30 #include <utilities.h>
31
32 using namespace std;
33
34 //=============================================================================
35 /*!
36  *  
37  */
38 //=============================================================================
39 NETGENPlugin_Hypothesis::NETGENPlugin_Hypothesis (int hypId, int studyId,
40                                                   SMESH_Gen * gen)
41   : SMESH_Hypothesis(hypId, studyId, gen),
42     _maxSize       (GetDefaultMaxSize()),
43     _growthRate    (GetDefaultGrowthRate()),
44     _nbSegPerEdge  (GetDefaultNbSegPerEdge()),
45     _nbSegPerRadius(GetDefaultNbSegPerRadius()),
46     _fineness      (GetDefaultFineness()),
47     _secondOrder   (GetDefaultSecondOrder()),
48     _optimize      (GetDefaultOptimize()),
49     _localSize     (GetDefaultLocalSize())
50 {
51   _name = "NETGEN_Parameters";
52   _param_algo_dim = 3;
53   _localSize.clear();
54 }
55
56 //=============================================================================
57 /*!
58  *  
59  */
60 //=============================================================================
61 void NETGENPlugin_Hypothesis::SetMaxSize(double theSize)
62 {
63   if (theSize != _maxSize)
64   {
65     _maxSize = theSize;
66     NotifySubMeshesHypothesisModification();
67   }
68 }
69
70 //=============================================================================
71 /*!
72  *  
73  */
74 //=============================================================================
75 void NETGENPlugin_Hypothesis::SetSecondOrder(bool theVal)
76 {
77   if (theVal != _secondOrder)
78   {
79     _secondOrder = theVal;
80     NotifySubMeshesHypothesisModification();
81   }
82 }
83
84 //=============================================================================
85 /*!
86  *  
87  */
88 //=============================================================================
89 void NETGENPlugin_Hypothesis::SetOptimize(bool theVal)
90 {
91   if (theVal != _optimize)
92   {
93     _optimize = theVal;
94     NotifySubMeshesHypothesisModification();
95   }
96 }
97
98 //=============================================================================
99 /*!
100  *  
101  */
102 //=============================================================================
103 void NETGENPlugin_Hypothesis::SetFineness(Fineness theFineness)
104 {
105   if (theFineness != _fineness)
106   {
107     _fineness = theFineness;
108     // the predefined values are taken from NETGEN 4.5 sources
109     switch (_fineness)
110     {
111     case VeryCoarse:
112       _growthRate = 0.7;
113       _nbSegPerEdge = 0.3;
114       _nbSegPerRadius = 1;
115       break;
116     case Coarse:
117       _growthRate = 0.5;
118       _nbSegPerEdge = 0.5;
119       _nbSegPerRadius = 1.5;
120       break;
121     case Fine:
122       _growthRate = 0.2;
123       _nbSegPerEdge = 2;
124       _nbSegPerRadius = 3;
125       break;
126     case VeryFine:
127       _growthRate = 0.1;
128       _nbSegPerEdge = 3;
129       _nbSegPerRadius = 5;
130       break;
131     case UserDefined:
132       break;
133     case Moderate:
134     default:
135       _growthRate = 0.3;
136       _nbSegPerEdge = 1;
137       _nbSegPerRadius = 2;
138       break;
139     }
140     NotifySubMeshesHypothesisModification();
141   }
142 }
143
144 //=============================================================================
145 /*!
146  *  
147  */
148 //=============================================================================
149 void NETGENPlugin_Hypothesis::SetGrowthRate(double theRate)
150 {
151   if (theRate != _growthRate)
152   {
153     _growthRate = theRate;
154     _fineness = UserDefined;
155     NotifySubMeshesHypothesisModification();
156   }
157 }
158
159 //=============================================================================
160 /*!
161  *  
162  */
163 //=============================================================================
164 void NETGENPlugin_Hypothesis::SetNbSegPerEdge(double theVal)
165 {
166   if (theVal != _nbSegPerEdge)
167   {
168     _nbSegPerEdge = theVal;
169     _fineness = UserDefined;
170     NotifySubMeshesHypothesisModification();
171   }
172 }
173
174 //=============================================================================
175 /*!
176  *  
177  */
178 //=============================================================================
179 void NETGENPlugin_Hypothesis::SetNbSegPerRadius(double theVal)
180 {
181   if (theVal != _nbSegPerRadius)
182   {
183     _nbSegPerRadius = theVal;
184     _fineness = UserDefined;
185     NotifySubMeshesHypothesisModification();
186   }
187 }
188
189 //=============================================================================
190 /*!
191  *  
192  */
193 //=============================================================================
194 void NETGENPlugin_Hypothesis::SetLocalSizeOnEntry(const std::string& entry, double localSize)
195 {
196   if(_localSize[entry] != localSize)
197     {
198       _localSize[entry] = localSize;
199       NotifySubMeshesHypothesisModification();
200     }
201 }
202
203 //=============================================================================
204 /*!
205  *  
206  */
207 //=============================================================================
208 double NETGENPlugin_Hypothesis::GetLocalSizeOnEntry(const std::string& entry)
209 {
210   TLocalSize::iterator it  = _localSize.find( entry );
211   if ( it != _localSize.end() )
212     return it->second;
213   else
214     return -1.0;
215 }
216
217 //=============================================================================
218 /*!
219  *  
220  */
221 //=============================================================================
222 void NETGENPlugin_Hypothesis::UnsetLocalSizeOnEntry(const std::string& entry)
223 {
224   _localSize.erase(entry);
225   NotifySubMeshesHypothesisModification();
226 }
227
228 //=============================================================================
229 /*!
230  *  
231  */
232 //=============================================================================
233 ostream & NETGENPlugin_Hypothesis::SaveTo(ostream & save)
234 {
235   save << _maxSize << " " << _fineness;
236
237   if (_fineness == UserDefined)
238     save << " " << _growthRate << " " << _nbSegPerEdge << " " << _nbSegPerRadius;
239
240   save << " " << (int)_secondOrder << " " << (int)_optimize;
241
242   TLocalSize::iterator it_sm  = _localSize.begin();
243   if (it_sm != _localSize.end()) {
244     save << " " << "__LOCALSIZE_BEGIN__";
245     for ( ; it_sm != _localSize.end(); ++it_sm ) {
246         save << " " << it_sm->first
247              << " " << it_sm->second << "%#"; // "%#" is a mark of value end
248     }
249     save << " " << "__LOCALSIZE_END__";
250   }
251
252   return save;
253 }
254
255 //=============================================================================
256 /*!
257  *  
258  */
259 //=============================================================================
260 istream & NETGENPlugin_Hypothesis::LoadFrom(istream & load)
261 {
262   bool isOK = true;
263   int is;
264   double val;
265
266   isOK = (load >> val);
267   if (isOK)
268     _maxSize = val;
269   else
270     load.clear(ios::badbit | load.rdstate());
271
272   isOK = (load >> is);
273   if (isOK)
274     SetFineness((Fineness) is);
275   else
276     load.clear(ios::badbit | load.rdstate());
277
278   if (_fineness == UserDefined)
279   {
280     isOK = (load >> val);
281     if (isOK)
282       _growthRate = val;
283     else
284       load.clear(ios::badbit | load.rdstate());
285
286     isOK = (load >> val);
287     if (isOK)
288       _nbSegPerEdge = val;
289     else
290       load.clear(ios::badbit | load.rdstate());
291
292     isOK = (load >> val);
293     if (isOK)
294       _nbSegPerRadius = val;
295     else
296       load.clear(ios::badbit | load.rdstate());
297   }
298
299   isOK = (load >> is);
300   if (isOK)
301     _secondOrder = (bool) is;
302   else
303     load.clear(ios::badbit | load.rdstate());
304
305   isOK = (load >> is);
306   if (isOK)
307     _optimize = (bool) is;
308   else
309     load.clear(ios::badbit | load.rdstate());
310
311   std::string option_or_sm;
312   bool hasLocalSize = false;
313
314   isOK = (load >> option_or_sm);
315   if (isOK)
316     if (option_or_sm == "__LOCALSIZE_BEGIN__")
317       hasLocalSize = true;
318
319   std::string smEntry, smValue;
320   while (isOK && hasLocalSize) {
321     isOK = (load >> smEntry);
322     if (isOK) {
323       if (smEntry == "__LOCALSIZE_END__")
324         break;
325       isOK = (load >> smValue);
326     }
327     if (isOK) {
328       std::istringstream tmp(smValue);
329       double val;
330       tmp >> val;
331       _localSize[ smEntry ] = val;
332     }
333   }
334
335   return load;
336 }
337
338 //=============================================================================
339 /*!
340  *  
341  */
342 //=============================================================================
343 ostream & operator <<(ostream & save, NETGENPlugin_Hypothesis & hyp)
344 {
345   return hyp.SaveTo( save );
346 }
347
348 //=============================================================================
349 /*!
350  *  
351  */
352 //=============================================================================
353 istream & operator >>(istream & load, NETGENPlugin_Hypothesis & hyp)
354 {
355   return hyp.LoadFrom( load );
356 }
357
358
359 //================================================================================
360 /*!
361  * \brief Does nothing
362  * \param theMesh - the built mesh
363  * \param theShape - the geometry of interest
364  * \retval bool - always false
365  */
366 //================================================================================
367 bool NETGENPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh*   theMesh,
368                                                   const TopoDS_Shape& theShape)
369 {
370   return false;
371 }
372
373 //================================================================================
374 /*!
375  * \brief Initialize my parameter values by default parameters.
376  *  \retval bool - true if parameter values have been successfully defined
377  */
378 //================================================================================
379
380 bool NETGENPlugin_Hypothesis::SetParametersByDefaults(const TDefaults&  dflts,
381                                                       const SMESH_Mesh* /*theMesh*/)
382 {
383   _nbSegPerEdge = dflts._nbSegments;
384   _maxSize      = dflts._elemLength;
385   return _nbSegPerEdge && _maxSize > 0;
386 }
387
388 //=============================================================================
389 /*!
390  *  
391  */
392 //=============================================================================
393 double NETGENPlugin_Hypothesis::GetDefaultMaxSize()
394 {
395   return 1000;
396 }
397
398 //=============================================================================
399 /*!
400  *  
401  */
402 //=============================================================================
403 NETGENPlugin_Hypothesis::Fineness NETGENPlugin_Hypothesis::GetDefaultFineness()
404 {
405   return Moderate;
406 }
407
408 //=============================================================================
409 /*!
410  *  
411  */
412 //=============================================================================
413 double NETGENPlugin_Hypothesis::GetDefaultGrowthRate()
414 {
415   return 0.3;
416 }
417
418 //=============================================================================
419 /*!
420  *  
421  */
422 //=============================================================================
423 double NETGENPlugin_Hypothesis::GetDefaultNbSegPerEdge()
424 {
425   return 1;
426 }
427
428 //=============================================================================
429 /*!
430  *  
431  */
432 //=============================================================================
433 double NETGENPlugin_Hypothesis::GetDefaultNbSegPerRadius()
434 {
435   return 2;
436 }
437
438 //=============================================================================
439 /*!
440  *  
441  */
442 //=============================================================================
443 bool NETGENPlugin_Hypothesis::GetDefaultSecondOrder()
444 {
445   return false;
446 }
447
448 //=============================================================================
449 /*!
450  *  
451  */
452 //=============================================================================
453 bool NETGENPlugin_Hypothesis::GetDefaultOptimize()
454 {
455   return true;
456 }