pimathvector.h div and std::initializer_list

This commit is contained in:
2020-10-21 18:54:41 +03:00
parent 6f3fdf4d49
commit 47be2d3d62
4 changed files with 48 additions and 22 deletions

View File

@@ -46,6 +46,10 @@ public:
assert(Size == val.size());
PIMV_FOR c[i] = val[i];
}
PIMathVectorT(std::initializer_list<Type> init_list) {
assert(Size == init_list.size());
PIMV_FOR c[i] = init_list.begin()[i];
}
static _CVector fromTwoPoints(const _CVector & st, const _CVector & fn) {
_CVector tv;
PIMV_FOR tv[i] = fn[i] - st[i];
@@ -157,7 +161,18 @@ public:
_CVector mul(const Type & v) const {
return (*this) * v;
}
_CVector div(const _CVector & v) const {
_CVector tv(*this);
PIMV_FOR {
assert(piAbs<Type>(v[i]) > PIMATHVECTOR_ZERO_CMP);
tv[i] /= v[i];
}
return tv;
}
_CVector div(const Type & v) const {
return (*this) / v;
}
PIMathMatrixT<1, Size, Type> transposed() const {
PIMathMatrixT<1, Size, Type> ret;
PIMV_FOR ret[0][i] = c[i];
@@ -195,6 +210,12 @@ public:
static _CVector mul(const _CVector & v1, const Type & v2) {
return v1 * v2;
}
static _CVector div(const _CVector & v1, const _CVector & v2) {
return v1.div(v2);
}
static _CVector div(const _CVector & v1, const Type & v2) {
return v1 / v2;
}
private:
Type c[Size];
@@ -210,29 +231,13 @@ template<uint Size, typename Type>
inline PICout operator <<(PICout s, const PIMathVectorT<Size, Type> & v) {s << "{"; PIMV_FOR {s << v[i]; if (i < Size - 1) s << ", ";} s << "}"; return s;}
template<typename T>
inline PIMathVectorT<2u, T> createVectorT2(T x, T y) {return PIMathVectorT<2u, T>(PIVector<T>() << x << y);}
template<typename T>
inline PIMathVectorT<3u, T> createVectorT3(T x, T y, T z) {return PIMathVectorT<3u, T>(PIVector<T>() << x << y << z);}
template<typename T>
inline PIMathVectorT<4u, T> createVectorT4(T x, T y, T z, T w) {return PIMathVectorT<4u, T>(PIVector<T>() << x << y << z << w);}
typedef PIMathVectorT<2u, int> PIMathVectorT2i;
typedef PIMathVectorT<3u, int> PIMathVectorT3i;
typedef PIMathVectorT<4u, int> PIMathVectorT4i;
typedef PIMathVectorT<2u, double> PIMathVectorT2d;
typedef PIMathVectorT<3u, double> PIMathVectorT3d;
typedef PIMathVectorT<4u, double> PIMathVectorT4d;
#define createVectorT2i createVectorT2<int>
#define createVectorT3i createVectorT3<int>
#define createVectorT4i createVectorT4<int>
#define createVectorT2f createVectorT2<float>
#define createVectorT3f createVectorT3<float>
#define createVectorT4f createVectorT4<float>
#define createVectorT2d createVectorT2<double>
#define createVectorT3d createVectorT3<double>
#define createVectorT4d createVectorT4<double>
#undef PIMV_FOR
@@ -248,6 +253,7 @@ class PIP_EXPORT PIMathVector {
public:
PIMathVector(const uint size = 0, const Type & new_value = Type()) {c.resize(size, new_value);}
PIMathVector(const PIVector<Type> & val) {c = val;}
PIMathVector(std::initializer_list<Type> init_list) {c = PIVector<Type>(init_list);}
template<uint Size>
PIMathVector(const PIMathVectorT<Size, Type> & val) {c.resize(Size); PIMV_FOR c[i] = val[i];}
@@ -405,6 +411,18 @@ public:
_CVector mul(const Type & v) const {
return (*this) * v;
}
_CVector div(const _CVector & v) const {
assert(c.size() == v.size());
_CVector tv(*this);
PIMV_FOR {
assert(piAbs<Type>(v[i]) > PIMATHVECTOR_ZERO_CMP);
tv[i] /= v[i];
}
return tv;
}
_CVector div(const Type & v) const {
return (*this) / v;
}
Type distToLine(const _CVector & lp0, const _CVector & lp1) {
assert(c.size() == lp0.size());
@@ -437,6 +455,12 @@ public:
static _CVector mul(const _CVector & v1, const Type & v2) {
return v1 * v2;
}
static _CVector div(const _CVector & v1, const _CVector & v2) {
return v1.div(v2);
}
static _CVector div(const _CVector & v1, const Type & v2) {
return v1 / v2;
}
private:
PIVector<Type> c;
};