Commit 736b1ef
authored
Fix @turf/nearest-point-on-line endpoint selection and degenerate input cases (#2940)
* @turf/nearest-point-on-line code simplifications
Before fixing behavioral bugs, making some non-behavior changing
cleanups:
- Removing unused distance calculations
- Cleaning up tuples and position variable use in main loop
- Removing unnecessary conditions and types for intersectPt (it will
always exist)
- Added return type to magnitude function
- Reduced duplicate calculations in return values for
nearestPointOnSegment
Fix @turf/nearest-point-on-line #2934 issue
Changed closest point determination logic to ensure that the correct
endpoint is returned in certain cases where the line segment spans more
than Pi radians.
Specific changes:
- Added normalize vector function
- Replaced inline coefficient calculations with existing vector
functions for readability
- Added geometric rationale comments for intermediate steps
- Added minimal failing test for #2934
- Replaced closest intersection point logic with dot product version
- Replaced closest endpoint distance logic with dot product version
(less calculations)
Fix @turf/nearest-point-on-line degenerate cases
Cross product usage leads to several degenerate edge cases that can
lead to spurious failures. This attempts to fix several of them:
- Clamp z-value before asin when converting to lng-lat
- Capture degenerate zero-vectors in the segment's normal from
coincident or antipodal points
- Early return from coincident points as this becomes a pt-pt distance
- Explicit throw from antipodal points as an infinite number of arcs
match
- Early return when the target is coincident with the segment's normal,
here all points are equidistant so choose the endpoint for consistency
@turf/nearest-point-on-line improve test
Improve test addressing degenerate line segment cases. These tests were
failing in browser but succeeding in Chrome. This test fix monkey
patches Node to capture any regressions.
* Fix @Turf nearest-point-on-line incorrect endpoints
Two additional fixes:
- Caught an additional failure case widely separated points could
incorrectly return that the intersection point was inside the line
segment; added regression test for this case
- Elminated throw on antipodal points, instead returning that the point
lies directly on the line; added comment in function signature
mentioning this behavior
* Fix: @turf/nearest-point-on-line failing test
After changes, tests in @turf/point-to-polygon-distance were failing.
Issue confirmed to be due to single bit floating point difference and
updated to new output value.
Also fixed partially missing comment.
* Fix: @turf/nearest-point-on-line faililng test
9edc247 introduced another floating point precision issue that is fixed
here.1 parent ad10a65 commit 736b1ef
File tree
5 files changed
+189
-93
lines changed- packages
- turf-nearest-point-on-line
- turf-point-to-polygon-distance/test_fixtures
5 files changed
+189
-93
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
9 | 13 | | |
10 | 14 | | |
11 | 15 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
16 | 20 | | |
17 | 21 | | |
18 | 22 | | |
| |||
75 | 79 | | |
76 | 80 | | |
77 | 81 | | |
78 | | - | |
79 | 82 | | |
80 | 83 | | |
81 | 84 | | |
82 | 85 | | |
83 | | - | |
84 | 86 | | |
85 | 87 | | |
86 | 88 | | |
| |||
89 | 91 | | |
90 | 92 | | |
91 | 93 | | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
99 | 100 | | |
100 | 101 | | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
105 | 106 | | |
106 | 107 | | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | 108 | | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
126 | 116 | | |
127 | 117 | | |
128 | 118 | | |
| |||
143 | 133 | | |
144 | 134 | | |
145 | 135 | | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
| 136 | + | |
152 | 137 | | |
153 | 138 | | |
154 | 139 | | |
| |||
164 | 149 | | |
165 | 150 | | |
166 | 151 | | |
167 | | - | |
| 152 | + | |
168 | 153 | | |
169 | 154 | | |
170 | 155 | | |
171 | | - | |
172 | | - | |
173 | | - | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
174 | 159 | | |
175 | 160 | | |
176 | 161 | | |
| |||
185 | 170 | | |
186 | 171 | | |
187 | 172 | | |
188 | | - | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
189 | 178 | | |
190 | 179 | | |
191 | 180 | | |
| |||
195 | 184 | | |
196 | 185 | | |
197 | 186 | | |
198 | | - | |
| 187 | + | |
199 | 188 | | |
200 | 189 | | |
201 | 190 | | |
| |||
206 | 195 | | |
207 | 196 | | |
208 | 197 | | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
221 | 220 | | |
222 | | - | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
223 | 224 | | |
224 | | - | |
225 | | - | |
226 | | - | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
227 | 232 | | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
| 233 | + | |
| 234 | + | |
235 | 235 | | |
236 | | - | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
237 | 240 | | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
246 | 246 | | |
247 | 247 | | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
258 | | - | |
259 | | - | |
260 | | - | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
261 | 259 | | |
262 | 260 | | |
263 | | - | |
264 | | - | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
265 | 270 | | |
266 | 271 | | |
267 | 272 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
517 | 518 | | |
518 | 519 | | |
519 | 520 | | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
392 | 392 | | |
393 | 393 | | |
394 | 394 | | |
395 | | - | |
| 395 | + | |
396 | 396 | | |
397 | 397 | | |
398 | 398 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
| 56 | + | |
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| |||
0 commit comments