Skip to content

Commit d70cba9

Browse files
committed
More correct vec3 calculations
Correct and faster operators.
1 parent c11ed7a commit d70cba9

File tree

3 files changed

+70
-76
lines changed

3 files changed

+70
-76
lines changed

Tests/Vec3Tests.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ TEST_F(Vec3Test, GreaterThanEq)
117117
TEST_F(Vec3Test, GreaterThan)
118118
{
119119
ASSERT_TRUE( sample3Vec > sample1Vec );
120-
ASSERT_FALSE( sample2Vec > sample1Vec );
120+
ASSERT_TRUE( sample2Vec > sample1Vec );
121121
}
122122

123123
TEST_F(Vec3Test, LessThanEq)
@@ -128,6 +128,5 @@ TEST_F(Vec3Test, LessThanEq)
128128
TEST_F(Vec3Test, LessThan)
129129
{
130130
ASSERT_TRUE( sample1Vec < sample3Vec );
131-
ASSERT_FALSE( sample1Vec < sample2Vec );
131+
ASSERT_TRUE( sample1Vec < sample2Vec );
132132
}
133-

vec3.cpp

+57-63
Original file line numberDiff line numberDiff line change
@@ -42,133 +42,123 @@ vec3 vec3::unitY(0,1,0);
4242
vec3 vec3::unitZ(0,0,1);
4343
const double vec3::PI = 3.14159265;
4444

45-
vec3::vec3() : x_(0), y_(0), z_(0)
45+
vec3::vec3() : data_{0,0,0}
4646
{
4747
}
4848

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}
5650
{
5751
}
5852

5953
bool vec3::operator!=(const vec3 &a) const
6054
{
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())
6458
return true;
6559
else
6660
return false;
6761
}
6862

6963
bool vec3::operator==(const vec3 &a) const
7064
{
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());
7468
}
7569

7670
bool vec3::operator<=(const vec3 &a) const
7771
{
78-
return ((this->x_ <= a.x_) &&
79-
(this->y_ <= a.y_) &&
80-
(this->z_ <= a.z_));
72+
return *this < a || *this == a;
8173
}
8274

8375
bool vec3::operator>=(const vec3 &a) const
8476
{
85-
return ((this->x_ >= a.x_) &&
86-
(this->y_ >= a.y_) &&
87-
(this->z_ >= a.z_));
77+
return *this > a || *this == a;
8878
}
8979

9080
bool vec3::operator<(const vec3 &a) const
9181
{
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;
95100
}
96101

97102
bool vec3::operator>(const vec3 &a) const
98103
{
99-
return ((this->x_ > a.x_) &&
100-
(this->y_ > a.y_) &&
101-
(this->z_ > a.z_));
104+
return a < *this;
102105
}
103106

104-
105107
vec3& vec3::operator=(const vec3 &a)
106108
{
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();
110112

111113
return *this;
112114
}
113115

114116
vec3& vec3::operator+=(const vec3 &a)
115117
{
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();
119121

120122
return *this;
121123
}
122124

123125
vec3& vec3::operator*=(double c)
124126
{
125-
this->x_ *= c;
126-
this->y_ *= c;
127-
this->z_ *= c;
127+
this->x() *= c;
128+
this->y() *= c;
129+
this->z() *= c;
128130

129131
return *this;
130132
}
131133

132134
vec3& vec3::operator/=(double c)
133135
{
134136
// 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;
138140
return *this;
139141
}
140142

141143
double& vec3::operator[](const size_t idx)
142144
{
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];
150146
}
151147

152148
// TODO: Restructure this class so this lookup is FASTER
153149
double vec3::operator[](const size_t idx) const
154150
{
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];
162152
}
163153

164154
double vec3::dot(const vec3 &b) const
165155
{
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();
167157
}
168158

169159
vec3 vec3::cross(const vec3 &b)
170160
{
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());
172162
}
173163

174164
vec3 cross(const vec3 &a, const vec3 &b)
@@ -186,6 +176,11 @@ double length(const vec3 &a)
186176
return sqrt(a.x()*a.x() + a.y()*a.y() + a.z()*a.z());
187177
}
188178

179+
double lengthSquared(const vec3 &a)
180+
{
181+
return a.x()*a.x() + a.y()*a.y() + a.z()*a.z();
182+
}
183+
189184
double distance(const vec3 &a, const vec3 &b)
190185
{
191186
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)
281276
std::string vec3::toString() const
282277
{
283278
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() << "]";
285280
return ss.str();
286281
}
287282

288283
vec3 vec3::min(const vec3 &a, const vec3 &b)
289284
{
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());
293288
}
294289

295290
vec3 vec3::max(const vec3 &a, const vec3 &b)
296291
{
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());
300295
}
301296

302297
double angleBetween(const vec3 &a, const vec3 &b)
@@ -308,4 +303,3 @@ double angleBetween(const vec3 &a, const vec3 &b, float epsilon)
308303
{
309304
return acos( dot(a,b) / ( ( L2(a)*L2(b) ) + epsilon ) );
310305
}
311-

vec3.h

+11-10
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,9 @@ class vec3
4646
public:
4747
vec3();
4848
vec3(double x, double y, double z);
49-
vec3(const vec3 &x);
5049

5150
private:
52-
double x_;
53-
double y_;
54-
double z_;
51+
double data_[3];
5552

5653
public:
5754
bool operator!=(const vec3 &a) const;
@@ -71,14 +68,17 @@ class vec3
7168
double dot(const vec3 &b) const;
7269
vec3 cross(const vec3 &b);
7370

74-
const double x() const { return this->x_; }
75-
void x( double b ) { this->x_ = b; }
71+
const double x() const { return data_[0]; }
72+
double& x() { return data_[0]; }
73+
void x( double b ) { data_[0] = b; }
7674

77-
const double y() const { return this->y_; }
78-
void y( double b ) { this->y_ = b; }
75+
const double y() const { return data_[1]; }
76+
double& y() { return data_[1]; }
77+
void y( double b ) { data_[1] = b; }
7978

80-
const double z() const { return this->z_; }
81-
void z( double b ) { this->z_ = b; }
79+
const double z() const { return data_[2]; }
80+
double& z() { return data_[2]; }
81+
void z( double b ) { data_[2] = b; }
8282

8383
static vec3 zero;
8484
static vec3 unitX;
@@ -96,6 +96,7 @@ vec3 cross(const vec3 &a, const vec3 &b);
9696
vec3 bisect(const vec3 &a, const vec3 &b);
9797
double dot(const vec3 &a, const vec3 &b);
9898
double length(const vec3 &a);
99+
double lengthSquared(const vec3 &a);
99100
double distance(const vec3 &a, const vec3 &b);
100101
double L1(const vec3 &a);
101102
double L2(const vec3 &a);

0 commit comments

Comments
 (0)