[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [32179] trunk/blender/source/blender: patch from Dan Eicher with some edirts.

Campbell Barton ideasman42 at gmail.com
Wed Sep 29 07:15:57 CEST 2010


Revision: 32179
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=32179
Author:   campbellbarton
Date:     2010-09-29 07:15:55 +0200 (Wed, 29 Sep 2010)

Log Message:
-----------
patch from Dan Eicher with some edirts.
  vec.rotate(axis, angle)
equivalent to...
  vec[:] = vec * mathutils.Quaternion(axis, angle)

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/BLI_math_vector.h
    trunk/blender/source/blender/blenlib/intern/math_vector.c
    trunk/blender/source/blender/python/generic/mathutils_vector.c

Modified: trunk/blender/source/blender/blenlib/BLI_math_vector.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_math_vector.h	2010-09-28 19:57:47 UTC (rev 32178)
+++ trunk/blender/source/blender/blenlib/BLI_math_vector.h	2010-09-29 05:15:55 UTC (rev 32179)
@@ -148,6 +148,8 @@
 void reflect_v3_v3v3(float r[3], const float v[3], const float n[3]);
 void ortho_basis_v3v3_v3(float r1[3], float r2[3], const float a[3]);
 void bisect_v3_v3v3v3(float r[3], const float a[3], const float b[3], const float c[3]);
+void rotate_v3_v3v3fl(float v[3], const float p[3], const float axis[3], const float angle);
+void rotate_normalized_v3_v3v3fl(float v[3], const float p[3], const float axis[3], const float angle);
 
 /*********************************** Other ***********************************/
 

Modified: trunk/blender/source/blender/blenlib/intern/math_vector.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/math_vector.c	2010-09-28 19:57:47 UTC (rev 32178)
+++ trunk/blender/source/blender/blenlib/intern/math_vector.c	2010-09-29 05:15:55 UTC (rev 32179)
@@ -296,6 +296,36 @@
 	}
 }
 
+/* Rotate a point p by angle theta around an arbitrary axis r
+   http://local.wasp.uwa.edu.au/~pbourke/geometry/
+*/
+void rotate_normalized_v3_v3v3fl(float r[3], const float p[3], const float axis[3], const float angle)
+{
+	const float costheta= cos(angle);
+	const float sintheta= sin(angle);
+
+	r[0]=	((costheta + (1 - costheta) * axis[0] * axis[0]) * p[0]) +
+			(((1 - costheta) * axis[0] * axis[1] - axis[2] * sintheta) * p[1]) +
+			(((1 - costheta) * axis[0] * axis[2] + axis[1] * sintheta) * p[2]);
+
+	r[1]=	(((1 - costheta) * axis[0] * axis[1] + axis[2] * sintheta) * p[0]) +
+			((costheta + (1 - costheta) * axis[1] * axis[1]) * p[1]) +
+			(((1 - costheta) * axis[1] * axis[2] - axis[0] * sintheta) * p[2]);
+
+	r[2]=	(((1 - costheta) * axis[0] * axis[2] - axis[1] * sintheta) * p[0]) +
+			(((1 - costheta) * axis[1] * axis[2] + axis[0] * sintheta) * p[1]) +
+			((costheta + (1 - costheta) * axis[2] * axis[2]) * p[2]);
+}
+
+void rotate_v3_v3v3fl(float r[3], const float p[3], const float axis[3], const float angle)
+{
+	float axis_n[3];
+
+	normalize_v3_v3(axis_n, axis);
+
+	rotate_normalized_v3_v3v3fl(r, p, axis_n, angle);
+}
+
 /*********************************** Other ***********************************/
 
 void print_v2(const char *str, const float v[2])

Modified: trunk/blender/source/blender/python/generic/mathutils_vector.c
===================================================================
--- trunk/blender/source/blender/python/generic/mathutils_vector.c	2010-09-28 19:57:47 UTC (rev 32178)
+++ trunk/blender/source/blender/python/generic/mathutils_vector.c	2010-09-29 05:15:55 UTC (rev 32179)
@@ -686,6 +686,45 @@
 	return newVectorObject(vec, self->size, Py_NEW, NULL);
 }
 
+/*---------------------------- Vector.rotate(angle, axis) ----------------------*/
+static char Vector_Rotate_doc[] =
+".. function:: rotate(axis, angle)\n"
+"\n"
+"   Return vector rotated around axis by angle.\n"
+"\n"
+"   :arg axis: rotation axis.\n"
+"   :type axis: :class:`Vector`\n"
+"   :arg angle: angle in radians.\n"
+"   :type angle: float\n"
+"   :return: an instance of itself\n"
+"   :rtype: :class:`Vector`\n";
+
+static PyObject *Vector_Rotate(VectorObject *self, PyObject *args)
+{
+	VectorObject *axis_vec = NULL;
+	float angle, vec[3];
+
+	if(!PyArg_ParseTuple(args, "O!f", &vector_Type, &axis_vec, &angle)){
+		PyErr_SetString(PyExc_TypeError, "vec.rotate(angle, axis): expected angle (float) and 3D axis");
+		return NULL;
+	}
+
+	if(self->size != 3 || axis_vec->size != 3) {
+		PyErr_SetString(PyExc_AttributeError, "vec.rotate(angle, axis): expects both vectors to be 3D\n");
+		return NULL;
+	}
+
+	if(!BaseMath_ReadCallback(self) || !BaseMath_ReadCallback(axis_vec))
+		return NULL;
+
+	rotate_v3_v3v3fl(vec, self->vec, axis_vec->vec, angle);
+
+	copy_v3_v3(self->vec, vec);
+
+	Py_INCREF(self);
+	return (PyObject *)self;
+}
+
 /*----------------------------Vector.copy() -------------------------------------- */
 static char Vector_copy_doc[] =
 ".. function:: copy()\n"
@@ -2050,6 +2089,7 @@
 	{"difference", ( PyCFunction ) Vector_Difference, METH_O, Vector_Difference_doc},
 	{"project", ( PyCFunction ) Vector_Project, METH_O, Vector_Project_doc},
 	{"lerp", ( PyCFunction ) Vector_Lerp, METH_VARARGS, Vector_Lerp_doc},
+	{"rotate", ( PyCFunction ) Vector_Rotate, METH_VARARGS, Vector_Rotate_doc},
 	{"copy", (PyCFunction) Vector_copy, METH_NOARGS, Vector_copy_doc},
 	{"__copy__", (PyCFunction) Vector_copy, METH_NOARGS, NULL},
 	{NULL, NULL, 0, NULL}





More information about the Bf-blender-cvs mailing list