Struct Box3

Namespace
Elegy.Common.Maths
Assembly
Elegy.Common.dll

Axis-aligned bounding box. Box3 consists of a position, a size, and several utility functions. It is typically used for fast overlap tests.

[Serializable]
public struct Box3 : IEquatable<Box3>
Implements
Inherited Members

Constructors

Box3(Vector3, Vector3)

Constructs an Box3 from a position and size.

public Box3(Vector3 position, Vector3 size)

Parameters

position Vector3

The position.

size Vector3

The size, typically positive.

Box3(Vector3, float, float, float)

Constructs an Box3 from a position, width, height, and depth.

public Box3(Vector3 position, float width, float height, float depth)

Parameters

position Vector3

The position.

width float

The width, typically positive.

height float

The height, typically positive.

depth float

The depth, typically positive.

Box3(float, float, float, Vector3)

Constructs an Box3 from x, y, z, and size.

public Box3(float x, float y, float z, Vector3 size)

Parameters

x float

The position's X coordinate.

y float

The position's Y coordinate.

z float

The position's Z coordinate.

size Vector3

The size, typically positive.

Box3(float, float, float, float, float, float)

Constructs an Box3 from x, y, z, width, height, and depth.

public Box3(float x, float y, float z, float width, float height, float depth)

Parameters

x float

The position's X coordinate.

y float

The position's Y coordinate.

z float

The position's Z coordinate.

width float

The width, typically positive.

height float

The height, typically positive.

depth float

The depth, typically positive.

Properties

End

Ending corner. This is calculated as Position plus Size. Setting this value will change the size.

public Vector3 End { readonly get; set; }

Property Value

Vector3

Getting is equivalent to value = Position + Size, setting is equivalent to Size = value - Position

Position

Beginning corner. Typically has values lower than End.

public Vector3 Position { readonly get; set; }

Property Value

Vector3

Directly uses a private field.

Size

Size from Position to End. Typically all components are positive. If the size is negative, you can use Abs() to fix it.

public Vector3 Size { readonly get; set; }

Property Value

Vector3

Directly uses a private field.

Volume

The volume of this Box3. See also HasVolume().

public readonly float Volume { get; }

Property Value

float

Methods

Abs()

Returns an Box3 with equivalent position and size, modified so that the most-negative corner is the origin and the size is positive.

public readonly Box3 Abs()

Returns

Box3

The modified Box3.

Encloses(Box3)

Returns true if this Box3 completely encloses another one.

public readonly bool Encloses(Box3 with)

Parameters

with Box3

The other Box3 that may be enclosed.

Returns

bool

A bool for whether or not this Box3 encloses with.

Equals(Box3)

Returns true if the AABBs are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Box3) instead, which is more reliable.

public readonly bool Equals(Box3 other)

Parameters

other Box3

The other AABB.

Returns

bool

Whether or not the AABBs are exactly equal.

Equals(object?)

Returns true if the AABB is exactly equal to the given object (obj). Note: Due to floating-point precision errors, consider using IsEqualApprox(Box3) instead, which is more reliable.

public override readonly bool Equals(object? obj)

Parameters

obj object

The object to compare with.

Returns

bool

Whether or not the AABB and the object are equal.

Expand(Vector3)

Returns this Box3 expanded to include a given point.

public readonly Box3 Expand(Vector3 point)

Parameters

point Vector3

The point to include.

Returns

Box3

The expanded Box3.

FromCorners(Vector3, Vector3)

Constructs a Box3 from two corners.

public static Box3 FromCorners(Vector3 a, Vector3 b)

Parameters

a Vector3
b Vector3

Returns

Box3

GetCenter()

Returns the center of the Box3, which is equal to Position + (Size / 2).

public readonly Vector3 GetCenter()

Returns

Vector3

The center.

GetEndpoint(int)

Gets the position of one of the 8 endpoints of the Box3.

public readonly Vector3 GetEndpoint(int idx)

Parameters

idx int

Which endpoint to get.

Returns

Vector3

An endpoint of the Box3.

Exceptions

ArgumentOutOfRangeException

idx is less than 0 or greater than 7.

GetHashCode()

Serves as the hash function for Box3.

public override readonly int GetHashCode()

Returns

int

A hash code for this AABB.

GetLongestAxis()

Returns the normalized longest axis of the Box3.

public readonly Vector3 GetLongestAxis()

Returns

Vector3

A vector representing the normalized longest axis of the Box3.

GetLongestAxisIndex()

Returns the Axis index of the longest axis of the Box3.

public readonly Axis GetLongestAxisIndex()

Returns

Axis

A Axis index for which axis is longest.

