00001
00002 #include <stdio.h>
00003 #include <math.h>
00004 #include <assert.h>
00005
00006 #include "vector.h"
00007
00008 float sqr(float a) {return a*a;}
00009
00010
00011
00012 float magnitude(Vector v) {
00013 return float(sqrt(sqr(v.x) + sqr( v.y)+ sqr(v.z)));
00014 }
00015 Vector normalize(Vector v) {
00016 float d=magnitude(v);
00017 if (d==0) {
00018 printf("Cant normalize ZERO vector\n");
00019 assert(0);
00020 d=0.1f;
00021 }
00022 v.x/=d;
00023 v.y/=d;
00024 v.z/=d;
00025 return v;
00026 }
00027
00028 Vector operator+(Vector v1,Vector v2) {return Vector(v1.x+v2.x,v1.y+v2.y,v1.z+v2.z);}
00029 Vector operator-(Vector v1,Vector v2) {return Vector(v1.x-v2.x,v1.y-v2.y,v1.z-v2.z);}
00030 Vector operator-(Vector v) {return Vector(-v.x,-v.y,-v.z);}
00031 Vector operator*(Vector v1,float s) {return Vector(v1.x*s,v1.y*s,v1.z*s);}
00032 Vector operator*(float s, Vector v1) {return Vector(v1.x*s,v1.y*s,v1.z*s);}
00033 Vector operator/(Vector v1,float s) {return v1*(1.0f/s);}
00034 float operator^(Vector v1,Vector v2) {return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;}
00035 Vector operator*(Vector v1,Vector v2) {
00036 return Vector(
00037 v1.y * v2.z - v1.z*v2.y,
00038 v1.z * v2.x - v1.x*v2.z,
00039 v1.x * v2.y - v1.y*v2.x);
00040 }
00041 Vector planelineintersection(Vector n,float d,Vector p1,Vector p2){
00042
00043 Vector dif = p2-p1;
00044 float dn= n^dif;
00045 float t = -(d+(n^p1) )/dn;
00046 return p1 + (dif*t);
00047 }
00048 int concurrent(Vector a,Vector b) {
00049 return(a.x==b.x && a.y==b.y && a.z==b.z);
00050 }
00051
00052
00053
00054 matrix transpose(matrix m) {
00055 return matrix( Vector(m.x.x,m.y.x,m.z.x),
00056 Vector(m.x.y,m.y.y,m.z.y),
00057 Vector(m.x.z,m.y.z,m.z.z));
00058 }
00059 Vector operator*(matrix m,Vector v){
00060 m=transpose(m);
00061 return Vector(m.x^v,m.y^v,m.z^v);
00062 }
00063 matrix operator*(matrix m1,matrix m2){
00064 m1=transpose(m1);
00065 return matrix(m1*m2.x,m1*m2.y,m1*m2.z);
00066 }
00067
00068
00069 Quaternion operator*(Quaternion a,Quaternion b) {
00070 Quaternion c;
00071 c.r = a.r*b.r - a.x*b.x - a.y*b.y - a.z*b.z;
00072 c.x = a.r*b.x + a.x*b.r + a.y*b.z - a.z*b.y;
00073 c.y = a.r*b.y - a.x*b.z + a.y*b.r + a.z*b.x;
00074 c.z = a.r*b.z + a.x*b.y - a.y*b.x + a.z*b.r;
00075 return c;
00076 }
00077 Quaternion operator-(Quaternion q) {
00078 return Quaternion(q.r*-1,q.x,q.y,q.z);
00079 }
00080 Quaternion operator*(Quaternion a,float b) {
00081 return Quaternion(a.r*b, a.x*b, a.y*b, a.z*b);
00082 }
00083 Vector operator*(Quaternion q,Vector v) {
00084 return q.getmatrix() * v;
00085 }
00086 Vector operator*(Vector v,Quaternion q){
00087 assert(0);
00088 return Vector(0.0f,0.0f,0.0f);
00089 }
00090
00091 Quaternion operator+(Quaternion a,Quaternion b) {
00092 return Quaternion(a.r+b.r, a.x+b.x, a.y+b.y, a.z+b.z);
00093 }
00094 float operator^(Quaternion a,Quaternion b) {
00095 return (a.r*b.r + a.x*b.x + a.y*b.y + a.z*b.z);
00096 }
00097 Quaternion slerp(Quaternion a,Quaternion b,float interp){
00098 if((a^b) <0.0) {
00099 a.r=-a.r;
00100 a.x=-a.x;
00101 a.y=-a.y;
00102 a.z=-a.z;
00103 }
00104 float theta = float(acos(a^b));
00105 if(theta==0.0f) { return(a);}
00106 return a*float(sin(theta-interp*theta)/sin(theta)) + b*float(sin(interp*theta)/sin(theta));
00107 }
00108
00109
00110