From 865cc4bcf05d406ab56a28d77afb327c9a8af0b2 Mon Sep 17 00:00:00 2001 From: Martin Opat Date: Mon, 30 Dec 2024 10:14:46 +0100 Subject: [PATCH] Added += etc. operators to Vec3 --- src/linalg/vec.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/linalg/vec.h b/src/linalg/vec.h index 62bab7a..e643c0d 100644 --- a/src/linalg/vec.h +++ b/src/linalg/vec.h @@ -10,9 +10,15 @@ struct Vec3 { // TODO: Maybe make this into a class __host__ __device__ Vec3(double x, double y, double z) : x(x), y(y), z(z) {} __host__ __device__ Vec3 operator+(const Vec3& b) const { return Vec3(x + b.x, y + b.y, z + b.z); } - __host__ __device__ Vec3 operator-(const Vec3& b) const { return Vec3(x - b.x, y - b.y, z - b.z); } - __host__ __device__ Vec3 operator*(double b) const { return Vec3(x * b, y * b, z * b); } + __host__ __device__ Vec3& operator+=(const Vec3& b) { x += b.x; y += b.y; z += b.z; return *this; } + __host__ __device__ Vec3 operator-() const { return Vec3(-x, -y, -z); } + __host__ __device__ Vec3 operator-(const Vec3& b) const { return Vec3(x - b.x, y - b.y, z - b.z); } + __host__ __device__ Vec3& operator-=(const Vec3& b) { x -= b.x; y -= b.y; z -= b.z; return *this; } + + __host__ __device__ Vec3 operator*(double b) const { return Vec3(x * b, y * b, z * b); } + __host__ __device__ Vec3& operator*=(double b) { x *= b; y *= b; z *= b; return *this; } + __host__ __device__ double dot(const Vec3& b) const { return x * b.x + y * b.y + z * b.z; } __host__ __device__ Vec3 cross(const Vec3& b) const { return Vec3(y * b.z - z * b.y, z * b.x - x * b.z, x * b.y - y * b.x); } __host__ __device__ Vec3 normalize() const { double len = sqrt(x * x + y * y + z * z); return Vec3(x / len, y / len, z / len); }