Repository navigation
Expand file tree
/
Copy pathUEPyFVector2D.cpp
More file actions
368 lines (324 loc) · 10.1 KB
/
Copy pathUEPyFVector2D.cpp
File metadata and controls
368 lines (324 loc) · 10.1 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include "UEPyFVector2D.h"
static PyObject *py_ue_fvector2d_length(ue_PyFVector2D *self, PyObject * args)
{
return PyFloat_FromDouble(self->vec.Size());
}
static PyObject *py_ue_fvector2d_length_squared(ue_PyFVector2D *self, PyObject * args)
{
return PyFloat_FromDouble(self->vec.SizeSquared());
}
static PyObject *py_ue_fvector2d_normalized(ue_PyFVector2D *self, PyObject * args)
{
FVector2D vec = self->vec;
vec.Normalize();
return py_ue_new_fvector2d(vec);
}
static PyObject *py_ue_fvector2d_dot(ue_PyFVector2D *self, PyObject * args)
{
PyObject *py_obj;
if (!PyArg_ParseTuple(args, "O:dot", &py_obj))
return NULL;
ue_PyFVector2D *py_vec = py_ue_is_fvector2d(py_obj);
if (!py_vec)
return PyErr_Format(PyExc_TypeError, "argument is not a FVector2D");
return PyFloat_FromDouble(FVector2D::DotProduct(self->vec, py_vec->vec));
}
static PyObject *py_ue_fvector2d_cross(ue_PyFVector2D *self, PyObject * args)
{
PyObject *py_obj;
if (!PyArg_ParseTuple(args, "O:cross", &py_obj))
return NULL;
ue_PyFVector2D *py_vec = py_ue_is_fvector2d(py_obj);
if (!py_vec)
return PyErr_Format(PyExc_TypeError, "argument is not a FVector2D");
return PyFloat_FromDouble(FVector2D::CrossProduct(self->vec, py_vec->vec));
}
static PyMethodDef ue_PyFVector2D_methods[] = {
{ "length", (PyCFunction)py_ue_fvector2d_length, METH_VARARGS, "" },
{ "size", (PyCFunction)py_ue_fvector2d_length, METH_VARARGS, "" },
{ "size_squared", (PyCFunction)py_ue_fvector2d_length_squared, METH_VARARGS, "" },
{ "length_squared", (PyCFunction)py_ue_fvector2d_length_squared, METH_VARARGS, "" },
{ "normalized", (PyCFunction)py_ue_fvector2d_normalized, METH_VARARGS, "" },
{ "dot", (PyCFunction)py_ue_fvector2d_dot, METH_VARARGS, "" },
{ "cross", (PyCFunction)py_ue_fvector2d_cross, METH_VARARGS, "" },
{ NULL } /* Sentinel */
};
static PyObject *py_ue_fvector2d_get_x(ue_PyFVector2D *self, void *closure)
{
return PyFloat_FromDouble(self->vec.X);
}
static int py_ue_fvector2d_set_x(ue_PyFVector2D *self, PyObject *value, void *closure)
{
if (value && PyNumber_Check(value))
{
PyObject *f_value = PyNumber_Float(value);
self->vec.X = PyFloat_AsDouble(f_value);
Py_DECREF(f_value);
return 0;
}
PyErr_SetString(PyExc_TypeError, "value is not numeric");
return -1;
}
static PyObject *py_ue_fvector2d_get_y(ue_PyFVector2D *self, void *closure)
{
return PyFloat_FromDouble(self->vec.Y);
}
static int py_ue_fvector2d_set_y(ue_PyFVector2D *self, PyObject *value, void *closure)
{
if (value && PyNumber_Check(value))
{
PyObject *f_value = PyNumber_Float(value);
self->vec.Y = PyFloat_AsDouble(f_value);
Py_DECREF(f_value);
return 0;
}
PyErr_SetString(PyExc_TypeError, "value is not numeric");
return -1;
}
static PyGetSetDef ue_PyFVector2D_getseters[] = {
{(char *) "x", (getter)py_ue_fvector2d_get_x, (setter)py_ue_fvector2d_set_x, (char *)"", NULL },
{(char *) "y", (getter)py_ue_fvector2d_get_y, (setter)py_ue_fvector2d_set_y, (char *)"", NULL },
{ NULL } /* Sentinel */
};
static PyObject *ue_PyFVector2D_str(ue_PyFVector2D *self)
{
return PyUnicode_FromFormat("<unreal_engine.FVector2D {'x': %S, 'y': %S}>",
PyFloat_FromDouble(self->vec.X), PyFloat_FromDouble(self->vec.Y));
}
PyTypeObject ue_PyFVector2DType = {
PyVarObject_HEAD_INIT(NULL, 0)
"unreal_engine.FVector2D", /* tp_name */
sizeof(ue_PyFVector2D), /* tp_basicsize */
0, /* tp_itemsize */
0, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
(reprfunc)ue_PyFVector2D_str, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
#if PY_MAJOR_VERSION < 3
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /* tp_flags */
#else
Py_TPFLAGS_DEFAULT, /* tp_flags */
#endif
"Unreal Engine FVector2D", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
ue_PyFVector2D_methods, /* tp_methods */
0,
ue_PyFVector2D_getseters,
};
static PyObject *ue_py_fvector2d_add(ue_PyFVector2D *self, PyObject *value)
{
FVector2D vec = self->vec;
ue_PyFVector2D *py_vec = py_ue_is_fvector2d(value);
if (py_vec)
{
vec += py_vec->vec;
}
else if (PyNumber_Check(value))
{
PyObject *f_value = PyNumber_Float(value);
float f = PyFloat_AsDouble(f_value);
vec.X += f;
vec.Y += f;
Py_DECREF(f_value);
}
return py_ue_new_fvector2d(vec);
}
static PyObject *ue_py_fvector2d_sub(ue_PyFVector2D *self, PyObject *value)
{
FVector2D vec = self->vec;
ue_PyFVector2D *py_vec = py_ue_is_fvector2d(value);
if (py_vec)
{
vec -= py_vec->vec;
}
else if (PyNumber_Check(value))
{
PyObject *f_value = PyNumber_Float(value);
float f = PyFloat_AsDouble(f_value);
vec.X -= f;
vec.Y -= f;
Py_DECREF(f_value);
}
return py_ue_new_fvector2d(vec);
}
static PyObject *ue_py_fvector2d_mul(ue_PyFVector2D *self, PyObject *value)
{
FVector2D vec = self->vec;
ue_PyFVector2D *py_vec = py_ue_is_fvector2d(value);
if (py_vec)
{
vec *= py_vec->vec;
}
else if (PyNumber_Check(value))
{
PyObject *f_value = PyNumber_Float(value);
float f = PyFloat_AsDouble(f_value);
vec *= f;
Py_DECREF(f_value);
}
return py_ue_new_fvector2d(vec);
}
static PyObject *ue_py_fvector2d_div(ue_PyFVector2D *self, PyObject *value)
{
FVector2D vec = self->vec;
ue_PyFVector2D *py_vec = py_ue_is_fvector2d(value);
if (py_vec)
{
if (py_vec->vec.X == 0 || py_vec->vec.Y == 0)
return PyErr_Format(PyExc_ZeroDivisionError, "division by zero");
vec /= py_vec->vec;
}
else if (PyNumber_Check(value))
{
PyObject *f_value = PyNumber_Float(value);
float f = PyFloat_AsDouble(f_value);
if (f == 0)
return PyErr_Format(PyExc_ZeroDivisionError, "division by zero");
vec /= f;
Py_DECREF(f_value);
}
return py_ue_new_fvector2d(vec);
}
static PyObject *ue_py_fvector2d_floor_div(ue_PyFVector2D *self, PyObject *value)
{
FVector2D vec = self->vec;
if (PyNumber_Check(value))
{
PyObject *f_value = PyNumber_Float(value);
float f = PyFloat_AsDouble(f_value);
if (f == 0)
return PyErr_Format(PyExc_ZeroDivisionError, "division by zero");
vec.X = floor(vec.X / f);
vec.Y = floor(vec.Y / f);
Py_DECREF(f_value);
return py_ue_new_fvector2d(vec);
}
return PyErr_Format(PyExc_TypeError, "value is not numeric");
}
PyNumberMethods ue_PyFVector2D_number_methods;
static Py_ssize_t ue_py_fvector2d_seq_length(ue_PyFVector2D *self)
{
return 2;
}
static PyObject *ue_py_fvector2d_seq_item(ue_PyFVector2D *self, Py_ssize_t i)
{
switch (i)
{
case 0:
return PyFloat_FromDouble(self->vec.X);
case 1:
return PyFloat_FromDouble(self->vec.Y);
}
return PyErr_Format(PyExc_IndexError, "FVector2D has only 2 items");
}
PySequenceMethods ue_PyFVector2D_sequence_methods;
static int ue_py_fvector2d_init(ue_PyFVector2D *self, PyObject *args, PyObject *kwargs)
{
float x = 0, y = 0;
if (!PyArg_ParseTuple(args, "|ff", &x, &y))
return -1;
if (PyTuple_Size(args) == 1)
{
y = x;
}
self->vec.X = x;
self->vec.Y = y;
return 0;
}
static PyObject *ue_py_fvector2d_richcompare(ue_PyFVector2D *vec1, PyObject *b, int op)
{
ue_PyFVector2D *vec2 = py_ue_is_fvector2d(b);
if (!vec2 || (op != Py_EQ && op != Py_NE))
{
return PyErr_Format(PyExc_NotImplementedError, "can only compare with another FVector2D");
}
if (op == Py_EQ)
{
if (vec1->vec.X == vec2->vec.X &&
vec1->vec.Y == vec2->vec.Y)
{
Py_INCREF(Py_True);
return Py_True;
}
Py_INCREF(Py_False);
return Py_False;
}
if (vec1->vec.X == vec2->vec.X &&
vec1->vec.Y == vec2->vec.Y)
{
Py_INCREF(Py_False);
return Py_False;
}
Py_INCREF(Py_True);
return Py_True;
}
void ue_python_init_fvector2d(PyObject *ue_module)
{
ue_PyFVector2DType.tp_new = PyType_GenericNew;
ue_PyFVector2DType.tp_init = (initproc)ue_py_fvector2d_init;
ue_PyFVector2DType.tp_richcompare = (richcmpfunc)ue_py_fvector2d_richcompare;
memset(&ue_PyFVector2D_number_methods, 0, sizeof(PyNumberMethods));
ue_PyFVector2DType.tp_as_number = &ue_PyFVector2D_number_methods;
ue_PyFVector2D_number_methods.nb_add = (binaryfunc)ue_py_fvector2d_add;
ue_PyFVector2D_number_methods.nb_subtract = (binaryfunc)ue_py_fvector2d_sub;
ue_PyFVector2D_number_methods.nb_multiply = (binaryfunc)ue_py_fvector2d_mul;
ue_PyFVector2D_number_methods.nb_true_divide = (binaryfunc)ue_py_fvector2d_div;
ue_PyFVector2D_number_methods.nb_floor_divide = (binaryfunc)ue_py_fvector2d_floor_div;
memset(&ue_PyFVector2D_sequence_methods, 0, sizeof(PySequenceMethods));
ue_PyFVector2DType.tp_as_sequence = &ue_PyFVector2D_sequence_methods;
ue_PyFVector2D_sequence_methods.sq_length = (lenfunc)ue_py_fvector2d_seq_length;
ue_PyFVector2D_sequence_methods.sq_item = (ssizeargfunc)ue_py_fvector2d_seq_item;
if (PyType_Ready(&ue_PyFVector2DType) < 0)
return;
Py_INCREF(&ue_PyFVector2DType);
PyModule_AddObject(ue_module, "FVector2D", (PyObject *)&ue_PyFVector2DType);
}
PyObject *py_ue_new_fvector2d(FVector2D vec)
{
ue_PyFVector2D *ret = (ue_PyFVector2D *)PyObject_New(ue_PyFVector2D, &ue_PyFVector2DType);
ret->vec = vec;
return (PyObject *)ret;
}
ue_PyFVector2D *py_ue_is_fvector2d(PyObject *obj)
{
if (!PyObject_IsInstance(obj, (PyObject *)&ue_PyFVector2DType))
return nullptr;
return (ue_PyFVector2D *)obj;
}
bool py_ue_vector2d_arg(PyObject *args, FVector2D &vec)
{
if (PyTuple_Size(args) == 1)
{
PyObject *arg = PyTuple_GetItem(args, 0);
ue_PyFVector2D *py_vec = py_ue_is_fvector2d(arg);
if (!py_vec)
{
PyErr_Format(PyExc_TypeError, "argument is not a FVector2D");
return false;
}
vec = py_vec->vec;
return true;
}
float x, y;
if (!PyArg_ParseTuple(args, "ff", &x, &y))
return false;
vec.X = x;
vec.Y = y;
return true;
}