[{"id":29850,"web_url":"https://patchwork.libcamera.org/comment/29850/","msgid":"<20240611221441.GD8290@pendragon.ideasonboard.com>","date":"2024-06-11T22:14:41","subject":"Re: [PATCH v8 3/4] ipa: libipa: pwl: Clean up Pwl class to match\n\tlibcamera","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:29PM +0900, Paul Elder wrote:\n> Clean up the Pwl class copied from the Raspberry Pi IPA to align it more\n> with the libcamera style.\n> \n> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>\n> Reviewed-by: Stefan Klug <stefan.klug@ideasonboard.com>\n> Acked-by: David Plowman <david.plowman@raspberrypi.com>\n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> \n> ---\n> Changes in v8:\n> - use the updated Vector interface\n> - remove unused functions (prepend, invert, extendDomain)\n> - improve class documentation\n> - checkstyle\n> - s/PointF/Point/\n> - make inverse() return pair instead of output parameter\n> - fix const order\n> - fix includes\n> \n> No change in v7\n> \n> Changes in v6:\n> - move adding pwl to meson here\n> \n> Changes in v5:\n> - fix documentation order\n> - fix some typos\n> - add the Vector-based PointF\n> \n> Changes in v4:\n> - update to apply to new copy of pwl\n> - add documentation\n> - fix doxygen\n> \n> No change in v3\n> \n> Changes in v2:\n> - s/FPoint/PointF/g\n> - improve documentation\n> - s/matchDomain/extendDomain/\n> ---\n>  src/ipa/libipa/meson.build |   2 +\n>  src/ipa/libipa/pwl.cpp     | 372 ++++++++++++++++++++++++++-----------\n>  src/ipa/libipa/pwl.h       | 133 +++++--------\n>  3 files changed, 311 insertions(+), 196 deletions(-)\n> \n> diff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build\n> index 8b0c8fff901b..3669f8939d3b 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> +    'pwl.h',\n>      'vector.h',\n>  ])\n>  \n> @@ -19,6 +20,7 @@ libipa_sources = files([\n>      'fc_queue.cpp',\n>      'histogram.cpp',\n>      'module.cpp',\n> +    'pwl.cpp',\n>      'vector.cpp',\n>  ])\n>  \n> diff --git a/src/ipa/libipa/pwl.cpp b/src/ipa/libipa/pwl.cpp\n> index e39123767aa6..4dc59981708d 100644\n> --- a/src/ipa/libipa/pwl.cpp\n> +++ b/src/ipa/libipa/pwl.cpp\n> @@ -1,19 +1,120 @@\n>  /* SPDX-License-Identifier: BSD-2-Clause */\n>  /*\n>   * Copyright (C) 2019, Raspberry Pi Ltd\n> + * Copyright (C) 2024, Ideas on Board Oy\n>   *\n> - * piecewise linear functions\n> + * Piecewise linear functions\n>   */\n>  \n> -#include <cassert>\n> +#include \"pwl.h\"\n> +\n> +#include <assert.h>\n>  #include <cmath>\n> +#include <sstream>\n>  #include <stdexcept>\n>  \n> -#include \"pwl.h\"\n> +#include <libcamera/geometry.h>\n\nUnless I'm missing something, this isn't needed.\n\n> +\n> +/**\n> + * \\file pwl.h\n> + * \\brief Piecewise linear functions\n> + */\n> +\n> +namespace libcamera {\n> +\n> +namespace ipa {\n> +\n> +/**\n> + * \\class Pwl\n> + * \\brief Describe a univariate piecewise linear function in two-dimensional\n> + * real space\n> + *\n> + * A piecewise linear function is a univariate function that maps reals to\n> + * reals, and it is composed of multiple straight-line segments.\n> + *\n> + * While a mathematical piecewise linear function would usually be defined by\n> + * a list of linear functions and for which values of the domain they apply,\n> + * this Pwl class is instead defined by a list of points at which these line\n> + * segments intersect. These intersecting points are known as knots.\n> + *\n> + * https://en.wikipedia.org/wiki/Piecewise_linear_function\n> + *\n> + * A consequence of the Pwl class being defined by knots instead of linear\n> + * functions is that the values of the piecewise linear function past the ends\n> + * of the function are constants as opposed to linear functions. In a\n> + * mathematical piecewise linear function that is defined by multiple linear\n> + * functions, the ends of the function are also linear functions and hence grow\n> + * to infinity (or negative infinity). However, since this Pwl class is defined\n> + * by knots, the y-value of the leftmost and rightmost knots will hold for all\n> + * x values to negative infinity and positive infinity, respectively.\n\nNice documentation, I especially like the part about what happens\noutside of the defined segments, that was not evident.\n\n> + */\n> +\n> +/**\n> + * \\typedef Pwl::Point\n> + * \\brief Describe a point in two-dimensional real space\n> + */\n> +\n> +/**\n> + * \\class Pwl::Interval\n> + * \\brief Describe an interval in one-dimensional real space\n> + */\n> +\n> +/**\n> + * \\fn Pwl::Interval::Interval(double _start, double _end)\n> + * \\brief Construct an interval\n> + * \\param _start Start of the interval\n> + * \\param _end End of the interval\n> + */\n> +\n> +/**\n> + * \\fn Pwl::Interval::contains\n> + * \\brief Check if a given value falls within the interval\n> + * \\param value Value to check\n\n * \\return True if the value falls within the interval, including its\n * bounds, or false otherwise\n\n> + */\n> +\n> +/**\n> + * \\fn Pwl::Interval::clamp\n> + * \\brief Clamp a value such that it is within the interval\n> + * \\param value Value to clamp\n\n * \\return The clamped value\n\n> + */\n> +\n> +/**\n> + * \\fn Pwl::Interval::length\n> + * \\brief Compute the length of the interval\n\n * \\return The length of the interval\n\n> + */\n> +\n> +/**\n> + * \\var Pwl::Interval::start\n> + * \\brief Start of the interval\n> + */\n>  \n> -using namespace RPiController;\n> +/**\n> + * \\var Pwl::Interval::end\n> + * \\brief End of the interval\n> + */\n>  \n> -int Pwl::read(const libcamera::YamlObject &params)\n> +/**\n> + * \\fn Pwl::Pwl(std::vector<Point> const &points)\n> + * \\brief Construct a piecewise linear function from a list of 2D points\n> + * \\param points Vector of points from which to construct the piecewise linear function\n> + *\n> + * \\a points must be in ascending order of x-value.\n> + */\n> +\n> +/**\n> + * \\brief Populate the piecewise linear function from yaml data\n> + * \\param params Yaml data to populate the piecewise linear function with\n> + *\n> + * Any existing points in the piecewise linear function will *not* be\n> + * overwritten.\n\nIt sounds like a bit of an off behaviour, compared to clearing the PWL\nfirst. Does anything depends on it ?\n\n> + *\n> + * The yaml data is expected to be a list with an even number of numerical\n> + * elements. These will be parsed in pairs into x and y points in the piecewise\n> + * linear function, and added in order. x must be monotonically increasing.\n> + *\n> + * \\return 0 on success, negative error code otherwise\n> + */\n> +int Pwl::readYaml(const libcamera::YamlObject &params)\n>  {\n>  \tif (!params.size() || params.size() % 2)\n>  \t\treturn -EINVAL;\n> @@ -24,64 +125,109 @@ int Pwl::read(const libcamera::YamlObject &params)\n>  \t\tauto x = it->get<double>();\n>  \t\tif (!x)\n>  \t\t\treturn -EINVAL;\n> -\t\tif (it != list.begin() && *x <= points_.back().x)\n> +\t\tif (it != list.begin() && *x <= points_.back().x())\n>  \t\t\treturn -EINVAL;\n>  \n>  \t\tauto y = (++it)->get<double>();\n>  \t\tif (!y)\n>  \t\t\treturn -EINVAL;\n>  \n> -\t\tpoints_.push_back(Point(*x, *y));\n> +\t\tpoints_.push_back(Point({ *x, *y }));\n>  \t}\n>  \n>  \treturn 0;\n>  }\n>  \n> +/**\n> + * \\brief Append a point to the end of the piecewise linear function\n> + * \\param x x-coordinate of the point to add to the piecewise linear function\n> + * \\param y y-coordinate of the point to add to the piecewise linear function\n> + * \\param eps Epsilon for the minimum x distance between points (optional)\n> + *\n> + * The point's x-coordinate must be greater than the x-coordinate of the last\n> + * (= greatest) point already in the piecewise linear function.\n> + */\n>  void Pwl::append(double x, double y, const double eps)\n>  {\n> -\tif (points_.empty() || points_.back().x + eps < x)\n> -\t\tpoints_.push_back(Point(x, y));\n> +\tif (points_.empty() || points_.back().x() + eps < x)\n> +\t\tpoints_.push_back(Point({ x, y }));\n>  }\n>  \n> +/**\n> + * \\brief Prepend a point to the beginning of the piecewise linear function\n> + * \\param x x-coordinate of the point to add to the piecewise linear function\n> + * \\param y y-coordinate of the point to add to the piecewise linear function\n> + * \\param eps Epsilon for the minimum x distance between points (optional)\n> + *\n> + * The point's x-coordinate must be less than the x-coordinate of the first\n> + * (= smallest) point already in the piecewise linear function.\n> + */\n>  void Pwl::prepend(double x, double y, const double eps)\n>  {\n> -\tif (points_.empty() || points_.front().x - eps > x)\n> -\t\tpoints_.insert(points_.begin(), Point(x, y));\n> +\tif (points_.empty() || points_.front().x() - eps > x)\n> +\t\tpoints_.insert(points_.begin(), Point({ x, y }));\n>  }\n>  \n> +/**\n> + * \\brief Get the domain of the piecewise linear function\n> + * \\return An interval representing the domain\n> + */\n>  Pwl::Interval Pwl::domain() const\n>  {\n> -\treturn Interval(points_[0].x, points_[points_.size() - 1].x);\n> +\treturn Interval(points_[0].x(), points_[points_.size() - 1].x());\n>  }\n>  \n> +/**\n> + * \\brief Get the range of the piecewise linear function\n> + * \\return An interval representing the range\n> + */\n>  Pwl::Interval Pwl::range() const\n>  {\n> -\tdouble lo = points_[0].y, hi = lo;\n> +\tdouble lo = points_[0].y(), hi = lo;\n>  \tfor (auto &p : points_)\n> -\t\tlo = std::min(lo, p.y), hi = std::max(hi, p.y);\n> +\t\tlo = std::min(lo, p.y()), hi = std::max(hi, p.y());\n>  \treturn Interval(lo, hi);\n>  }\n>  \n> +/**\n> + * \\brief Check if the piecewise linear function is empty\n> + * \\return True if there are no points in the function, false otherwise\n> + */\n>  bool Pwl::empty() const\n>  {\n>  \treturn points_.empty();\n>  }\n>  \n> -double Pwl::eval(double x, int *spanPtr, bool updateSpan) const\n> +/**\n> + * \\brief Evaluate the piecewise linear function\n> + * \\param[in] x The x value to input into the function\n> + * \\param[inout] span Initial guess for span\n> + * \\param[in] updateSpan Set to true to update span\n> + *\n> + * Evaluate Pwl, optionally supplying an initial guess for the\n> + * \"span\". The \"span\" may be optionally be updated. If you want to know\n> + * the \"span\" value but don't have an initial guess you can set it to\n> + * -1.\n> + *\n> + *  \\return The result of evaluating the piecewise linear function at position \\a x\n> + */\n> +double Pwl::eval(double x, int *span, bool updateSpan) const\n>  {\n> -\tint span = findSpan(x, spanPtr && *spanPtr != -1 ? *spanPtr : points_.size() / 2 - 1);\n> -\tif (spanPtr && updateSpan)\n> -\t\t*spanPtr = span;\n> -\treturn points_[span].y +\n> -\t       (x - points_[span].x) * (points_[span + 1].y - points_[span].y) /\n> -\t\t       (points_[span + 1].x - points_[span].x);\n> +\tint index = findSpan(x, span && *span != -1\n> +\t\t\t\t\t? *span\n> +\t\t\t\t\t: points_.size() / 2 - 1);\n> +\tif (span && updateSpan)\n> +\t\t*span = index;\n> +\treturn points_[index].y() +\n> +\t       (x - points_[index].x()) * (points_[index + 1].y() - points_[index].y()) /\n> +\t\t       (points_[index + 1].x() - points_[index].x());\n>  }\n>  \n>  int Pwl::findSpan(double x, int span) const\n>  {\n>  \t/*\n>  \t * Pwls are generally small, so linear search may well be faster than\n> -\t * binary, though could review this if large PWls start turning up.\n> +\t * binary, though could review this if large Pwls start turning up.\n>  \t */\n>  \tint lastSpan = points_.size() - 2;\n>  \t/*\n> @@ -89,65 +235,43 @@ int Pwl::findSpan(double x, int span) const\n>  \t * control point\n>  \t */\n>  \tspan = std::max(0, std::min(lastSpan, span));\n> -\twhile (span < lastSpan && x >= points_[span + 1].x)\n> +\twhile (span < lastSpan && x >= points_[span + 1].x())\n>  \t\tspan++;\n> -\twhile (span && x < points_[span].x)\n> +\twhile (span && x < points_[span].x())\n>  \t\tspan--;\n>  \treturn span;\n>  }\n>  \n> -Pwl::PerpType Pwl::invert(Point const &xy, Point &perp, int &span,\n> -\t\t\t  const double eps) const\n> -{\n> -\tassert(span >= -1);\n> -\tbool prevOffEnd = false;\n> -\tfor (span = span + 1; span < (int)points_.size() - 1; span++) {\n> -\t\tPoint spanVec = points_[span + 1] - points_[span];\n> -\t\tdouble t = ((xy - points_[span]) % spanVec) / spanVec.len2();\n> -\t\tif (t < -eps) /* off the start of this span */\n> -\t\t{\n> -\t\t\tif (span == 0) {\n> -\t\t\t\tperp = points_[span];\n> -\t\t\t\treturn PerpType::Start;\n> -\t\t\t} else if (prevOffEnd) {\n> -\t\t\t\tperp = points_[span];\n> -\t\t\t\treturn PerpType::Vertex;\n> -\t\t\t}\n> -\t\t} else if (t > 1 + eps) /* off the end of this span */\n> -\t\t{\n> -\t\t\tif (span == (int)points_.size() - 2) {\n> -\t\t\t\tperp = points_[span + 1];\n> -\t\t\t\treturn PerpType::End;\n> -\t\t\t}\n> -\t\t\tprevOffEnd = true;\n> -\t\t} else /* a true perpendicular */\n> -\t\t{\n> -\t\t\tperp = points_[span] + spanVec * t;\n> -\t\t\treturn PerpType::Perpendicular;\n> -\t\t}\n> -\t}\n> -\treturn PerpType::None;\n> -}\n> -\n> -Pwl Pwl::inverse(bool *trueInverse, const double eps) const\n> +/**\n> + * \\brief Compute the inverse function\n> + * \\param[in] eps Epsilon for the minimum x distance between points (optional)\n> + *\n> + * The output includes whether the resulting inverse function is a proper\n> + * (true) inverse, or only a best effort (e.g. input was non-monotonic).\n> + *\n> + * \\return A pair of the inverse piecewise linear function, and whether or not\n> + * the result is a proper/true inverse\n> + */\n> +std::pair<Pwl, bool> Pwl::inverse(const double eps) const\n>  {\n>  \tbool appended = false, prepended = false, neither = false;\n>  \tPwl inverse;\n>  \n>  \tfor (Point const &p : points_) {\n> -\t\tif (inverse.empty())\n> -\t\t\tinverse.append(p.y, p.x, eps);\n> -\t\telse if (std::abs(inverse.points_.back().x - p.y) <= eps ||\n> -\t\t\t std::abs(inverse.points_.front().x - p.y) <= eps)\n> +\t\tif (inverse.empty()) {\n> +\t\t\tinverse.append(p.y(), p.x(), eps);\n> +\t\t} else if (std::abs(inverse.points_.back().x() - p.y()) <= eps ||\n> +\t\t\t   std::abs(inverse.points_.front().x() - p.y()) <= eps) {\n>  \t\t\t/* do nothing */;\n> -\t\telse if (p.y > inverse.points_.back().x) {\n> -\t\t\tinverse.append(p.y, p.x, eps);\n> +\t\t} else if (p.y() > inverse.points_.back().x()) {\n> +\t\t\tinverse.append(p.y(), p.x(), eps);\n>  \t\t\tappended = true;\n> -\t\t} else if (p.y < inverse.points_.front().x) {\n> -\t\t\tinverse.prepend(p.y, p.x, eps);\n> +\t\t} else if (p.y() < inverse.points_.front().x()) {\n> +\t\t\tinverse.prepend(p.y(), p.x(), eps);\n>  \t\t\tprepended = true;\n> -\t\t} else\n> +\t\t} else {\n>  \t\t\tneither = true;\n> +\t\t}\n>  \t}\n>  \n>  \t/*\n> @@ -155,50 +279,58 @@ Pwl Pwl::inverse(bool *trueInverse, const double eps) const\n>  \t * onto both ends of the inverse, or if there were points that couldn't\n>  \t * go on either.\n>  \t */\n> -\tif (trueInverse)\n> -\t\t*trueInverse = !(neither || (appended && prepended));\n> +\tbool trueInverse = !(neither || (appended && prepended));\n>  \n> -\treturn inverse;\n> +\treturn { inverse, trueInverse };\n>  }\n>  \n> +/**\n> + * \\brief Compose two piecewise linear functions together\n> + * \\param[in] other The \"other\" piecewise linear function\n> + * \\param[in] eps Epsilon for the minimum x distance between points (optional)\n> + *\n> + * The \"this\" function is done first, and \"other\" after.\n> + *\n> + * \\return The composed piecewise linear function\n> + */\n>  Pwl Pwl::compose(Pwl const &other, const double eps) const\n>  {\n> -\tdouble thisX = points_[0].x, thisY = points_[0].y;\n> +\tdouble thisX = points_[0].x(), thisY = points_[0].y();\n>  \tint thisSpan = 0, otherSpan = other.findSpan(thisY, 0);\n> -\tPwl result({ { thisX, other.eval(thisY, &otherSpan, false) } });\n> +\tPwl result({ Point({ thisX, other.eval(thisY, &otherSpan, false) }) });\n> +\n>  \twhile (thisSpan != (int)points_.size() - 1) {\n> -\t\tdouble dx = points_[thisSpan + 1].x - points_[thisSpan].x,\n> -\t\t       dy = points_[thisSpan + 1].y - points_[thisSpan].y;\n> +\t\tdouble dx = points_[thisSpan + 1].x() - points_[thisSpan].x(),\n> +\t\t       dy = points_[thisSpan + 1].y() - points_[thisSpan].y();\n>  \t\tif (std::abs(dy) > eps &&\n>  \t\t    otherSpan + 1 < (int)other.points_.size() &&\n> -\t\t    points_[thisSpan + 1].y >=\n> -\t\t\t    other.points_[otherSpan + 1].x + eps) {\n> +\t\t    points_[thisSpan + 1].y() >= other.points_[otherSpan + 1].x() + eps) {\n>  \t\t\t/*\n>  \t\t\t * next control point in result will be where this\n>  \t\t\t * function's y reaches the next span in other\n>  \t\t\t */\n> -\t\t\tthisX = points_[thisSpan].x +\n> -\t\t\t\t(other.points_[otherSpan + 1].x -\n> -\t\t\t\t points_[thisSpan].y) *\n> +\t\t\tthisX = points_[thisSpan].x() +\n> +\t\t\t\t(other.points_[otherSpan + 1].x() -\n> +\t\t\t\t points_[thisSpan].y()) *\n>  \t\t\t\t\tdx / dy;\n> -\t\t\tthisY = other.points_[++otherSpan].x;\n> +\t\t\tthisY = other.points_[++otherSpan].x();\n>  \t\t} else if (std::abs(dy) > eps && otherSpan > 0 &&\n> -\t\t\t   points_[thisSpan + 1].y <=\n> -\t\t\t\t   other.points_[otherSpan - 1].x - eps) {\n> +\t\t\t   points_[thisSpan + 1].y() <=\n> +\t\t\t\t   other.points_[otherSpan - 1].x() - eps) {\n>  \t\t\t/*\n>  \t\t\t * next control point in result will be where this\n>  \t\t\t * function's y reaches the previous span in other\n>  \t\t\t */\n> -\t\t\tthisX = points_[thisSpan].x +\n> -\t\t\t\t(other.points_[otherSpan + 1].x -\n> -\t\t\t\t points_[thisSpan].y) *\n> +\t\t\tthisX = points_[thisSpan].x() +\n> +\t\t\t\t(other.points_[otherSpan + 1].x() -\n> +\t\t\t\t points_[thisSpan].y()) *\n>  \t\t\t\t\tdx / dy;\n> -\t\t\tthisY = other.points_[--otherSpan].x;\n> +\t\t\tthisY = other.points_[--otherSpan].x();\n>  \t\t} else {\n>  \t\t\t/* we stay in the same span in other */\n>  \t\t\tthisSpan++;\n> -\t\t\tthisX = points_[thisSpan].x,\n> -\t\t\tthisY = points_[thisSpan].y;\n> +\t\t\tthisX = points_[thisSpan].x(),\n> +\t\t\tthisY = points_[thisSpan].y();\n>  \t\t}\n>  \t\tresult.append(thisX, other.eval(thisY, &otherSpan, false),\n>  \t\t\t      eps);\n> @@ -206,32 +338,47 @@ Pwl Pwl::compose(Pwl const &other, const double eps) const\n>  \treturn result;\n>  }\n>  \n> +/**\n> + * \\brief Apply function to (x,y) values at every control point\n> + * \\param f Function to be applied\n> + */\n>  void Pwl::map(std::function<void(double x, double y)> f) const\n>  {\n>  \tfor (auto &pt : points_)\n> -\t\tf(pt.x, pt.y);\n> +\t\tf(pt.x(), pt.y());\n>  }\n>  \n> +/**\n> + * \\brief Apply function to (x, y0, y1) values wherever either Pwl has a\n> + * control point.\n\nMissing \\param\n\n> + */\n>  void Pwl::map2(Pwl const &pwl0, Pwl const &pwl1,\n>  \t       std::function<void(double x, double y0, double y1)> f)\n>  {\n>  \tint span0 = 0, span1 = 0;\n> -\tdouble x = std::min(pwl0.points_[0].x, pwl1.points_[0].x);\n> +\tdouble x = std::min(pwl0.points_[0].x(), pwl1.points_[0].x());\n>  \tf(x, pwl0.eval(x, &span0, false), pwl1.eval(x, &span1, false));\n> +\n>  \twhile (span0 < (int)pwl0.points_.size() - 1 ||\n>  \t       span1 < (int)pwl1.points_.size() - 1) {\n>  \t\tif (span0 == (int)pwl0.points_.size() - 1)\n> -\t\t\tx = pwl1.points_[++span1].x;\n> +\t\t\tx = pwl1.points_[++span1].x();\n>  \t\telse if (span1 == (int)pwl1.points_.size() - 1)\n> -\t\t\tx = pwl0.points_[++span0].x;\n> -\t\telse if (pwl0.points_[span0 + 1].x > pwl1.points_[span1 + 1].x)\n> -\t\t\tx = pwl1.points_[++span1].x;\n> +\t\t\tx = pwl0.points_[++span0].x();\n> +\t\telse if (pwl0.points_[span0 + 1].x() > pwl1.points_[span1 + 1].x())\n> +\t\t\tx = pwl1.points_[++span1].x();\n>  \t\telse\n> -\t\t\tx = pwl0.points_[++span0].x;\n> +\t\t\tx = pwl0.points_[++span0].x();\n>  \t\tf(x, pwl0.eval(x, &span0, false), pwl1.eval(x, &span1, false));\n>  \t}\n>  }\n>  \n> +/**\n> + * \\brief Combine two Pwls\n\nMissing \\param\n\n> + *\n> + * Create a new Pwl where the y values are given by running f wherever either\n> + * has a knot.\n\nMissing \\return\n\n> + */\n>  Pwl Pwl::combine(Pwl const &pwl0, Pwl const &pwl1,\n>  \t\t std::function<double(double x, double y0, double y1)> f,\n>  \t\t const double eps)\n> @@ -243,27 +390,32 @@ Pwl Pwl::combine(Pwl const &pwl0, Pwl const &pwl1,\n>  \treturn result;\n>  }\n>  \n> -void Pwl::matchDomain(Interval const &domain, bool clip, const double eps)\n> -{\n> -\tint span = 0;\n> -\tprepend(domain.start, eval(clip ? points_[0].x : domain.start, &span),\n> -\t\teps);\n> -\tspan = points_.size() - 2;\n> -\tappend(domain.end, eval(clip ? points_.back().x : domain.end, &span),\n> -\t       eps);\n> -}\n> -\n> +/**\n> + * \\brief Multiply the piecewise linear function\n> + * \\param d Scalar multiplier to multiply the function by\n> + * \\return This function, after it has been multiplied by \\a d\n> + */\n>  Pwl &Pwl::operator*=(double d)\n>  {\n>  \tfor (auto &pt : points_)\n> -\t\tpt.y *= d;\n> +\t\tpt[1] *= d;\n\nIf you add non-const x() and y() accessors to the Vector class that\nreturn a reference, you could use\n\n\t\tpt.y() *= d;\n\nUp to you.\n\n>  \treturn *this;\n>  }\n>  \n> -void Pwl::debug(FILE *fp) const\n> +/**\n> + * \\brief Assemble and return a string describing the piecewise linear function\n> + * \\return A string describing the piecewise linear function\n> + */\n> +std::string Pwl::toString() const\n>  {\n> -\tfprintf(fp, \"Pwl {\\n\");\n> +\tstd::stringstream ss;\n> +\tss << \"Pwl { \";\n>  \tfor (auto &p : points_)\n> -\t\tfprintf(fp, \"\\t(%g, %g)\\n\", p.x, p.y);\n> -\tfprintf(fp, \"}\\n\");\n> +\t\tss << \"(\" << p.x() << \", \" << p.y() << \") \";\n> +\tss << \"}\";\n> +\treturn ss.str();\n>  }\n> +\n> +} /* namespace ipa */\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/ipa/libipa/pwl.h b/src/ipa/libipa/pwl.h\n> index 7d5e7e4d3fda..a2cbad6c1597 100644\n> --- a/src/ipa/libipa/pwl.h\n> +++ b/src/ipa/libipa/pwl.h\n> @@ -2,126 +2,87 @@\n>  /*\n>   * Copyright (C) 2019, Raspberry Pi Ltd\n>   *\n> - * piecewise linear functions interface\n> + * Piecewise linear functions interface\n>   */\n>  #pragma once\n>  \n> +#include <algorithm>\n> +#include <cmath>\n>  #include <functional>\n> -#include <math.h>\n> +#include <string>\n> +#include <utility>\n>  #include <vector>\n>  \n> +#include <libcamera/geometry.h>\n\nUnless I'm missing something, this isn't needed.\n\n> +\n>  #include \"libcamera/internal/yaml_parser.h\"\n>  \n> -namespace RPiController {\n> +#include \"vector.h\"\n> +\n> +namespace libcamera {\n> +\n> +namespace ipa {\n>  \n>  class Pwl\n>  {\n>  public:\n> +\tusing Point = Vector<double, 2>;\n> +\n>  \tstruct Interval {\n>  \t\tInterval(double _start, double _end)\n> -\t\t\t: start(_start), end(_end)\n> -\t\t{\n> -\t\t}\n> -\t\tdouble start, end;\n> +\t\t\t: start(_start), end(_end) {}\n> +\n>  \t\tbool contains(double value)\n>  \t\t{\n>  \t\t\treturn value >= start && value <= end;\n>  \t\t}\n> -\t\tdouble clip(double value)\n> -\t\t{\n> -\t\t\treturn value < start ? start\n> -\t\t\t\t\t     : (value > end ? end : value);\n> -\t\t}\n> -\t\tdouble len() const { return end - start; }\n> -\t};\n> -\tstruct Point {\n> -\t\tPoint() : x(0), y(0) {}\n> -\t\tPoint(double _x, double _y)\n> -\t\t\t: x(_x), y(_y) {}\n> -\t\tdouble x, y;\n> -\t\tPoint operator-(Point const &p) const\n> -\t\t{\n> -\t\t\treturn Point(x - p.x, y - p.y);\n> -\t\t}\n> -\t\tPoint operator+(Point const &p) const\n> -\t\t{\n> -\t\t\treturn Point(x + p.x, y + p.y);\n> -\t\t}\n> -\t\tdouble operator%(Point const &p) const\n> +\n> +\t\tdouble clamp(double value)\n>  \t\t{\n> -\t\t\treturn x * p.x + y * p.y;\n> +\t\t\treturn std::clamp(value, start, end);\n>  \t\t}\n> -\t\tPoint operator*(double f) const { return Point(x * f, y * f); }\n> -\t\tPoint operator/(double f) const { return Point(x / f, y / f); }\n> -\t\tdouble len2() const { return x * x + y * y; }\n> -\t\tdouble len() const { return sqrt(len2()); }\n> +\n> +\t\tdouble length() const { return end - start; }\n> +\n> +\t\tdouble start, end;\n>  \t};\n> +\n>  \tPwl() {}\n\n\tPwl() = default;\n\n> -\tPwl(std::vector<Point> const &points) : points_(points) {}\n> -\tint read(const libcamera::YamlObject &params);\n> +\tPwl(const std::vector<Point> &points)\n> +\t\t: points_(points) {}\n\n\tPwl(const std::vector<Point> &points)\n\t\t: points_(points)\n\t{\n\t}\n\nbut I think it would be better to not make the constructor inline. You\ncan move the implementation to the .cpp file. Same for the default\nconstructor.\n\n> +\tint readYaml(const libcamera::YamlObject &params);\n> +\n>  \tvoid append(double x, double y, const double eps = 1e-6);\n\nIs there a reason to qualify eps with const but not x and y ? I would\nqualify them all, or none (likely none). Same for other functions using\neps, I think you can drop the const qualifier.\n\n> -\tvoid prepend(double x, double y, const double eps = 1e-6);\n> +\n> +\tbool empty() const;\n>  \tInterval domain() const;\n>  \tInterval range() const;\n> -\tbool empty() const;\n> -\t/*\n> -\t * Evaluate Pwl, optionally supplying an initial guess for the\n> -\t * \"span\". The \"span\" may be optionally be updated.  If you want to know\n> -\t * the \"span\" value but don't have an initial guess you can set it to\n> -\t * -1.\n> -\t */\n> -\tdouble eval(double x, int *spanPtr = nullptr,\n> +\n> +\tdouble eval(double x, int *span = nullptr,\n>  \t\t    bool updateSpan = true) const;\n> -\t/*\n> -\t * Find perpendicular closest to xy, starting from span+1 so you can\n> -\t * call it repeatedly to check for multiple closest points (set span to\n> -\t * -1 on the first call). Also returns \"pseudo\" perpendiculars; see\n> -\t * PerpType enum.\n> -\t */\n> -\tenum class PerpType {\n> -\t\tNone, /* no perpendicular found */\n> -\t\tStart, /* start of Pwl is closest point */\n> -\t\tEnd, /* end of Pwl is closest point */\n> -\t\tVertex, /* vertex of Pwl is closest point */\n> -\t\tPerpendicular /* true perpendicular found */\n> -\t};\n> -\tPerpType invert(Point const &xy, Point &perp, int &span,\n> -\t\t\tconst double eps = 1e-6) const;\n> -\t/*\n> -\t * Compute the inverse function. Indicate if it is a proper (true)\n> -\t * inverse, or only a best effort (e.g. input was non-monotonic).\n> -\t */\n> -\tPwl inverse(bool *trueInverse = nullptr, const double eps = 1e-6) const;\n> -\t/* Compose two Pwls together, doing \"this\" first and \"other\" after. */\n> -\tPwl compose(Pwl const &other, const double eps = 1e-6) const;\n> -\t/* Apply function to (x,y) values at every control point. */\n> +\n> +\tstd::pair<Pwl, bool> inverse(const double eps = 1e-6) const;\n> +\tPwl compose(const Pwl &other, const double eps = 1e-6) const;\n> +\n>  \tvoid map(std::function<void(double x, double y)> f) const;\n> -\t/*\n> -\t * Apply function to (x, y0, y1) values wherever either Pwl has a\n> -\t * control point.\n> -\t */\n> -\tstatic void map2(Pwl const &pwl0, Pwl const &pwl1,\n> -\t\t\t std::function<void(double x, double y0, double y1)> f);\n> -\t/*\n> -\t * Combine two Pwls, meaning we create a new Pwl where the y values are\n> -\t * given by running f wherever either has a knot.\n> -\t */\n> +\n>  \tstatic Pwl\n> -\tcombine(Pwl const &pwl0, Pwl const &pwl1,\n> +\tcombine(const Pwl &pwl0, const Pwl &pwl1,\n>  \t\tstd::function<double(double x, double y0, double y1)> f,\n>  \t\tconst double eps = 1e-6);\n> -\t/*\n> -\t * Make \"this\" match (at least) the given domain. Any extension my be\n> -\t * clipped or linear.\n> -\t */\n> -\tvoid matchDomain(Interval const &domain, bool clip = true,\n> -\t\t\t const double eps = 1e-6);\n> +\n>  \tPwl &operator*=(double d);\n> -\tvoid debug(FILE *fp = stdout) const;\n> +\n> +\tstd::string toString() const;\n>  \n>  private:\n> +\tvoid prepend(double x, double y, const double eps = 1e-6);\n> +\tstatic void map2(const Pwl &pwl0, const Pwl &pwl1,\n> +\t\t\t std::function<void(double x, double y0, double y1)> f);\n\nWe usually put the static functions first or last, but not in the\nmiddle.\n\n>  \tint findSpan(double x, int span) const;\n\nAnd a blank line here to separate functions from variables.\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n>  \tstd::vector<Point> points_;\n>  };\n>  \n> -} /* namespace RPiController */\n> +} /* namespace ipa */\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 C8072C3237\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 11 Jun 2024 22:15:04 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 03BE165456;\n\tWed, 12 Jun 2024 00:15:04 +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 AB23E61A26\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 12 Jun 2024 00:15:01 +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 6DC2E230;\n\tWed, 12 Jun 2024 00:14:48 +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=\"gD0AkK1v\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1718144088;\n\tbh=7J9TE6xDtmwTAXlbKI6jww3UBa8mw2jOKn/ARGdDLp0=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=gD0AkK1v9nh6PweXIjy5SyPJFVUfYJG6oVYPWkT8lLlQ50O+2us7O+VugTPfTfymT\n\tEp7x9S+mQIhiZly649rXbikfgdR3yMaW67jOxckg+x6zWdFQmQh9Zi4/ljnphegx4Y\n\tLmgTmjBlgoYf1JPvjSzkTbT5HshhGeQxxPb5KhwQ=","Date":"Wed, 12 Jun 2024 01:14:41 +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\tDavid Plowman <david.plowman@raspberrypi.com>,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>","Subject":"Re: [PATCH v8 3/4] ipa: libipa: pwl: Clean up Pwl class to match\n\tlibcamera","Message-ID":"<20240611221441.GD8290@pendragon.ideasonboard.com>","References":"<20240611132430.404814-1-paul.elder@ideasonboard.com>\n\t<20240611132430.404814-4-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20240611132430.404814-4-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>"}},{"id":29866,"web_url":"https://patchwork.libcamera.org/comment/29866/","msgid":"<ZmlUovx4LB-X8dD8@pyrite.rasen.tech>","date":"2024-06-12T07:56:18","subject":"Re: [PATCH v8 3/4] ipa: libipa: pwl: Clean up Pwl class to match\n\tlibcamera","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/people/17/","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"content":"On Wed, Jun 12, 2024 at 01:14:41AM +0300, Laurent Pinchart wrote:\n> Hi Paul,\n> \n> Thank you for the patch.\n> \n> On Tue, Jun 11, 2024 at 10:24:29PM +0900, Paul Elder wrote:\n> > Clean up the Pwl class copied from the Raspberry Pi IPA to align it more\n> > with the libcamera style.\n> > \n> > Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>\n> > Reviewed-by: Stefan Klug <stefan.klug@ideasonboard.com>\n> > Acked-by: David Plowman <david.plowman@raspberrypi.com>\n> > Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> > \n> > ---\n> > Changes in v8:\n> > - use the updated Vector interface\n> > - remove unused functions (prepend, invert, extendDomain)\n> > - improve class documentation\n> > - checkstyle\n> > - s/PointF/Point/\n> > - make inverse() return pair instead of output parameter\n> > - fix const order\n> > - fix includes\n> > \n> > No change in v7\n> > \n> > Changes in v6:\n> > - move adding pwl to meson here\n> > \n> > Changes in v5:\n> > - fix documentation order\n> > - fix some typos\n> > - add the Vector-based PointF\n> > \n> > Changes in v4:\n> > - update to apply to new copy of pwl\n> > - add documentation\n> > - fix doxygen\n> > \n> > No change in v3\n> > \n> > Changes in v2:\n> > - s/FPoint/PointF/g\n> > - improve documentation\n> > - s/matchDomain/extendDomain/\n> > ---\n> >  src/ipa/libipa/meson.build |   2 +\n> >  src/ipa/libipa/pwl.cpp     | 372 ++++++++++++++++++++++++++-----------\n> >  src/ipa/libipa/pwl.h       | 133 +++++--------\n> >  3 files changed, 311 insertions(+), 196 deletions(-)\n> > \n> > diff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build\n> > index 8b0c8fff901b..3669f8939d3b 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> > +    'pwl.h',\n> >      'vector.h',\n> >  ])\n> >  \n> > @@ -19,6 +20,7 @@ libipa_sources = files([\n> >      'fc_queue.cpp',\n> >      'histogram.cpp',\n> >      'module.cpp',\n> > +    'pwl.cpp',\n> >      'vector.cpp',\n> >  ])\n> >  \n> > diff --git a/src/ipa/libipa/pwl.cpp b/src/ipa/libipa/pwl.cpp\n> > index e39123767aa6..4dc59981708d 100644\n> > --- a/src/ipa/libipa/pwl.cpp\n> > +++ b/src/ipa/libipa/pwl.cpp\n> > @@ -1,19 +1,120 @@\n> >  /* SPDX-License-Identifier: BSD-2-Clause */\n> >  /*\n> >   * Copyright (C) 2019, Raspberry Pi Ltd\n> > + * Copyright (C) 2024, Ideas on Board Oy\n> >   *\n> > - * piecewise linear functions\n> > + * Piecewise linear functions\n> >   */\n> >  \n> > -#include <cassert>\n> > +#include \"pwl.h\"\n> > +\n> > +#include <assert.h>\n> >  #include <cmath>\n> > +#include <sstream>\n> >  #include <stdexcept>\n> >  \n> > -#include \"pwl.h\"\n> > +#include <libcamera/geometry.h>\n> \n> Unless I'm missing something, this isn't needed.\n> \n> > +\n> > +/**\n> > + * \\file pwl.h\n> > + * \\brief Piecewise linear functions\n> > + */\n> > +\n> > +namespace libcamera {\n> > +\n> > +namespace ipa {\n> > +\n> > +/**\n> > + * \\class Pwl\n> > + * \\brief Describe a univariate piecewise linear function in two-dimensional\n> > + * real space\n> > + *\n> > + * A piecewise linear function is a univariate function that maps reals to\n> > + * reals, and it is composed of multiple straight-line segments.\n> > + *\n> > + * While a mathematical piecewise linear function would usually be defined by\n> > + * a list of linear functions and for which values of the domain they apply,\n> > + * this Pwl class is instead defined by a list of points at which these line\n> > + * segments intersect. These intersecting points are known as knots.\n> > + *\n> > + * https://en.wikipedia.org/wiki/Piecewise_linear_function\n> > + *\n> > + * A consequence of the Pwl class being defined by knots instead of linear\n> > + * functions is that the values of the piecewise linear function past the ends\n> > + * of the function are constants as opposed to linear functions. In a\n> > + * mathematical piecewise linear function that is defined by multiple linear\n> > + * functions, the ends of the function are also linear functions and hence grow\n> > + * to infinity (or negative infinity). However, since this Pwl class is defined\n> > + * by knots, the y-value of the leftmost and rightmost knots will hold for all\n> > + * x values to negative infinity and positive infinity, respectively.\n> \n> Nice documentation, I especially like the part about what happens\n> outside of the defined segments, that was not evident.\n> \n> > + */\n> > +\n> > +/**\n> > + * \\typedef Pwl::Point\n> > + * \\brief Describe a point in two-dimensional real space\n> > + */\n> > +\n> > +/**\n> > + * \\class Pwl::Interval\n> > + * \\brief Describe an interval in one-dimensional real space\n> > + */\n> > +\n> > +/**\n> > + * \\fn Pwl::Interval::Interval(double _start, double _end)\n> > + * \\brief Construct an interval\n> > + * \\param _start Start of the interval\n> > + * \\param _end End of the interval\n> > + */\n> > +\n> > +/**\n> > + * \\fn Pwl::Interval::contains\n> > + * \\brief Check if a given value falls within the interval\n> > + * \\param value Value to check\n> \n>  * \\return True if the value falls within the interval, including its\n>  * bounds, or false otherwise\n> \n> > + */\n> > +\n> > +/**\n> > + * \\fn Pwl::Interval::clamp\n> > + * \\brief Clamp a value such that it is within the interval\n> > + * \\param value Value to clamp\n> \n>  * \\return The clamped value\n> \n> > + */\n> > +\n> > +/**\n> > + * \\fn Pwl::Interval::length\n> > + * \\brief Compute the length of the interval\n> \n>  * \\return The length of the interval\n> \n> > + */\n> > +\n> > +/**\n> > + * \\var Pwl::Interval::start\n> > + * \\brief Start of the interval\n> > + */\n> >  \n> > -using namespace RPiController;\n> > +/**\n> > + * \\var Pwl::Interval::end\n> > + * \\brief End of the interval\n> > + */\n> >  \n> > -int Pwl::read(const libcamera::YamlObject &params)\n> > +/**\n> > + * \\fn Pwl::Pwl(std::vector<Point> const &points)\n> > + * \\brief Construct a piecewise linear function from a list of 2D points\n> > + * \\param points Vector of points from which to construct the piecewise linear function\n> > + *\n> > + * \\a points must be in ascending order of x-value.\n> > + */\n> > +\n> > +/**\n> > + * \\brief Populate the piecewise linear function from yaml data\n> > + * \\param params Yaml data to populate the piecewise linear function with\n> > + *\n> > + * Any existing points in the piecewise linear function will *not* be\n> > + * overwritten.\n> \n> It sounds like a bit of an off behaviour, compared to clearing the PWL\n> first. Does anything depends on it ?\n> \n\nActually, from what I can tell everything else assumes it's cleared, so\nI'll clear it first.\n\n\n> > + *\n> > + * The yaml data is expected to be a list with an even number of numerical\n> > + * elements. These will be parsed in pairs into x and y points in the piecewise\n> > + * linear function, and added in order. x must be monotonically increasing.\n> > + *\n> > + * \\return 0 on success, negative error code otherwise\n> > + */\n> > +int Pwl::readYaml(const libcamera::YamlObject &params)\n> >  {\n> >  \tif (!params.size() || params.size() % 2)\n> >  \t\treturn -EINVAL;\n> > @@ -24,64 +125,109 @@ int Pwl::read(const libcamera::YamlObject &params)\n> >  \t\tauto x = it->get<double>();\n> >  \t\tif (!x)\n> >  \t\t\treturn -EINVAL;\n> > -\t\tif (it != list.begin() && *x <= points_.back().x)\n> > +\t\tif (it != list.begin() && *x <= points_.back().x())\n> >  \t\t\treturn -EINVAL;\n> >  \n> >  \t\tauto y = (++it)->get<double>();\n> >  \t\tif (!y)\n> >  \t\t\treturn -EINVAL;\n> >  \n> > -\t\tpoints_.push_back(Point(*x, *y));\n> > +\t\tpoints_.push_back(Point({ *x, *y }));\n> >  \t}\n> >  \n> >  \treturn 0;\n> >  }\n> >  \n> > +/**\n> > + * \\brief Append a point to the end of the piecewise linear function\n> > + * \\param x x-coordinate of the point to add to the piecewise linear function\n> > + * \\param y y-coordinate of the point to add to the piecewise linear function\n> > + * \\param eps Epsilon for the minimum x distance between points (optional)\n> > + *\n> > + * The point's x-coordinate must be greater than the x-coordinate of the last\n> > + * (= greatest) point already in the piecewise linear function.\n> > + */\n> >  void Pwl::append(double x, double y, const double eps)\n> >  {\n> > -\tif (points_.empty() || points_.back().x + eps < x)\n> > -\t\tpoints_.push_back(Point(x, y));\n> > +\tif (points_.empty() || points_.back().x() + eps < x)\n> > +\t\tpoints_.push_back(Point({ x, y }));\n> >  }\n> >  \n> > +/**\n> > + * \\brief Prepend a point to the beginning of the piecewise linear function\n> > + * \\param x x-coordinate of the point to add to the piecewise linear function\n> > + * \\param y y-coordinate of the point to add to the piecewise linear function\n> > + * \\param eps Epsilon for the minimum x distance between points (optional)\n> > + *\n> > + * The point's x-coordinate must be less than the x-coordinate of the first\n> > + * (= smallest) point already in the piecewise linear function.\n> > + */\n> >  void Pwl::prepend(double x, double y, const double eps)\n> >  {\n> > -\tif (points_.empty() || points_.front().x - eps > x)\n> > -\t\tpoints_.insert(points_.begin(), Point(x, y));\n> > +\tif (points_.empty() || points_.front().x() - eps > x)\n> > +\t\tpoints_.insert(points_.begin(), Point({ x, y }));\n> >  }\n> >  \n> > +/**\n> > + * \\brief Get the domain of the piecewise linear function\n> > + * \\return An interval representing the domain\n> > + */\n> >  Pwl::Interval Pwl::domain() const\n> >  {\n> > -\treturn Interval(points_[0].x, points_[points_.size() - 1].x);\n> > +\treturn Interval(points_[0].x(), points_[points_.size() - 1].x());\n> >  }\n> >  \n> > +/**\n> > + * \\brief Get the range of the piecewise linear function\n> > + * \\return An interval representing the range\n> > + */\n> >  Pwl::Interval Pwl::range() const\n> >  {\n> > -\tdouble lo = points_[0].y, hi = lo;\n> > +\tdouble lo = points_[0].y(), hi = lo;\n> >  \tfor (auto &p : points_)\n> > -\t\tlo = std::min(lo, p.y), hi = std::max(hi, p.y);\n> > +\t\tlo = std::min(lo, p.y()), hi = std::max(hi, p.y());\n> >  \treturn Interval(lo, hi);\n> >  }\n> >  \n> > +/**\n> > + * \\brief Check if the piecewise linear function is empty\n> > + * \\return True if there are no points in the function, false otherwise\n> > + */\n> >  bool Pwl::empty() const\n> >  {\n> >  \treturn points_.empty();\n> >  }\n> >  \n> > -double Pwl::eval(double x, int *spanPtr, bool updateSpan) const\n> > +/**\n> > + * \\brief Evaluate the piecewise linear function\n> > + * \\param[in] x The x value to input into the function\n> > + * \\param[inout] span Initial guess for span\n> > + * \\param[in] updateSpan Set to true to update span\n> > + *\n> > + * Evaluate Pwl, optionally supplying an initial guess for the\n> > + * \"span\". The \"span\" may be optionally be updated. If you want to know\n> > + * the \"span\" value but don't have an initial guess you can set it to\n> > + * -1.\n> > + *\n> > + *  \\return The result of evaluating the piecewise linear function at position \\a x\n> > + */\n> > +double Pwl::eval(double x, int *span, bool updateSpan) const\n> >  {\n> > -\tint span = findSpan(x, spanPtr && *spanPtr != -1 ? *spanPtr : points_.size() / 2 - 1);\n> > -\tif (spanPtr && updateSpan)\n> > -\t\t*spanPtr = span;\n> > -\treturn points_[span].y +\n> > -\t       (x - points_[span].x) * (points_[span + 1].y - points_[span].y) /\n> > -\t\t       (points_[span + 1].x - points_[span].x);\n> > +\tint index = findSpan(x, span && *span != -1\n> > +\t\t\t\t\t? *span\n> > +\t\t\t\t\t: points_.size() / 2 - 1);\n> > +\tif (span && updateSpan)\n> > +\t\t*span = index;\n> > +\treturn points_[index].y() +\n> > +\t       (x - points_[index].x()) * (points_[index + 1].y() - points_[index].y()) /\n> > +\t\t       (points_[index + 1].x() - points_[index].x());\n> >  }\n> >  \n> >  int Pwl::findSpan(double x, int span) const\n> >  {\n> >  \t/*\n> >  \t * Pwls are generally small, so linear search may well be faster than\n> > -\t * binary, though could review this if large PWls start turning up.\n> > +\t * binary, though could review this if large Pwls start turning up.\n> >  \t */\n> >  \tint lastSpan = points_.size() - 2;\n> >  \t/*\n> > @@ -89,65 +235,43 @@ int Pwl::findSpan(double x, int span) const\n> >  \t * control point\n> >  \t */\n> >  \tspan = std::max(0, std::min(lastSpan, span));\n> > -\twhile (span < lastSpan && x >= points_[span + 1].x)\n> > +\twhile (span < lastSpan && x >= points_[span + 1].x())\n> >  \t\tspan++;\n> > -\twhile (span && x < points_[span].x)\n> > +\twhile (span && x < points_[span].x())\n> >  \t\tspan--;\n> >  \treturn span;\n> >  }\n> >  \n> > -Pwl::PerpType Pwl::invert(Point const &xy, Point &perp, int &span,\n> > -\t\t\t  const double eps) const\n> > -{\n> > -\tassert(span >= -1);\n> > -\tbool prevOffEnd = false;\n> > -\tfor (span = span + 1; span < (int)points_.size() - 1; span++) {\n> > -\t\tPoint spanVec = points_[span + 1] - points_[span];\n> > -\t\tdouble t = ((xy - points_[span]) % spanVec) / spanVec.len2();\n> > -\t\tif (t < -eps) /* off the start of this span */\n> > -\t\t{\n> > -\t\t\tif (span == 0) {\n> > -\t\t\t\tperp = points_[span];\n> > -\t\t\t\treturn PerpType::Start;\n> > -\t\t\t} else if (prevOffEnd) {\n> > -\t\t\t\tperp = points_[span];\n> > -\t\t\t\treturn PerpType::Vertex;\n> > -\t\t\t}\n> > -\t\t} else if (t > 1 + eps) /* off the end of this span */\n> > -\t\t{\n> > -\t\t\tif (span == (int)points_.size() - 2) {\n> > -\t\t\t\tperp = points_[span + 1];\n> > -\t\t\t\treturn PerpType::End;\n> > -\t\t\t}\n> > -\t\t\tprevOffEnd = true;\n> > -\t\t} else /* a true perpendicular */\n> > -\t\t{\n> > -\t\t\tperp = points_[span] + spanVec * t;\n> > -\t\t\treturn PerpType::Perpendicular;\n> > -\t\t}\n> > -\t}\n> > -\treturn PerpType::None;\n> > -}\n> > -\n> > -Pwl Pwl::inverse(bool *trueInverse, const double eps) const\n> > +/**\n> > + * \\brief Compute the inverse function\n> > + * \\param[in] eps Epsilon for the minimum x distance between points (optional)\n> > + *\n> > + * The output includes whether the resulting inverse function is a proper\n> > + * (true) inverse, or only a best effort (e.g. input was non-monotonic).\n> > + *\n> > + * \\return A pair of the inverse piecewise linear function, and whether or not\n> > + * the result is a proper/true inverse\n> > + */\n> > +std::pair<Pwl, bool> Pwl::inverse(const double eps) const\n> >  {\n> >  \tbool appended = false, prepended = false, neither = false;\n> >  \tPwl inverse;\n> >  \n> >  \tfor (Point const &p : points_) {\n> > -\t\tif (inverse.empty())\n> > -\t\t\tinverse.append(p.y, p.x, eps);\n> > -\t\telse if (std::abs(inverse.points_.back().x - p.y) <= eps ||\n> > -\t\t\t std::abs(inverse.points_.front().x - p.y) <= eps)\n> > +\t\tif (inverse.empty()) {\n> > +\t\t\tinverse.append(p.y(), p.x(), eps);\n> > +\t\t} else if (std::abs(inverse.points_.back().x() - p.y()) <= eps ||\n> > +\t\t\t   std::abs(inverse.points_.front().x() - p.y()) <= eps) {\n> >  \t\t\t/* do nothing */;\n> > -\t\telse if (p.y > inverse.points_.back().x) {\n> > -\t\t\tinverse.append(p.y, p.x, eps);\n> > +\t\t} else if (p.y() > inverse.points_.back().x()) {\n> > +\t\t\tinverse.append(p.y(), p.x(), eps);\n> >  \t\t\tappended = true;\n> > -\t\t} else if (p.y < inverse.points_.front().x) {\n> > -\t\t\tinverse.prepend(p.y, p.x, eps);\n> > +\t\t} else if (p.y() < inverse.points_.front().x()) {\n> > +\t\t\tinverse.prepend(p.y(), p.x(), eps);\n> >  \t\t\tprepended = true;\n> > -\t\t} else\n> > +\t\t} else {\n> >  \t\t\tneither = true;\n> > +\t\t}\n> >  \t}\n> >  \n> >  \t/*\n> > @@ -155,50 +279,58 @@ Pwl Pwl::inverse(bool *trueInverse, const double eps) const\n> >  \t * onto both ends of the inverse, or if there were points that couldn't\n> >  \t * go on either.\n> >  \t */\n> > -\tif (trueInverse)\n> > -\t\t*trueInverse = !(neither || (appended && prepended));\n> > +\tbool trueInverse = !(neither || (appended && prepended));\n> >  \n> > -\treturn inverse;\n> > +\treturn { inverse, trueInverse };\n> >  }\n> >  \n> > +/**\n> > + * \\brief Compose two piecewise linear functions together\n> > + * \\param[in] other The \"other\" piecewise linear function\n> > + * \\param[in] eps Epsilon for the minimum x distance between points (optional)\n> > + *\n> > + * The \"this\" function is done first, and \"other\" after.\n> > + *\n> > + * \\return The composed piecewise linear function\n> > + */\n> >  Pwl Pwl::compose(Pwl const &other, const double eps) const\n> >  {\n> > -\tdouble thisX = points_[0].x, thisY = points_[0].y;\n> > +\tdouble thisX = points_[0].x(), thisY = points_[0].y();\n> >  \tint thisSpan = 0, otherSpan = other.findSpan(thisY, 0);\n> > -\tPwl result({ { thisX, other.eval(thisY, &otherSpan, false) } });\n> > +\tPwl result({ Point({ thisX, other.eval(thisY, &otherSpan, false) }) });\n> > +\n> >  \twhile (thisSpan != (int)points_.size() - 1) {\n> > -\t\tdouble dx = points_[thisSpan + 1].x - points_[thisSpan].x,\n> > -\t\t       dy = points_[thisSpan + 1].y - points_[thisSpan].y;\n> > +\t\tdouble dx = points_[thisSpan + 1].x() - points_[thisSpan].x(),\n> > +\t\t       dy = points_[thisSpan + 1].y() - points_[thisSpan].y();\n> >  \t\tif (std::abs(dy) > eps &&\n> >  \t\t    otherSpan + 1 < (int)other.points_.size() &&\n> > -\t\t    points_[thisSpan + 1].y >=\n> > -\t\t\t    other.points_[otherSpan + 1].x + eps) {\n> > +\t\t    points_[thisSpan + 1].y() >= other.points_[otherSpan + 1].x() + eps) {\n> >  \t\t\t/*\n> >  \t\t\t * next control point in result will be where this\n> >  \t\t\t * function's y reaches the next span in other\n> >  \t\t\t */\n> > -\t\t\tthisX = points_[thisSpan].x +\n> > -\t\t\t\t(other.points_[otherSpan + 1].x -\n> > -\t\t\t\t points_[thisSpan].y) *\n> > +\t\t\tthisX = points_[thisSpan].x() +\n> > +\t\t\t\t(other.points_[otherSpan + 1].x() -\n> > +\t\t\t\t points_[thisSpan].y()) *\n> >  \t\t\t\t\tdx / dy;\n> > -\t\t\tthisY = other.points_[++otherSpan].x;\n> > +\t\t\tthisY = other.points_[++otherSpan].x();\n> >  \t\t} else if (std::abs(dy) > eps && otherSpan > 0 &&\n> > -\t\t\t   points_[thisSpan + 1].y <=\n> > -\t\t\t\t   other.points_[otherSpan - 1].x - eps) {\n> > +\t\t\t   points_[thisSpan + 1].y() <=\n> > +\t\t\t\t   other.points_[otherSpan - 1].x() - eps) {\n> >  \t\t\t/*\n> >  \t\t\t * next control point in result will be where this\n> >  \t\t\t * function's y reaches the previous span in other\n> >  \t\t\t */\n> > -\t\t\tthisX = points_[thisSpan].x +\n> > -\t\t\t\t(other.points_[otherSpan + 1].x -\n> > -\t\t\t\t points_[thisSpan].y) *\n> > +\t\t\tthisX = points_[thisSpan].x() +\n> > +\t\t\t\t(other.points_[otherSpan + 1].x() -\n> > +\t\t\t\t points_[thisSpan].y()) *\n> >  \t\t\t\t\tdx / dy;\n> > -\t\t\tthisY = other.points_[--otherSpan].x;\n> > +\t\t\tthisY = other.points_[--otherSpan].x();\n> >  \t\t} else {\n> >  \t\t\t/* we stay in the same span in other */\n> >  \t\t\tthisSpan++;\n> > -\t\t\tthisX = points_[thisSpan].x,\n> > -\t\t\tthisY = points_[thisSpan].y;\n> > +\t\t\tthisX = points_[thisSpan].x(),\n> > +\t\t\tthisY = points_[thisSpan].y();\n> >  \t\t}\n> >  \t\tresult.append(thisX, other.eval(thisY, &otherSpan, false),\n> >  \t\t\t      eps);\n> > @@ -206,32 +338,47 @@ Pwl Pwl::compose(Pwl const &other, const double eps) const\n> >  \treturn result;\n> >  }\n> >  \n> > +/**\n> > + * \\brief Apply function to (x,y) values at every control point\n> > + * \\param f Function to be applied\n> > + */\n> >  void Pwl::map(std::function<void(double x, double y)> f) const\n> >  {\n> >  \tfor (auto &pt : points_)\n> > -\t\tf(pt.x, pt.y);\n> > +\t\tf(pt.x(), pt.y());\n> >  }\n> >  \n> > +/**\n> > + * \\brief Apply function to (x, y0, y1) values wherever either Pwl has a\n> > + * control point.\n> \n> Missing \\param\n> \n> > + */\n> >  void Pwl::map2(Pwl const &pwl0, Pwl const &pwl1,\n> >  \t       std::function<void(double x, double y0, double y1)> f)\n> >  {\n> >  \tint span0 = 0, span1 = 0;\n> > -\tdouble x = std::min(pwl0.points_[0].x, pwl1.points_[0].x);\n> > +\tdouble x = std::min(pwl0.points_[0].x(), pwl1.points_[0].x());\n> >  \tf(x, pwl0.eval(x, &span0, false), pwl1.eval(x, &span1, false));\n> > +\n> >  \twhile (span0 < (int)pwl0.points_.size() - 1 ||\n> >  \t       span1 < (int)pwl1.points_.size() - 1) {\n> >  \t\tif (span0 == (int)pwl0.points_.size() - 1)\n> > -\t\t\tx = pwl1.points_[++span1].x;\n> > +\t\t\tx = pwl1.points_[++span1].x();\n> >  \t\telse if (span1 == (int)pwl1.points_.size() - 1)\n> > -\t\t\tx = pwl0.points_[++span0].x;\n> > -\t\telse if (pwl0.points_[span0 + 1].x > pwl1.points_[span1 + 1].x)\n> > -\t\t\tx = pwl1.points_[++span1].x;\n> > +\t\t\tx = pwl0.points_[++span0].x();\n> > +\t\telse if (pwl0.points_[span0 + 1].x() > pwl1.points_[span1 + 1].x())\n> > +\t\t\tx = pwl1.points_[++span1].x();\n> >  \t\telse\n> > -\t\t\tx = pwl0.points_[++span0].x;\n> > +\t\t\tx = pwl0.points_[++span0].x();\n> >  \t\tf(x, pwl0.eval(x, &span0, false), pwl1.eval(x, &span1, false));\n> >  \t}\n> >  }\n> >  \n> > +/**\n> > + * \\brief Combine two Pwls\n> \n> Missing \\param\n> \n> > + *\n> > + * Create a new Pwl where the y values are given by running f wherever either\n> > + * has a knot.\n> \n> Missing \\return\n> \n> > + */\n> >  Pwl Pwl::combine(Pwl const &pwl0, Pwl const &pwl1,\n> >  \t\t std::function<double(double x, double y0, double y1)> f,\n> >  \t\t const double eps)\n> > @@ -243,27 +390,32 @@ Pwl Pwl::combine(Pwl const &pwl0, Pwl const &pwl1,\n> >  \treturn result;\n> >  }\n> >  \n> > -void Pwl::matchDomain(Interval const &domain, bool clip, const double eps)\n> > -{\n> > -\tint span = 0;\n> > -\tprepend(domain.start, eval(clip ? points_[0].x : domain.start, &span),\n> > -\t\teps);\n> > -\tspan = points_.size() - 2;\n> > -\tappend(domain.end, eval(clip ? points_.back().x : domain.end, &span),\n> > -\t       eps);\n> > -}\n> > -\n> > +/**\n> > + * \\brief Multiply the piecewise linear function\n> > + * \\param d Scalar multiplier to multiply the function by\n> > + * \\return This function, after it has been multiplied by \\a d\n> > + */\n> >  Pwl &Pwl::operator*=(double d)\n> >  {\n> >  \tfor (auto &pt : points_)\n> > -\t\tpt.y *= d;\n> > +\t\tpt[1] *= d;\n> \n> If you add non-const x() and y() accessors to the Vector class that\n> return a reference, you could use\n> \n> \t\tpt.y() *= d;\n> \n> Up to you.\n\nEeh I'll go without it.\n\n\nPaul\n\n> \n> >  \treturn *this;\n> >  }\n> >  \n> > -void Pwl::debug(FILE *fp) const\n> > +/**\n> > + * \\brief Assemble and return a string describing the piecewise linear function\n> > + * \\return A string describing the piecewise linear function\n> > + */\n> > +std::string Pwl::toString() const\n> >  {\n> > -\tfprintf(fp, \"Pwl {\\n\");\n> > +\tstd::stringstream ss;\n> > +\tss << \"Pwl { \";\n> >  \tfor (auto &p : points_)\n> > -\t\tfprintf(fp, \"\\t(%g, %g)\\n\", p.x, p.y);\n> > -\tfprintf(fp, \"}\\n\");\n> > +\t\tss << \"(\" << p.x() << \", \" << p.y() << \") \";\n> > +\tss << \"}\";\n> > +\treturn ss.str();\n> >  }\n> > +\n> > +} /* namespace ipa */\n> > +\n> > +} /* namespace libcamera */\n> > diff --git a/src/ipa/libipa/pwl.h b/src/ipa/libipa/pwl.h\n> > index 7d5e7e4d3fda..a2cbad6c1597 100644\n> > --- a/src/ipa/libipa/pwl.h\n> > +++ b/src/ipa/libipa/pwl.h\n> > @@ -2,126 +2,87 @@\n> >  /*\n> >   * Copyright (C) 2019, Raspberry Pi Ltd\n> >   *\n> > - * piecewise linear functions interface\n> > + * Piecewise linear functions interface\n> >   */\n> >  #pragma once\n> >  \n> > +#include <algorithm>\n> > +#include <cmath>\n> >  #include <functional>\n> > -#include <math.h>\n> > +#include <string>\n> > +#include <utility>\n> >  #include <vector>\n> >  \n> > +#include <libcamera/geometry.h>\n> \n> Unless I'm missing something, this isn't needed.\n> \n> > +\n> >  #include \"libcamera/internal/yaml_parser.h\"\n> >  \n> > -namespace RPiController {\n> > +#include \"vector.h\"\n> > +\n> > +namespace libcamera {\n> > +\n> > +namespace ipa {\n> >  \n> >  class Pwl\n> >  {\n> >  public:\n> > +\tusing Point = Vector<double, 2>;\n> > +\n> >  \tstruct Interval {\n> >  \t\tInterval(double _start, double _end)\n> > -\t\t\t: start(_start), end(_end)\n> > -\t\t{\n> > -\t\t}\n> > -\t\tdouble start, end;\n> > +\t\t\t: start(_start), end(_end) {}\n> > +\n> >  \t\tbool contains(double value)\n> >  \t\t{\n> >  \t\t\treturn value >= start && value <= end;\n> >  \t\t}\n> > -\t\tdouble clip(double value)\n> > -\t\t{\n> > -\t\t\treturn value < start ? start\n> > -\t\t\t\t\t     : (value > end ? end : value);\n> > -\t\t}\n> > -\t\tdouble len() const { return end - start; }\n> > -\t};\n> > -\tstruct Point {\n> > -\t\tPoint() : x(0), y(0) {}\n> > -\t\tPoint(double _x, double _y)\n> > -\t\t\t: x(_x), y(_y) {}\n> > -\t\tdouble x, y;\n> > -\t\tPoint operator-(Point const &p) const\n> > -\t\t{\n> > -\t\t\treturn Point(x - p.x, y - p.y);\n> > -\t\t}\n> > -\t\tPoint operator+(Point const &p) const\n> > -\t\t{\n> > -\t\t\treturn Point(x + p.x, y + p.y);\n> > -\t\t}\n> > -\t\tdouble operator%(Point const &p) const\n> > +\n> > +\t\tdouble clamp(double value)\n> >  \t\t{\n> > -\t\t\treturn x * p.x + y * p.y;\n> > +\t\t\treturn std::clamp(value, start, end);\n> >  \t\t}\n> > -\t\tPoint operator*(double f) const { return Point(x * f, y * f); }\n> > -\t\tPoint operator/(double f) const { return Point(x / f, y / f); }\n> > -\t\tdouble len2() const { return x * x + y * y; }\n> > -\t\tdouble len() const { return sqrt(len2()); }\n> > +\n> > +\t\tdouble length() const { return end - start; }\n> > +\n> > +\t\tdouble start, end;\n> >  \t};\n> > +\n> >  \tPwl() {}\n> \n> \tPwl() = default;\n> \n> > -\tPwl(std::vector<Point> const &points) : points_(points) {}\n> > -\tint read(const libcamera::YamlObject &params);\n> > +\tPwl(const std::vector<Point> &points)\n> > +\t\t: points_(points) {}\n> \n> \tPwl(const std::vector<Point> &points)\n> \t\t: points_(points)\n> \t{\n> \t}\n> \n> but I think it would be better to not make the constructor inline. You\n> can move the implementation to the .cpp file. Same for the default\n> constructor.\n> \n> > +\tint readYaml(const libcamera::YamlObject &params);\n> > +\n> >  \tvoid append(double x, double y, const double eps = 1e-6);\n> \n> Is there a reason to qualify eps with const but not x and y ? I would\n> qualify them all, or none (likely none). Same for other functions using\n> eps, I think you can drop the const qualifier.\n> \n> > -\tvoid prepend(double x, double y, const double eps = 1e-6);\n> > +\n> > +\tbool empty() const;\n> >  \tInterval domain() const;\n> >  \tInterval range() const;\n> > -\tbool empty() const;\n> > -\t/*\n> > -\t * Evaluate Pwl, optionally supplying an initial guess for the\n> > -\t * \"span\". The \"span\" may be optionally be updated.  If you want to know\n> > -\t * the \"span\" value but don't have an initial guess you can set it to\n> > -\t * -1.\n> > -\t */\n> > -\tdouble eval(double x, int *spanPtr = nullptr,\n> > +\n> > +\tdouble eval(double x, int *span = nullptr,\n> >  \t\t    bool updateSpan = true) const;\n> > -\t/*\n> > -\t * Find perpendicular closest to xy, starting from span+1 so you can\n> > -\t * call it repeatedly to check for multiple closest points (set span to\n> > -\t * -1 on the first call). Also returns \"pseudo\" perpendiculars; see\n> > -\t * PerpType enum.\n> > -\t */\n> > -\tenum class PerpType {\n> > -\t\tNone, /* no perpendicular found */\n> > -\t\tStart, /* start of Pwl is closest point */\n> > -\t\tEnd, /* end of Pwl is closest point */\n> > -\t\tVertex, /* vertex of Pwl is closest point */\n> > -\t\tPerpendicular /* true perpendicular found */\n> > -\t};\n> > -\tPerpType invert(Point const &xy, Point &perp, int &span,\n> > -\t\t\tconst double eps = 1e-6) const;\n> > -\t/*\n> > -\t * Compute the inverse function. Indicate if it is a proper (true)\n> > -\t * inverse, or only a best effort (e.g. input was non-monotonic).\n> > -\t */\n> > -\tPwl inverse(bool *trueInverse = nullptr, const double eps = 1e-6) const;\n> > -\t/* Compose two Pwls together, doing \"this\" first and \"other\" after. */\n> > -\tPwl compose(Pwl const &other, const double eps = 1e-6) const;\n> > -\t/* Apply function to (x,y) values at every control point. */\n> > +\n> > +\tstd::pair<Pwl, bool> inverse(const double eps = 1e-6) const;\n> > +\tPwl compose(const Pwl &other, const double eps = 1e-6) const;\n> > +\n> >  \tvoid map(std::function<void(double x, double y)> f) const;\n> > -\t/*\n> > -\t * Apply function to (x, y0, y1) values wherever either Pwl has a\n> > -\t * control point.\n> > -\t */\n> > -\tstatic void map2(Pwl const &pwl0, Pwl const &pwl1,\n> > -\t\t\t std::function<void(double x, double y0, double y1)> f);\n> > -\t/*\n> > -\t * Combine two Pwls, meaning we create a new Pwl where the y values are\n> > -\t * given by running f wherever either has a knot.\n> > -\t */\n> > +\n> >  \tstatic Pwl\n> > -\tcombine(Pwl const &pwl0, Pwl const &pwl1,\n> > +\tcombine(const Pwl &pwl0, const Pwl &pwl1,\n> >  \t\tstd::function<double(double x, double y0, double y1)> f,\n> >  \t\tconst double eps = 1e-6);\n> > -\t/*\n> > -\t * Make \"this\" match (at least) the given domain. Any extension my be\n> > -\t * clipped or linear.\n> > -\t */\n> > -\tvoid matchDomain(Interval const &domain, bool clip = true,\n> > -\t\t\t const double eps = 1e-6);\n> > +\n> >  \tPwl &operator*=(double d);\n> > -\tvoid debug(FILE *fp = stdout) const;\n> > +\n> > +\tstd::string toString() const;\n> >  \n> >  private:\n> > +\tvoid prepend(double x, double y, const double eps = 1e-6);\n> > +\tstatic void map2(const Pwl &pwl0, const Pwl &pwl1,\n> > +\t\t\t std::function<void(double x, double y0, double y1)> f);\n> \n> We usually put the static functions first or last, but not in the\n> middle.\n> \n> >  \tint findSpan(double x, int span) const;\n> \n> And a blank line here to separate functions from variables.\n> \n> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> \n> >  \tstd::vector<Point> points_;\n> >  };\n> >  \n> > -} /* namespace RPiController */\n> > +} /* namespace ipa */\n> > +\n> > +} /* namespace libcamera */\n> \n> -- \n> Regards,\n> \n> Laurent Pinchart","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 C47C2BD87C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 12 Jun 2024 07:56:29 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 0505F6545A;\n\tWed, 12 Jun 2024 09:56:29 +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 EF9496545A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 12 Jun 2024 09:56:26 +0200 (CEST)","from pyrite.rasen.tech (h175-177-049-156.catv02.itscom.jp\n\t[175.177.49.156])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id BACB44D0;\n\tWed, 12 Jun 2024 09:56:11 +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=\"MtD1790Q\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1718178973;\n\tbh=dfYo/4zx/8JoHU3dku7Xi26jFQZgRyMx/oXZ0n88M08=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=MtD1790QNrvcHoIemga4LmgVBFS1KMruk1SM4Ptgi37PVTsMvr6pRdjRuaM+ygm5n\n\tN9xs846I6t6RkK2OKotzqa5kAXl1bFUISydQE0ItBHMx5CauWDPgonOScdupRBiDIv\n\tloo1eKwv/TTOC/07LEL0oIkM0ODOPIO7JFGISpNg=","Date":"Wed, 12 Jun 2024 16:56:18 +0900","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org,\n\tStefan Klug <stefan.klug@ideasonboard.com>,\n\tDavid Plowman <david.plowman@raspberrypi.com>,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>","Subject":"Re: [PATCH v8 3/4] ipa: libipa: pwl: Clean up Pwl class to match\n\tlibcamera","Message-ID":"<ZmlUovx4LB-X8dD8@pyrite.rasen.tech>","References":"<20240611132430.404814-1-paul.elder@ideasonboard.com>\n\t<20240611132430.404814-4-paul.elder@ideasonboard.com>\n\t<20240611221441.GD8290@pendragon.ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=us-ascii","Content-Disposition":"inline","In-Reply-To":"<20240611221441.GD8290@pendragon.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>"}}]