|
26 | 26 | use function abs;
|
27 | 27 | use const PHP_INT_MAX;
|
28 | 28 |
|
29 |
| -final class AxisAlignedBB{ |
| 29 | +final readonly class AxisAlignedBB{ |
30 | 30 |
|
31 | 31 | public float $minX;
|
32 | 32 | public float $minY;
|
@@ -88,183 +88,121 @@ public function addCoord(float $x, float $y, float $z) : AxisAlignedBB{
|
88 | 88 | }
|
89 | 89 |
|
90 | 90 | /**
|
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. |
117 | 92 | */
|
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 | + ); |
127 | 102 | }
|
128 | 103 |
|
129 | 104 | /**
|
130 |
| - * Returns an offset clone of this AxisAlignedBB. |
| 105 | + * Returns a copy of the AxisAlignedBB offset by the specified X, Y and Z. |
131 | 106 | */
|
132 | 107 | 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 | + ); |
134 | 116 | }
|
135 | 117 |
|
136 | 118 | /**
|
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. |
149 | 120 | */
|
150 | 121 | public function offsetTowardsCopy(Facing $face, float $distance) : AxisAlignedBB{
|
151 |
| - return (clone $this)->offsetTowards($face, $distance); |
152 |
| - } |
| 122 | + [$offsetX, $offsetY, $offsetZ] = $face->offset(); |
153 | 123 |
|
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); |
168 | 125 | }
|
169 | 126 |
|
170 | 127 | /**
|
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. |
172 | 129 | */
|
173 | 130 | 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 | + ); |
175 | 139 | }
|
176 | 140 |
|
177 | 141 | /**
|
178 |
| - * Extends the AABB in the given direction. |
| 142 | + * Returns a copy of the AxisAlignedBB extended in the given direction. |
179 | 143 | *
|
180 | 144 | * @param float $distance Negative values pull the face in, positive values push out.
|
181 |
| - * |
182 |
| - * @return $this |
183 | 145 | */
|
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 | + |
185 | 154 | 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 |
192 | 161 | };
|
193 | 162 |
|
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); |
203 | 164 | }
|
204 | 165 |
|
205 | 166 | /**
|
206 |
| - * Inverse of extend(). |
207 |
| - * @see AxisAlignedBB::extend() |
| 167 | + * Inverse of extendedCopy(). |
| 168 | + * @see AxisAlignedBB::extendedCopy() |
208 | 169 | *
|
209 | 170 | * @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() |
220 | 171 | */
|
221 | 172 | public function trimmedCopy(Facing $face, float $distance) : AxisAlignedBB{
|
222 | 173 | return $this->extendedCopy($face, -$distance);
|
223 | 174 | }
|
224 | 175 |
|
225 | 176 | /**
|
226 |
| - * Increases the dimension of the AABB along the given axis. |
| 177 | + * Returns a copy of the AxisAlignedBB stretched along the given axis. |
227 | 178 | *
|
228 | 179 | * @param float $distance Negative values reduce width, positive values increase width.
|
229 |
| - * |
230 |
| - * @return $this |
231 | 180 | */
|
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 | + |
233 | 189 | if($axis === Axis::Y){
|
234 |
| - $this->minY -= $distance; |
235 |
| - $this->maxY += $distance; |
| 190 | + $minY -= $distance; |
| 191 | + $maxY += $distance; |
236 | 192 | }elseif($axis === Axis::Z){
|
237 |
| - $this->minZ -= $distance; |
238 |
| - $this->maxZ += $distance; |
| 193 | + $minZ -= $distance; |
| 194 | + $maxZ += $distance; |
239 | 195 | }elseif($axis === Axis::X){
|
240 |
| - $this->minX -= $distance; |
241 |
| - $this->maxX += $distance; |
| 196 | + $minX -= $distance; |
| 197 | + $maxX += $distance; |
242 | 198 | }
|
243 | 199 |
|
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); |
263 | 201 | }
|
264 | 202 |
|
265 | 203 | /**
|
266 |
| - * Returns a squashed copy of this bounding box. |
267 |
| - * @see AxisAlignedBB::squash() |
| 204 | + * Inverse of stretchedCopy(). |
| 205 | + * @see AxisAlignedBB::stretchedCopy() |
268 | 206 | */
|
269 | 207 | public function squashedCopy(Axis $axis, float $distance) : AxisAlignedBB{
|
270 | 208 | return $this->stretchedCopy($axis, -$distance);
|
|
0 commit comments