Salome HOME
Merge from BR_V5_DEV 17Feb09
[plugins/blsurfplugin.git] / src / BLSURFPlugin / BLSURFPlugin_Hypothesis.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // ---
20 // File    : BLSURFPlugin_Hypothesis.cxx
21 // Authors : Francis KLOSS (OCC) & Patrick LAUG (INRIA) & Lioka RAZAFINDRAZAKA (CEA)
22 //           & Aurelien ALLEAUME (DISTENE)
23 // ---
24 //
25 #include <BLSURFPlugin_Hypothesis.hxx>
26 #include <utilities.h>
27
28 //=============================================================================
29 BLSURFPlugin_Hypothesis::BLSURFPlugin_Hypothesis (int hypId, int studyId,
30                                                   SMESH_Gen * gen)
31   : SMESH_Hypothesis(hypId, studyId, gen),
32     _topology(GetDefaultTopology()),
33     _physicalMesh(GetDefaultPhysicalMesh()),
34     _phySize(GetDefaultPhySize()),
35     _phyMax(GetDefaultMaxSize()),
36     _phyMin(GetDefaultMinSize()),
37     _hgeoMax(GetDefaultMaxSize()),
38     _hgeoMin(GetDefaultMinSize()),
39     _geometricMesh(GetDefaultGeometricMesh()),
40     _angleMeshS(GetDefaultAngleMeshS()),
41     _angleMeshC(GetDefaultAngleMeshC()),
42     _gradation(GetDefaultGradation()),
43     _quadAllowed(GetDefaultQuadAllowed()),
44     _decimesh(GetDefaultDecimesh()),
45     _verb( GetDefaultVerbosity() )
46 {
47   _name = "BLSURF_Parameters";
48   _param_algo_dim = 2;
49
50   // to desable writing boundaries
51   //_phyMin = _phyMax = _hgeoMin = _hgeoMax = undefinedDouble();
52   
53
54   const char* intOptionNames[] = {
55     "addsurf_ivertex",
56     "background",
57     "CheckAdjacentEdges",
58     "CheckCloseEdges",
59     "CheckWellDefined",
60     "coiter",
61     "communication",
62     "decim",
63     "export_flag",
64     "file_h",
65     "frontal",
66     "gridnu",
67     "gridnv",
68     "hinterpol_flag",
69     "hmean_flag",
70     "intermedfile",
71     "memory",
72     "normals",
73     "optim",
74     "pardom_flag",
75     "pinch",
76     "refs",
77     "rigid",
78     "surforient",
79     "tconf",
80     "topo_collapse",
81     "" // mark of end
82   };
83   const char* doubleOptionNames[] = {
84     "addsurf_angle",
85     "addsurf_R",
86     "addsurf_H",
87     "addsurf_FG",
88     "addsurf_r",
89     "addsurf_PA",
90     "angle_compcurv",
91     "angle_ridge",
92     "CoefRectangle",
93     "eps_collapse",
94     "eps_ends",
95     "eps_pardom",
96     "LSS",
97     "topo_eps1",
98     "topo_eps2",
99     "" // mark of end
100   };
101   const char* charOptionNames[] = {
102     "export_format",
103     "export_option",
104     "import_option",
105     "prefix",
106     "" // mark of end
107   };
108
109   int i = 0;
110   while ( intOptionNames[i][0] )
111     _option2value[ intOptionNames[i++] ].clear();
112
113   i = 0;
114   while ( doubleOptionNames[i][0] ) {
115     _doubleOptions.insert( doubleOptionNames[i] );
116     _option2value[ doubleOptionNames[i++] ].clear();
117   }
118   i = 0;
119   while ( charOptionNames[i][0] ) {
120     _charOptions.insert( charOptionNames[i] );
121     _option2value[ charOptionNames[i++] ].clear();
122   }
123 }
124
125 //=============================================================================
126 void BLSURFPlugin_Hypothesis::SetTopology(Topology theTopology)
127 {
128   if (theTopology != _topology) {
129     _topology = theTopology;
130     NotifySubMeshesHypothesisModification();
131   }
132 }
133
134 //=============================================================================
135 void BLSURFPlugin_Hypothesis::SetPhysicalMesh(PhysicalMesh thePhysicalMesh)
136 {
137   if (thePhysicalMesh != _physicalMesh) {
138     _physicalMesh = thePhysicalMesh;
139     switch( _physicalMesh ) {
140       case DefaultSize:
141       default:
142         _phySize = GetDefaultPhySize();
143         break;
144       }
145     NotifySubMeshesHypothesisModification();
146   }
147 }
148
149 //=============================================================================
150 void BLSURFPlugin_Hypothesis::SetPhySize(double theVal)
151 {
152   if (theVal != _phySize) {
153     _phySize = theVal;
154     NotifySubMeshesHypothesisModification();
155   }
156 }
157
158 //=============================================================================
159 void BLSURFPlugin_Hypothesis::SetPhyMin(double theMinSize)
160 {
161   if (theMinSize != _phyMin) {
162     _phyMin = theMinSize;
163     NotifySubMeshesHypothesisModification();
164   }
165 }
166
167 //=============================================================================
168 void BLSURFPlugin_Hypothesis::SetPhyMax(double theMaxSize)
169 {
170   if (theMaxSize != _phyMax) {
171     _phyMax = theMaxSize;
172     NotifySubMeshesHypothesisModification();
173   }
174 }
175
176
177 //=============================================================================
178 void BLSURFPlugin_Hypothesis::SetGeoMin(double theMinSize)
179 {
180   if (theMinSize != _hgeoMin) {
181     _hgeoMin = theMinSize;
182     NotifySubMeshesHypothesisModification();
183   }
184 }
185
186 //=============================================================================
187 void BLSURFPlugin_Hypothesis::SetGeoMax(double theMaxSize)
188 {
189   if (theMaxSize != _hgeoMax) {
190     _hgeoMax = theMaxSize;
191     NotifySubMeshesHypothesisModification();
192   }
193 }
194
195 //=============================================================================
196 void BLSURFPlugin_Hypothesis::SetGeometricMesh(GeometricMesh theGeometricMesh)
197 {
198   if (theGeometricMesh != _geometricMesh) {
199     _geometricMesh = theGeometricMesh;
200     switch( _geometricMesh ) {
201       case DefaultGeom:
202       default:
203         _angleMeshS = GetDefaultAngleMeshS();
204         _gradation  = GetDefaultGradation();
205         break;
206       }
207     NotifySubMeshesHypothesisModification();
208   }
209 }
210
211 //=============================================================================
212 void BLSURFPlugin_Hypothesis::SetAngleMeshS(double theVal)
213 {
214   if (theVal != _angleMeshS) {
215     _angleMeshS = theVal;
216     NotifySubMeshesHypothesisModification();
217   }
218 }
219
220 //=============================================================================
221 void BLSURFPlugin_Hypothesis::SetAngleMeshC(double theVal)
222 {
223   if (theVal != _angleMeshC) {
224     _angleMeshC = theVal;
225     NotifySubMeshesHypothesisModification();
226   }
227 }
228
229 //=============================================================================
230 void BLSURFPlugin_Hypothesis::SetGradation(double theVal)
231 {
232   if (theVal != _gradation) {
233     _gradation = theVal;
234     NotifySubMeshesHypothesisModification();
235   }
236 }
237
238 //=============================================================================
239 void BLSURFPlugin_Hypothesis::SetQuadAllowed(bool theVal)
240 {
241   if (theVal != _quadAllowed) {
242     _quadAllowed = theVal;
243     NotifySubMeshesHypothesisModification();
244   }
245 }
246
247 //=============================================================================
248 void BLSURFPlugin_Hypothesis::SetDecimesh(bool theVal)
249 {
250   if (theVal != _decimesh) {
251     _decimesh = theVal;
252     NotifySubMeshesHypothesisModification();
253   }
254 }
255
256 //=============================================================================
257 void BLSURFPlugin_Hypothesis::SetVerbosity(int theVal)
258 {
259   if (theVal != _verb) {
260     _verb = theVal;
261     NotifySubMeshesHypothesisModification();
262   }
263 }
264 //=============================================================================
265 void BLSURFPlugin_Hypothesis::SetOptionValue(const std::string& optionName,
266                                              const std::string& optionValue)
267   throw (std::invalid_argument)
268 {
269   TOptionValues::iterator op_val = _option2value.find( optionName );
270   if ( op_val == _option2value.end() ) {
271     std::string msg = "Unknown BLSURF option: '" + optionName + "'";
272     throw std::invalid_argument(msg);
273   }
274   if ( op_val->second != optionValue ) {
275     const char* ptr = optionValue.c_str();
276     // strip white spaces
277     while ( ptr[0] == ' ' )
278       ptr++;
279     int i = strlen( ptr );
280     while ( i != 0 && ptr[i-1] == ' ')
281       i--;
282     // check value type
283     bool typeOk = true;
284     std::string typeName;
285     if ( i == 0 ) {
286       // empty string
287     }
288     else if ( _charOptions.find( optionName ) != _charOptions.end() ) {
289       // do not check strings
290     }
291     else if ( _doubleOptions.find( optionName ) != _doubleOptions.end() ) {
292       // check if value is double
293       char * endPtr;
294       strtod(ptr, &endPtr);
295       typeOk = ( ptr != endPtr );
296       typeName = "real";
297     }
298     else {
299       // check if value is int
300       char * endPtr;
301       strtol(ptr, &endPtr,10);
302       typeOk = ( ptr != endPtr );
303       typeName = "integer";
304     }
305     if ( !typeOk ) {
306       std::string msg = "Advanced option '" + optionName + "' = '" + optionValue +
307         "' but must be " + typeName;
308       throw std::invalid_argument(msg);
309     }
310     op_val->second = optionValue;
311     NotifySubMeshesHypothesisModification();
312   }
313 }
314
315 //=============================================================================
316 std::string BLSURFPlugin_Hypothesis::GetOptionValue(const std::string& optionName)
317   throw (std::invalid_argument)
318 {
319   TOptionValues::iterator op_val = _option2value.find( optionName );
320   if ( op_val == _option2value.end() ) {
321     std::string msg = "Unknown BLSURF option: <";
322     msg += optionName + ">";
323     throw std::invalid_argument(msg);
324   }
325   return op_val->second;
326 }
327
328 //=============================================================================
329 void BLSURFPlugin_Hypothesis::ClearOption(const std::string& optionName)
330 {
331   TOptionValues::iterator op_val = _option2value.find( optionName );
332   if ( op_val != _option2value.end() )
333     op_val->second.clear();
334 }
335
336 //=============================================================================
337 std::ostream & BLSURFPlugin_Hypothesis::SaveTo(std::ostream & save)
338 {
339   save << " " << (int)_topology
340        << " " << (int)_physicalMesh
341        << " " << (int)_geometricMesh
342        << " " << _phySize
343        << " " << _angleMeshS   
344        << " " << _gradation     
345        << " " << (int)_quadAllowed 
346        << " " << (int)_decimesh;
347   save << " " << _phyMin
348        << " " << _phyMax
349        << " " << _angleMeshC
350        << " " << _hgeoMin
351        << " " << _hgeoMax
352        << " " << _verb;
353
354   TOptionValues::iterator op_val = _option2value.begin();
355   for ( ; op_val != _option2value.end(); ++op_val ) {
356     if ( !op_val->second.empty() )
357       save << " " << op_val->first
358            << " " << op_val->second << "%#"; // "%#" is a mark of value end
359   }
360
361   return save;
362 }
363
364 //=============================================================================
365 std::istream & BLSURFPlugin_Hypothesis::LoadFrom(std::istream & load)
366 {
367   bool isOK = true;
368   int i;
369   double val;
370
371   isOK = (load >> i);
372   if (isOK)
373     _topology = (Topology) i;
374   else
375     load.clear(std::ios::badbit | load.rdstate());
376
377   isOK = (load >> i);
378   if (isOK)
379     _physicalMesh = (PhysicalMesh) i;
380   else
381     load.clear(std::ios::badbit | load.rdstate());
382
383   isOK = (load >> i);
384   if (isOK)
385     _geometricMesh = (GeometricMesh) i;
386   else
387     load.clear(std::ios::badbit | load.rdstate());
388
389   isOK = (load >> val);
390   if (isOK)
391     _phySize = val;
392   else
393     load.clear(std::ios::badbit | load.rdstate());
394
395   isOK = (load >> val);
396   if (isOK)
397     _angleMeshS = val;
398   else
399     load.clear(std::ios::badbit | load.rdstate());
400
401   isOK = (load >> val);
402   if (isOK)
403     _gradation = val;
404   else
405     load.clear(std::ios::badbit | load.rdstate());
406
407   isOK = (load >> i);
408   if (isOK)
409     _quadAllowed = (bool) i;
410   else
411     load.clear(std::ios::badbit | load.rdstate());
412
413   isOK = (load >> i);
414   if (isOK)
415     _decimesh = (bool) i;
416   else
417     load.clear(std::ios::badbit | load.rdstate());
418
419   isOK = (load >> val);
420   if (isOK)
421     _phyMin = val;
422   else
423     load.clear(std::ios::badbit | load.rdstate());
424   
425   isOK = (load >> val);
426   if (isOK)
427     _phyMax = val;
428   else
429     load.clear(std::ios::badbit | load.rdstate());
430   
431   isOK = (load >> val);
432   if (isOK)
433     _angleMeshC = val;
434   else
435     load.clear(std::ios::badbit | load.rdstate());
436   
437   isOK = (load >> val);
438   if (isOK)
439     _hgeoMin = val;
440   else
441     load.clear(std::ios::badbit | load.rdstate());
442   
443   isOK = (load >> val);
444   if (isOK)
445     _hgeoMax = val;
446   else
447     load.clear(std::ios::badbit | load.rdstate());
448   
449   isOK = (load >> i);
450   if (isOK)
451     _verb = i;
452   else
453     load.clear(std::ios::badbit | load.rdstate());
454
455   std::string optName, optValue;
456   while (isOK) {
457     isOK = (load >> optName);
458     if (isOK)
459       isOK = (load >> optValue);
460     if (isOK) {
461       std::string & value = _option2value[ optName ];
462       value = optValue;
463       int len = value.size();
464       // continue reading until "%#" encountered
465       while ( value[len-1] != '#' || value[len-2] != '%' )
466       {
467         isOK = (load >> optValue);
468         if (isOK) {
469           value += " ";
470           value += optValue;
471           len = value.size();
472         }
473         else {
474           break;
475         }
476       }
477       value[ len-2 ] = '\0'; //cut off "%#"
478     }
479   }
480
481   return load;
482 }
483
484 //=============================================================================
485 std::ostream & operator <<(std::ostream & save, BLSURFPlugin_Hypothesis & hyp)
486 {
487   return hyp.SaveTo( save );
488 }
489
490 //=============================================================================
491 std::istream & operator >>(std::istream & load, BLSURFPlugin_Hypothesis & hyp)
492 {
493   return hyp.LoadFrom( load );
494 }
495
496 //================================================================================
497 /*!
498  * \brief Does nothing
499  */
500 //================================================================================
501
502 bool BLSURFPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh*   theMesh,
503                                                       const TopoDS_Shape& theShape)
504 {
505   return false;
506 }
507
508 //=============================================================================
509 /*!
510  * \brief Initialize my parameter values by default parameters.
511  *  \retval bool - true if parameter values have been successfully defined
512  */
513 //=============================================================================
514
515 bool BLSURFPlugin_Hypothesis::SetParametersByDefaults(const TDefaults&   dflts,
516                                                       const SMESH_Mesh* theMesh)
517 {
518   return bool( _phySize = dflts._elemLength );
519 }
520
521 //=============================================================================
522 BLSURFPlugin_Hypothesis::Topology BLSURFPlugin_Hypothesis::GetDefaultTopology()
523 {
524   return FromCAD;
525 }
526
527 //=============================================================================
528 BLSURFPlugin_Hypothesis::PhysicalMesh BLSURFPlugin_Hypothesis::GetDefaultPhysicalMesh()
529 {
530   return PhysicalUserDefined;
531 }
532
533 //=============================================================================
534 double BLSURFPlugin_Hypothesis::GetDefaultPhySize()
535 {
536   return 10;
537 }
538
539 //======================================================================
540 double BLSURFPlugin_Hypothesis::GetDefaultMaxSize()
541 {
542   return undefinedDouble(); // 1e+4;
543 }
544
545 //======================================================================
546 double BLSURFPlugin_Hypothesis::GetDefaultMinSize()
547 {
548   return undefinedDouble(); //1e-4;
549 }
550
551 //======================================================================
552 BLSURFPlugin_Hypothesis::GeometricMesh BLSURFPlugin_Hypothesis::GetDefaultGeometricMesh()
553 {
554   return DefaultGeom;
555 }
556
557 //=============================================================================
558 double BLSURFPlugin_Hypothesis::GetDefaultAngleMeshS()
559 {
560   return 8;
561 }
562
563 //=============================================================================
564 double BLSURFPlugin_Hypothesis::GetDefaultGradation()
565 {
566   return 1.1;
567 }
568
569 //=============================================================================
570 bool BLSURFPlugin_Hypothesis::GetDefaultQuadAllowed()
571 {
572   return false;
573 }
574
575 //=============================================================================
576 bool BLSURFPlugin_Hypothesis::GetDefaultDecimesh()
577 {
578   return false;
579 }