diff --git a/libs/main/math/pimathbase.h b/libs/main/math/pimathbase.h index 0841ac4c..553437f5 100644 --- a/libs/main/math/pimathbase.h +++ b/libs/main/math/pimathbase.h @@ -98,9 +98,6 @@ const double rad2deg = M_180_PI; inline int sign(const float & x) {return (x < 0.) ? -1 : (x > 0. ? 1 : 0);} inline int sign(const double & x) {return (x < 0.) ? -1 : (x > 0. ? 1 : 0);} inline int pow2(const int p) {return 1 << p;} -inline int sqr(const int v) {return v * v;} -inline float sqr(const float & v) {return v * v;} -inline double sqr(const double & v) {return v * v;} inline double sinc(const double & v) {if (v == 0.) return 1.; double t = M_PI * v; return sin(t) / t;} PIP_EXPORT double piJ0(const double & v); @@ -109,22 +106,23 @@ PIP_EXPORT double piJn(int n, const double & v); PIP_EXPORT double piY0(const double & v); PIP_EXPORT double piY1(const double & v); PIP_EXPORT double piYn(int n, const double & v); -inline double toDb(double val) {return 10. * log10(val);} -inline double fromDb(double val) {return pow(10., val / 10.);} -inline double toRad(double deg) {return deg * M_PI_180;} -inline double toDeg(double rad) {return rad * M_180_PI;} + +template inline constexpr T toDb(T val) {return T(10.) * std::log10(val);} +template inline constexpr T fromDb(T val) {return std::pow(T(10.), val / T(10.));} +template inline constexpr T toRad(T deg) {return deg * T(M_PI_180);} +template inline constexpr T toDeg(T rad) {return rad * T(M_180_PI);} +template inline constexpr T sqr(const T & v) {return v * v;} // [-1 ; 1] PIP_EXPORT double randomd(); // [-1 ; 1] normal PIP_EXPORT double randomn(double dv = 0., double sv = 1.); - -inline PIVector abs(const PIVector & v) { - PIVector result; +template inline PIVector piAbs(const PIVector & v) { + PIVector result; result.resize(v.size()); for (uint i = 0; i < v.size(); i++) - result[i] = fabs(v[i]); + result[i] = piAbs(v[i]); return result; } diff --git a/libs/main/math/pimathmatrix.h b/libs/main/math/pimathmatrix.h index b495d90d..2d9dffd1 100644 --- a/libs/main/math/pimathmatrix.h +++ b/libs/main/math/pimathmatrix.h @@ -425,7 +425,7 @@ public: for (uint k = i; k < Cols; ++k) smat.m[k][j] -= mul * smat.m[k][i]; } if (i < Cols - 1) { - if (fabs(smat.m[i + 1][i + 1]) < Type(1E-200)) { + if (piAbs(smat.m[i + 1][i + 1]) < Type(1E-200)) { if (ok != 0) *ok = false; return *this; } @@ -469,7 +469,7 @@ public: for (uint k = 0; k < Cols; ++k) mtmp.m[k][j] -= mul * mtmp.m[k][i]; } if (i < Cols - 1) { - if (fabs(smat.m[i + 1][i + 1]) < Type(1E-200)) { + if (piAbs(smat.m[i + 1][i + 1]) < Type(1E-200)) { if (ok != 0) *ok = false; return *this; } @@ -513,6 +513,18 @@ public: return tm; } + _CMatrix rotate(Type angle) { + static_assert(Rows == 2 && Cols == 2, "Works only with 2x2 matrix"); + Type c = std::cos(angle); + Type s = std::sin(angle); + PIMathMatrixT<2u, 2u> tm; + tm[0][0] = tm[1][1] = c; + tm[0][1] = -s; + tm[1][0] = s; + *this = *this * tm; + return *this; + } + private: Type m[Rows][Cols]; }; diff --git a/libs/main/math/pimathvector.h b/libs/main/math/pimathvector.h index a19ae5e3..547b03bb 100644 --- a/libs/main/math/pimathvector.h +++ b/libs/main/math/pimathvector.h @@ -69,7 +69,7 @@ public: PIMV_FOR tv += c[i] * c[i]; return tv; } - Type length() const {return sqrt(lengthSqr());} + Type length() const {return std::sqrt(lengthSqr());} Type manhattanLength() const { Type tv(0); PIMV_FOR tv += piAbs(c[i]); @@ -82,10 +82,10 @@ public: } Type angleSin(const _CVector & v) const { Type tv = angleCos(v); - return sqrt(Type(1) - tv * tv); + return std::sqrt(Type(1) - tv * tv); } - Type angleRad(const _CVector & v) const {return acos(angleCos(v));} - Type angleDeg(const _CVector & v) const {return toDeg(angleRad(v));} + Type angleRad(const _CVector & v) const {return std::acos(angleCos(v));} + Type angleDeg(const _CVector & v) const {return toDeg(angleRad(v));} Type angleElevation(const _CVector & v) const {return 90.0 - angleDeg(v - *this);} _CVector projection(const _CVector & v) { Type tv = v.length(); @@ -302,7 +302,7 @@ public: PIMV_FOR tv += c[i] * c[i]; return tv; } - Type length() const {return sqrt(lengthSqr());} + Type length() const {return std::sqrt(lengthSqr());} Type manhattanLength() const { Type tv(0); PIMV_FOR tv += piAbs(c[i]); @@ -317,9 +317,9 @@ public: Type angleSin(const _CVector & v) const { assert(c.size() == v.size()); Type tv = angleCos(v); - return sqrt(Type(1) - tv * tv); + return std::sqrt(Type(1) - tv * tv); } - Type angleRad(const _CVector & v) const {return acos(angleCos(v));} + Type angleRad(const _CVector & v) const {return std::acos(angleCos(v));} Type angleDeg(const _CVector & v) const {return toDeg(angleRad(v));} _CVector projection(const _CVector & v) { assert(c.size() == v.size()); diff --git a/main.cpp b/main.cpp index 10dfb678..0f49cc37 100644 --- a/main.cpp +++ b/main.cpp @@ -45,5 +45,9 @@ int main() { PIMathVectord v2({3,2,1}); piCout << v; piCout << v2; + PIMathMatrixT22d m = PIMathMatrixT22d::identity(); + piCout << m; + m.rotate(toRad(90.)); + piCout << m; return 0; }