| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * \file | ||
| 3 | * \brief Intersection utilities | ||
| 4 | *//* | ||
| 5 | * Authors: | ||
| 6 | * Krzysztof KosiĆski <tweenk.pl@gmail.com> | ||
| 7 | * | ||
| 8 | * Copyright 2015 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, write 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 | |||
| 34 | #ifndef SEEN_LIB2GEOM_INTERSECTION_H | ||
| 35 | #define SEEN_LIB2GEOM_INTERSECTION_H | ||
| 36 | |||
| 37 | #include <2geom/coord.h> | ||
| 38 | #include <2geom/point.h> | ||
| 39 | |||
| 40 | namespace Geom { | ||
| 41 | |||
| 42 | |||
| 43 | /** @brief Intersection between two shapes. | ||
| 44 | */ | ||
| 45 | template <typename TimeA = Coord, typename TimeB = TimeA> | ||
| 46 | class Intersection | ||
| 47 | : boost::totally_ordered< Intersection<TimeA, TimeB> > | ||
| 48 | { | ||
| 49 | public: | ||
| 50 | /** @brief Construct from shape references and time values. | ||
| 51 | * By default, the intersection point will be halfway between the evaluated | ||
| 52 | * points on the two shapes. */ | ||
| 53 | template <typename TA, typename TB> | ||
| 54 | 60243 | Intersection(TA const &sa, TB const &sb, TimeA const &ta, TimeB const &tb) | |
| 55 | 60243 | : first(ta) | |
| 56 | 60243 | , second(tb) | |
| 57 |
3/6✓ Branch 2 taken 40124 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 40124 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 40124 times.
✗ Branch 10 not taken.
|
60243 | , _point(lerp(0.5, sa.pointAt(ta), sb.pointAt(tb))) |
| 58 | 60243 | {} | |
| 59 | |||
| 60 | /// Additionally report the intersection point. | ||
| 61 | 595849 | Intersection(TimeA const &ta, TimeB const &tb, Point const &p) | |
| 62 | 595849 | : first(ta) | |
| 63 | 595849 | , second(tb) | |
| 64 | 595849 | , _point(p) | |
| 65 | 595849 | {} | |
| 66 | |||
| 67 | /// Intersection point, as calculated by the intersection algorithm. | ||
| 68 | 146 | Point point() const { | |
| 69 | 146 | return _point; | |
| 70 | } | ||
| 71 | /// Implicit conversion to Point. | ||
| 72 | operator Point() const { | ||
| 73 | return _point; | ||
| 74 | } | ||
| 75 | |||
| 76 | ✗ | friend inline void swap(Intersection &a, Intersection &b) { | |
| 77 | using std::swap; | ||
| 78 | ✗ | swap(a.first, b.first); | |
| 79 | ✗ | swap(a.second, b.second); | |
| 80 | ✗ | swap(a._point, b._point); | |
| 81 | ✗ | } | |
| 82 | |||
| 83 | 32 | bool operator==(Intersection const &other) const { | |
| 84 |
2/2✓ Branch 1 taken 20 times.
✓ Branch 2 taken 12 times.
|
32 | if (first != other.first) return false; |
| 85 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
|
12 | if (second != other.second) return false; |
| 86 | 12 | return true; | |
| 87 | } | ||
| 88 | 149 | bool operator<(Intersection const &other) const { | |
| 89 |
3/3✓ Branch 0 taken 1 times.
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 94 times.
|
149 | if (first < other.first) return true; |
| 90 |
4/8✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 77 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 17 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 94 times.
|
108 | if (first == other.first && second < other.second) return true; |
| 91 | 108 | return false; | |
| 92 | } | ||
| 93 | |||
| 94 | public: | ||
| 95 | /// First shape and time value. | ||
| 96 | TimeA first; | ||
| 97 | /// Second shape and time value. | ||
| 98 | TimeB second; | ||
| 99 | private: | ||
| 100 | // Recalculation of the intersection point from the time values is in many cases | ||
| 101 | // less precise than the value obtained directly from the intersection algorithm, | ||
| 102 | // so we need to store it. | ||
| 103 | Point _point; | ||
| 104 | }; | ||
| 105 | |||
| 106 | |||
| 107 | // TODO: move into new header? | ||
| 108 | template <typename T> | ||
| 109 | struct ShapeTraits { | ||
| 110 | typedef Coord TimeType; | ||
| 111 | typedef Interval IntervalType; | ||
| 112 | typedef T AffineClosureType; | ||
| 113 | typedef Intersection<> IntersectionType; | ||
| 114 | }; | ||
| 115 | |||
| 116 | template <typename A, typename B> inline | ||
| 117 | std::vector< Intersection<A, B> > transpose(std::vector< Intersection<B, A> > const &in) { | ||
| 118 | std::vector< Intersection<A, B> > result; | ||
| 119 | for (std::size_t i = 0; i < in.size(); ++i) { | ||
| 120 | result.push_back(Intersection<A, B>(in[i].second, in[i].first, in[i].point())); | ||
| 121 | } | ||
| 122 | return result; | ||
| 123 | } | ||
| 124 | |||
| 125 | template <typename T> inline | ||
| 126 | 9 | void transpose_in_place(std::vector< Intersection<T, T> > &xs) { | |
| 127 |
2/2✓ Branch 1 taken 13 times.
✓ Branch 2 taken 9 times.
|
22 | for (std::size_t i = 0; i < xs.size(); ++i) { |
| 128 | 13 | std::swap(xs[i].first, xs[i].second); | |
| 129 | } | ||
| 130 | 9 | } | |
| 131 | |||
| 132 | typedef Intersection<> ShapeIntersection; | ||
| 133 | |||
| 134 | |||
| 135 | } // namespace Geom | ||
| 136 | |||
| 137 | #endif // SEEN_LIB2GEOM_INTERSECTION_H | ||
| 138 | /* | ||
| 139 | Local Variables: | ||
| 140 | mode:c++ | ||
| 141 | c-file-style:"stroustrup" | ||
| 142 | c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) | ||
| 143 | indent-tabs-mode:nil | ||
| 144 | fill-column:99 | ||
| 145 | End: | ||
| 146 | */ | ||
| 147 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : | ||
| 148 |