Record TBox3D
Unit
Declaration
type TBox3D = record
Description
Axisaligned box. Rectangular prism with all sides parallel to basic planes X = 0, Y = 0 and Z = 0. This is sometimes called AABB, "axisaligned bounding box". Many geometric operations are fast and easy on this type.
The actual box dimensions are stored inside the Data field, as two 3D points. First point has always all the smaller coords, second point has all the larger coords. I.e. always
Data[0].Data[0] <= Data[1].Data[0] and Data[0].Data[1] <= Data[1].Data[1] and Data[0].Data[2] <= Data[1].Data[2]
The only exception is the special value TBox3D.Empty.
Note that the box may still have all sizes equal 0. Consider a 3D model with only a single 3D point — it's not empty, but all the sizes must be 0.
Overview
Fields
Data: array [0..1] of TVector3; 
Methods
class function Empty: TBox3D; static; 

function IsEmpty: boolean; 

function IsEmptyOrZero: boolean; 

procedure CheckNonEmpty; 

function Middle: TVector3; deprecated 'use Center'; 

function Center: TVector3; 

function AverageSize: Single; overload; 

function MaxSize: Single; overload; 

function MinSize: Single; overload; 

function SizeX: Single; 

function SizeY: Single; 

function SizeZ: Single; 

function AverageSize(const AllowZero: boolean; const EmptyBoxSize: Single): Single; overload; 

function MaxSize(const AllowZero: boolean; const EmptyBoxSize: Single): Single; overload; 

function Area(const AllowZero: boolean; const EmptyBoxArea: Single): Single; 

procedure ExpandMe(const AExpand: Single); overload; 

procedure ExpandMe(const AExpand: TVector3); overload; 

function Grow(const AExpand: Single): TBox3D; overload; 

function Grow(const AExpand: TVector3): TBox3D; overload; 

function Expand(const AExpand: Single): TBox3D; overload; deprecated 'use Grow, consistent with TRectangle.Grow'; 

function Expand(const AExpand: TVector3): TBox3D; overload; deprecated 'use Grow, consistent with TRectangle.Grow'; 

function Contains(const Point: TVector3): boolean; overload; 

function PointInside(const Point: TVector3): boolean; overload; deprecated 'use Contains method, which is consistent with TRectangle'; 

function Contains2D(const Point: TVector2): boolean; overload; 

function PointInside2D(const Point: TVector2): boolean; overload; deprecated 'use Contains2d method'; 

function Contains2D(const Point: TVector3; const IgnoreIndex: Integer): boolean; overload; 

function PointInside2D(const Point: TVector3; const IgnoreIndex: Integer): boolean; overload; deprecated 'use Contains2D method'; 

procedure Include(const box2: TBox3D); overload; 

procedure Include(const Point: TVector3); overload; 

procedure Include(const Points: TVector3List); overload; 

class function FromPoints(const Points: TVector3List): TBox3D; static; 

function Sizes: TVector3; deprecated 'use Size'; 

function Size: TVector3; 

procedure Corners(out AllPoints: TBoxCorners); 

procedure GetAllPoints(AllPoints: PVector3Array); deprecated 'use Corners'; 

function Transform(const Matrix: TMatrix4): TBox3D; 

function Translate(const Translation: TVector3): TBox3D; 

function AntiTranslate(const Translation: TVector3): TBox3D; 

function ToNiceStr: string; deprecated 'use ToString'; 

function ToRawStr: string; deprecated 'use ToRawString'; 

function ToString: string; 

function ToRawString: string; 

procedure ClampVar(var point: TVector3); overload; 

function TryRayClosestIntersection( out Intersection: TVector3; out IntersectionDistance: Single; const RayOrigin, RayDirection: TVector3): boolean; overload; 

function TryRayClosestIntersection( out Intersection: TVector3; const RayOrigin, RayDirection: TVector3): boolean; overload; 

function TryRayClosestIntersection( out IntersectionDistance: Single; const RayOrigin, RayDirection: TVector3): boolean; overload; 

function TryRayEntrance( out Entrance: TVector3; out EntranceDistance: Single; const RayOrigin, RayDirection: TVector3): boolean; overload; 

function TryRayEntrance( out Entrance: TVector3; const RayOrigin, RayDirection: TVector3): boolean; overload; 

function SegmentCollision( const Segment1, Segment2: TVector3): boolean; 

function PlaneCollision(const Plane: TVector4): TPlaneCollision; 

function PlaneCollisionInside(const Plane: TVector4): boolean; 

