File: Combinatorial_map_save_load.h

package info (click to toggle)
cgal 6.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 144,952 kB
  • sloc: cpp: 811,597; ansic: 208,576; sh: 493; python: 411; makefile: 286; javascript: 174
file content (856 lines) | stat: -rw-r--r-- 29,852 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
// Copyright (c) 2010-2011 CNRS and LIRIS' Establishments (France).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org)
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1.1/Combinatorial_map/include/CGAL/Combinatorial_map_save_load.h $
// $Id: include/CGAL/Combinatorial_map_save_load.h 08b27d3db14 $
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s)     : Guillaume Damiand <guillaume.damiand@liris.cnrs.fr>
//                 Guillaume Castano <guillaume.castano@gmail.com>
//                 Pascal Khieu <pascal.khieu@gmail.com>
//
#ifndef CGAL_COMBINATORIAL_MAP_SAVE_LOAD_H
#define CGAL_COMBINATORIAL_MAP_SAVE_LOAD_H

#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/lexical_cast.hpp>

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Combinatorial_map_functors.h>

#include <algorithm>
#include <unordered_map>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <typeinfo>

namespace CGAL {

    typedef Exact_predicates_inexact_constructions_kernel::Point_2 RPoint_2;
    typedef Exact_predicates_exact_constructions_kernel::Point_2 EPoint_2;
    typedef Exact_predicates_inexact_constructions_kernel::Point_3 RPoint_3;
    typedef Exact_predicates_exact_constructions_kernel::Point_3 EPoint_3;

  // Tags used in xml tree:
  // For darts:
  //  <darts>
  //    <d> // new dart
  //        <b i="1"> neighbor dart index for beta1 </b>
  //        ...
  //        <v> value of dart (optional) </v>
  //    </d>
  //  ...
  // </darts>
  // For attributes:
  // <attributes>
  //   <dimension index="1"> // new type of non void attribute
  //   <type>type of the info associated</type>
  //   <a> // new attribute
  //    <d> dart index </d>
  //    <v> value of attribute </v>
  //   </a>
  //   ...
  // </attributes>

  // Here T is a Dart_const_descriptor so we don't need &
  template<typename T>
  void write_cmap_dart_node(boost::property_tree::ptree & /*node*/, T)
  {}

  template<typename T>
  void write_cmap_attribute_node(boost::property_tree::ptree & /*node*/, const T&)
  {}

  inline
  void write_cmap_attribute_node(boost::property_tree::ptree & node,
                       char val)
  {node.add("v",val);}
  inline
  void write_cmap_attribute_node(boost::property_tree::ptree & node,
                       unsigned char val)
  {node.add("v",val);}
  inline
  void write_cmap_attribute_node(boost::property_tree::ptree & node,
                       short int val)
  {node.add("v",val);}
  inline
  void write_cmap_attribute_node(boost::property_tree::ptree & node,
                       unsigned short int val)
  {node.add("v",val);}
  inline
  void write_cmap_attribute_node(boost::property_tree::ptree & node,
                       int val)
  {node.add("v",val);}
  inline
  void write_cmap_attribute_node(boost::property_tree::ptree & node,
                       unsigned int val)
  {node.add("v",val);}
  inline
  void write_cmap_attribute_node(boost::property_tree::ptree & node,
                       long int val)
  {node.add("v",val);}
  inline
  void write_cmap_attribute_node(boost::property_tree::ptree & node,
                       unsigned long int val)
  {node.add("v",val);}
  inline
  void write_cmap_attribute_node(boost::property_tree::ptree & node,
                       float val)
  {node.add("v",val);}
  inline
  void write_cmap_attribute_node(boost::property_tree::ptree & node,
                       double val)
  {node.add("v",val);}
  inline
  void write_cmap_attribute_node(boost::property_tree::ptree & node,
                       long double val)
  {node.add("v",val);}
  inline
  void write_cmap_attribute_node(boost::property_tree::ptree & node,
                       bool val)
  {node.add("v",val);}
  inline
  void write_cmap_attribute_node(boost::property_tree::ptree & node,
                       const std::string& val)
  {node.add("v",val);}
  inline
  void write_cmap_attribute_node(boost::property_tree::ptree & node,
                                 const RPoint_2& val)
  {
    node.add("p.x",val.x());
    node.add("p.y",val.y());
  }
  inline
  void write_cmap_attribute_node(boost::property_tree::ptree & node,
                                 const EPoint_2& val)
  {
    node.add("p.x",CGAL::to_double(val.x()));
    node.add("p.y",CGAL::to_double(val.y()));
  }
  inline
  void write_cmap_attribute_node(boost::property_tree::ptree & node,
                                 const RPoint_3& val)
  {
    node.add("p.x",val.x());
    node.add("p.y",val.y());
    node.add("p.z",val.z());
  }
  inline
  void write_cmap_attribute_node(boost::property_tree::ptree & node,
                                 const EPoint_3& val)
  {
    node.add("p.x",CGAL::to_double(val.x()));
    node.add("p.y",CGAL::to_double(val.y()));
    node.add("p.z",CGAL::to_double(val.z()));
  }

