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

Facing and Axis to enum #91

Merged
merged 35 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
69e3791
Facing and Axis to enum
Dhaiven Nov 19, 2023
7886af0
Modif toString function
Dhaiven Nov 19, 2023
7f6b638
Remove useless doc
Dhaiven Nov 20, 2023
c7e15ad
remove outside functionality of this pr
Dhaiven Nov 20, 2023
a45f1f0
Ditch numeric values
Dhaiven Nov 20, 2023
ac69813
Remove ditch values
Dhaiven Nov 21, 2023
d5ad9ac
Fix incorrect opposite side
Dhaiven Nov 21, 2023
426223d
Merge branch 'enum' of https://github.com/Dhaiven/Math into enum
Dhaiven Nov 21, 2023
d5e44f7
Modifications
Dhaiven Nov 21, 2023
19d771c
Follow order of cases
Dhaiven Nov 21, 2023
bca40e8
Fix bug in rotate function
Dhaiven Nov 27, 2023
5d66326
Modification
Dhaiven Nov 27, 2023
0e132c9
Merge branch 'stable' into enum
dktapps Feb 28, 2024
1c1a45e
Updates
Dhaiven May 12, 2024
89f49e0
Merge branch 'enum' of https://github.com/Dhaiven/Math into enum
Dhaiven May 12, 2024
54d40bb
Fix bugs
Dhaiven May 12, 2024
7857d74
Fix cs
Dhaiven May 12, 2024
2395e80
Fix cs
Dhaiven May 12, 2024
64d0112
Replace static functions by object functions
Dhaiven Dec 2, 2024
4fe606a
Add facing to key for Generator
Dhaiven Dec 2, 2024
9016d06
Fix tests
Dhaiven Dec 2, 2024
4d1f0e4
Prefix phpstan return
Dhaiven Dec 2, 2024
0fdc693
Update documentation
Dhaiven Dec 2, 2024
c3a71f9
Remove @var to specific content of variable
Dhaiven Dec 2, 2024
8ccea24
Update AxisAlignedBB.php
dktapps Dec 2, 2024
9f734d4
smh
dktapps Dec 2, 2024
c1ccf1e
formatting tidy
dktapps Dec 2, 2024
c9ebd9f
Remove unrealated changes
Dhaiven Dec 2, 2024
adf4a5a
Cs
Dhaiven Dec 2, 2024
f76eca3
Remove unrelated documentation
Dhaiven Dec 3, 2024
e212ce7
Don't repeat error or condition
Dhaiven Dec 3, 2024
e3fe09a
Update Facing.php
dktapps Dec 3, 2024
6865929
Update Vector3.php
dktapps Dec 3, 2024
f25c608
github web editor try not to suck challenge IMPOSSIBLE
dktapps Dec 3, 2024
49e2fef
Update Facing.php
dktapps Dec 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions src/Axis.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,20 @@

namespace pocketmine\math;

