using OpenTK.Mathematics;
namespace Yw.WinFrmUI.Hydro
{
///
/// 包围盒
///
internal class BoundingBox3
{
///
///
///
public BoundingBox3(Vector3 min, Vector3 max)
{
this.Min = min;
this.Max = max;
}
///
///
///
public BoundingBox3(List pts)
{
Vector3 min = new(float.MaxValue);
Vector3 max = new(float.MinValue);
foreach (var pt in pts)
{
min = Vector3.ComponentMin(min, pt);
max = Vector3.ComponentMax(max, pt);
}
this.Min = min;
this.Max = max;
}
///
///
///
public Vector3 Min { get; set; }
///
///
///
public Vector3 Max { get; set; }
///
/// 获取中心
///
public Vector3 GetCenter()
{
return new Vector3()
{
X = (this.Max.X + this.Min.X) / 2f,
Y = (this.Max.Y + this.Min.Y) / 2f,
Z = (this.Max.Z + this.Min.Z) / 2f
};
}
///
/// 获取角
///
public List GetCorners()
{
return new List
{
new Vector3(this.Min.X, this.Min.Y, this.Min.Z),
new Vector3(this.Max.X, this.Min.Y, this.Min.Z),
new Vector3(this.Min.X, this.Max.Y, this.Min.Z),
new Vector3(this.Max.X, this.Max.Y, this.Min.Z),
new Vector3(this.Min.X, this.Min.Y, this.Max.Z),
new Vector3(this.Max.X, this.Min.Y, this.Max.Z),
new Vector3(this.Min.X, this.Max.Y, this.Max.Z),
new Vector3(this.Max.X, this.Max.Y, this.Max.Z)
};
}
///
/// 合并
///
public static BoundingBox3 Merge(BoundingBox3 a, BoundingBox3 b)
{
return new BoundingBox3(
Vector3.ComponentMin(a.Min, b.Min),
Vector3.ComponentMin(a.Max, b.Max)
);
}
///
/// 计算
///
public static BoundingBox3 Calculate(List pts)
{
if (pts == null || pts.Count < 1)
{
return default;
}
return new BoundingBox3(pts);
}
}
}