18 #include <libcamera/base/span.h> 28 template<
typename T,
unsigned int Rows,
29 std::enable_if_t<std::is_arithmetic_v<T>> * =
nullptr>
31 template<
typename T,
unsigned int Rows>
36 constexpr
Vector() =
default;
43 constexpr
Vector(
const std::array<T, Rows> &data)
45 for (
unsigned int i = 0; i < Rows; i++)
64 for (
unsigned int i = 0; i < Rows; i++)
71 return apply(*
this, other, std::plus<>{});
76 return apply(*
this, scalar, std::plus<>{});
81 return apply(*
this, other, std::minus<>{});
86 return apply(*
this, scalar, std::minus<>{});
91 return apply(*
this, other, std::multiplies<>{});
96 return apply(*
this, scalar, std::multiplies<>{});
101 return apply(*
this, other, std::divides<>{});
106 return apply(*
this, scalar, std::divides<>{});
111 return apply(other, [](T a, T
b) {
return a +
b; });
116 return apply(scalar, [](T a, T
b) {
return a +
b; });
121 return apply(other, [](T a, T
b) {
return a -
b; });
126 return apply(scalar, [](T a, T
b) {
return a -
b; });
131 return apply(other, [](T a, T
b) {
return a *
b; });
136 return apply(scalar, [](T a, T
b) {
return a *
b; });
141 return apply(other, [](T a, T
b) {
return a /
b; });
146 return apply(scalar, [](T a, T
b) {
return a /
b; });
151 return apply(*
this, other, [](T a, T
b) {
return std::min(a, b); });
156 return apply(*
this, scalar, [](T a, T
b) {
return std::min(a, b); });
161 return apply(*
this, other, [](T a, T
b) {
return std::max(a, b); });
166 return apply(*
this, scalar, [](T a, T
b) -> T {
return std::max(a, b); });
172 for (
unsigned int i = 0; i < Rows; i++)
173 ret += data_[i] * other[i];
178 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 1>>
180 constexpr
const T &
x()
const {
return data_[0]; }
182 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 2>>
184 constexpr
const T &
y()
const {
return data_[1]; }
186 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 3>>
188 constexpr
const T &
z()
const {
return data_[2]; }
190 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 1>>
192 constexpr T &
x() {
return data_[0]; }
194 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 2>>
196 constexpr T &
y() {
return data_[1]; }
198 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 3>>
200 constexpr T &
z() {
return data_[2]; }
203 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 1>>
205 constexpr
const T &
r()
const {
return data_[0]; }
207 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 2>>
209 constexpr
const T &
g()
const {
return data_[1]; }
211 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 3>>
213 constexpr
const T &
b()
const {
return data_[2]; }
215 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 1>>
217 constexpr T &
r() {
return data_[0]; }
219 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 2>>
221 constexpr T &
g() {
return data_[1]; }
223 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 3>>
225 constexpr T &
b() {
return data_[2]; }
230 for (
unsigned int i = 0; i < Rows; i++)
231 ret += data_[i] * data_[i];
240 template<
typename R = T>
243 return std::accumulate(data_.begin(), data_.end(), R{});
247 template<
class BinaryOp>
251 std::transform(lhs.data_.begin(), lhs.data_.end(),
252 rhs.data_.begin(), result.data_.begin(),
258 template<
class BinaryOp>
259 static constexpr
Vector apply(
const Vector &lhs, T rhs, BinaryOp op)
262 std::transform(lhs.data_.begin(), lhs.data_.end(),
263 result.data_.begin(),
264 [&op, rhs](T v) {
return op(v, rhs); });
269 template<
class BinaryOp>
272 auto itOther = other.data_.begin();
273 std::for_each(data_.begin(), data_.end(),
274 [&op, &itOther](T &v) { v = op(v, *itOther++); });
279 template<
class BinaryOp>
280 Vector &apply(T scalar, BinaryOp op)
282 std::for_each(data_.begin(), data_.end(),
283 [&op, scalar](T &v) { v = op(v, scalar); });
288 std::array<T, Rows> data_;
294 template<
typename T,
unsigned int Rows,
unsigned int Cols>
299 for (
unsigned int i = 0; i < Rows; i++) {
301 for (
unsigned int j = 0; j < Cols; j++)
302 sum += m[i][j] * v[j];
309 template<
typename T,
unsigned int Rows>
312 for (
unsigned int i = 0; i < Rows; i++) {
313 if (lhs[i] != rhs[i])
320 template<
typename T,
unsigned int Rows>
323 return !(lhs == rhs);
327 bool vectorValidateYaml(
const YamlObject &obj,
unsigned int size);
331 template<
typename T,
unsigned int Rows>
332 std::ostream &operator<<(std::ostream &out, const Vector<T, Rows> &v)
335 for (
unsigned int i = 0; i < Rows; i++) {
337 out << ((i + 1 < Rows) ?
", " :
" ");
344 template<
typename T,
unsigned int Rows>
345 struct YamlObject::Getter<Vector<T, Rows>> {
346 std::optional<Vector<T, Rows>>
get(
const YamlObject &obj)
const 348 if (!vectorValidateYaml(obj, Rows))
355 const auto value = entry.get<T>();
358 vector[i++] = *value;
bool operator!=(const Vector< T, Rows > &lhs, const Vector< T, Rows > &rhs)
Compare vectors for inequality.
Definition: vector.h:321
constexpr const T & g() const
Convenience function to access the second element of the vector.
Definition: vector.h:209
constexpr const T & b() const
Convenience function to access the third element of the vector.
Definition: vector.h:213
constexpr R sum() const
Calculate the sum of all the vector elements.
Definition: vector.h:241
constexpr Vector()=default
Construct an uninitialized vector.
constexpr Vector min(const Vector &other) const
Calculate the minimum of this vector and other element-wise.
Definition: vector.h:149
constexpr const T & r() const
Convenience function to access the first element of the vector.
Definition: vector.h:205
bool operator==(const ColorSpace &lhs, const ColorSpace &rhs)
Compare color spaces for equality.
Definition: color_space.cpp:506
constexpr Vector(const std::array< T, Rows > &data)
Construct vector from supplied data.
Definition: vector.h:43
constexpr T & r()
Convenience function to access the first element of the vector.
Definition: vector.h:217
constexpr Vector operator+(const Vector &other) const
Calculate the sum of this vector and other element-wise.
Definition: vector.h:69
Top-level libcamera namespace.
Definition: backtrace.h:17
ListAdapter asList() const
Wrap a list YamlObject in an adapter that exposes iterators.
Definition: yaml_parser.h:206
constexpr const T & y() const
Convenience function to access the second element of the vector.
Definition: vector.h:184
constexpr Vector min(T scalar) const
Calculate the minimum of this vector and scalar element-wise.
Definition: vector.h:154
constexpr Vector operator/(T scalar) const
Calculate the quotient of this vector and scalar element-wise.
Definition: vector.h:104
#define ASSERT(condition)
Abort program execution if assertion fails.
Vector & operator/=(T scalar)
Divide this vector by scalar element-wise.
Definition: vector.h:144
constexpr Vector max(const Vector &other) const
Calculate the maximum of this vector and other element-wise.
Definition: vector.h:159
Matrix class.
Definition: matrix.h:28
constexpr T & g()
Convenience function to access the second element of the vector.
Definition: vector.h:221
Vector & operator+=(const Vector &other)
Add other element-wise to this vector.
Definition: vector.h:109
constexpr T & x()
Convenience function to access the first element of the vector.
Definition: vector.h:192
const T & operator[](size_t i) const
Index to an element in the vector.
Definition: vector.h:49
constexpr Vector operator*(T scalar) const
Calculate the product of this vector and scalar element-wise.
Definition: vector.h:94
#define LOG_DECLARE_CATEGORY(name)
Declare a category of log messages.
constexpr Vector< T, Rows > operator-() const
Negate a Vector by negating both all of its coordinates.
Definition: vector.h:61
constexpr T & b()
Convenience function to access the third element of the vector.
Definition: vector.h:225
constexpr const T & x() const
Convenience function to access the first element of the vector.
Definition: vector.h:180
constexpr double length2() const
Get the squared length of the vector.
Definition: vector.h:227
Vector & operator*=(const Vector &other)
Multiply this vector by other element-wise.
Definition: vector.h:129
constexpr double length() const
Get the length of the vector.
Definition: vector.h:235
A class representing the tree structure of the YAML content.
Definition: yaml_parser.h:27
T & operator[](size_t i)
Index to an element in the vector.
Definition: vector.h:55
constexpr Vector operator-(T scalar) const
Calculate the difference of this vector and scalar element-wise.
Definition: vector.h:84
constexpr T & z()
Convenience function to access the third element of the vector.
Definition: vector.h:200
constexpr Vector operator+(T scalar) const
Calculate the sum of this vector and scalar element-wise.
Definition: vector.h:74
constexpr T & y()
Convenience function to access the second element of the vector.
Definition: vector.h:196
Vector & operator/=(const Vector &other)
Divide this vector by other element-wise.
Definition: vector.h:139
constexpr Vector operator-(const Vector &other) const
Calculate the difference of this vector and other element-wise.
Definition: vector.h:79
Vector & operator-=(const Vector &other)
Subtract other element-wise from this vector.
Definition: vector.h:119
constexpr Vector operator*(const Vector &other) const
Calculate the product of this vector and other element-wise.
Definition: vector.h:89
constexpr T dot(const Vector< T, Rows > &other) const
Compute the dot product.
Definition: vector.h:169
constexpr const T & z() const
Convenience function to access the third element of the vector.
Definition: vector.h:188
Vector & operator*=(T scalar)
Multiply this vector by scalar element-wise.
Definition: vector.h:134
Vector class.
Definition: vector.h:33
constexpr Vector max(T scalar) const
Calculate the maximum of this vector and scalar element-wise.
Definition: vector.h:164
constexpr Vector(T scalar)
Construct a vector filled with a scalar value.
Definition: vector.h:38
Vector & operator+=(T scalar)
Add scalar element-wise to this vector.
Definition: vector.h:114
Vector & operator-=(T scalar)
Subtract scalar element-wise from this vector.
Definition: vector.h:124
constexpr Vector operator/(const Vector &other) const
Calculate the quotient of this vector and other element-wise.
Definition: vector.h:99