/* 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; }
static int validate_array(PyObject *rvalue, PointerRNA *ptr, PropertyRNA *prop, int lvalue_dim, ItemTypeCheckFunc check_item_type, const char *item_type_str, int *totitem, const char *error_prefix) { int dimsize[MAX_ARRAY_DIMENSION]; int totdim = RNA_property_array_dimension(ptr, prop, dimsize); /* validate type first because length validation may modify property array length */ #ifdef USE_MATHUTILS if (lvalue_dim == 0) { /* only valid for first level array */ if (MatrixObject_Check(rvalue)) { MatrixObject *pymat = (MatrixObject *)rvalue; if (BaseMath_ReadCallback(pymat) == -1) return -1; if (RNA_property_type(prop) != PROP_FLOAT) { PyErr_Format(PyExc_ValueError, "%s %.200s.%.200s, matrix assign to non float array", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop)); return -1; } else if (totdim != 2) { PyErr_Format(PyExc_ValueError, "%s %.200s.%.200s, matrix assign array with %d dimensions", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop), totdim); return -1; } else if (pymat->num_col != dimsize[0] || pymat->num_row != dimsize[1]) { PyErr_Format(PyExc_ValueError, "%s %.200s.%.200s, matrix assign dimension size mismatch, " "is %dx%d, expected be %dx%d", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop), pymat->num_col, pymat->num_row, dimsize[0], dimsize[1]); return -1; } else { *totitem = dimsize[0] * dimsize[1]; return 0; } } } #endif /* USE_MATHUTILS */ { if (validate_array_type(rvalue, lvalue_dim, totdim, dimsize, check_item_type, item_type_str, error_prefix) == -1) return -1; return validate_array_length(rvalue, ptr, prop, lvalue_dim, totitem, error_prefix); } }
static PyObject *M_Geometry_intersect_line_line_2d(PyObject *UNUSED(self), PyObject* args) { VectorObject *line_a1, *line_a2, *line_b1, *line_b2; float vi[2]; if(!PyArg_ParseTuple(args, "O!O!O!O!:intersect_line_line_2d", &vector_Type, &line_a1, &vector_Type, &line_a2, &vector_Type, &line_b1, &vector_Type, &line_b2) ) { return NULL; } if(BaseMath_ReadCallback(line_a1) == -1 || BaseMath_ReadCallback(line_a2) == -1 || BaseMath_ReadCallback(line_b1) == -1 || BaseMath_ReadCallback(line_b2) == -1) return NULL; if(isect_seg_seg_v2_point(line_a1->vec, line_a2->vec, line_b1->vec, line_b2->vec, vi) == 1) { return newVectorObject(vi, 2, Py_NEW, NULL); } else { Py_RETURN_NONE; } }
/* 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; }
static PyObject *Quaternion_str(QuaternionObject *self) { DynStr *ds; if (BaseMath_ReadCallback(self) == -1) return NULL; ds = BLI_dynstr_new(); BLI_dynstr_appendf(ds, "<Quaternion (w=%.4f, x=%.4f, y=%.4f, z=%.4f)>", self->quat[0], self->quat[1], self->quat[2], self->quat[3]); return mathutils_dynstr_to_py(ds); /* frees ds */ }
//----------------------------print object (internal)-------------- //print the object to screen static PyObject *Quaternion_repr(QuaternionObject *self) { PyObject *ret, *tuple; if (BaseMath_ReadCallback(self) == -1) return NULL; tuple = Quaternion_to_tuple_ext(self, -1); ret = PyUnicode_FromFormat("Quaternion(%R)", tuple); Py_DECREF(tuple); return ret; }
static PyObject *Euler_str(EulerObject *self) { DynStr *ds; if (BaseMath_ReadCallback(self) == -1) return NULL; ds = BLI_dynstr_new(); BLI_dynstr_appendf(ds, "<Euler (x=%.4f, y=%.4f, z=%.4f), order='%s'>", self->eul[0], self->eul[1], self->eul[2], euler_order_str(self)); return mathutils_dynstr_to_py(ds); /* frees ds */ }
static PyObject *Color_repr(ColorObject * self) { PyObject *ret, *tuple; if (BaseMath_ReadCallback(self) == -1) return NULL; tuple= Color_ToTupleExt(self, -1); ret= PyUnicode_FromFormat("Color(%R)", tuple); Py_DECREF(tuple); return ret; }
static PyObject *Euler_repr(EulerObject * self) { PyObject *ret, *tuple; if(!BaseMath_ReadCallback(self)) return NULL; tuple= Euler_ToTupleExt(self, -1); ret= PyUnicode_FromFormat("Euler(%R, '%s')", tuple, euler_order_str(self)); Py_DECREF(tuple); return ret; }
static PyObject *Color_str(ColorObject *self) { DynStr *ds; if (BaseMath_ReadCallback(self) == -1) return NULL; ds = BLI_dynstr_new(); BLI_dynstr_appendf(ds, "<Color (r=%.4f, g=%.4f, b=%.4f)>", self->col[0], self->col[1], self->col[2]); return mathutils_dynstr_to_py(ds); /* frees ds */ }
static PyObject *M_Geometry_intersect_point_tri_2d(PyObject *UNUSED(self), PyObject *args) { VectorObject *pt_vec, *tri_p1, *tri_p2, *tri_p3; if (!PyArg_ParseTuple(args, "O!O!O!O!:intersect_point_tri_2d", &vector_Type, &pt_vec, &vector_Type, &tri_p1, &vector_Type, &tri_p2, &vector_Type, &tri_p3)) { return NULL; } if (BaseMath_ReadCallback(pt_vec) == -1 || BaseMath_ReadCallback(tri_p1) == -1 || BaseMath_ReadCallback(tri_p2) == -1 || BaseMath_ReadCallback(tri_p3) == -1) { return NULL; } return PyLong_FromLong(isect_point_tri_v2(pt_vec->vec, tri_p1->vec, tri_p2->vec, tri_p3->vec)); }
static PyObject *M_Geometry_ClosestPointOnLine( PyObject * self, PyObject * args ) { VectorObject *pt, *line_1, *line_2; float pt_in[3], pt_out[3], l1[3], l2[3]; float lambda; PyObject *ret; if( !PyArg_ParseTuple ( args, "O!O!O!", &vector_Type, &pt, &vector_Type, &line_1, &vector_Type, &line_2) ) { PyErr_SetString( PyExc_TypeError, "expected 3 vector types\n" ); return NULL; } if(!BaseMath_ReadCallback(pt) || !BaseMath_ReadCallback(line_1) || !BaseMath_ReadCallback(line_2)) return NULL; /* accept 2d verts */ if (pt->size==3) { VECCOPY(pt_in, pt->vec);} else { pt_in[2]=0.0; VECCOPY2D(pt_in, pt->vec) } if (line_1->size==3) { VECCOPY(l1, line_1->vec);} else { l1[2]=0.0; VECCOPY2D(l1, line_1->vec) } if (line_2->size==3) { VECCOPY(l2, line_2->vec);} else { l2[2]=0.0; VECCOPY2D(l2, line_2->vec) } /* do the calculation */ lambda = lambda_cp_line_ex(pt_in, l1, l2, pt_out); ret = PyTuple_New(2); PyTuple_SET_ITEM( ret, 0, newVectorObject(pt_out, 3, Py_NEW, NULL) ); PyTuple_SET_ITEM( ret, 1, PyFloat_FromDouble(lambda) ); return ret; }
static PyObject *M_Geometry_intersect_line_plane(PyObject *UNUSED(self), PyObject *args) { VectorObject *line_a, *line_b, *plane_co, *plane_no; int no_flip = 0; float isect[3]; if (!PyArg_ParseTuple(args, "O!O!O!O!|i:intersect_line_plane", &vector_Type, &line_a, &vector_Type, &line_b, &vector_Type, &plane_co, &vector_Type, &plane_no, &no_flip)) { return NULL; } if (BaseMath_ReadCallback(line_a) == -1 || BaseMath_ReadCallback(line_b) == -1 || BaseMath_ReadCallback(plane_co) == -1 || BaseMath_ReadCallback(plane_no) == -1) { return NULL; } if (ELEM4(2, line_a->size, line_b->size, plane_co->size, plane_no->size)) { PyErr_SetString(PyExc_ValueError, "geometry.intersect_line_plane(...): " " can't use 2D Vectors"); return NULL; } if (isect_line_plane_v3(isect, line_a->vec, line_b->vec, plane_co->vec, plane_no->vec, no_flip) == 1) { return Vector_CreatePyObject(isect, 3, Py_NEW, NULL); } else { Py_RETURN_NONE; } }
static PyObject *M_Geometry_area_tri(PyObject *UNUSED(self), PyObject *args) { VectorObject *vec1, *vec2, *vec3; if (!PyArg_ParseTuple(args, "O!O!O!:area_tri", &vector_Type, &vec1, &vector_Type, &vec2, &vector_Type, &vec3)) { return NULL; } if (vec1->size != vec2->size || vec1->size != vec3->size) { PyErr_SetString(PyExc_ValueError, "vectors must be of the same size"); return NULL; } if (BaseMath_ReadCallback(vec1) == -1 || BaseMath_ReadCallback(vec2) == -1 || BaseMath_ReadCallback(vec3) == -1) { return NULL; } if (vec1->size == 3) { return PyFloat_FromDouble(area_tri_v3(vec1->vec, vec2->vec, vec3->vec)); } else if (vec1->size == 2) { return PyFloat_FromDouble(area_tri_v2(vec1->vec, vec2->vec, vec3->vec)); } else { PyErr_SetString(PyExc_ValueError, "only 2D,3D vectors are supported"); return NULL; } }
static PyObject *Quaternion_cross(QuaternionObject *self, PyObject *value) { float quat[QUAT_SIZE], tquat[QUAT_SIZE]; if (BaseMath_ReadCallback(self) == -1) return NULL; if (mathutils_array_parse(tquat, QUAT_SIZE, QUAT_SIZE, value, "Quaternion.cross(other), invalid 'other' arg") == -1) { return NULL; } mul_qt_qtqt(quat, self->quat, tquat); return Quaternion_CreatePyObject(quat, Py_NEW, Py_TYPE(self)); }
/* 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; }
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; }
static PyObject *Quaternion_dot(QuaternionObject *self, PyObject *value) { float tquat[QUAT_SIZE]; if (BaseMath_ReadCallback(self) == -1) return NULL; if (mathutils_array_parse(tquat, QUAT_SIZE, QUAT_SIZE, value, "Quaternion.dot(other), invalid 'other' arg") == -1) { return NULL; } return PyFloat_FromDouble(dot_qtqt(self->quat, tquat)); }
static PyObject *Euler_richcmpr(PyObject *a, PyObject *b, int op) { PyObject *res; int ok = -1; /* zero is true */ if (EulerObject_Check(a) && EulerObject_Check(b)) { EulerObject *eulA = (EulerObject *)a; EulerObject *eulB = (EulerObject *)b; if (BaseMath_ReadCallback(eulA) == -1 || BaseMath_ReadCallback(eulB) == -1) return NULL; ok = ((eulA->order == eulB->order) && EXPP_VectorsAreEqual(eulA->eul, eulB->eul, EULER_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; }
static PyObject *M_Geometry_intersect_point_line(PyObject *UNUSED(self), PyObject* args) { VectorObject *pt, *line_1, *line_2; float pt_in[3], pt_out[3], l1[3], l2[3]; float lambda; PyObject *ret; if(!PyArg_ParseTuple(args, "O!O!O!:intersect_point_line", &vector_Type, &pt, &vector_Type, &line_1, &vector_Type, &line_2) ) { return NULL; } if(BaseMath_ReadCallback(pt) == -1 || BaseMath_ReadCallback(line_1) == -1 || BaseMath_ReadCallback(line_2) == -1) return NULL; /* accept 2d verts */ if (pt->size==3) { VECCOPY(pt_in, pt->vec);} else { pt_in[2]=0.0; VECCOPY2D(pt_in, pt->vec) } if (line_1->size==3) { VECCOPY(l1, line_1->vec);} else { l1[2]=0.0; VECCOPY2D(l1, line_1->vec) } if (line_2->size==3) { VECCOPY(l2, line_2->vec);} else { l2[2]=0.0; VECCOPY2D(l2, line_2->vec) } /* do the calculation */ lambda= closest_to_line_v3(pt_out, pt_in, l1, l2); ret= PyTuple_New(2); PyTuple_SET_ITEM(ret, 0, newVectorObject(pt_out, 3, Py_NEW, NULL)); PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(lambda)); return ret; }
static PyObject *Color_hsv_get(ColorObject *self, void *UNUSED(closure)) { float hsv[3]; PyObject *ret; if (BaseMath_ReadCallback(self) == -1) return NULL; rgb_to_hsv(self->col[0], self->col[1], self->col[2], &(hsv[0]), &(hsv[1]), &(hsv[2])); ret = PyTuple_New(3); PyTuple_SET_ITEM(ret, 0, PyFloat_FromDouble(hsv[0])); PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(hsv[1])); PyTuple_SET_ITEM(ret, 2, PyFloat_FromDouble(hsv[2])); return ret; }
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; }
static PyObject *Quaternion_angle_get(QuaternionObject *self, void *UNUSED(closure)) { float tquat[4]; float angle; if (BaseMath_ReadCallback(self) == -1) return NULL; normalize_qt_qt(tquat, self->quat); angle = 2.0f * saacos(tquat[0]); quat__axis_angle_sanitize(NULL, &angle); return PyFloat_FromDouble(angle); }
static PyObject *Quaternion_axis_vector_get(QuaternionObject *self, void *UNUSED(closure)) { float tquat[4]; float axis[3]; float angle_dummy; if (BaseMath_ReadCallback(self) == -1) return NULL; normalize_qt_qt(tquat, self->quat); quat_to_axis_angle(axis, &angle_dummy, tquat); quat__axis_angle_sanitize(axis, NULL); return Vector_CreatePyObject(axis, 3, Py_NEW, NULL); }
static PyObject *Quaternion_rotation_difference(QuaternionObject *self, PyObject *value) { float tquat[QUAT_SIZE], quat[QUAT_SIZE]; if (BaseMath_ReadCallback(self) == -1) return NULL; if (mathutils_array_parse(tquat, QUAT_SIZE, QUAT_SIZE, value, "Quaternion.difference(other), invalid 'other' arg") == -1) { return NULL; } rotation_between_quats_to_quat(quat, self->quat, tquat); return Quaternion_CreatePyObject(quat, Py_NEW, Py_TYPE(self)); }
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; }
//----------------------------object[z:y]------------------------ //sequence slice (get) static PyObject *Color_slice(ColorObject * self, int begin, int end) { PyObject *tuple; int count; if (BaseMath_ReadCallback(self) == -1) return NULL; CLAMP(begin, 0, COLOR_SIZE); if (end<0) end= (COLOR_SIZE + 1) + end; CLAMP(end, 0, COLOR_SIZE); begin= MIN2(begin, end); tuple= PyTuple_New(end - begin); for (count= begin; count < end; count++) { PyTuple_SET_ITEM(tuple, count - begin, PyFloat_FromDouble(self->col[count])); } return tuple; }
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; }
static PyObject *M_Geometry_normal(PyObject *UNUSED(self), PyObject* args) { VectorObject *vec1, *vec2, *vec3, *vec4; float n[3]; if(PyTuple_GET_SIZE(args) == 3) { if(!PyArg_ParseTuple(args, "O!O!O!:normal", &vector_Type, &vec1, &vector_Type, &vec2, &vector_Type, &vec3)) { return NULL; } if(vec1->size != vec2->size || vec1->size != vec3->size) { PyErr_SetString(PyExc_ValueError, "vectors must be of the same size"); return NULL; } if(vec1->size < 3) { PyErr_SetString(PyExc_ValueError, "2D vectors unsupported"); return NULL; } if(BaseMath_ReadCallback(vec1) == -1 || BaseMath_ReadCallback(vec2) == -1 || BaseMath_ReadCallback(vec3) == -1) return NULL; normal_tri_v3(n, vec1->vec, vec2->vec, vec3->vec); } else { if(!PyArg_ParseTuple(args, "O!O!O!O!:normal", &vector_Type, &vec1, &vector_Type, &vec2, &vector_Type, &vec3, &vector_Type, &vec4)) { return NULL; } if(vec1->size != vec2->size || vec1->size != vec3->size || vec1->size != vec4->size) { PyErr_SetString(PyExc_ValueError, "vectors must be of the same size"); return NULL; } if(vec1->size < 3) { PyErr_SetString(PyExc_ValueError, "2D vectors unsupported"); return NULL; } if(BaseMath_ReadCallback(vec1) == -1 || BaseMath_ReadCallback(vec2) == -1 || BaseMath_ReadCallback(vec3) == -1 || BaseMath_ReadCallback(vec4) == -1) return NULL; normal_quad_v3(n, vec1->vec, vec2->vec, vec3->vec, vec4->vec); } return newVectorObject(n, 3, Py_NEW, NULL); }
static PyObject *Quaternion_to_axis_angle(QuaternionObject *self) { PyObject *ret; float tquat[4]; float axis[3]; float angle; if (BaseMath_ReadCallback(self) == -1) return NULL; normalize_qt_qt(tquat, self->quat); quat_to_axis_angle(axis, &angle, tquat); quat__axis_angle_sanitize(axis, &angle); ret = PyTuple_New(2); PyTuple_SET_ITEM(ret, 0, Vector_CreatePyObject(axis, 3, Py_NEW, NULL)); PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(angle)); return ret; }