  template<typename CMap, unsigned int i,
           bool WithInfo=CGAL::Is_attribute_has_non_void_info
                          <typename CMap::template Attribute_type<i>::type>::value,
           bool WithPoint=CGAL::Is_attribute_has_point
                          <typename CMap::template Attribute_type<i>::type >::value >
  struct My_functor_cmap_save_one_attrib;

  // An attrib with point and with info
  template<typename CMap, unsigned int i>
  struct My_functor_cmap_save_one_attrib<CMap, i, true, true>
  {
    static void run(CMap& amap, boost::property_tree::ptree& ptree,
                    std::unordered_map<typename CMap::Dart_const_descriptor,
                    typename CMap::size_type>& myDarts)
    {
      // add dimension & type
      boost::property_tree::ptree& ndim = ptree.add("dimension", "");
      ndim.put("<xmlattr>.index", i);
      ndim.add("type", typeid(typename CMap::template Attribute_type<i>::type::Info).name());
      ndim.add("type_point", typeid(typename CMap::Point).name());

      // for every attribute of the dimension
      for (auto it=amap.template one_dart_per_cell<i>().begin(),
           itend=amap.template one_dart_per_cell<i>().end(); it!=itend; ++it)
      {
        if (amap.template attribute<i>(it)!=amap.null_descriptor)
        {
          // make component, dart and property node
          boost::property_tree::ptree & nattr = ndim.add("a", "");
          /* boost::property_tree::ptree & ndarts = */
          nattr.add("d", myDarts[it]);

          // update property node to add a value node (from basic or custom type
          write_cmap_attribute_node(nattr, amap.template info<i>(it));
          write_cmap_attribute_node(nattr, amap.point(it));
        }
      }
    }
  };

  // An attribute with point and without info
  template<typename CMap, unsigned int i>
  struct My_functor_cmap_save_one_attrib<CMap, i, false, true>
  {
    static void run(CMap& amap, boost::property_tree::ptree& ptree,
                    std::unordered_map<typename CMap::Dart_const_descriptor,
                    typename CMap::size_type>& myDarts)
    {
      // add dimension & type
      boost::property_tree::ptree& ndim = ptree.add("dimension", "");
      ndim.put("<xmlattr>.index", i);
      ndim.add("type", "void");
      ndim.add("type_point", typeid(typename CMap::Point).name());

      // for every attribute of the dimension
      for (auto it=amap.template one_dart_per_cell<i>().begin(),
           itend=amap.template one_dart_per_cell<i>().end(); it!=itend; ++it)
      {
        if (amap.template attribute<i>(it)!=amap.null_descriptor)
        {
          // make component, dart and property node
          boost::property_tree::ptree & nattr = ndim.add("a", "");
          /* boost::property_tree::ptree & ndarts = */
          nattr.add("d", myDarts[it]);

          // update property node to add a value node (from basic or custom type
          write_cmap_attribute_node(nattr, amap.point(it));
        }
      }
    }
  };

