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