forked from CGAL/cgal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Path_on_surface.h
1266 lines (1094 loc) · 38.3 KB
/
Path_on_surface.h
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
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2019 CNRS and LIRIS' Establishments (France).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
// $URL$
// $Id$
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s) : Guillaume Damiand <[email protected]>
//
#ifndef CGAL_PATH_ON_SURFACE_H
#define CGAL_PATH_ON_SURFACE_H 1
#include <CGAL/license/Surface_mesh_topology.h>
#include <CGAL/Combinatorial_map_operations.h>
#include <CGAL/Combinatorial_map.h>
#include <CGAL/Random.h>
#include <CGAL/Face_graph_wrapper.h>
#include <CGAL/Surface_mesh_topology/internal/Path_on_surface_with_rle.h>
#include <boost/algorithm/searching/knuth_morris_pratt.hpp>
#include <utility>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <initializer_list>
// A Path_on_surface contains two vectors of equal length n
// The first one is a vector of darts called m_path and the second one a vector
// of booleans called m_flip.
// If n = 0, the path represented by those vectors is the empty path.
// Else, it is the path represented by the n-1 first elements of both vectors,
// at the one we add the m_path[n-1] dart if m_flip[n-1] is false and the
// opposite of this dart if m_flip[n-1] is true i.e. if m_flip[i] is true means
// that the i-th dart m_path[i] has to be flipped.
// We use flips because sometimes opposite darts doesn't exist on surfaces with
// boundaries. But if m_flip[i] is true doesn't necesary mean that
// m_path[i] is 2-free
namespace CGAL {
namespace Surface_mesh_topology {
template<typename Mesh_>
class Path_on_surface
{
public:
typedef Path_on_surface<Mesh_> Self;
typedef Mesh_ Mesh;
typedef typename Get_map<Mesh, Mesh>::type Map; // Mesh seen as a 2-map
typedef typename Map::Dart_const_handle Dart_const_handle;
typedef Dart_const_handle halfedge_descriptor; // To be compatible with BGL
Path_on_surface(const Mesh& amesh) : m_map(amesh), m_is_closed(false)
{}
template<class COST>
Path_on_surface(const internal::Path_on_surface_with_rle<COST>& apath) :
m_map(apath.get_map()),
m_is_closed(apath.is_closed())
{
for (auto it=apath.m_path.begin(), itend=apath.m_path.end(); it!=itend; ++it)
{
push_back(it->begin, false, false);
if (it->length>0)
{ extend_straight_positive(it->length, false); }
else if (it->length<0)
{ extend_straight_negative(-(it->length), false); }
}
update_is_closed();
CGAL_assertion(is_valid(true));
}
Path_on_surface(const Self& apath) : m_map(apath.m_map),
m_path(apath.m_path),
m_is_closed(apath.m_is_closed),
m_flip(apath.m_flip)
{}
void swap(Self& p2)
{
if (this==&p2) { return; }
CGAL_assertion(&m_map==&(p2.m_map));
m_path.swap(p2.m_path);
std::swap(m_is_closed, p2.m_is_closed);
m_flip.swap(p2.m_flip);
}
Self& operator=(const Self& other)
{
CGAL_assertion(&m_map==&(other.m_map));
if (this!=&other)
{
m_path=other.m_path;
m_is_closed=other.m_is_closed;
m_flip=other.m_flip;
}
return *this;
}
/// @return true iff the path is empty
bool is_empty() const
{ return m_path.empty(); }
/// @return the length of the path, i.e. its number of darts.
std::size_t length() const
{ return m_path.size(); }
/// @return true iff the path is closed.
/// (m_is_closed is updated after each path modification).
bool is_closed() const
{ return m_is_closed; }
/// @return the combinatorial map supporting this path.
const Map& get_map() const
{ return m_map; }
/// @return the combinatorial map supporting this path.
const Mesh& get_mesh() const
{ return Get_map<Mesh, Mesh>::get_mesh(m_map); }
const std::vector<bool>& get_flip() const
{ return m_flip; }
/// clear the path.
void clear()
{
m_path.clear();
m_flip.clear();
m_is_closed=false;
}
/// @return true iff the next index exists
bool next_index_exists(std::size_t i) const
{ return is_closed() || i<(m_path.size()-1); }
/// @return the index after index i.
std::size_t next_index(std::size_t i) const
{ return ((is_closed() && i==(m_path.size()-1))?0:(i+1)); }
/// @return the index before index i.
std::size_t prev_index(std::size_t i) const
{ return ((is_closed() && i==0)?(m_path.size()-1):(i-1)); }
/// @return the ith dart of the path.
Dart_const_handle get_ith_dart(std::size_t i) const
{
CGAL_assertion(i<m_path.size());
return m_path[i];
}
/// @return true iff the ith dart is flipped
bool get_ith_flip(std::size_t i) const
{
CGAL_assertion(i<m_path.size());
return m_flip[i];
}
/// @return the ith dart of the path.
Dart_const_handle operator[] (std::size_t i) const
{ return get_ith_dart(i); }
/// @return the dart before the ith dart of the path,
/// nullptr if such a dart does not exist.
Dart_const_handle get_prev_dart(std::size_t i) const
{
CGAL_assertion(i<m_path.size());
if (i==0 && !is_closed()) return nullptr;
return m_path[prev_index(i)];
}
/// @return the dart after the ith dart of the path,
/// nullptr if such a dart does not exist.
Dart_const_handle get_next_dart(std::size_t i) const
{
CGAL_assertion(i<m_path.size());
if (i==m_path.size()-1 && !is_closed()) return nullptr;
return m_path[next_index(i)];
}
/// @return the flip before the ith flip of the path,
/// nullptr if such a flip does not exist.
bool get_prev_flip(std::size_t i) const
{
CGAL_assertion(i<m_path.size());
if (i==0 && !is_closed()) return false;
return m_flip[prev_index(i)];
}
/// @return the flip after the ith flip of the path,
/// nullptr if such a flip does not exist.
bool get_next_flip(std::size_t i) const
{
CGAL_assertion(i<m_path.size());
if (i==m_path.size()-1 && !is_closed()) return false;
return m_flip[next_index(i)];
}
/// @return the first dart of the path.
/// @pre !is_empty()
Dart_const_handle front() const
{
CGAL_assertion(!is_empty());
return m_path.front();
}
/// @return the last dart of the path.
/// @pre !is_empty()
Dart_const_handle back() const
{
CGAL_assertion(!is_empty());
return m_path.back();
}
/// @return the first flip of the path.
/// @pre !is_empty()
bool front_flip() const
{
CGAL_assertion(!is_empty());
return m_flip.front();
}
/// @return the last flip of the path.
/// @pre !is_empty()
bool back_flip() const
{
CGAL_assertion(!is_empty());
return m_flip.back();
}
/// @return the index of the first dart of the path.
/// @pre !is_empty()
std::size_t front_index() const
{ return get_map().darts().index(front()); }
/// @return the index of the last dart of the path.
/// @pre !is_empty()
std::size_t back_index() const
{ return get_map().darts().index(back()); }
/// @return the ith dart of the path taking into account flip.
/// return null_handle if flip and there is no beta2
Dart_const_handle get_ith_real_dart(std::size_t i) const
{
CGAL_assertion(i<m_path.size());
return (get_ith_flip(i)?get_map().template beta<2>(get_ith_dart(i)):
get_ith_dart(i));
}
/// @return the opposite of the ith dart of the path taking into account flip.
/// return null_handle if !flip and there is no beta2
Dart_const_handle get_opposite_ith_real_dart(std::size_t i) const
{
CGAL_assertion(i<m_path.size());
return (get_ith_flip(i)?get_ith_dart(i):
get_map().template beta<2>(get_ith_dart(i)));
}
/// @return the first dart of the path, taking into account flip.
/// @pre !is_empty()
Dart_const_handle real_front() const
{
CGAL_assertion(!is_empty());
return get_ith_real_dart(0);
}
/// @return the last dart of the path, taking into account flip.
/// @pre !is_empty()
Dart_const_handle real_back() const
{
CGAL_assertion(!is_empty());
return get_ith_real_dart(length()-1);
}
/// @return true iff df can be added at the end of the path.
bool can_be_pushed(Dart_const_handle dh, bool flip=false) const
{
// This assert is too long CGAL_assertion(m_map.darts().owns(dh));
if (is_empty()) return true;
return m_map.template belong_to_same_cell<0>
(m_flip.back() ? back() : m_map.other_extremity(back()),
flip ? m_map.other_extremity(dh) : dh);
}
/// Add the given dart at the end of this path.
/// @pre can_be_pushed(dh)
void push_back(Dart_const_handle dh, bool flip=false,
bool update_isclosed=true)
{
CGAL_assertion(dh!=Map::null_handle);
/* This assert is too long, it is tested in the is_valid method. */
// CGAL_assertion(can_be_pushed(dh, flip));
m_path.push_back(dh);
m_flip.push_back(flip);
if (update_isclosed) { update_is_closed(); }
}
/// @return true iff the ith dart can be added at the end of the path.
bool can_be_pushed_by_index(std::size_t i, bool flip=false,
bool update_isclosed=true) const
{ return can_be_pushed(get_map().dart_handle(i), flip, update_isclosed); }
/// Add the given ith dart at the end of this path.
void push_back_by_index(std::size_t i, bool flip=false,
bool update_isclosed=true)
{ push_back(get_map().dart_handle(i), flip, update_isclosed); }
void push_back_by_index(std::initializer_list<std::size_t> l,
bool update_isclosed=true)
{
for (std::size_t i : l)
{ push_back_by_index(i, false, update_isclosed); }
}
/// @return true iff the dart labeled e can be added at the end of the path.
bool can_be_pushed_by_label(const std::string& e, bool flip=false) const
{
Dart_const_handle dh=get_map().get_dart_labeled(e);
if (dh==nullptr) { return false; }
return can_be_pushed(dh, flip);
}
/// Add the dart having the given labels at the end of this path.
/// Each label is a word, possibly starting by -, words are separated by spaces
void push_back_by_label(const std::string& s, bool update_isclosed=true)
{
std::istringstream iss(s);
for (std::string e; std::getline(iss, e, ' '); )
{
Dart_const_handle dh=get_map().get_dart_labeled(e);
if (dh!=nullptr) { push_back(dh, false, update_isclosed); }
}
}
void push_back_by_label(std::initializer_list<const char*> l,
bool update_isclosed=true)
{
for (const char* e : l)
{ push_back_by_label(e, false, update_isclosed); }
}
Self& operator+=(const Self& other)
{
m_path.reserve(m_path.size()+other.m_path.size());
// Be careful to the special case when *this==other
// this is the reason of the iend.
for (std::size_t i=0, iend=other.length(); i<iend; ++i)
{ push_back(other[i], other.m_flip[i], false); }
update_is_closed();
return *this;
}
Self operator+(const Self& other) const
{
Self res=*this;
res+=other;
return res;
}
/// change m_path and m_flip in order to get the lower number of flips possible
void simplify_flips(bool show_flips_left=false)
{
if (show_flips_left)
{ std::cout<<"Flips left (maybe none) : "<<std::flush; }
for(unsigned int i=0; i<length(); ++i)
{
if (m_flip[i] && !get_map().template is_free<2>(m_path[i]))
{
m_path[i]=get_map().template beta<2>(m_path[i]);
m_flip[i]=!m_flip[i];
}
else if (show_flips_left)
{ std::cout<<i<<" "<<std::flush; }
}
if (show_flips_left)
{ std::cout<<std::endl; }
}
/// @return the number of flips of this path.
unsigned int nb_flips()
{
unsigned int res=0;
for (unsigned int i=0; i<length(); ++i)
{ if (m_flip[i]) ++res; }
return res;
}
/// Cut this path to keep only the n first darts.
void cut(std::size_t n, bool update_isclosed=true)
{
if (n>=length()) return;
m_path.resize(n);
m_flip.resize(n);
if (update_isclosed) { update_is_closed(); }
}
/// copy all darts starting from begin and going to the dart before end
/// from this path to new_path.
void copy_rest_of_path(std::size_t begin, std::size_t end,
Self& new_path)
{
CGAL_assertion(begin<=end);
CGAL_assertion(end<=length());
new_path.m_path.reserve(new_path.m_path.size()+end-begin+1);
while(begin!=end)
{
new_path.push_back(get_ith_dart(begin), get_ith_flip(begin), false);
++begin;
}
update_is_closed();
}
/// Debugging method.
void display_failed_extention(const std::string& /*name_of_function*/)
{
// std::cout<<"Cant extend the path this way ("<<name_of_function<<")"
// <<std::endl;
}
/// Extend the path straight positive.
/// @pre must be non empty.
void extend_straight_positive(std::size_t nb=1, bool update_isclosed=true)
{
if (is_empty() || nb==0)
{ display_failed_extention("extend_straight_positive"); return; }
Dart_const_handle dh=back();
if(back_flip())
{
if (get_map().template is_free<2>(dh))
{ display_failed_extention("extend_straight_positive"); return; }
else
{ dh=get_map().template beta<2>(dh); }
}
for (unsigned int i=0; i<nb; ++i)
{
dh=get_map().template beta<1>(dh);
if (get_map().template is_free<2>(dh))
{ display_failed_extention("extend_straight_positive"); return; }
dh=get_map().template beta<2, 1>(dh);
push_back(dh, false, false);
}
if (update_isclosed) { update_is_closed(); }
}
/// Extend the path straight negative.
/// @pre must be non empty.
void extend_straight_negative(std::size_t nb=1, bool update_isclosed=true)
{
if (is_empty() || nb==0)
{ display_failed_extention("extend_straight_negative"); return; }
Dart_const_handle dh=back();
if(!back_flip())
{
if (get_map().template is_free<2>(dh))
{ display_failed_extention("extend_straight_positive"); return; }
else
{ dh=get_map().template beta<2>(dh); }
}
for (unsigned int i=0; i<nb; ++i)
{
dh=get_map().template beta<0>(dh);
if (get_map().template is_free<2>(dh))
{ display_failed_extention("extend_straight_negative"); return; }
dh=get_map().template beta<2, 0>(dh);
push_back(dh, true, false);
}
if (update_isclosed) { update_is_closed(); }
}
/// Extend the path given a positive turn.
/// @pre must be non empty.
void extend_positive_turn(std::size_t nb=1, bool update_isclosed=true)
{
if (is_empty())
{ display_failed_extention("extend_positive_turn"); return; }
if (nb==0)
{
push_back(back(), !back_flip(), update_isclosed);
return;
}
Dart_const_handle dh=back();
if(back_flip())
{
if (get_map().template is_free<2>(dh))
{ display_failed_extention("extend_positive_turn"); return; }
else
{ dh=get_map().template beta<2>(dh); }
}
dh=get_map().template beta<1>(dh);
for (unsigned int i=1; i<nb; ++i)
{
if (get_map().template is_free<2>(dh))
{ display_failed_extention("extend_positive_turn"); return; }
dh=get_map().template beta<2, 1>(dh);
}
push_back(dh, false, update_isclosed);
}
/// Extend the path given a negative turn.
/// @pre must be non empty.
void extend_negative_turn(std::size_t nb=1, bool update_isclosed=true)
{
if (is_empty()) { display_failed_extention("extend_negative_turn"); return; }
if (nb==0)
{
push_back(back(), !back_flip(), update_isclosed);
return;
}
Dart_const_handle dh=back();
if(!back_flip())
{
if (get_map().template is_free<2>(dh))
{ display_failed_extention("extend_negative_turn"); return; }
else
{ dh=get_map().template beta<2>(dh); }
}
dh=get_map().template beta<0>(dh);
for (unsigned int i=1; i<nb; ++i)
{
if (get_map().template is_free<2>(dh))
{ display_failed_extention("extend_negative_turn"); return; }
dh=get_map().template beta<2, 0>(dh);
}
push_back(dh, true, update_isclosed);
}
/// Initializes this path to a random starting path.
/// @pre must be empty.
bool initialize_random_starting_dart(CGAL::Random& random,
bool update_isclosed=true)
{
if (!is_empty() || get_map().is_empty()) { return false; }
// first select a random edge by taking the lower index of
// the two darts when it is not a boundary
std::size_t index=static_cast<std::size_t>
(random.get_int(0, static_cast<int>(get_map().darts().capacity())));
while (!get_map().darts().is_used(index) ||
(!get_map().template is_free<2>(get_map().dart_handle(index)) &&
get_map().dart_handle(index)>get_map().
template beta<2>(get_map().dart_handle(index))))
{
++index;
if (index==get_map().darts().capacity()) index=0;
}
// second we take randomly one of the two darts of this edge
// (potentially with the help of a flip)
bool heads_or_tails=random.get_bool();
if (get_map().template is_free<2>(get_map().dart_handle(index)))
{
push_back(get_map().dart_handle(index), heads_or_tails, update_isclosed);
}
else
{
if (heads_or_tails)
{ push_back(get_map().dart_handle(index), false, update_isclosed); }
else
{ push_back(get_map().template beta<2>(get_map().dart_handle(index)),
false, update_isclosed); }
}
return true;
}
/// Initializes this path to a random starting path.
/// @pre must be empty.
bool initialize_random_starting_dart(bool update_isclosed=true)
{
CGAL::Random& random=get_default_random();
return initialize_random_starting_dart(random, update_isclosed);
}
/// Extends this path with a random dart.
/// @pre must be non empty.
bool extend_path_randomly(CGAL::Random& random,
bool allow_half_turn=true,
bool update_isclosed=true)
{
if (is_empty())
{ return initialize_random_starting_dart(random, update_isclosed); }
if(get_map().template is_free<1>(back()))
{ return false; }
Dart_const_handle next_vertex;
if (back_flip())
{ next_vertex=back(); }
else if (get_map().template is_free<2>(back()))
{ next_vertex=get_map().template beta<1>(back()); }
else
{ next_vertex=get_map().template beta<2>(back()); }
std::vector<std::pair<Dart_const_handle, bool> > candidats;
for (auto it=get_map().template darts_of_cell<0>(next_vertex).begin(),
itend=get_map().template darts_of_cell<0>(next_vertex).end();
it!=itend; ++it )
{
if (back_flip() || !get_map().template is_free<2>(back()))
{
candidats.push_back(std::make_pair(it, false));
if (get_map().template is_free<2>(get_map().template beta<0>(it)))
{ candidats.push_back
(std::make_pair(get_map().template beta<0>(it), true)); }
}
else
{
if (get_map().template is_free<2>(get_map().template beta<0>(it)))
{ candidats.push_back
(std::make_pair(get_map().template beta<0>(it), true)); }
candidats.push_back(std::make_pair(it, false));
}
}
//candidats is now the list of all the darts that can be pushed back to
// the path (maybe with a flip) the first of them in the list is the
// opposite of back(), or back() itself if it is 2-free
std::size_t i=static_cast<std::size_t>
(random.get_int(allow_half_turn?0:1,static_cast<int>(candidats.size())));
auto it=candidats.begin();
for (std::size_t nb=0; nb<i; ++nb, ++it) {}
push_back(it->first, it->second, update_isclosed);
return true;
}
/// Extends this path with a random dart.
/// @pre must be non empty.
bool extend_path_randomly(bool allow_half_turn=false,
bool update_isclosed=true)
{
CGAL::Random& random=get_default_random();
return extend_path_randomly(random, allow_half_turn, update_isclosed);
}
/// Generates a random path, with a number of darts >= length.
void generate_random_path(std::size_t length,
CGAL::Random& random=get_default_random(),
bool allow_half_turns=true,
bool update_isclosed=true)
{
m_path.reserve(m_path.size()+length);
for (std::size_t i=0; i<length; ++i)
{ extend_path_randomly(random, allow_half_turns, true); }
if (update_isclosed) { update_is_closed(); }
}
/// Generates a random path.
template<typename Path>
void generate_random_path(CGAL::Random& random,
bool update_isclosed=true)
{ generate_random_path(random.get_int(1, 10000),
random, true, update_isclosed); }
/// Generates a random path.
template<typename Path>
void generate_random_path(std::size_t length,
bool update_isclosed=true)
{
CGAL::Random& random=get_default_random();
generate_random_path(length, random, true, update_isclosed);
}
/// Generates a random path.
template<typename Path>
void generate_random_path(bool update_isclosed=true)
{
CGAL::Random& random=get_default_random();
generate_random_path(random, update_isclosed);
}
/// Generates a random closed path.
void generate_random_closed_path(std::size_t length, CGAL::Random& random)
{
m_path.reserve(m_path.size()+length);
std::size_t i=0;
while(i<length || !is_closed())
{
extend_path_randomly(random, true, true);
++i;
}
}
/// Generates a random closed path.
void generate_random_closed_path(std::size_t length)
{
CGAL::Random& random=get_default_random();
generate_random_closed_path(length, random);
}
/// Generates a random closed path.
void generate_random_closed_path(CGAL::Random& random)
{ generate_random_closed_path(random.get_int(1, 10000), random); }
/// Generates a random closed path.
void generate_random_closed_path()
{
CGAL::Random& random=get_default_random();
generate_random_closed_path(random.get_int(1, 10000), random);
}
/// Replace edge [i] by the path of darts along the face.
/// If this face does not exist (if it is a boundary) then replace the edge
/// by the face on the other side. Problem of complexity when used many times
/// (like in update_path_randomly).
bool push_around_face(std::size_t i, bool update_isclosed=true)
{
CGAL_assertion(i<length());
// It is not possible to push around a perforated face since it changes
// the homotopy of the path.
if (get_map().is_perforated(get_ith_dart(i))) { return false; }
Self p2(get_mesh());
// 1) We add in p2 the part of the path which is pushed.
if (get_ith_flip(i))
{
Dart_const_handle dh=get_map().template beta<1>(get_ith_dart(i));
do
{
p2.push_back(dh, false, false);
dh=get_map().template beta<1>(dh);
}
while(dh!=get_ith_dart(i));
}
else
{
Dart_const_handle dh=get_map().template beta<0>(get_ith_dart(i));
do
{
p2.push_back(dh, true, false);
dh=get_map().template beta<0>(dh);
}
while(dh!=get_ith_dart(i));
}
// 2) We copy the end of the path.
p2.m_path.reserve(p2.length()+length()-i);
for (std::size_t j=i+1; j<length(); ++j)
{ p2.push_back(get_ith_dart(j), get_ith_flip(j), false); }
// 3) We cut this path to keep the first i darts.
cut(i, false);
m_path.reserve(length()+p2.length());
for (std::size_t j=0; j<p2.length(); ++j)
{ push_back(p2[j], p2.get_ith_flip(j), false); }
if (update_isclosed) { update_is_closed(); }
return true;
//CGAL_assertion(is_valid());
}
/// Transform the current path by pushing some dart around faces.
/// At the end, the new path is homotopic to the original one.
void update_path_randomly(std::size_t nb, CGAL::Random& random,
bool update_isclosed=true)
{
if (is_empty()) return;
for (unsigned int i=0; i<nb; ++i)
{
std::size_t dartn=static_cast<std::size_t>
(random.get_int(0, static_cast<int>(length())));
std::size_t j=dartn;
while(!push_around_face(dartn, false) && dartn!=j)
{ ++dartn; }
}
if (update_isclosed) { update_is_closed(); }
}
void update_path_randomly(CGAL::Random& random,
bool update_isclosed=true)
{ update_path_randomly(random.get_int(0, 10000), update_isclosed); }
void update_path_randomly(std::size_t nb, bool update_isclosed=true)
{
CGAL::Random random;
update_path_randomly(nb, random, update_isclosed);
}
void update_path_randomly(bool update_isclosed=true)
{
CGAL::Random& random=get_default_random();
update_path_randomly(random, update_isclosed);
}
/// @return true iff the i-th dart of the path and the j-th dart of the other
/// are the same (taking into account the flips !)
bool are_same_step(std::size_t i, const Self& other, std::size_t j) const
{
if (get_ith_flip(i)==other.get_ith_flip(j))
{ return get_ith_dart(i)==other[j]; }
if (get_map().template is_free<2>(get_ith_dart(i)) ||
get_map().template is_free<2>(other[j]))
{ return false; }
return get_ith_dart(i)==get_map().template beta<2>(other[j]);
}
/// @return true if this path is equal to other path, identifying dart 0 of
/// this path with dart start in other path.
bool are_same_paths_from(const Self& other, std::size_t start) const
{
CGAL_assertion(start==0 || start<length());
CGAL_assertion(is_closed() || start==0);
CGAL_assertion(length()==other.length() && is_closed()==other.is_closed());
for(std::size_t i=0; i<length(); ++i)
{
if (!are_same_step(i, other, start))
{ return false; }
start=next_index(start);
}
return true;
}
/// @return true if this path is equal to other path. For closed paths, test
/// all possible starting darts. Old quadratic version, new version
/// (operator==) use linear version based on Knuth, Morris, Pratt
bool are_paths_equals(const Self& other) const
{
if (length()!=other.length() || is_closed()!=other.is_closed())
{ return false; }
if (!is_closed())
{ return are_same_paths_from(other, 0); }
for(std::size_t start=0; start<length(); ++start)
{
if (are_same_paths_from(other, start))
{ return true; }
}
return false;
}
/// @return true if this path is equal to other path. For closed paths,
/// equality is achieved whatever the first dart.
bool operator==(const Self& other) const
{
if (length()!=other.length() || is_closed()!=other.is_closed())
{ return false; }
if (!is_closed())
{ return are_same_paths_from(other, 0); }
Self pp1=*this;
pp1.simplify_flips();
Self pp2=other;
pp2.simplify_flips();
pp2+=pp2;
// Now we search if pp1 is a sub-motif of pp2 <=> *this==other
return boost::algorithm::knuth_morris_pratt_search(pp2.m_path.begin(),
pp2.m_path.end(),
pp1.m_path.begin(),
pp1.m_path.end())
#if BOOST_VERSION>=106200
.first
#endif
!=pp2.m_path.end();
}
bool operator!=(const Self& other) const
{ return !(operator==(other)); }
/// @Return true if this path is equal to other path, identifying dart 0 of
/// this path with dart start in other path. other path is given
/// by index of its darts, in text format.
bool are_same_paths_from(const char* other, std::size_t start) const
{
CGAL_assertion(start==0 || start<length());
CGAL_assertion(is_closed() || start==0);
std::string sother(other);
std::istringstream iss(sother);
uint64_t nb;
for(std::size_t i=0; i<length(); ++i)
{
if (!iss.good())
{ return false; }
iss>>nb;
if (nb!=m_map.darts().index(get_ith_dart(start)))
{ return false; }
start=next_index(start);
}
iss>>nb;
if (iss.good())
{ return false; } // There are more elements in other than in this path
return true;
}
/// @return true if this path is equal to other path. For closed paths, test
/// all possible starting darts. other path is given by index of its
/// darts, in text format.
bool operator==(const char* other) const
{
if (!is_closed())
{ return are_same_paths_from(other, 0); }
for(std::size_t start=0; start<length(); ++start)
{
if (are_same_paths_from(other, start))
{ return true; }
}
return false;
}
bool operator!=(const char* other) const
{ return !(operator==(other)); }
/// @return true iff the path is valid; i.e. a sequence of edges two by
/// two adjacent.
bool is_valid(bool display_error=false) const
{
if (is_empty()) { return !is_closed(); } // an empty past is not closed
Dart_const_handle last_vertex;
for (unsigned int i=1; i<m_path.size(); ++i)
{
/* This assert is long if (!m_map.darts().owns(m_path[i]))
{ return false; } */
if (m_path[i]==m_map.null_dart_handle)
{ return false; }
last_vertex=m_flip[i-1]?m_path[i-1]:get_map().beta(m_path[i-1], 1);
if (last_vertex==Map::null_handle)
{
if (display_error)
{ std::cout<<"Invalid path: one of the vertices doesn't exist"
<<std::endl; }
return false;
}
if (!m_map.template belong_to_same_cell<0>
(m_flip[i]?get_map().beta(m_path[i], 1):m_path[i], last_vertex))
{
if (display_error)
{ std::cout<<"Invalid path: dart "<<i-1<<" and dart "<<i
<<" are not adjacents"<<std::endl; }
return false;
}
}
last_vertex=back_flip()?back():get_map().template beta<1>(back());
if (is_closed())
{
if (last_vertex==Map::null_handle)
{
if (display_error)
{ std::cout<<"Invalid path: one of the vertices doesn't exist"
<<std::endl; }
return false;
}
if (!m_map.template belong_to_same_cell<0>
(front_flip()?get_map().beta(front(), 1):front(), last_vertex))
{
if (display_error)
{ std::cout<<"Invalid path: m_is_closed is true but the path is "
<<"not closed"<<std::endl; }
return false;
}
}
else
{
if (last_vertex==Map::null_handle)
{
if (display_error)
{ std::cout<<"Invalid path: one of the vertices doesn't exist"
<<std::endl; }
return false;
}
if (m_map.template belong_to_same_cell<0>
(front_flip()?get_map().beta(front(), 1):front(), last_vertex))
{
if (display_error)