00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <blort/TomGine/tgVector3.h>
00011 #include <blort/TomGine/tgMatrix3.h>
00012
00013 using namespace TomGine;
00014
00015 tgVector3::tgVector3()
00016 :x(0), y(0), z(0)
00017 {
00018
00019 }
00020
00021 tgVector3::tgVector3(float all)
00022 :x(all), y(all), z(all)
00023 {
00024
00025
00026 }
00027
00028 tgVector3::tgVector3(float xn, float yn, float zn)
00029 {
00030 x = xn; y = yn; z = zn;
00031 }
00032
00033 tgVector3 tgVector3::operator=(tgVector3 v){
00034 x = v.x;
00035 y = v.y;
00036 z = v.z;
00037 return (*this);
00038 }
00039
00040 tgVector3 tgVector3::operator+(tgVector3 v)
00041 {
00042 return tgVector3(x+v.x, y+v.y, z+v.z);
00043 }
00044
00045 tgVector3 tgVector3::operator-(tgVector3 v)
00046 {
00047 return tgVector3(x-v.x, y-v.y, z-v.z);
00048 }
00049
00050 tgVector3 tgVector3::operator*(tgVector3 v)
00051 {
00052 return tgVector3(x*v.x, y*v.y, z*v.z);
00053 }
00054
00055 tgVector3 tgVector3::operator*(float m)
00056 {
00057 return tgVector3(x*m, y*m, z*m);
00058 }
00059
00060 tgVector3 tgVector3::operator/(float m)
00061 {
00062 return tgVector3(x/m, y/m, z/m);
00063 }
00064
00065 tgVector3 tgVector3::cross(tgVector3 v)
00066 {
00067 return tgVector3( y*v.z - z*v.y,
00068 z*v.x - x*v.z,
00069 x*v.y - y*v.x);
00070 }
00071
00072 tgVector3 tgVector3::cross(tgVector3 v1, tgVector3 v2)
00073 {
00074 return tgVector3( v1.y*v2.z - v1.z*v2.y,
00075 v1.z*v2.x - v1.x*v2.z,
00076 v1.x*v2.y - v1.y*v2.x);
00077 }
00078
00079 float tgVector3::dot(tgVector3 v)
00080 {
00081 return (x*v.x + y*v.y + z*v.z);
00082 }
00083
00084 void tgVector3::normalize()
00085 {
00086 float s = sqrt(x*x + y*y + z*z);
00087 if(s != 0.0f)
00088 {
00089 x /= s;
00090 y /= s;
00091 z /= s;
00092 }
00093 }
00094
00095 float tgVector3::length()
00096 {
00097 return sqrt(x*x + y*y + z*z);
00098 }
00099
00100 void tgVector3::setLength(float l)
00101 {
00102 normalize();
00103 x *= l;
00104 y *= l;
00105 z *= l;
00106 }
00107
00108 void tgVector3::rotateX(float fAngle)
00109 {
00110 float tz = z;
00111 float ty = y;
00112 y = ty * cos(fAngle) - tz * sin(fAngle);
00113 z = ty * sin(fAngle) + tz * cos(fAngle);
00114 }
00115
00116 void tgVector3::rotateY(float fAngle)
00117 {
00118 float tz = z;
00119 float tx = x;
00120 z = tz * cos(fAngle) - tx * sin(fAngle);
00121 x = tz * sin(fAngle) + tx * cos(fAngle);
00122 }
00123
00124 void tgVector3::rotateZ(float fAngle)
00125 {
00126 float tx = x;
00127 float ty = y;
00128 x = tx * cos(fAngle) - ty * sin(fAngle);
00129 y = tx * sin(fAngle) + ty * cos(fAngle);
00130 }
00131
00132 void tgVector3::rotate(float alpha, tgVector3 r) {
00133
00134 tgVector3 v,s,t,n;
00135 tgMatrix3 M,Mt,Rx,X;
00136
00137 if(alpha != 0){
00138
00139 r.normalize();
00140 s = r.cross(tgVector3(1,0,0));
00141
00142 if(s.length() < 0.001f)
00143 s = r.cross(tgVector3(0,1,0));
00144
00145 s.normalize();
00146 t = r.cross(s);
00147
00148 Mt = tgMatrix3(r,s,t);
00149 M = tgMatrix3(Mt);
00150 M.transpose();
00151
00152 Rx = tgMatrix3( 1.0f, 0.0f, 0.0f,
00153 0.0f, cos(alpha), -sin(alpha),
00154 0.0f, sin(alpha), cos(alpha));
00155
00156 X=Mt*Rx*M;
00157
00158 v = tgVector3(x,y,z);
00159 n = X*v;
00160
00161 x = n.x;
00162 y = n.y;
00163 z = n.z;
00164 }
00165 }
00166
00167 float Angle(tgVector3 a, tgVector3 b)
00168 {
00169 float d = a.dot(b)/(a.length() * b.length());
00170 return acos(d);
00171 }
00172
00173
00174