Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
intersection.h
Go to the documentation of this file.
1/*
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
40namespace Geom {
41
42
45template <typename TimeA = Coord, typename TimeB = TimeA>
47 : boost::totally_ordered< Intersection<TimeA, TimeB> >
48{
49public:
53 template <typename TA, typename TB>
54 Intersection(TA const &sa, TB const &sb, TimeA const &ta, TimeB const &tb)
55 : first(ta)
56 , second(tb)
57 , _point(lerp(0.5, sa.pointAt(ta), sb.pointAt(tb)))
58 {}
59
61 Intersection(TimeA const &ta, TimeB const &tb, Point const &p)
62 : first(ta)
63 , second(tb)
64 , _point(p)
65 {}
66
68 Point point() const {
69 return _point;
70 }
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 bool operator==(Intersection const &other) const {
84 if (first != other.first) return false;
85 if (second != other.second) return false;
86 return true;
87 }
88 bool operator<(Intersection const &other) const {
89 if (first < other.first) return true;
90 if (first == other.first && second < other.second) return true;
91 return false;
92 }
93
94public:
96 TimeA first;
98 TimeB second;
99private:
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.
104};
105
106
107// TODO: move into new header?
108template <typename T>
115
116template <typename A, typename B> inline
117std::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
125template <typename T> inline
126void transpose_in_place(std::vector< Intersection<T, T> > &xs) {
127 for (std::size_t i = 0; i < xs.size(); ++i) {
128 std::swap(xs[i].first, xs[i].second);
129 }
130}
131
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 :
Cartesian point / 2D vector and related operations.
Intersection between two shapes.
TimeB second
Second shape and time value.
bool operator==(Intersection const &other) const
bool operator<(Intersection const &other) const
Point point() const
Intersection point, as calculated by the intersection algorithm.
Intersection(TA const &sa, TB const &sb, TimeA const &ta, TimeB const &tb)
Construct from shape references and time values.
TimeA first
First shape and time value.
friend void swap(Intersection &a, Intersection &b)
Intersection(TimeA const &ta, TimeB const &tb, Point const &p)
Additionally report the intersection point.
Range of real numbers that is never empty.
Definition interval.h:59
Two-dimensional point that doubles as a vector.
Definition point.h:66
Integral and real coordinate types and some basic utilities.
Css & result
constexpr Coord lerp(Coord t, Coord a, Coord b)
Numerically stable linear interpolation.
Definition coord.h:97
double Coord
Floating point type used to store coordinates.
Definition coord.h:76
Various utility functions.
Definition affine.h:22
void transpose_in_place(std::vector< Intersection< T, T > > &xs)
std::vector< Intersection< A, B > > transpose(std::vector< Intersection< B, A > > const &in)
Intersection ShapeIntersection
Intersection IntersectionType