File: Combinatorial_map_basic_operations.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 (503 lines) | stat: -rw-r--r-- 18,797 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
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
// 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/Combinatorial_map/include/CGAL/Combinatorial_map_basic_operations.h $
// $Id: include/CGAL/Combinatorial_map_basic_operations.h b26b07a1242 $
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s)     : Guillaume Damiand <guillaume.damiand@liris.cnrs.fr>
//
#ifndef CGAL_COMBINATORIAL_MAP_BASIC_OPERATIONS_H
#define CGAL_COMBINATORIAL_MAP_BASIC_OPERATIONS_H 1

#include <CGAL/Combinatorial_map_iterators_base.h>
#include <CGAL/tags.h>

#include <type_traits>

namespace CGAL
{
  /** @file Combinatorial_map_basic_operations.h
   * Basic operations on a combinatorial map.
   */

  /** Test if the two given darts belong to the same given orbit.
   * @param amap a combinatorial map.
   * @param adart1 the first dart.
   * @param adart2 the second dart.
   * @return true iff the two darts belong to the same orbit.
   */
  template < class Map, class Iterator >
  bool belong_to_same_orbit(const Map & amap,
                            typename Map::Dart_const_descriptor adart1,
                            typename Map::Dart_const_descriptor adart2)
  {
    static_assert(std::is_same<typename Iterator::Basic_iterator,
                                           Tag_false>::value);
    bool found=false;

    for (Iterator it(amap, adart1); !found && it.cont(); ++it)
    {
      if (it == adart2) found = true;
    }

    return found;
  }

  /** Test if all the darts of a given orbit are marked.
   * @param amap a combinatorial map.
   * @param adart a dart of the orbit.
   * @param amark the mark.
   * @return true iff all the darts are marked.
   */
  template < class Map, class Iterator >
  bool is_whole_orbit_marked(const Map & amap,
                             typename Map::Dart_const_descriptor adart,
                             typename Map::size_type amark)
  {
    static_assert(std::is_same<typename Iterator::Basic_iterator,
                                           Tag_false>::value);
    bool res=true;

    for ( Iterator it(amap, adart); res && it.cont(); ++it )
    {
      if (!amap.is_marked(it, amark)) res = false;
    }

    return res;
  }

  /** Test if all the darts of a given orbit are unmarked.
   * @param amap a combinatorial map.
   * @param adart a dart of the orbit.
   * @param amark the mark.
   * @return true iff all the darts are unmarked.
   */
  template < class Map, class Iterator >
  bool is_whole_orbit_unmarked(const Map & amap,
                               typename Map::Dart_const_descriptor adart,
                               typename Map::size_type amark)
  {
    amap.negate_mark(amark);
    bool res=CGAL::template is_whole_orbit_marked<Map,Iterator>(amap, adart, amark);
    amap.negate_mark(amark);
    return res;
  }

  /** Mark a given orbit with a given mark.
   * @param amap a combinatorial map.
   * @param adart a dart of the orbit.
   * @param amark the mark.
   * @return the number of darts of the marked orbit.
   * @pre The whole orbit must be unmarked.
   */
  template < class Map, class Iterator >
  typename Map::size_type mark_orbit(const Map & amap,
                                     typename Map::Dart_const_descriptor adart,
                                     typename Map::size_type amark)
  {
    static_assert(std::is_same<typename Iterator::Basic_iterator,
                            Tag_true>::value);
    CGAL_assertion( (is_whole_orbit_unmarked<Map,
                     CMap_non_basic_iterator<Map,Iterator> >
                     (amap, adart, amark)) );
    typename Map::size_type res=0;

    for (Iterator it(amap, adart, amark); it.cont(); ++it)
    {
      amap.mark(it, amark);
      ++res;
    }

    return res;
  }

  /** Unmark a given orbit with a given mark.
   * @param amap a combinatorial map.
   * @param adart a dart of the orbit.
   * @param amark the mark.
   * @return the number of darts of the unmarked orbit.
   * @pre The whole orbit must be marked.
   */
  template < class Map, class Iterator >
  typename Map::size_type unmark_orbit(const Map & amap,
                                       typename Map::Dart_const_descriptor adart,
                                       typename Map::size_type amark)
  {
    amap.negate_mark(amark);
    typename Map::size_type
        res=CGAL::template mark_orbit<Map, Iterator>(amap, adart, amark);
    amap.negate_mark(amark);
    return res;
  }

