File: Generalized_map_save_load.h

package info (click to toggle)
cgal 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,912 kB
  • sloc: cpp: 810,858; ansic: 208,477; sh: 493; python: 411; makefile: 286; javascript: 174
file content (216 lines) | stat: -rw-r--r-- 6,052 bytes parent folder | download
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
// Copyright (c) 2016 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/Generalized_map/include/CGAL/Generalized_map_save_load.h $
// $Id: include/CGAL/Generalized_map_save_load.h b26b07a1242 $
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s)     : Guillaume Damiand <guillaume.damiand@liris.cnrs.fr>
//
#ifndef CGAL_GENERALIZED_MAP_SAVE_LOAD_H
#define CGAL_GENERALIZED_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_save_load.h>

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

/* We reuse the following functions from Combinatorial_map_save_load.h:
 *   write_cmap_dart_node
 *   write_cmap_attribute_node
 *   My_functor_cmap_save_one_attrib
 *   My_functor_cmap_save_attrib
 *   cmap_save_attributes
 *
 *   read_cmap_dart_node
 *   read_cmap_attribute_node
 *   My_functor_cmap_load_one_attrib
 *   My_functor_cmap_load_attrib
 *   cmap_load_attributes
 */

namespace CGAL {

    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
  //        <a i="0"> neighbor dart index for alpha0 </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>

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

    // First we numbered each dart by using the unordered_map.
    typename GMap::Dart_range::const_iterator it(amap.darts().begin());
    for(typename GMap::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 GMap::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=0; dim<=amap.dimension; dim++)
      {
        if(!amap.is_free(it, dim))
        {
          ptree& currentNext = ndart.add("a", myDarts[amap.alpha(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 GMap >
  bool save_generalized_map(const GMap& amap, std::ostream & output)
  {
    using boost::property_tree::ptree;
    ptree tree;

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

    // Save darts
    ptree pt_darts=gmap_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 GMap >
  bool save_generalized_map(const GMap& amap, const char* filename)
  {
    std::ofstream output(filename);
    if (!output) return false;
    return save_generalized_map(amap, output);
  }

  template < class GMap >
  bool gmap_load_darts(boost::property_tree::ptree &pt, GMap& amap,
                       std::vector<typename GMap::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" ) // d for dart
      {
        for(const ptree::value_type& v2 : v.second )
        {
          if (v2.first == "a") // a for alpha
          {
            index = v2.second.get("<xmlattr>.i", 0);
            nextDartInt = boost::lexical_cast< int >(v2.second.data())-1;

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

    return true;
  }

  template < class GMap >
  bool load_generalized_map(std::ifstream & input, GMap& amap)
  {
    using boost::property_tree::ptree;
    ptree pt;
    read_xml(input, pt);
    std::vector<typename GMap::Dart_descriptor> myDarts;
    gmap_load_darts(pt,amap,myDarts);
    cmap_load_attributes(pt,amap,myDarts);
    return true;
  }

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

} // namespace CGAL

#endif // CGAL_GENERALIZED_MAP_SAVE_LOAD_H //
// EOF //