From 1d173b26cbe95140c4b5af781f47f13640009308 Mon Sep 17 00:00:00 2001 From: Nils Henrik Hals <3185998+Strepto@users.noreply.github.com> Date: Thu, 21 Nov 2024 15:14:11 +0100 Subject: [PATCH] Update CameraPositioning tolerances (#253) --- .../Operations/CameraPositioning.cs | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/CadRevealComposer/Operations/CameraPositioning.cs b/CadRevealComposer/Operations/CameraPositioning.cs index f24e0c0d..2a299b0f 100644 --- a/CadRevealComposer/Operations/CameraPositioning.cs +++ b/CadRevealComposer/Operations/CameraPositioning.cs @@ -16,9 +16,6 @@ public record CameraPosition( public static CameraPosition CalculateInitialCamera(APrimitive[] geometries) { - static float PythagorasTan(float oppositeLeg, float angleRad) => oppositeLeg / MathF.Tan(angleRad); - static float DegToRad(float degree) => MathF.PI / 180f * degree; - // Camera looks towards platform center, with tilt down. // The camera is positioned such that the longest side X or Y is in view. @@ -26,7 +23,9 @@ public static CameraPosition CalculateInitialCamera(APrimitive[] geometries) const float additionalCameraDistanceFactor = 1.1f; const float cameraVerticalHorizonAngleDeg = 30f; - var (boundingBoxMin, boundingBoxMax) = GetPlatformBoundingBox(geometries); + (Vector3 boundingBoxMin, Vector3 boundingBoxMax) = GetApproximatePlatformBoundingBoxForCameraPosition( + geometries + ); var platformSides = boundingBoxMax - boundingBoxMin; var platformCenter = boundingBoxMin + (platformSides / 2); @@ -80,13 +79,17 @@ public static CameraPosition CalculateInitialCamera(APrimitive[] geometries) SerializableVector3.FromVector3(platformCenter), SerializableVector3.FromVector3(direction) ); + + static float PythagorasTan(float oppositeLeg, float angleRad) => oppositeLeg / MathF.Tan(angleRad); + + static float DegToRad(float degree) => MathF.PI / 180f * degree; } - private static (Vector3 PlatformBoundingBoxMin, Vector3 PlatformBoundingBoxMax) GetPlatformBoundingBox( - APrimitive[] geometries - ) + /// + /// Gets the bounding box, but tries to avoid outliers having too big impact on bounds. + /// + private static BoundingBox GetApproximatePlatformBoundingBoxForCameraPosition(APrimitive[] geometries) { - // TODO: does not handle empty geometries correctly if (geometries.Length == 0) { throw new Exception( @@ -94,8 +97,8 @@ APrimitive[] geometries ); } - // get bounding box for platform using approximation (99th percentile) - const double percentile = 0.01; + // get bounding box for platform using approximation (95th percentile) + const double percentile = 0.05; var platformMinX = geometries .Select(node => node.AxisAlignedBoundingBox.Min.X) .OrderBy(x => x) @@ -129,6 +132,6 @@ APrimitive[] geometries var bbMin = new Vector3(platformMinX, platformMinY, platformMinZ); var bbMax = new Vector3(platformMaxX, platformMaxY, platformMaxZ); - return (bbMin, bbMax); + return new BoundingBox(bbMin, bbMax); } }