// Copyright (c) 2019 CNRS and LIRIS' Establishments (France). // All rights reserved. // // This file is part of CGAL (www.cgal.org). // // $URL: https://github.com/CGAL/cgal/blob/v5.1/Surface_mesh_topology/include/CGAL/Path_on_surface.h $ // $Id: Path_on_surface.h 52186a0 2020-05-14T11:38:15+02:00 Guillaume Damiand // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial // // Author(s) : Guillaume Damiand // #ifndef CGAL_PATH_ON_SURFACE_H #define CGAL_PATH_ON_SURFACE_H 1 #include #include #include #include #include #include #include #include #include #include #include #include #include // 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 class Path_on_surface { public: typedef Path_on_surface Self; typedef Mesh_ Mesh; typedef typename Get_map::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 Path_on_surface(const internal::Path_on_surface_with_rle& 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(&get_mesh()==&(p2.get_mesh())); 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(&get_mesh()==&(other.get_mesh())); 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::get_mesh(m_map); } const std::vector& 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 prev index exists bool prev_index_exists(std::size_t i) const { return is_closed() || i>0; } /// @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_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(typename Map::size_type 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(typename Map::size_type 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 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==Map::null_handle) { 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!=Map::null_handle) { push_back(dh, false, update_isclosed); } } } void push_back_by_label(std::initializer_list 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(m_path[i])) { m_path[i]=get_map().opposite2(m_path[i]); m_flip[i]=!m_flip[i]; } else if (show_flips_left) { std::cout<=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 ("<(dh)) { display_failed_extention("extend_straight_positive"); return; } else { dh=get_map().opposite2(dh); } } for (unsigned int i=0; i(dh)) { display_failed_extention("extend_straight_positive"); return; } dh=get_map().next(get_map().opposite2(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().opposite2(dh); } } for (unsigned int i=0; i(dh)) { display_failed_extention("extend_straight_negative"); return; } dh=get_map().previous(get_map().opposite2(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().opposite2(dh); } } dh=get_map().next(dh); for (unsigned int i=1; i(dh)) { display_failed_extention("extend_positive_turn"); return; } dh=get_map().next(get_map().opposite2(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().opposite2(dh); } } dh=get_map().previous(dh); for (unsigned int i=1; i(dh)) { display_failed_extention("extend_negative_turn"); return; } dh=get_map().previous(get_map().opposite2(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 typename Map::size_type index=static_cast (random.get_int(0, static_cast(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(). opposite2(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().opposite2(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().next(back()); } else { next_vertex=get_map().opposite2(back()); } std::vector > 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().previous(it))) { candidats.push_back (std::make_pair(get_map().previous(it), true)); } } else { if (get_map().template is_free<2>(get_map().previous(it))) { candidats.push_back (std::make_pair(get_map().previous(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 (random.get_int(allow_half_turn?0:1,static_cast(candidats.size()))); auto it=candidats.begin(); for (std::size_t nb=0; nbfirst, 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 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 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 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 (random.get_int(0, static_cast(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().opposite2(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 *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>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 (m_flip[i]?get_map().next(m_path[i]):m_path[i], last_vertex)) { if (display_error) { std::cout<<"Invalid path: dart "< (front_flip()?get_map().next(front()):front(), last_vertex)) { if (display_error) { std::cout<<"Invalid path: m_is_closed is true but the path is " <<"not closed"< (front_flip()?get_map().next(front()):front(), last_vertex)) { if (display_error) { std::cout<<"Invalid path: m_is_closed is false but the path " <<"is closed"<(pbegin, pend); } } } /// @return true iff the path does not pass twice through a same edge /// or a same vertex. bool is_simple() const { typename Map::size_type markvertex=m_map.get_new_mark(); typename Map::size_type markedge=m_map.get_new_mark(); bool res=true; Dart_const_handle dh_vertex; unsigned int i=0; for (i=0; res && i(m_map, dh_vertex, markvertex); } if (m_map.is_marked(m_path[i], markedge)) { res=false; } else { CGAL::mark_cell(m_map, m_path[i], markedge); } } i=0; while(m_map.number_of_marked_darts(markedge)>0 || m_map.number_of_marked_darts(markvertex)>0) { CGAL_assertion(i(m_map, dh_vertex, markvertex); } if (m_map.is_marked(m_path[i], markedge)) { CGAL::unmark_cell(m_map, m_path[i], markedge); } ++i; } m_map.free_mark(markvertex); m_map.free_mark(markedge); return res; } /// Reverse the path (i.e. negate its orientation). void reverse() { bool tmpbool; for (unsigned int i=0; i=0; --i) { m_path.push_back(m_path[i], !m_flip[i], false); } m_is_closed=true; } } /// @return the turn between dart number i and dart number i+1. /// (turn is position of the second edge in the cyclic ordering of /// edges starting from the first edge around the second extremity /// of the first dart) std::size_t next_positive_turn(std::size_t i) const { // CGAL_assertion(is_valid()); CGAL_assertion(i(get_ith_dart(i))) || (get_next_flip(i) && get_map().template is_free<2>(get_next_dart(i)))) { return (std::numeric_limits::max)(); } return m_map.positive_turn(get_ith_real_dart(i), get_ith_real_dart(next_index(i))); } /// Same than next_positive_turn but turning in reverse orientation /// around vertex. std::size_t next_negative_turn(std::size_t i) const { // CGAL_assertion(is_valid()); CGAL_assertion(i(get_ith_dart(i))) || (!get_next_flip(i) && get_map().template is_free<2>(get_next_dart(i)))) { return (std::numeric_limits::max)(); } return m_map.positive_turn(get_opposite_ith_real_dart(next_index(i)), get_opposite_ith_real_dart(i)); } /// Computes all positive turns of this path. std::vector compute_positive_turns() const { std::vector res; if (is_empty()) return res; std::size_t i; for (i=0; i compute_negative_turns() const { std::vector res; if (is_empty()) return res; std::size_t i; for (i=0; i compute_turns(bool p) const { return (p?compute_positive_turns():compute_negative_turns()); } bool same_turns_from(const char* turns, const std::vector& resplus, const std::vector& resmoins, std::size_t start) const { CGAL_assertion(start==0 || start>nb; if ((nb>=0 && resplus[start]!=static_cast(nb)) || (nb<0 && resmoins[start]!=static_cast(-nb))) { return false; } start=next_index(start); } iss>>nb; if (iss.good()) { return false; } // There are more elements in turns than in res return true; } bool same_turns(const char* turns) const { std::vector resplus=compute_positive_turns(); std::vector resmoins=compute_negative_turns(); if (!is_closed()) { return same_turns_from(turns, resplus, resmoins, 0); } for (std::size_t start=0; start res=compute_positive_turns(); for (std::size_t i=0; i res=compute_negative_turns(); for (std::size_t i=0; i::storage_type m_map; // The underlying map std::vector m_path; /// The sequence of darts bool m_is_closed; /// True iff the path is a cycle std::vector m_flip; /// The sequence of flips }; } // namespace Surface_mesh_topology } // namespace CGAL #endif // CGAL_PATH_ON_SURFACE_H // // EOF //