  /** Test if the two given darts belong to the same cell.
   * @param amap a combinatorial map.
   * @param adart1 the first dart.
   * @param adart2 the second dart.
   * @return true iff the two darts belong to the same cell.
   */
  template < class Map, unsigned int i, unsigned int d>
  bool belong_to_same_cell(const Map & amap,
                           typename Map::Dart_const_descriptor adart1,
                           typename Map::Dart_const_descriptor adart2)
  {
    return CGAL::belong_to_same_orbit<Map,
        typename Map::template Dart_of_cell_range<i,d>::const_iterator>
        (amap, adart1, adart2);
  }

  template < class Map, unsigned int i>
  bool belong_to_same_cell(const Map & amap,
                           typename Map::Dart_const_descriptor adart1,
                           typename Map::Dart_const_descriptor adart2)
  {
    return CGAL::template belong_to_same_cell<Map,i,Map::dimension>(amap,adart1,adart2);
  }


  /** Test if all the darts of a given cell are marked.
   * @param amap a combinatorial map.
   * @param adart a dart of the cell.
   * @param amark the mark.
   * @return true iff all the darts are marked.
   */
  template < class Map, unsigned int i, unsigned int d>
  bool is_whole_cell_marked(const Map & amap,
                            typename Map::Dart_const_descriptor adart,
                            typename Map::size_type amark)
  {
    return CGAL::template is_whole_orbit_marked<Map,
        typename Map::template Dart_of_cell_range<i,d>::const_iterator>
        (amap, adart, amark);
  }

  template < class Map, unsigned int i>
  bool is_whole_cell_marked(const Map & amap,
                            typename Map::Dart_const_descriptor adart,
                            typename Map::size_type amark)
  {
    return CGAL::template is_whole_cell_marked<Map,i,Map::dimension>
      (amap,adart,amark);
  }

  /** Test if all the darts of a given cell are unmarked.
   * @param amap a combinatorial map.
   * @param adart a dart of the cell.
   * @param amark the mark.
   * @return true iff all the darts are marked.
   */
  template < class Map, unsigned int i, unsigned int d >
  bool is_whole_cell_unmarked(const Map & amap,
                              typename Map::Dart_const_descriptor adart,
                              typename Map::size_type amark)
  {
    return CGAL::template is_whole_orbit_unmarked<Map,
        typename Map::template Dart_of_cell_range<i,d>::const_iterator>
        (amap, adart, amark);
  }

  template < class Map, unsigned int i>
  bool is_whole_cell_unmarked(const Map & amap,
                              typename Map::Dart_const_descriptor adart,
                              typename Map::size_type amark)
  {
    return CGAL::template is_whole_cell_unmarked<Map,i,Map::dimension>
        (amap,adart,amark);
  }

  /** Mark a given cell with a given mark.
   * @param amap a combinatorial map.
   * @param adart a dart of the cell.
   * @param amark the mark.
   * @return the number of darts of the marked cell.
   * @pre The whole cell must be unmarked.
   */
  template < class Map, unsigned int i, unsigned int d >
  typename Map::size_type mark_cell(const Map & amap,
                                    typename Map::Dart_const_descriptor adart,
                                    typename Map::size_type amark)
  { return CGAL::template mark_orbit<Map,
        typename Map::template Dart_of_cell_basic_range<i,d>::const_iterator>
        (amap, adart, amark); }

  template < class Map, unsigned int i>
  typename Map::size_type mark_cell(const Map & amap,
                                    typename Map::Dart_const_descriptor adart,
                                    typename Map::size_type amark)
  { return CGAL::template mark_cell<Map,i,Map::dimension>(amap, adart, amark);}

  /** Unmark a given cell with a given mark.
   * @param amap a combinatorial map.
   * @param adart a dart of the cell.
   * @param amark the mark.
   * @return the number of darts of the unmarked cell.
   * @pre The whole cell must be marked.
   */
  template < class Map, unsigned int i, unsigned int d >
  typename Map::size_type unmark_cell(const Map & amap,
                                      typename Map::Dart_const_descriptor adart,
                                      typename Map::size_type amark)
  { return CGAL::template unmark_orbit<Map,
        typename Map::template Dart_of_cell_basic_range<i,d>::const_iterator>
        (amap, adart, amark);}

  template < class Map, unsigned int i >
  typename Map::size_type unmark_cell(const Map & amap,
                                      typename Map::Dart_const_descriptor adart,
                                      typename Map::size_type amark)
  { return CGAL::template unmark_cell<Map,i,Map::dimension>(amap, adart, amark); }

  /// Functor to test if the given dart must be marked, when we want
  /// to mark an oriented orbit. By default (for CMap), return true.
  template<class Map, class T=typename Map::Combinatorial_data_structure>
  struct Need_to_mark_for_oriented_cell
  {
    static bool run(const Map & /*amap*/,
                    typename Map::Dart_const_descriptor /*adart*/,
                    typename Map::size_type /*amark*/,
                    OperationState /*op*/)
    { return true; }
  };