GetLongestAxisSize()

Returns the scalar length of the longest axis of the Box3.

public readonly float GetLongestAxisSize()

Returns

float

The scalar length of the longest axis of the Box3.

GetShortestAxis()

Returns the normalized shortest axis of the Box3.

public readonly Vector3 GetShortestAxis()

Returns

Vector3

A vector representing the normalized shortest axis of the Box3.

GetShortestAxisIndex()

Returns the Axis index of the shortest axis of the Box3.

public readonly Axis GetShortestAxisIndex()

Returns

Axis

A Axis index for which axis is shortest.

GetShortestAxisSize()

Returns the scalar length of the shortest axis of the Box3.

public readonly float GetShortestAxisSize()

Returns

float

The scalar length of the shortest axis of the Box3.

GetSupport(Vector3)

Returns the support point in a given direction. This is useful for collision detection algorithms.

public readonly Vector3 GetSupport(Vector3 dir)

Parameters

dir Vector3

The direction to find support for.

Returns

Vector3

A vector representing the support.

Grow(float)

Returns a copy of the Box3 grown a given amount of units towards all the sides.

public readonly Box3 Grow(float by)

Parameters

by float

The amount to grow by.

Returns

Box3

The grown Box3.

HasPoint(Vector3)

Returns true if the Box3 contains a point, or false otherwise.

public readonly bool HasPoint(Vector3 point)

Parameters

point Vector3

The point to check.

Returns

bool

A bool for whether or not the Box3 contains point.

HasSurface()

Returns true if the Box3 has a surface or a length, and false if the Box3 is empty (all components of Size are zero or negative).

public readonly bool HasSurface()

Returns

bool

A bool for whether or not the Box3 has surface.

HasVolume()

Returns true if the Box3 has area, and false if the Box3 is linear, empty, or has a negative Size. See also Volume.

public readonly bool HasVolume()

Returns

bool

A bool for whether or not the Box3 has volume.

Intersection(Box3)

Returns the intersection of this Box3 and with.

public readonly Box3 Intersection(Box3 with)

Parameters

with Box3

The other Box3.

Returns

Box3

The clipped Box3.

Intersects(Box3)

Returns true if the Box3 overlaps with with (i.e. they have at least one point in common).

public readonly bool Intersects(Box3 with)

Parameters

with Box3

The other Box3 to check for intersections with.

Returns

bool

A bool for whether or not they are intersecting.

IntersectsPlane(Plane)

Returns true if the Box3 is on both sides of plane.

public readonly bool IntersectsPlane(Plane plane)

Parameters

plane Plane

The Plane to check for intersection.

Returns

bool

A bool for whether or not the Box3 intersects the Plane.

IntersectsSegment(Vector3, Vector3)

Returns true if the Box3 intersects the line segment between from and to.

public readonly bool IntersectsSegment(Vector3 from, Vector3 to)

Parameters

from Vector3

The start of the line segment.

to Vector3

The end of the line segment.

Returns

bool

A bool for whether or not the Box3 intersects the line segment.

IsEqualApprox(Box3)

Returns true if this AABB and other are approximately equal, by running IsEqualApprox(Vector3, Vector3) on each component.

public readonly bool IsEqualApprox(Box3 other)

Parameters

other Box3

The other AABB to compare.

Returns

bool

Whether or not the AABBs structures are approximately equal.

IsFinite()

Returns true if this Box3 is finite, by calling IsFinite(float) on each component.

public readonly bool IsFinite()

Returns

bool

Whether this vector is finite or not.

Merge(Box3)

Returns a larger Box3 that contains this Box3 and with.

public readonly Box3 Merge(Box3 with)

Parameters

with Box3

The other Box3.

Returns

Box3

The merged Box3.

ToString()

Converts this Box3 to a string.

public override readonly string ToString()

Returns

string

A string representation of this AABB.

ToString(string?)

Converts this Box3 to a string with the given format.

public readonly string ToString(string? format)

Parameters

format string

Returns

string

A string representation of this AABB.

Operators

operator ==(Box3, Box3)

Returns true if the AABBs are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Box3) instead, which is more reliable.

public static bool operator ==(Box3 left, Box3 right)

Parameters

left Box3

The left AABB.

right Box3

The right AABB.

Returns

bool

Whether or not the AABBs are exactly equal.

operator !=(Box3, Box3)

Returns true if the AABBs are not equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Box3) instead, which is more reliable.

public static bool operator !=(Box3 left, Box3 right)

Parameters

left Box3

The left AABB.

right Box3

The right AABB.

Returns

bool

Whether or not the AABBs are not equal.