package org.apache.lucene.geo;
import org.apache.lucene.index.PointValues;
import static org.apache.lucene.geo.GeoUtils.orient;
final class Point2D implements Component2D {
final private double x;
final private double y;
private Point2D(double x, double y) {
this.x = x;
this.y = y;
}
@Override
public double getMinX() {
return x;
}
@Override
public double getMaxX() {
return x;
}
@Override
public double getMinY() {
return y;
}
@Override
public double getMaxY() {
return y;
}
@Override
public boolean contains(double x, double y) {
return x == this.x && y == this.y;
}
@Override
public PointValues.Relation relate(double minX, double maxX, double minY, double maxY) {
if (Component2D.containsPoint(x, y, minX, maxX, minY, maxY)) {
return PointValues.Relation.CELL_CROSSES_QUERY;
}
return PointValues.Relation.CELL_OUTSIDE_QUERY;
}
@Override
public boolean intersectsLine(double minX, double maxX, double minY, double maxY,
double aX, double aY, double bX, double bY) {
return Component2D.containsPoint(x, y, minX, maxX, minY, maxY) &&
orient(aX, aY, bX, bY, x, y) == 0;
}
@Override
public boolean intersectsTriangle(double minX, double maxX, double minY, double maxY,
double aX, double aY, double bX, double bY, double cX, double cY) {
return Component2D.pointInTriangle(minX, maxX, minY, maxY, x, y, aX, aY, bX, bY, cX, cY);
}
@Override
public boolean containsLine(double minX, double maxX, double minY, double maxY,
double aX, double aY, double bX, double bY) {
return false;
}
@Override
public boolean containsTriangle(double minX, double maxX, double minY, double maxY,
double aX, double aY, double bX, double bY, double cX, double cY) {
return false;
}
@Override
public WithinRelation withinPoint(double x, double y) {
return contains(x, y) ? WithinRelation.CANDIDATE : WithinRelation.DISJOINT;
}
@Override
public WithinRelation withinLine(double minX, double maxX, double minY, double maxY,
double aX, double aY, boolean ab, double bX, double bY) {
return intersectsLine(minX, maxX, minY, maxY, aX, aY, bX, bY) ? WithinRelation.CANDIDATE : WithinRelation.DISJOINT;
}
@Override
public WithinRelation withinTriangle(double minX, double maxX, double minY, double maxY,
double aX, double aY, boolean ab, double bX, double bY, boolean bc, double cX, double cY, boolean ca) {
if (Component2D.pointInTriangle(minX, maxX, minY, maxY, x, y, aX, aY, bX, bY, cX, cY)) {
return WithinRelation.CANDIDATE;
}
return WithinRelation.DISJOINT;
}
static Component2D create(Point point) {
return new Point2D(GeoEncodingUtils.decodeLongitude(GeoEncodingUtils.encodeLongitude(point.getLon())),
GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(point.getLat())));
}
static Component2D create(XYPoint xyPoint) {
return new Point2D(xyPoint.getX(), xyPoint.getY());
}
}