From b107a2a6def926b477f683866aed0cac25a142dc Mon Sep 17 00:00:00 2001 From: Martin Opat Date: Thu, 9 Jan 2025 11:53:28 +0100 Subject: [PATCH] Added some more useful math functions --- src/linalg/mat.cu | 16 ++++++++++++++++ src/linalg/mat.h | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/src/linalg/mat.cu b/src/linalg/mat.cu index c5bdd4a..c42a8b3 100644 --- a/src/linalg/mat.cu +++ b/src/linalg/mat.cu @@ -44,3 +44,19 @@ __device__ unsigned int packUnorm4x8(float r, float g, float b, float a) { return u.out; } + +// Clamp a value between a min and max value +__device__ float clamp(float value, float min, float max) { + return fmaxf(min, fminf(value, max)); +} + +// Normalize a float to the range [0, 1] +__device__ float normalize(float value, float min, float max) { + return (value - min) / (max - min); +} + +// Interpolate between two values +template +__device__ T interpolate(T start, T end, float t) { + return start + t * (end - start); +} diff --git a/src/linalg/mat.h b/src/linalg/mat.h index cd070d7..559b8c5 100644 --- a/src/linalg/mat.h +++ b/src/linalg/mat.h @@ -7,4 +7,11 @@ __device__ Vec3 computeGradient(float* volumeData, const int volW, const int vol __device__ unsigned int packUnorm4x8(float r, float g, float b, float a); +__device__ float clamp(float value, float min, float max); +__device__ float normalize(float value, float min, float max); + +template +__device__ float interpolate(T start, T end, float t); + + #endif // MAT_H