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