[{"id":33326,"web_url":"https://patchwork.libcamera.org/comment/33326/","msgid":"<173926671103.2293020.9975184227417336105@ping.linuxembedded.co.uk>","date":"2025-02-11T09:38:31","subject":"Re: [PATCH 1/4] libcamera: Copy Vector class files from libipa","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Stefan Klug (2025-02-06 14:10:08)\n> Prepare the move of the Vector class from libipa to libcamera by copying\n> the relevant files into the corresponding libcamera directories. The\n> files are copied without modification.\n\nNo modification makes this easy:\n\nAcked-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\nBut I think there are a few checkstyle issues in this file. It\nmight be a good time to clean the file up as we're here.\n\n29ce46bb9f92ed742df64305e68afee811ef7605 libcamera: Copy Vector class files from libipa\n---------------------------------------------------------------------------------------\nHeader include/libcamera/internal/vector.h added without corresponding update to include/libcamera/internal/meson.build\n--- include/libcamera/internal/vector.h\n+++ include/libcamera/internal/vector.h\n@@ -179,52 +179,88 @@\n #ifndef __DOXYGEN__\n \ttemplate<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 1>>\n #endif /* __DOXYGEN__ */\n-\tconstexpr const T &x() const { return data_[0]; }\n+\tconstexpr const T &x() const\n+\t{\n+\t\treturn data_[0];\n+\t}\n #ifndef __DOXYGEN__\n \ttemplate<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 2>>\n #endif /* __DOXYGEN__ */\n-\tconstexpr const T &y() const { return data_[1]; }\n+\tconstexpr const T &y() const\n+\t{\n+\t\treturn data_[1];\n+\t}\n #ifndef __DOXYGEN__\n \ttemplate<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 3>>\n #endif /* __DOXYGEN__ */\n-\tconstexpr const T &z() const { return data_[2]; }\n+\tconstexpr const T &z() const\n+\t{\n+\t\treturn data_[2];\n+\t}\n #ifndef __DOXYGEN__\n \ttemplate<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 1>>\n #endif /* __DOXYGEN__ */\n-\tconstexpr T &x() { return data_[0]; }\n+\tconstexpr T &x()\n+\t{\n+\t\treturn data_[0];\n+\t}\n #ifndef __DOXYGEN__\n \ttemplate<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 2>>\n #endif /* __DOXYGEN__ */\n-\tconstexpr T &y() { return data_[1]; }\n+\tconstexpr T &y()\n+\t{\n+\t\treturn data_[1];\n+\t}\n #ifndef __DOXYGEN__\n \ttemplate<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 3>>\n #endif /* __DOXYGEN__ */\n-\tconstexpr T &z() { return data_[2]; }\n+\tconstexpr T &z()\n+\t{\n+\t\treturn data_[2];\n+\t}\n \n #ifndef __DOXYGEN__\n \ttemplate<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 1>>\n #endif /* __DOXYGEN__ */\n-\tconstexpr const T &r() const { return data_[0]; }\n+\tconstexpr const T &r() const\n+\t{\n+\t\treturn data_[0];\n+\t}\n #ifndef __DOXYGEN__\n \ttemplate<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 2>>\n #endif /* __DOXYGEN__ */\n-\tconstexpr const T &g() const { return data_[1]; }\n+\tconstexpr const T &g() const\n+\t{\n+\t\treturn data_[1];\n+\t}\n #ifndef __DOXYGEN__\n \ttemplate<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 3>>\n #endif /* __DOXYGEN__ */\n-\tconstexpr const T &b() const { return data_[2]; }\n+\tconstexpr const T &b() const\n+\t{\n+\t\treturn data_[2];\n+\t}\n #ifndef __DOXYGEN__\n \ttemplate<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 1>>\n #endif /* __DOXYGEN__ */\n-\tconstexpr T &r() { return data_[0]; }\n+\tconstexpr T &r()\n+\t{\n+\t\treturn data_[0];\n+\t}\n #ifndef __DOXYGEN__\n \ttemplate<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 2>>\n #endif /* __DOXYGEN__ */\n-\tconstexpr T &g() { return data_[1]; }\n+\tconstexpr T &g()\n+\t{\n+\t\treturn data_[1];\n+\t}\n #ifndef __DOXYGEN__\n \ttemplate<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 3>>\n #endif /* __DOXYGEN__ */\n-\tconstexpr T &b() { return data_[2]; }\n+\tconstexpr T &b()\n+\t{\n+\t\treturn data_[2];\n+\t}\n \n \tconstexpr double length2() const\n \t{\n--- test/vector.cpp\n+++ test/vector.cpp\n@@ -14,12 +14,12 @@\n \n using namespace libcamera::ipa;\n \n-#define ASSERT_EQ(a, b)\t\t\t\t\t\t\t\\\n-if ((a) != (b)) {\t\t\t\t\t\t\t\\\n-\tstd::cout << #a \" != \" #b << \" (line \" << __LINE__ << \")\"\t\\\n-\t\t  << std::endl;\t\t\t\t\t\t\\\n-\treturn TestFail;\t\t\t\t\t\t\\\n-}\n+#define ASSERT_EQ(a, b)                                                   \\\n+\tif ((a) != (b)) {                                                 \\\n+\t\tstd::cout << #a \" != \" #b << \" (line \" << __LINE__ << \")\" \\\n+\t\t\t  << std::endl;                                   \\\n+\t\treturn TestFail;                                          \\\n+\t}\n \n class VectorTest : public Test\n {\n@@ -35,7 +35,7 @@\n \t\tASSERT_EQ(v1.length(), 0.0);\n \t\tASSERT_EQ(v1.length2(), 0.0);\n \n-\t\tVector<double, 3> v2{{ 1.0, 4.0, 8.0 }};\n+\t\tVector<double, 3> v2{ { 1.0, 4.0, 8.0 } };\n \n \t\tASSERT_EQ(v2[0], 1.0);\n \t\tASSERT_EQ(v2[1], 4.0);\n@@ -57,41 +57,41 @@\n \n \t\tASSERT_EQ(v2, v3);\n \n-\t\tv3 = Vector<double, 3>{{ 4.0, 4.0, 4.0 }};\n+\t\tv3 = Vector<double, 3>{ { 4.0, 4.0, 4.0 } };\n \n-\t\tASSERT_EQ(v2 + v3, (Vector<double, 3>{{ 5.0, 8.0, 12.0 }}));\n-\t\tASSERT_EQ(v2 + 4.0, (Vector<double, 3>{{ 5.0, 8.0, 12.0 }}));\n-\t\tASSERT_EQ(v2 - v3, (Vector<double, 3>{{ -3.0, 0.0, 4.0 }}));\n-\t\tASSERT_EQ(v2 - 4.0, (Vector<double, 3>{{ -3.0, 0.0, 4.0 }}));\n-\t\tASSERT_EQ(v2 * v3, (Vector<double, 3>{{ 4.0, 16.0, 32.0 }}));\n-\t\tASSERT_EQ(v2 * 4.0, (Vector<double, 3>{{ 4.0, 16.0, 32.0 }}));\n-\t\tASSERT_EQ(v2 / v3, (Vector<double, 3>{{ 0.25, 1.0, 2.0 }}));\n-\t\tASSERT_EQ(v2 / 4.0, (Vector<double, 3>{{ 0.25, 1.0, 2.0 }}));\n+\t\tASSERT_EQ(v2 + v3, (Vector<double, 3>{ { 5.0, 8.0, 12.0 } }));\n+\t\tASSERT_EQ(v2 + 4.0, (Vector<double, 3>{ { 5.0, 8.0, 12.0 } }));\n+\t\tASSERT_EQ(v2 - v3, (Vector<double, 3>{ { -3.0, 0.0, 4.0 } }));\n+\t\tASSERT_EQ(v2 - 4.0, (Vector<double, 3>{ { -3.0, 0.0, 4.0 } }));\n+\t\tASSERT_EQ(v2 * v3, (Vector<double, 3>{ { 4.0, 16.0, 32.0 } }));\n+\t\tASSERT_EQ(v2 * 4.0, (Vector<double, 3>{ { 4.0, 16.0, 32.0 } }));\n+\t\tASSERT_EQ(v2 / v3, (Vector<double, 3>{ { 0.25, 1.0, 2.0 } }));\n+\t\tASSERT_EQ(v2 / 4.0, (Vector<double, 3>{ { 0.25, 1.0, 2.0 } }));\n \n-\t\tASSERT_EQ(v2.min(v3), (Vector<double, 3>{{ 1.0, 4.0, 4.0 }}));\n-\t\tASSERT_EQ(v2.min(4.0), (Vector<double, 3>{{ 1.0, 4.0, 4.0 }}));\n-\t\tASSERT_EQ(v2.max(v3), (Vector<double, 3>{{ 4.0, 4.0, 8.0 }}));\n-\t\tASSERT_EQ(v2.max(4.0), (Vector<double, 3>{{ 4.0, 4.0, 8.0 }}));\n+\t\tASSERT_EQ(v2.min(v3), (Vector<double, 3>{ { 1.0, 4.0, 4.0 } }));\n+\t\tASSERT_EQ(v2.min(4.0), (Vector<double, 3>{ { 1.0, 4.0, 4.0 } }));\n+\t\tASSERT_EQ(v2.max(v3), (Vector<double, 3>{ { 4.0, 4.0, 8.0 } }));\n+\t\tASSERT_EQ(v2.max(4.0), (Vector<double, 3>{ { 4.0, 4.0, 8.0 } }));\n \n \t\tASSERT_EQ(v2.dot(v3), 52.0);\n \n \t\tv2 += v3;\n-\t\tASSERT_EQ(v2, (Vector<double, 3>{{ 5.0, 8.0, 12.0 }}));\n+\t\tASSERT_EQ(v2, (Vector<double, 3>{ { 5.0, 8.0, 12.0 } }));\n \t\tv2 -= v3;\n-\t\tASSERT_EQ(v2, (Vector<double, 3>{{ 1.0, 4.0, 8.0 }}));\n+\t\tASSERT_EQ(v2, (Vector<double, 3>{ { 1.0, 4.0, 8.0 } }));\n \t\tv2 *= v3;\n-\t\tASSERT_EQ(v2, (Vector<double, 3>{{ 4.0, 16.0, 32.0 }}));\n+\t\tASSERT_EQ(v2, (Vector<double, 3>{ { 4.0, 16.0, 32.0 } }));\n \t\tv2 /= v3;\n-\t\tASSERT_EQ(v2, (Vector<double, 3>{{ 1.0, 4.0, 8.0 }}));\n+\t\tASSERT_EQ(v2, (Vector<double, 3>{ { 1.0, 4.0, 8.0 } }));\n \n \t\tv2 += 4.0;\n-\t\tASSERT_EQ(v2, (Vector<double, 3>{{ 5.0, 8.0, 12.0 }}));\n+\t\tASSERT_EQ(v2, (Vector<double, 3>{ { 5.0, 8.0, 12.0 } }));\n \t\tv2 -= 4.0;\n-\t\tASSERT_EQ(v2, (Vector<double, 3>{{ 1.0, 4.0, 8.0 }}));\n+\t\tASSERT_EQ(v2, (Vector<double, 3>{ { 1.0, 4.0, 8.0 } }));\n \t\tv2 *= 4.0;\n-\t\tASSERT_EQ(v2, (Vector<double, 3>{{ 4.0, 16.0, 32.0 }}));\n+\t\tASSERT_EQ(v2, (Vector<double, 3>{ { 4.0, 16.0, 32.0 } }));\n \t\tv2 /= 4.0;\n-\t\tASSERT_EQ(v2, (Vector<double, 3>{{ 1.0, 4.0, 8.0 }}));\n+\t\tASSERT_EQ(v2, (Vector<double, 3>{ { 1.0, 4.0, 8.0 } }));\n \n \t\treturn TestPass;\n \t}\n---\n5 potential issues detected, please review\n\nTo reduce future checkstyle noise, I'd be fine applying that diff to\nthis commit as part of the move (documenting that only\ncheckstyle/formatting updates are made during the copy).\n\n\n\n\n> \n> Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>\n> ---\n>  include/libcamera/internal/vector.h | 370 ++++++++++++++++++++++++++++\n>  src/libcamera/vector.cpp            | 351 ++++++++++++++++++++++++++\n>  test/vector.cpp                     | 100 ++++++++\n>  3 files changed, 821 insertions(+)\n>  create mode 100644 include/libcamera/internal/vector.h\n>  create mode 100644 src/libcamera/vector.cpp\n>  create mode 100644 test/vector.cpp\n> \n> diff --git a/include/libcamera/internal/vector.h b/include/libcamera/internal/vector.h\n> new file mode 100644\n> index 000000000000..fe33c9d6fbd1\n> --- /dev/null\n> +++ b/include/libcamera/internal/vector.h\n> @@ -0,0 +1,370 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2024, Paul Elder <paul.elder@ideasonboard.com>\n> + *\n> + * Vector and related operations\n> + */\n> +#pragma once\n> +\n> +#include <algorithm>\n> +#include <array>\n> +#include <cmath>\n> +#include <functional>\n> +#include <numeric>\n> +#include <optional>\n> +#include <ostream>\n> +\n> +#include <libcamera/base/log.h>\n> +#include <libcamera/base/span.h>\n> +\n> +#include \"libcamera/internal/matrix.h\"\n> +#include \"libcamera/internal/yaml_parser.h\"\n> +\n> +namespace libcamera {\n> +\n> +LOG_DECLARE_CATEGORY(Vector)\n> +\n> +namespace ipa {\n> +\n> +#ifndef __DOXYGEN__\n> +template<typename T, unsigned int Rows,\n> +        std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr>\n> +#else\n> +template<typename T, unsigned int Rows>\n> +#endif /* __DOXYGEN__ */\n> +class Vector\n> +{\n> +public:\n> +       constexpr Vector() = default;\n> +\n> +       constexpr explicit Vector(T scalar)\n> +       {\n> +               data_.fill(scalar);\n> +       }\n> +\n> +       constexpr Vector(const std::array<T, Rows> &data)\n> +       {\n> +               for (unsigned int i = 0; i < Rows; i++)\n> +                       data_[i] = data[i];\n> +       }\n> +\n> +       const T &operator[](size_t i) const\n> +       {\n> +               ASSERT(i < data_.size());\n> +               return data_[i];\n> +       }\n> +\n> +       T &operator[](size_t i)\n> +       {\n> +               ASSERT(i < data_.size());\n> +               return data_[i];\n> +       }\n> +\n> +       constexpr Vector<T, Rows> operator-() const\n> +       {\n> +               Vector<T, Rows> ret;\n> +               for (unsigned int i = 0; i < Rows; i++)\n> +                       ret[i] = -data_[i];\n> +               return ret;\n> +       }\n> +\n> +       constexpr Vector operator+(const Vector &other) const\n> +       {\n> +               return apply(*this, other, std::plus<>{});\n> +       }\n> +\n> +       constexpr Vector operator+(T scalar) const\n> +       {\n> +               return apply(*this, scalar, std::plus<>{});\n> +       }\n> +\n> +       constexpr Vector operator-(const Vector &other) const\n> +       {\n> +               return apply(*this, other, std::minus<>{});\n> +       }\n> +\n> +       constexpr Vector operator-(T scalar) const\n> +       {\n> +               return apply(*this, scalar, std::minus<>{});\n> +       }\n> +\n> +       constexpr Vector operator*(const Vector &other) const\n> +       {\n> +               return apply(*this, other, std::multiplies<>{});\n> +       }\n> +\n> +       constexpr Vector operator*(T scalar) const\n> +       {\n> +               return apply(*this, scalar, std::multiplies<>{});\n> +       }\n> +\n> +       constexpr Vector operator/(const Vector &other) const\n> +       {\n> +               return apply(*this, other, std::divides<>{});\n> +       }\n> +\n> +       constexpr Vector operator/(T scalar) const\n> +       {\n> +               return apply(*this, scalar, std::divides<>{});\n> +       }\n> +\n> +       Vector &operator+=(const Vector &other)\n> +       {\n> +               return apply(other, [](T a, T b) { return a + b; });\n> +       }\n> +\n> +       Vector &operator+=(T scalar)\n> +       {\n> +               return apply(scalar, [](T a, T b) { return a + b; });\n> +       }\n> +\n> +       Vector &operator-=(const Vector &other)\n> +       {\n> +               return apply(other, [](T a, T b) { return a - b; });\n> +       }\n> +\n> +       Vector &operator-=(T scalar)\n> +       {\n> +               return apply(scalar, [](T a, T b) { return a - b; });\n> +       }\n> +\n> +       Vector &operator*=(const Vector &other)\n> +       {\n> +               return apply(other, [](T a, T b) { return a * b; });\n> +       }\n> +\n> +       Vector &operator*=(T scalar)\n> +       {\n> +               return apply(scalar, [](T a, T b) { return a * b; });\n> +       }\n> +\n> +       Vector &operator/=(const Vector &other)\n> +       {\n> +               return apply(other, [](T a, T b) { return a / b; });\n> +       }\n> +\n> +       Vector &operator/=(T scalar)\n> +       {\n> +               return apply(scalar, [](T a, T b) { return a / b; });\n> +       }\n> +\n> +       constexpr Vector min(const Vector &other) const\n> +       {\n> +               return apply(*this, other, [](T a, T b) { return std::min(a, b); });\n> +       }\n> +\n> +       constexpr Vector min(T scalar) const\n> +       {\n> +               return apply(*this, scalar, [](T a, T b) { return std::min(a, b); });\n> +       }\n> +\n> +       constexpr Vector max(const Vector &other) const\n> +       {\n> +               return apply(*this, other, [](T a, T b) { return std::max(a, b); });\n> +       }\n> +\n> +       constexpr Vector max(T scalar) const\n> +       {\n> +               return apply(*this, scalar, [](T a, T b) -> T { return std::max(a, b); });\n> +       }\n> +\n> +       constexpr T dot(const Vector<T, Rows> &other) const\n> +       {\n> +               T ret = 0;\n> +               for (unsigned int i = 0; i < Rows; i++)\n> +                       ret += data_[i] * other[i];\n> +               return ret;\n> +       }\n> +\n> +#ifndef __DOXYGEN__\n> +       template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 1>>\n> +#endif /* __DOXYGEN__ */\n> +       constexpr const T &x() const { return data_[0]; }\n> +#ifndef __DOXYGEN__\n> +       template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 2>>\n> +#endif /* __DOXYGEN__ */\n> +       constexpr const T &y() const { return data_[1]; }\n> +#ifndef __DOXYGEN__\n> +       template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 3>>\n> +#endif /* __DOXYGEN__ */\n> +       constexpr const T &z() const { return data_[2]; }\n> +#ifndef __DOXYGEN__\n> +       template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 1>>\n> +#endif /* __DOXYGEN__ */\n> +       constexpr T &x() { return data_[0]; }\n> +#ifndef __DOXYGEN__\n> +       template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 2>>\n> +#endif /* __DOXYGEN__ */\n> +       constexpr T &y() { return data_[1]; }\n> +#ifndef __DOXYGEN__\n> +       template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 3>>\n> +#endif /* __DOXYGEN__ */\n> +       constexpr T &z() { return data_[2]; }\n> +\n> +#ifndef __DOXYGEN__\n> +       template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 1>>\n> +#endif /* __DOXYGEN__ */\n> +       constexpr const T &r() const { return data_[0]; }\n> +#ifndef __DOXYGEN__\n> +       template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 2>>\n> +#endif /* __DOXYGEN__ */\n> +       constexpr const T &g() const { return data_[1]; }\n> +#ifndef __DOXYGEN__\n> +       template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 3>>\n> +#endif /* __DOXYGEN__ */\n> +       constexpr const T &b() const { return data_[2]; }\n> +#ifndef __DOXYGEN__\n> +       template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 1>>\n> +#endif /* __DOXYGEN__ */\n> +       constexpr T &r() { return data_[0]; }\n> +#ifndef __DOXYGEN__\n> +       template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 2>>\n> +#endif /* __DOXYGEN__ */\n> +       constexpr T &g() { return data_[1]; }\n> +#ifndef __DOXYGEN__\n> +       template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 3>>\n> +#endif /* __DOXYGEN__ */\n> +       constexpr T &b() { return data_[2]; }\n> +\n> +       constexpr double length2() const\n> +       {\n> +               double ret = 0;\n> +               for (unsigned int i = 0; i < Rows; i++)\n> +                       ret += data_[i] * data_[i];\n> +               return ret;\n> +       }\n> +\n> +       constexpr double length() const\n> +       {\n> +               return std::sqrt(length2());\n> +       }\n> +\n> +       template<typename R = T>\n> +       constexpr R sum() const\n> +       {\n> +               return std::accumulate(data_.begin(), data_.end(), R{});\n> +       }\n> +\n> +private:\n> +       template<class BinaryOp>\n> +       static constexpr Vector apply(const Vector &lhs, const Vector &rhs, BinaryOp op)\n> +       {\n> +               Vector result;\n> +               std::transform(lhs.data_.begin(), lhs.data_.end(),\n> +                              rhs.data_.begin(), result.data_.begin(),\n> +                              op);\n> +\n> +               return result;\n> +       }\n> +\n> +       template<class BinaryOp>\n> +       static constexpr Vector apply(const Vector &lhs, T rhs, BinaryOp op)\n> +       {\n> +               Vector result;\n> +               std::transform(lhs.data_.begin(), lhs.data_.end(),\n> +                              result.data_.begin(),\n> +                              [&op, rhs](T v) { return op(v, rhs); });\n> +\n> +               return result;\n> +       }\n> +\n> +       template<class BinaryOp>\n> +       Vector &apply(const Vector &other, BinaryOp op)\n> +       {\n> +               auto itOther = other.data_.begin();\n> +               std::for_each(data_.begin(), data_.end(),\n> +                             [&op, &itOther](T &v) { v = op(v, *itOther++); });\n> +\n> +               return *this;\n> +       }\n> +\n> +       template<class BinaryOp>\n> +       Vector &apply(T scalar, BinaryOp op)\n> +       {\n> +               std::for_each(data_.begin(), data_.end(),\n> +                             [&op, scalar](T &v) { v = op(v, scalar); });\n> +\n> +               return *this;\n> +       }\n> +\n> +       std::array<T, Rows> data_;\n> +};\n> +\n> +template<typename T>\n> +using RGB = Vector<T, 3>;\n> +\n> +template<typename T, unsigned int Rows, unsigned int Cols>\n> +Vector<T, Rows> operator*(const Matrix<T, Rows, Cols> &m, const Vector<T, Cols> &v)\n> +{\n> +       Vector<T, Rows> result;\n> +\n> +       for (unsigned int i = 0; i < Rows; i++) {\n> +               T sum = 0;\n> +               for (unsigned int j = 0; j < Cols; j++)\n> +                       sum += m[i][j] * v[j];\n> +               result[i] = sum;\n> +       }\n> +\n> +       return result;\n> +}\n> +\n> +template<typename T, unsigned int Rows>\n> +bool operator==(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs)\n> +{\n> +       for (unsigned int i = 0; i < Rows; i++) {\n> +               if (lhs[i] != rhs[i])\n> +                       return false;\n> +       }\n> +\n> +       return true;\n> +}\n> +\n> +template<typename T, unsigned int Rows>\n> +bool operator!=(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs)\n> +{\n> +       return !(lhs == rhs);\n> +}\n> +\n> +#ifndef __DOXYGEN__\n> +bool vectorValidateYaml(const YamlObject &obj, unsigned int size);\n> +#endif /* __DOXYGEN__ */\n> +\n> +} /* namespace ipa */\n> +\n> +#ifndef __DOXYGEN__\n> +template<typename T, unsigned int Rows>\n> +std::ostream &operator<<(std::ostream &out, const ipa::Vector<T, Rows> &v)\n> +{\n> +       out << \"Vector { \";\n> +       for (unsigned int i = 0; i < Rows; i++) {\n> +               out << v[i];\n> +               out << ((i + 1 < Rows) ? \", \" : \" \");\n> +       }\n> +       out << \" }\";\n> +\n> +       return out;\n> +}\n> +\n> +template<typename T, unsigned int Rows>\n> +struct YamlObject::Getter<ipa::Vector<T, Rows>> {\n> +       std::optional<ipa::Vector<T, Rows>> get(const YamlObject &obj) const\n> +       {\n> +               if (!ipa::vectorValidateYaml(obj, Rows))\n> +                       return std::nullopt;\n> +\n> +               ipa::Vector<T, Rows> vector;\n> +\n> +               unsigned int i = 0;\n> +               for (const YamlObject &entry : obj.asList()) {\n> +                       const auto value = entry.get<T>();\n> +                       if (!value)\n> +                               return std::nullopt;\n> +                       vector[i++] = *value;\n> +               }\n> +\n> +               return vector;\n> +       }\n> +};\n> +#endif /* __DOXYGEN__ */\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/libcamera/vector.cpp b/src/libcamera/vector.cpp\n> new file mode 100644\n> index 000000000000..8019f8cfdc85\n> --- /dev/null\n> +++ b/src/libcamera/vector.cpp\n> @@ -0,0 +1,351 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2024, Paul Elder <paul.elder@ideasonboard.com>\n> + *\n> + * Vector and related operations\n> + */\n> +\n> +#include \"vector.h\"\n> +\n> +#include <libcamera/base/log.h>\n> +\n> +/**\n> + * \\file vector.h\n> + * \\brief Vector class\n> + */\n> +\n> +namespace libcamera {\n> +\n> +LOG_DEFINE_CATEGORY(Vector)\n> +\n> +namespace ipa {\n> +\n> +/**\n> + * \\class Vector\n> + * \\brief Vector class\n> + * \\tparam T Type of numerical values to be stored in the vector\n> + * \\tparam Rows Number of dimension of the vector (= number of elements)\n> + */\n> +\n> +/**\n> + * \\fn Vector::Vector()\n> + * \\brief Construct an uninitialized vector\n> + */\n> +\n> +/**\n> + * \\fn Vector::Vector(T scalar)\n> + * \\brief Construct a vector filled with a \\a scalar value\n> + * \\param[in] scalar The scalar value\n> + */\n> +\n> +/**\n> + * \\fn Vector::Vector(const std::array<T, Rows> &data)\n> + * \\brief Construct vector from supplied data\n> + * \\param data Data from which to construct a vector\n> + *\n> + * The size of \\a data must be equal to the dimension size Rows of the vector.\n> + */\n> +\n> +/**\n> + * \\fn T Vector::operator[](size_t i) const\n> + * \\brief Index to an element in the vector\n> + * \\param i Index of element to retrieve\n> + * \\return Element at index \\a i from the vector\n> + */\n> +\n> +/**\n> + * \\fn T &Vector::operator[](size_t i)\n> + * \\copydoc Vector::operator[](size_t i) const\n> + */\n> +\n> +/**\n> + * \\fn Vector::operator-() const\n> + * \\brief Negate a Vector by negating both all of its coordinates\n> + * \\return The negated vector\n> + */\n> +\n> +/**\n> + * \\fn Vector::operator+(Vector const &other) const\n> + * \\brief Calculate the sum of this vector and \\a other element-wise\n> + * \\param[in] other The other vector\n> + * \\return The element-wise sum of this vector and \\a other\n> + */\n> +\n> +/**\n> + * \\fn Vector::operator+(T scalar) const\n> + * \\brief Calculate the sum of this vector and \\a scalar element-wise\n> + * \\param[in] scalar The scalar\n> + * \\return The element-wise sum of this vector and \\a other\n> + */\n> +\n> +/**\n> + * \\fn Vector::operator-(Vector const &other) const\n> + * \\brief Calculate the difference of this vector and \\a other element-wise\n> + * \\param[in] other The other vector\n> + * \\return The element-wise subtraction of \\a other from this vector\n> + */\n> +\n> +/**\n> + * \\fn Vector::operator-(T scalar) const\n> + * \\brief Calculate the difference of this vector and \\a scalar element-wise\n> + * \\param[in] scalar The scalar\n> + * \\return The element-wise subtraction of \\a scalar from this vector\n> + */\n> +\n> +/**\n> + * \\fn Vector::operator*(const Vector &other) const\n> + * \\brief Calculate the product of this vector and \\a other element-wise\n> + * \\param[in] other The other vector\n> + * \\return The element-wise product of this vector and \\a other\n> + */\n> +\n> +/**\n> + * \\fn Vector::operator*(T scalar) const\n> + * \\brief Calculate the product of this vector and \\a scalar element-wise\n> + * \\param[in] scalar The scalar\n> + * \\return The element-wise product of this vector and \\a scalar\n> + */\n> +\n> +/**\n> + * \\fn Vector::operator/(const Vector &other) const\n> + * \\brief Calculate the quotient of this vector and \\a other element-wise\n> + * \\param[in] other The other vector\n> + * \\return The element-wise division of this vector by \\a other\n> + */\n> +\n> +/**\n> + * \\fn Vector::operator/(T scalar) const\n> + * \\brief Calculate the quotient of this vector and \\a scalar element-wise\n> + * \\param[in] scalar The scalar\n> + * \\return The element-wise division of this vector by \\a scalar\n> + */\n> +\n> +/**\n> + * \\fn Vector::operator+=(Vector const &other)\n> + * \\brief Add \\a other element-wise to this vector\n> + * \\param[in] other The other vector\n> + * \\return This vector\n> + */\n> +\n> +/**\n> + * \\fn Vector::operator+=(T scalar)\n> + * \\brief Add \\a scalar element-wise to this vector\n> + * \\param[in] scalar The scalar\n> + * \\return This vector\n> + */\n> +\n> +/**\n> + * \\fn Vector::operator-=(Vector const &other)\n> + * \\brief Subtract \\a other element-wise from this vector\n> + * \\param[in] other The other vector\n> + * \\return This vector\n> + */\n> +\n> +/**\n> + * \\fn Vector::operator-=(T scalar)\n> + * \\brief Subtract \\a scalar element-wise from this vector\n> + * \\param[in] scalar The scalar\n> + * \\return This vector\n> + */\n> +\n> +/**\n> + * \\fn Vector::operator*=(const Vector &other)\n> + * \\brief Multiply this vector by \\a other element-wise\n> + * \\param[in] other The other vector\n> + * \\return This vector\n> + */\n> +\n> +/**\n> + * \\fn Vector::operator*=(T scalar)\n> + * \\brief Multiply this vector by \\a scalar element-wise\n> + * \\param[in] scalar The scalar\n> + * \\return This vector\n> + */\n> +\n> +/**\n> + * \\fn Vector::operator/=(const Vector &other)\n> + * \\brief Divide this vector by \\a other element-wise\n> + * \\param[in] other The other vector\n> + * \\return This vector\n> + */\n> +\n> +/**\n> + * \\fn Vector::operator/=(T scalar)\n> + * \\brief Divide this vector by \\a scalar element-wise\n> + * \\param[in] scalar The scalar\n> + * \\return This vector\n> + */\n> +\n> +/**\n> + * \\fn Vector::min(const Vector &other) const\n> + * \\brief Calculate the minimum of this vector and \\a other element-wise\n> + * \\param[in] other The other vector\n> + * \\return The element-wise minimum of this vector and \\a other\n> + */\n> +\n> +/**\n> + * \\fn Vector::min(T scalar) const\n> + * \\brief Calculate the minimum of this vector and \\a scalar element-wise\n> + * \\param[in] scalar The scalar\n> + * \\return The element-wise minimum of this vector and \\a scalar\n> + */\n> +\n> +/**\n> + * \\fn Vector::max(const Vector &other) const\n> + * \\brief Calculate the maximum of this vector and \\a other element-wise\n> + * \\param[in] other The other vector\n> + * \\return The element-wise maximum of this vector and \\a other\n> + */\n> +\n> +/**\n> + * \\fn Vector::max(T scalar) const\n> + * \\brief Calculate the maximum of this vector and \\a scalar element-wise\n> + * \\param[in] scalar The scalar\n> + * \\return The element-wise maximum of this vector and \\a scalar\n> + */\n> +\n> +/**\n> + * \\fn Vector::dot(const Vector<T, Rows> &other) const\n> + * \\brief Compute the dot product\n> + * \\param[in] other The other vector\n> + * \\return The dot product of the two vectors\n> + */\n> +\n> +/**\n> + * \\fn constexpr T &Vector::x()\n> + * \\brief Convenience function to access the first element of the vector\n> + * \\return The first element of the vector\n> + */\n> +\n> +/**\n> + * \\fn constexpr T &Vector::y()\n> + * \\brief Convenience function to access the second element of the vector\n> + * \\return The second element of the vector\n> + */\n> +\n> +/**\n> + * \\fn constexpr T &Vector::z()\n> + * \\brief Convenience function to access the third element of the vector\n> + * \\return The third element of the vector\n> + */\n> +\n> +/**\n> + * \\fn constexpr const T &Vector::x() const\n> + * \\copydoc Vector::x()\n> + */\n> +\n> +/**\n> + * \\fn constexpr const T &Vector::y() const\n> + * \\copydoc Vector::y()\n> + */\n> +\n> +/**\n> + * \\fn constexpr const T &Vector::z() const\n> + * \\copydoc Vector::z()\n> + */\n> +\n> +/**\n> + * \\fn constexpr T &Vector::r()\n> + * \\brief Convenience function to access the first element of the vector\n> + * \\return The first element of the vector\n> + */\n> +\n> +/**\n> + * \\fn constexpr T &Vector::g()\n> + * \\brief Convenience function to access the second element of the vector\n> + * \\return The second element of the vector\n> + */\n> +\n> +/**\n> + * \\fn constexpr T &Vector::b()\n> + * \\brief Convenience function to access the third element of the vector\n> + * \\return The third element of the vector\n> + */\n> +\n> +/**\n> + * \\fn constexpr const T &Vector::r() const\n> + * \\copydoc Vector::r()\n> + */\n> +\n> +/**\n> + * \\fn constexpr const T &Vector::g() const\n> + * \\copydoc Vector::g()\n> + */\n> +\n> +/**\n> + * \\fn constexpr const T &Vector::b() const\n> + * \\copydoc Vector::b()\n> + */\n> +\n> +/**\n> + * \\fn Vector::length2()\n> + * \\brief Get the squared length of the vector\n> + * \\return The squared length of the vector\n> + */\n> +\n> +/**\n> + * \\fn Vector::length()\n> + * \\brief Get the length of the vector\n> + * \\return The length of the vector\n> + */\n> +\n> +/**\n> + * \\fn Vector::sum() const\n> + * \\brief Calculate the sum of all the vector elements\n> + * \\tparam R The type of the sum\n> + *\n> + * The type R of the sum defaults to the type T of the elements, but can be set\n> + * explicitly to use a different type in case the type T would risk\n> + * overflowing.\n> + *\n> + * \\return The sum of all the vector elements\n> + */\n> +\n> +/**\n> + * \\fn Vector<T, Rows> operator*(const Matrix<T, Rows, Cols> &m, const Vector<T, Cols> &v)\n> + * \\brief Multiply a matrix by a vector\n> + * \\tparam T Numerical type of the contents of the matrix and vector\n> + * \\tparam Rows The number of rows in the matrix\n> + * \\tparam Cols The number of columns in the matrix (= rows in the vector)\n> + * \\param m The matrix\n> + * \\param v The vector\n> + * \\return Product of matrix \\a m and vector \\a v\n> + */\n> +\n> +/**\n> + * \\typedef RGB\n> + * \\brief A Vector of 3 elements representing an RGB pixel value\n> + */\n> +\n> +/**\n> + * \\fn bool operator==(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs)\n> + * \\brief Compare vectors for equality\n> + * \\return True if the two vectors are equal, false otherwise\n> + */\n> +\n> +/**\n> + * \\fn bool operator!=(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs)\n> + * \\brief Compare vectors for inequality\n> + * \\return True if the two vectors are not equal, false otherwise\n> + */\n> +\n> +#ifndef __DOXYGEN__\n> +bool vectorValidateYaml(const YamlObject &obj, unsigned int size)\n> +{\n> +       if (!obj.isList())\n> +               return false;\n> +\n> +       if (obj.size() != size) {\n> +               LOG(Vector, Error)\n> +                       << \"Wrong number of values in YAML vector: expected \"\n> +                       << size << \", got \" << obj.size();\n> +               return false;\n> +       }\n> +\n> +       return true;\n> +}\n> +#endif /* __DOXYGEN__ */\n> +\n> +} /* namespace ipa */\n> +\n> +} /* namespace libcamera */\n> diff --git a/test/vector.cpp b/test/vector.cpp\n> new file mode 100644\n> index 000000000000..8e4ec77d7820\n> --- /dev/null\n> +++ b/test/vector.cpp\n> @@ -0,0 +1,100 @@\n> +/* SPDX-License-Identifier: GPL-2.0-or-later */\n> +/*\n> + * Copyright (C) 2024, Ideas on Board Oy\n> + *\n> + * Vector tests\n> + */\n> +\n> +#include \"../src/ipa/libipa/vector.h\"\n> +\n> +#include <cmath>\n> +#include <iostream>\n> +\n> +#include \"test.h\"\n> +\n> +using namespace libcamera::ipa;\n> +\n> +#define ASSERT_EQ(a, b)                                                        \\\n> +if ((a) != (b)) {                                                      \\\n> +       std::cout << #a \" != \" #b << \" (line \" << __LINE__ << \")\"       \\\n> +                 << std::endl;                                         \\\n> +       return TestFail;                                                \\\n> +}\n> +\n> +class VectorTest : public Test\n> +{\n> +protected:\n> +       int run()\n> +       {\n> +               Vector<double, 3> v1{ 0.0 };\n> +\n> +               ASSERT_EQ(v1[0], 0.0);\n> +               ASSERT_EQ(v1[1], 0.0);\n> +               ASSERT_EQ(v1[2], 0.0);\n> +\n> +               ASSERT_EQ(v1.length(), 0.0);\n> +               ASSERT_EQ(v1.length2(), 0.0);\n> +\n> +               Vector<double, 3> v2{{ 1.0, 4.0, 8.0 }};\n> +\n> +               ASSERT_EQ(v2[0], 1.0);\n> +               ASSERT_EQ(v2[1], 4.0);\n> +               ASSERT_EQ(v2[2], 8.0);\n> +\n> +               ASSERT_EQ(v2.x(), 1.0);\n> +               ASSERT_EQ(v2.y(), 4.0);\n> +               ASSERT_EQ(v2.z(), 8.0);\n> +\n> +               ASSERT_EQ(v2.r(), 1.0);\n> +               ASSERT_EQ(v2.g(), 4.0);\n> +               ASSERT_EQ(v2.b(), 8.0);\n> +\n> +               ASSERT_EQ(v2.length2(), 81.0);\n> +               ASSERT_EQ(v2.length(), 9.0);\n> +               ASSERT_EQ(v2.sum(), 13.0);\n> +\n> +               Vector<double, 3> v3{ v2 };\n> +\n> +               ASSERT_EQ(v2, v3);\n> +\n> +               v3 = Vector<double, 3>{{ 4.0, 4.0, 4.0 }};\n> +\n> +               ASSERT_EQ(v2 + v3, (Vector<double, 3>{{ 5.0, 8.0, 12.0 }}));\n> +               ASSERT_EQ(v2 + 4.0, (Vector<double, 3>{{ 5.0, 8.0, 12.0 }}));\n> +               ASSERT_EQ(v2 - v3, (Vector<double, 3>{{ -3.0, 0.0, 4.0 }}));\n> +               ASSERT_EQ(v2 - 4.0, (Vector<double, 3>{{ -3.0, 0.0, 4.0 }}));\n> +               ASSERT_EQ(v2 * v3, (Vector<double, 3>{{ 4.0, 16.0, 32.0 }}));\n> +               ASSERT_EQ(v2 * 4.0, (Vector<double, 3>{{ 4.0, 16.0, 32.0 }}));\n> +               ASSERT_EQ(v2 / v3, (Vector<double, 3>{{ 0.25, 1.0, 2.0 }}));\n> +               ASSERT_EQ(v2 / 4.0, (Vector<double, 3>{{ 0.25, 1.0, 2.0 }}));\n> +\n> +               ASSERT_EQ(v2.min(v3), (Vector<double, 3>{{ 1.0, 4.0, 4.0 }}));\n> +               ASSERT_EQ(v2.min(4.0), (Vector<double, 3>{{ 1.0, 4.0, 4.0 }}));\n> +               ASSERT_EQ(v2.max(v3), (Vector<double, 3>{{ 4.0, 4.0, 8.0 }}));\n> +               ASSERT_EQ(v2.max(4.0), (Vector<double, 3>{{ 4.0, 4.0, 8.0 }}));\n> +\n> +               ASSERT_EQ(v2.dot(v3), 52.0);\n> +\n> +               v2 += v3;\n> +               ASSERT_EQ(v2, (Vector<double, 3>{{ 5.0, 8.0, 12.0 }}));\n> +               v2 -= v3;\n> +               ASSERT_EQ(v2, (Vector<double, 3>{{ 1.0, 4.0, 8.0 }}));\n> +               v2 *= v3;\n> +               ASSERT_EQ(v2, (Vector<double, 3>{{ 4.0, 16.0, 32.0 }}));\n> +               v2 /= v3;\n> +               ASSERT_EQ(v2, (Vector<double, 3>{{ 1.0, 4.0, 8.0 }}));\n> +\n> +               v2 += 4.0;\n> +               ASSERT_EQ(v2, (Vector<double, 3>{{ 5.0, 8.0, 12.0 }}));\n> +               v2 -= 4.0;\n> +               ASSERT_EQ(v2, (Vector<double, 3>{{ 1.0, 4.0, 8.0 }}));\n> +               v2 *= 4.0;\n> +               ASSERT_EQ(v2, (Vector<double, 3>{{ 4.0, 16.0, 32.0 }}));\n> +               v2 /= 4.0;\n> +               ASSERT_EQ(v2, (Vector<double, 3>{{ 1.0, 4.0, 8.0 }}));\n> +\n> +               return TestPass;\n> +       }\n> +};\n> +\n> +TEST_REGISTER(VectorTest)\n> -- \n> 2.43.0\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 62DCBBD1F1\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 11 Feb 2025 09:38:38 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 40B726861B;\n\tTue, 11 Feb 2025 10:38:37 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id BD9E661865\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 11 Feb 2025 10:38:34 +0100 (CET)","from pendragon.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 8A1332B3;\n\tTue, 11 Feb 2025 10:37:17 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"kIEZp87z\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1739266637;\n\tbh=XR1ez1WNDCYJmNYnggHeNea/+eHqQ2l3U8VVnJAN1xs=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=kIEZp87zadJqE4sS5PEVyqng6V3pEnSiXIAEfStx8C+MSyewUffVK4KaJou4VwqHY\n\tMebI70GpelJeo/hWjl8rTrzh0iwiPndZn7VW7kRdCgzBU/njp3+foj186QE5dLXoDz\n\t/SD4YE0Oo33W90bQmwfXr5+9s3lbWp77NMfU2bEg=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20250206141018.236272-2-stefan.klug@ideasonboard.com>","References":"<20250206141018.236272-1-stefan.klug@ideasonboard.com>\n\t<20250206141018.236272-2-stefan.klug@ideasonboard.com>","Subject":"Re: [PATCH 1/4] libcamera: Copy Vector class files from libipa","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"Stefan Klug <stefan.klug@ideasonboard.com>","To":"Stefan Klug <stefan.klug@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Tue, 11 Feb 2025 09:38:31 +0000","Message-ID":"<173926671103.2293020.9975184227417336105@ping.linuxembedded.co.uk>","User-Agent":"alot/0.10","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]