final class Axis{
private function __construct(){
//NOOP
}

public const Y = 0;
public const Z = 1;
public const X = 2;
enum Axis: int{

Check failure on line 26 in src/Axis.php

View workflow job for this annotation

GitHub Actions / PHP 8.0

Syntax error, unexpected T_STRING on line 26
case Y = 0;

Check failure on line 27 in src/Axis.php

View workflow job for this annotation

GitHub Actions / PHP 8.0

Syntax error, unexpected T_CASE on line 27
case Z = 1;
case X = 2;

/**
* @deprecated use Axis->name
* Returns a human-readable string representation of the given axis.
*/
public static function toString(int $axis) : string{
public static function toString(Axis $axis) : string{

Check failure on line 35 in src/Axis.php

View workflow job for this annotation

GitHub Actions / PHP 8.0

Syntax error, unexpected T_VARIABLE, expecting ')' on line 35
return match($axis){
Axis::Y => "y",
Axis::Z => "z",
Axis::X => "x",
default => throw new \InvalidArgumentException("Invalid axis $axis")
};
}

Check failure on line 41 in src/Axis.php

View workflow job for this annotation

GitHub Actions / PHP 8.0

Syntax error, unexpected '}', expecting EOF on line 41
}
35 changes: 12 additions & 23 deletions src/AxisAlignedBB.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,18 @@
/**
* Offsets this AxisAlignedBB in the given direction by the specified distance.
*
* @param int $face one of the Facing::* constants
*
* @return $this
*/
public function offsetTowards(int $face, float $distance) : AxisAlignedBB{
[$offsetX, $offsetY, $offsetZ] = Facing::OFFSET[$face] ?? throw new \InvalidArgumentException("Invalid Facing $face");
public function offsetTowards(Facing $face, float $distance) : AxisAlignedBB{

Check failure on line 141 in src/AxisAlignedBB.php

View workflow job for this annotation

GitHub Actions / PHP 8.0

Parameter $face of method pocketmine\math\AxisAlignedBB::offsetTowards() has invalid type pocketmine\math\Facing.
[$offsetX, $offsetY, $offsetZ] = $face->offset();

Check failure on line 142 in src/AxisAlignedBB.php

View workflow job for this annotation

GitHub Actions / PHP 8.0

Call to method offset() on an unknown class pocketmine\math\Facing.

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

/**
* Returns an offset clone of this AxisAlignedBB.
*/
public function offsetTowardsCopy(int $face, float $distance) : AxisAlignedBB{
public function offsetTowardsCopy(Facing $face, float $distance) : AxisAlignedBB{

Check failure on line 150 in src/AxisAlignedBB.php

View workflow job for this annotation

GitHub Actions / PHP 8.0

Parameter $face of method pocketmine\math\AxisAlignedBB::offsetTowardsCopy() has invalid type pocketmine\math\Facing.
return (clone $this)->offsetTowards($face, $distance);
}

Expand Down Expand Up @@ -184,15 +182,14 @@
* @return $this
* @throws \InvalidArgumentException
*/
public function extend(int $face, float $distance) : AxisAlignedBB{
public function extend(Facing $face, float $distance) : AxisAlignedBB{

Check failure on line 185 in src/AxisAlignedBB.php

View workflow job for this annotation

GitHub Actions / PHP 8.0

Parameter $face of method pocketmine\math\AxisAlignedBB::extend() has invalid type pocketmine\math\Facing.
match($face){

Check failure on line 186 in src/AxisAlignedBB.php

View workflow job for this annotation

GitHub Actions / PHP 8.0

Match expression does not handle remaining value: pocketmine\math\Facing
Facing::DOWN => $this->minY -= $distance,

Check failure on line 187 in src/AxisAlignedBB.php

View workflow job for this annotation

GitHub Actions / PHP 8.0

Access to constant DOWN on an unknown class pocketmine\math\Facing.
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,
default => throw new \InvalidArgumentException("Invalid face $face"),
};

return $this;
Expand All @@ -204,7 +201,7 @@
*
* @throws \InvalidArgumentException
*/
public function extendedCopy(int $face, float $distance) : AxisAlignedBB{
public function extendedCopy(Facing $face, float $distance) : AxisAlignedBB{
return (clone $this)->extend($face, $distance);
}

Expand All @@ -217,7 +214,7 @@
* @return $this
* @throws \InvalidArgumentException
*/
public function trim(int $face, float $distance) : AxisAlignedBB{
public function trim(Facing $face, float $distance) : AxisAlignedBB{
return $this->extend($face, -$distance);
}

Expand All @@ -227,20 +224,18 @@
*
* @throws \InvalidArgumentException
*/
public function trimmedCopy(int $face, float $distance) : AxisAlignedBB{
public function trimmedCopy(Facing $face, float $distance) : AxisAlignedBB{
return $this->extendedCopy($face, -$distance);
}

/**
* Increases the dimension of the AABB along the given axis.
*
* @param int $axis one of the Axis::* constants
* @param float $distance Negative values reduce width, positive values increase width.
*
* @return $this
* @throws \InvalidArgumentException
*/
public function stretch(int $axis, float $distance) : AxisAlignedBB{
public function stretch(Axis $axis, float $distance) : AxisAlignedBB{
if($axis === Axis::Y){
$this->minY -= $distance;
$this->maxY += $distance;
Expand All @@ -250,19 +245,16 @@
}elseif($axis === Axis::X){
$this->minX -= $distance;
$this->maxX += $distance;
}else{
throw new \InvalidArgumentException("Invalid axis $axis");
}

return $this;
}

/**
* Returns a stretched copy of this bounding box.
* @see AxisAlignedBB::stretch()
*
* @throws \InvalidArgumentException
*/
public function stretchedCopy(int $axis, float $distance) : AxisAlignedBB{
public function stretchedCopy(Axis $axis, float $distance) : AxisAlignedBB{
return (clone $this)->stretch($axis, $distance);
}

Expand All @@ -271,19 +263,16 @@
* @see AxisAlignedBB::stretch()
*
* @return $this
* @throws \InvalidArgumentException
*/
public function squash(int $axis, float $distance) : AxisAlignedBB{
public function squash(Axis $axis, float $distance) : AxisAlignedBB{
return $this->stretch($axis, -$distance);
}

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

Expand Down Expand Up @@ -468,12 +457,12 @@
$face = -1;

foreach([
Facing::WEST => $v1,

Check failure on line 460 in src/AxisAlignedBB.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

Invalid array key type pocketmine\math\Facing::WEST.
Facing::EAST => $v2,

Check failure on line 461 in src/AxisAlignedBB.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

Invalid array key type pocketmine\math\Facing::EAST.
Facing::DOWN => $v3,

Check failure on line 462 in src/AxisAlignedBB.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

Invalid array key type pocketmine\math\Facing::DOWN.
Facing::UP => $v4,

Check failure on line 463 in src/AxisAlignedBB.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

Invalid array key type pocketmine\math\Facing::UP.
Facing::NORTH => $v5,

Check failure on line 464 in src/AxisAlignedBB.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

Invalid array key type pocketmine\math\Facing::NORTH.
Facing::SOUTH => $v6

Check failure on line 465 in src/AxisAlignedBB.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

Invalid array key type pocketmine\math\Facing::SOUTH.
] as $f => $v){
if($v !== null and ($d = $pos1->distanceSquared($v)) < $distance){
$vector = $v;
Expand All @@ -486,7 +475,7 @@
return null;
}

return new RayTraceResult($this, $face, $vector);

Check failure on line 478 in src/AxisAlignedBB.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

Parameter #2 $hitFace of class pocketmine\math\RayTraceResult constructor expects pocketmine\math\Facing, (int|string) given.
}

public function __toString() : string{
Expand Down
89 changes: 44 additions & 45 deletions src/Facing.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@

use function in_array;

final class Facing{
private function __construct(){
//NOOP
}
enum Facing: int{

public const FLAG_AXIS_POSITIVE = 1;
private const FLAG_AXIS_POSITIVE = 1;

/* most significant 2 bits = axis, least significant bit = is positive direction */
public const DOWN = Axis::Y << 1;
public const UP = (Axis::Y << 1) | self::FLAG_AXIS_POSITIVE;
public const NORTH = Axis::Z << 1;
public const SOUTH = (Axis::Z << 1) | self::FLAG_AXIS_POSITIVE;
public const WEST = Axis::X << 1;
public const EAST = (Axis::X << 1) | self::FLAG_AXIS_POSITIVE;
case DOWN = Axis::Y << 1;

Check failure on line 33 in src/Facing.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

Binary operation "<<" between pocketmine\math\Axis::Y and 1 results in an error.

Check failure on line 33 in src/Facing.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

Enum case pocketmine\math\Facing::DOWN value mixed does not match the "int" type.
case UP = (Axis::Y << 1) | self::FLAG_AXIS_POSITIVE;

Check failure on line 34 in src/Facing.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

Binary operation "<<" between pocketmine\math\Axis::Y and 1 results in an error.
case NORTH = Axis::Z << 1;
case SOUTH = (Axis::Z << 1) | self::FLAG_AXIS_POSITIVE;
case WEST = Axis::X << 1;
case EAST = (Axis::X << 1) | self::FLAG_AXIS_POSITIVE;

/**
* @deprecated use Facing::cases()
*/
public const ALL = [
self::DOWN,
self::UP,
Expand All @@ -56,6 +56,9 @@
self::EAST
];
Dhaiven marked this conversation as resolved.
Show resolved Hide resolved

/**
* @deprecated
*/
public const OFFSET = [
Dhaiven marked this conversation as resolved.
Show resolved Hide resolved
self::DOWN => [ 0, -1, 0],
self::UP => [ 0, +1, 0],
Expand All @@ -65,6 +68,9 @@
self::EAST => [+1, 0, 0]
];

/**
* @var Facing[][]
*/
private const CLOCKWISE = [
Axis::Y => [
self::NORTH => self::EAST,
Expand All @@ -89,67 +95,60 @@
/**
* Returns the axis of the given direction.
*/
public static function axis(int $direction) : int{
return $direction >> 1; //shift off positive/negative bit
public static function axis(Facing $direction) : Axis{
return Axis::from($direction >> 1); //shift off positive/negative bit
}

/**
* Returns whether the direction is facing the positive of its axis.
*/
public static function isPositive(int $direction) : bool{
return ($direction & self::FLAG_AXIS_POSITIVE) === self::FLAG_AXIS_POSITIVE;
public static function isPositive(Facing $direction) : bool{
Dhaiven marked this conversation as resolved.
Show resolved Hide resolved
return ($direction->value & self::FLAG_AXIS_POSITIVE) === self::FLAG_AXIS_POSITIVE;
}

/**
* Returns the opposite Facing of the specified one.
*
* @param int $direction 0-5 one of the Facing::* constants
* @throws \ValueError if opposite facing don't exist
Dhaiven marked this conversation as resolved.
Show resolved Hide resolved
*/
public static function opposite(int $direction) : int{
return $direction ^ self::FLAG_AXIS_POSITIVE;
public static function opposite(Facing $direction) : Facing{
Dhaiven marked this conversation as resolved.
Show resolved Hide resolved
return self::from($direction->value ^ self::FLAG_AXIS_POSITIVE);
}

/**
* Rotates the given direction around the axis.
*
* @throws \InvalidArgumentException if not possible to rotate $direction around $axis
*/
public static function rotate(int $direction, int $axis, bool $clockwise) : int{
if(!isset(self::CLOCKWISE[$axis])){
throw new \InvalidArgumentException("Invalid axis $axis");
}
if(!isset(self::CLOCKWISE[$axis][$direction])){
throw new \InvalidArgumentException("Cannot rotate facing \"" . self::toString($direction) . "\" around axis \"" . Axis::toString($axis) . "\"");
}

$rotated = self::CLOCKWISE[$axis][$direction];
public static function rotate(Facing $direction, Axis $axis, bool $clockwise) : Facing{
Dhaiven marked this conversation as resolved.
Show resolved Hide resolved
$rotated = self::CLOCKWISE[$axis->value][$direction->value];
Dhaiven marked this conversation as resolved.
Show resolved Hide resolved
return $clockwise ? $rotated : self::opposite($rotated);
}

/**
* @throws \InvalidArgumentException
*/
public static function rotateY(int $direction, bool $clockwise) : int{
public static function rotateY(Facing $direction, bool $clockwise) : Facing{
Dhaiven marked this conversation as resolved.
Show resolved Hide resolved
return self::rotate($direction, Axis::Y, $clockwise);
}

/**
* @throws \InvalidArgumentException
*/
public static function rotateZ(int $direction, bool $clockwise) : int{
public static function rotateZ(Facing $direction, bool $clockwise) : Facing{
Dhaiven marked this conversation as resolved.
Show resolved Hide resolved
return self::rotate($direction, Axis::Z, $clockwise);
}

/**
* @throws \InvalidArgumentException
*/
public static function rotateX(int $direction, bool $clockwise) : int{
public static function rotateX(Facing $direction, bool $clockwise) : Facing{
Dhaiven marked this conversation as resolved.
Show resolved Hide resolved
return self::rotate($direction, Axis::X, $clockwise);
}

public function offset(): array {
return match($this){
self::DOWN => [ 0, -1, 0],
self::UP => [ 0, +1, 0],
self::NORTH => [ 0, 0, -1],
self::SOUTH => [ 0, 0, +1],
self::WEST => [-1, 0, 0],
self::EAST => [+1, 0, 0]
};
}

/**
* Validates the given integer as a Facing direction.
*
* @deprecated
* @throws \InvalidArgumentException if the argument is not a valid Facing constant
*/
public static function validate(int $facing) : void{
Expand All @@ -159,17 +158,17 @@
}
Dhaiven marked this conversation as resolved.
Show resolved Hide resolved

/**
* @deprecated use Facing->name
Dhaiven marked this conversation as resolved.
Show resolved Hide resolved
* Returns a human-readable string representation of the given Facing direction.
*/
public static function toString(int $facing) : string{
public static function toString(Facing $facing) : string{
return match($facing){
self::DOWN => "down",
self::UP => "up",
self::NORTH => "north",
self::SOUTH => "south",
self::WEST => "west",
self::EAST => "east",
default => throw new \InvalidArgumentException("Invalid facing $facing")
};
}
}
}
7 changes: 2 additions & 5 deletions src/RayTraceResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,17 @@
*/
class RayTraceResult{

/**
* @param int $hitFace one of the Facing::* constants
*/
public function __construct(
public AxisAlignedBB $bb,
public int $hitFace,
public Facing $hitFace,
public Vector3 $hitVector
){}

public function getBoundingBox() : AxisAlignedBB{
return $this->bb;
}

public function getHitFace() : int{
public function getHitFace() : Facing{
return $this->hitFace;
}

Expand Down
12 changes: 5 additions & 7 deletions src/Vector3.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public function abs() : Vector3{
/**
* @return Vector3
*/
public function getSide(int $side, int $step = 1){
[$offsetX, $offsetY, $offsetZ] = Facing::OFFSET[$side] ?? [0, 0, 0];
public function getSide(Facing $side, int $step = 1){
[$offsetX, $offsetY, $offsetZ] = $side->offset();

return $this->add($offsetX * $step, $offsetY * $step, $offsetZ * $step);
}
Expand Down Expand Up @@ -174,7 +174,7 @@ public function east(int $step = 1){
* @phpstan-return \Generator<int, Vector3, void, void>
*/
public function sides(int $step = 1) : \Generator{
foreach(Facing::ALL as $facing){
foreach(Facing::cases() as $facing){
yield $facing => $this->getSide($facing, $step);
}
}
Expand All @@ -191,13 +191,11 @@ public function sidesArray(bool $keys = false, int $step = 1) : array{
/**
* Yields vectors stepped out from this one in directions except those on the given axis.
*
* @param int $axis Facing directions on this axis will be excluded
*
* @return \Generator|Vector3[]
* @phpstan-return \Generator<int, Vector3, void, void>
*/
public function sidesAroundAxis(int $axis, int $step = 1) : \Generator{
foreach(Facing::ALL as $facing){
public function sidesAroundAxis(Axis $axis, int $step = 1) : \Generator{
foreach(Facing::cases() as $facing){
if(Facing::axis($facing) !== $axis){
yield $facing => $this->getSide($facing, $step);
}
Expand Down
Loading
Loading