From cfbcc158bfc2f83eb67001de295870119e2be222 Mon Sep 17 00:00:00 2001 From: Thomas Ferreira de Lima Date: Thu, 2 Aug 2018 00:37:43 -0400 Subject: [PATCH] adding / and /= operators to Point and Vector types in db [ci skip] --- src/db/db/dbPoint.h | 40 +++++++++++++++++++++++++++++++++++ src/db/db/dbVector.h | 40 +++++++++++++++++++++++++++++++++++ src/db/db/gsiDeclDbPoint.cc | 41 ++++++++++++++++++++++++++++++++++++ src/db/db/gsiDeclDbVector.cc | 41 ++++++++++++++++++++++++++++++++++++ src/pya/pya/pyaModule.cc | 8 +++++++ 5 files changed, 170 insertions(+) diff --git a/src/db/db/dbPoint.h b/src/db/db/dbPoint.h index c767f7a57..157412fe6 100644 --- a/src/db/db/dbPoint.h +++ b/src/db/db/dbPoint.h @@ -194,6 +194,20 @@ public: */ point &operator*= (long s); + /** + * @brief Division by some divisor. + * + * Scaline involves rounding which in our case is simply handled + * with the coord_traits scheme. + */ + + point &operator/= (double s); + + /** + * @brief Dividing self by some integer divisor + */ + point &operator/= (long s); + /** * @brief The euclidian distance to another point * @@ -452,6 +466,32 @@ operator* (const db::point &p, unsigned int s) return point (p.x () * s, p.y () * s); } +template +inline point +operator/ (const db::point &p, Number s) +{ + double mult = 1.0 / static_cast(s); + return point (p.x () * mult, p.y () * mult); +} + +template +inline point & +point::operator/= (double s) +{ + double mult = 1.0 / static_cast(s); + *this *= mult; + return *this; +} + +template +inline point & +point::operator/= (long s) +{ + double mult = 1.0 / static_cast(s); + *this *= mult; + return *this; +} + template inline point & point::operator*= (double s) diff --git a/src/db/db/dbVector.h b/src/db/db/dbVector.h index ad86d4502..f3432c057 100644 --- a/src/db/db/dbVector.h +++ b/src/db/db/dbVector.h @@ -266,6 +266,20 @@ public: */ vector operator*= (long s); + /** + * @brief Division by some divisor. + * + * Scaline involves rounding which in our case is simply handled + * with the coord_traits scheme. + */ + + vector &operator/= (double s); + + /** + * @brief Dividing self by some integer divisor + */ + vector &operator/= (long s); + /** * @brief The euclidian length */ @@ -450,6 +464,32 @@ vector::operator* (long s) const return vector (m_x * s, m_y * s); } +template +inline vector +operator/ (const db::vector &p, Number s) +{ + double mult = 1.0 / static_cast(s); + return vector (p.x () * mult, p.y () * mult); +} + +template +inline vector & +vector::operator/= (double s) +{ + double mult = 1.0 / static_cast(s); + *this *= mult; + return *this; +} + +template +inline vector & +vector::operator/= (long s) +{ + double mult = 1.0 / static_cast(s); + *this *= mult; + return *this; +} + template inline vector vector::operator*= (double s) diff --git a/src/db/db/gsiDeclDbPoint.cc b/src/db/db/gsiDeclDbPoint.cc index d80b36959..46892b586 100644 --- a/src/db/db/gsiDeclDbPoint.cc +++ b/src/db/db/gsiDeclDbPoint.cc @@ -70,6 +70,23 @@ struct point_defs return C (*p * s); } + static C divide (const C *p, double s) + { + return C (*p / s); + } + + static C iscale (C *p, double s) + { + *p *= s; + return *p; + } + + static C idiv (C *p, double s) + { + *p /= s; + return *p; + } + static C negate (const C *p) { return -*p; @@ -175,6 +192,30 @@ struct point_defs "Returns the scaled object. All coordinates are multiplied with the given factor and if " "necessary rounded." ) + + method_ext ("*=", &iscale, + "@brief Scaling by some factor\n" + "\n" + "@args f\n" + "\n" + "Scales object in place. All coordinates are multiplied with the given factor and if " + "necessary rounded." + ) + + method_ext ("/", ÷, + "@brief Division by some divisor\n" + "\n" + "@args d\n" + "\n" + "Returns the scaled object. All coordinates are divided with the given divisor and if " + "necessary rounded." + ) + + method_ext ("/=", &idiv, + "@brief Division by some divisor\n" + "\n" + "@args d\n" + "\n" + "Divides the object in place. All coordinates are divided with the given divisor and if " + "necessary rounded." + ) + method ("distance", (double (C::*) (const C &) const) &C::double_distance, "@brief The Euclidian distance to another point\n" "\n" diff --git a/src/db/db/gsiDeclDbVector.cc b/src/db/db/gsiDeclDbVector.cc index 49cf76bb0..aacf6aba4 100644 --- a/src/db/db/gsiDeclDbVector.cc +++ b/src/db/db/gsiDeclDbVector.cc @@ -70,6 +70,23 @@ struct vector_defs return C (*p * s); } + static C divide (const C *p, double s) + { + return C (*p / s); + } + + static C iscale (C *p, double s) + { + *p *= s; + return *p; + } + + static C idiv (C *p, double s) + { + *p /= s; + return *p; + } + static C negate (const C *p) { return -*p; @@ -202,6 +219,30 @@ struct vector_defs "Returns the scaled object. All coordinates are multiplied with the given factor and if " "necessary rounded." ) + + method_ext ("*=", &iscale, + "@brief Scaling by some factor\n" + "\n" + "@args f\n" + "\n" + "Scales object in place. All coordinates are multiplied with the given factor and if " + "necessary rounded." + ) + + method_ext ("/", ÷, + "@brief Division by some divisor\n" + "\n" + "@args d\n" + "\n" + "Returns the scaled object. All coordinates are divided with the given divisor and if " + "necessary rounded." + ) + + method_ext ("/=", &idiv, + "@brief Division by some divisor\n" + "\n" + "@args d\n" + "\n" + "Divides the object in place. All coordinates are divided with the given divisor and if " + "necessary rounded." + ) + method_ext ("vprod", &vprod, "@brief Computes the vector product between self and the given vector\n" "\n" diff --git a/src/pya/pya/pyaModule.cc b/src/pya/pya/pyaModule.cc index 90b03a704..df7ed6c19 100644 --- a/src/pya/pya/pyaModule.cc +++ b/src/pya/pya/pyaModule.cc @@ -493,7 +493,11 @@ static std::string extract_python_name (const std::string &name) } else if (name == "-@") { return "__neg__"; } else if (name == "/") { + #if PY_MAJOR_VERSION < 3 return "__div__"; + #else + return "__truediv__"; + #endif } else if (name == "*") { return "__mul__"; } else if (name == "%") { @@ -515,7 +519,11 @@ static std::string extract_python_name (const std::string &name) } else if (name == "-=") { return "__isub__"; } else if (name == "/=") { + #if PY_MAJOR_VERSION < 3 return "__idiv__"; + #else + return "__itruediv__"; + #endif } else if (name == "*=") { return "__imul__"; } else if (name == "%=") {