C++ / OpenCL Raytracer
vector.h
1 #ifndef VECTOR_H
2 #define VECTOR_H
3 
4 #define _USE_MATH_DEFINES
5 #include <math.h>
6 #include <stdlib.h>
7 #include <cfloat>
8 #include <iostream>
9 #include <vector>
10 
11 #define drand48() ((double)rand()/RAND_MAX)
12 
16 class Vector3D {
17 public:
24  Vector3D() {}
31  Vector3D(double e0) { m_dE[0] = e0; m_dE[1] = e0; m_dE[2] = e0; }
38  Vector3D(double e0, double e1, double e2) { m_dE[0] = e0; m_dE[1] = e1; m_dE[2] = e2; }
39 
40  inline double x() const { return m_dE[0]; }
41  inline double y() const { return m_dE[1]; }
42  inline double z() const { return m_dE[2]; }
43  inline double r() const { return m_dE[0]; }
44  inline double g() const { return m_dE[1]; }
45  inline double b() const { return m_dE[2]; }
46 
47  inline const Vector3D &operator+() const { return *this; }
48  inline Vector3D operator-() const { return Vector3D(-m_dE[0], -m_dE[1], -m_dE[2]); }
49  inline double operator[](int i) const { return m_dE[i]; }
50  inline double &operator[](int i) { return m_dE[i]; }
51  inline Vector3D &operator+=(const Vector3D &v2);
52  inline Vector3D &operator-=(const Vector3D &v2);
53  inline Vector3D &operator*=(const Vector3D &v2);
54  inline Vector3D &operator/=(const Vector3D &v2);
55  inline Vector3D &operator*=(const double t);
56  inline Vector3D &operator/=(const double t);
57 
58  inline double Length() const { return sqrt(m_dE[0] * m_dE[0] + m_dE[1] * m_dE[1] + m_dE[2] * m_dE[2]); }
59  inline double SquaredLength() const { return m_dE[0] * m_dE[0] + m_dE[1] * m_dE[1] + m_dE[2] * m_dE[2]; }
60  inline double Dot(const Vector3D &v2) { return m_dE[0] * v2.m_dE[0] + m_dE[1] * v2.m_dE[1] + m_dE[2] * v2.m_dE[2]; }
61 
63  inline Vector3D Cross(const Vector3D &v2) {
64  return Vector3D((m_dE[1] * v2.m_dE[2] - m_dE[2] * v2.m_dE[1]),
65  (-(m_dE[0] * v2.m_dE[2] - m_dE[2] * v2.m_dE[0])),
66  (m_dE[0] * v2.m_dE[1] - m_dE[1] * v2.m_dE[0]));
67  }
68 
69  inline void MakeUnitVector();
70 
71  double m_dE[3];
72 };
73 
74 inline std::istream &operator>>(std::istream &is, Vector3D &t) {
75  is >> t.m_dE[0] >> t.m_dE[1] >> t.m_dE[2];
76  return is;
77 }
78 
79 inline std::ostream &operator<<(std::ostream &os, const Vector3D &t) {
80  os << t.m_dE[0] << " " << t.m_dE[1] << " " << t.m_dE[2];
81  return os;
82 }
83 
84 inline void Vector3D::MakeUnitVector() {
85  double k = 1.0 / sqrt(m_dE[0] * m_dE[0] + m_dE[1] * m_dE[1] + m_dE[2] * m_dE[2]);
86  m_dE[0] *= k; m_dE[1] *= k; m_dE[2] *= k;
87 }
88 
89 inline Vector3D operator+(const Vector3D &v1, const Vector3D &v2) {
90  return Vector3D(v1.m_dE[0] + v2.m_dE[0], v1.m_dE[1] + v2.m_dE[1], v1.m_dE[2] + v2.m_dE[2]);
91 }
92 
93 inline Vector3D operator+(const Vector3D &v1, const int &i2) {
94  return Vector3D(v1.m_dE[0] + i2, v1.m_dE[1] + i2, v1.m_dE[2] + i2);
95 }
96 
97 inline Vector3D operator+(const Vector3D &v1, const double &i2) {
98  return Vector3D(v1.m_dE[0] + i2, v1.m_dE[1] + i2, v1.m_dE[2] + i2);
99 }
100 
101 inline Vector3D operator-(const Vector3D &v1, const Vector3D &v2) {
102  return Vector3D(v1.m_dE[0] - v2.m_dE[0], v1.m_dE[1] - v2.m_dE[1], v1.m_dE[2] - v2.m_dE[2]);
103 }
104 
105 inline Vector3D operator-(const Vector3D &v1, const int &i2) {
106  return Vector3D(v1.m_dE[0] - i2, v1.m_dE[1] - i2, v1.m_dE[2] - i2);
107 }
108 
109 inline Vector3D operator-(const Vector3D &v1, double &i2) {
110  return Vector3D(v1.m_dE[0] - i2, v1.m_dE[1] - i2, v1.m_dE[2] - i2);
111 }
112 
113 inline Vector3D operator*(const Vector3D &v1, const Vector3D &v2) {
114  return Vector3D(v1.m_dE[0] * v2.m_dE[0], v1.m_dE[1] * v2.m_dE[1], v1.m_dE[2] * v2.m_dE[2]);
115 }
116 
117 inline Vector3D operator/(const Vector3D &v1, const Vector3D &v2) {
118  return Vector3D(v1.m_dE[0] / v2.m_dE[0], v1.m_dE[1] / v2.m_dE[1], v1.m_dE[2] / v2.m_dE[2]);
119 }
120 
121 inline Vector3D operator*(double t, const Vector3D &v) {
122  return Vector3D(t*v.m_dE[0], t*v.m_dE[1], t*v.m_dE[2]);
123 }
124 
125 inline Vector3D operator/(Vector3D v, double t) {
126  return Vector3D(v.m_dE[0] / t, v.m_dE[1] / t, v.m_dE[2] / t);
127 }
128 
129 inline Vector3D operator/(double t, Vector3D v) {
130  return Vector3D(t / v.m_dE[0], t / v.m_dE[1], t / v.m_dE[2]);
131 }
132 
133 inline Vector3D operator*(const Vector3D &v, double t) {
134  return Vector3D(t*v.m_dE[0], t*v.m_dE[1], t*v.m_dE[2]);
135 }
136 
138  m_dE[0] += v.m_dE[0];
139  m_dE[1] += v.m_dE[1];
140  m_dE[2] += v.m_dE[2];
141  return *this;
142 }
143 
145  m_dE[0] *= v.m_dE[0];
146  m_dE[1] *= v.m_dE[1];
147  m_dE[2] *= v.m_dE[2];
148  return *this;
149 }
150 
152  m_dE[0] /= v.m_dE[0];
153  m_dE[1] /= v.m_dE[1];
154  m_dE[2] /= v.m_dE[2];
155  return *this;
156 }
157 
159  m_dE[0] -= v.m_dE[0];
160  m_dE[1] -= v.m_dE[1];
161  m_dE[2] -= v.m_dE[2];
162  return *this;
163 }
164 
165 inline Vector3D &Vector3D::operator*=(const double t) {
166  m_dE[0] *= t;
167  m_dE[1] *= t;
168  m_dE[2] *= t;
169  return *this;
170 }
171 
172 inline Vector3D &Vector3D::operator/=(const double t) {
173  double k = 1.0 / t;
174 
175  m_dE[0] *= k;
176  m_dE[1] *= k;
177  m_dE[2] *= k;
178  return *this;
179 }
180 
181 inline Vector3D UnitVector(Vector3D v) {
182  return v / v.Length();
183 }
184 
185 inline double StdDot(const Vector3D &v1, const Vector3D &v2) {
186  return v1.m_dE[0] * v2.m_dE[0] + v1.m_dE[1] * v2.m_dE[1] + v1.m_dE[2] * v2.m_dE[2];
187 }
188 
189 inline Vector3D StdCross(const Vector3D &v1, const Vector3D &v2) {
190  return Vector3D((v1.m_dE[1] * v2.m_dE[2] - v1.m_dE[2] * v2.m_dE[1]),
191  (-(v1.m_dE[0] * v2.m_dE[2] - v1.m_dE[2] * v2.m_dE[0])),
192  (v1.m_dE[0] * v2.m_dE[1] - v1.m_dE[1] * v2.m_dE[0]));
193 }
194 
195 #endif // VECTOR_H
Vector3D & operator/=(const Vector3D &v2)
Divide Assign Vector3D Operator.
Definition: vector.h:151
double y() const
Returns second (Y) location parameter in Vector3D.
Definition: vector.h:41
double operator[](int i) const
Constant Vector3D Array Operator.
Definition: vector.h:49
void MakeUnitVector()
Convert Vector3D into unit vector.
Definition: vector.h:84
Vector3D & operator+=(const Vector3D &v2)
Add Assign Vector3D Operator.
Definition: vector.h:137
double Dot(const Vector3D &v2)
Return dot product (double) of two Vector3D.
Definition: vector.h:60
Vector3D & operator-=(const Vector3D &v2)
Subtract Assign Assign Operator.
Definition: vector.h:158
double & operator[](int i)
Vector3D Array Operator.
Definition: vector.h:50
double z() const
Returns third (Z) location parameter in Vector3D.
Definition: vector.h:42
const Vector3D & operator+() const
Add Vector3D Operator.
Definition: vector.h:47
Vector3D & operator *=(const Vector3D &v2)
Multiply Assign Vector3D Operator.
Definition: vector.h:144
Vector3D Cross(const Vector3D &v2)
Definition: vector.h:63
double x() const
Returns first (X) location parameter in Vector3D.
Definition: vector.h:40
Vector3D(double e0)
Definition: vector.h:31
Definition: vector.h:16
Vector3D operator-() const
Subtract Vector3D Operator.
Definition: vector.h:48
double b() const
Returns third (B) color parameter in Vector3D.
Definition: vector.h:45
Vector3D()
Definition: vector.h:24
double r() const
Returns first (R) color parameter in Vector3D.
Definition: vector.h:43
double g() const
Returns second (G) color parameter in Vector3D.
Definition: vector.h:44
Vector3D(double e0, double e1, double e2)
Definition: vector.h:38
double m_dE[3]
Value array (double) for color or location parameters.
Definition: vector.h:71
double SquaredLength() const
Return squared length (double) of Vector3D.
Definition: vector.h:59
double Length() const
Return length (double) of Vector3D.
Definition: vector.h:58