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