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

View File

@@ -40,7 +40,7 @@ PIMathVectorT3d PIQuaternion::eyler() const {
angle_z = atan2(-rmat[0][1] / c, rmat[0][0] / c);
}
if (angle_z < 0) angle_z = 2*M_PI + angle_z;
return createVectorT3d(angle_x,angle_y,angle_z);
return PIMathVectorT3d({angle_x,angle_y,angle_z});
}
@@ -153,7 +153,7 @@ PIQuaternion PIQuaternion::fromAngles(double ax, double ay, double az) {
}
PIQuaternion PIQuaternion::fromAngles2(double ax, double ay, double az) {
double om = createVectorT3d(ax,ay,az).length();
double om = PIMathVectorT3d({ax,ay,az}).length();
if (om == 0.) return PIQuaternion(PIMathVectorT3d(), 1.);
PIQuaternion res;
res.q[0] = cos(om/2);

View File

@@ -39,7 +39,7 @@ public:
double & scalar() {return q[0];}
double scalar() const {return q[0];}
PIMathVectorT3d vector() const {return createVectorT3<double>(q[1], q[2], q[3]);}
PIMathVectorT3d vector() const {return PIMathVectorT3d({q[1], q[2], q[3]});}
PIMathVectorT3d eyler() const;
PIMathMatrixT33d rotationMatrix() const;

View File

@@ -40,8 +40,10 @@ inline PIByteArray & operator >>(PIByteArray & ba, MM & v) {piCout << ">>"
int main() {
PIMathVectorT3d v3 = createVectorT3d(1,2,3);
PIMathVectorT3d v3({1,2,3});
PIMathVectord v(v3);
PIMathVectord v2({3,2,1});
piCout << v;
piCout << v2;
return 0;
}