[02/10] libcamera: vector: Add cast function
diff mbox series

Message ID 20250217100203.297894-3-stefan.klug@ideasonboard.com
State New
Headers show
Series
  • Some rkisp1 awb improvements
Related show

Commit Message

Stefan Klug Feb. 17, 2025, 10:01 a.m. UTC
The libcamera code base uses matrices and vectors of type float and
double. Add a cast function to easily convert between different
underlying types.

Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>
---
 include/libcamera/internal/vector.h |  9 +++++++++
 src/libcamera/vector.cpp            | 10 ++++++++++
 2 files changed, 19 insertions(+)

Patch
diff mbox series

diff --git a/include/libcamera/internal/vector.h b/include/libcamera/internal/vector.h
index a67a09474204..c8f9a8c412f2 100644
--- a/include/libcamera/internal/vector.h
+++ b/include/libcamera/internal/vector.h
@@ -243,6 +243,15 @@  public:
 		return std::accumulate(data_.begin(), data_.end(), R{});
 	}
 
+	template<typename T2>
+	Vector<T2, Rows> cast() const
+	{
+		Vector<T2, Rows> ret;
+		for (unsigned int i = 0; i < Rows; i++)
+			ret[i] = static_cast<T2>(data_[i]);
+		return ret;
+	}
+
 private:
 	template<class BinaryOp>
 	static constexpr Vector apply(const Vector &lhs, const Vector &rhs, BinaryOp op)
diff --git a/src/libcamera/vector.cpp b/src/libcamera/vector.cpp
index 85ca2208245a..57dcef8bf3d8 100644
--- a/src/libcamera/vector.cpp
+++ b/src/libcamera/vector.cpp
@@ -202,6 +202,16 @@  LOG_DEFINE_CATEGORY(Vector)
  * \return The element-wise maximum of this vector and \a scalar
  */
 
+/**
+ * \fn template<typename T2> Vector<T2, Rows> Vector::cast() const
+ * \brief Cast the vector to a different type
+ *
+ * This function returns a new vector with the same size and values but a
+ * different type.
+ *
+ * \return The new vector
+ */
+
 /**
  * \fn Vector::dot(const Vector<T, Rows> &other) const
  * \brief Compute the dot product