From 32f44d39f01b62a51db177b6f3004ee0b835188d Mon Sep 17 00:00:00 2001 From: Keuin Date: Mon, 11 Apr 2022 15:19:05 +0800 Subject: Implement vec3 scalar and vector product, product by a scalar, get unit vector and print to ostream. Fix wrong vec3 norm implementation. --- vec.h | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 12 deletions(-) (limited to 'vec.h') diff --git a/vec.h b/vec.h index ac6ea44..ad5f727 100644 --- a/vec.h +++ b/vec.h @@ -36,35 +36,75 @@ struct vec3 { } vec3 operator-(const vec3 &b) const { - return vec3{.x=x - b.x, .y=y - b.y, .z=z - b.z}; - } - - // cross product - vec3 operator*(const vec3 &b) const { - return vec3{.x=y * b.z - z * b.y, .y=x * b.z - z * b.x, .z=x * b.y - y * b.x}; + return *this + (-b); } bool operator==(const vec3 b) const { return eq(x, b.x) && eq(y, b.y) && eq(z, b.z); } - // dot product - int dot(const vec3 &b) const { + // dot product (aka inner product, or scalar product, producing a scalar) + T dot(const vec3 &b) const { return x * b.x + y * b.y + z * b.z; } + // cross product (aka outer product, or vector product, producing a vector) + vec3 cross(const vec3 &b) const { + return vec3{.x=y * b.z - z * b.y, .y=x * b.z - z * b.x, .z=x * b.y - y * b.x}; + } + // norm value - int norm(const int level = 2) const { + double norm(const int level = 2) const { if (level == 2) { - return std::abs(x * x + y * y + z * z); + return std::abs(sqrt(x * x + y * y + z * z)); } else if (level == 1) { - return std::abs(x + y + z); + return std::abs(x) + std::abs(y) + std::abs(z); } else { - return (int) std::abs(powl(x, level) + powl(y, level) + powl(z, level)); + return powl(powl(x, level) + powl(y, level) + powl(z, level), 1.0 / level); } } + + vec3 unit_vec() const { + return *this * (1.0 / norm()); + } }; +// print to ostream +template +std::ostream &operator<<(std::ostream &out, const vec3 &vec) { + return out << "vec3[x=" << vec.x << ", y=" << vec.y << ", z=" << vec.z << ']'; +} + +// product vec3 by a scalar +template +vec3 operator*(const vec3 &vec, const S &b) { + return vec3{.x=(T) (vec.x * b), .y=(T) (vec.y * b), .z=(T) (vec.z * b)}; +} + +// product vec3 by a scalar +template +vec3 operator*(const S &b, const vec3 &vec) { + return vec3{.x=(T) (vec.x * b), .y=(T) (vec.y * b), .z=(T) (vec.z * b)}; +} + +// product vec3 by the inversion of a scalar (div by a scalar) +template +vec3 operator/(const vec3 &vec, const S &b) { + return vec3{.x=(T) (vec.x / b), .y=(T) (vec.y / b), .z=(T) (vec.z / b)}; +} + +// scalar product (inner product) +template +T dot(const vec3 &a, const vec3 &b) { + return a.dot(b); +} + +// vector product (outer product) +template +vec3 cross(const vec3 &a, const vec3 &b) { + return a.cross(b); +} + // 3-dim vector (int) using vec3i = vec3; -- cgit v1.2.3