using System;
using System.Text;
using UnityEngine;
namespace Framework.Geometry.Volumes {
/// Axis aligned box in 2D
public struct Box3 {
/// A box with no volume
public static Box3 Empty = Box3.FromMinMax(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
/// Coordinates of the box' lesser corner
public Vector3 Min;
/// Coordinates of the box' larger corner
public Vector3 Max;
/// Initializes a new rectangle using the specified coordinates
/// Minimum X coordinate of the rectangle
/// Minimum Y coordinate of the rectangle
/// Minimum Z coordinate of the rectangle
/// Maximum X coordinate of the rectangle
/// Maximum Y coordinate of the rectangle
/// Maximum Z coordinate of the rectangle
/// The rectangle constructed from the specified boundaries
public static Box3 FromMinMax(
float minX, float minY, float minZ, float maxX, float maxY, float maxZ
) {
return new Box3() {
Min = new Vector3(minX, minY, minZ),
Max = new Vector3(maxX, maxY, maxZ)
};
}
/// Initializes a box from a starting point, width and height
/// Starting point X coordinate
/// Starting point Y coordinate
/// Starting point Z coordinate
/// Width of the box
/// Height of the box
/// Depth of the box
/// A rectangle with the specified starting point, width and height
public static Box3 FromPointAndSize(
float x, float y, float z, float width, float height, float depth
) {
return new Box3() {
Min = new Vector3(x, y, z),
Max = new Vector3(x + width, y + height, z + depth)
};
}
/// Initializes a new box using the specified min max points
/// Minimum coordinates of the box
/// Maximum coordinates of the box
/// The box constructed from the specified boundaries
public static Box3 FromMinMax(Vector3 min, Vector3 max) {
return new Box3() {
Min = min,
Max = max
};
}
/// Initializes a rectangle from a starting corner, width and height
/// Starting point
/// Size of the rectangle
/// A rectangle with the specified starting point, width and height
public static Box3 FromPointAndSize(Vector3 min, Vector3 size) {
return new Box3() {
Min = min,
Max = min + size
};
}
/// Width of the box
public float Width {
get { return Math.Abs(this.Max.x - this.Min.x); }
}
/// Height of the box
public float Height {
get { return Math.Abs(this.Max.y - this.Min.y); }
}
/// Depth of the box
public float Depth {
get { return Math.Abs(this.Max.z - this.Min.z); }
}
/// Volume occupied by the box
public float Volume {
get { return Width * Height * Depth; }
}
/// Area of the box' surface
public float Area {
get { return (Width * Height * 2) + (Width * Depth * 2) + (Height * Depth * 2); }
}
/// Checks two boxes for inequality
/// First instance to be compared
/// Second instance fo tbe compared
/// True if the instances differ or exactly one reference is set to null
public static bool operator !=(Box3 first, Box3 second) {
return (
(first.Min.x != second.Min.x) ||
(first.Min.y != second.Min.y) ||
(first.Min.z != second.Min.z) ||
(first.Max.x != second.Max.x) ||
(first.Max.y != second.Max.y) ||
(first.Max.z != second.Max.z)
);
}
/// Checks two points for equality
/// First instance to be compared
/// Second instance fo tbe compared
/// True if both instances are equal or both references are null
public static bool operator ==(Box3 first, Box3 second) {
return (
(first.Min.x == second.Min.x) &&
(first.Min.y == second.Min.y) &&
(first.Min.z == second.Min.z) &&
(first.Max.x == second.Max.x) &&
(first.Max.y == second.Max.y) &&
(first.Max.z == second.Max.z)
);
}
/// Checks whether this box is equal to another box
/// Other box, boxed into an object
/// True if the other object is a box with the same attributes
public override bool Equals(object otherAsObject) {
if(otherAsObject is Box3) {
return (this == (Box3)otherAsObject);
} else {
return false;
}
}
///
/// Generates a hash code that is identical for instances with the same state
///
/// A hash code for the instance
public override int GetHashCode() {
return (
(this.Min.x.GetHashCode()) ^
(this.Min.y.GetHashCode() << 5) ^
(this.Min.z.GetHashCode() << 10) ^
(this.Max.x.GetHashCode() << 15) ^
(this.Max.y.GetHashCode() << 20) ^
(this.Max.z.GetHashCode() << 25)
);
}
/// Describes the 3D box as a string
/// A string describing the 3D box
public override string ToString() {
var builder = new StringBuilder();
builder.Append('{');
builder.Append(this.Min.x);
builder.Append(';');
builder.Append(this.Min.y);
builder.Append(';');
builder.Append(this.Min.z);
builder.Append("} - {");
builder.Append(this.Max.x);
builder.Append(';');
builder.Append(this.Max.y);
builder.Append(';');
builder.Append(this.Max.z);
builder.Append('}');
return builder.ToString();
}
}
} // namespace Framework.Geometry.Volumes