Commit fa16f248 authored by Philip Trettner's avatar Philip Trettner

after long discussion, added pos + pos = pos

parent cce3e0eb
......@@ -2,7 +2,6 @@
#include <typed-geometry/detail/macros.hh>
#include <typed-geometry/detail/scalar_traits.hh>
#include <typed-geometry/detail/sum_of_pos.hh>
#include <typed-geometry/types/dir.hh>
#include <typed-geometry/types/pos.hh>
#include <typed-geometry/types/size.hh>
......@@ -36,13 +35,10 @@ TG_IMPL_DEFINE_BINARY_OP_SCALAR(pos, +);
TG_IMPL_DEFINE_BINARY_OP_SCALAR(pos, *);
TG_IMPL_DEFINE_BINARY_OP_SCALAR_DIV(pos);
// special pos+pos handling
template <int D, class ScalarT>
[[nodiscard]] constexpr sum_of_pos<D, ScalarT> operator+(pos<D, ScalarT> const& a, pos<D, ScalarT> const& b)
{
return sum_of_pos(a) + b;
}
// deprecated / not supported operations
// pos + pos results in a pos
// NOTE: this should only be used if the result is still a proper position
// e.g. for weighted sums, such as "pos * 0.3 + pos * 0.7"
// or as a shortcut for "tanslation(pos) * pos"
TG_IMPL_DEFINE_BINARY_OP(pos, pos, pos, +);
} // namespace tg
#pragma once
#include <typed-geometry/types/pos.hh>
#include <typed-geometry/types/vec.hh>
namespace tg
{
/*
* sum_of_pos is a helper struct for guaranteeing syntactic soundness when dealing with sums of positions
* it is the result of operator+(pos, pos)
* it can only be added onto other sum_of_pos
* to get back to a pos one needs to use operator/(sum_of_pos, ScalarT)
*/
template <int D, class ScalarT>
struct sum_of_pos
{
using scalar_t = ScalarT;
using div_t = decltype(ScalarT() / 0.0f);
using pos_t = pos<D, ScalarT>;
constexpr sum_of_pos() = default;
/* implicit */ constexpr sum_of_pos(pos_t const& p) : accum(p) {}
constexpr auto operator/(div_t const& rhs) const
{
using T = std::common_type_t<ScalarT, div_t>;
return pos<D, T>(accum) / T(rhs);
}
constexpr auto operator*(div_t const& rhs) const
{
using T = std::common_type_t<ScalarT, div_t>;
return pos<D, T>(accum) * T(rhs);
}
constexpr sum_of_pos operator+(sum_of_pos const& rhs) const { return accum + vec(rhs); }
constexpr sum_of_pos operator+(pos_t const& rhs) const { return accum + vec(rhs); }
private:
pos_t accum;
};
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment