From 8e4c5290d7b2d11a46bc6723d2edfa39a4618de8 Mon Sep 17 00:00:00 2001 From: Martin Opat Date: Mon, 30 Dec 2024 12:08:08 +0100 Subject: [PATCH] Re-implemented vec3 constructor to allow for uninitialized declaration (cuda does not allow dynamic intialization) --- src/linalg/vec.h | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/linalg/vec.h b/src/linalg/vec.h index d26293f..9fc6745 100644 --- a/src/linalg/vec.h +++ b/src/linalg/vec.h @@ -3,26 +3,27 @@ #include #include -struct Vec3 { // TODO: Maybe make this into a class +struct Vec3 { // TODO: Maybe make this into a class ... maybe double x, y, z; - __host__ __device__ Vec3() : x(0), y(0), z(0) {} - __host__ __device__ Vec3(double x, double y, double z) : x(x), y(y), z(z) {} - __host__ __device__ Vec3(const double (&arr)[3]) : x(arr[0]), y(arr[1]), z(arr[2]) {} + static __host__ __device__ Vec3 init(double x, double y, double z) {Vec3 v = {x, y, z}; return v;} + static __host__ __device__ Vec3 zero() { return Vec3::init(0, 0, 0); } + static __host__ __device__ Vec3 init() { return zero(); } + static __host__ __device__ Vec3 init(const double (&arr)[3]) { return Vec3::init(arr[0], arr[1], arr[2]); } - __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::init(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-() 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 { return Vec3::init(-x, -y, -z); } + __host__ __device__ Vec3 operator-(const Vec3& b) const { return Vec3::init(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) const { return Vec3::init(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); } + __host__ __device__ Vec3 cross(const Vec3& b) const { return Vec3::init(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::init(x / len, y / len, z / len); } }; typedef Vec3 Point3;