package org.apache.lucene.spatial3d.geom;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
class XdYdZSolid extends BaseXYZSolid {
protected final double minX;
protected final double maxX;
protected final double Y;
protected final double Z;
protected final GeoPoint[] surfacePoints;
public XdYdZSolid(final PlanetModel planetModel,
final double minX,
final double maxX,
final double Y,
final double Z) {
super(planetModel);
if (maxX - minX < Vector.MINIMUM_RESOLUTION)
throw new IllegalArgumentException("X values in wrong order or identical");
this.minX = minX;
this.maxX = maxX;
this.Y = Y;
this.Z = Z;
final Plane yPlane = new Plane(yUnitVector,-Y);
final Plane zPlane = new Plane(zUnitVector,-Z);
final SidedPlane minXPlane = new SidedPlane(maxX,0.0,0.0,xUnitVector,-minX);
final SidedPlane maxXPlane = new SidedPlane(minX,0.0,0.0,xUnitVector,-maxX);
surfacePoints = yPlane.findIntersections(planetModel,zPlane,minXPlane,maxXPlane);
}
public XdYdZSolid(final PlanetModel planetModel, final InputStream inputStream) throws IOException {
this(planetModel,
SerializableObject.readDouble(inputStream),
SerializableObject.readDouble(inputStream),
SerializableObject.readDouble(inputStream),
SerializableObject.readDouble(inputStream));
}
@Override
public void write(final OutputStream outputStream) throws IOException {
SerializableObject.writeDouble(outputStream, minX);
SerializableObject.writeDouble(outputStream, maxX);
SerializableObject.writeDouble(outputStream, Y);
SerializableObject.writeDouble(outputStream, Z);
}
@Override
protected GeoPoint[] getEdgePoints() {
return surfacePoints;
}
@Override
public boolean isWithin(final double x, final double y, final double z) {
for (final GeoPoint p : surfacePoints) {
if (p.isIdentical(x,y,z))
return true;
}
return false;
}
@Override
public int getRelationship(final GeoShape path) {
final int insideRectangle = isShapeInsideArea(path);
if (insideRectangle == SOME_INSIDE) {
return OVERLAPS;
}
final int insideShape = isAreaInsideShape(path);
if (insideShape == SOME_INSIDE) {
return OVERLAPS;
}
if (insideRectangle == ALL_INSIDE && insideShape == ALL_INSIDE) {
return OVERLAPS;
}
if (insideRectangle == ALL_INSIDE) {
return WITHIN;
}
if (insideShape == ALL_INSIDE) {
return CONTAINS;
}
return DISJOINT;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof XdYdZSolid))
return false;
XdYdZSolid other = (XdYdZSolid) o;
if (!super.equals(other) || surfacePoints.length != other.surfacePoints.length ) {
return false;
}
for (int i = 0; i < surfacePoints.length; i++) {
if (!surfacePoints[i].equals(other.surfacePoints[i]))
return false;
}
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
for (final GeoPoint p : surfacePoints) {
result = 31 * result + p.hashCode();
}
return result;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
for (final GeoPoint p : surfacePoints) {
sb.append(" ").append(p).append(" ");
}
return "XdYdZSolid: {planetmodel="+planetModel+", "+sb.toString()+"}";
}
}