  // An attribute without point and with info
  template<typename CMap, unsigned int i>
  struct My_functor_cmap_save_one_attrib<CMap, i, true, false>
  {
    static void run(CMap& amap, boost::property_tree::ptree& ptree,
                    std::unordered_map<typename CMap::Dart_const_descriptor,
                    typename CMap::size_type>& myDarts)
    {
      // add dimension & type
      boost::property_tree::ptree& ndim = ptree.add("dimension", "");
      ndim.put("<xmlattr>.index", i);
      ndim.add("type", typeid(typename CMap::template
                              Attribute_type<i>::type::Info).name());
      ndim.add("type_point", "void");

      // for every attribute of the dimension
      for (auto it=amap.template one_dart_per_cell<i>().begin(),
           itend=amap.template one_dart_per_cell<i>().end(); it!=itend; ++it)
      {
        if (amap.template attribute<i>(it)!=amap.null_descriptor)
        {
          // make component, dart and property node
          boost::property_tree::ptree & nattr = ndim.add("a", "");
          /* boost::property_tree::ptree & ndarts = */
          nattr.add("d", myDarts[it]);

          // update property node to add a value node (from basic or custom type
          write_cmap_attribute_node(nattr, amap.template info<i>(it));
        }
      }
    }
  };

  // An attrib without point and without info
  template<typename CMap, unsigned int i>
  struct My_functor_cmap_save_one_attrib<CMap, i, false, false>
  {
    static void run(CMap& amap, boost::property_tree::ptree& ptree,
                    std::unordered_map<typename CMap::Dart_const_descriptor,
                    typename CMap::size_type>& myDarts)
    {
      // add dimension & type
      boost::property_tree::ptree& ndim = ptree.add("dimension", "");
      ndim.put("<xmlattr>.index", i);
      ndim.add("type", "void");
      ndim.add("type_point", "void");

      // for every attribute of the dimension
      for (auto it=amap.template one_dart_per_cell<i>().begin(),
           itend=amap.template one_dart_per_cell<i>().end(); it!=itend; ++it)
      {
        if (amap.template attribute<i>(it)!=amap.null_descriptor)
        {
          // make component, dart and property node
          boost::property_tree::ptree & nattr = ndim.add("a", "");
          /* boost::property_tree::ptree & ndarts = */
          nattr.add("d", myDarts[it]);
        }
      }
    }
  };

  template<typename CMap>
  struct My_functor_cmap_save_attrib
  {
    template <unsigned int i>
    static void run(CMap& amap, boost::property_tree::ptree& ptree,
                    std::unordered_map<typename CMap::Dart_const_descriptor,
                    typename CMap::size_type>& myDarts)
    {
      My_functor_cmap_save_one_attrib<CMap, i>::run(amap, ptree, myDarts);
    }
  };

  template < class CMap >
  boost::property_tree::ptree cmap_save_darts
  (CMap& amap, std::unordered_map<typename CMap::Dart_const_descriptor,
   typename CMap::size_type>& myDarts)
  {
    CGAL_assertion( myDarts.empty() );

    // First we numbered each dart by using the unordered_map.
    typename CMap::Dart_range::const_iterator it(amap.darts().begin());
    for(typename CMap::size_type num=1; num<=amap.number_of_darts();
        ++num, ++it)
    {
      myDarts[it] = num;
    }

    // make a tree
    using boost::property_tree::ptree;
    ptree pt;

    // Now we save each dart, and its neighbors.
    it=amap.darts().begin();
    for(typename CMap::size_type num=0; num<amap.number_of_darts(); ++num, ++it)
    {
      // make a dart node
      ptree& ndart = pt.add("d", "");

      // the beta, only for non free sews
      for(unsigned int dim=1; dim<=amap.dimension; dim++)
      {
        if(!amap.is_free(it, dim))
        {
          ptree& currentNext = ndart.add("b", myDarts[amap.beta(it, dim)]);
          currentNext.put("<xmlattr>.i", dim);
        }
      }

      // update property node to add a value node (if user defined its own
      // function)
      write_cmap_dart_node(ndart, it);
    }

    return pt;
  }

  template < class CMap >
  boost::property_tree::ptree cmap_save_attributes
  (const CMap& amap, std::unordered_map<typename CMap::Dart_const_descriptor,
   typename CMap::size_type>& myDarts)
  {
    using boost::property_tree::ptree;
    ptree pt;

    // update pt adding nodes containing attributes information
    CMap::Helper::template Foreach_enabled_attributes
      <My_functor_cmap_save_attrib<CMap> >::run(const_cast<CMap&>(amap), pt, myDarts);

    return pt;
  }

