| 1 | package net.minecraft.world.phys;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.Optional;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.util.Mth;
|
| 8 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 9 | import org.joml.Vector3f;
|
| 10 | import org.joml.Vector3fc;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | public class AABB {
|
| 14 | private static final double EPSILON = 1.0E-7;
|
| 15 | public final double minX;
|
| 16 | public final double minY;
|
| 17 | public final double minZ;
|
| 18 | public final double maxX;
|
| 19 | public final double maxY;
|
| 20 | public final double maxZ;
|
| 21 |
|
| 22 | public AABB(final double minX, final double minY, final double minZ, final double maxX, final double maxY, final double maxZ) {
|
| 23 | this.minX = Math.min(minX, maxX);
|
| 24 | this.minY = Math.min(minY, maxY);
|
| 25 | this.minZ = Math.min(minZ, maxZ);
|
| 26 | this.maxX = Math.max(minX, maxX);
|
| 27 | this.maxY = Math.max(minY, maxY);
|
| 28 | this.maxZ = Math.max(minZ, maxZ);
|
| 29 | }
|
| 30 |
|
| 31 | public AABB(final BlockPos pos) {
|
| 32 | this(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, pos.getY() + 1, pos.getZ() + 1);
|
| 33 | }
|
| 34 |
|
| 35 | public AABB(final Vec3 begin, final Vec3 end) {
|
| 36 | this(begin.x, begin.y, begin.z, end.x, end.y, end.z);
|
| 37 | }
|
| 38 |
|
| 39 | public static AABB of(final BoundingBox box) {
|
| 40 | return new AABB(box.minX(), box.minY(), box.minZ(), box.maxX() + 1, box.maxY() + 1, box.maxZ() + 1);
|
| 41 | }
|
| 42 |
|
| 43 | public static AABB unitCubeFromLowerCorner(final Vec3 pos) {
|
| 44 | return new AABB(pos.x, pos.y, pos.z, pos.x + 1.0, pos.y + 1.0, pos.z + 1.0);
|
| 45 | }
|
| 46 |
|
| 47 | public static AABB encapsulatingFullBlocks(final BlockPos pos0, final BlockPos pos1) {
|
| 48 | return new AABB(
|
| 49 | Math.min(pos0.getX(), pos1.getX()),
|
| 50 | Math.min(pos0.getY(), pos1.getY()),
|
| 51 | Math.min(pos0.getZ(), pos1.getZ()),
|
| 52 | Math.max(pos0.getX(), pos1.getX()) + 1,
|
| 53 | Math.max(pos0.getY(), pos1.getY()) + 1,
|
| 54 | Math.max(pos0.getZ(), pos1.getZ()) + 1
|
| 55 | );
|
| 56 | }
|
| 57 |
|
| 58 | public AABB setMinX(final double minX) {
|
| 59 | return new AABB(minX, this.minY, this.minZ, this.maxX, this.maxY, this.maxZ);
|
| 60 | }
|
| 61 |
|
| 62 | public AABB setMinY(final double minY) {
|
| 63 | return new AABB(this.minX, minY, this.minZ, this.maxX, this.maxY, this.maxZ);
|
| 64 | }
|
| 65 |
|
| 66 | public AABB setMinZ(final double minZ) {
|
| 67 | return new AABB(this.minX, this.minY, minZ, this.maxX, this.maxY, this.maxZ);
|
| 68 | }
|
| 69 |
|
| 70 | public AABB setMaxX(final double maxX) {
|
| 71 | return new AABB(this.minX, this.minY, this.minZ, maxX, this.maxY, this.maxZ);
|
| 72 | }
|
| 73 |
|
| 74 | public AABB setMaxY(final double maxY) {
|
| 75 | return new AABB(this.minX, this.minY, this.minZ, this.maxX, maxY, this.maxZ);
|
| 76 | }
|
| 77 |
|
| 78 | public AABB setMaxZ(final double maxZ) {
|
| 79 | return new AABB(this.minX, this.minY, this.minZ, this.maxX, this.maxY, maxZ);
|
| 80 | }
|
| 81 |
|
| 82 | public double min(final Direction.Axis axis) {
|
| 83 | return axis.choose(this.minX, this.minY, this.minZ);
|
| 84 | }
|
| 85 |
|
| 86 | public double max(final Direction.Axis axis) {
|
| 87 | return axis.choose(this.maxX, this.maxY, this.maxZ);
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | public boolean equals(final Object o) {
|
| 92 | if (this == o) {
|
| 93 | return true;
|
| 94 | }
|
| 95 |
|
| 96 | if (o instanceof AABB aabb) {
|
| 97 | if (Double.compare(aabb.minX, this.minX) != 0) {
|
| 98 | return false;
|
| 99 | } else if (Double.compare(aabb.minY, this.minY) != 0) {
|
| 100 | return false;
|
| 101 | } else if (Double.compare(aabb.minZ, this.minZ) != 0) {
|
| 102 | return false;
|
| 103 | } else if (Double.compare(aabb.maxX, this.maxX) != 0) {
|
| 104 | return false;
|
| 105 | } else {
|
| 106 | return Double.compare(aabb.maxY, this.maxY) != 0 ? false : Double.compare(aabb.maxZ, this.maxZ) == 0;
|
| 107 | }
|
| 108 | } else {
|
| 109 | return false;
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public int hashCode() {
|
| 115 | long temp = Double.doubleToLongBits(this.minX);
|
| 116 | int result = (int)(temp ^ temp >>> 32);
|
| 117 | temp = Double.doubleToLongBits(this.minY);
|
| 118 | result = 31 * result + (int)(temp ^ temp >>> 32);
|
| 119 | temp = Double.doubleToLongBits(this.minZ);
|
| 120 | result = 31 * result + (int)(temp ^ temp >>> 32);
|
| 121 | temp = Double.doubleToLongBits(this.maxX);
|
| 122 | result = 31 * result + (int)(temp ^ temp >>> 32);
|
| 123 | temp = Double.doubleToLongBits(this.maxY);
|
| 124 | result = 31 * result + (int)(temp ^ temp >>> 32);
|
| 125 | temp = Double.doubleToLongBits(this.maxZ);
|
| 126 | return 31 * result + (int)(temp ^ temp >>> 32);
|
| 127 | }
|
| 128 |
|
| 129 | public AABB contract(final double xa, final double ya, final double za) {
|
| 130 | double minX = this.minX;
|
| 131 | double minY = this.minY;
|
| 132 | double minZ = this.minZ;
|
| 133 | double maxX = this.maxX;
|
| 134 | double maxY = this.maxY;
|
| 135 | double maxZ = this.maxZ;
|
| 136 | if (xa < 0.0) {
|
| 137 | minX -= xa;
|
| 138 | } else if (xa > 0.0) {
|
| 139 | maxX -= xa;
|
| 140 | }
|
| 141 |
|
| 142 | if (ya < 0.0) {
|
| 143 | minY -= ya;
|
| 144 | } else if (ya > 0.0) {
|
| 145 | maxY -= ya;
|
| 146 | }
|
| 147 |
|
| 148 | if (za < 0.0) {
|
| 149 | minZ -= za;
|
| 150 | } else if (za > 0.0) {
|
| 151 | maxZ -= za;
|
| 152 | }
|
| 153 |
|
| 154 | return new AABB(minX, minY, minZ, maxX, maxY, maxZ);
|
| 155 | }
|
| 156 |
|
| 157 | public AABB expandTowards(final Vec3 delta) {
|
| 158 | return this.expandTowards(delta.x, delta.y, delta.z);
|
| 159 | }
|
| 160 |
|
| 161 | public AABB expandTowards(final double xa, final double ya, final double za) {
|
| 162 | double minX = this.minX;
|
| 163 | double minY = this.minY;
|
| 164 | double minZ = this.minZ;
|
| 165 | double maxX = this.maxX;
|
| 166 | double maxY = this.maxY;
|
| 167 | double maxZ = this.maxZ;
|
| 168 | if (xa < 0.0) {
|
| 169 | minX += xa;
|
| 170 | } else if (xa > 0.0) {
|
| 171 | maxX += xa;
|
| 172 | }
|
| 173 |
|
| 174 | if (ya < 0.0) {
|
| 175 | minY += ya;
|
| 176 | } else if (ya > 0.0) {
|
| 177 | maxY += ya;
|
| 178 | }
|
| 179 |
|
| 180 | if (za < 0.0) {
|
| 181 | minZ += za;
|
| 182 | } else if (za > 0.0) {
|
| 183 | maxZ += za;
|
| 184 | }
|
| 185 |
|
| 186 | return new AABB(minX, minY, minZ, maxX, maxY, maxZ);
|
| 187 | }
|
| 188 |
|
| 189 | public AABB inflate(final double xAdd, final double yAdd, final double zAdd) {
|
| 190 | double minX = this.minX - xAdd;
|
| 191 | double minY = this.minY - yAdd;
|
| 192 | double minZ = this.minZ - zAdd;
|
| 193 | double maxX = this.maxX + xAdd;
|
| 194 | double maxY = this.maxY + yAdd;
|
| 195 | double maxZ = this.maxZ + zAdd;
|
| 196 | return new AABB(minX, minY, minZ, maxX, maxY, maxZ);
|
| 197 | }
|
| 198 |
|
| 199 | public AABB inflate(final double amountToAddInAllDirections) {
|
| 200 | return this.inflate(amountToAddInAllDirections, amountToAddInAllDirections, amountToAddInAllDirections);
|
| 201 | }
|
| 202 |
|
| 203 | public AABB intersect(final AABB other) {
|
| 204 | double minX = Math.max(this.minX, other.minX);
|
| 205 | double minY = Math.max(this.minY, other.minY);
|
| 206 | double minZ = Math.max(this.minZ, other.minZ);
|
| 207 | double maxX = Math.min(this.maxX, other.maxX);
|
| 208 | double maxY = Math.min(this.maxY, other.maxY);
|
| 209 | double maxZ = Math.min(this.maxZ, other.maxZ);
|
| 210 | return new AABB(minX, minY, minZ, maxX, maxY, maxZ);
|
| 211 | }
|
| 212 |
|
| 213 | public AABB minmax(final AABB other) {
|
| 214 | double minX = Math.min(this.minX, other.minX);
|
| 215 | double minY = Math.min(this.minY, other.minY);
|
| 216 | double minZ = Math.min(this.minZ, other.minZ);
|
| 217 | double maxX = Math.max(this.maxX, other.maxX);
|
| 218 | double maxY = Math.max(this.maxY, other.maxY);
|
| 219 | double maxZ = Math.max(this.maxZ, other.maxZ);
|
| 220 | return new AABB(minX, minY, minZ, maxX, maxY, maxZ);
|
| 221 | }
|
| 222 |
|
| 223 | public AABB move(final double xa, final double ya, final double za) {
|
| 224 | return new AABB(this.minX + xa, this.minY + ya, this.minZ + za, this.maxX + xa, this.maxY + ya, this.maxZ + za);
|
| 225 | }
|
| 226 |
|
| 227 | public AABB move(final BlockPos pos) {
|
| 228 | return new AABB(
|
| 229 | this.minX + pos.getX(), this.minY + pos.getY(), this.minZ + pos.getZ(), this.maxX + pos.getX(), this.maxY + pos.getY(), this.maxZ + pos.getZ()
|
| 230 | );
|
| 231 | }
|
| 232 |
|
| 233 | public AABB move(final Vec3 pos) {
|
| 234 | return this.move(pos.x, pos.y, pos.z);
|
| 235 | }
|
| 236 |
|
| 237 | public AABB move(final Vector3f pos) {
|
| 238 | return this.move(pos.x, pos.y, pos.z);
|
| 239 | }
|
| 240 |
|
| 241 | public boolean intersects(final AABB aabb) {
|
| 242 | return this.intersects(aabb.minX, aabb.minY, aabb.minZ, aabb.maxX, aabb.maxY, aabb.maxZ);
|
| 243 | }
|
| 244 |
|
| 245 | public boolean intersects(final double minX, final double minY, final double minZ, final double maxX, final double maxY, final double maxZ) {
|
| 246 | return this.minX < maxX && this.maxX > minX && this.minY < maxY && this.maxY > minY && this.minZ < maxZ && this.maxZ > minZ;
|
| 247 | }
|
| 248 |
|
| 249 | public boolean intersects(final Vec3 min, final Vec3 max) {
|
| 250 | return this.intersects(
|
| 251 | Math.min(min.x, max.x), Math.min(min.y, max.y), Math.min(min.z, max.z), Math.max(min.x, max.x), Math.max(min.y, max.y), Math.max(min.z, max.z)
|
| 252 | );
|
| 253 | }
|
| 254 |
|
| 255 | public boolean intersects(final BlockPos pos) {
|
| 256 | return this.intersects(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, pos.getY() + 1, pos.getZ() + 1);
|
| 257 | }
|
| 258 |
|
| 259 | public boolean contains(final Vec3 vec) {
|
| 260 | return this.contains(vec.x, vec.y, vec.z);
|
| 261 | }
|
| 262 |
|
| 263 | public boolean contains(final double x, final double y, final double z) {
|
| 264 | return x >= this.minX && x < this.maxX && y >= this.minY && y < this.maxY && z >= this.minZ && z < this.maxZ;
|
| 265 | }
|
| 266 |
|
| 267 | public double getSize() {
|
| 268 | double xs = this.getXsize();
|
| 269 | double ys = this.getYsize();
|
| 270 | double zs = this.getZsize();
|
| 271 | return (xs + ys + zs) / 3.0;
|
| 272 | }
|
| 273 |
|
| 274 | public double getXsize() {
|
| 275 | return this.maxX - this.minX;
|
| 276 | }
|
| 277 |
|
| 278 | public double getYsize() {
|
| 279 | return this.maxY - this.minY;
|
| 280 | }
|
| 281 |
|
| 282 | public double getZsize() {
|
| 283 | return this.maxZ - this.minZ;
|
| 284 | }
|
| 285 |
|
| 286 | public AABB deflate(final double xSubstract, final double ySubtract, final double zSubtract) {
|
| 287 | return this.inflate(-xSubstract, -ySubtract, -zSubtract);
|
| 288 | }
|
| 289 |
|
| 290 | public AABB deflate(final double amount) {
|
| 291 | return this.inflate(-amount);
|
| 292 | }
|
| 293 |
|
| 294 | public Optional<Vec3> clip(final Vec3 from, final Vec3 to) {
|
| 295 | return clip(this.minX, this.minY, this.minZ, this.maxX, this.maxY, this.maxZ, from, to);
|
| 296 | }
|
| 297 |
|
| 298 | public static Optional<Vec3> clip(
|
| 299 | final double minX, final double minY, final double minZ, final double maxX, final double maxY, final double maxZ, final Vec3 from, final Vec3 to
|
| 300 | ) {
|
| 301 | double[] scaleReference = new double[]{1.0};
|
| 302 | double dx = to.x - from.x;
|
| 303 | double dy = to.y - from.y;
|
| 304 | double dz = to.z - from.z;
|
| 305 | Direction direction = getDirection(minX, minY, minZ, maxX, maxY, maxZ, from, scaleReference, null, dx, dy, dz);
|
| 306 | if (direction == null) {
|
| 307 | return Optional.empty();
|
| 308 | }
|
| 309 |
|
| 310 | double scale = scaleReference[0];
|
| 311 | return Optional.of(from.add(scale * dx, scale * dy, scale * dz));
|
| 312 | }
|
| 313 |
|
| 314 | public static @Nullable BlockHitResult clip(final Iterable<AABB> aabBs, final Vec3 from, final Vec3 to, final BlockPos pos) {
|
| 315 | double[] scaleReference = new double[]{1.0};
|
| 316 | Direction direction = null;
|
| 317 | double dx = to.x - from.x;
|
| 318 | double dy = to.y - from.y;
|
| 319 | double dz = to.z - from.z;
|
| 320 |
|
| 321 | for (AABB aabb : aabBs) {
|
| 322 | direction = getDirection(aabb.move(pos), from, scaleReference, direction, dx, dy, dz);
|
| 323 | }
|
| 324 |
|
| 325 | if (direction == null) {
|
| 326 | return null;
|
| 327 | }
|
| 328 |
|
| 329 | double scale = scaleReference[0];
|
| 330 | return new BlockHitResult(from.add(scale * dx, scale * dy, scale * dz), direction, pos, false);
|
| 331 | }
|
| 332 |
|
| 333 | private static @Nullable Direction getDirection(
|
| 334 | final AABB aabb, final Vec3 from, final double[] scaleReference, final @Nullable Direction direction, final double dx, final double dy, final double dz
|
| 335 | ) {
|
| 336 | return getDirection(aabb.minX, aabb.minY, aabb.minZ, aabb.maxX, aabb.maxY, aabb.maxZ, from, scaleReference, direction, dx, dy, dz);
|
| 337 | }
|
| 338 |
|
| 339 | private static @Nullable Direction getDirection(
|
| 340 | final double minX,
|
| 341 | final double minY,
|
| 342 | final double minZ,
|
| 343 | final double maxX,
|
| 344 | final double maxY,
|
| 345 | final double maxZ,
|
| 346 | final Vec3 from,
|
| 347 | final double[] scaleReference,
|
| 348 | @Nullable Direction direction,
|
| 349 | final double dx,
|
| 350 | final double dy,
|
| 351 | final double dz
|
| 352 | ) {
|
| 353 | if (dx > 1.0E-7) {
|
| 354 | direction = clipPoint(scaleReference, direction, dx, dy, dz, minX, minY, maxY, minZ, maxZ, Direction.WEST, from.x, from.y, from.z);
|
| 355 | } else if (dx < -1.0E-7) {
|
| 356 | direction = clipPoint(scaleReference, direction, dx, dy, dz, maxX, minY, maxY, minZ, maxZ, Direction.EAST, from.x, from.y, from.z);
|
| 357 | }
|
| 358 |
|
| 359 | if (dy > 1.0E-7) {
|
| 360 | direction = clipPoint(scaleReference, direction, dy, dz, dx, minY, minZ, maxZ, minX, maxX, Direction.DOWN, from.y, from.z, from.x);
|
| 361 | } else if (dy < -1.0E-7) {
|
| 362 | direction = clipPoint(scaleReference, direction, dy, dz, dx, maxY, minZ, maxZ, minX, maxX, Direction.UP, from.y, from.z, from.x);
|
| 363 | }
|
| 364 |
|
| 365 | if (dz > 1.0E-7) {
|
| 366 | direction = clipPoint(scaleReference, direction, dz, dx, dy, minZ, minX, maxX, minY, maxY, Direction.NORTH, from.z, from.x, from.y);
|
| 367 | } else if (dz < -1.0E-7) {
|
| 368 | direction = clipPoint(scaleReference, direction, dz, dx, dy, maxZ, minX, maxX, minY, maxY, Direction.SOUTH, from.z, from.x, from.y);
|
| 369 | }
|
| 370 |
|
| 371 | return direction;
|
| 372 | }
|
| 373 |
|
| 374 | private static @Nullable Direction clipPoint(
|
| 375 | final double[] scaleReference,
|
| 376 | final @Nullable Direction direction,
|
| 377 | final double da,
|
| 378 | final double db,
|
| 379 | final double dc,
|
| 380 | final double point,
|
| 381 | final double minB,
|
| 382 | final double maxB,
|
| 383 | final double minC,
|
| 384 | final double maxC,
|
| 385 | final Direction newDirection,
|
| 386 | final double fromA,
|
| 387 | final double fromB,
|
| 388 | final double fromC
|
| 389 | ) {
|
| 390 | double s = (point - fromA) / da;
|
| 391 | double pb = fromB + s * db;
|
| 392 | double pc = fromC + s * dc;
|
| 393 | if (0.0 < s && s < scaleReference[0] && minB - 1.0E-7 < pb && pb < maxB + 1.0E-7 && minC - 1.0E-7 < pc && pc < maxC + 1.0E-7) {
|
| 394 | scaleReference[0] = s;
|
| 395 | return newDirection;
|
| 396 | } else {
|
| 397 | return direction;
|
| 398 | }
|
| 399 | }
|
| 400 |
|
| 401 | public boolean collidedAlongVector(final Vec3 vector, final List<AABB> aabbs) {
|
| 402 | Vec3 from = this.getCenter();
|
| 403 | Vec3 to = from.add(vector);
|
| 404 |
|
| 405 | for (AABB shapePart : aabbs) {
|
| 406 | AABB inflated = shapePart.inflate(this.getXsize() * 0.5 - 1.0E-7, this.getYsize() * 0.5 - 1.0E-7, this.getZsize() * 0.5 - 1.0E-7);
|
| 407 | if (inflated.contains(to) || inflated.contains(from)) {
|
| 408 | return true;
|
| 409 | }
|
| 410 |
|
| 411 | if (inflated.clip(from, to).isPresent()) {
|
| 412 | return true;
|
| 413 | }
|
| 414 | }
|
| 415 |
|
| 416 | return false;
|
| 417 | }
|
| 418 |
|
| 419 | public double distanceToSqr(final Vec3 point) {
|
| 420 | double dx = Math.max(Math.max(this.minX - point.x, point.x - this.maxX), 0.0);
|
| 421 | double dy = Math.max(Math.max(this.minY - point.y, point.y - this.maxY), 0.0);
|
| 422 | double dz = Math.max(Math.max(this.minZ - point.z, point.z - this.maxZ), 0.0);
|
| 423 | return Mth.lengthSquared(dx, dy, dz);
|
| 424 | }
|
| 425 |
|
| 426 | public double distanceToSqr(final AABB boundingBox) {
|
| 427 | double dx = Math.max(Math.max(this.minX - boundingBox.maxX, boundingBox.minX - this.maxX), 0.0);
|
| 428 | double dy = Math.max(Math.max(this.minY - boundingBox.maxY, boundingBox.minY - this.maxY), 0.0);
|
| 429 | double dz = Math.max(Math.max(this.minZ - boundingBox.maxZ, boundingBox.minZ - this.maxZ), 0.0);
|
| 430 | return Mth.lengthSquared(dx, dy, dz);
|
| 431 | }
|
| 432 |
|
| 433 | @Override
|
| 434 | public String toString() {
|
| 435 | return "AABB[" + this.minX + ", " + this.minY + ", " + this.minZ + "] -> [" + this.maxX + ", " + this.maxY + ", " + this.maxZ + "]";
|
| 436 | }
|
| 437 |
|
| 438 | public boolean hasNaN() {
|
| 439 | return Double.isNaN(this.minX)
|
| 440 | || Double.isNaN(this.minY)
|
| 441 | || Double.isNaN(this.minZ)
|
| 442 | || Double.isNaN(this.maxX)
|
| 443 | || Double.isNaN(this.maxY)
|
| 444 | || Double.isNaN(this.maxZ);
|
| 445 | }
|
| 446 |
|
| 447 | public Vec3 getCenter() {
|
| 448 | return new Vec3(Mth.lerp(0.5, this.minX, this.maxX), Mth.lerp(0.5, this.minY, this.maxY), Mth.lerp(0.5, this.minZ, this.maxZ));
|
| 449 | }
|
| 450 |
|
| 451 | public Vec3 getBottomCenter() {
|
| 452 | return new Vec3(Mth.lerp(0.5, this.minX, this.maxX), this.minY, Mth.lerp(0.5, this.minZ, this.maxZ));
|
| 453 | }
|
| 454 |
|
| 455 | public Vec3 getMinPosition() {
|
| 456 | return new Vec3(this.minX, this.minY, this.minZ);
|
| 457 | }
|
| 458 |
|
| 459 | public Vec3 getMaxPosition() {
|
| 460 | return new Vec3(this.maxX, this.maxY, this.maxZ);
|
| 461 | }
|
| 462 |
|
| 463 | public static AABB ofSize(final Vec3 center, final double sizeX, final double sizeY, final double sizeZ) {
|
| 464 | return new AABB(
|
| 465 | center.x - sizeX / 2.0, center.y - sizeY / 2.0, center.z - sizeZ / 2.0, center.x + sizeX / 2.0, center.y + sizeY / 2.0, center.z + sizeZ / 2.0
|
| 466 | );
|
| 467 | }
|
| 468 |
|
| 469 | public AABB nextDeflated() {
|
| 470 | return new AABB(
|
| 471 | Math.nextUp(this.minX), Math.nextUp(this.minY), Math.nextUp(this.minZ), Math.nextDown(this.maxX), Math.nextDown(this.maxY), Math.nextDown(this.maxZ)
|
| 472 | );
|
| 473 | }
|
| 474 |
|
| 475 | public static class Builder {
|
| 476 | private float minX = Float.POSITIVE_INFINITY;
|
| 477 | private float minY = Float.POSITIVE_INFINITY;
|
| 478 | private float minZ = Float.POSITIVE_INFINITY;
|
| 479 | private float maxX = Float.NEGATIVE_INFINITY;
|
| 480 | private float maxY = Float.NEGATIVE_INFINITY;
|
| 481 | private float maxZ = Float.NEGATIVE_INFINITY;
|
| 482 | private boolean defined;
|
| 483 |
|
| 484 | public void include(final Vector3fc v) {
|
| 485 | this.minX = Math.min(this.minX, v.x());
|
| 486 | this.minY = Math.min(this.minY, v.y());
|
| 487 | this.minZ = Math.min(this.minZ, v.z());
|
| 488 | this.maxX = Math.max(this.maxX, v.x());
|
| 489 | this.maxY = Math.max(this.maxY, v.y());
|
| 490 | this.maxZ = Math.max(this.maxZ, v.z());
|
| 491 | this.defined = true;
|
| 492 | }
|
| 493 |
|
| 494 | public boolean isDefined() {
|
| 495 | return this.defined;
|
| 496 | }
|
| 497 |
|
| 498 | public AABB build() {
|
| 499 | if (!this.defined) {
|
| 500 | throw new IllegalStateException("Cannot build an undefined AABB. Include at least one point.");
|
| 501 | } else {
|
| 502 | return new AABB(this.minX, this.minY, this.minZ, this.maxX, this.maxY, this.maxZ);
|
| 503 | }
|
| 504 | }
|
| 505 | }
|
| 506 | }
|
| 507 |
|