libcamera: include/libcamera/geometry.h Source File

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