Skip to content

Commit

Permalink
Refactor predicate API
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-jts committed Jan 20, 2024
1 parent 74a0965 commit 0d0de8a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ protected void updateValue(boolean val) {
updateValue(TopologyPredicateValue.toValue(val));
}

protected void setRequire(boolean cond) {
if (! cond)
updateValue(false);
}

protected void setValueIf(boolean value, boolean cond) {
if (cond)
updateValue(value);
}

protected void updateValue(int val) {
//-- don't change already-known value
if (isKnown())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package org.locationtech.jts.operation.relateng;

import org.locationtech.jts.geom.Dimension;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.IntersectionMatrix;
import org.locationtech.jts.geom.Location;

Expand Down Expand Up @@ -62,6 +63,10 @@ protected int valuePartial() {
return TopologyPredicateValue.UNKNOWN;
}

protected void requireCovers(Envelope a, Envelope b) {
setRequire(a.covers(b));
}

protected boolean intersectsExteriorOf(boolean isA) {
if (isA) {
return isIntersects(Location.EXTERIOR, Location.INTERIOR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@ public static TopologyPredicate intersects() {

@Override
public void init(Envelope envA, Envelope envB) {
updateValue(requireIntersects(envA, envB));
setRequire(envA.intersects(envB));
}

@Override
public void updateDim(int locA, int locB, int dimension) {
if (isIntersection(locA, locB)) {
updateValue(true);
}
setValueIf(true, isIntersection(locA, locB));
}

@Override
public void finish() {
//-- if no intersecting locations were found
updateValue(false);
}

Expand All @@ -49,19 +48,17 @@ public static TopologyPredicate disjoint() {

@Override
public void init(Envelope envA, Envelope envB) {
updateValue(valueIf(true, envA.disjoint(envB)));
setValueIf(true, envA.disjoint(envB));
}

@Override
public void updateDim(int locA, int locB, int dimension) {
if (isIntersection(locA, locB)) {
updateValue(false);
}
setValueIf(false, isIntersection(locA, locB));
}

@Override
public void finish() {
//-- no intersecting locations have been found
//-- if no intersecting locations were found
updateValue(true);
}

Expand All @@ -76,12 +73,12 @@ public static TopologyPredicate contains() {
@Override
public void init(int dimA, int dimB) {
super.init(dimA, dimB);
updateValue( require( isDimsCompatibleWithCovers(dimA, dimB) ));
setRequire( isDimsCompatibleWithCovers(dimA, dimB) );
}

@Override
public void init(Envelope envA, Envelope envB) {
updateValue(requireCovers(envA, envB));
requireCovers(envA, envB);
}

@Override
Expand All @@ -104,12 +101,12 @@ public static TopologyPredicate covers() {
@Override
public void init(int dimA, int dimB) {
super.init(dimA, dimB);
updateValue( require( isDimsCompatibleWithCovers(dimA, dimB) ));
setRequire( isDimsCompatibleWithCovers(dimA, dimB) );
}

@Override
public void init(Envelope envA, Envelope envB) {
updateValue(requireCovers(envA, envB));
requireCovers(envA, envB);
}

@Override
Expand All @@ -132,12 +129,12 @@ public static TopologyPredicate within() {
@Override
public void init(int dimA, int dimB) {
super.init(dimA, dimB);
updateValue( require( isDimsCompatibleWithCovers(dimB, dimA) ));
setRequire( isDimsCompatibleWithCovers(dimB, dimA) );
}

@Override
public void init(Envelope envA, Envelope envB) {
updateValue(requireCovers(envB, envA));
requireCovers(envB, envA);
}

@Override
Expand All @@ -158,12 +155,12 @@ public static TopologyPredicate coveredBy() {
@Override
public void init(int dimA, int dimB) {
super.init(dimA, dimB);
updateValue( require( isDimsCompatibleWithCovers(dimB, dimA) ));
setRequire( isDimsCompatibleWithCovers(dimB, dimA) );
}

@Override
public void init(Envelope envA, Envelope envB) {
updateValue(requireCovers(envB, envA));
requireCovers(envB, envA);
}

@Override
Expand All @@ -187,7 +184,7 @@ public void init(int dimA, int dimB) {
super.init(dimA, dimB);
boolean isBothPointsOrAreas = (dimA == Dimension.P && dimB == Dimension.P)
|| (dimA == Dimension.A && dimB == Dimension.A);
updateValue( require(! isBothPointsOrAreas));
setRequire(! isBothPointsOrAreas);
}

@Override
Expand Down Expand Up @@ -226,12 +223,12 @@ public static TopologyPredicate equalsTopo() {
@Override
public void init(int dimA, int dimB) {
super.init(dimA, dimB);
updateValue( require(dimA == dimB));
setRequire(dimA == dimB);
}

@Override
public void init(Envelope envA, Envelope envB) {
updateValue( requireEquals(envA, envB));
setRequire(envA.equals(envB));
}

@Override
Expand Down Expand Up @@ -259,7 +256,7 @@ public static TopologyPredicate overlaps() {
@Override
public void init(int dimA, int dimB) {
super.init(dimA, dimB);
updateValue( require(dimA == dimB));
setRequire(dimA == dimB);
}

@Override
Expand Down Expand Up @@ -295,7 +292,7 @@ public void init(int dimA, int dimB) {
super.init(dimA, dimB);
//-- Points have only interiors, so cannot touch
boolean isBothPoints = dimA == 0 && dimB == 0;
updateValue( require(! isBothPoints));
setRequire(! isBothPoints);
}

@Override
Expand All @@ -312,33 +309,6 @@ public boolean valueIM() {
};
}

public static int requireEquals(Envelope envA, Envelope envB) {
return require(envA.equals(envB));
}

public static int requireCovers(Envelope envA, Envelope envB) {
return require(envA.covers(envB));
}

public static int requireIntersects(Envelope envA, Envelope envB) {
return require(envA.intersects(envB));
}

/**
* Returns FALSE if the condition is not met,
* or UNKNOWN if it is.
* Equivalent to "valueFalseIfNot".
*
* @param cond the required condition
* @return FALSE or UNKNOWN
*/
public static int require(boolean cond) {
if (! cond) {
return TopologyPredicateValue.FALSE;
}
return TopologyPredicateValue.UNKNOWN;
}

/**
* Returns a known predicate value if the condition holds.
* Otherwise returns UNKNOWN.
Expand Down

0 comments on commit 0d0de8a

Please sign in to comment.