From 0435c2b332ed48f37668a17349c1c708e47d85b6 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, 27 Feb 2019 11:28:54 +0000 Subject: [PATCH] git-svn-id: svn://db.shs.com.ru/pip@764 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5 --- src_main/core/pivariant.cpp | 27 ++++++++++++++++++++++++++- src_main/core/pivariant.h | 30 +++++++++++++++++++++++++++++- src_main/core/pivarianttypes.cpp | 2 +- src_main/core/pivarianttypes.h | 5 +++-- src_main/math/pigeometry.h | 4 ++-- src_main/math/pimathmatrix.h | 15 ++++++--------- src_main/math/pimathvector.h | 19 +++++++++++++++---- src_opencl/piopencl.cpp | 1 + 8 files changed, 83 insertions(+), 20 deletions(-) diff --git a/src_main/core/pivariant.cpp b/src_main/core/pivariant.cpp index eedf6f0b..bffb5920 100755 --- a/src_main/core/pivariant.cpp +++ b/src_main/core/pivariant.cpp @@ -1,7 +1,7 @@ /* PIP - Platform Independent Primitives Variant type - Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru + Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru 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 @@ -146,6 +146,8 @@ PIVariant::Type PIVariant::typeFromName(const PIString & tname) { if (s == "color") return PIVariant::pivColor; if (s == "point") return PIVariant::pivPoint; if (s == "rect") return PIVariant::pivRect; + if (s == "vector") return PIVariant::pivMathVector; + if (s == "matrix") return PIVariant::pivMathMatrix; return PIVariant::pivInvalid; } @@ -189,6 +191,8 @@ PIString PIVariant::typeName(PIVariant::Type type) { case PIVariant::pivColor: return "Color"; case PIVariant::pivPoint: return "Point"; case PIVariant::pivRect: return "Rect"; + case PIVariant::pivMathVector: return "Vector"; + case PIVariant::pivMathMatrix: return "Matrix"; case PIVariant::pivCustom: return "Custom"; default: break; } @@ -593,3 +597,24 @@ PIRectd PIVariant::toRect() const { if (_type == PIVariant::pivRect) {PIRectd r; ba >> r; return r;} return PIRectd(); } + + +/** \brief Returns variant content as math vector + * \details In case of PIMathVectord type returns rect value. \n + * In case of other types returns empty PIMathVectord. */ +PIMathVectord PIVariant::toMathVector() const { + PIByteArray ba(_content); + if (_type == PIVariant::pivMathVector) {PIMathVectord r; ba >> r; return r;} + return PIMathVectord(); +} + + +/** \brief Returns variant content as math matrix + * \details In case of PIMathMatrixd type returns rect value. \n + * In case of other types returns empty PIMathMatrixd. */ +PIMathMatrixd PIVariant::toMathMatrix() const { + PIByteArray ba(_content); + if (_type == PIVariant::pivMathMatrix) {PIMathMatrixd r; ba >> r; return r;} + return PIMathMatrixd(); +} + diff --git a/src_main/core/pivariant.h b/src_main/core/pivariant.h index 4f0ca9cf..643c2008 100755 --- a/src_main/core/pivariant.h +++ b/src_main/core/pivariant.h @@ -6,7 +6,7 @@ /* PIP - Platform Independent Primitives Variant type - Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru + Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru 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 @@ -237,6 +237,8 @@ public: pivPoint /** PIPoint */ , pivRect /** PIRect */ , pivIODevice /** PIVariantTypes::IODevice */ , + pivMathVector /** PIMathVectord */ , + pivMathMatrix /** PIMathMatrixd */ , pivCustom /** Custom */ = 0xFF }; @@ -329,6 +331,12 @@ public: //! Constructs variant from rect PIVariant(const PIRectd & v) {initType(v);} + //! Constructs variant from MathVector + PIVariant(const PIMathVectord & v) {initType(v);} + + //! Constructs variant from MathMatrix + PIVariant(const PIMathMatrixd & v) {initType(v);} + //! Set variant content and type to string void setValue(const char * v) {setValue(PIString(v));} @@ -414,6 +422,13 @@ public: //! Set variant content and type to rect void setValue(const PIRectd & v) {initType(v);} + //! Set variant content and type to math vector + void setValue(const PIMathVectord & v) {initType(v);} + + //! Set variant content and type to math matrix + void setValue(const PIMathMatrixd & v) {initType(v);} + + //! Set current value from string without change type void setValueFromString(const PIString & v); @@ -439,6 +454,8 @@ public: PIVariantTypes::IODevice toIODevice() const; PIPointd toPoint() const; PIRectd toRect() const; + PIMathVectord toMathVector() const; + PIMathMatrixd toMathMatrix() const; /** \brief Returns variant content as custom type @@ -530,6 +547,10 @@ public: PIVariant & operator =(const PIPointd & v) {setValue(v); return *this;} //! Assign operator PIVariant & operator =(const PIRectd & v) {setValue(v); return *this;} + //! Assign operator + PIVariant & operator =(const PIMathVectord & v) {setValue(v); return *this;} + //! Assign operator + PIVariant & operator =(const PIMathMatrixd & v) {setValue(v); return *this;} //! Compare operator @@ -688,6 +709,8 @@ template<> inline PIVariant PIVariant::fromValue(const PIVariantTypes::Color & v template<> inline PIVariant PIVariant::fromValue(const PIVariantTypes::IODevice & v) {return PIVariant(v);} template<> inline PIVariant PIVariant::fromValue(const PIPointd & v) {return PIVariant(v);} template<> inline PIVariant PIVariant::fromValue(const PIRectd & v) {return PIVariant(v);} +template<> inline PIVariant PIVariant::fromValue(const PIMathVectord & v) {return PIVariant(v);} +template<> inline PIVariant PIVariant::fromValue(const PIMathMatrixd & v) {return PIVariant(v);} template<> inline PIVariant::Type PIVariant::getType() {return PIVariant::pivBool;} template<> inline PIVariant::Type PIVariant::getType() {return PIVariant::pivChar;} @@ -716,6 +739,8 @@ template<> inline PIVariant::Type PIVariant::getType() {r template<> inline PIVariant::Type PIVariant::getType() {return PIVariant::pivIODevice;} template<> inline PIVariant::Type PIVariant::getType() {return PIVariant::pivPoint;} template<> inline PIVariant::Type PIVariant::getType() {return PIVariant::pivRect;} +template<> inline PIVariant::Type PIVariant::getType() {return PIVariant::pivMathVector;} +template<> inline PIVariant::Type PIVariant::getType() {return PIVariant::pivMathMatrix;} REGISTER_VARIANT(bool) REGISTER_VARIANT(char) @@ -744,6 +769,9 @@ REGISTER_NS_VARIANT(PIVariantTypes, Color) REGISTER_NS_VARIANT(PIVariantTypes, IODevice) REGISTER_VARIANT(PIPointd) REGISTER_VARIANT(PIRectd) +REGISTER_VARIANT(PIMathVectord) +REGISTER_VARIANT(PIMathMatrixd) + inline PIByteArray & operator <<(PIByteArray & s, const PIVariant & v) { s << v._content << int(v._type); diff --git a/src_main/core/pivarianttypes.cpp b/src_main/core/pivarianttypes.cpp index cdb18b23..d595a42c 100644 --- a/src_main/core/pivarianttypes.cpp +++ b/src_main/core/pivarianttypes.cpp @@ -1,7 +1,7 @@ /* PIP - Platform Independent Primitives Variant types - Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru + Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru 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 diff --git a/src_main/core/pivarianttypes.h b/src_main/core/pivarianttypes.h index 80fc4090..740570aa 100644 --- a/src_main/core/pivarianttypes.h +++ b/src_main/core/pivarianttypes.h @@ -6,7 +6,7 @@ /* PIP - Platform Independent Primitives Variant types - Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru + Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru 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 @@ -26,6 +26,8 @@ #define PIVARIANTYPES_H #include "pistring.h" +#include "pimathmatrix.h" + class PIPropertyStorage; @@ -114,5 +116,4 @@ inline PIByteArray & operator <<(PIByteArray & s, const PIVariantTypes::IODevice inline PIByteArray & operator >>(PIByteArray & s, PIVariantTypes::IODevice & v) {s >> v.prefix >> v.mode >> v.options >> v.props; return s;} inline PICout operator <<(PICout s, const PIVariantTypes::IODevice & v) {s << v.toPICout(); return s;} - #endif // PIVARIANTYPES_H diff --git a/src_main/math/pigeometry.h b/src_main/math/pigeometry.h index 57d6c6ab..78630762 100644 --- a/src_main/math/pigeometry.h +++ b/src_main/math/pigeometry.h @@ -56,7 +56,7 @@ public: }; template -PICout operator <<(PICout & s, const PIPoint & v) {s.setControl(0, true); s << '{' << v.x << ", " << v.y << '}'; s.restoreControl(); return s;} +PICout operator <<(PICout & s, const PIPoint & v) {s.setControl(0, true); s << "Point{" << v.x << ", " << v.y << "}"; s.restoreControl(); return s;} template inline PIByteArray & operator <<(PIByteArray & s, PIPoint v) {s << v.x << v.y; return s;} @@ -141,7 +141,7 @@ public: }; template -PICout operator <<(PICout & s, const PIRect & v) {s.setControl(0, true); s << '{' << v.x0 << ", " << v.y0 << "; " << v.x1 - v.x0 << ", " << v.y1 - v.y0 << '}'; s.restoreControl(); return s;} +PICout operator <<(PICout & s, const PIRect & v) {s.setControl(0, true); s << "Rect{" << v.x0 << ", " << v.y0 << "; " << v.x1 - v.x0 << ", " << v.y1 - v.y0 << "}"; s.restoreControl(); return s;} template inline PIByteArray & operator <<(PIByteArray & s, PIRect v) {s << v.x0 << v.x1 << v.y0 << v.y1; return s;} diff --git a/src_main/math/pimathmatrix.h b/src_main/math/pimathmatrix.h index 419bea17..c7232d8d 100644 --- a/src_main/math/pimathmatrix.h +++ b/src_main/math/pimathmatrix.h @@ -232,11 +232,11 @@ template<> inline PIMathMatrixT<3u, 3u> PIMathMatrixT<3u, 3u>::scaleZ(double fac #ifdef PIP_STD_IOSTREAM template -inline std::ostream & operator <<(std::ostream & s, const PIMathMatrixT & m) {s << '{'; PIMM_FOR_I(r, c) s << m[r][c]; if (c < Cols - 1 || r < Rows - 1) s << ", ";} if (r < Rows - 1) s << std::endl << ' ';} s << '}'; return s;} +inline std::ostream & operator <<(std::ostream & s, const PIMathMatrixT & m) {s << "{"; PIMM_FOR_I(r, c) s << m[r][c]; if (c < Cols - 1 || r < Rows - 1) s << ", ";} if (r < Rows - 1) s << std::endl << " ";} s << "}"; return s;} #endif template -inline PICout operator <<(PICout s, const PIMathMatrixT & m) {s << '{'; PIMM_FOR_I(r, c) s << m[r][c]; if (c < Cols - 1 || r < Rows - 1) s << ", ";} if (r < Rows - 1) s << PICoutManipulators::NewLine << ' ';} s << '}'; return s;} +inline PICout operator <<(PICout s, const PIMathMatrixT & m) {s << "{"; PIMM_FOR_I(r, c) s << m[r][c]; if (c < Cols - 1 || r < Rows - 1) s << ", ";} if (r < Rows - 1) s << PICoutManipulators::NewLine << " ";} s << "}"; return s;} /// Multiply matrices {Rows0 x CR} on {CR x Cols1}, result is {Rows0 x Cols1} template @@ -328,7 +328,7 @@ class PIP_EXPORT PIMathMatrix : public PIVector2D { typedef PIMathMatrix _CMatrix; typedef PIMathVector _CMCol; public: - PIMathMatrix(const uint cols = 3, const uint rows = 3) {resize(cols, rows);} + PIMathMatrix(const uint cols = 0, const uint rows = 0) {resize(cols, rows);} PIMathMatrix(const uint cols, const uint rows, const PIVector & val) {resize(cols, rows); int i=0; PIMM_FOR_I(c, r) _V2D::element(r, c) = val[i++];} PIMathMatrix(const PIVector > & val) {if(!val.isEmpty()) {resize(val[0].size(), val.size()); PIMM_FOR_I(c, r) _V2D::element(r, c) = val[r][c];}} PIMathMatrix(const PIVector2D & val) {if(!val.isEmpty()) {resize(val.cols(), val.rows()); PIMM_FOR_I(c, r) _V2D::element(r, c) = val.element(r, c);}} @@ -346,6 +346,7 @@ public: bool isSquare() const {return _V2D::cols_ == _V2D::rows_;} bool isIdentity() const {PIMM_FOR(c, r) if ((c == r) ? _V2D::element(r, c) != Type(1) : _V2D::element(r, c) != Type(0)) return false; return true;} bool isNull() const {PIMM_FOR_A(i) if (_V2D::mat[i] != Type(0)) return false; return true;} + bool isValid() const {return !PIVector2D::isEmpty();} _CMatrix & operator =(const PIVector > & v) {*this = _CMatrix(v); return *this;} bool operator ==(const _CMatrix & sm) const {PIMM_FOR_A(i) if (_V2D::mat[i] != sm.mat[i]) return false; return true;} @@ -482,20 +483,16 @@ public: } _CMatrix inverted(bool * ok = 0) const {_CMatrix tm(*this); tm.invert(ok); return tm;} _CMatrix transposed() const {_CMatrix tm(_V2D::rows_, _V2D::cols_); PIMM_FOR(c, r) tm.element(c, r) = _V2D::element(r, c); return tm;} - -private: -// size_t rows_, cols_; -// PIVector mat; }; #ifdef PIP_STD_IOSTREAM template -inline std::ostream & operator <<(std::ostream & s, const PIMathMatrix & m) {s << '{'; for (uint r = 0; r < m.rows(); ++r) { for (uint c = 0; c < m.cols(); ++c) { s << m.element(r, c); if (c < m.cols() - 1 || r < m.rows() - 1) s << ", ";} if (r < m.rows() - 1) s << std::endl << ' ';} s << '}'; return s;} +inline std::ostream & operator <<(std::ostream & s, const PIMathMatrix & m) {s << "{"; for (uint r = 0; r < m.rows(); ++r) { for (uint c = 0; c < m.cols(); ++c) { s << m.element(r, c); if (c < m.cols() - 1 || r < m.rows() - 1) s << ", ";} if (r < m.rows() - 1) s << std::endl << " ";} s << "}"; return s;} #endif template -inline PICout operator <<(PICout s, const PIMathMatrix & m) {s << '{'; for (uint r = 0; r < m.rows(); ++r) { for (uint c = 0; c < m.cols(); ++c) { s << m.element(r, c); if (c < m.cols() - 1 || r < m.rows() - 1) s << ", ";} if (r < m.rows() - 1) s << PICoutManipulators::NewLine << ' ';} s << '}'; return s;} +inline PICout operator <<(PICout s, const PIMathMatrix & m) {s << "Matrix{"; for (uint r = 0; r < m.rows(); ++r) { for (uint c = 0; c < m.cols(); ++c) { s << m.element(r, c); if (c < m.cols() - 1 || r < m.rows() - 1) s << ", ";} if (r < m.rows() - 1) s << PICoutManipulators::NewLine << " ";} s << "}"; return s;} /// Multiply matrices {CR x Rows0} on {Cols1 x CR}, result is {Cols1 x Rows0} template diff --git a/src_main/math/pimathvector.h b/src_main/math/pimathvector.h index 14b47cc1..9c0f3a46 100644 --- a/src_main/math/pimathvector.h +++ b/src_main/math/pimathvector.h @@ -129,7 +129,7 @@ inline PIMathVectorT operator *(const Type & x, const PIMathVectorT< } template -inline PICout operator <<(PICout s, const PIMathVectorT & v) {s << '{'; PIMV_FOR(i, 0) {s << v[i]; if (i < Size - 1) s << ", ";} s << '}'; return s;} +inline PICout operator <<(PICout s, const PIMathVectorT & v) {s << "{"; PIMV_FOR(i, 0) {s << v[i]; if (i < Size - 1) s << ", ";} s << "}"; return s;} template inline bool operator ||(const PIMathVectorT & f, const PIMathVectorT & s) {return (f * s).isNull();} template @@ -179,7 +179,7 @@ template class PIP_EXPORT PIMathVector { typedef PIMathVector _CVector; public: - PIMathVector(const uint size = 3) {c.resize(size);} + PIMathVector(const uint size = 0) {c.resize(size);} PIMathVector(const PIVector & val) {c.resize(val.size()); PIMV_FOR(i, 0) c[i] = val[i];} PIMathVector(const _CVector & st, const _CVector & fn) {c.resize(st.size()); PIMV_FOR(i, 0) c[i] = fn[i] - st[i];} @@ -201,6 +201,8 @@ public: _CVector & normalize() {Type tv = length(); if (tv == Type(1)) return *this; if (piAbs(tv) <= Type(1E-100)) {fill(Type(0)); return *this;} PIMV_FOR(i, 0) c[i] /= tv; return *this;} _CVector normalized() {_CVector tv(*this); tv.normalize(); return tv;} bool isNull() const {PIMV_FOR(i, 0) if (c[i] != Type(0)) return false; return true;} + bool isValid() const {return !c.isEmpty();} + bool isOrtho(const _CVector & v) const {return ((*this) ^ v) == Type(0);} Type & at(uint index) {return c[index];} @@ -237,6 +239,9 @@ public: PIMathVector turnTo(uint size) const {PIMathVector tv; uint sz = piMin(c.size(), size); for (uint i = 0; i < sz; ++i) tv[i] = c[i]; return tv;} PIVector toVector() const {return c;} + inline Type * data() {return c.data();} + inline const Type * data() const {return c.data();} + private: // uint size_; PIVector c; @@ -247,11 +252,17 @@ private: #ifdef PIP_STD_IOSTREAM template -inline std::ostream & operator <<(std::ostream & s, const PIMathVector & v) {s << '{'; for (uint i = 0; i < v.size(); ++i) {s << v[i]; if (i < v.size() - 1) s << ", ";} s << '}'; return s;} +inline std::ostream & operator <<(std::ostream & s, const PIMathVector & v) {s << "{"; for (uint i = 0; i < v.size(); ++i) {s << v[i]; if (i < v.size() - 1) s << ", ";} s << "}"; return s;} #endif template -inline PICout operator <<(PICout s, const PIMathVector & v) {s << '{'; for (uint i = 0; i < v.size(); ++i) {s << v[i]; if (i < v.size() - 1) s << ", ";} s << '}'; return s;} +inline PICout operator <<(PICout s, const PIMathVector & v) {s << "Vector{"; for (uint i = 0; i < v.size(); ++i) {s << v[i]; if (i < v.size() - 1) s << ", ";} s << "}"; return s;} + +template +inline PIByteArray & operator <<(PIByteArray & s, const PIMathVector & v) {s << v.c; return s;} +template +inline PIByteArray & operator >>(PIByteArray & s, PIMathVector & v) {s >> v.c; return s;} + typedef PIMathVector PIMathVectori; typedef PIMathVector PIMathVectord; diff --git a/src_opencl/piopencl.cpp b/src_opencl/piopencl.cpp index f896bae9..83fa5b7b 100644 --- a/src_opencl/piopencl.cpp +++ b/src_opencl/piopencl.cpp @@ -1,4 +1,5 @@ #include "piopencl.h" +#define CL_USE_DEPRECATED_OPENCL_1_2_APIS #ifdef MAC_OS # include "cl.h" #else