function PlaneCollisionOutside(const Plane: TVector4): boolean; 

function IsTriangleCollision( const Triangle: TTriangle3): boolean; 

procedure BoundingSphere( var SphereCenter: TVector3; var SphereRadiusSqr: Single); 

function Collision(const Box2: TBox3D): boolean; 

function Collides(const Box2: TBox3D): boolean; 

function Radius: Single; 

function Radius2D(const IgnoreIndex: Integer): Single; 

function SphereSimpleCollision( const SphereCenter: TVector3; const SphereRadius: Single): boolean; 

function SphereCollision( const SphereCenter: TVector3; const SphereRadius: Single): boolean; 

function SphereCollision2D( const SphereCenter: TVector2; const SphereRadius: Single): boolean; 

function MaximumPlane(const Direction: TVector3): TVector4; 

function MinimumPlane(const Direction: TVector3): TVector4; 

function MaximumCorner(const Direction: TVector3): TVector3; 

function MinimumCorner(const Direction: TVector3): TVector3; 

procedure PointDistances(const P: TVector3; out MinDistance, MaxDistance: Single); 

procedure DirectionDistances( const Point, Dir: TVector3; out MinDistance, MaxDistance: Single); 

function PointDistance(const Point: TVector3): Single; 

function PointMaxDistance(const Point: TVector3; const EmptyBoxDistance: Single): Single; 

function Equal(const Box2: TBox3D): boolean; overload; 

function Equal(const Box2: TBox3D; const Epsilon: Single): boolean; overload; 

function Diagonal: Single; 

function RectangleXY: TFloatRectangle; 

function RectangleXZ: TFloatRectangle; 

function OrthoProject(const Pos, Dir, Side, Up: TVector3): TFloatRectangle; 

class function CompareBackToFront3D( const A, B: TBox3D; const SortPosition: TVector3): Integer; static; 

class function CompareBackToFront2D( const A, B: TBox3D): Integer; static; 

class operator + (const Box1, Box2: TBox3D): TBox3D; 

class operator + (const B: TBox3D; const V: TVector3): TBox3D; deprecated 'use TBox3D.Translate. Operator is ambiguous (do we add a point, or translate?)'; 

class operator + (const V: TVector3; const B: TBox3D): TBox3D; deprecated 'use TBox3D.Translate. Operator is ambiguous (do we add a point, or translate?)'; 

class function FromCenterSize(const ACenter, ASize: TVector3): TBox3D; static; 

procedure ToCenterSize(out ACenter, ASize: TVector3); 
Properties
property Min: TVector3 read GetMin write SetMin; 

property Max: TVector3 read GetMax write SetMax; 
Description
Fields
Data: array [0..1] of TVector3; 

Methods
class function Empty: TBox3D; static; 

Special TBox3D value meaning "bounding box is empty". This is different than just bounding box with zero sizes, as bounding box with zero sizes still has some position. Empty bounding box doesn't contain any portion of 3D space. 
function IsEmpty: boolean; 

Check is box empty. You can think of this function as "compare Box with TBox3D.Empty". But actually it works a little faster, by utilizing the assumption that TBox3D.Empty is the only allowed value that breaks 
function IsEmptyOrZero: boolean; 

Check is box empty or has all the sizes equal 0. 
procedure CheckNonEmpty; 

function Middle: TVector3; deprecated 'use Center'; 

Warning: this symbol is deprecated: use Center Center of the box. Exceptions raised

function Center: TVector3; 

Center of the box. Exceptions raised

function AverageSize: Single; overload; 

Average size of the box. Exceptions raised

function MaxSize: Single; overload; 

Largest size of the box. Exceptions raised

function MinSize: Single; overload; 

Smallest size of the box. Exceptions raised

function SizeX: Single; 

Size in X (width) of the box. Exceptions raised

function SizeY: Single; 

Size in Y (height) of the box. Exceptions raised

function SizeZ: Single; 

Size in Z (depth) of the box. Exceptions raised

function AverageSize(const AllowZero: boolean; const EmptyBoxSize: Single): Single; overload; 

Average size of TBox3D, or EmptyBoxSize if box is empty. Parameters

function MaxSize(const AllowZero: boolean; const EmptyBoxSize: Single): Single; overload; 

Largest size of TBox3D, or EmptyBoxSize if box is empty. Parameters

function Area(const AllowZero: boolean; const EmptyBoxArea: Single): Single; 

Area of the six TBox3D sides, EmptyBoxArea if box is empty. Parameters

