Skip to content

Commit 139c8ba

Browse files
committed
Simplify FacetSequence implementation
1 parent 1e7f248 commit 139c8ba

File tree

3 files changed

+31
-73
lines changed

3 files changed

+31
-73
lines changed

modules/core/src/main/java/org/locationtech/jts/operation/distance/FacetSequence.java

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,20 @@
1616
import org.locationtech.jts.geom.Coordinate;
1717
import org.locationtech.jts.geom.CoordinateSequence;
1818
import org.locationtech.jts.geom.Envelope;
19-
import org.locationtech.jts.geom.Geometry;
2019
import org.locationtech.jts.geom.LineSegment;
2120

2221
/**
2322
* Represents a sequence of facets (points or line segments)
24-
* of a {@link Geometry}
2523
* specified by a subsequence of a {@link CoordinateSequence}.
2624
*
2725
* @author Martin Davis
2826
*
2927
*/
3028
public class FacetSequence
3129
{
32-
private Geometry geom = null;
3330
private CoordinateSequence pts;
3431
private int start;
35-
private int end;
36-
37-
/**
38-
* Creates a new sequence of facets based on a {@link CoordinateSequence}
39-
* contained in the given {@link Geometry}.
40-
*
41-
* @param geom the geometry containing the facets
42-
* @param pts the sequence containing the facet points
43-
* @param start the index of the start point
44-
* @param end the index of the end point + 1
45-
*/
46-
public FacetSequence(Geometry geom, CoordinateSequence pts, int start, int end)
47-
{
48-
this.geom = geom;
49-
this.pts = pts;
50-
this.start = start;
51-
this.end = end;
52-
}
32+
private int end;
5333

5434
/**
5535
* Creates a new sequence of facets based on a {@link CoordinateSequence}.
@@ -139,19 +119,19 @@ else if (isPointOther) {
139119
* and another sequence.
140120
* The locations are presented in the same order as the input sequences.
141121
*
142-
* @return a pair of {@link GeometryLocation}s for the nearest points
122+
* @return a pair of {@link Coordinate}s for the nearest points
143123
*/
144-
public GeometryLocation[] nearestLocations(FacetSequence facetSeq)
124+
public Coordinate[] nearestLocations(FacetSequence facetSeq)
145125
{
146126
boolean isPoint = isPoint();
147127
boolean isPointOther = facetSeq.isPoint();
148-
GeometryLocation[] locs = new GeometryLocation[2];
128+
Coordinate[] locs = new Coordinate[2];
149129

150130
if (isPoint && isPointOther) {
151131
Coordinate pt = pts.getCoordinate(start);
152132
Coordinate seqPt = facetSeq.pts.getCoordinate(facetSeq.start);
153-
locs[0] = new GeometryLocation(geom, start, new Coordinate(pt));
154-
locs[1] = new GeometryLocation(facetSeq.geom, facetSeq.start, new Coordinate(seqPt));
133+
locs[0] = pt.copy();
134+
locs[1] = seqPt.copy();
155135
}
156136
else if (isPoint) {
157137
Coordinate pt = pts.getCoordinate(start);
@@ -161,7 +141,7 @@ else if (isPointOther) {
161141
Coordinate seqPt = facetSeq.pts.getCoordinate(facetSeq.start);
162142
computeDistancePointLine(seqPt, this, locs);
163143
// unflip the locations
164-
GeometryLocation tmp = locs[0];
144+
Coordinate tmp = locs[0];
165145
locs[0] = locs[1];
166146
locs[1] = tmp;
167147
}
@@ -171,7 +151,7 @@ else if (isPointOther) {
171151
return locs;
172152
}
173153

174-
private double computeDistanceLineLine(FacetSequence facetSeq, GeometryLocation[] locs)
154+
private double computeDistanceLineLine(FacetSequence facetSeq, Coordinate[] locs)
175155
{
176156
// both linear - compute minimum segment-segment distance
177157
double minDistance = Double.MAX_VALUE;
@@ -195,15 +175,15 @@ private double computeDistanceLineLine(FacetSequence facetSeq, GeometryLocation[
195175
}
196176

197177
private void updateNearestLocationsLineLine(int i, Coordinate p0, Coordinate p1, FacetSequence facetSeq, int j,
198-
Coordinate q0, Coordinate q1, GeometryLocation[] locs) {
178+
Coordinate q0, Coordinate q1, Coordinate[] locs) {
199179
LineSegment seg0 = new LineSegment(p0, p1);
200180
LineSegment seg1 = new LineSegment(q0, q1);
201181
Coordinate[] closestPt = seg0.closestPoints(seg1);
202-
locs[0] = new GeometryLocation(geom, i, new Coordinate(closestPt[0]));
203-
locs[1] = new GeometryLocation(facetSeq.geom, j, new Coordinate(closestPt[1]));
182+
locs[0] = closestPt[0].copy();
183+
locs[1] = closestPt[1].copy();
204184
}
205185

206-
private double computeDistancePointLine(Coordinate pt, FacetSequence facetSeq, GeometryLocation[] locs)
186+
private double computeDistancePointLine(Coordinate pt, FacetSequence facetSeq, Coordinate[] locs)
207187
{
208188
double minDistance = Double.MAX_VALUE;
209189

@@ -222,11 +202,11 @@ private double computeDistancePointLine(Coordinate pt, FacetSequence facetSeq, G
222202

223203
private void updateNearestLocationsPointLine(Coordinate pt,
224204
FacetSequence facetSeq, int i, Coordinate q0, Coordinate q1,
225-
GeometryLocation[] locs) {
226-
locs[0] = new GeometryLocation(geom, start, new Coordinate(pt));
205+
Coordinate[] locs) {
206+
locs[0] = pt.copy();
227207
LineSegment seg = new LineSegment(q0, q1);
228208
Coordinate segClosestPoint = seg.closestPoint(pt);
229-
locs[1] = new GeometryLocation(facetSeq.geom, i, new Coordinate(segClosestPoint));
209+
locs[1] = segClosestPoint.copy();
230210
}
231211

232212
public String toString()

modules/core/src/main/java/org/locationtech/jts/operation/distance/FacetSequenceTreeBuilder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,18 @@ public void filter(Geometry geom) {
5757
CoordinateSequence seq = null;
5858
if (geom instanceof LineString) {
5959
seq = ((LineString) geom).getCoordinateSequence();
60-
addFacetSequences(geom, seq, sections);
60+
addFacetSequences(seq, sections);
6161
}
6262
else if (geom instanceof Point) {
6363
seq = ((Point) geom).getCoordinateSequence();
64-
addFacetSequences(geom, seq, sections);
64+
addFacetSequences(seq, sections);
6565
}
6666
}
6767
});
6868
return sections;
6969
}
7070

71-
private static void addFacetSequences(Geometry geom, CoordinateSequence pts, List sections) {
71+
private static void addFacetSequences(CoordinateSequence pts, List sections) {
7272
int i = 0;
7373
int size = pts.size();
7474
while (i <= size - 1) {
@@ -77,7 +77,7 @@ private static void addFacetSequences(Geometry geom, CoordinateSequence pts, Lis
7777
// section
7878
if (end >= size - 1)
7979
end = size;
80-
FacetSequence sect = new FacetSequence(geom, pts, i, end);
80+
FacetSequence sect = new FacetSequence(pts, i, end);
8181
sections.add(sect);
8282
i = i + FACET_SEQUENCE_SIZE;
8383
}

modules/core/src/main/java/org/locationtech/jts/operation/distance/IndexedFacetDistance.java

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -125,53 +125,34 @@ public IndexedFacetDistance(Geometry geom) {
125125
*/
126126
public double distance(Geometry g)
127127
{
128-
STRtree tree2 = FacetSequenceTreeBuilder.build(g);
129-
Object[] obj = cachedTree.nearestNeighbour(tree2,
130-
FACET_SEQ_DIST);
128+
Object[] obj = nearestFacets(g);
131129
FacetSequence fs1 = (FacetSequence) obj[0];
132130
FacetSequence fs2 = (FacetSequence) obj[1];
133131
return fs1.distance(fs2);
134132
}
133+
134+
private Object[] nearestFacets(Geometry g) {
135+
STRtree tree2 = FacetSequenceTreeBuilder.build(g);
136+
Object[] obj = cachedTree.nearestNeighbour(tree2,
137+
FACET_SEQ_DIST);
138+
return obj;
139+
}
135140

136141
/**
137-
* Computes the nearest locations on the base geometry
142+
* Computes the nearest points on the base geometry
138143
* and the given geometry.
139144
*
140145
* @param g the geometry to compute the nearest location to
141-
* @return the nearest locations
146+
* @return the nearest points
142147
*/
143-
public GeometryLocation[] nearestLocations(Geometry g)
148+
public Coordinate[] nearestPoints(Geometry g)
144149
{
145-
STRtree tree2 = FacetSequenceTreeBuilder.build(g);
146-
Object[] obj = cachedTree.nearestNeighbour(tree2,
147-
FACET_SEQ_DIST);
150+
Object[] obj = nearestFacets(g);
148151
FacetSequence fs1 = (FacetSequence) obj[0];
149152
FacetSequence fs2 = (FacetSequence) obj[1];
150153
return fs1.nearestLocations(fs2);
151154
}
152155

153-
/**
154-
* Compute the nearest locations on the target geometry
155-
* and the given geometry.
156-
*
157-
* @param g the geometry to compute the nearest point to
158-
* @return the nearest points
159-
*/
160-
public Coordinate[] nearestPoints(Geometry g) {
161-
GeometryLocation[] minDistanceLocation = nearestLocations(g);
162-
Coordinate[] nearestPts = toPoints(minDistanceLocation);
163-
return nearestPts;
164-
}
165-
166-
private static Coordinate[] toPoints(GeometryLocation[] locations) {
167-
if (locations == null)
168-
return null;
169-
Coordinate[] nearestPts = new Coordinate[] {
170-
locations[0].getCoordinate(),
171-
locations[1].getCoordinate() };
172-
return nearestPts;
173-
}
174-
175156
/**
176157
* Tests whether the base geometry lies within
177158
* a specified distance of the given geometry.
@@ -200,9 +181,6 @@ public double distance(ItemBoundable item1, ItemBoundable item2) {
200181
return fs1.distance(fs2);
201182
}
202183
}
203-
204-
205-
206184
}
207185

208186

0 commit comments

Comments
 (0)