1 package com.irtimaled.bbor.client.models;
3 import com.irtimaled.bbor.common.BoundingBoxType;
4 import com.irtimaled.bbor.common.MathHelper;
5 import com.irtimaled.bbor.common.TypeHelper;
6 import com.irtimaled.bbor.common.models.AbstractBoundingBox;
8 public class BoundingBoxLine extends AbstractBoundingBox {
9 private final Point minPoint;
10 private final Point maxPoint;
11 private final Double width;
12 private final Point[] corners;
14 protected BoundingBoxLine(Point minPoint, Point maxPoint, double width, BoundingBoxType type, Point... corners) {
16 this.minPoint = minPoint;
17 this.maxPoint = maxPoint;
19 this.corners = corners;
22 public static BoundingBoxLine from(Point minPoint, Point maxPoint, Double width, BoundingBoxType type) {
23 if (width == 0) return new BoundingBoxLine(minPoint, maxPoint, width, type);
25 double halfWidth = width / 2.0d;
27 double dx = maxPoint.getX() - minPoint.getX();
28 double dz = maxPoint.getZ() - minPoint.getZ();
30 double dxm = dx == 0 ? 0 : dx / Math.abs(dx);
31 double dzm = dz == 0 ? 0 : dz / Math.abs(dz);
34 if (dxm == 0 || dzm == 0) {
35 xc = Math.abs(dzm) * halfWidth;
36 zc = Math.abs(dxm) * halfWidth;
38 double h = Math.sqrt(dx * dx + dz * dz);
39 double theta = Math.acos((dz * dz + h * h - dx * dx) / (2 * dz * h));
40 zc = halfWidth * Math.sin(theta);
41 xc = Math.sqrt(halfWidth * halfWidth - zc * zc) * dxm * dzm;
44 return new BoundingBoxLine(minPoint, maxPoint, width, type,
45 new Point(minPoint.getX() + xc, minPoint.getY(), minPoint.getZ() - zc),
46 new Point(minPoint.getX() - xc, minPoint.getY(), minPoint.getZ() + zc),
47 new Point(maxPoint.getX() - xc, maxPoint.getY(), maxPoint.getZ() + zc),
48 new Point(maxPoint.getX() + xc, maxPoint.getY(), maxPoint.getZ() - zc));
52 public int hashCode() {
53 return TypeHelper.combineHashCodes(minPoint.hashCode(), maxPoint.hashCode());
57 public boolean equals(Object obj) {
58 if (this == obj) return true;
59 if (obj == null || getClass() != obj.getClass()) return false;
60 BoundingBoxLine other = (BoundingBoxLine) obj;
61 return minPoint.equals(other.minPoint) && maxPoint.equals(other.maxPoint);
64 public Point getMinPoint() {
68 public Point getMaxPoint() {
72 public double getWidth() {
76 public Point[] getCorners() {
81 public Boolean intersectsBounds(int minX, int minZ, int maxX, int maxZ) {
82 boolean minXWithinBounds = isBetween(minPoint.getX(), minX, maxX);
83 boolean maxXWithinBounds = isBetween(maxPoint.getX(), minX, maxX);
84 boolean minZWithinBounds = isBetween(minPoint.getZ(), minZ, maxZ);
85 boolean maxZWithinBounds = isBetween(maxPoint.getZ(), minZ, maxZ);
87 return (minXWithinBounds && minZWithinBounds) ||
88 (maxXWithinBounds && maxZWithinBounds) ||
89 (minXWithinBounds && maxZWithinBounds) ||
90 (maxXWithinBounds && minZWithinBounds);
94 protected double getDistanceX(double x) {
95 return x - MathHelper.clamp(x, minPoint.getX(), maxPoint.getX());
99 protected double getDistanceY(double y) {
100 return y - MathHelper.clamp(y, minPoint.getY(), maxPoint.getY());
104 protected double getDistanceZ(double z) {
105 return z - MathHelper.clamp(z, minPoint.getZ(), maxPoint.getZ());
108 private boolean isBetween(double val, int min, int max) {
109 return val >= min && val <= max;