  struct Generalized_map_tag;

  /// Specialization for GMap. We mark one out of two darts.
  template<class Map>
  struct Need_to_mark_for_oriented_cell<Map, CGAL::Generalized_map_tag>
  {
    static bool run(const Map & amap,
                    typename Map::Dart_const_descriptor adart,
                    typename Map::size_type amark,
                    OperationState op)
    {
      CGAL_assertion(op!=OP_END);

      if (op==OP_NONE) return true;

      bool all_darts_unmarked=true;
      for (int i=0; all_darts_unmarked && i<=(int)Map::dimension; ++i)
      {
        if (!amap.is_free(adart, i) &&
            amap.is_marked(amap.alpha(adart, i), amark))
        { all_darts_unmarked=false; }
      }
      return all_darts_unmarked;
    }
  };

  /** Mark a given oriented orbit with a given mark. For combinatorial map,
   *  equivalent to mark_orbit. For generalized map, mark one out of two dart
   *  if the orbit is orientable. If amark2!=INVALID_MARK, mark also all the darts
   *  of the orbit with amark2.
   * @param amap a combinatorial map / generalized.
   * @param adart a dart of the orbit.
   * @param amark the mark.
   * @param amark2 a second mark.
   * @return the number of darts of the marked orbit.
   * @pre The whole orbit must be unmarked.
   */
  template < class Map, class Iterator >
  typename Map::size_type mark_oriented_orbit(const Map & amap,
                                              typename Map::Dart_const_descriptor adart,
                                              typename Map::size_type amark,
                                              typename Map::size_type amark2=Map::INVALID_MARK)
  {
    static_assert(std::is_same<typename Iterator::Basic_iterator,
                            Tag_true>::value);
    CGAL_assertion( (is_whole_orbit_unmarked<Map,
                     CMap_non_basic_iterator<Map,Iterator> >
                     (amap, adart, amark)) );
    typename Map::size_type res=0;
    typename Map::size_type amark3=(amark2==Map::INVALID_MARK?amap.get_new_mark():amark2);
    for (Iterator it(amap, adart, amark3); it.cont(); ++it)
    {
      amap.mark(it, amark3);
      if (CGAL::template Need_to_mark_for_oriented_cell<Map>::
          run(amap, it, amark, it.prev_operation()))
      {
        amap.mark(it, amark);
        ++res;
      }
    }

    if (amark2==Map::INVALID_MARK)
    {
      CGAL::template unmark_orbit<Map, Iterator>(amap, adart, amark3);
      amap.free_mark(amark3);
    }

    return res;
  }

  template < class Map, class Iterator >
  typename Map::size_type unmark_oriented_orbit(const Map & amap,
                                                typename Map::Dart_const_descriptor adart,
                                                typename Map::size_type amark,
                                                typename Map::size_type amark2=Map::INVALID_MARK)
  {
    amap.negate_mark(amark);
    if (amark2!=Map::INVALID_MARK) { amap.negate_mark(amark2); }
    typename Map::size_type
      res=CGAL::template mark_oriented_orbit<Map, Iterator>(amap, adart, amark, amark2);
    amap.negate_mark(amark);
    if (amark2!=Map::INVALID_MARK) { amap.negate_mark(amark2); }
    return res;
  }

  /** Mark a given oriented cell with a given mark. For combinatorial map,
   *  equivalent to mark_cell. For generalized map, mark one out of two dart
   *  if the cell is orientable. If amark2!=INVALID_MARK, mark also all the darts
   *  of the cell with amark2.
   * @param amap a combinatorial map / generalized.
   * @param adart a dart of the cell.
   * @param amark the mark.
   * @param amark2 a second mark.
   * @return the number of darts of the marked cell.
   * @pre The whole cell must be unmarked.
   */
  template < class Map, unsigned int i, unsigned int d >
  typename Map::size_type mark_oriented_cell(const Map & amap,
                                             typename Map::Dart_const_descriptor adart,
                                             typename Map::size_type amark,
                                             typename Map::size_type amark2=Map::INVALID_MARK)
  { return CGAL::template mark_oriented_orbit
      <Map, typename Map::template Dart_of_cell_basic_range<i,d>::const_iterator>
      (amap, adart, amark, amark2); }

  template < class Map, unsigned int i>
  typename Map::size_type mark_oriented_cell(const Map & amap,
                                             typename Map::Dart_const_descriptor adart,
                                             typename Map::size_type amark,
                                             typename Map::size_type amark2=Map::INVALID_MARK)
  { return CGAL::template mark_oriented_cell<Map,i,Map::dimension>
      (amap, adart, amark, amark2);}

