Code

52410896500b6df8de161b9dea8012206835678a
[inkscape.git] / src / 2geom / convex-cover.h
1 #ifndef GEOM_CONVEX_COVER_H
2 #define GEOM_CONVEX_COVER_H
4 /**
5  * \file
6  * \brief \todo brief description
7  *
8  * Copyright 2006 Nathan Hurst <njh@mail.csse.monash.edu.au>
9  * Copyright 2006 Michael G. Sloan <mgsloan@gmail.com>
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it either under the terms of the GNU Lesser General Public
13  * License version 2.1 as published by the Free Software Foundation
14  * (the "LGPL") or, at your option, under the terms of the Mozilla
15  * Public License Version 1.1 (the "MPL"). If you do not alter this
16  * notice, a recipient may use your version of this file under either
17  * the MPL or the LGPL.
18  *
19  * You should have received a copy of the LGPL along with this library
20  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  * You should have received a copy of the MPL along with this library
23  * in the file COPYING-MPL-1.1
24  *
25  * The contents of this file are subject to the Mozilla Public License
26  * Version 1.1 (the "License"); you may not use this file except in
27  * compliance with the License. You may obtain a copy of the License at
28  * http://www.mozilla.org/MPL/
29  *
30  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
31  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
32  * the specific language governing rights and limitations.
33  *
34  */
36 /** A convex cover is a sequence of convex polygons that completely cover the path.  For now a
37  * convex hull class is included here (the convex-hull header is wrong)
38  */
40 #include <2geom/point.h>
41 #include <vector>
43 namespace Geom{
45 /** ConvexHull
46  * A convexhull is a convex region - every point between two points in the convex hull is also in
47  * the convex hull.  It is defined by a set of points travelling in a clockwise direction.  We require the first point to be top most, and of the topmost, leftmost.
49  * An empty hull has no points, we allow a single point or two points degenerate cases.
51  * We could provide the centroid as a member for efficient direction determination.  We can update the
52  * centroid with all operations with the same time complexity as the operation.
53  */
55 class ConvexHull{
56 public: // XXX: should be private :)
57     // extracts the convex hull of boundary. internal use only
58     void find_pivot();
59     void angle_sort();
60     void graham_scan();
61     void graham();
62 public:
63     std::vector<Point> boundary;
64     //Point centroid;
66     void merge(Point p);
67     bool contains_point(Point p);
68     bool strict_contains_point(Point p);
70     inline Point operator[](int i) const {
72         int l = boundary.size();
73         if(l == 0) return Point();
74         return boundary[i >= 0 ? i % l : (i % l) + l];
75     }
77     /*inline Point &operator[](unsigned i) {
78         int l = boundary.size();
79         if(l == 0) return Point();
80         return boundary[i >= 0 ? i % l : i % l + l];
81     }*/
83 public:
84     ConvexHull() {}
85     ConvexHull(std::vector<Point> const & points) {
86         boundary = points;
87         graham();
88     }
90     template <typename T>
91     ConvexHull(T b, T e) :boundary(b,e) {}
93     ~ConvexHull()
94     {
95     }
97 public:
98     /** Is the convex hull clockwise?  We use the definition of clockwise from point.h
99     **/
100     bool is_clockwise() const;
101     bool no_colinear_points() const;
102     bool top_point_first() const;
103     bool meets_invariants() const;
105     // contains no points
106     bool empty() const { return boundary.empty();}
108     // contains exactly one point
109     bool singular() const { return boundary.size() == 1;}
111     //  all points are on a line
112     bool linear() const { return boundary.size() == 2;}
113     bool is_degenerate() const;
115     // area of the convex hull
116     double centroid_and_area(Geom::Point& centroid) const;
117     double area() const {
118         Point tmp;
119         return centroid_and_area(tmp);
120     }
122     // furthest point in a direction (lg time)
123     Point const * furthest(Point direction) const;
125     bool is_left(Point p, int n);
126     bool is_strict_left(Point p, int n);
127     int find_left(Point p);
128     int find_strict_left(Point p);
129     double narrowest_diameter(Point &a, Point &b, Point &c);
131 };
133 // do two convex hulls intersect?
134 bool intersectp(ConvexHull a, ConvexHull b);
136 std::vector<Point> bridge_points(ConvexHull a, ConvexHull b);
138 // find the convex hull intersection
139 ConvexHull intersection(ConvexHull a, ConvexHull b);
140 ConvexHull sweepline_intersection(ConvexHull const &a, ConvexHull const &b);
142 // find the convex hull of a set of convex hulls
143 ConvexHull merge(ConvexHull a, ConvexHull b);
145 // naive approach
146 ConvexHull graham_merge(ConvexHull a, ConvexHull b);
148 unsigned find_bottom_right(ConvexHull const &a);
150 /*** Arbitrary transform operator.
151  * Take a convex hull and apply an arbitrary convexity preserving transform.
152  *  we should be concerned about singular tranforms here.
153  */
154 template <class T> ConvexHull operator*(ConvexHull const &p, T const &m) {
155     ConvexHull pr;
157     pr.boundary.reserve(p.boundary.size());
159     for(unsigned i = 0; i < p.boundary.size(); i++) {
160         pr.boundary.push_back(p.boundary[i]*m);
161     }
162     return pr;
165 ConvexHull clip(ConvexHull const & ch, Point n, double d);
167 //TODO: reinstate
168 /*class ConvexCover{
169 public:
170     Path const* path;
171     std::vector<ConvexHull> cc;
173     ConvexCover(Path const &sp);
174 };*/
176 };
178 #endif //2GEOM_CONVEX_COVER_H
180 /*
181   Local Variables:
182   mode:c++
183   c-file-style:"stroustrup"
184   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
185   indent-tabs-mode:nil
186   fill-column:99
187   End:
188 */
189 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :