|
16 | 16 | #include <stdlib.h> |
17 | 17 |
|
18 | 18 |
|
19 | | -class vec3 { |
20 | | - |
21 | | - |
22 | | -public: |
23 | | - vec3() {} |
24 | | - vec3(float e0, float e1, float e2) { e[0] = e0; e[1] = e1; e[2] = e2; } |
25 | | - inline float x() const { return e[0]; } |
26 | | - inline float y() const { return e[1]; } |
27 | | - inline float z() const { return e[2]; } |
28 | | - inline float r() const { return e[0]; } |
29 | | - inline float g() const { return e[1]; } |
30 | | - inline float b() const { return e[2]; } |
31 | | - |
32 | | - inline const vec3& operator+() const { return *this; } |
33 | | - inline vec3 operator-() const { return vec3(-e[0], -e[1], -e[2]); } |
34 | | - inline float operator[](int i) const { return e[i]; } |
35 | | - inline float& operator[](int i) { return e[i]; }; |
36 | | - |
37 | | - inline vec3& operator+=(const vec3 &v2); |
38 | | - inline vec3& operator-=(const vec3 &v2); |
39 | | - inline vec3& operator*=(const vec3 &v2); |
40 | | - inline vec3& operator/=(const vec3 &v2); |
41 | | - inline vec3& operator*=(const float t); |
42 | | - inline vec3& operator/=(const float t); |
43 | | - |
44 | | - inline float length() const { return sqrt(e[0]*e[0] + e[1]*e[1] + e[2]*e[2]); } |
45 | | - inline float squared_length() const { return e[0]*e[0] + e[1]*e[1] + e[2]*e[2]; } |
46 | | - inline void make_unit_vector(); |
47 | | - |
48 | | - float e[3]; |
| 19 | +class vec3 { |
| 20 | + public: |
| 21 | + vec3() {} |
| 22 | + vec3(float e0, float e1, float e2) { e[0] = e0; e[1] = e1; e[2] = e2; } |
| 23 | + inline float x() const { return e[0]; } |
| 24 | + inline float y() const { return e[1]; } |
| 25 | + inline float z() const { return e[2]; } |
| 26 | + inline float r() const { return e[0]; } |
| 27 | + inline float g() const { return e[1]; } |
| 28 | + inline float b() const { return e[2]; } |
| 29 | + |
| 30 | + inline const vec3& operator+() const { return *this; } |
| 31 | + inline vec3 operator-() const { return vec3(-e[0], -e[1], -e[2]); } |
| 32 | + inline float operator[](int i) const { return e[i]; } |
| 33 | + inline float& operator[](int i) { return e[i]; } |
| 34 | + |
| 35 | + inline vec3& operator+=(const vec3 &v2); |
| 36 | + inline vec3& operator-=(const vec3 &v2); |
| 37 | + inline vec3& operator*=(const vec3 &v2); |
| 38 | + inline vec3& operator/=(const vec3 &v2); |
| 39 | + inline vec3& operator*=(const float t); |
| 40 | + inline vec3& operator/=(const float t); |
| 41 | + |
| 42 | + inline float length() const { return sqrt(e[0]*e[0] + e[1]*e[1] + e[2]*e[2]); } |
| 43 | + inline float squared_length() const { return e[0]*e[0] + e[1]*e[1] + e[2]*e[2]; } |
| 44 | + inline void make_unit_vector(); |
| 45 | + |
| 46 | + float e[3]; |
49 | 47 | }; |
50 | 48 |
|
51 | 49 |
|
52 | | - |
53 | 50 | inline std::istream& operator>>(std::istream &is, vec3 &t) { |
54 | 51 | is >> t.e[0] >> t.e[1] >> t.e[2]; |
55 | 52 | return is; |
@@ -94,61 +91,64 @@ inline vec3 operator*(const vec3 &v, float t) { |
94 | 91 | } |
95 | 92 |
|
96 | 93 | inline float dot(const vec3 &v1, const vec3 &v2) { |
97 | | - return v1.e[0] *v2.e[0] + v1.e[1] *v2.e[1] + v1.e[2] *v2.e[2]; |
| 94 | + return v1.e[0] * v2.e[0] |
| 95 | + + v1.e[1] * v2.e[1] |
| 96 | + + v1.e[2] * v2.e[2]; |
98 | 97 | } |
99 | 98 |
|
100 | 99 | inline vec3 cross(const vec3 &v1, const vec3 &v2) { |
101 | | - return vec3(v1.e[1]*v2.e[2] - v1.e[2]*v2.e[1], |
102 | | - v1.e[2]*v2.e[0] - v1.e[0]*v2.e[2], |
103 | | - v1.e[0]*v2.e[1] - v1.e[1]*v2.e[0]); |
| 100 | + return vec3(v1.e[1] * v2.e[2] - v1.e[2] * v2.e[1], |
| 101 | + v1.e[2] * v2.e[0] - v1.e[0] * v2.e[2], |
| 102 | + v1.e[0] * v2.e[1] - v1.e[1] * v2.e[0]); |
104 | 103 | } |
105 | 104 |
|
106 | 105 | inline vec3& vec3::operator+=(const vec3 &v){ |
107 | | - e[0] += v.e[0]; |
108 | | - e[1] += v.e[1]; |
109 | | - e[2] += v.e[2]; |
| 106 | + e[0] += v.e[0]; |
| 107 | + e[1] += v.e[1]; |
| 108 | + e[2] += v.e[2]; |
110 | 109 | return *this; |
111 | 110 | } |
112 | 111 |
|
113 | 112 | inline vec3& vec3::operator*=(const vec3 &v){ |
114 | | - e[0] *= v.e[0]; |
115 | | - e[1] *= v.e[1]; |
116 | | - e[2] *= v.e[2]; |
| 113 | + e[0] *= v.e[0]; |
| 114 | + e[1] *= v.e[1]; |
| 115 | + e[2] *= v.e[2]; |
117 | 116 | return *this; |
118 | 117 | } |
119 | 118 |
|
120 | 119 | inline vec3& vec3::operator/=(const vec3 &v){ |
121 | | - e[0] /= v.e[0]; |
122 | | - e[1] /= v.e[1]; |
123 | | - e[2] /= v.e[2]; |
| 120 | + e[0] /= v.e[0]; |
| 121 | + e[1] /= v.e[1]; |
| 122 | + e[2] /= v.e[2]; |
124 | 123 | return *this; |
125 | 124 | } |
126 | 125 |
|
127 | 126 | inline vec3& vec3::operator-=(const vec3& v) { |
128 | | - e[0] -= v.e[0]; |
129 | | - e[1] -= v.e[1]; |
130 | | - e[2] -= v.e[2]; |
| 127 | + e[0] -= v.e[0]; |
| 128 | + e[1] -= v.e[1]; |
| 129 | + e[2] -= v.e[2]; |
131 | 130 | return *this; |
132 | 131 | } |
133 | 132 |
|
134 | 133 | inline vec3& vec3::operator*=(const float t) { |
135 | | - e[0] *= t; |
136 | | - e[1] *= t; |
137 | | - e[2] *= t; |
| 134 | + e[0] *= t; |
| 135 | + e[1] *= t; |
| 136 | + e[2] *= t; |
138 | 137 | return *this; |
139 | 138 | } |
140 | 139 |
|
141 | 140 | inline vec3& vec3::operator/=(const float t) { |
142 | | - float k = 1.0/t; |
| 141 | + float k = 1.0f/t; |
143 | 142 |
|
144 | | - e[0] *= k; |
145 | | - e[1] *= k; |
146 | | - e[2] *= k; |
| 143 | + e[0] *= k; |
| 144 | + e[1] *= k; |
| 145 | + e[2] *= k; |
147 | 146 | return *this; |
148 | 147 | } |
149 | 148 |
|
150 | 149 | inline vec3 unit_vector(vec3 v) { |
151 | 150 | return v / v.length(); |
152 | 151 | } |
153 | 152 |
|
| 153 | + |
154 | 154 | #endif |
0 commit comments