From 47be2d3d6297838a9eb22617e7a724824fb59d9d Mon Sep 17 00:00:00 2001 From: andrey Date: Wed, 21 Oct 2020 18:54:41 +0300 Subject: [PATCH] pimathvector.h div and std::initializer_list --- libs/main/math/pimathvector.h | 60 +++++++++++++++++++++++---------- libs/main/math/piquaternion.cpp | 4 +-- libs/main/math/piquaternion.h | 2 +- main.cpp | 4 ++- 4 files changed, 48 insertions(+), 22 deletions(-) diff --git a/libs/main/math/pimathvector.h b/libs/main/math/pimathvector.h index 8983fd37..4f89a7b9 100644 --- a/libs/main/math/pimathvector.h +++ b/libs/main/math/pimathvector.h @@ -46,6 +46,10 @@ public: assert(Size == val.size()); PIMV_FOR c[i] = val[i]; } + PIMathVectorT(std::initializer_list 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(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 inline PICout operator <<(PICout s, const PIMathVectorT & v) {s << "{"; PIMV_FOR {s << v[i]; if (i < Size - 1) s << ", ";} s << "}"; return s;} - -template -inline PIMathVectorT<2u, T> createVectorT2(T x, T y) {return PIMathVectorT<2u, T>(PIVector() << x << y);} -template -inline PIMathVectorT<3u, T> createVectorT3(T x, T y, T z) {return PIMathVectorT<3u, T>(PIVector() << x << y << z);} -template -inline PIMathVectorT<4u, T> createVectorT4(T x, T y, T z, T w) {return PIMathVectorT<4u, T>(PIVector() << 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 -#define createVectorT3i createVectorT3 -#define createVectorT4i createVectorT4 -#define createVectorT2f createVectorT2 -#define createVectorT3f createVectorT3 -#define createVectorT4f createVectorT4 -#define createVectorT2d createVectorT2 -#define createVectorT3d createVectorT3 -#define createVectorT4d createVectorT4 + #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 & val) {c = val;} + PIMathVector(std::initializer_list init_list) {c = PIVector(init_list);} template PIMathVector(const PIMathVectorT & 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(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 c; }; diff --git a/libs/main/math/piquaternion.cpp b/libs/main/math/piquaternion.cpp index fa95ad7c..9dc570a2 100644 --- a/libs/main/math/piquaternion.cpp +++ b/libs/main/math/piquaternion.cpp @@ -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); diff --git a/libs/main/math/piquaternion.h b/libs/main/math/piquaternion.h index e9b79e92..c54dbf89 100644 --- a/libs/main/math/piquaternion.h +++ b/libs/main/math/piquaternion.h @@ -39,7 +39,7 @@ public: double & scalar() {return q[0];} double scalar() const {return q[0];} - PIMathVectorT3d vector() const {return createVectorT3(q[1], q[2], q[3]);} + PIMathVectorT3d vector() const {return PIMathVectorT3d({q[1], q[2], q[3]});} PIMathVectorT3d eyler() const; PIMathMatrixT33d rotationMatrix() const; diff --git a/main.cpp b/main.cpp index 072c42ab..10dfb678 100644 --- a/main.cpp +++ b/main.cpp @@ -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; }