Skip to content

Commit f291b4d

Browse files
Dhaivendktapps
andauthored
AxisAlignedBB is now immutable (#95)
Co-authored-by: Dylan T. <dktapps@pmmp.io>
1 parent 670f525 commit f291b4d

File tree

1 file changed

+68
-130
lines changed

1 file changed

+68
-130
lines changed

src/AxisAlignedBB.php

Lines changed: 68 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
use function abs;
2727
use const PHP_INT_MAX;
2828

29-
final class AxisAlignedBB{
29+
final readonly class AxisAlignedBB{
3030

3131
public float $minX;
3232
public float $minY;
@@ -88,183 +88,121 @@ public function addCoord(float $x, float $y, float $z) : AxisAlignedBB{
8888
}
8989

9090
/**
91-
* Outsets the bounds of this AxisAlignedBB by the specified X, Y and Z.
92-
*
93-
* @return $this
94-
*/
95-
public function expand(float $x, float $y, float $z){
96-
$this->minX -= $x;
97-
$this->minY -= $y;
98-
$this->minZ -= $z;
99-
$this->maxX += $x;
100-
$this->maxY += $y;
101-
$this->maxZ += $z;
102-
103-
return $this;
104-
}
105-
106-
/**
107-
* Returns an expanded clone of this AxisAlignedBB.
108-
*/
109-
public function expandedCopy(float $x, float $y, float $z) : AxisAlignedBB{
110-
return (clone $this)->expand($x, $y, $z);
111-
}
112-
113-
/**
114-
* Shifts this AxisAlignedBB by the given X, Y and Z.
115-
*
116-
* @return $this
91+
* Returns a copy of the AxisAlignedBB with bounds outset by the specified X, Y and Z.
11792
*/
118-
public function offset(float $x, float $y, float $z) : AxisAlignedBB{
119-
$this->minX += $x;
120-
$this->minY += $y;
121-
$this->minZ += $z;
122-
$this->maxX += $x;
123-
$this->maxY += $y;
124-
$this->maxZ += $z;
125-
126-
return $this;
93+
public function expandedCopy(float $x, float $y, float $z): AxisAlignedBB{
94+
return new AxisAlignedBB(
95+
$this->minX - $x,
96+
$this->minY - $y,
97+
$this->minZ - $z,
98+
$this->maxX + $x,
99+
$this->maxY + $y,
100+
$this->maxZ + $z
101+
);
127102
}
128103

129104
/**
130-
* Returns an offset clone of this AxisAlignedBB.
105+
* Returns a copy of the AxisAlignedBB offset by the specified X, Y and Z.
131106
*/
132107
public function offsetCopy(float $x, float $y, float $z) : AxisAlignedBB{
133-
return (clone $this)->offset($x, $y, $z);
108+
return new AxisAlignedBB(
109+
$this->minX + $x,
110+
$this->minY + $y,
111+
$this->minZ + $z,
112+
$this->maxX + $x,
113+
$this->maxY + $y,
114+
$this->maxZ + $z
115+
);
134116
}
135117

136118
/**
137-
* Offsets this AxisAlignedBB in the given direction by the specified distance.
138-
*
139-
* @return $this
140-
*/
141-
public function offsetTowards(Facing $face, float $distance) : AxisAlignedBB{
142-
[$offsetX, $offsetY, $offsetZ] = $face->offset();
143-
144-
return $this->offset($offsetX * $distance, $offsetY * $distance, $offsetZ * $distance);
145-
}
146-
147-
/**
148-
* Returns an offset clone of this AxisAlignedBB.
119+
* Returns a copy of the AxisAlignedBB offset in the given direction by the specified distance.
149120
*/
150121
public function offsetTowardsCopy(Facing $face, float $distance) : AxisAlignedBB{
151-
return (clone $this)->offsetTowards($face, $distance);
152-
}
122+
[$offsetX, $offsetY, $offsetZ] = $face->offset();
153123

154-
/**
155-
* Insets the bounds of this AxisAlignedBB by the specified X, Y and Z.
156-
*
157-
* @return $this
158-
*/
159-
public function contract(float $x, float $y, float $z) : AxisAlignedBB{
160-
$this->minX += $x;
161-
$this->minY += $y;
162-
$this->minZ += $z;
163-
$this->maxX -= $x;
164-
$this->maxY -= $y;
165-
$this->maxZ -= $z;
166-
167-
return $this;
124+
return $this->offsetCopy($offsetX * $distance, $offsetY * $distance, $offsetZ * $distance);
168125
}
169126

170127
/**
171-
* Returns a contracted clone of this AxisAlignedBB.
128+
* Returns a copy of the AxisAlignedBB with bounds contracted by the specified X, Y and Z.
172129
*/
173130
public function contractedCopy(float $x, float $y, float $z) : AxisAlignedBB{
174-
return (clone $this)->contract($x, $y, $z);
131+
return new AxisAlignedBB(
132+
$this->minX + $x,
133+
$this->minY + $y,
134+
$this->minZ + $z,
135+
$this->maxX - $x,
136+
$this->maxY - $y,
137+
$this->maxZ - $z
138+
);
175139
}
176140

177141
/**
178-
* Extends the AABB in the given direction.
142+
* Returns a copy of the AxisAlignedBB extended in the given direction.
179143
*
180144
* @param float $distance Negative values pull the face in, positive values push out.
181-
*
182-
* @return $this
183145
*/
184-
public function extend(Facing $face, float $distance) : AxisAlignedBB{
146+
public function extendedCopy(Facing $face, float $distance) : AxisAlignedBB{
147+
$minX = $this->minX;
148+
$minY = $this->minY;
149+
$minZ = $this->minZ;
150+
$maxX = $this->maxX;
151+
$maxY = $this->maxY;
152+
$maxZ = $this->maxZ;
153+
185154
match($face){
186-
Facing::DOWN => $this->minY -= $distance,
187-
Facing::UP => $this->maxY += $distance,
188-
Facing::NORTH => $this->minZ -= $distance,
189-
Facing::SOUTH => $this->maxZ += $distance,
190-
Facing::WEST => $this->minX -= $distance,
191-
Facing::EAST => $this->maxX += $distance,
155+
Facing::DOWN => $minY -= $distance,
156+
Facing::UP => $maxY += $distance,
157+
Facing::NORTH => $minZ -= $distance,
158+
Facing::SOUTH => $maxZ += $distance,
159+
Facing::WEST => $minX -= $distance,
160+
Facing::EAST => $maxX += $distance
192161
};
193162

194-
return $this;
195-
}
196-
197-
/**
198-
* Returns an extended clone of this bounding box.
199-
* @see AxisAlignedBB::extend()
200-
*/
201-
public function extendedCopy(Facing $face, float $distance) : AxisAlignedBB{
202-
return (clone $this)->extend($face, $distance);
163+
return new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ);
203164
}
204165

205166
/**
206-
* Inverse of extend().
207-
* @see AxisAlignedBB::extend()
167+
* Inverse of extendedCopy().
168+
* @see AxisAlignedBB::extendedCopy()
208169
*
209170
* @param float $distance Positive values pull the face in, negative values push out.
210-
*
211-
* @return $this
212-
*/
213-
public function trim(Facing $face, float $distance) : AxisAlignedBB{
214-
return $this->extend($face, -$distance);
215-
}
216-
217-
/**
218-
* Returns a trimmed clone of this bounding box.
219-
* @see AxisAlignedBB::trim()
220171
*/
221172
public function trimmedCopy(Facing $face, float $distance) : AxisAlignedBB{
222173
return $this->extendedCopy($face, -$distance);
223174
}
224175

225176
/**
226-
* Increases the dimension of the AABB along the given axis.
177+
* Returns a copy of the AxisAlignedBB stretched along the given axis.
227178
*
228179
* @param float $distance Negative values reduce width, positive values increase width.
229-
*
230-
* @return $this
231180
*/
232-
public function stretch(Axis $axis, float $distance) : AxisAlignedBB{
181+
public function stretchedCopy(Axis $axis, float $distance) : AxisAlignedBB{
182+
$minX = $this->minX;
183+
$minY = $this->minY;
184+
$minZ = $this->minZ;
185+
$maxX = $this->maxX;
186+
$maxY = $this->maxY;
187+
$maxZ = $this->maxZ;
188+
233189
if($axis === Axis::Y){
234-
$this->minY -= $distance;
235-
$this->maxY += $distance;
190+
$minY -= $distance;
191+
$maxY += $distance;
236192
}elseif($axis === Axis::Z){
237-
$this->minZ -= $distance;
238-
$this->maxZ += $distance;
193+
$minZ -= $distance;
194+
$maxZ += $distance;
239195
}elseif($axis === Axis::X){
240-
$this->minX -= $distance;
241-
$this->maxX += $distance;
196+
$minX -= $distance;
197+
$maxX += $distance;
242198
}
243199

244-
return $this;
245-
}
246-
247-
/**
248-
* Returns a stretched copy of this bounding box.
249-
* @see AxisAlignedBB::stretch()
250-
*/
251-
public function stretchedCopy(Axis $axis, float $distance) : AxisAlignedBB{
252-
return (clone $this)->stretch($axis, $distance);
253-
}
254-
255-
/**
256-
* Reduces the dimension of the AABB on the given axis. Inverse of stretch().
257-
* @see AxisAlignedBB::stretch()
258-
*
259-
* @return $this
260-
*/
261-
public function squash(Axis $axis, float $distance) : AxisAlignedBB{
262-
return $this->stretch($axis, -$distance);
200+
return new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ);
263201
}
264202

265203
/**
266-
* Returns a squashed copy of this bounding box.
267-
* @see AxisAlignedBB::squash()
204+
* Inverse of stretchedCopy().
205+
* @see AxisAlignedBB::stretchedCopy()
268206
*/
269207
public function squashedCopy(Axis $axis, float $distance) : AxisAlignedBB{
270208
return $this->stretchedCopy($axis, -$distance);

0 commit comments

Comments
 (0)