[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [39300] trunk/blender/source/blender/ python/mathutils/mathutils_Vector.c: Adding a readonly length_squared property to mathutils.Vector.

Mitchell Stokes mogurijin at gmail.com
Thu Aug 11 11:40:14 CEST 2011


Revision: 39300
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=39300
Author:   moguri
Date:     2011-08-11 09:40:14 +0000 (Thu, 11 Aug 2011)
Log Message:
-----------
Adding a readonly length_squared property to mathutils.Vector. This is simply vector.dot(vector), so nothing new is really added, but it's nice for writing more intent revealing code. In other words:

if vec.dot(vec) > some_distance*some_distance:
    do_something()

might not be quite as obvious looking as:

if vec.length_squared > some_distance*some_distance:
    do_something()

As to why you'd want to use length_squared over length is that length uses a square root, which isn't always necessary for simple distance checks (e.g., closest object, checks like the ones above, ect).

Modified Paths:
--------------
    trunk/blender/source/blender/python/mathutils/mathutils_Vector.c

Modified: trunk/blender/source/blender/python/mathutils/mathutils_Vector.c
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils_Vector.c	2011-08-11 09:28:07 UTC (rev 39299)
+++ trunk/blender/source/blender/python/mathutils/mathutils_Vector.c	2011-08-11 09:40:14 UTC (rev 39300)
@@ -1728,6 +1728,21 @@
 	return 0;
 }
 
+/* vector.length_squared */
+static PyObject *Vector_getLengthSquared(VectorObject *self, void *UNUSED(closure))
+{
+	double dot = 0.0f;
+	int i;
+
+	if(BaseMath_ReadCallback(self) == -1)
+		return NULL;
+
+	for(i = 0; i < self->size; i++){
+		dot += (double)(self->vec[i] * self->vec[i]);
+	}
+	return PyFloat_FromDouble(dot);
+}
+
 /* Get a new Vector according to the provided swizzle. This function has little
    error checking, as we are in control of the inputs: the closure is set by us
    in Vector_createSwizzleGetSeter. */
@@ -1851,6 +1866,7 @@
 	{(char *)"z", (getter)Vector_getAxis, (setter)Vector_setAxis, (char *)"Vector Z axis (3D Vectors only).\n\n:type: float", (void *)2},
 	{(char *)"w", (getter)Vector_getAxis, (setter)Vector_setAxis, (char *)"Vector W axis (4D Vectors only).\n\n:type: float", (void *)3},
 	{(char *)"length", (getter)Vector_getLength, (setter)Vector_setLength, (char *)"Vector Length.\n\n:type: float", NULL},
+	{(char *)"length_squared", (getter)Vector_getLengthSquared, (setter)NULL, (char *)"Vector length squared (v.dot(v)).\n\n:type: float", NULL},
 	{(char *)"magnitude", (getter)Vector_getLength, (setter)Vector_setLength, (char *)"Vector Length.\n\n:type: float", NULL},
 	{(char *)"is_wrapped", (getter)BaseMathObject_getWrapped, (setter)NULL, (char *)BaseMathObject_Wrapped_doc, NULL},
 	{(char *)"owner", (getter)BaseMathObject_getOwner, (setter)NULL, (char *)BaseMathObject_Owner_doc, NULL},




More information about the Bf-blender-cvs mailing list