00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef ARCHON_MATH_VECTOR_H
00021 #define ARCHON_MATH_VECTOR_H
00022
00023 #include <cmath>
00024 #include <algorithm>
00025 #include <iostream>
00026
00027 #include <archon/math/functions.H>
00028
00029 namespace Archon
00030 {
00031 namespace Math
00032 {
00033 template<typename T, int N>
00034 struct BasicVector
00035 {
00036 typedef T ElementType;
00037 static const unsigned size = N;
00038
00039 BasicVector() {}
00040 BasicVector(const T v[]) { set(v); }
00041
00042 static const BasicVector &zero();
00043
00044 T &operator[](int i) { return a[i]; }
00045 const T &operator[](int i) const { return a[i]; }
00046
00047 T squareSum() const;
00048 T length() const { return sqrt(squareSum()); }
00049 T min() const;
00050 T max() const;
00051
00052 BasicVector operator+(const BasicVector &) const;
00053 BasicVector operator-(const BasicVector &) const;
00054 BasicVector operator*(T) const;
00055 BasicVector operator/(T) const;
00056 BasicVector operator-() const;
00057
00058 BasicVector &operator+=(const BasicVector &);
00059 BasicVector &operator-=(const BasicVector &);
00060 BasicVector &operator*=(T);
00061 BasicVector &operator/=(T);
00062
00063 bool operator==(const BasicVector &) const;
00064 bool operator!=(const BasicVector &) const;
00065
00066 void set(const T []);
00067 void negate();
00068 void normalize() { *this /= length(); }
00069
00070 private:
00071 T a[N];
00072
00073 static BasicVector makeZero();
00074 };
00075
00076 template<typename T>
00077 struct BasicVector<T, 2>
00078 {
00079 BasicVector() {}
00080 BasicVector(const T v[]) { set(v); }
00081
00082 static const BasicVector &zero();
00083
00084 T &operator[](int i) { return a[i]; }
00085 const T &operator[](int i) const { return a[i]; }
00086
00087 T squareSum() const;
00088 T length() const { return sqrt(squareSum()); }
00089 T min() const;
00090 T max() const;
00091
00092 BasicVector operator+(const BasicVector &) const;
00093 BasicVector operator-(const BasicVector &) const;
00094 BasicVector operator*(T) const;
00095 BasicVector operator/(T) const;
00096 BasicVector operator-() const;
00097
00098 BasicVector &operator+=(const BasicVector &);
00099 BasicVector &operator-=(const BasicVector &);
00100 BasicVector &operator*=(T);
00101 BasicVector &operator/=(T);
00102
00103 bool operator==(const BasicVector &) const;
00104 bool operator!=(const BasicVector &) const;
00105
00106 void set(const T []);
00107 void negate();
00108 void normalize() { *this /= length(); }
00109
00110
00111
00112
00113 void set(T v0, T v1) { a[0] = v0; a[1] = v1; }
00114
00115 BasicVector(T v0, T v1) { set(v0, v1); }
00116 explicit BasicVector(const BasicVector<T, 3> &v) { set(v[0], v[1]); }
00117
00118
00119 T angle() const { return angle(a[0], a[1]); }
00120
00121
00122 BasicVector perp() const { return BasicVector(-a[1], a[0]); }
00123
00124 private:
00125 T a[2];
00126
00127 static BasicVector makeZero();
00128 };
00129
00130 template<typename T>
00131 struct BasicVector<T, 3>
00132 {
00133 BasicVector() {}
00134 BasicVector(const T v[]) { set(v); }
00135
00136 static const BasicVector &zero();
00137
00138 T &operator[](int i) { return a[i]; }
00139 const T &operator[](int i) const { return a[i]; }
00140
00141 T squareSum() const;
00142 T length() const { return sqrt(squareSum()); }
00143 T min() const;
00144 T max() const;
00145
00146 BasicVector operator+(const BasicVector &) const;
00147 BasicVector operator-(const BasicVector &) const;
00148 BasicVector operator*(T) const;
00149 BasicVector operator/(T) const;
00150 BasicVector operator-() const;
00151
00152 BasicVector &operator+=(const BasicVector &);
00153 BasicVector &operator-=(const BasicVector &);
00154 BasicVector &operator*=(T);
00155 BasicVector &operator/=(T);
00156
00157 bool operator==(const BasicVector &) const;
00158 bool operator!=(const BasicVector &) const;
00159
00160 void set(const T []);
00161 void negate();
00162 void normalize() { *this /= length(); }
00163
00164
00165
00166
00167 void set(T v0, T v1, T v2) { a[0] = v0; a[1] = v1; a[2] = v2; }
00168
00169 BasicVector(T v0, T v1, T v2) { set(v0, v1, v2); }
00170 explicit BasicVector(const BasicVector<T, 2> &v) { set(v[0], v[1], 0); }
00171
00175 BasicVector &operator*=(const BasicVector &v)
00176 {
00177 set(a[1]*v[2] - a[2]*v[1], a[2]*v[0] - a[0]*v[2], a[0]*v[1] - a[1]*v[0]);
00178 return *this;
00179 }
00180
00184 BasicVector operator*(const BasicVector &v) const
00185 {
00186 BasicVector w(*this);
00187 return w *= v;
00188 }
00189
00190 private:
00191 T a[3];
00192
00193 static BasicVector makeZero();
00194 };
00195
00196
00197
00198
00199 template<typename T, int N>
00200 BasicVector<T, N> BasicVector<T, N>::makeZero()
00201 {
00202 BasicVector z;
00203 for(int i=0; i<N; ++i) z[i] = 0;
00204 return z;
00205 }
00206
00207 template<typename T, int N>
00208 inline const BasicVector<T, N> &BasicVector<T, N>::zero()
00209 {
00210 static BasicVector z = makeZero();
00211 return z;
00212 }
00213
00214 template<typename T, int N>
00215 inline BasicVector<T, N> operator*(T f, const BasicVector<T, N> &v)
00216 {
00217 BasicVector<T, N> w = v; return w *= f;
00218 }
00219
00220 template<typename T, int N>
00221 inline BasicVector<T, N> normalize(const BasicVector<T, N> &v)
00222 {
00223 BasicVector<T, N> w = v; w.normalize(); return w;
00224 }
00225
00226 template<typename T, int N>
00227 inline T squareDist(BasicVector<T, N> v, const BasicVector<T, N> &w)
00228 {
00229 v -= w;
00230 return v.squareSum();
00231 }
00232
00233 template<typename T, int N>
00234 inline T dist(BasicVector<T, N> v, const BasicVector<T, N> &w)
00235 {
00236 return sqrt(squareDist(v, w));
00237 }
00238
00242 template<typename T, int N>
00243 inline T dot(const BasicVector<T, N> &v, const BasicVector<T, N> &w)
00244 {
00245 T s = 0;
00246 for(int i=0; i<N; ++i) s += v[i] * w[i];
00247 return s;
00248 }
00249
00250 template<typename T, int N>
00251 std::ostream &operator<<(std::ostream &o, const BasicVector<T, N> &v)
00252 {
00253 o << "[";
00254 if(N > 0)
00255 {
00256 o << v[0];
00257 for(int i=1; i<N; ++i) o << ", " << v[i];
00258 }
00259 o << "]";
00260 return o;
00261 }
00262
00263 template<typename T, int N>
00264 inline T BasicVector<T, N>::squareSum() const
00265 {
00266 T s = 0;
00267 for(int i=0; i<N; ++i) s += sq(a[i]);
00268 return s;
00269 }
00270
00271 template<typename T, int N>
00272 inline T BasicVector<T, N>::min() const
00273 {
00274 T v = a[0];
00275 for(int i=1; i<N; ++i) if(a[i] < v) v = a[i];
00276 return v;
00277 }
00278
00279 template<typename T, int N>
00280 inline T BasicVector<T, N>::max() const
00281 {
00282 T v = a[0];
00283 for(int i=1; i<N; ++i) if(a[i] > v) v = a[i];
00284 return v;
00285 }
00286
00287 template<typename T, int N>
00288 inline BasicVector<T, N> BasicVector<T, N>::operator+(const BasicVector<T, N> &v) const
00289 {
00290 BasicVector w(*this);
00291 return w += v;
00292 }
00293
00294 template<typename T, int N>
00295 inline BasicVector<T, N> BasicVector<T, N>::operator-(const BasicVector<T, N> &v) const
00296 {
00297 BasicVector w(*this);
00298 return w -= v;
00299 }
00300
00301 template<typename T, int N>
00302 inline BasicVector<T, N> BasicVector<T, N>::operator*(T f) const
00303 {
00304 BasicVector v(*this);
00305 return v *= f;
00306 }
00307
00308 template<typename T, int N>
00309 inline BasicVector<T, N> BasicVector<T, N>::operator/(T f) const
00310 {
00311 BasicVector v(*this);
00312 return v /= f;
00313 }
00314
00315 template<typename T, int N>
00316 inline BasicVector<T, N> BasicVector<T, N>::operator-() const
00317 {
00318 BasicVector v(*this);
00319 v.negate();
00320 return v;
00321 }
00322
00323 template<typename T, int N>
00324 inline BasicVector<T, N> &BasicVector<T, N>::operator+=(const BasicVector<T, N> &v)
00325 {
00326 for(int i=0; i<N; ++i) a[i] += v.a[i];
00327 return *this;
00328 }
00329
00330 template<typename T, int N>
00331 inline BasicVector<T, N> &BasicVector<T, N>::operator-=(const BasicVector<T, N> &v)
00332 {
00333 for(int i=0; i<N; ++i) a[i] -= v.a[i];
00334 return *this;
00335 }
00336
00337 template<typename T, int N>
00338 inline BasicVector<T, N> &BasicVector<T, N>::operator*=(T f)
00339 {
00340 for(int i=0; i<N; ++i) a[i] *= f;
00341 return *this;
00342 }
00343
00344 template<typename T, int N>
00345 inline BasicVector<T, N> &BasicVector<T, N>::operator/=(T f)
00346 {
00347 for(int i=0; i<N; ++i) a[i] /= f;
00348 return *this;
00349 }
00350
00351 template<typename T, int N>
00352 inline bool BasicVector<T, N>::operator==(const BasicVector<T, N> &v) const
00353 {
00354 for(int i=0; i<N; ++i) if(a[i] != v.a[i]) return false;
00355 return true;
00356 }
00357
00358 template<typename T, int N>
00359 inline bool BasicVector<T, N>::operator!=(const BasicVector<T, N> &v) const
00360 {
00361 for(int i=0; i<N; ++i) if(a[i] != v.a[i]) return true;
00362 return false;
00363 }
00364
00365 template<typename T, int N>
00366 inline void BasicVector<T, N>::set(const T v[])
00367 {
00368 for(int i=0; i<N; ++i) a[i] = v[i];
00369 }
00370
00371 template<typename T, int N>
00372 inline void BasicVector<T, N>::negate()
00373 {
00374 for(int i=0; i<N; ++i) a[i] = -a[i];
00375 }
00376
00377
00378
00379
00380 template<typename T>
00381 BasicVector<T, 2> BasicVector<T, 2>::makeZero()
00382 {
00383 BasicVector z;
00384 for(int i=0; i<2; ++i) z[i] = 0;
00385 return z;
00386 }
00387
00388 template<typename T>
00389 inline const BasicVector<T, 2> &BasicVector<T, 2>::zero()
00390 {
00391 static BasicVector z = makeZero();
00392 return z;
00393 }
00394
00395 template<typename T>
00396 inline BasicVector<T, 2> operator*(T f, const BasicVector<T, 2> &v)
00397 {
00398 BasicVector<T, 2> w = v; return w *= f;
00399 }
00400
00401 template<typename T>
00402 inline BasicVector<T, 2> normalize(const BasicVector<T, 2> &v)
00403 {
00404 BasicVector<T, 2> w = v; w.normalize(); return w;
00405 }
00406
00407 template<typename T>
00408 inline T squareDist(BasicVector<T, 2> v, const BasicVector<T, 2> &w)
00409 {
00410 v -= w;
00411 return v.squareSum();
00412 }
00413
00414 template<typename T>
00415 inline T dist(BasicVector<T, 2> v, const BasicVector<T, 2> &w)
00416 {
00417 return sqrt(squareDist(v, w));
00418 }
00419
00423 template<typename T>
00424 inline T dot(const BasicVector<T, 2> &v, const BasicVector<T, 2> &w)
00425 {
00426 T s = 0;
00427 for(int i=0; i<2; ++i) s += v[i] * w[i];
00428 return s;
00429 }
00430
00431 template<typename T>
00432 std::ostream &operator<<(std::ostream &o, const BasicVector<T, 2> &v)
00433 {
00434 o << "[";
00435 if(2 > 0)
00436 {
00437 o << v[0];
00438 for(int i=1; i<2; ++i) o << ", " << v[i];
00439 }
00440 o << "]";
00441 return o;
00442 }
00443
00444 template<typename T>
00445 inline T BasicVector<T, 2>::squareSum() const
00446 {
00447 T s = 0;
00448 for(int i=0; i<2; ++i) s += sq(a[i]);
00449 return s;
00450 }
00451
00452 template<typename T>
00453 inline T BasicVector<T, 2>::min() const
00454 {
00455 return std::min(a[0], a[1]);
00456 }
00457
00458 template<typename T>
00459 inline T BasicVector<T, 2>::max() const
00460 {
00461 return std::max(a[0], a[1]);
00462 }
00463
00464 template<typename T>
00465 inline BasicVector<T, 2> BasicVector<T, 2>::operator+(const BasicVector<T, 2> &v) const
00466 {
00467 BasicVector w(*this);
00468 return w += v;
00469 }
00470
00471 template<typename T>
00472 inline BasicVector<T, 2> BasicVector<T, 2>::operator-(const BasicVector<T, 2> &v) const
00473 {
00474 BasicVector w(*this);
00475 return w -= v;
00476 }
00477
00478 template<typename T>
00479 inline BasicVector<T, 2> BasicVector<T, 2>::operator*(T f) const
00480 {
00481 BasicVector v(*this);
00482 return v *= f;
00483 }
00484
00485 template<typename T>
00486 inline BasicVector<T, 2> BasicVector<T, 2>::operator/(T f) const
00487 {
00488 BasicVector v(*this);
00489 return v /= f;
00490 }
00491
00492 template<typename T>
00493 inline BasicVector<T, 2> BasicVector<T, 2>::operator-() const
00494 {
00495 BasicVector v(*this);
00496 v.negate();
00497 return v;
00498 }
00499
00500 template<typename T>
00501 inline BasicVector<T, 2> &BasicVector<T, 2>::operator+=(const BasicVector<T, 2> &v)
00502 {
00503 for(int i=0; i<2; ++i) a[i] += v.a[i];
00504 return *this;
00505 }
00506
00507 template<typename T>
00508 inline BasicVector<T, 2> &BasicVector<T, 2>::operator-=(const BasicVector<T, 2> &v)
00509 {
00510 for(int i=0; i<2; ++i) a[i] -= v.a[i];
00511 return *this;
00512 }
00513
00514 template<typename T>
00515 inline BasicVector<T, 2> &BasicVector<T, 2>::operator*=(T f)
00516 {
00517 for(int i=0; i<2; ++i) a[i] *= f;
00518 return *this;
00519 }
00520
00521 template<typename T>
00522 inline BasicVector<T, 2> &BasicVector<T, 2>::operator/=(T f)
00523 {
00524 for(int i=0; i<2; ++i) a[i] /= f;
00525 return *this;
00526 }
00527
00528 template<typename T>
00529 inline bool BasicVector<T, 2>::operator==(const BasicVector<T, 2> &v) const
00530 {
00531 for(int i=0; i<2; ++i) if(a[i] != v.a[i]) return false;
00532 return true;
00533 }
00534
00535 template<typename T>
00536 inline bool BasicVector<T, 2>::operator!=(const BasicVector<T, 2> &v) const
00537 {
00538 for(int i=0; i<2; ++i) if(a[i] != v.a[i]) return true;
00539 return false;
00540 }
00541
00542 template<typename T>
00543 inline void BasicVector<T, 2>::set(const T v[])
00544 {
00545 for(int i=0; i<2; ++i) a[i] = v[i];
00546 }
00547
00548 template<typename T>
00549 inline void BasicVector<T, 2>::negate()
00550 {
00551 for(int i=0; i<2; ++i) a[i] = -a[i];
00552 }
00553
00554
00555
00556
00557 template<typename T>
00558 BasicVector<T, 3> BasicVector<T, 3>::makeZero()
00559 {
00560 BasicVector z;
00561 for(int i=0; i<3; ++i) z[i] = 0;
00562 return z;
00563 }
00564
00565 template<typename T>
00566 inline const BasicVector<T, 3> &BasicVector<T, 3>::zero()
00567 {
00568 static BasicVector z = makeZero();
00569 return z;
00570 }
00571
00572 template<typename T>
00573 inline BasicVector<T, 3> operator*(T f, const BasicVector<T, 3> &v)
00574 {
00575 BasicVector<T, 3> w = v; return w *= f;
00576 }
00577
00578 template<typename T>
00579 inline BasicVector<T, 3> normalize(const BasicVector<T, 3> &v)
00580 {
00581 BasicVector<T, 3> w = v; w.normalize(); return w;
00582 }
00583
00584 template<typename T>
00585 inline T squareDist(BasicVector<T, 3> v, const BasicVector<T, 3> &w)
00586 {
00587 v -= w;
00588 return v.squareSum();
00589 }
00590
00591 template<typename T>
00592 inline T dist(BasicVector<T, 3> v, const BasicVector<T, 3> &w)
00593 {
00594 return sqrt(squareDist(v, w));
00595 }
00596
00600 template<typename T>
00601 inline T dot(const BasicVector<T, 3> &v, const BasicVector<T, 3> &w)
00602 {
00603 T s = 0;
00604 for(int i=0; i<3; ++i) s += v[i] * w[i];
00605 return s;
00606 }
00607
00608 template<typename T>
00609 std::ostream &operator<<(std::ostream &o, const BasicVector<T, 3> &v)
00610 {
00611 o << "[";
00612 if(3 > 0)
00613 {
00614 o << v[0];
00615 for(int i=1; i<3; ++i) o << ", " << v[i];
00616 }
00617 o << "]";
00618 return o;
00619 }
00620
00621 template<typename T>
00622 inline T BasicVector<T, 3>::squareSum() const
00623 {
00624 T s = 0;
00625 for(int i=0; i<3; ++i) s += sq(a[i]);
00626 return s;
00627 }
00628
00629 template<typename T>
00630 inline T BasicVector<T, 3>::min() const
00631 {
00632 return std::min(std::min(a[0], a[1]), a[2]);
00633 }
00634
00635 template<typename T>
00636 inline T BasicVector<T, 3>::max() const
00637 {
00638 return std::max(std::max(a[0], a[1]), a[2]);
00639 }
00640
00641 template<typename T>
00642 inline BasicVector<T, 3> BasicVector<T, 3>::operator+(const BasicVector<T, 3> &v) const
00643 {
00644 BasicVector w(*this);
00645 return w += v;
00646 }
00647
00648 template<typename T>
00649 inline BasicVector<T, 3> BasicVector<T, 3>::operator-(const BasicVector<T, 3> &v) const
00650 {
00651 BasicVector w(*this);
00652 return w -= v;
00653 }
00654
00655 template<typename T>
00656 inline BasicVector<T, 3> BasicVector<T, 3>::operator*(T f) const
00657 {
00658 BasicVector v(*this);
00659 return v *= f;
00660 }
00661
00662 template<typename T>
00663 inline BasicVector<T, 3> BasicVector<T, 3>::operator/(T f) const
00664 {
00665 BasicVector v(*this);
00666 return v /= f;
00667 }
00668
00669 template<typename T>
00670 inline BasicVector<T, 3> BasicVector<T, 3>::operator-() const
00671 {
00672 BasicVector v(*this);
00673 v.negate();
00674 return v;
00675 }
00676
00677 template<typename T>
00678 inline BasicVector<T, 3> &BasicVector<T, 3>::operator+=(const BasicVector<T, 3> &v)
00679 {
00680 for(int i=0; i<3; ++i) a[i] += v.a[i];
00681 return *this;
00682 }
00683
00684 template<typename T>
00685 inline BasicVector<T, 3> &BasicVector<T, 3>::operator-=(const BasicVector<T, 3> &v)
00686 {
00687 for(int i=0; i<3; ++i) a[i] -= v.a[i];
00688 return *this;
00689 }
00690
00691 template<typename T>
00692 inline BasicVector<T, 3> &BasicVector<T, 3>::operator*=(T f)
00693 {
00694 for(int i=0; i<3; ++i) a[i] *= f;
00695 return *this;
00696 }
00697
00698 template<typename T>
00699 inline BasicVector<T, 3> &BasicVector<T, 3>::operator/=(T f)
00700 {
00701 for(int i=0; i<3; ++i) a[i] /= f;
00702 return *this;
00703 }
00704
00705 template<typename T>
00706 inline bool BasicVector<T, 3>::operator==(const BasicVector<T, 3> &v) const
00707 {
00708 for(int i=0; i<3; ++i) if(a[i] != v.a[i]) return false;
00709 return true;
00710 }
00711
00712 template<typename T>
00713 inline bool BasicVector<T, 3>::operator!=(const BasicVector<T, 3> &v) const
00714 {
00715 for(int i=0; i<3; ++i) if(a[i] != v.a[i]) return true;
00716 return false;
00717 }
00718
00719 template<typename T>
00720 inline void BasicVector<T, 3>::set(const T v[])
00721 {
00722 for(int i=0; i<3; ++i) a[i] = v[i];
00723 }
00724
00725 template<typename T>
00726 inline void BasicVector<T, 3>::negate()
00727 {
00728 for(int i=0; i<3; ++i) a[i] = -a[i];
00729 }
00730
00731
00732
00733 typedef BasicVector<double, 2> Vector2;
00734 typedef BasicVector<double, 3> Vector3;
00735 typedef BasicVector<double, 4> Vector4;
00736 }
00737 }
00738
00739 #endif // ARCHON_MATH_VECTOR_H