Skip to content

Commit

Permalink
Update CameraPositioning tolerances (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
Strepto authored Nov 21, 2024
1 parent a8e085e commit 1d173b2
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions CadRevealComposer/Operations/CameraPositioning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@ 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.

const float cameraVerticalFieldOfViewDeg = 45f;
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);

Expand Down Expand Up @@ -80,22 +79,26 @@ 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
)
/// <summary>
/// Gets the bounding box, but tries to avoid outliers having too big impact on bounds.
/// </summary>
private static BoundingBox GetApproximatePlatformBoundingBoxForCameraPosition(APrimitive[] geometries)
{
// TODO: does not handle empty geometries correctly
if (geometries.Length == 0)
{
throw new Exception(
"The input data had 0 elements, we cannot position the camera. Does the 3D scene have any valid meshes?"
);
}

// 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)
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 1d173b2

Please sign in to comment.