  struct EmptyFunctor
  {
    void operator() (boost::property_tree::ptree & /*node*/) const
    {
      // node.add("myinfo.myvalie",15);
    }
  };

  template < class CMap, class Functor >
  bool save_combinatorial_map(const CMap& amap, std::ostream & output,
                              const Functor& f)
  {
    using boost::property_tree::ptree;
    ptree tree;
    tree.put("data", "");

    /** First we save general information of the map (by default nothing,
        the function can be specialized by users). */
    f(tree);

    // map dart => number
    std::unordered_map<typename CMap::Dart_const_descriptor, typename CMap::size_type> myDarts;

    // Save darts
    ptree pt_darts=cmap_save_darts(amap, myDarts);
    tree.add_child("data.darts",pt_darts);

    // Save attributes
    ptree pt_attr=cmap_save_attributes(amap, myDarts);
    tree.add_child("data.attributes", pt_attr);

    // save data in output
    write_xml(output, tree);

    return true;
  }

  template < class CMap, class Functor >
  bool save_combinatorial_map(const CMap& amap, const char* filename,
                              const Functor& f)
  {
    std::ofstream output(filename);
    if (!output) return false;
    return save_combinatorial_map(amap, output, f);
  }

  template < class CMap >
  bool save_combinatorial_map(const CMap& amap, std::ostream & output)
  {
    EmptyFunctor f;
    return save_combinatorial_map(amap, output, f);
  }

  template < class CMap >
  bool save_combinatorial_map(const CMap& amap, const char* filename)
  {
    EmptyFunctor f;
    return save_combinatorial_map(amap, filename, f);
  }

  // Here T is a Dart_descriptor so no need of &
  template<typename T>
  void read_cmap_dart_node
  (const boost::property_tree::ptree::value_type &/*v*/, T /*val*/)
  {}
  template<typename T>
  void read_cmap_attribute_node
  (const boost::property_tree::ptree::value_type &/*v*/, T &/*val*/)
  {}
  template<> inline
  void read_cmap_attribute_node
  (const boost::property_tree::ptree::value_type &v,char &val)
  {val=boost::lexical_cast< char >(v.second.data());}
  template<> inline
  void read_cmap_attribute_node
  (const boost::property_tree::ptree::value_type &v,unsigned char &val)
  {val=boost::lexical_cast< unsigned char >(v.second.data());}
  template<> inline
  void read_cmap_attribute_node
  (const boost::property_tree::ptree::value_type &v,short int &val)
  {val=boost::lexical_cast< short int >(v.second.data());}
  template<> inline
  void read_cmap_attribute_node
  (const boost::property_tree::ptree::value_type &v,unsigned short int &val)
  {val=boost::lexical_cast< unsigned short int >(v.second.data());}
  template<> inline
  void read_cmap_attribute_node
  (const boost::property_tree::ptree::value_type &v,int &val)
  {val=boost::lexical_cast< int >(v.second.data());}
  template<> inline
  void read_cmap_attribute_node
  (const boost::property_tree::ptree::value_type &v,unsigned int &val)
  {val=boost::lexical_cast< unsigned int >(v.second.data());}
  template<> inline
  void read_cmap_attribute_node
  (const boost::property_tree::ptree::value_type &v,long int &val)
  {val=boost::lexical_cast< long int >(v.second.data());}
  template<> inline
  void read_cmap_attribute_node
  (const boost::property_tree::ptree::value_type &v,unsigned long int &val)
  {val=boost::lexical_cast< unsigned long int >(v.second.data());}
  template<> inline
  void read_cmap_attribute_node
  (const boost::property_tree::ptree::value_type &v,float &val)
  {val=boost::lexical_cast< float >(v.second.data());}
  template<> inline
  void read_cmap_attribute_node
  (const boost::property_tree::ptree::value_type &v,double &val)
  {val=boost::lexical_cast< double >(v.second.data());}
  template<> inline
  void read_cmap_attribute_node
  (const boost::property_tree::ptree::value_type &v,long double &val)
  {val=boost::lexical_cast< long double >(v.second.data());}
  template<> inline
  void read_cmap_attribute_node
  (const boost::property_tree::ptree::value_type &v,bool &val)
  {val=boost::lexical_cast< bool >(v.second.data());}
  template<> inline
  void read_cmap_attribute_node
  (const boost::property_tree::ptree::value_type &v,std::string &val)
  {val=boost::lexical_cast< std::string >(v.second.data());}
  template<> inline
  void read_cmap_attribute_node
  (const boost::property_tree::ptree::value_type &v,RPoint_2 &val)
  {
    double x=v.second.get<double>("x");
    double y=v.second.get<double>("y");
    val = RPoint_2(x,y);
  }
  template<> inline
  void read_cmap_attribute_node
  (const boost::property_tree::ptree::value_type &v,RPoint_3 &val)
  {
    double x=v.second.get<double>("x");
    double y=v.second.get<double>("y");
    double z=v.second.get<double>("z");
    val = RPoint_3(x,y,z);
  }

