00001 typedef struct{
00002 float x;
00003 float y;
00004 float z;
00005 } vector3;
00006
00007
00008
00009 vector3 InitV(float x, float y, float z)
00010 {
00011 vector3 v;
00012 v.x = x;
00013 v.y = y;
00014 v.z = z;
00015 return v;
00016 }
00017
00018
00019 vector3 InitV1(float phi, float cosTheta, float magnitude)
00020 {
00021 vector3 v;
00022 float sinTheta = sqrt(1.0-cosTheta*cosTheta);
00023 v.z = magnitude*cosTheta;
00024 v.y = magnitude*sinTheta*sin(phi);
00025 v.x = magnitude*sinTheta*cos(phi);
00026 return v;
00027 }
00028
00029
00030 float Mag(vector3 v)
00031 {
00032 return sqrt(v.x*v.x+v.y*v.y+v.z*v.z);
00033 }
00034
00035
00036 float Mag2(vector3 v)
00037 {
00038 return v.x*v.x+v.y*v.y+v.z*v.z;
00039 }
00040
00041
00042 float Dot(vector3 v1, vector3 v2)
00043 {
00044 return v1.x*v2.x+v1.y*v2.y+v1.z*v2.z;
00045 }
00046
00047
00048
00049 vector3 Cross(vector3 v1, vector3 v2)
00050 {
00051 vector3 v;
00052 v.x = v1.y*v2.z - v1.z*v2.y;
00053 v.y = v1.z*v2.x - v1.x*v2.z;
00054 v.z = v1.x*v2.y - v1.y*v2.x;
00055 return v;
00056 }
00057
00058
00059 vector3 Unit(vector3 v)
00060 {
00061 vector3 vv;
00062 float p = Mag(v);
00063 vv.x = v.x/p;
00064 vv.y = v.y/p;
00065 vv.z = v.z/p;
00066 return vv;
00067 }
00068
00069
00070
00071
00072 vector3 Intersection(float z0, vector3 vec, vector3 pos)
00073 {
00074 vector3 vz0;
00075 vz0.z = z0;
00076 vz0.x = (vz0.z - pos.z)*vec.x/vec.z + pos.x;
00077 vz0.y = (vz0.z - pos.z)*vec.y/vec.z + pos.y;
00078 return vz0;
00079 }
00080
00081
00082
00083 vector3 TimesA(float a, vector3 v)
00084 {
00085 vector3 vv;
00086 vv.x = a*v.x;
00087 vv.y = a*v.y;
00088 vv.z = a*v.z;
00089 return vv;
00090 }
00091
00092
00093 vector3 AddV(vector3 v1, vector3 v2)
00094 {
00095 vector3 v;
00096 v.x = v1.x + v2.x;
00097 v.y = v1.y + v2.y;
00098 v.z = v1.z + v2.z;
00099 return v;
00100 }
00101
00102
00103 vector3 SubV(vector3 v1, vector3 v2)
00104 {
00105 vector3 v;
00106 v.x = v1.x - v2.x;
00107 v.y = v1.y - v2.y;
00108 v.z = v1.z - v2.z;
00109 return v;
00110 }
00111
00112
00113 vector3 TransformFrom(vector3 v, vector3 ux, vector3 uy, vector3 uz)
00114 {
00115 ux = TimesA(v.x,ux);
00116 uy = TimesA(v.y,uy);
00117 uz = TimesA(v.z,uz);
00118 v = AddV(AddV(ux, uy), uz);
00119 return v;
00120 }
00121
00122
00123
00124 vector3 TransformTo(vector3 v, vector3 ux, vector3 uy, vector3 uz)
00125 {
00126 vector3 vv;
00127 vv.x = Dot(v,ux);
00128 vv.y = Dot(v,uy);
00129 vv.z = Dot(v,uz);
00130 return vv;
00131 }
00132
00133 const float pi = 3.1415926536;
00134 const float rad = 57.29578;
00135
00136 typedef struct{
00137 float r;
00138 float theta;
00139 float phi;
00140 } polar;
00141
00142 polar XYZ2Polar(vector3 v)
00143 {
00144 polar s;
00145 float rxy;
00146
00147 s.r = sqrt(v.x*v.x+v.y*v.y+v.z*v.z);
00148 if(s.r==0.0){
00149 s.theta = 0.0;
00150 }
00151 else{
00152 s.theta = acos(v.z/s.r);
00153 }
00154
00155 rxy = sqrt(v.x*v.x+v.y*v.y);
00156 if(rxy==0.0){
00157 s.phi = 0.0;
00158 }
00159 else{
00160 if(v.y>=0.0) s.phi = acos(v.x/rxy);
00161 else{
00162 s.phi = 2.0*pi-acos(v.x/rxy);
00163 }
00164 }
00165
00166 return s;
00167 }