diff options
Diffstat (limited to 'src/libslic3r/Point.hpp')
-rw-r--r-- | src/libslic3r/Point.hpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/libslic3r/Point.hpp b/src/libslic3r/Point.hpp index e095f1c75..e511a6316 100644 --- a/src/libslic3r/Point.hpp +++ b/src/libslic3r/Point.hpp @@ -114,6 +114,7 @@ public: Point& operator+=(const Point& rhs) { (*this)(0) += rhs(0); (*this)(1) += rhs(1); return *this; } Point& operator-=(const Point& rhs) { (*this)(0) -= rhs(0); (*this)(1) -= rhs(1); return *this; } Point& operator*=(const double &rhs) { (*this)(0) = coord_t((*this)(0) * rhs); (*this)(1) = coord_t((*this)(1) * rhs); return *this; } + Point operator*(const double &rhs) { return Point((*this)(0) * rhs, (*this)(1) * rhs); } void rotate(double angle); void rotate(double angle, const Point ¢er); @@ -288,6 +289,72 @@ private: std::ostream& operator<<(std::ostream &stm, const Vec2d &pointf); + +// ///////////////////////////////////////////////////////////////////////////// +// Type safe conversions to and from scaled and unscaled coordinates +// ///////////////////////////////////////////////////////////////////////////// + +// Semantics are the following: +// Upscaling (scaled()): only from floating point types (or Vec) to either +// floating point or integer 'scaled coord' coordinates. +// Downscaling (unscaled()): from arithmetic (or Vec) to floating point only + +// Conversion definition from unscaled to floating point scaled +template<class Tout, + class Tin, + class = FloatingOnly<Tin>> +inline constexpr FloatingOnly<Tout> scaled(const Tin &v) noexcept +{ + return Tout(v / Tin(SCALING_FACTOR)); +} + +// Conversion definition from unscaled to integer 'scaled coord'. +// TODO: is the rounding necessary? Here it is commented out to show that +// it can be different for integers but it does not have to be. Using +// std::round means loosing noexcept and constexpr modifiers +template<class Tout = coord_t, class Tin, class = FloatingOnly<Tin>> +inline constexpr ScaledCoordOnly<Tout> scaled(const Tin &v) noexcept +{ + //return static_cast<Tout>(std::round(v / SCALING_FACTOR)); + return Tout(v / Tin(SCALING_FACTOR)); +} + +// Conversion for Eigen vectors (N dimensional points) +template<class Tout = coord_t, + class Tin, + int N, + class = FloatingOnly<Tin>, + int...EigenArgs> +inline Eigen::Matrix<ArithmeticOnly<Tout>, N, EigenArgs...> +scaled(const Eigen::Matrix<Tin, N, EigenArgs...> &v) +{ + return (v / SCALING_FACTOR).template cast<Tout>(); +} + +// Conversion from arithmetic scaled type to floating point unscaled +template<class Tout = double, + class Tin, + class = ArithmeticOnly<Tin>, + class = FloatingOnly<Tout>> +inline constexpr Tout unscaled(const Tin &v) noexcept +{ + return Tout(v * Tout(SCALING_FACTOR)); +} + +// Unscaling for Eigen vectors. Input base type can be arithmetic, output base +// type can only be floating point. +template<class Tout = double, + class Tin, + int N, + class = ArithmeticOnly<Tin>, + class = FloatingOnly<Tout>, + int...EigenArgs> +inline constexpr Eigen::Matrix<Tout, N, EigenArgs...> +unscaled(const Eigen::Matrix<Tin, N, EigenArgs...> &v) noexcept +{ + return v.template cast<Tout>() * SCALING_FACTOR; +} + } // namespace Slic3r // start Boost |