  template<typename CMap, unsigned int i,
           bool WithInfo=CGAL::Is_attribute_has_non_void_info
                          <typename CMap::template Attribute_type<i>::type>::value,
           bool WithPoint=CGAL::Is_attribute_has_point
                          <typename CMap::template Attribute_type<i>::type >::value >
  struct My_functor_cmap_load_one_attrib;

  // An attrib with point and with info
  template<typename CMap, unsigned int i>
  struct My_functor_cmap_load_one_attrib<CMap, i, true, true>
  {
    static void run(const boost::property_tree::ptree& pt, CMap& amap,
                    const std::vector<typename CMap::Dart_descriptor>& myDarts)
    {
      for(const boost::property_tree::ptree::value_type& v0 :
          pt.get_child("data.attributes") )
      {
        // <dimension>
        if (v0.first == "dimension")
        {
          int dimension=v0.second.get("<xmlattr>.index", -1);

          // if map.dimension == dimension saved in the xml file
          if (dimension==i)
          {
            unsigned int id_dart_cellule=0;
            std::string type =  v0.second.get<std::string>("type");
            std::string type_map=std::string
              (typeid(typename CMap::template Attribute_type<i>::type::Info).name());

            std::string ptype =  v0.second.get<std::string>("type_point");
            std::string ptype_map= std::string
              (typeid(typename CMap::template Attribute_type<i>::type::Point).name());

                //  std::cout<<"ptype="<<ptype<<"  and type_map="<<type_map<<std::endl;
                /* if(type!=type_map && ptype!=ptype_map)
                {
                  //  std::cout<<"Not loaded."<<std::endl;
                  return;
                  }*/

            for(const boost::property_tree::ptree::value_type &v1 :
                          v0.second )
            {
              if( v1.first == "a" )
              {
                id_dart_cellule=v1.second.get<unsigned int>("d")-1;

                for(const boost::property_tree::ptree::value_type &v2 :
                              v1.second )
                {
                  if( type==type_map && v2.first == "v" )
                  {
                    if (amap.template attribute<i>(myDarts[id_dart_cellule])
                        ==amap.null_descriptor )
                      amap.template set_attribute<i>
                        (myDarts[id_dart_cellule],
                         amap.template create_attribute<i>());
                    read_cmap_attribute_node
                      (v2,
                       amap.template info<i>(myDarts[id_dart_cellule]));
                  }
                  if( ptype==ptype_map && v2.first == "p" )
                  {
                    if (amap.template attribute<i>(myDarts[id_dart_cellule])
                        ==amap.null_descriptor )
                      amap.template set_attribute<i>
                        (myDarts[id_dart_cellule],
                         amap.template create_attribute<i>());
                    read_cmap_attribute_node
                      (v2,
                       amap.template get_attribute<i>
                       (amap.template attribute<i>(myDarts[id_dart_cellule])).
                       point());
                  }
                }
              }
            }
          }
        }
      }
    }
  };

  // An attribute with point and without info
  template<typename CMap, unsigned int i>
  struct My_functor_cmap_load_one_attrib<CMap, i, false, true>
  {
    static void run(const boost::property_tree::ptree& pt, CMap& amap,
                    const std::vector<typename CMap::Dart_descriptor>& myDarts)
    {
      for( const boost::property_tree::ptree::value_type &v0 :
                     pt.get_child("data.attributes") )
      {
        // <dimension>
        if (v0.first == "dimension")
        {
          int dimension=v0.second.get("<xmlattr>.index", -1);

          // if map.dimension == dimension saved in the xml file
          if (dimension==i)
          {
            unsigned int id_dart_cellule=0;
            std::string ptype =  v0.second.get<std::string>("type_point");
            std::string type_map= typeid
              (typename CMap::template Attribute_type<i>::type::Point).name();
                //  std::cout<<"ptype="<<ptype<<"  and type_map="<<type_map<<std::endl;
                /*                if(ptype!=type_map)
                {
                  //  std::cout<<"Not loaded."<<std::endl;
                  return;
                  }*/

            for(const boost::property_tree::ptree::value_type &v1 :
                          v0.second )
            {
              if( v1.first == "a" )
              {
                id_dart_cellule=v1.second.get<unsigned int>("d")-1;

                for(const boost::property_tree::ptree::value_type &v2 :
                              v1.second )
                {
                  if( v2.first == "p" )
                  {
                    if (amap.template attribute<i>
                        (myDarts[id_dart_cellule])==amap.null_descriptor )
                      amap.template set_attribute<i>
                        (myDarts[id_dart_cellule],
                         amap.template create_attribute<i>());

                    read_cmap_attribute_node
                      (v2,
                       (amap.template get_attribute<i>
                        (amap.template attribute<i>(myDarts[id_dart_cellule])).
                        point()));
                  }
                }
              }
            }
          }
        }
      }
    }
  };

  // An attribute without point and with info
  template<typename CMap, unsigned int i>
  struct My_functor_cmap_load_one_attrib<CMap, i, true, false>
  {
    static void run(const boost::property_tree::ptree& pt, CMap& amap,
                    const std::vector<typename CMap::Dart_descriptor>& myDarts)
    {
      for( const boost::property_tree::ptree::value_type &v0 :
                     pt.get_child("data.attributes") )
      {
        // <dimension>
        if (v0.first == "dimension")
        {
          int dimension=v0.second.get("<xmlattr>.index", -1);

          // if map.dimension == dimension saved in the xml file
          if (dimension==i)
          {
            unsigned int id_dart_cellule=0;
            std::string ptype =  v0.second.get<std::string>("type");
            std::string type_map= typeid
              (typename CMap::template Attribute_type<i>::type::Info).name();
                //  std::cout<<"ptype="<<ptype<<"  and type_map="<<type_map<<std::endl;
                /*      if(ptype!=type_map)
                {
                  //  std::cout<<"Not loaded."<<std::endl;
                  return;
                  } */

            for(const boost::property_tree::ptree::value_type &v1 :
                          v0.second )
            {
              if( v1.first == "a" )
              {
                id_dart_cellule=v1.second.get<unsigned int>("d")-1;

                for(const boost::property_tree::ptree::value_type &v2 :
                              v1.second )
                {
                  if( v2.first == "v" )
                  {
                    if (amap.template attribute<i>
                        (myDarts[id_dart_cellule])==amap.null_descriptor)
                      amap.template set_attribute<i>
                        (myDarts[id_dart_cellule],
                         amap.template create_attribute<i>());
                    read_cmap_attribute_node
                      (v2,
                       amap.template info<i>(myDarts[id_dart_cellule]));
                  }
                }
              }
            }
          }
        }
      }
    }
  };

