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