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