Skip to content

Commit c105123

Browse files
authored
Do more of line simplification with integer coordinates (#113)
* Turn off unit, which has bit-rotted * Instrument line simplification * Do more work in integer space * Update tests with simplification done with more integers * Clean up * Update changelog * Eradicate calls to deprecated sprintf() * Revert "Turn off unit, which has bit-rotted" This reverts commit 9987793. * Fix catch by switching to an unsigned type
1 parent b09a880 commit c105123

File tree

59 files changed

+282
-280
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+282
-280
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Do more of line simplification in integer coordinates, to make behavior consistent across platforms
2+
13
# 2.26.1
24

35
* Avoid crashing if there is a polygon ring with only one point

catch/catch.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -6461,7 +6461,7 @@ namespace Catch {
64616461
namespace Catch {
64626462

64636463
struct RandomNumberGenerator {
6464-
typedef int result_type;
6464+
typedef unsigned int result_type;
64656465

64666466
result_type operator()( result_type n ) const { return std::rand() % n; }
64676467

geojson-loop.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static void json_context(json_object *j) {
2929
char *s = json_stringify(j);
3030

3131
if (strlen(s) >= 500) {
32-
sprintf(s + 497, "...");
32+
snprintf(s + 497, strlen(s) + 1 - 497, "...");
3333
}
3434

3535
fprintf(stderr, "in JSON object %s\n", s);

geometry.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -811,8 +811,8 @@ drawvec clip_lines(drawvec &geom, long long minx, long long miny, long long maxx
811811
}
812812

813813
static double square_distance_from_line(long long point_x, long long point_y, long long segA_x, long long segA_y, long long segB_x, long long segB_y) {
814-
double p2x = segB_x - segA_x;
815-
double p2y = segB_y - segA_y;
814+
long long p2x = segB_x - segA_x;
815+
long long p2y = segB_y - segA_y;
816816
double something = p2x * p2x + p2y * p2y;
817817
double u = 0 == something ? 0 : ((point_x - segA_x) * p2x + (point_y - segA_y) * p2y) / something;
818818

@@ -822,11 +822,11 @@ static double square_distance_from_line(long long point_x, long long point_y, lo
822822
u = 0;
823823
}
824824

825-
double x = segA_x + u * p2x;
826-
double y = segA_y + u * p2y;
825+
long long x = std::round(segA_x + u * p2x);
826+
long long y = std::round(segA_y + u * p2y);
827827

828-
double dx = x - point_x;
829-
double dy = y - point_y;
828+
long long dx = x - point_x;
829+
long long dy = y - point_y;
830830

831831
return dx * dx + dy * dy;
832832
}

jsontool.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ std::string sort_quote(const char *s) {
114114
for (size_t i = 0; i < utf32.size(); i++) {
115115
if (utf32[i] < 0xD800) {
116116
char buf[8];
117-
sprintf(buf, "\\u%04lu", utf32[i]);
117+
snprintf(buf, sizeof(buf), "\\u%04lu", utf32[i]);
118118
ret.append(std::string(buf));
119119
} else {
120120
unsigned long c = utf32[i];

main.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -723,9 +723,9 @@ void radix1(int *geomfds_in, int *indexfds_in, int inputs, int prefix, int split
723723
sub_geompos[i] = 0;
724724

725725
char geomname[strlen(tmpdir) + strlen("/geom.XXXXXXXX") + 1];
726-
sprintf(geomname, "%s%s", tmpdir, "/geom.XXXXXXXX");
726+
snprintf(geomname, sizeof(geomname), "%s%s", tmpdir, "/geom.XXXXXXXX");
727727
char indexname[strlen(tmpdir) + strlen("/index.XXXXXXXX") + 1];
728-
sprintf(indexname, "%s%s", tmpdir, "/index.XXXXXXXX");
728+
snprintf(indexname, sizeof(indexname), "%s%s", tmpdir, "/index.XXXXXXXX");
729729

730730
geomfds[i] = mkstemp_cloexec(geomname);
731731
if (geomfds[i] < 0) {
@@ -1187,10 +1187,10 @@ std::pair<int, metadata> read_input(std::vector<source> &sources, char *fname, i
11871187
char geomname[strlen(tmpdir) + strlen("/geom.XXXXXXXX") + 1];
11881188
char indexname[strlen(tmpdir) + strlen("/index.XXXXXXXX") + 1];
11891189

1190-
sprintf(poolname, "%s%s", tmpdir, "/pool.XXXXXXXX");
1191-
sprintf(treename, "%s%s", tmpdir, "/tree.XXXXXXXX");
1192-
sprintf(geomname, "%s%s", tmpdir, "/geom.XXXXXXXX");
1193-
sprintf(indexname, "%s%s", tmpdir, "/index.XXXXXXXX");
1190+
snprintf(poolname, sizeof(poolname), "%s%s", tmpdir, "/pool.XXXXXXXX");
1191+
snprintf(treename, sizeof(treename), "%s%s", tmpdir, "/tree.XXXXXXXX");
1192+
snprintf(geomname, sizeof(geomname), "%s%s", tmpdir, "/geom.XXXXXXXX");
1193+
snprintf(indexname, sizeof(indexname), "%s%s", tmpdir, "/index.XXXXXXXX");
11941194

11951195
r->poolfd = mkstemp_cloexec(poolname);
11961196
if (r->poolfd < 0) {
@@ -1649,7 +1649,7 @@ std::pair<int, metadata> read_input(std::vector<source> &sources, char *fname, i
16491649
// Serial reading of chunks that are then parsed in parallel
16501650

16511651
char readname[strlen(tmpdir) + strlen("/read.XXXXXXXX") + 1];
1652-
sprintf(readname, "%s%s", tmpdir, "/read.XXXXXXXX");
1652+
snprintf(readname, sizeof(readname), "%s%s", tmpdir, "/read.XXXXXXXX");
16531653
int readfd = mkstemp_cloexec(readname);
16541654
if (readfd < 0) {
16551655
perror(readname);
@@ -1702,7 +1702,7 @@ std::pair<int, metadata> read_input(std::vector<source> &sources, char *fname, i
17021702
checkdisk(&readers);
17031703
ahead = 0;
17041704

1705-
sprintf(readname, "%s%s", tmpdir, "/read.XXXXXXXX");
1705+
snprintf(readname, sizeof(readname), "%s%s", tmpdir, "/read.XXXXXXXX");
17061706
readfd = mkstemp_cloexec(readname);
17071707
if (readfd < 0) {
17081708
perror(readname);
@@ -1837,7 +1837,7 @@ std::pair<int, metadata> read_input(std::vector<source> &sources, char *fname, i
18371837
}
18381838

18391839
char poolname[strlen(tmpdir) + strlen("/pool.XXXXXXXX") + 1];
1840-
sprintf(poolname, "%s%s", tmpdir, "/pool.XXXXXXXX");
1840+
snprintf(poolname, sizeof(poolname), "%s%s", tmpdir, "/pool.XXXXXXXX");
18411841

18421842
int poolfd = mkstemp_cloexec(poolname);
18431843
if (poolfd < 0) {
@@ -1917,7 +1917,7 @@ std::pair<int, metadata> read_input(std::vector<source> &sources, char *fname, i
19171917
}
19181918

19191919
char indexname[strlen(tmpdir) + strlen("/index.XXXXXXXX") + 1];
1920-
sprintf(indexname, "%s%s", tmpdir, "/index.XXXXXXXX");
1920+
snprintf(indexname, sizeof(indexname), "%s%s", tmpdir, "/index.XXXXXXXX");
19211921

19221922
int indexfd = mkstemp_cloexec(indexname);
19231923
if (indexfd < 0) {
@@ -1933,7 +1933,7 @@ std::pair<int, metadata> read_input(std::vector<source> &sources, char *fname, i
19331933
unlink(indexname);
19341934

19351935
char geomname[strlen(tmpdir) + strlen("/geom.XXXXXXXX") + 1];
1936-
sprintf(geomname, "%s%s", tmpdir, "/geom.XXXXXXXX");
1936+
snprintf(geomname, sizeof(geomname), "%s%s", tmpdir, "/geom.XXXXXXXX");
19371937

19381938
int geomfd = mkstemp_cloexec(geomname);
19391939
if (geomfd < 0) {

mvt.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ static std::string quote(std::string const &s) {
481481
buf.push_back(ch);
482482
} else if (ch < ' ') {
483483
char tmp[7];
484-
sprintf(tmp, "\\u%04x", ch);
484+
snprintf(tmp, sizeof(tmp), "\\u%04x", ch);
485485
buf.append(std::string(tmp));
486486
} else {
487487
buf.push_back(ch);

read_json.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void json_context(json_object *j) {
4545
char *s = json_stringify(j);
4646

4747
if (strlen(s) >= 500) {
48-
sprintf(s + 497, "...");
48+
snprintf(s + 497, strlen(s) + 1 - 497, "...");
4949
}
5050

5151
fprintf(stderr, "in JSON object %s\n", s);

0 commit comments

Comments
 (0)