static int Color_channel_hsv_set(ColorObject *self, PyObject *value, void *type)
{
	float hsv[3];
	int i = GET_INT_FROM_POINTER(type);
	float f = PyFloat_AsDouble(value);

	if (f == -1 && PyErr_Occurred()) {
		PyErr_SetString(PyExc_TypeError,
		                "color.h/s/v = value: "
		                "assigned value not a number");
		return -1;
	}

	if (BaseMath_ReadCallback(self) == -1)
		return -1;

	rgb_to_hsv_v(self->col, hsv);
	CLAMP(f, 0.0f, 1.0f);
	hsv[i] = f;
	hsv_to_rgb_v(hsv, self->col);

	if (BaseMath_WriteCallback(self) == -1)
		return -1;

	return 0;
}
/* multiplication 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_Format(PyExc_TypeError,
		             "Color division: (%s /= %s) "
		             "invalid type for this operation",
		             Py_TYPE(v1)->tp_name, Py_TYPE(v2)->tp_name);
		return NULL;
	}

	(void)BaseMath_WriteCallback(color);
	Py_INCREF(v1);
	return v1;
}
Esempio n. 3
0
//----------------------------object[z:y]------------------------
//sequence slice (set)
static int Euler_ass_slice(EulerObject * self, int begin, int end, PyObject * seq)
{
	int i, size;
	float eul[EULER_SIZE];

	if(!BaseMath_ReadCallback(self))
		return -1;

	CLAMP(begin, 0, EULER_SIZE);
	if (end<0) end= (EULER_SIZE + 1) + end;
	CLAMP(end, 0, EULER_SIZE);
	begin = MIN2(begin,end);

	if((size=mathutils_array_parse(eul, 0, EULER_SIZE, seq, "mathutils.Euler[begin:end] = []")) == -1)
		return -1;

	if(size != (end - begin)){
		PyErr_SetString(PyExc_TypeError, "euler[begin:end] = []: size mismatch in slice assignment");
		return -1;
	}

	for(i= 0; i < EULER_SIZE; i++)
		self->eul[begin + i] = eul[i];

	(void)BaseMath_WriteCallback(self);
	return 0;
}
Esempio n. 4
0
static int Quaternion_angle_set(QuaternionObject *self, PyObject *value, void *UNUSED(closure))
{
	float tquat[4];
	float len;

	float axis[3], angle_dummy;
	float angle;

	if (BaseMath_ReadCallback(self) == -1)
		return -1;

	len = normalize_qt_qt(tquat, self->quat);
	quat_to_axis_angle(axis, &angle_dummy, tquat);

	angle = PyFloat_AsDouble(value);

	if (angle == -1.0f && PyErr_Occurred()) { /* parsed item not a number */
		PyErr_SetString(PyExc_TypeError,
		                "Quaternion.angle = value: float expected");
		return -1;
	}

	angle = angle_wrap_rad(angle);

	quat__axis_angle_sanitize(axis, &angle);

	axis_angle_to_quat(self->quat, axis, angle);
	mul_qt_fl(self->quat, len);

	if (BaseMath_WriteCallback(self) == -1)
		return -1;

	return 0;
}
Esempio n. 5
0
static PyObject *Euler_zero(EulerObject * self)
{
	zero_v3(self->eul);

	(void)BaseMath_WriteCallback(self);
	Py_RETURN_NONE;
}
Esempio n. 6
0
//----------------------------object[z:y]------------------------
//sequence slice (set)
static int Color_ass_slice(ColorObject *self, int begin, int end, PyObject *seq)
{
	int i, size;
	float col[COLOR_SIZE];

	if (BaseMath_ReadCallback(self) == -1)
		return -1;

	CLAMP(begin, 0, COLOR_SIZE);
	if (end<0) end= (COLOR_SIZE + 1) + end;
	CLAMP(end, 0, COLOR_SIZE);
	begin = MIN2(begin, end);

	if ((size=mathutils_array_parse(col, 0, COLOR_SIZE, seq, "mathutils.Color[begin:end] = []")) == -1)
		return -1;

	if (size != (end - begin)) {
		PyErr_SetString(PyExc_ValueError,
		                "color[begin:end] = []: "
		                "size mismatch in slice assignment");
		return -1;
	}

	for (i= 0; i < COLOR_SIZE; i++)
		self->col[begin + i] = col[i];

	(void)BaseMath_WriteCallback(self);
	return 0;
}
Esempio n. 7
0
static PyObject *Euler_rotate_axis(EulerObject *self, PyObject *args)
{
	float angle = 0.0f;
	int axis; /* actually a character */

	if (!PyArg_ParseTuple(args, "Cf:rotate_axis", &axis, &angle)) {
		PyErr_SetString(PyExc_TypeError,
		                "Euler.rotate_axis(): "
		                "expected an axis 'X', 'Y', 'Z' and an angle (float)");
		return NULL;
	}

	if (!(ELEM3(axis, 'X', 'Y', 'Z'))) {
		PyErr_SetString(PyExc_ValueError,
		                "Euler.rotate_axis(): "
		                "expected axis to be 'X', 'Y' or 'Z'");
		return NULL;
	}

	if (BaseMath_ReadCallback(self) == -1)
		return NULL;


	rotate_eulO(self->eul, self->order, (char)axis, angle);

	(void)BaseMath_WriteCallback(self);

	Py_RETURN_NONE;
}
Esempio n. 8
0
static PyObject *Euler_rotate_axis(EulerObject * self, PyObject *args)
{
	float angle = 0.0f;
	const char *axis;

	if(!PyArg_ParseTuple(args, "sf:rotate", &axis, &angle)){
		PyErr_SetString(PyExc_TypeError,
		                "euler.rotate(): "
		                "expected angle (float) and axis (x, y, z)");
		return NULL;
	}
	if(!(ELEM3(*axis, 'X', 'Y', 'Z') && axis[1]=='\0')){
		PyErr_SetString(PyExc_ValueError, "euler.rotate(): "
		                "expected axis to be 'X', 'Y' or 'Z'");
		return NULL;
	}

	if(BaseMath_ReadCallback(self) == -1)
		return NULL;


	rotate_eulO(self->eul, self->order, *axis, angle);

	(void)BaseMath_WriteCallback(self);

	Py_RETURN_NONE;
}
Esempio n. 9
0
static int Quaternion_axis_vector_set(QuaternionObject *self, PyObject *value, void *UNUSED(closure))
{
	float tquat[4];
	float len;

	float axis[3];
	float angle;

	if (BaseMath_ReadCallback(self) == -1)
		return -1;

	len = normalize_qt_qt(tquat, self->quat);
	quat_to_axis_angle(axis, &angle, tquat); /* axis value is unused */

	if (mathutils_array_parse(axis, 3, 3, value, "quat.axis = other") == -1)
		return -1;

	quat__axis_angle_sanitize(axis, &angle);

	axis_angle_to_quat(self->quat, axis, angle);
	mul_qt_fl(self->quat, len);

	if (BaseMath_WriteCallback(self) == -1)
		return -1;

	return 0;
}
Esempio n. 10
0
//----------------------------object[z:y]------------------------
//sequence slice (set)
static int Quaternion_ass_slice(QuaternionObject *self, int begin, int end, PyObject *seq)
{
	int i, size;
	float quat[QUAT_SIZE];

	if (BaseMath_ReadCallback(self) == -1)
		return -1;

	CLAMP(begin, 0, QUAT_SIZE);
	if (end < 0) end = (QUAT_SIZE + 1) + end;
	CLAMP(end, 0, QUAT_SIZE);
	begin = MIN2(begin, end);

	if ((size = mathutils_array_parse(quat, 0, QUAT_SIZE, seq, "mathutils.Quaternion[begin:end] = []")) == -1)
		return -1;

	if (size != (end - begin)) {
		PyErr_SetString(PyExc_ValueError,
		                "quaternion[begin:end] = []: "
		                "size mismatch in slice assignment");
		return -1;
	}

	/* parsed well - now set in vector */
	for (i = 0; i < size; i++)
		self->quat[begin + i] = quat[i];

	(void)BaseMath_WriteCallback(self);
	return 0;
}
Esempio n. 11
0
static PyObject *Euler_zero(EulerObject *self)
{
	zero_v3(self->eul);

	if (BaseMath_WriteCallback(self) == -1)
		return NULL;

	Py_RETURN_NONE;
}
Esempio n. 12
0
static PyObject *Quaternion_conjugate(QuaternionObject *self)
{
	if (BaseMath_ReadCallback(self) == -1)
		return NULL;

	conjugate_qt(self->quat);

	(void)BaseMath_WriteCallback(self);
	Py_RETURN_NONE;
}
Esempio n. 13
0
static PyObject *Quaternion_negate(QuaternionObject *self)
{
	if (BaseMath_ReadCallback(self) == -1)
		return NULL;

	mul_qt_fl(self->quat, -1.0f);

	(void)BaseMath_WriteCallback(self);
	Py_RETURN_NONE;
}
Esempio n. 14
0
static PyObject *Quaternion_identity(QuaternionObject *self)
{
	if (BaseMath_ReadCallback(self) == -1)
		return NULL;

	unit_qt(self->quat);

	(void)BaseMath_WriteCallback(self);
	Py_RETURN_NONE;
}
Esempio n. 15
0
static int Euler_setOrder(EulerObject *self, PyObject *value, void *UNUSED(closure))
{
	const char *order_str= _PyUnicode_AsString(value);
	short order= euler_order_from_string(order_str, "euler.order");

	if(order == -1)
		return -1;

	self->order= order;
	(void)BaseMath_WriteCallback(self); /* order can be written back */
	return 0;
}
Esempio n. 16
0
static PyObject *Euler_make_compatible(EulerObject * self, PyObject *value)
{
	float teul[EULER_SIZE];

	if(!BaseMath_ReadCallback(self))
		return NULL;

	if(mathutils_array_parse(teul, EULER_SIZE, EULER_SIZE, value, "euler.make_compatible(other), invalid 'other' arg") == -1)
		return NULL;

	compatible_eul(self->eul, teul);

	(void)BaseMath_WriteCallback(self);

	Py_RETURN_NONE;
}
Esempio n. 17
0
static PyObject *Euler_rotate(EulerObject * self, PyObject *value)
{
	float self_rmat[3][3], other_rmat[3][3], rmat[3][3];

	if(!BaseMath_ReadCallback(self))
		return NULL;

	if(mathutils_any_to_rotmat(other_rmat, value, "euler.rotate(value)") == -1)
		return NULL;

	eulO_to_mat3(self_rmat, self->eul, self->order);
	mul_m3_m3m3(rmat, self_rmat, other_rmat);

	mat3_to_compatible_eulO(self->eul, self->eul, self->order, rmat);

	(void)BaseMath_WriteCallback(self);
	Py_RETURN_NONE;
}
Esempio n. 18
0
static int Color_hsv_set(ColorObject *self, PyObject *value, void *UNUSED(closure))
{
	float hsv[3];

	if (mathutils_array_parse(hsv, 3, 3, value, "mathutils.Color.hsv = value") == -1)
		return -1;

	CLAMP(hsv[0], 0.0f, 1.0f);
	CLAMP(hsv[1], 0.0f, 1.0f);
	CLAMP(hsv[2], 0.0f, 1.0f);

	hsv_to_rgb_v(hsv, self->col);

	if (BaseMath_WriteCallback(self) == -1)
		return -1;

	return 0;
}
Esempio n. 19
0
static PyObject *Quaternion_rotate(QuaternionObject *self, PyObject *value)
{
	float self_rmat[3][3], other_rmat[3][3], rmat[3][3];
	float tquat[4], length;

	if (BaseMath_ReadCallback(self) == -1)
		return NULL;

	if (mathutils_any_to_rotmat(other_rmat, value, "Quaternion.rotate(value)") == -1)
		return NULL;

	length = normalize_qt_qt(tquat, self->quat);
	quat_to_mat3(self_rmat, tquat);
	mul_m3_m3m3(rmat, other_rmat, self_rmat);

	mat3_to_quat(self->quat, rmat);
	mul_qt_fl(self->quat, length); /* maintain length after rotating */

	(void)BaseMath_WriteCallback(self);
	Py_RETURN_NONE;
}
Esempio n. 20
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;
}
Esempio n. 21
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;
}
Esempio n. 22
0
/* multiplication 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_Format(PyExc_TypeError,
		             "Color multiplication: (%s *= %s) "
		             "invalid type for this operation",
		             Py_TYPE(v1)->tp_name, Py_TYPE(v2)->tp_name);
		return NULL;
	}

	(void)BaseMath_WriteCallback(color);
	Py_INCREF(v1);
	return v1;
}