From a46beac3f87ee78d6231b2f0038db31c50aa2315 Mon Sep 17 00:00:00 2001 From: Martin Poirier Date: Sun, 6 Sep 2009 01:11:47 +0000 Subject: [PATCH 1/2] Rename Vec3ToTangent VecBisect3, since that's what it does. --- source/blender/blenkernel/intern/curve.c | 8 ++++---- source/blender/blenlib/BLI_arithb.h | 2 +- source/blender/blenlib/intern/arithb.c | 9 ++++----- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/source/blender/blenkernel/intern/curve.c b/source/blender/blenkernel/intern/curve.c index 709507a9b26..25af0cb5ce3 100644 --- a/source/blender/blenkernel/intern/curve.c +++ b/source/blender/blenkernel/intern/curve.c @@ -1903,7 +1903,7 @@ void makeBevelList(Object *ob) } /* this has to be >2 points */ else if(cu->flag & CU_NO_TWIST && cu->flag & CU_3D && bl->poly != -1) { - /* Special case, cyclic curve with no twisy. tricky... */ + /* Special case, cyclic curve with no twist. tricky... */ float quat[4], q[4], cross[3]; @@ -1924,7 +1924,7 @@ void makeBevelList(Object *ob) while(nr--) { /* Normalizes */ - Vec3ToTangent(vec, &bevp0->x, &bevp1->x, &bevp2->x); + VecBisect3(vec, &bevp0->x, &bevp1->x, &bevp2->x); if(bl->nr==nr+1) { /* first time */ vectoquat(vec, 5, 1, quat); @@ -1976,7 +1976,7 @@ void makeBevelList(Object *ob) nr= bl->nr; while(nr--) { - Vec3ToTangent(vec, &bevp0->x, &bevp1->x, &bevp2->x); + VecBisect3(vec, &bevp0->x, &bevp1->x, &bevp2->x); quat_tmp1= (float *)bevp1->mat; quat_tmp2= quat_tmp1+4; @@ -2014,7 +2014,7 @@ void makeBevelList(Object *ob) if(cu->flag & CU_3D) { /* 3D */ /* Normalizes */ - Vec3ToTangent(vec, &bevp0->x, &bevp1->x, &bevp2->x); + VecBisect3(vec, &bevp0->x, &bevp1->x, &bevp2->x); if(bl->nr==nr+1 || !(cu->flag & CU_NO_TWIST)) { /* first time */ vectoquat(vec, 5, 1, quat); diff --git a/source/blender/blenlib/BLI_arithb.h b/source/blender/blenlib/BLI_arithb.h index 63b351016d4..86b626dabdf 100644 --- a/source/blender/blenlib/BLI_arithb.h +++ b/source/blender/blenlib/BLI_arithb.h @@ -270,7 +270,7 @@ void AxisAngleToQuat(float *q, float *axis, float angle); void RotationBetweenVectorsToQuat(float *q, float v1[3], float v2[3]); void vectoquat(float *vec, short axis, short upflag, float *q); -void Vec3ToTangent(float *v, float *v1, float *v2, float *v3); +void VecBisect3(float *v, float *v1, float *v2, float *v3); float VecAngle2(float *v1, float *v2); float VecAngle3(float *v1, float *v2, float *v3); float NormalizedVecAngle2(float *v1, float *v2); diff --git a/source/blender/blenlib/intern/arithb.c b/source/blender/blenlib/intern/arithb.c index 970d8f7d0de..c20794953b9 100644 --- a/source/blender/blenlib/intern/arithb.c +++ b/source/blender/blenlib/intern/arithb.c @@ -2969,17 +2969,16 @@ void VecRotToQuat( float *vec, float phi, float *quat) } } -/* get a direction from 3 vectors that wont depend - * on the distance between the points */ -void Vec3ToTangent(float *v, float *v1, float *v2, float *v3) +/* Returns a vector bisecting the angle at v2 formed by v1, v2 and v3 */ +void VecBisect3(float *out, float *v1, float *v2, float *v3) { float d_12[3], d_23[3]; VecSubf(d_12, v2, v1); VecSubf(d_23, v3, v2); Normalize(d_12); Normalize(d_23); - VecAddf(v, d_12, d_23); - Normalize(v); + VecAddf(out, d_12, d_23); + Normalize(out); } /* Return the angle in degrees between vecs 1-2 and 2-3 in degrees From 7064f6c5d88e17406df1c7c8d1fec20364571084 Mon Sep 17 00:00:00 2001 From: Dalai Felinto Date: Sun, 6 Sep 2009 19:51:57 +0000 Subject: [PATCH 2/2] Mathutils fix: Vector.reflect * correct function for reflection and moving it to arithb.c * note: 2.5 has an already more elegant solution for it (still wrong, but the code is cleaner). Therefore the merge may need to be manual in that case. Specifically in 2.5 we are doing: if(!BaseMath_ReadCallback(self) || !BaseMath_ReadCallback(value)) return NULL; And there we don't need to create a VectorObject *mirrvec; only to get the values. Code used to test it: http://www.pasteall.org/7654/python * YoFrankie script probably needs to be fixed too. --- source/blender/blenlib/BLI_arithb.h | 1 + source/blender/blenlib/intern/arithb.c | 23 +++++++++++++++++++ source/blender/python/api2_2x/vector.c | 31 +++++--------------------- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/source/blender/blenlib/BLI_arithb.h b/source/blender/blenlib/BLI_arithb.h index 86b626dabdf..0c3ec40f16d 100644 --- a/source/blender/blenlib/BLI_arithb.h +++ b/source/blender/blenlib/BLI_arithb.h @@ -270,6 +270,7 @@ void AxisAngleToQuat(float *q, float *axis, float angle); void RotationBetweenVectorsToQuat(float *q, float v1[3], float v2[3]); void vectoquat(float *vec, short axis, short upflag, float *q); +void VecReflect(float *out, float *v1, float *v2); void VecBisect3(float *v, float *v1, float *v2, float *v3); float VecAngle2(float *v1, float *v2); float VecAngle3(float *v1, float *v2, float *v3); diff --git a/source/blender/blenlib/intern/arithb.c b/source/blender/blenlib/intern/arithb.c index c20794953b9..e7a31e37581 100644 --- a/source/blender/blenlib/intern/arithb.c +++ b/source/blender/blenlib/intern/arithb.c @@ -2981,6 +2981,29 @@ void VecBisect3(float *out, float *v1, float *v2, float *v3) Normalize(out); } +/* Returns a reflection vector from a vector and a normal vector +reflect = vec - ((2 * DotVecs(vec, mirror)) * mirror) +*/ +void VecReflect(float *out, float *v1, float *v2) +{ + float vec[3], normal[3]; + float reflect[3] = {0.0f, 0.0f, 0.0f}; + float dot2; + + VecCopyf(vec, v1); + VecCopyf(normal, v2); + + Normalize(normal); + + dot2 = 2 * Inpf(vec, normal); + + reflect[0] = vec[0] - (dot2 * normal[0]); + reflect[1] = vec[1] - (dot2 * normal[1]); + reflect[2] = vec[2] - (dot2 * normal[2]); + + VecCopyf(out, reflect); +} + /* Return the angle in degrees between vecs 1-2 and 2-3 in degrees If v1 is a shoulder, v2 is the elbow and v3 is the hand, this would return the angle at the elbow */ diff --git a/source/blender/python/api2_2x/vector.c b/source/blender/python/api2_2x/vector.c index a7e00e2878a..6a0107142c1 100644 --- a/source/blender/python/api2_2x/vector.c +++ b/source/blender/python/api2_2x/vector.c @@ -277,19 +277,14 @@ PyObject *Vector_ToTrackQuat( VectorObject * self, PyObject * args ) /*----------------------------Vector.reflect(mirror) ---------------------- return a reflected vector on the mirror normal - ((2 * DotVecs(vec, mirror)) * mirror) - vec - using arithb.c would be nice here */ + vec - ((2 * DotVecs(vec, mirror)) * mirror) +*/ 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; + float reflect[3] = {0.0f, 0.0f, 0.0f}; if (!VectorObject_Check(value)) { PyErr_SetString( PyExc_TypeError, "vec.reflect(value): expected a vector argument" ); @@ -302,27 +297,13 @@ PyObject *Vector_Reflect( VectorObject * self, PyObject * value ) 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]; - + + VecReflect(reflect, vec, mirror); + return newVectorObject(reflect, self->size, Py_NEW); }