  // An attribute without point and without info
  template<typename CMap, unsigned int i>
  struct My_functor_cmap_load_one_attrib<CMap, i, false, false>
  {
    static void run(const boost::property_tree::ptree& pt, CMap& amap,
                    const std::vector<typename CMap::Dart_descriptor>& myDarts)
    {
      for( const boost::property_tree::ptree::value_type &v0 :
                     pt.get_child("data.attributes") )
      {
        // <dimension>
        if (v0.first == "dimension")
        {
          int dimension=v0.second.get("<xmlattr>.index", -1);

          // if map.dimension == dimension saved in the xml file
          if (dimension==i)
          {
            unsigned int id_dart_cellule=0;

            for(const boost::property_tree::ptree::value_type &v1 :
                          v0.second )
            {
              if( v1.first == "a" )
              {
                id_dart_cellule=v1.second.get<unsigned int>("d")-1;

                if (amap.template attribute<i>(myDarts[id_dart_cellule])==
                    amap.null_descriptor)
                  amap.template set_attribute<i>
                    (myDarts[id_dart_cellule],
                   amap.template create_attribute<i>());
              }
            }
          }
        }
      }
    }
  };

  /** Functor called to load i-attributes.
   *  @param pt a boost::property_tree::ptree load from an xml file
   *  @param amap a pointer to the map to load into
   *  @param myDarts an array of Dart_descriptor st myDarts[i] is the ith dart.
   */
  template<class CMap>
  struct My_functor_cmap_load_attrib
  {
    template <unsigned int i>
    static void run(const boost::property_tree::ptree& pt, CMap& amap,
                    const std::vector<typename CMap::Dart_descriptor>& myDarts)
    {
       My_functor_cmap_load_one_attrib<CMap, i>::run(pt, amap, myDarts);
    }
  };

  template < class CMap >
  bool cmap_load_darts(boost::property_tree::ptree &pt, CMap& amap,
                       std::vector<typename CMap::Dart_descriptor>& myDarts)
  {
    // use a boost::property_tree
    using boost::property_tree::ptree;

    // make darts
    for( const ptree::value_type &v : pt.get_child("data.darts") )
    {
      if( v.first == "d" )
        myDarts.push_back(amap.create_dart());
    }

    // update beta links
    unsigned int index;
    unsigned int currentDartInt = 0;
    unsigned int nextDartInt;

    for( const ptree::value_type &v : pt.get_child("data.darts") )
    {
      if( v.first == "d" )
      {
        for( const ptree::value_type &v2 : v.second )
        {
          if (v2.first == "b")
          {
            index = v2.second.get("<xmlattr>.i", 0);
            nextDartInt = boost::lexical_cast< int >(v2.second.data())-1;

            if ( index<=amap.dimension )
            {
              amap.basic_link_beta(myDarts[currentDartInt],
                                   myDarts[nextDartInt],
                                   index);
            }
          }
          else if (v2.first=="v")
            read_cmap_dart_node(v2,myDarts[currentDartInt]);
        }
      }
      ++currentDartInt;
    }

    return true;
  }

  template < class CMap >
  void cmap_load_attributes(const boost::property_tree::ptree& pt, CMap& amap,
                            const std::vector<typename CMap::Dart_descriptor>& myDarts)
  {
    CMap::Helper::template Foreach_enabled_attributes
      <My_functor_cmap_load_attrib<CMap> >::run(pt, amap, myDarts);
  }

  template < class CMap, class Functor >
  bool load_combinatorial_map(std::ifstream & input, CMap& amap,
                              Functor& f)
  {
    using boost::property_tree::ptree;
    ptree pt;
    read_xml(input, pt);

    /** First we load general information of the map (by default nothing,
        the function can be specialized by users). */
    f(pt);

    // Then we load darts and attributes.
    std::vector<typename CMap::Dart_descriptor> myDarts;
    cmap_load_darts(pt,amap,myDarts);
    cmap_load_attributes(pt,amap,myDarts);
    return true;
  }

  template < class CMap, class Functor >
  bool load_combinatorial_map(const char* filename, CMap& amap,
                              Functor& f)
  {
    std::ifstream input(filename);
    if (!input) return false;
    return load_combinatorial_map(input, amap, f);
  }

  template < class CMap >
  bool load_combinatorial_map(std::ifstream & input, CMap& amap)
  {
    EmptyFunctor f;
    return load_combinatorial_map(input, amap, f);
  }

  template < class CMap >
  bool load_combinatorial_map(const char* filename, CMap& amap)
  {
    EmptyFunctor f;
    return load_combinatorial_map(filename, amap, f);
  }
} // namespace CGAL

#endif // CGAL_COMBINATORIAL_MAP_SAVE_LOAD_H //
// EOF //