enable complex type for PIMathVectorT and PIMathMatrixT
TODO: add precision to invert and test vector
This commit is contained in:
@@ -36,6 +36,12 @@
|
||||
|
||||
using std::complex;
|
||||
|
||||
template<typename T>
|
||||
struct is_complex: std::false_type {};
|
||||
|
||||
template<typename T>
|
||||
struct is_complex<std::complex<T>>: std::true_type {};
|
||||
|
||||
typedef complex<int> complexi;
|
||||
typedef complex<short> complexs;
|
||||
typedef complex<float> complexf;
|
||||
|
||||
@@ -63,7 +63,7 @@ class PIP_EXPORT PIMathMatrixT {
|
||||
typedef PIMathMatrixT<Cols, Rows, Type> _CMatrixI;
|
||||
typedef PIMathVectorT<Rows, Type> _CMCol;
|
||||
typedef PIMathVectorT<Cols, Type> _CMRow;
|
||||
static_assert(std::is_arithmetic<Type>::value, "Type must be arithmetic");
|
||||
static_assert(std::is_arithmetic<Type>::value || is_complex<Type>::value, "Type must be arithmetic or complex");
|
||||
static_assert(Rows > 0, "Row count must be > 0");
|
||||
static_assert(Cols > 0, "Column count must be > 0");
|
||||
|
||||
@@ -386,7 +386,7 @@ public:
|
||||
//! \brief Деление с присваиванием с матрицей `v`.
|
||||
//! \param sm матрица для деления с присваиванием.
|
||||
void operator/=(const Type & v) {
|
||||
assert(piAbs<Type>(v) > PIMATHVECTOR_ZERO_CMP);
|
||||
assert(std::abs(v) > PIMATHVECTOR_ZERO_CMP);
|
||||
PIMM_FOR m[r][c] /= v;
|
||||
}
|
||||
|
||||
@@ -453,7 +453,7 @@ public:
|
||||
//! \param v делитель.
|
||||
//! \return результат деления.
|
||||
PIMathMatrixT<Rows, Cols, Type> operator/(const Type & v) const {
|
||||
assert(piAbs<Type>(v) > PIMATHVECTOR_ZERO_CMP);
|
||||
assert(std::abs(v) > PIMATHVECTOR_ZERO_CMP);
|
||||
PIMathMatrixT<Rows, Cols, Type> tm = PIMathMatrixT<Rows, Cols, Type>(*this);
|
||||
PIMM_FOR tm.m[r][c] /= v;
|
||||
return tm;
|
||||
@@ -517,7 +517,7 @@ public:
|
||||
for (uint i = 0; i < Cols; ++i) {
|
||||
ndet = true;
|
||||
for (uint j = 0; j < Rows; ++j)
|
||||
if (smat.m[i][j] != 0) ndet = false;
|
||||
if (smat.m[i][j] != Type{}) ndet = false;
|
||||
if (ndet) {
|
||||
if (ok != 0) *ok = false;
|
||||
return *this;
|
||||
@@ -525,15 +525,16 @@ public:
|
||||
}
|
||||
for (uint i = 0; i < Cols; ++i) {
|
||||
crow = i;
|
||||
while (smat.m[i][i] == Type(0))
|
||||
while (smat.m[i][i] == Type{}) {
|
||||
smat.swapRows(i, ++crow);
|
||||
}
|
||||
for (uint j = i + 1; j < Rows; ++j) {
|
||||
mul = smat.m[i][j] / smat.m[i][i];
|
||||
for (uint k = i; k < Cols; ++k)
|
||||
smat.m[k][j] -= mul * smat.m[k][i];
|
||||
}
|
||||
if (i < Cols - 1) {
|
||||
if (piAbs<Type>(smat.m[i + 1][i + 1]) < Type(1E-200)) {
|
||||
if (std::abs(smat.m[i + 1][i + 1]) < PIMATHVECTOR_ZERO_CMP) {
|
||||
if (ok != 0) *ok = false;
|
||||
return *this;
|
||||
}
|
||||
@@ -562,8 +563,9 @@ public:
|
||||
Type mul, iddiv;
|
||||
for (uint i = 0; i < Cols; ++i) {
|
||||
ndet = true;
|
||||
for (uint j = 0; j < Rows; ++j)
|
||||
if (smat.m[i][j] != 0) ndet = false;
|
||||
for (uint j = 0; j < Rows; ++j) {
|
||||
if (std::abs(smat.m[i][j]) >= PIMATHVECTOR_ZERO_CMP) ndet = false;
|
||||
}
|
||||
if (ndet) {
|
||||
if (ok != 0) *ok = false;
|
||||
return *this;
|
||||
@@ -571,7 +573,7 @@ public:
|
||||
}
|
||||
for (uint i = 0; i < Cols; ++i) {
|
||||
crow = i;
|
||||
while (smat.m[i][i] == Type(0)) {
|
||||
while (std::abs(smat.m[i][i]) < PIMATHVECTOR_ZERO_CMP) {
|
||||
++crow;
|
||||
smat.swapRows(i, crow);
|
||||
mtmp.swapRows(i, crow);
|
||||
@@ -584,7 +586,7 @@ public:
|
||||
mtmp.m[k][j] -= mul * mtmp.m[k][i];
|
||||
}
|
||||
if (i < Cols - 1) {
|
||||
if (piAbs<Type>(smat.m[i + 1][i + 1]) < Type(1E-200)) {
|
||||
if (std::abs(smat.m[i + 1][i + 1]) < PIMATHVECTOR_ZERO_CMP) {
|
||||
if (ok != 0) *ok = false;
|
||||
return *this;
|
||||
}
|
||||
@@ -650,7 +652,7 @@ public:
|
||||
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;
|
||||
PIMathMatrixT<2u, 2u, Type> tm;
|
||||
tm[0][0] = tm[1][1] = c;
|
||||
tm[0][1] = -s;
|
||||
tm[1][0] = s;
|
||||
@@ -671,7 +673,7 @@ public:
|
||||
PIMathMatrixT<Cols, Rows, Type> outm;
|
||||
Type c = std::cos(angle);
|
||||
Type s = std::sin(angle);
|
||||
PIMathMatrixT<2u, 2u> tm;
|
||||
PIMathMatrixT<2u, 2u, Type> tm;
|
||||
tm[0][0] = tm[1][1] = c;
|
||||
tm[0][1] = -s;
|
||||
tm[1][0] = s;
|
||||
|
||||
@@ -27,12 +27,13 @@
|
||||
#define PIMATHVECTOR_H
|
||||
|
||||
#include "pimathbase.h"
|
||||
#include "pimathcomplex.h"
|
||||
|
||||
|
||||
template<uint Cols, uint Rows, typename Type>
|
||||
class PIMathMatrixT;
|
||||
|
||||
#define PIMATHVECTOR_ZERO_CMP Type(1E-100)
|
||||
#define PIMATHVECTOR_ZERO_CMP (1E-100)
|
||||
|
||||
|
||||
/// Vector templated
|
||||
@@ -42,7 +43,7 @@ class PIMathMatrixT;
|
||||
template<uint Size, typename Type = double>
|
||||
class PIP_EXPORT PIMathVectorT {
|
||||
typedef PIMathVectorT<Size, Type> _CVector;
|
||||
static_assert(std::is_arithmetic<Type>::value, "Type must be arithmetic");
|
||||
static_assert(std::is_arithmetic<Type>::value || is_complex<Type>::value, "Type must be arithmetic or complex");
|
||||
static_assert(Size > 0, "Size must be > 0");
|
||||
|
||||
public:
|
||||
@@ -83,35 +84,71 @@ public:
|
||||
PIMV_FOR tv += c[i] * c[i];
|
||||
return tv;
|
||||
}
|
||||
Type length() const { return std::sqrt(lengthSqr()); }
|
||||
|
||||
Type length() const {
|
||||
static_assert(std::is_arithmetic<Type>::value, "Unavailable for complex");
|
||||
if (std::is_arithmetic<Type>::value) return std::sqrt(lengthSqr());
|
||||
// if (is_complex<Type>::value) return 1000.; // std::sqrt(lengthSqr());
|
||||
}
|
||||
|
||||
Type manhattanLength() const {
|
||||
Type tv(0);
|
||||
PIMV_FOR tv += piAbs<Type>(c[i]);
|
||||
return tv;
|
||||
static_assert(std::is_arithmetic<Type>::value, "Unavailable for complex");
|
||||
if (std::is_arithmetic<Type>::value) {
|
||||
Type tv(0);
|
||||
PIMV_FOR tv += piAbs<Type>(c[i]);
|
||||
return tv;
|
||||
}
|
||||
}
|
||||
Type angleCos(const _CVector & v) const {
|
||||
Type tv = v.length() * length();
|
||||
assert(piAbs<Type>(tv) > PIMATHVECTOR_ZERO_CMP);
|
||||
return dot(v) / tv;
|
||||
static_assert(std::is_arithmetic<Type>::value, "Unavailable for complex");
|
||||
if (std::is_arithmetic<Type>::value) {
|
||||
Type tv = v.length() * length();
|
||||
assert(piAbs<Type>(tv) > PIMATHVECTOR_ZERO_CMP);
|
||||
return dot(v) / tv;
|
||||
}
|
||||
}
|
||||
Type angleSin(const _CVector & v) const {
|
||||
Type tv = angleCos(v);
|
||||
return std::sqrt(Type(1) - tv * tv);
|
||||
static_assert(std::is_arithmetic<Type>::value, "Unavailable for complex");
|
||||
if (std::is_arithmetic<Type>::value) {
|
||||
Type tv = angleCos(v);
|
||||
return std::sqrt(Type(1) - tv * tv);
|
||||
}
|
||||
}
|
||||
Type angleRad(const _CVector & v) const {
|
||||
static_assert(std::is_arithmetic<Type>::value, "Unavailable for complex");
|
||||
if (std::is_arithmetic<Type>::value) {
|
||||
return std::acos(angleCos(v));
|
||||
}
|
||||
}
|
||||
Type angleDeg(const _CVector & v) const {
|
||||
static_assert(std::is_arithmetic<Type>::value, "Unavailable for complex");
|
||||
if (std::is_arithmetic<Type>::value) {
|
||||
return toDeg(angleRad(v));
|
||||
}
|
||||
}
|
||||
Type angleElevation(const _CVector & v) const {
|
||||
static_assert(std::is_arithmetic<Type>::value, "Unavailable for complex");
|
||||
if (std::is_arithmetic<Type>::value) {
|
||||
return 90.0 - angleDeg(v - *this);
|
||||
}
|
||||
}
|
||||
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();
|
||||
assert(piAbs<Type>(tv) > PIMATHVECTOR_ZERO_CMP);
|
||||
return v * (dot(v) / tv);
|
||||
static_assert(std::is_arithmetic<Type>::value, "Unavailable for complex");
|
||||
if (std::is_arithmetic<Type>::value) {
|
||||
Type tv = v.length();
|
||||
assert(piAbs<Type>(tv) > PIMATHVECTOR_ZERO_CMP);
|
||||
return v * (dot(v) / tv);
|
||||
}
|
||||
}
|
||||
_CVector & normalize() {
|
||||
Type tv = length();
|
||||
assert(piAbs<Type>(tv) > PIMATHVECTOR_ZERO_CMP);
|
||||
if (tv == Type(1)) return *this;
|
||||
PIMV_FOR c[i] /= tv;
|
||||
return *this;
|
||||
static_assert(std::is_arithmetic<Type>::value, "Unavailable for complex");
|
||||
if (std::is_arithmetic<Type>::value) {
|
||||
Type tv = length();
|
||||
assert(piAbs<Type>(tv) > PIMATHVECTOR_ZERO_CMP);
|
||||
if (tv == Type(1)) return *this;
|
||||
PIMV_FOR c[i] /= tv;
|
||||
return *this;
|
||||
}
|
||||
}
|
||||
_CVector normalized() {
|
||||
_CVector tv(*this);
|
||||
@@ -119,10 +156,10 @@ public:
|
||||
return tv;
|
||||
}
|
||||
bool isNull() const {
|
||||
PIMV_FOR if (c[i] != Type(0)) return false;
|
||||
PIMV_FOR if (c[i] != Type{}) return false;
|
||||
return true;
|
||||
}
|
||||
bool isOrtho(const _CVector & v) const { return ((*this) ^ v) == Type(0); }
|
||||
bool isOrtho(const _CVector & v) const { return ((*this) ^ v) == Type{}; }
|
||||
|
||||
Type & operator[](uint index) { return c[index]; }
|
||||
const Type & operator[](uint index) const { return c[index]; }
|
||||
@@ -145,7 +182,7 @@ public:
|
||||
void operator-=(const _CVector & v) { PIMV_FOR c[i] -= v[i]; }
|
||||
void operator*=(const Type & v) { PIMV_FOR c[i] *= v; }
|
||||
void operator/=(const Type & v) {
|
||||
assert(piAbs<Type>(v) > PIMATHVECTOR_ZERO_CMP);
|
||||
assert(std::abs(v) > PIMATHVECTOR_ZERO_CMP);
|
||||
PIMV_FOR c[i] /= v;
|
||||
}
|
||||
_CVector operator-() const {
|
||||
@@ -169,7 +206,7 @@ public:
|
||||
return tv;
|
||||
}
|
||||
_CVector operator/(const Type & v) const {
|
||||
assert(piAbs<Type>(v) > PIMATHVECTOR_ZERO_CMP);
|
||||
assert(std::abs(v) > PIMATHVECTOR_ZERO_CMP);
|
||||
_CVector tv = _CVector(*this);
|
||||
PIMV_FOR tv[i] /= v;
|
||||
return tv;
|
||||
@@ -184,7 +221,7 @@ public:
|
||||
return tv;
|
||||
}
|
||||
Type dot(const _CVector & v) const {
|
||||
Type tv(0);
|
||||
Type tv{};
|
||||
PIMV_FOR tv += c[i] * v[i];
|
||||
return tv;
|
||||
}
|
||||
@@ -197,7 +234,7 @@ public:
|
||||
_CVector div(const _CVector & v) const {
|
||||
_CVector tv(*this);
|
||||
PIMV_FOR {
|
||||
assert(piAbs<Type>(v[i]) > PIMATHVECTOR_ZERO_CMP);
|
||||
assert(std::abs(v[i]) > PIMATHVECTOR_ZERO_CMP);
|
||||
tv[i] /= v[i];
|
||||
}
|
||||
return tv;
|
||||
@@ -211,11 +248,14 @@ public:
|
||||
}
|
||||
|
||||
Type distToLine(const _CVector & lp0, const _CVector & lp1) {
|
||||
_CVector a(lp0, lp1);
|
||||
Type tv = a.length();
|
||||
assert(piAbs<Type>(tv) > PIMATHVECTOR_ZERO_CMP);
|
||||
_CVector b(lp0, *this);
|
||||
return piAbs<Type>(a[0] * b[1] - a[1] * b[0]) / tv;
|
||||
static_assert(std::is_arithmetic<Type>::value, "Unavailable for complex");
|
||||
if (std::is_arithmetic<Type>::value) {
|
||||
_CVector a(lp0, lp1);
|
||||
Type tv = a.length();
|
||||
assert(std::abs(tv) > PIMATHVECTOR_ZERO_CMP);
|
||||
_CVector b(lp0, *this);
|
||||
return piAbs<Type>(a[0] * b[1] - a[1] * b[0]) / tv;
|
||||
}
|
||||
}
|
||||
|
||||
template<uint Size1, typename Type1> /// vector {Size, Type} to vector {Size1, Type1}
|
||||
@@ -394,7 +434,7 @@ public:
|
||||
Type angleCos(const _CVector & v) const {
|
||||
assert(c.size() == v.size());
|
||||
Type tv = v.length() * length();
|
||||
assert(piAbs<Type>(tv) > PIMATHVECTOR_ZERO_CMP);
|
||||
assert(std::abs(tv) > PIMATHVECTOR_ZERO_CMP);
|
||||
return dot(v) / tv;
|
||||
}
|
||||
Type angleSin(const _CVector & v) const {
|
||||
@@ -407,12 +447,12 @@ public:
|
||||
_CVector projection(const _CVector & v) {
|
||||
assert(c.size() == v.size());
|
||||
Type tv = v.length();
|
||||
assert(piAbs<Type>(tv) > PIMATHVECTOR_ZERO_CMP);
|
||||
assert(std::abs(tv) > PIMATHVECTOR_ZERO_CMP);
|
||||
return v * (dot(v) / tv);
|
||||
}
|
||||
_CVector & normalize() {
|
||||
Type tv = length();
|
||||
assert(piAbs<Type>(tv) > PIMATHVECTOR_ZERO_CMP);
|
||||
assert(std::abs(tv) > PIMATHVECTOR_ZERO_CMP);
|
||||
if (tv == Type(1)) return *this;
|
||||
PIMV_FOR c[i] /= tv;
|
||||
return *this;
|
||||
@@ -451,7 +491,7 @@ public:
|
||||
}
|
||||
void operator*=(const Type & v) { PIMV_FOR c[i] *= v; }
|
||||
void operator/=(const Type & v) {
|
||||
assert(piAbs<Type>(v) > PIMATHVECTOR_ZERO_CMP);
|
||||
assert(std::abs(v) > PIMATHVECTOR_ZERO_CMP);
|
||||
PIMV_FOR c[i] /= v;
|
||||
}
|
||||
_CVector operator-() const {
|
||||
@@ -477,7 +517,7 @@ public:
|
||||
return tv;
|
||||
}
|
||||
_CVector operator/(const Type & v) const {
|
||||
assert(piAbs<Type>(v) > PIMATHVECTOR_ZERO_CMP);
|
||||
assert(std::abs(v) > PIMATHVECTOR_ZERO_CMP);
|
||||
_CVector tv(*this);
|
||||
PIMV_FOR tv[i] /= v;
|
||||
return tv;
|
||||
@@ -508,7 +548,7 @@ public:
|
||||
assert(c.size() == v.size());
|
||||
_CVector tv(*this);
|
||||
PIMV_FOR {
|
||||
assert(piAbs<Type>(v[i]) > PIMATHVECTOR_ZERO_CMP);
|
||||
assert(std::abs(v[i]) > PIMATHVECTOR_ZERO_CMP);
|
||||
tv[i] /= v[i];
|
||||
}
|
||||
return tv;
|
||||
@@ -520,7 +560,7 @@ public:
|
||||
assert(c.size() == lp1.size());
|
||||
_CVector a = _CVector::fromTwoPoints(lp0, lp1);
|
||||
Type tv = a.length();
|
||||
assert(piAbs<Type>(tv) > PIMATHVECTOR_ZERO_CMP);
|
||||
assert(std::abs(tv) > PIMATHVECTOR_ZERO_CMP);
|
||||
_CVector b = _CVector::fromTwoPoints(lp0, *this);
|
||||
return piAbs<Type>(a[0] * b[1] - a[1] * b[0]) / tv;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user