Mir
displacement.h
Go to the documentation of this file.
1/*
2 * Copyright © Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License version 2 or 3,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#ifndef MIR_GEOMETRY_DISPLACEMENT_H_
18#define MIR_GEOMETRY_DISPLACEMENT_H_
19
20#include "forward.h"
21#include "dimensions.h"
22#include "point.h"
23#include <ostream>
24
25namespace mir
26{
27namespace geometry
28{
29namespace generic
30{
31template<typename T>
32struct Point;
33
34template<typename T>
35struct Size;
36
37template<typename T>
38struct Displacement
39{
40 using ValueType = T;
41
42 constexpr Displacement() {}
43 constexpr Displacement(Displacement const&) = default;
44 Displacement& operator=(Displacement const&) = default;
45
46 template<typename U>
47 explicit constexpr Displacement(Displacement<U> const& other) noexcept
48 : dx{DeltaX<T>{other.dx}},
49 dy{DeltaY<T>{other.dy}}
50 {
51 }
52
53 template<typename DeltaXType, typename DeltaYType>
54 constexpr Displacement(DeltaXType&& dx, DeltaYType&& dy) : dx{dx}, dy{dy} {}
55
56 template <typename Q = T>
57 constexpr typename std::enable_if<std::is_integral<Q>::value, long long>::type length_squared() const
58 {
59 long long x = dx.as_value(), y = dy.as_value();
60 return x * x + y * y;
61 }
62
63 template <typename Q = T>
64 constexpr typename std::enable_if<!std::is_integral<Q>::value, T>::type length_squared() const
65 {
66 T x = dx.as_value(), y = dy.as_value();
67 return x * x + y * y;
68 }
69
70 DeltaX<T> dx;
71 DeltaY<T> dy;
72};
73
74template<typename T>
75inline constexpr bool operator==(Displacement<T> const& lhs, Displacement<T> const& rhs)
76{
77 return lhs.dx == rhs.dx && lhs.dy == rhs.dy;
78}
79
80template<typename T>
81inline constexpr bool operator!=(Displacement<T> const& lhs, Displacement<T> const& rhs)
82{
83 return lhs.dx != rhs.dx || lhs.dy != rhs.dy;
84}
85
86template<typename T>
87std::ostream& operator<<(std::ostream& out, Displacement<T> const& value)
88{
89 out << '(' << value.dx << ", " << value.dy << ')';
90 return out;
91}
92
93template<typename T>
94inline constexpr Displacement<T> operator+(Displacement<T> const& lhs, Displacement<T> const& rhs)
95{
96 return Displacement<T>{lhs.dx + rhs.dx, lhs.dy + rhs.dy};
97}
98
99template<typename T>
100inline constexpr Displacement<T> operator-(Displacement<T> const& lhs, Displacement<T> const& rhs)
101{
102 return Displacement<T>{lhs.dx - rhs.dx, lhs.dy - rhs.dy};
103}
104
105template<typename T>
106inline constexpr Displacement<T> operator-(Displacement<T> const& rhs)
107{
108 return Displacement<T>{-rhs.dx, -rhs.dy};
109}
110
111template<typename T>
112inline constexpr Point<T> operator+(Point<T> const& lhs, Displacement<T> const& rhs)
113{
114 return Point<T>{lhs.x + rhs.dx, lhs.y + rhs.dy};
115}
116
117template<typename T>
118inline constexpr Point<T> operator+(Displacement<T> const& lhs, Point<T> const& rhs)
119{
120 return Point<T>{rhs.x + lhs.dx, rhs.y + lhs.dy};
121}
122
123template<typename T>
124inline constexpr Point<T> operator-(Point<T> const& lhs, Displacement<T> const& rhs)
125{
126 return Point<T>{lhs.x - rhs.dx, lhs.y - rhs.dy};
127}
128
129template<typename T>
130inline constexpr Displacement<T> operator-(Point<T> const& lhs, Point<T> const& rhs)
131{
132 return Displacement<T>{lhs.x - rhs.x, lhs.y - rhs.y};
133}
134
135template<typename T>
136inline constexpr Point<T>& operator+=(Point<T>& lhs, Displacement<T> const& rhs)
137{
138 return lhs = lhs + rhs;
139}
140
141template<typename T>
142inline constexpr Point<T>& operator-=(Point<T>& lhs, Displacement<T> const& rhs)
143{
144 return lhs = lhs - rhs;
145}
146
147template<typename T>
148inline bool operator<(Displacement<T> const& lhs, Displacement<T> const& rhs)
149{
150 return lhs.length_squared() < rhs.length_squared();
151}
152
153template<typename T, typename Scalar>
154inline constexpr Displacement<T> operator*(Scalar scale, Displacement<T> const& disp)
155{
156 return Displacement<T>{scale*disp.dx, scale*disp.dy};
157}
158
159template<typename T, typename Scalar>
160inline constexpr Displacement<T> operator*(Displacement<T> const& disp, Scalar scale)
161{
162 return scale*disp;
163}
164
165template<typename T>
166inline constexpr Displacement<T> as_displacement(Size<T> const& size)
167{
168 return Displacement<T>{size.width.as_value(), size.height.as_value()};
169}
170
171template<typename T>
172inline constexpr Size<T> as_size(Displacement<T> const& disp)
173{
174 return Size<T>{disp.dx.as_value(), disp.dy.as_value()};
175}
176
177template<typename T>
178inline constexpr Displacement<T> as_displacement(Point<T> const& point)
179{
180 return Displacement<T>{point.x.as_value(), point.y.as_value()};
181}
182
183template<typename T>
184inline constexpr Point<T> as_point(Displacement<T> const& disp)
185{
186 return Point<T>{disp.dx.as_value(), disp.dy.as_value()};
187}
188}
189}
190}
191
192#endif // MIR_GEOMETRY_DISPLACEMENT_H_
Definition: size.h:29
constexpr Displacement< T > operator-(Displacement< T > const &rhs)
Definition: displacement.h:106
constexpr Point< T > operator+(Displacement< T > const &lhs, Point< T > const &rhs)
Definition: displacement.h:118
constexpr Point< T > & operator-=(Point< T > &lhs, Displacement< T > const &rhs)
Definition: displacement.h:142
constexpr Displacement< T > as_displacement(Point< T > const &point)
Definition: displacement.h:178
constexpr bool operator==(Displacement< T > const &lhs, Displacement< T > const &rhs)
Definition: displacement.h:75
bool operator<(Displacement< T > const &lhs, Displacement< T > const &rhs)
Definition: displacement.h:148
constexpr Point< T > operator-(Point< T > const &lhs, Displacement< T > const &rhs)
Definition: displacement.h:124
constexpr Displacement< T > operator-(Point< T > const &lhs, Point< T > const &rhs)
Definition: displacement.h:130
constexpr Displacement< T > operator*(Displacement< T > const &disp, Scalar scale)
Definition: displacement.h:160
constexpr Displacement< T > operator*(Scalar scale, Displacement< T > const &disp)
Definition: displacement.h:154
constexpr Displacement< T > as_displacement(Size< T > const &size)
Definition: displacement.h:166
constexpr Size< T > as_size(Displacement< T > const &disp)
Definition: displacement.h:172
constexpr bool operator!=(Displacement< T > const &lhs, Displacement< T > const &rhs)
Definition: displacement.h:81
constexpr Point< T > operator+(Point< T > const &lhs, Displacement< T > const &rhs)
Definition: displacement.h:112
constexpr Point< T > & operator+=(Point< T > &lhs, Displacement< T > const &rhs)
Definition: displacement.h:136
constexpr Displacement< T > operator-(Displacement< T > const &lhs, Displacement< T > const &rhs)
Definition: displacement.h:100
constexpr Point< T > as_point(Displacement< T > const &disp)
Definition: displacement.h:184
constexpr Displacement< T > operator+(Displacement< T > const &lhs, Displacement< T > const &rhs)
Definition: displacement.h:94
Basic geometry types. Types for dimensions, displacements, etc. and the operations that they support.
Definition: size.h:27
Definition: runner.h:27
constexpr Displacement(Displacement< U > const &other) noexcept
Definition: displacement.h:47
Displacement & operator=(Displacement const &)=default
DeltaX< T > dx
Definition: displacement.h:70
DeltaY< T > dy
Definition: displacement.h:71
constexpr Displacement(Displacement const &)=default
constexpr std::enable_if<!std::is_integral< Q >::value, T >::type length_squared() const
Definition: displacement.h:64
constexpr Displacement()
Definition: displacement.h:42
constexpr std::enable_if< std::is_integral< Q >::value, longlong >::type length_squared() const
Definition: displacement.h:57
constexpr Displacement(DeltaXType &&dx, DeltaYType &&dy)
Definition: displacement.h:54
Definition: size.h:37

Copyright © 2012-2023 Canonical Ltd.
Generated on Tue 2 May 10:01:24 UTC 2023
This documentation is licensed under the GPL version 2 or 3.