]> SALOME platform Git repositories - plugins/blsurfplugin.git/blob - src/BLSURFPlugin/BLSURFPlugin_Hypothesis.cxx
Salome HOME
First version of SizeMap for BLSurf :
[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   _sizeMap.clear();
126 }
127
128 //=============================================================================
129 void BLSURFPlugin_Hypothesis::SetTopology(Topology theTopology)
130 {
131   if (theTopology != _topology) {
132     _topology = theTopology;
133     NotifySubMeshesHypothesisModification();
134   }
135 }
136
137 //=============================================================================
138 void BLSURFPlugin_Hypothesis::SetPhysicalMesh(PhysicalMesh thePhysicalMesh)
139 {
140   if (thePhysicalMesh != _physicalMesh) {
141     _physicalMesh = thePhysicalMesh;
142     switch( _physicalMesh ) {
143       case DefaultSize:
144       default:
145         _phySize = GetDefaultPhySize();
146         break;
147       }
148     NotifySubMeshesHypothesisModification();
149   }
150 }
151
152 //=============================================================================
153 void BLSURFPlugin_Hypothesis::SetPhySize(double theVal)
154 {
155   if (theVal != _phySize) {
156     _phySize = theVal;
157     NotifySubMeshesHypothesisModification();
158   }
159 }
160
161 //=============================================================================
162 void BLSURFPlugin_Hypothesis::SetPhyMin(double theMinSize)
163 {
164   if (theMinSize != _phyMin) {
165     _phyMin = theMinSize;
166     NotifySubMeshesHypothesisModification();
167   }
168 }
169
170 //=============================================================================
171 void BLSURFPlugin_Hypothesis::SetPhyMax(double theMaxSize)
172 {
173   if (theMaxSize != _phyMax) {
174     _phyMax = theMaxSize;
175     NotifySubMeshesHypothesisModification();
176   }
177 }
178
179
180 //=============================================================================
181 void BLSURFPlugin_Hypothesis::SetGeoMin(double theMinSize)
182 {
183   if (theMinSize != _hgeoMin) {
184     _hgeoMin = theMinSize;
185     NotifySubMeshesHypothesisModification();
186   }
187 }
188
189 //=============================================================================
190 void BLSURFPlugin_Hypothesis::SetGeoMax(double theMaxSize)
191 {
192   if (theMaxSize != _hgeoMax) {
193     _hgeoMax = theMaxSize;
194     NotifySubMeshesHypothesisModification();
195   }
196 }
197
198 //=============================================================================
199 void BLSURFPlugin_Hypothesis::SetGeometricMesh(GeometricMesh theGeometricMesh)
200 {
201   if (theGeometricMesh != _geometricMesh) {
202     _geometricMesh = theGeometricMesh;
203     switch( _geometricMesh ) {
204       case DefaultGeom:
205       default:
206         _angleMeshS = GetDefaultAngleMeshS();
207         _gradation  = GetDefaultGradation();
208         break;
209       }
210     NotifySubMeshesHypothesisModification();
211   }
212 }
213
214 //=============================================================================
215 void BLSURFPlugin_Hypothesis::SetAngleMeshS(double theVal)
216 {
217   if (theVal != _angleMeshS) {
218     _angleMeshS = theVal;
219     NotifySubMeshesHypothesisModification();
220   }
221 }
222
223 //=============================================================================
224 void BLSURFPlugin_Hypothesis::SetAngleMeshC(double theVal)
225 {
226   if (theVal != _angleMeshC) {
227     _angleMeshC = theVal;
228     NotifySubMeshesHypothesisModification();
229   }
230 }
231
232 //=============================================================================
233 void BLSURFPlugin_Hypothesis::SetGradation(double theVal)
234 {
235   if (theVal != _gradation) {
236     _gradation = theVal;
237     NotifySubMeshesHypothesisModification();
238   }
239 }
240
241 //=============================================================================
242 void BLSURFPlugin_Hypothesis::SetQuadAllowed(bool theVal)
243 {
244   if (theVal != _quadAllowed) {
245     _quadAllowed = theVal;
246     NotifySubMeshesHypothesisModification();
247   }
248 }
249
250 //=============================================================================
251 void BLSURFPlugin_Hypothesis::SetDecimesh(bool theVal)
252 {
253   if (theVal != _decimesh) {
254     _decimesh = theVal;
255     NotifySubMeshesHypothesisModification();
256   }
257 }
258
259 //=============================================================================
260 void BLSURFPlugin_Hypothesis::SetVerbosity(int theVal)
261 {
262   if (theVal != _verb) {
263     _verb = theVal;
264     NotifySubMeshesHypothesisModification();
265   }
266 }
267 //=============================================================================
268 void BLSURFPlugin_Hypothesis::SetOptionValue(const std::string& optionName,
269                                              const std::string& optionValue)
270   throw (std::invalid_argument)
271 {
272   TOptionValues::iterator op_val = _option2value.find( optionName );
273   if ( op_val == _option2value.end() ) {
274     std::string msg = "Unknown BLSURF option: '" + optionName + "'";
275     throw std::invalid_argument(msg);
276   }
277   if ( op_val->second != optionValue ) {
278     const char* ptr = optionValue.c_str();
279     // strip white spaces
280     while ( ptr[0] == ' ' )
281       ptr++;
282     int i = strlen( ptr );
283     while ( i != 0 && ptr[i-1] == ' ')
284       i--;
285     // check value type
286     bool typeOk = true;
287     std::string typeName;
288     if ( i == 0 ) {
289       // empty string
290     }
291     else if ( _charOptions.find( optionName ) != _charOptions.end() ) {
292       // do not check strings
293     }
294     else if ( _doubleOptions.find( optionName ) != _doubleOptions.end() ) {
295       // check if value is double
296       char * endPtr;
297       strtod(ptr, &endPtr);
298       typeOk = ( ptr != endPtr );
299       typeName = "real";
300     }
301     else {
302       // check if value is int
303       char * endPtr;
304       strtol(ptr, &endPtr,10);
305       typeOk = ( ptr != endPtr );
306       typeName = "integer";
307     }
308     if ( !typeOk ) {
309       std::string msg = "Advanced option '" + optionName + "' = '" + optionValue +
310         "' but must be " + typeName;
311       throw std::invalid_argument(msg);
312     }
313     op_val->second = optionValue;
314     NotifySubMeshesHypothesisModification();
315   }
316 }
317
318 //=============================================================================
319 std::string BLSURFPlugin_Hypothesis::GetOptionValue(const std::string& optionName)
320   throw (std::invalid_argument)
321 {
322   TOptionValues::iterator op_val = _option2value.find( optionName );
323   if ( op_val == _option2value.end() ) {
324     std::string msg = "Unknown BLSURF option: <";
325     msg += optionName + ">";
326     throw std::invalid_argument(msg);
327   }
328   return op_val->second;
329 }
330
331 //=============================================================================
332 void BLSURFPlugin_Hypothesis::ClearOption(const std::string& optionName)
333 {
334   TOptionValues::iterator op_val = _option2value.find( optionName );
335   if ( op_val != _option2value.end() )
336     op_val->second.clear();
337 }
338
339
340 void  BLSURFPlugin_Hypothesis::SetSizeMapEntry(const std::string& entry,const std::string& sizeMap)
341 {
342   if (_sizeMap[entry].compare(sizeMap) != 0) {
343     _sizeMap[entry]=sizeMap;
344     NotifySubMeshesHypothesisModification();
345   }
346 }
347
348 std::string  BLSURFPlugin_Hypothesis::GetSizeMapEntry(const std::string& entry)
349 {
350  TSizeMap::iterator it  = _sizeMap.find( entry );
351  if ( it != _sizeMap.end() )
352    return it->second;
353  else
354    return "No_Such_Entry";
355 }
356
357 void BLSURFPlugin_Hypothesis::ClearEntry(const std::string& entry)
358 {
359  TSizeMap::iterator it  = _sizeMap.find( entry );
360  if ( it != _sizeMap.end() )
361    _sizeMap.erase(it);
362  else
363    std::cout<<"No_Such_Entry"<<std::endl;
364 }
365
366
367 void BLSURFPlugin_Hypothesis::ClearSizeMaps()
368 {
369   _sizeMap.clear();
370 }
371
372
373
374 //=============================================================================
375 std::ostream & BLSURFPlugin_Hypothesis::SaveTo(std::ostream & save)
376 {
377   save << " " << (int)_topology
378        << " " << (int)_physicalMesh
379        << " " << (int)_geometricMesh
380        << " " << _phySize
381        << " " << _angleMeshS   
382        << " " << _gradation     
383        << " " << (int)_quadAllowed 
384        << " " << (int)_decimesh;
385   save << " " << _phyMin
386        << " " << _phyMax
387        << " " << _angleMeshC
388        << " " << _hgeoMin
389        << " " << _hgeoMax
390        << " " << _verb;
391
392   TOptionValues::iterator op_val = _option2value.begin();
393   if (op_val != _option2value.end()) {
394     save << " " << "__OPTIONS_BEGIN__";
395     for ( ; op_val != _option2value.end(); ++op_val ) {
396       if ( !op_val->second.empty() )
397         save << " " << op_val->first
398              << " " << op_val->second << "%#"; // "%#" is a mark of value end
399     }
400     save << " " << "__OPTIONS_END__";
401   }
402
403   TSizeMap::iterator it_sm  = _sizeMap.begin();
404   if (it_sm != _sizeMap.end()) {
405     save << " " << "__SIZEMAP_BEGIN__";
406     for ( ; it_sm != _sizeMap.end(); ++it_sm ) {
407         save << " " << it_sm->first
408              << " " << it_sm->second << "%#"; // "%#" is a mark of value end
409     }
410     save << " " << "__SIZEMAP_END__";
411   }
412   
413   
414   return save;
415 }
416
417 //=============================================================================
418 std::istream & BLSURFPlugin_Hypothesis::LoadFrom(std::istream & load)
419 {
420   bool isOK = true;
421   int i;
422   double val;
423
424   isOK = (load >> i);
425   if (isOK)
426     _topology = (Topology) i;
427   else
428     load.clear(std::ios::badbit | load.rdstate());
429
430   isOK = (load >> i);
431   if (isOK)
432     _physicalMesh = (PhysicalMesh) i;
433   else
434     load.clear(std::ios::badbit | load.rdstate());
435
436   isOK = (load >> i);
437   if (isOK)
438     _geometricMesh = (GeometricMesh) i;
439   else
440     load.clear(std::ios::badbit | load.rdstate());
441
442   isOK = (load >> val);
443   if (isOK)
444     _phySize = val;
445   else
446     load.clear(std::ios::badbit | load.rdstate());
447
448   isOK = (load >> val);
449   if (isOK)
450     _angleMeshS = val;
451   else
452     load.clear(std::ios::badbit | load.rdstate());
453
454   isOK = (load >> val);
455   if (isOK)
456     _gradation = val;
457   else
458     load.clear(std::ios::badbit | load.rdstate());
459
460   isOK = (load >> i);
461   if (isOK)
462     _quadAllowed = (bool) i;
463   else
464     load.clear(std::ios::badbit | load.rdstate());
465
466   isOK = (load >> i);
467   if (isOK)
468     _decimesh = (bool) i;
469   else
470     load.clear(std::ios::badbit | load.rdstate());
471
472   isOK = (load >> val);
473   if (isOK)
474     _phyMin = val;
475   else
476     load.clear(std::ios::badbit | load.rdstate());
477   
478   isOK = (load >> val);
479   if (isOK)
480     _phyMax = val;
481   else
482     load.clear(std::ios::badbit | load.rdstate());
483   
484   isOK = (load >> val);
485   if (isOK)
486     _angleMeshC = val;
487   else
488     load.clear(std::ios::badbit | load.rdstate());
489   
490   isOK = (load >> val);
491   if (isOK)
492     _hgeoMin = val;
493   else
494     load.clear(std::ios::badbit | load.rdstate());
495   
496   isOK = (load >> val);
497   if (isOK)
498     _hgeoMax = val;
499   else
500     load.clear(std::ios::badbit | load.rdstate());
501   
502   isOK = (load >> i);
503   if (isOK)
504     _verb = i;
505   else
506     load.clear(std::ios::badbit | load.rdstate());
507
508   std::string option_or_sm;
509   bool hasOptions = false;
510   bool hasSizeMap = false;
511
512   isOK = (load >> option_or_sm);
513   if (isOK)
514     if (option_or_sm == "__OPTIONS_BEGIN__")
515       hasOptions = true;
516     else if (option_or_sm == "__SIZEMAP_BEGIN__")
517       hasSizeMap = true;
518
519   std::string optName, optValue;
520   while (isOK && hasOptions) {
521     isOK = (load >> optName);
522     if (isOK) {
523       if (optName == "__OPTIONS_END__")
524         break;
525       isOK = (load >> optValue);
526     }
527     if (isOK) {
528       std::string & value = _option2value[ optName ];
529       value = optValue;
530       int len = value.size();
531       // continue reading until "%#" encountered
532       while ( value[len-1] != '#' || value[len-2] != '%' )
533       {
534         isOK = (load >> optValue);
535         if (isOK) {
536           value += " ";
537           value += optValue;
538           len = value.size();
539         }
540         else {
541           break;
542         }
543       }
544       value[ len-2 ] = '\0'; //cut off "%#"
545     }
546   }
547
548   if (hasOptions) {
549     isOK = (load >> option_or_sm);
550     if (isOK && option_or_sm == "__SIZEMAP_BEGIN__")
551       hasSizeMap = true;
552   }
553
554   std::string smEntry, smValue;
555   while (isOK && hasSizeMap) {
556     isOK = (load >> smEntry);
557     if (isOK) {
558       if (smEntry == "__SIZEMAP_END__")
559         break;
560       isOK = (load >> smValue);
561     }
562     if (isOK) {
563       std::string & value2 = _sizeMap[ smEntry ];
564       value2 = smValue;
565       int len2= value2.size();
566       // continue reading until "%#" encountered
567       while ( value2[len2-1] != '#' || value2[len2-2] != '%' )
568       {
569         isOK = (load >> smValue);
570         if (isOK) {
571           value2 += " ";
572           value2 += smValue;
573           len2 = value2.size();
574         }
575         else {
576           break;
577         }
578       }
579       value2[ len2-2 ] = '\0'; //cut off "%#"
580     }
581   }
582
583   return load;
584 }
585
586 //=============================================================================
587 std::ostream & operator <<(std::ostream & save, BLSURFPlugin_Hypothesis & hyp)
588 {
589   return hyp.SaveTo( save );
590 }
591
592 //=============================================================================
593 std::istream & operator >>(std::istream & load, BLSURFPlugin_Hypothesis & hyp)
594 {
595   return hyp.LoadFrom( load );
596 }
597
598 //================================================================================
599 /*!
600  * \brief Does nothing
601  */
602 //================================================================================
603
604 bool BLSURFPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh*   theMesh,
605                                                       const TopoDS_Shape& theShape)
606 {
607   return false;
608 }
609
610 //=============================================================================
611 /*!
612  * \brief Initialize my parameter values by default parameters.
613  *  \retval bool - true if parameter values have been successfully defined
614  */
615 //=============================================================================
616
617 bool BLSURFPlugin_Hypothesis::SetParametersByDefaults(const TDefaults&   dflts,
618                                                       const SMESH_Mesh* theMesh)
619 {
620   return bool( _phySize = dflts._elemLength );
621 }
622
623 //=============================================================================
624 BLSURFPlugin_Hypothesis::Topology BLSURFPlugin_Hypothesis::GetDefaultTopology()
625 {
626   return FromCAD;
627 }
628
629 //=============================================================================
630 BLSURFPlugin_Hypothesis::PhysicalMesh BLSURFPlugin_Hypothesis::GetDefaultPhysicalMesh()
631 {
632   return PhysicalUserDefined;
633 }
634
635 //=============================================================================
636 double BLSURFPlugin_Hypothesis::GetDefaultPhySize()
637 {
638   return 10;
639 }
640
641 //======================================================================
642 double BLSURFPlugin_Hypothesis::GetDefaultMaxSize()
643 {
644   return undefinedDouble(); // 1e+4;
645 }
646
647 //======================================================================
648 double BLSURFPlugin_Hypothesis::GetDefaultMinSize()
649 {
650   return undefinedDouble(); //1e-4;
651 }
652
653 //======================================================================
654 BLSURFPlugin_Hypothesis::GeometricMesh BLSURFPlugin_Hypothesis::GetDefaultGeometricMesh()
655 {
656   return DefaultGeom;
657 }
658
659 //=============================================================================
660 double BLSURFPlugin_Hypothesis::GetDefaultAngleMeshS()
661 {
662   return 8;
663 }
664
665 //=============================================================================
666 double BLSURFPlugin_Hypothesis::GetDefaultGradation()
667 {
668   return 1.1;
669 }
670
671 //=============================================================================
672 bool BLSURFPlugin_Hypothesis::GetDefaultQuadAllowed()
673 {
674   return false;
675 }
676
677 //=============================================================================
678 bool BLSURFPlugin_Hypothesis::GetDefaultDecimesh()
679 {
680   return false;
681 }