175 lines
2.1 KiB
C++
Executable File
175 lines
2.1 KiB
C++
Executable File
//
|
|
// Math.inl
|
|
// amoeba
|
|
//
|
|
// Created by Timothy Prepscius on 12/30/16.
|
|
// Copyright © 2016 Timothy Prepscius. All rights reserved.
|
|
//
|
|
|
|
// inline
|
|
|
|
#pragma once
|
|
|
|
#include "Real.hpp"
|
|
|
|
#include <cmath>
|
|
#include <algorithm>
|
|
|
|
namespace tjp {
|
|
namespace core {
|
|
namespace math {
|
|
|
|
template<typename T>
|
|
T clamp(const T &t, T l=0, T r=1)
|
|
{
|
|
return std::min(std::max(t, l), r);
|
|
}
|
|
|
|
template<typename Real>
|
|
Real ceil(const Real &r)
|
|
{
|
|
return std::ceil(r);
|
|
}
|
|
|
|
template<typename Real>
|
|
Real floor(const Real &r)
|
|
{
|
|
return std::floor(r);
|
|
}
|
|
|
|
template<typename Real>
|
|
Real trunc(const Real &r)
|
|
{
|
|
return std::trunc(r);
|
|
}
|
|
|
|
|
|
template<typename Real>
|
|
Real pow2(const Real &r)
|
|
{
|
|
return r*r;
|
|
}
|
|
|
|
template<typename Real>
|
|
Real min_value(const Real &lhs, const Real &rhs)
|
|
{
|
|
if (lhs < rhs)
|
|
return lhs;
|
|
|
|
return rhs;
|
|
}
|
|
|
|
template<typename Real>
|
|
Real max_value(const Real &lhs, const Real &rhs)
|
|
{
|
|
if (lhs > rhs)
|
|
return lhs;
|
|
|
|
return rhs;
|
|
}
|
|
|
|
|
|
template<typename Real>
|
|
Real sign_of(const Real & a)
|
|
{
|
|
if (a < 0)
|
|
return (Real)-1;
|
|
|
|
return 1;
|
|
}
|
|
|
|
template<typename Real>
|
|
Real abs(const Real & a)
|
|
{
|
|
return std::abs(a);
|
|
}
|
|
|
|
template<typename Real>
|
|
Real sin(const Real & a)
|
|
{
|
|
return std::sin(a);
|
|
}
|
|
|
|
// inline
|
|
template<typename Real>
|
|
Real cos(const Real & a)
|
|
{
|
|
return std::cos(a);
|
|
}
|
|
|
|
// inline
|
|
template<typename Real>
|
|
Real sqrt(const Real &v)
|
|
{
|
|
return std::sqrt(v);
|
|
}
|
|
|
|
// inline
|
|
template<typename Real>
|
|
Real acos(const Real & v)
|
|
{
|
|
return std::acos(v);
|
|
}
|
|
|
|
// inline
|
|
template<typename Real>
|
|
Real atan(const Real & v)
|
|
{
|
|
return std::atan(v);
|
|
}
|
|
|
|
// inline
|
|
template<typename Real>
|
|
Real atan2(const Real & v, const Real & e)
|
|
{
|
|
return std::atan2(v, e);
|
|
}
|
|
|
|
|
|
// inline
|
|
template<typename Real>
|
|
Real hypot(const Real & v, const Real & e)
|
|
{
|
|
return std::hypot(v, e);
|
|
}
|
|
|
|
template<typename Real>
|
|
Real length(const Real & r)
|
|
{
|
|
return abs(r);
|
|
}
|
|
|
|
template<typename Real>
|
|
Real lengthSquared(Real r)
|
|
{
|
|
return r*r;
|
|
}
|
|
|
|
// ----
|
|
|
|
template<>
|
|
inline
|
|
u128 abs(const u128 &v)
|
|
{
|
|
return v;
|
|
}
|
|
|
|
template<>
|
|
inline
|
|
s128 abs(const s128 &v)
|
|
{
|
|
return v >= 0 ? v : -v;
|
|
}
|
|
|
|
|
|
template<typename Real>
|
|
Real fract(Real r)
|
|
{
|
|
Real _;
|
|
return std::modf(r, &_);
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace
|
|
} // namespace
|