From 92f49293dc25c074aa89977d77335beb8cb3655e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=8B=D1=87=D0=BA=D0=BE=D0=B2=20=D0=90=D0=BD=D0=B4?= =?UTF-8?q?=D1=80=D0=B5=D0=B9?= Date: Wed, 25 May 2016 13:40:16 +0000 Subject: [PATCH] git-svn-id: svn://db.shs.com.ru/pip@201 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5 --- src/math/pimathmatrix.h | 39 +++++++++++++++++++++++++++++++-------- src/math/pimathvector.h | 24 +++++++++++++++--------- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/math/pimathmatrix.h b/src/math/pimathmatrix.h index a68ced93..82487e28 100644 --- a/src/math/pimathmatrix.h +++ b/src/math/pimathmatrix.h @@ -4,7 +4,7 @@ /* PIP - Platform Independent Primitives PIMathMatrix - Copyright (C) 2015 Ivan Pelipenko peri4ko@gmail.com + Copyright (C) 2016 Ivan Pelipenko peri4ko@gmail.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -82,11 +82,11 @@ public: void operator -=(const _CMatrix & sm) {PIMM_FOR_WB(r, c) m[r][c] -= sm.m[r][c];} void operator *=(const Type & v) {PIMM_FOR_WB(r, c) m[r][c] *= v;} void operator /=(const Type & v) {PIMM_FOR_WB(r, c) m[r][c] /= v;} - _CMatrix operator -() {_CMatrix tm; PIMM_FOR_WB(r, c) tm.m[r][c] = -m[r][c]; return tm;} - _CMatrix operator +(const _CMatrix & sm) {_CMatrix tm = _CMatrix(*this); PIMM_FOR_WB(r, c) tm.m[r][c] += sm.m[r][c]; return tm;} - _CMatrix operator -(const _CMatrix & sm) {_CMatrix tm = _CMatrix(*this); PIMM_FOR_WB(r, c) tm.m[r][c] -= sm.m[r][c]; return tm;} - _CMatrix operator *(const Type & v) {_CMatrix tm = _CMatrix(*this); PIMM_FOR_WB(r, c) tm.m[r][c] *= v; return tm;} - _CMatrix operator /(const Type & v) {_CMatrix tm = _CMatrix(*this); PIMM_FOR_WB(r, c) tm.m[r][c] /= v; return tm;} + _CMatrix operator -() const {_CMatrix tm; PIMM_FOR_WB(r, c) tm.m[r][c] = -m[r][c]; return tm;} + _CMatrix operator +(const _CMatrix & sm) const {_CMatrix tm = _CMatrix(*this); PIMM_FOR_WB(r, c) tm.m[r][c] += sm.m[r][c]; return tm;} + _CMatrix operator -(const _CMatrix & sm) const {_CMatrix tm = _CMatrix(*this); PIMM_FOR_WB(r, c) tm.m[r][c] -= sm.m[r][c]; return tm;} + _CMatrix operator *(const Type & v) const {_CMatrix tm = _CMatrix(*this); PIMM_FOR_WB(r, c) tm.m[r][c] *= v; return tm;} + _CMatrix operator /(const Type & v) const {_CMatrix tm = _CMatrix(*this); PIMM_FOR_WB(r, c) tm.m[r][c] /= v; return tm;} Type determinant(bool * ok = 0) const { _CMatrix m(*this); @@ -202,7 +202,7 @@ public: return *this; } _CMatrix inverted(bool * ok = 0) const {_CMatrix tm(*this); tm.invert(ok); return tm;} - _CMatrixI transposed() const {_CMatrixI tm; PIMM_FOR_WB(r, c) tm[r][c] = m[r][c]; return tm;} + _CMatrixI transposed() const {_CMatrixI tm; PIMM_FOR_WB(r, c) tm[c][r] = m[r][c]; return tm;} private: void resize(uint rows_, uint cols_, const Type & new_value = Type()) {r_ = rows_; c_ = cols_; PIMM_FOR_WB(r, c) m[r][c] = new_value;} @@ -246,7 +246,7 @@ inline PIMathMatrixT operator *(const PIMathMatrixT inline PIMathVectorT operator *(const PIMathMatrixT & fm, const PIMathVectorT & sv) { @@ -261,6 +261,29 @@ inline PIMathVectorT operator *(const PIMathMatrixT +inline PIMathVectorT operator *(const PIMathMatrixT & fm, + const PIMathVectorT & sv) { + PIMathVectorT tv; + Type t; + for (uint j = 0; j < Rows; ++j) { + t = Type(0); + for (uint i = 0; i < Cols; ++i) + t += fm[j][i] * sv[i]; + tv[j] = t; + } + return tv; +}*/ + +/// Multiply value(T) on matrix {Rows x Cols}, result is vector {Rows} +template +inline PIMathMatrixT operator *(const Type & x, const PIMathMatrixT & v) { + return v * x; +} + + typedef PIMathMatrixT<2u, 2u, int> PIMathMatrixT22i; typedef PIMathMatrixT<3u, 3u, int> PIMathMatrixT33i; typedef PIMathMatrixT<4u, 4u, int> PIMathMatrixT44i; diff --git a/src/math/pimathvector.h b/src/math/pimathvector.h index 53828e94..53e143ff 100644 --- a/src/math/pimathvector.h +++ b/src/math/pimathvector.h @@ -4,7 +4,7 @@ /* PIP - Platform Independent Primitives PIMathVector - Copyright (C) 2015 Ivan Pelipenko peri4ko@gmail.com + Copyright (C) 2016 Ivan Pelipenko peri4ko@gmail.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -96,12 +96,13 @@ public: return ret; } - operator PIMathMatrixT<1, Size, Type>() {return transposed();} - operator PIMathMatrixT() { - PIMathMatrixT ret; - PIMV_FOR(i, 0) ret[i][0] = c[i]; - return ret; - } +// operator PIMathMatrixT<1, Size, Type>() {return transposed();} +// operator PIMathMatrixT() { +// PIMathMatrixT ret; +// PIMV_FOR(i, 0) ret[i][0] = c[i]; +// return ret; +// } + Type distToLine(const _CVector & lp0, const _CVector & lp1) { _CVector a(lp0, lp1), b(lp0, *this), c(lp1, *this); Type f = fabs(a[0]*b[1] - a[1]*b[0]) / a.length();//, s = b.length() + c.length() - a.length(); @@ -110,6 +111,8 @@ public: template /// vector {Size, Type} to vector {Size1, Type1} PIMathVectorT turnTo() const {PIMathVectorT tv; uint sz = piMin(Size, Size1); for (uint i = 0; i < sz; ++i) tv[i] = c[i]; return tv;} + static _CVector filled(const Type & v) {_CVector vv; PIMV_FOR(i, 0) vv[i] = v; return vv;} + private: void resize(const Type & new_value = Type()) {for (int i = 0; i < Size; ++i) c[i] = new_value;} @@ -118,6 +121,11 @@ private: }; //#pragma pack(pop) +template +inline PIMathVectorT operator *(const Type & x, const PIMathVectorT & v) { + return v * x; +} + template inline std::ostream & operator <<(std::ostream & s, const PIMathVectorT & v) {s << '{'; PIMV_FOR(i, 0) {s << v[i]; if (i < Size - 1) s << ", ";} s << '}'; return s;} template @@ -228,6 +236,4 @@ inline PICout operator <<(PICout s, const PIMathVector & v) {s << '{'; for typedef PIMathVector PIMathVectori; typedef PIMathVector PIMathVectord; -//#include "pimathmatrix.h" - #endif // PIMATHVECTOR_H