Added vec.reflect(mirror) method to Python Mathutils Vector type

also made sensor and actuator text areas a bit bigger so full names can be read.
This commit is contained in:
Campbell Barton
2008-08-18 02:29:25 +00:00
parent 89a735e4f8
commit ff45582551
4 changed files with 69 additions and 9 deletions

View File

@@ -521,6 +521,15 @@ class Vector:
@return: Return a quaternion rotation from the vector and the track and up axis.
"""
def reflect(mirror):
"""
Return the reflection vector from the mirror vector argument.
@type mirror: Vector object
@param mirror: This vector could be a normal from the reflecting surface.
@rtype: Vector object matching the size of this vector.
@return: The reflected vector.
"""
class Euler:
"""
The Euler object

View File

@@ -42,6 +42,7 @@ char Vector_Resize3D_doc[] = "() - resize a vector to [x,y,z]";
char Vector_Resize4D_doc[] = "() - resize a vector to [x,y,z,w]";
char Vector_toPoint_doc[] = "() - create a new Point Object from this vector";
char Vector_ToTrackQuat_doc[] = "(track, up) - extract a quaternion from the vector and the track and up axis";
char Vector_reflect_doc[] = "(mirror) - return a vector reflected on the mirror normal";
char Vector_copy_doc[] = "() - return a copy of the vector";
/*-----------------------METHOD DEFINITIONS ----------------------*/
struct PyMethodDef Vector_methods[] = {
@@ -53,6 +54,7 @@ struct PyMethodDef Vector_methods[] = {
{"resize4D", (PyCFunction) Vector_Resize4D, METH_NOARGS, Vector_Resize2D_doc},
{"toPoint", (PyCFunction) Vector_toPoint, METH_NOARGS, Vector_toPoint_doc},
{"toTrackQuat", ( PyCFunction ) Vector_ToTrackQuat, METH_VARARGS, Vector_ToTrackQuat_doc},
{"reflect", ( PyCFunction ) Vector_reflect, METH_O, Vector_reflect_doc},
{"copy", (PyCFunction) Vector_copy, METH_NOARGS, Vector_copy_doc},
{"__copy__", (PyCFunction) Vector_copy, METH_NOARGS, Vector_copy_doc},
{NULL, NULL, 0, NULL}
@@ -273,7 +275,55 @@ PyObject *Vector_ToTrackQuat( VectorObject * self, PyObject * args )
return newQuaternionObject(quat, Py_NEW);
}
/*----------------------------Vector.reflect(mirror) ----------------------
return a reflected vector on the mirror normal
((2 * DotVecs(vec, mirror)) * mirror) - vec
using arithb.c would be nice here */
PyObject *Vector_reflect( VectorObject * self, PyObject * value )
{
VectorObject *mirrvec;
float mirror[3];
float vec[3];
float reflect[4] = {0.0f, 0.0f, 0.0f, 0.0f};
float dot2;
/* for normalizing */
int i;
float norm = 0.0f;
if (!VectorObject_Check(value))
return EXPP_ReturnPyObjError( PyExc_TypeError, "expected a vector argument" );
mirrvec = (VectorObject *)value;
mirror[0] = mirrvec->vec[0];
mirror[1] = mirrvec->vec[1];
if (mirrvec->size > 2) mirror[2] = mirrvec->vec[2];
else mirror[2] = 0.0;
/* normalize, whos idea was it not to use arithb.c? :-/ */
for(i = 0; i < 3; i++) {
norm += mirror[i] * mirror[i];
}
norm = (float) sqrt(norm);
for(i = 0; i < 3; i++) {
mirror[i] /= norm;
}
/* done */
vec[0] = self->vec[0];
vec[1] = self->vec[1];
if (self->size > 2) vec[2] = self->vec[2];
else vec[2] = 0.0;
dot2 = 2 * vec[0]*mirror[0]+vec[1]*mirror[1]+vec[2]*mirror[2];
reflect[0] = (dot2 * mirror[0]) - vec[0];
reflect[1] = (dot2 * mirror[1]) - vec[1];
reflect[2] = (dot2 * mirror[2]) - vec[2];
return newVectorObject(reflect, self->size, Py_NEW);
}
/*----------------------------Vector.copy() --------------------------------------
return a copy of the vector */

View File

@@ -52,6 +52,7 @@ PyObject *Vector_Resize3D( VectorObject * self );
PyObject *Vector_Resize4D( VectorObject * self );
PyObject *Vector_toPoint( VectorObject * self );
PyObject *Vector_ToTrackQuat( VectorObject * self, PyObject * args );
PyObject *Vector_reflect( VectorObject * self, PyObject * value );
PyObject *Vector_copy( VectorObject * self );
PyObject *newVectorObject(float *vec, int size, int type);