1
// Copyright 2010 Jussi Lepisto
6
#include <Eigen/Geometry>
14
PyGetSetDef Matrix4f_getset[] = {
18
PyMethodDef Matrix4f_methods[] = {
19
{"set", (PyCFunction)Matrix4f_set, METH_VARARGS,
21
{"set_zero", (PyCFunction)Matrix4f_set_zero, METH_NOARGS,
23
{"set_ones", (PyCFunction)Matrix4f_set_ones, METH_NOARGS,
25
{"set_identity", (PyCFunction)Matrix4f_set_identity, METH_NOARGS,
27
{"set_constant", (PyCFunction)Matrix4f_set_constant, METH_VARARGS,
29
{"set_random", (PyCFunction)Matrix4f_set_random, METH_NOARGS,
32
{"get_block2", (PyCFunction)Matrix4f_get_block2, METH_VARARGS,
34
{"set_block2", (PyCFunction)Matrix4f_set_block2, METH_VARARGS,
36
{"get_block3", (PyCFunction)Matrix4f_get_block3, METH_VARARGS,
38
{"set_block3", (PyCFunction)Matrix4f_set_block3, METH_VARARGS,
40
{"get_col", (PyCFunction)Matrix4f_get_col, METH_VARARGS,
42
{"set_col", (PyCFunction)Matrix4f_set_col, METH_VARARGS,
44
{"get_row", (PyCFunction)Matrix4f_get_row, METH_VARARGS,
46
{"set_row", (PyCFunction)Matrix4f_set_row, METH_VARARGS,
52
PyNumberMethods Matrix4f_numbermethods = {
53
Matrix4f_add, // nb_add
54
Matrix4f_subtract, // nb_subtract
55
Matrix4f_multiply, // nb_multiply
56
Matrix4f_divide, // nb_divide
60
Matrix4f_negative, // nb_negative
76
Matrix4f_inplace_add, // nb_inplace_add
77
Matrix4f_inplace_subtract, // nb_inplace_subtract
78
Matrix4f_inplace_multiply, // nb_inplace_multiply
79
Matrix4f_inplace_divide, // nb_inplace_divide
82
PyMappingMethods Matrix4f_mappingmethods = {
83
(lenfunc)Matrix4f_length, // mp_length
84
(binaryfunc)Matrix4f_subscript, // mp_subscript
85
(objobjargproc)Matrix4f_ass_subscript, // mp_ass_subscript
88
PyTypeObject Matrix4fType = {
89
PyObject_HEAD_INIT(NULL)
92
"pyeigen.Matrix4f", // tp_name
93
sizeof(Matrix4f), // tp_basicsize
100
(reprfunc)Matrix4f_repr, // tp_repr
101
&Matrix4f_numbermethods, // tp_as_number
103
&Matrix4f_mappingmethods, // tp_as_mapping
110
Py_TPFLAGS_DEFAULT | // tp_flags
111
Py_TPFLAGS_BASETYPE |
112
Py_TPFLAGS_CHECKTYPES,
117
0, // tp_weaklistoffset
120
Matrix4f_methods, // tp_methods
122
Matrix4f_getset, // tp_getset
128
(initproc)Matrix4f_init, // tp_init
130
Matrix4f_new, // tp_new
143
int Matrix4f_Check(PyObject* p)
145
return p->ob_type == &Matrix4fType;
148
int Matrix4f_ParseKey(PyObject* key, Py_ssize_t& key1, Py_ssize_t& key2)
150
if(!PyArg_ParseTuple(key, "nn", &key1, &key2))
158
if(key1 >= 4 || key2 >= 4)
160
PyErr_SetString(PyExc_IndexError, "index out of range");
168
PyObject* Matrix4f_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
170
return type->tp_alloc(type, 0);
173
int Matrix4f_init(Matrix4f* self, PyObject* args, PyObject* kwds)
175
if(!Matrix4f_set(self, args))
184
PyObject* Matrix4f_set(Matrix4f* self, PyObject* args)
186
float m11 = 0.0f, m12 = 0.0f, m13 = 0.0f, m14 = 0.0f;
187
float m21 = 0.0f, m22 = 0.0f, m23 = 0.0f, m24 = 0.0f;
188
float m31 = 0.0f, m32 = 0.0f, m33 = 0.0f, m34 = 0.0f;
189
float m41 = 0.0f, m42 = 0.0f, m43 = 0.0f, m44 = 0.0f;
190
if(!PyArg_ParseTuple(args, "|ffffffffffffffff",
191
&m11, &m12, &m13, &m14,
192
&m21, &m22, &m23, &m24,
193
&m31, &m32, &m33, &m34,
194
&m41, &m42, &m43, &m44))
197
self->matrix << m11, m12, m13, m14,
204
PyObject* Matrix4f_set_zero(Matrix4f* self, PyObject* noargs)
206
self->matrix.setZero();
210
PyObject* Matrix4f_set_ones(Matrix4f* self, PyObject* noargs)
212
self->matrix.setOnes();
216
PyObject* Matrix4f_set_identity(Matrix4f* self, PyObject* noargs)
218
self->matrix.setIdentity();
222
PyObject* Matrix4f_set_constant(Matrix4f* self, PyObject* args)
224
float constant = 0.0f;
225
if(!PyArg_ParseTuple(args, "f", &constant))
228
self->matrix.setConstant(constant);
232
PyObject* Matrix4f_set_random(Matrix4f* self, PyObject* noargs)
234
self->matrix.setRandom();
238
PyObject* Matrix4f_get_block2(Matrix4f* self, PyObject* args)
240
Py_ssize_t i = 0, j = 0;
241
if(!PyArg_ParseTuple(args, "nn", &i, &j))
249
if(i < 0 || i > 2 || j < 0 || j > 2)
251
PyErr_SetString(PyExc_IndexError, "index out of range");
255
Matrix2f* result = PyObject_New(Matrix2f, &Matrix2fType);
257
result->matrix = self->matrix.block<2, 2>(i, j);
259
return (PyObject*)result;
262
PyObject* Matrix4f_set_block2(Matrix4f* self, PyObject* args)
264
Py_ssize_t i = 0, j = 0;
265
Matrix2f* value = NULL;
266
if(!PyArg_ParseTuple(args, "nnO!", &i, &j, &Matrix2fType, &value))
274
if(i < 0 || i > 2 || j < 0 || j > 2)
276
PyErr_SetString(PyExc_IndexError, "index out of range");
280
self->matrix.block<2, 2>(i, j) = value->matrix;
284
PyObject* Matrix4f_get_block3(Matrix4f* self, PyObject* args)
286
Py_ssize_t i = 0, j = 0;
287
if(!PyArg_ParseTuple(args, "nn", &i, &j))
295
if(i < 0 || i > 1 || j < 0 || j > 1)
297
PyErr_SetString(PyExc_IndexError, "index out of range");
301
Matrix3f* result = PyObject_New(Matrix3f, &Matrix3fType);
303
result->matrix = self->matrix.block<3, 3>(i, j);
305
return (PyObject*)result;
308
PyObject* Matrix4f_set_block3(Matrix4f* self, PyObject* args)
310
Py_ssize_t i = 0, j = 0;
311
Matrix3f* value = NULL;
312
if(!PyArg_ParseTuple(args, "nnO!", &i, &j, &Matrix3fType, &value))
320
if(i < 0 || i > 1 || j < 0 || j > 1)
322
PyErr_SetString(PyExc_IndexError, "index out of range");
326
self->matrix.block<3, 3>(i, j) = value->matrix;
330
PyObject* Matrix4f_get_col(Matrix4f* self, PyObject* args)
333
if(!PyArg_ParseTuple(args, "n", &index))
336
if(index < 0 || index >= 2)
338
PyErr_SetString(PyExc_IndexError, "index out of range");
342
Vector4f* result = PyObject_New(Vector4f, &Vector4fType);
344
result->vector = self->matrix.col(index);
346
return (PyObject*)result;
349
PyObject* Matrix4f_set_col(Matrix4f* self, PyObject* args)
352
Vector4f* value = NULL;
353
if(!PyArg_ParseTuple(args, "nO!", &index, &Vector4fType, &value))
356
if(index < 0 || index >= 2)
358
PyErr_SetString(PyExc_IndexError, "index out of range");
362
self->matrix.col(index) = value->vector;
366
PyObject* Matrix4f_get_row(Matrix4f* self, PyObject* args)
369
if(!PyArg_ParseTuple(args, "n", &index))
372
if(index < 0 || index >= 2)
374
PyErr_SetString(PyExc_IndexError, "index out of range");
378
Vector4f* result = PyObject_New(Vector4f, &Vector4fType);
380
result->vector = self->matrix.row(index);
382
return (PyObject*)result;
385
PyObject* Matrix4f_set_row(Matrix4f* self, PyObject* args)
388
Vector4f* value = NULL;
389
if(!PyArg_ParseTuple(args, "nO!", &index, &Vector4fType, &value))
392
if(index < 0 || index >= 2)
394
PyErr_SetString(PyExc_IndexError, "index out of range");
398
self->matrix.row(index) = value->vector;
403
PyObject* Matrix4f_add(PyObject* o1, PyObject* o2)
405
if(!Matrix4f_Check(o1) || !Matrix4f_Check(o2))
406
RETURN_NOTIMPLEMENTED;
408
Matrix4f* m1 = (Matrix4f*)o1;
409
Matrix4f* m2 = (Matrix4f*)o2;
410
Matrix4f* result = PyObject_New(Matrix4f, &Matrix4fType);
412
result->matrix = m1->matrix + m2->matrix;
414
return (PyObject*)result;
417
PyObject* Matrix4f_subtract(PyObject* o1, PyObject* o2)
419
if(!Matrix4f_Check(o1) || !Matrix4f_Check(o2))
420
RETURN_NOTIMPLEMENTED;
422
Matrix4f* m1 = (Matrix4f*)o1;
423
Matrix4f* m2 = (Matrix4f*)o2;
424
Matrix4f* result = PyObject_New(Matrix4f, &Matrix4fType);
426
result->matrix = m1->matrix - m2->matrix;
428
return (PyObject*)result;
431
PyObject* Matrix4f_multiply(PyObject* o1, PyObject* o2)
437
if(Matrix4f_Check(o1) && Matrix4f_Check(o2))
439
Matrix4f* m1 = (Matrix4f*)o1;
440
Matrix4f* m2 = (Matrix4f*)o2;
441
Matrix4f* result = PyObject_New(Matrix4f, &Matrix4fType);
443
result->matrix = m1->matrix * m2->matrix;
445
return (PyObject*)result;
448
else if(Matrix4f_Check(o1) && PyNumber_Check(o2))
451
scalar = (float)PyFloat_AsDouble(o2);
452
if(PyErr_Occurred() != NULL)
456
else if(PyNumber_Check(o1) && Matrix4f_Check(o2))
458
scalar = (float)PyFloat_AsDouble(o1);
460
if(PyErr_Occurred() != NULL)
464
RETURN_NOTIMPLEMENTED;
466
Matrix4f* result = PyObject_New(Matrix4f, &Matrix4fType);
468
result->matrix = m->matrix * scalar;
470
return (PyObject*)result;
473
PyObject* Matrix4f_divide(PyObject* o1, PyObject* o2)
475
if(!Matrix4f_Check(o1) || !PyNumber_Check(o2))
476
RETURN_NOTIMPLEMENTED;
478
Matrix4f* m = (Matrix4f*)o1;
479
float scalar = (float)PyFloat_AsDouble(o2);
480
if(PyErr_Occurred() != NULL)
483
Matrix4f* result = PyObject_New(Matrix4f, &Matrix4fType);
485
result->matrix = m->matrix / scalar;
487
return (PyObject*)result;
490
PyObject* Matrix4f_negative(PyObject* o)
492
if(!Matrix4f_Check(o))
493
RETURN_NOTIMPLEMENTED;
495
Matrix4f* m = (Matrix4f*)o;
496
Matrix4f* result = PyObject_New(Matrix4f, &Matrix4fType);
498
result->matrix = -m->matrix;
500
return (PyObject*)result;
503
PyObject* Matrix4f_inplace_add(PyObject* o1, PyObject* o2)
505
if(!Matrix4f_Check(o1) || !PyNumber_Check(o2))
506
RETURN_NOTIMPLEMENTED;
508
Matrix4f* m1 = (Matrix4f*)o1;
509
Matrix4f* m2 = (Matrix4f*)o2;
510
m1->matrix += m2->matrix;
515
PyObject* Matrix4f_inplace_subtract(PyObject* o1, PyObject* o2)
517
if(!Matrix4f_Check(o1) || !PyNumber_Check(o2))
518
RETURN_NOTIMPLEMENTED;
520
Matrix4f* m1 = (Matrix4f*)o1;
521
Matrix4f* m2 = (Matrix4f*)o2;
522
m1->matrix -= m2->matrix;
527
PyObject* Matrix4f_inplace_multiply(PyObject* o1, PyObject* o2)
529
if(!Matrix4f_Check(o1))
530
RETURN_NOTIMPLEMENTED;
532
Matrix4f* m1 = (Matrix4f*)o1;
537
if(Matrix4f_Check(o2))
540
m1->matrix *= m2->matrix;
543
else if(PyNumber_Check(o2))
545
scalar = (float)PyFloat_AsDouble(o2);
546
if(PyErr_Occurred() != NULL)
549
m1->matrix *= scalar;
552
RETURN_NOTIMPLEMENTED;
554
return (PyObject*)m1;
557
PyObject* Matrix4f_inplace_divide(PyObject* o1, PyObject* o2)
559
if(!Matrix4f_Check(o1) || !PyNumber_Check(o2))
560
RETURN_NOTIMPLEMENTED;
562
Matrix4f* m = (Matrix4f*)o1;
563
float scalar = (float)PyFloat_AsDouble(o2);
564
if(PyErr_Occurred() != NULL)
573
Py_ssize_t Matrix4f_length(Matrix4f* self)
578
PyObject* Matrix4f_subscript(Matrix4f* self, PyObject* key)
580
Py_ssize_t key1, key2;
581
if(!Matrix4f_ParseKey(key, key1, key2))
584
return PyFloat_FromDouble(self->matrix(key1, key2));
587
int Matrix4f_ass_subscript(Matrix4f* self, PyObject* key, PyObject* value)
589
Py_ssize_t key1, key2;
590
if(!Matrix4f_ParseKey(key, key1, key2))
593
self->matrix(key1, key2) = (float)PyFloat_AsDouble(value);
598
PyObject* Matrix4f_repr(Matrix4f* self)
601
_snprintf(buffer, 1024, "Matrix4f(%f, %f, %f, %f,\n %f, %f, %f, %f,\n %f, %f, %f, %f\n %f, %f, %f, %f)",
602
self->matrix(0, 0), self->matrix(0, 1), self->matrix(0, 2), self->matrix(0, 3),
603
self->matrix(1, 0), self->matrix(1, 1), self->matrix(1, 2), self->matrix(1, 3),
604
self->matrix(2, 0), self->matrix(2, 1), self->matrix(2, 2), self->matrix(2, 3),
605
self->matrix(3, 0), self->matrix(3, 1), self->matrix(3, 2), self->matrix(3, 3));
606
return PyString_FromString(buffer);