procedure ExpandMe(const AExpand: TVector3); overload; 

Decrease "minimum corner" by AExpand vector, and increase "maximum corner" by the same vector. So you get Box with all sizes increased by 2 * AExpand. Box must not be empty. Note that AExpand may be negative, but then you must be sure that it doesn't make Box empty. 
function Grow(const AExpand: Single): TBox3D; overload; 

function Grow(const AExpand: TVector3): TBox3D; overload; 

function Expand(const AExpand: Single): TBox3D; overload; deprecated 'use Grow, consistent with TRectangle.Grow'; 

Warning: this symbol is deprecated: use Grow, consistent with TRectangle.Grow 
function Expand(const AExpand: TVector3): TBox3D; overload; deprecated 'use Grow, consistent with TRectangle.Grow'; 

Warning: this symbol is deprecated: use Grow, consistent with TRectangle.Grow 
function Contains(const Point: TVector3): boolean; overload; 

Check is the point inside the box. Always false if Box is empty (obviously, no point is inside an empty box). 
function PointInside(const Point: TVector3): boolean; overload; deprecated 'use Contains method, which is consistent with TRectangle'; 

Warning: this symbol is deprecated: use Contains method, which is consistent with TRectangle 
function Contains2D(const Point: TVector2): boolean; overload; 

Is the 2D point inside the 2D projection of the box, ignores the Z coord of box. 
function PointInside2D(const Point: TVector2): boolean; overload; deprecated 'use Contains2d method'; 

Warning: this symbol is deprecated: use Contains2d method 
function Contains2D(const Point: TVector3; const IgnoreIndex: Integer): boolean; overload; 

Is the 2D point inside the 2D projection of the box. 2D projection (of point and box) is obtained by rejecting the IgnoreIndex coordinate (must be 0, 1 or 2). 
function PointInside2D(const Point: TVector3; const IgnoreIndex: Integer): boolean; overload; deprecated 'use Contains2D method'; 

Warning: this symbol is deprecated: use Contains2D method 
procedure Include(const box2: TBox3D); overload; 

Add another box to our box. This calculates the smallest box that encloses both the current box, and Box2. Doing 
procedure Include(const Point: TVector3); overload; 

Make box larger, if necessary, to contain given Point. 
procedure Include(const Points: TVector3List); overload; 

class function FromPoints(const Points: TVector3List): TBox3D; static; 

Make a box that contains given points. 
function Sizes: TVector3; deprecated 'use Size'; 

Warning: this symbol is deprecated: use Size Three box sizes. 
function Size: TVector3; 

Three box sizes. Name consistent with TBoxNode.Size. Exceptions raised

procedure Corners(out AllPoints: TBoxCorners); 

Calculate eight 
procedure GetAllPoints(AllPoints: PVector3Array); deprecated 'use Corners'; 

Warning: this symbol is deprecated: use Corners 
function Transform(const Matrix: TMatrix4): TBox3D; 

Transform the Box by given matrix. Since this is still an axisaligned box, rotating etc. of the box usually makes larger box. Note that this is very optimized for Matrix with no projection (where last row of the last matrix = [0, 0, 0, 1]). It still works for all matrices (eventually fallbacks to simple "transform 8 corners and get box enclosing them" method). Exceptions raised

function Translate(const Translation: TVector3): TBox3D; 

Move Box. Does nothing if Box is empty. 
function AntiTranslate(const Translation: TVector3): TBox3D; 

Move Box, by Translation. Does nothing if Box is empty. 
function ToNiceStr: string; deprecated 'use ToString'; 

Warning: this symbol is deprecated: use ToString 
function ToRawStr: string; deprecated 'use ToRawString'; 

Warning: this symbol is deprecated: use ToRawString 
function ToString: string; 

function ToRawString: string; 

procedure ClampVar(var point: TVector3); overload; 

function TryRayClosestIntersection( out Intersection: TVector3; out IntersectionDistance: Single; const RayOrigin, RayDirection: TVector3): boolean; overload; 

TryBoxRayClosestIntersection calculates intersection between the ray (returns closest intersection to RayOrigin) and the box. The box is treated just like a set of 6 rectangles in 3D. This means that the intersection will always be placed on one of the box sides, even if RayOrigin starts inside the box. See TryBoxRayEntrance for the other version. Returns also IntersectionDistance, which is the distance to the Intersection relative to RayDirection (i.e. Intersection is always = RayOrigin + IntersectionDistance * RayDirection). 
function TryRayClosestIntersection( out Intersection: TVector3; const RayOrigin, RayDirection: TVector3): boolean; overload; 

