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