ipa: libipa: vector: Add r(), g() and b() accessors

The Vector class can be useful to represent RGB pixel values. Add r(),
g() and b() accessors, similar to x(), y() and z(), along with an RGB
type that aliases Vector<T, 3>.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Milan Zamazal <mzamazal@redhat.com>
This commit is contained in:
Laurent Pinchart 2024-11-16 21:02:52 +02:00
parent 6089b5bc94
commit bc10ffca97
2 changed files with 66 additions and 0 deletions

View file

@ -126,6 +126,39 @@ namespace ipa {
* \copydoc Vector::z() * \copydoc Vector::z()
*/ */
/**
* \fn constexpr T &Vector::r()
* \brief Convenience function to access the first element of the vector
* \return The first element of the vector
*/
/**
* \fn constexpr T &Vector::g()
* \brief Convenience function to access the second element of the vector
* \return The second element of the vector
*/
/**
* \fn constexpr T &Vector::b()
* \brief Convenience function to access the third element of the vector
* \return The third element of the vector
*/
/**
* \fn constexpr const T &Vector::r() const
* \copydoc Vector::r()
*/
/**
* \fn constexpr const T &Vector::g() const
* \copydoc Vector::g()
*/
/**
* \fn constexpr const T &Vector::b() const
* \copydoc Vector::b()
*/
/** /**
* \fn Vector::length2() * \fn Vector::length2()
* \brief Get the squared length of the vector * \brief Get the squared length of the vector
@ -149,6 +182,11 @@ namespace ipa {
* \return Product of matrix \a m and vector \a v * \return Product of matrix \a m and vector \a v
*/ */
/**
* \typedef RGB
* \brief A Vector of 3 elements representing an RGB pixel value
*/
/** /**
* \fn bool operator==(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs) * \fn bool operator==(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs)
* \brief Compare vectors for equality * \brief Compare vectors for equality

View file

@ -126,6 +126,31 @@ public:
#endif /* __DOXYGEN__ */ #endif /* __DOXYGEN__ */
constexpr T &z() { return data_[2]; } constexpr T &z() { return data_[2]; }
#ifndef __DOXYGEN__
template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 1>>
#endif /* __DOXYGEN__ */
constexpr const T &r() const { return data_[0]; }
#ifndef __DOXYGEN__
template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 2>>
#endif /* __DOXYGEN__ */
constexpr const T &g() const { return data_[1]; }
#ifndef __DOXYGEN__
template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 3>>
#endif /* __DOXYGEN__ */
constexpr const T &b() const { return data_[2]; }
#ifndef __DOXYGEN__
template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 1>>
#endif /* __DOXYGEN__ */
constexpr T &r() { return data_[0]; }
#ifndef __DOXYGEN__
template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 2>>
#endif /* __DOXYGEN__ */
constexpr T &g() { return data_[1]; }
#ifndef __DOXYGEN__
template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 3>>
#endif /* __DOXYGEN__ */
constexpr T &b() { return data_[2]; }
constexpr double length2() const constexpr double length2() const
{ {
double ret = 0; double ret = 0;
@ -143,6 +168,9 @@ private:
std::array<T, Rows> data_; std::array<T, Rows> data_;
}; };
template<typename T>
using RGB = Vector<T, 3>;
template<typename T, unsigned int Rows, unsigned int Cols> template<typename T, unsigned int Rows, unsigned int Cols>
Vector<T, Rows> operator*(const Matrix<T, Rows, Cols> &m, const Vector<T, Cols> &v) Vector<T, Rows> operator*(const Matrix<T, Rows, Cols> &m, const Vector<T, Cols> &v)
{ {