-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.h
127 lines (101 loc) · 3.68 KB
/
vector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#ifndef VECTOR_H
#define VECTOR_H
#include <cmath>
#include <iostream>
class Vector {
public:
double e[3];
__host__ __device__ Vector() {}
__host__ __device__ Vector(double e0, double e1, double e2) {
e[0] = e0; e[1] = e1; e[2] = e2;
}
__host__ __device__ double x() const {return e[0];}
__host__ __device__ double y() const {return e[1];}
__host__ __device__ double z() const {return e[2];}
__host__ __device__ Vector operator-() const {return Vector(-e[0], -e[1], -e[2]);}
__host__ __device__ double operator[](int i) const {return e[i];}
__host__ __device__ double& operator[](int i) {return e[i];}
__host__ __device__ inline Vector& operator+=(const Vector& v2);
__host__ __device__ inline Vector& operator-=(const Vector& v2);
__host__ __device__ inline Vector& operator*=(const Vector& v2);
__host__ __device__ inline Vector& operator/=(const Vector& v2);
__host__ __device__ inline Vector& operator*=(const double t);
__host__ __device__ inline Vector& operator/=(const double t);
__host__ __device__ double length() const {
return std::sqrt(length_squared());
}
__host__ __device__ double length_squared() const {
return e[0]*e[0] + e[1]*e[1] + e[2]*e[2];
}
};
// Vector Utility Functions
__host__ __device__ inline std::ostream& operator<<(std::ostream& out, const Vector& v) {
return out << v.e[0] << ' ' << v.e[1] << ' ' << v.e[2];
}
__host__ __device__ inline Vector operator+(const Vector& u, const Vector& v) {
return Vector(u.e[0] + v.e[0], u.e[1] + v.e[1], u.e[2] + v.e[2]);
}
__host__ __device__ inline Vector operator-(const Vector& u, const Vector& v) {
return Vector(u.e[0] - v.e[0], u.e[1] - v.e[1], u.e[2] - v.e[2]);
}
__host__ __device__ inline Vector operator*(const Vector& u, const Vector& v) {
return Vector(u.e[0] * v.e[0], u.e[1] * v.e[1], u.e[2] * v.e[2]);
}
__host__ __device__ inline Vector operator*(double t, const Vector& v) {
return Vector(t * v.e[0], t * v.e[1], t * v.e[2]);
}
__host__ __device__ inline Vector operator*(const Vector& v, double t) {
return t * v;
}
__host__ __device__ inline Vector operator/(const Vector& v, double t) {
return (1 / t) * v;
}
__host__ __device__ inline double dot(const Vector& u, const Vector& v) {
return u.e[0] * v.e[0] + u.e[1] * v.e[1] + u.e[2] * v.e[2];
}
__host__ __device__ inline Vector cross(const Vector& u, const Vector& v) {
return Vector(u.e[0] * v.e[2] - u.e[2] * v.e[1],
u.e[2] * v.e[0] - u.e[0] * v.e[2],
u.e[0] * v.e[1] - u.e[1] * v.e[0]);
}
__host__ __device__ inline Vector unit_vector(const Vector& v) {
return v / v.length();
}
__host__ __device__ inline Vector& Vector::operator+=(const Vector& v) {
e[0] += v.e[0];
e[1] += v.e[1];
e[2] += v.e[2];
return *this;
}
__host__ __device__ inline Vector& Vector::operator*=(const Vector& v) {
e[0] *= v.e[0];
e[1] *= v.e[1];
e[2] *= v.e[2];
return *this;
}
__host__ __device__ inline Vector& Vector::operator/=(const Vector& v) {
e[0] /= v.e[0];
e[1] /= v.e[1];
e[2] /= v.e[2];
return *this;
}
__host__ __device__ inline Vector& Vector::operator-=(const Vector& v) {
e[0] -= v.e[0];
e[1] -= v.e[1];
e[2] -= v.e[2];
return *this;
}
__host__ __device__ inline Vector& Vector::operator*=(double t) {
e[0] *= t;
e[1] *= t;
e[2] *= t;
return *this;
}
__host__ __device__ inline Vector& Vector::operator/=(double t) {
double k = 1.0 / t;
e[0] *= k;
e[1] *= k;
e[2] *= k;
return *this;
}
#endif // VECTOR_H