1 package com.irtimaled.bbor.client.models;
3 import com.irtimaled.bbor.common.BoundingBoxType;
4 import com.irtimaled.bbor.common.TypeHelper;
5 import com.irtimaled.bbor.common.models.AbstractBoundingBox;
7 public class BoundingBoxLine extends AbstractBoundingBox {
8 private final Point minPoint;
9 private final Point maxPoint;
10 private final Double width;
11 private final Point[] corners;
13 protected BoundingBoxLine(Point minPoint, Point maxPoint, double width, BoundingBoxType type, Point... corners) {
15 this.minPoint = minPoint;
16 this.maxPoint = maxPoint;
18 this.corners = corners;
21 public static BoundingBoxLine from(Point minPoint, Point maxPoint, Double width, BoundingBoxType type) {
22 if (width == 0) return new BoundingBoxLine(minPoint, maxPoint, width, type);
24 double halfWidth = width / 2.0d;
26 double dx = maxPoint.getX() - minPoint.getX();
27 double dz = maxPoint.getZ() - minPoint.getZ();
29 double dxm = dx == 0 ? 0 : dx / Math.abs(dx);
30 double dzm = dz == 0 ? 0 : dz / Math.abs(dz);
33 if (dxm == 0 || dzm == 0) {
34 xc = Math.abs(dzm) * halfWidth;
35 zc = Math.abs(dxm) * halfWidth;
37 double h = Math.sqrt(dx * dx + dz * dz);
38 double theta = Math.acos((dz * dz + h * h - dx * dx) / (2 * dz * h));
39 zc = halfWidth * Math.sin(theta);
40 xc = Math.sqrt(halfWidth * halfWidth - zc * zc) * dxm * dzm;
43 return new BoundingBoxLine(minPoint, maxPoint, width, type,
44 new Point(minPoint.getX() + xc, minPoint.getY(), minPoint.getZ() - zc),
45 new Point(minPoint.getX() - xc, minPoint.getY(), minPoint.getZ() + zc),
46 new Point(maxPoint.getX() - xc, maxPoint.getY(), maxPoint.getZ() + zc),
47 new Point(maxPoint.getX() + xc, maxPoint.getY(), maxPoint.getZ() - zc));
51 public int hashCode() {
52 return TypeHelper.combineHashCodes(minPoint.hashCode(), maxPoint.hashCode());
56 public boolean equals(Object obj) {
57 if (this == obj) return true;
58 if (obj == null || getClass() != obj.getClass()) return false;
59 BoundingBoxLine other = (BoundingBoxLine) obj;
60 return minPoint.equals(other.minPoint) && maxPoint.equals(other.maxPoint);
63 public Point getMinPoint() {
67 public Point getMaxPoint() {
71 public double getWidth() {
75 public Point[] getCorners() {
80 public Boolean intersectsBounds(int minX, int minZ, int maxX, int maxZ) {
81 boolean minXWithinBounds = isBetween(minPoint.getX(), minX, maxX);
82 boolean maxXWithinBounds = isBetween(maxPoint.getX(), minX, maxX);
83 boolean minZWithinBounds = isBetween(minPoint.getZ(), minZ, maxZ);
84 boolean maxZWithinBounds = isBetween(maxPoint.getZ(), minZ, maxZ);
86 return (minXWithinBounds && minZWithinBounds) ||
87 (maxXWithinBounds && maxZWithinBounds) ||
88 (minXWithinBounds && maxZWithinBounds) ||
89 (maxXWithinBounds && minZWithinBounds);
92 private boolean isBetween(double val, int min, int max) {
93 return val >= min && val <= max;