GCC Code Coverage Report


Directory: ./
File: include/2geom/rect.h
Date: 2024-03-18 17:01:34
Exec Total Coverage
Lines: 7 18 38.9%
Functions: 7 11 63.6%
Branches: 0 20 0.0%

Line Branch Exec Source
1 /**
2 * \file
3 * \brief Axis-aligned rectangle
4 *//*
5 * Authors:
6 * Michael Sloan <mgsloan@gmail.com>
7 * Krzysztof KosiƄski <tweenk.pl@gmail.com>
8 * Copyright 2007-2011 Authors
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it either under the terms of the GNU Lesser General Public
12 * License version 2.1 as published by the Free Software Foundation
13 * (the "LGPL") or, at your option, under the terms of the Mozilla
14 * Public License Version 1.1 (the "MPL"). If you do not alter this
15 * notice, a recipient may use your version of this file under either
16 * the MPL or the LGPL.
17 *
18 * You should have received a copy of the LGPL along with this library
19 * in the file COPYING-LGPL-2.1; if not, output to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * You should have received a copy of the MPL along with this library
22 * in the file COPYING-MPL-1.1
23 *
24 * The contents of this file are subject to the Mozilla Public License
25 * Version 1.1 (the "License"); you may not use this file except in
26 * compliance with the License. You may obtain a copy of the License at
27 * http://www.mozilla.org/MPL/
28 *
29 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
30 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
31 * the specific language governing rights and limitations.
32 *
33 * Authors of original rect class:
34 * Lauris Kaplinski <lauris@kaplinski.com>
35 * Nathan Hurst <njh@mail.csse.monash.edu.au>
36 * bulia byak <buliabyak@users.sf.net>
37 * MenTaLguY <mental@rydia.net>
38 */
39
40 #ifndef LIB2GEOM_SEEN_RECT_H
41 #define LIB2GEOM_SEEN_RECT_H
42
43 #include <2geom/affine.h>
44 #include <2geom/interval.h>
45 #include <2geom/int-rect.h>
46
47 namespace Geom {
48
49 /** Values for the <align> parameter of preserveAspectRatio.
50 * See: http://www.w3.org/TR/SVG/coords.html#PreserveAspectRatioAttribute */
51 enum Align {
52 ALIGN_NONE,
53 ALIGN_XMIN_YMIN,
54 ALIGN_XMID_YMIN,
55 ALIGN_XMAX_YMIN,
56 ALIGN_XMIN_YMID,
57 ALIGN_XMID_YMID,
58 ALIGN_XMAX_YMID,
59 ALIGN_XMIN_YMAX,
60 ALIGN_XMID_YMAX,
61 ALIGN_XMAX_YMAX
62 };
63
64 /** Values for the <meetOrSlice> parameter of preserveAspectRatio.
65 * See: http://www.w3.org/TR/SVG/coords.html#PreserveAspectRatioAttribute */
66 enum Expansion {
67 EXPANSION_MEET,
68 EXPANSION_SLICE
69 };
70
71 /// Convert an align specification to coordinate fractions.
72 Point align_factors(Align align);
73
74 /** @brief Structure that specifies placement of within a viewport.
75 * Use this to create transformations that preserve aspect. */
76 struct Aspect {
77 Align align;
78 Expansion expansion;
79 bool deferred; ///< for SVG compatibility
80
81 Aspect(Align a = ALIGN_NONE, Expansion ex = EXPANSION_MEET)
82 : align(a), expansion(ex), deferred(false)
83 {}
84 };
85
86 /**
87 * @brief Axis aligned, non-empty rectangle.
88 * @ingroup Primitives
89 */
90 class Rect
91 : public GenericRect<Coord>
92 {
93 typedef GenericRect<Coord> Base;
94 public:
95 /// @name Create rectangles.
96 /// @{
97 /** @brief Create a rectangle that contains only the point at (0,0). */
98 100 Rect() {}
99 /** @brief Create a rectangle from X and Y intervals. */
100 32015 Rect(Interval const &a, Interval const &b) : Base(a,b) {}
101 /** @brief Create a rectangle from two points. */
102 118594 Rect(Point const &a, Point const &b) : Base(a,b) {}
103 Rect(Coord x0, Coord y0, Coord x1, Coord y1) : Base(x0, y0, x1, y1) {}
104 32006 Rect(Base const &b) : Base(b) {}
105 Rect(IntRect const &ir) : Base(ir.min(), ir.max()) {}
106 /// @}
107
108 /// @name Inspect dimensions.
109 /// @{
110 /** @brief Check whether the rectangle has zero area up to specified tolerance.
111 * @param eps Maximum value of the area to consider empty
112 * @return True if rectangle has an area smaller than tolerance, false otherwise */
113 bool hasZeroArea(Coord eps = EPSILON) const { return (area() <= eps); }
114 /// Check whether the rectangle has finite area
115 bool isFinite() const { return (*this)[X].isFinite() && (*this)[Y].isFinite(); }
116 /// Calculate the diameter of the smallest circle that would contain the rectangle.
117 Coord diameter() const { return distance(corner(0), corner(2)); }
118 /// @}
119
120 /// @name Test other rectangles and points for inclusion.
121 /// @{
122 /** @brief Check whether the interiors of the rectangles have any common points. */
123 bool interiorIntersects(Rect const &r) const {
124 return f[X].interiorIntersects(r[X]) && f[Y].interiorIntersects(r[Y]);
125 }
126 /** @brief Check whether the interior includes the given point. */
127 bool interiorContains(Point const &p) const {
128 return f[X].interiorContains(p[X]) && f[Y].interiorContains(p[Y]);
129 }
130 /** @brief Check whether the interior includes all points in the given rectangle.
131 * Interior of the rectangle is the entire rectangle without its borders. */
132 bool interiorContains(Rect const &r) const {
133 return f[X].interiorContains(r[X]) && f[Y].interiorContains(r[Y]);
134 }
135 inline bool interiorContains(OptRect const &r) const;
136 /// @}
137
138 /// @name Rounding to integer coordinates
139 /// @{
140 /** @brief Return the smallest integer rectangle which contains this one. */
141 IntRect roundOutwards() const {
142 IntRect ir(f[X].roundOutwards(), f[Y].roundOutwards());
143 return ir;
144 }
145 /** @brief Return the largest integer rectangle which is contained in this one. */
146 OptIntRect roundInwards() const {
147 OptIntRect oir(f[X].roundInwards(), f[Y].roundInwards());
148 return oir;
149 }
150 /// @}
151
152 /// @name SVG viewbox functionality.
153 /// @{
154 /** @brief Transform contents to viewport.
155 * Computes an affine that transforms the contents of this rectangle
156 * to the specified viewport. The aspect parameter specifies how to
157 * to the transformation (whether the aspect ratio of content
158 * should be kept and where it should be placed in the viewport). */
159 Affine transformTo(Rect const &viewport, Aspect const &aspect = Aspect()) const;
160 /// @}
161
162 /// @name Operators
163 /// @{
164 Rect &operator*=(Affine const &m);
165 bool operator==(IntRect const &ir) const {
166 return f[X] == ir[X] && f[Y] == ir[Y];
167 }
168 bool operator==(Rect const &other) const {
169 return Base::operator==(other);
170 }
171 /// @}
172 };
173
174 /**
175 * @brief Axis-aligned rectangle that can be empty.
176 * @ingroup Primitives
177 */
178 class OptRect
179 : public GenericOptRect<Coord>
180 {
181 typedef GenericOptRect<Coord> Base;
182 public:
183 591150 OptRect() : Base() {}
184 107 OptRect(Rect const &a) : Base(a) {}
185 OptRect(Point const &a, Point const &b) : Base(a, b) {}
186 OptRect(Coord x0, Coord y0, Coord x1, Coord y1) : Base(x0, y0, x1, y1) {}
187 32000 OptRect(OptInterval const &x_int, OptInterval const &y_int) : Base(x_int, y_int) {}
188 OptRect(Base const &b) : Base(b) {}
189
190 OptRect(IntRect const &r) : Base(Rect(r)) {}
191 OptRect(OptIntRect const &r) : Base() {
192 if (r) *this = Rect(*r);
193 }
194
195 Affine transformTo(Rect const &viewport, Aspect const &aspect = Aspect()) {
196 Affine ret = Affine::identity();
197 if (empty()) return ret;
198 ret = (*this)->transformTo(viewport, aspect);
199 return ret;
200 }
201
202 bool operator==(OptRect const &other) const {
203 return Base::operator==(other);
204 }
205 bool operator==(Rect const &other) const {
206 return Base::operator==(other);
207 }
208 };
209
210 Coord distanceSq(Point const &p, Rect const &rect);
211 Coord distance(Point const &p, Rect const &rect);
212 /// Minimum square of distance to rectangle, or infinity if empty.
213 Coord distanceSq(Point const &p, OptRect const &rect);
214 /// Minimum distance to rectangle, or infinity if empty.
215 Coord distance(Point const &p, OptRect const &rect);
216
217 inline bool Rect::interiorContains(OptRect const &r) const {
218 return !r || interiorContains(static_cast<Rect const &>(*r));
219 }
220
221 // the functions below do not work when defined generically
222 inline OptRect operator&(Rect const &a, Rect const &b) {
223 OptRect ret(a);
224 ret.intersectWith(b);
225 return ret;
226 }
227 inline OptRect intersect(Rect const &a, Rect const &b) {
228 return a & b;
229 }
230 inline OptRect intersect(OptRect const &a, OptRect const &b) {
231 return a & b;
232 }
233 inline Rect unify(Rect const &a, Rect const &b) {
234 return a | b;
235 }
236 inline OptRect unify(OptRect const &a, OptRect const &b) {
237 return a | b;
238 }
239
240 /** @brief Union a list of rectangles
241 * @deprecated Use OptRect::from_range instead */
242 inline Rect union_list(std::vector<Rect> const &r) {
243 if(r.empty()) return Rect(Interval(0,0), Interval(0,0));
244 Rect ret = r[0];
245 for(unsigned i = 1; i < r.size(); i++)
246 ret.unionWith(r[i]);
247 return ret;
248 }
249
250 } // end namespace Geom
251
252 #endif // LIB2GEOM_SEEN_RECT_H
253
254 /*
255 Local Variables:
256 mode:c++
257 c-file-style:"stroustrup"
258 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
259 indent-tabs-mode:nil
260 fill-column:99
261 End:
262 */
263 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
264