Salome HOME
7d2be5b279286d74d0ee9d34537c56925ad8e0fa
[plugins/blsurfplugin.git] / src / BLSURFPlugin / BLSURFPlugin_Hypothesis.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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 // ---
21 // File    : BLSURFPlugin_Hypothesis.cxx
22 // Authors : Francis KLOSS (OCC) & Patrick LAUG (INRIA) & Lioka RAZAFINDRAZAKA (CEA)
23 //           & Aurelien ALLEAUME (DISTENE)
24 //           Size maps development: Nicolas GEIMER (OCC) & Gilles DAVID (EURIWARE)
25 // ---
26 //
27 #include "BLSURFPlugin_Hypothesis.hxx"
28 #include "BLSURFPlugin_Attractor.hxx"
29 #include "SMESH_Gen_i.hxx"
30 #include <utilities.h>
31 #include <cstring>
32 #include <iostream>
33 #include <sstream>
34
35 // cascade include
36 #include "ShapeAnalysis.hxx"
37
38 // CORBA includes
39 #include CORBA_CLIENT_HEADER(SALOMEDS)
40 #include CORBA_CLIENT_HEADER(GEOM_Gen)
41
42 namespace
43 {
44   struct GET_DEFAULT // struct used to get default value from GetOptionValue()
45   {
46     bool isDefault;
47     operator bool* () { return &isDefault; }
48   };
49 }
50
51 //=============================================================================
52 BLSURFPlugin_Hypothesis::BLSURFPlugin_Hypothesis(int hypId, int studyId, SMESH_Gen * gen) :
53   SMESH_Hypothesis(hypId, studyId, gen), 
54   _physicalMesh(GetDefaultPhysicalMesh()),
55   _geometricMesh(GetDefaultGeometricMesh()),
56   _phySize(GetDefaultPhySize()),
57   _phySizeRel(GetDefaultPhySizeRel()),
58   _minSize(GetDefaultMinSize()),
59   _maxSize(GetDefaultMaxSize()),
60   _minSizeRel(GetDefaultMinSizeRel()),
61   _maxSizeRel(GetDefaultMaxSizeRel()),
62   _useGradation(GetDefaultUseGradation()),
63   _gradation(GetDefaultGradation()),
64   _useVolumeGradation(GetDefaultUseVolumeGradation()),
65   _volumeGradation(GetDefaultVolumeGradation()),
66   _quadAllowed(GetDefaultQuadAllowed()),
67   _angleMesh(GetDefaultAngleMesh()),
68   _chordalError(GetDefaultChordalError()), 
69   _anisotropic(GetDefaultAnisotropic()),
70   _anisotropicRatio(GetDefaultAnisotropicRatio()),
71   _removeTinyEdges(GetDefaultRemoveTinyEdges()),
72   _tinyEdgeLength(GetDefaultTinyEdgeLength()),
73   _optimiseTinyEdges(GetDefaultOptimiseTinyEdges()),
74   _tinyEdgeOptimisationLength(GetDefaultTinyEdgeOptimisationLength()),
75   _correctSurfaceIntersec(GetDefaultCorrectSurfaceIntersection()),
76   _corrSurfaceIntersCost(GetDefaultCorrectSurfaceIntersectionMaxCost()),
77   _badElementRemoval(GetDefaultBadElementRemoval()),
78   _badElementAspectRatio(GetDefaultBadElementAspectRatio()),
79   _optimizeMesh(GetDefaultOptimizeMesh()),
80   _quadraticMesh(GetDefaultQuadraticMesh()),
81   _verb(GetDefaultVerbosity()),
82   _topology(GetDefaultTopology()),
83   _preCADMergeEdges(GetDefaultPreCADMergeEdges()),
84   _preCADRemoveTinyUVEdges(GetDefaultPreCADRemoveTinyUVEdges()),
85   _preCADRemoveDuplicateCADFaces(GetDefaultPreCADRemoveDuplicateCADFaces()),
86   _preCADProcess3DTopology(GetDefaultPreCADProcess3DTopology()),
87   _preCADDiscardInput(GetDefaultPreCADDiscardInput()),
88   _sizeMap(GetDefaultSizeMap()),
89   _attractors(GetDefaultSizeMap()),
90   _classAttractors(GetDefaultAttractorMap()),
91   _faceEntryEnfVertexListMap(GetDefaultFaceEntryEnfVertexListMap()),
92   _enfVertexList(GetDefaultEnfVertexList()),
93   _faceEntryCoordsListMap(GetDefaultFaceEntryCoordsListMap()),
94   _coordsEnfVertexMap(GetDefaultCoordsEnfVertexMap()),
95   _faceEntryEnfVertexEntryListMap(GetDefaultFaceEntryEnfVertexEntryListMap()),
96   _enfVertexEntryEnfVertexMap(GetDefaultEnfVertexEntryEnfVertexMap()),
97   _groupNameNodeIDMap(GetDefaultGroupNameNodeIDMap()),
98   _enforcedInternalVerticesAllFaces(GetDefaultInternalEnforcedVertex()),
99   _preCadFacesPeriodicityVector(GetDefaultPreCadFacesPeriodicityVector()),
100   _preCadEdgesPeriodicityVector(GetDefaultPreCadEdgesPeriodicityVector()),
101   _GMFFileName(GetDefaultGMFFile())
102 {
103   _name = GetHypType();
104   _param_algo_dim = 2;
105   
106 //   _GMFFileMode = false; // GMF ascii mode
107
108   // Advanced options with their defaults according to MG User Manual
109
110   const char* boolOptionNames[] = {         "enforce_cad_edge_sizes",                   // default = 0
111                                             // "correct_surface_intersections",            // default = 1
112                                             // "create_tag_on_collision",                  // default = 1
113                                             "jacobian_rectification_respect_geometry",  // default = 1
114                                             "rectify_jacobian",                         // default = 1
115                                             "respect_geometry",                         // default = 1
116                                             // "optimise_tiny_edges",                      // default = 0
117                                             // "remove_duplicate_cad_faces",               // default = 1
118                                             "tiny_edge_avoid_surface_intersections",    // default = 1
119                                             // "tiny_edge_respect_geometry",               // default = 0
120                                             "" // mark of end
121       };
122
123   const char* intOptionNames[] = {          "max_number_of_points_per_patch",           // default = 100000
124                                             "" // mark of end
125       };
126   const char* doubleOptionNames[] = {       // "surface_intersections_processing_max_cost",// default = 15
127                                             // "periodic_tolerance",                       // default = diag/100
128                                             // "volume_gradation",
129                                             // "tiny_edge_optimisation_length",            // default = diag * 1e-6
130                                             "" // mark of end
131       };
132   const char* charOptionNames[] = {         // "required_entities",                        // default = "respect"
133                                             // "tags",                                     // default = "respect"
134                                             "" // mark of end
135       };
136
137   // PreCAD advanced options
138   const char* preCADboolOptionNames[] = {   "closed_geometry",                          // default = 0
139                                             "discard_input_topology",                   // default = 0
140                                             "merge_edges",                              // default =  = 1
141                                             "remove_duplicate_cad_faces",               // default = 1
142                                             // "create_tag_on_collision",                  // default = 1
143                                             "debug",                                    // default = 0 
144                                             "process_3d_topology",                      // default = 1
145                                             // "remove_tiny_edges",                        // default = 0
146                                             "" // mark of end
147       };
148   const char* preCADintOptionNames[] = {    // "manifold_geometry",                        // default = 0
149                                             "" // mark of end
150       };
151   const char* preCADdoubleOptionNames[] = { "periodic_tolerance",                       // default = diag * 1e-5
152                                             "sewing_tolerance",                         // default = diag * 5e-4
153                                             // "tiny_edge_length",                         // default = diag * 1e-5
154                                             "" // mark of end
155       };
156   const char* preCADcharOptionNames[] = {   "required_entities",                        // default = "respect"
157                                             "tags",                                     // default = "respect"
158                                             "" // mark of end
159       };
160   
161   int i = 0;
162   while (boolOptionNames[i][0])
163   {
164     _boolOptions.insert( boolOptionNames[i] );
165     _option2value[boolOptionNames[i++]].clear();
166   }
167   i = 0;
168   while (preCADboolOptionNames[i][0])
169   {
170     _boolOptions.insert( preCADboolOptionNames[i] );
171     _preCADoption2value[preCADboolOptionNames[i++]].clear();
172   }
173   i = 0;
174   while (intOptionNames[i][0])
175     _option2value[intOptionNames[i++]].clear();
176   
177   i = 0;
178   while (preCADintOptionNames[i][0])
179     _preCADoption2value[preCADintOptionNames[i++]].clear();
180
181   i = 0;
182   while (doubleOptionNames[i][0]) {
183     _doubleOptions.insert(doubleOptionNames[i]);
184     _option2value[doubleOptionNames[i++]].clear();
185   }
186   i = 0;
187   while (preCADdoubleOptionNames[i][0]) {
188     _preCADdoubleOptions.insert(preCADdoubleOptionNames[i]);
189     _preCADoption2value[preCADdoubleOptionNames[i++]].clear();
190   }
191   i = 0;
192   while (charOptionNames[i][0]) {
193     _charOptions.insert(charOptionNames[i]);
194     _option2value[charOptionNames[i++]].clear();
195   }
196   i = 0;
197   while (preCADcharOptionNames[i][0]) {
198     _preCADcharOptions.insert(preCADcharOptionNames[i]);
199     _preCADoption2value[preCADcharOptionNames[i++]].clear();
200   }
201
202   // default values to be used while MG meshing
203
204   _defaultOptionValues["enforce_cad_edge_sizes"                 ] = "no";
205   _defaultOptionValues["jacobian_rectification_respect_geometry"] = "yes";
206   _defaultOptionValues["max_number_of_points_per_patch"         ] = "0";
207   _defaultOptionValues["rectify_jacobian"                       ] = "yes";
208   _defaultOptionValues["respect_geometry"                       ] = "yes";
209   _defaultOptionValues["tiny_edge_avoid_surface_intersections"  ] = "yes";
210   _defaultOptionValues["process_3d_topology"                    ] = "no";
211   _defaultOptionValues["closed_geometry"                        ] = "no";
212   _defaultOptionValues["debug"                                  ] = "no";
213   _defaultOptionValues["discard_input_topology"                 ] = "no";
214   _defaultOptionValues["merge_edges"                            ] = "no";
215   _defaultOptionValues["periodic_tolerance"                     ] = "1e-5*D";
216   _defaultOptionValues["remove_duplicate_cad_faces"             ] = "no";
217   _defaultOptionValues["required_entities"                      ] = "respect";
218   _defaultOptionValues["sewing_tolerance"                       ] = "5e-4*D";
219   _defaultOptionValues["tags"                                   ] = "respect";
220
221 #ifdef _DEBUG_
222   // check validity of option names of _defaultOptionValues
223   TOptionValues::iterator n2v = _defaultOptionValues.begin();
224   for ( ; n2v != _defaultOptionValues.end(); ++n2v )
225     ASSERT( _option2value.count( n2v->first ) || _preCADoption2value.count( n2v->first ));
226   ASSERT( _option2value.size() + _preCADoption2value.size() == _defaultOptionValues.size() );
227 #endif
228       
229   _sizeMap.clear();
230   _attractors.clear();
231   _faceEntryEnfVertexListMap.clear();
232   _enfVertexList.clear();
233   _faceEntryCoordsListMap.clear();
234   _coordsEnfVertexMap.clear();
235   _faceEntryEnfVertexEntryListMap.clear();
236   _enfVertexEntryEnfVertexMap.clear();
237   _groupNameNodeIDMap.clear();
238
239   /* TODO GROUPS
240    _groupNameEnfVertexListMap.clear();
241    _enfVertexGroupNameMap.clear();
242    */
243 }
244
245 TopoDS_Shape BLSURFPlugin_Hypothesis::entryToShape(std::string entry)
246 {
247   GEOM::GEOM_Object_var aGeomObj;
248   SMESH_Gen_i* smeshGen_i = SMESH_Gen_i::GetSMESHGen();
249   SALOMEDS::Study_ptr myStudy = smeshGen_i->GetCurrentStudy();
250   
251   TopoDS_Shape S = TopoDS_Shape();
252   SALOMEDS::SObject_var aSObj = myStudy->FindObjectID( entry.c_str() );
253   if (!aSObj->_is_nil() ) {
254     CORBA::Object_var obj = aSObj->GetObject();
255     aGeomObj = GEOM::GEOM_Object::_narrow(obj);
256     aSObj->UnRegister();
257   }
258   if ( !aGeomObj->_is_nil() )
259     S = smeshGen_i->GeomObjectToShape( aGeomObj.in() );
260   return S;
261 }
262
263 //=============================================================================
264 void BLSURFPlugin_Hypothesis::SetPhysicalMesh(PhysicalMesh thePhysicalMesh) {
265   if (thePhysicalMesh != _physicalMesh) {
266     _physicalMesh = thePhysicalMesh;
267     NotifySubMeshesHypothesisModification();
268   }
269 }
270
271 //=============================================================================
272 void BLSURFPlugin_Hypothesis::SetGeometricMesh(GeometricMesh theGeometricMesh) {
273   if (theGeometricMesh != _geometricMesh) {
274     _geometricMesh = theGeometricMesh;
275 //     switch (_geometricMesh) {
276 //       case DefaultGeom:
277 //       default:
278 //         _angleMesh = GetDefaultAngleMesh();
279 //         _gradation = GetDefaultGradation();
280 //         break;
281 //     }
282     NotifySubMeshesHypothesisModification();
283   }
284 }
285
286 //=============================================================================
287 void BLSURFPlugin_Hypothesis::SetPhySize(double theVal, bool isRelative) {
288   if ((theVal != _phySize) || (isRelative != _phySizeRel)) {
289     _phySizeRel = isRelative;
290     if (theVal == 0) {
291       _phySize = GetMaxSize();
292     }
293     else
294       _phySize = theVal;
295     NotifySubMeshesHypothesisModification();
296   }
297 }
298
299 //=============================================================================
300 void BLSURFPlugin_Hypothesis::SetMinSize(double theMinSize, bool isRelative) {
301   if ((theMinSize != _minSize) || (isRelative != _minSizeRel)) {
302     _minSizeRel = isRelative;
303     _minSize = theMinSize;
304     NotifySubMeshesHypothesisModification();
305   }
306 }
307
308 //=============================================================================
309 void BLSURFPlugin_Hypothesis::SetMaxSize(double theMaxSize, bool isRelative) {
310   if ((theMaxSize != _maxSize) || (isRelative != _maxSizeRel)) {
311     _maxSizeRel = isRelative;
312     _maxSize = theMaxSize;
313     NotifySubMeshesHypothesisModification();
314   }
315 }
316
317 //=============================================================================
318 void BLSURFPlugin_Hypothesis::SetUseGradation(bool theVal) {
319   if (theVal != _useGradation) {
320     _useGradation = theVal;
321     NotifySubMeshesHypothesisModification();
322   }
323 }
324
325 //=============================================================================
326 void BLSURFPlugin_Hypothesis::SetGradation(double theVal) {
327   _useGradation = ( theVal > 0 );
328   if (theVal != _gradation) {
329     _gradation = theVal;
330     NotifySubMeshesHypothesisModification();
331   }
332 }
333
334 //=============================================================================
335 void BLSURFPlugin_Hypothesis::SetUseVolumeGradation(bool theVal) {
336   if (theVal != _useVolumeGradation) {
337     _useVolumeGradation = theVal;
338     NotifySubMeshesHypothesisModification();
339   }
340 }
341
342 //=============================================================================
343 void BLSURFPlugin_Hypothesis::SetVolumeGradation(double theVal) {
344   _useVolumeGradation = ( theVal > 0 );
345   if (theVal != _volumeGradation) {
346     _volumeGradation = theVal;
347     NotifySubMeshesHypothesisModification();
348   }
349 }
350
351 //=============================================================================
352 void BLSURFPlugin_Hypothesis::SetQuadAllowed(bool theVal) {
353   if (theVal != _quadAllowed) {
354     _quadAllowed = theVal;
355     NotifySubMeshesHypothesisModification();
356   }
357 }
358
359 //=============================================================================
360 void BLSURFPlugin_Hypothesis::SetAngleMesh(double theVal) {
361   if (theVal != _angleMesh) {
362     _angleMesh = theVal;
363     NotifySubMeshesHypothesisModification();
364   }
365 }
366
367 //=============================================================================
368 void BLSURFPlugin_Hypothesis::SetChordalError(double theDistance) {
369   if (theDistance != _chordalError) {
370     _chordalError = theDistance;
371     NotifySubMeshesHypothesisModification();
372   }
373 }
374
375 //=============================================================================
376 void BLSURFPlugin_Hypothesis::SetAnisotropic(bool theVal) {
377   if (theVal != _anisotropic) {
378     _anisotropic = theVal;
379     NotifySubMeshesHypothesisModification();
380   }
381 }
382
383 //=============================================================================
384 void BLSURFPlugin_Hypothesis::SetAnisotropicRatio(double theVal) {
385   if (theVal != _anisotropicRatio) {
386     _anisotropicRatio = theVal;
387     NotifySubMeshesHypothesisModification();
388   }
389 }
390
391 //=============================================================================
392 void BLSURFPlugin_Hypothesis::SetRemoveTinyEdges(bool theVal) {
393   if (theVal != _removeTinyEdges) {
394     _removeTinyEdges = theVal;
395     NotifySubMeshesHypothesisModification();
396   }
397 }
398
399 //=============================================================================
400 void BLSURFPlugin_Hypothesis::SetTinyEdgeLength(double theVal) {
401   if (theVal != _tinyEdgeLength) {
402     _tinyEdgeLength = theVal;
403     NotifySubMeshesHypothesisModification();
404   }
405 }
406
407 //=============================================================================
408 void BLSURFPlugin_Hypothesis::SetOptimiseTinyEdges(bool theVal) {
409   if (theVal != _optimiseTinyEdges) {
410     _optimiseTinyEdges = theVal;
411     NotifySubMeshesHypothesisModification();
412   }
413 }
414
415 //=============================================================================
416 void BLSURFPlugin_Hypothesis::SetTinyEdgeOptimisationLength(double theVal) {
417   if (theVal != _tinyEdgeOptimisationLength) {
418     _tinyEdgeOptimisationLength = theVal;
419     NotifySubMeshesHypothesisModification();
420   }
421 }
422
423 //=============================================================================
424 void BLSURFPlugin_Hypothesis::SetCorrectSurfaceIntersection(bool theVal) {
425   if (theVal != _correctSurfaceIntersec) {
426     _correctSurfaceIntersec = theVal;
427     NotifySubMeshesHypothesisModification();
428   }
429 }
430
431 //=============================================================================
432 void BLSURFPlugin_Hypothesis::SetCorrectSurfaceIntersectionMaxCost(double theVal) {
433   if (theVal != _corrSurfaceIntersCost) {
434     _corrSurfaceIntersCost = theVal;
435     NotifySubMeshesHypothesisModification();
436   }
437 }
438
439 //=============================================================================
440 void BLSURFPlugin_Hypothesis::SetBadElementRemoval(bool theVal) {
441   if (theVal != _badElementRemoval) {
442     _badElementRemoval = theVal;
443     NotifySubMeshesHypothesisModification();
444   }
445 }
446
447 //=============================================================================
448 void BLSURFPlugin_Hypothesis::SetBadElementAspectRatio(double theVal) {
449   if (theVal != _badElementAspectRatio) {
450     _badElementAspectRatio = theVal;
451     NotifySubMeshesHypothesisModification();
452   }
453 }
454
455 //=============================================================================
456 void BLSURFPlugin_Hypothesis::SetOptimizeMesh(bool theVal) {
457   if (theVal != _optimizeMesh) {
458     _optimizeMesh = theVal;
459     NotifySubMeshesHypothesisModification();
460   }
461 }
462
463 //=============================================================================
464 void BLSURFPlugin_Hypothesis::SetQuadraticMesh(bool theVal) {
465   if (theVal != _quadraticMesh) {
466     _quadraticMesh = theVal;
467     NotifySubMeshesHypothesisModification();
468   }
469 }
470
471 //=============================================================================
472 void BLSURFPlugin_Hypothesis::SetTopology(Topology theTopology) {
473   if (theTopology != _topology) {
474     _topology = theTopology;
475     NotifySubMeshesHypothesisModification();
476   }
477 }
478
479 //=============================================================================
480 void BLSURFPlugin_Hypothesis::SetVerbosity(int theVal) {
481   if (theVal != _verb) {
482     _verb = theVal;
483     NotifySubMeshesHypothesisModification();
484   }
485 }
486
487 //=============================================================================
488 void BLSURFPlugin_Hypothesis::SetEnforceCadEdgesSize( bool toEnforce )
489 {
490   if ( GetEnforceCadEdgesSize() != toEnforce )
491   {
492     SetOptionValue( "enforce_cad_edge_sizes", toEnforce ? "yes" : "no" );
493     NotifySubMeshesHypothesisModification();
494   }
495 }
496 //=============================================================================
497 bool BLSURFPlugin_Hypothesis::GetEnforceCadEdgesSize()
498 {
499   return ToBool( GetOptionValue( "enforce_cad_edge_sizes" ), GET_DEFAULT() );
500 }
501 //=============================================================================
502
503 void BLSURFPlugin_Hypothesis::SetJacobianRectificationRespectGeometry( bool allowRectification )
504 {
505   if ( GetJacobianRectificationRespectGeometry() != allowRectification )
506   {
507     SetOptionValue("jacobian_rectification_respect_geometry", allowRectification ? "yes" : "no" );
508     NotifySubMeshesHypothesisModification();
509   }
510 }
511 //=============================================================================
512 bool BLSURFPlugin_Hypothesis::GetJacobianRectificationRespectGeometry()
513 {
514   return ToBool( GetOptionValue("jacobian_rectification_respect_geometry", GET_DEFAULT()));
515 }
516 //=============================================================================
517
518 void BLSURFPlugin_Hypothesis::SetJacobianRectification( bool allowRectification )
519 {
520   if ( GetJacobianRectification() != allowRectification )
521   {
522     SetOptionValue( "rectify_jacobian", allowRectification ? "yes" : "no" );
523     NotifySubMeshesHypothesisModification();
524   }
525 }
526 //=============================================================================
527 bool BLSURFPlugin_Hypothesis::GetJacobianRectification()
528 {
529   return ToBool( GetOptionValue("rectify_jacobian", GET_DEFAULT()));
530 }
531 //=============================================================================
532
533 void BLSURFPlugin_Hypothesis::SetMaxNumberOfPointsPerPatch( int nb )
534   throw (std::invalid_argument)
535 {
536   if ( nb < 0 )
537     throw std::invalid_argument( SMESH_Comment("Invalid number of points: ") << nb );
538
539   if ( GetMaxNumberOfPointsPerPatch() != nb )
540   {
541     SetOptionValue("max_number_of_points_per_patch", SMESH_Comment( nb ));
542     NotifySubMeshesHypothesisModification();
543   }
544 }
545 //=============================================================================
546 int BLSURFPlugin_Hypothesis::GetMaxNumberOfPointsPerPatch()
547 {
548   return ToInt( GetOptionValue("max_number_of_points_per_patch", GET_DEFAULT()));
549 }
550 //=============================================================================
551
552 void BLSURFPlugin_Hypothesis::SetRespectGeometry( bool toRespect )
553 {
554   if ( GetRespectGeometry() != toRespect )
555   {
556     SetOptionValue("respect_geometry", toRespect ? "yes" : "no" );
557     NotifySubMeshesHypothesisModification();
558   }
559 }
560 //=============================================================================
561 bool BLSURFPlugin_Hypothesis::GetRespectGeometry()
562 {
563   return ToBool( GetOptionValue( "respect_geometry", GET_DEFAULT()));
564 }
565 //=============================================================================
566
567 void BLSURFPlugin_Hypothesis::SetTinyEdgesAvoidSurfaceIntersections( bool toAvoidIntersection )
568 {
569   if ( GetTinyEdgesAvoidSurfaceIntersections() != toAvoidIntersection )
570   {
571     SetOptionValue("tiny_edge_avoid_surface_intersections", toAvoidIntersection ? "yes" : "no" );
572     NotifySubMeshesHypothesisModification();
573   }
574 }
575 //=============================================================================
576 bool BLSURFPlugin_Hypothesis::GetTinyEdgesAvoidSurfaceIntersections()
577 {
578   return ToBool( GetOptionValue("tiny_edge_avoid_surface_intersections", GET_DEFAULT()));
579 }
580 //=============================================================================
581
582 void BLSURFPlugin_Hypothesis::SetClosedGeometry( bool isClosed )
583 {
584   if ( GetClosedGeometry() != isClosed )
585   {
586     SetPreCADOptionValue("closed_geometry", isClosed ? "yes" : "no" );
587     NotifySubMeshesHypothesisModification();
588   }
589 }
590 //=============================================================================
591 bool BLSURFPlugin_Hypothesis::GetClosedGeometry()
592 {
593   return ToBool( GetPreCADOptionValue( "closed_geometry", GET_DEFAULT()));
594 }
595 //=============================================================================
596
597 void BLSURFPlugin_Hypothesis::SetDebug( bool isDebug )
598 {
599   if ( GetDebug() != isDebug )
600   {
601     SetPreCADOptionValue("debug", isDebug ? "yes" : "no" );
602     NotifySubMeshesHypothesisModification();
603   }
604 }
605 //=============================================================================
606 bool BLSURFPlugin_Hypothesis::GetDebug()
607 {
608   return ToBool( GetPreCADOptionValue("debug", GET_DEFAULT()));
609 }
610 //=============================================================================
611
612 void BLSURFPlugin_Hypothesis::SetPeriodicTolerance( CORBA::Double tol )
613   throw (std::invalid_argument)
614 {
615   if ( tol <= 0 )
616     throw std::invalid_argument( SMESH_Comment("Invalid tolerance: ") << tol );
617   if ( GetPeriodicTolerance() != tol )
618   {
619     SetPreCADOptionValue("periodic_tolerance", SMESH_Comment( tol ) );
620     NotifySubMeshesHypothesisModification();
621   }
622 }
623 //=============================================================================
624 double BLSURFPlugin_Hypothesis::GetPeriodicTolerance()
625 {
626   return ToDbl( GetPreCADOptionValue( "periodic_tolerance", GET_DEFAULT()));
627 }
628 //=============================================================================
629
630 void BLSURFPlugin_Hypothesis::SetRequiredEntities( const std::string& howToTreat )
631   throw (std::invalid_argument)
632 {
633   if ( howToTreat != "respect" && howToTreat != "ignore" && howToTreat != "clear"  )
634     throw std::invalid_argument
635       ( SMESH_Comment("required_entities must be in ['respect','ignore','clear'] "));
636
637   if ( GetRequiredEntities() != howToTreat )
638   {
639     SetPreCADOptionValue("required_entities", howToTreat );
640     NotifySubMeshesHypothesisModification();
641   }
642 }
643 //=============================================================================
644 std::string BLSURFPlugin_Hypothesis::GetRequiredEntities()
645 {
646   return GetPreCADOptionValue("required_entities", GET_DEFAULT());
647 }
648 //=============================================================================
649
650 void BLSURFPlugin_Hypothesis::SetSewingTolerance( CORBA::Double tol )
651   throw (std::invalid_argument)
652 {
653   if ( tol <= 0 )
654     throw std::invalid_argument( SMESH_Comment("Invalid tolerance: ") << tol );
655   if ( GetSewingTolerance() != tol )
656   {
657     SetPreCADOptionValue("sewing_tolerance", SMESH_Comment( tol ) );
658     NotifySubMeshesHypothesisModification();
659   }
660 }
661 //=============================================================================
662 CORBA::Double BLSURFPlugin_Hypothesis::GetSewingTolerance()
663 {
664   return ToDbl( GetPreCADOptionValue("sewing_tolerance", GET_DEFAULT()));
665 }
666 //=============================================================================
667
668 void BLSURFPlugin_Hypothesis::SetTags( const std::string& howToTreat )
669   throw (std::invalid_argument)
670 {
671   if ( howToTreat != "respect" && howToTreat != "ignore" && howToTreat != "clear"  )
672     throw std::invalid_argument
673       ( SMESH_Comment("'tags' must be in ['respect','ignore','clear'] "));
674
675   if ( GetTags() != howToTreat )
676   {
677     SetPreCADOptionValue("tags", howToTreat );
678     NotifySubMeshesHypothesisModification();
679   }
680 }
681 //=============================================================================
682 std::string BLSURFPlugin_Hypothesis::GetTags()
683 {
684   return GetPreCADOptionValue("tags", GET_DEFAULT());
685 }
686 //=============================================================================
687 void BLSURFPlugin_Hypothesis::SetPreCADMergeEdges(bool theVal)
688 {
689   if (theVal != ToBool( GetPreCADOptionValue("merge_edges", GET_DEFAULT()))) {
690     _preCADMergeEdges = theVal;
691     SetPreCADOptionValue("merge_edges", theVal ? "yes" : "no" );
692     NotifySubMeshesHypothesisModification();
693   }
694 }
695
696 //=============================================================================
697 void BLSURFPlugin_Hypothesis::SetPreCADRemoveTinyUVEdges(bool theVal)
698 {
699   if (theVal != _preCADRemoveTinyUVEdges) {
700     _preCADRemoveTinyUVEdges = theVal;
701     NotifySubMeshesHypothesisModification();
702   }
703 }
704
705 //=============================================================================
706 void BLSURFPlugin_Hypothesis::SetPreCADRemoveDuplicateCADFaces(bool theVal)
707 {
708   if (theVal != ToBool( GetPreCADOptionValue("remove_duplicate_cad_faces", GET_DEFAULT()))) {
709     _preCADRemoveDuplicateCADFaces = theVal;
710     SetPreCADOptionValue("remove_duplicate_cad_faces", theVal ? "yes" : "no" );
711     NotifySubMeshesHypothesisModification();
712   }
713 }
714
715 //=============================================================================
716 void BLSURFPlugin_Hypothesis::SetPreCADProcess3DTopology(bool theVal)
717 {
718   if (theVal != ToBool( GetPreCADOptionValue("process_3d_topology", GET_DEFAULT()))) {
719     _preCADProcess3DTopology = theVal;
720     SetPreCADOptionValue("process_3d_topology", theVal ? "yes" : "no" );
721     NotifySubMeshesHypothesisModification();
722   }
723 }
724
725 //=============================================================================
726 void BLSURFPlugin_Hypothesis::SetPreCADDiscardInput(bool theVal)
727 {
728   if (theVal != ToBool( GetPreCADOptionValue("discard_input_topology", GET_DEFAULT()))) {
729     _preCADDiscardInput = theVal;
730     SetPreCADOptionValue("discard_input_topology", theVal ? "yes" : "no" );
731     NotifySubMeshesHypothesisModification();
732   }
733 }
734
735 //=============================================================================
736 // Return true if any PreCAD option is activated
737 bool BLSURFPlugin_Hypothesis::HasPreCADOptions(const BLSURFPlugin_Hypothesis* hyp)
738 {
739   if ( !hyp )
740   {
741     return false;
742   }
743   bool orDefault, isOk;
744   return ( ToBool( hyp->GetPreCADOptionValue("closed_geometry"           , &orDefault )) ||
745            ToBool( hyp->GetPreCADOptionValue("discard_input_topology"    , &orDefault )) ||
746            ToBool( hyp->GetPreCADOptionValue("merge_edges"               , &orDefault )) ||
747            ToBool( hyp->GetPreCADOptionValue("remove_duplicate_cad_faces", &orDefault )) ||
748            ToBool( hyp->GetPreCADOptionValue("process_3d_topology"       , &orDefault )) ||
749            ToBool( hyp->GetPreCADOption     ("manifold_geometry")        , &isOk       ) ||
750            hyp->GetPreCADOptionValue("sewing_tolerance", &orDefault ) != "5e-4*D"        ||
751            !hyp->_preCadFacesPeriodicityVector.empty()                                   ||
752            !hyp->_preCadEdgesPeriodicityVector.empty()                                   ||
753            !hyp->_facesPeriodicityVector.empty()                                         ||
754            !hyp->_edgesPeriodicityVector.empty()                                         ||
755            !hyp->_verticesPeriodicityVector.empty()                                      ||
756            hyp->GetTopology() != FromCAD );
757 }
758
759 //=============================================================================
760 // void BLSURFPlugin_Hypothesis::SetGMFFile(const std::string& theFileName, bool isBinary)
761 void BLSURFPlugin_Hypothesis::SetGMFFile(const std::string& theFileName)
762 {
763   _GMFFileName = theFileName;
764   //   _GMFFileMode = isBinary;
765   NotifySubMeshesHypothesisModification();
766 }
767
768 //=============================================================================
769 void BLSURFPlugin_Hypothesis::SetOptionValue(const std::string& optionName, const std::string& optionValue)
770   throw (std::invalid_argument) {
771
772   TOptionValues::iterator op_val = _option2value.find(optionName);
773   if (op_val == _option2value.end())
774   {
775     op_val = _preCADoption2value.find(optionName);
776     if (op_val == _preCADoption2value.end())
777     {
778       std::string msg = "Unknown MG-CADSurf option: '" + optionName + "'. Try SetAdvancedOption()";
779       throw std::invalid_argument(msg);
780     }
781   }
782   if (op_val->second != optionValue)
783   {
784     const char* ptr = optionValue.c_str();
785     // strip white spaces
786     while (ptr[0] == ' ')
787       ptr++;
788     int i = strlen(ptr);
789     while (i != 0 && ptr[i - 1] == ' ')
790       i--;
791     // check value type
792     bool typeOk = true;
793     std::string typeName;
794     if (i == 0) {
795       // empty string
796     } else if (_charOptions.count(optionName)) {
797       // do not check strings
798     } else if (_doubleOptions.count(optionName)) {
799       // check if value is double
800       ToDbl(ptr, &typeOk);
801       typeName = "real";
802     } else if (_boolOptions.count(optionName)) {
803       // check if value is bool
804       ToBool(ptr, &typeOk);
805       typeName = "bool";
806     } else {
807       // check if value is int
808       ToInt(ptr, &typeOk);
809       typeName = "integer";
810     }
811     if (!typeOk) {
812       std::string msg = "Advanced option '" + optionName + "' = '" + optionValue + "' but must be " + typeName;
813       throw std::invalid_argument(msg);
814     }
815     std::string value( ptr, i );
816     if ( _defaultOptionValues[ optionName ] == value )
817       value.clear();
818
819     op_val->second = value;
820
821     NotifySubMeshesHypothesisModification();
822   }
823 }
824
825 //=============================================================================
826 void BLSURFPlugin_Hypothesis::SetPreCADOptionValue(const std::string& optionName, const std::string& optionValue)
827   throw (std::invalid_argument) {
828
829   TOptionValues::iterator op_val = _preCADoption2value.find(optionName);
830   if (op_val == _preCADoption2value.end()) {
831     op_val = _option2value.find(optionName);
832     if (op_val == _option2value.end()) {
833       std::string msg = "Unknown MG-PreCAD option: '" + optionName + "'. Try SetAdvancedOption()";
834       throw std::invalid_argument(msg);
835     }
836   }
837   if (op_val->second != optionValue)
838   {
839     const char* ptr = optionValue.c_str();
840     // strip white spaces
841     while (ptr[0] == ' ')
842       ptr++;
843     int i = strlen(ptr);
844     while (i != 0 && ptr[i - 1] == ' ')
845       i--;
846     // check value type
847     bool typeOk = true;
848     std::string typeName;
849     if (i == 0) {
850       // empty string
851     } else if (_preCADcharOptions.find(optionName) != _preCADcharOptions.end()) {
852       // do not check strings
853     } else if (_preCADdoubleOptions.find(optionName) != _preCADdoubleOptions.end()) {
854       // check if value is double
855       char * endPtr;
856       strtod(ptr, &endPtr);
857       typeOk = (ptr != endPtr);
858       typeName = "real";
859     } else if (_boolOptions.count(optionName)) {
860       // check if value is bool
861       ToBool(ptr, &typeOk);
862       typeName = "bool";
863     } else {
864       // check if value is int
865       char * endPtr;
866       strtol(ptr, &endPtr, 10);
867       typeOk = (ptr != endPtr);
868       typeName = "integer";
869     }
870     if (!typeOk) {
871       std::string msg = "PreCAD advanced option '" + optionName + "' = '" + optionValue + "' but must be " + typeName;
872       throw std::invalid_argument(msg);
873     }
874     std::string value( ptr, i );
875     if ( _defaultOptionValues[ optionName ] == value )
876       value.clear();
877
878     op_val->second = value;
879
880     NotifySubMeshesHypothesisModification();
881   }
882 }
883
884 //=============================================================================
885 std::string BLSURFPlugin_Hypothesis::GetOptionValue(const std::string& optionName,
886                                                     bool*              isDefault) const
887   throw (std::invalid_argument)
888 {
889   TOptionValues::const_iterator op_val = _option2value.find(optionName);
890   if (op_val == _option2value.end())
891   {
892     op_val = _preCADoption2value.find(optionName);
893     if (op_val == _preCADoption2value.end())
894     {
895       op_val = _customOption2value.find(optionName);
896       if (op_val == _customOption2value.end())
897       {
898         std::string msg = "Unknown MG-CADSurf option: <" + optionName + ">";
899         throw std::invalid_argument(msg);
900       }
901     }
902   }
903   std::string val = op_val->second;
904   if ( isDefault ) *isDefault = ( val.empty() );
905
906   if ( val.empty() && isDefault )
907   {
908     op_val = _defaultOptionValues.find( optionName );
909     if (op_val != _defaultOptionValues.end())
910       val = op_val->second;
911   }
912   return val;
913 }
914
915 //=============================================================================
916 std::string BLSURFPlugin_Hypothesis::GetPreCADOptionValue(const std::string& optionName,
917                                                           bool*              isDefault) const
918   throw (std::invalid_argument)
919 {
920   TOptionValues::const_iterator op_val = _preCADoption2value.find(optionName);
921   if (op_val == _preCADoption2value.end())
922   {
923     op_val = _option2value.find(optionName);
924     if (op_val == _option2value.end())
925     {
926       op_val = _customOption2value.find(optionName);
927       if (op_val == _customOption2value.end())
928       {
929         std::string msg = "Unknown MG-CADSurf option: <" + optionName + ">";
930         throw std::invalid_argument(msg);
931       }
932     }
933   }
934   std::string val = op_val->second;
935   if ( isDefault ) *isDefault = ( val.empty() );
936
937   if ( val.empty() && isDefault )
938   {
939     op_val = _defaultOptionValues.find( optionName );
940     if (op_val != _option2value.end())
941       val = op_val->second;
942   }
943   return val;
944 }
945
946 //=============================================================================
947 void BLSURFPlugin_Hypothesis::ClearOption(const std::string& optionName)
948 {
949   TOptionValues::iterator op_val = _customOption2value.find(optionName);
950   if (op_val != _customOption2value.end())
951    _customOption2value.erase(op_val);
952   else {
953     op_val = _option2value.find(optionName);
954     if (op_val != _option2value.end())
955       op_val->second.clear();
956     else {
957       op_val = _preCADoption2value.find(optionName);
958       if (op_val != _preCADoption2value.end())
959         op_val->second.clear();
960     }
961   }
962 }
963
964 //=============================================================================
965 void BLSURFPlugin_Hypothesis::ClearPreCADOption(const std::string& optionName)
966 {
967   TOptionValues::iterator op_val = _preCADoption2value.find(optionName);
968   if (op_val != _preCADoption2value.end())
969     op_val->second.clear();
970 }
971
972 //=============================================================================
973 void BLSURFPlugin_Hypothesis::AddOption(const std::string& optionName, const std::string& optionValue)
974 {
975   bool modif = true;
976   TOptionValues::iterator op_val = _option2value.find(optionName);
977   if (op_val != _option2value.end())
978   {
979     if (op_val->second != optionValue)
980       op_val->second = optionValue;
981     else
982       modif = false;
983   }
984   else
985   {
986     op_val = _preCADoption2value.find(optionName);
987     if (op_val != _preCADoption2value.end())
988     {
989       if (op_val->second != optionValue)
990         op_val->second = optionValue;
991       else
992         modif = false;
993     }
994     else if ( optionValue.empty() )
995     {
996       _customOption2value.erase( optionName );
997     }
998     else
999     {
1000       op_val = _customOption2value.find(optionName);
1001       if (op_val == _customOption2value.end())
1002         _customOption2value[optionName] = optionValue;
1003       else if (op_val->second != optionValue)
1004         op_val->second = optionValue;
1005       else
1006         modif = false;
1007     }
1008   }
1009   if ( modif )
1010     NotifySubMeshesHypothesisModification();
1011 }
1012
1013 //=============================================================================
1014 void BLSURFPlugin_Hypothesis::AddPreCADOption(const std::string& optionName, const std::string& optionValue)
1015 {
1016   AddOption( optionName, optionValue );
1017 }
1018
1019 //=============================================================================
1020 std::string BLSURFPlugin_Hypothesis::GetOption(const std::string& optionName) const
1021 {
1022   TOptionValues::const_iterator op_val = _customOption2value.find(optionName);
1023   if (op_val != _customOption2value.end())
1024     return op_val->second;
1025   else
1026     return "";
1027 }
1028
1029 //=============================================================================
1030 std::string BLSURFPlugin_Hypothesis::GetPreCADOption(const std::string& optionName) const
1031 {
1032   TOptionValues::const_iterator op_val = _customOption2value.find(optionName);
1033   if (op_val != _customOption2value.end())
1034     return op_val->second;
1035   else
1036     return "";
1037 }
1038
1039 //=============================================================================
1040 BLSURFPlugin_Hypothesis::TOptionValues BLSURFPlugin_Hypothesis::GetOptionValues() const
1041 {
1042   TOptionValues vals;
1043   TOptionValues::const_iterator op_val = _option2value.begin();
1044   for ( ; op_val != _option2value.end(); ++op_val )
1045     vals.insert( make_pair( op_val->first, GetOptionValue( op_val->first, GET_DEFAULT() )));
1046
1047   return vals;
1048 }
1049
1050 //=============================================================================
1051 BLSURFPlugin_Hypothesis::TOptionValues BLSURFPlugin_Hypothesis::GetPreCADOptionValues() const
1052 {
1053   TOptionValues vals;
1054   TOptionValues::const_iterator op_val = _preCADoption2value.begin();
1055   for ( ; op_val != _preCADoption2value.end(); ++op_val )
1056     vals.insert( make_pair( op_val->first, GetPreCADOptionValue( op_val->first, GET_DEFAULT() )));
1057
1058   return vals;
1059 }
1060
1061 //=======================================================================
1062 //function : SetSizeMapEntry
1063 //=======================================================================
1064 void BLSURFPlugin_Hypothesis::SetSizeMapEntry(const std::string& entry, const std::string& sizeMap) {
1065   if (_sizeMap[entry].compare(sizeMap) != 0) {
1066     SetPhysicalMesh(PhysicalLocalSize);
1067     _sizeMap[entry] = sizeMap;
1068     NotifySubMeshesHypothesisModification();
1069   }
1070 }
1071
1072 //=======================================================================
1073 //function : GetSizeMapEntry
1074 //=======================================================================
1075 std::string BLSURFPlugin_Hypothesis::GetSizeMapEntry(const std::string& entry) {
1076   TSizeMap::iterator it = _sizeMap.find(entry);
1077   if (it != _sizeMap.end())
1078     return it->second;
1079   else
1080     return "No_Such_Entry";
1081 }
1082
1083 /*!
1084  * \brief Return the size maps
1085  */
1086 BLSURFPlugin_Hypothesis::TSizeMap BLSURFPlugin_Hypothesis::GetSizeMapEntries(const BLSURFPlugin_Hypothesis* hyp) {
1087   return hyp ? hyp->_GetSizeMapEntries() : GetDefaultSizeMap();
1088 }
1089
1090 //=======================================================================
1091 //function : SetAttractorEntry
1092 //=======================================================================
1093 void BLSURFPlugin_Hypothesis::SetAttractorEntry(const std::string& entry, const std::string& attractor) {
1094   if (_attractors[entry].compare(attractor) != 0) {
1095     SetPhysicalMesh(PhysicalLocalSize);
1096     _attractors[entry] = attractor;
1097     NotifySubMeshesHypothesisModification();
1098   }
1099 }
1100
1101 //=======================================================================
1102 //function : GetAttractorEntry
1103 //=======================================================================
1104 std::string BLSURFPlugin_Hypothesis::GetAttractorEntry(const std::string& entry) {
1105   TSizeMap::iterator it = _attractors.find(entry);
1106   if (it != _attractors.end())
1107     return it->second;
1108   else
1109     return "No_Such_Entry";
1110 }
1111
1112 /*!
1113  * \brief Return the attractors
1114  */
1115 BLSURFPlugin_Hypothesis::TSizeMap BLSURFPlugin_Hypothesis::GetAttractorEntries(const BLSURFPlugin_Hypothesis* hyp) {
1116   return hyp ? hyp->_GetAttractorEntries() : GetDefaultSizeMap();
1117 }
1118
1119 //=======================================================================
1120 //function : SetClassAttractorEntry
1121 //=======================================================================
1122 void BLSURFPlugin_Hypothesis::SetClassAttractorEntry(const std::string& entry, const std::string& attEntry, double StartSize, double EndSize, double ActionRadius, double ConstantRadius)
1123 {
1124   SetPhysicalMesh(PhysicalLocalSize);
1125
1126   // The new attractor can't be defined on the same face as another sizemap
1127   TSizeMap::iterator it  = _sizeMap.find( entry );
1128   if ( it != _sizeMap.end() ) {
1129     _sizeMap.erase(it);
1130     NotifySubMeshesHypothesisModification();
1131   }
1132   else {
1133     TSizeMap::iterator itAt  = _attractors.find( entry );
1134     if ( itAt != _attractors.end() ) {
1135       _attractors.erase(itAt);
1136       NotifySubMeshesHypothesisModification();
1137     }
1138   }
1139   
1140   const TopoDS_Shape AttractorShape = BLSURFPlugin_Hypothesis::entryToShape(attEntry);
1141   const TopoDS_Face FaceShape = TopoDS::Face(BLSURFPlugin_Hypothesis::entryToShape(entry));
1142   TAttractorMap::iterator attIt = _classAttractors.find(entry);
1143   for ( ; attIt != _classAttractors.end(); ++attIt )
1144     if ( attIt->first == entry && 
1145          attIt->second->GetAttractorEntry() == attEntry )
1146       break;
1147   bool attExists = (attIt != _classAttractors.end());
1148
1149   BLSURFPlugin_Attractor* myAttractor;
1150   if ( !attExists ) {
1151     myAttractor = new BLSURFPlugin_Attractor(FaceShape, AttractorShape, attEntry);//, 0.1 );
1152     _classAttractors.insert( make_pair( entry, myAttractor ));
1153   }
1154   else {
1155     myAttractor = attIt->second;
1156   }
1157   // if (!myAttractor->IsMapBuilt())
1158   //   myAttractor->BuildMap();
1159   myAttractor->SetParameters(StartSize, EndSize, ActionRadius, ConstantRadius);
1160
1161   NotifySubMeshesHypothesisModification();
1162 }
1163
1164 //=======================================================================
1165 //function : SetConstantSizeOnAdjacentFaces
1166 //=======================================================================
1167 // TODO uncomment and test (include the needed .hxx)
1168 // SetConstantSizeOnAdjacentFaces(myShape, att_entry, startSize, endSize = user_size, const_dist  ) {
1169 //   TopTools_IndexedMapOfShapListOdShape anEdge2FaceMap;
1170 //   TopExp::MapShapesAnAncestors(myShape,TopAbs_EDGE, TopAbs_FACE, anEdge2FaceMap);
1171 //   TopTools_IndexedMapOfShapListOdShape::iterator it;
1172 //   for (it = anEdge2FaceMap.begin();it != anEdge2FaceMap.end();it++){
1173 //       SetClassAttractorEntry((*it).first, att_entry, startSize, endSize, 0, const_dist)
1174 //   }
1175
1176
1177
1178
1179
1180
1181 //=======================================================================
1182 //function : GetClassAttractorEntry
1183 //=======================================================================
1184 // BLSURFPlugin_Attractor&  BLSURFPlugin_Hypothesis::GetClassAttractorEntry(const std::string& entry)
1185 // {
1186 //  TAttractorMap::iterator it  = _classAttractors.find( entry );
1187 //  if ( it != _classAttractors.end() )
1188 //    return it->second;
1189 //  else
1190 //    return "No_Such_Entry";
1191 // }
1192 // 
1193   /*!
1194    * \brief Return the map of attractor instances
1195    */
1196 BLSURFPlugin_Hypothesis::TAttractorMap BLSURFPlugin_Hypothesis::GetClassAttractorEntries(const BLSURFPlugin_Hypothesis* hyp)
1197 {
1198     return hyp ? hyp->_GetClassAttractorEntries():GetDefaultAttractorMap();
1199 }
1200
1201 //=======================================================================
1202 //function : ClearEntry
1203 //=======================================================================
1204 void BLSURFPlugin_Hypothesis::ClearEntry(const std::string& entry,
1205                                          const char * attEntry/*=0*/)
1206 {
1207  TSizeMap::iterator it  = _sizeMap.find( entry );
1208  
1209  if ( it != _sizeMap.end() ) {
1210    _sizeMap.erase(it);
1211    NotifySubMeshesHypothesisModification();
1212  }
1213  else {
1214    TSizeMap::iterator itAt  = _attractors.find( entry );
1215    if ( itAt != _attractors.end() ) {
1216      _attractors.erase(itAt);
1217      NotifySubMeshesHypothesisModification();
1218    }
1219    else {
1220      TAttractorMap::iterator it_clAt = _classAttractors.find( entry );
1221      if ( it_clAt != _classAttractors.end() ) {
1222        do {
1223          if ( !attEntry || it_clAt->second->GetAttractorEntry() == attEntry )
1224            _classAttractors.erase( it_clAt++ );
1225          else
1226            ++it_clAt;
1227        }
1228        while ( it_clAt != _classAttractors.end() );
1229        NotifySubMeshesHypothesisModification();
1230      }
1231      else
1232        std::cout<<"No_Such_Entry"<<std::endl;
1233    }
1234  }
1235 }
1236
1237 //=======================================================================
1238 //function : ClearSizeMaps
1239 //=======================================================================
1240 void BLSURFPlugin_Hypothesis::ClearSizeMaps() {
1241   _sizeMap.clear();
1242   _attractors.clear();
1243   _classAttractors.clear();
1244 }
1245
1246 // Enable internal enforced vertices on specific face if requested by user
1247
1248 ////=======================================================================
1249 ////function : SetInternalEnforcedVertex
1250 ////=======================================================================
1251 //void BLSURFPlugin_Hypothesis::SetInternalEnforcedVertex(TEntry theFaceEntry,
1252 //                                                        bool toEnforceInternalVertices,
1253 //                                                        TEnfGroupName theGroupName) {
1254
1255 //      << toEnforceInternalVertices << ", " << theGroupName << ")");
1256   
1257 //  TFaceEntryInternalVerticesList::iterator it = _faceEntryInternalVerticesList.find(theFaceEntry);
1258 //  if (it != _faceEntryInternalVerticesList.end()) {
1259 //    if (!toEnforceInternalVertices) {
1260 //      _faceEntryInternalVerticesList.erase(it);
1261 //    }
1262 //  }
1263 //  else {
1264 //    if (toEnforceInternalVertices) {
1265 //      _faceEntryInternalVerticesList.insert(theFaceEntry);
1266 //    }
1267 //  }
1268   
1269 //  // TODO
1270 //  // Take care of groups
1271 //}
1272
1273
1274 //=======================================================================
1275 //function : SetEnforcedVertex
1276 //=======================================================================
1277 bool BLSURFPlugin_Hypothesis::SetEnforcedVertex(TEntry        theFaceEntry,
1278                                                 TEnfName      theVertexName,
1279                                                 TEntry        theVertexEntry,
1280                                                 TEnfGroupName theGroupName,
1281                                                 double x, double y, double z)
1282 {
1283   SetPhysicalMesh(PhysicalLocalSize);
1284
1285   bool toNotify = false;
1286   bool toCreate = true;
1287
1288   TEnfVertex *oldEnVertex;
1289   TEnfVertex *newEnfVertex = new TEnfVertex();
1290   newEnfVertex->name = theVertexName;
1291   newEnfVertex->geomEntry = theVertexEntry;
1292   newEnfVertex->coords.clear();
1293   if (theVertexEntry == "") {
1294     newEnfVertex->coords.push_back(x);
1295     newEnfVertex->coords.push_back(y);
1296     newEnfVertex->coords.push_back(z);
1297   }
1298   newEnfVertex->grpName = theGroupName;
1299   newEnfVertex->faceEntries.clear();
1300   newEnfVertex->faceEntries.insert(theFaceEntry);
1301
1302
1303   // update _enfVertexList
1304   TEnfVertexList::iterator it = _enfVertexList.find(newEnfVertex);
1305   if (it != _enfVertexList.end()) {
1306     toCreate = false;
1307     oldEnVertex = (*it);
1308     if (oldEnVertex->name != theVertexName) {
1309       oldEnVertex->name = theVertexName;
1310       toNotify = true;
1311     }
1312     if (oldEnVertex->grpName != theGroupName) {
1313       oldEnVertex->grpName = theGroupName;
1314       toNotify = true;
1315     }
1316     TEntryList::iterator it_faceEntries = oldEnVertex->faceEntries.find(theFaceEntry);
1317     if (it_faceEntries == oldEnVertex->faceEntries.end()) {
1318       oldEnVertex->faceEntries.insert(theFaceEntry);
1319       _faceEntryEnfVertexListMap[theFaceEntry].insert(oldEnVertex);
1320       toNotify = true;
1321     }
1322     if (toNotify) {
1323       // update map coords / enf vertex if needed
1324       if (oldEnVertex->coords.size()) {
1325         _coordsEnfVertexMap[oldEnVertex->coords] = oldEnVertex;
1326         _faceEntryCoordsListMap[theFaceEntry].insert(oldEnVertex->coords);
1327       }
1328
1329       // update map geom entry / enf vertex if needed
1330       if (oldEnVertex->geomEntry != "") {
1331         _enfVertexEntryEnfVertexMap[oldEnVertex->geomEntry] = oldEnVertex;
1332         _faceEntryEnfVertexEntryListMap[theFaceEntry].insert(oldEnVertex->geomEntry);
1333       }
1334     }
1335   }
1336
1337 //   //////// CREATE ////////////
1338   if (toCreate) {
1339     toNotify = true;
1340     _faceEntryEnfVertexListMap[theFaceEntry].insert(newEnfVertex);
1341     _enfVertexList.insert(newEnfVertex);
1342     if (theVertexEntry == "") {
1343       _faceEntryCoordsListMap[theFaceEntry].insert(newEnfVertex->coords);
1344       _coordsEnfVertexMap[newEnfVertex->coords] = newEnfVertex;
1345     }
1346     else {
1347       _faceEntryEnfVertexEntryListMap[theFaceEntry].insert(newEnfVertex->geomEntry);
1348       _enfVertexEntryEnfVertexMap[newEnfVertex->geomEntry] = newEnfVertex;
1349     }
1350   }
1351
1352   if (toNotify)
1353     NotifySubMeshesHypothesisModification();
1354
1355   return toNotify;
1356 }
1357
1358
1359 //=======================================================================
1360 //function : GetEnforcedVertices
1361 //=======================================================================
1362
1363 BLSURFPlugin_Hypothesis::TEnfVertexList BLSURFPlugin_Hypothesis::GetEnfVertexList(const TEntry& theFaceEntry)
1364     throw (std::invalid_argument) {
1365
1366   if (_faceEntryEnfVertexListMap.count(theFaceEntry) > 0)
1367     return _faceEntryEnfVertexListMap[theFaceEntry];
1368   else
1369     return GetDefaultEnfVertexList();
1370
1371   std::ostringstream msg;
1372   msg << "No enforced vertex for face entry " << theFaceEntry;
1373   throw std::invalid_argument(msg.str());
1374 }
1375
1376 //=======================================================================
1377 //function : GetEnfVertexCoordsList
1378 //=======================================================================
1379
1380 BLSURFPlugin_Hypothesis::TEnfVertexCoordsList BLSURFPlugin_Hypothesis::GetEnfVertexCoordsList(
1381     const TEntry& theFaceEntry) throw (std::invalid_argument) {
1382
1383   if (_faceEntryCoordsListMap.count(theFaceEntry) > 0)
1384     return _faceEntryCoordsListMap[theFaceEntry];
1385
1386   std::ostringstream msg;
1387   msg << "No enforced vertex coords for face entry " << theFaceEntry;
1388   throw std::invalid_argument(msg.str());
1389 }
1390
1391 //=======================================================================
1392 //function : GetEnfVertexEntryList
1393 //=======================================================================
1394
1395 BLSURFPlugin_Hypothesis::TEntryList BLSURFPlugin_Hypothesis::GetEnfVertexEntryList(const TEntry& theFaceEntry)
1396     throw (std::invalid_argument) {
1397
1398   if (_faceEntryEnfVertexEntryListMap.count(theFaceEntry) > 0)
1399     return _faceEntryEnfVertexEntryListMap[theFaceEntry];
1400
1401   std::ostringstream msg;
1402   msg << "No enforced vertex entry for face entry " << theFaceEntry;
1403   throw std::invalid_argument(msg.str());
1404 }
1405
1406 //=======================================================================
1407 //function : GetEnfVertex(TEnfVertexCoords coords)
1408 //=======================================================================
1409
1410 BLSURFPlugin_Hypothesis::TEnfVertex* BLSURFPlugin_Hypothesis::GetEnfVertex(TEnfVertexCoords coords)
1411     throw (std::invalid_argument) {
1412
1413   if (_coordsEnfVertexMap.count(coords) > 0)
1414     return _coordsEnfVertexMap[coords];
1415
1416   std::ostringstream msg;
1417   msg << "No enforced vertex with coords (" << coords[0] << ", " << coords[1] << ", " << coords[2] << ")";
1418   throw std::invalid_argument(msg.str());
1419 }
1420
1421 //=======================================================================
1422 //function : GetEnfVertex(const TEntry& theEnfVertexEntry)
1423 //=======================================================================
1424
1425 BLSURFPlugin_Hypothesis::TEnfVertex* BLSURFPlugin_Hypothesis::GetEnfVertex(const TEntry& theEnfVertexEntry)
1426     throw (std::invalid_argument) {
1427
1428   if (_enfVertexEntryEnfVertexMap.count(theEnfVertexEntry) > 0)
1429     return _enfVertexEntryEnfVertexMap[theEnfVertexEntry];
1430
1431   std::ostringstream msg;
1432   msg << "No enforced vertex with entry " << theEnfVertexEntry;
1433   throw std::invalid_argument(msg.str());
1434 }
1435
1436 //Enable internal enforced vertices on specific face if requested by user
1437 ////=======================================================================
1438 ////function : GetInternalEnforcedVertex
1439 ////=======================================================================
1440
1441 //bool BLSURFPlugin_Hypothesis::GetInternalEnforcedVertex(const TEntry& theFaceEntry)
1442 //{
1443 //  if (_faceEntryInternalVerticesList.count(theFaceEntry) > 0)
1444 //    return true;
1445 //  return false;
1446 //}
1447
1448 //=======================================================================
1449 //function : ClearEnforcedVertex
1450 //=======================================================================
1451
1452 bool BLSURFPlugin_Hypothesis::ClearEnforcedVertex(const TEntry& theFaceEntry, double x, double y, double z,
1453     const TEntry& theVertexEntry) throw (std::invalid_argument) {
1454
1455   bool toNotify = false;
1456   std::ostringstream msg;
1457   TEnfVertex *oldEnfVertex;
1458   TEnfVertexCoords coords;
1459   coords.clear();
1460   coords.push_back(x);
1461   coords.push_back(y);
1462   coords.push_back(z);
1463
1464   // check that enf vertex with given enf vertex entry exists
1465   TEnfVertexEntryEnfVertexMap::iterator it_enfVertexEntry = _enfVertexEntryEnfVertexMap.find(theVertexEntry);
1466   if (it_enfVertexEntry != _enfVertexEntryEnfVertexMap.end()) {
1467     // Success
1468     oldEnfVertex = it_enfVertexEntry->second;
1469
1470     _enfVertexEntryEnfVertexMap.erase(it_enfVertexEntry);
1471
1472     TEntryList& enfVertexEntryList = _faceEntryEnfVertexEntryListMap[theFaceEntry];
1473     enfVertexEntryList.erase(theVertexEntry);
1474     if (enfVertexEntryList.size() == 0)
1475       _faceEntryEnfVertexEntryListMap.erase(theFaceEntry);
1476     //    TFaceEntryEnfVertexEntryListMap::iterator it_entry_entry = _faceEntryEnfVertexEntryListMap.find(theFaceEntry);
1477     //    TEntryList::iterator it_entryList = it_entry_entry->second.find(theVertexEntry);
1478     //    it_entry_entry->second.erase(it_entryList);
1479     //    if (it_entry_entry->second.size() == 0)
1480     //      _faceEntryEnfVertexEntryListMap.erase(it_entry_entry);
1481   } else {
1482     // Fail
1483     MESSAGE("Enforced vertex with geom entry " << theVertexEntry << " not found");
1484     msg << "No enforced vertex with geom entry " << theVertexEntry;
1485     // check that enf vertex with given coords exists
1486     TCoordsEnfVertexMap::iterator it_coords_enf = _coordsEnfVertexMap.find(coords);
1487     if (it_coords_enf != _coordsEnfVertexMap.end()) {
1488       // Success
1489       oldEnfVertex = it_coords_enf->second;
1490
1491       _coordsEnfVertexMap.erase(it_coords_enf);
1492
1493       TEnfVertexCoordsList& enfVertexCoordsList = _faceEntryCoordsListMap[theFaceEntry];
1494       enfVertexCoordsList.erase(coords);
1495       if (enfVertexCoordsList.size() == 0)
1496         _faceEntryCoordsListMap.erase(theFaceEntry);
1497       //      TFaceEntryCoordsListMap::iterator it_entry_coords = _faceEntryCoordsListMap.find(theFaceEntry);
1498       //      TEnfVertexCoordsList::iterator it_coordsList = it_entry_coords->second.find(coords);
1499       //      it_entry_coords->second.erase(it_coordsList);
1500       //      if (it_entry_coords->second.size() == 0)
1501       //        _faceEntryCoordsListMap.erase(it_entry_coords);
1502     } else {
1503       // Fail
1504       MESSAGE("Enforced vertex with coords " << x << ", " << y << ", " << z << " not found");
1505       msg << std::endl;
1506       msg << "No enforced vertex at " << x << ", " << y << ", " << z;
1507       throw std::invalid_argument(msg.str());
1508     }
1509   }
1510
1511   // update _enfVertexList
1512   TEnfVertexList::iterator it = _enfVertexList.find(oldEnfVertex);
1513   if (it != _enfVertexList.end()) {
1514     (*it)->faceEntries.erase(theFaceEntry);
1515     if ((*it)->faceEntries.size() == 0){
1516       _enfVertexList.erase(it);
1517       toNotify = true;
1518     }
1519   }
1520
1521   // update _faceEntryEnfVertexListMap
1522   TEnfVertexList& currentEnfVertexList = _faceEntryEnfVertexListMap[theFaceEntry];
1523   currentEnfVertexList.erase(oldEnfVertex);
1524
1525   if (currentEnfVertexList.size() == 0) {
1526     _faceEntryEnfVertexListMap.erase(theFaceEntry);
1527   }
1528
1529   if (toNotify)
1530     NotifySubMeshesHypothesisModification();
1531
1532   return toNotify;
1533 }
1534
1535 //=======================================================================
1536 //function : ClearEnforcedVertices
1537 //=======================================================================
1538
1539 bool BLSURFPlugin_Hypothesis::ClearEnforcedVertices(const TEntry& theFaceEntry) throw (std::invalid_argument) {
1540
1541   bool toNotify = false;
1542   TEnfVertex *oldEnfVertex;
1543
1544   TFaceEntryCoordsListMap::iterator it_entry_coords = _faceEntryCoordsListMap.find(theFaceEntry);
1545   if (it_entry_coords != _faceEntryCoordsListMap.end()) {
1546     toNotify = true;
1547     TEnfVertexCoordsList coordsList = it_entry_coords->second;
1548     TEnfVertexCoordsList::iterator it_coordsList = coordsList.begin();
1549     for (; it_coordsList != coordsList.end(); ++it_coordsList) {
1550       TEnfVertexCoords coords = (*it_coordsList);
1551       oldEnfVertex = _coordsEnfVertexMap[coords];
1552       _coordsEnfVertexMap.erase(coords);
1553       // update _enfVertexList
1554       TEnfVertexList::iterator it = _enfVertexList.find(oldEnfVertex);
1555       if (it != _enfVertexList.end()) {
1556         (*it)->faceEntries.erase(theFaceEntry);
1557         if ((*it)->faceEntries.size() == 0){
1558           _enfVertexList.erase(it);
1559           toNotify = true;
1560         }
1561       }
1562     }
1563     _faceEntryCoordsListMap.erase(it_entry_coords);
1564     _faceEntryEnfVertexListMap.erase(theFaceEntry);
1565   }
1566
1567   TFaceEntryEnfVertexEntryListMap::iterator it_entry_entry = _faceEntryEnfVertexEntryListMap.find(theFaceEntry);
1568   if (it_entry_entry != _faceEntryEnfVertexEntryListMap.end()) {
1569     toNotify = true;
1570     TEntryList enfVertexEntryList = it_entry_entry->second;
1571     TEntryList::iterator it_enfVertexEntryList = enfVertexEntryList.begin();
1572     for (; it_enfVertexEntryList != enfVertexEntryList.end(); ++it_enfVertexEntryList) {
1573       TEntry enfVertexEntry = (*it_enfVertexEntryList);
1574       oldEnfVertex = _enfVertexEntryEnfVertexMap[enfVertexEntry];
1575       _enfVertexEntryEnfVertexMap.erase(enfVertexEntry);
1576       // update _enfVertexList
1577       TEnfVertexList::iterator it = _enfVertexList.find(oldEnfVertex);
1578       if (it != _enfVertexList.end()) {
1579         (*it)->faceEntries.erase(theFaceEntry);
1580         if ((*it)->faceEntries.size() == 0){
1581           _enfVertexList.erase(it);
1582           toNotify = true;
1583         }
1584       }
1585     }
1586     _faceEntryEnfVertexEntryListMap.erase(it_entry_entry);
1587     _faceEntryEnfVertexListMap.erase(theFaceEntry);
1588   }
1589
1590   if (toNotify)
1591     NotifySubMeshesHypothesisModification();
1592
1593   return toNotify;
1594   //  std::ostringstream msg;
1595   //  msg << "No enforced vertex for " << theFaceEntry;
1596   //  throw std::invalid_argument(msg.str());
1597 }
1598
1599 //=======================================================================
1600 //function : ClearAllEnforcedVertices
1601 //=======================================================================
1602 void BLSURFPlugin_Hypothesis::ClearAllEnforcedVertices() {
1603   _faceEntryEnfVertexListMap.clear();
1604   _enfVertexList.clear();
1605   _faceEntryCoordsListMap.clear();
1606   _coordsEnfVertexMap.clear();
1607   _faceEntryEnfVertexEntryListMap.clear();
1608   _enfVertexEntryEnfVertexMap.clear();
1609 //  Enable internal enforced vertices on specific face if requested by user
1610 //  _faceEntryInternalVerticesList.clear();
1611   NotifySubMeshesHypothesisModification();
1612 }
1613
1614 //================================================================================
1615 /*!
1616  * \brief Return the enforced vertices
1617  */
1618 //================================================================================
1619
1620
1621 BLSURFPlugin_Hypothesis::TFaceEntryEnfVertexListMap BLSURFPlugin_Hypothesis::GetAllEnforcedVerticesByFace(
1622     const BLSURFPlugin_Hypothesis* hyp) {
1623   return hyp ? hyp->_GetAllEnforcedVerticesByFace() : GetDefaultFaceEntryEnfVertexListMap();
1624 }
1625
1626 //Enable internal enforced vertices on specific face if requested by user
1627 //BLSURFPlugin_Hypothesis::TFaceEntryInternalVerticesList BLSURFPlugin_Hypothesis::GetAllInternalEnforcedVerticesByFace(
1628 //    const BLSURFPlugin_Hypothesis* hyp) {
1629 //  return hyp ? hyp->_GetAllInternalEnforcedVerticesByFace() : GetDefaultFaceEntryInternalVerticesMap();
1630 //}
1631
1632 bool BLSURFPlugin_Hypothesis::GetInternalEnforcedVertexAllFaces(const BLSURFPlugin_Hypothesis* hyp)
1633 {
1634   return hyp ? hyp->_GetInternalEnforcedVertexAllFaces() : GetDefaultInternalEnforcedVertex();
1635 }
1636
1637 BLSURFPlugin_Hypothesis::TEnfGroupName BLSURFPlugin_Hypothesis::GetInternalEnforcedVertexAllFacesGroup(const BLSURFPlugin_Hypothesis* hyp)
1638 {
1639   return hyp ? hyp->_GetInternalEnforcedVertexAllFacesGroup() : BLSURFPlugin_Hypothesis::TEnfGroupName();
1640 }
1641
1642 BLSURFPlugin_Hypothesis::TEnfVertexList BLSURFPlugin_Hypothesis::GetAllEnforcedVertices(
1643     const BLSURFPlugin_Hypothesis* hyp) {
1644   return hyp ? hyp->_GetAllEnforcedVertices() : GetDefaultEnfVertexList();
1645 }
1646
1647 BLSURFPlugin_Hypothesis::TFaceEntryCoordsListMap BLSURFPlugin_Hypothesis::GetAllCoordsByFace(
1648     const BLSURFPlugin_Hypothesis* hyp) {
1649   return hyp ? hyp->_GetAllCoordsByFace() : GetDefaultFaceEntryCoordsListMap();
1650 }
1651
1652 BLSURFPlugin_Hypothesis::TCoordsEnfVertexMap BLSURFPlugin_Hypothesis::GetAllEnforcedVerticesByCoords(
1653     const BLSURFPlugin_Hypothesis* hyp) {
1654   return hyp ? hyp->_GetAllEnforcedVerticesByCoords() : GetDefaultCoordsEnfVertexMap();
1655 }
1656
1657 BLSURFPlugin_Hypothesis::TFaceEntryEnfVertexEntryListMap BLSURFPlugin_Hypothesis::GetAllEnfVertexEntriesByFace(
1658     const BLSURFPlugin_Hypothesis* hyp) {
1659   return hyp ? hyp->_GetAllEnfVertexEntriesByFace() : GetDefaultFaceEntryEnfVertexEntryListMap();
1660 }
1661
1662 BLSURFPlugin_Hypothesis::TEnfVertexEntryEnfVertexMap BLSURFPlugin_Hypothesis::GetAllEnforcedVerticesByEnfVertexEntry(
1663     const BLSURFPlugin_Hypothesis* hyp) {
1664   return hyp ? hyp->_GetAllEnforcedVerticesByEnfVertexEntry() : GetDefaultEnfVertexEntryEnfVertexMap();
1665 }
1666
1667 std::set<int> BLSURFPlugin_Hypothesis::GetEnfVertexNodeIDs(TEnfGroupName theGroupName) throw (std::invalid_argument)
1668 {
1669   TGroupNameNodeIDMap::const_iterator it = _groupNameNodeIDMap.find(theGroupName);
1670   if (it != _groupNameNodeIDMap.end()) {
1671     return it->second;
1672   }
1673   std::ostringstream msg;
1674   msg << "No group " << theGroupName;
1675   throw std::invalid_argument(msg.str());
1676 }
1677
1678 void BLSURFPlugin_Hypothesis::AddEnfVertexNodeID(TEnfGroupName theGroupName,int theNodeID)
1679 {
1680   _groupNameNodeIDMap[theGroupName].insert(theNodeID);
1681 }
1682
1683 void BLSURFPlugin_Hypothesis::RemoveEnfVertexNodeID(TEnfGroupName theGroupName,int theNodeID) throw (std::invalid_argument)
1684 {
1685   TGroupNameNodeIDMap::iterator it = _groupNameNodeIDMap.find(theGroupName);
1686   if (it != _groupNameNodeIDMap.end()) {
1687     std::set<int>::iterator IDit = it->second.find(theNodeID);
1688     if (IDit != it->second.end())
1689       it->second.erase(IDit);
1690     std::ostringstream msg;
1691     msg << "No node IDs " << theNodeID << " for group " << theGroupName;
1692     throw std::invalid_argument(msg.str());
1693   }
1694   std::ostringstream msg;
1695   msg << "No group " << theGroupName;
1696   throw std::invalid_argument(msg.str());
1697 }
1698
1699
1700 //=============================================================================
1701 void BLSURFPlugin_Hypothesis::SetInternalEnforcedVertexAllFaces(bool toEnforceInternalVertices) {
1702   if (toEnforceInternalVertices != _enforcedInternalVerticesAllFaces) {
1703     _enforcedInternalVerticesAllFaces = toEnforceInternalVertices;
1704     if (toEnforceInternalVertices)
1705       SetPhysicalMesh(PhysicalLocalSize);
1706     NotifySubMeshesHypothesisModification();
1707   }
1708 }
1709
1710
1711 //=============================================================================
1712 void BLSURFPlugin_Hypothesis::SetInternalEnforcedVertexAllFacesGroup(BLSURFPlugin_Hypothesis::TEnfGroupName theGroupName) {
1713   if (std::string(theGroupName) != std::string(_enforcedInternalVerticesAllFacesGroup)) {
1714     _enforcedInternalVerticesAllFacesGroup = theGroupName;
1715     NotifySubMeshesHypothesisModification();
1716   }
1717 }
1718
1719 //=============================================================================
1720 BLSURFPlugin_Hypothesis::TPreCadPeriodicityVector BLSURFPlugin_Hypothesis::GetPreCadFacesPeriodicityVector(
1721     const BLSURFPlugin_Hypothesis* hyp) {
1722   return hyp ? hyp->_GetPreCadFacesPeriodicityVector() : GetDefaultPreCadFacesPeriodicityVector();
1723 }
1724
1725 //=============================================================================
1726 BLSURFPlugin_Hypothesis::TPreCadPeriodicityVector BLSURFPlugin_Hypothesis::GetPreCadEdgesPeriodicityVector(
1727     const BLSURFPlugin_Hypothesis* hyp) {
1728   return hyp ? hyp->_GetPreCadEdgesPeriodicityVector() : GetDefaultPreCadEdgesPeriodicityVector();
1729 }
1730
1731 //=============================================================================
1732 BLSURFPlugin_Hypothesis::TFacesPeriodicityVector BLSURFPlugin_Hypothesis::GetFacesPeriodicityVector(
1733     const BLSURFPlugin_Hypothesis* hyp) {
1734   return hyp ? hyp->_GetFacesPeriodicityVector() : GetDefaultFacesPeriodicityVector();
1735 }
1736
1737 //=============================================================================
1738 BLSURFPlugin_Hypothesis::TEdgesPeriodicityVector BLSURFPlugin_Hypothesis::GetEdgesPeriodicityVector(
1739     const BLSURFPlugin_Hypothesis* hyp){
1740   return hyp ? hyp->_GetEdgesPeriodicityVector() : GetDefaultEdgesPeriodicityVector();
1741 }
1742
1743 //=============================================================================
1744 BLSURFPlugin_Hypothesis::TVerticesPeriodicityVector BLSURFPlugin_Hypothesis::GetVerticesPeriodicityVector(
1745     const BLSURFPlugin_Hypothesis* hyp){
1746   return hyp ? hyp->_GetVerticesPeriodicityVector() : GetDefaultVerticesPeriodicityVector();
1747 }
1748
1749 //=======================================================================
1750 //function : ClearAllEnforcedVertices
1751 //=======================================================================
1752 void BLSURFPlugin_Hypothesis::ClearPreCadPeriodicityVectors() {
1753   _preCadFacesPeriodicityVector.clear();
1754   _preCadEdgesPeriodicityVector.clear();
1755   NotifySubMeshesHypothesisModification();
1756 }
1757
1758 //=======================================================================
1759 //function : AddPreCadFacesPeriodicity
1760 //=======================================================================
1761 void BLSURFPlugin_Hypothesis::AddPreCadFacesPeriodicity(TEntry theFace1Entry, TEntry theFace2Entry,
1762     std::vector<std::string> &theSourceVerticesEntries, std::vector<std::string> &theTargetVerticesEntries) {
1763
1764   TPreCadPeriodicity preCadFacesPeriodicity;
1765   preCadFacesPeriodicity.shape1Entry = theFace1Entry;
1766   preCadFacesPeriodicity.shape2Entry = theFace2Entry;
1767   preCadFacesPeriodicity.theSourceVerticesEntries = theSourceVerticesEntries;
1768   preCadFacesPeriodicity.theTargetVerticesEntries = theTargetVerticesEntries;
1769
1770   _preCadFacesPeriodicityVector.push_back(preCadFacesPeriodicity);
1771
1772   NotifySubMeshesHypothesisModification();
1773 }
1774
1775 //=======================================================================
1776 //function : AddPreCadEdgesPeriodicity
1777 //=======================================================================
1778 void BLSURFPlugin_Hypothesis::AddPreCadEdgesPeriodicity(TEntry theEdge1Entry, TEntry theEdge2Entry,
1779     std::vector<std::string> &theSourceVerticesEntries, std::vector<std::string> &theTargetVerticesEntries) {
1780
1781   TPreCadPeriodicity preCadEdgesPeriodicity;
1782   preCadEdgesPeriodicity.shape1Entry = theEdge1Entry;
1783   preCadEdgesPeriodicity.shape2Entry = theEdge2Entry;
1784   preCadEdgesPeriodicity.theSourceVerticesEntries = theSourceVerticesEntries;
1785   preCadEdgesPeriodicity.theTargetVerticesEntries = theTargetVerticesEntries;
1786
1787   _preCadEdgesPeriodicityVector.push_back(preCadEdgesPeriodicity);
1788
1789   NotifySubMeshesHypothesisModification();
1790 }
1791
1792 //=============================================================================
1793 std::ostream & BLSURFPlugin_Hypothesis::SaveTo(std::ostream & save) {
1794    // We must keep at least the same number of arguments when increasing the SALOME version
1795    // When MG-CADSurf becomes CADMESH, some parameters were fused into a single one. Thus the same
1796    // parameter can be written several times to keep the old global number of parameters.
1797
1798    // Treat old options which are now in the advanced options
1799    TOptionValues::iterator op_val;
1800    int _decimesh = -1;
1801    int _preCADRemoveNanoEdges = -1;
1802    double _preCADEpsNano = -1.0;
1803    op_val = _option2value.find("respect_geometry");
1804    if (op_val != _option2value.end()) {
1805      std::string value = op_val->second;
1806      if (!value.empty())
1807        _decimesh = value.compare("1") == 0 ? 1 : 0;
1808    }
1809    op_val = _preCADoption2value.find("remove_tiny_edges");
1810    if (op_val != _preCADoption2value.end()) {
1811      std::string value = op_val->second;
1812      if (!value.empty())
1813        _preCADRemoveNanoEdges = value.compare("1") == 0 ? 1 : 0;
1814    }
1815    op_val = _preCADoption2value.find("tiny_edge_length");
1816    if (op_val != _preCADoption2value.end()) {
1817      std::string value = op_val->second;
1818      if (!value.empty())
1819        _preCADEpsNano = strtod(value.c_str(), NULL);
1820    }
1821    
1822   save << " " << (int) _topology << " " << (int) _physicalMesh << " " << (int) _geometricMesh << " " << _phySize << " "
1823       << _angleMesh << " " << _gradation << " " << (int) _quadAllowed << " " << _decimesh;
1824   save << " " << _minSize << " " << _maxSize << " " << _angleMesh << " " << _minSize << " " << _maxSize << " " << _verb;
1825   save << " " << (int) _preCADMergeEdges << " " << _preCADRemoveNanoEdges << " " << (int) _preCADDiscardInput << " " << _preCADEpsNano ;
1826   save << " " << (int) _enforcedInternalVerticesAllFaces;
1827   save << " " << (int) _phySizeRel << " " << (int) _minSizeRel << " " << (int) _maxSizeRel << " " << _chordalError ;
1828   save << " " << (int) _anisotropic << " " << _anisotropicRatio << " " << (int) _removeTinyEdges << " " << _tinyEdgeLength ;
1829   save << " " << (int) _badElementRemoval << " " << _badElementAspectRatio << " " << (int) _optimizeMesh << " " << (int) _quadraticMesh ;
1830   save << " " << (int) _preCADProcess3DTopology << " " << (int) _preCADRemoveDuplicateCADFaces << " " << (int) _preCADRemoveTinyUVEdges;
1831   save << " " << (int)_optimiseTinyEdges << " " << _tinyEdgeOptimisationLength;
1832   save << " " << (int)_correctSurfaceIntersec << " " << _corrSurfaceIntersCost;
1833   save << " " << (int)_useGradation << " " << (int)_useVolumeGradation << " " << _volumeGradation;
1834
1835   op_val = _option2value.begin();
1836   if (op_val != _option2value.end()) {
1837     save << " " << "__OPTIONS_BEGIN__";
1838     for (; op_val != _option2value.end(); ++op_val) {
1839       if (!op_val->second.empty())
1840         save << " " << op_val->first << " " << op_val->second << "%#"; // "%#" is a mark of value end
1841     }
1842     save << " " << "__OPTIONS_END__";
1843   }
1844   
1845   op_val = _customOption2value.begin();
1846   if (op_val != _customOption2value.end()) {
1847     save << " " << "__CUSTOM_OPTIONS_BEGIN__";
1848     for (; op_val != _customOption2value.end(); ++op_val) {
1849       if (!op_val->second.empty())
1850         save << " " << op_val->first << " " << op_val->second << "%#"; // "%#" is a mark of value end
1851     }
1852     save << " " << "__CUSTOM_OPTIONS_END__";
1853   }
1854
1855   op_val = _preCADoption2value.begin();
1856   if (op_val != _preCADoption2value.end()) {
1857     save << " " << "__PRECAD_OPTIONS_BEGIN__";
1858     for (; op_val != _preCADoption2value.end(); ++op_val) {
1859       if (!op_val->second.empty())
1860         save << " " << op_val->first << " " << op_val->second << "%#"; // "%#" is a mark of value end
1861     }
1862     save << " " << "__PRECAD_OPTIONS_END__";
1863   }
1864
1865   TSizeMap::iterator it_sm = _sizeMap.begin();
1866   if (it_sm != _sizeMap.end()) {
1867     save << " " << "__SIZEMAP_BEGIN__";
1868     for (; it_sm != _sizeMap.end(); ++it_sm) {
1869       save << " " << it_sm->first << " " << it_sm->second << "%#"; // "%#" is a mark of value end
1870     }
1871     save << " " << "__SIZEMAP_END__";
1872   }
1873
1874   TSizeMap::iterator it_at = _attractors.begin();
1875   if (it_at != _attractors.end()) {
1876     save << " " << "__ATTRACTORS_BEGIN__";
1877     for (; it_at != _attractors.end(); ++it_at) {
1878       save << " " << it_at->first << " " << it_at->second << "%#"; // "%#" is a mark of value end
1879     }
1880     save << " " << "__ATTRACTORS_END__";
1881   }
1882   
1883   TAttractorMap::iterator it_At = _classAttractors.begin();
1884   if (it_At != _classAttractors.end()) {
1885     std::ostringstream test;
1886     save << " " << "__NEW_ATTRACTORS_BEGIN__";
1887     test << " " << "__NEW_ATTRACTORS_BEGIN__";
1888     for (; it_At != _classAttractors.end(); ++it_At) {
1889       std::vector<double> attParams;
1890       attParams   = it_At->second->GetParameters();
1891 //       double step = it_At->second->GetStep();
1892       save << " " << it_At->first;
1893       save << " " << it_At->second->GetAttractorEntry();
1894       save << " " << attParams[0]  << " " <<  attParams[1] << " " <<  attParams[2] << " " <<  attParams[3];
1895 //       save << " " << step;
1896       test << " " << it_At->first;
1897       test << " " << it_At->second->GetAttractorEntry();
1898       test << " " << attParams[0]  << " " <<  attParams[1] << " " <<  attParams[2] << " " <<  attParams[3];
1899 //       test << " " << step;
1900     }
1901     save << " " << "__NEW_ATTRACTORS_END__";
1902     test << " " << "__NEW_ATTRACTORS_END__";
1903   }
1904
1905   TEnfVertexList::const_iterator it_enf = _enfVertexList.begin();
1906   if (it_enf != _enfVertexList.end()) {
1907     save << " " << "__ENFORCED_VERTICES_BEGIN__";
1908     for (; it_enf != _enfVertexList.end(); ++it_enf) {
1909       TEnfVertex *enfVertex = (*it_enf);
1910       save << " " << "__BEGIN_VERTEX__";
1911       if (!enfVertex->name.empty()) {
1912         save << " " << "__BEGIN_NAME__";
1913         save << " " << enfVertex->name;
1914         save << " " << "__END_NAME__";
1915       }
1916       if (!enfVertex->geomEntry.empty()) {
1917         save << " " << "__BEGIN_ENTRY__";
1918         save << " " << enfVertex->geomEntry;
1919         save << " " << "__END_ENTRY__";
1920       }
1921       if (!enfVertex->grpName.empty()) {
1922         save << " " << "__BEGIN_GROUP__";
1923         save << " " << enfVertex->grpName;
1924         save << " " << "__END_GROUP__";
1925       }
1926       if (enfVertex->coords.size()) {
1927         save << " " << "__BEGIN_COORDS__";
1928         for ( size_t i = 0; i < enfVertex->coords.size(); i++ )
1929           save << " " << enfVertex->coords[i];
1930         save << " " << "__END_COORDS__";
1931       }
1932       TEntryList::const_iterator faceEntriesIt = enfVertex->faceEntries.begin();
1933       bool hasFaces = false;
1934       if (faceEntriesIt != enfVertex->faceEntries.end()) {
1935         hasFaces = true;
1936         save << " " << "__BEGIN_FACELIST__";
1937       }
1938       for (; faceEntriesIt != enfVertex->faceEntries.end(); ++faceEntriesIt)
1939         if ( faceEntriesIt->empty() )
1940           save << " _no_face_";
1941         else
1942           save << " " << (*faceEntriesIt);
1943       if (hasFaces)
1944         save << " " << "__END_FACELIST__";
1945       save << " " << "__END_VERTEX__";
1946     }
1947     save << " " << "__ENFORCED_VERTICES_END__";
1948   }
1949
1950   //PERIODICITY
1951
1952   SavePreCADPeriodicity(save, "FACES");
1953   SavePreCADPeriodicity(save, "EDGES");
1954
1955   SaveFacesPeriodicity(save);
1956   SaveEdgesPeriodicity(save);
1957   SaveVerticesPeriodicity(save);
1958
1959   return save;
1960 }
1961
1962 void BLSURFPlugin_Hypothesis::SaveFacesPeriodicity(std::ostream & save){
1963
1964   TFacesPeriodicityVector::const_iterator it_faces_periodicity = _facesPeriodicityVector.begin();
1965   if (it_faces_periodicity != _facesPeriodicityVector.end()) {
1966     save << " " << "__FACES_PERIODICITY_BEGIN__";
1967     for (; it_faces_periodicity != _facesPeriodicityVector.end(); ++it_faces_periodicity) {
1968       TFacesPeriodicity periodicity_i = (*it_faces_periodicity);
1969       save << " " << "__BEGIN_PERIODICITY_DESCRIPTION__";
1970       save << " " << "__BEGIN_ENTRY1__";
1971       save << " " << periodicity_i.first;
1972       save << " " << "__END_ENTRY1__";
1973       save << " " << "__BEGIN_ENTRY2__";
1974       save << " " << periodicity_i.second;
1975       save << " " << "__END_ENTRY2__";
1976       save << " " << "__END_PERIODICITY_DESCRIPTION__";
1977     }
1978     save << " " << "__FACES_PERIODICITY_END__";
1979   }
1980 }
1981
1982 void BLSURFPlugin_Hypothesis::SaveEdgesPeriodicity(std::ostream & save){
1983
1984   TEdgesPeriodicityVector::const_iterator it_edges_periodicity = _edgesPeriodicityVector.begin();
1985   if (it_edges_periodicity != _edgesPeriodicityVector.end()) {
1986     save << " " << "__EDGES_PERIODICITY_BEGIN__";
1987     for (; it_edges_periodicity != _edgesPeriodicityVector.end(); ++it_edges_periodicity) {
1988       TEdgePeriodicity periodicity_i = (*it_edges_periodicity);
1989       save << " " << "__BEGIN_PERIODICITY_DESCRIPTION__";
1990       if (! periodicity_i.theFace1Entry.empty()){
1991         save << " " << "__BEGIN_FACE1__";
1992         save << " " << periodicity_i.theFace1Entry;
1993         save << " " << "__END_FACE1__";
1994       }
1995       save << " " << "__BEGIN_EDGE1__";
1996       save << " " << periodicity_i.theEdge1Entry;
1997       save << " " << "__END_EDGE1__";
1998       if (! periodicity_i.theFace2Entry.empty()){
1999         save << " " << "__BEGIN_FACE2__";
2000         save << " " << periodicity_i.theFace2Entry;
2001         save << " " << "__END_FACE2__";
2002       }
2003       save << " " << "__BEGIN_EDGE2__";
2004       save << " " << periodicity_i.theEdge2Entry;
2005       save << " " << "__END_EDGE2__";
2006       save << " " << "__BEGIN_EDGE_ORIENTATION__";
2007       save << " " << periodicity_i.edge_orientation;
2008       save << " " << "__END_EDGE_ORIENTATION__";
2009       save << " " << "__END_PERIODICITY_DESCRIPTION__";
2010     }
2011     save << " " << "__EDGES_PERIODICITY_END__";
2012   }
2013 }
2014
2015 void BLSURFPlugin_Hypothesis::SaveVerticesPeriodicity(std::ostream & save){
2016
2017   TVerticesPeriodicityVector::const_iterator it_vertices_periodicity = _verticesPeriodicityVector.begin();
2018   if (it_vertices_periodicity != _verticesPeriodicityVector.end()) {
2019     save << " " << "__VERTICES_PERIODICITY_BEGIN__";
2020     for (; it_vertices_periodicity != _verticesPeriodicityVector.end(); ++it_vertices_periodicity) {
2021       TVertexPeriodicity periodicity_i = (*it_vertices_periodicity);
2022       save << " " << "__BEGIN_PERIODICITY_DESCRIPTION__";
2023       save << " " << "__BEGIN_EDGE1__";
2024       save << " " << periodicity_i.theEdge1Entry;
2025       save << " " << "__END_EDGE1__";
2026       save << " " << "__BEGIN_VERTEX1__";
2027       save << " " << periodicity_i.theVertex1Entry;
2028       save << " " << "__END_VERTEX1__";
2029       save << " " << "__BEGIN_EDGE2__";
2030       save << " " << periodicity_i.theEdge2Entry;
2031       save << " " << "__END_EDGE2__";
2032       save << " " << "__BEGIN_VERTEX2__";
2033       save << " " << periodicity_i.theVertex2Entry;
2034       save << " " << "__END_VERTEX2__";
2035       save << " " << "__END_PERIODICITY_DESCRIPTION__";
2036     }
2037     save << " " << "__VERTICES_PERIODICITY_END__";
2038   }
2039 }
2040
2041 void BLSURFPlugin_Hypothesis::SavePreCADPeriodicity(std::ostream & save, const char* shapeType) {
2042   TPreCadPeriodicityVector precad_periodicity;
2043   if ( shapeType  &&  strcmp( shapeType, "FACES" ) == 0 )
2044     precad_periodicity = _preCadFacesPeriodicityVector;
2045   else
2046     precad_periodicity = _preCadEdgesPeriodicityVector;
2047   TPreCadPeriodicityVector::const_iterator it_precad_periodicity = precad_periodicity.begin();
2048   if (it_precad_periodicity != precad_periodicity.end()) {
2049     save << " " << "__PRECAD_" << shapeType << "_PERIODICITY_BEGIN__";
2050     for (; it_precad_periodicity != precad_periodicity.end(); ++it_precad_periodicity) {
2051       TPreCadPeriodicity periodicity_i = (*it_precad_periodicity);
2052       save << " " << "__BEGIN_PERIODICITY_DESCRIPTION__";
2053       if (!periodicity_i.shape1Entry.empty()) {
2054         save << " " << "__BEGIN_ENTRY1__";
2055         save << " " << periodicity_i.shape1Entry;
2056         save << " " << "__END_ENTRY1__";
2057       }
2058       if (!periodicity_i.shape2Entry.empty()) {
2059         save << " " << "__BEGIN_ENTRY2__";
2060         save << " " << periodicity_i.shape2Entry;
2061         save << " " << "__END_ENTRY2__";
2062       }
2063
2064       std::vector<std::string>::const_iterator sourceVerticesEntriesIt = periodicity_i.theSourceVerticesEntries.begin();
2065       bool hasSourceVertices = false;
2066       if (sourceVerticesEntriesIt != periodicity_i.theSourceVerticesEntries.end()) {
2067         hasSourceVertices = true;
2068         save << " " << "__BEGIN_SOURCE_VERTICES_LIST__";
2069       }
2070       for (; sourceVerticesEntriesIt != periodicity_i.theSourceVerticesEntries.end(); ++sourceVerticesEntriesIt)
2071         save << " " << (*sourceVerticesEntriesIt);
2072       if (hasSourceVertices)
2073         save << " " << "__END_SOURCE_VERTICES_LIST__";
2074
2075       std::vector<std::string>::const_iterator targetVerticesEntriesIt = periodicity_i.theTargetVerticesEntries.begin();
2076       bool hasTargetVertices = false;
2077       if (targetVerticesEntriesIt != periodicity_i.theTargetVerticesEntries.end()) {
2078         hasTargetVertices = true;
2079         save << " " << "__BEGIN_TARGET_VERTICES_LIST__";
2080       }
2081       for (; targetVerticesEntriesIt != periodicity_i.theTargetVerticesEntries.end(); ++targetVerticesEntriesIt)
2082         save << " " << (*targetVerticesEntriesIt);
2083       if (hasTargetVertices)
2084         save << " " << "__END_TARGET_VERTICES_LIST__";
2085
2086       save << " " << "__END_PERIODICITY_DESCRIPTION__";
2087     }
2088     save << " " << "__PRECAD_" << shapeType << "_PERIODICITY_END__";
2089   }
2090
2091 }
2092
2093 //=============================================================================
2094 std::istream & BLSURFPlugin_Hypothesis::LoadFrom(std::istream & load) {
2095   bool isOK = true;
2096   int i;
2097   double val;
2098   std::string option_or_sm;
2099
2100   isOK = static_cast<bool>(load >> i);
2101   if (isOK)
2102     _topology = (Topology) i;
2103   else
2104     load.clear(std::ios::badbit | load.rdstate());
2105
2106   isOK = static_cast<bool>(load >> i);
2107   if (isOK)
2108     _physicalMesh = (PhysicalMesh) i;
2109   else
2110     load.clear(std::ios::badbit | load.rdstate());
2111
2112   isOK = static_cast<bool>(load >> i);
2113   if (isOK)
2114     _geometricMesh = (GeometricMesh) i;
2115   else
2116     load.clear(std::ios::badbit | load.rdstate());
2117
2118   isOK = static_cast<bool>(load >> val);
2119   if (isOK)
2120     _phySize = val;
2121   else
2122     load.clear(std::ios::badbit | load.rdstate());
2123
2124   isOK = static_cast<bool>(load >> val);
2125   if (isOK)
2126     _angleMesh = val;
2127   else
2128     load.clear(std::ios::badbit | load.rdstate());
2129
2130   isOK = static_cast<bool>(load >> val);
2131   if (isOK)
2132     _gradation = val;
2133   else
2134     load.clear(std::ios::badbit | load.rdstate());
2135
2136   isOK = static_cast<bool>(load >> i);
2137   if (isOK)
2138     _quadAllowed = (bool) i;
2139   else
2140     load.clear(std::ios::badbit | load.rdstate());
2141
2142   isOK = static_cast<bool>(load >> i);
2143   if (isOK) {
2144     if ( i != -1) { // if value is -1, then this is no longer a standard option
2145       std::string & value = _option2value["respect_geometry"];
2146       bool _decimesh = (bool) i;
2147       value = _decimesh ? "1" : "0";
2148     }
2149   }
2150   else
2151     load.clear(std::ios::badbit | load.rdstate());
2152
2153   isOK = static_cast<bool>(load >> val);
2154   if (isOK)
2155     _minSize = val;
2156   else
2157     load.clear(std::ios::badbit | load.rdstate());
2158
2159   isOK = static_cast<bool>(load >> val);
2160   if (isOK)
2161     _maxSize = val;
2162   else
2163     load.clear(std::ios::badbit | load.rdstate());
2164
2165   isOK = static_cast<bool>(load >> val);
2166   if (isOK)
2167     // former parameter: get min value
2168     _angleMesh = std::min(val,_angleMesh);
2169   else
2170     load.clear(std::ios::badbit | load.rdstate());
2171
2172   isOK = static_cast<bool>(load >> val);
2173   if (isOK)
2174     // former parameter: get min value
2175     _minSize = std::min(val,_minSize);
2176   else
2177     load.clear(std::ios::badbit | load.rdstate());
2178
2179   isOK = static_cast<bool>(load >> val);
2180   if (isOK)
2181     // former parameter: get max value
2182     _maxSize = std::max(val,_maxSize);
2183   else
2184     load.clear(std::ios::badbit | load.rdstate());
2185
2186   isOK = static_cast<bool>(load >> i);
2187   if (isOK)
2188     _verb = i;
2189   else
2190     load.clear(std::ios::badbit | load.rdstate());
2191
2192   isOK = static_cast<bool>(load >> i);
2193   if (isOK)
2194     _preCADMergeEdges = (bool) i;
2195   else
2196     load.clear(std::ios::badbit | load.rdstate());
2197
2198   isOK = static_cast<bool>(load >> i);
2199   if (isOK) {
2200     if ( i != -1) { // if value is -1, then this is no longer a standard option
2201       std::string & value = _preCADoption2value["remove_tiny_edges"];
2202       bool _preCADRemoveNanoEdges = (bool) i;
2203       value = _preCADRemoveNanoEdges ? "1" : "0";
2204     }
2205   }
2206   else
2207     load.clear(std::ios::badbit | load.rdstate());
2208
2209   isOK = static_cast<bool>(load >> i);
2210   if (isOK)
2211     _preCADDiscardInput = (bool) i;
2212   else
2213     load.clear(std::ios::badbit | load.rdstate());
2214
2215   isOK = static_cast<bool>(load >> val);
2216   if (isOK) { // _preCADEpsNano
2217     if ( (i + 1.0) < 1e-6 ) { // if value is -1, then this is no longer a standard option: get optional value "tiny_edge_length" instead
2218       std::string & value = _preCADoption2value["tiny_edge_length"];
2219       std::ostringstream oss;
2220       oss << i;
2221       value = oss.str();
2222     }
2223   }
2224   else
2225     load.clear(std::ios::badbit | load.rdstate());
2226
2227   isOK = static_cast<bool>(load >> i);
2228   if (isOK)
2229     _enforcedInternalVerticesAllFaces = (bool) i;
2230   else
2231     load.clear(std::ios::badbit | load.rdstate());
2232
2233   // New options with MeshGems-CADSurf
2234
2235   bool hasCADSurfOptions = false;
2236   bool hasOptions = false;
2237   bool hasCustomOptions = false;
2238   bool hasPreCADOptions = false;
2239   bool hasSizeMap = false;
2240   bool hasAttractor = false;
2241   bool hasNewAttractor = false;
2242   bool hasEnforcedVertex = false;
2243   bool hasPreCADFacesPeriodicity = false;
2244   bool hasPreCADEdgesPeriodicity = false;
2245   bool hasFacesPeriodicity = false;
2246   bool hasEdgesPeriodicity = false;
2247   bool hasVerticesPeriodicity = false;
2248
2249   isOK = static_cast<bool>(load >> option_or_sm);
2250   if (isOK)
2251     if ( (option_or_sm == "1")||(option_or_sm == "0") ) {
2252       i = atoi(option_or_sm.c_str());
2253       hasCADSurfOptions = true;
2254       _phySizeRel = (bool) i;
2255     }
2256     if (option_or_sm == "__OPTIONS_BEGIN__")
2257       hasOptions = true;
2258     else if (option_or_sm == "__CUSTOM_OPTIONS_BEGIN__")
2259       hasCustomOptions = true;
2260     else if (option_or_sm == "__PRECAD_OPTIONS_BEGIN__")
2261       hasPreCADOptions = true;
2262     else if (option_or_sm == "__SIZEMAP_BEGIN__")
2263       hasSizeMap = true;
2264     else if (option_or_sm == "__ATTRACTORS_BEGIN__")
2265       hasAttractor = true;
2266     else if (option_or_sm == "__NEW_ATTRACTORS_BEGIN__")
2267       hasNewAttractor = true;
2268     else if (option_or_sm == "__ENFORCED_VERTICES_BEGIN__")
2269       hasEnforcedVertex = true;
2270     else if (option_or_sm == "__PRECAD_FACES_PERIODICITY_BEGIN__")
2271       hasPreCADFacesPeriodicity = true;
2272     else if (option_or_sm == "__PRECAD_EDGES_PERIODICITY_BEGIN__")
2273       hasPreCADEdgesPeriodicity = true;
2274     else if (option_or_sm == "__FACES_PERIODICITY_BEGIN__")
2275       hasFacesPeriodicity = true;
2276     else if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2277       hasEdgesPeriodicity = true;
2278     else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2279       hasVerticesPeriodicity = true;
2280
2281   if (isOK && hasCADSurfOptions) {
2282     isOK = static_cast<bool>(load >> i);
2283     if (isOK)
2284       _minSizeRel = (bool) i;
2285     else
2286       load.clear(std::ios::badbit | load.rdstate());
2287
2288     isOK = static_cast<bool>(load >> i);
2289     if (isOK)
2290       _maxSizeRel = (bool) i;
2291     else
2292       load.clear(std::ios::badbit | load.rdstate());
2293
2294     isOK = static_cast<bool>(load >> val);
2295     if (isOK)
2296       _chordalError = val;
2297     else
2298       load.clear(std::ios::badbit | load.rdstate());
2299
2300     isOK = static_cast<bool>(load >> i);
2301     if (isOK)
2302       _anisotropic = (bool) i;
2303     else
2304       load.clear(std::ios::badbit | load.rdstate());
2305
2306     isOK = static_cast<bool>(load >> val);
2307     if (isOK)
2308       _anisotropicRatio = val;
2309     else
2310       load.clear(std::ios::badbit | load.rdstate());
2311
2312     isOK = static_cast<bool>(load >> i);
2313     if (isOK)
2314       _removeTinyEdges = (bool) i;
2315     else
2316       load.clear(std::ios::badbit | load.rdstate());
2317
2318     isOK = static_cast<bool>(load >> val);
2319     if (isOK)
2320       _tinyEdgeLength = val;
2321     else
2322       load.clear(std::ios::badbit | load.rdstate());
2323
2324     isOK = static_cast<bool>(load >> i);
2325     if (isOK)
2326       _badElementRemoval = (bool) i;
2327     else
2328       load.clear(std::ios::badbit | load.rdstate());
2329
2330     isOK = static_cast<bool>(load >> val);
2331     if (isOK)
2332       _badElementAspectRatio = val;
2333     else
2334       load.clear(std::ios::badbit | load.rdstate());
2335
2336     isOK = static_cast<bool>(load >> i);
2337     if (isOK)
2338       _optimizeMesh = (bool) i;
2339     else
2340       load.clear(std::ios::badbit | load.rdstate());
2341
2342     isOK = static_cast<bool>(load >> i);
2343     if (isOK)
2344       _quadraticMesh = (bool) i;
2345     else
2346       load.clear(std::ios::badbit | load.rdstate());
2347
2348     isOK = static_cast<bool>(load >> i);
2349     if (isOK)
2350       _preCADProcess3DTopology = (bool) i;
2351     else
2352       load.clear(std::ios::badbit | load.rdstate());
2353
2354     if (( load >> std::ws).peek() != '_' )
2355     {
2356       isOK = static_cast<bool>(load >> i);
2357       if (isOK)
2358         _preCADRemoveDuplicateCADFaces = (bool) i;
2359       else
2360         load.clear(std::ios::badbit | load.rdstate());
2361
2362       isOK = static_cast<bool>(load >> i);
2363       if (isOK)
2364         _preCADRemoveTinyUVEdges = (bool) i;
2365       else
2366         load.clear(std::ios::badbit | load.rdstate());
2367
2368       isOK = static_cast<bool>(load >> i);
2369       if (isOK)
2370         _optimiseTinyEdges = (bool) i;
2371       else
2372         load.clear(std::ios::badbit | load.rdstate());
2373
2374       isOK = static_cast<bool>(load >> val);
2375       if (isOK)
2376         _tinyEdgeOptimisationLength = val;
2377       else
2378         load.clear(std::ios::badbit | load.rdstate());
2379
2380       isOK = static_cast<bool>(load >> i);
2381       if (isOK)
2382         _correctSurfaceIntersec = (bool) i;
2383       else
2384         load.clear(std::ios::badbit | load.rdstate());
2385
2386       isOK = static_cast<bool>(load >> val);
2387       if (isOK)
2388         _corrSurfaceIntersCost = val;
2389       else
2390         load.clear(std::ios::badbit | load.rdstate());
2391
2392       isOK = static_cast<bool>(load >> i);
2393       if (isOK)
2394         _useGradation = (bool) i;
2395       else
2396         load.clear(std::ios::badbit | load.rdstate());
2397
2398       isOK = static_cast<bool>(load >> i);
2399       if (isOK)
2400         _useVolumeGradation = (bool) i;
2401       else
2402         load.clear(std::ios::badbit | load.rdstate());
2403
2404       isOK = static_cast<bool>(load >> val);
2405       if (isOK)
2406         _volumeGradation = val;
2407       else
2408         load.clear(std::ios::badbit | load.rdstate());
2409     }
2410   }
2411
2412
2413   if (hasCADSurfOptions) {
2414     isOK = static_cast<bool>(load >> option_or_sm);
2415     if (isOK) {
2416       if (option_or_sm == "__OPTIONS_BEGIN__")
2417         hasOptions = true;
2418       else if (option_or_sm == "__CUSTOM_OPTIONS_BEGIN__")
2419         hasCustomOptions = true;
2420       else if (option_or_sm == "__PRECAD_OPTIONS_BEGIN__")
2421         hasPreCADOptions = true;
2422       else if (option_or_sm == "__SIZEMAP_BEGIN__")
2423         hasSizeMap = true;
2424       else if (option_or_sm == "__ATTRACTORS_BEGIN__")
2425         hasAttractor = true;
2426       else if (option_or_sm == "__NEW_ATTRACTORS_BEGIN__")
2427         hasNewAttractor = true;
2428       else if (option_or_sm == "__ENFORCED_VERTICES_BEGIN__")
2429         hasEnforcedVertex = true;
2430       else if (option_or_sm == "__PRECAD_FACES_PERIODICITY_BEGIN__")
2431         hasPreCADFacesPeriodicity = true;
2432       else if (option_or_sm == "__PRECAD_EDGES_PERIODICITY_BEGIN__")
2433         hasPreCADEdgesPeriodicity = true;
2434       else if (option_or_sm == "__FACES_PERIODICITY_BEGIN__")
2435         hasFacesPeriodicity = true;
2436       else if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2437         hasEdgesPeriodicity = true;
2438       else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2439         hasVerticesPeriodicity = true;
2440     }
2441   }
2442   
2443   std::string optName, optValue;
2444   while (isOK && hasOptions) {
2445     isOK = static_cast<bool>(load >> optName);
2446     if (isOK) {
2447       if (optName == "__OPTIONS_END__")
2448         break;
2449       isOK = static_cast<bool>(load >> optValue);
2450     }
2451     if (isOK) {
2452       std::string & value = _option2value[optName];
2453       value = optValue;
2454       int len = value.size();
2455       // continue reading until "%#" encountered
2456       while (value[len - 1] != '#' || value[len - 2] != '%') {
2457         isOK = static_cast<bool>(load >> optValue);
2458         if (isOK) {
2459           value += " ";
2460           value += optValue;
2461           len = value.size();
2462         } else {
2463           break;
2464         }
2465       }
2466       if ( value[ len - 1] == '#' )
2467         value.resize(len - 2); //cut off "%#"
2468     }
2469   }
2470
2471   if (hasOptions) {
2472     isOK = static_cast<bool>(load >> option_or_sm);
2473     if (isOK) {
2474       if (option_or_sm == "__CUSTOM_OPTIONS_BEGIN__")
2475         hasCustomOptions = true;
2476       else if (option_or_sm == "__PRECAD_OPTIONS_BEGIN__")
2477         hasPreCADOptions = true;
2478       else if (option_or_sm == "__SIZEMAP_BEGIN__")
2479         hasSizeMap = true;
2480       else if (option_or_sm == "__ATTRACTORS_BEGIN__")
2481         hasAttractor = true;
2482       else if (option_or_sm == "__NEW_ATTRACTORS_BEGIN__")
2483         hasNewAttractor = true;
2484       else if (option_or_sm == "__ENFORCED_VERTICES_BEGIN__")
2485         hasEnforcedVertex = true;
2486       else if (option_or_sm == "__PRECAD_FACES_PERIODICITY_BEGIN__")
2487         hasPreCADFacesPeriodicity = true;
2488       else if (option_or_sm == "__PRECAD_EDGES_PERIODICITY_BEGIN__")
2489         hasPreCADEdgesPeriodicity = true;
2490       else if (option_or_sm == "__FACES_PERIODICITY_BEGIN__")
2491         hasFacesPeriodicity = true;
2492       else if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2493         hasEdgesPeriodicity = true;
2494       else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2495         hasVerticesPeriodicity = true;
2496     }
2497   }
2498
2499   while (isOK && hasCustomOptions) {
2500     isOK = static_cast<bool>(load >> optName);
2501     if (isOK) {
2502       if (optName == "__CUSTOM_OPTIONS_END__")
2503         break;
2504       isOK = static_cast<bool>(load >> optValue);
2505     }
2506     if (isOK) {
2507       std::string& value = optValue;
2508       int len = value.size();
2509       // continue reading until "%#" encountered
2510       while (value[len - 1] != '#' || value[len - 2] != '%') {
2511         isOK = static_cast<bool>(load >> optValue);
2512         if (isOK) {
2513           value += " ";
2514           value += optValue;
2515           len = value.size();
2516         } else {
2517           break;
2518         }
2519       }
2520       if ( value[ len - 1] == '#' )
2521         value.resize(len - 2); //cut off "%#"
2522       _customOption2value[optName] = value;
2523     }
2524   }
2525
2526   if (hasCustomOptions) {
2527     isOK = static_cast<bool>(load >> option_or_sm);
2528     if (isOK) {
2529       if (option_or_sm == "__PRECAD_OPTIONS_BEGIN__")
2530         hasPreCADOptions = true;
2531       else if (option_or_sm == "__SIZEMAP_BEGIN__")
2532         hasSizeMap = true;
2533       else if (option_or_sm == "__ATTRACTORS_BEGIN__")
2534         hasAttractor = true;
2535       else if (option_or_sm == "__NEW_ATTRACTORS_BEGIN__")
2536         hasNewAttractor = true;
2537       else if (option_or_sm == "__ENFORCED_VERTICES_BEGIN__")
2538         hasEnforcedVertex = true;
2539       else if (option_or_sm == "__PRECAD_FACES_PERIODICITY_BEGIN__")
2540         hasPreCADFacesPeriodicity = true;
2541       else if (option_or_sm == "__PRECAD_EDGES_PERIODICITY_BEGIN__")
2542         hasPreCADEdgesPeriodicity = true;
2543       else if (option_or_sm == "__FACES_PERIODICITY_BEGIN__")
2544         hasFacesPeriodicity = true;
2545       else if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2546         hasEdgesPeriodicity = true;
2547       else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2548         hasVerticesPeriodicity = true;
2549     }
2550   }
2551
2552   while (isOK && hasPreCADOptions) {
2553     isOK = static_cast<bool>(load >> optName);
2554     if (isOK) {
2555       if (optName == "__PRECAD_OPTIONS_END__")
2556         break;
2557       isOK = static_cast<bool>(load >> optValue);
2558     }
2559     if (isOK) {
2560       std::string & value = _preCADoption2value[optName];
2561       value = optValue;
2562       int len = value.size();
2563       // continue reading until "%#" encountered
2564       while (value[len - 1] != '#' || value[len - 2] != '%') {
2565         isOK = static_cast<bool>(load >> optValue);
2566         if (isOK) {
2567           value += " ";
2568           value += optValue;
2569           len = value.size();
2570         } else {
2571           break;
2572         }
2573       }
2574       if ( value[ len - 1] == '#' )
2575         value.resize(len - 2); //cut off "%#"
2576     }
2577   }
2578
2579   if (hasPreCADOptions) {
2580     isOK = static_cast<bool>(load >> option_or_sm);
2581     if (isOK) {
2582       if (option_or_sm == "__SIZEMAP_BEGIN__")
2583         hasSizeMap = true;
2584       else if (option_or_sm == "__ATTRACTORS_BEGIN__")
2585         hasAttractor = true;
2586       else if (option_or_sm == "__NEW_ATTRACTORS_BEGIN__")
2587         hasNewAttractor = true;
2588       else if (option_or_sm == "__ENFORCED_VERTICES_BEGIN__")
2589         hasEnforcedVertex = true;
2590       else if (option_or_sm == "__PRECAD_FACES_PERIODICITY_BEGIN__")
2591         hasPreCADFacesPeriodicity = true;
2592       else if (option_or_sm == "__PRECAD_EDGES_PERIODICITY_BEGIN__")
2593         hasPreCADEdgesPeriodicity = true;
2594       else if (option_or_sm == "__FACES_PERIODICITY_BEGIN__")
2595         hasFacesPeriodicity = true;
2596       else if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2597         hasEdgesPeriodicity = true;
2598       else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2599         hasVerticesPeriodicity = true;
2600     }
2601   }
2602  
2603   std::string smEntry, smValue;
2604   while (isOK && hasSizeMap) {
2605     isOK = static_cast<bool>(load >> smEntry);
2606     if (isOK) {
2607       if (smEntry == "__SIZEMAP_END__")
2608         break;
2609       isOK = static_cast<bool>(load >> smValue);
2610     }
2611     if (isOK) {
2612       std::string & value2 = _sizeMap[smEntry];
2613       value2 = smValue;
2614       int len2 = value2.size();
2615       // continue reading until "%#" encountered
2616       while (value2[len2 - 1] != '#' || value2[len2 - 2] != '%') {
2617         isOK = static_cast<bool>(load >> smValue);
2618         if (isOK) {
2619           value2 += " ";
2620           value2 += smValue;
2621           len2 = value2.size();
2622         } else {
2623           break;
2624         }
2625       }
2626       value2.resize(len2 - 2); //cut off "%#"
2627     }
2628   }
2629
2630   if (hasSizeMap) {
2631     isOK = static_cast<bool>(load >> option_or_sm);
2632     if (isOK)
2633       if (option_or_sm == "__ATTRACTORS_BEGIN__")
2634         hasAttractor = true;
2635       if (option_or_sm == "__NEW_ATTRACTORS_BEGIN__")
2636         hasNewAttractor = true;
2637       else if (option_or_sm == "__ENFORCED_VERTICES_BEGIN__")
2638         hasEnforcedVertex = true;
2639       else if (option_or_sm == "__PRECAD_FACES_PERIODICITY_BEGIN__")
2640         hasPreCADFacesPeriodicity = true;
2641       else if (option_or_sm == "__PRECAD_EDGES_PERIODICITY_BEGIN__")
2642         hasPreCADEdgesPeriodicity = true;
2643       else if (option_or_sm == "__FACES_PERIODICITY_BEGIN__")
2644         hasFacesPeriodicity = true;
2645       else if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2646         hasEdgesPeriodicity = true;
2647       else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2648         hasVerticesPeriodicity = true;
2649   }
2650
2651   std::string atEntry, atValue;
2652   while (isOK && hasAttractor) {
2653     isOK = static_cast<bool>(load >> atEntry);
2654     if (isOK) {
2655       if (atEntry == "__ATTRACTORS_END__")
2656         break;
2657       isOK = static_cast<bool>(load >> atValue);
2658     }
2659     if (isOK) {
2660       std::string & value3 = _attractors[atEntry];
2661       value3 = atValue;
2662       int len3 = value3.size();
2663       // continue reading until "%#" encountered
2664       while (value3[len3 - 1] != '#' || value3[len3 - 2] != '%') {
2665         isOK = static_cast<bool>(load >> atValue);
2666         if (isOK) {
2667           value3 += " ";
2668           value3 += atValue;
2669           len3 = value3.size();
2670         } else {
2671           break;
2672         }
2673       }
2674       value3.resize(len3 - 2); //cut off "%#"
2675     }
2676   }
2677
2678   if (hasAttractor) {
2679     isOK = static_cast<bool>(load >> option_or_sm);
2680     if (isOK) {
2681       if (option_or_sm == "__NEW_ATTRACTORS_BEGIN__")
2682         hasNewAttractor = true;
2683       else if (option_or_sm == "__ENFORCED_VERTICES_BEGIN__")
2684         hasEnforcedVertex = true;
2685       else if (option_or_sm == "__PRECAD_FACES_PERIODICITY_BEGIN__")
2686         hasPreCADFacesPeriodicity = true;
2687       else if (option_or_sm == "__PRECAD_EDGES_PERIODICITY_BEGIN__")
2688         hasPreCADEdgesPeriodicity = true;
2689       else if (option_or_sm == "__FACES_PERIODICITY_BEGIN__")
2690         hasFacesPeriodicity = true;
2691       else if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2692         hasEdgesPeriodicity = true;
2693       else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2694         hasVerticesPeriodicity = true;
2695     }
2696   }
2697
2698   std::string newAtFaceEntry, atTestString;
2699   std::string newAtShapeEntry;
2700   double attParams[4];
2701   //double step;
2702   while (isOK && hasNewAttractor) {
2703     //std::cout<<"Load new attractor"<<std::endl;
2704     isOK = static_cast<bool>(load >> newAtFaceEntry);
2705     if (isOK) {
2706       if (newAtFaceEntry == "__NEW_ATTRACTORS_END__")
2707         break;
2708       isOK = static_cast<bool>(load >> newAtShapeEntry);
2709       if (!isOK)
2710     break;
2711       isOK = static_cast<bool>(load >> attParams[0]>>attParams[1]>>attParams[2]>>attParams[3]); //>>step);
2712     }
2713     if (isOK) {
2714       const TopoDS_Shape attractorShape = BLSURFPlugin_Hypothesis::entryToShape(newAtShapeEntry);
2715       const TopoDS_Face faceShape = TopoDS::Face(BLSURFPlugin_Hypothesis::entryToShape(newAtFaceEntry));
2716       BLSURFPlugin_Attractor* attractor = new BLSURFPlugin_Attractor(faceShape, attractorShape, newAtShapeEntry);//, step);
2717       attractor->SetParameters(attParams[0], attParams[1], attParams[2], attParams[3]);
2718       //attractor->BuildMap();                     
2719       _classAttractors.insert( make_pair( newAtFaceEntry, attractor ));
2720     }
2721   }
2722   
2723   
2724   if (hasNewAttractor) {
2725     isOK = static_cast<bool>(load >> option_or_sm);
2726     if (isOK) {
2727       if (option_or_sm == "__ENFORCED_VERTICES_BEGIN__")
2728         hasEnforcedVertex = true;
2729       else if (option_or_sm == "__PRECAD_FACES_PERIODICITY_BEGIN__")
2730         hasPreCADFacesPeriodicity = true;
2731       else if (option_or_sm == "__PRECAD_EDGES_PERIODICITY_BEGIN__")
2732         hasPreCADEdgesPeriodicity = true;
2733       else if (option_or_sm == "__FACES_PERIODICITY_BEGIN__")
2734         hasFacesPeriodicity = true;
2735       else if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2736         hasEdgesPeriodicity = true;
2737       else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2738         hasVerticesPeriodicity = true;
2739     }
2740   }
2741
2742
2743 // 
2744 // Here is a example of the saved stream:
2745 // __ENFORCED_VERTICES_BEGIN__ 
2746 // __BEGIN_VERTEX__  => no name, no entry
2747 // __BEGIN_GROUP__ mon groupe __END_GROUP__
2748 // __BEGIN_COORDS__ 10 10 10 __END_COORDS__ 
2749 // __BEGIN_FACELIST__ 0:1:1:1:1 __END_FACELIST__ 
2750 // __END_VERTEX__ 
2751 // __BEGIN_VERTEX__ => no coords
2752 // __BEGIN_NAME__ mes points __END_NAME__ 
2753 // __BEGIN_ENTRY__ 0:1:1:4 __END_ENTRY__
2754 // __BEGIN_GROUP__ mon groupe __END_GROUP__
2755 // __BEGIN_FACELIST__ 0:1:1:1:3 __END_FACELIST__
2756 // __END_VERTEX__ 
2757 // __ENFORCED_VERTICES_END__
2758 // 
2759
2760   std::string enfSeparator;
2761   std::string enfName;
2762   std::string enfGeomEntry;
2763   std::string enfGroup;
2764   TEntryList enfFaceEntryList;
2765   double enfCoords[3];
2766   bool hasCoords = false;
2767   
2768   _faceEntryEnfVertexListMap.clear();
2769   _enfVertexList.clear();
2770   _faceEntryCoordsListMap.clear();
2771   _coordsEnfVertexMap.clear();
2772   _faceEntryEnfVertexEntryListMap.clear();
2773   _enfVertexEntryEnfVertexMap.clear();
2774   
2775   
2776   while (isOK && hasEnforcedVertex) {
2777     isOK = static_cast<bool>(load >> enfSeparator); // __BEGIN_VERTEX__
2778     TEnfVertex *enfVertex = new TEnfVertex();
2779     if (enfSeparator == "__ENFORCED_VERTICES_END__")
2780       break; // __ENFORCED_VERTICES_END__
2781     if (enfSeparator != "__BEGIN_VERTEX__")
2782       throw std::exception();
2783     
2784     while (isOK) {
2785       isOK = static_cast<bool>(load >> enfSeparator);
2786       if (enfSeparator == "__END_VERTEX__") {
2787         
2788         enfVertex->name = enfName;
2789         enfVertex->geomEntry = enfGeomEntry;
2790         enfVertex->grpName = enfGroup;
2791         enfVertex->coords.clear();
2792         if (hasCoords)
2793           enfVertex->coords.assign(enfCoords,enfCoords+3);
2794         enfVertex->faceEntries = enfFaceEntryList;
2795         
2796         _enfVertexList.insert(enfVertex);
2797         
2798         if (enfVertex->coords.size()) {
2799           _coordsEnfVertexMap[enfVertex->coords] = enfVertex;
2800           for (TEntryList::const_iterator it = enfVertex->faceEntries.begin() ; it != enfVertex->faceEntries.end(); ++it) {
2801             _faceEntryCoordsListMap[(*it)].insert(enfVertex->coords);
2802             _faceEntryEnfVertexListMap[(*it)].insert(enfVertex);
2803           }
2804         }
2805         if (!enfVertex->geomEntry.empty()) {
2806           _enfVertexEntryEnfVertexMap[enfVertex->geomEntry] = enfVertex;
2807           for (TEntryList::const_iterator it = enfVertex->faceEntries.begin() ; it != enfVertex->faceEntries.end(); ++it) {
2808             _faceEntryEnfVertexEntryListMap[(*it)].insert(enfVertex->geomEntry);
2809             _faceEntryEnfVertexListMap[(*it)].insert(enfVertex);
2810           }
2811         }
2812         
2813         enfName.clear();
2814         enfGeomEntry.clear();
2815         enfGroup.clear();
2816         enfFaceEntryList.clear();
2817         hasCoords = false;
2818         break; // __END_VERTEX__
2819       }
2820         
2821       if (enfSeparator == "__BEGIN_NAME__") {  // __BEGIN_NAME__
2822         while (isOK && (enfSeparator != "__END_NAME__")) {
2823           isOK = static_cast<bool>(load >> enfSeparator);
2824           if (enfSeparator != "__END_NAME__") {
2825             if (!enfName.empty())
2826               enfName += " ";
2827             enfName += enfSeparator;
2828           }
2829         }
2830       }
2831         
2832       if (enfSeparator == "__BEGIN_ENTRY__") {  // __BEGIN_ENTRY__
2833         isOK = static_cast<bool>(load >> enfGeomEntry);
2834         isOK = static_cast<bool>(load >> enfSeparator); // __END_ENTRY__
2835         if (enfSeparator != "__END_ENTRY__")
2836           throw std::exception();
2837       }
2838         
2839       if (enfSeparator == "__BEGIN_GROUP__") {  // __BEGIN_GROUP__
2840         while (isOK && (enfSeparator != "__END_GROUP__")) {
2841           isOK = static_cast<bool>(load >> enfSeparator);
2842           if (enfSeparator != "__END_GROUP__") {
2843             if (!enfGroup.empty())
2844               enfGroup += " ";
2845             enfGroup += enfSeparator;
2846           }
2847         }
2848       }
2849         
2850       if (enfSeparator == "__BEGIN_COORDS__") {  // __BEGIN_COORDS__
2851         hasCoords = true;
2852         isOK = static_cast<bool>(load >> enfCoords[0] >> enfCoords[1] >> enfCoords[2]);
2853         isOK = static_cast<bool>(load >> enfSeparator); // __END_COORDS__
2854         if (enfSeparator != "__END_COORDS__")
2855           throw std::exception();
2856       } 
2857         
2858       if (enfSeparator == "__BEGIN_FACELIST__") {  // __BEGIN_FACELIST__
2859         while (isOK && (enfSeparator != "__END_FACELIST__")) {
2860           isOK = static_cast<bool>(load >> enfSeparator);
2861           if (enfSeparator != "__END_FACELIST__") {
2862             enfFaceEntryList.insert(enfSeparator);
2863           }
2864         }
2865       } 
2866     }
2867   }
2868
2869   // PERIODICITY
2870
2871   if (hasPreCADFacesPeriodicity){
2872     LoadPreCADPeriodicity(load, "FACES");
2873
2874     isOK = static_cast<bool>(load >> option_or_sm);
2875     if (isOK) {
2876       if (option_or_sm == "__PRECAD_EDGES_PERIODICITY_BEGIN__")
2877         hasPreCADEdgesPeriodicity = true;
2878       else if (option_or_sm == "__FACES_PERIODICITY_BEGIN__")
2879         hasFacesPeriodicity = true;
2880       else if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2881         hasEdgesPeriodicity = true;
2882       else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2883         hasVerticesPeriodicity = true;
2884     }
2885   }
2886
2887   if (hasPreCADEdgesPeriodicity){
2888     LoadPreCADPeriodicity(load, "EDGES");
2889
2890     isOK = static_cast<bool>(load >> option_or_sm);
2891     if (isOK) {
2892       if (option_or_sm == "__FACES_PERIODICITY_BEGIN__")
2893         hasFacesPeriodicity = true;
2894       else if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2895         hasEdgesPeriodicity = true;
2896       else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2897         hasVerticesPeriodicity = true;
2898     }
2899   }
2900
2901   if (hasFacesPeriodicity){
2902       LoadFacesPeriodicity(load);
2903
2904     isOK = static_cast<bool>(load >> option_or_sm);
2905     if (isOK) {
2906       if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2907         hasEdgesPeriodicity = true;
2908       else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2909         hasVerticesPeriodicity = true;
2910     }
2911   }
2912
2913   if (hasEdgesPeriodicity){
2914       LoadEdgesPeriodicity(load);
2915
2916     isOK = static_cast<bool>(load >> option_or_sm);
2917     if (isOK)
2918       if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2919         hasVerticesPeriodicity = true;
2920   }
2921
2922   if (hasVerticesPeriodicity)
2923       LoadVerticesPeriodicity(load);
2924
2925   return load;
2926 }
2927
2928 void BLSURFPlugin_Hypothesis::LoadFacesPeriodicity(std::istream & load){
2929
2930   bool isOK = true;
2931
2932   std::string periodicitySeparator;
2933   TEntry shape1Entry;
2934   TEntry shape2Entry;
2935
2936   _facesPeriodicityVector.clear();
2937
2938   while (isOK) {
2939     isOK = static_cast<bool>(load >> periodicitySeparator); // __BEGIN_PERIODICITY_DESCRIPTION__
2940     TFacesPeriodicity *periodicity_i = new TFacesPeriodicity();
2941     if (periodicitySeparator == "__FACES_PERIODICITY_END__")
2942       break; // __FACES_PERIODICITY_END__
2943     if (periodicitySeparator != "__BEGIN_PERIODICITY_DESCRIPTION__"){
2944       throw std::exception();
2945     }
2946
2947     while (isOK) {
2948       isOK = static_cast<bool>(load >> periodicitySeparator);
2949       if (periodicitySeparator == "__END_PERIODICITY_DESCRIPTION__") {
2950
2951         periodicity_i->first = shape1Entry;
2952         periodicity_i->second = shape2Entry;
2953
2954         _facesPeriodicityVector.push_back(*periodicity_i);
2955
2956         break; // __END_PERIODICITY_DESCRIPTION__
2957       }
2958
2959       if (periodicitySeparator == "__BEGIN_ENTRY1__") {  // __BEGIN_ENTRY1__
2960         isOK = static_cast<bool>(load >> shape1Entry);
2961         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_ENTRY1__
2962         if (periodicitySeparator != "__END_ENTRY1__")
2963           throw std::exception();
2964       }
2965
2966       if (periodicitySeparator == "__BEGIN_ENTRY2__") {  // __BEGIN_ENTRY2__
2967         isOK = static_cast<bool>(load >> shape2Entry);
2968         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_ENTRY2__
2969         if (periodicitySeparator != "__END_ENTRY2__")
2970           throw std::exception();
2971       }
2972     }
2973   }
2974 }
2975
2976
2977 void BLSURFPlugin_Hypothesis::LoadEdgesPeriodicity(std::istream & load){
2978
2979   bool isOK = true;
2980
2981   std::string periodicitySeparator;
2982   TEntry theFace1Entry;
2983   TEntry theEdge1Entry;
2984   TEntry theFace2Entry;
2985   TEntry theEdge2Entry;
2986   int edge_orientation = 0;
2987
2988   _edgesPeriodicityVector.clear();
2989
2990   while (isOK) {
2991     isOK = static_cast<bool>(load >> periodicitySeparator); // __BEGIN_PERIODICITY_DESCRIPTION__
2992     TEdgePeriodicity *periodicity_i = new TEdgePeriodicity();
2993     if (periodicitySeparator == "__EDGES_PERIODICITY_END__")
2994       break; // __EDGES_PERIODICITY_END__
2995     if (periodicitySeparator != "__BEGIN_PERIODICITY_DESCRIPTION__"){
2996       throw std::exception();
2997     }
2998
2999     while (isOK) {
3000       isOK = static_cast<bool>(load >> periodicitySeparator);
3001       if (periodicitySeparator == "__END_PERIODICITY_DESCRIPTION__") {
3002
3003         periodicity_i->theFace1Entry = theFace1Entry;
3004         periodicity_i->theEdge1Entry = theEdge1Entry;
3005         periodicity_i->theFace2Entry = theFace2Entry;
3006         periodicity_i->theEdge2Entry = theEdge2Entry;
3007         periodicity_i->edge_orientation = edge_orientation;
3008
3009         _edgesPeriodicityVector.push_back(*periodicity_i);
3010
3011         break; // __END_PERIODICITY_DESCRIPTION__
3012       }
3013
3014       if (periodicitySeparator == "__BEGIN_FACE1__") {  // __BEGIN_FACE1__
3015         isOK = static_cast<bool>(load >> theFace1Entry);
3016         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_FACE1__
3017         if (periodicitySeparator != "__END_FACE1__"){
3018           throw std::exception();
3019         }
3020       }
3021
3022       if (periodicitySeparator == "__BEGIN_EDGE1__") {  // __BEGIN_EDGE1__
3023         isOK = static_cast<bool>(load >> theEdge1Entry);
3024         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_EDGE1__
3025         if (periodicitySeparator != "__END_EDGE1__")
3026           throw std::exception();
3027       }
3028
3029       if (periodicitySeparator == "__BEGIN_FACE2__") {  // __BEGIN_FACE2__
3030         isOK = static_cast<bool>(load >> theFace2Entry);
3031         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_FACE2__
3032         if (periodicitySeparator != "__END_FACE2__")
3033           throw std::exception();
3034       }
3035
3036       if (periodicitySeparator == "__BEGIN_EDGE2__") {  // __BEGIN_EDGE2__
3037         isOK = static_cast<bool>(load >> theEdge2Entry);
3038         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_EDGE2__
3039         if (periodicitySeparator != "__END_EDGE2__")
3040           throw std::exception();
3041       }
3042
3043       if (periodicitySeparator == "__BEGIN_EDGE_ORIENTATION__") {  // __BEGIN_EDGE_ORIENTATION__
3044         isOK = static_cast<bool>(load >> edge_orientation);
3045         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_EDGE_ORIENTATION__
3046         if (periodicitySeparator != "__END_EDGE_ORIENTATION__")
3047           throw std::exception();
3048       }
3049     }
3050   }
3051 }
3052
3053 void BLSURFPlugin_Hypothesis::LoadVerticesPeriodicity(std::istream & load){
3054
3055   bool isOK = true;
3056
3057   std::string periodicitySeparator;
3058   TEntry theEdge1Entry;
3059   TEntry theVertex1Entry;
3060   TEntry theEdge2Entry;
3061   TEntry theVertex2Entry;
3062
3063   _verticesPeriodicityVector.clear();
3064
3065   while (isOK) {
3066     isOK = static_cast<bool>(load >> periodicitySeparator); // __BEGIN_PERIODICITY_DESCRIPTION__
3067     TVertexPeriodicity *periodicity_i = new TVertexPeriodicity();
3068     if (periodicitySeparator == "__VERTICES_PERIODICITY_END__")
3069       break; // __VERTICES_PERIODICITY_END__
3070     if (periodicitySeparator != "__BEGIN_PERIODICITY_DESCRIPTION__"){
3071       throw std::exception();
3072     }
3073
3074     while (isOK) {
3075       isOK = static_cast<bool>(load >> periodicitySeparator);
3076       if (periodicitySeparator == "__END_PERIODICITY_DESCRIPTION__") {
3077
3078         periodicity_i->theEdge1Entry = theEdge1Entry;
3079         periodicity_i->theVertex1Entry = theVertex1Entry;
3080         periodicity_i->theEdge2Entry = theEdge2Entry;
3081         periodicity_i->theVertex2Entry = theVertex2Entry;
3082
3083         _verticesPeriodicityVector.push_back(*periodicity_i);
3084
3085         break; // __END_PERIODICITY_DESCRIPTION__
3086       }
3087
3088       if (periodicitySeparator == "__BEGIN_EDGE1__") {  // __BEGIN_EDGE1__
3089         isOK = static_cast<bool>(load >> theEdge1Entry);
3090         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_EDGE1__
3091         if (periodicitySeparator != "__END_EDGE1__")
3092           throw std::exception();
3093       }
3094
3095       if (periodicitySeparator == "__BEGIN_VERTEX1__") {  // __BEGIN_VERTEX1__
3096         isOK = static_cast<bool>(load >> theVertex1Entry);
3097         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_VERTEX1__
3098         if (periodicitySeparator != "__END_VERTEX1__")
3099           throw std::exception();
3100       }
3101
3102       if (periodicitySeparator == "__BEGIN_EDGE2__") {  // __BEGIN_EDGE2__
3103         isOK = static_cast<bool>(load >> theEdge2Entry);
3104         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_EDGE2__
3105         if (periodicitySeparator != "__END_EDGE2__")
3106           throw std::exception();
3107       }
3108
3109       if (periodicitySeparator == "__BEGIN_VERTEX2__") {  // __BEGIN_VERTEX2__
3110         isOK = static_cast<bool>(load >> theVertex2Entry);
3111         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_VERTEX2__
3112         if (periodicitySeparator != "__END_VERTEX2__")
3113           throw std::exception();
3114       }
3115     }
3116   }
3117 }
3118
3119 void BLSURFPlugin_Hypothesis::LoadPreCADPeriodicity(std::istream & load, const char* shapeType) {
3120
3121   bool isOK = true;
3122
3123   std::string periodicitySeparator;
3124   TEntry shape1Entry;
3125   TEntry shape2Entry;
3126   std::vector<std::string> theSourceVerticesEntries;
3127   std::vector<std::string> theTargetVerticesEntries;
3128
3129   bool hasSourceVertices = false;
3130   bool hasTargetVertices = false;
3131
3132   if ( shapeType  &&  strcmp( shapeType, "FACES") == 0 )
3133     _preCadFacesPeriodicityVector.clear();
3134   else
3135     _preCadEdgesPeriodicityVector.clear();
3136
3137
3138   while (isOK) {
3139     isOK = static_cast<bool>(load >> periodicitySeparator); // __BEGIN_PERIODICITY_DESCRIPTION__
3140     TPreCadPeriodicity *periodicity_i = new TPreCadPeriodicity();
3141     std::string endSeparator = "__PRECAD_" + std::string(shapeType) + "_PERIODICITY_END__";
3142     if (periodicitySeparator == endSeparator)
3143       break; // __PRECAD_FACES_PERIODICITY_END__
3144     if (periodicitySeparator != "__BEGIN_PERIODICITY_DESCRIPTION__"){
3145       throw std::exception();
3146     }
3147
3148     while (isOK) {
3149       isOK = static_cast<bool>(load >> periodicitySeparator);
3150       if (periodicitySeparator == "__END_PERIODICITY_DESCRIPTION__") {
3151
3152         periodicity_i->shape1Entry = shape1Entry;
3153         periodicity_i->shape2Entry = shape2Entry;
3154
3155         if (hasSourceVertices)
3156           periodicity_i->theSourceVerticesEntries = theSourceVerticesEntries;
3157         if (hasTargetVertices)
3158           periodicity_i->theTargetVerticesEntries = theTargetVerticesEntries;
3159
3160         if ( shapeType  &&  strcmp( shapeType, "FACES" ))
3161           _preCadFacesPeriodicityVector.push_back(*periodicity_i);
3162         else
3163           _preCadEdgesPeriodicityVector.push_back(*periodicity_i);
3164
3165         theSourceVerticesEntries.clear();
3166         theTargetVerticesEntries.clear();
3167         hasSourceVertices = false;
3168         hasTargetVertices = false;
3169         break; // __END_PERIODICITY_DESCRIPTION__
3170       }
3171
3172       if (periodicitySeparator == "__BEGIN_ENTRY1__") {  // __BEGIN_ENTRY1__
3173         isOK = static_cast<bool>(load >> shape1Entry);
3174         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_ENTRY1__
3175         if (periodicitySeparator != "__END_ENTRY1__")
3176           throw std::exception();
3177       }
3178
3179       if (periodicitySeparator == "__BEGIN_ENTRY2__") {  // __BEGIN_ENTRY2__
3180         isOK = static_cast<bool>(load >> shape2Entry);
3181         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_ENTRY2__
3182         if (periodicitySeparator != "__END_ENTRY2__")
3183           throw std::exception();
3184       }
3185
3186       if (periodicitySeparator == "__BEGIN_SOURCE_VERTICES_LIST__") {  // __BEGIN_SOURCE_VERTICES_LIST__
3187         hasSourceVertices = true;
3188         while (isOK && (periodicitySeparator != "__END_SOURCE_VERTICES_LIST__")) {
3189           isOK = static_cast<bool>(load >> periodicitySeparator);
3190           if (periodicitySeparator != "__END_SOURCE_VERTICES_LIST__") {
3191             theSourceVerticesEntries.push_back(periodicitySeparator);
3192           }
3193         }
3194       }
3195
3196       if (periodicitySeparator == "__BEGIN_TARGET_VERTICES_LIST__") {  // __BEGIN_TARGET_VERTICES_LIST__
3197         hasTargetVertices = true;
3198         while (isOK && (periodicitySeparator != "__END_TARGET_VERTICES_LIST__")) {
3199           isOK = static_cast<bool>(load >> periodicitySeparator);
3200           if (periodicitySeparator != "__END_TARGET_VERTICES_LIST__") {
3201             theTargetVerticesEntries.push_back(periodicitySeparator);
3202           }
3203         }
3204       }
3205     }
3206   }
3207 }
3208
3209 //=============================================================================
3210 std::ostream & operator <<(std::ostream & save, BLSURFPlugin_Hypothesis & hyp) {
3211   return hyp.SaveTo(save);
3212 }
3213
3214 //=============================================================================
3215 std::istream & operator >>(std::istream & load, BLSURFPlugin_Hypothesis & hyp) {
3216   return hyp.LoadFrom(load);
3217 }
3218
3219 //================================================================================
3220 /*!
3221  * \brief Does nothing
3222  */
3223 //================================================================================
3224
3225 bool BLSURFPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh* theMesh, const TopoDS_Shape& theShape) {
3226   return false;
3227 }
3228
3229 //================================================================================
3230 /*!
3231  * \brief Returns default global constant physical size given a default value of element length ratio
3232  */
3233 //================================================================================
3234
3235 double BLSURFPlugin_Hypothesis::GetDefaultPhySize(double diagonal, double bbSegmentation) {
3236   if (bbSegmentation != 0 && diagonal != 0)
3237     return diagonal / bbSegmentation ;
3238   return 10;
3239 }
3240
3241 //================================================================================
3242 /*!
3243  * \brief Returns default min size given a default value of element length ratio
3244  */
3245 //================================================================================
3246
3247 double BLSURFPlugin_Hypothesis::GetDefaultMinSize(double diagonal) {
3248   if (diagonal != 0)
3249     return diagonal / 1000.0 ;
3250   return undefinedDouble();
3251 }
3252
3253 //================================================================================
3254 /*!
3255  * \brief Returns default max size given a default value of element length ratio
3256  */
3257 //================================================================================
3258
3259 double BLSURFPlugin_Hypothesis::GetDefaultMaxSize(double diagonal) {
3260   if (diagonal != 0)
3261     return diagonal / 5.0 ;
3262   return undefinedDouble();
3263 }
3264
3265 //================================================================================
3266 /*!
3267  * \brief Returns default chordal error given a default value of element length ratio
3268  */
3269 //================================================================================
3270
3271 double BLSURFPlugin_Hypothesis::GetDefaultChordalError(double diagonal) {
3272   if (diagonal != 0)
3273     return diagonal;
3274   return undefinedDouble();
3275 }
3276
3277 //================================================================================
3278 /*!
3279  * \brief Returns default tiny edge length given a default value of element length ratio
3280  */
3281 //================================================================================
3282
3283 double BLSURFPlugin_Hypothesis::GetDefaultTinyEdgeLength(double diagonal) {
3284   if (diagonal != 0)
3285     return diagonal * 1e-6 ;
3286   return undefinedDouble();
3287 }
3288
3289 //================================================================================
3290 /*!
3291  * \brief Returns default tiny edge optimisation length given a default value of element length ratio
3292  */
3293 //================================================================================
3294
3295 double BLSURFPlugin_Hypothesis::GetDefaultTinyEdgeOptimisationLength(double diagonal) {
3296   if (diagonal != 0)
3297     return diagonal * 1e-6 ;
3298   return undefinedDouble();
3299 }
3300
3301 //=============================================================================
3302 /*!
3303  * \brief Initialize my parameter values by default parameters.
3304  *  \retval bool - true if parameter values have been successfully defined
3305  */
3306 //=============================================================================
3307
3308 bool BLSURFPlugin_Hypothesis::SetParametersByDefaults(const TDefaults& dflts, const SMESH_Mesh* theMesh) {
3309   double diagonal = dflts._elemLength*_gen->GetBoundaryBoxSegmentation();
3310   _phySize = GetDefaultPhySize(diagonal, _gen->GetBoundaryBoxSegmentation());
3311   _minSize = GetDefaultMinSize(diagonal);
3312   _maxSize = GetDefaultMaxSize(diagonal);
3313   _chordalError = 0.5 * _phySize; //GetDefaultChordalError(diagonal); IMP 0023307
3314   _tinyEdgeLength = GetDefaultTinyEdgeLength(diagonal);
3315   _tinyEdgeOptimisationLength = GetDefaultTinyEdgeOptimisationLength(diagonal);
3316
3317   return true;
3318 }
3319
3320 //================================================================================
3321 /*!
3322  * \brief Converts a string to a bool
3323  */
3324 //================================================================================
3325
3326 bool BLSURFPlugin_Hypothesis::ToBool(const std::string& str, bool* isOk )
3327   throw (std::invalid_argument)
3328 {
3329   std::string s = str;
3330   if ( isOk ) *isOk = true;
3331
3332   for ( size_t i = 0; i <= s.size(); ++i )
3333     s[i] = tolower( s[i] );
3334
3335   if ( s == "1" || s == "true" || s == "active" || s == "yes" )
3336     return true;
3337
3338   if ( s == "0" || s == "false" || s == "inactive" || s == "no" )
3339     return false;
3340
3341   if ( isOk )
3342     *isOk = false;
3343   else {
3344     std::string msg = "Not a Boolean value:'" + str + "'";
3345     throw std::invalid_argument(msg);
3346   }
3347   return false;
3348 }
3349
3350 //================================================================================
3351 /*!
3352  * \brief Converts a string to a real value
3353  */
3354 //================================================================================
3355
3356 double BLSURFPlugin_Hypothesis::ToDbl(const std::string& str, bool* isOk )
3357   throw (std::invalid_argument)
3358 {
3359   if ( str.empty() ) throw std::invalid_argument("Empty value provided");
3360
3361   char * endPtr;
3362   double val = strtod(&str[0], &endPtr);
3363   bool ok = (&str[0] != endPtr);
3364
3365   if ( isOk ) *isOk = ok;
3366
3367   if ( !ok )
3368   {
3369     std::string msg = "Not a real value:'" + str + "'";
3370     throw std::invalid_argument(msg);
3371   }
3372   return val;
3373 }
3374
3375 //================================================================================
3376 /*!
3377  * \brief Converts a string to a integer value
3378  */
3379 //================================================================================
3380
3381 int BLSURFPlugin_Hypothesis::ToInt(const std::string& str, bool* isOk )
3382   throw (std::invalid_argument)
3383 {
3384   if ( str.empty() ) throw std::invalid_argument("Empty value provided");
3385
3386   char * endPtr;
3387   int val = (int)strtol( &str[0], &endPtr, 10);
3388   bool ok = (&str[0] != endPtr);
3389
3390   if ( isOk ) *isOk = ok;
3391
3392   if ( !ok )
3393   {
3394     std::string msg = "Not an integer value:'" + str + "'";
3395     throw std::invalid_argument(msg);
3396   }
3397   return val;
3398 }
3399