[{"id":29848,"web_url":"https://patchwork.libcamera.org/comment/29848/","msgid":"<20240611213959.GB8290@pendragon.ideasonboard.com>","date":"2024-06-11T21:39:59","subject":"Re: [PATCH v8 1/4] ipa: libipa: Add Vector class","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Paul,\n\nThank you for the patch.\n\nOn Tue, Jun 11, 2024 at 10:24:27PM +0900, Paul Elder wrote:\n> Add a vector class to libipa. The original purpose of this is to replace\n> the floating-point Point class that Raspberry Pi used in their Pwl, as\n> that implementation of Point seemed more akin to a Vector than a Point.\n> \n> This is added to libipa instead of to geometry.h to avoid public API\n> issues, plus this is not expected to be needed by applications.\n> \n> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>\n> Reviewed-by: Stefan Klug <stefan.klug@ideasonboard.com>\n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> \n> ---\n> Changes in v8:\n> - s/D/Rows/ in Vector class\n> - add z accessor function\n> - move SFINAE for x() and y() (and z()) to the functions instead of the\n>   class\n> - add vector to meson\n> \n> Changes in v7:\n> - fix documentation\n> - fix license and copyright\n> - remove toString()\n> \n> No change in v6\n> \n> New in v5\n> ---\n>  src/ipa/libipa/meson.build |   2 +\n>  src/ipa/libipa/vector.cpp  | 150 +++++++++++++++++++++++++++\n>  src/ipa/libipa/vector.h    | 203 +++++++++++++++++++++++++++++++++++++\n>  3 files changed, 355 insertions(+)\n>  create mode 100644 src/ipa/libipa/vector.cpp\n>  create mode 100644 src/ipa/libipa/vector.h\n> \n> diff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build\n> index 7ce885da7b99..8b0c8fff901b 100644\n> --- a/src/ipa/libipa/meson.build\n> +++ b/src/ipa/libipa/meson.build\n> @@ -8,6 +8,7 @@ libipa_headers = files([\n>      'fc_queue.h',\n>      'histogram.h',\n>      'module.h',\n> +    'vector.h',\n>  ])\n>  \n>  libipa_sources = files([\n> @@ -18,6 +19,7 @@ libipa_sources = files([\n>      'fc_queue.cpp',\n>      'histogram.cpp',\n>      'module.cpp',\n> +    'vector.cpp',\n>  ])\n>  \n>  libipa_includes = include_directories('..')\n> diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp\n> new file mode 100644\n> index 000000000000..b9fad90386a1\n> --- /dev/null\n> +++ b/src/ipa/libipa/vector.cpp\n> @@ -0,0 +1,150 @@\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 a zero vector\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 Vector::readYaml\n> + * \\brief Populate the vector with yaml data\n> + * \\param yaml Yaml data to populate the vector with\n> + *\n> + * Any existing data in the vector will be overwritten. The size of the data\n> + * read from \\a yaml must be equal to the dimension size Rows of the vector.\n> + *\n> + * The yaml data is expected to be a list with elements of type T.\n> + *\n> + * \\return 0 on success, negative error code otherwise\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::x()\n> + * \\brief Convenience function to access the first element of the vector\n\n * \\return The first element of the vector\n\nSame below.\n\n> + */\n> +\n> +/**\n> + * \\fn Vector::y()\n> + * \\brief Convenience function to access the second element of the vector\n> + */\n> +\n> +/**\n> + * \\fn Vector::z()\n> + * \\brief Convenience function to access the third element of the vector\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 Subtract one vector from another\n> + * \\param[in] other The other vector\n> + * \\return The difference of \\a other from this vector\n> + */\n> +\n> +/**\n> + * \\fn Vector::operator+()\n> + * \\brief Add two vectors together\n> + * \\param[in] other The other vector\n> + * \\return The sum of the two vectors\n> + */\n> +\n> +/**\n> + * \\fn Vector::operator*(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 Vector::operator*(T factor) const\n> + * \\brief Multiply the vector by a scalar\n> + * \\param[in] factor The factor\n> + * \\return The vector multiplied by \\a factor\n> + */\n> +\n> +/**\n> + * \\fn Vector::operator/()\n> + * \\brief Divide the vector by a scalar\n> + * \\param[in] factor The factor\n> + * \\return The vector divided by \\a factor\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 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> +} /* namespace ipa */\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h\n> new file mode 100644\n> index 000000000000..b281f079365a\n> --- /dev/null\n> +++ b/src/ipa/libipa/vector.h\n> @@ -0,0 +1,203 @@\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 <sstream>\n> +\n> +#include <libcamera/base/log.h>\n> +#include <libcamera/base/span.h>\n> +\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> +\t 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> +\tconstexpr Vector() = default;\n> +\n> +\tconstexpr Vector(const std::array<T, Rows> &data)\n> +\t{\n> +\t\tfor (unsigned int i = 0; i < Rows; i++)\n> +\t\t\tdata_[i] = data[i];\n> +\t}\n\nA constructor taking an initializer list may be useful. It can be added\nlater.\n\n> +\n> +\tint readYaml(const libcamera::YamlObject &yaml)\n\nI still don't like having this as a member function. I'm working on\nhandling this on top, so it's not a blocker.\n\n> +\t{\n> +\t\tif (yaml.size() != Rows) {\n> +\t\t\tLOG(Vector, Error)\n> +\t\t\t\t<< \"Wrong number of values in vector: expected \"\n> +\t\t\t\t<< Rows << \", got \" << yaml.size();\n> +\t\t\treturn -EINVAL;\n> +\t\t}\n> +\n> +\t\tunsigned int i = 0;\n> +\t\tfor (const auto &x : yaml.asList()) {\n> +\t\t\tauto value = x.get<T>();\n> +\t\t\tif (!value) {\n> +\t\t\t\tLOG(Vector, Error) << \"Failed to read vector value\";\n> +\t\t\t\treturn -EINVAL;\n> +\t\t\t}\n> +\n> +\t\t\tdata_[i++] = *value;\n> +\t\t}\n> +\n> +\t\treturn 0;\n> +\t}\n> +\n> +\tconst T &operator[](size_t i) const\n> +\t{\n> +\t\tASSERT(i < data_.size());\n> +\t\treturn data_[i];\n> +\t}\n> +\n> +\tT &operator[](size_t i)\n> +\t{\n> +\t\tASSERT(i < data_.size());\n> +\t\treturn data_[i];\n> +\t}\n> +\n> +#ifndef __DOXYGEN__\n> +\ttemplate<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 1>>\n> +#endif /* __DOXYGEN__ */\n> +\tconstexpr T x() const\n> +\t{\n> +\t\treturn data_[0];\n> +\t}\n> +\n> +#ifndef __DOXYGEN__\n> +\ttemplate<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 2>>\n> +#endif /* __DOXYGEN__ */\n> +\tconstexpr T y() const\n> +\t{\n> +\t\treturn data_[1];\n> +\t}\n> +\n> +#ifndef __DOXYGEN__\n> +\ttemplate<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 3>>\n> +#endif /* __DOXYGEN__ */\n> +\tconstexpr T z() const\n> +\t{\n> +\t\treturn data_[2];\n> +\t}\n> +\n> +\tconstexpr Vector<T, Rows> operator-() const\n> +\t{\n> +\t\tVector<T, Rows> ret;\n> +\t\tfor (unsigned int i = 0; i < Rows; i++)\n> +\t\t\tret[i] = -data_[i];\n> +\t\treturn ret;\n> +\t}\n> +\n> +\tconstexpr Vector<T, Rows> operator-(const Vector<T, Rows> &other) const\n> +\t{\n> +\t\tVector<T, Rows> ret;\n> +\t\tfor (unsigned int i = 0; i < Rows; i++)\n> +\t\t\tret[i] = data_[i] - other[i];\n> +\t\treturn ret;\n> +\t}\n> +\n> +\tconstexpr Vector<T, Rows> operator+(const Vector<T, Rows> &other) const\n> +\t{\n> +\t\tVector<T, Rows> ret;\n> +\t\tfor (unsigned int i = 0; i < Rows; i++)\n> +\t\t\tret[i] = data_[i] + other[i];\n> +\t\treturn ret;\n> +\t}\n> +\n> +\tconstexpr T operator*(const Vector<T, Rows> &other) const\n> +\t{\n> +\t\tT ret = 0;\n> +\t\tfor (unsigned int i = 0; i < Rows; i++)\n> +\t\t\tret += data_[i] * other[i];\n> +\t\treturn ret;\n> +\t}\n> +\n> +\tconstexpr Vector<T, Rows> operator*(T factor) const\n> +\t{\n> +\t\tVector<T, Rows> ret;\n> +\t\tfor (unsigned int i = 0; i < Rows; i++)\n> +\t\t\tret[i] = data_[i] * factor;\n> +\t\treturn ret;\n> +\t}\n> +\n> +\tconstexpr Vector<T, Rows> operator/(T factor) const\n> +\t{\n> +\t\tVector<T, Rows> ret;\n> +\t\tfor (unsigned int i = 0; i < Rows; i++)\n> +\t\t\tret[i] = data_[i] / factor;\n> +\t\treturn ret;\n> +\t}\n> +\n> +\tconstexpr double length2() const\n> +\t{\n> +\t\tdouble ret = 0;\n> +\t\tfor (unsigned int i = 0; i < Rows; i++)\n> +\t\t\tret += data_[i] * data_[i];\n> +\t\treturn ret;\n> +\t}\n> +\n> +\tconstexpr double length() const\n> +\t{\n> +\t\treturn std::sqrt(length2());\n> +\t}\n> +\n> +private:\n> +\tstd::array<T, Rows> data_;\n> +};\n> +\n> +template<typename T, unsigned int Rows>\n> +bool operator==(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs)\n> +{\n> +\tfor (unsigned int i = 0; i < Rows; i++)\n> +\t\tif (lhs[i] != rhs[i])\n> +\t\t\treturn false;\n\nCurly braces around the for statement. Same below.\n\n> +\n> +\treturn 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\n\treturn !(lhs == rhs);\n\nshould do.\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> +\tfor (unsigned int i = 0; i < Rows; i++)\n> +\t\tif (lhs[i] != rhs[i])\n> +\t\t\treturn true;\n> +\n> +\treturn false;\n> +}\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> +\tout << \"Vector { \";\n> +\tfor (unsigned int i = 0; i < Rows; i++) {\n> +\t\tout << v[i];\n> +\t\tout << ((i + 1 < Rows) ? \", \" : \" \");\n> +\t}\n> +\tout << \" }\";\n> +\n> +\treturn out;\n> +}\n> +#endif /* __DOXYGEN__ */\n> +\n> +} /* namespace libcamera */","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 EF036BD87C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 11 Jun 2024 21:40:21 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 13C4361A26;\n\tTue, 11 Jun 2024 23:40:21 +0200 (CEST)","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 7EC6A61A26\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 11 Jun 2024 23:40:19 +0200 (CEST)","from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi\n\t[81.175.209.231])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 35130512;\n\tTue, 11 Jun 2024 23:40:06 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"l7hIK7CE\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1718142006;\n\tbh=h12ChxjMeoEIdr5S9k8eSFeZOk2uSG5jALbauOc9/VE=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=l7hIK7CE8NIPABDAtveIIRQVanu9UC8gsd8ChoYIWUavBJy7V+GpEhMAEp8GlYEJd\n\taqB6WPvCaZiznZlJId2SaQFn93fSr0UcfMp2PhTghReLLcdrU+bPs8IUD4nOCtaQXZ\n\t5rmUBTcPP1de3fzxVTuqgURcrXyAUbWyA54KOX88=","Date":"Wed, 12 Jun 2024 00:39:59 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Paul Elder <paul.elder@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org,\n\tStefan Klug <stefan.klug@ideasonboard.com>,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>","Subject":"Re: [PATCH v8 1/4] ipa: libipa: Add Vector class","Message-ID":"<20240611213959.GB8290@pendragon.ideasonboard.com>","References":"<20240611132430.404814-1-paul.elder@ideasonboard.com>\n\t<20240611132430.404814-2-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20240611132430.404814-2-paul.elder@ideasonboard.com>","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>"}}]