Exemplo n.º 1
0
bool Vec3f_ptr_from_Color(PyObject *obj, Vec3f &vec)
{
	if (!ColorObject_Check(obj))
		return false;
	if (BaseMath_ReadCallback((BaseMathObject *)obj) == -1)
		return false;
	vec[0] = ((ColorObject *)obj)->col[0];
	vec[1] = ((ColorObject *)obj)->col[1];
	vec[2] = ((ColorObject *)obj)->col[2];
	return true;
}
Exemplo n.º 2
0
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, don't 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;
}
Exemplo n.º 3
0
/* 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_TypeError,
		                "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));
}
Exemplo n.º 4
0
int mathutils_array_parse_alloc(float **array, int array_min, PyObject *value, const char *error_prefix)
{
	int size;

#if 1 /* approx 6x speedup for mathutils types */

	if ( (size = VectorObject_Check(value)     ? ((VectorObject *)value)->size : 0) ||
	     (size = EulerObject_Check(value)      ? 3 : 0) ||
	     (size = QuaternionObject_Check(value) ? 4 : 0) ||
	     (size = ColorObject_Check(value)      ? 3 : 0))
	{
		if (BaseMath_ReadCallback((BaseMathObject *)value) == -1) {
			return -1;
		}

		if (size < array_min) {
			PyErr_Format(PyExc_ValueError,
			             "%.200s: sequence size is %d, expected > %d",
			             error_prefix, size, array_min);
			return -1;
		}
		
		*array = PyMem_Malloc(size * sizeof(float));
		memcpy(*array, ((BaseMathObject *)value)->data, size * sizeof(float));
		return size;
	}
	else
