/* SPDX-License-Identifier: LGPL-2.1-or-later */ /* * Copyright (C) 2024, Paul Elder * * Vector and related operations */ #pragma once #include #include #include #include #include #include #include "libcamera/internal/yaml_parser.h" #include "matrix.h" namespace libcamera { LOG_DECLARE_CATEGORY(Vector) namespace ipa { #ifndef __DOXYGEN__ template> * = nullptr> #else template #endif /* __DOXYGEN__ */ class Vector { public: constexpr Vector() = default; constexpr explicit Vector(T scalar) { data_.fill(scalar); } constexpr Vector(const std::array &data) { for (unsigned int i = 0; i < Rows; i++) data_[i] = data[i]; } const T &operator[](size_t i) const { ASSERT(i < data_.size()); return data_[i]; } T &operator[](size_t i) { ASSERT(i < data_.size()); return data_[i]; } constexpr Vector operator-() const { Vector ret; for (unsigned int i = 0; i < Rows; i++) ret[i] = -data_[i]; return ret; } constexpr Vector operator-(const Vector &other) const { Vector ret; for (unsigned int i = 0; i < Rows; i++) ret[i] = data_[i] - other[i]; return ret; } constexpr Vector operator+(const Vector &other) const { Vector ret; for (unsigned int i = 0; i < Rows; i++) ret[i] = data_[i] + other[i]; return ret; } constexpr T operator*(const Vector &other) const { T ret = 0; for (unsigned int i = 0; i < Rows; i++) ret += data_[i] * other[i]; return ret; } constexpr Vector operator*(T factor) const { Vector ret; for (unsigned int i = 0; i < Rows; i++) ret[i] = data_[i] * factor; return ret; } constexpr Vector operator/(T factor) const { Vector ret; for (unsigned int i = 0; i < Rows; i++) ret[i] = data_[i] / factor; return ret; } #ifndef __DOXYGEN__ template= 1>> #endif /* __DOXYGEN__ */ constexpr const T &x() const { return data_[0]; } #ifndef __DOXYGEN__ template= 2>> #endif /* __DOXYGEN__ */ constexpr const T &y() const { return data_[1]; } #ifndef __DOXYGEN__ template= 3>> #endif /* __DOXYGEN__ */ constexpr const T &z() const { return data_[2]; } #ifndef __DOXYGEN__ template= 1>> #endif /* __DOXYGEN__ */ constexpr T &x() { return data_[0]; } #ifndef __DOXYGEN__ template= 2>> #endif /* __DOXYGEN__ */ constexpr T &y() { return data_[1]; } #ifndef __DOXYGEN__ template= 3>> #endif /* __DOXYGEN__ */ constexpr T &z() { return data_[2]; } #ifndef __DOXYGEN__ template= 1>> #endif /* __DOXYGEN__ */ constexpr const T &r() const { return data_[0]; } #ifndef __DOXYGEN__ template= 2>> #endif /* __DOXYGEN__ */ constexpr const T &g() const { return data_[1]; } #ifndef __DOXYGEN__ template= 3>> #endif /* __DOXYGEN__ */ constexpr const T &b() const { return data_[2]; } #ifndef __DOXYGEN__ template= 1>> #endif /* __DOXYGEN__ */ constexpr T &r() { return data_[0]; } #ifndef __DOXYGEN__ template= 2>> #endif /* __DOXYGEN__ */ constexpr T &g() { return data_[1]; } #ifndef __DOXYGEN__ template= 3>> #endif /* __DOXYGEN__ */ constexpr T &b() { return data_[2]; } constexpr double length2() const { double ret = 0; for (unsigned int i = 0; i < Rows; i++) ret += data_[i] * data_[i]; return ret; } constexpr double length() const { return std::sqrt(length2()); } private: std::array data_; }; template using RGB = Vector; template Vector operator*(const Matrix &m, const Vector &v) { Vector result; for (unsigned int i = 0; i < Rows; i++) { T sum = 0; for (unsigned int j = 0; j < Cols; j++) sum += m[i][j] * v[j]; result[i] = sum; } return result; } template bool operator==(const Vector &lhs, const Vector &rhs) { for (unsigned int i = 0; i < Rows; i++) { if (lhs[i] != rhs[i]) return false; } return true; } template bool operator!=(const Vector &lhs, const Vector &rhs) { return !(lhs == rhs); } #ifndef __DOXYGEN__ bool vectorValidateYaml(const YamlObject &obj, unsigned int size); #endif /* __DOXYGEN__ */ } /* namespace ipa */ #ifndef __DOXYGEN__ template std::ostream &operator<<(std::ostream &out, const ipa::Vector &v) { out << "Vector { "; for (unsigned int i = 0; i < Rows; i++) { out << v[i]; out << ((i + 1 < Rows) ? ", " : " "); } out << " }"; return out; } template struct YamlObject::Getter> { std::optional> get(const YamlObject &obj) const { if (!ipa::vectorValidateYaml(obj, Rows)) return std::nullopt; ipa::Vector vector; unsigned int i = 0; for (const YamlObject &entry : obj.asList()) { const auto value = entry.get(); if (!value) return std::nullopt; vector[i++] = *value; } return vector; } }; #endif /* __DOXYGEN__ */ } /* namespace libcamera */