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, std::plus<>{}, other);
81 return apply(*
this, std::plus<>{}, scalar);
86 return apply(*
this, std::minus<>{}, other);
91 return apply(*
this, std::minus<>{}, scalar);
96 return apply(*
this, std::multiplies<>{}, other);
101 return apply(*
this, std::multiplies<>{}, scalar);
106 return apply(*
this, std::divides<>{}, other);
111 return apply(*
this, std::divides<>{}, scalar);
116 static_assert(std::is_integral_v<T>,
117 "Vector::operator>> requires an integer element type");
118 return apply(*
this, [](T a,
unsigned int b) {
return a >>
b; }, shift);
123 return apply(std::plus<>{}, other);
128 return apply(std::plus<>{}, scalar);
133 return apply(std::minus<>{}, other);
138 return apply(std::minus<>{}, scalar);
143 return apply(std::multiplies<>{}, other);
148 return apply(std::multiplies<>{}, scalar);
153 return apply(std::divides<>{}, other);
158 return apply(std::divides<>{}, scalar);
163 static_assert(std::is_integral_v<T>,
164 "Vector::operator>>= requires an integer element type");
165 return apply([](T a,
unsigned int b) {
return a >>
b; }, shift);
170 return apply(*
this, [](T a, T
b) {
return std::min(a,
b); }, other);
175 return apply(*
this, [](T a, T
b) {
return std::min(a,
b); }, scalar);
180 return apply(*
this, [](T a, T
b) {
return std::max(a,
b); }, other);
185 return apply(*
this, [](T a, T
b) -> T {
return std::max(a,
b); }, scalar);
191 [](T v, T lo, T hi) -> T {
return std::clamp(v, lo, hi); },
198 for (
unsigned int i = 0; i < Rows; i++)
199 ret += data_[i] * other[i];
204 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 1>>
206 constexpr const T &
x()
const {
return data_[0]; }
208 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 2>>
210 constexpr const T &
y()
const {
return data_[1]; }
212 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 3>>
214 constexpr const T &
z()
const {
return data_[2]; }
216 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 1>>
218 constexpr T &
x() {
return data_[0]; }
220 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 2>>
222 constexpr T &
y() {
return data_[1]; }
224 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 3>>
226 constexpr T &
z() {
return data_[2]; }
229 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 1>>
231 constexpr const T &
r()
const {
return data_[0]; }
233 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 2>>
235 constexpr const T &
g()
const {
return data_[1]; }
237 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 3>>
239 constexpr const T &
b()
const {
return data_[2]; }
241 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 1>>
243 constexpr T &
r() {
return data_[0]; }
245 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 2>>
247 constexpr T &
g() {
return data_[1]; }
249 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 3>>
251 constexpr T &
b() {
return data_[2]; }
256 for (
unsigned int i = 0; i < Rows; i++)
257 ret += data_[i] * data_[i];
266 template<
typename R = T>
269 return std::accumulate(data_.begin(), data_.end(), R{});
273 template<
typename BinaryOp>
277 std::transform(lhs.data_.begin(), lhs.data_.end(),
278 rhs.data_.begin(), result.data_.begin(),
284 template<
typename Op,
typename... U>
285 static constexpr Vector apply(
const Vector &vector, Op op, U... scalar)
288 std::transform(vector.data_.begin(), vector.data_.end(),
289 result.data_.begin(),
290 [&op, scalar...](T v) { return op(v, scalar...); });
295 template<
typename BinaryOp>
298 auto itOther = other.data_.begin();
299 std::for_each(data_.begin(), data_.end(),
300 [&op, &itOther](T &v) { v = op(v, *itOther++); });
305 template<
typename Op,
typename... U>
306 Vector &apply(Op op, U... scalar)
308 std::for_each(data_.begin(), data_.end(),
309 [&op, scalar...](T &v) { v = op(v, scalar...); });
314 std::array<T, Rows> data_;
320template<
typename T,
typename U,
unsigned int Rows,
unsigned int Cols>
325 for (
unsigned int i = 0; i < Rows; i++) {
326 std::common_type_t<T, U> sum = 0;
327 for (
unsigned int j = 0; j < Cols; j++)
328 sum += m[i][j] * v[j];
335template<
typename T,
unsigned int Rows>
338 for (
unsigned int i = 0; i < Rows; i++) {
339 if (lhs[i] != rhs[i])
346template<
typename T,
unsigned int Rows>
349 return !(lhs == rhs);
353bool vectorValidateValueNode(
const ValueNode &obj,
unsigned int size);
355template<
typename T,
unsigned int Rows>
356std::ostream &
operator<<(std::ostream &out,
const Vector<T, Rows> &v)
359 for (
unsigned int i = 0; i < Rows; i++) {
361 out << ((i + 1 < Rows) ?
", " :
" ");
368template<
typename T,
unsigned int Rows>
369struct ValueNode::Accessor<
Vector<T, Rows>> {
370 std::optional<Vector<T, Rows>>
get(
const ValueNode &obj)
const
372 if (!vectorValidateValueNode(obj, Rows))
375 Vector<T, Rows> vector;
378 for (
const ValueNode &entry : obj.asList()) {
379 const auto value = entry.get<T>();
382 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
Vector class.
Definition vector.h:35
constexpr double length2() const
Get the squared length of the vector.
Definition vector.h:253
constexpr Vector operator/(const Vector &other) const
Calculate the quotient of this vector and other element-wise.
Definition vector.h:104
constexpr const T & b() const
Definition vector.h:239
constexpr Vector operator+(const Vector &other) const
Calculate the sum of this vector and other element-wise.
Definition vector.h:74
constexpr T & g()
Convenience function to access the second element of the vector.
Definition vector.h:247
constexpr double length() const
Get the length of the vector.
Definition vector.h:261
constexpr T & r()
Convenience function to access the first element of the vector.
Definition vector.h:243
constexpr T & b()
Convenience function to access the third element of the vector.
Definition vector.h:251
constexpr Vector(const std::array< T, Rows > &data)
Construct vector from supplied data.
Definition vector.h:44
constexpr Vector min(const Vector &other) const
Calculate the minimum of this vector and other element-wise.
Definition vector.h:168
constexpr Vector operator>>(unsigned int shift) const
Right-shift each element of this vector by shift bits.
Definition vector.h:114
constexpr const T & y() const
Convenience function to access the second element of the vector.
Definition vector.h:210
constexpr T & z()
Convenience function to access the third element of the vector.
Definition vector.h:226
Vector & operator+=(const Vector &other)
Add other element-wise to this vector.
Definition vector.h:121
constexpr Vector< T, Rows > operator-() const
Negate a Vector by negating both all of its coordinates.
Definition vector.h:66
const T & operator[](size_t i) const
Index to an element in the vector.
Definition vector.h:54
constexpr const T & z() const
Convenience function to access the third element of the vector.
Definition vector.h:214
constexpr Vector(T scalar)
Construct a vector filled with a scalar value.
Definition vector.h:39
constexpr Vector min(T scalar) const
Calculate the minimum of this vector and scalar element-wise.
Definition vector.h:173
constexpr Vector max(const Vector &other) const
Calculate the maximum of this vector and other element-wise.
Definition vector.h:178
Vector & operator-=(T scalar)
Subtract scalar element-wise from this vector.
Definition vector.h:136
Vector & operator/=(T scalar)
Divide this vector by scalar element-wise.
Definition vector.h:156
Vector & operator/=(const Vector &other)
Divide this vector by other element-wise.
Definition vector.h:151
constexpr Vector()=default
Construct an uninitialized vector.
constexpr T & x()
Convenience function to access the first element of the vector.
Definition vector.h:218
constexpr Vector operator*(const Vector &other) const
Calculate the product of this vector and other element-wise.
Definition vector.h:94
Vector & operator>>=(unsigned int shift)
Right-shift each element of this vector by shift bits in place.
Definition vector.h:161
constexpr T & y()
Convenience function to access the second element of the vector.
Definition vector.h:222
constexpr R sum() const
Calculate the sum of all the vector elements.
Definition vector.h:267
constexpr const T & x() const
Convenience function to access the first element of the vector.
Definition vector.h:206
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 sum of this vector and scalar element-wise.
Definition vector.h:79
constexpr const T & r() const
Convenience function to access the first element of the vector.
Definition vector.h:231
Vector & operator*=(T scalar)
Multiply this vector by scalar element-wise.
Definition vector.h:146
constexpr Vector max(T scalar) const
Calculate the maximum of this vector and scalar element-wise.
Definition vector.h:183
constexpr Vector operator-(T scalar) const
Calculate the difference of this vector and scalar element-wise.
Definition vector.h:89
constexpr Vector clamp(T low, T high) const
Clamp the vector element-wise between low and high.
Definition vector.h:188
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)
Subtract other element-wise from this vector.
Definition vector.h:131
Vector & operator+=(T scalar)
Add scalar element-wise to this vector.
Definition vector.h:126
constexpr Vector(const Span< const T, Rows > data)
Construct vector from supplied data.
Definition vector.h:49
Vector & operator*=(const Vector &other)
Multiply this vector by other element-wise.
Definition vector.h:141
constexpr Vector operator*(T scalar) const
Calculate the product of this vector and scalar element-wise.
Definition vector.h:99
constexpr const T & g() const
Convenience function to access the second element of the vector.
Definition vector.h:235
constexpr T dot(const Vector< T, Rows > &other) const
Compute the dot product.
Definition vector.h:195
T & operator[](size_t i)
Index to an element in the vector.
Definition vector.h:60
#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:159
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
Matrix< U, Rows, Cols > operator*(T d, const Matrix< U, Rows, Cols > &m)
Multiply the matrix by a scalar.
Definition matrix.h:133
Vector< T, 3 > RGB
A Vector of 3 elements representing an RGB pixel value.
Definition vector.h:318
Data structure to manage tree of values.