package org.apache.lucene.geo;
import org.apache.lucene.index.PointValues.Relation;
import org.apache.lucene.util.SloppyMath;
class Circle2D implements Component2D {
private final DistanceCalculator calculator;
private Circle2D(DistanceCalculator calculator) {
this.calculator = calculator;
}
@Override
public double getMinX() {
return calculator.getMinX();
}
@Override
public double getMaxX() {
return calculator.getMaxX();
}
@Override
public double getMinY() {
return calculator.getMinY();
}
@Override
public double getMaxY() {
return calculator.getMaxY();
}
@Override
public boolean contains(double x, double y) {
return calculator.contains(x, y);
}
@Override
public Relation relate(double minX, double maxX, double minY, double maxY) {
if (calculator.disjoint(minX, maxX, minY, maxY)) {
return Relation.CELL_OUTSIDE_QUERY;
}
if (calculator.within(minX, maxX, minY, maxY)) {
return Relation.CELL_CROSSES_QUERY;
}
return calculator.relate(minX, maxX, minY, maxY);
}
@Override
public boolean intersectsLine(double minX, double maxX, double minY, double maxY,
double aX, double aY, double bX, double bY) {
if (calculator.disjoint(minX, maxX, minY, maxY)) {
return false;
}
return contains(aX, aY) || contains(bX, bY) ||
calculator.intersectsLine(aX, aY, bX, bY);
}
@Override
public boolean intersectsTriangle(double minX, double maxX, double minY, double maxY,
double aX, double aY, double bX, double bY, double cX, double cY) {
if (calculator.disjoint(minX, maxX, minY, maxY)) {
return false;
}
return contains(aX, aY) || contains(bX, bY) || contains(cX, cY) ||
Component2D.pointInTriangle(minX, maxX, minY, maxY, calculator.geX(), calculator.getY(), aX, aY, bX, bY, cX, cY) ||
calculator.intersectsLine(aX, aY, bX, bY) ||
calculator.intersectsLine(bX, bY, cX, cY) ||
calculator.intersectsLine(cX, cY, aX, aY);
}
@Override
public boolean containsLine(double minX, double maxX, double minY, double maxY,
double aX, double aY, double bX, double bY) {
if (calculator.disjoint(minX, maxX, minY, maxY)) {
return false;
}
return contains(aX, aY) && contains(bX, bY);
}
@Override
public boolean containsTriangle(double minX, double maxX, double minY, double maxY,
double aX, double aY, double bX, double bY, double cX, double cY) {
if (calculator.disjoint(minX, maxX, minY, maxY)) {
return false;
}
return contains(aX, aY) && contains(bX, bY) && contains(cX, cY);
}
@Override
public WithinRelation withinPoint(double x, double y) {
return WithinRelation.DISJOINT;
}
@Override
public WithinRelation withinLine(double minX, double maxX, double minY, double maxY,
double aX, double aY, boolean ab, double bX, double bY) {
if (calculator.disjoint(minX, maxX, minY, maxY)) {
return WithinRelation.DISJOINT;
}
if (ab == true && calculator.intersectsLine(aX, aY, bX, bY)) {
return WithinRelation.NOTWITHIN;
}
return 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 (calculator.disjoint(minX, maxX, minY, maxY)) {
return WithinRelation.DISJOINT;
}
if (contains(aX, aY) || contains(bX, bY) || contains(cX, cY)) {
return WithinRelation.NOTWITHIN;
}
if (ab == true && calculator.intersectsLine(aX, aY, bX, bY)) {
return WithinRelation.NOTWITHIN;
}
if (bc == true && calculator.intersectsLine(bX, bY, cX, cY)) {
return WithinRelation.NOTWITHIN;
}
if (ca == true && calculator.intersectsLine(cX, cY, aX, aY)) {
return WithinRelation.NOTWITHIN;
}
if (Component2D.pointInTriangle(minX, maxX, minY, maxY, calculator.geX(), calculator.getY(), aX, aY, bX, bY, cX, cY) == true) {
return WithinRelation.CANDIDATE;
}
return WithinRelation.DISJOINT;
}
private static boolean intersectsLine(double centerX, double centerY, double aX, double aY, double bX, double bY, DistanceCalculator calculator) {
final double vectorAPX = centerX - aX;
final double vectorAPY = centerY - aY;
final double vectorABX = bX - aX;
final double vectorABY = bY - aY;
final double magnitudeAB = vectorABX * vectorABX + vectorABY * vectorABY;
final double dotProduct = vectorAPX * vectorABX + vectorAPY * vectorABY;
final double distance = dotProduct / magnitudeAB;
if (distance < 0 || distance > dotProduct) {
return false;
}
final double pX = aX + vectorABX * distance;
final double pY = aY + vectorABY * distance;
final double minX = StrictMath.min(aX, bX);
final double minY = StrictMath.min(aY, bY);
final double maxX = StrictMath.max(aX, bX);
final double maxY = StrictMath.max(aY, bY);
if (pX >= minX && pX <= maxX && pY >= minY && pY <= maxY) {
return calculator.contains(pX, pY);
}
return false;
}
private interface DistanceCalculator {
boolean contains(double x, double y);
boolean intersectsLine(double aX, double aY, double bX, double bY);
Relation relate(double minX, double maxX, double minY, double maxY);
boolean disjoint(double minX, double maxX, double minY, double maxY);
boolean within(double minX, double maxX, double minY, double maxY);
double getMinX();
double getMaxX();
double getMinY();
double getMaxY();
double geX();
double getY();
}
private static class CartesianDistance implements DistanceCalculator {
private final double centerX;
private final double centerY;
private final double radiusSquared;
private final XYRectangle rectangle;
public CartesianDistance(float centerX, float centerY, float radius) {
this.centerX = centerX;
this.centerY = centerY;
this.rectangle = XYRectangle.fromPointDistance(centerX, centerY, radius);
this.radiusSquared = (double) radius * radius;
}
@Override
public Relation relate(double minX, double maxX, double minY, double maxY) {
if (Component2D.containsPoint(centerX, centerY, minX, maxX, minY, maxY)) {
if (contains(minX, minY) && contains(maxX, minY) && contains(maxX, maxY) && contains(minX, maxY)) {
return Relation.CELL_INSIDE_QUERY;
}
} else {
double sumOfSquaredDiffs = 0.0d;
if (centerX < minX) {
double diff = minX - centerX;
sumOfSquaredDiffs += diff * diff;
} else if (centerX > maxX) {
double diff = maxX - centerX;
sumOfSquaredDiffs += diff * diff;
}
if (centerY < minY) {
double diff = minY - centerY;
sumOfSquaredDiffs += diff * diff;
} else if (centerY > maxY) {
double diff = maxY - centerY;
sumOfSquaredDiffs += diff * diff;
}
if (sumOfSquaredDiffs > radiusSquared) {
return Relation.CELL_OUTSIDE_QUERY;
}
}
return Relation.CELL_CROSSES_QUERY;
}
@Override
public boolean contains(double x, double y) {
if (Component2D.containsPoint(x, y, rectangle.minX, rectangle.maxX, rectangle.minY, rectangle.maxY)) {
final double diffX = x - this.centerX;
final double diffY = y - this.centerY;
return diffX * diffX + diffY * diffY <= radiusSquared;
}
return false;
}
@Override
public boolean intersectsLine(double aX, double aY, double bX, double bY) {
return Circle2D.intersectsLine(centerX, centerY, aX, aY, bX, bY, this);
}
@Override
public boolean disjoint(double minX, double maxX, double minY, double maxY) {
return Component2D.disjoint(rectangle.minX, rectangle.maxX, rectangle.minY, rectangle.maxY, minX, maxX, minY, maxY);
}
@Override
public boolean within(double minX, double maxX, double minY, double maxY) {
return Component2D.within(rectangle.minX, rectangle.maxX, rectangle.minY, rectangle.maxY, minX, maxX, minY, maxY);
}
@Override
public double getMinX() {
return rectangle.minX;
}
@Override
public double getMaxX() {
return rectangle.maxX;
}
@Override
public double getMinY() {
return rectangle.minY;
}
@Override
public double getMaxY() {
return rectangle.maxY;
}
@Override
public double geX() {
return centerX;
}
@Override
public double getY() {
return centerY;
}
}
private static class HaversinDistance implements DistanceCalculator {
final double centerLat;
final double centerLon;
final double sortKey;
final double axisLat;
final Rectangle rectangle;
final boolean crossesDateline;
public HaversinDistance(double centerLon, double centerLat, double radius) {
this.centerLat = centerLat;
this.centerLon = centerLon;
this.sortKey = GeoUtils.distanceQuerySortKey(radius);
this.axisLat = Rectangle.axisLat(centerLat, radius);
this.rectangle = Rectangle.fromPointDistance(centerLat, centerLon, radius);
this.crossesDateline = rectangle.minLon > rectangle.maxLon;
}
@Override
public Relation relate(double minX, double maxX, double minY, double maxY) {
return GeoUtils.relate(minY, maxY, minX, maxX, centerLat, centerLon, sortKey, axisLat);
}
@Override
public boolean contains(double x, double y) {
if (crossesDateline) {
if (Component2D.containsPoint(x, y, rectangle.minLon, GeoUtils.MAX_LON_INCL, rectangle.minLat, rectangle.maxLat) ||
Component2D.containsPoint(x, y, GeoUtils.MIN_LON_INCL, rectangle.maxLon, rectangle.minLat, rectangle.maxLat)) {
return SloppyMath.haversinSortKey(y, x, this.centerLat, this.centerLon) <= sortKey;
}
} else {
if (Component2D.containsPoint(x, y, rectangle.minLon, rectangle.maxLon, rectangle.minLat, rectangle.maxLat)) {
return SloppyMath.haversinSortKey(y, x, this.centerLat, this.centerLon) <= sortKey;
}
}
return false;
}
@Override
public boolean intersectsLine(double aX, double aY, double bX, double bY) {
if (Circle2D.intersectsLine(centerLon, centerLat, aX, aY, bX, bY, this)) {
return true;
}
if (crossesDateline) {
double newCenterLon = (centerLon > 0) ? centerLon - 360 : centerLon + 360;
return Circle2D.intersectsLine(newCenterLon, centerLat, aX, aY, bX, bY, this);
}
return false;
}
@Override
public boolean disjoint(double minX, double maxX, double minY, double maxY) {
if (crossesDateline) {
return Component2D.disjoint(rectangle.minLon, GeoUtils.MAX_LON_INCL, rectangle.minLat, rectangle.maxLat, minX, maxX, minY, maxY)
&& Component2D.disjoint(GeoUtils.MIN_LON_INCL, rectangle.maxLon, rectangle.minLat, rectangle.maxLat, minX, maxX, minY, maxY);
} else {
return Component2D.disjoint(rectangle.minLon, rectangle.maxLon, rectangle.minLat, rectangle.maxLat, minX, maxX, minY, maxY);
}
}
@Override
public boolean within(double minX, double maxX, double minY, double maxY) {
if (crossesDateline) {
return Component2D.within(rectangle.minLon, GeoUtils.MAX_LON_INCL, rectangle.minLat, rectangle.maxLat, minX, maxX, minY, maxY)
|| Component2D.within(GeoUtils.MIN_LON_INCL, rectangle.maxLon, rectangle.minLat, rectangle.maxLat, minX, maxX, minY, maxY);
} else {
return Component2D.within(rectangle.minLon, rectangle.maxLon, rectangle.minLat, rectangle.maxLat, minX, maxX, minY, maxY);
}
}
@Override
public double getMinX() {
if (crossesDateline) {
return GeoUtils.MIN_LON_INCL;
}
return rectangle.minLon;
}
@Override
public double getMaxX() {
if (crossesDateline) {
return GeoUtils.MAX_LON_INCL;
}
return rectangle.maxLon;
}
@Override
public double getMinY() {
return rectangle.minLat;
}
@Override
public double getMaxY() {
return rectangle.maxLat;
}
@Override
public double geX() {
return centerLon;
}
@Override
public double getY() {
return centerLat;
}
}
static Component2D create(XYCircle circle) {
DistanceCalculator calculator = new CartesianDistance(circle.getX(), circle.getY(), circle.getRadius());
return new Circle2D(calculator);
}
static Component2D create(Circle circle) {
DistanceCalculator calculator = new HaversinDistance(circle.getLon(), circle.getLat(), circle.getRadius());
return new Circle2D(calculator);
}
}