| 1 |
#include "MathInterface.h" |
| 2 |
|
| 3 |
#include <pybind11/pybind11.h> |
| 4 |
#include <pybind11/operators.h> |
| 5 |
|
| 6 |
#include "math/FloatTools.h" |
| 7 |
#include "math/AABB.h" |
| 8 |
#include "math/Vector2.h" |
| 9 |
#include "math/Vector3.h" |
| 10 |
#include "math/Vector4.h" |
| 11 |
#include "render/Vertex3f.h" |
| 12 |
#include "string/convert.h" |
| 13 |
|
| 14 |
namespace script { |
| 15 |
|
| 16 |
// IScriptInterface implementation |
| 17 |
void MathInterface::registerInterface(py::module& scope, py::dict& globals) |
| 18 |
{ |
| 19 |
// Add the Vector3 class |
| 20 |
py::class_<Vector3> vec3(scope, "Vector3"); |
| 21 |
vec3.def(py::init<double, double, double>()); |
| 22 |
vec3.def(py::init<const Vector3&>()); |
| 23 |
|
| 24 |
// greebo: Pick the correct overload - this is hard to read, but it is necessary |
| 25 |
vec3.def("x", static_cast<double& (Vector3::*)()>(&Vector3::x), py::return_value_policy::reference); |
| 26 |
vec3.def("y", static_cast<double& (Vector3::*)()>(&Vector3::y), py::return_value_policy::reference); |
| 27 |
vec3.def("z", static_cast<double& (Vector3::*)()>(&Vector3::z), py::return_value_policy::reference); |
| 28 |
vec3.def("getLength", &Vector3::getLength); |
| 29 |
vec3.def("getLengthSquared", &Vector3::getLengthSquared); |
| 30 |
vec3.def("getNormalised", &Vector3::getNormalised); |
| 31 |
vec3.def("normalise", &Vector3::normalise); |
| 32 |
vec3.def("dot", &Vector3::dot); |
| 33 |
vec3.def("angle", &Vector3::angle); |
| 34 |
vec3.def("crossProduct", &Vector3::cross); |
| 35 |
// Most important operators |
| 36 |
vec3.def(py::self + py::self); // __add__ |
| 37 |
vec3.def(py::self - py::self); // __sub__ |
| 38 |
vec3.def(py::self += py::self); // __iadd__ |
| 39 |
vec3.def(py::self -= py::self); // __isub__ |
| 40 |
vec3.def(py::self < py::self); // __lt__ |
| 41 |
vec3.def("__repr__", [](const Vector3& vec) |
| 42 |
{ |
| 43 |
return "(" + string::to_string(vec.x()) + " " + string::to_string(vec.y()) + |
| 44 |
" " + string::to_string(vec.z()) + ")"; |
| 45 |
}); |
| 46 |
|
| 47 |
// Register Vertex3, which is an alias of Vector3 |
| 48 |
scope.add_object("Vertex3", vec3); |
| 49 |
|
| 50 |
// Add the Vector2 class |
| 51 |
py::class_<Vector2> vec2(scope, "Vector2"); |
| 52 |
vec2.def(py::init<double, double>()); |
| 53 |
vec2.def(py::init<const Vector2&>()); |
| 54 |
|
| 55 |
// greebo: Pick the correct overload - this is hard to read, but it is necessary |
| 56 |
vec2.def("x", static_cast<double& (Vector2::*)()>(&Vector2::x), py::return_value_policy::reference); |
| 57 |
vec2.def("y", static_cast<double& (Vector2::*)()>(&Vector2::y), py::return_value_policy::reference); |
| 58 |
vec2.def("getLength", &Vector2::getLength); |
| 59 |
vec2.def("getLengthSquared", &Vector2::getLengthSquared); |
| 60 |
vec2.def("dot", &Vector2::dot<double>); |
| 61 |
vec2.def("crossProduct", &Vector2::crossProduct<double>); |
| 62 |
// Most important operators |
| 63 |
vec2.def(py::self + py::self); // __add__ |
| 64 |
vec2.def(py::self - py::self); // __sub__ |
| 65 |
vec2.def(py::self += py::self); |
| 66 |
vec2.def(py::self -= py::self); |
| 67 |
vec2.def(py::self < py::self); // __lt__ |
| 68 |
vec2.def("__repr__", [](const Vector2& vec) |
| 69 |
{ |
| 70 |
return "(" + string::to_string(vec.x()) + " " + string::to_string(vec.y()) + ")"; |
| 71 |
}); |
| 72 |
|
| 73 |
// Add the Vector4 class |
| 74 |
py::class_<Vector4> vec4(scope, "Vector4"); |
| 75 |
vec4.def(py::init<double, double, double, double>()); |
| 76 |
vec4.def(py::init<const Vector4&>()); |
| 77 |
|
| 78 |
// greebo: Pick the correct overload - this is hard to read, but it is necessary |
| 79 |
vec4.def("x", static_cast<double& (Vector4::*)()>(&Vector4::x), py::return_value_policy::reference); |
| 80 |
vec4.def("y", static_cast<double& (Vector4::*)()>(&Vector4::y), py::return_value_policy::reference); |
| 81 |
vec4.def("z", static_cast<double& (Vector4::*)()>(&Vector4::z), py::return_value_policy::reference); |
| 82 |
vec4.def("w", static_cast<double& (Vector4::*)()>(&Vector4::w), py::return_value_policy::reference); |
| 83 |
vec4.def("getVector3", &Vector4::getVector3); |
| 84 |
vec4.def("getProjected", &Vector4::getProjected); |
| 85 |
vec4.def("dot", &Vector4::dot); |
| 86 |
// Most important operators |
| 87 |
vec4.def(py::self + py::self); // __add__ |
| 88 |
vec4.def(py::self - py::self); // __sub__ |
| 89 |
vec4.def(py::self += py::self); |
| 90 |
vec4.def(py::self -= py::self); |
| 91 |
vec4.def("__repr__", [](const Vector4& vec) |
| 92 |
{ |
| 93 |
return "(" + string::to_string(vec.x()) + " " + string::to_string(vec.y()) + " " + |
| 94 |
string::to_string(vec.z()) + " " + string::to_string(vec.w()) + ")"; |
| 95 |
}); |
| 96 |
|
| 97 |
// Add the Vector4 and Quaternion with the same interface |
| 98 |
scope.add_object("Quaternion", vec4); |
| 99 |
|
| 100 |
// Declare AABB to python |
| 101 |
py::class_<AABB> aabb(scope, "AABB"); |
| 102 |
aabb.def(py::init<>()); |
| 103 |
aabb.def(py::init<const Vector3&, const Vector3&>()); |
| 104 |
aabb.def_readwrite("origin", &AABB::origin); |
| 105 |
aabb.def_readwrite("extents", &AABB::extents); |
| 106 |
aabb.def("isValid", &AABB::isValid); |
| 107 |
aabb.def("getRadius", &AABB::getRadius); |
| 108 |
aabb.def("includePoint", &AABB::includePoint); |
| 109 |
aabb.def("includeAABB", &AABB::includeAABB); |
| 110 |
} |
| 111 |
|
| 112 |
} // namespace script |