{"id":20224,"url":"https://patchwork.libcamera.org/api/1.1/patches/20224/?format=json","web_url":"https://patchwork.libcamera.org/patch/20224/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20240607075714.2648851-2-paul.elder@ideasonboard.com>","date":"2024-06-07T07:57:11","name":"[v5,1/4] ipa: libipa: Add Vector class","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"b9128ca7be4786fa5e3cff1599d8e9de3d26a15a","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/1.1/people/17/?format=json","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/20224/mbox/","series":[{"id":4370,"url":"https://patchwork.libcamera.org/api/1.1/series/4370/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4370","date":"2024-06-07T07:57:10","name":"ipa: Move Pwl from Raspberry Pi to libipa","version":5,"mbox":"https://patchwork.libcamera.org/series/4370/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/20224/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/20224/checks/","tags":{},"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 18EDABD87C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri,  7 Jun 2024 07:57:36 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 935A965457;\n\tFri,  7 Jun 2024 09:57:33 +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 DC35E65455\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri,  7 Jun 2024 09:57:31 +0200 (CEST)","from neptunite.hamster-moth.ts.net\n\t(h175-177-049-156.catv02.itscom.jp [175.177.49.156])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 6147A1FA4;\n\tFri,  7 Jun 2024 09:57:21 +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=\"r/i4Sjo7\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1717747042;\n\tbh=U2RQyhXBvJJGX5A+sLaLgIhRGxzL76oN2v8JaCc5jTg=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=r/i4Sjo7wQv0Oc/RBTInwFDBU7hMC6wEFdEmFpTmO+HcK/cxs2jNoPwyxOpgPJ036\n\t/+hLQk0WtFnc64ElOL2gqCkOnly66TpQ7Q+atRqxtYvwCDVjDl3GK8errId1P42e4u\n\tUlxDRdktpm1XedZLV3Db2eQ2c6E5IS1NIMhD3uJU=","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Paul Elder <paul.elder@ideasonboard.com>","Subject":"[PATCH v5 1/4] ipa: libipa: Add Vector class","Date":"Fri,  7 Jun 2024 16:57:11 +0900","Message-Id":"<20240607075714.2648851-2-paul.elder@ideasonboard.com>","X-Mailer":"git-send-email 2.39.2","In-Reply-To":"<20240607075714.2648851-1-paul.elder@ideasonboard.com>","References":"<20240607075714.2648851-1-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","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>"},"content":"Add a vector class to libipa. The original purpose of this is to replace\nthe floating-point Point class that Raspberry Pi used in their Pwl, as\nthat implementation of Point seemed more akin to a Vector than a Point.\n\nThis is added to libipa instead of to geometry.h to avoid public API\nissues, plus this is not expected to be needed by applications.\n\nSigned-off-by: Paul Elder <paul.elder@ideasonboard.com>\n\n---\nNew in v5\n---\n src/ipa/libipa/vector.cpp | 162 ++++++++++++++++++++++++++++++\n src/ipa/libipa/vector.h   | 206 ++++++++++++++++++++++++++++++++++++++\n 2 files changed, 368 insertions(+)\n create mode 100644 src/ipa/libipa/vector.cpp\n create mode 100644 src/ipa/libipa/vector.h","diff":"diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp\nnew file mode 100644\nindex 000000000..bdc3c447c\n--- /dev/null\n+++ b/src/ipa/libipa/vector.cpp\n@@ -0,0 +1,162 @@\n+/* SPDX-License-Identifier: BSD-2-Clause */\n+/*\n+ * Copyright (C) 2019, Raspberry Pi Ltd\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 R Number of rows in the vector\n+ */\n+\n+/**\n+ * \\fn Vector::Vector()\n+ * \\brief Construct an identity vector\n+ */\n+\n+/**\n+ * \\fn Vector::Vector(const std::array<T, R> &data)\n+ * \\brief Construct vector from supplied data\n+ * \\param data Data from which to construct a vector\n+ *\n+ * \\a data is a one-dimensional vector and will be turned into a vector in\n+ * row-major order. The size of \\a data must be equal to the product of the\n+ * number of rows and columns of the vector (RxC).\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 product of the number of rows and\n+ * columns of the vector (RxC).\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 Vector::toString\n+ * \\brief Assemble and return a string describing the vector\n+ * \\return A string describing the vector\n+ */\n+\n+/**\n+ * \\fn T Vector::operator[](size_t i) const\n+ * \\brief Index to a row in the vector\n+ * \\param i Index of row to retrieve\n+ *\n+ * This operator[] returns a Span, which can then be indexed into again with\n+ * another operator[], allowing a convenient m[i][j] to access elements of the\n+ * vector. Note that the lifetime of the Span returned by this first-level\n+ * operator[] is bound to that of the Vector itself, so it is not recommended\n+ * to save the Span that is the result of this operator[].\n+ *\n+ * \\return Row \\a i from the vector, as a Span\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+\n+/**\n+ * \\fn Vector::y()\n+ * \\brief Convenience function to access the second 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, R> &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 Scale up the vector\n+ * \\param[in] factor The factor\n+ * \\return The vector scaled up by \\a factor\n+ */\n+\n+/**\n+ * \\fn Vector::operator/()\n+ * \\brief Scale down the vector\n+ * \\param[in] factor The factor\n+ * \\return The vector scaled down by \\a factor\n+ */\n+\n+/**\n+ * \\fn Vector::len2()\n+ * \\brief Get the squared length of the vector\n+ * \\return The squared length of the vector\n+ */\n+\n+/**\n+ * \\fn Vector::len()\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, R> &lhs, const Vector<T, R> &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, R> &lhs, const Vector<T, R> &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 */\ndiff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h\nnew file mode 100644\nindex 000000000..87aad28b5\n--- /dev/null\n+++ b/src/ipa/libipa/vector.h\n@@ -0,0 +1,206 @@\n+/* SPDX-License-Identifier: BSD-2-Clause */\n+/*\n+ * Copyright (C) 2019, Raspberry Pi Ltd\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 R,\n+\t std::enable_if_t<std::is_arithmetic_v<T> && R >= 2> * = nullptr>\n+#else\n+template<typename T, unsigned int R>\n+#endif /* __DOXYGEN__ */\n+class Vector\n+{\n+public:\n+\tVector() = default;\n+\n+\tVector(const std::array<T, R> &data)\n+\t{\n+\t\tASSERT(data.size() == R);\n+\n+\t\tfor (unsigned int i = 0; i < R; i++)\n+\t\t\tdata_[i] = data[i];\n+\t}\n+\n+\t~Vector() = default;\n+\n+\tint readYaml(const libcamera::YamlObject &yaml)\n+\t{\n+\t\tif (yaml.size() != R) {\n+\t\t\tLOG(Vector, Error)\n+\t\t\t\t<< \"Wrong number of values in vector: expected \"\n+\t\t\t\t<< R << \", 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 std::string toString() const\n+\t{\n+\t\tstd::stringstream out;\n+\n+\t\tout << \"Vector { \";\n+\t\tfor (unsigned int i = 0; i < R; i++) {\n+\t\t\tout << (*this)[i];\n+\t\t\tout << ((i + 1 < R) ? \", \" : \" \");\n+\t\t}\n+\t\tout << \" }\";\n+\n+\t\treturn out.str();\n+\t}\n+\n+\tconst T operator[](size_t i) const\n+\t{\n+\t\treturn data_[i];\n+\t}\n+\n+\tT &operator[](size_t i)\n+\t{\n+\t\treturn data_[i];\n+\t}\n+\n+\tconst T x() const\n+\t{\n+\t\treturn data_[0];\n+\t}\n+\n+\tconst T y() const\n+\t{\n+\t\treturn data_[1];\n+\t}\n+\n+\tconstexpr Vector<T, R> operator-() const\n+\t{\n+\t\tVector<T, R> ret;\n+\t\tfor (unsigned int i = 0; i < R; i++)\n+\t\t\tret[i] = -data_[i];\n+\t\treturn ret;\n+\t}\n+\n+\tconstexpr Vector<T, R> operator-(const Vector<T, R> &other) const\n+\t{\n+\t\tVector<T, R> ret;\n+\t\tfor (unsigned int i = 0; i < R; i++)\n+\t\t\tret[i] = data_[i] - other[i];\n+\t\treturn ret;\n+\t}\n+\n+\tconstexpr Vector<T, R> operator+(const Vector<T, R> &other) const\n+\t{\n+\t\tVector<T, R> ret;\n+\t\tfor (unsigned int i = 0; i < R; i++)\n+\t\t\tret[i] = data_[i] + other[i];\n+\t\treturn ret;\n+\t}\n+\n+\tconstexpr T operator*(const Vector<T, R> &other) const\n+\t{\n+\t\tT ret = 0;\n+\t\tfor (unsigned int i = 0; i < R; i++)\n+\t\t\tret += data_[i] * other[i];\n+\t\treturn ret;\n+\t}\n+\n+\tconstexpr Vector<T, R> operator*(T factor) const\n+\t{\n+\t\tVector<T, R> ret;\n+\t\tfor (unsigned int i = 0; i < R; i++)\n+\t\t\tret[i] = data_[i] * factor;\n+\t\treturn ret;\n+\t}\n+\n+\tconstexpr Vector<T, R> operator/(T factor) const\n+\t{\n+\t\tVector<T, R> ret;\n+\t\tfor (unsigned int i = 0; i < R; i++)\n+\t\t\tret[i] = data_[i] / factor;\n+\t\treturn ret;\n+\t}\n+\n+\tconstexpr T len2() const\n+\t{\n+\t\tT ret = 0;\n+\t\tfor (unsigned int i = 0; i < R; i++)\n+\t\t\tret += data_[i] * data_[i];\n+\t\treturn ret;\n+\t}\n+\n+\tconstexpr double len() const\n+\t{\n+\t\treturn std::sqrt(len2());\n+\t}\n+\n+private:\n+\tstd::array<T, R> data_;\n+};\n+\n+#ifndef __DOXYGEN__\n+template<typename T, unsigned int R,\n+\t std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr>\n+#endif /* __DOXYGEN__ */\n+bool operator==(const Vector<T, R> &lhs, const Vector<T, R> &rhs)\n+{\n+\tfor (unsigned int i = 0; i < R; i++)\n+\t\tif (lhs[i] != rhs[i])\n+\t\t\treturn false;\n+\n+\treturn true;\n+}\n+\n+#ifndef __DOXYGEN__\n+template<typename T, unsigned int R,\n+\t std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr>\n+#endif /* __DOXYGEN__ */\n+bool operator!=(const Vector<T, R> &lhs, const Vector<T, R> &rhs)\n+{\n+\tfor (unsigned int i = 0; i < R; 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 R>\n+std::ostream &operator<<(std::ostream &out, const ipa::Vector<T, R> &v)\n+{\n+\tout << v.toString();\n+\treturn out;\n+}\n+#endif /* __DOXYGEN__ */\n+\n+} /* namespace libcamera */\n","prefixes":["v5","1/4"]}