function TryRayClosestIntersection( out IntersectionDistance: Single; const RayOrigin, RayDirection: TVector3): boolean; overload; 

function TryRayEntrance( out Entrance: TVector3; out EntranceDistance: Single; const RayOrigin, RayDirection: TVector3): boolean; overload; 

Intersection between the ray (returns closest intersection to RayOrigin) and the box, treating the box as a filled volume. If RayOrigin is inside the box, TryBoxRayEntrance simply returns RayOrigin. If RayOrigin is outside of the box, the answer is the same as with TryBoxRayClosestIntersection. 
function TryRayEntrance( out Entrance: TVector3; const RayOrigin, RayDirection: TVector3): boolean; overload; 

function SegmentCollision( const Segment1, Segment2: TVector3): boolean; 

function PlaneCollision(const Plane: TVector4): TPlaneCollision; 

Collision between axisaligned box (TBox3D) and 3D plane. Returns detailed result as TPlaneCollision. 
function PlaneCollisionInside(const Plane: TVector4): boolean; 

Check is axisaligned box (TBox3D) fully inside/outside the plane. Inside/outside are defined as for TPlaneCollision: Outside is where plane direction (normal) points. Inside is where the inverted plane direction (normal) points. They work exactly like Box3DPlaneCollision, except they returns For example Box3DPlaneCollisionInside doesn't differentiate between case when box is empty, of partially intersects the plane, and is on the outside. But it works (very slightly) faster. 
function PlaneCollisionOutside(const Plane: TVector4): boolean; 

function IsTriangleCollision( const Triangle: TTriangle3): boolean; 

procedure BoundingSphere( var SphereCenter: TVector3; var SphereRadiusSqr: Single); 

Smallest possible sphere completely enclosing the current box. When this is empty (IsEmpty) we return SphereRadiusSqr = 0 and an undefined SphereCenter.
Parameters

function Collision(const Box2: TBox3D): boolean; 

Does it have any common part with another box. Better use Collides, which has a name consistent with TFloatRectangle.Collides, this method will be deprecated some day and later removed. 
function Collides(const Box2: TBox3D): boolean; 

Does it have any common part with another box. 
function Radius: Single; 

Radius of the minimal sphere that contains this box. Sphere center is assumed to be in (0, 0, 0). 0 if box is empty. 
function Radius2D(const IgnoreIndex: Integer): Single; 

Radius of the minimal circle that contains the 2D projection of this box. 2D box projection is obtained by rejecting the IgnoreIndex coordinate (must be 0, 1 or 2). Circle center is assumed to be in (0, 0). 0 if box is empty. 
function SphereSimpleCollision( const SphereCenter: TVector3; const SphereRadius: Single): boolean; 

Check for collision between box and sphere, fast but not entirely correct. This considers a Box enlarged by SphereRadius in each direction. Then checks whether SphereCenter is inside such enlarged Box. So this check will incorrectly report collision while in fact there's no collision in the case when the sphere center is near the corner of the Box. So this check is not 100% correct. But often this is good enough — in games, if you know that the SphereRadius is going to be relatively small compared to the Box, this may be perfectly acceptable. And it's fast. 
function SphereCollision( const SphereCenter: TVector3; const SphereRadius: Single): boolean; 

Check box vs sphere collision. 
function SphereCollision2D( const SphereCenter: TVector2; const SphereRadius: Single): boolean; 

Check box vs sphere collision in 2D (ignores Z coordinates of box). 
function MaximumPlane(const Direction: TVector3): TVector4; 

Calculate a plane in 3D space with direction = given Direction, moved maximally in Direction and still intersecting the given Box. For example, if Direction = Z = (0, 0, 1), then this will return the bottom plane of this box. For Direction = (1, 1, 1), this will return a plane intersecting the Data[1] (maximum) point, with slope = (1, 1, 1). The resulting plane always intersects at least one of the 8 corners of the box. Exceptions raised

function MinimumPlane(const Direction: TVector3): TVector4; 

Calculate a plane in 3D space with direction = given Direction, moved such that it touches the Box but takes minimum volume of this box. For example, if Direction = +Z = (0, 0, 1), then this will return the bottom plane of this box. For Direction = (1, 1, 1), this will return a plane intersecting the Data[0] (minimum) point, with slope = (1, 1, 1). The resulting plane always intersects at least one of the 8 corners of the box. Exceptions raised

function MaximumCorner(const Direction: TVector3): TVector3; 

