00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
#ifndef VECTOR_H
00022
#define VECTOR_H
00023
00024
#include <cmath>
00025
#include <iostream>
00026
#include "types.h"
00027
00028
class Matrix;
00029
00033 class Vector3 {
00034
00035
public:
00036
00039 static Vector3 positiveInfinit() {
return Vector3(DBL_INFINITY);}
00040
00043 static Vector3 negativeInfinit() {
return Vector3(-DBL_INFINITY);}
00044
00045
public:
00046
00049 Vector3() {}
00050
00054 Vector3(DBL w) {
for(
int i=0; i<3; i++) a[i]=w;}
00055
00058 Vector3(DBL x, DBL y, DBL z) {
00059 a[0]=x; a[1]=y; a[2]=z;
00060 }
00061
00062
public:
00063
00064
00067 DBL&
operator[](
int i) {
return a[i];}
00068
00071 const DBL&
operator[](
int i)
const {
return a[i];}
00072
00075 void add(
const Vector3 &v) {
for(
int i=0; i<3; i++) a[i]+=v[i];}
00076
00079 void sub(
const Vector3 &v) {
for(
int i=0; i<3; i++) a[i]-=v[i];}
00080
00083 void scale(DBL s) {
for(
int i=0; i<3; i++) a[i]*=s;}
00084
00087 void scale(
const Vector3& v) {
for(
int i=0; i<3; i++) a[i]*=v[i];}
00088
00092 void normalize() {
scale(1.0/
length());}
00093
00096 void inverse() {
for(
int i=0; i<3; i++) a[i]=1/a[i];}
00097
00101
void applyFromLeft(
const Matrix &m);
00102
00106
void applyFromLeftTransposed(
const Matrix &m);
00107
00110
void rotate(
const Vector3& v);
00111
00114 void minimize(
const Vector3 &v) {
for(
int i=0; i<3; i++)
if (v[i]<a[i]) a[i]=v[i];}
00115
00118 void maximize(
const Vector3 &v) {
for(
int i=0; i<3; i++) if (v[i]>a[i]) a[i]=v[i];}
00119
00123 Vector3 scaled(DBL s)
const {
00124
Vector3 ret;
00125
for(
int i=0; i<3; i++) ret[i] = a[i]*s;
00126
return ret;
00127 }
00128
00132 double lengthSq()
const {
return a[0]*a[0]+a[1]*a[1]+a[2]*a[2];}
00133
00137 double length()
const {
return sqrt(
lengthSq());}
00138
00141 bool equals(
const Vector3& v)
const {
00142
for(
int i=0; i<3; i++)
00143
if(a[i]!=v[i])
return false;
00144
return true;
00145 }
00146
00149 unsigned int hashValue()
const {
00150
unsigned int res = 0;
00151
for(
int i=0; i<3; i++) {
00152
const long long* p = reinterpret_cast<const long long*>(&(a[i]));
00153
unsigned int val = ((*p)>>38) ^ ((*p)>>49);
00154 res = (res<<4) ^ val;
00155 }
00156
return res;
00157 }
00158
00159
00160
00163 bool operator<(
const Vector3& v)
const {
00164
for(
int i=0; i<3; i++) {
00165
if (a[i]<v[i])
00166
return true;
00167
else
00168
if (a[i]>v[i])
return false;
00169 }
00170
return false;
00171 }
00172
00175 bool operator==(
const Vector3& v)
const {
return equals(v);}
00176
00179 void operator+=(
const Vector3& v) {
add(v);}
00180
00183 void operator-=(
const Vector3& v) {
sub(v);}
00184
00187 void operator*=(DBL s) {
scale(s);}
00188
00189
public:
00190
00194 static DBL
dotProd(
const Vector3& v,
const Vector3 &w) {
00195 DBL ret = 0;
00196
for(
int i=0; i<3; i++) ret+=v[i]*w[i];
00197
return ret;
00198 }
00199
00203 static Vector3 crossProd(
const Vector3& u,
const Vector3& v) {
00204
Vector3 res;
00205 res[0] = u[1]*v[2] - u[2]*v[1];
00206 res[1] = u[2]*v[0] - u[0]*v[2];
00207 res[2] = u[0]*v[1] - u[1]*v[0];
00208
return res;
00209 }
00210
00213 static Vector3 add(
const Vector3& v,
const Vector3 &w) {
00214
Vector3 ret;
00215
for(
int i=0; i<3; i++) ret[i] = v[i]+w[i];
00216
return ret;
00217 }
00218
00221 static Vector3 sub(
const Vector3& v,
const Vector3 &w) {
00222
Vector3 ret;
00223
for(
int i=0; i<3; i++) ret[i] = v[i]-w[i];
00224
return ret;
00225 }
00226
00227
Vector3 operator*(DBL s)
const {
00228
return scaled(s);
00229 }
00230
00233 static DBL
distance(
const Vector3& v,
const Vector3 &w) {
00234
return sub(v,w).length();
00235 }
00236
00240 static Vector3 deg2rad(
const Vector3& v) {
00241
return v.
scaled(PI/180.0);
00242 }
00243
00247 static Vector3 rad2deg(
const Vector3& v) {
00248
return v.
scaled(180.0/PI);
00249 }
00250
00251
private:
00252 DBL a[3];
00253
00254 };
00255
00256
00257 std::ostream& operator<<(std::ostream&s,
const Vector3& v);
00258
00259
00260
00262 class Vector3f {
00263
00264
public:
00265
00266
00267
00269 Vector3f() {}
00270
00272 Vector3f(
const Vector3& v) {
00273
for(
int i=0; i<3; i++) a[i] = v[i];
00274 }
00275
00277 Vector3 toVector3()
const {
00278
Vector3 ret;
00279
for(
int i=0; i<3; i++) ret[i] =a[i];
00280
return ret;
00281 }
00282
00285 FLT&
operator[](
int i) {
return a[i];}
00286
00289 const FLT&
operator[](
int i)
const {
return a[i];}
00290
00294 bool equals(
const Vector3f& v)
const {
00295
for(
int i=0; i<3; i++)
00296
if(a[i]!=v[i])
return false;
00297
return true;
00298 }
00299
00302 unsigned int hashValue()
const {
00303
unsigned int res = 0;
00304
for(
int i=0; i<3; i++) {
00305
const unsigned int* p = reinterpret_cast<const unsigned int*>(&(a[i]));
00306
unsigned int val = ((*p)>>9) ^ ((*p)>>22);
00307 res = (res<<4) ^ val;
00308 }
00309
return res;
00310 }
00311
00312
private:
00313 FLT a[3];
00314
00315 };
00316
00317
#endif