Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
coord.h
Go to the documentation of this file.
1/*
4 * Authors:
5 * Nathan Hurst <njh@mail.csse.monash.edu.au>
6 * Krzysztof KosiƄski <tweenk.pl@gmail.com>
7 * Copyright 2006-2015 Authors
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it either under the terms of the GNU Lesser General Public
11 * License version 2.1 as published by the Free Software Foundation
12 * (the "LGPL") or, at your option, under the terms of the Mozilla
13 * Public License Version 1.1 (the "MPL"). If you do not alter this
14 * notice, a recipient may use your version of this file under either
15 * the MPL or the LGPL.
16 *
17 * You should have received a copy of the LGPL along with this library
18 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * You should have received a copy of the MPL along with this library
21 * in the file COPYING-MPL-1.1
22 *
23 * The contents of this file are subject to the Mozilla Public License
24 * Version 1.1 (the "License"); you may not use this file except in
25 * compliance with the License. You may obtain a copy of the License at
26 * http://www.mozilla.org/MPL/
27 *
28 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
29 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
30 * the specific language governing rights and limitations.
31 *
32 */
33
34#ifndef LIB2GEOM_SEEN_COORD_H
35#define LIB2GEOM_SEEN_COORD_H
36
37#include <cmath>
38#include <limits>
39#include <string>
40#include <functional>
41#include <boost/operators.hpp>
42#include <2geom/forward.h>
43
44namespace Geom {
45
48enum Dim2 { X = 0, Y = 1 };
49
52constexpr Dim2 other_dimension(Dim2 d) { return static_cast<Dim2>(static_cast<int>(d) ^ 1); }
53
54// TODO: make a smarter implementation with C++11
55template <typename T>
56struct D2Traits {
57 using D1Value = typename T::D1Value;
58 using D1Reference = typename T::D1Reference;
59 using D1ConstReference = typename T::D1ConstReference;
60};
61
65template <Dim2 D, typename T>
66struct GetAxis {
68 using argument_type = T;
69 typename D2Traits<T>::D1Value operator()(T const &a) const {
70 return a[D];
71 }
72};
73
76using Coord = double;
77
80using IntCoord = int;
81
84constexpr Coord EPSILON = 1e-6;
85
88constexpr Coord infinity() { return std::numeric_limits<Coord>::infinity(); }
89
92inline bool are_near(Coord a, Coord b, double eps = EPSILON) { return std::abs(a - b) <= eps; }
93inline bool rel_error_bound(Coord a, Coord b, double eps = EPSILON) { return std::abs(a) <= eps * b; }
94
97constexpr Coord lerp(Coord t, Coord a, Coord b) {
98 return (1 - t) * a + t * b;
99}
100
104template <typename C>
111
113 boost::equality_comparable< IntervalType
114 , boost::orable< IntervalType
115 >>;
116
117 using RectOps =
118 boost::equality_comparable< RectType
119 , boost::orable< RectType
120 , boost::orable< RectType, OptRectType
121 >>>;
122};
123
124// NOTE: operator helpers for Rect and Interval are defined here.
125// This is to avoid increasing their size through multiple inheritance.
126
127template<>
134
136 boost::equality_comparable< IntInterval
137 , boost::additive< IntInterval
138 , boost::additive< IntInterval, IntCoord
139 , boost::orable< IntInterval
140 >>>>;
141
142 using RectOps =
143 boost::equality_comparable< IntRect
144 , boost::orable< IntRect
145 , boost::orable< IntRect, OptIntRect
146 , boost::additive< IntRect, IntPoint
147 >>>>;
148};
149
150template<>
155 using RectType = Rect;
157
159 boost::equality_comparable< Interval
160 , boost::equality_comparable< Interval, IntInterval
161 , boost::additive< Interval
162 , boost::multipliable< Interval
163 , boost::orable< Interval
164 , boost::arithmetic< Interval, Coord
165 >>>>>>;
166
167 using RectOps =
168 boost::equality_comparable< Rect
169 , boost::equality_comparable< Rect, IntRect
170 , boost::orable< Rect
171 , boost::orable< Rect, OptRect
172 , boost::additive< Rect, Point
173 , boost::multipliable< Rect, Affine
174 >>>>>>;
175};
176
180std::string format_coord_shortest(Coord x);
181
187std::string format_coord_nice(Coord x);
188
193Coord parse_coord(std::string const &s);
194
195} // namespace Geom
196
197#endif // LIB2GEOM_SEEN_COORD_H
198
199/*
200 Local Variables:
201 mode:c++
202 c-file-style:"stroustrup"
203 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
204 indent-tabs-mode:nil
205 fill-column:99
206 End:
207*/
208// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
3x3 matrix representing an affine transformation.
Definition affine.h:70
Adaptor that creates 2D functions from 1D ones.
Definition d2.h:55
A range of numbers which is never empty.
A range of numbers that can be empty.
Axis-aligned generic rectangle that can be empty.
Axis aligned, non-empty, generic rectangle.
Two-dimensional point with integer coordinates.
Definition int-point.h:57
Range of real numbers that is never empty.
Definition interval.h:59
Range of real numbers that can be empty.
Definition interval.h:199
Axis-aligned rectangle that can be empty.
Definition rect.h:203
Two-dimensional point that doubles as a vector.
Definition point.h:66
Axis aligned, non-empty rectangle.
Definition rect.h:92
Contains forward declarations of 2geom types.
constexpr Coord lerp(Coord t, Coord a, Coord b)
Numerically stable linear interpolation.
Definition coord.h:97
Dim2
2D axis enumeration (X or Y).
Definition coord.h:48
int IntCoord
Type used for integral coordinates.
Definition coord.h:80
constexpr Dim2 other_dimension(Dim2 d)
Get the other (perpendicular) dimension.
Definition coord.h:52
constexpr Coord infinity()
Get a value representing infinity.
Definition coord.h:88
double Coord
Floating point type used to store coordinates.
Definition coord.h:76
GenericInterval< IntCoord > IntInterval
Range of integers that is never empty.
Definition forward.h:51
constexpr Coord EPSILON
Default "acceptably small" value.
Definition coord.h:84
GenericOptInterval< IntCoord > OptIntInterval
Range of integers that can be empty.
Definition forward.h:52
@ Y
Definition coord.h:48
@ X
Definition coord.h:48
Various utility functions.
Definition affine.h:22
GenericRect< IntCoord > IntRect
Definition forward.h:57
Coord parse_coord(std::string const &s)
Definition coord.cpp:101
bool rel_error_bound(Coord a, Coord b, double eps=EPSILON)
Definition coord.h:93
GenericOptRect< IntCoord > OptIntRect
Definition forward.h:58
std::string format_coord_nice(Coord x)
Definition coord.cpp:89
std::string format_coord_shortest(Coord x)
Definition coord.cpp:77
bool are_near(Affine const &a1, Affine const &a2, Coord eps=EPSILON)
boost::equality_comparable< Interval, boost::equality_comparable< Interval, IntInterval, boost::additive< Interval, boost::multipliable< Interval, boost::orable< Interval, boost::arithmetic< Interval, Coord > > > > > > IntervalOps
Definition coord.h:165
boost::equality_comparable< Rect, boost::equality_comparable< Rect, IntRect, boost::orable< Rect, boost::orable< Rect, OptRect, boost::additive< Rect, Point, boost::multipliable< Rect, Affine > > > > > > RectOps
Definition coord.h:174
boost::equality_comparable< IntRect, boost::orable< IntRect, boost::orable< IntRect, OptIntRect, boost::additive< IntRect, IntPoint > > > > RectOps
Definition coord.h:147
boost::equality_comparable< IntInterval, boost::additive< IntInterval, boost::additive< IntInterval, IntCoord, boost::orable< IntInterval > > > > IntervalOps
Definition coord.h:140
Traits class used with coordinate types.
Definition coord.h:105
boost::equality_comparable< IntervalType, boost::orable< IntervalType > > IntervalOps
Definition coord.h:115
GenericInterval< C > IntervalType
Definition coord.h:107
boost::equality_comparable< RectType, boost::orable< RectType, boost::orable< RectType, OptRectType > > > RectOps
Definition coord.h:121
GenericRect< C > RectType
Definition coord.h:109
typename T::D1ConstReference D1ConstReference
Definition coord.h:59
typename T::D1Reference D1Reference
Definition coord.h:58
typename T::D1Value D1Value
Definition coord.h:57
Axis extraction functor.
Definition coord.h:66
T argument_type
Definition coord.h:68
typename D2Traits< T >::D1Value result_type
Definition coord.h:67
D2Traits< T >::D1Value operator()(T const &a) const
Definition coord.h:69