enable complex type for PIMathVectorT and PIMathMatrixT
TODO: add precision to invert and test vector
This commit is contained in:
@@ -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