  /** Unmark a given oriented cell with a given mark.
   * @param amap a combinatorial map.
   * @param adart a dart of the cell.
   * @param amark the mark.
   * @return the number of darts of the unmarked cell.
   * @pre The whole cell must be marked.
   */
  template < class Map, unsigned int i, unsigned int d >
  typename Map::size_type unmark_oriented_cell(const Map & amap,
                                               typename Map::Dart_const_descriptor adart,
                                               typename Map::size_type amark,
                                               typename Map::size_type amark2=Map::INVALID_MARK)
  { return CGAL::template unmark_oriented_orbit
      <Map, typename Map::template Dart_of_cell_basic_range<i,d>::const_iterator>
      (amap, adart, amark, amark2);}

  template < class Map, unsigned int i >
  typename Map::size_type unmark_oriented_cell(const Map & amap,
                                               typename Map::Dart_const_descriptor adart,
                                               typename Map::size_type amark,
                                               typename Map::size_type amark2=Map::INVALID_MARK)
  { return CGAL::template unmark_oriented_cell<Map,i,Map::dimension>
      (amap, adart, amark, amark2); }

  /** Compute the degree of a given i-cell c.
   * The degree is the number of distinct i+1 cells incident to c.
   * @param amap a combinatorial map.
   * @param adart a dart of the cell.
   * @return the degree of the cell.
   */
  template < class Map, unsigned int i >
  typename Map::size_type degree(const Map & amap,
                                 typename Map::Dart_const_descriptor adart)
  {
    CGAL_assertion(adart!=amap.null_descriptor);

    typename Map::size_type nbIncident = 0;
    typename Map::size_type mark;
    typename Map::size_type treated;
    mark = amap.get_new_mark();
    treated = amap.get_new_mark();

    typename Map::template
      Dart_of_cell_basic_range<i>::const_iterator it(amap, adart, mark);
    for ( ;it.cont(); ++it )
    {
      if (!amap.is_marked(it, treated))
      {
        ++nbIncident;
        CGAL::mark_cell<Map,i+1>(amap, it, treated);
      }
      amap.mark(it,mark);
    }

    amap.negate_mark(mark);
    for (it.rewind(); it.cont(); ++it)
    {
      if (amap.is_marked(it, treated))
      { CGAL::unmark_cell<Map,i+1>(amap, it, treated); }
      amap.mark(it,mark);
    }

    amap.negate_mark(mark);
    CGAL_assertion( amap.is_whole_map_unmarked(mark) );
    CGAL_assertion( amap.is_whole_map_unmarked(treated) );

    amap.free_mark(mark);
    amap.free_mark(treated);

    CGAL_assertion(nbIncident != 0);
    return nbIncident;
  }

  /** Compute the co-degree of a given i-cell c.
   * The co-degree is the number of distinct i-1 cells incident to c.
   * @param amap a combinatorial map.
   * @param adart a dart of the cell.
   * @return the co-degree of the cell.
   */
  template < class Map, unsigned int i >
  typename Map::size_type codegree(const Map & amap,
                                   typename Map::Dart_const_descriptor adart)
  {
    CGAL_assertion(adart!=amap.null_descriptor);

    typename Map::size_type nbIncident = 0;
    typename Map::size_type mark;
    typename Map::size_type treated;
    mark = amap.get_new_mark();
    treated = amap.get_new_mark();

    typename Map::template
      Dart_of_cell_basic_range<i>::const_iterator it(amap, adart, mark);
    for ( ; it.cont(); ++it)
    {
      if (!amap.is_marked(it, treated))
      {
        ++nbIncident;
        CGAL::mark_cell<Map,i-1>(amap, it, treated);
      }
      amap.mark(it,mark);
    }

    amap.negate_mark(mark);
    for (it.rewind(); it.cont(); ++it)
    {
      if (amap.is_marked(it, treated))
      { CGAL::unmark_cell<Map,i-1>(amap, it, treated); }
      amap.mark(it,mark);
    }

    amap.negate_mark(mark);
    CGAL_assertion( amap.is_whole_map_unmarked(mark) );
    CGAL_assertion( amap.is_whole_map_unmarked(treated) );

    amap.free_mark(mark);
    amap.free_mark(treated);

    CGAL_assertion(nbIncident != 0);
    return nbIncident;
  }

} // namespace CGAL

#endif // CGAL_COMBINATORIAL_MAP_BASIC_OPERATIONS_H //
// EOF //