@@ -185,6 +185,13 @@ public:
return apply(*this, [](T a, T b) -> T { return std::max(a, b); }, scalar);
}
+ constexpr Vector clamp(T low, T high) const
+ {
+ return apply(*this,
+ [](T v, T lo, T hi) -> T { return std::clamp(v, lo, hi); },
+ low, high);
+ }
+
constexpr T dot(const Vector<T, Rows> &other) const
{
T ret = 0;
@@ -224,6 +224,14 @@ LOG_DEFINE_CATEGORY(Vector)
* \return The element-wise maximum of this vector and \a scalar
*/
+/**
+ * \fn Vector::clamp(T low, T high) const
+ * \brief Clamp the vector element-wise between \a low and \a high
+ * \param[in] low The lower limit
+ * \param[in] high The upper limit
+ * \return A vector with each element clamped between \a low and \a high
+ */
+
/**
* \fn Vector::dot(const Vector<T, Rows> &other) const
* \brief Compute the dot product
@@ -72,6 +72,7 @@ protected:
ASSERT_EQ(v2.min(4.0), (Vector<double, 3>{{ 1.0, 4.0, 4.0 }}));
ASSERT_EQ(v2.max(v3), (Vector<double, 3>{{ 4.0, 4.0, 8.0 }}));
ASSERT_EQ(v2.max(4.0), (Vector<double, 3>{{ 4.0, 4.0, 8.0 }}));
+ ASSERT_EQ(v2.clamp(2.0, 6.0), (Vector<double, 3>{{ 2.0, 4.0, 6.0 }}));
ASSERT_EQ(v2.dot(v3), 52.0);