19#include <libcamera/base/span.h>
29template<
typename T,
unsigned int Rows,
30 std::enable_if_t<std::is_arithmetic_v<T>> * =
nullptr>
32template<
typename T,
unsigned int Rows>
44 constexpr Vector(
const std::array<T, Rows> &data)
46 std::copy(data.begin(), data.end(), data_.begin());
49 constexpr Vector(
const Span<const T, Rows> data)
51 std::copy(data.begin(), data.end(), data_.begin());
69 for (
unsigned int i = 0; i < Rows; i++)
76 return apply(*
this, other, std::plus<>{});
81 return apply(*
this, scalar, std::plus<>{});
86 return apply(*
this, other, std::minus<>{});
91 return apply(*
this, scalar, std::minus<>{});
96 return apply(*
this, other, std::multiplies<>{});
101 return apply(*
this, scalar, std::multiplies<>{});
106 return apply(*
this, other, std::divides<>{});
111 return apply(*
this, scalar, std::divides<>{});
116 static_assert(std::is_integral_v<T>,
117 "Vector::operator>> requires an integer element type");
118 return apply(*
this, shift, [](T a,
unsigned int b) {
return a >>
b; });
123 return apply(other, [](T a, T
b) {
return a +
b; });
128 return apply(scalar, [](T a, T
b) {
return a +
b; });
133 return apply(other, [](T a, T
b) {
return a -
b; });
138 return apply(scalar, [](T a, T
b) {
return a -
b; });
143 return apply(other, [](T a, T
b) {
return a *
b; });
148 return apply(scalar, [](T a, T
b) {
return a *
b; });
153 return apply(other, [](T a, T
b) {
return a /
b; });
158 return apply(scalar, [](T a, T
b) {
return a /
b; });
163 static_assert(std::is_integral_v<T>,
164 "Vector::operator>>= requires an integer element type");
165 return apply(shift, [](T a,
unsigned int b) {
return a >>
b; });
170 return apply(*
this, other, [](T a, T
b) {
return std::min(a,
b); });
175 return apply(*
this, scalar, [](T a, T
b) {
return std::min(a,
b); });
180 return apply(*
this, other, [](T a, T
b) {
return std::max(a,
b); });
185 return apply(*
this, scalar, [](T a, T
b) -> T {
return std::max(a,
b); });
191 for (
unsigned int i = 0; i < Rows; i++)
192 ret += data_[i] * other[i];
197 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 1>>
199 constexpr const T &
x()
const {
return data_[0]; }
201 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 2>>
203 constexpr const T &
y()
const {
return data_[1]; }
205 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 3>>
207 constexpr const T &
z()
const {
return data_[2]; }
209 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 1>>
211 constexpr T &
x() {
return data_[0]; }
213 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 2>>
215 constexpr T &
y() {
return data_[1]; }
217 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 3>>
219 constexpr T &
z() {
return data_[2]; }
222 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 1>>
224 constexpr const T &
r()
const {
return data_[0]; }
226 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 2>>
228 constexpr const T &
g()
const {
return data_[1]; }
230 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 3>>
232 constexpr const T &
b()
const {
return data_[2]; }
234 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 1>>
236 constexpr T &
r() {
return data_[0]; }
238 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 2>>
240 constexpr T &
g() {
return data_[1]; }
242 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 3>>
244 constexpr T &
b() {
return data_[2]; }
249 for (
unsigned int i = 0; i < Rows; i++)
250 ret += data_[i] * data_[i];
259 template<
typename R = T>
262 return std::accumulate(data_.begin(), data_.end(), R{});
266 template<
class BinaryOp>
270 std::transform(lhs.data_.begin(), lhs.data_.end(),
271 rhs.data_.begin(), result.data_.begin(),
277 template<
class U,
class BinaryOp>
278 static constexpr Vector apply(
const Vector &lhs, U rhs, BinaryOp op)
281 std::transform(lhs.data_.begin(), lhs.data_.end(),
282 result.data_.begin(),
283 [&op, rhs](T v) { return op(v, rhs); });
288 template<
class BinaryOp>
291 auto itOther = other.data_.begin();
292 std::for_each(data_.begin(), data_.end(),
293 [&op, &itOther](T &v) { v = op(v, *itOther++); });
298 template<
class U,
class BinaryOp>
299 Vector &apply(U scalar, BinaryOp op)
301 std::for_each(data_.begin(), data_.end(),
302 [&op, scalar](T &v) { v = op(v, scalar); });
307 std::array<T, Rows> data_;
313template<
typename T,
typename U,
unsigned int Rows,
unsigned int Cols>
318 for (
unsigned int i = 0; i < Rows; i++) {
319 std::common_type_t<T, U> sum = 0;
320 for (
unsigned int j = 0; j < Cols; j++)
321 sum += m[i][j] * v[j];
328template<
typename T,
unsigned int Rows>
331 for (
unsigned int i = 0; i < Rows; i++) {
332 if (lhs[i] != rhs[i])
339template<
typename T,
unsigned int Rows>
342 return !(lhs == rhs);
346bool vectorValidateYaml(
const ValueNode &obj,
unsigned int size);
350template<
typename T,
unsigned int Rows>
351std::ostream &
operator<<(std::ostream &out,
const Vector<T, Rows> &v)
354 for (
unsigned int i = 0; i < Rows; i++) {
356 out << ((i + 1 < Rows) ?
", " :
" ");
363template<
typename T,
unsigned int Rows>
364struct ValueNode::Accessor<Vector<T, Rows>> {
365 std::optional<Vector<T, Rows>>
get(
const ValueNode &obj)
const
367 if (!vectorValidateYaml(obj, Rows))
370 Vector<T, Rows> vector;
373 for (
const ValueNode &entry : obj.
asList()) {
374 const auto value = entry.get<T>();
377 vector[i++] = *value;
Matrix class.
Definition matrix.h:31
std::optional< T > get() const
Parse the ValueNode as a T value.
Definition value_node.h:211
ListAdapter asList()
Wrap a list ValueNode in an adapter that exposes iterators.
Definition value_node.h:230
Vector class.
Definition vector.h:35
constexpr double length() const
Get the length of the vector.
Definition vector.h:254
constexpr const T & b() const
Convenience function to access the third element of the vector.
Definition vector.h:232
constexpr Vector operator/(T scalar) const
Calculate the quotient of this vector and scalar element-wise.
Definition vector.h:109
constexpr Vector operator*(T scalar) const
Calculate the product of this vector and scalar element-wise.
Definition vector.h:99
constexpr T & g()
Convenience function to access the second element of the vector.
Definition vector.h:240
constexpr Vector()=default
Construct an uninitialized vector.
constexpr Vector min(T scalar) const
Calculate the minimum of this vector and scalar element-wise.
Definition vector.h:173
constexpr T & r()
Convenience function to access the first element of the vector.
Definition vector.h:236
constexpr T & b()
Convenience function to access the third element of the vector.
Definition vector.h:244
constexpr Vector< T, Rows > operator-() const
Negate a Vector by negating both all of its coordinates.
Definition vector.h:66
constexpr Vector operator>>(unsigned int shift) const
Right-shift each element of this vector by shift bits.
Definition vector.h:114
constexpr T dot(const Vector< T, Rows > &other) const
Compute the dot product.
Definition vector.h:188
constexpr Vector(const std::array< T, Rows > &data)
Construct vector from supplied data.
Definition vector.h:44
constexpr Vector(T scalar)
Construct a vector filled with a scalar value.
Definition vector.h:39
const T & operator[](size_t i) const
Index to an element in the vector.
Definition vector.h:54
constexpr const T & y() const
Convenience function to access the second element of the vector.
Definition vector.h:203
constexpr T & z()
Convenience function to access the third element of the vector.
Definition vector.h:219
Vector & operator-=(const Vector &other)
Subtract other element-wise from this vector.
Definition vector.h:131
constexpr double length2() const
Get the squared length of the vector.
Definition vector.h:246
constexpr const T & z() const
Convenience function to access the third element of the vector.
Definition vector.h:207
constexpr Vector operator-(T scalar) const
Calculate the difference of this vector and scalar element-wise.
Definition vector.h:89
Vector & operator-=(T scalar)
Subtract scalar element-wise from this vector.
Definition vector.h:136
Vector & operator>>=(unsigned int shift)
Right-shift each element of this vector by shift bits in place.
Definition vector.h:161
constexpr Vector operator/(const Vector &other) const
Calculate the quotient of this vector and other element-wise.
Definition vector.h:104
constexpr R sum() const
Calculate the sum of all the vector elements.
Definition vector.h:260
Vector & operator/=(const Vector &other)
Divide this vector by other element-wise.
Definition vector.h:151
Vector & operator/=(T scalar)
Divide this vector by scalar element-wise.
Definition vector.h:156
constexpr T & x()
Convenience function to access the first element of the vector.
Definition vector.h:211
constexpr Vector min(const Vector &other) const
Calculate the minimum of this vector and other element-wise.
Definition vector.h:168
constexpr T & y()
Convenience function to access the second element of the vector.
Definition vector.h:215
constexpr const T & x() const
Convenience function to access the first element of the vector.
Definition vector.h:199
constexpr Vector operator+(const Vector &other) const
Calculate the sum of this vector and other element-wise.
Definition vector.h:74
Vector & operator+=(T scalar)
Add scalar element-wise to this vector.
Definition vector.h:126
constexpr Vector operator-(const Vector &other) const
Calculate the difference of this vector and other element-wise.
Definition vector.h:84
Vector & operator*=(const Vector &other)
Multiply this vector by other element-wise.
Definition vector.h:141
Vector & operator*=(T scalar)
Multiply this vector by scalar element-wise.
Definition vector.h:146
constexpr const T & r() const
Convenience function to access the first element of the vector.
Definition vector.h:224
Vector & operator+=(const Vector &other)
Add other element-wise to this vector.
Definition vector.h:121
constexpr Vector operator*(const Vector &other) const
Calculate the product of this vector and other element-wise.
Definition vector.h:94
constexpr Vector operator+(T scalar) const
Calculate the sum of this vector and scalar element-wise.
Definition vector.h:79
constexpr const T & g() const
Convenience function to access the second element of the vector.
Definition vector.h:228
constexpr Vector max(T scalar) const
Calculate the maximum of this vector and scalar element-wise.
Definition vector.h:183
T & operator[](size_t i)
Index to an element in the vector.
Definition vector.h:60
constexpr Vector(const Span< const T, Rows > data)
Construct vector from supplied data.
Definition vector.h:49
constexpr Vector max(const Vector &other) const
Calculate the maximum of this vector and other element-wise.
Definition vector.h:178
#define LOG_DECLARE_CATEGORY(name)
Declare a category of log messages.
Definition log.h:51
#define ASSERT(condition)
Abort program execution if assertion fails.
Definition log.h:133
Top-level libcamera namespace.
Definition backtrace.h:17
std::ostream & operator<<(std::ostream &out, const Point &p)
Insert a text representation of a Point into an output stream.
Definition geometry.cpp:93
bool operator==(const ColorSpace &lhs, const ColorSpace &rhs)
Compare color spaces for equality.
Definition color_space.cpp:506
bool operator!=(const Vector< T, Rows > &lhs, const Vector< T, Rows > &rhs)
Compare vectors for inequality.
Definition vector.h:340
Matrix< U, Rows, Cols > operator*(T d, const Matrix< U, Rows, Cols > &m)
Multiply the matrix by a scalar.
Definition matrix.h:133
Data structure to manage tree of values.