Loading...
Searching...
No Matches
geometry.h
Go to the documentation of this file.
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2019, Google Inc.
*
* Geometry-related classes
*/
#pragma once
#include <algorithm>
#include <ostream>
#include <string>
namespace libcamera {
class Rectangle;
class Point
{
public:
constexpr Point()
: x(0), y(0)
{
}
constexpr Point(int xpos, int ypos)
: x(xpos), y(ypos)
{
}
int x;
int y;
std::string toString() const;
constexpr Point operator-() const
{
return { -x, -y };
}
};
bool operator==(const Point &lhs, const Point &rhs);
static inline bool operator!=(const Point &lhs, const Point &rhs)
{
return !(lhs == rhs);
}
std::ostream &operator<<(std::ostream &out, const Point &p);
class Size
{
public:
constexpr Size()
: Size(0, 0)
{
}
constexpr Size(unsigned int w, unsigned int h)
: width(w), height(h)
{
}
unsigned int width;
unsigned int height;
bool isNull() const { return !width && !height; }
std::string toString() const;
Size &alignDownTo(unsigned int hAlignment, unsigned int vAlignment)
{
width = width / hAlignment * hAlignment;
height = height / vAlignment * vAlignment;
return *this;
}
Size &alignUpTo(unsigned int hAlignment, unsigned int vAlignment)
{
width = (width + hAlignment - 1) / hAlignment * hAlignment;
height = (height + vAlignment - 1) / vAlignment * vAlignment;
return *this;
}
Size &boundTo(const Size &bound)
{
width = std::min(width, bound.width);
height = std::min(height, bound.height);
return *this;
}
Size &expandTo(const Size &expand)
{
width = std::max(width, expand.width);
height = std::max(height, expand.height);
return *this;
}
Size &growBy(const Size &margins)
{
width += margins.width;
height += margins.height;
return *this;
}
Size &shrinkBy(const Size &margins)
{
width = width > margins.width ? width - margins.width : 0;
height = height > margins.height ? height - margins.height : 0;
return *this;
}
Size &transpose()
{
std::swap(width, height);
return *this;
}
[[nodiscard]] constexpr Size alignedDownTo(unsigned int hAlignment,
unsigned int vAlignment) const
{
return {
width / hAlignment * hAlignment,
height / vAlignment * vAlignment
};
}
[[nodiscard]] constexpr Size alignedUpTo(unsigned int hAlignment,
unsigned int vAlignment) const
{
return {
(width + hAlignment - 1) / hAlignment * hAlignment,
(height + vAlignment - 1) / vAlignment * vAlignment
};
}
[[nodiscard]] constexpr Size boundedTo(const Size &bound) const
{
return {
std::min(width, bound.width),
std::min(height, bound.height)
};
}
[[nodiscard]] constexpr Size expandedTo(const Size &expand) const
{
return {
std::max(width, expand.width),
std::max(height, expand.height)
};
}
[[nodiscard]] constexpr Size grownBy(const Size &margins) const
{
return {
width + margins.width,
height + margins.height
};
}
[[nodiscard]] constexpr Size shrunkBy(const Size &margins) const
{
return {
width > margins.width ? width - margins.width : 0,
height > margins.height ? height - margins.height : 0
};
}
[[nodiscard]] Size boundedToAspectRatio(const Size &ratio) const;
[[nodiscard]] Size expandedToAspectRatio(const Size &ratio) const;
[[nodiscard]] Rectangle centeredTo(const Point &center) const;
Size operator*(float factor) const;
Size operator/(float factor) const;
Size &operator*=(float factor);
Size &operator/=(float factor);
};
bool operator==(const Size &lhs, const Size &rhs);
bool operator<(const Size &lhs, const Size &rhs);
static inline bool operator!=(const Size &lhs, const Size &rhs)
{
return !(lhs == rhs);
}
static inline bool operator<=(const Size &lhs, const Size &rhs)
{
return lhs < rhs || lhs == rhs;
}
static inline bool operator>(const Size &lhs, const Size &rhs)
{
return !(lhs <= rhs);
}
static inline bool operator>=(const Size &lhs, const Size &rhs)
{
return !(lhs < rhs);
}
std::ostream &operator<<(std::ostream &out, const Size &s);
class SizeRange
{
public:
SizeRange()
: hStep(0), vStep(0)
{
}
SizeRange(const Size &size)
: min(size), max(size), hStep(1), vStep(1)
{
}
SizeRange(const Size &minSize, const Size &maxSize)
: min(minSize), max(maxSize), hStep(1), vStep(1)
{
}
SizeRange(const Size &minSize, const Size &maxSize,
unsigned int hstep, unsigned int vstep)
: min(minSize), max(maxSize), hStep(hstep), vStep(vstep)
{
}
bool contains(const Size &size) const;
std::string toString() const;
Size min;
Size max;
unsigned int hStep;
unsigned int vStep;
};
bool operator==(const SizeRange &lhs, const SizeRange &rhs);
static inline bool operator!=(const SizeRange &lhs, const SizeRange &rhs)
{
return !(lhs == rhs);
}
std::ostream &operator<<(std::ostream &out, const SizeRange &sr);
class Rectangle
{
public:
constexpr Rectangle()
: Rectangle(0, 0, 0, 0)
{
}
constexpr Rectangle(int xpos, int ypos, const Size &size)
: x(xpos), y(ypos), width(size.width), height(size.height)
{
}
constexpr Rectangle(int xpos, int ypos, unsigned int w, unsigned int h)
: x(xpos), y(ypos), width(w), height(h)
{
}
constexpr explicit Rectangle(const Size &size)
: x(0), y(0), width(size.width), height(size.height)
{
}
constexpr Rectangle(const Point &point1, const Point &point2)
: Rectangle(std::min(point1.x, point2.x), std::min(point1.y, point2.y),
static_cast<unsigned int>(std::max(point1.x, point2.x)) -
static_cast<unsigned int>(std::min(point1.x, point2.x)),
static_cast<unsigned int>(std::max(point1.y, point2.y)) -
static_cast<unsigned int>(std::min(point1.y, point2.y)))
{
}
int x;
int y;
unsigned int width;
unsigned int height;
bool isNull() const { return !width && !height; }
std::string toString() const;
Point center() const;
Size size() const
{
return { width, height };
}
Point topLeft() const
{
return { x, y };
}
Rectangle &scaleBy(const Size &numerator, const Size &denominator);
Rectangle &translateBy(const Point &point);
[[nodiscard]] Rectangle boundedTo(const Rectangle &bound) const;
[[nodiscard]] Rectangle enclosedIn(const Rectangle &boundary) const;
[[nodiscard]] Rectangle scaledBy(const Size &numerator,
const Size &denominator) const;
[[nodiscard]] Rectangle translatedBy(const Point &point) const;
Rectangle transformedBetween(const Rectangle &source,
const Rectangle &target) const;
};
bool operator==(const Rectangle &lhs, const Rectangle &rhs);
static inline bool operator!=(const Rectangle &lhs, const Rectangle &rhs)
{
return !(lhs == rhs);
}
std::ostream &operator<<(std::ostream &out, const Rectangle &r);
} /* 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:56
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:732
libcamera::Rectangle::center
Point center() const
Retrieve the center point of this rectangle.
Definition geometry.cpp:701
libcamera::Rectangle::translatedBy
Rectangle translatedBy(const Point &point) const
Translate a Rectangle by the given amounts.
Definition geometry.cpp:845
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:689
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:802
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:884
libcamera::Rectangle::translateBy
Rectangle & translateBy(const Point &point)
Translate this Rectangle in place by the given Point.
Definition geometry.cpp:751
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:771
libcamera::Rectangle::scaledBy
Rectangle scaledBy(const Size &numerator, const Size &denominator) const
Apply a non-uniform rational scaling to this Rectangle.
Definition geometry.cpp:826
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:549
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:564
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:382
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::operator/
Size operator/(float factor) const
Scale size down by the given factor.
Definition geometry.cpp:360
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:337
libcamera::Size::toString
std::string toString() const
Assemble and return a string describing the size.
Definition geometry.cpp:136
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:288
libcamera::Size::expandedToAspectRatio
Size expandedToAspectRatio(const Size &ratio) const
Expand the size to match the aspect ratio given by ratio.
Definition geometry.cpp:313
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::operator*
Size operator*(float factor) const
Scale size up by the given factor.
Definition geometry.cpp:350
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:370
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 Size &lhs, const Size &rhs)
Compare sizes for smaller than order.
Definition geometry.cpp:409