@@ -42,133 +42,123 @@ vec3 vec3::unitY(0,1,0);
42
42
vec3 vec3::unitZ (0 ,0 ,1 );
43
43
const double vec3::PI = 3.14159265 ;
44
44
45
- vec3::vec3 () : x_( 0 ), y_( 0 ), z_( 0 )
45
+ vec3::vec3 () : data_{ 0 , 0 , 0 }
46
46
{
47
47
}
48
48
49
- // default constructor
50
- vec3::vec3 (double x, double y, double z) : x_(x), y_(y), z_(z)
51
- {
52
- }
53
-
54
- // copy constructor
55
- vec3::vec3 (const vec3 &x) : x_(x.x_), y_(x.y_), z_(x.z_)
49
+ vec3::vec3 (double x, double y, double z) : data_{x,y,z}
56
50
{
57
51
}
58
52
59
53
bool vec3::operator !=(const vec3 &a) const
60
54
{
61
- if (this ->x_ != a.x_ ||
62
- this ->y_ != a.y_ ||
63
- this ->z_ != a.z_ )
55
+ if (this ->x () != a.x () ||
56
+ this ->y () != a.y () ||
57
+ this ->z () != a.z () )
64
58
return true ;
65
59
else
66
60
return false ;
67
61
}
68
62
69
63
bool vec3::operator ==(const vec3 &a) const
70
64
{
71
- return (this ->x_ == a.x_ &&
72
- this ->y_ == a.y_ &&
73
- this ->z_ == a.z_ );
65
+ return (this ->x () == a.x () &&
66
+ this ->y () == a.y () &&
67
+ this ->z () == a.z () );
74
68
}
75
69
76
70
bool vec3::operator <=(const vec3 &a) const
77
71
{
78
- return ((this ->x_ <= a.x_ ) &&
79
- (this ->y_ <= a.y_ ) &&
80
- (this ->z_ <= a.z_ ));
72
+ return *this < a || *this == a;
81
73
}
82
74
83
75
bool vec3::operator >=(const vec3 &a) const
84
76
{
85
- return ((this ->x_ >= a.x_ ) &&
86
- (this ->y_ >= a.y_ ) &&
87
- (this ->z_ >= a.z_ ));
77
+ return *this > a || *this == a;
88
78
}
89
79
90
80
bool vec3::operator <(const vec3 &a) const
91
81
{
92
- return ((this ->x_ < a.x_ ) &&
93
- (this ->y_ < a.y_ ) &&
94
- (this ->z_ < a.z_ ));
82
+ if (x () < a.x ())
83
+ return true ;
84
+ if (x () > a.x ())
85
+ return false ;
86
+
87
+ // equal x
88
+ if (y () < a.y ())
89
+ return true ;
90
+ if (y () > a.y ())
91
+ return false ;
92
+
93
+ // equal x and y
94
+ if (z () < a.z ())
95
+ return true ;
96
+ if (z () > a.z ())
97
+ return false ;
98
+
99
+ return false ;
95
100
}
96
101
97
102
bool vec3::operator >(const vec3 &a) const
98
103
{
99
- return ((this ->x_ > a.x_ ) &&
100
- (this ->y_ > a.y_ ) &&
101
- (this ->z_ > a.z_ ));
104
+ return a < *this ;
102
105
}
103
106
104
-
105
107
vec3& vec3::operator =(const vec3 &a)
106
108
{
107
- this ->x_ = a.x_ ;
108
- this ->y_ = a.y_ ;
109
- this ->z_ = a.z_ ;
109
+ this ->x () = a.x () ;
110
+ this ->y () = a.y () ;
111
+ this ->z () = a.z () ;
110
112
111
113
return *this ;
112
114
}
113
115
114
116
vec3& vec3::operator +=(const vec3 &a)
115
117
{
116
- this ->x_ += a.x_ ;
117
- this ->y_ += a.y_ ;
118
- this ->z_ += a.z_ ;
118
+ this ->x () += a.x () ;
119
+ this ->y () += a.y () ;
120
+ this ->z () += a.z () ;
119
121
120
122
return *this ;
121
123
}
122
124
123
125
vec3& vec3::operator *=(double c)
124
126
{
125
- this ->x_ *= c;
126
- this ->y_ *= c;
127
- this ->z_ *= c;
127
+ this ->x () *= c;
128
+ this ->y () *= c;
129
+ this ->z () *= c;
128
130
129
131
return *this ;
130
132
}
131
133
132
134
vec3& vec3::operator /=(double c)
133
135
{
134
136
// TODO: throw exception if c == 0
135
- this ->x_ /= c;
136
- this ->y_ /= c;
137
- this ->z_ /= c;
137
+ this ->x () /= c;
138
+ this ->y () /= c;
139
+ this ->z () /= c;
138
140
return *this ;
139
141
}
140
142
141
143
double & vec3::operator [](const size_t idx)
142
144
{
143
- switch (idx)
144
- {
145
- case 0 : return this ->x_ ;
146
- case 1 : return this ->y_ ;
147
- case 2 : return this ->z_ ;
148
- default : throw -1 ; // bad index
149
- }
145
+ return data_[idx];
150
146
}
151
147
152
148
// TODO: Restructure this class so this lookup is FASTER
153
149
double vec3::operator [](const size_t idx) const
154
150
{
155
- switch (idx)
156
- {
157
- case 0 : return this ->x_ ;
158
- case 1 : return this ->y_ ;
159
- case 2 : return this ->z_ ;
160
- default : throw -1 ; // bad index
161
- }
151
+ return data_[idx];
162
152
}
163
153
164
154
double vec3::dot (const vec3 &b) const
165
155
{
166
- return this ->x_ *b.x_ + this ->y_ *b.y_ + this ->z_ *b.z_ ;
156
+ return this ->x () *b.x () + this ->y () *b.y () + this ->z () *b.z () ;
167
157
}
168
158
169
159
vec3 vec3::cross (const vec3 &b)
170
160
{
171
- return vec3 (this ->y_ *b.z_ - this ->z_ *b.y_ , this ->z_ *b.x_ - this ->x_ *b.z_ , this ->x_ *b.y_ - this ->y_ *b.x_ );
161
+ return vec3 (this ->y () *b.z () - this ->z () *b.y () , this ->z () *b.x () - this ->x () *b.z () , this ->x () *b.y () - this ->y () *b.x () );
172
162
}
173
163
174
164
vec3 cross (const vec3 &a, const vec3 &b)
@@ -186,6 +176,11 @@ double length(const vec3 &a)
186
176
return sqrt (a.x ()*a.x () + a.y ()*a.y () + a.z ()*a.z ());
187
177
}
188
178
179
+ double lengthSquared (const vec3 &a)
180
+ {
181
+ return a.x ()*a.x () + a.y ()*a.y () + a.z ()*a.z ();
182
+ }
183
+
189
184
double distance (const vec3 &a, const vec3 &b)
190
185
{
191
186
double xdiff = a.x ()-b.x (), ydiff = a.y ()-b.y (), zdiff = a.z ()-b.z ();
@@ -281,22 +276,22 @@ double clamp(double value, double min, double max)
281
276
std::string vec3::toString () const
282
277
{
283
278
std::stringstream ss;
284
- ss << " [" << std::setprecision (5 ) << this ->x () << " , " << this ->y_ << " , " << this ->z_ << " ]" ;
279
+ ss << " [" << std::setprecision (5 ) << this ->x () << " , " << this ->y () << " , " << this ->z () << " ]" ;
285
280
return ss.str ();
286
281
}
287
282
288
283
vec3 vec3::min (const vec3 &a, const vec3 &b)
289
284
{
290
- return vec3 ((a.x_ < b.x_ ) ? a.x_ : b.x_ ,
291
- (a.y_ < b.y_ ) ? a.y_ : b.y_ ,
292
- (a.z_ < b.z_ ) ? a.z_ : b.z_ );
285
+ return vec3 ((a.x () < b.x ()) ? a.x () : b.x () ,
286
+ (a.y () < b.y ()) ? a.y () : b.y () ,
287
+ (a.z () < b.z ()) ? a.z () : b.z () );
293
288
}
294
289
295
290
vec3 vec3::max (const vec3 &a, const vec3 &b)
296
291
{
297
- return vec3 ((a.x_ > b.x_ ) ? a.x_ : b.x_ ,
298
- (a.y_ > b.y_ ) ? a.y_ : b.y_ ,
299
- (a.z_ > b.z_ ) ? a.z_ : b.z_ );
292
+ return vec3 ((a.x () > b.x ()) ? a.x () : b.x () ,
293
+ (a.y () > b.y ()) ? a.y () : b.y () ,
294
+ (a.z () > b.z ()) ? a.z () : b.z () );
300
295
}
301
296
302
297
double angleBetween (const vec3 &a, const vec3 &b)
@@ -308,4 +303,3 @@ double angleBetween(const vec3 &a, const vec3 &b, float epsilon)
308
303
{
309
304
return acos ( dot (a,b) / ( ( L2 (a)*L2 (b) ) + epsilon ) );
310
305
}
311
-
0 commit comments