| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | public class AxisAlignedBB
|
| 4 | {
|
| 5 | public final double minX;
|
| 6 | public final double minY;
|
| 7 | public final double minZ;
|
| 8 | public final double maxX;
|
| 9 | public final double maxY;
|
| 10 | public final double maxZ;
|
| 11 |
|
| 12 | public AxisAlignedBB(double x1, double y1, double z1, double x2, double y2, double z2)
|
| 13 | {
|
| 14 | this.minX = Math.min(x1, x2);
|
| 15 | this.minY = Math.min(y1, y2);
|
| 16 | this.minZ = Math.min(z1, z2);
|
| 17 | this.maxX = Math.max(x1, x2);
|
| 18 | this.maxY = Math.max(y1, y2);
|
| 19 | this.maxZ = Math.max(z1, z2);
|
| 20 | }
|
| 21 |
|
| 22 | public AxisAlignedBB(BlockPos pos1, BlockPos pos2)
|
| 23 | {
|
| 24 | this.minX = (double)pos1.getX();
|
| 25 | this.minY = (double)pos1.getY();
|
| 26 | this.minZ = (double)pos1.getZ();
|
| 27 | this.maxX = (double)pos2.getX();
|
| 28 | this.maxY = (double)pos2.getY();
|
| 29 | this.maxZ = (double)pos2.getZ();
|
| 30 | }
|
| 31 |
|
| 32 | /**
|
| 33 | * Adds the coordinates to the bounding box extending it if the point lies outside the current ranges. Args: x, y, z
|
| 34 | */
|
| 35 | public AxisAlignedBB addCoord(double x, double y, double z)
|
| 36 | {
|
| 37 | double d0 = this.minX;
|
| 38 | double d1 = this.minY;
|
| 39 | double d2 = this.minZ;
|
| 40 | double d3 = this.maxX;
|
| 41 | double d4 = this.maxY;
|
| 42 | double d5 = this.maxZ;
|
| 43 |
|
| 44 | if (x < 0.0D)
|
| 45 | {
|
| 46 | d0 += x;
|
| 47 | }
|
| 48 | else if (x > 0.0D)
|
| 49 | {
|
| 50 | d3 += x;
|
| 51 | }
|
| 52 |
|
| 53 | if (y < 0.0D)
|
| 54 | {
|
| 55 | d1 += y;
|
| 56 | }
|
| 57 | else if (y > 0.0D)
|
| 58 | {
|
| 59 | d4 += y;
|
| 60 | }
|
| 61 |
|
| 62 | if (z < 0.0D)
|
| 63 | {
|
| 64 | d2 += z;
|
| 65 | }
|
| 66 | else if (z > 0.0D)
|
| 67 | {
|
| 68 | d5 += z;
|
| 69 | }
|
| 70 |
|
| 71 | return new AxisAlignedBB(d0, d1, d2, d3, d4, d5);
|
| 72 | }
|
| 73 |
|
| 74 | /**
|
| 75 | * Returns a bounding box expanded by the specified vector (if negative numbers are given it will shrink). Args: x,
|
| 76 | * y, z
|
| 77 | */
|
| 78 | public AxisAlignedBB expand(double x, double y, double z)
|
| 79 | {
|
| 80 | double d0 = this.minX - x;
|
| 81 | double d1 = this.minY - y;
|
| 82 | double d2 = this.minZ - z;
|
| 83 | double d3 = this.maxX + x;
|
| 84 | double d4 = this.maxY + y;
|
| 85 | double d5 = this.maxZ + z;
|
| 86 | return new AxisAlignedBB(d0, d1, d2, d3, d4, d5);
|
| 87 | }
|
| 88 |
|
| 89 | public AxisAlignedBB union(AxisAlignedBB other)
|
| 90 | {
|
| 91 | double d0 = Math.min(this.minX, other.minX);
|
| 92 | double d1 = Math.min(this.minY, other.minY);
|
| 93 | double d2 = Math.min(this.minZ, other.minZ);
|
| 94 | double d3 = Math.max(this.maxX, other.maxX);
|
| 95 | double d4 = Math.max(this.maxY, other.maxY);
|
| 96 | double d5 = Math.max(this.maxZ, other.maxZ);
|
| 97 | return new AxisAlignedBB(d0, d1, d2, d3, d4, d5);
|
| 98 | }
|
| 99 |
|
| 100 | /**
|
| 101 | * returns an AABB with corners x1, y1, z1 and x2, y2, z2
|
| 102 | */
|
| 103 | public static AxisAlignedBB fromBounds(double x1, double y1, double z1, double x2, double y2, double z2)
|
| 104 | {
|
| 105 | double d0 = Math.min(x1, x2);
|
| 106 | double d1 = Math.min(y1, y2);
|
| 107 | double d2 = Math.min(z1, z2);
|
| 108 | double d3 = Math.max(x1, x2);
|
| 109 | double d4 = Math.max(y1, y2);
|
| 110 | double d5 = Math.max(z1, z2);
|
| 111 | return new AxisAlignedBB(d0, d1, d2, d3, d4, d5);
|
| 112 | }
|
| 113 |
|
| 114 | /**
|
| 115 | * Offsets the current bounding box by the specified coordinates. Args: x, y, z
|
| 116 | */
|
| 117 | public AxisAlignedBB offset(double x, double y, double z)
|
| 118 | {
|
| 119 | return new AxisAlignedBB(this.minX + x, this.minY + y, this.minZ + z, this.maxX + x, this.maxY + y, this.maxZ + z);
|
| 120 | }
|
| 121 |
|
| 122 | /**
|
| 123 | * if instance and the argument bounding boxes overlap in the Y and Z dimensions, calculate the offset between them
|
| 124 | * in the X dimension. return var2 if the bounding boxes do not overlap or if var2 is closer to 0 then the
|
| 125 | * calculated offset. Otherwise return the calculated offset.
|
| 126 | */
|
| 127 | public double calculateXOffset(AxisAlignedBB other, double offsetX)
|
| 128 | {
|
| 129 | if (other.maxY > this.minY && other.minY < this.maxY && other.maxZ > this.minZ && other.minZ < this.maxZ)
|
| 130 | {
|
| 131 | if (offsetX > 0.0D && other.maxX <= this.minX)
|
| 132 | {
|
| 133 | double d1 = this.minX - other.maxX;
|
| 134 |
|
| 135 | if (d1 < offsetX)
|
| 136 | {
|
| 137 | offsetX = d1;
|
| 138 | }
|
| 139 | }
|
| 140 | else if (offsetX < 0.0D && other.minX >= this.maxX)
|
| 141 | {
|
| 142 | double d0 = this.maxX - other.minX;
|
| 143 |
|
| 144 | if (d0 > offsetX)
|
| 145 | {
|
| 146 | offsetX = d0;
|
| 147 | }
|
| 148 | }
|
| 149 |
|
| 150 | return offsetX;
|
| 151 | }
|
| 152 | else
|
| 153 | {
|
| 154 | return offsetX;
|
| 155 | }
|
| 156 | }
|
| 157 |
|
| 158 | /**
|
| 159 | * if instance and the argument bounding boxes overlap in the X and Z dimensions, calculate the offset between them
|
| 160 | * in the Y dimension. return var2 if the bounding boxes do not overlap or if var2 is closer to 0 then the
|
| 161 | * calculated offset. Otherwise return the calculated offset.
|
| 162 | */
|
| 163 | public double calculateYOffset(AxisAlignedBB other, double offsetY)
|
| 164 | {
|
| 165 | if (other.maxX > this.minX && other.minX < this.maxX && other.maxZ > this.minZ && other.minZ < this.maxZ)
|
| 166 | {
|
| 167 | if (offsetY > 0.0D && other.maxY <= this.minY)
|
| 168 | {
|
| 169 | double d1 = this.minY - other.maxY;
|
| 170 |
|
| 171 | if (d1 < offsetY)
|
| 172 | {
|
| 173 | offsetY = d1;
|
| 174 | }
|
| 175 | }
|
| 176 | else if (offsetY < 0.0D && other.minY >= this.maxY)
|
| 177 | {
|
| 178 | double d0 = this.maxY - other.minY;
|
| 179 |
|
| 180 | if (d0 > offsetY)
|
| 181 | {
|
| 182 | offsetY = d0;
|
| 183 | }
|
| 184 | }
|
| 185 |
|
| 186 | return offsetY;
|
| 187 | }
|
| 188 | else
|
| 189 | {
|
| 190 | return offsetY;
|
| 191 | }
|
| 192 | }
|
| 193 |
|
| 194 | /**
|
| 195 | * if instance and the argument bounding boxes overlap in the Y and X dimensions, calculate the offset between them
|
| 196 | * in the Z dimension. return var2 if the bounding boxes do not overlap or if var2 is closer to 0 then the
|
| 197 | * calculated offset. Otherwise return the calculated offset.
|
| 198 | */
|
| 199 | public double calculateZOffset(AxisAlignedBB other, double offsetZ)
|
| 200 | {
|
| 201 | if (other.maxX > this.minX && other.minX < this.maxX && other.maxY > this.minY && other.minY < this.maxY)
|
| 202 | {
|
| 203 | if (offsetZ > 0.0D && other.maxZ <= this.minZ)
|
| 204 | {
|
| 205 | double d1 = this.minZ - other.maxZ;
|
| 206 |
|
| 207 | if (d1 < offsetZ)
|
| 208 | {
|
| 209 | offsetZ = d1;
|
| 210 | }
|
| 211 | }
|
| 212 | else if (offsetZ < 0.0D && other.minZ >= this.maxZ)
|
| 213 | {
|
| 214 | double d0 = this.maxZ - other.minZ;
|
| 215 |
|
| 216 | if (d0 > offsetZ)
|
| 217 | {
|
| 218 | offsetZ = d0;
|
| 219 | }
|
| 220 | }
|
| 221 |
|
| 222 | return offsetZ;
|
| 223 | }
|
| 224 | else
|
| 225 | {
|
| 226 | return offsetZ;
|
| 227 | }
|
| 228 | }
|
| 229 |
|
| 230 | /**
|
| 231 | * Returns whether the given bounding box intersects with this one. Args: axisAlignedBB
|
| 232 | */
|
| 233 | public boolean intersectsWith(AxisAlignedBB other)
|
| 234 | {
|
| 235 | return other.maxX > this.minX && other.minX < this.maxX ? (other.maxY > this.minY && other.minY < this.maxY ? other.maxZ > this.minZ && other.minZ < this.maxZ : false) : false;
|
| 236 | }
|
| 237 |
|
| 238 | /**
|
| 239 | * Returns if the supplied Vec3D is completely inside the bounding box
|
| 240 | */
|
| 241 | public boolean isVecInside(Vec3 vec)
|
| 242 | {
|
| 243 | return vec.xCoord > this.minX && vec.xCoord < this.maxX ? (vec.yCoord > this.minY && vec.yCoord < this.maxY ? vec.zCoord > this.minZ && vec.zCoord < this.maxZ : false) : false;
|
| 244 | }
|
| 245 |
|
| 246 | /**
|
| 247 | * Returns the average length of the edges of the bounding box.
|
| 248 | */
|
| 249 | public double getAverageEdgeLength()
|
| 250 | {
|
| 251 | double d0 = this.maxX - this.minX;
|
| 252 | double d1 = this.maxY - this.minY;
|
| 253 | double d2 = this.maxZ - this.minZ;
|
| 254 | return (d0 + d1 + d2) / 3.0D;
|
| 255 | }
|
| 256 |
|
| 257 | /**
|
| 258 | * Returns a bounding box that is inset by the specified amounts
|
| 259 | */
|
| 260 | public AxisAlignedBB contract(double x, double y, double z)
|
| 261 | {
|
| 262 | double d0 = this.minX + x;
|
| 263 | double d1 = this.minY + y;
|
| 264 | double d2 = this.minZ + z;
|
| 265 | double d3 = this.maxX - x;
|
| 266 | double d4 = this.maxY - y;
|
| 267 | double d5 = this.maxZ - z;
|
| 268 | return new AxisAlignedBB(d0, d1, d2, d3, d4, d5);
|
| 269 | }
|
| 270 |
|
| 271 | public MovingObjectPosition calculateIntercept(Vec3 vecA, Vec3 vecB)
|
| 272 | {
|
| 273 | Vec3 vec3 = vecA.getIntermediateWithXValue(vecB, this.minX);
|
| 274 | Vec3 vec31 = vecA.getIntermediateWithXValue(vecB, this.maxX);
|
| 275 | Vec3 vec32 = vecA.getIntermediateWithYValue(vecB, this.minY);
|
| 276 | Vec3 vec33 = vecA.getIntermediateWithYValue(vecB, this.maxY);
|
| 277 | Vec3 vec34 = vecA.getIntermediateWithZValue(vecB, this.minZ);
|
| 278 | Vec3 vec35 = vecA.getIntermediateWithZValue(vecB, this.maxZ);
|
| 279 |
|
| 280 | if (!this.isVecInYZ(vec3))
|
| 281 | {
|
| 282 | vec3 = null;
|
| 283 | }
|
| 284 |
|
| 285 | if (!this.isVecInYZ(vec31))
|
| 286 | {
|
| 287 | vec31 = null;
|
| 288 | }
|
| 289 |
|
| 290 | if (!this.isVecInXZ(vec32))
|
| 291 | {
|
| 292 | vec32 = null;
|
| 293 | }
|
| 294 |
|
| 295 | if (!this.isVecInXZ(vec33))
|
| 296 | {
|
| 297 | vec33 = null;
|
| 298 | }
|
| 299 |
|
| 300 | if (!this.isVecInXY(vec34))
|
| 301 | {
|
| 302 | vec34 = null;
|
| 303 | }
|
| 304 |
|
| 305 | if (!this.isVecInXY(vec35))
|
| 306 | {
|
| 307 | vec35 = null;
|
| 308 | }
|
| 309 |
|
| 310 | Vec3 vec36 = null;
|
| 311 |
|
| 312 | if (vec3 != null)
|
| 313 | {
|
| 314 | vec36 = vec3;
|
| 315 | }
|
| 316 |
|
| 317 | if (vec31 != null && (vec36 == null || vecA.squareDistanceTo(vec31) < vecA.squareDistanceTo(vec36)))
|
| 318 | {
|
| 319 | vec36 = vec31;
|
| 320 | }
|
| 321 |
|
| 322 | if (vec32 != null && (vec36 == null || vecA.squareDistanceTo(vec32) < vecA.squareDistanceTo(vec36)))
|
| 323 | {
|
| 324 | vec36 = vec32;
|
| 325 | }
|
| 326 |
|
| 327 | if (vec33 != null && (vec36 == null || vecA.squareDistanceTo(vec33) < vecA.squareDistanceTo(vec36)))
|
| 328 | {
|
| 329 | vec36 = vec33;
|
| 330 | }
|
| 331 |
|
| 332 | if (vec34 != null && (vec36 == null || vecA.squareDistanceTo(vec34) < vecA.squareDistanceTo(vec36)))
|
| 333 | {
|
| 334 | vec36 = vec34;
|
| 335 | }
|
| 336 |
|
| 337 | if (vec35 != null && (vec36 == null || vecA.squareDistanceTo(vec35) < vecA.squareDistanceTo(vec36)))
|
| 338 | {
|
| 339 | vec36 = vec35;
|
| 340 | }
|
| 341 |
|
| 342 | if (vec36 == null)
|
| 343 | {
|
| 344 | return null;
|
| 345 | }
|
| 346 | else
|
| 347 | {
|
| 348 | EnumFacing enumfacing = null;
|
| 349 |
|
| 350 | if (vec36 == vec3)
|
| 351 | {
|
| 352 | enumfacing = EnumFacing.WEST;
|
| 353 | }
|
| 354 | else if (vec36 == vec31)
|
| 355 | {
|
| 356 | enumfacing = EnumFacing.EAST;
|
| 357 | }
|
| 358 | else if (vec36 == vec32)
|
| 359 | {
|
| 360 | enumfacing = EnumFacing.DOWN;
|
| 361 | }
|
| 362 | else if (vec36 == vec33)
|
| 363 | {
|
| 364 | enumfacing = EnumFacing.UP;
|
| 365 | }
|
| 366 | else if (vec36 == vec34)
|
| 367 | {
|
| 368 | enumfacing = EnumFacing.NORTH;
|
| 369 | }
|
| 370 | else
|
| 371 | {
|
| 372 | enumfacing = EnumFacing.SOUTH;
|
| 373 | }
|
| 374 |
|
| 375 | return new MovingObjectPosition(vec36, enumfacing);
|
| 376 | }
|
| 377 | }
|
| 378 |
|
| 379 | /**
|
| 380 | * Checks if the specified vector is within the YZ dimensions of the bounding box. Args: Vec3D
|
| 381 | */
|
| 382 | private boolean isVecInYZ(Vec3 vec)
|
| 383 | {
|
| 384 | return vec == null ? false : vec.yCoord >= this.minY && vec.yCoord <= this.maxY && vec.zCoord >= this.minZ && vec.zCoord <= this.maxZ;
|
| 385 | }
|
| 386 |
|
| 387 | /**
|
| 388 | * Checks if the specified vector is within the XZ dimensions of the bounding box. Args: Vec3D
|
| 389 | */
|
| 390 | private boolean isVecInXZ(Vec3 vec)
|
| 391 | {
|
| 392 | return vec == null ? false : vec.xCoord >= this.minX && vec.xCoord <= this.maxX && vec.zCoord >= this.minZ && vec.zCoord <= this.maxZ;
|
| 393 | }
|
| 394 |
|
| 395 | /**
|
| 396 | * Checks if the specified vector is within the XY dimensions of the bounding box. Args: Vec3D
|
| 397 | */
|
| 398 | private boolean isVecInXY(Vec3 vec)
|
| 399 | {
|
| 400 | return vec == null ? false : vec.xCoord >= this.minX && vec.xCoord <= this.maxX && vec.yCoord >= this.minY && vec.yCoord <= this.maxY;
|
| 401 | }
|
| 402 |
|
| 403 | public String toString()
|
| 404 | {
|
| 405 | return "box[" + this.minX + ", " + this.minY + ", " + this.minZ + " -> " + this.maxX + ", " + this.maxY + ", " + this.maxZ + "]";
|
| 406 | }
|
| 407 |
|
| 408 | public boolean hasNaN()
|
| 409 | {
|
| 410 | return Double.isNaN(this.minX) || Double.isNaN(this.minY) || Double.isNaN(this.minZ) || Double.isNaN(this.maxX) || Double.isNaN(this.maxY) || Double.isNaN(this.maxZ);
|
| 411 | }
|
| 412 | }
|
| 413 |
|