18 #include <libcamera/base/span.h> 30 template<
typename T,
unsigned int Rows,
31 std::enable_if_t<std::is_arithmetic_v<T>> * =
nullptr>
33 template<
typename T,
unsigned int Rows>
38 constexpr
Vector() =
default;
45 constexpr
Vector(
const std::array<T, Rows> &data)
47 for (
unsigned int i = 0; i < Rows; i++)
66 for (
unsigned int i = 0; i < Rows; i++)
73 return apply(*
this, other, std::plus<>{});
78 return apply(*
this, scalar, std::plus<>{});
83 return apply(*
this, other, std::minus<>{});
88 return apply(*
this, scalar, std::minus<>{});
93 return apply(*
this, other, std::multiplies<>{});
98 return apply(*
this, scalar, std::multiplies<>{});
103 return apply(*
this, other, std::divides<>{});
108 return apply(*
this, scalar, std::divides<>{});
113 return apply(other, [](T a, T
b) {
return a +
b; });
118 return apply(scalar, [](T a, T
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(*
this, other, [](T a, T
b) {
return std::min(a, b); });
158 return apply(*
this, scalar, [](T a, T
b) {
return std::min(a, b); });
163 return apply(*
this, other, [](T a, T
b) {
return std::max(a, b); });
168 return apply(*
this, scalar, [](T a, T
b) -> T {
return std::max(a, b); });
174 for (
unsigned int i = 0; i < Rows; i++)
175 ret += data_[i] * other[i];
180 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 1>>
182 constexpr
const T &
x()
const {
return data_[0]; }
184 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 2>>
186 constexpr
const T &
y()
const {
return data_[1]; }
188 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 3>>
190 constexpr
const T &
z()
const {
return data_[2]; }
192 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 1>>
194 constexpr T &
x() {
return data_[0]; }
196 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 2>>
198 constexpr T &
y() {
return data_[1]; }
200 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 3>>
202 constexpr T &
z() {
return data_[2]; }
205 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 1>>
207 constexpr
const T &
r()
const {
return data_[0]; }
209 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 2>>
211 constexpr
const T &
g()
const {
return data_[1]; }
213 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 3>>
215 constexpr
const T &
b()
const {
return data_[2]; }
217 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 1>>
219 constexpr T &
r() {
return data_[0]; }
221 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 2>>
223 constexpr T &
g() {
return data_[1]; }
225 template<
bool Dependent = false,
typename = std::enable_if_t<Dependent || Rows >= 3>>
227 constexpr T &
b() {
return data_[2]; }
232 for (
unsigned int i = 0; i < Rows; i++)
233 ret += data_[i] * data_[i];
242 template<
typename R = T>
245 return std::accumulate(data_.begin(), data_.end(), R{});
249 template<
class BinaryOp>
253 std::transform(lhs.data_.begin(), lhs.data_.end(),
254 rhs.data_.begin(), result.data_.begin(),
260 template<
class BinaryOp>
261 static constexpr
Vector apply(
const Vector &lhs, T rhs, BinaryOp op)
264 std::transform(lhs.data_.begin(), lhs.data_.end(),
265 result.data_.begin(),
266 [&op, rhs](T v) {
return op(v, rhs); });
271 template<
class BinaryOp>
274 auto itOther = other.data_.begin();
275 std::for_each(data_.begin(), data_.end(),
276 [&op, &itOther](T &v) { v = op(v, *itOther++); });
281 template<
class BinaryOp>
282 Vector &apply(T scalar, BinaryOp op)
284 std::for_each(data_.begin(), data_.end(),
285 [&op, scalar](T &v) { v = op(v, scalar); });
290 std::array<T, Rows> data_;
296 template<
typename T,
unsigned int Rows,
unsigned int Cols>
301 for (
unsigned int i = 0; i < Rows; i++) {
303 for (
unsigned int j = 0; j < Cols; j++)
304 sum += m[i][j] * v[j];
311 template<
typename T,
unsigned int Rows>
314 for (
unsigned int i = 0; i < Rows; i++) {
315 if (lhs[i] != rhs[i])
322 template<
typename T,
unsigned int Rows>
325 return !(lhs == rhs);
329 bool vectorValidateYaml(
const YamlObject &obj,
unsigned int size);
335 template<
typename T,
unsigned int Rows>
336 std::ostream &operator<<(std::ostream &out, const ipa::Vector<T, Rows> &v)
339 for (
unsigned int i = 0; i < Rows; i++) {
341 out << ((i + 1 < Rows) ?
", " :
" ");
348 template<
typename T,
unsigned int Rows>
349 struct YamlObject::Getter<ipa::
Vector<T, Rows>> {
350 std::optional<ipa::Vector<T, Rows>>
get(
const YamlObject &obj)
const 352 if (!ipa::vectorValidateYaml(obj, Rows))
359 const auto value = entry.get<T>();
362 vector[i++] = *value;
T & operator[](size_t i)
Index to an element in the vector.
Definition: vector.h:57
constexpr Vector operator+(const Vector &other) const
Calculate the sum of this vector and other element-wise.
Definition: vector.h:71
constexpr const T & r() const
Convenience function to access the first element of the vector.
Definition: vector.h:207
const T & operator[](size_t i) const
Index to an element in the vector.
Definition: vector.h:51
Vector & operator/=(T scalar)
Divide this vector by scalar element-wise.
Definition: vector.h:146
constexpr Vector operator+(T scalar) const
Calculate the sum of this vector and scalar element-wise.
Definition: vector.h:76
constexpr Vector operator-(const Vector &other) const
Calculate the difference of this vector and other element-wise.
Definition: vector.h:81
Top-level libcamera namespace.
Definition: backtrace.h:17
Vector & operator+=(T scalar)
Add scalar element-wise to this vector.
Definition: vector.h:116
constexpr T & y()
Convenience function to access the second element of the vector.
Definition: vector.h:198
ListAdapter asList() const
Wrap a list YamlObject in an adapter that exposes iterators.
Definition: yaml_parser.h:206
Vector & operator-=(T scalar)
Subtract scalar element-wise from this vector.
Definition: vector.h:126
constexpr double length2() const
Get the squared length of the vector.
Definition: vector.h:229
bool operator==(const Vector< T, Rows > &lhs, const Vector< T, Rows > &rhs)
Compare vectors for equality.
Definition: vector.h:312
constexpr const T & x() const
Convenience function to access the first element of the vector.
Definition: vector.h:182
Vector & operator/=(const Vector &other)
Divide this vector by other element-wise.
Definition: vector.h:141
#define ASSERT(condition)
Abort program execution if assertion fails.
constexpr const T & z() const
Convenience function to access the third element of the vector.
Definition: vector.h:190
Matrix class.
Definition: matrix.h:28
Vector & operator+=(const Vector &other)
Add other element-wise to this vector.
Definition: vector.h:111
constexpr T & g()
Convenience function to access the second element of the vector.
Definition: vector.h:223
constexpr double length() const
Get the length of the vector.
Definition: vector.h:237
#define LOG_DECLARE_CATEGORY(name)
Declare a category of log messages.
constexpr const T & g() const
Convenience function to access the second element of the vector.
Definition: vector.h:211
constexpr const T & y() const
Convenience function to access the second element of the vector.
Definition: vector.h:186
constexpr T & r()
Convenience function to access the first element of the vector.
Definition: vector.h:219
constexpr Vector max(const Vector &other) const
Calculate the maximum of this vector and other element-wise.
Definition: vector.h:161
constexpr Vector()=default
Construct an uninitialized vector.
constexpr Vector(const std::array< T, Rows > &data)
Construct vector from supplied data.
Definition: vector.h:45
constexpr Vector operator-(T scalar) const
Calculate the difference of this vector and scalar element-wise.
Definition: vector.h:86
Vector class.
Definition: vector.h:35
Vector & operator*=(const Vector &other)
Multiply this vector by other element-wise.
Definition: vector.h:131
constexpr Vector< T, Rows > operator-() const
Negate a Vector by negating both all of its coordinates.
Definition: vector.h:63
constexpr const T & b() const
Convenience function to access the third element of the vector.
Definition: vector.h:215
constexpr Vector max(T scalar) const
Calculate the maximum of this vector and scalar element-wise.
Definition: vector.h:166
A class representing the tree structure of the YAML content.
Definition: yaml_parser.h:27
constexpr T & x()
Convenience function to access the first element of the vector.
Definition: vector.h:194
constexpr Vector(T scalar)
Construct a vector filled with a scalar value.
Definition: vector.h:40
bool operator!=(const Vector< T, Rows > &lhs, const Vector< T, Rows > &rhs)
Compare vectors for inequality.
Definition: vector.h:323
constexpr Vector operator/(T scalar) const
Calculate the quotient of this vector and scalar element-wise.
Definition: vector.h:106
constexpr R sum() const
Calculate the sum of all the vector elements.
Definition: vector.h:243
constexpr Vector operator*(const Vector &other) const
Calculate the product of this vector and other element-wise.
Definition: vector.h:91
constexpr Vector min(const Vector &other) const
Calculate the minimum of this vector and other element-wise.
Definition: vector.h:151
constexpr Vector min(T scalar) const
Calculate the minimum of this vector and scalar element-wise.
Definition: vector.h:156
constexpr T & z()
Convenience function to access the third element of the vector.
Definition: vector.h:202
Vector & operator-=(const Vector &other)
Subtract other element-wise from this vector.
Definition: vector.h:121
Vector & operator*=(T scalar)
Multiply this vector by scalar element-wise.
Definition: vector.h:136
constexpr T & b()
Convenience function to access the third element of the vector.
Definition: vector.h:227
constexpr Vector operator*(T scalar) const
Calculate the product of this vector and scalar element-wise.
Definition: vector.h:96
constexpr Vector operator/(const Vector &other) const
Calculate the quotient of this vector and other element-wise.
Definition: vector.h:101
constexpr T dot(const Vector< T, Rows > &other) const
Compute the dot product.
Definition: vector.h:171