ClassiCube/ClassicalSharp/Selections/SelectionBoxComparer.cs
UnknownShadow200 98d435b110 fix licensing
2017-01-20 09:12:04 +11:00

39 lines
1.6 KiB
C#

// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
using System;
using System.Collections.Generic;
using OpenTK;
namespace ClassicalSharp.Selections {
internal class SelectionBoxComparer : IComparer<SelectionBox> {
public int Compare(SelectionBox a, SelectionBox b) {
// Reversed comparison order because we need to render back to front for alpha blending.
return a.MinDist == b.MinDist ? b.MaxDist.CompareTo(a.MaxDist)
: b.MinDist.CompareTo(a.MinDist);
}
internal void Intersect(SelectionBox box, Vector3 origin) {
Vector3I min = box.Min, max = box.Max;
float closest = float.PositiveInfinity, furthest = float.NegativeInfinity;
// Bottom corners
UpdateDist(origin, min.X, min.Y, min.Z, ref closest, ref furthest);
UpdateDist(origin, max.X, min.Y, min.Z, ref closest, ref furthest);
UpdateDist(origin, max.X, min.Y, max.Z, ref closest, ref furthest);
UpdateDist(origin, min.X, min.Y, max.Z, ref closest, ref furthest);
// Top corners
UpdateDist(origin, min.X, max.Y, min.Z, ref closest, ref furthest);
UpdateDist(origin, max.X, max.Y, min.Z, ref closest, ref furthest);
UpdateDist(origin, max.X, max.Y, max.Z, ref closest, ref furthest);
UpdateDist(origin, min.X, max.Y, max.Z, ref closest, ref furthest);
box.MinDist = closest; box.MaxDist = furthest;
}
static void UpdateDist(Vector3 p, float x2, float y2, float z2,
ref float closest, ref float furthest) {
float dist = Utils.DistanceSquared(p.X, p.Y, p.Z, x2, y2, z2);
if (dist < closest) closest = dist;
if (dist > furthest) furthest = dist;
}
}
}