From ddc653081449a97319273b605f6ecaedd5dfd21d Mon Sep 17 00:00:00 2001 From: sherholz Date: Thu, 18 Dec 2025 09:53:44 +0100 Subject: [PATCH 1/5] [sam] Numerical stability improvements when splitting components --- .../vmm/VMMChiSquareComponentSplitter.h | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/openpgl/directional/vmm/VMMChiSquareComponentSplitter.h b/openpgl/directional/vmm/VMMChiSquareComponentSplitter.h index ef12ddb..1f4ab3f 100644 --- a/openpgl/directional/vmm/VMMChiSquareComponentSplitter.h +++ b/openpgl/directional/vmm/VMMChiSquareComponentSplitter.h @@ -166,7 +166,7 @@ inline Vec2Type Map3DTo2D(const Vec3Type &vec3D) OPENPGL_ASSERT(embree::isvalid(alpha)); ScalarType sinAlpha = embree::fastapprox::sin(alpha); OPENPGL_ASSERT(embree::isvalid(sinAlpha)); - ScalarType inv_sinc = sinAlpha != 0.f ? alpha / sinAlpha : 0.f; + ScalarType inv_sinc = std::fabsf(sinAlpha) > FLT_EPSILON ? alpha / sinAlpha : 0.f; OPENPGL_ASSERT(embree::isvalid(inv_sinc)); vec2D.x = embree::select(alpha > 0.0f, vec3D.x * inv_sinc, vec2D.x); @@ -182,7 +182,7 @@ inline Vec3Type Map2DTo3D(const Vec2Type &vec2D) { Vec3Type vec3D = Vec3Type(0.0f); ScalarType norm2 = vec2D.x * vec2D.x + vec2D.y * vec2D.y; - ScalarType length = norm2 > 0.f ? embree::sqrt(norm2) : 0.f; + ScalarType length = norm2 > FLT_EPSILON ? embree::sqrt(norm2) : 0.f; OPENPGL_ASSERT(length < M_PI_F); ScalarType sinc = length > FLT_EPSILON ? embree::fastapprox::sin(length) / length : 0.f; @@ -503,7 +503,7 @@ void VonMisesFisherChiSquareComponentSplitter::UpdateSplitStatistic chiSquareEst += vmfPDF; chiSquareEst /= samplePDF; - chiSquareEst = select(softAssign.assignments[k] > 0.f, chiSquareEst, zeros); + chiSquareEst = select(softAssign.assignments[k] > FLT_EPSILON, chiSquareEst, zeros); splitStats.sumAssignedSamples[k] += softAssign.assignments[k]; // incremental updated of the MC chiSquare estimate @@ -1056,7 +1056,7 @@ void VonMisesFisherChiSquareComponentSplitter::ComponentSplitStatis Vector2 meanDirection2DItoK = Map3DTo2D(inv_transformK * meanDirectionI3D); Vector2 meanDirection2DJtoK = Map3DTo2D(inv_transformK * meanDirectionJ3D); - const float inv_weightK = (weightK > 0.f) ? embree::rcp(weightK) : 1.f; + const float inv_weightK = (weightK > FLT_EPSILON) ? embree::rcp(weightK) : 1.f; const float sumWeightsI = sumWeights[tmpI.quot][tmpI.rem]; const float sumWeightsJ = sumWeights[tmpJ.quot][tmpJ.rem]; @@ -1065,14 +1065,14 @@ void VonMisesFisherChiSquareComponentSplitter::ComponentSplitStatis // std::cout << "\tsumWeightsI: " << sumWeightsI << "\tsumWeightsJ: " << sumWeightsJ << "\tsumWeightsK: " << sumWeightsK << std::endl; // std::cout << "\tnumSamplesI: " << numSamples[tmpI.quot][tmpI.rem] << "\tsumWeightsJ: " << numSamples[tmpJ.quot][tmpJ.rem] << std::endl; - const Vector3 covarianceI = (sumWeightsI > 0.f) ? Vector3(splitWeightedSampleCovariances[tmpI.quot].x[tmpI.rem], splitWeightedSampleCovariances[tmpI.quot].y[tmpI.rem], - splitWeightedSampleCovariances[tmpI.quot].z[tmpI.rem]) * - embree::rcp(sumWeightsI) - : Vector3(0.f); - const Vector3 covarianceJ = (sumWeightsJ > 0.f) ? Vector3(splitWeightedSampleCovariances[tmpJ.quot].x[tmpJ.rem], splitWeightedSampleCovariances[tmpJ.quot].y[tmpJ.rem], - splitWeightedSampleCovariances[tmpJ.quot].z[tmpJ.rem]) * - embree::rcp(sumWeightsJ) - : Vector3(0.f); + const Vector3 covarianceI = (sumWeightsI > FLT_EPSILON) ? Vector3(splitWeightedSampleCovariances[tmpI.quot].x[tmpI.rem], splitWeightedSampleCovariances[tmpI.quot].y[tmpI.rem], + splitWeightedSampleCovariances[tmpI.quot].z[tmpI.rem]) * + embree::rcp(sumWeightsI) + : Vector3(0.f); + const Vector3 covarianceJ = (sumWeightsJ > FLT_EPSILON) ? Vector3(splitWeightedSampleCovariances[tmpJ.quot].x[tmpJ.rem], splitWeightedSampleCovariances[tmpJ.quot].y[tmpJ.rem], + splitWeightedSampleCovariances[tmpJ.quot].z[tmpJ.rem]) * + embree::rcp(sumWeightsJ) + : Vector3(0.f); #ifdef OPENPGL_ZERO_MEAN const Vector2 meanDirectionK2D(0.f); From ac16d08019f2720e9e73d1e112489a93fff28d21 Mon Sep 17 00:00:00 2001 From: Sebastian Herholz Date: Sun, 8 Nov 2026 20:02:38 +0100 Subject: [PATCH 2/5] fix --- openpgl/directional/vmm/VMMChiSquareComponentSplitter.h | 1 + 1 file changed, 1 insertion(+) diff --git a/openpgl/directional/vmm/VMMChiSquareComponentSplitter.h b/openpgl/directional/vmm/VMMChiSquareComponentSplitter.h index 1f4ab3f..cd06af5 100644 --- a/openpgl/directional/vmm/VMMChiSquareComponentSplitter.h +++ b/openpgl/directional/vmm/VMMChiSquareComponentSplitter.h @@ -7,6 +7,7 @@ #include #include +#include #include #include #include From 3600b386958f21d107adee35d255ec5debc82c8a Mon Sep 17 00:00:00 2001 From: Sebastian Herholz Date: Sun, 8 Nov 2026 20:06:00 +0100 Subject: [PATCH 3/5] fix2 --- openpgl/data/PathSegmentDataStorage.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openpgl/data/PathSegmentDataStorage.h b/openpgl/data/PathSegmentDataStorage.h index 9fe31df..2fc320e 100644 --- a/openpgl/data/PathSegmentDataStorage.h +++ b/openpgl/data/PathSegmentDataStorage.h @@ -228,7 +228,7 @@ struct PathSegmentDataStorage openpgl::Vector3(currentPathSegment.directionOut.x, currentPathSegment.directionOut.y, currentPathSegment.directionOut.z)); float cosThetaO = embree::dot(openpgl::Vector3(currentPathSegment.normal.x, currentPathSegment.normal.y, currentPathSegment.normal.z), openpgl::Vector3(currentPathSegment.directionIn.x, currentPathSegment.directionIn.y, currentPathSegment.directionIn.z)); - lastDistance *= std::fabs(cosThetaI / (cosThetaO * currentPathSegment.eta)); + lastDistance *= std::fabsf(cosThetaI / (cosThetaO * currentPathSegment.eta)); } } From ed67f2762a089901944397bd53c1b45c05747433 Mon Sep 17 00:00:00 2001 From: Sebastian Herholz Date: Sun, 8 Nov 2026 20:11:58 +0100 Subject: [PATCH 4/5] fix3 --- openpgl/directional/vmm/VMMChiSquareComponentSplitter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openpgl/directional/vmm/VMMChiSquareComponentSplitter.h b/openpgl/directional/vmm/VMMChiSquareComponentSplitter.h index cd06af5..dabfaa5 100644 --- a/openpgl/directional/vmm/VMMChiSquareComponentSplitter.h +++ b/openpgl/directional/vmm/VMMChiSquareComponentSplitter.h @@ -167,7 +167,7 @@ inline Vec2Type Map3DTo2D(const Vec3Type &vec3D) OPENPGL_ASSERT(embree::isvalid(alpha)); ScalarType sinAlpha = embree::fastapprox::sin(alpha); OPENPGL_ASSERT(embree::isvalid(sinAlpha)); - ScalarType inv_sinc = std::fabsf(sinAlpha) > FLT_EPSILON ? alpha / sinAlpha : 0.f; + ScalarType inv_sinc = std::fabs(sinAlpha) > FLT_EPSILON ? alpha / sinAlpha : 0.f; OPENPGL_ASSERT(embree::isvalid(inv_sinc)); vec2D.x = embree::select(alpha > 0.0f, vec3D.x * inv_sinc, vec2D.x); From c3ef05af80b4a4ff6a8beb808a5884395e4ad83b Mon Sep 17 00:00:00 2001 From: Sebastian Herholz Date: Sun, 8 Nov 2026 20:15:06 +0100 Subject: [PATCH 5/5] fix4 --- openpgl/data/PathSegmentDataStorage.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openpgl/data/PathSegmentDataStorage.h b/openpgl/data/PathSegmentDataStorage.h index 2fc320e..9fe31df 100644 --- a/openpgl/data/PathSegmentDataStorage.h +++ b/openpgl/data/PathSegmentDataStorage.h @@ -228,7 +228,7 @@ struct PathSegmentDataStorage openpgl::Vector3(currentPathSegment.directionOut.x, currentPathSegment.directionOut.y, currentPathSegment.directionOut.z)); float cosThetaO = embree::dot(openpgl::Vector3(currentPathSegment.normal.x, currentPathSegment.normal.y, currentPathSegment.normal.z), openpgl::Vector3(currentPathSegment.directionIn.x, currentPathSegment.directionIn.y, currentPathSegment.directionIn.z)); - lastDistance *= std::fabsf(cosThetaI / (cosThetaO * currentPathSegment.eta)); + lastDistance *= std::fabs(cosThetaI / (cosThetaO * currentPathSegment.eta)); } }