sources:
BOX.CPP (3.1k)
BOX.H (507 bytes)
RAY.CPP (609 bytes)
RAY.H (604 bytes)
SPHERE.CPP (1.9k)
SPHERE.H (564 bytes)
TRIANGLE.CPP (2.1k)
TRIANGLE.H (1.2k)
UTIL.H (1.7k)
VECTOR.CPP (1.6k)
VECTOR.H (5.0k)
cgapplication.cpp (3.2k)
cgapplication.h (3.5k)
cgraytracer.cpp (18.8k)
cgraytracer.h (1.6k)
shape.h (754 bytes)


binaries:
Release/raytracer.exe (38.0k)


website:
more info here


screenshot:
studies/grafik2/Computergrafik-Code6/Aufgabe17/VECTOR.H
download file

  1 #ifndef _VECTOR_H
  2 #define _VECTOR_H
  3
  4 #include <assert.h>
  5 #include <iostream.h>
  6 #include <math.h>
  7
  8 //- Vector
  9 class Vector {
 10 //. 3D Vector Class.
 11 public:
 12     //- Vector
 13     Vector();
 14     Vector(double x, double y, double z);
 15
 16     //- operator==, operator!=, operator<
 17     bool operator==(const Vector&) const;
 18     bool operator!=(const Vector&) const;
 19     bool operator<(const Vector&) const;
 20         //. The comparison is based on the lexiographic order.
 21
 22     //- operator[]
 23     double& operator[](int i);
 24     double operator[](int i) const;
 25         //. Indices 0,1, and 2 correspond to the x,y,z coordinates
 26         //. of the vector.
 27         //. CAUTION: For performance reasons, indices are not checked.
 28
 29     //- operator+=, operator-=, operator*=
 30     Vector& operator+=(const Vector&);
 31     Vector& operator-=(const Vector&);
 32     Vector& operator*=(const Vector&);
 33     Vector& operator*=(double);
 34
 35     //- operator+, operator-, operator*, operator/, operator-, operator*
 36     Vector operator+(const Vector&) const;
 37     Vector operator-(const Vector&) const;
 38     Vector operator*(const Vector&) const;
 39     Vector operator/(double) const;
 40     Vector operator-() const;
 41     Vector operator*(double) const;
 42     friend inline Vector operator*(double, const Vector&);
 43
 44     //- normalized, normalize
 45     Vector normalized() const;
 46     bool normalize();
 47         //. `normalized' returns a normalized copy of the vector object.
 48         //. `normalize' normalizes the vector object. If the length
 49         //. of the vector was > 0, it returns 1 for success, otherwise 0.
 50
 51     //- abs, abs2, dotProduct
 52     friend inline double abs(const Vector&);
 53     friend inline double abs2(const Vector&);
 54     friend inline double dotProduct(const Vector&, const Vector&);
 55         //. Vector length and scalar product.
 56
 57     //- makeCWTriangleNormal, makeCCWTriangleNormal
 58     friend inline Vector makeCWTriangleNormal (
 59         const Vector& p0, const Vector& p1, const Vector& p2
 60     )
;
 61     friend inline Vector makeCCWTriangleNormal (
 62         const Vector& p0, const Vector& p1, const Vector& p2
 63     )
;
 64         //. Computes ((p0-p1)*(p1-p2)).normalized, i.e. a vector that is
 65         //. perpendicular the the plane defined by the three vertices.
 66
 67     //- operator<<, operator>>
 68     friend  ostream& operator<<(ostream&, const Vector&);
 69     friend  istream& operator>>(istream&, Vector&);
 70
 71     //- rep
 72     double* rep() const;
 73         //. Direct data access. It is guaranteed that the
 74         //. vector components are stored continuously.
 75    
 76
 77 private:
 78     double v_[3];
 79 };
 80
 81 inline Vector::Vector() {
 82     v_[0] = v_[1] = v_[2] = 0;
 83 }
 84
 85 inline Vector::Vector(double x, double y, double z) {
 86     v_[0] = x; v_[1] = y; v_[2] = z;
 87 }
 88
 89 inline double Vector::operator[](int i) const { return v_[i]; }
 90
 91 inline double& Vector::operator[](int i) { return v_[i]; }
 92
 93 inline bool Vector::operator==(const Vector& u) const {
 94     return(v_[0]==u.v_[0] && v_[1]==u.v_[1] && v_[2]==u.v_[2]);
 95 }
 96
 97 inline bool Vector::operator!=(const Vector& u) const {
 98     return(v_[0]!=u.v_[0] || v_[1]!=u.v_[1] || v_[2]!=u.v_[2]);
 99 }
100
101 inline Vector& Vector::operator+=(const Vector& u) {
102     v_[0] += u.v_[0];
103     v_[1] += u.v_[1];
104     v_[2] += u.v_[2];
105     return *this;
106 }
107
108 inline Vector& Vector::operator-=(const Vector& u) {
109     v_[0] -= u.v_[0];
110     v_[1] -= u.v_[1];
111     v_[2] -= u.v_[2];
112     return *this;
113 }
114
115 inline Vector& Vector::operator*=(double t) {
116     v_[0] *= t;
117     v_[1] *= t;
118     v_[2] *= t;
119     return *this;
120 }
121
122 inline Vector Vector::operator+(const Vector& u) const {
123     return Vector(v_[0]+u.v_[0],v_[1]+u.v_[1],v_[2]+u.v_[2]);
124 }
125
126 inline Vector Vector::operator-(const Vector& u) const {
127     return Vector(v_[0]-u.v_[0],v_[1]-u.v_[1],v_[2]-u.v_[2]);
128 }
129
130 inline Vector Vector::operator*(const Vector& w) const {
131     return Vector(v_[1]*w.v_[2]-v_[2]*w.v_[1],
132                    v_[2]*w.v_[0]-v_[0]*w.v_[2],
133                    v_[0]*w.v_[1]-v_[1]*w.v_[0])
;
134 }
135
136 inline Vector Vector::operator/(double s) const {
137     return Vector(v_[0]/s, v_[1]/s, v_[2]/s);
138 }
139
140 inline Vector Vector::operator-() const {
141     return Vector(-v_[0], -v_[1], -v_[2]);
142 }
143
144 inline Vector Vector::operator*(double f) const {
145     return Vector(v_[0]*f, v_[1]*f, v_[2]*f);
146 }
147
148 inline Vector operator*(double s, const Vector& u) {
149     return Vector(u[0]*s, u[1]*s, u[2]*s);
150 }
151
152 inline double dotProduct(const Vector& u1, const Vector& u2) {
153     return(u1[0]*u2[0] + u1[1]*u2[1] + u1[2]*u2[2]);
154 }
155
156 inline double abs(const Vector& u) { return(sqrt(dotProduct(u,u))); }
157 inline double abs2(const Vector& u) { return dotProduct(u,u); }
158
159 inline Vector makeCWTriangleNormal(const Vector& p0, const Vector& p1, const Vector& p2) {
160     return(((p0 - p1) * (p1 - p2)).normalized());
161 }
162
163 inline Vector makeCCWTriangleNormal(const Vector& p0, const Vector& p1, const Vector& p2) {
164     return(makeCWTriangleNormal(p2, p1, p0));
165 }
166
167 inline double* Vector::rep() const {
168     return (double*)(&(v_[0]));
169 }
170
171 #endif // _VECTOR_H
172