From d07325932c7df7750728ff2f43eb02f9a663c878 Mon Sep 17 00:00:00 2001 From: Yggdrasil75 Date: Tue, 10 Feb 2026 13:22:46 -0500 Subject: [PATCH] removed spheres --- tests/g3etest.cpp | 2 +- util/grid/grid3eigen.hpp | 175 +++++++++-------------------------- util/materials/materials.hpp | 6 +- 3 files changed, 49 insertions(+), 134 deletions(-) diff --git a/tests/g3etest.cpp b/tests/g3etest.cpp index 1152004..4ed8730 100644 --- a/tests/g3etest.cpp +++ b/tests/g3etest.cpp @@ -127,7 +127,7 @@ void createSphere(const defaults& config, const spheredefaults& sconfig, Octree< pos.z() >= 0 && pos.z() < config.gridSizecube) { grid.set(1, pos, true, colorVec, voxelSize, true, 1, - sconfig.light, sconfig.emittance, sconfig.refraction, sconfig.reflection, Octree::Shape::CUBE); + sconfig.light, sconfig.emittance, sconfig.refraction, sconfig.reflection); } } } diff --git a/util/grid/grid3eigen.hpp b/util/grid/grid3eigen.hpp index 6f10fab..bea4900 100644 --- a/util/grid/grid3eigen.hpp +++ b/util/grid/grid3eigen.hpp @@ -29,11 +29,6 @@ public: using PointType = Eigen::Matrix; using BoundingBox = std::pair; - enum class Shape { - SPHERE, - CUBE - }; - struct NodeData { T data; PointType position; @@ -46,17 +41,16 @@ public: float emittance; float refraction; float reflection; - Shape shape; NodeData(const T& data, const PointType& pos, bool visible, Eigen::Vector3f color, float size = 0.01f, bool active = true, int objectId = -1, bool light = false, float emittance = 0.0f, - float refraction = 0.0f, float reflection = 0.0f, Shape shape = Shape::SPHERE) + float refraction = 0.0f, float reflection = 0.0f) : data(data), position(pos), objectId(objectId), active(active), visible(visible), color(color), size(size), light(light), emittance(emittance), refraction(refraction), - reflection(reflection), shape(shape) {} + reflection(reflection) {} NodeData() : objectId(-1), active(false), visible(false), size(0.0f), light(false), - emittance(0.0f), refraction(0.0f), reflection(0.0f), shape(Shape::SPHERE) {} + emittance(0.0f), refraction(0.0f), reflection(0.0f) {} // Helper method to get half-size for cube PointType getHalfSize() const { @@ -214,7 +208,6 @@ private: return; } } - auto accumulate = [&](const std::shared_ptr& item) { if (!item || !item->active || !item->visible) return; @@ -255,7 +248,6 @@ private: lod->light = anyLight; lod->active = true; lod->visible = true; - lod->shape = Shape::CUBE; lod->objectId = -1; node->lodData = lod; @@ -304,7 +296,6 @@ private: writeVal(out, pt->emittance); writeVal(out, pt->refraction); writeVal(out, pt->reflection); - writeVal(out, static_cast(pt->shape)); } } else { // Write bitmask of active children @@ -351,7 +342,6 @@ private: readVal(in, pt->reflection); int shapeInt; readVal(in, shapeInt); - pt->shape = static_cast(shapeInt); node->points.push_back(pt); } } else { @@ -439,26 +429,6 @@ private: return tMax >= std::max(0.0f, tMin); } - bool raySphereIntersect(const Ray& ray, const PointType& center, float radiusSq, float& t) const { - PointType oc = ray.origin - center; - float b = 2.0f * oc.dot(ray.dir); - float c = oc.dot(oc) - radiusSq; - float discriminant = b * b - 4 * c; - - if (discriminant < 0) return false; - - float sqrtDisc = sqrt(discriminant); - float t0 = (-b - sqrtDisc) * 0.5f; - float t1 = (-b + sqrtDisc) * 0.5f; - - t = t0; - if (t0 < EPSILON) { - t = t1; - if (t1 < EPSILON) return false; - } - return true; - } - bool rayCubeIntersect(const Ray& ray, const NodeData* cube, float& t, PointType& normal, PointType& hitPoint) const { BoundingBox bounds = cube->getCubeBounds(); @@ -563,9 +533,9 @@ public: bool set(const T& data, const PointType& pos, bool visible, Eigen::Vector3f color, float size, bool active, int objectId = -1, bool light = false, float emittance = 0.0f, float refraction = 0.0f, - float reflection = 0.0f, Shape shape = Shape::SPHERE) { + float reflection = 0.0f) { auto pointData = std::make_shared(data, pos, visible, color, size, active, objectId, - light, emittance, refraction, reflection, shape); + light, emittance, refraction, reflection); if (insertRecursive(root_.get(), pointData, 0)) { this->size++; return true; @@ -744,61 +714,38 @@ public: bool update(const PointType& oldPos, const PointType& newPos, const T& newData, bool newVisible = true, Eigen::Vector3f newColor = Eigen::Vector3f(1.0f, 1.0f, 1.0f), float newSize = 0.01f, bool newActive = true, int newObjectId = -2, bool newLight = false, float newEmittance = 0.0f, float newRefraction = 0.0f, - float newReflection = 0.0f, Shape newShape = Shape::SPHERE, float tolerance = EPSILON) { + float newReflection = 0.0f, float tolerance = EPSILON) { - // Find the existing point auto pointData = find(oldPos, tolerance); if (!pointData) return false; - // If position changed, we need to remove and reinsert - float moveDistSq = (newPos - oldPos).squaredNorm(); - if (moveDistSq > tolerance * tolerance) { - // Save the data - T dataCopy = pointData->data; - bool activeCopy = pointData->active; - bool visibleCopy = pointData->visible; - Eigen::Vector3f colorCopy = pointData->color; - float sizeCopy = pointData->size; - int objectIdCopy = pointData->objectId; - bool lightCopy = pointData->light; - float emittanceCopy = pointData->emittance; - float refractionCopy = pointData->refraction; - float reflectionCopy = pointData->reflection; - Shape shapeCopy = pointData->shape; - - // Remove the old point - if (!remove(oldPos, tolerance)) { - return false; - } - - // Insert at new position with updated properties - return set(newData, newPos, - newVisible ? newVisible : visibleCopy, - newColor != Eigen::Vector3f(1.0f, 1.0f, 1.0f) ? newColor : colorCopy, - newSize > 0 ? newSize : sizeCopy, - newActive ? newActive : activeCopy, - newObjectId != -2 ? newObjectId : objectIdCopy, - newLight ? newLight : lightCopy, - newEmittance > 0 ? newEmittance : emittanceCopy, - newRefraction >= 0 ? newRefraction : refractionCopy, - newReflection >= 0 ? newReflection : reflectionCopy, - newShape); - } else { - // Just update properties in place - pointData->data = newData; - pointData->position = newPos; // Minor adjustment within tolerance - pointData->visible = newVisible; - pointData->color = newColor; - pointData->size = newSize; - if (newObjectId != -2) pointData->objectId = newObjectId; - pointData->active = newActive; - pointData->light = newLight; - pointData->emittance = newEmittance; - pointData->refraction = newRefraction; - pointData->reflection = newReflection; - pointData->shape = newShape; - return true; + T dataCopy = pointData->data; + bool activeCopy = pointData->active; + bool visibleCopy = pointData->visible; + Eigen::Vector3f colorCopy = pointData->color; + float sizeCopy = pointData->size; + int objectIdCopy = pointData->objectId; + bool lightCopy = pointData->light; + float emittanceCopy = pointData->emittance; + float refractionCopy = pointData->refraction; + float reflectionCopy = pointData->reflection; + + // Remove the old point + if (!remove(oldPos, tolerance)) { + return false; } + + // Insert at new position with updated properties + return set(newData, newPos, + newVisible ? newVisible : visibleCopy, + newColor != Eigen::Vector3f(1.0f, 1.0f, 1.0f) ? newColor : colorCopy, + newSize > 0 ? newSize : sizeCopy, + newActive ? newActive : activeCopy, + newObjectId != -2 ? newObjectId : objectIdCopy, + newLight ? newLight : lightCopy, + newEmittance > 0 ? newEmittance : emittanceCopy, + newRefraction >= 0 ? newRefraction : refractionCopy, + newReflection >= 0 ? newReflection : reflectionCopy); } bool move(const PointType& pos, const PointType& newPos) { @@ -806,7 +753,7 @@ public: if (!pointData) return false; bool success = set(pointData->data, newPos, pointData->visible, pointData->color, pointData->size, pointData->active, pointData->objectId, pointData->light, pointData->emittance, - pointData->refraction, pointData->reflection, pointData->shape); + pointData->refraction, pointData->reflection); if (success) { remove(pos); return true; @@ -877,14 +824,6 @@ public: return true; } - bool setShape(const PointType& pos, Shape shape, float tolerance = EPSILON) { - auto pointData = find(pos, tolerance); - if (!pointData) return false; - - pointData->shape = shape; - return true; - } - std::vector> voxelTraverse(const PointType& origin, const PointType& direction, float maxDist, bool stopAtFirstHit, bool enableLOD = false) const { std::vector> hits; @@ -924,20 +863,11 @@ public: if (!pointData->active) continue; float t; - if (pointData->shape == Shape::SPHERE) { - if (raySphereIntersect(ray, pointData->position, pointData->size * pointData->size, t)) { - if (t >= 0 && t <= maxDist) { - hits.emplace_back(pointData); - if (stopAtFirstHit) return; - } - } - } else { - PointType normal, hitPoint; - if (rayCubeIntersect(ray, pointData.get(), t, normal, hitPoint)) { - if (t >= 0 && t <= maxDist) { - hits.emplace_back(pointData); - if (stopAtFirstHit) return; - } + PointType normal, hitPoint; + if (rayCubeIntersect(ray, pointData.get(), t, normal, hitPoint)) { + if (t >= 0 && t <= maxDist) { + hits.emplace_back(pointData); + if (stopAtFirstHit) return; } } } @@ -1003,29 +933,10 @@ public: PointType normal; float t = 0.0f; - // Calculate intersection based on shape - if (obj->shape == Shape::SPHERE) { - // Sphere intersection - PointType center = obj->position; - float radius = obj->size; - PointType L_vec = center - rayOrig; - float tca = L_vec.dot(rayDir); - float d2 = L_vec.dot(L_vec) - tca * tca; - float radius2 = radius * radius; - - if (d2 <= radius2) { - float thc = std::sqrt(radius2 - d2); - t = tca - thc; - if (t < 0.001f) t = tca + thc; - } - - hitPoint = rayOrig + rayDir * t; - normal = (hitPoint - center).normalized(); - } else { - PointType cubeNormal; - if (!rayCubeIntersect(ray, obj.get(), t, normal, hitPoint)) { - return globalIllumination ? skylight_ : Eigen::Vector3f::Zero(); - } + // Calculate intersection + PointType cubeNormal; + if (!rayCubeIntersect(ray, obj.get(), t, normal, hitPoint)) { + return globalIllumination ? skylight_ : Eigen::Vector3f::Zero(); } Eigen::Vector3f finalColor = globalIllumination ? skylight_ : Eigen::Vector3f::Zero(); @@ -1224,7 +1135,7 @@ public: PointType rayDir = dir + (right * px) + (up * py); rayDir.normalize(); - Eigen::Vector3f color = backgroundColor_; // Start with background color + Eigen::Vector3f color = backgroundColor_; auto hit = fastVoxelTraverse(origin, rayDir, std::numeric_limits::max(), true); if (hit != nullptr) { diff --git a/util/materials/materials.hpp b/util/materials/materials.hpp index b9ec4f9..4cf0757 100644 --- a/util/materials/materials.hpp +++ b/util/materials/materials.hpp @@ -1,6 +1,7 @@ #ifndef G3_MATERIALS_HPP #define G3_MATERIALS_HPP +#include "../../eigen/Eigen/Dense" #include "colors.hpp" struct Material { @@ -15,6 +16,7 @@ struct Material { float density; float speedOfSound; float audioAbsorption; + Eigen::Vector3f rgb; //for the fast version. // Constructor with sensible defaults Material(float ior = 1.5f, float dispersion = 0.0f, float chromaticity = 550.0f, @@ -39,7 +41,9 @@ struct Material { float bandwidth = 10.0f + (1.0f - saturation) * 290.0f; - return Material(ior, 0.02f, wavelength, bandwidth, transmission, roughness, emittance, 2.0f, 3000.0f, 0.1f, isLight); + Material outerial(ior, 0.02f, wavelength, bandwidth, transmission, roughness, emittance, 2.0f, 3000.0f, 0.1f, isLight); + outerial.rgb = Eigen::Vector3f(r, g, b); + return outerial; } };