0.2.0-dev Download ZIP
172 lines ·6.5 KB Raw
| 1 | using EvilEngine.Formats.Maps; |
| 2 | |
| 3 | namespace EvilEngine.Host.Liquids; |
| 4 | |
| 5 | internal sealed class LiquidVolume |
| 6 | { |
| 7 | private const double Inside = 1e-6; |
| 8 | private const double Upward = 1e-9; |
| 9 | private readonly double _level; |
| 10 | |
| 11 | private readonly MapPlane[] _planes; |
| 12 | |
| 13 | private LiquidVolume(MapPlane[] planes, double level, MapVector min, MapVector max, |
| 14 | (double X, double Z) topCentre) |
| 15 | { |
| 16 | _planes = planes; |
| 17 | _level = level; |
| 18 | Min = min; |
| 19 | Max = max; |
| 20 | TopCentre = topCentre; |
| 21 | } |
| 22 | |
| 23 | public MapVector Min { get; } |
| 24 | |
| 25 | public MapVector Max { get; } |
| 26 | |
| 27 | public (double X, double Z) TopCentre { get; } |
| 28 | |
| 29 | public bool IsSphere { get; private init; } |
| 30 | |
| 31 | public bool IsLevel { get; private init; } |
| 32 | |
| 33 | public MapVector Centre { get; private init; } |
| 34 | |
| 35 | public double Radius { get; private init; } |
| 36 | |
| 37 | public static LiquidVolume? Convex(IReadOnlyList<MapPlane> planes) |
| 38 | { |
| 39 | var corners = new List<MapVector>(); |
| 40 | for (var i = 0; i < planes.Count; i++) |
| 41 | for (var j = i + 1; j < planes.Count; j++) |
| 42 | for (var k = j + 1; k < planes.Count; k++) |
| 43 | if (Meet(planes[i], planes[j], planes[k]) is { } corner && |
| 44 | planes.All(plane => Dot(plane.Normal, corner) <= plane.Distance + 1e-4)) |
| 45 | corners.Add(corner); |
| 46 | if (corners.Count < 4) |
| 47 | return null; |
| 48 | var min = new MapVector(corners.Min(c => c.X), corners.Min(c => c.Y), corners.Min(c => c.Z)); |
| 49 | var max = new MapVector(corners.Max(c => c.X), corners.Max(c => c.Y), corners.Max(c => c.Z)); |
| 50 | if (!(max.X - min.X > Inside && max.Y - min.Y > Inside && max.Z - min.Z > Inside)) |
| 51 | return null; |
| 52 | return new LiquidVolume(planes.ToArray(), 0, min, max, |
| 53 | (corners.Average(c => c.X), corners.Average(c => c.Z))); |
| 54 | } |
| 55 | |
| 56 | public static LiquidVolume Box(MapVector min, MapVector max, MapVector angles) |
| 57 | { |
| 58 | MapPlane[] local = |
| 59 | [ |
| 60 | new(new MapVector(1, 0, 0), max.X), new(new MapVector(-1, 0, 0), -min.X), |
| 61 | new(new MapVector(0, 1, 0), max.Y), new(new MapVector(0, -1, 0), -min.Y), |
| 62 | new(new MapVector(0, 0, 1), max.Z), new(new MapVector(0, 0, -1), -min.Z) |
| 63 | ]; |
| 64 | if (angles == MapVector.Zero) |
| 65 | return Convex(local)!; |
| 66 | var basis = MapAngles.ToBasis(angles); |
| 67 | var centre = new MapVector((min.X + max.X) / 2, (min.Y + max.Y) / 2, (min.Z + max.Z) / 2); |
| 68 | var turned = local.Select(plane => |
| 69 | { |
| 70 | var normal = Turn(basis, plane.Normal); |
| 71 | var onPlane = new MapVector(plane.Normal.X * plane.Distance - centre.X, |
| 72 | plane.Normal.Y * plane.Distance - centre.Y, plane.Normal.Z * plane.Distance - centre.Z); |
| 73 | var moved = Turn(basis, onPlane); |
| 74 | var point = new MapVector(moved.X + centre.X, moved.Y + centre.Y, moved.Z + centre.Z); |
| 75 | return new MapPlane(normal, Dot(normal, point)); |
| 76 | }).ToArray(); |
| 77 | return Convex(turned)!; |
| 78 | } |
| 79 | |
| 80 | public static LiquidVolume Level(double height, MapArea? area) |
| 81 | { |
| 82 | var min = area is { } a |
| 83 | ? new MapVector(a.MinX, double.NegativeInfinity, a.MinZ) |
| 84 | : new MapVector( |
| 85 | double.NegativeInfinity, double.NegativeInfinity, double.NegativeInfinity); |
| 86 | var max = area is { } b |
| 87 | ? new MapVector(b.MaxX, height, b.MaxZ) |
| 88 | : new MapVector( |
| 89 | double.PositiveInfinity, height, double.PositiveInfinity); |
| 90 | var centre = area is { } c ? ((c.MinX + c.MaxX) / 2, (c.MinZ + c.MaxZ) / 2) : (0.0, 0.0); |
| 91 | return new LiquidVolume([], height, min, max, centre) { IsLevel = true }; |
| 92 | } |
| 93 | |
| 94 | public static LiquidVolume Sphere(MapVector centre, double radius) |
| 95 | { |
| 96 | return new LiquidVolume([], 0, |
| 97 | new MapVector(centre.X - radius, centre.Y - radius, centre.Z - radius), |
| 98 | new MapVector(centre.X + radius, centre.Y + radius, centre.Z + radius), (centre.X, centre.Z)) |
| 99 | { |
| 100 | IsSphere = true, Centre = centre, Radius = radius |
| 101 | }; |
| 102 | } |
| 103 | |
| 104 | public bool Contains(MapVector point) |
| 105 | { |
| 106 | if (point.X < Min.X - Inside || point.Y < Min.Y - Inside || point.Z < Min.Z - Inside || |
| 107 | point.X > Max.X + Inside || point.Y > Max.Y + Inside || point.Z > Max.Z + Inside) |
| 108 | return false; |
| 109 | if (IsSphere) |
| 110 | { |
| 111 | var (x, y, z) = (point.X - Centre.X, point.Y - Centre.Y, point.Z - Centre.Z); |
| 112 | return x * x + y * y + z * z <= (Radius + Inside) * (Radius + Inside); |
| 113 | } |
| 114 | |
| 115 | if (IsLevel) |
| 116 | return true; |
| 117 | foreach (var plane in _planes) |
| 118 | if (Dot(plane.Normal, point) > plane.Distance + Inside) |
| 119 | return false; |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | public double Top(double x, double z) |
| 124 | { |
| 125 | if (IsLevel) |
| 126 | return _level; |
| 127 | if (IsSphere) |
| 128 | { |
| 129 | var (dx, dz) = (x - Centre.X, z - Centre.Z); |
| 130 | var rest = Radius * Radius - dx * dx - dz * dz; |
| 131 | return rest >= 0 ? Centre.Y + Math.Sqrt(rest) : double.NegativeInfinity; |
| 132 | } |
| 133 | |
| 134 | var top = double.PositiveInfinity; |
| 135 | foreach (var plane in _planes) |
| 136 | if (plane.Normal.Y > Upward) |
| 137 | top = Math.Min(top, (plane.Distance - plane.Normal.X * x - plane.Normal.Z * z) / plane.Normal.Y); |
| 138 | return top; |
| 139 | } |
| 140 | |
| 141 | private static MapVector? Meet(MapPlane a, MapPlane b, MapPlane c) |
| 142 | { |
| 143 | var bc = Cross(b.Normal, c.Normal); |
| 144 | var determinant = Dot(a.Normal, bc); |
| 145 | if (Math.Abs(determinant) < 1e-12) |
| 146 | return null; |
| 147 | var ca = Cross(c.Normal, a.Normal); |
| 148 | var ab = Cross(a.Normal, b.Normal); |
| 149 | return new MapVector( |
| 150 | (bc.X * a.Distance + ca.X * b.Distance + ab.X * c.Distance) / determinant, |
| 151 | (bc.Y * a.Distance + ca.Y * b.Distance + ab.Y * c.Distance) / determinant, |
| 152 | (bc.Z * a.Distance + ca.Z * b.Distance + ab.Z * c.Distance) / determinant); |
| 153 | } |
| 154 | |
| 155 | private static MapVector Turn(double[,] basis, MapVector vector) |
| 156 | { |
| 157 | return new MapVector( |
| 158 | basis[0, 0] * vector.X + basis[0, 1] * vector.Y + basis[0, 2] * vector.Z, |
| 159 | basis[1, 0] * vector.X + basis[1, 1] * vector.Y + basis[1, 2] * vector.Z, |
| 160 | basis[2, 0] * vector.X + basis[2, 1] * vector.Y + basis[2, 2] * vector.Z); |
| 161 | } |
| 162 | |
| 163 | private static double Dot(MapVector a, MapVector b) |
| 164 | { |
| 165 | return a.X * b.X + a.Y * b.Y + a.Z * b.Z; |
| 166 | } |
| 167 | |
| 168 | private static MapVector Cross(MapVector a, MapVector b) |
| 169 | { |
| 170 | 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); |
| 171 | } |
| 172 | } |