Skip to content

Commit 0608ad7

Browse files
committed
add const index operator for vector classes
1 parent 1543cb2 commit 0608ad7

File tree

6 files changed

+46
-0
lines changed

6 files changed

+46
-0
lines changed

include/threepp/math/Vector2.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ namespace threepp {
4444

4545
float& operator[](unsigned int index);
4646

47+
float operator[](unsigned int index) const;
48+
4749
Vector2& copy(const Vector2& v);
4850

4951
Vector2& add(const Vector2& v);

include/threepp/math/Vector3.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ namespace threepp {
6464

6565
float& operator[](size_t index);
6666

67+
float operator[](size_t index) const;
68+
6769
Vector3& copy(const Vector3& v);
6870

6971
// Adds v to this vector.

include/threepp/math/Vector4.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ namespace threepp {
2626

2727
float& operator[](unsigned int index);
2828

29+
float operator[](unsigned int index) const;
30+
2931
Vector4& set(float x, float y, float z, float w);
3032

3133
Vector4& setScalar(float value);

src/threepp/math/Vector2.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ float& Vector2::operator[](unsigned int index) {
5757
}
5858
}
5959

60+
float Vector2::operator[](unsigned int index) const {
61+
if (index >= 2) throw std::runtime_error("index out of bounds: " + std::to_string(index));
62+
switch (index) {
63+
case 0:
64+
return x;
65+
case 1:
66+
return y;
67+
default:
68+
throw std::runtime_error("index out of bound: " + std::to_string(index));
69+
}
70+
}
71+
6072
Vector2& Vector2::copy(const Vector2& v) {
6173

6274
this->x = v.x;

src/threepp/math/Vector3.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ float& Vector3::operator[](size_t index) {
7676
}
7777
}
7878

79+
float Vector3::operator[](size_t index) const {
80+
switch (index) {
81+
case 0:
82+
return x;
83+
case 1:
84+
return y;
85+
case 2:
86+
return z;
87+
default:
88+
throw std::runtime_error("index out of bound: " + std::to_string(index));
89+
}
90+
}
91+
7992
Vector3& Vector3::copy(const Vector3& v) {
8093

8194
this->x = v.x;

src/threepp/math/Vector4.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ float& Vector4::operator[](unsigned int index) {
3030
}
3131
}
3232

33+
float Vector4::operator[](unsigned int index) const {
34+
switch (index) {
35+
case 0:
36+
return x;
37+
case 1:
38+
return y;
39+
case 2:
40+
return z;
41+
case 3:
42+
return w;
43+
default:
44+
throw std::runtime_error("index out of bound: " + std::to_string(index));
45+
}
46+
}
47+
3348
Vector4& Vector4::set(float x, float y, float z, float w) {
3449

3550
this->x = x;

0 commit comments

Comments
 (0)