⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 polygon_2_simplicity.h

📁 很多二维 三维几何计算算法 C++ 类库
💻 H
📖 第 1 页 / 共 2 页
字号:
// Copyright (c) 2001  Utrecht University (The Netherlands),// ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),// INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg// (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),// and Tel-Aviv University (Israel).  All rights reserved.//// This file is part of CGAL (www.cgal.org); you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public License as// published by the Free Software Foundation; version 2.1 of the License.// See the file LICENSE.LGPL distributed with CGAL.//// Licensees holding a valid commercial license may use this file in// accordance with the commercial license agreement provided with the software.//// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.//// $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.3-branch/Polygon/include/CGAL/Polygon_2/Polygon_2_simplicity.h $// $Id: Polygon_2_simplicity.h 31310 2006-05-29 07:28:42Z wein $// //// Author(s)     : Geert-Jan Giezeman <geert@cs.uu.nl>#ifndef CGAL_POLYGON_2_SIMPLICITY_H#define CGAL_POLYGON_2_SIMPLICITY_H#include <CGAL/enum.h>#include <CGAL/Polygon_2/polygon_assertions.h>#include <set>#include <vector>#include <algorithm>/*  A polygon is called simple of no edges intersect each other, except  consecutive edges, which intersect in their common vertex.  The test for simplicity is implemented by means of a sweep line algorithm.  The vertical line is swept from left to right. The edges of the polygon that  are crossed by the sweep line are stored in a tree from bottom to top.  We discern three types of events:  - insertion events. When both edges of a polygon vertex extend to the right    we need to insert both edges in the tree. We need to search with the vertex    to find out between which edges the new edges are to be inserted.  - deletion events. When both edges extend to the left of the vertex we need    to remove both edges from the tree. We have to check that the vertex lies    between the edges above and below the removed edges.  - replacement event. In the other case we need to replace the edge that    extends to the left by the edge that extends to the right. We need to check    that the vertex lies between the edges above and below the current edge.  We represent the tree by a std::set. This is not a perfect fit for the  operations described above. In particular, the fact that we search with a  VERTEX, while the set contains EDGES, is not directly supported. The  insertion of edges is also complicated by the fact that we need to insert  two edges starting at the same vertex. The order in which they are inserted  in the tree does matter, because the edges go in separate directions.  Because of this the set needs a special associated comparison function.  Every edge has a flag 'is_in_tree', which is true for the edges in the tree  but false for the edges when they are inserted. The comparison function  treats the latter type of edge as a vertex. Another flag, is_left_to_right,  tells which of the two vertices to take. The problem of the coinciding  vertices is solved by the convention that the highest edges is always  inserted first. The comparison function uses this fact.  Vertex indices of the polygon play a double role. The number v can be used to  identify vertex v or the edge from vertex v to vertex v+1.*/namespace CGAL {namespace i_polygon { // namespace CGAL::i_polygon is used for internal functionstypedef std::vector<int>::size_type Index_t;struct Vertex_index {    Vertex_index() {}    explicit Vertex_index(Index_t i): m_i(i) {}    Index_t as_int() const {return m_i;}    Vertex_index operator++() {++m_i; return *this; }private:    Index_t m_i;};struct Vertex_order {    explicit Vertex_order(Index_t i): m_i(i) {}    Index_t as_int() {return m_i;}private:    Index_t m_i;};template <class ForwardIterator, class PolygonTraits>class Vertex_data ;template <class VertexData>class Less_segments {    typedef VertexData         Vertex_data;    Vertex_data *m_vertex_data;    bool less_than_in_tree(Vertex_index i, Vertex_index j);  public:    Less_segments(Vertex_data *vertex_data) : m_vertex_data(vertex_data) {}    bool operator()(Vertex_index i, Vertex_index j);};// The data in Edge_data is attached to an edge when it is (about to be)// inserted in the tree.// Although conceptually this data belongs in the tree, it is stored with// the vertices in the Vertex_data structure.template <class LessSegments>struct Edge_data {    typedef std::set<Vertex_index, LessSegments> Tree;    Edge_data() : is_in_tree(false) {}    Edge_data(typename Tree::iterator it) : tree_it(it), is_in_tree(false) {}    typename Tree::iterator tree_it; // The iterator of the edge in the tree.                                     // Needed for cross reference. If edge j				     // is in the tree: *edges[j].tree_it == j    bool is_in_tree :1;              // Must be set -after- inserting the edge                                     // in the tree. Plays a role in the				     // comparison function of the tree.    bool is_left_to_right :1;        // Direction of edge from vertex v to v+1};template <class ForwardIterator, class PolygonTraits>class Vertex_data_base {public:    typedef typename PolygonTraits::Point_2              Point_2;//    ForwardIterator points_start;    std::vector<ForwardIterator> iterators;    std::vector<Vertex_order> m_order_of;    std::vector<Vertex_index> m_idx_at_rank;    std::vector<Vertex_index>::size_type m_size;    typename PolygonTraits::Orientation_2 orientation_2;    typename PolygonTraits::Less_xy_2 less_xy_2;    bool is_simple_result;    Vertex_data_base(ForwardIterator begin, ForwardIterator end,                const PolygonTraits& pgnt);    bool ordered_left_to_right(Vertex_index v1, Vertex_index v2)        { return  m_order_of[v1.as_int()].as_int() <	m_order_of[v2.as_int()].as_int();}    Vertex_index index_at_rank(Vertex_order vo) const        { return m_idx_at_rank[vo.as_int()];}    Vertex_index next(Vertex_index k) const        { ++k; return k.as_int() == m_size ? Vertex_index(0) : k;}    Vertex_index prev(Vertex_index k) const        { return k.as_int() == 0	       ?  Vertex_index(m_size-1)	       : Vertex_index(k.as_int()-1);	}    Point_2 point(Vertex_index i)        { return *iterators[i.as_int()];}//    { return points_start[i.as_int()];}};template <class ForwardIterator, class PolygonTraits>class Vertex_data : public Vertex_data_base<ForwardIterator, PolygonTraits> {public:    typedef Vertex_data Self;  // Indirection needed by Borland compiler    typedef Less_segments<Self> Less_segs;    typedef std::set<Vertex_index, Less_segs> Tree;    typedef Vertex_data_base<ForwardIterator, PolygonTraits> Base_class;    using Base_class::ordered_left_to_right;    using Base_class::next;    using Base_class::prev;    using Base_class::index_at_rank;    using Base_class::point;    std::vector<Edge_data<Less_segs> > edges;    Vertex_data(ForwardIterator begin, ForwardIterator end,                const PolygonTraits& pgnt);    void init(Tree *tree);    void left_and_right_index(Vertex_index &left, Vertex_index &right,            Vertex_index edge);    Vertex_index left_index(Vertex_index edge)        { return edges[edge.as_int()].is_left_to_right ? edge : next(edge); }    void sweep(Tree *tree);    bool insertion_event(Tree *tree,                Vertex_index i, Vertex_index j, Vertex_index k);    bool replacement_event(Tree *tree,                Vertex_index cur, Vertex_index to_insert);    bool deletion_event(Tree *tree, Vertex_index i, Vertex_index j);    bool on_right_side(Vertex_index vt, Vertex_index edge, bool above);};template <class VertexData>class Less_vertex_data {    VertexData *m_vertex_data;public:    Less_vertex_data(VertexData *vd)    : m_vertex_data(vd) {}    bool operator()(Vertex_index i, Vertex_index j);};} // end of namespace i_polygon// ----- implementation of i_polygon functions. -----namespace i_polygon {template <class VertexData>bool Less_segments<VertexData>::operator()(Vertex_index i, Vertex_index j){    if (m_vertex_data->edges[j.as_int()].is_in_tree) {        return less_than_in_tree(i,j);    } else {        return !less_than_in_tree(j,i);    }}template <class VertexData>bool Less_segments<VertexData>::less_than_in_tree(Vertex_index new_edge, Vertex_index tree_edge){    CGAL_polygon_precondition(       m_vertex_data->edges[tree_edge.as_int()].is_in_tree);    CGAL_polygon_precondition(       !m_vertex_data->edges[new_edge.as_int()].is_in_tree);    Vertex_index left, mid, right;    m_vertex_data->left_and_right_index(left, right, tree_edge);    mid = m_vertex_data->left_index(new_edge);    if (mid.as_int() == left.as_int()) {        return true;    }    switch (m_vertex_data->orientation_2( m_vertex_data->point(left),            m_vertex_data->point(mid), m_vertex_data->point(right))) {      case LEFT_TURN: return true;      case RIGHT_TURN: return false;      case COLLINEAR: break;    }    m_vertex_data->is_simple_result = false;    return true;}template <class VertexData>bool Less_vertex_data<VertexData>::operator()(Vertex_index i, Vertex_index j){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -