16 #include <type_traits> 19 #include <libcamera/base/span.h> 29 template<
typename T,
unsigned int Rows,
30 std::enable_if_t<std::is_arithmetic_v<T>> * =
nullptr>
32 template<
typename T,
unsigned int Rows>
37 constexpr
Vector() =
default;
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_;
299 template<
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];
314 template<
typename T,
unsigned int Rows>
317 for (
unsigned int i = 0; i < Rows; i++) {
318 if (lhs[i] != rhs[i])
325 template<
typename T,
unsigned int Rows>
328 return !(lhs == rhs);
332 bool vectorValidateYaml(
const YamlObject &obj,
unsigned int size);
336 template<
typename T,
unsigned int Rows>
337 std::ostream &operator<<(std::ostream &out, const Vector<T, Rows> &v)
340 for (
unsigned int i = 0; i < Rows; i++) {
342 out << ((i + 1 < Rows) ?
", " :
" ");
349 template<
typename T,
unsigned int Rows>
350 struct YamlObject::Getter<Vector<T, Rows>> {
351 std::optional<Vector<T, Rows>>
get(
const YamlObject &obj)
const 353 if (!vectorValidateYaml(obj, Rows))
360 const auto value = entry.get<T>();
363 vector[i++] = *value;
bool operator!=(const Vector< T, Rows > &lhs, const Vector< T, Rows > &rhs)
Compare vectors for inequality.
Definition: vector.h:326
constexpr const T & g() const
Convenience function to access the second element of the vector.
Definition: vector.h:214
constexpr const T & b() const
Convenience function to access the third element of the vector.
Definition: vector.h:218
constexpr R sum() const
Calculate the sum of all the vector elements.
Definition: vector.h:246
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:154
constexpr const T & r() const
Convenience function to access the first element of the vector.
Definition: vector.h:210
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:44
constexpr T & r()
Convenience function to access the first element of the vector.
Definition: vector.h:222
constexpr Vector operator+(const Vector &other) const
Calculate the sum of this vector and other element-wise.
Definition: vector.h:74
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:189
constexpr Vector min(T scalar) const
Calculate the minimum of this vector and scalar element-wise.
Definition: vector.h:159
constexpr Vector operator/(T scalar) const
Calculate the quotient of this vector and scalar element-wise.
Definition: vector.h:109
#define ASSERT(condition)
Abort program execution if assertion fails.
Vector & operator/=(T scalar)
Divide this vector by scalar element-wise.
Definition: vector.h:149
constexpr Vector max(const Vector &other) const
Calculate the maximum of this vector and other element-wise.
Definition: vector.h:164
Matrix class.
Definition: matrix.h:30
constexpr T & g()
Convenience function to access the second element of the vector.
Definition: vector.h:226
Vector & operator+=(const Vector &other)
Add other element-wise to this vector.
Definition: vector.h:114
constexpr T & x()
Convenience function to access the first element of the vector.
Definition: vector.h:197
const T & operator[](size_t i) const
Index to an element in the vector.
Definition: vector.h:54
constexpr Vector operator*(T scalar) const
Calculate the product of this vector and scalar element-wise.
Definition: vector.h:99
#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:66
constexpr T & b()
Convenience function to access the third element of the vector.
Definition: vector.h:230
constexpr const T & x() const
Convenience function to access the first element of the vector.
Definition: vector.h:185
constexpr double length2() const
Get the squared length of the vector.
Definition: vector.h:232
Vector & operator*=(const Vector &other)
Multiply this vector by other element-wise.
Definition: vector.h:134
constexpr Vector(const Span< const T, Rows > data)
Construct vector from supplied data.
Definition: vector.h:49
constexpr double length() const
Get the length of the vector.
Definition: vector.h:240
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:60
constexpr Vector operator-(T scalar) const
Calculate the difference of this vector and scalar element-wise.
Definition: vector.h:89
constexpr T & z()
Convenience function to access the third element of the vector.
Definition: vector.h:205
constexpr Vector operator+(T scalar) const
Calculate the sum of this vector and scalar element-wise.
Definition: vector.h:79
constexpr T & y()
Convenience function to access the second element of the vector.
Definition: vector.h:201
Vector & operator/=(const Vector &other)
Divide this vector by other element-wise.
Definition: vector.h:144
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
constexpr Vector operator*(const Vector &other) const
Calculate the product of this vector and other element-wise.
Definition: vector.h:94
constexpr T dot(const Vector< T, Rows > &other) const
Compute the dot product.
Definition: vector.h:174
constexpr const T & z() const
Convenience function to access the third element of the vector.
Definition: vector.h:193
Vector & operator*=(T scalar)
Multiply this vector by scalar element-wise.
Definition: vector.h:139
Vector class.
Definition: vector.h:34
constexpr Vector max(T scalar) const
Calculate the maximum of this vector and scalar element-wise.
Definition: vector.h:169
constexpr Vector(T scalar)
Construct a vector filled with a scalar value.
Definition: vector.h:39
Vector & operator+=(T scalar)
Add scalar element-wise to this vector.
Definition: vector.h:119
Vector & operator-=(T scalar)
Subtract scalar element-wise from this vector.
Definition: vector.h:129
constexpr Vector operator/(const Vector &other) const
Calculate the quotient of this vector and other element-wise.
Definition: vector.h:104