From 2ed39ad4eb9ebf64768dbf4aeefafc5ebb072ca7 Mon Sep 17 00:00:00 2001 From: Keuin Date: Fri, 15 Apr 2022 11:11:08 +0800 Subject: Add vec3::is_zero and vec3::reflect. (not tested) --- vec.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/vec.h b/vec.h index 8efbc9a..64eaaca 100644 --- a/vec.h +++ b/vec.h @@ -35,6 +35,12 @@ struct vec3 { return vec3{0, 0, 0}; } + bool is_zero() const { +#define EPS (1e-8) + return x >= -EPS && y >= -EPS && z >= -EPS && x <= EPS && y <= EPS && z <= EPS; +#undef EPS + } + vec3 operator+(const vec3 &b) const { return vec3{.x=x + b.x, .y=y + b.y, .z=z + b.z}; } @@ -80,6 +86,12 @@ struct vec3 { vec3 unit_vec() const { 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; + } }; // print to ostream -- cgit v1.2.3