#endif
	{
		PyObject *value_fast = NULL;
		//*array = NULL;

		/* non list/tuple cases */
		if (!(value_fast = PySequence_Fast(value, error_prefix))) {
			/* PySequence_Fast sets the error */
			return -1;
		}

		size = PySequence_Fast_GET_SIZE(value_fast);

		if (size < array_min) {
			PyErr_Format(PyExc_ValueError,
			             "%.200s: sequence size is %d, expected > %d",
			             error_prefix, size, array_min);
			return -1;
		}

		*array = PyMem_Malloc(size * sizeof(float));

		return mathutils_array_parse_fast(*array, size, value_fast, error_prefix);
	}
}
Exemplo n.º 5
0
/* 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_Format(PyExc_TypeError,
		             "Color subtraction: (%s - %s) "
		             "invalid type for this operation",
		             Py_TYPE(v1)->tp_name, Py_TYPE(v2)->tp_name);
		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 Color_CreatePyObject(col, Py_NEW, Py_TYPE(v1));
}
Exemplo n.º 6
0
/* 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_TypeError,
		                "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;
}
Exemplo n.º 7
0
/* 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_Format(PyExc_TypeError,
		             "Color subtraction: (%s -= %s) "
		             "invalid type for this operation",
		             Py_TYPE(v1)->tp_name, Py_TYPE(v2)->tp_name);
		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;
}
Exemplo n.º 8
0
/* returns -1 exception, 0 false, 1 true */
static PyObject *Color_richcmpr(PyObject *a, PyObject *b, int op)
{
	PyObject *res;
	int ok = -1; /* zero is true */

	if (ColorObject_Check(a) && ColorObject_Check(b)) {
		ColorObject *colA = (ColorObject *)a;
		ColorObject *colB = (ColorObject *)b;

		if (BaseMath_ReadCallback(colA) == -1 || BaseMath_ReadCallback(colB) == -1)
			return NULL;

		ok = EXPP_VectorsAreEqual(colA->col, colB->col, COLOR_SIZE, 1) ? 0 : -1;
	}

	switch (op) {
		case Py_NE:
			ok = !ok;
			/* fall-through */
		case Py_EQ:
			res = ok ? Py_False : Py_True;
			break;

		case Py_LT:
		case Py_LE:
		case Py_GT:
		case Py_GE:
			res = Py_NotImplemented;
			break;
		default:
			PyErr_BadArgument();
			return NULL;
	}

	return Py_INCREF(res), res;
}
Exemplo n.º 9
0
/* helper functionm returns length of the 'value', -1 on error */
int mathutils_array_parse(float *array, int array_min, int array_max, PyObject *value, const char *error_prefix)
{
#if 1 /* approx 6x speedup for mathutils types */
	int size;

	if (    (size= VectorObject_Check(value)     ? ((VectorObject *)value)->size : 0) ||
	        (size= EulerObject_Check(value)      ? 3 : 0) ||
	        (size= QuaternionObject_Check(value) ? 4 : 0) ||
	        (size= ColorObject_Check(value)      ? 3 : 0))
	{
		if (BaseMath_ReadCallback((BaseMathObject *)value) == -1) {
			return -1;
		}

		if (size > array_max || size < array_min) {
			if (array_max == array_min)	{
				PyErr_Format(PyExc_ValueError,
				             "%.200s: sequence size is %d, expected %d",
				             error_prefix, size, array_max);
			}
			else {
				PyErr_Format(PyExc_ValueError,
				             "%.200s: sequence size is %d, expected [%d - %d]",
				             error_prefix, size, array_min, array_max);
			}
			return -1;
		}

		memcpy(array, ((BaseMathObject *)value)->data, size * sizeof(float));
		return size;
	}
	else
#endif
	{
		return mathutils_array_parse_fast(array, array_min, array_max, value, error_prefix);
	}
}
Exemplo n.º 10
0
bool float_array_from_PyObject(PyObject *obj, float *v, int n)
{
	if (VectorObject_Check(obj) && ((VectorObject *)obj)->size == n) {
		if (BaseMath_ReadCallback((BaseMathObject *)obj) == -1)
			return 0;
		for (int i = 0; i < n; i++)
			v[i] = ((VectorObject *)obj)->vec[i];
		return 1;
	}
	else if (ColorObject_Check(obj) && n == 3) {
		if (BaseMath_ReadCallback((BaseMathObject *)obj) == -1)
			return 0;
		for (int i = 0; i < n; i++)
			v[i] = ((ColorObject *)obj)->col[i];
		return 1;
	}
	else if (PyList_Check(obj) && PyList_GET_SIZE(obj) == n) {
		return float_array_from_PyList(obj, v, n);
	}
	else if (PyTuple_Check(obj) && PyTuple_GET_SIZE(obj) == n) {
		return float_array_from_PyTuple(obj, v, n);
	}
	return 0;
}
Exemplo n.º 11
0
/* helper function returns length of the 'value', -1 on error */
int mathutils_array_parse(float *array, int array_min, int array_max, PyObject *value, const char *error_prefix)
{
	const int flag = array_max;
	int size;

	array_max &= ~MU_ARRAY_FLAGS;

#if 1 /* approx 6x speedup for mathutils types */

	if ((size = VectorObject_Check(value)     ? ((VectorObject *)value)->size : 0) ||
	    (size = EulerObject_Check(value)      ? 3 : 0) ||
	    (size = QuaternionObject_Check(value) ? 4 : 0) ||
	    (size = ColorObject_Check(value)      ? 3 : 0))
	{
		if (BaseMath_ReadCallback((BaseMathObject *)value) == -1) {
			return -1;
		}

		if (flag & MU_ARRAY_SPILL) {
			CLAMP_MAX(size, array_max);
		}

		if (size > array_max || size < array_min) {
			if (array_max == array_min) {
				PyErr_Format(PyExc_ValueError,
				             "%.200s: sequence size is %d, expected %d",
				             error_prefix, size, array_max);
			}
			else {
				PyErr_Format(PyExc_ValueError,
				             "%.200s: sequence size is %d, expected [%d - %d]",
				             error_prefix, size, array_min, array_max);
			}
			return -1;
		}

		memcpy(array, ((BaseMathObject *)value)->data, size * sizeof(float));
	}
	else
#endif
	{
		PyObject *value_fast = NULL;

		/* non list/tuple cases */
		if (!(value_fast = PySequence_Fast(value, error_prefix))) {
			/* PySequence_Fast sets the error */
			return -1;
		}

		size = PySequence_Fast_GET_SIZE(value_fast);

		if (flag & MU_ARRAY_SPILL) {
			CLAMP_MAX(size, array_max);
		}

		if (size > array_max || size < array_min) {
			if (array_max == array_min) {
				PyErr_Format(PyExc_ValueError,
				             "%.200s: sequence size is %d, expected %d",
				             error_prefix, size, array_max);
			}
			else {
				PyErr_Format(PyExc_ValueError,
				             "%.200s: sequence size is %d, expected [%d - %d]",
				             error_prefix, size, array_min, array_max);
			}
			Py_DECREF(value_fast);
			return -1;
		}

		size = mathutils_array_parse_fast(array, size, value_fast, error_prefix);
	}

	if (size != -1) {
		if (flag & MU_ARRAY_ZERO) {
			int size_left = array_max - size;
			if (size_left) {
				memset(&array[size], 0, sizeof(float) * size_left);
			}
		}
	}

	return size;
}