summaryrefslogtreecommitdiff
path: root/modules/codec/c++/math.hpp
diff options
context:
space:
mode:
authorClaudius "keldu" Holeksa <mail@keldu.de>2026-11-07 18:05:26 +0100
committerClaudius "keldu" Holeksa <mail@keldu.de>2026-11-07 18:05:26 +0100
commit2e836ead5c10f00917560e9a98d46b59b2f977d4 (patch)
treeeba00a1337dbca7db3937c56d7ba9f20f9a9d146 /modules/codec/c++/math.hpp
parentbeed9c20da5fbacf8626e2d4b747bb84ae7f6d93 (diff)
downloadforstio-forstio-2e836ead5c10f00917560e9a98d46b59b2f977d4.tar.gz
Fixing double bracketing
Diffstat (limited to 'modules/codec/c++/math.hpp')
-rw-r--r--modules/codec/c++/math.hpp62
1 files changed, 57 insertions, 5 deletions
diff --git a/modules/codec/c++/math.hpp b/modules/codec/c++/math.hpp
index 1d627e6..f831256 100644
--- a/modules/codec/c++/math.hpp
+++ b/modules/codec/c++/math.hpp
@@ -36,12 +36,30 @@ public:
};
namespace math {
-/*
+/**
+ * Builds a scalar from its native value. Shorthand for the two step
+ * data<T>{value} -> data<Scalar<T>>{...} conversion, which the language won't
+ * do implicitly.
+ */
template<typename T, typename Encoding = FORSTIO_DEFAULT_DATA_ENCODING>
-data<typename T::InnerType,Encoding> norm_2(const data<T,Encoding>& d){
- return {};
+constexpr data<schema::Scalar<T>, Encoding> make_scalar(typename native_data_type<T>::type value){
+ return data<schema::Scalar<T>, Encoding>{data<T,Encoding>{value}};
+}
+
+/**
+ * Builds a vector from its native component values. The dimension is deduced
+ * from the argument count.
+ */
+template<typename T, typename Encoding = FORSTIO_DEFAULT_DATA_ENCODING, typename... Args>
+constexpr data<schema::Vector<T,sizeof...(Args)>, Encoding> make_vector(Args... args){
+ return data<schema::Vector<T,sizeof...(Args)>, Encoding>{
+ data<schema::FixedArray<T,sizeof...(Args)>, Encoding>{
+ std::array<data<T,Encoding>, sizeof...(Args)>{
+ data<T,Encoding>{static_cast<typename native_data_type<T>::type>(args)}...
+ }
+ }
+ };
}
-*/
template<typename T, uint64_t D, typename Encoding = FORSTIO_DEFAULT_DATA_ENCODING>
data<schema::Scalar<T>, Encoding> dot(const data<schema::Vector<T,D>, Encoding>& left, const data<schema::Vector<T,D>, Encoding>& right){
@@ -61,6 +79,14 @@ data<schema::Scalar<T>,Encoding> sqrt(const data<schema::Scalar<T>,Encoding>& in
return out;
}
+/**
+ * Euclidean length of a vector.
+ */
+template<typename T, uint64_t D, typename Encoding = FORSTIO_DEFAULT_DATA_ENCODING>
+data<schema::Scalar<T>, Encoding> norm(const data<schema::Vector<T,D>, Encoding>& input){
+ return sqrt<T,Encoding>(dot<T,D,Encoding>(input,input));
+}
+
template<typename T, uint64_t D, typename Encoding = FORSTIO_DEFAULT_DATA_ENCODING>
data<schema::Vector<T,D>, Encoding> normalize(const data<schema::Vector<T,D>>& input ) {
auto inp_dot = dot<T,D,Encoding>(input,input);
@@ -174,6 +200,32 @@ data<schema::Scalar<T>,Encoding> sin(
}
+/**
+ * Angle of a 2d vector measured counter clockwise from the positive x axis.
+ * Inverse of unit_vector().
+ */
+template<typename T, typename Encoding = FORSTIO_DEFAULT_DATA_ENCODING>
+data<schema::Scalar<T>,Encoding> angle(
+ const data<schema::Vector<T,2u>, Encoding>& vec
+){
+ data<schema::Scalar<T>,Encoding> ret;
+ ret.at({}).set(std::atan2(vec.at({{1u}}).get(), vec.at({{0u}}).get()));
+ return ret;
+}
+
+/**
+ * Unit length 2d vector pointing along the given angle. Inverse of angle().
+ */
+template<typename T, typename Encoding = FORSTIO_DEFAULT_DATA_ENCODING>
+data<schema::Vector<T,2u>,Encoding> unit_vector(
+ const data<schema::Scalar<T>, Encoding>& rot
+){
+ data<schema::Vector<T,2u>,Encoding> ret;
+ ret.at({{0u}}) = cos<T,Encoding>(rot).at({});
+ ret.at({{1u}}) = sin<T,Encoding>(rot).at({});
+ return ret;
+}
+
template<typename T, typename Encoding = FORSTIO_DEFAULT_DATA_ENCODING>
data<schema::Vector<T,2u>,Encoding> rotate(
const data<schema::Vector<T,2u>, Encoding> vec,
@@ -192,7 +244,7 @@ data<schema::Vector<T,D>,Encoding> scale(
){
data<schema::Vector<T,D>, Encoding> sc_vec;
for(uint64_t i = 0u; i< D; ++i){
- sc_vec.at({{i}}) = vec.at({{i}}) * scale;
+ sc_vec.at({{i}}) = vec.at({{i}}) * scale.at({});
}
return sc_vec;
}