Mir
displacement.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2021 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 
25 namespace mir
26 {
27 namespace geometry
28 {
29 namespace generic
30 {
31 template<typename T>
32 struct Point;
33 
34 template<typename T>
35 struct Size;
36 
37 template<typename T>
38 struct 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 
72 };
73 
74 template<typename T>
75 inline constexpr bool operator==(Displacement<T> const& lhs, Displacement<T> const& rhs)
76 {
77  return lhs.dx == rhs.dx && lhs.dy == rhs.dy;
78 }
79 
80 template<typename T>
81 inline constexpr bool operator!=(Displacement<T> const& lhs, Displacement<T> const& rhs)
82 {
83  return lhs.dx != rhs.dx || lhs.dy != rhs.dy;
84 }
85 
86 template<typename T>
87 std::ostream& operator<<(std::ostream& out, Displacement<T> const& value)
88 {
89  out << '(' << value.dx << ", " << value.dy << ')';
90  return out;
91 }
92 
93 template<typename T>
94 inline 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 
99 template<typename T>
100 inline 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 
105 template<typename T>
106 inline constexpr Displacement<T> operator-(Displacement<T> const& rhs)
107 {
108  return Displacement<T>{-rhs.dx, -rhs.dy};
109 }
110 
111 template<typename T>
112 inline 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 
117 template<typename T>
118 inline 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 
123 template<typename T>
124 inline 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 
129 template<typename T>
130 inline 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 
135 template<typename T>
136 inline constexpr Point<T>& operator+=(Point<T>& lhs, Displacement<T> const& rhs)
137 {
138  return lhs = lhs + rhs;
139 }
140 
141 template<typename T>
142 inline constexpr Point<T>& operator-=(Point<T>& lhs, Displacement<T> const& rhs)
143 {
144  return lhs = lhs - rhs;
145 }
146 
147 template<typename T>
148 inline bool operator<(Displacement<T> const& lhs, Displacement<T> const& rhs)
149 {
150  return lhs.length_squared() < rhs.length_squared();
151 }
152 
153 template<typename T, typename Scalar>
154 inline constexpr Displacement<T> operator*(Scalar scale, Displacement<T> const& disp)
155 {
156  return Displacement<T>{scale*disp.dx, scale*disp.dy};
157 }
158 
159 template<typename T, typename Scalar>
160 inline constexpr Displacement<T> operator*(Displacement<T> const& disp, Scalar scale)
161 {
162  return scale*disp;
163 }
164 
165 template<typename T>
166 inline constexpr Displacement<T> as_displacement(Size<T> const& size)
167 {
168  return Displacement<T>{size.width.as_value(), size.height.as_value()};
169 }
170 
171 template<typename T>
172 inline constexpr Size<T> as_size(Displacement<T> const& disp)
173 {
174  return Size<T>{disp.dx.as_value(), disp.dy.as_value()};
175 }
176 
177 template<typename T>
178 inline constexpr Displacement<T> as_displacement(Point<T> const& point)
179 {
180  return Displacement<T>{point.x.as_value(), point.y.as_value()};
181 }
182 
183 template<typename T>
184 inline 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_

Copyright © 2012-2022 Canonical Ltd.
Generated on Thu Sep 8 12:37:23 UTC 2022
This documentation is licensed under the GPL version 2 or 3.