# Vector.h

a guest
Nov 29th, 2013
46
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #ifndef VECTOR3D_H
2. #define VECTOR3D_H
3.
4. #include <math.h>
5. #include <ostream>
6. #include <iomanip>
7. #include "cuda_runtime.h"
8. #include "device_launch_parameters.h"
9.
10. class Vector
11. {
12. public:
13.     union
14.     {
15.         struct
16.         {
17.             double x, y, z;
18.         };
19.         double components[3];
20.     };
21.
22.     __device__ Vector();
23.     __device__ Vector(double _x, double _y, double _z);
24.
25.     __device__ void makeZero();
26.     __device__ double length() const;
27.     __device__ double lengthSqr() const;
28.     __device__ void scale(double multiplier);
29.
30.     __device__ void operator *= (double multiplier);
31.     __device__ void operator += (const Vector& rhs);
32.     __device__ void operator /= (double divider);
33.
34.     __device__ void normalize();
35.     __device__ void setLength(double newLength);
36.     __device__ double& operator[] (int index);
37.     __device__ const double& operator[] (int index) const;
38. };
39.
40. __device__ inline Vector operator + (const Vector& a, const Vector& b)
41. {
42.     return Vector(a.x + b.x, a.y + b.y, a.z + b.z);
43. }
44.
45. __device__ inline Vector operator - (const Vector& a, const Vector& b)
46. {
47.     return Vector(a.x - b.x, a.y - b.y, a.z - b.z);
48. }
49.
50. __device__ inline Vector operator - (const Vector& a)
51. {
52.     return Vector(-a.x, -a.y, -a.z);
53. }
54.
55. //* dot product
56. __device__ inline double operator * (const Vector& a, const Vector& b)
57. {
58.     return a.x * b.x + a.y * b.y + a.z * b.z;
59. }
60.
61. //* dot product (functional form, to make it more explicit):
62. __device__ inline double dot(const Vector& a, const Vector& b)
63. {
64.     return a.x * b.x + a.y * b.y + a.z * b.z;
65. }
66.
67. //* cross product
68. __device__ inline Vector operator ^ (const Vector& a, const Vector& b)
69. {
70.     return Vector(
71.         a.y * b.z - a.z * b.y,
72.         a.z * b.x - a.x * b.z,
73.         a.x * b.y - a.y * b.x
74.     );
75. }
76.
77. __device__ inline Vector operator * (const Vector& a, double multiplier)
78. {
79.     return Vector(a.x * multiplier, a.y * multiplier, a.z * multiplier);
80. }
81.
82. __device__ inline Vector operator * (double multiplier, const Vector& a)
83. {
84.     return Vector(a.x * multiplier, a.y * multiplier, a.z * multiplier);
85. }
86.
87. __device__ inline Vector operator / (const Vector& a, double divider)
88. {
89.     double multiplier = 1.0 / divider;
90.     return Vector(a.x * multiplier, a.y * multiplier, a.z * multiplier);
91. }
92.
93. __device__ inline Vector reflect(const Vector& ray, const Vector& norm)
94. {
95.     Vector result = ray - 2 * dot(ray, norm) * norm;
96.     result.normalize();
97.     return result;
98. }
99.
100. __device__ inline Vector faceforward(const Vector& ray, const Vector& norm)
101. {
102.     if (dot(ray, norm) < 0)
103.     {
104.         return norm;
105.     }
106.     else
107.     {
108.         return -norm;
109.     }
110. }
111.
112. __device__ inline Vector project(const Vector& v, int a, int b, int c)
113. {
114.     Vector result;
115.     result[a] = v[0];
116.     result[b] = v[1];
117.     result[c] = v[2];
118.     return result;
119. }
120.
121. __device__ inline Vector unproject(const Vector& v, int a, int b, int c)
122. {
123.     Vector result;
124.     result[0] = v[a];
125.     result[1] = v[b];
126.     result[2] = v[c];
127.     return result;
128. }
129.
130. struct Ray
131. {
132.     Vector start;
133.     Vector dir;
134.
135.     bool debug;
136.
137.     __device__ Ray();
138.     __device__ Ray(const Vector& _start, const Vector& _dir);
139. };
140.
141. __device__ inline Ray project(const Ray& v, int a, int b, int c)
142. {
143.     return Ray(project(v.start, a, b, c), project(v.dir, a, b, c));
144. }
145.
146. // iostream Vector print routine:
147. //__host__ __device__ inline std::ostream& operator << (std::ostream& os, const Vector& vec)
148. //{
149. //  os << "(" << std::fixed << std::setprecision(3) << vec.x << ", " << vec.y << ", " << vec.z << ")";
150. //  return os;
151. //}
152.
153. #endif // VECTOR3D_H
RAW Paste Data