From f27e9b2a86bb6ea7621d5ecfa74fc212c04dec0a Mon Sep 17 00:00:00 2001 From: rdb Date: Mon, 5 Jul 2021 14:13:46 +0200 Subject: [PATCH] linmath: Add __rmul__ operator for left scalar multiplication Fixes #1048 --- panda/src/linmath/lmatrix3_ext_src.I | 8 ++++++++ panda/src/linmath/lmatrix3_ext_src.h | 3 +++ panda/src/linmath/lmatrix3_src.h | 2 ++ panda/src/linmath/lmatrix4_ext_src.I | 8 ++++++++ panda/src/linmath/lmatrix4_ext_src.h | 3 +++ panda/src/linmath/lmatrix4_src.h | 2 ++ panda/src/linmath/lpoint2_ext_src.I | 8 ++++++++ panda/src/linmath/lpoint2_ext_src.h | 3 +++ panda/src/linmath/lpoint2_src.h | 2 ++ panda/src/linmath/lpoint3_ext_src.I | 8 ++++++++ panda/src/linmath/lpoint3_ext_src.h | 3 +++ panda/src/linmath/lpoint3_src.h | 2 ++ panda/src/linmath/lpoint4_ext_src.I | 8 ++++++++ panda/src/linmath/lpoint4_ext_src.h | 3 +++ panda/src/linmath/lpoint4_src.h | 2 ++ panda/src/linmath/lvecBase2_ext_src.I | 19 +++++++++++++++++++ panda/src/linmath/lvecBase2_ext_src.h | 2 ++ panda/src/linmath/lvecBase2_src.h | 2 ++ panda/src/linmath/lvecBase3_ext_src.I | 21 +++++++++++++++++++++ panda/src/linmath/lvecBase3_ext_src.h | 2 ++ panda/src/linmath/lvecBase3_src.h | 2 ++ panda/src/linmath/lvecBase4_ext_src.I | 22 ++++++++++++++++++++++ panda/src/linmath/lvecBase4_ext_src.h | 2 ++ panda/src/linmath/lvecBase4_src.h | 2 ++ panda/src/linmath/lvector2_ext_src.I | 8 ++++++++ panda/src/linmath/lvector2_ext_src.h | 3 +++ panda/src/linmath/lvector2_src.h | 2 ++ panda/src/linmath/lvector3_ext_src.I | 8 ++++++++ panda/src/linmath/lvector3_ext_src.h | 3 +++ panda/src/linmath/lvector3_src.h | 2 ++ panda/src/linmath/lvector4_ext_src.I | 8 ++++++++ panda/src/linmath/lvector4_ext_src.h | 3 +++ panda/src/linmath/lvector4_src.h | 2 ++ tests/linmath/test_lvector2.py | 4 ++++ tests/linmath/test_lvector3.py | 4 ++++ tests/linmath/test_lvector4.py | 4 ++++ 36 files changed, 190 insertions(+) diff --git a/panda/src/linmath/lmatrix3_ext_src.I b/panda/src/linmath/lmatrix3_ext_src.I index 0d92b37aa4..38b47f4c04 100644 --- a/panda/src/linmath/lmatrix3_ext_src.I +++ b/panda/src/linmath/lmatrix3_ext_src.I @@ -34,6 +34,14 @@ __reduce__(PyObject *self) const { return result; } +/** + * + */ +INLINE_LINMATH FLOATNAME(LMatrix3) Extension:: +__rmul__(FLOATTYPE scalar) const { + return *_this * scalar; +} + /** * */ diff --git a/panda/src/linmath/lmatrix3_ext_src.h b/panda/src/linmath/lmatrix3_ext_src.h index 6c4894c69f..fa979338ff 100644 --- a/panda/src/linmath/lmatrix3_ext_src.h +++ b/panda/src/linmath/lmatrix3_ext_src.h @@ -19,6 +19,9 @@ template<> class Extension : public ExtensionBase { public: INLINE_LINMATH PyObject *__reduce__(PyObject *self) const; + + INLINE_LINMATH FLOATNAME(LMatrix3) __rmul__(FLOATTYPE scalar) const; + INLINE_LINMATH std::string __repr__() const; }; diff --git a/panda/src/linmath/lmatrix3_src.h b/panda/src/linmath/lmatrix3_src.h index 2509471e9e..cf5f7c6776 100644 --- a/panda/src/linmath/lmatrix3_src.h +++ b/panda/src/linmath/lmatrix3_src.h @@ -183,6 +183,8 @@ PUBLISHED: INLINE_LINMATH FLOATNAME(LMatrix3) &operator *= (FLOATTYPE scalar); INLINE_LINMATH FLOATNAME(LMatrix3) &operator /= (FLOATTYPE scalar); + EXTENSION(INLINE_LINMATH FLOATNAME(LMatrix3) __rmul__(FLOATTYPE scalar) const); + INLINE_LINMATH void componentwise_mult(const FLOATNAME(LMatrix3) &other); INLINE_LINMATH FLOATTYPE determinant() const; diff --git a/panda/src/linmath/lmatrix4_ext_src.I b/panda/src/linmath/lmatrix4_ext_src.I index 68a2741bbb..2d4e6d07fb 100644 --- a/panda/src/linmath/lmatrix4_ext_src.I +++ b/panda/src/linmath/lmatrix4_ext_src.I @@ -35,6 +35,14 @@ __reduce__(PyObject *self) const { return result; } +/** + * + */ +INLINE_LINMATH FLOATNAME(LMatrix4) Extension:: +__rmul__(FLOATTYPE scalar) const { + return *_this * scalar; +} + /** * */ diff --git a/panda/src/linmath/lmatrix4_ext_src.h b/panda/src/linmath/lmatrix4_ext_src.h index 768be01526..da7a430175 100644 --- a/panda/src/linmath/lmatrix4_ext_src.h +++ b/panda/src/linmath/lmatrix4_ext_src.h @@ -19,6 +19,9 @@ template<> class Extension : public ExtensionBase { public: INLINE_LINMATH PyObject *__reduce__(PyObject *self) const; + + INLINE_LINMATH FLOATNAME(LMatrix4) __rmul__(FLOATTYPE scalar) const; + INLINE_LINMATH std::string __repr__() const; }; diff --git a/panda/src/linmath/lmatrix4_src.h b/panda/src/linmath/lmatrix4_src.h index 9fd695dbf6..e3cec2090f 100644 --- a/panda/src/linmath/lmatrix4_src.h +++ b/panda/src/linmath/lmatrix4_src.h @@ -189,6 +189,8 @@ PUBLISHED: INLINE_LINMATH FLOATNAME(LMatrix4) &operator *= (FLOATTYPE scalar); INLINE_LINMATH FLOATNAME(LMatrix4) &operator /= (FLOATTYPE scalar); + EXTENSION(INLINE_LINMATH FLOATNAME(LMatrix4) __rmul__(FLOATTYPE scalar) const); + INLINE_LINMATH void componentwise_mult(const FLOATNAME(LMatrix4) &other); INLINE_LINMATH void transpose_from(const FLOATNAME(LMatrix4) &other); diff --git a/panda/src/linmath/lpoint2_ext_src.I b/panda/src/linmath/lpoint2_ext_src.I index acfa098501..09f357686f 100644 --- a/panda/src/linmath/lpoint2_ext_src.I +++ b/panda/src/linmath/lpoint2_ext_src.I @@ -79,3 +79,11 @@ __setattr__(PyObject *self, const std::string &attr_name, PyObject *assign) { // Upcall to LVecBase2. return invoke_extension(_this).__setattr__(self, attr_name, assign); } + +/** + * + */ +INLINE_LINMATH FLOATNAME(LPoint2) Extension:: +__rmul__(FLOATTYPE scalar) const { + return *_this * scalar; +} diff --git a/panda/src/linmath/lpoint2_ext_src.h b/panda/src/linmath/lpoint2_ext_src.h index 27eadd1bdf..496e7ec1a8 100644 --- a/panda/src/linmath/lpoint2_ext_src.h +++ b/panda/src/linmath/lpoint2_ext_src.h @@ -20,6 +20,9 @@ class Extension : public ExtensionBase { public: INLINE_LINMATH PyObject *__getattr__(PyObject *self, const std::string &attr_name) const; INLINE_LINMATH int __setattr__(PyObject *self, const std::string &attr_name, PyObject *assign); + + INLINE_LINMATH FLOATNAME(LPoint2) __rmul__(FLOATTYPE scalar) const; + INLINE_LINMATH std::string __repr__() const; }; diff --git a/panda/src/linmath/lpoint2_src.h b/panda/src/linmath/lpoint2_src.h index 070fc13086..8d8a97948c 100644 --- a/panda/src/linmath/lpoint2_src.h +++ b/panda/src/linmath/lpoint2_src.h @@ -46,6 +46,8 @@ PUBLISHED: INLINE_LINMATH FLOATNAME(LPoint2) operator * (FLOATTYPE scalar) const; INLINE_LINMATH FLOATNAME(LPoint2) operator / (FLOATTYPE scalar) const; + EXTENSION(INLINE_LINMATH FLOATNAME(LPoint2) __rmul__(FLOATTYPE scalar) const); + #ifndef FLOATTYPE_IS_INT INLINE_LINMATH FLOATNAME(LPoint2) normalized() const; INLINE_LINMATH FLOATNAME(LPoint2) project(const FLOATNAME(LVecBase2) &onto) const; diff --git a/panda/src/linmath/lpoint3_ext_src.I b/panda/src/linmath/lpoint3_ext_src.I index 95cb1dbdd3..b45b62e936 100644 --- a/panda/src/linmath/lpoint3_ext_src.I +++ b/panda/src/linmath/lpoint3_ext_src.I @@ -80,3 +80,11 @@ __setattr__(PyObject *self, const std::string &attr_name, PyObject *assign) { // Upcall to LVecBase2. return invoke_extension(_this).__setattr__(self, attr_name, assign); } + +/** + * + */ +INLINE_LINMATH FLOATNAME(LPoint3) Extension:: +__rmul__(FLOATTYPE scalar) const { + return *_this * scalar; +} diff --git a/panda/src/linmath/lpoint3_ext_src.h b/panda/src/linmath/lpoint3_ext_src.h index 3c820d3a45..6789810ff5 100644 --- a/panda/src/linmath/lpoint3_ext_src.h +++ b/panda/src/linmath/lpoint3_ext_src.h @@ -20,6 +20,9 @@ class Extension : public ExtensionBase { public: INLINE_LINMATH PyObject *__getattr__(PyObject *self, const std::string &attr_name) const; INLINE_LINMATH int __setattr__(PyObject *self, const std::string &attr_name, PyObject *assign); + + INLINE_LINMATH FLOATNAME(LPoint3) __rmul__(FLOATTYPE scalar) const; + INLINE_LINMATH std::string __repr__() const; }; diff --git a/panda/src/linmath/lpoint3_src.h b/panda/src/linmath/lpoint3_src.h index 3510aa329e..512bc5ef88 100644 --- a/panda/src/linmath/lpoint3_src.h +++ b/panda/src/linmath/lpoint3_src.h @@ -66,6 +66,8 @@ PUBLISHED: INLINE_LINMATH FLOATNAME(LPoint3) operator * (FLOATTYPE scalar) const; INLINE_LINMATH FLOATNAME(LPoint3) operator / (FLOATTYPE scalar) const; + EXTENSION(INLINE_LINMATH FLOATNAME(LPoint3) __rmul__(FLOATTYPE scalar) const); + // Some special named constructors for LPoint3. INLINE_LINMATH static const FLOATNAME(LPoint3) &origin(CoordinateSystem cs = CS_default); diff --git a/panda/src/linmath/lpoint4_ext_src.I b/panda/src/linmath/lpoint4_ext_src.I index e4101b5ed9..ee64b2c287 100644 --- a/panda/src/linmath/lpoint4_ext_src.I +++ b/panda/src/linmath/lpoint4_ext_src.I @@ -85,3 +85,11 @@ __setattr__(PyObject *self, const std::string &attr_name, PyObject *assign) { // Upcall to LVecBase4. return invoke_extension(_this).__setattr__(self, attr_name, assign); } + +/** + * + */ +INLINE_LINMATH FLOATNAME(LPoint4) Extension:: +__rmul__(FLOATTYPE scalar) const { + return *_this * scalar; +} diff --git a/panda/src/linmath/lpoint4_ext_src.h b/panda/src/linmath/lpoint4_ext_src.h index 82c9ea62f1..2cb8eb6b9f 100644 --- a/panda/src/linmath/lpoint4_ext_src.h +++ b/panda/src/linmath/lpoint4_ext_src.h @@ -20,6 +20,9 @@ class Extension : public ExtensionBase { public: INLINE_LINMATH PyObject *__getattr__(PyObject *self, const std::string &attr_name) const; INLINE_LINMATH int __setattr__(PyObject *self, const std::string &attr_name, PyObject *assign); + + INLINE_LINMATH FLOATNAME(LPoint4) __rmul__(FLOATTYPE scalar) const; + INLINE_LINMATH std::string __repr__() const; }; diff --git a/panda/src/linmath/lpoint4_src.h b/panda/src/linmath/lpoint4_src.h index 7a9902f55d..b0d8eb8036 100644 --- a/panda/src/linmath/lpoint4_src.h +++ b/panda/src/linmath/lpoint4_src.h @@ -54,6 +54,8 @@ PUBLISHED: INLINE_LINMATH FLOATNAME(LPoint4) operator * (FLOATTYPE scalar) const; INLINE_LINMATH FLOATNAME(LPoint4) operator / (FLOATTYPE scalar) const; + EXTENSION(INLINE_LINMATH FLOATNAME(LPoint4) __rmul__(FLOATTYPE scalar) const); + #ifndef FLOATTYPE_IS_INT INLINE_LINMATH FLOATNAME(LPoint4) normalized() const; INLINE_LINMATH FLOATNAME(LPoint4) project(const FLOATNAME(LVecBase4) &onto) const; diff --git a/panda/src/linmath/lvecBase2_ext_src.I b/panda/src/linmath/lvecBase2_ext_src.I index 45d64e0de8..5d731fec7a 100644 --- a/panda/src/linmath/lvecBase2_ext_src.I +++ b/panda/src/linmath/lvecBase2_ext_src.I @@ -192,6 +192,25 @@ __setattr__(PyObject *self, const std::string &attr_name, PyObject *assign) { return 0; } +/** + * + */ +INLINE_LINMATH PyObject *Extension:: +__rmul__(PyObject *self, FLOATTYPE scalar) const { +#ifndef CPPPARSER + extern struct Dtool_PyTypedObject FLOATNAME(Dtool_LVecBase2); +#endif + PyObject *py_vec = PyObject_CallNoArgs((PyObject *)DtoolInstance_TYPE(self)); + if (py_vec != nullptr) { + FLOATNAME(LVecBase2) *vec = (FLOATNAME(LVecBase2) *)DtoolInstance_UPCAST(py_vec, FLOATNAME(Dtool_LVecBase2)); + nassertr(vec != nullptr, nullptr); + + vec->_v(0) = _this->_v(0) * scalar; + vec->_v(1) = _this->_v(1) * scalar; + } + return py_vec; +} + /** * */ diff --git a/panda/src/linmath/lvecBase2_ext_src.h b/panda/src/linmath/lvecBase2_ext_src.h index c57dc15504..dad3807548 100644 --- a/panda/src/linmath/lvecBase2_ext_src.h +++ b/panda/src/linmath/lvecBase2_ext_src.h @@ -23,6 +23,8 @@ public: INLINE_LINMATH int __setattr__(PyObject *self, const std::string &attr_name, PyObject *assign); INLINE_LINMATH std::string __repr__() const; + INLINE_LINMATH PyObject *__rmul__(PyObject *self, FLOATTYPE scalar) const; + INLINE_LINMATH PyObject *__floordiv__(PyObject *self, FLOATTYPE scalar) const; INLINE_LINMATH PyObject *__ifloordiv__(PyObject *self, FLOATTYPE scalar); diff --git a/panda/src/linmath/lvecBase2_src.h b/panda/src/linmath/lvecBase2_src.h index 6e41088706..9e6af9170f 100644 --- a/panda/src/linmath/lvecBase2_src.h +++ b/panda/src/linmath/lvecBase2_src.h @@ -133,6 +133,8 @@ PUBLISHED: INLINE_LINMATH void componentwise_mult(const FLOATNAME(LVecBase2) &other); + EXTENSION(INLINE_LINMATH PyObject *__rmul__(PyObject *self, FLOATTYPE scalar) const); + EXTENSION(INLINE_LINMATH PyObject *__floordiv__(PyObject *self, FLOATTYPE scalar) const); EXTENSION(INLINE_LINMATH PyObject *__ifloordiv__(PyObject *self, FLOATTYPE scalar)); diff --git a/panda/src/linmath/lvecBase3_ext_src.I b/panda/src/linmath/lvecBase3_ext_src.I index 1197f203d6..85486df43d 100644 --- a/panda/src/linmath/lvecBase3_ext_src.I +++ b/panda/src/linmath/lvecBase3_ext_src.I @@ -193,6 +193,27 @@ __setattr__(PyObject *self, const std::string &attr_name, PyObject *assign) { return 0; } + +/** + * + */ +INLINE_LINMATH PyObject *Extension:: +__rmul__(PyObject *self, FLOATTYPE scalar) const { +#ifndef CPPPARSER + extern struct Dtool_PyTypedObject FLOATNAME(Dtool_LVecBase3); +#endif + PyObject *py_vec = PyObject_CallNoArgs((PyObject *)DtoolInstance_TYPE(self)); + if (py_vec != nullptr) { + FLOATNAME(LVecBase3) *vec = (FLOATNAME(LVecBase3) *)DtoolInstance_UPCAST(py_vec, FLOATNAME(Dtool_LVecBase3)); + nassertr(vec != nullptr, nullptr); + + vec->_v(0) = _this->_v(0) * scalar; + vec->_v(1) = _this->_v(1) * scalar; + vec->_v(2) = _this->_v(2) * scalar; + } + return py_vec; +} + /** * */ diff --git a/panda/src/linmath/lvecBase3_ext_src.h b/panda/src/linmath/lvecBase3_ext_src.h index 09b6c30234..6d3d9c85a9 100644 --- a/panda/src/linmath/lvecBase3_ext_src.h +++ b/panda/src/linmath/lvecBase3_ext_src.h @@ -23,6 +23,8 @@ public: INLINE_LINMATH int __setattr__(PyObject *self, const std::string &attr_name, PyObject *assign); INLINE_LINMATH std::string __repr__() const; + INLINE_LINMATH PyObject *__rmul__(PyObject *self, FLOATTYPE scalar) const; + INLINE_LINMATH PyObject *__floordiv__(PyObject *self, FLOATTYPE scalar) const; INLINE_LINMATH PyObject *__ifloordiv__(PyObject *self, FLOATTYPE scalar); diff --git a/panda/src/linmath/lvecBase3_src.h b/panda/src/linmath/lvecBase3_src.h index 72f5ffdf00..64785a4747 100644 --- a/panda/src/linmath/lvecBase3_src.h +++ b/panda/src/linmath/lvecBase3_src.h @@ -152,6 +152,8 @@ PUBLISHED: INLINE_LINMATH void componentwise_mult(const FLOATNAME(LVecBase3) &other); + EXTENSION(INLINE_LINMATH PyObject *__rmul__(PyObject *self, FLOATTYPE scalar) const); + EXTENSION(INLINE_LINMATH PyObject *__floordiv__(PyObject *self, FLOATTYPE scalar) const); EXTENSION(INLINE_LINMATH PyObject *__ifloordiv__(PyObject *self, FLOATTYPE scalar)); diff --git a/panda/src/linmath/lvecBase4_ext_src.I b/panda/src/linmath/lvecBase4_ext_src.I index 858abf7207..b18012cbdc 100644 --- a/panda/src/linmath/lvecBase4_ext_src.I +++ b/panda/src/linmath/lvecBase4_ext_src.I @@ -199,6 +199,28 @@ __setattr__(PyObject *self, const std::string &attr_name, PyObject *assign) { return 0; } + +/** + * + */ +INLINE_LINMATH PyObject *Extension:: +__rmul__(PyObject *self, FLOATTYPE scalar) const { +#ifndef CPPPARSER + extern struct Dtool_PyTypedObject FLOATNAME(Dtool_LVecBase4); +#endif + PyObject *py_vec = PyObject_CallNoArgs((PyObject *)DtoolInstance_TYPE(self)); + if (py_vec != nullptr) { + FLOATNAME(LVecBase4) *vec = (FLOATNAME(LVecBase4) *)DtoolInstance_UPCAST(py_vec, FLOATNAME(Dtool_LVecBase4)); + nassertr(vec != nullptr, nullptr); + + vec->_v(0) = _this->_v(0) * scalar; + vec->_v(1) = _this->_v(1) * scalar; + vec->_v(2) = _this->_v(2) * scalar; + vec->_v(3) = _this->_v(3) * scalar; + } + return py_vec; +} + /** * */ diff --git a/panda/src/linmath/lvecBase4_ext_src.h b/panda/src/linmath/lvecBase4_ext_src.h index 693c07e111..da9dcd3d4b 100644 --- a/panda/src/linmath/lvecBase4_ext_src.h +++ b/panda/src/linmath/lvecBase4_ext_src.h @@ -23,6 +23,8 @@ public: INLINE_LINMATH int __setattr__(PyObject *self, const std::string &attr_name, PyObject *assign); INLINE_LINMATH std::string __repr__() const; + INLINE_LINMATH PyObject *__rmul__(PyObject *self, FLOATTYPE scalar) const; + INLINE_LINMATH PyObject *__floordiv__(PyObject *self, FLOATTYPE scalar) const; INLINE_LINMATH PyObject *__ifloordiv__(PyObject *self, FLOATTYPE scalar); diff --git a/panda/src/linmath/lvecBase4_src.h b/panda/src/linmath/lvecBase4_src.h index fd73d5dbed..e5a23374a4 100644 --- a/panda/src/linmath/lvecBase4_src.h +++ b/panda/src/linmath/lvecBase4_src.h @@ -160,6 +160,8 @@ PUBLISHED: INLINE_LINMATH void componentwise_mult(const FLOATNAME(LVecBase4) &other); + EXTENSION(INLINE_LINMATH PyObject *__rmul__(PyObject *self, FLOATTYPE scalar) const); + EXTENSION(INLINE_LINMATH PyObject *__floordiv__(PyObject *self, FLOATTYPE scalar) const); EXTENSION(INLINE_LINMATH PyObject *__ifloordiv__(PyObject *self, FLOATTYPE scalar)); diff --git a/panda/src/linmath/lvector2_ext_src.I b/panda/src/linmath/lvector2_ext_src.I index 0d9aaa0258..dba65a1b61 100644 --- a/panda/src/linmath/lvector2_ext_src.I +++ b/panda/src/linmath/lvector2_ext_src.I @@ -79,3 +79,11 @@ __setattr__(PyObject *self, const std::string &attr_name, PyObject *assign) { // Upcall to LVecBase2. return invoke_extension(_this).__setattr__(self, attr_name, assign); } + +/** + * + */ +INLINE_LINMATH FLOATNAME(LVector2) Extension:: +__rmul__(FLOATTYPE scalar) const { + return *_this * scalar; +} diff --git a/panda/src/linmath/lvector2_ext_src.h b/panda/src/linmath/lvector2_ext_src.h index 0028c73431..6518c6ebd4 100644 --- a/panda/src/linmath/lvector2_ext_src.h +++ b/panda/src/linmath/lvector2_ext_src.h @@ -20,6 +20,9 @@ class Extension : public ExtensionBase public: INLINE_LINMATH PyObject *__getattr__(PyObject *self, const std::string &attr_name) const; INLINE_LINMATH int __setattr__(PyObject *self, const std::string &attr_name, PyObject *assign); + + INLINE_LINMATH FLOATNAME(LVector2) __rmul__(FLOATTYPE scalar) const; + INLINE_LINMATH std::string __repr__() const; }; diff --git a/panda/src/linmath/lvector2_src.h b/panda/src/linmath/lvector2_src.h index 1454205985..2e0b68f0d3 100644 --- a/panda/src/linmath/lvector2_src.h +++ b/panda/src/linmath/lvector2_src.h @@ -40,6 +40,8 @@ PUBLISHED: INLINE_LINMATH FLOATNAME(LVector2) operator * (FLOATTYPE scalar) const; INLINE_LINMATH FLOATNAME(LVector2) operator / (FLOATTYPE scalar) const; + EXTENSION(INLINE_LINMATH FLOATNAME(LVector2) __rmul__(FLOATTYPE scalar) const); + #ifndef FLOATTYPE_IS_INT INLINE_LINMATH FLOATNAME(LVector2) normalized() const; INLINE_LINMATH FLOATNAME(LVector2) project(const FLOATNAME(LVecBase2) &onto) const; diff --git a/panda/src/linmath/lvector3_ext_src.I b/panda/src/linmath/lvector3_ext_src.I index ee620ba30a..1c309ef87e 100644 --- a/panda/src/linmath/lvector3_ext_src.I +++ b/panda/src/linmath/lvector3_ext_src.I @@ -80,3 +80,11 @@ __setattr__(PyObject *self, const std::string &attr_name, PyObject *assign) { // Upcall to LVecBase3. return invoke_extension(_this).__setattr__(self, attr_name, assign); } + +/** + * + */ +INLINE_LINMATH FLOATNAME(LVector3) Extension:: +__rmul__(FLOATTYPE scalar) const { + return *_this * scalar; +} diff --git a/panda/src/linmath/lvector3_ext_src.h b/panda/src/linmath/lvector3_ext_src.h index 577382dcd8..b6b94c9c74 100644 --- a/panda/src/linmath/lvector3_ext_src.h +++ b/panda/src/linmath/lvector3_ext_src.h @@ -20,6 +20,9 @@ class Extension : public ExtensionBase public: INLINE_LINMATH PyObject *__getattr__(PyObject *self, const std::string &attr_name) const; INLINE_LINMATH int __setattr__(PyObject *self, const std::string &attr_name, PyObject *assign); + + INLINE_LINMATH FLOATNAME(LVector3) __rmul__(FLOATTYPE scalar) const; + INLINE_LINMATH std::string __repr__() const; }; diff --git a/panda/src/linmath/lvector3_src.h b/panda/src/linmath/lvector3_src.h index 1052e5cee8..075dbe338f 100644 --- a/panda/src/linmath/lvector3_src.h +++ b/panda/src/linmath/lvector3_src.h @@ -71,6 +71,8 @@ PUBLISHED: INLINE_LINMATH FLOATNAME(LVector3) operator * (FLOATTYPE scalar) const; INLINE_LINMATH FLOATNAME(LVector3) operator / (FLOATTYPE scalar) const; + EXTENSION(INLINE_LINMATH FLOATNAME(LVector3) __rmul__(FLOATTYPE scalar) const); + // Some special named constructors for LVector3. INLINE_LINMATH static FLOATNAME(LVector3) up(CoordinateSystem cs = CS_default); diff --git a/panda/src/linmath/lvector4_ext_src.I b/panda/src/linmath/lvector4_ext_src.I index 6d4f7743b6..dd8b3a6b48 100644 --- a/panda/src/linmath/lvector4_ext_src.I +++ b/panda/src/linmath/lvector4_ext_src.I @@ -85,3 +85,11 @@ __setattr__(PyObject *self, const std::string &attr_name, PyObject *assign) { // Upcall to LVecBase4. return invoke_extension(_this).__setattr__(self, attr_name, assign); } + +/** + * + */ +INLINE_LINMATH FLOATNAME(LVector4) Extension:: +__rmul__(FLOATTYPE scalar) const { + return *_this * scalar; +} diff --git a/panda/src/linmath/lvector4_ext_src.h b/panda/src/linmath/lvector4_ext_src.h index 4aebf7c1e3..e398875517 100644 --- a/panda/src/linmath/lvector4_ext_src.h +++ b/panda/src/linmath/lvector4_ext_src.h @@ -20,6 +20,9 @@ class Extension : public ExtensionBase public: INLINE_LINMATH PyObject *__getattr__(PyObject *self, const std::string &attr_name) const; INLINE_LINMATH int __setattr__(PyObject *self, const std::string &attr_name, PyObject *assign); + + INLINE_LINMATH FLOATNAME(LVector4) __rmul__(FLOATTYPE scalar) const; + INLINE_LINMATH std::string __repr__() const; }; diff --git a/panda/src/linmath/lvector4_src.h b/panda/src/linmath/lvector4_src.h index cc04c5bbdf..d8a00c7e9f 100644 --- a/panda/src/linmath/lvector4_src.h +++ b/panda/src/linmath/lvector4_src.h @@ -48,6 +48,8 @@ PUBLISHED: INLINE_LINMATH FLOATNAME(LVector4) operator * (FLOATTYPE scalar) const; INLINE_LINMATH FLOATNAME(LVector4) operator / (FLOATTYPE scalar) const; + EXTENSION(INLINE_LINMATH FLOATNAME(LVector4) __rmul__(FLOATTYPE scalar) const); + #ifndef FLOATTYPE_IS_INT INLINE_LINMATH FLOATNAME(LVector4) normalized() const; INLINE_LINMATH FLOATNAME(LVector4) project(const FLOATNAME(LVecBase4) &onto) const; diff --git a/tests/linmath/test_lvector2.py b/tests/linmath/test_lvector2.py index 1f61887bc1..9b77076fa2 100644 --- a/tests/linmath/test_lvector2.py +++ b/tests/linmath/test_lvector2.py @@ -120,6 +120,10 @@ def test_vec2_ceil(): assert rounded_vector.y == -2 +def test_vec2_rmul(): + assert 2 * Vec2(3, -4) == Vec2(6, -8) + + @pytest.mark.parametrize("type", (core.LVecBase2f, core.LVecBase2d, core.LVecBase2i)) def test_vec2_floordiv(type): with pytest.raises(ZeroDivisionError): diff --git a/tests/linmath/test_lvector3.py b/tests/linmath/test_lvector3.py index 4ef45a246f..2e1ceae67d 100644 --- a/tests/linmath/test_lvector3.py +++ b/tests/linmath/test_lvector3.py @@ -105,6 +105,10 @@ def test_vec3_ceil(): assert rounded_vector.z == 4 +def test_vec3_rmul(): + assert 2 * Vec3(0, 3, -4) == Vec3(0, 6, -8) + + @pytest.mark.parametrize("type", (core.LVecBase3f, core.LVecBase3d, core.LVecBase3i)) def test_vec3_floordiv(type): with pytest.raises(ZeroDivisionError): diff --git a/tests/linmath/test_lvector4.py b/tests/linmath/test_lvector4.py index f51bd3f718..0127359c50 100644 --- a/tests/linmath/test_lvector4.py +++ b/tests/linmath/test_lvector4.py @@ -121,6 +121,10 @@ def test_vec4_ceil(): assert rounded_vector.w == 1 +def test_vec4_rmul(): + assert 2 * Vec4(0, 3, -4, 0.5) == Vec4(0, 6, -8, 1) + + @pytest.mark.parametrize("type", (core.LVecBase4f, core.LVecBase4d, core.LVecBase4i)) def test_vec4_floordiv(type): with pytest.raises(ZeroDivisionError):