Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AxisAlignedBB is now immutable #95

Merged
merged 13 commits into from
Dec 9, 2024
157 changes: 44 additions & 113 deletions src/AxisAlignedBB.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use function abs;
use const PHP_INT_MAX;

final class AxisAlignedBB{
final readonly class AxisAlignedBB{

public float $minX;
public float $minY;
Expand Down Expand Up @@ -92,86 +92,58 @@ public function addCoord(float $x, float $y, float $z) : AxisAlignedBB{
*
* @return $this
Dhaiven marked this conversation as resolved.
Show resolved Hide resolved
*/
public function expand(float $x, float $y, float $z){
$this->minX -= $x;
$this->minY -= $y;
$this->minZ -= $z;
$this->maxX += $x;
$this->maxY += $y;
$this->maxZ += $z;

return $this;
}

/**
* Returns an expanded clone of this AxisAlignedBB.
*/
public function expandedCopy(float $x, float $y, float $z) : AxisAlignedBB{
return (clone $this)->expand($x, $y, $z);
public function expandedCopy(float $x, float $y, float $z){
return new AxisAlignedBB(
$this->minX - $x,
$this->minY - $y,
$this->minZ - $z,
$this->maxX + $x,
$this->maxY + $y,
$this->maxZ + $z
);
}

/**
* Shifts this AxisAlignedBB by the given X, Y and Z.
*
* @return $this
*/
public function offset(float $x, float $y, float $z) : AxisAlignedBB{
$this->minX += $x;
$this->minY += $y;
$this->minZ += $z;
$this->maxX += $x;
$this->maxY += $y;
$this->maxZ += $z;

return $this;
}

/**
* Returns an offset clone of this AxisAlignedBB.
*/
public function offsetCopy(float $x, float $y, float $z) : AxisAlignedBB{
return (clone $this)->offset($x, $y, $z);
return new AxisAlignedBB(
$this->minX + $x,
$this->minY + $y,
$this->minZ + $z,
$this->maxX + $x,
$this->maxY + $y,
$this->maxZ + $z
);
}

/**
* Offsets this AxisAlignedBB in the given direction by the specified distance.
*
* @return $this
*/
public function offsetTowards(Facing $face, float $distance) : AxisAlignedBB{
public function offsetTowardsCopy(Facing $face, float $distance) : AxisAlignedBB{
[$offsetX, $offsetY, $offsetZ] = $face->offset();

return $this->offset($offsetX * $distance, $offsetY * $distance, $offsetZ * $distance);
}

/**
* Returns an offset clone of this AxisAlignedBB.
*/
public function offsetTowardsCopy(Facing $face, float $distance) : AxisAlignedBB{
return (clone $this)->offsetTowards($face, $distance);
return $this->offsetCopy($offsetX * $distance, $offsetY * $distance, $offsetZ * $distance);
}

/**
* Insets the bounds of this AxisAlignedBB by the specified X, Y and Z.
*
* @return $this
*/
public function contract(float $x, float $y, float $z) : AxisAlignedBB{
$this->minX += $x;
$this->minY += $y;
$this->minZ += $z;
$this->maxX -= $x;
$this->maxY -= $y;
$this->maxZ -= $z;

return $this;
}

/**
* Returns a contracted clone of this AxisAlignedBB.
*/
public function contractedCopy(float $x, float $y, float $z) : AxisAlignedBB{
return (clone $this)->contract($x, $y, $z);
return new AxisAlignedBB(
$this->minX + $x,
$this->minY + $y,
$this->minZ + $z,
$this->maxX - $x,
$this->maxY - $y,
$this->maxZ - $z
);
}

/**
Expand All @@ -181,44 +153,26 @@ public function contractedCopy(float $x, float $y, float $z) : AxisAlignedBB{
*
* @return $this
*/
public function extend(Facing $face, float $distance) : AxisAlignedBB{
match($face){
Facing::DOWN => $this->minY -= $distance,
Facing::UP => $this->maxY += $distance,
Facing::NORTH => $this->minZ -= $distance,
Facing::SOUTH => $this->maxZ += $distance,
Facing::WEST => $this->minX -= $distance,
Facing::EAST => $this->maxX += $distance,
};

return $this;
}

/**
* Returns an extended clone of this bounding box.
* @see AxisAlignedBB::extend()
*/
public function extendedCopy(Facing $face, float $distance) : AxisAlignedBB{
return (clone $this)->extend($face, $distance);
return match($face){
Facing::DOWN => new AxisAlignedBB($this->minX, $this->minY - $distance, $this->minZ, $this->maxX, $this->maxY, $this->maxZ),
Facing::UP => new AxisAlignedBB($this->minX, $this->minY, $this->minZ, $this->maxX + $distance, $this->maxY, $this->maxZ),
Facing::NORTH => new AxisAlignedBB($this->minX, $this->minY, $this->minZ - $distance, $this->maxX, $this->maxY, $this->maxZ),
Facing::SOUTH => new AxisAlignedBB($this->minX, $this->minY, $this->minZ, $this->maxX, $this->maxY, $this->maxZ + $distance),
Facing::WEST => new AxisAlignedBB($this->minX - $distance, $this->minY, $this->minZ, $this->maxX, $this->maxY, $this->maxZ),
Facing::EAST => new AxisAlignedBB($this->minX, $this->minY, $this->minZ, $this->maxX + $distance, $this->maxY, $this->maxZ)
};
}

/**
* Inverse of extend().
Dhaiven marked this conversation as resolved.
Show resolved Hide resolved
* @see AxisAlignedBB::extend()
* @see AxisAlignedBB::extendedCopy()
*
* @param float $distance Positive values pull the face in, negative values push out.
*
* @return $this
*/
public function trim(Facing $face, float $distance) : AxisAlignedBB{
return $this->extend($face, -$distance);
}

/**
* Returns a trimmed clone of this bounding box.
* @see AxisAlignedBB::trim()
*/
public function trimmedCopy(Facing $face, float $distance) : AxisAlignedBB{
public function trimedCopy(Facing $face, float $distance) : AxisAlignedBB{
Dhaiven marked this conversation as resolved.
Show resolved Hide resolved
return $this->extendedCopy($face, -$distance);
}

Expand All @@ -229,43 +183,20 @@ public function trimmedCopy(Facing $face, float $distance) : AxisAlignedBB{
*
* @return $this
*/
public function stretch(Axis $axis, float $distance) : AxisAlignedBB{
if($axis === Axis::Y){
$this->minY -= $distance;
$this->maxY += $distance;
}elseif($axis === Axis::Z){
$this->minZ -= $distance;
$this->maxZ += $distance;
}elseif($axis === Axis::X){
$this->minX -= $distance;
$this->maxX += $distance;
}

return $this;
}

/**
* Returns a stretched copy of this bounding box.
* @see AxisAlignedBB::stretch()
*/
public function stretchedCopy(Axis $axis, float $distance) : AxisAlignedBB{
return (clone $this)->stretch($axis, $distance);
return match($axis){
Axis::Y => new AxisAlignedBB($this->minX, $this->minY - $distance, $this->minZ, $this->maxX, $this->maxY + $distance, $this->maxZ),
Axis::Z => new AxisAlignedBB($this->minX, $this->minY, $this->minZ - $distance, $this->maxX, $this->maxY, $this->maxZ + $distance),
Axis::X => new AxisAlignedBB($this->minX - $distance, $this->minY, $this->minZ, $this->maxX + $distance, $this->maxY, $this->maxZ)
};
Dhaiven marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Reduces the dimension of the AABB on the given axis. Inverse of stretch().
* @see AxisAlignedBB::stretch()
* @see AxisAlignedBB::stretchedCopy()
*
* @return $this
*/
public function squash(Axis $axis, float $distance) : AxisAlignedBB{
return $this->stretch($axis, -$distance);
}

/**
* Returns a squashed copy of this bounding box.
* @see AxisAlignedBB::squash()
*/
public function squashedCopy(Axis $axis, float $distance) : AxisAlignedBB{
return $this->stretchedCopy($axis, -$distance);
}
Expand Down
Loading