Browse Source

python: return proper type when doing operations on Color.

pull/2/head
Vladimír Vondruš 7 years ago
parent
commit
124980cec4
  1. 60
      src/python/magnum/math.vector.h
  2. 5
      src/python/magnum/math.vectorfloat.cpp
  3. 3
      src/python/magnum/math.vectorintegral.cpp
  4. 12
      src/python/magnum/test/test_math.py

60
src/python/magnum/math.vector.h

@ -33,6 +33,33 @@
namespace magnum {
/* Things that have to be defined for both VectorN and Color so they construct
/ return a proper type */
template<class T, class ...Args> void everyVector(py::class_<T, Args...>& c) {
c
.def_static("zero_init", []() {
return T{Math::ZeroInit};
}, "Construct a zero vector")
.def(py::init(), "Default constructor")
/* Operators */
.def(-py::self, "Negated vector")
.def(py::self += py::self, "Add and assign a vector")
.def(py::self + py::self, "Add a vector")
.def(py::self -= py::self, "Subtract and assign a vector")
.def(py::self - py::self, "Subtract a vector")
.def(py::self *= typename T::Type{}, "Multiply with a scalar and assign")
.def(py::self * typename T::Type{}, "Multiply with a scalar")
.def(py::self /= typename T::Type{}, "Divide with a scalar and assign")
.def(py::self / typename T::Type{}, "Divide with a scalar")
.def(py::self *= py::self, "Multiply a vector component-wise and assign")
.def(py::self * py::self, "Multiply a vector component-wise")
.def(py::self /= py::self, "Divide a vector component-wise and assign")
.def(py::self / py::self, "Divide a vector component-wise")
.def(typename T::Type{} * py::self, "Multiply a scalar with a vector")
.def(typename T::Type{} / py::self, "Divide a vector with a scalar and invert");
}
/* Things common for vectors of all sizes and types */
template<class T> void vector(py::module& m, py::class_<T>& c) {
/*
@ -50,10 +77,6 @@ template<class T> void vector(py::module& m, py::class_<T>& c) {
c
/* Constructors */
.def_static("zero_init", []() {
return T{Math::ZeroInit};
}, "Construct a zero vector")
.def(py::init(), "Default constructor")
.def(py::init<typename T::Type>(), "Construct a vector with one value for all components")
/* Comparison */
@ -75,23 +98,6 @@ template<class T> void vector(py::module& m, py::class_<T>& c) {
return self[i];
}, "Value at given position")
/* Operators */
.def(-py::self, "Negated vector")
.def(py::self += py::self, "Add and assign a vector")
.def(py::self + py::self, "Add a vector")
.def(py::self -= py::self, "Subtract and assign a vector")
.def(py::self - py::self, "Subtract a vector")
.def(py::self *= typename T::Type{}, "Multiply with a scalar and assign")
.def(py::self * typename T::Type{}, "Multiply with a scalar")
.def(py::self /= typename T::Type{}, "Divide with a scalar and assign")
.def(py::self / typename T::Type{}, "Divide with a scalar")
.def(py::self *= py::self, "Multiply a vector component-wise and assign")
.def(py::self * py::self, "Multiply a vector component-wise")
.def(py::self /= py::self, "Divide a vector component-wise and assign")
.def(py::self / py::self, "Divide a vector component-wise")
.def(typename T::Type{} * py::self, "Multiply a scalar with a vector")
.def(typename T::Type{} / py::self, "Divide a vector with a scalar and invert")
/* Member functions common for floating-point and integer types */
.def("is_zero", &T::isZero, "Whether the vector is zero")
.def("dot", static_cast<typename T::Type(T::*)() const>(&T::dot), "Dot product of the vector")
@ -341,7 +347,17 @@ template<class T> void color4(py::class_<Math::Color4<T>, Math::Vector4<T>>& c)
return Degd(self.hue());
}, "Hue")
.def("saturation", &Math::Color4<T>::saturation, "Saturation")
.def("value", &Math::Color4<T>::value, "Value");
.def("value", &Math::Color4<T>::value, "Value")
/* Properties */
.def_property("xyz",
static_cast<const Math::Color3<T>(Math::Color4<T>::*)() const>(&Math::Color4<T>::xyz),
[](Math::Color4<T>& self, const Math::Color3<T>& value) { self.xyz() = value; },
"XYZ part of the vector")
.def_property("rgb",
static_cast<const Math::Color3<T>(Math::Color4<T>::*)() const>(&Math::Color4<T>::rgb),
[](Math::Color4<T>& self, const Math::Color3<T>& value) { self.rgb() = value; },
"RGB part of the vector");
}
}

