[PATCH 02/10] libcamera: vector: Add cast function
Stefan Klug
stefan.klug at ideasonboard.com
Mon Feb 17 11:01:43 CET 2025
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 at ideasonboard.com>
---
include/libcamera/internal/vector.h | 9 +++++++++
src/libcamera/vector.cpp | 10 ++++++++++
2 files changed, 19 insertions(+)
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
--
2.43.0
More information about the libcamera-devel
mailing list