using EvilEngine.Formats.Maps; namespace EvilEngine.Host.Liquids; internal sealed class LiquidVolume { private const double Inside = 1e-6; private const double Upward = 1e-9; private readonly double _level; private readonly MapPlane[] _planes; private LiquidVolume(MapPlane[] planes, double level, MapVector min, MapVector max, (double X, double Z) topCentre) { _planes = planes; _level = level; Min = min; Max = max; TopCentre = topCentre; } public MapVector Min { get; } public MapVector Max { get; } public (double X, double Z) TopCentre { get; } public bool IsSphere { get; private init; } public bool IsLevel { get; private init; } public MapVector Centre { get; private init; } public double Radius { get; private init; } public static LiquidVolume? Convex(IReadOnlyList planes) { var corners = new List(); for (var i = 0; i < planes.Count; i++) for (var j = i + 1; j < planes.Count; j++) for (var k = j + 1; k < planes.Count; k++) if (Meet(planes[i], planes[j], planes[k]) is { } corner && planes.All(plane => Dot(plane.Normal, corner) <= plane.Distance + 1e-4)) corners.Add(corner); if (corners.Count < 4) return null; var min = new MapVector(corners.Min(c => c.X), corners.Min(c => c.Y), corners.Min(c => c.Z)); var max = new MapVector(corners.Max(c => c.X), corners.Max(c => c.Y), corners.Max(c => c.Z)); if (!(max.X - min.X > Inside && max.Y - min.Y > Inside && max.Z - min.Z > Inside)) return null; return new LiquidVolume(planes.ToArray(), 0, min, max, (corners.Average(c => c.X), corners.Average(c => c.Z))); } public static LiquidVolume Box(MapVector min, MapVector max, MapVector angles) { MapPlane[] local = [ new(new MapVector(1, 0, 0), max.X), new(new MapVector(-1, 0, 0), -min.X), new(new MapVector(0, 1, 0), max.Y), new(new MapVector(0, -1, 0), -min.Y), new(new MapVector(0, 0, 1), max.Z), new(new MapVector(0, 0, -1), -min.Z) ]; if (angles == MapVector.Zero) return Convex(local)!; var basis = MapAngles.ToBasis(angles); var centre = new MapVector((min.X + max.X) / 2, (min.Y + max.Y) / 2, (min.Z + max.Z) / 2); var turned = local.Select(plane => { var normal = Turn(basis, plane.Normal); var onPlane = new MapVector(plane.Normal.X * plane.Distance - centre.X, plane.Normal.Y * plane.Distance - centre.Y, plane.Normal.Z * plane.Distance - centre.Z); var moved = Turn(basis, onPlane); var point = new MapVector(moved.X + centre.X, moved.Y + centre.Y, moved.Z + centre.Z); return new MapPlane(normal, Dot(normal, point)); }).ToArray(); return Convex(turned)!; } public static LiquidVolume Level(double height, MapArea? area) { var min = area is { } a ? new MapVector(a.MinX, double.NegativeInfinity, a.MinZ) : new MapVector( double.NegativeInfinity, double.NegativeInfinity, double.NegativeInfinity); var max = area is { } b ? new MapVector(b.MaxX, height, b.MaxZ) : new MapVector( double.PositiveInfinity, height, double.PositiveInfinity); var centre = area is { } c ? ((c.MinX + c.MaxX) / 2, (c.MinZ + c.MaxZ) / 2) : (0.0, 0.0); return new LiquidVolume([], height, min, max, centre) { IsLevel = true }; } public static LiquidVolume Sphere(MapVector centre, double radius) { return new LiquidVolume([], 0, new MapVector(centre.X - radius, centre.Y - radius, centre.Z - radius), new MapVector(centre.X + radius, centre.Y + radius, centre.Z + radius), (centre.X, centre.Z)) { IsSphere = true, Centre = centre, Radius = radius }; } public bool Contains(MapVector point) { if (point.X < Min.X - Inside || point.Y < Min.Y - Inside || point.Z < Min.Z - Inside || point.X > Max.X + Inside || point.Y > Max.Y + Inside || point.Z > Max.Z + Inside) return false; if (IsSphere) { var (x, y, z) = (point.X - Centre.X, point.Y - Centre.Y, point.Z - Centre.Z); return x * x + y * y + z * z <= (Radius + Inside) * (Radius + Inside); } if (IsLevel) return true; foreach (var plane in _planes) if (Dot(plane.Normal, point) > plane.Distance + Inside) return false; return true; } public double Top(double x, double z) { if (IsLevel) return _level; if (IsSphere) { var (dx, dz) = (x - Centre.X, z - Centre.Z); var rest = Radius * Radius - dx * dx - dz * dz; return rest >= 0 ? Centre.Y + Math.Sqrt(rest) : double.NegativeInfinity; } var top = double.PositiveInfinity; foreach (var plane in _planes) if (plane.Normal.Y > Upward) top = Math.Min(top, (plane.Distance - plane.Normal.X * x - plane.Normal.Z * z) / plane.Normal.Y); return top; } private static MapVector? Meet(MapPlane a, MapPlane b, MapPlane c) { var bc = Cross(b.Normal, c.Normal); var determinant = Dot(a.Normal, bc); if (Math.Abs(determinant) < 1e-12) return null; var ca = Cross(c.Normal, a.Normal); var ab = Cross(a.Normal, b.Normal); return new MapVector( (bc.X * a.Distance + ca.X * b.Distance + ab.X * c.Distance) / determinant, (bc.Y * a.Distance + ca.Y * b.Distance + ab.Y * c.Distance) / determinant, (bc.Z * a.Distance + ca.Z * b.Distance + ab.Z * c.Distance) / determinant); } private static MapVector Turn(double[,] basis, MapVector vector) { return new MapVector( basis[0, 0] * vector.X + basis[0, 1] * vector.Y + basis[0, 2] * vector.Z, basis[1, 0] * vector.X + basis[1, 1] * vector.Y + basis[1, 2] * vector.Z, basis[2, 0] * vector.X + basis[2, 1] * vector.Y + basis[2, 2] * vector.Z); } private static double Dot(MapVector a, MapVector b) { return a.X * b.X + a.Y * b.Y + a.Z * b.Z; } private static MapVector Cross(MapVector a, MapVector b) { return new MapVector(a.Y * b.Z - a.Z * b.Y, a.Z * b.X - a.X * b.Z, a.X * b.Y - a.Y * b.X); } }