This repository was archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmathtypes.cpp
More file actions
205 lines (160 loc) · 6.31 KB
/
mathtypes.cpp
File metadata and controls
205 lines (160 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <pybind11/operators.h>
#include "../RocketSim/src/Math/MathTypes/MathTypes.h"
namespace py = pybind11;
using namespace pybind11::literals;
void init_mathtypes(py::module_ &m) {
py::class_<Vec>(m, "Vec")
.def(py::init<float, float, float>(), "x"_a = 0, "y"_a = 0, "z"_a = 0)
//The _a suffix forms a C++11 literal which is equivalent to py::arg()
.def_readwrite("x", &Vec::x)
.def_readwrite("y", &Vec::y)
.def_readwrite("z", &Vec::z)
.def("is_zero", &Vec::IsZero)
.def("length_sq", &Vec::LengthSq)
.def("length", &Vec::Length)
.def("length", &Vec::Length)
.def("dot", &Vec::Dot, "other"_a)
.def("cross", &Vec::Cross, "other"_a)
.def("dist_sq", &Vec::DistSq, "other"_a)
.def("dist", &Vec::Dist, "other"_a)
.def("dist_sq_2d", &Vec::DistSq2D, "other"_a)
.def("dist_2d", &Vec::Dist2D, "other"_a)
.def("normalized", &Vec::Normalized)
.def("__getitem__", [](const Vec &vec, uint32_t index) {
if (0 <= index && index < 3)
return vec[index];
throw py::index_error("list index out of range");
})
.def("__setitem__", [](Vec &vec, uint32_t index, float val) {
if (0 <= index && index < 3)
vec[index] = val;
else throw py::index_error("list index out of range");
})
.def(py::self + py::self)
.def(py::self - py::self)
.def(py::self * py::self)
.def(py::self / py::self)
.def(py::self += py::self)
.def(py::self -= py::self)
.def(py::self *= py::self)
.def(py::self /= py::self)
// some operators were not defined in RocketSim at the time of writing this
.def(py::self * float())
.def(py::self / float())
.def(py::self *= float())
.def(py::self /= float())
.def(py::self < py::self)
.def(py::self > py::self)
.def(-py::self)
.def(py::self == py::self)
.def(py::self != py::self)
.def("__format__", [](const Vec& vec, const char* spec) {
return py::str("[{1:{0}}, {2:{0}}, {3:{0}}]").format(spec,
vec.x, vec.y, vec.z);
})
.def("__str__", [](const Vec &vec) {
return py::str("[{}, {}, {}]").format(vec.x, vec.y, vec.z);
})
.def("__repr__", [](const Vec &vec) {
return py::str("<Vec: {}>").format(vec);
})
.def("as_tuple", [](const Vec &vec) {
return py::make_tuple(vec.x, vec.y, vec.z);
})
.def("as_numpy", [](const Vec &vec) {
py::array_t<float> arr({3});
auto buf = arr.mutable_data();
buf[0] = vec.x;
buf[1] = vec.y;
buf[2] = vec.z;
return arr;
});
py::class_<RotMat>(m, "RotMat")
.def(py::init<>())
.def(py::init<Vec, Vec, Vec>(), "forward"_a, "right"_a, "up"_a)
.def_readwrite("forward", &RotMat::forward)
.def_readwrite("right", &RotMat::right)
.def_readwrite("up", &RotMat::up)
.def_static("get_identity", &RotMat::GetIdentity)
.def("__getitem__", [](const RotMat &mat, int index) {
if (0 <= index && index < 3)
return mat[index];
throw py::index_error("list index out of range");
})
.def("__setitem__", [](RotMat &mat, uint32_t index, Vec &vec) {
if (0 <= index && index < 3)
mat[index] = vec;
else throw py::index_error("list index out of range");
})
.def(py::self + py::self)
.def(py::self - py::self)
.def(py::self += py::self)
.def(py::self -= py::self)
.def(py::self * float())
.def(py::self / float())
.def(py::self *= float())
.def(py::self /= float())
.def(py::self == py::self)
.def(py::self != py::self)
.def("dot", &RotMat::Dot, "vec"_a)
.def("transpose", &RotMat::Transpose)
.def("__format__", [](const RotMat& mat, const char* spec) {
return py::str("(FRU) [\n {1:{0}},\n {2:{0}},\n {3:{0}}]").format(spec,
mat.forward, mat.right, mat.up);
})
.def("__str__", [](const RotMat &mat) {
return py::str("{}").format(mat);
})
.def("__repr__", [](const RotMat &mat) {
return py::str("<RotMat: {}>").format(mat);
})
.def("as_angle", [](const RotMat &mat) {
return Angle().FromRotMat(mat);
})
.def("as_numpy", [](const RotMat &mat) {
py::array_t<float> arr({3, 3});
auto buf = arr.mutable_data();
buf[0] = mat.forward.x;
buf[1] = mat.forward.y;
buf[2] = mat.forward.z;
buf[3] = mat.right.x;
buf[4] = mat.right.y;
buf[5] = mat.right.z;
buf[6] = mat.up.x;
buf[7] = mat.up.y;
buf[8] = mat.up.z;
return arr;
});
py::class_<Angle>(m, "Angle")
.def(py::init<float, float, float>(), "yaw"_a = 0, "pitch"_a = 0, "roll"_a = 0)
.def_readwrite("yaw", &Angle::yaw)
.def_readwrite("pitch", &Angle::pitch)
.def_readwrite("roll", &Angle::roll)
.def_static("from_rot_mat", &Angle::FromRotMat, "rot_mat"_a)
.def("to_rot_mat", &Angle::ToRotMat)
.def("get_forward_vector", &Angle::GetForwardVector)
.def("normalize_fix", &Angle::NormalizeFix)
.def("as_tuple", [](const Angle &ang) {
return py::make_tuple(ang.yaw, ang.pitch, ang.roll);
})
.def("as_numpy", [](const Angle &ang) {
py::array_t<float> arr({3});
auto buf = arr.mutable_data();
buf[0] = ang.yaw;
buf[1] = ang.pitch;
buf[2] = ang.roll;
return arr;
})
.def("__format__", [](const Angle &ang, const char* spec) {
return py::str("(YPR) [{1:{0}}, {2:{0}}, {3:{0}}]").format(spec,
ang.yaw, ang.pitch, ang.roll);
})
.def("__str__", [](const Angle &ang) {
return py::str("{}").format(ang);
})
.def("__repr__", [](const Angle &ang) {
return py::str("<Angle: {}>").format(ang);
});
}