Farthest corner of the box in the given Direction. Exceptions raised

function MinimumCorner(const Direction: TVector3): TVector3; 

Corner of the box such that the rest of the box lies in the given Direction from this corner. Exceptions raised

procedure PointDistances(const P: TVector3; out MinDistance, MaxDistance: Single); 

Calculate the distances between a given 3D point and a box. MinDistance is the distance to the closest point of the box, MaxDistance is the distance to the farthest point of the box. Note that always MinDistance <= MaxDistance. Note that both distances are always >= 0. When the point is inside the box, it works correct too: minimum distance is zero in this case.
TODO: calculation of MinDistance is not perfect now. We assume that the closest/farthest point of the box is one of the 8 box corners. Which may not be true in case of the closest point, because it may lie in the middle of some box face (imagine a sphere with increasing radius reaching from a point to a box). So our minimum may be a *little* too large. Exceptions raised

procedure DirectionDistances( const Point, Dir: TVector3; out MinDistance, MaxDistance: Single); 

Calculate the distances along a direction to a box. The idea is that you have a 3D plane orthogonal to direction Dir and passing through Point. You can move this plane, but you have to keep it's direction constant. MinDistance is the minimal distance along the Dir that you can move this plane, such that it touches the box. MaxDistance is the maximum such distance. Note that always MinDistance <= MaxDistance. Note that one distance (MinDistance) or both distances may be negative. As a practical example: imagine a DirectionalLight (light rays are parallel) that has a location. Now MinDistance and MaxDistance give ranges of depth where the Box is, as seen from the light source. Exceptions raised

function PointDistance(const Point: TVector3): Single; 

Shortest distance between the box and a point. Always zero when the point is inside the box. Exceptions raised

function PointMaxDistance(const Point: TVector3; const EmptyBoxDistance: Single): Single; 

Maximum distance between the box and a point. Returns EmptyBoxDistance when box is empty. 
function Equal(const Box2: TBox3D): boolean; overload; 

function Equal(const Box2: TBox3D; const Epsilon: Single): boolean; overload; 

function Diagonal: Single; 


function RectangleXY: TFloatRectangle; 

function RectangleXZ: TFloatRectangle; 

function OrthoProject(const Pos, Dir, Side, Up: TVector3): TFloatRectangle; 

Project box along a given direction to a 2D rectangle. Assumes that Dir, Side and Up vectors are already orthogonal and normalized. 
class function CompareBackToFront3D( const A, B: TBox3D; const SortPosition: TVector3): Integer; static; 

Compare two bounding boxes based on their distance to the SortPosition point, suitable for depth sorting in 3D. Follows the algorithm documented at bs3D. Returns 1 if A < B, 1 if A > B, 0 if A = B. Using this with a typical sorting function will result in boxes backtofront ordering, which means that the farthest box will be first. 
class function CompareBackToFront2D( const A, B: TBox3D): Integer; static; 

Compare two bounding boxes based on their Z coordinates, suitable for depth sorting in 2D. Follows the algorithm documented at bs2D. Returns 1 if A < B, 1 if A > B, 0 if A = B. Using this with a typical sorting function will result in boxes backtofront ordering, which means that the farthest box will be first. 
class operator + (const Box1, Box2: TBox3D): TBox3D; 

class operator + (const B: TBox3D; const V: TVector3): TBox3D; deprecated 'use TBox3D.Translate. Operator is ambiguous (do we add a point, or translate?)'; 

Warning: this symbol is deprecated: use TBox3D.Translate. Operator is ambiguous (do we add a point, or translate?) 
class operator + (const V: TVector3; const B: TBox3D): TBox3D; deprecated 'use TBox3D.Translate. Operator is ambiguous (do we add a point, or translate?)'; 

Warning: this symbol is deprecated: use TBox3D.Translate. Operator is ambiguous (do we add a point, or translate?) 
class function FromCenterSize(const ACenter, ASize: TVector3): TBox3D; static; 

Convert from center and size vector. Empty box has size (1,1,1). 
procedure ToCenterSize(out ACenter, ASize: TVector3); 

Convert to center and size vector. Empty box has size (1,1,1). 
Properties
property Min: TVector3 read GetMin write SetMin; 

The minimum 3D position within this box. Use only if not IsEmpty, in which case this is just a shortcut for 
property Max: TVector3 read GetMax write SetMax; 

The maximum 3D position within this box. Use only if not IsEmpty, in which case this is just a shortcut for 
Generated by PasDoc 0.16.0.