template math functions in pimathmatrix.h and pimathvector.h and pimathbase.h

add PIMathMatrixT::rotate for matrix 2x2
This commit is contained in:
2020-10-22 17:29:58 +03:00
parent f5652efc32
commit fbe850abf0
4 changed files with 34 additions and 20 deletions

View File

@@ -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 <typename T> inline constexpr T toDb(T val) {return T(10.) * std::log10(val);}
template <typename T> inline constexpr T fromDb(T val) {return std::pow(T(10.), val / T(10.));}
template <typename T> inline constexpr T toRad(T deg) {return deg * T(M_PI_180);}
template <typename T> inline constexpr T toDeg(T rad) {return rad * T(M_180_PI);}
template <typename T> 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<double> abs(const PIVector<double> & v) {
PIVector<double> result;
template<typename T> inline PIVector<T> piAbs(const PIVector<T> & v) {
PIVector<T> result;
result.resize(v.size());
for (uint i = 0; i < v.size(); i++)
result[i] = fabs(v[i]);
result[i] = piAbs<T>(v[i]);
return result;
}

View File

@@ -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<Type>(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<Type>(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];
};

View File

@@ -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<Type>(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<Type>(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<Type>(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());

View File

@@ -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;
}