Cleanup: use doxy sections for mathutils types

Also minor improvements & corrections to comments.
This commit is contained in:
Campbell Barton 2022-05-25 12:33:15 +10:00
parent b0da080c2c
commit ae73bd3d9e
10 changed files with 2240 additions and 1629 deletions

View File

@ -22,8 +22,40 @@
#define COLOR_SIZE 3
/* ----------------------------------mathutils.Color() ------------------- */
/* makes a new color for you to play with */
/* -------------------------------------------------------------------- */
/** \name Utilities
* \{ */
/**
* \note #BaseMath_ReadCallback must be called beforehand.
*/
static PyObject *Color_to_tuple_ex(ColorObject *self, int ndigits)
{
PyObject *ret;
int i;
ret = PyTuple_New(COLOR_SIZE);
if (ndigits >= 0) {
for (i = 0; i < COLOR_SIZE; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->col[i], ndigits)));
}
}
else {
for (i = 0; i < COLOR_SIZE; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->col[i]));
}
}
return ret;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Color Type: `__new__` / `mathutils.Color()`
* \{ */
static PyObject *Color_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
float col[3] = {0.0f, 0.0f, 0.0f};
@ -54,29 +86,11 @@ static PyObject *Color_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return Color_CreatePyObject(col, type);
}
/* -----------------------------METHODS---------------------------- */
/** \} */
/* NOTE: BaseMath_ReadCallback must be called beforehand. */
static PyObject *Color_ToTupleExt(ColorObject *self, int ndigits)
{
PyObject *ret;
int i;
ret = PyTuple_New(COLOR_SIZE);
if (ndigits >= 0) {
for (i = 0; i < COLOR_SIZE; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->col[i], ndigits)));
}
}
else {
for (i = 0; i < COLOR_SIZE; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->col[i]));
}
}
return ret;
}
/* -------------------------------------------------------------------- */
/** \name Color Methods: Color Space Conversion
* \{ */
PyDoc_STRVAR(Color_from_scene_linear_to_srgb_doc,
".. function:: from_scene_linear_to_srgb()\n"
@ -190,7 +204,11 @@ static PyObject *Color_from_rec709_linear_to_scene_linear(ColorObject *self)
return Color_CreatePyObject(col, Py_TYPE(self));
}
/* ---------------------------- Colorspace conversion -------------- */
/** \} */
/* -------------------------------------------------------------------- */
/** \name Color Methods: Color Copy/Deep-Copy
* \{ */
PyDoc_STRVAR(Color_copy_doc,
".. function:: copy()\n"
@ -218,8 +236,11 @@ static PyObject *Color_deepcopy(ColorObject *self, PyObject *args)
return Color_copy(self);
}
/* ----------------------------print object (internal)-------------- */
/* print the object to screen */
/** \} */
/* -------------------------------------------------------------------- */
/** \name Color Type: `__repr__` & `__str__`
* \{ */
static PyObject *Color_repr(ColorObject *self)
{
@ -229,7 +250,7 @@ static PyObject *Color_repr(ColorObject *self)
return NULL;
}
tuple = Color_ToTupleExt(self, -1);
tuple = Color_to_tuple_ex(self, -1);
ret = PyUnicode_FromFormat("Color(%R)", tuple);
@ -255,8 +276,12 @@ static PyObject *Color_str(ColorObject *self)
}
#endif
/* ------------------------tp_richcmpr */
/* returns -1 exception, 0 false, 1 true */
/** \} */
/* -------------------------------------------------------------------- */
/** \name Color Type: Rich Compare
* \{ */
static PyObject *Color_richcmpr(PyObject *a, PyObject *b, int op)
{
PyObject *res;
@ -295,6 +320,12 @@ static PyObject *Color_richcmpr(PyObject *a, PyObject *b, int op)
return Py_INCREF_RET(res);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Color Type: Hash (`__hash__`)
* \{ */
static Py_hash_t Color_hash(ColorObject *self)
{
if (BaseMath_ReadCallback(self) == -1) {
@ -308,15 +339,19 @@ static Py_hash_t Color_hash(ColorObject *self)
return mathutils_array_hash(self->col, COLOR_SIZE);
}
/* ---------------------SEQUENCE PROTOCOLS------------------------ */
/* ----------------------------len(object)------------------------ */
/* sequence length */
/** \} */
/* -------------------------------------------------------------------- */
/** \name Color Type: Sequence & Mapping Protocols Implementation
* \{ */
/** Sequence length: `len(object)`. */
static int Color_len(ColorObject *UNUSED(self))
{
return COLOR_SIZE;
}
/* ----------------------------object[]--------------------------- */
/* sequence accessor (get) */
/** Sequence accessor (get): `x = object[i]`. */
static PyObject *Color_item(ColorObject *self, int i)
{
if (i < 0) {
@ -336,8 +371,8 @@ static PyObject *Color_item(ColorObject *self, int i)
return PyFloat_FromDouble(self->col[i]);
}
/* ----------------------------object[]------------------------- */
/* sequence accessor (set) */
/** Sequence accessor (set): `object[i] = x`. */
static int Color_ass_item(ColorObject *self, int i, PyObject *value)
{
float f;
@ -373,8 +408,8 @@ static int Color_ass_item(ColorObject *self, int i, PyObject *value)
return 0;
}
/* ----------------------------object[z:y]------------------------ */
/* sequence slice (get) */
/** Sequence slice accessor (get): `x = object[i:j]`. */
static PyObject *Color_slice(ColorObject *self, int begin, int end)
{
PyObject *tuple;
@ -398,8 +433,8 @@ static PyObject *Color_slice(ColorObject *self, int begin, int end)
return tuple;
}
/* ----------------------------object[z:y]------------------------ */
/* sequence slice (set) */
/** Sequence slice accessor (set): `object[i:j] = x`. */
static int Color_ass_slice(ColorObject *self, int begin, int end, PyObject *seq)
{
int i, size;
@ -436,6 +471,7 @@ static int Color_ass_slice(ColorObject *self, int begin, int end, PyObject *seq)
return 0;
}
/** Sequence generic subscript (get): `x = object[...]`. */
static PyObject *Color_subscript(ColorObject *self, PyObject *item)
{
if (PyIndex_Check(item)) {
@ -472,6 +508,7 @@ static PyObject *Color_subscript(ColorObject *self, PyObject *item)
return NULL;
}
/** Sequence generic subscript (set): `object[...] = x`. */
static int Color_ass_subscript(ColorObject *self, PyObject *item, PyObject *value)
{
if (PyIndex_Check(item)) {
@ -504,29 +541,13 @@ static int Color_ass_subscript(ColorObject *self, PyObject *item, PyObject *valu
return -1;
}
/* -----------------PROTCOL DECLARATIONS-------------------------- */
static PySequenceMethods Color_SeqMethods = {
(lenfunc)Color_len, /* sq_length */
(binaryfunc)NULL, /* sq_concat */
(ssizeargfunc)NULL, /* sq_repeat */
(ssizeargfunc)Color_item, /* sq_item */
NULL, /* sq_slice, deprecated */
(ssizeobjargproc)Color_ass_item, /* sq_ass_item */
NULL, /* sq_ass_slice, deprecated */
(objobjproc)NULL, /* sq_contains */
(binaryfunc)NULL, /* sq_inplace_concat */
(ssizeargfunc)NULL, /* sq_inplace_repeat */
};
/** \} */
static PyMappingMethods Color_AsMapping = {
(lenfunc)Color_len,
(binaryfunc)Color_subscript,
(objobjargproc)Color_ass_subscript,
};
/* -------------------------------------------------------------------- */
/** \name Color Type: Numeric Protocol Implementation
* \{ */
/* numeric */
/* addition: obj + obj */
/** Addition: `object + object`. */
static PyObject *Color_add(PyObject *v1, PyObject *v2)
{
ColorObject *color1 = NULL, *color2 = NULL;
@ -552,7 +573,7 @@ static PyObject *Color_add(PyObject *v1, PyObject *v2)
return Color_CreatePyObject(col, Py_TYPE(v1));
}
/* addition in-place: obj += obj */
/** Addition in-place: `object += object`. */
static PyObject *Color_iadd(PyObject *v1, PyObject *v2)
{
ColorObject *color1 = NULL, *color2 = NULL;
@ -579,7 +600,7 @@ static PyObject *Color_iadd(PyObject *v1, PyObject *v2)
return v1;
}
/* subtraction: obj - obj */
/** Subtraction: `object - object`. */
static PyObject *Color_sub(PyObject *v1, PyObject *v2)
{
ColorObject *color1 = NULL, *color2 = NULL;
@ -605,7 +626,7 @@ static PyObject *Color_sub(PyObject *v1, PyObject *v2)
return Color_CreatePyObject(col, Py_TYPE(v1));
}
/* subtraction in-place: obj -= obj */
/** Subtraction in-place: `object -= object`. */
static PyObject *Color_isub(PyObject *v1, PyObject *v2)
{
ColorObject *color1 = NULL, *color2 = NULL;
@ -639,6 +660,7 @@ static PyObject *color_mul_float(ColorObject *color, const float scalar)
return Color_CreatePyObject(tcol, Py_TYPE(color));
}
/** Multiplication: `object * object`. */
static PyObject *Color_mul(PyObject *v1, PyObject *v2)
{
ColorObject *color1 = NULL, *color2 = NULL;
@ -683,6 +705,7 @@ static PyObject *Color_mul(PyObject *v1, PyObject *v2)
return NULL;
}
/** Division: `object / object`. */
static PyObject *Color_div(PyObject *v1, PyObject *v2)
{
ColorObject *color1 = NULL;
@ -716,7 +739,7 @@ static PyObject *Color_div(PyObject *v1, PyObject *v2)
return NULL;
}
/* multiplication in-place: obj *= obj */
/** Multiplication in-place: `object *= object`. */
static PyObject *Color_imul(PyObject *v1, PyObject *v2)
{
ColorObject *color = (ColorObject *)v1;
@ -744,7 +767,7 @@ static PyObject *Color_imul(PyObject *v1, PyObject *v2)
return v1;
}
/* multiplication in-place: obj *= obj */
/** Division in-place: `object *= object`. */
static PyObject *Color_idiv(PyObject *v1, PyObject *v2)
{
ColorObject *color = (ColorObject *)v1;
@ -777,8 +800,7 @@ static PyObject *Color_idiv(PyObject *v1, PyObject *v2)
return v1;
}
/* -obj
* returns the negative of this object */
/** Negative (returns the negative of this object): `-object`. */
static PyObject *Color_neg(ColorObject *self)
{
float tcol[COLOR_SIZE];
@ -791,6 +813,31 @@ static PyObject *Color_neg(ColorObject *self)
return Color_CreatePyObject(tcol, Py_TYPE(self));
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Color Type: Protocol Declarations
* \{ */
static PySequenceMethods Color_SeqMethods = {
(lenfunc)Color_len, /*sq_length*/
(binaryfunc)NULL, /*sq_concat*/
(ssizeargfunc)NULL, /*sq_repeat*/
(ssizeargfunc)Color_item, /*sq_item*/
NULL, /*sq_slice(DEPRECATED)*/
(ssizeobjargproc)Color_ass_item, /*sq_ass_item*/
NULL, /*sq_ass_slice(DEPRECATED)*/
(objobjproc)NULL, /*sq_contains*/
(binaryfunc)NULL, /*sq_inplace_concat*/
(ssizeargfunc)NULL, /*sq_inplace_repeat*/
};
static PyMappingMethods Color_AsMapping = {
(lenfunc)Color_len,
(binaryfunc)Color_subscript,
(objobjargproc)Color_ass_subscript,
};
static PyNumberMethods Color_NumMethods = {
(binaryfunc)Color_add, /*nb_add*/
(binaryfunc)Color_sub, /*nb_subtract*/
@ -811,24 +858,31 @@ static PyNumberMethods Color_NumMethods = {
NULL, /*nb_int*/
NULL, /*nb_reserved*/
NULL, /*nb_float*/
Color_iadd, /* nb_inplace_add */
Color_isub, /* nb_inplace_subtract */
Color_imul, /* nb_inplace_multiply */
NULL, /* nb_inplace_remainder */
NULL, /* nb_inplace_power */
NULL, /* nb_inplace_lshift */
NULL, /* nb_inplace_rshift */
NULL, /* nb_inplace_and */
NULL, /* nb_inplace_xor */
NULL, /* nb_inplace_or */
NULL, /* nb_floor_divide */
Color_div, /* nb_true_divide */
NULL, /* nb_inplace_floor_divide */
Color_idiv, /* nb_inplace_true_divide */
NULL, /* nb_index */
Color_iadd, /*nb_inplace_add*/
Color_isub, /*nb_inplace_subtract*/
Color_imul, /*nb_inplace_multiply*/
NULL, /*nb_inplace_remainder*/
NULL, /*nb_inplace_power*/
NULL, /*nb_inplace_lshift*/
NULL, /*nb_inplace_rshift*/
NULL, /*nb_inplace_and*/
NULL, /*nb_inplace_xor*/
NULL, /*nb_inplace_or*/
NULL, /*nb_floor_divide*/
Color_div, /*nb_true_divide*/
NULL, /*nb_inplace_floor_divide*/
Color_idiv, /*nb_inplace_true_divide*/
NULL, /*nb_index*/
};
/* color channel, vector.r/g/b */
/** \} */
/* -------------------------------------------------------------------- */
/** \name Color Type: Get/Set Item Implementation
* \{ */
/* Color channel (RGB): `color.r/g/b`. */
PyDoc_STRVAR(Color_channel_r_doc, "Red color channel.\n\n:type: float");
PyDoc_STRVAR(Color_channel_g_doc, "Green color channel.\n\n:type: float");
PyDoc_STRVAR(Color_channel_b_doc, "Blue color channel.\n\n:type: float");
@ -843,7 +897,8 @@ static int Color_channel_set(ColorObject *self, PyObject *value, void *type)
return Color_ass_item(self, POINTER_AS_INT(type), value);
}
/* color channel (HSV), color.h/s/v */
/* Color channel (HSV): `color.h/s/v`. */
PyDoc_STRVAR(Color_channel_hsv_h_doc, "HSV Hue component in [0, 1].\n\n:type: float");
PyDoc_STRVAR(Color_channel_hsv_s_doc, "HSV Saturation component in [0, 1].\n\n:type: float");
PyDoc_STRVAR(Color_channel_hsv_v_doc, "HSV Value component in [0, 1].\n\n:type: float");
@ -891,8 +946,8 @@ static int Color_channel_hsv_set(ColorObject *self, PyObject *value, void *type)
return 0;
}
/* color channel (HSV), color.h/s/v */
PyDoc_STRVAR(Color_hsv_doc, "HSV Values in [0, 1].\n\n:type: float triplet");
/** Color channel HSV (get): `x = color.hsv`. */
static PyObject *Color_hsv_get(ColorObject *self, void *UNUSED(closure))
{
float hsv[3];
@ -910,6 +965,7 @@ static PyObject *Color_hsv_get(ColorObject *self, void *UNUSED(closure))
return ret;
}
/** Color channel HSV (set): `color.hsv = x`. */
static int Color_hsv_set(ColorObject *self, PyObject *value, void *UNUSED(closure))
{
float hsv[3];
@ -932,9 +988,12 @@ static int Color_hsv_set(ColorObject *self, PyObject *value, void *UNUSED(closur
return 0;
}
/*****************************************************************************/
/* Python attributes get/set structure: */
/*****************************************************************************/
/** \} */
/* -------------------------------------------------------------------- */
/** \name Color Type: Get/Set Item Definitions
* \{ */
static PyGetSetDef Color_getseters[] = {
{"r", (getter)Color_channel_get, (setter)Color_channel_set, Color_channel_r_doc, (void *)0},
{"g", (getter)Color_channel_get, (setter)Color_channel_set, Color_channel_g_doc, (void *)1},
@ -977,7 +1036,12 @@ static PyGetSetDef Color_getseters[] = {
{NULL, NULL, NULL, NULL, NULL} /* Sentinel */
};
/* -----------------------METHOD DEFINITIONS ---------------------- */
/** \} */
/* -------------------------------------------------------------------- */
/** \name Color Type: Method Definitions
* \{ */
static struct PyMethodDef Color_methods[] = {
{"copy", (PyCFunction)Color_copy, METH_NOARGS, Color_copy_doc},
{"__copy__", (PyCFunction)Color_copy, METH_NOARGS, Color_copy_doc},
@ -1022,7 +1086,12 @@ static struct PyMethodDef Color_methods[] = {
{NULL, NULL, 0, NULL},
};
/* ------------------PY_OBECT DEFINITION-------------------------- */
/** \} */
/* -------------------------------------------------------------------- */
/** \name Color Type: Python Object Definition
* \{ */
PyDoc_STRVAR(
color_doc,
".. class:: Color(rgb)\n"
@ -1087,6 +1156,12 @@ PyTypeObject color_Type = {
NULL, /* tp_del */
};
/** \} */
/* -------------------------------------------------------------------- */
/** \name Color Type: C/API Constructors
* \{ */
PyObject *Color_CreatePyObject(const float col[3], PyTypeObject *base_type)
{
ColorObject *self;
@ -1156,3 +1231,5 @@ PyObject *Color_CreatePyObject_cb(PyObject *cb_user, uchar cb_type, uchar cb_sub
return (PyObject *)self;
}
/** \} */

View File

@ -19,7 +19,8 @@ typedef struct {
* be stored in py_data) or be a wrapper for data allocated through
* Blender (stored in blend_data). This is an either/or struct not both. */
/* prototypes */
/* Prototypes. */
PyObject *Color_CreatePyObject(const float col[3],
PyTypeObject *base_type) ATTR_WARN_UNUSED_RESULT;
PyObject *Color_CreatePyObject_wrap(float col[3], PyTypeObject *base_type) ATTR_WARN_UNUSED_RESULT

View File

@ -19,45 +19,11 @@
#define EULER_SIZE 3
/* ----------------------------------mathutils.Euler() ------------------- */
/* makes a new euler for you to play with */
static PyObject *Euler_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *seq = NULL;
const char *order_str = NULL;
/* -------------------------------------------------------------------- */
/** \name Utilities
* \{ */
float eul[EULER_SIZE] = {0.0f, 0.0f, 0.0f};
short order = EULER_ORDER_XYZ;
if (kwds && PyDict_Size(kwds)) {
PyErr_SetString(PyExc_TypeError,
"mathutils.Euler(): "
"takes no keyword args");
return NULL;
}
if (!PyArg_ParseTuple(args, "|Os:mathutils.Euler", &seq, &order_str)) {
return NULL;
}
switch (PyTuple_GET_SIZE(args)) {
case 0:
break;
case 2:
if ((order = euler_order_from_string(order_str, "mathutils.Euler()")) == -1) {
return NULL;
}
ATTR_FALLTHROUGH;
case 1:
if (mathutils_array_parse(eul, EULER_SIZE, EULER_SIZE, seq, "mathutils.Euler()") == -1) {
return NULL;
}
break;
}
return Euler_CreatePyObject(eul, order, type);
}
/* internal use, assume read callback is done */
/** Internal use, assume read callback is done. */
static const char *euler_order_str(EulerObject *self)
{
static const char order[][4] = {"XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"};
@ -96,8 +62,10 @@ short euler_order_from_string(const char *str, const char *error_prefix)
return -1;
}
/* NOTE: BaseMath_ReadCallback must be called beforehand. */
static PyObject *Euler_ToTupleExt(EulerObject *self, int ndigits)
/**
* \note #BaseMath_ReadCallback must be called beforehand.
*/
static PyObject *Euler_to_tuple_ex(EulerObject *self, int ndigits)
{
PyObject *ret;
int i;
@ -118,8 +86,53 @@ static PyObject *Euler_ToTupleExt(EulerObject *self, int ndigits)
return ret;
}
/* -----------------------------METHODS----------------------------
* return a quaternion representation of the euler */
/** \} */
/* -------------------------------------------------------------------- */
/** \name Euler Type: `__new__` / `mathutils.Euler()`
* \{ */
static PyObject *Euler_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *seq = NULL;
const char *order_str = NULL;
float eul[EULER_SIZE] = {0.0f, 0.0f, 0.0f};
short order = EULER_ORDER_XYZ;
if (kwds && PyDict_Size(kwds)) {
PyErr_SetString(PyExc_TypeError,
"mathutils.Euler(): "
"takes no keyword args");
return NULL;
}
if (!PyArg_ParseTuple(args, "|Os:mathutils.Euler", &seq, &order_str)) {
return NULL;
}
switch (PyTuple_GET_SIZE(args)) {
case 0:
break;
case 2:
if ((order = euler_order_from_string(order_str, "mathutils.Euler()")) == -1) {
return NULL;
}
ATTR_FALLTHROUGH;
case 1:
if (mathutils_array_parse(eul, EULER_SIZE, EULER_SIZE, seq, "mathutils.Euler()") == -1) {
return NULL;
}
break;
}
return Euler_CreatePyObject(eul, order, type);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Euler Methods
* \{ */
PyDoc_STRVAR(Euler_to_quaternion_doc,
".. method:: to_quaternion()\n"
@ -141,7 +154,6 @@ static PyObject *Euler_to_quaternion(EulerObject *self)
return Quaternion_CreatePyObject(quat, NULL);
}
/* return a matrix representation of the euler */
PyDoc_STRVAR(Euler_to_matrix_doc,
".. method:: to_matrix()\n"
"\n"
@ -279,9 +291,6 @@ static PyObject *Euler_make_compatible(EulerObject *self, PyObject *value)
Py_RETURN_NONE;
}
/* ----------------------------Euler.rotate()-----------------------
* return a copy of the euler */
PyDoc_STRVAR(Euler_copy_doc,
".. function:: copy()\n"
"\n"
@ -308,8 +317,11 @@ static PyObject *Euler_deepcopy(EulerObject *self, PyObject *args)
return Euler_copy(self);
}
/* ----------------------------print object (internal)--------------
* print the object to screen */
/** \} */
/* -------------------------------------------------------------------- */
/** \name Euler Type: `__repr__` & `__str__`
* \{ */
static PyObject *Euler_repr(EulerObject *self)
{
@ -319,7 +331,7 @@ static PyObject *Euler_repr(EulerObject *self)
return NULL;
}
tuple = Euler_ToTupleExt(self, -1);
tuple = Euler_to_tuple_ex(self, -1);
ret = PyUnicode_FromFormat("Euler(%R, '%s')", tuple, euler_order_str(self));
@ -349,6 +361,12 @@ static PyObject *Euler_str(EulerObject *self)
}
#endif
/** \} */
/* -------------------------------------------------------------------- */
/** \name Euler Type: Rich Compare
* \{ */
static PyObject *Euler_richcmpr(PyObject *a, PyObject *b, int op)
{
PyObject *res;
@ -390,6 +408,12 @@ static PyObject *Euler_richcmpr(PyObject *a, PyObject *b, int op)
return Py_INCREF_RET(res);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Euler Type: Hash (`__hash__`)
* \{ */
static Py_hash_t Euler_hash(EulerObject *self)
{
if (BaseMath_ReadCallback(self) == -1) {
@ -403,15 +427,19 @@ static Py_hash_t Euler_hash(EulerObject *self)
return mathutils_array_hash(self->eul, EULER_SIZE);
}
/* ---------------------SEQUENCE PROTOCOLS------------------------ */
/* ----------------------------len(object)------------------------ */
/* sequence length */
/** \} */
/* -------------------------------------------------------------------- */
/** \name Euler Type: Sequence Protocol
* \{ */
/** Sequence length: `len(object)`. */
static int Euler_len(EulerObject *UNUSED(self))
{
return EULER_SIZE;
}
/* ----------------------------object[]--------------------------- */
/* sequence accessor (get) */
/** Sequence accessor (get): `x = object[i]`. */
static PyObject *Euler_item(EulerObject *self, int i)
{
if (i < 0) {
@ -431,8 +459,8 @@ static PyObject *Euler_item(EulerObject *self, int i)
return PyFloat_FromDouble(self->eul[i]);
}
/* ----------------------------object[]------------------------- */
/* sequence accessor (set) */
/** Sequence accessor (set): `object[i] = x`. */
static int Euler_ass_item(EulerObject *self, int i, PyObject *value)
{
float f;
@ -468,8 +496,8 @@ static int Euler_ass_item(EulerObject *self, int i, PyObject *value)
return 0;
}
/* ----------------------------object[z:y]------------------------ */
/* sequence slice (get) */
/** Sequence slice accessor (get): `x = object[i:j]`. */
static PyObject *Euler_slice(EulerObject *self, int begin, int end)
{
PyObject *tuple;
@ -493,8 +521,8 @@ static PyObject *Euler_slice(EulerObject *self, int begin, int end)
return tuple;
}
/* ----------------------------object[z:y]------------------------ */
/* sequence slice (set) */
/** Sequence slice accessor (set): `object[i:j] = x`. */
static int Euler_ass_slice(EulerObject *self, int begin, int end, PyObject *seq)
{
int i, size;
@ -531,6 +559,7 @@ static int Euler_ass_slice(EulerObject *self, int begin, int end, PyObject *seq)
return 0;
}
/** Sequence generic subscript (get): `x = object[...]`. */
static PyObject *Euler_subscript(EulerObject *self, PyObject *item)
{
if (PyIndex_Check(item)) {
@ -567,6 +596,7 @@ static PyObject *Euler_subscript(EulerObject *self, PyObject *item)
return NULL;
}
/** Sequence generic subscript (set): `object[...] = x`. */
static int Euler_ass_subscript(EulerObject *self, PyObject *item, PyObject *value)
{
if (PyIndex_Check(item)) {
@ -599,18 +629,23 @@ static int Euler_ass_subscript(EulerObject *self, PyObject *item, PyObject *valu
return -1;
}
/* -----------------PROTCOL DECLARATIONS-------------------------- */
/** \} */
/* -------------------------------------------------------------------- */
/** \name Euler Type: Sequence & Mapping Protocol Declarations
* \{ */
static PySequenceMethods Euler_SeqMethods = {
(lenfunc)Euler_len, /* sq_length */
(binaryfunc)NULL, /* sq_concat */
(ssizeargfunc)NULL, /* sq_repeat */
(ssizeargfunc)Euler_item, /* sq_item */
(ssizessizeargfunc)NULL, /* sq_slice (deprecated) */
(ssizeobjargproc)Euler_ass_item, /* sq_ass_item */
(ssizessizeobjargproc)NULL, /* sq_ass_slice (deprecated) */
(objobjproc)NULL, /* sq_contains */
(binaryfunc)NULL, /* sq_inplace_concat */
(ssizeargfunc)NULL, /* sq_inplace_repeat */
(lenfunc)Euler_len, /*sq_length*/
(binaryfunc)NULL, /*sq_concat*/
(ssizeargfunc)NULL, /*sq_repeat*/
(ssizeargfunc)Euler_item, /*sq_item*/
(ssizessizeargfunc)NULL, /*sq_slice(DEPRECATED)*/
(ssizeobjargproc)Euler_ass_item, /*sq_ass_item*/
(ssizessizeobjargproc)NULL, /*sq_ass_slice(DEPRECATED)*/
(objobjproc)NULL, /*sq_contains*/
(binaryfunc)NULL, /*sq_inplace_concat*/
(ssizeargfunc)NULL, /*sq_inplace_repeat*/
};
static PyMappingMethods Euler_AsMapping = {
@ -619,7 +654,13 @@ static PyMappingMethods Euler_AsMapping = {
(objobjargproc)Euler_ass_subscript,
};
/* euler axis, euler.x/y/z */
/** \} */
/* -------------------------------------------------------------------- */
/** \name Euler Type: Get/Set Item Implementation
* \{ */
/* Euler axis: `euler.x/y/z`. */
PyDoc_STRVAR(Euler_axis_doc, "Euler axis angle in radians.\n\n:type: float");
static PyObject *Euler_axis_get(EulerObject *self, void *type)
@ -632,7 +673,7 @@ static int Euler_axis_set(EulerObject *self, PyObject *value, void *type)
return Euler_ass_item(self, POINTER_AS_INT(type), value);
}
/* rotation order */
/* Euler rotation order: `euler.order`. */
PyDoc_STRVAR(
Euler_order_doc,
@ -666,9 +707,12 @@ static int Euler_order_set(EulerObject *self, PyObject *value, void *UNUSED(clos
return 0;
}
/*****************************************************************************/
/* Python attributes get/set structure: */
/*****************************************************************************/
/** \} */
/* -------------------------------------------------------------------- */
/** \name Euler Type: Get/Set Item Definitions
* \{ */
static PyGetSetDef Euler_getseters[] = {
{"x", (getter)Euler_axis_get, (setter)Euler_axis_set, Euler_axis_doc, (void *)0},
{"y", (getter)Euler_axis_get, (setter)Euler_axis_set, Euler_axis_doc, (void *)1},
@ -694,7 +738,12 @@ static PyGetSetDef Euler_getseters[] = {
{NULL, NULL, NULL, NULL, NULL} /* Sentinel */
};
/* -----------------------METHOD DEFINITIONS ---------------------- */
/** \} */
/* -------------------------------------------------------------------- */
/** \name Euler Type: Method Definitions
* \{ */
static struct PyMethodDef Euler_methods[] = {
{"zero", (PyCFunction)Euler_zero, METH_NOARGS, Euler_zero_doc},
{"to_matrix", (PyCFunction)Euler_to_matrix, METH_NOARGS, Euler_to_matrix_doc},
@ -711,7 +760,12 @@ static struct PyMethodDef Euler_methods[] = {
{NULL, NULL, 0, NULL},
};
/* ------------------PY_OBECT DEFINITION-------------------------- */
/** \} */
/* -------------------------------------------------------------------- */
/** \name Euler Type: Python Object Definition
* \{ */
PyDoc_STRVAR(
euler_doc,
".. class:: Euler(angles, order='XYZ')\n"
@ -776,6 +830,12 @@ PyTypeObject euler_Type = {
NULL, /* tp_del */
};
/** \} */
/* -------------------------------------------------------------------- */
/** \name Euler Type: C/API Constructors
* \{ */
PyObject *Euler_CreatePyObject(const float eul[3], const short order, PyTypeObject *base_type)
{
EulerObject *self;
@ -849,3 +909,5 @@ PyObject *Euler_CreatePyObject_cb(PyObject *cb_user,
return (PyObject *)self;
}
/** \} */

View File

@ -22,6 +22,7 @@ typedef struct {
* blender (stored in blend_data). This is an either/or struct not both */
/* prototypes */
PyObject *Euler_CreatePyObject(const float eul[3],
short order,
PyTypeObject *base_type) ATTR_WARN_UNUSED_RESULT;

File diff suppressed because it is too large Load Diff

View File

@ -45,7 +45,8 @@ typedef struct {
* be stored in py_data) or be a wrapper for data allocated through
* blender (stored in blend_data). This is an either/or struct not both */
/* prototypes */
/* Prototypes. */
PyObject *Matrix_CreatePyObject(const float *mat,
ushort col_num,
ushort row_num,
@ -70,6 +71,7 @@ PyObject *Matrix_CreatePyObject_alloc(float *mat,
PyTypeObject *base_type) ATTR_WARN_UNUSED_RESULT;
/* PyArg_ParseTuple's "O&" formatting helpers. */
int Matrix_ParseAny(PyObject *o, void *p);
int Matrix_Parse2x2(PyObject *o, void *p);
int Matrix_Parse3x3(PyObject *o, void *p);

View File

@ -26,9 +26,49 @@ static void quat__axis_angle_sanitize(float axis[3], float *angle);
static PyObject *Quaternion_copy(QuaternionObject *self);
static PyObject *Quaternion_deepcopy(QuaternionObject *self, PyObject *args);
/* -----------------------------METHODS------------------------------ */
/* -------------------------------------------------------------------- */
/** \name Utilities
* \{ */
/* NOTE: BaseMath_ReadCallback must be called beforehand. */
static PyObject *quat__apply_to_copy(PyObject *(*quat_func)(QuaternionObject *),
QuaternionObject *self)
{
PyObject *ret = Quaternion_copy(self);
PyObject *ret_dummy = quat_func((QuaternionObject *)ret);
if (ret_dummy) {
Py_DECREF(ret_dummy);
return ret;
}
/* error */
Py_DECREF(ret);
return NULL;
}
/** Axis vector suffers from precision errors, use this function to ensure. */
static void quat__axis_angle_sanitize(float axis[3], float *angle)
{
if (axis) {
if (is_zero_v3(axis) || !isfinite(axis[0]) || !isfinite(axis[1]) || !isfinite(axis[2])) {
axis[0] = 1.0f;
axis[1] = 0.0f;
axis[2] = 0.0f;
}
else if (EXPP_FloatsAreEqual(axis[0], 0.0f, 10) && EXPP_FloatsAreEqual(axis[1], 0.0f, 10) &&
EXPP_FloatsAreEqual(axis[2], 0.0f, 10)) {
axis[0] = 1.0f;
}
}
if (angle) {
if (!isfinite(*angle)) {
*angle = 0.0f;
}
}
}
/**
* \note #BaseMath_ReadCallback must be called beforehand.
*/
static PyObject *Quaternion_to_tuple_ext(QuaternionObject *self, int ndigits)
{
PyObject *ret;
@ -50,6 +90,72 @@ static PyObject *Quaternion_to_tuple_ext(QuaternionObject *self, int ndigits)
return ret;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Type: `__new__` / `mathutils.Quaternion()`
* \{ */
static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *seq = NULL;
double angle = 0.0f;
float quat[QUAT_SIZE];
unit_qt(quat);
if (kwds && PyDict_Size(kwds)) {
PyErr_SetString(PyExc_TypeError,
"mathutils.Quaternion(): "
"takes no keyword args");
return NULL;
}
if (!PyArg_ParseTuple(args, "|Od:mathutils.Quaternion", &seq, &angle)) {
return NULL;
}
switch (PyTuple_GET_SIZE(args)) {
case 0:
break;
case 1: {
int size;
if ((size = mathutils_array_parse(quat, 3, QUAT_SIZE, seq, "mathutils.Quaternion()")) ==
-1) {
return NULL;
}
if (size == 4) {
/* 4d: Quaternion (common case) */
}
else {
/* 3d: Interpret as exponential map */
BLI_assert(size == 3);
expmap_to_quat(quat, quat);
}
break;
}
case 2: {
float axis[3];
if (mathutils_array_parse(axis, 3, 3, seq, "mathutils.Quaternion()") == -1) {
return NULL;
}
angle = angle_wrap_rad(angle); /* clamp because of precision issues */
axis_angle_to_quat(quat, axis, angle);
break;
/* PyArg_ParseTuple assures no more than 2 */
}
}
return Quaternion_CreatePyObject(quat, type);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Methods: To Euler
* \{ */
PyDoc_STRVAR(Quaternion_to_euler_doc,
".. method:: to_euler(order, euler_compat)\n"
"\n"
@ -114,6 +220,12 @@ static PyObject *Quaternion_to_euler(QuaternionObject *self, PyObject *args)
return Euler_CreatePyObject(eul, order, NULL);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Methods: To Matrix
* \{ */
PyDoc_STRVAR(Quaternion_to_matrix_doc,
".. method:: to_matrix()\n"
"\n"
@ -133,6 +245,12 @@ static PyObject *Quaternion_to_matrix(QuaternionObject *self)
return Matrix_CreatePyObject(mat, 3, 3, NULL);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Methods: To Axis/Angle
* \{ */
PyDoc_STRVAR(Quaternion_to_axis_angle_doc,
".. method:: to_axis_angle()\n"
"\n"
@ -163,6 +281,12 @@ static PyObject *Quaternion_to_axis_angle(QuaternionObject *self)
return ret;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Methods: To Swing/Twist
* \{ */
PyDoc_STRVAR(Quaternion_to_swing_twist_doc,
".. method:: to_swing_twist(axis)\n"
"\n"
@ -207,6 +331,12 @@ static PyObject *Quaternion_to_swing_twist(QuaternionObject *self, PyObject *axi
return ret;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Methods: To Exponential Map
* \{ */
PyDoc_STRVAR(
Quaternion_to_exponential_map_doc,
".. method:: to_exponential_map()\n"
@ -232,6 +362,12 @@ static PyObject *Quaternion_to_exponential_map(QuaternionObject *self)
return Vector_CreatePyObject(expmap, 3, NULL);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Methods: Cross Product
* \{ */
PyDoc_STRVAR(Quaternion_cross_doc,
".. method:: cross(other)\n"
"\n"
@ -259,6 +395,12 @@ static PyObject *Quaternion_cross(QuaternionObject *self, PyObject *value)
return Quaternion_CreatePyObject(quat, Py_TYPE(self));
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Methods: Dot Product
* \{ */
PyDoc_STRVAR(Quaternion_dot_doc,
".. method:: dot(other)\n"
"\n"
@ -285,6 +427,12 @@ static PyObject *Quaternion_dot(QuaternionObject *self, PyObject *value)
return PyFloat_FromDouble(dot_qtqt(self->quat, tquat));
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Methods: Rotation Difference
* \{ */
PyDoc_STRVAR(Quaternion_rotation_difference_doc,
".. function:: rotation_difference(other)\n"
"\n"
@ -315,6 +463,12 @@ static PyObject *Quaternion_rotation_difference(QuaternionObject *self, PyObject
return Quaternion_CreatePyObject(quat, Py_TYPE(self));
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Methods: Spherical Interpolation (slerp)
* \{ */
PyDoc_STRVAR(Quaternion_slerp_doc,
".. function:: slerp(other, factor)\n"
"\n"
@ -360,6 +514,12 @@ static PyObject *Quaternion_slerp(QuaternionObject *self, PyObject *args)
return Quaternion_CreatePyObject(quat, Py_TYPE(self));
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Methods: Rotate
* \{ */
PyDoc_STRVAR(Quaternion_rotate_doc,
".. method:: rotate(other)\n"
"\n"
@ -423,9 +583,15 @@ static PyObject *Quaternion_make_compatible(QuaternionObject *self, PyObject *va
Py_RETURN_NONE;
}
/* ----------------------------Quaternion.normalize()---------------- */
/* Normalize the quaternion. This may change the angle as well as the
* rotation axis, as all of (w, x, y, z) are scaled. */
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Methods: Normalize
*
* Normalize the quaternion. This may change the angle as well as the
* rotation axis, as all of (w, x, y, z) are scaled.
* \{ */
PyDoc_STRVAR(Quaternion_normalize_doc,
".. function:: normalize()\n"
"\n"
@ -453,6 +619,15 @@ static PyObject *Quaternion_normalized(QuaternionObject *self)
return quat__apply_to_copy(Quaternion_normalize, self);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Methods: Invert
*
* Normalize the quaternion. This may change the angle as well as the
* rotation axis, as all of (w, x, y, z) are scaled.
* \{ */
PyDoc_STRVAR(Quaternion_invert_doc,
".. function:: invert()\n"
"\n"
@ -480,6 +655,12 @@ static PyObject *Quaternion_inverted(QuaternionObject *self)
return quat__apply_to_copy(Quaternion_invert, self);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Methods: Set Identity
* \{ */
PyDoc_STRVAR(Quaternion_identity_doc,
".. function:: identity()\n"
"\n"
@ -498,6 +679,12 @@ static PyObject *Quaternion_identity(QuaternionObject *self)
Py_RETURN_NONE;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Methods: Negate
* \{ */
PyDoc_STRVAR(Quaternion_negate_doc,
".. function:: negate()\n"
"\n"
@ -516,6 +703,12 @@ static PyObject *Quaternion_negate(QuaternionObject *self)
Py_RETURN_NONE;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Methods: Conjugate
* \{ */
PyDoc_STRVAR(Quaternion_conjugate_doc,
".. function:: conjugate()\n"
"\n"
@ -543,6 +736,12 @@ static PyObject *Quaternion_conjugated(QuaternionObject *self)
return quat__apply_to_copy(Quaternion_conjugate, self);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Methods: Copy/Deep-Copy
* \{ */
PyDoc_STRVAR(Quaternion_copy_doc,
".. function:: copy()\n"
"\n"
@ -569,7 +768,12 @@ static PyObject *Quaternion_deepcopy(QuaternionObject *self, PyObject *args)
return Quaternion_copy(self);
}
/* print the object to screen */
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Type: `__repr__` & `__str__`
* \{ */
static PyObject *Quaternion_repr(QuaternionObject *self)
{
PyObject *ret, *tuple;
@ -608,6 +812,12 @@ static PyObject *Quaternion_str(QuaternionObject *self)
}
#endif
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Type: Rich Compare
* \{ */
static PyObject *Quaternion_richcmpr(PyObject *a, PyObject *b, int op)
{
PyObject *res;
@ -646,6 +856,12 @@ static PyObject *Quaternion_richcmpr(PyObject *a, PyObject *b, int op)
return Py_INCREF_RET(res);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Type: Hash (`__hash__`)
* \{ */
static Py_hash_t Quaternion_hash(QuaternionObject *self)
{
if (BaseMath_ReadCallback(self) == -1) {
@ -659,15 +875,19 @@ static Py_hash_t Quaternion_hash(QuaternionObject *self)
return mathutils_array_hash(self->quat, QUAT_SIZE);
}
/* ---------------------SEQUENCE PROTOCOLS------------------------ */
/* ----------------------------len(object)------------------------ */
/* sequence length */
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Type: Sequence & Mapping Protocols Implementation
* \{ */
/** Sequence length: `len(object)`. */
static int Quaternion_len(QuaternionObject *UNUSED(self))
{
return QUAT_SIZE;
}
/* ----------------------------object[]--------------------------- */
/* sequence accessor (get) */
/** Sequence accessor (get): `x = object[i]`. */
static PyObject *Quaternion_item(QuaternionObject *self, int i)
{
if (i < 0) {
@ -687,8 +907,8 @@ static PyObject *Quaternion_item(QuaternionObject *self, int i)
return PyFloat_FromDouble(self->quat[i]);
}
/* ----------------------------object[]------------------------- */
/* sequence accessor (set) */
/** Sequence accessor (set): `object[i] = x`. */
static int Quaternion_ass_item(QuaternionObject *self, int i, PyObject *ob)
{
float f;
@ -724,8 +944,8 @@ static int Quaternion_ass_item(QuaternionObject *self, int i, PyObject *ob)
return 0;
}
/* ----------------------------object[z:y]------------------------ */
/* sequence slice (get) */
/** Sequence slice accessor (get): `x = object[i:j]`. */
static PyObject *Quaternion_slice(QuaternionObject *self, int begin, int end)
{
PyObject *tuple;
@ -749,8 +969,8 @@ static PyObject *Quaternion_slice(QuaternionObject *self, int begin, int end)
return tuple;
}
/* ----------------------------object[z:y]------------------------ */
/* sequence slice (set) */
/** Sequence slice accessor (set): `object[i:j] = x`. */
static int Quaternion_ass_slice(QuaternionObject *self, int begin, int end, PyObject *seq)
{
int i, size;
@ -779,7 +999,7 @@ static int Quaternion_ass_slice(QuaternionObject *self, int begin, int end, PyOb
return -1;
}
/* parsed well - now set in vector */
/* Parsed well, now set in vector. */
for (i = 0; i < size; i++) {
self->quat[begin + i] = quat[i];
}
@ -788,6 +1008,7 @@ static int Quaternion_ass_slice(QuaternionObject *self, int begin, int end, PyOb
return 0;
}
/** Sequence generic subscript (get): `x = object[...]`. */
static PyObject *Quaternion_subscript(QuaternionObject *self, PyObject *item)
{
if (PyIndex_Check(item)) {
@ -824,6 +1045,7 @@ static PyObject *Quaternion_subscript(QuaternionObject *self, PyObject *item)
return NULL;
}
/** Sequence generic subscript (set): `object[...] = x`. */
static int Quaternion_ass_subscript(QuaternionObject *self, PyObject *item, PyObject *value)
{
if (PyIndex_Check(item)) {
@ -856,9 +1078,13 @@ static int Quaternion_ass_subscript(QuaternionObject *self, PyObject *item, PyOb
return -1;
}
/* ------------------------NUMERIC PROTOCOLS---------------------- */
/* ------------------------obj + obj------------------------------ */
/* addition */
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Type: Numeric Protocol Implementation
* \{ */
/** Addition: `object + object`. */
static PyObject *Quaternion_add(PyObject *q1, PyObject *q2)
{
float quat[QUAT_SIZE];
@ -882,8 +1108,8 @@ static PyObject *Quaternion_add(PyObject *q1, PyObject *q2)
add_qt_qtqt(quat, quat1->quat, quat2->quat, 1.0f);
return Quaternion_CreatePyObject(quat, Py_TYPE(q1));
}
/* ------------------------obj - obj------------------------------ */
/* subtraction */
/** Subtraction: `object - object`. */
static PyObject *Quaternion_sub(PyObject *q1, PyObject *q2)
{
int x;
@ -921,8 +1147,7 @@ static PyObject *quat_mul_float(QuaternionObject *quat, const float scalar)
return Quaternion_CreatePyObject(tquat, Py_TYPE(quat));
}
/*------------------------obj * obj------------------------------
* multiplication */
/** Multiplication (element-wise or scalar): `object * object`. */
static PyObject *Quaternion_mul(PyObject *q1, PyObject *q2)
{
float scalar;
@ -965,8 +1190,8 @@ static PyObject *Quaternion_mul(PyObject *q1, PyObject *q2)
Py_TYPE(q2)->tp_name);
return NULL;
}
/*------------------------obj *= obj------------------------------
* in-place multiplication */
/** Multiplication in-place (element-wise or scalar): `object *= object`. */
static PyObject *Quaternion_imul(PyObject *q1, PyObject *q2)
{
float scalar;
@ -1005,8 +1230,8 @@ static PyObject *Quaternion_imul(PyObject *q1, PyObject *q2)
Py_INCREF(q1);
return q1;
}
/*------------------------obj @ obj------------------------------
* quaternion multiplication */
/** Multiplication (quaternion multiply): `object @ object`. */
static PyObject *Quaternion_matmul(PyObject *q1, PyObject *q2)
{
float quat[QUAT_SIZE];
@ -1060,8 +1285,8 @@ static PyObject *Quaternion_matmul(PyObject *q1, PyObject *q2)
Py_TYPE(q2)->tp_name);
return NULL;
}
/*------------------------obj @= obj------------------------------
* in-place quaternion multiplication */
/** Multiplication in-place (quaternion multiply): `object @= object`. */
static PyObject *Quaternion_imatmul(PyObject *q1, PyObject *q2)
{
float quat[QUAT_SIZE];
@ -1098,8 +1323,7 @@ static PyObject *Quaternion_imatmul(PyObject *q1, PyObject *q2)
return q1;
}
/* -obj
* Returns the negative of this object. */
/** Negative (returns the negative of this object): `-object`. */
static PyObject *Quaternion_neg(QuaternionObject *self)
{
float tquat[QUAT_SIZE];
@ -1112,18 +1336,23 @@ static PyObject *Quaternion_neg(QuaternionObject *self)
return Quaternion_CreatePyObject(tquat, Py_TYPE(self));
}
/* -----------------PROTOCOL DECLARATIONS-------------------------- */
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Type: Protocol Declarations
* \{ */
static PySequenceMethods Quaternion_SeqMethods = {
(lenfunc)Quaternion_len, /* sq_length */
(binaryfunc)NULL, /* sq_concat */
(ssizeargfunc)NULL, /* sq_repeat */
(ssizeargfunc)Quaternion_item, /* sq_item */
(ssizessizeargfunc)NULL, /* sq_slice, deprecated */
(ssizeobjargproc)Quaternion_ass_item, /* sq_ass_item */
(ssizessizeobjargproc)NULL, /* sq_ass_slice, deprecated */
(objobjproc)NULL, /* sq_contains */
(binaryfunc)NULL, /* sq_inplace_concat */
(ssizeargfunc)NULL, /* sq_inplace_repeat */
(lenfunc)Quaternion_len, /*sq_length*/
(binaryfunc)NULL, /*sq_concat*/
(ssizeargfunc)NULL, /*sq_repeat*/
(ssizeargfunc)Quaternion_item, /*sq_item*/
(ssizessizeargfunc)NULL, /*sq_slice(deprecated)*/
(ssizeobjargproc)Quaternion_ass_item, /*sq_ass_item*/
(ssizessizeobjargproc)NULL, /*sq_ass_slice(deprecated)*/
(objobjproc)NULL, /*sq_contains*/
(binaryfunc)NULL, /*sq_inplace_concat*/
(ssizeargfunc)NULL, /*sq_inplace_repeat*/
};
static PyMappingMethods Quaternion_AsMapping = {
@ -1152,25 +1381,31 @@ static PyNumberMethods Quaternion_NumMethods = {
NULL, /*nb_int*/
NULL, /*nb_reserved*/
NULL, /*nb_float*/
NULL, /* nb_inplace_add */
NULL, /* nb_inplace_subtract */
(binaryfunc)Quaternion_imul, /* nb_inplace_multiply */
NULL, /* nb_inplace_remainder */
NULL, /* nb_inplace_power */
NULL, /* nb_inplace_lshift */
NULL, /* nb_inplace_rshift */
NULL, /* nb_inplace_and */
NULL, /* nb_inplace_xor */
NULL, /* nb_inplace_or */
NULL, /* nb_floor_divide */
NULL, /* nb_true_divide */
NULL, /* nb_inplace_floor_divide */
NULL, /* nb_inplace_true_divide */
NULL, /* nb_index */
(binaryfunc)Quaternion_matmul, /* nb_matrix_multiply */
(binaryfunc)Quaternion_imatmul, /* nb_inplace_matrix_multiply */
NULL, /*nb_inplace_add*/
NULL, /*nb_inplace_subtract*/
(binaryfunc)Quaternion_imul, /*nb_inplace_multiply*/
NULL, /*nb_inplace_remainder*/
NULL, /*nb_inplace_power*/
NULL, /*nb_inplace_lshift*/
NULL, /*nb_inplace_rshift*/
NULL, /*nb_inplace_and*/
NULL, /*nb_inplace_xor*/
NULL, /*nb_inplace_or*/
NULL, /*nb_floor_divide*/
NULL, /*nb_true_divide*/
NULL, /*nb_inplace_floor_divide*/
NULL, /*nb_inplace_true_divide*/
NULL, /*nb_index*/
(binaryfunc)Quaternion_matmul, /*nb_matrix_multiply*/
(binaryfunc)Quaternion_imatmul, /*nb_inplace_matrix_multiply*/
};
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Type: Get/Set Item Implementation
* \{ */
PyDoc_STRVAR(Quaternion_axis_doc, "Quaternion axis value.\n\n:type: float");
static PyObject *Quaternion_axis_get(QuaternionObject *self, void *type)
{
@ -1300,98 +1535,69 @@ static int Quaternion_axis_vector_set(QuaternionObject *self,
return 0;
}
/* ----------------------------------mathutils.Quaternion() -------------- */
static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *seq = NULL;
double angle = 0.0f;
float quat[QUAT_SIZE];
unit_qt(quat);
/** \} */
if (kwds && PyDict_Size(kwds)) {
PyErr_SetString(PyExc_TypeError,
"mathutils.Quaternion(): "
"takes no keyword args");
return NULL;
}
/* -------------------------------------------------------------------- */
/** \name Quaternion Type: Get/Set Item Definitions
* \{ */
if (!PyArg_ParseTuple(args, "|Od:mathutils.Quaternion", &seq, &angle)) {
return NULL;
}
static PyGetSetDef Quaternion_getseters[] = {
{"w",
(getter)Quaternion_axis_get,
(setter)Quaternion_axis_set,
Quaternion_axis_doc,
(void *)0},
{"x",
(getter)Quaternion_axis_get,
(setter)Quaternion_axis_set,
Quaternion_axis_doc,
(void *)1},
{"y",
(getter)Quaternion_axis_get,
(setter)Quaternion_axis_set,
Quaternion_axis_doc,
(void *)2},
{"z",
(getter)Quaternion_axis_get,
(setter)Quaternion_axis_set,
Quaternion_axis_doc,
(void *)3},
{"magnitude", (getter)Quaternion_magnitude_get, (setter)NULL, Quaternion_magnitude_doc, NULL},
{"angle",
(getter)Quaternion_angle_get,
(setter)Quaternion_angle_set,
Quaternion_angle_doc,
NULL},
{"axis",
(getter)Quaternion_axis_vector_get,
(setter)Quaternion_axis_vector_set,
Quaternion_axis_vector_doc,
NULL},
{"is_wrapped",
(getter)BaseMathObject_is_wrapped_get,
(setter)NULL,
BaseMathObject_is_wrapped_doc,
NULL},
{"is_frozen",
(getter)BaseMathObject_is_frozen_get,
(setter)NULL,
BaseMathObject_is_frozen_doc,
NULL},
{"is_valid",
(getter)BaseMathObject_is_valid_get,
(setter)NULL,
BaseMathObject_is_valid_doc,
NULL},
{"owner", (getter)BaseMathObject_owner_get, (setter)NULL, BaseMathObject_owner_doc, NULL},
{NULL, NULL, NULL, NULL, NULL} /* Sentinel */
};
switch (PyTuple_GET_SIZE(args)) {
case 0:
break;
case 1: {
int size;
/** \} */
if ((size = mathutils_array_parse(quat, 3, QUAT_SIZE, seq, "mathutils.Quaternion()")) ==
-1) {
return NULL;
}
/* -------------------------------------------------------------------- */
/** \name Quaternion Type: Method Definitions
* \{ */
if (size == 4) {
/* 4d: Quaternion (common case) */
}
else {
/* 3d: Interpret as exponential map */
BLI_assert(size == 3);
expmap_to_quat(quat, quat);
}
break;
}
case 2: {
float axis[3];
if (mathutils_array_parse(axis, 3, 3, seq, "mathutils.Quaternion()") == -1) {
return NULL;
}
angle = angle_wrap_rad(angle); /* clamp because of precision issues */
axis_angle_to_quat(quat, axis, angle);
break;
/* PyArg_ParseTuple assures no more than 2 */
}
}
return Quaternion_CreatePyObject(quat, type);
}
static PyObject *quat__apply_to_copy(PyObject *(*quat_func)(QuaternionObject *),
QuaternionObject *self)
{
PyObject *ret = Quaternion_copy(self);
PyObject *ret_dummy = quat_func((QuaternionObject *)ret);
if (ret_dummy) {
Py_DECREF(ret_dummy);
return ret;
}
/* error */
Py_DECREF(ret);
return NULL;
}
/* axis vector suffers from precision errors, use this function to ensure */
static void quat__axis_angle_sanitize(float axis[3], float *angle)
{
if (axis) {
if (is_zero_v3(axis) || !isfinite(axis[0]) || !isfinite(axis[1]) || !isfinite(axis[2])) {
axis[0] = 1.0f;
axis[1] = 0.0f;
axis[2] = 0.0f;
}
else if (EXPP_FloatsAreEqual(axis[0], 0.0f, 10) && EXPP_FloatsAreEqual(axis[1], 0.0f, 10) &&
EXPP_FloatsAreEqual(axis[2], 0.0f, 10)) {
axis[0] = 1.0f;
}
}
if (angle) {
if (!isfinite(*angle)) {
*angle = 0.0f;
}
}
}
/* -----------------------METHOD DEFINITIONS ---------------------- */
static struct PyMethodDef Quaternion_methods[] = {
/* In place only. */
{"identity", (PyCFunction)Quaternion_identity, METH_NOARGS, Quaternion_identity_doc},
@ -1446,61 +1652,12 @@ static struct PyMethodDef Quaternion_methods[] = {
{NULL, NULL, 0, NULL},
};
/*****************************************************************************/
/* Python attributes get/set structure: */
/*****************************************************************************/
static PyGetSetDef Quaternion_getseters[] = {
{"w",
(getter)Quaternion_axis_get,
(setter)Quaternion_axis_set,
Quaternion_axis_doc,
(void *)0},
{"x",
(getter)Quaternion_axis_get,
(setter)Quaternion_axis_set,
Quaternion_axis_doc,
(void *)1},
{"y",
(getter)Quaternion_axis_get,
(setter)Quaternion_axis_set,
Quaternion_axis_doc,
(void *)2},
{"z",
(getter)Quaternion_axis_get,
(setter)Quaternion_axis_set,
Quaternion_axis_doc,
(void *)3},
{"magnitude", (getter)Quaternion_magnitude_get, (setter)NULL, Quaternion_magnitude_doc, NULL},
{"angle",
(getter)Quaternion_angle_get,
(setter)Quaternion_angle_set,
Quaternion_angle_doc,
NULL},
{"axis",
(getter)Quaternion_axis_vector_get,
(setter)Quaternion_axis_vector_set,
Quaternion_axis_vector_doc,
NULL},
{"is_wrapped",
(getter)BaseMathObject_is_wrapped_get,
(setter)NULL,
BaseMathObject_is_wrapped_doc,
NULL},
{"is_frozen",
(getter)BaseMathObject_is_frozen_get,
(setter)NULL,
BaseMathObject_is_frozen_doc,
NULL},
{"is_valid",
(getter)BaseMathObject_is_valid_get,
(setter)NULL,
BaseMathObject_is_valid_doc,
NULL},
{"owner", (getter)BaseMathObject_owner_get, (setter)NULL, BaseMathObject_owner_doc, NULL},
{NULL, NULL, NULL, NULL, NULL} /* Sentinel */
};
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Type: Python Object Definition
* \{ */
/* ------------------PY_OBECT DEFINITION-------------------------- */
PyDoc_STRVAR(quaternion_doc,
".. class:: Quaternion([seq, [angle]])\n"
"\n"
@ -1577,6 +1734,12 @@ PyTypeObject quaternion_Type = {
NULL, /* tp_del */
};
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quaternion Type: C/API Constructors
* \{ */
PyObject *Quaternion_CreatePyObject(const float quat[4], PyTypeObject *base_type)
{
QuaternionObject *self;
@ -1643,3 +1806,5 @@ PyObject *Quaternion_CreatePyObject_cb(PyObject *cb_user, uchar cb_type, uchar c
return (PyObject *)self;
}
/** \} */

View File

@ -20,7 +20,8 @@ typedef struct {
* be stored in py_data) or be a wrapper for data allocated through
* blender (stored in blend_data). This is an either/or struct not both */
/* prototypes */
/* Prototypes. */
PyObject *Quaternion_CreatePyObject(const float quat[4],
PyTypeObject *base_type) ATTR_WARN_UNUSED_RESULT;
PyObject *Quaternion_CreatePyObject_wrap(float quat[4],

File diff suppressed because it is too large Load Diff

View File

@ -18,7 +18,8 @@ typedef struct {
int vec_num;
} VectorObject;
/*prototypes*/
/* Prototypes. */
PyObject *Vector_CreatePyObject(const float *vec,
int vec_num,
PyTypeObject *base_type) ATTR_WARN_UNUSED_RESULT;