libcamera v0.5.1
Supporting cameras in Linux since 2019
Loading...
Searching...
No Matches
geometry.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/*
3 * Copyright (C) 2019, Google Inc.
4 *
5 * Geometry-related classes
6 */
7
8#pragma once
9
10#include <algorithm>
11#include <ostream>
12#include <string>
13
14namespace libcamera {
15
16class Rectangle;
17
18class Point
19{
20public:
21 constexpr Point()
22 : x(0), y(0)
23 {
24 }
25
26 constexpr Point(int xpos, int ypos)
27 : x(xpos), y(ypos)
28 {
29 }
30
31 int x;
32 int y;
33
34 const std::string toString() const;
35
36 constexpr Point operator-() const
37 {
38 return { -x, -y };
39 }
40};
41
42bool operator==(const Point &lhs, const Point &rhs);
43static inline bool operator!=(const Point &lhs, const Point &rhs)
44{
45 return !(lhs == rhs);
46}
47
48std::ostream &operator<<(std::ostream &out, const Point &p);
49
50class Size
51{
52public:
53 constexpr Size()
54 : Size(0, 0)
55 {
56 }
57
58 constexpr Size(unsigned int w, unsigned int h)
59 : width(w), height(h)
60 {
61 }
62
63 unsigned int width;
64 unsigned int height;
65
66 bool isNull() const { return !width && !height; }
67 const std::string toString() const;
68
69 Size &alignDownTo(unsigned int hAlignment, unsigned int vAlignment)
70 {
71 width = width / hAlignment * hAlignment;
72 height = height / vAlignment * vAlignment;
73 return *this;
74 }
75
76 Size &alignUpTo(unsigned int hAlignment, unsigned int vAlignment)
77 {
78 width = (width + hAlignment - 1) / hAlignment * hAlignment;
79 height = (height + vAlignment - 1) / vAlignment * vAlignment;
80 return *this;
81 }
82
83 Size &boundTo(const Size &bound)
84 {
85 width = std::min(width, bound.width);
86 height = std::min(height, bound.height);
87 return *this;
88 }
89
90 Size &expandTo(const Size &expand)
91 {
92 width = std::max(width, expand.width);
93 height = std::max(height, expand.height);
94 return *this;
95 }
96
97 Size &growBy(const Size &margins)
98 {
99 width += margins.width;
100 height += margins.height;
101 return *this;
102 }
103
104 Size &shrinkBy(const Size &margins)
105 {
106 width = width > margins.width ? width - margins.width : 0;
107 height = height > margins.height ? height - margins.height : 0;
108 return *this;
109 }
110
111 [[nodiscard]] constexpr Size alignedDownTo(unsigned int hAlignment,
112 unsigned int vAlignment) const
113 {
114 return {
115 width / hAlignment * hAlignment,
116 height / vAlignment * vAlignment
117 };
118 }
119
120 [[nodiscard]] constexpr Size alignedUpTo(unsigned int hAlignment,
121 unsigned int vAlignment) const
122 {
123 return {
124 (width + hAlignment - 1) / hAlignment * hAlignment,
125 (height + vAlignment - 1) / vAlignment * vAlignment
126 };
127 }
128
129 [[nodiscard]] constexpr Size boundedTo(const Size &bound) const
130 {
131 return {
132 std::min(width, bound.width),
133 std::min(height, bound.height)
134 };
135 }
136
137 [[nodiscard]] constexpr Size expandedTo(const Size &expand) const
138 {
139 return {
140 std::max(width, expand.width),
141 std::max(height, expand.height)
142 };
143 }
144
145 [[nodiscard]] constexpr Size grownBy(const Size &margins) const
146 {
147 return {
148 width + margins.width,
149 height + margins.height
150 };
151 }
152
153 [[nodiscard]] constexpr Size shrunkBy(const Size &margins) const
154 {
155 return {
156 width > margins.width ? width - margins.width : 0,
157 height > margins.height ? height - margins.height : 0
158 };
159 }
160
161 [[nodiscard]] Size boundedToAspectRatio(const Size &ratio) const;
162 [[nodiscard]] Size expandedToAspectRatio(const Size &ratio) const;
163
164 [[nodiscard]] Rectangle centeredTo(const Point &center) const;
165
166 Size operator*(float factor) const;
167 Size operator/(float factor) const;
168
169 Size &operator*=(float factor);
170 Size &operator/=(float factor);
171};
172
173bool operator==(const Size &lhs, const Size &rhs);
174bool operator<(const Size &lhs, const Size &rhs);
175
176static inline bool operator!=(const Size &lhs, const Size &rhs)
177{
178 return !(lhs == rhs);
179}
180
181static inline bool operator<=(const Size &lhs, const Size &rhs)
182{
183 return lhs < rhs || lhs == rhs;
184}
185
186static inline bool operator>(const Size &lhs, const Size &rhs)
187{
188 return !(lhs <= rhs);
189}
190
191static inline bool operator>=(const Size &lhs, const Size &rhs)
192{
193 return !(lhs < rhs);
194}
195
196std::ostream &operator<<(std::ostream &out, const Size &s);
197
199{
200public:
202 : hStep(0), vStep(0)
203 {
204 }
205
206 SizeRange(const Size &size)
207 : min(size), max(size), hStep(1), vStep(1)
208 {
209 }
210
211 SizeRange(const Size &minSize, const Size &maxSize)
212 : min(minSize), max(maxSize), hStep(1), vStep(1)
213 {
214 }
215
216 SizeRange(const Size &minSize, const Size &maxSize,
217 unsigned int hstep, unsigned int vstep)
218 : min(minSize), max(maxSize), hStep(hstep), vStep(vstep)
219 {
220 }
221
222 bool contains(const Size &size) const;
223
224 std::string toString() const;
225
228 unsigned int hStep;
229 unsigned int vStep;
230};
231
232bool operator==(const SizeRange &lhs, const SizeRange &rhs);
233static inline bool operator!=(const SizeRange &lhs, const SizeRange &rhs)
234{
235 return !(lhs == rhs);
236}
237
238std::ostream &operator<<(std::ostream &out, const SizeRange &sr);
239
241{
242public:
243 constexpr Rectangle()
244 : Rectangle(0, 0, 0, 0)
245 {
246 }
247
248 constexpr Rectangle(int xpos, int ypos, const Size &size)
249 : x(xpos), y(ypos), width(size.width), height(size.height)
250 {
251 }
252
253 constexpr Rectangle(int xpos, int ypos, unsigned int w, unsigned int h)
254 : x(xpos), y(ypos), width(w), height(h)
255 {
256 }
257
258 constexpr explicit Rectangle(const Size &size)
259 : x(0), y(0), width(size.width), height(size.height)
260 {
261 }
262
263 constexpr Rectangle(const Point &point1, const Point &point2)
264 : Rectangle(std::min(point1.x, point2.x), std::min(point1.y, point2.y),
265 static_cast<unsigned int>(std::max(point1.x, point2.x)) -
266 static_cast<unsigned int>(std::min(point1.x, point2.x)),
267 static_cast<unsigned int>(std::max(point1.y, point2.y)) -
268 static_cast<unsigned int>(std::min(point1.y, point2.y)))
269 {
270 }
271
272 int x;
273 int y;
274 unsigned int width;
275 unsigned int height;
276
277 bool isNull() const { return !width && !height; }
278 const std::string toString() const;
279
280 Point center() const;
281
282 Size size() const
283 {
284 return { width, height };
285 }
286
288 {
289 return { x, y };
290 }
291
292 Rectangle &scaleBy(const Size &numerator, const Size &denominator);
293 Rectangle &translateBy(const Point &point);
294
295 [[nodiscard]] Rectangle boundedTo(const Rectangle &bound) const;
296 [[nodiscard]] Rectangle enclosedIn(const Rectangle &boundary) const;
297 [[nodiscard]] Rectangle scaledBy(const Size &numerator,
298 const Size &denominator) const;
299 [[nodiscard]] Rectangle translatedBy(const Point &point) const;
300
302 const Rectangle &target) const;
303};
304
305bool operator==(const Rectangle &lhs, const Rectangle &rhs);
306static inline bool operator!=(const Rectangle &lhs, const Rectangle &rhs)
307{
308 return !(lhs == rhs);
309}
310
311std::ostream &operator<<(std::ostream &out, const Rectangle &r);
312
313} /* namespace libcamera */
Describe a point in two-dimensional space.
Definition geometry.h:19
int y
The y-coordinate of the Point.
Definition geometry.h:32
constexpr Point operator-() const
Negate a Point by negating both its x and y coordinates.
Definition geometry.h:36
constexpr Point(int xpos, int ypos)
Construct a Point at given xpos and ypos values.
Definition geometry.h:26
int x
The x-coordinate of the Point.
Definition geometry.h:31
constexpr Point()
Construct a Point with x and y set to 0.
Definition geometry.h:21
const std::string toString() const
Assemble and return a string describing the point.
Definition geometry.cpp:56
Describe a rectangle's position and dimensions.
Definition geometry.h:241
int x
The horizontal coordinate of the rectangle's top-left corner.
Definition geometry.h:272
Rectangle & scaleBy(const Size &numerator, const Size &denominator)
Apply a non-uniform rational scaling in place to this Rectangle.
Definition geometry.cpp:722
Point center() const
Retrieve the center point of this rectangle.
Definition geometry.cpp:691
Rectangle translatedBy(const Point &point) const
Translate a Rectangle by the given amounts.
Definition geometry.cpp:835
constexpr Rectangle(int xpos, int ypos, unsigned int w, unsigned int h)
Construct a Rectangle with the given position and size.
Definition geometry.h:253
constexpr Rectangle(const Point &point1, const Point &point2)
Construct a Rectangle from two opposite corners.
Definition geometry.h:263
unsigned int width
The distance between the left and right sides.
Definition geometry.h:274
const std::string toString() const
Assemble and return a string describing the rectangle.
Definition geometry.cpp:679
constexpr Rectangle(const Size &size)
Construct a Rectangle of size with its top left corner located at (0,0)
Definition geometry.h:258
unsigned int height
The distance between the top and bottom sides.
Definition geometry.h:275
constexpr Rectangle()
Construct a Rectangle with all coordinates set to 0.
Definition geometry.h:243
Point topLeft() const
Retrieve the coordinates of the top left corner of this Rectangle.
Definition geometry.h:287
Rectangle enclosedIn(const Rectangle &boundary) const
Enclose a Rectangle so as not to exceed another Rectangle.
Definition geometry.cpp:792
constexpr Rectangle(int xpos, int ypos, const Size &size)
Construct a Rectangle with the given position and size.
Definition geometry.h:248
bool isNull() const
Check if the rectangle is null.
Definition geometry.h:277
Rectangle transformedBetween(const Rectangle &source, const Rectangle &target) const
Transform a Rectangle from one reference rectangle to another.
Definition geometry.cpp:874
Rectangle & translateBy(const Point &point)
Translate this Rectangle in place by the given Point.
Definition geometry.cpp:741
Size size() const
Retrieve the size of this rectangle.
Definition geometry.h:282
int y
The vertical coordinate of the rectangle's top-left corner.
Definition geometry.h:273
Rectangle boundedTo(const Rectangle &bound) const
Calculate the intersection of this Rectangle with another.
Definition geometry.cpp:761
Rectangle scaledBy(const Size &numerator, const Size &denominator) const
Apply a non-uniform rational scaling to this Rectangle.
Definition geometry.cpp:816
Describe a range of sizes.
Definition geometry.h:199
SizeRange(const Size &minSize, const Size &maxSize, unsigned int hstep, unsigned int vstep)
Construct a size range with specified min, max and step.
Definition geometry.h:216
SizeRange(const Size &minSize, const Size &maxSize)
Construct a size range with specified min and max, and steps of 1.
Definition geometry.h:211
Size min
The minimum size.
Definition geometry.h:226
SizeRange(const Size &size)
Construct a size range representing a single size.
Definition geometry.h:206
unsigned int hStep
The horizontal step.
Definition geometry.h:228
SizeRange()
Construct a size range initialized to 0.
Definition geometry.h:201
bool contains(const Size &size) const
Test if a size is contained in the range.
Definition geometry.cpp:539
Size max
The maximum size.
Definition geometry.h:227
std::string toString() const
Assemble and return a string describing the size range.
Definition geometry.cpp:554
unsigned int vStep
The vertical step.
Definition geometry.h:229
Describe a two-dimensional size.
Definition geometry.h:51
unsigned int width
The Size width.
Definition geometry.h:63
Size & operator/=(float factor)
Scale this size down by the given factor in place.
Definition geometry.cpp:372
constexpr Size boundedTo(const Size &bound) const
Bound the size to bound.
Definition geometry.h:129
constexpr Size()
Construct a Size with width and height set to 0.
Definition geometry.h:53
const std::string toString() const
Assemble and return a string describing the size.
Definition geometry.cpp:136
Size & boundTo(const Size &bound)
Bound the size to bound in place.
Definition geometry.h:83
Size operator/(float factor) const
Scale size down by the given factor.
Definition geometry.cpp:350
constexpr Size(unsigned int w, unsigned int h)
Construct a Size with given width and height.
Definition geometry.h:58
Size & alignDownTo(unsigned int hAlignment, unsigned int vAlignment)
Align the size down horizontally and vertically in place.
Definition geometry.h:69
constexpr Size shrunkBy(const Size &margins) const
Shrink the size by margins.
Definition geometry.h:153
Size & alignUpTo(unsigned int hAlignment, unsigned int vAlignment)
Align the size up horizontally and vertically in place.
Definition geometry.h:76
Size & shrinkBy(const Size &margins)
Shrink the size by margins in place.
Definition geometry.h:104
unsigned int height
The Size height.
Definition geometry.h:64
constexpr Size alignedDownTo(unsigned int hAlignment, unsigned int vAlignment) const
Align the size down horizontally and vertically.
Definition geometry.h:111
Rectangle centeredTo(const Point &center) const
Center a rectangle of this size at a given Point.
Definition geometry.cpp:327
bool isNull() const
Check if the size is null.
Definition geometry.h:66
constexpr Size alignedUpTo(unsigned int hAlignment, unsigned int vAlignment) const
Align the size up horizontally and vertically.
Definition geometry.h:120
Size boundedToAspectRatio(const Size &ratio) const
Bound the size down to match the aspect ratio given by ratio.
Definition geometry.cpp:278
Size expandedToAspectRatio(const Size &ratio) const
Expand the size to match the aspect ratio given by ratio.
Definition geometry.cpp:303
constexpr Size expandedTo(const Size &expand) const
Expand the size to expand.
Definition geometry.h:137
Size operator*(float factor) const
Scale size up by the given factor.
Definition geometry.cpp:340
constexpr Size grownBy(const Size &margins) const
Grow the size by margins.
Definition geometry.h:145
Size & expandTo(const Size &expand)
Expand the size to expand.
Definition geometry.h:90
Size & growBy(const Size &margins)
Grow the size by margins in place.
Definition geometry.h:97
Size & operator*=(float factor)
Scale this size up by the given factor in place.
Definition geometry.cpp:360
Top-level libcamera namespace.
Definition bound_method.h:15
std::ostream & operator<<(std::ostream &out, const Point &p)
Insert a text representation of a Point into an output stream.
Definition geometry.cpp:91
bool operator<(const Size &lhs, const Size &rhs)
Compare sizes for smaller than order.
Definition geometry.cpp:399