Skip to content

Commit b32f735

Browse files
committed
Extract shared delta-decode loop in CompactLineStringUtils
Both uncompactLineString overloads had a duplicate delta-decode loop. Extract it into a private decodeDeltaCoordinates() helper that both methods now call with their respective offset and initial origin.
1 parent e162d51 commit b32f735

1 file changed

Lines changed: 30 additions & 23 deletions

File tree

street/src/main/java/org/opentripplanner/street/geometry/CompactLineStringUtils.java

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -141,54 +141,61 @@ public static LineString uncompactLineString(
141141
boolean reverse
142142
) {
143143
int[] coords = DlugoszVarLenIntPacker.unpack(packedCoords);
144-
int size = coords == null ? 2 : (coords.length / 2) + 2;
145-
Coordinate[] c = new Coordinate[size];
146144
double x0 = reverse ? xb : xa;
147145
double y0 = reverse ? yb : ya;
148146
double x1 = reverse ? xa : xb;
149147
double y1 = reverse ? ya : yb;
148+
int intermediateCount = coords == null ? 0 : coords.length / 2;
149+
Coordinate[] c = new Coordinate[intermediateCount + 2];
150150
c[0] = new Coordinate(x0, y0);
151151
if (coords != null) {
152152
int oix = IntUtils.round(x0 * FIXED_FLOAT_MULT);
153153
int oiy = IntUtils.round(y0 * FIXED_FLOAT_MULT);
154-
for (int i = 1; i < c.length - 1; i++) {
155-
int ix = oix + coords[(i - 1) * 2];
156-
int iy = oiy + coords[(i - 1) * 2 + 1];
157-
c[i] = new Coordinate(ix / FIXED_FLOAT_MULT, iy / FIXED_FLOAT_MULT);
158-
oix = ix;
159-
oiy = iy;
160-
}
154+
decodeDeltaCoordinates(coords, c, 1, oix, oiy);
161155
}
162156
c[c.length - 1] = new Coordinate(x1, y1);
163157
LineString out = GeometryUtils.makeLineString(c);
164-
if (reverse) {
165-
out = out.reverse();
166-
}
167-
return out;
158+
return reverse ? out.reverse() : out;
168159
}
169160

170161
/**
171162
* Uncompact a line string that was compacted without start/end endpoint context. Decodes the
172-
* delta-encoded coordinates directly, avoiding the intermediate dummy-endpoint LineString and
173-
* subsequent strip that the previous implementation used.
163+
* delta-encoded coordinates directly without adding/removing dummy endpoints.
174164
*/
175165
public static LineString uncompactLineString(byte[] packedCoords, boolean reverse) {
176166
int[] coords = DlugoszVarLenIntPacker.unpack(packedCoords);
177167
if (coords == null || coords.length == 0) {
178168
return GeometryUtils.makeLineString(new Coordinate[0]);
179169
}
180-
int size = coords.length / 2;
181-
Coordinate[] c = new Coordinate[size];
182-
int oix = 0;
183-
int oiy = 0;
184-
for (int i = 0; i < size; i++) {
170+
Coordinate[] c = new Coordinate[coords.length / 2];
171+
decodeDeltaCoordinates(coords, c, 0, 0, 0);
172+
LineString out = GeometryUtils.makeLineString(c);
173+
return reverse ? out.reverse() : out;
174+
}
175+
176+
/**
177+
* Decode delta-encoded coordinate pairs into a Coordinate array.
178+
*
179+
* @param coords Delta-encoded int pairs [dx0, dy0, dx1, dy1, ...]
180+
* @param out Target array to write decoded coordinates into
181+
* @param offset Starting index in {@code out} to write to
182+
* @param oix Initial x in fixed-point (start of delta chain)
183+
* @param oiy Initial y in fixed-point (start of delta chain)
184+
*/
185+
private static void decodeDeltaCoordinates(
186+
int[] coords,
187+
Coordinate[] out,
188+
int offset,
189+
int oix,
190+
int oiy
191+
) {
192+
int count = coords.length / 2;
193+
for (int i = 0; i < count; i++) {
185194
int ix = oix + coords[i * 2];
186195
int iy = oiy + coords[i * 2 + 1];
187-
c[i] = new Coordinate(ix / FIXED_FLOAT_MULT, iy / FIXED_FLOAT_MULT);
196+
out[offset + i] = new Coordinate(ix / FIXED_FLOAT_MULT, iy / FIXED_FLOAT_MULT);
188197
oix = ix;
189198
oiy = iy;
190199
}
191-
LineString out = GeometryUtils.makeLineString(c);
192-
return reverse ? out.reverse() : out;
193200
}
194201
}

0 commit comments

Comments
 (0)