[PATCH 1/4] libcamera: geometry: Add floating-point version of Point class

Paul Elder paul.elder at ideasonboard.com
Fri Apr 5 10:02:56 CEST 2024


The piecewise linear function (Pwl) class from the Raspberry Pi IPA has
its own Point class while one already exists in libcamera's geometry.h
header. The reason is because libcamera's Point is on the plane of
integer, while Raspberry Pi's is on the plane of reals.

While making this a template class might be cleaner, it was deemed to be
too intrusive of a change, and it might not feel nice to need to specify
the type from a public API point of view. Hence we introduce a FPoint
class to designate a Point class on the plane of reals.

This is in preparation for copying/moving the Pwl class from the
Raspberry Pi IPA to libipa.

Signed-off-by: Paul Elder <paul.elder at ideasonboard.com>
---
 include/libcamera/geometry.h | 50 ++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)

diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h
index d7fdbe70..7d0c0f23 100644
--- a/include/libcamera/geometry.h
+++ b/include/libcamera/geometry.h
@@ -8,6 +8,7 @@
 #pragma once
 
 #include <algorithm>
+#include <cmath>
 #include <ostream>
 #include <string>
 
@@ -49,6 +50,55 @@ static inline bool operator!=(const Point &lhs, const Point &rhs)
 
 std::ostream &operator<<(std::ostream &out, const Point &p);
 
+struct FPoint {
+	constexpr FPoint()
+		: x(0), y(0)
+	{
+	}
+
+	constexpr FPoint(double _x, double _y)
+		: x(_x), y(_y)
+	{
+	}
+
+	constexpr FPoint operator-(FPoint const &p) const
+	{
+		return FPoint(x - p.x, y - p.y);
+	}
+
+	constexpr FPoint operator+(FPoint const &p) const
+	{
+		return FPoint(x + p.x, y + p.y);
+	}
+
+	constexpr double operator%(FPoint const &p) const
+	{
+		return x * p.x + y * p.y;
+	}
+
+	constexpr FPoint operator*(double f) const
+	{
+		return FPoint(x * f, y * f);
+	}
+
+	constexpr FPoint operator/(double f) const
+	{
+		return FPoint(x / f, y / f);
+	}
+
+	constexpr double len2() const
+	{
+		return x * x + y * y;
+	}
+
+	constexpr double len() const
+	{
+		return std::sqrt(len2());
+	}
+
+	double x, y;
+};
+
 class Size
 {
 public:
-- 
2.39.2



More information about the libcamera-devel mailing list