Salome HOME
e3120bdf1bf5a34afc640d4ba8a21c554c7b1bea
[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 //           Size maps developement: Nicolas GEIMER (OCC) & Gilles DAVID (EURIWARE)
24 // ---
25 //
26 #include <BLSURFPlugin_Hypothesis.hxx>
27 #include <utilities.h>
28 #include <cstring>
29
30 //=============================================================================
31 BLSURFPlugin_Hypothesis::BLSURFPlugin_Hypothesis (int hypId, int studyId,
32                                                   SMESH_Gen * gen)
33   : SMESH_Hypothesis(hypId, studyId, gen),
34     _topology(GetDefaultTopology()),
35     _physicalMesh(GetDefaultPhysicalMesh()),
36     _phySize(GetDefaultPhySize()),
37     _phyMax(GetDefaultMaxSize()),
38     _phyMin(GetDefaultMinSize()),
39     _hgeoMax(GetDefaultMaxSize()),
40     _hgeoMin(GetDefaultMinSize()),
41     _geometricMesh(GetDefaultGeometricMesh()),
42     _angleMeshS(GetDefaultAngleMeshS()),
43     _angleMeshC(GetDefaultAngleMeshC()),
44     _gradation(GetDefaultGradation()),
45     _quadAllowed(GetDefaultQuadAllowed()),
46     _decimesh(GetDefaultDecimesh()),
47     _verb( GetDefaultVerbosity() )
48 {
49   _name = "BLSURF_Parameters";
50   _param_algo_dim = 2;
51
52   // to desable writing boundaries
53   //_phyMin = _phyMax = _hgeoMin = _hgeoMax = undefinedDouble();
54   
55
56   const char* intOptionNames[] = {
57     "addsurf_ivertex",
58     "background",
59     "CheckAdjacentEdges",
60     "CheckCloseEdges",
61     "CheckWellDefined",
62     "coiter",
63     "communication",
64     "decim",
65     "export_flag",
66     "file_h",
67     "frontal",
68     "gridnu",
69     "gridnv",
70     "hinterpol_flag",
71     "hmean_flag",
72     "intermedfile",
73     "memory",
74     "normals",
75     "optim",
76     "pardom_flag",
77     "pinch",
78     "refs",
79     "rigid",
80     "surforient",
81     "tconf",
82     "topo_collapse",
83     "" // mark of end
84   };
85   const char* doubleOptionNames[] = {
86     "addsurf_angle",
87     "addsurf_R",
88     "addsurf_H",
89     "addsurf_FG",
90     "addsurf_r",
91     "addsurf_PA",
92     "angle_compcurv",
93     "angle_ridge",
94     "CoefRectangle",
95     "eps_collapse",
96     "eps_ends",
97     "eps_pardom",
98     "LSS",
99     "topo_eps1",
100     "topo_eps2",
101     "" // mark of end
102   };
103   const char* charOptionNames[] = {
104     "export_format",
105     "export_option",
106     "import_option",
107     "prefix",
108     "" // mark of end
109   };
110
111   int i = 0;
112   while ( intOptionNames[i][0] )
113     _option2value[ intOptionNames[i++] ].clear();
114
115   i = 0;
116   while ( doubleOptionNames[i][0] ) {
117     _doubleOptions.insert( doubleOptionNames[i] );
118     _option2value[ doubleOptionNames[i++] ].clear();
119   }
120   i = 0;
121   while ( charOptionNames[i][0] ) {
122     _charOptions.insert( charOptionNames[i] );
123     _option2value[ charOptionNames[i++] ].clear();
124   }
125   
126   _sizeMap.clear();
127 }
128
129 //=============================================================================
130 void BLSURFPlugin_Hypothesis::SetTopology(Topology theTopology)
131 {
132   if (theTopology != _topology) {
133     _topology = theTopology;
134     NotifySubMeshesHypothesisModification();
135   }
136 }
137
138 //=============================================================================
139 void BLSURFPlugin_Hypothesis::SetPhysicalMesh(PhysicalMesh thePhysicalMesh)
140 {
141   if (thePhysicalMesh != _physicalMesh) {
142     _physicalMesh = thePhysicalMesh;
143     switch( _physicalMesh ) {
144       case DefaultSize:
145       default:
146         _phySize = GetDefaultPhySize();
147         break;
148       }
149     NotifySubMeshesHypothesisModification();
150   }
151 }
152
153 //=============================================================================
154 void BLSURFPlugin_Hypothesis::SetPhySize(double theVal)
155 {
156   if (theVal != _phySize) {
157     _phySize = theVal;
158     NotifySubMeshesHypothesisModification();
159   }
160 }
161
162 //=============================================================================
163 void BLSURFPlugin_Hypothesis::SetPhyMin(double theMinSize)
164 {
165   if (theMinSize != _phyMin) {
166     _phyMin = theMinSize;
167     NotifySubMeshesHypothesisModification();
168   }
169 }
170
171 //=============================================================================
172 void BLSURFPlugin_Hypothesis::SetPhyMax(double theMaxSize)
173 {
174   if (theMaxSize != _phyMax) {
175     _phyMax = theMaxSize;
176     NotifySubMeshesHypothesisModification();
177   }
178 }
179
180
181 //=============================================================================
182 void BLSURFPlugin_Hypothesis::SetGeoMin(double theMinSize)
183 {
184   if (theMinSize != _hgeoMin) {
185     _hgeoMin = theMinSize;
186     NotifySubMeshesHypothesisModification();
187   }
188 }
189
190 //=============================================================================
191 void BLSURFPlugin_Hypothesis::SetGeoMax(double theMaxSize)
192 {
193   if (theMaxSize != _hgeoMax) {
194     _hgeoMax = theMaxSize;
195     NotifySubMeshesHypothesisModification();
196   }
197 }
198
199 //=============================================================================
200 void BLSURFPlugin_Hypothesis::SetGeometricMesh(GeometricMesh theGeometricMesh)
201 {
202   if (theGeometricMesh != _geometricMesh) {
203     _geometricMesh = theGeometricMesh;
204     switch( _geometricMesh ) {
205       case DefaultGeom:
206       default:
207         _angleMeshS = GetDefaultAngleMeshS();
208         _gradation  = GetDefaultGradation();
209         break;
210       }
211     NotifySubMeshesHypothesisModification();
212   }
213 }
214
215 //=============================================================================
216 void BLSURFPlugin_Hypothesis::SetAngleMeshS(double theVal)
217 {
218   if (theVal != _angleMeshS) {
219     _angleMeshS = theVal;
220     NotifySubMeshesHypothesisModification();
221   }
222 }
223
224 //=============================================================================
225 void BLSURFPlugin_Hypothesis::SetAngleMeshC(double theVal)
226 {
227   if (theVal != _angleMeshC) {
228     _angleMeshC = theVal;
229     NotifySubMeshesHypothesisModification();
230   }
231 }
232
233 //=============================================================================
234 void BLSURFPlugin_Hypothesis::SetGradation(double theVal)
235 {
236   if (theVal != _gradation) {
237     _gradation = theVal;
238     NotifySubMeshesHypothesisModification();
239   }
240 }
241
242 //=============================================================================
243 void BLSURFPlugin_Hypothesis::SetQuadAllowed(bool theVal)
244 {
245   if (theVal != _quadAllowed) {
246     _quadAllowed = theVal;
247     NotifySubMeshesHypothesisModification();
248   }
249 }
250
251 //=============================================================================
252 void BLSURFPlugin_Hypothesis::SetDecimesh(bool theVal)
253 {
254   if (theVal != _decimesh) {
255     _decimesh = theVal;
256     NotifySubMeshesHypothesisModification();
257   }
258 }
259
260 //=============================================================================
261 void BLSURFPlugin_Hypothesis::SetVerbosity(int theVal)
262 {
263   if (theVal != _verb) {
264     _verb = theVal;
265     NotifySubMeshesHypothesisModification();
266   }
267 }
268 //=============================================================================
269 void BLSURFPlugin_Hypothesis::SetOptionValue(const std::string& optionName,
270                                              const std::string& optionValue)
271   throw (std::invalid_argument)
272 {
273   TOptionValues::iterator op_val = _option2value.find( optionName );
274   if ( op_val == _option2value.end() ) {
275     std::string msg = "Unknown BLSURF option: '" + optionName + "'";
276     throw std::invalid_argument(msg);
277   }
278   if ( op_val->second != optionValue ) {
279     const char* ptr = optionValue.c_str();
280     // strip white spaces
281     while ( ptr[0] == ' ' )
282       ptr++;
283     int i = strlen( ptr );
284     while ( i != 0 && ptr[i-1] == ' ')
285       i--;
286     // check value type
287     bool typeOk = true;
288     std::string typeName;
289     if ( i == 0 ) {
290       // empty string
291     }
292     else if ( _charOptions.find( optionName ) != _charOptions.end() ) {
293       // do not check strings
294     }
295     else if ( _doubleOptions.find( optionName ) != _doubleOptions.end() ) {
296       // check if value is double
297       char * endPtr;
298       strtod(ptr, &endPtr);
299       typeOk = ( ptr != endPtr );
300       typeName = "real";
301     }
302     else {
303       // check if value is int
304       char * endPtr;
305       strtol(ptr, &endPtr,10);
306       typeOk = ( ptr != endPtr );
307       typeName = "integer";
308     }
309     if ( !typeOk ) {
310       std::string msg = "Advanced option '" + optionName + "' = '" + optionValue +
311         "' but must be " + typeName;
312       throw std::invalid_argument(msg);
313     }
314     op_val->second = optionValue;
315     NotifySubMeshesHypothesisModification();
316   }
317 }
318
319 //=============================================================================
320 std::string BLSURFPlugin_Hypothesis::GetOptionValue(const std::string& optionName)
321   throw (std::invalid_argument)
322 {
323   TOptionValues::iterator op_val = _option2value.find( optionName );
324   if ( op_val == _option2value.end() ) {
325     std::string msg = "Unknown BLSURF option: <";
326     msg += optionName + ">";
327     throw std::invalid_argument(msg);
328   }
329   return op_val->second;
330 }
331
332 //=============================================================================
333 void BLSURFPlugin_Hypothesis::ClearOption(const std::string& optionName)
334 {
335   TOptionValues::iterator op_val = _option2value.find( optionName );
336   if ( op_val != _option2value.end() )
337     op_val->second.clear();
338 }
339
340
341 void  BLSURFPlugin_Hypothesis::SetSizeMapEntry(const std::string& entry,const std::string& sizeMap)
342 {
343   if (_sizeMap[entry].compare(sizeMap) != 0) {
344     _sizeMap[entry]=sizeMap;
345     NotifySubMeshesHypothesisModification();
346   }
347 }
348
349 std::string  BLSURFPlugin_Hypothesis::GetSizeMapEntry(const std::string& entry)
350 {
351  TSizeMap::iterator it  = _sizeMap.find( entry );
352  if ( it != _sizeMap.end() )
353    return it->second;
354  else
355    return "No_Such_Entry";
356 }
357
358 void  BLSURFPlugin_Hypothesis::SetAttractorEntry(const std::string& entry,const std::string& attractor)
359 {
360   if (_attractors[entry].compare(attractor) != 0) {
361     _attractors[entry]=attractor;
362     NotifySubMeshesHypothesisModification();
363   }
364 }
365
366 std::string  BLSURFPlugin_Hypothesis::GetAttractorEntry(const std::string& entry)
367 {
368  TSizeMap::iterator it  = _attractors.find( entry );
369  if ( it != _attractors.end() )
370    return it->second;
371  else
372    return "No_Such_Entry";
373 }
374
375
376 void BLSURFPlugin_Hypothesis::ClearEntry(const std::string& entry)
377 {
378  TSizeMap::iterator it  = _sizeMap.find( entry );
379  if ( it != _sizeMap.end() ) {
380    _sizeMap.erase(it);
381    NotifySubMeshesHypothesisModification();
382  }
383  else {
384    TSizeMap::iterator itAt  = _attractors.find( entry );
385    if ( itAt != _attractors.end() ) {
386      _attractors.erase(itAt);
387      NotifySubMeshesHypothesisModification();
388    }
389    else
390      std::cout<<"No_Such_Entry"<<std::endl;
391  }
392 }
393
394
395 void BLSURFPlugin_Hypothesis::ClearSizeMaps()
396 {
397   _sizeMap.clear();
398   _attractors.clear();
399 }
400
401
402
403 //=============================================================================
404 std::ostream & BLSURFPlugin_Hypothesis::SaveTo(std::ostream & save)
405 {
406   save << " " << (int)_topology
407        << " " << (int)_physicalMesh
408        << " " << (int)_geometricMesh
409        << " " << _phySize
410        << " " << _angleMeshS   
411        << " " << _gradation     
412        << " " << (int)_quadAllowed 
413        << " " << (int)_decimesh;
414   save << " " << _phyMin
415        << " " << _phyMax
416        << " " << _angleMeshC
417        << " " << _hgeoMin
418        << " " << _hgeoMax
419        << " " << _verb;
420
421   TOptionValues::iterator op_val = _option2value.begin();
422   if (op_val != _option2value.end()) {
423     save << " " << "__OPTIONS_BEGIN__";
424     for ( ; op_val != _option2value.end(); ++op_val ) {
425       if ( !op_val->second.empty() )
426         save << " " << op_val->first
427              << " " << op_val->second << "%#"; // "%#" is a mark of value end
428     }
429     save << " " << "__OPTIONS_END__";
430   }
431
432   TSizeMap::iterator it_sm  = _sizeMap.begin();
433   if (it_sm != _sizeMap.end()) {
434     save << " " << "__SIZEMAP_BEGIN__";
435     for ( ; it_sm != _sizeMap.end(); ++it_sm ) {
436         save << " " << it_sm->first
437              << " " << it_sm->second << "%#"; // "%#" is a mark of value end
438     }
439     save << " " << "__SIZEMAP_END__";
440   }
441
442   TSizeMap::iterator it_at  = _attractors.begin();
443   if (it_at != _attractors.end()) {
444     save << " " << "__ATTRACTORS_BEGIN__";
445     for ( ; it_at != _attractors.end(); ++it_at ) {
446         save << " " << it_at->first
447              << " " << it_at->second << "%#"; // "%#" is a mark of value end
448     }
449     save << " " << "__ATTRACTORS_END__";
450   }
451   
452   
453   return save;
454 }
455
456 //=============================================================================
457 std::istream & BLSURFPlugin_Hypothesis::LoadFrom(std::istream & load)
458 {
459   bool isOK = true;
460   int i;
461   double val;
462
463   isOK = (load >> i);
464   if (isOK)
465     _topology = (Topology) i;
466   else
467     load.clear(std::ios::badbit | load.rdstate());
468
469   isOK = (load >> i);
470   if (isOK)
471     _physicalMesh = (PhysicalMesh) i;
472   else
473     load.clear(std::ios::badbit | load.rdstate());
474
475   isOK = (load >> i);
476   if (isOK)
477     _geometricMesh = (GeometricMesh) i;
478   else
479     load.clear(std::ios::badbit | load.rdstate());
480
481   isOK = (load >> val);
482   if (isOK)
483     _phySize = val;
484   else
485     load.clear(std::ios::badbit | load.rdstate());
486
487   isOK = (load >> val);
488   if (isOK)
489     _angleMeshS = val;
490   else
491     load.clear(std::ios::badbit | load.rdstate());
492
493   isOK = (load >> val);
494   if (isOK)
495     _gradation = val;
496   else
497     load.clear(std::ios::badbit | load.rdstate());
498
499   isOK = (load >> i);
500   if (isOK)
501     _quadAllowed = (bool) i;
502   else
503     load.clear(std::ios::badbit | load.rdstate());
504
505   isOK = (load >> i);
506   if (isOK)
507     _decimesh = (bool) i;
508   else
509     load.clear(std::ios::badbit | load.rdstate());
510
511   isOK = (load >> val);
512   if (isOK)
513     _phyMin = val;
514   else
515     load.clear(std::ios::badbit | load.rdstate());
516   
517   isOK = (load >> val);
518   if (isOK)
519     _phyMax = val;
520   else
521     load.clear(std::ios::badbit | load.rdstate());
522   
523   isOK = (load >> val);
524   if (isOK)
525     _angleMeshC = val;
526   else
527     load.clear(std::ios::badbit | load.rdstate());
528   
529   isOK = (load >> val);
530   if (isOK)
531     _hgeoMin = val;
532   else
533     load.clear(std::ios::badbit | load.rdstate());
534   
535   isOK = (load >> val);
536   if (isOK)
537     _hgeoMax = val;
538   else
539     load.clear(std::ios::badbit | load.rdstate());
540   
541   isOK = (load >> i);
542   if (isOK)
543     _verb = i;
544   else
545     load.clear(std::ios::badbit | load.rdstate());
546
547   std::string option_or_sm;
548   bool hasOptions = false;
549   bool hasSizeMap = false;
550   bool hasAttractor = false;
551
552   isOK = (load >> option_or_sm);
553   if (isOK)
554     if (option_or_sm == "__OPTIONS_BEGIN__")
555       hasOptions = true;
556     else if (option_or_sm == "__SIZEMAP_BEGIN__")
557       hasSizeMap = true;
558     else if (option_or_sm == "__ATTRACTORS_BEGIN__")
559       hasAttractor = true;
560
561   std::string optName, optValue;
562   while (isOK && hasOptions) {
563     isOK = (load >> optName);
564     if (isOK) {
565       if (optName == "__OPTIONS_END__")
566         break;
567       isOK = (load >> optValue);
568     }
569     if (isOK) {
570       std::string & value = _option2value[ optName ];
571       value = optValue;
572       int len = value.size();
573       // continue reading until "%#" encountered
574       while ( value[len-1] != '#' || value[len-2] != '%' )
575       {
576         isOK = (load >> optValue);
577         if (isOK) {
578           value += " ";
579           value += optValue;
580           len = value.size();
581         }
582         else {
583           break;
584         }
585       }
586       value[ len-2 ] = '\0'; //cut off "%#"
587     }
588   }
589
590   if (hasOptions) {
591     isOK = (load >> option_or_sm);
592     if (isOK)
593       if (option_or_sm == "__SIZEMAP_BEGIN__")
594         hasSizeMap = true;
595       else if (option_or_sm == "__ATTRACTORS_BEGIN__")
596         hasAttractor = true;
597   }
598
599   std::string smEntry, smValue;
600   while (isOK && hasSizeMap) {
601     isOK = (load >> smEntry);
602     if (isOK) {
603       if (smEntry == "__SIZEMAP_END__")
604         break;
605       isOK = (load >> smValue);
606     }
607     if (isOK) {
608       std::string & value2 = _sizeMap[ smEntry ];
609       value2 = smValue;
610       int len2= value2.size();
611       // continue reading until "%#" encountered
612       while ( value2[len2-1] != '#' || value2[len2-2] != '%' )
613       {
614         isOK = (load >> smValue);
615         if (isOK) {
616           value2 += " ";
617           value2 += smValue;
618           len2 = value2.size();
619         }
620         else {
621           break;
622         }
623       }
624       value2[ len2-2 ] = '\0'; //cut off "%#"
625     }
626   }
627
628   if (hasSizeMap) {
629     isOK = (load >> option_or_sm);
630     if (isOK && option_or_sm == "__ATTRACTORS_BEGIN__")
631       hasAttractor = true;
632   }
633
634   std::string atEntry, atValue;
635   while (isOK && hasAttractor) {
636     isOK = (load >> atEntry);
637     if (isOK) {
638       if (atEntry == "__ATTRACTORS_END__")
639         break;
640       isOK = (load >> atValue);
641     }
642     if (isOK) {
643       std::string & value3 = _attractors[ atEntry ];
644       value3 = atValue;
645       int len3= value3.size();
646       // continue reading until "%#" encountered
647       while ( value3[len3-1] != '#' || value3[len3-2] != '%' )
648       {
649         isOK = (load >> atValue);
650         if (isOK) {
651           value3 += " ";
652           value3 += atValue;
653           len3 = value3.size();
654         }
655         else {
656           break;
657         }
658       }
659       value3[ len3-2 ] = '\0'; //cut off "%#"
660     }
661   }
662
663   return load;
664 }
665
666 //=============================================================================
667 std::ostream & operator <<(std::ostream & save, BLSURFPlugin_Hypothesis & hyp)
668 {
669   return hyp.SaveTo( save );
670 }
671
672 //=============================================================================
673 std::istream & operator >>(std::istream & load, BLSURFPlugin_Hypothesis & hyp)
674 {
675   return hyp.LoadFrom( load );
676 }
677
678 //================================================================================
679 /*!
680  * \brief Does nothing
681  */
682 //================================================================================
683
684 bool BLSURFPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh*   theMesh,
685                                                       const TopoDS_Shape& theShape)
686 {
687   return false;
688 }
689
690 //=============================================================================
691 /*!
692  * \brief Initialize my parameter values by default parameters.
693  *  \retval bool - true if parameter values have been successfully defined
694  */
695 //=============================================================================
696
697 bool BLSURFPlugin_Hypothesis::SetParametersByDefaults(const TDefaults&   dflts,
698                                                       const SMESH_Mesh* theMesh)
699 {
700   return bool( _phySize = dflts._elemLength );
701 }
702
703 //=============================================================================
704 BLSURFPlugin_Hypothesis::Topology BLSURFPlugin_Hypothesis::GetDefaultTopology()
705 {
706   return FromCAD;
707 }
708
709 //=============================================================================
710 BLSURFPlugin_Hypothesis::PhysicalMesh BLSURFPlugin_Hypothesis::GetDefaultPhysicalMesh()
711 {
712   return PhysicalUserDefined;
713 }
714
715 //=============================================================================
716 double BLSURFPlugin_Hypothesis::GetDefaultPhySize()
717 {
718   return 10;
719 }
720
721 //======================================================================
722 double BLSURFPlugin_Hypothesis::GetDefaultMaxSize()
723 {
724   return undefinedDouble(); // 1e+4;
725 }
726
727 //======================================================================
728 double BLSURFPlugin_Hypothesis::GetDefaultMinSize()
729 {
730   return undefinedDouble(); //1e-4;
731 }
732
733 //======================================================================
734 BLSURFPlugin_Hypothesis::GeometricMesh BLSURFPlugin_Hypothesis::GetDefaultGeometricMesh()
735 {
736   return DefaultGeom;
737 }
738
739 //=============================================================================
740 double BLSURFPlugin_Hypothesis::GetDefaultAngleMeshS()
741 {
742   return 8;
743 }
744
745 //=============================================================================
746 double BLSURFPlugin_Hypothesis::GetDefaultGradation()
747 {
748   return 1.1;
749 }
750
751 //=============================================================================
752 bool BLSURFPlugin_Hypothesis::GetDefaultQuadAllowed()
753 {
754   return false;
755 }
756
757 //=============================================================================
758 bool BLSURFPlugin_Hypothesis::GetDefaultDecimesh()
759 {
760   return false;
761 }