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 return apply(other, [](T a, T
b) {
return a +
b; });
121 return apply(scalar, [](T a, T
b) {
return a +
b; });
126 return apply(other, [](T a, T
b) {
return a -
b; });
131 return apply(scalar, [](T a, T
b) {
return a -
b; });
136 return apply(other, [](T a, T
b) {
return a *
b; });
141 return apply(scalar, [](T a, T
b) {
return a *
b; });
146 return apply(other, [](T a, T
b) {
return a /
b; });
151 return apply(scalar, [](T a, T
b) {
return a /
b; });
156 return apply(*
this, other, [](T a, T
b) {
return std::min(a,
b); });
161 return apply(*
this, scalar, [](T a, T
b) {
return std::min(a,
b); });
166 return apply(*
this, other, [](T a, T
b) {
return std::max(a,
b); });
171 return apply(*
this, scalar, [](T a, T
b) -> T {
return std::max(a,
b); });
177 for (
unsigned int i = 0; i < Rows; i++)
178 ret += data_[i] * other[i];
183 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 1>>
185 constexpr const T &
x()
const {
return data_[0]; }
187 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 2>>
189 constexpr const T &
y()
const {
return data_[1]; }
191 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 3>>
193 constexpr const T &
z()
const {
return data_[2]; }
195 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 1>>
197 constexpr T &
x() {
return data_[0]; }
199 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 2>>
201 constexpr T &
y() {
return data_[1]; }
203 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 3>>
205 constexpr T &
z() {
return data_[2]; }
208 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 1>>
210 constexpr const T &
r()
const {
return data_[0]; }
212 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 2>>
214 constexpr const T &
g()
const {
return data_[1]; }
216 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 3>>
218 constexpr const T &
b()
const {
return data_[2]; }
220 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 1>>
222 constexpr T &
r() {
return data_[0]; }
224 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 2>>
226 constexpr T &
g() {
return data_[1]; }
228 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 3>>
230 constexpr T &
b() {
return data_[2]; }
235 for (
unsigned int i = 0; i < Rows; i++)
236 ret += data_[i] * data_[i];
245 template<
typename R = T>
248 return std::accumulate(data_.begin(), data_.end(), R{});
252 template<
class BinaryOp>
256 std::transform(lhs.data_.begin(), lhs.data_.end(),
257 rhs.data_.begin(), result.data_.begin(),
263 template<
class BinaryOp>
264 static constexpr Vector apply(
const Vector &lhs, T rhs, BinaryOp op)
267 std::transform(lhs.data_.begin(), lhs.data_.end(),
268 result.data_.begin(),
269 [&op, rhs](T v) { return op(v, rhs); });
274 template<
class BinaryOp>
277 auto itOther = other.data_.begin();
278 std::for_each(data_.begin(), data_.end(),
279 [&op, &itOther](T &v) { v = op(v, *itOther++); });
284 template<
class BinaryOp>
285 Vector &apply(T scalar, BinaryOp op)
287 std::for_each(data_.begin(), data_.end(),
288 [&op, scalar](T &v) { v = op(v, scalar); });
293 std::array<T, Rows> data_;
299template<
typename T,
typename U,
unsigned int Rows,
unsigned int Cols>
304 for (
unsigned int i = 0; i < Rows; i++) {
305 std::common_type_t<T, U> sum = 0;
306 for (
unsigned int j = 0; j < Cols; j++)
307 sum += m[i][j] * v[j];
314template<
typename T,
unsigned int Rows>
317 for (
unsigned int i = 0; i < Rows; i++) {
318 if (lhs[i] != rhs[i])
325template<
typename T,
unsigned int Rows>
328 return !(lhs == rhs);
332bool vectorValidateYaml(
const YamlObject &obj,
unsigned int size);
336template<
typename T,
unsigned int Rows>
337std::ostream &
operator<<(std::ostream &out,
const Vector<T, Rows> &v)
340 for (
unsigned int i = 0; i < Rows; i++) {
342 out << ((i + 1 < Rows) ?
", " :
" ");
349template<
typename T,
unsigned int Rows>
350struct YamlObject::Getter<
Vector<T, Rows>> {
351 std::optional<Vector<T, Rows>>
get(
const YamlObject &obj)
const
353 if (!vectorValidateYaml(obj, Rows))
356 Vector<T, Rows> vector;
359 for (
const YamlObject &entry : obj.asList()) {
360 const auto value = entry.get<T>();
363 vector[i++] = *value;
Matrix class.
Definition matrix.h:31
Vector class.
Definition vector.h:35
constexpr double length2() const
Get the squared length of the vector.
Definition vector.h:232
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:218
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:226
constexpr double length() const
Get the length of the vector.
Definition vector.h:240
constexpr T & r()
Convenience function to access the first element of the vector.
Definition vector.h:222
constexpr T & b()
Convenience function to access the third element of the vector.
Definition vector.h:230
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:154
constexpr const T & y() const
Convenience function to access the second element of the vector.
Definition vector.h:189
constexpr T & z()
Convenience function to access the third element of the vector.
Definition vector.h:205
Vector & operator+=(const Vector &other)
Add other element-wise to this vector.
Definition vector.h:114
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:193
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:159
constexpr Vector max(const Vector &other) const
Calculate the maximum of this vector and other element-wise.
Definition vector.h:164
Vector & operator-=(T scalar)
Subtract scalar element-wise from this vector.
Definition vector.h:129
Vector & operator/=(T scalar)
Divide this vector by scalar element-wise.
Definition vector.h:149
Vector & operator/=(const Vector &other)
Divide this vector by other element-wise.
Definition vector.h:144
constexpr Vector()=default
Construct an uninitialized vector.
constexpr T & x()
Convenience function to access the first element of the vector.
Definition vector.h:197
constexpr Vector operator*(const Vector &other) const
Calculate the product of this vector and other element-wise.
Definition vector.h:94
constexpr T & y()
Convenience function to access the second element of the vector.
Definition vector.h:201
constexpr R sum() const
Calculate the sum of all the vector elements.
Definition vector.h:246
constexpr const T & x() const
Convenience function to access the first element of the vector.
Definition vector.h:185
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:210
Vector & operator*=(T scalar)
Multiply this vector by scalar element-wise.
Definition vector.h:139
constexpr Vector max(T scalar) const
Calculate the maximum of this vector and scalar element-wise.
Definition vector.h:169
constexpr Vector operator-(T scalar) const
Calculate the difference of this vector and scalar element-wise.
Definition vector.h:89
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:124
Vector & operator+=(T scalar)
Add scalar element-wise to this vector.
Definition vector.h:119
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:134
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:214
constexpr T dot(const Vector< T, Rows > &other) const
Compute the dot product.
Definition vector.h:174
T & operator[](size_t i)
Index to an element in the vector.
Definition vector.h:60
std::optional< T > get() const
Parse the YamlObject as a T value.
Definition yaml_parser.h:175
#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:127
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:91
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:297