| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** @file | ||
| 2 | * @brief Integral and real coordinate types and some basic utilities | ||
| 3 | *//* | ||
| 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 | |||
| 44 | namespace Geom { | ||
| 45 | |||
| 46 | /** @brief 2D axis enumeration (X or Y). | ||
| 47 | * @ingroup Primitives */ | ||
| 48 | enum Dim2 { X = 0, Y = 1 }; | ||
| 49 | |||
| 50 | /** @brief Get the other (perpendicular) dimension. | ||
| 51 | * @ingroup Primitives */ | ||
| 52 | 15 | constexpr Dim2 other_dimension(Dim2 d) { return static_cast<Dim2>(static_cast<int>(d) ^ 1); } | |
| 53 | |||
| 54 | // TODO: make a smarter implementation with C++11 | ||
| 55 | template <typename T> | ||
| 56 | struct D2Traits { | ||
| 57 | using D1Value = typename T::D1Value; | ||
| 58 | using D1Reference = typename T::D1Reference; | ||
| 59 | using D1ConstReference = typename T::D1ConstReference; | ||
| 60 | }; | ||
| 61 | |||
| 62 | /** @brief Axis extraction functor. | ||
| 63 | * For use with things such as Boost's transform_iterator. | ||
| 64 | * @ingroup Utilities */ | ||
| 65 | template <Dim2 D, typename T> | ||
| 66 | struct GetAxis { | ||
| 67 | using result_type = typename D2Traits<T>::D1Value; | ||
| 68 | using argument_type = T; | ||
| 69 | ✗ | typename D2Traits<T>::D1Value operator()(T const &a) const { | |
| 70 | ✗ | return a[D]; | |
| 71 | } | ||
| 72 | }; | ||
| 73 | |||
| 74 | /** @brief Floating point type used to store coordinates. | ||
| 75 | * @ingroup Primitives */ | ||
| 76 | using Coord = double; | ||
| 77 | |||
| 78 | /** @brief Type used for integral coordinates. | ||
| 79 | * @ingroup Primitives */ | ||
| 80 | using IntCoord = int; | ||
| 81 | |||
| 82 | /** @brief Default "acceptably small" value. | ||
| 83 | * @ingroup Primitives */ | ||
| 84 | constexpr Coord EPSILON = 1e-6; | ||
| 85 | |||
| 86 | /** @brief Get a value representing infinity. | ||
| 87 | * @ingroup Primitives */ | ||
| 88 | 13 | constexpr Coord infinity() { return std::numeric_limits<Coord>::infinity(); } | |
| 89 | |||
| 90 | /** @brief Nearness predicate for values. | ||
| 91 | * @ingroup Primitives */ | ||
| 92 | 6185726 | inline bool are_near(Coord a, Coord b, double eps = EPSILON) { return std::abs(a - b) <= eps; } | |
| 93 | 1243 | inline bool rel_error_bound(Coord a, Coord b, double eps = EPSILON) { return std::abs(a) <= eps * b; } | |
| 94 | |||
| 95 | /** @brief Numerically stable linear interpolation. | ||
| 96 | * @ingroup Primitives */ | ||
| 97 | 1094081 | constexpr Coord lerp(Coord t, Coord a, Coord b) { | |
| 98 | 1094081 | return (1 - t) * a + t * b; | |
| 99 | } | ||
| 100 | |||
| 101 | /** @brief Traits class used with coordinate types. | ||
| 102 | * Defines point, interval and rectangle types for the given coordinate type. | ||
| 103 | * @ingroup Utilities */ | ||
| 104 | template <typename C> | ||
| 105 | struct CoordTraits { | ||
| 106 | using PointType = D2<C>; | ||
| 107 | using IntervalType = GenericInterval<C>; | ||
| 108 | using OptIntervalType = GenericOptInterval<C>; | ||
| 109 | using RectType = GenericRect<C>; | ||
| 110 | using OptRectType = GenericOptRect<C>; | ||
| 111 | |||
| 112 | using IntervalOps = | ||
| 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 | |||
| 127 | template<> | ||
| 128 | struct CoordTraits<IntCoord> { | ||
| 129 | using PointType = IntPoint; | ||
| 130 | using IntervalType = IntInterval; | ||
| 131 | using OptIntervalType = OptIntInterval; | ||
| 132 | using RectType = IntRect; | ||
| 133 | using OptRectType = OptIntRect; | ||
| 134 | |||
| 135 | using IntervalOps = | ||
| 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 | |||
| 150 | template<> | ||
| 151 | struct CoordTraits<Coord> { | ||
| 152 | using PointType = Point; | ||
| 153 | using IntervalType = Interval; | ||
| 154 | using OptIntervalType = OptInterval; | ||
| 155 | using RectType = Rect; | ||
| 156 | using OptRectType = OptRect; | ||
| 157 | |||
| 158 | using IntervalOps = | ||
| 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 | |||
| 177 | /** @brief Convert coordinate to shortest possible string. | ||
| 178 | * @return The shortest string that parses back to the original value. | ||
| 179 | * @relates Coord */ | ||
| 180 | std::string format_coord_shortest(Coord x); | ||
| 181 | |||
| 182 | /** @brief Convert coordinate to human-readable string. | ||
| 183 | * Unlike format_coord_shortest, this function will not omit a leading zero | ||
| 184 | * before a decimal point or use small negative exponents. The output format | ||
| 185 | * is similar to Javascript functions. | ||
| 186 | * @relates Coord */ | ||
| 187 | std::string format_coord_nice(Coord x); | ||
| 188 | |||
| 189 | /** @brief Parse coordinate string. | ||
| 190 | * When using this function in conjunction with format_coord_shortest() | ||
| 191 | * or format_coord_nice(), the value is guaranteed to be preserved exactly. | ||
| 192 | * @relates Coord */ | ||
| 193 | Coord 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 : | ||
| 209 |