5
src/python/magnum/math.vectorfloat.cpp

@ -58,6 +58,7 @@ template<class T> void vectorsFloat(py::module& m, py::class_<Math::Vector2<T>>&
"Aspect ratio")
.def("cross", static_cast<T(*)(const Math::Vector2<T>&, const Math::Vector2<T>&)>(Math::cross),
"2D cross product");
everyVector(vector2_);
vector<Math::Vector2<T>>(m, vector2_);
vectorFloat<Math::Vector2<T>>(m, vector2_);
vector2<T>(vector2_);
@ -65,10 +66,12 @@ template<class T> void vectorsFloat(py::module& m, py::class_<Math::Vector2<T>>&
vector3_
.def("cross", static_cast<Math::Vector3<T>(*)(const Math::Vector3<T>&, const Math::Vector3<T>&)>(Math::cross),
"Cross product");
everyVector(vector3_);
vector<Math::Vector3<T>>(m, vector3_);
vectorFloat<Math::Vector3<T>>(m, vector3_);
vector3<T>(vector3_);
everyVector(vector4_);
vector<Math::Vector4<T>>(m, vector4_);
vectorFloat<Math::Vector4<T>>(m, vector4_);
vector4<T>(vector4_);
@ -87,10 +90,12 @@ void mathVectorFloat(py::module& root, py::module& m) {
vectorsFloat<Double>(m, vector2d, vector3d, vector4d);
py::class_<Color3, Vector3> color3_{root, "Color3", "Color in linear RGB color space"};
everyVector(color3_);
color(color3_);
color3(color3_);
py::class_<Color4, Vector4> color4_{root, "Color4", "Color in linear RGBA color space"};
everyVector(color4_);
color(color4_);
color4(color4_);

3
src/python/magnum/math.vectorintegral.cpp

@ -54,14 +54,17 @@ template<class T> void vectorIntegral(py::class_<T>& c) {
}
template<class T> void vectorsIntegral(py::module& m, py::class_<Math::Vector2<T>>& vector2_, py::class_<Math::Vector3<T>>& vector3_, py::class_<Math::Vector4<T>>& vector4_) {
everyVector(vector2_);
vector<Math::Vector2<T>>(m, vector2_);
vectorIntegral<Math::Vector2<T>>(vector2_);
vector2<T>(vector2_);
everyVector(vector3_);
vector<Math::Vector3<T>>(m, vector3_);
vectorIntegral<Math::Vector3<T>>(vector3_);
vector3<T>(vector3_);
everyVector(vector4_);
vector<Math::Vector4<T>>(m, vector4_);
vectorIntegral<Math::Vector4<T>>(vector4_);
vector4<T>(vector4_);

12
src/python/magnum/test/test_math.py

@ -292,6 +292,11 @@ class Color3_(unittest.TestCase):
self.assertAlmostEqual(a.to_hsv()[1], 0.749)
self.assertAlmostEqual(a.to_hsv()[2], 0.427)
def test_methods_return_type(self):
self.assertIsInstance(Color3()*1.5, Color3)
self.assertIsInstance(Color3()+Color3(), Color3)
self.assertIsInstance(Color3.zero_init(), Color3)
class Color4_(unittest.TestCase):
def test_init(self):
a1 = Color4()
@ -338,6 +343,13 @@ class Color4_(unittest.TestCase):
b = Color4.from_hsv(Deg(230.0), 0.749, 0.427)
self.assertEqual(b, Color4(0.107177, 0.160481, 0.427, 1.0))
def test_methods_return_type(self):
self.assertIsInstance(Color4()*1.5, Color4)
self.assertIsInstance(Color4()+Color4(), Color4)
self.assertIsInstance(Color4.zero_init(), Color4)
self.assertIsInstance(Color4().rgb, Color3)
self.assertIsInstance(Color4().xyz, Color3)
class Matrix(unittest.TestCase):
def test_init(self):
a = Matrix3x2()

Loading…
Cancel
Save