summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeuin <[email protected]>2022-04-15 12:50:23 +0800
committerKeuin <[email protected]>2022-04-15 12:50:23 +0800
commit50cd8dd3dd029ce432f8e517b4c054e75b5cfe8e (patch)
tree8fd78325d8443bb8399fbc86777ba51eabd31176
parent569be1744453d9e03cf6ba6360c42b9da89a596a (diff)
Fix vec3::reflect. Add tests for vec3::reflect and vec3::is_zero.
-rw-r--r--test_vec.cpp24
-rw-r--r--vec.h8
2 files changed, 28 insertions, 4 deletions
diff --git a/test_vec.cpp b/test_vec.cpp
index 1b765aa..143d5b3 100644
--- a/test_vec.cpp
+++ b/test_vec.cpp
@@ -74,4 +74,28 @@ TEST(Vec, Mod2) {
vec3d b{2.5, 3, 1.2};
ASSERT_EQ(a.mod2(), 14);
ASSERT_LE(abs(b.mod2() - (2.5 * 2.5 + 3 * 3 + 1.2 * 1.2)), 1e-10);
+}
+
+TEST(Vec, IsZero) {
+ vec3i a{1,0,0}, b{0,-1,0}, c{1,2,3};
+ ASSERT_FALSE(a.is_zero());
+ ASSERT_FALSE(b.is_zero());
+ ASSERT_FALSE(c.is_zero());
+
+ vec3d d{0.1,0,0}, e{0,-0.1,0},f{0.1,0.1,0.1};
+ ASSERT_FALSE(d.is_zero());
+ ASSERT_FALSE(e.is_zero());
+ ASSERT_FALSE(f.is_zero());
+
+ vec3i g{0,0,0};
+ vec3d h{0,0,0}, i{1e-10,0,0}, j{1e-10,1e-10,1e-10};
+ ASSERT_TRUE(g.is_zero());
+ ASSERT_TRUE(h.is_zero());
+ ASSERT_TRUE(i.is_zero());
+ ASSERT_TRUE(j.is_zero());
+}
+
+TEST(Vec, Reflect) {
+ vec3d n{1,0,0}, u{-1,1.1,0}, v{1,1.1,0};
+ ASSERT_EQ(v, n.reflect(u));
} \ No newline at end of file
diff --git a/vec.h b/vec.h
index c7669bf..5050065 100644
--- a/vec.h
+++ b/vec.h
@@ -88,10 +88,10 @@ struct vec3 {
return *this * (1.0 / norm());
}
- // Get the reflected vector. Current vector is the incoming vector, n is the normal vector (length should be 1).
- vec3 reflect(const vec3 &n) const {
- assert(fabs(n.mod2() - 1.0) < 1e-8);
- return *this - 2.0 * dot(*this, n) * n;
+ // Get the reflected vector. Current vector is the normal vector (length should be 1), v is the incoming vector.
+ vec3 reflect(const vec3 &v) const {
+ assert(fabs(mod2() - 1.0) < 1e-8);
+ return v - (2.0 * dot(v)) * (*this);
}
};