mathutils support for color arithmetic, also some minor whitespace edits.
This commit is contained in:
@@ -344,6 +344,272 @@ static PyMappingMethods Color_AsMapping = {
|
|||||||
(objobjargproc)Color_ass_subscript
|
(objobjargproc)Color_ass_subscript
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* numeric */
|
||||||
|
|
||||||
|
|
||||||
|
/* addition: obj + obj */
|
||||||
|
static PyObject *Color_add(PyObject *v1, PyObject *v2)
|
||||||
|
{
|
||||||
|
ColorObject *color1 = NULL, *color2 = NULL;
|
||||||
|
float col[COLOR_SIZE];
|
||||||
|
|
||||||
|
if (!ColorObject_Check(v1) || !ColorObject_Check(v2)) {
|
||||||
|
PyErr_SetString(PyExc_AttributeError, "Color addition: arguments not valid for this operation");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
color1 = (ColorObject*)v1;
|
||||||
|
color2 = (ColorObject*)v2;
|
||||||
|
|
||||||
|
if(BaseMath_ReadCallback(color1) == -1 || BaseMath_ReadCallback(color2) == -1)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
add_vn_vnvn(col, color1->col, color2->col, COLOR_SIZE);
|
||||||
|
|
||||||
|
return newColorObject(col, Py_NEW, Py_TYPE(v1));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* addition in-place: obj += obj */
|
||||||
|
static PyObject *Color_iadd(PyObject *v1, PyObject *v2)
|
||||||
|
{
|
||||||
|
ColorObject *color1 = NULL, *color2 = NULL;
|
||||||
|
|
||||||
|
if (!ColorObject_Check(v1) || !ColorObject_Check(v2)) {
|
||||||
|
PyErr_SetString(PyExc_AttributeError, "Color addition: arguments not valid for this operation");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
color1 = (ColorObject*)v1;
|
||||||
|
color2 = (ColorObject*)v2;
|
||||||
|
|
||||||
|
if(BaseMath_ReadCallback(color1) == -1 || BaseMath_ReadCallback(color2) == -1)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
add_vn_vn(color1->col, color2->col, COLOR_SIZE);
|
||||||
|
|
||||||
|
(void)BaseMath_WriteCallback(color1);
|
||||||
|
Py_INCREF(v1);
|
||||||
|
return v1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* subtraction: obj - obj */
|
||||||
|
static PyObject *Color_sub(PyObject *v1, PyObject *v2)
|
||||||
|
{
|
||||||
|
ColorObject *color1 = NULL, *color2 = NULL;
|
||||||
|
float col[COLOR_SIZE];
|
||||||
|
|
||||||
|
if (!ColorObject_Check(v1) || !ColorObject_Check(v2)) {
|
||||||
|
PyErr_SetString(PyExc_AttributeError, "Color subtraction: arguments not valid for this operation");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
color1 = (ColorObject*)v1;
|
||||||
|
color2 = (ColorObject*)v2;
|
||||||
|
|
||||||
|
if(BaseMath_ReadCallback(color1) == -1 || BaseMath_ReadCallback(color2) == -1)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
sub_vn_vnvn(col, color1->col, color2->col, COLOR_SIZE);
|
||||||
|
|
||||||
|
return newColorObject(col, Py_NEW, Py_TYPE(v1));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* subtraction in-place: obj -= obj */
|
||||||
|
static PyObject *Color_isub(PyObject *v1, PyObject *v2)
|
||||||
|
{
|
||||||
|
ColorObject *color1= NULL, *color2= NULL;
|
||||||
|
|
||||||
|
if (!ColorObject_Check(v1) || !ColorObject_Check(v2)) {
|
||||||
|
PyErr_SetString(PyExc_AttributeError, "Color subtraction: arguments not valid for this operation");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
color1 = (ColorObject*)v1;
|
||||||
|
color2 = (ColorObject*)v2;
|
||||||
|
|
||||||
|
if(BaseMath_ReadCallback(color1) == -1 || BaseMath_ReadCallback(color2) == -1)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
sub_vn_vn(color1->col, color2->col, COLOR_SIZE);
|
||||||
|
|
||||||
|
(void)BaseMath_WriteCallback(color1);
|
||||||
|
Py_INCREF(v1);
|
||||||
|
return v1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static PyObject *color_mul_float(ColorObject *color, const float scalar)
|
||||||
|
{
|
||||||
|
float tcol[COLOR_SIZE];
|
||||||
|
mul_vn_vn_fl(tcol, color->col, COLOR_SIZE, scalar);
|
||||||
|
return newColorObject(tcol, Py_NEW, Py_TYPE(color));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static PyObject *Color_mul(PyObject *v1, PyObject *v2)
|
||||||
|
{
|
||||||
|
ColorObject *color1 = NULL, *color2 = NULL;
|
||||||
|
float scalar;
|
||||||
|
|
||||||
|
if ColorObject_Check(v1) {
|
||||||
|
color1= (ColorObject *)v1;
|
||||||
|
if(BaseMath_ReadCallback(color1) == -1)
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if ColorObject_Check(v2) {
|
||||||
|
color2= (ColorObject *)v2;
|
||||||
|
if(BaseMath_ReadCallback(color2) == -1)
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* make sure v1 is always the vector */
|
||||||
|
if (color1 && color2) {
|
||||||
|
/* col * col, dont support yet! */
|
||||||
|
}
|
||||||
|
else if (color1) {
|
||||||
|
if (((scalar= PyFloat_AsDouble(v2)) == -1.0f && PyErr_Occurred())==0) { /* COLOR * FLOAT */
|
||||||
|
return color_mul_float(color1, scalar);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (color2) {
|
||||||
|
if (((scalar= PyFloat_AsDouble(v1)) == -1.0f && PyErr_Occurred())==0) { /* FLOAT * COLOR */
|
||||||
|
return color_mul_float(color2, scalar);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
BLI_assert(!"internal error");
|
||||||
|
}
|
||||||
|
|
||||||
|
PyErr_Format(PyExc_TypeError, "Color multiplication: not supported between '%.200s' and '%.200s' types", Py_TYPE(v1)->tp_name, Py_TYPE(v2)->tp_name);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static PyObject *Color_div(PyObject *v1, PyObject *v2)
|
||||||
|
{
|
||||||
|
ColorObject *color1 = NULL;
|
||||||
|
float scalar;
|
||||||
|
|
||||||
|
if ColorObject_Check(v1) {
|
||||||
|
color1= (ColorObject *)v1;
|
||||||
|
if(BaseMath_ReadCallback(color1) == -1)
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
PyErr_SetString(PyExc_TypeError, "Color division not supported in this order");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* make sure v1 is always the vector */
|
||||||
|
if (((scalar= PyFloat_AsDouble(v2)) == -1.0f && PyErr_Occurred())==0) { /* COLOR * FLOAT */
|
||||||
|
if(scalar==0.0f) {
|
||||||
|
PyErr_SetString(PyExc_ZeroDivisionError, "Color division: divide by zero error");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return color_mul_float(color1, 1.0f / scalar);
|
||||||
|
}
|
||||||
|
|
||||||
|
PyErr_Format(PyExc_TypeError, "Color multiplication: not supported between '%.200s' and '%.200s' types", Py_TYPE(v1)->tp_name, Py_TYPE(v2)->tp_name);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* mulplication in-place: obj *= obj */
|
||||||
|
static PyObject *Color_imul(PyObject *v1, PyObject *v2)
|
||||||
|
{
|
||||||
|
ColorObject *color = (ColorObject *)v1;
|
||||||
|
float scalar;
|
||||||
|
|
||||||
|
if(BaseMath_ReadCallback(color) == -1)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
/* only support color *= float */
|
||||||
|
if (((scalar= PyFloat_AsDouble(v2)) == -1.0f && PyErr_Occurred())==0) { /* COLOR *= FLOAT */
|
||||||
|
mul_vn_fl(color->col, COLOR_SIZE, scalar);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
PyErr_SetString(PyExc_TypeError, "Color multiplication: arguments not acceptable for this operation");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
(void)BaseMath_WriteCallback(color);
|
||||||
|
Py_INCREF(v1);
|
||||||
|
return v1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* mulplication in-place: obj *= obj */
|
||||||
|
static PyObject *Color_idiv(PyObject *v1, PyObject *v2)
|
||||||
|
{
|
||||||
|
ColorObject *color = (ColorObject *)v1;
|
||||||
|
float scalar;
|
||||||
|
|
||||||
|
if(BaseMath_ReadCallback(color) == -1)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
/* only support color /= float */
|
||||||
|
if (((scalar= PyFloat_AsDouble(v2)) == -1.0f && PyErr_Occurred())==0) { /* COLOR /= FLOAT */
|
||||||
|
if(scalar==0.0f) {
|
||||||
|
PyErr_SetString(PyExc_ZeroDivisionError, "Color division: divide by zero error");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
mul_vn_fl(color->col, COLOR_SIZE, 1.0f / scalar);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
PyErr_SetString(PyExc_TypeError, "Color multiplication: arguments not acceptable for this operation");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
(void)BaseMath_WriteCallback(color);
|
||||||
|
Py_INCREF(v1);
|
||||||
|
return v1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* -obj
|
||||||
|
returns the negative of this object*/
|
||||||
|
static PyObject *Color_neg(ColorObject *self)
|
||||||
|
{
|
||||||
|
float tcol[COLOR_SIZE];
|
||||||
|
|
||||||
|
if(BaseMath_ReadCallback(self) == -1)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
negate_vn_vn(tcol, self->col, COLOR_SIZE);
|
||||||
|
return newColorObject(tcol, Py_NEW, Py_TYPE(self));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static PyNumberMethods Color_NumMethods = {
|
||||||
|
(binaryfunc) Color_add, /*nb_add*/
|
||||||
|
(binaryfunc) Color_sub, /*nb_subtract*/
|
||||||
|
(binaryfunc) Color_mul, /*nb_multiply*/
|
||||||
|
NULL, /*nb_remainder*/
|
||||||
|
NULL, /*nb_divmod*/
|
||||||
|
NULL, /*nb_power*/
|
||||||
|
(unaryfunc) Color_neg, /*nb_negative*/
|
||||||
|
(unaryfunc) NULL, /*tp_positive*/
|
||||||
|
(unaryfunc) NULL, /*tp_absolute*/
|
||||||
|
(inquiry) NULL, /*tp_bool*/
|
||||||
|
(unaryfunc) NULL, /*nb_invert*/
|
||||||
|
NULL, /*nb_lshift*/
|
||||||
|
(binaryfunc)NULL, /*nb_rshift*/
|
||||||
|
NULL, /*nb_and*/
|
||||||
|
NULL, /*nb_xor*/
|
||||||
|
NULL, /*nb_or*/
|
||||||
|
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 channel, vector.r/g/b */
|
/* color channel, vector.r/g/b */
|
||||||
static PyObject *Color_getChannel(ColorObject * self, void *type)
|
static PyObject *Color_getChannel(ColorObject * self, void *type)
|
||||||
{
|
{
|
||||||
@@ -473,7 +739,7 @@ PyTypeObject color_Type = {
|
|||||||
NULL, //tp_setattr
|
NULL, //tp_setattr
|
||||||
NULL, //tp_compare
|
NULL, //tp_compare
|
||||||
(reprfunc) Color_repr, //tp_repr
|
(reprfunc) Color_repr, //tp_repr
|
||||||
NULL, //tp_as_number
|
&Color_NumMethods, //tp_as_number
|
||||||
&Color_SeqMethods, //tp_as_sequence
|
&Color_SeqMethods, //tp_as_sequence
|
||||||
&Color_AsMapping, //tp_as_mapping
|
&Color_AsMapping, //tp_as_mapping
|
||||||
NULL, //tp_hash
|
NULL, //tp_hash
|
||||||
|
@@ -869,8 +869,7 @@ static PyObject *Vector_slice(VectorObject *self, int begin, int end)
|
|||||||
return tuple;
|
return tuple;
|
||||||
}
|
}
|
||||||
/* sequence slice (set): vector[a:b] = value */
|
/* sequence slice (set): vector[a:b] = value */
|
||||||
static int Vector_ass_slice(VectorObject *self, int begin, int end,
|
static int Vector_ass_slice(VectorObject *self, int begin, int end, PyObject *seq)
|
||||||
PyObject * seq)
|
|
||||||
{
|
{
|
||||||
int y, size = 0;
|
int y, size = 0;
|
||||||
float vec[MAX_DIMENSIONS];
|
float vec[MAX_DIMENSIONS];
|
||||||
@@ -1121,7 +1120,7 @@ static PyObject *Vector_mul(PyObject * v1, PyObject * v2)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (vec2) {
|
else if (vec2) {
|
||||||
if (((scalar= PyFloat_AsDouble(v1)) == -1.0f && PyErr_Occurred())==0) { /* VEC*FLOAT */
|
if (((scalar= PyFloat_AsDouble(v1)) == -1.0f && PyErr_Occurred())==0) { /* FLOAT * VEC */
|
||||||
return vector_mul_float(vec2, scalar);
|
return vector_mul_float(vec2, scalar);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -171,6 +171,8 @@ void BPY_context_set(bContext *C)
|
|||||||
|
|
||||||
/* defined in AUD_C-API.cpp */
|
/* defined in AUD_C-API.cpp */
|
||||||
extern PyObject *AUD_initPython(void);
|
extern PyObject *AUD_initPython(void);
|
||||||
|
/* defined in gpu_python.c */
|
||||||
|
extern PyObject *GPU_initPython(void);
|
||||||
|
|
||||||
static struct _inittab bpy_internal_modules[]= {
|
static struct _inittab bpy_internal_modules[]= {
|
||||||
{(char *)"noise", BPyInit_noise},
|
{(char *)"noise", BPyInit_noise},
|
||||||
@@ -179,6 +181,7 @@ static struct _inittab bpy_internal_modules[]= {
|
|||||||
{(char *)"bgl", BPyInit_bgl},
|
{(char *)"bgl", BPyInit_bgl},
|
||||||
{(char *)"blf", BPyInit_blf},
|
{(char *)"blf", BPyInit_blf},
|
||||||
{(char *)"aud", AUD_initPython},
|
{(char *)"aud", AUD_initPython},
|
||||||
|
{(char *)"gpu", GPU_initPython},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user