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