libcamera: include/libcamera/geometry.h Source File

libcamera v0.7.2 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
18 class 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 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
50 class 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 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 Size &transpose()
112 {
113 std::swap(width, height);
114 return *this;
115 }
116
117 [[nodiscard]] constexpr Size alignedDownTo(unsigned int hAlignment,
118 unsigned int vAlignment) const
119 {
120 return {
121 width / hAlignment * hAlignment,
122 height / vAlignment * vAlignment
123 };
124 }
125
126 [[nodiscard]] constexpr Size alignedUpTo(unsigned int hAlignment,
127 unsigned int vAlignment) const
128 {
129 return {
130 (width + hAlignment - 1) / hAlignment * hAlignment,
131 (height + vAlignment - 1) / vAlignment * vAlignment
132 };
133 }
134
135 [[nodiscard]] constexpr Size boundedTo(const Size &bound) const
136 {
137 return {
138 std::min(width, bound.width),
139 std::min(height, bound.height)
140 };
141 }
142
143 [[nodiscard]] constexpr Size expandedTo(const Size &expand) const
144 {
145 return {
146 std::max(width, expand.width),
147 std::max(height, expand.height)
148 };
149 }
150
151 [[nodiscard]] constexpr Size grownBy(const Size &margins) const
152 {
153 return {
154 width + margins.width,
155 height + margins.height
156 };
157 }
158
159 [[nodiscard]] constexpr Size shrunkBy(const Size &margins) const
160 {
161 return {
162 width > margins.width ? width - margins.width : 0,
163 height > margins.height ? height - margins.height : 0
164 };
165 }
166
167 [[nodiscard]] Size boundedToAspectRatio(const Size &ratio) const;
168 [[nodiscard]] Size expandedToAspectRatio(const Size &ratio) const;
169
170 [[nodiscard]] Rectangle centeredTo(const Point &center) const;
171
172 Size operator*(float factor) const;
173 Size operator/(float factor) const;
174
175 Size &operator*=(float factor);
176 Size &operator/=(float factor);
177};
178
179bool operator==(const Size &lhs, const Size &rhs);
180bool operator<(const Size &lhs, const Size &rhs);
181
182static inline bool operator!=(const Size &lhs, const Size &rhs)
183{
184 return !(lhs == rhs);
185}
186
187static inline bool operator<=(const Size &lhs, const Size &rhs)
188{
189 return lhs < rhs || lhs == rhs;
190}
191
192static inline bool operator>(const Size &lhs, const Size &rhs)
193{
194 return !(lhs <= rhs);
195}
196
197static inline bool operator>=(const Size &lhs, const Size &rhs)
198{
199 return !(lhs < rhs);
200}
201
202std::ostream &operator<<(std::ostream &out, const Size &s);
203
204 class SizeRange
205{
206public:
207 SizeRange()
208 : hStep(0), vStep(0)
209 {
210 }
211
212 SizeRange(const Size &size)
213 : min(size), max(size), hStep(1), vStep(1)
214 {
215 }
216
217 SizeRange(const Size &minSize, const Size &maxSize)
218 : min(minSize), max(maxSize), hStep(1), vStep(1)
219 {
220 }
221
222 SizeRange(const Size &minSize, const Size &maxSize,
223 unsigned int hstep, unsigned int vstep)
224 : min(minSize), max(maxSize), hStep(hstep), vStep(vstep)
225 {
226 }
227
228 bool contains(const Size &size) const;
229
230 std::string toString() const;
231
232 Size min;
233 Size max;
234 unsigned int hStep;
235 unsigned int vStep;
236};
237
238bool operator==(const SizeRange &lhs, const SizeRange &rhs);
239static inline bool operator!=(const SizeRange &lhs, const SizeRange &rhs)
240{
241 return !(lhs == rhs);
242}
243
244std::ostream &operator<<(std::ostream &out, const SizeRange &sr);
245
246 class Rectangle
247{
248public:
249 constexpr Rectangle()
250 : Rectangle(0, 0, 0, 0)
251 {
252 }
253
254 constexpr Rectangle(int xpos, int ypos, const Size &size)
255 : x(xpos), y(ypos), width(size.width), height(size.height)
256 {
257 }
258
259 constexpr Rectangle(int xpos, int ypos, unsigned int w, unsigned int h)
260 : x(xpos), y(ypos), width(w), height(h)
261 {
262 }
263
264 constexpr explicit Rectangle(const Size &size)
265 : x(0), y(0), width(size.width), height(size.height)
266 {
267 }
268
269 constexpr Rectangle(const Point &point1, const Point &point2)
270 : Rectangle(std::min(point1.x, point2.x), std::min(point1.y, point2.y),
271 static_cast<unsigned int>(std::max(point1.x, point2.x)) -
272 static_cast<unsigned int>(std::min(point1.x, point2.x)),
273 static_cast<unsigned int>(std::max(point1.y, point2.y)) -
274 static_cast<unsigned int>(std::min(point1.y, point2.y)))
275 {
276 }
277
278 int x;
279 int y;
280 unsigned int width;
281 unsigned int height;
282
283 bool isNull() const { return !width && !height; }
284 std::string toString() const;
285
286 Point center() const;
287
288 Size size() const
289 {
290 return { width, height };
291 }
292
293 Point topLeft() const
294 {
295 return { x, y };
296 }
297
298 Rectangle &scaleBy(const Size &numerator, const Size &denominator);
299 Rectangle &translateBy(const Point &point);
300
301 [[nodiscard]] Rectangle boundedTo(const Rectangle &bound) const;
302 [[nodiscard]] Rectangle enclosedIn(const Rectangle &boundary) const;
303 [[nodiscard]] Rectangle scaledBy(const Size &numerator,
304 const Size &denominator) const;
305 [[nodiscard]] Rectangle translatedBy(const Point &point) const;
306
307 Rectangle transformedBetween(const Rectangle &source,
308 const Rectangle &target) const;
309};
310
311bool operator==(const Rectangle &lhs, const Rectangle &rhs);
312static inline bool operator!=(const Rectangle &lhs, const Rectangle &rhs)
313{
314 return !(lhs == rhs);
315}
316
317std::ostream &operator<<(std::ostream &out, const Rectangle &r);
318
319} /* namespace libcamera */
libcamera::Point
Describe a point in two-dimensional space.
Definition geometry.h:19
libcamera::Point::y
int y
The y-coordinate of the Point.
Definition geometry.h:32
libcamera::Point::operator-
constexpr Point operator-() const
Negate a Point by negating both its x and y coordinates.
Definition geometry.h:36
libcamera::Point::Point
constexpr Point(int xpos, int ypos)
Construct a Point at given xpos and ypos values.
Definition geometry.h:26
libcamera::Point::x
int x
The x-coordinate of the Point.
Definition geometry.h:31
libcamera::Point::toString
std::string toString() const
Assemble and return a string describing the point.
Definition geometry.cpp:58
libcamera::Point::Point
constexpr Point()
Construct a Point with x and y set to 0.
Definition geometry.h:21
libcamera::Rectangle
Describe a rectangle's position and dimensions.
Definition geometry.h:247
libcamera::Rectangle::x
int x
The horizontal coordinate of the rectangle's top-left corner.
Definition geometry.h:278
libcamera::Rectangle::scaleBy
Rectangle & scaleBy(const Size &numerator, const Size &denominator)
Apply a non-uniform rational scaling in place to this Rectangle.
Definition geometry.cpp:734
libcamera::Rectangle::center
Point center() const
Retrieve the center point of this rectangle.
Definition geometry.cpp:703
libcamera::Rectangle::translatedBy
Rectangle translatedBy(const Point &point) const
Translate a Rectangle by the given amounts.
Definition geometry.cpp:847
libcamera::Rectangle::Rectangle
constexpr Rectangle(int xpos, int ypos, unsigned int w, unsigned int h)
Construct a Rectangle with the given position and size.
Definition geometry.h:259
libcamera::Rectangle::toString
std::string toString() const
Assemble and return a string describing the rectangle.
Definition geometry.cpp:691
libcamera::Rectangle::Rectangle
constexpr Rectangle(const Point &point1, const Point &point2)
Construct a Rectangle from two opposite corners.
Definition geometry.h:269
libcamera::Rectangle::width
unsigned int width
The distance between the left and right sides.
Definition geometry.h:280
libcamera::Rectangle::Rectangle
constexpr Rectangle(const Size &size)
Construct a Rectangle of size with its top left corner located at (0,0).
Definition geometry.h:264
libcamera::Rectangle::height
unsigned int height
The distance between the top and bottom sides.
Definition geometry.h:281
libcamera::Rectangle::Rectangle
constexpr Rectangle()
Construct a Rectangle with all coordinates set to 0.
Definition geometry.h:249
libcamera::Rectangle::topLeft
Point topLeft() const
Retrieve the coordinates of the top left corner of this Rectangle.
Definition geometry.h:293
libcamera::Rectangle::enclosedIn
Rectangle enclosedIn(const Rectangle &boundary) const
Enclose a Rectangle so as not to exceed another Rectangle.
Definition geometry.cpp:804
libcamera::Rectangle::Rectangle
constexpr Rectangle(int xpos, int ypos, const Size &size)
Construct a Rectangle with the given position and size.
Definition geometry.h:254
libcamera::Rectangle::isNull
bool isNull() const
Check if the rectangle is null.
Definition geometry.h:283
libcamera::Rectangle::transformedBetween
Rectangle transformedBetween(const Rectangle &source, const Rectangle &target) const
Transform a Rectangle from one reference rectangle to another.
Definition geometry.cpp:886
libcamera::Rectangle::translateBy
Rectangle & translateBy(const Point &point)
Translate this Rectangle in place by the given Point.
Definition geometry.cpp:753
libcamera::Rectangle::size
Size size() const
Retrieve the size of this rectangle.
Definition geometry.h:288
libcamera::Rectangle::y
int y
The vertical coordinate of the rectangle's top-left corner.
Definition geometry.h:279
libcamera::Rectangle::boundedTo
Rectangle boundedTo(const Rectangle &bound) const
Calculate the intersection of this Rectangle with another.
Definition geometry.cpp:773
libcamera::Rectangle::scaledBy
Rectangle scaledBy(const Size &numerator, const Size &denominator) const
Apply a non-uniform rational scaling to this Rectangle.
Definition geometry.cpp:828
libcamera::SizeRange
Describe a range of sizes.
Definition geometry.h:205
libcamera::SizeRange::SizeRange
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:222
libcamera::SizeRange::SizeRange
SizeRange(const Size &minSize, const Size &maxSize)
Construct a size range with specified min and max, and steps of 1.
Definition geometry.h:217
libcamera::SizeRange::min
Size min
The minimum size.
Definition geometry.h:232
libcamera::SizeRange::SizeRange
SizeRange(const Size &size)
Construct a size range representing a single size.
Definition geometry.h:212
libcamera::SizeRange::hStep
unsigned int hStep
The horizontal step.
Definition geometry.h:234
libcamera::SizeRange::SizeRange
SizeRange()
Construct a size range initialized to 0.
Definition geometry.h:207
libcamera::SizeRange::contains
bool contains(const Size &size) const
Test if a size is contained in the range.
Definition geometry.cpp:551
libcamera::SizeRange::max
Size max
The maximum size.
Definition geometry.h:233
libcamera::SizeRange::toString
std::string toString() const
Assemble and return a string describing the size range.
Definition geometry.cpp:566
libcamera::SizeRange::vStep
unsigned int vStep
The vertical step.
Definition geometry.h:235
libcamera::Size
Describe a two-dimensional size.
Definition geometry.h:51
libcamera::Size::width
unsigned int width
The Size width.
Definition geometry.h:63
libcamera::Size::operator/=
Size & operator/=(float factor)
Scale this size down by the given factor in place.
Definition geometry.cpp:384
libcamera::Size::boundedTo
constexpr Size boundedTo(const Size &bound) const
Bound the size to bound.
Definition geometry.h:135
libcamera::Size::Size
constexpr Size()
Construct a Size with width and height set to 0.
Definition geometry.h:53
libcamera::Size::boundTo
Size & boundTo(const Size &bound)
Bound the size to bound in place.
Definition geometry.h:83
libcamera::Size::Size
constexpr Size(unsigned int w, unsigned int h)
Construct a Size with given width and height.
Definition geometry.h:58
libcamera::Size::alignDownTo
Size & alignDownTo(unsigned int hAlignment, unsigned int vAlignment)
Align the size down horizontally and vertically in place.
Definition geometry.h:69
libcamera::Size::shrunkBy
constexpr Size shrunkBy(const Size &margins) const
Shrink the size by margins.
Definition geometry.h:159
libcamera::Size::alignUpTo
Size & alignUpTo(unsigned int hAlignment, unsigned int vAlignment)
Align the size up horizontally and vertically in place.
Definition geometry.h:76
libcamera::Size::shrinkBy
Size & shrinkBy(const Size &margins)
Shrink the size by margins in place.
Definition geometry.h:104
libcamera::Size::height
unsigned int height
The Size height.
Definition geometry.h:64
libcamera::Size::alignedDownTo
constexpr Size alignedDownTo(unsigned int hAlignment, unsigned int vAlignment) const
Align the size down horizontally and vertically.
Definition geometry.h:117
libcamera::Size::centeredTo
Rectangle centeredTo(const Point &center) const
Center a rectangle of this size at a given Point.
Definition geometry.cpp:339
libcamera::Size::toString
std::string toString() const
Assemble and return a string describing the size.
Definition geometry.cpp:138
libcamera::Size::isNull
bool isNull() const
Check if the size is null.
Definition geometry.h:66
libcamera::Size::alignedUpTo
constexpr Size alignedUpTo(unsigned int hAlignment, unsigned int vAlignment) const
Align the size up horizontally and vertically.
Definition geometry.h:126
libcamera::Size::boundedToAspectRatio
Size boundedToAspectRatio(const Size &ratio) const
Bound the size down to match the aspect ratio given by ratio.
Definition geometry.cpp:290
libcamera::Size::expandedToAspectRatio
Size expandedToAspectRatio(const Size &ratio) const
Expand the size to match the aspect ratio given by ratio.
Definition geometry.cpp:315
libcamera::Size::transpose
Size & transpose()
Transpose the size in place.
Definition geometry.h:111
libcamera::Size::expandedTo
constexpr Size expandedTo(const Size &expand) const
Expand the size to expand.
Definition geometry.h:143
libcamera::Size::grownBy
constexpr Size grownBy(const Size &margins) const
Grow the size by margins.
Definition geometry.h:151
libcamera::Size::expandTo
Size & expandTo(const Size &expand)
Expand the size to expand.
Definition geometry.h:90
libcamera::Size::growBy
Size & growBy(const Size &margins)
Grow the size by margins in place.
Definition geometry.h:97
libcamera::Size::operator*=
Size & operator*=(float factor)
Scale this size up by the given factor in place.
Definition geometry.cpp:372
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
libcamera::operator<<
std::ostream & operator<<(std::ostream &out, const Point &p)
Insert a text representation of a Point into an output stream.
Definition geometry.cpp:93
libcamera::operator*
Matrix< U, Rows, Cols > operator*(T d, const Matrix< U, Rows, Cols > &m)
Multiply the matrix by a scalar.
Definition matrix.h:133
libcamera::operator==
bool operator==(const ColorSpace &lhs, const ColorSpace &rhs)
Compare color spaces for equality.
Definition color_space.cpp:506
libcamera::operator!=
bool operator!=(const Vector< T, Rows > &lhs, const Vector< T, Rows > &rhs)
Compare vectors for inequality.
Definition vector.h:347
libcamera::operator<
bool operator<(const Size &lhs, const Size &rhs)
Compare sizes for smaller than order.
Definition geometry.cpp:411
libcamera::operator/
Transform operator/(const Orientation &o1, const Orientation &o2)
Return the Transform that applied to o2 gives o1.
Definition transform.cpp:347