package org.apache.lucene.geo;
import java.util.Arrays;
import static org.apache.lucene.geo.XYEncodingUtils.checkVal;
public final class XYPolygon extends XYGeometry {
private final float[] x;
private final float[] y;
private final XYPolygon[] holes;
public final float minX;
public final float maxX;
public final float minY;
public final float maxY;
private final GeoUtils.WindingOrder windingOrder;
public XYPolygon(float[] x, float[] y, XYPolygon... holes) {
if (x == null) {
throw new IllegalArgumentException("x must not be null");
}
if (y == null) {
throw new IllegalArgumentException("y must not be null");
}
if (holes == null) {
throw new IllegalArgumentException("holes must not be null");
}
if (x.length != y.length) {
throw new IllegalArgumentException("x and y must be equal length");
}
if (x.length < 4) {
throw new IllegalArgumentException("at least 4 polygon points required");
}
if (x[0] != x[x.length-1]) {
throw new IllegalArgumentException("first and last points of the polygon must be the same (it must close itself): x[0]=" + x[0] + " x[" + (x.length-1) + "]=" + x[x.length-1]);
}
if (y[0] != y[y.length-1]) {
throw new IllegalArgumentException("first and last points of the polygon must be the same (it must close itself): y[0]=" + y[0] + " y[" + (y.length-1) + "]=" + y[y.length-1]);
}
for (int i = 0; i < holes.length; i++) {
XYPolygon inner = holes[i];
if (inner.holes.length > 0) {
throw new IllegalArgumentException("holes may not contain holes: polygons may not nest.");
}
}
this.x = x.clone();
this.y = y.clone();
this.holes = holes.clone();
float minX = checkVal(x[0]);
float maxX = x[0];
float minY = checkVal(y[0]);
float maxY = y[0];
double windingSum = 0d;
final int numPts = x.length - 1;
for (int i = 1, j = 0; i < numPts; j = i++) {
minX = Math.min(checkVal(x[i]), minX);
maxX = Math.max(x[i], maxX);
minY = Math.min(checkVal(y[i]), minY);
maxY = Math.max(y[i], maxY);
windingSum += (x[j] - x[numPts])*(y[i] - y[numPts])
- (y[j] - y[numPts])*(x[i] - x[numPts]);
}
this.minX = minX;
this.maxX = maxX;
this.minY = minY;
this.maxY = maxY;
this.windingOrder = (windingSum < 0) ? GeoUtils.WindingOrder.CCW : GeoUtils.WindingOrder.CW;
}
public int numPoints() {
return x.length;
}
public float[] getPolyX() {
return x.clone();
}
public float getPolyX(int vertex) {
return x[vertex];
}
public float[] getPolyY() {
return y.clone();
}
public float getPolyY(int vertex) {
return y[vertex];
}
public XYPolygon[] getHoles() {
return holes.clone();
}
XYPolygon getHole(int i) {
return holes[i];
}
public GeoUtils.WindingOrder getWindingOrder() {
return this.windingOrder;
}
public int numHoles() {
return holes.length;
}
@Override
protected Component2D toComponent2D() {
return Polygon2D.create(this);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(holes);
result = prime * result + Arrays.hashCode(x);
result = prime * result + Arrays.hashCode(y);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
XYPolygon other = (XYPolygon) obj;
if (!Arrays.equals(holes, other.holes)) return false;
if (!Arrays.equals(x, other.x)) return false;
if (!Arrays.equals(y, other.y)) return false;
return true;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < x.length; i++) {
sb.append("[")
.append(x[i])
.append(", ")
.append(y[i])
.append("] ");
}
if (holes.length > 0) {
sb.append(", holes=");
sb.append(Arrays.toString(holes));
}
return sb.toString();
}
}