1 /**
2 * \brief Shapes are special paths on which boolops can be performed
3 *
4 * Authors:
5 * Michael G. Sloan <mgsloan@gmail.com>
6 * Nathan Hurst <njh@mail.csse.monash.edu.au>
7 * MenTaLguY <mental@rydia.net>
8 *
9 * Copyright 2007-2009 Authors
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 #ifndef __2GEOM_SHAPE_H
37 #define __2GEOM_SHAPE_H
39 #include <vector>
40 #include <set>
42 #include <2geom/region.h>
44 //TODO: BBOX optimizations
46 namespace Geom {
48 enum {
49 BOOLOP_JUST_A = 1,
50 BOOLOP_JUST_B = 2,
51 BOOLOP_BOTH = 4,
52 BOOLOP_NEITHER = 8
53 };
55 enum {
56 BOOLOP_NULL = 0,
57 BOOLOP_INTERSECT = BOOLOP_BOTH,
58 BOOLOP_SUBTRACT_A_B = BOOLOP_JUST_B,
59 BOOLOP_IDENTITY_A = BOOLOP_JUST_A | BOOLOP_BOTH,
60 BOOLOP_SUBTRACT_B_A = BOOLOP_JUST_A,
61 BOOLOP_IDENTITY_B = BOOLOP_JUST_B | BOOLOP_BOTH,
62 BOOLOP_EXCLUSION = BOOLOP_JUST_A | BOOLOP_JUST_B,
63 BOOLOP_UNION = BOOLOP_JUST_A | BOOLOP_JUST_B | BOOLOP_BOTH
64 };
66 class Shape {
67 Regions content;
68 mutable bool fill;
69 //friend Shape shape_region_boolean(bool rev, Shape const & a, Region const & b);
70 friend CrossingSet crossings_between(Shape const &a, Shape const &b);
71 friend Shape shape_boolean(bool rev, Shape const &, Shape const &, CrossingSet const &);
72 friend Shape boolop(Shape const &a, Shape const &b, unsigned);
73 friend Shape boolop(Shape const &a, Shape const &b, unsigned, CrossingSet const &);
74 friend void add_to_shape(Shape &s, Path const &p, bool);
75 public:
76 Shape() : fill(true) {}
77 explicit Shape(Region const & r) {
78 content = Regions(1, r);
79 fill = r.fill;
80 }
81 explicit Shape(Regions const & r) : content(r) { update_fill(); }
82 explicit Shape(bool f) : fill(f) {}
83 Shape(Regions const & r, bool f) : content(r), fill(f) {}
85 Regions getContent() const { return content; }
86 bool isFill() const { return fill; }
88 unsigned size() const { return content.size(); }
89 const Region &operator[](unsigned ix) const { return content[ix]; }
91 Shape inverse() const;
92 Shape operator*(Matrix const &m) const;
94 bool contains(Point const &p) const;
96 bool inside_invariants() const; //semi-slow & easy to violate : checks that the insides are inside, the outsides are outside
97 bool region_invariants() const; //semi-slow : checks for self crossing
98 bool cross_invariants() const; //slow : checks that everything is disjoint
99 bool invariants() const; //vera slow (combo, checks the above)
101 private:
102 std::vector<unsigned> containment_list(Point p) const;
103 void update_fill() const {
104 unsigned ix = outer_index(content);
105 if(ix < size())
106 fill = content[ix].fill;
107 else if(size() > 0)
108 fill = content.front().fill;
109 else
110 fill = true;
111 }
112 };
114 inline CrossingSet crossings_between(Shape const &a, Shape const &b) { return crossings(paths_from_regions(a.content), paths_from_regions(b.content)); }
116 Shape shape_boolean(bool rev, Shape const &, Shape const &, CrossingSet const &);
117 Shape shape_boolean(bool rev, Shape const &, Shape const &);
119 //unsigned pick_coincident(unsigned ix, unsigned jx, bool &rev, std::vector<Path> const &ps, CrossingSet const &crs);
120 //void outer_crossing(unsigned &ix, unsigned &jx, bool & dir, std::vector<Path> const & ps, CrossingSet const & crs);
121 void crossing_dual(unsigned &i, unsigned &j, CrossingSet const & crs);
122 unsigned crossing_along(double t, unsigned ix, unsigned jx, bool dir, Crossings const & crs);
124 Shape boolop(Shape const &, Shape const &, unsigned flags);
125 Shape boolop(Shape const &, Shape const &, unsigned flags, CrossingSet &);
127 Shape sanitize(std::vector<Path> const &ps);
129 Shape stopgap_cleaner(std::vector<Path> const &ps);
131 inline std::vector<Path> desanitize(Shape const & s) {
132 return paths_from_regions(s.getContent());
133 }
135 }
137 #endif
139 /*
140 Local Variables:
141 mode:c++
142 c-file-style:"stroustrup"
143 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
144 indent-tabs-mode:nil
145 fill-column:99
146 End:
147 */
148 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :