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