00001 #include <math.h>
00002
00003 template <unsigned int n>
00004 const double Vector<n>::NORMAL_EPSILON = 1e-6;
00005
00006 template <unsigned int n>
00007 const double Object<n>::INTERSECT_EPSILON = 1e-6;
00008
00009
00010
00011
00012
00013 template <unsigned int n>
00014 Point<n>::Point() {
00015 assert (n > 0);
00016 data(n,0) = 1.0;
00017 }
00018
00019 template <unsigned int n>
00020 Point<n>::Point (const double data[n]) {
00021 assert (n > 0);
00022 for (unsigned int i = 0; i < n; i ++)
00023 this->data(i,0) = data[i];
00024 this->data(n,0) = 1.0;
00025 }
00026
00027 template <unsigned int n>
00028 std::ostream& operator<< (std::ostream &os, const Point<n> &p) {
00029 os << "p<";
00030 for (unsigned int i = 0; i < n; i ++) {
00031 if (i > 0)
00032 os << ',';
00033 os << p[i];
00034 }
00035 os << '>';
00036 return os;
00037 }
00038
00039
00040
00041
00042
00043 template <unsigned int n>
00044 Vector<n>::Vector (const double data[n]) {
00045 assert (n > 0);
00046 for (unsigned int i = 0; i < n; i ++)
00047 this->data(i,0) = data[i];
00048 }
00049
00050 template <unsigned int n>
00051 double Vector<n>::getLength() const {
00052 double lensq = 0.0;
00053 for (unsigned int i = 0; i < n; i ++)
00054 lensq += data(i,0)*data(i,0);
00055 return sqrt(lensq);
00056 }
00057
00058 template <unsigned int n>
00059 void Vector<n>::normalize() {
00060 double len = getLength();
00061 assert (len > 0.0);
00062 for (unsigned int i = 0; i < n; i ++)
00063 data(i,0) /= len;
00064 }
00065
00066 template <unsigned int n>
00067 void Vector<n>::translateGL() const {
00068 assert (n <= 3);
00069 if (n == 1) {
00070 glTranslated (data(0,0), 0.0, 0.0);
00071 } else if (n == 2) {
00072 glTranslated (data(0,0), data(1,0), 0.0);
00073 } else {
00074 glTranslated (data(0,0), data(1,0), data(2,0));
00075 }
00076 }
00077
00078 template <unsigned int n>
00079 Vector<n> Vector<n>::operator-() const {
00080 Vector<n> v;
00081 for (unsigned int i = 0; i < n; i ++)
00082 v.data(i,0) = -data(i,0);
00083 return v;
00084 }
00085
00086 template <unsigned int n>
00087 double operator* (const Vector<n> &a, const Vector<n> &b) {
00088 double dot = 0.0;
00089 for (unsigned int i = 0; i < n; i ++)
00090 dot += a[i] * b[i];
00091 return dot;
00092 }
00093
00094 template <unsigned int n>
00095 std::ostream& operator<< (std::ostream &os, const Vector<n> &p) {
00096 os << "v<";
00097 for (unsigned int i = 0; i < n; i ++) {
00098 if (i > 0)
00099 os << ',';
00100 os << p[i];
00101 }
00102 os << '>';
00103 return os;
00104 }
00105
00106 template <unsigned int n>
00107 Vector<n> operator- (const Point<n> &a, const Point<n> &b) {
00108 Vector<n> v;
00109 for (unsigned int i = 0; i < n; i ++)
00110 v.data(i,0) = a[i] - b[i];
00111 return v;
00112 }
00113
00114
00115
00116
00117
00118 template <unsigned int n>
00119 Ray<n>::Ray() {
00120 assert (n > 0);
00121 Matrix<n+1,1> vdata;
00122 for (unsigned int i = 0; i < n; i ++)
00123 vdata(i,0) = 1.0 / static_cast<double>(n);
00124 dir.setMatrix (vdata);
00125 }
00126
00127 template <unsigned int n>
00128 std::ostream& operator<< (std::ostream &os, const Ray<n> &r) {
00129 os << "ray(" << r.getStart() << ',' << r.getDirection() << ")";
00130 return os;
00131 }