#region usings using System; using OpenTK; #endregion namespace SM.OGL.Mesh { /// /// Contains information about bounding boxes of meshes /// public class BoundingBox { /// /// The maximum corner. /// public Vector3 Max = Vector3.Zero; /// /// The minimum corner. /// public Vector3 Min = Vector3.Zero; /// /// Empty constructor /// public BoundingBox() { } /// /// Creates the bounding box with predefined min and max values /// /// /// public BoundingBox(Vector3 min, Vector3 max) { Min = min; Max = max; } /// /// Returns specific configurations of corners /// /// If true, it takes the X-value of maximum, otherwise the minimum. /// If true, it takes the Y-value of maximum, otherwise the minimum. /// If true, it takes the Z-value of maximum, otherwise the minimum. /// public Vector3 this[bool x, bool y, bool z] => Get(x,y,z); /// /// Equivalent to . /// Returns specific configurations of corners /// /// If true, it takes the X-value of maximum, otherwise the minimum. /// If true, it takes the Y-value of maximum, otherwise the minimum. /// If true, it takes the Z-value of maximum, otherwise the minimum. /// public Vector3 Get(bool x, bool y, bool z) { return new Vector3(x ? Max.X : Min.X, y ? Max.Y : Min.Y, z ? Max.Z : Min.Z); } /// /// Returns the configuration of the two furthest away corners. /// /// If true, it will take the maximum, otherwise the minimum. /// public Vector3 Get(bool xyz) => Get(xyz, xyz, xyz); /// /// Returns the configuration of one corner and applies a transformation to it. /// If the transformation causes the points to shift to no being in the right location is NOT checked! /// For that use /// /// The transformation /// If true, it takes the X-value of maximum, otherwise the minimum. /// If true, it takes the Y-value of maximum, otherwise the minimum. /// If true, it takes the Z-value of maximum, otherwise the minimum. /// public Vector3 Get(Matrix4 transformation, bool x, bool y, bool z) { Vector3 get = Get(x, y, z); return (new Vector4(get, 1) * transformation).Xyz; } /// /// Returns the configuration of the two furthest away corners. /// If the transformation causes the points to shift to no being in the right location is NOT checked! /// For that use /// /// The transformation /// If true, it will take the maximum, otherwise the minimum. /// public Vector3 Get(Matrix4 transformation, bool xyz) => Get(transformation, xyz, xyz, xyz); /// /// Returns the bounds of the bounding box while applying a transformation. /// This takes care of min and max locations. /// /// /// /// public void GetBounds(Matrix4 transformation, out Vector3 min, out Vector3 max) { min = Get(transformation, false); max = Get(transformation, true); for (int i = 0; i < 3; i++) { float newmin = Math.Min(min[i], max[i]); float newmax = Math.Max(min[i], max[i]); min[i] = newmin; max[i] = newmax; } } /// /// Updates the bounding box to the mesh provided. /// /// public void Update(GenericMesh mesh) { int pos = 0; foreach (float f in mesh.Vertex) { Min[pos] = Math.Min(Min[pos], f); Max[pos] = Math.Max(Max[pos], f); pos++; pos %= mesh.Vertex.PointerSize; } } /// /// Updates the bounding box. /// /// public void Update(Vector2 vector) { for (var i = 0; i < 2; i++) { Min[i] = Math.Min(Min[i], vector[i]); Max[i] = Math.Max(Max[i], vector[i]); } } /// /// Updates the bounding box. /// /// public void Update(Vector3 vector) { for (var i = 0; i < 3; i++) { Min[i] = Math.Min(Min[i], vector[i]); Max[i] = Math.Max(Min[i], vector[i]); } } } }