Skip to content

Commit 12ca77d

Browse files
committed
Improve TestBuilder vertex/segment info
1 parent db94a4a commit 12ca77d

File tree

2 files changed

+39
-43
lines changed

2 files changed

+39
-43
lines changed

modules/app/src/main/java/org/locationtech/jtstest/testbuilder/geom/FacetLocater.java

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import java.util.ArrayList;
1616
import java.util.Collection;
17-
import java.util.Iterator;
1817
import java.util.List;
1918
import java.util.Stack;
2019
import java.util.Vector;
@@ -46,39 +45,38 @@ public class FacetLocater
4645
* @param locations the source collection
4746
* @return a list of the vertex locations, if any
4847
*/
49-
public static List filterVertexLocations(Collection locations)
48+
public static List<GeometryLocation> filterVertexLocations(Collection<GeometryLocation> locations)
5049
{
51-
ArrayList vertexLocs = new ArrayList();
52-
for (Iterator i = locations.iterator(); i.hasNext(); ) {
53-
GeometryLocation loc = (GeometryLocation) i.next();
50+
ArrayList<GeometryLocation> vertexLocs = new ArrayList<GeometryLocation>();
51+
for (GeometryLocation loc : locations) {
5452
if (loc.isVertex()) vertexLocs.add(loc);
5553
}
5654
return vertexLocs;
5755
}
5856

5957
private Geometry parentGeom;
60-
private List locations = new ArrayList();
58+
private List<GeometryLocation> locations = new ArrayList<GeometryLocation>();
6159
private Coordinate queryPt;
6260
private double tolerance = 0.0;
6361

6462
public FacetLocater(Geometry parentGeom) {
6563
this.parentGeom = parentGeom;
6664
}
6765

68-
public List getLocations(Coordinate queryPt, double tolerance)
66+
public List<GeometryLocation> getLocations(Coordinate queryPt, double tolerance)
6967
{
7068
this.queryPt = queryPt;
7169
this.tolerance = tolerance;
7270
findLocations(parentGeom, locations);
7371
return locations;
7472
}
7573

76-
private void findLocations(Geometry geom, List locations)
74+
private void findLocations(Geometry geom, List<GeometryLocation> locations)
7775
{
78-
findLocations(new Stack(), parentGeom, locations);
76+
findLocations(new Stack<Integer>(), parentGeom, locations);
7977
}
8078

81-
private void findLocations(Stack path, Geometry geom, List locations)
79+
private void findLocations(Stack<Integer> path, Geometry geom, List<GeometryLocation> locations)
8280
{
8381
if (geom instanceof GeometryCollection) {
8482
for (int i = 0; i < geom.getNumGeometries(); i++ ) {
@@ -108,7 +106,7 @@ else if (geom instanceof Point) {
108106
}
109107
}
110108

111-
private void findLocations(Stack path, Polygon poly, List locations)
109+
private void findLocations(Stack<Integer> path, Polygon poly, List<GeometryLocation> locations)
112110
{
113111
path.push(0);
114112
findLocations(path,
@@ -125,40 +123,38 @@ private void findLocations(Stack path, Polygon poly, List locations)
125123
}
126124
}
127125

128-
private void findLocations(Stack path, Geometry compGeom, CoordinateSequence seq, List locations)
126+
private void findLocations(Stack<Integer> path, Geometry compGeom, CoordinateSequence seq, List<GeometryLocation> locations)
129127
{
130-
findVertexLocations(path, compGeom, seq, locations);
131-
findSegmentLocations(path, compGeom, seq, locations);
132-
}
133-
134-
private void findVertexLocations(Stack path, Geometry compGeom, CoordinateSequence seq, List locations)
135-
{
136-
for (int i = 0; i < seq.size(); i++) {
137-
Coordinate p = seq.getCoordinate(i);
138-
double dist = p.distance(queryPt);
139-
if (dist <= tolerance)
140-
locations.add(new GeometryLocation(parentGeom, compGeom, toIntArray(path), i, true, p));
141-
}
142-
}
143-
144-
private void findSegmentLocations(Stack path, Geometry compGeom, CoordinateSequence seq, List locations)
145-
{
146-
LineSegment seg = new LineSegment();
147-
for (int i = 0; i < seq.size() - 1; i++) {
128+
int lastVertexIndexAdded = -1;
129+
Coordinate p0 = seq.getCoordinate(0);
130+
if (p0.distance(queryPt) <= tolerance) {
131+
locations.add(new GeometryLocation(parentGeom, compGeom, toIntArray(path), 0, true, p0));
132+
lastVertexIndexAdded = 0;
133+
}
134+
135+
LineSegment seg = new LineSegment();
136+
for (int i = 0; i < seq.size() - 1; i++) {
148137
seg.p0 = seq.getCoordinate(i);
149138
seg.p1 = seq.getCoordinate(i+1);
150-
double dist = seg.distance(queryPt);
151-
if (dist <= tolerance)
152-
locations.add(new GeometryLocation(parentGeom, compGeom, toIntArray(path), i, false, seg.p0));
153-
}
139+
140+
if (seg.p1.distance(queryPt) <= tolerance) {
141+
locations.add(new GeometryLocation(parentGeom, compGeom, toIntArray(path), i+1, true, seg.p1));
142+
lastVertexIndexAdded = i+1;
143+
}
144+
else {
145+
//-- to avoid redundancy only add segment location if vertex was NOT added
146+
double dist = seg.distance(queryPt);
147+
if (dist <= tolerance && i > lastVertexIndexAdded)
148+
locations.add(new GeometryLocation(parentGeom, compGeom, toIntArray(path), i, false, seg.p0));
149+
}
150+
}
154151
}
155152

156-
public static int[] toIntArray(Vector path)
153+
public static int[] toIntArray(Vector<Integer> path)
157154
{
158155
int[] index = new int[path.size()];
159156
int i = 0;
160-
for (Iterator it = path.iterator(); it.hasNext(); ) {
161-
Integer pathIndex = (Integer) it.next();
157+
for (Integer pathIndex : path ) {
162158
index[i++] = pathIndex.intValue();
163159
}
164160
return index;

modules/app/src/main/java/org/locationtech/jtstest/testbuilder/ui/GeometryLocationsWriter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,26 +172,26 @@ public String writeComponentLocation(Geometry geom, Coordinate p, double toleran
172172
public String writeFacetLocation(Geometry geom, Coordinate p, double tolerance)
173173
{
174174
FacetLocater locater = new FacetLocater(geom);
175-
List locs = locater.getLocations(p, tolerance);
176-
List vertexLocs = FacetLocater.filterVertexLocations(locs);
175+
List<GeometryLocation> locs = locater.getLocations(p, tolerance);
176+
/*
177+
List<GeometryLocation> vertexLocs = FacetLocater.filterVertexLocations(locs);
177178
178179
// only show vertices if some are present, to avoid confusing with segments
179180
if (! vertexLocs.isEmpty())
180181
return writeFacetLocations(vertexLocs);
181-
182+
*/
182183
// write 'em all
183184
return writeFacetLocations(locs);
184185
}
185186

186-
private String writeFacetLocations(List locs)
187+
private String writeFacetLocations(List<GeometryLocation> locs)
187188
{
188189
if (locs.size() <= 0) return null;
189190

190191
StringBuffer buf = new StringBuffer();
191192
boolean isFirst = true;
192193
int count = 0;
193-
for (Iterator i = locs.iterator(); i.hasNext(); ) {
194-
GeometryLocation loc = (GeometryLocation) i.next();
194+
for (GeometryLocation loc : locs) {
195195

196196
if (! isFirst) {
197197
buf.append(eol);

0 commit comments

Comments
 (0)