libcamera  v0.0.0+100-debian/0_git20200629+e7aa92a-8-9-g77f5237c-dirty (2021-05-05T16:20:29+01:00)
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 #ifndef __LIBCAMERA_GEOMETRY_H__
9 #define __LIBCAMERA_GEOMETRY_H__
10 
11 #include <algorithm>
12 #include <string>
13 
14 #include <libcamera/compiler.h>
15 
16 namespace libcamera {
17 
18 class Rectangle;
19 
20 class Point
21 {
22 public:
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 
44 bool operator==(const Point &lhs, const Point &rhs);
45 static inline bool operator!=(const Point &lhs, const Point &rhs)
46 {
47  return !(lhs == rhs);
48 }
49 
50 class Size
51 {
52 public:
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  __nodiscard constexpr Size alignedDownTo(unsigned int hAlignment,
98  unsigned int vAlignment) const
99  {
100  return {
101  width / hAlignment * hAlignment,
102  height / vAlignment * vAlignment
103  };
104  }
105 
106  __nodiscard constexpr Size alignedUpTo(unsigned int hAlignment,
107  unsigned int vAlignment) const
108  {
109  return {
110  (width + hAlignment - 1) / hAlignment * hAlignment,
111  (height + vAlignment - 1) / vAlignment * vAlignment
112  };
113  }
114 
115  __nodiscard constexpr Size boundedTo(const Size &bound) const
116  {
117  return {
118  std::min(width, bound.width),
119  std::min(height, bound.height)
120  };
121  }
122 
123  __nodiscard constexpr Size expandedTo(const Size &expand) const
124  {
125  return {
126  std::max(width, expand.width),
127  std::max(height, expand.height)
128  };
129  }
130 
131  __nodiscard Size boundedToAspectRatio(const Size &ratio) const;
132  __nodiscard Size expandedToAspectRatio(const Size &ratio) const;
133 
134  __nodiscard Rectangle centeredTo(const Point &center) const;
135 
136  Size operator*(float factor) const;
137  Size operator/(float factor) const;
138 
139  Size &operator*=(float factor);
140  Size &operator/=(float factor);
141 };
142 
143 bool operator==(const Size &lhs, const Size &rhs);
144 bool operator<(const Size &lhs, const Size &rhs);
145 
146 static inline bool operator!=(const Size &lhs, const Size &rhs)
147 {
148  return !(lhs == rhs);
149 }
150 
151 static inline bool operator<=(const Size &lhs, const Size &rhs)
152 {
153  return lhs < rhs || lhs == rhs;
154 }
155 
156 static inline bool operator>(const Size &lhs, const Size &rhs)
157 {
158  return !(lhs <= rhs);
159 }
160 
161 static inline bool operator>=(const Size &lhs, const Size &rhs)
162 {
163  return !(lhs < rhs);
164 }
165 
167 {
168 public:
170  : hStep(0), vStep(0)
171  {
172  }
173 
174  SizeRange(const Size &size)
175  : min(size), max(size), hStep(1), vStep(1)
176  {
177  }
178 
179  SizeRange(const Size &minSize, const Size &maxSize)
180  : min(minSize), max(maxSize), hStep(1), vStep(1)
181  {
182  }
183 
184  SizeRange(const Size &minSize, const Size &maxSize,
185  unsigned int hstep, unsigned int vstep)
186  : min(minSize), max(maxSize), hStep(hstep), vStep(vstep)
187  {
188  }
189 
190  bool contains(const Size &size) const;
191 
192  std::string toString() const;
193 
196  unsigned int hStep;
197  unsigned int vStep;
198 };
199 
200 bool operator==(const SizeRange &lhs, const SizeRange &rhs);
201 static inline bool operator!=(const SizeRange &lhs, const SizeRange &rhs)
202 {
203  return !(lhs == rhs);
204 }
205 
207 {
208 public:
209  constexpr Rectangle()
210  : Rectangle(0, 0, 0, 0)
211  {
212  }
213 
214  constexpr Rectangle(int xpos, int ypos, const Size &size)
215  : x(xpos), y(ypos), width(size.width), height(size.height)
216  {
217  }
218 
219  constexpr Rectangle(int xpos, int ypos, unsigned int w, unsigned int h)
220  : x(xpos), y(ypos), width(w), height(h)
221  {
222  }
223 
224  constexpr explicit Rectangle(const Size &size)
225  : x(0), y(0), width(size.width), height(size.height)
226  {
227  }
228 
229  int x;
230  int y;
231  unsigned int width;
232  unsigned int height;
233 
234  bool isNull() const { return !width && !height; }
235  const std::string toString() const;
236 
237  Point center() const;
238 
239  Size size() const
240  {
241  return { width, height };
242  }
243 
244  Point topLeft() const
245  {
246  return { x, y };
247  }
248 
249  Rectangle &scaleBy(const Size &numerator, const Size &denominator);
250  Rectangle &translateBy(const Point &point);
251 
252  __nodiscard Rectangle boundedTo(const Rectangle &bound) const;
253  __nodiscard Rectangle enclosedIn(const Rectangle &boundary) const;
254  __nodiscard Rectangle scaledBy(const Size &numerator,
255  const Size &denominator) const;
256  __nodiscard Rectangle translatedBy(const Point &point) const;
257 };
258 
259 bool operator==(const Rectangle &lhs, const Rectangle &rhs);
260 static inline bool operator!=(const Rectangle &lhs, const Rectangle &rhs)
261 {
262  return !(lhs == rhs);
263 }
264 
265 } /* namespace libcamera */
266 
267 #endif /* __LIBCAMERA_GEOMETRY_H__ */
__nodiscard constexpr Size expandedTo(const Size &expand) const
Expand the size to expand.
Definition: geometry.h:123
constexpr Point operator-() const
Negate a Point by negating both its x and y coordinates.
Definition: geometry.h:38
Point topLeft() const
Retrieve the coordinates of the top left corner of this Rectangle.
Definition: geometry.h:244
constexpr Rectangle(const Size &size)
Construct a Rectangle of size with its top left corner located at (0,0)
Definition: geometry.h:224
unsigned int vStep
The vertical step.
Definition: geometry.h:197
__nodiscard constexpr Size alignedUpTo(unsigned int hAlignment, unsigned int vAlignment) const
Align the size up horizontally and vertically.
Definition: geometry.h:106
Size & alignUpTo(unsigned int hAlignment, unsigned int vAlignment)
Align the size up horizontally and vertically in place.
Definition: geometry.h:76
Size & expandTo(const Size &expand)
Expand the size to expand.
Definition: geometry.h:90
SizeRange(const Size &size)
Construct a size range representing a single size.
Definition: geometry.h:174
Transform operator*(Transform t0, Transform t1)
Compose two transforms together.
Definition: transform.cpp:207
Top-level libcamera namespace.
Definition: bound_method.h:15
unsigned int hStep
The horizontal step.
Definition: geometry.h:196
int y
The vertical coordinate of the rectangle&#39;s top-left corner.
Definition: geometry.h:230
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:184
constexpr Rectangle(int xpos, int ypos, unsigned int w, unsigned int h)
Construct a Rectangle with the given position and size.
Definition: geometry.h:219
Describe a point in two-dimensional space.
Definition: geometry.h:20
Describe a two-dimensional size.
Definition: geometry.h:50
SizeRange()
Construct a size range initialized to 0.
Definition: geometry.h:169
bool isNull() const
Check if the rectangle is null.
Definition: geometry.h:234
__nodiscard constexpr Size boundedTo(const Size &bound) const
Bound the size to bound.
Definition: geometry.h:115
int x
The horizontal coordinate of the rectangle&#39;s top-left corner.
Definition: geometry.h:229
Size & alignDownTo(unsigned int hAlignment, unsigned int vAlignment)
Align the size down horizontally and vertically in place.
Definition: geometry.h:69
bool isNull() const
Check if the size is null.
Definition: geometry.h:66
int x
The x-coordinate of the Point.
Definition: geometry.h:33
constexpr Rectangle()
Construct a Rectangle with all coordinates set to 0.
Definition: geometry.h:209
Size size() const
Retrieve the size of this rectangle.
Definition: geometry.h:239
bool operator<(const Size &lhs, const Size &rhs)
Compare sizes for smaller than order.
Definition: geometry.cpp:343
Describe a rectangle&#39;s position and dimensions.
Definition: geometry.h:206
constexpr Point(int xpos, int ypos)
Construct a Point at given xpos and ypos values.
Definition: geometry.h:28
unsigned int width
The Size width.
Definition: geometry.h:63
constexpr Size()
Construct a Size with width and height set to 0.
Definition: geometry.h:53
Size & boundTo(const Size &bound)
Bound the size to bound in place.
Definition: geometry.h:83
Size min
The minimum size.
Definition: geometry.h:194
unsigned int height
The Size height.
Definition: geometry.h:64
constexpr Size(unsigned int w, unsigned int h)
Construct a Size with given width and height.
Definition: geometry.h:58
SizeRange(const Size &minSize, const Size &maxSize)
Construct a size range with specified min and max, and steps of 1.
Definition: geometry.h:179
Describe a range of sizes.
Definition: geometry.h:166
const std::string toString() const
Assemble and return a string describing the point.
Definition: geometry.cpp:56
bool operator==(const Point &lhs, const Point &rhs)
Compare points for equality.
Definition: geometry.cpp:75
unsigned int width
The distance between the left and right sides.
Definition: geometry.h:231
constexpr Point()
Construct a Point with x and y set to 0.
Definition: geometry.h:23
constexpr Rectangle(int xpos, int ypos, const Size &size)
Construct a Rectangle with the given position and size.
Definition: geometry.h:214
unsigned int height
The distance between the top and bottom sides.
Definition: geometry.h:232
__nodiscard constexpr Size alignedDownTo(unsigned int hAlignment, unsigned int vAlignment) const
Align the size down horizontally and vertically.
Definition: geometry.h:97
int y
The y-coordinate of the Point.
Definition: geometry.h:34
Size max
The maximum size.
Definition: geometry.h:195