From 08b613182545f2580a29c60099076f740b2801aa Mon Sep 17 00:00:00 2001 From: Martin Opat Date: Mon, 23 Dec 2024 21:50:29 +0100 Subject: [PATCH] Added simple volume generation into sphere.h for debugging --- src/objs/sphere.h | 60 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/src/objs/sphere.h b/src/objs/sphere.h index a9b20b4..4beed27 100644 --- a/src/objs/sphere.h +++ b/src/objs/sphere.h @@ -20,4 +20,62 @@ struct Sphere { t = -b - h; return true; } -}; \ No newline at end of file +}; + +// A function to generate two concentric spherical shells +__host__ void generateVolume(float* hostVolume, int volW, int volH, int volD) { + int cx = volW / 2; + int cy = volH / 2; + int cz = volD / 2; + float maxRadius = static_cast(volW) * 0.5f; + + // Let's define two shells: + float shell1Inner = 0.2f * maxRadius; + float shell1Outer = 0.3f * maxRadius; + float shell2Inner = 0.4f * maxRadius; + float shell2Outer = 0.5f * maxRadius; + + // Intensities + float shell1Intensity = 0.8f; + float shell2Intensity = 0.6f; + + for (int z = 0; z < volD; ++z) { + for (int y = 0; y < volH; ++y) { + for (int x = 0; x < volW; ++x) { + float dx = (float)(x - cx); + float dy = (float)(y - cy); + float dz = (float)(z - cz); + float dist = sqrtf(dx*dx + dy*dy + dz*dz); + + float intensity = 0.0f; + + // Shell 1 + if (dist >= shell1Inner && dist <= shell1Outer) { + float mid = 0.5f * (shell1Inner + shell1Outer); + if (dist < mid) { + float inFactor = (dist - shell1Inner) / (mid - shell1Inner); + intensity += shell1Intensity * inFactor; + } else { + float outFactor = (shell1Outer - dist) / (shell1Outer - mid); + intensity += shell1Intensity * outFactor; + } + } + + // Shell 2 + if (dist >= shell2Inner && dist <= shell2Outer) { + float mid = 0.5f * (shell2Inner + shell2Outer); + if (dist < mid) { + float inFactor = (dist - shell2Inner) / (mid - shell2Inner); + intensity += shell2Intensity * inFactor; + } else { + float outFactor = (shell2Outer - dist) / (shell2Outer - mid); + intensity += shell2Intensity * outFactor; + } + } + + if (intensity > 1.0f) intensity = 1.0f; + hostVolume[z * volW * volH + y * volW + x] = intensity; + } + } + } +}