mirror of
https://github.com/mapbox/tippecanoe.git
synced 2025-06-24 17:55:11 +00:00
Compare commits
463 Commits
1.9.2
...
c++-readme
Author | SHA1 | Date | |
---|---|---|---|
801c5d6574 | |||
a00903ecc6 | |||
72086b7e92 | |||
2fdec7d2e4 | |||
859202165d | |||
ece768bfe4 | |||
c1ec437756 | |||
bdac4d4e80 | |||
e53c8ed7e6 | |||
c87d6eadfc | |||
8ab5bb4809 | |||
6a2e80769e | |||
78d91b3fde | |||
275d25739d | |||
b3c116b989 | |||
5549789da5 | |||
56e1f55bb6 | |||
3e4fcd22ec | |||
d7d5bed781 | |||
af3d48e5b3 | |||
ce3ffee8ff | |||
f928133993 | |||
a4f5406cfb | |||
22cb5186d7 | |||
c0d5171f1d | |||
89bfd27a8b | |||
bdd2632fb7 | |||
785d341cde | |||
e8c5759f70 | |||
afa782fb7f | |||
f6d5d1803b | |||
ab9eafa0b7 | |||
f1ff7301c1 | |||
0db8d9ed8b | |||
c68d553e94 | |||
c867ce5f32 | |||
2d022c6c57 | |||
bedace67e7 | |||
e9aa8c1b7d | |||
dc9e68b128 | |||
5479e59aa9 | |||
439b544c8c | |||
a69a087b08 | |||
eb1c724fc4 | |||
9ea29601ab | |||
32aa653082 | |||
013e6512b4 | |||
38ce49d2d4 | |||
ee48be26e0 | |||
700ca489c7 | |||
a8bdbe8012 | |||
689f2ef7e9 | |||
2e3ba8f374 | |||
d0db3323fb | |||
faa5720e3b | |||
2e026f9b3b | |||
0834626d63 | |||
80e5159144 | |||
7e6aa19d42 | |||
8296190487 | |||
93d3c40593 | |||
9fbc7b9a55 | |||
7727b3a92c | |||
565b5dc6b4 | |||
10fc9254d1 | |||
db859e8801 | |||
667e8f7a29 | |||
feb8ac0165 | |||
4b3fc4aebe | |||
1594a09eb5 | |||
057d7b759c | |||
2798bf7b6f | |||
f32916c472 | |||
7cb7fc4913 | |||
3cc95231ec | |||
83e73e8840 | |||
1cfc58267e | |||
e1655941cc | |||
17cd74d7e4 | |||
92cc08a554 | |||
eb1c64db27 | |||
1f38e85f30 | |||
82377944ee | |||
2a856b49bd | |||
71bf20b205 | |||
feb9b4481a | |||
9160e6add5 | |||
a5d803aa9a | |||
6aee0d39df | |||
bbfc7c677e | |||
abac4f2b85 | |||
b26e6a5a4e | |||
c4ee5d3e69 | |||
2e9971c6d5 | |||
081e330845 | |||
81d8fe21f8 | |||
948ea138bb | |||
9d37bd104a | |||
08310d9564 | |||
d381f5a9e1 | |||
5ab41417fc | |||
c8a1b082e0 | |||
896c9d8398 | |||
7258643d5a | |||
4c1b135848 | |||
46b634ce46 | |||
6455b6633e | |||
938274aa91 | |||
2b4280695f | |||
d35dc4936a | |||
57cc343855 | |||
91bfc2ca89 | |||
59619fb6cd | |||
6b4076684c | |||
2bc9e15975 | |||
f7daa05515 | |||
04157e7728 | |||
d4d966893c | |||
9806db3c0a | |||
ef38318a6d | |||
dc86eb6b5a | |||
32f32e45b6 | |||
4912f4ad08 | |||
083a280659 | |||
4fb54eaeeb | |||
4ba98062d6 | |||
87e4a338f6 | |||
021d792d33 | |||
6c74f4a1cd | |||
437152e02b | |||
d7037f3d3a | |||
232056c0da | |||
470c0e2b5c | |||
7a30aeaa6b | |||
8d57f031ee | |||
a37fc361c2 | |||
47288ec05f | |||
c20eab972b | |||
d130ca5d55 | |||
b84b2b066d | |||
965f4c225c | |||
4dd3411c64 | |||
76739fd27b | |||
d8ba9db386 | |||
1bed572350 | |||
1f53491009 | |||
3e881a428c | |||
d490d8475e | |||
d4e1ee0627 | |||
31d2a3738a | |||
531c238c5b | |||
7f49ce5caa | |||
c26fa23564 | |||
5a8a7216cb | |||
7f4ef43113 | |||
6a7a139170 | |||
0d1931319c | |||
9161b74d99 | |||
f7e64dca5f | |||
85fd49f28c | |||
c85303aada | |||
938e6a9eea | |||
b2aa6de898 | |||
be6e3c88d4 | |||
9ef9954d20 | |||
475ce9dd23 | |||
8b339abd40 | |||
bf571571a9 | |||
6de7920c9e | |||
84370c59b8 | |||
67272bfe4a | |||
6d6c1abc64 | |||
6f58d31bc8 | |||
e760521382 | |||
1b1b745419 | |||
10b9af81d3 | |||
7c664dad09 | |||
488dff0efb | |||
2bc1b9bd91 | |||
a1f8564631 | |||
f9609302a9 | |||
92e323435e | |||
26e21b0bee | |||
9908db5e56 | |||
cb45f1c3bd | |||
666729e344 | |||
09ab013461 | |||
d127c43566 | |||
85b27d3a49 | |||
40f2f61d98 | |||
6bc9c5c18a | |||
32fed3b78a | |||
5d06f01e28 | |||
e48e2152cc | |||
e31908bfb4 | |||
d352cd0fee | |||
f552e6951d | |||
ae50abefc4 | |||
0975c91670 | |||
850a36b2f6 | |||
c40ec6c194 | |||
d170ebc312 | |||
8cae9971e8 | |||
3d023f34d3 | |||
864440e9a9 | |||
d18f93df4c | |||
cf4a51c819 | |||
7c0c8e0434 | |||
de46cfa798 | |||
a504840bd5 | |||
2578781a37 | |||
e1427ab9e4 | |||
af412e2038 | |||
5da636ba23 | |||
20b0fe1a52 | |||
9d0a41521f | |||
692112ec3a | |||
4030cc7c58 | |||
b5c5d9dad6 | |||
1d636e5c0d | |||
e253bbfe1d | |||
fa7a52d032 | |||
4638c6f273 | |||
0f02e9fa95 | |||
bba1c13b07 | |||
f03fbdb5c1 | |||
b80081ec38 | |||
331deca4b4 | |||
f0e90620e4 | |||
c0edefa721 | |||
cc4a2736ea | |||
3655a54d22 | |||
167f3c59ef | |||
2a5e2091f3 | |||
24327e195f | |||
4f01b13fe1 | |||
f920c05c75 | |||
5cc6d97d9f | |||
fa523bff2b | |||
0bd06c6d82 | |||
808de5378b | |||
1ce3f950b9 | |||
0f1d2e4220 | |||
43ffd6fe11 | |||
ab3835d249 | |||
f1b3f6d231 | |||
5775d088eb | |||
9fcd079084 | |||
d712edcdc9 | |||
271ec3d154 | |||
68c3bafab0 | |||
1aac686670 | |||
364450ad4c | |||
023ce03672 | |||
adc70341ad | |||
87b90a5033 | |||
f75d9e0dd4 | |||
444de1f086 | |||
3f0904cce8 | |||
40a6b7b37a | |||
5490f3e15f | |||
666565e820 | |||
744915025d | |||
2b393ad8e5 | |||
48a82039ce | |||
24db559f0b | |||
c4274303ea | |||
ee97e6c307 | |||
3662f1a66b | |||
a52733eb07 | |||
f3b9e15267 | |||
65253cba50 | |||
a57c247508 | |||
7b0bb9a443 | |||
3f3a341c0a | |||
3d56a56464 | |||
fb9f3b6068 | |||
575072bb2f | |||
94db232a89 | |||
d32d4bb35f | |||
b12413eddb | |||
8c7ac58ba1 | |||
737ae44dd7 | |||
d4504da2f5 | |||
3182930f35 | |||
d35ef72a99 | |||
87fea8082f | |||
5cfd7cf68f | |||
1e16eb9294 | |||
39c180a673 | |||
60318e664e | |||
6a37ea4e54 | |||
0205086886 | |||
235939ea23 | |||
65964c93f5 | |||
d0b5ba3862 | |||
b2063108b2 | |||
86e6cd717a | |||
ef3b9bea7c | |||
ce64565b38 | |||
bd5dc4ad18 | |||
77bc24d743 | |||
bebb0dda90 | |||
49d29ad368 | |||
33f9d91a4c | |||
a9a14b33e0 | |||
535b328425 | |||
3570d93434 | |||
c4a13fc6e0 | |||
8c82ee4c5f | |||
d72639b8f7 | |||
aaec2c98b6 | |||
52f2804122 | |||
e10a71e152 | |||
7bb4c7dbe9 | |||
2dea1d1564 | |||
23934166b1 | |||
2afd0bf31b | |||
4e71c40f54 | |||
03d5c89742 | |||
9e9afb06d2 | |||
b15956b476 | |||
4cee508e95 | |||
135aea8527 | |||
5ec41d7bbb | |||
f9c4fb8374 | |||
f837577b38 | |||
358f019372 | |||
f902721dab | |||
b91e8f6d3e | |||
2607a76c63 | |||
ea638914ce | |||
41099ed731 | |||
56d8178a7c | |||
24f401da52 | |||
62a74afbdc | |||
694fa8ee97 | |||
45cddc57bb | |||
86b8567201 | |||
04c56320d2 | |||
2a6e41126f | |||
81517a0cb4 | |||
732a51d684 | |||
222735004d | |||
137bb46db5 | |||
cae20bb5e6 | |||
7c9bd5da2f | |||
fbcb00fee9 | |||
4dba8b3f70 | |||
cb4a83776e | |||
e5461b1863 | |||
3a46c05b46 | |||
968c94a409 | |||
efe66dcafe | |||
c6d2988485 | |||
e846b11ce7 | |||
9d48d6a93d | |||
acb97361a0 | |||
64faa1d79e | |||
1e6332bae8 | |||
1792a13e1a | |||
41ae22164c | |||
73fcdba02b | |||
1eb0537302 | |||
39285c8102 | |||
22ede9ac8c | |||
574a2b79c8 | |||
1e31edbfb3 | |||
d1456c0f66 | |||
4a572b810b | |||
b10b436ac9 | |||
e394501faa | |||
c0a0aef060 | |||
2a4be4f6d4 | |||
c62762078a | |||
72e485f285 | |||
b1d3849889 | |||
2b0ac890c5 | |||
d4b9f79d3c | |||
31c4d4850c | |||
2fe841c6dc | |||
7b6cd2d0f4 | |||
c9e542e688 | |||
986719f2ff | |||
e2b36a8ee9 | |||
55f93963be | |||
cf806baca2 | |||
60139532c8 | |||
9793be1517 | |||
851d7b576b | |||
1e8c030b03 | |||
560add2ccd | |||
24deae5a43 | |||
9acecdf93e | |||
8cc844c9dd | |||
389cdf2aa4 | |||
e5157ec66b | |||
4da5a1996b | |||
5d701913ab | |||
62fac4d6f4 | |||
bd3b9a5136 | |||
10cd9c8875 | |||
c404082421 | |||
6f5199adc7 | |||
c6051d876f | |||
72b7dc977a | |||
448d1a124e | |||
edce0f088d | |||
b47653e2e6 | |||
9f2e221338 | |||
5a2a1b793a | |||
cf2abf67d2 | |||
356575d0e0 | |||
7d602987e7 | |||
21a635fb7a | |||
a92bdce12e | |||
8c9aa53bb5 | |||
1e5d420b66 | |||
12be3e5a32 | |||
52dbed8132 | |||
c2231318bd | |||
eee596d5f5 | |||
bc5a7b251f | |||
91ffb084a7 | |||
052c0a55df | |||
76ab63c1d0 | |||
a03b461aa5 | |||
64c98b38b4 | |||
05ff656aba | |||
f37eccafe6 | |||
d4776c8bfb | |||
705a58d255 | |||
54c7cfff4c | |||
eebba7b75b | |||
962f82d44e | |||
d960963623 | |||
4fc6ca3c3b | |||
a989611515 | |||
707036fe79 | |||
7140a3dd91 | |||
16ca5cfeec | |||
c66fd315e1 | |||
7896c3c2c9 | |||
7eccc7a758 | |||
652abc9a62 | |||
c387af48cc | |||
cf5082122a | |||
bf585a5849 | |||
11e737ff29 | |||
310887e019 | |||
81d2a8bfed | |||
b7f3fcfc7f | |||
f17cec5c44 | |||
d38b5a999e | |||
e4ab47b3d6 | |||
b3aa32b9a3 | |||
c9fdd62c91 | |||
739f2dbc75 | |||
fb9525cf4f | |||
ae827af8dc | |||
7e5db337c6 | |||
a847db8c5f | |||
0fcd2d9a79 |
29
.gitignore
vendored
Normal file
29
.gitignore
vendored
Normal file
@ -0,0 +1,29 @@
|
||||
# Compiled Object files
|
||||
*.slo
|
||||
*.lo
|
||||
*.o
|
||||
*.obj
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Compiled Dynamic libraries
|
||||
*.so
|
||||
*.dylib
|
||||
*.dll
|
||||
|
||||
# Fortran module files
|
||||
*.mod
|
||||
*.smod
|
||||
|
||||
# Compiled Static libraries
|
||||
*.lai
|
||||
*.la
|
||||
*.a
|
||||
*.lib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
69
.travis.yml
69
.travis.yml
@ -1,41 +1,76 @@
|
||||
language: c
|
||||
language: generic
|
||||
|
||||
sudo: false
|
||||
|
||||
matrix:
|
||||
include:
|
||||
# debug+integer-santizer build
|
||||
- os: linux
|
||||
compiler: clang
|
||||
env: CLANG_VERSION='3.8.0' BUILDTYPE=Debug CC="clang-3.8" CXX="clang++-3.8" CXXFLAGS="-fsanitize=integer" CFLAGS="-fsanitize=integer" LDFLAGS="-fsanitize=integer"
|
||||
addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test' ]
|
||||
packages: [ 'libstdc++6','libstdc++-5-dev' ]
|
||||
# debug+leak+address-sanitizer build
|
||||
- os: linux
|
||||
compiler: clang
|
||||
env: CLANG_VERSION='3.8.0' BUILDTYPE=Debug ASAN_OPTIONS=detect_leaks=1 CC="clang-3.8" CXX="clang++-3.8" CXXFLAGS="-fsanitize=address" CFLAGS="-fsanitize=address" LDFLAGS="-fsanitize=address"
|
||||
addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test' ]
|
||||
packages: [ 'libstdc++6','libstdc++-5-dev' ]
|
||||
# coverage+debug build
|
||||
- os: linux
|
||||
compiler: clang
|
||||
env: CLANG_VERSION='3.8.0' BUILDTYPE=Debug CC="clang-3.8" CXX="clang++-3.8" CXXFLAGS="--coverage" CFLAGS="--coverage" LDFLAGS="--coverage"
|
||||
addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test' ]
|
||||
packages: [ 'libstdc++6','libstdc++-5-dev' ]
|
||||
# release+linux+g++
|
||||
- os: linux
|
||||
compiler: gcc
|
||||
env: COVERAGE=gcov-4.9
|
||||
env: BUILDTYPE=Release CC="gcc-4.9" CXX="g++-4.9"
|
||||
addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test']
|
||||
packages: [ 'g++-4.9', 'protobuf-compiler', 'libprotobuf-dev' ]
|
||||
packages: [ 'g++-4.9' ]
|
||||
# release+linux+clang++
|
||||
- os: linux
|
||||
compiler: clang
|
||||
env: CLANG_VERSION='3.8.0' BUILDTYPE=Release CC="clang-3.8" CXX="clang++-3.8"
|
||||
addons:
|
||||
apt:
|
||||
packages: [ 'protobuf-compiler', 'libprotobuf-dev' ]
|
||||
sources: ['ubuntu-toolchain-r-test' ]
|
||||
packages: [ 'libstdc++6','libstdc++-5-dev' ]
|
||||
# release+osx
|
||||
- os: osx
|
||||
compiler: clang
|
||||
env: BUILDTYPE=Release
|
||||
# debug+osx
|
||||
- os: osx
|
||||
compiler: clang
|
||||
env: BUILDTYPE=Debug
|
||||
|
||||
before_install:
|
||||
- DEPS_DIR="${TRAVIS_BUILD_DIR}/deps"
|
||||
- export PATH=${DEPS_DIR}/bin:${PATH} && mkdir -p ${DEPS_DIR}
|
||||
- |
|
||||
if [[ ${CLANG_VERSION:-false} != false ]]; then
|
||||
export CCOMPILER='clang'
|
||||
export CXXCOMPILER='clang++'
|
||||
CLANG_URL="https://mason-binaries.s3.amazonaws.com/${TRAVIS_OS_NAME}-x86_64/clang/${CLANG_VERSION}.tar.gz"
|
||||
travis_retry wget --quiet -O - ${CLANG_URL} | tar --strip-components=1 -xz -C ${DEPS_DIR}
|
||||
fi
|
||||
|
||||
install:
|
||||
- if [[ $(uname -s) == 'Darwin' ]]; then brew install protobuf; fi;
|
||||
- if [ -n "${COVERAGE}" ]; then
|
||||
export CXX=g++-4.9;
|
||||
export CC=gcc-4.9;
|
||||
export CXXFLAGS="--coverage -g";
|
||||
export CFLAGS="--coverage -g";
|
||||
export LDFLAGS="--coverage";
|
||||
fi;
|
||||
- make
|
||||
- BUILDTYPE=${BUILDTYPE} make -j
|
||||
|
||||
script:
|
||||
- make test
|
||||
- BUILDTYPE=${BUILDTYPE} make test
|
||||
- if [ -n "${COVERAGE}" ]; then
|
||||
rm vector_tile.pb.o;
|
||||
${COVERAGE} -lp *.o;
|
||||
/usr/bin/llvm-cov-3.5 -lp *.o;
|
||||
pip install --user cpp-coveralls;
|
||||
~/.local/bin/coveralls --no-gcov -i ./ --exclude clipper --exclude vector_tile.pb.cc --exclude vector_tile.pb.h;
|
||||
~/.local/bin/coveralls --no-gcov -i ./ --exclude clipper;
|
||||
fi
|
||||
|
228
CHANGELOG.md
228
CHANGELOG.md
@ -1,3 +1,231 @@
|
||||
## 1.16.4
|
||||
|
||||
* Use Wagyu's quick_lr_clip() instead of a separate implementation
|
||||
|
||||
## 1.16.3
|
||||
|
||||
* Upgrade Wagyu to bfbf2893
|
||||
|
||||
## 1.16.2
|
||||
|
||||
* Associate attributes with the right layer when explicitly tagged
|
||||
|
||||
## 1.16.1
|
||||
|
||||
* Choose a deeper starting tile than 0/0/0 if there is one that contains
|
||||
all the features
|
||||
|
||||
## 1.16.0
|
||||
|
||||
* Switch from Clipper to Wagyu for polygon topology correction
|
||||
|
||||
## 1.15.4
|
||||
|
||||
* Dot-dropping with -r/-B doesn't apply if there is a per-feature minzoom tag
|
||||
|
||||
## 1.15.3
|
||||
|
||||
* Round coordinates in low-zoom grid math instead of truncating
|
||||
|
||||
## 1.15.2
|
||||
|
||||
* Add --grid-low-zooms option to snap low-zoom features to the tile grid
|
||||
|
||||
## 1.15.1
|
||||
|
||||
* Stop --drop-smallest-as-needed from always dropping all points
|
||||
|
||||
## 1.15.0
|
||||
|
||||
* New strategies for making tiles smaller, with uniform behavior across
|
||||
the whole zoom level: --increase-gamma-as-needed,
|
||||
--drop-densest-as-needed, --drop-fraction-as-needed,
|
||||
--drop-smallest-as-needed.
|
||||
* Option to specify the maximum tile size in bytes
|
||||
* Option to turn off tiny polygon reduction
|
||||
* Better error checking in JSON parsing
|
||||
|
||||
## 1.14.4
|
||||
|
||||
* Make -B/-r feature-dropping consistent between tiles and zoom levels
|
||||
|
||||
## 1.14.3
|
||||
|
||||
* Add --detect-shared-borders option for better polygon simplification
|
||||
|
||||
## 1.14.2
|
||||
|
||||
* Enforce that string feature attributes must be encoded as UTF-8
|
||||
|
||||
## 1.14.1
|
||||
|
||||
* Whitespace after commas in tile-join .csv input is no longer significant
|
||||
|
||||
## 1.14.0
|
||||
|
||||
* Tile-join is multithreaded and can merge multiple vector mbtiles files together
|
||||
|
||||
## 1.13.0
|
||||
|
||||
* Add the ability to specify layer names within the GeoJSON input
|
||||
|
||||
## 1.12.11
|
||||
|
||||
* Don't try to revive a placeholder for a degenerate polygon that had negative area
|
||||
|
||||
## 1.12.10
|
||||
|
||||
* Pass feature IDs through in tile-join
|
||||
|
||||
## 1.12.9
|
||||
|
||||
* Clean up parsing and serialization. Provide some context with parsing errors.
|
||||
|
||||
## 1.12.8
|
||||
|
||||
* Fix the spelling of the --preserve-input-order option
|
||||
|
||||
## 1.12.7
|
||||
|
||||
* Support the "id" field of GeoJSON objects and vector tile features
|
||||
|
||||
## 1.12.6
|
||||
|
||||
* Fix error reports when reading from an empty file with parallel input
|
||||
|
||||
## 1.12.5
|
||||
|
||||
* Add an option to vary the level of line and polygon simplification
|
||||
* Be careful not to produce an empty tile if there was a feature with
|
||||
empty geometry.
|
||||
|
||||
## 1.12.4
|
||||
|
||||
* Be even more careful not to produce features with empty geometry
|
||||
|
||||
## 1.12.3
|
||||
|
||||
* Fix double-counted progress in the progress indicator
|
||||
|
||||
## 1.12.2
|
||||
|
||||
* Add ability to specify a projection to tippecanoe-decode
|
||||
|
||||
## 1.12.1
|
||||
|
||||
* Fix incorrect tile layer version numbers in tile-join output
|
||||
|
||||
## 1.12.0
|
||||
|
||||
* Fix a tile-join bug that would retain fields that were supposed to be excluded
|
||||
|
||||
## 1.11.9
|
||||
|
||||
* Add minimal support for alternate input projections (EPSG:3857).
|
||||
|
||||
## 1.11.8
|
||||
|
||||
* Add an option to calculate the density of features as a feature attribute
|
||||
|
||||
## 1.11.7
|
||||
|
||||
* Keep metadata together with geometry for features that don't span many tiles,
|
||||
to avoid extra memory load from indexing into a separate metadata file
|
||||
|
||||
## 1.11.6
|
||||
|
||||
* Reduce the size of critical data structures to reduce dynamic memory use
|
||||
|
||||
## 1.11.5
|
||||
|
||||
* Let zoom level 0 have just as much extent and buffer as any other zoom
|
||||
* Fix tippecanoe-decode bug that would sometimes show outer rings as inner
|
||||
|
||||
## 1.11.4
|
||||
|
||||
* Don't let polygons with nonzero area disappear during cleaning
|
||||
|
||||
## 1.11.3
|
||||
|
||||
* Internal code cleanup
|
||||
|
||||
## 1.11.2
|
||||
|
||||
* Update Clipper to fix potential crash
|
||||
|
||||
## 1.11.1
|
||||
|
||||
* Make better use of C++ standard libraries
|
||||
|
||||
## 1.11.0
|
||||
|
||||
* Convert C source files to C++
|
||||
|
||||
## 1.10.0
|
||||
|
||||
* Upgrade Clipper to fix potential crashes and improve polygon topology
|
||||
|
||||
## 1.9.16
|
||||
|
||||
* Switch to protozero as the library for reading and writing protocol buffers
|
||||
|
||||
## 1.9.15
|
||||
|
||||
* Add option not to clip features
|
||||
|
||||
## 1.9.14
|
||||
|
||||
* Clean up polygons after coalescing, if necessary
|
||||
|
||||
## 1.9.13
|
||||
|
||||
* Don't trust the OS so much about how many files can be open
|
||||
|
||||
## 1.9.12
|
||||
|
||||
* Limit the size of the parallel parsing streaming input buffer
|
||||
* Add an option to set the tileset's attribution
|
||||
|
||||
## 1.9.11
|
||||
|
||||
* Fix a line simplification crash when a segment degenerates to a single point
|
||||
|
||||
## 1.9.10
|
||||
|
||||
* Warn if temporary disk space starts to run low
|
||||
|
||||
## 1.9.9
|
||||
|
||||
* Add --drop-polygons to drop a fraction of polygons by zoom level
|
||||
* Only complain once about failing to clean polygons
|
||||
|
||||
## 1.9.8
|
||||
|
||||
* Use an on-disk radix sort for the index to control virtual memory thrashing
|
||||
when the geometry and index are too large to fit in memory
|
||||
|
||||
## 1.9.7
|
||||
|
||||
* Fix build problem (wrong spelling of long long max/min constants)
|
||||
|
||||
## 1.9.6
|
||||
|
||||
* Add an option to give specific layer names to specific input files
|
||||
|
||||
## 1.9.5
|
||||
|
||||
* Remove temporary files that were accidentally left behind
|
||||
* Be more careful about checking memory allocations and array bounds
|
||||
* Add GNU-style long options
|
||||
|
||||
## 1.9.4
|
||||
|
||||
* Tippecanoe-decode can decode .pbf files that aren't in an .mbtiles container
|
||||
|
||||
## 1.9.3
|
||||
|
||||
* Don't get stuck in a loop trying to split up very small, very complicated polygons
|
||||
|
||||
## 1.9.2
|
||||
|
||||
* Increase maximum tile size for tippecanoe-decode
|
||||
|
112
Makefile
112
Makefile
@ -1,14 +1,25 @@
|
||||
PREFIX ?= /usr/local
|
||||
MANDIR ?= $(PREFIX)/share/man/man1/
|
||||
BUILDTYPE ?= Release
|
||||
SHELL = /bin/bash
|
||||
|
||||
# inherit from env if set
|
||||
CC := $(CC)
|
||||
CXX := $(CXX)
|
||||
CFLAGS := $(CFLAGS)
|
||||
CXXFLAGS := $(CXXFLAGS)
|
||||
CXXFLAGS := $(CXXFLAGS) -std=c++14
|
||||
LDFLAGS := $(LDFLAGS)
|
||||
WARNING_FLAGS := -Wall -Wshadow -Wsign-compare
|
||||
RELEASE_FLAGS := -O3 -DNDEBUG
|
||||
DEBUG_FLAGS := -O0 -DDEBUG -fno-inline-functions -fno-omit-frame-pointer
|
||||
|
||||
all: tippecanoe tippecanoe-enumerate tippecanoe-decode tile-join
|
||||
ifeq ($(BUILDTYPE),Release)
|
||||
FINAL_FLAGS := -g $(WARNING_FLAGS) $(RELEASE_FLAGS)
|
||||
else
|
||||
FINAL_FLAGS := -g $(WARNING_FLAGS) $(DEBUG_FLAGS)
|
||||
endif
|
||||
|
||||
all: tippecanoe tippecanoe-enumerate tippecanoe-decode tile-join unit
|
||||
|
||||
docs: man/tippecanoe.1
|
||||
|
||||
@ -21,84 +32,125 @@ install: tippecanoe tippecanoe-enumerate tippecanoe-decode tile-join
|
||||
cp tile-join $(PREFIX)/bin/tile-join
|
||||
cp man/tippecanoe.1 $(MANDIR)/tippecanoe.1
|
||||
|
||||
uninstall:
|
||||
rm $(PREFIX)/bin/tippecanoe $(PREFIX)/bin/tippecanoe-enumerate $(PREFIX)/bin/tippecanoe-decode $(PREFIX)/bin/tile-join $(MANDIR)/tippecanoe.1
|
||||
|
||||
man/tippecanoe.1: README.md
|
||||
md2man-roff README.md > man/tippecanoe.1
|
||||
|
||||
vector_tile.pb.cc vector_tile.pb.h: vector_tile.proto
|
||||
protoc --cpp_out=. vector_tile.proto
|
||||
|
||||
PG=
|
||||
|
||||
H = $(shell find . '(' -name '*.h' -o -name '*.hh' ')')
|
||||
C = $(shell find . '(' -name '*.c' -o -name '*.cc' ')')
|
||||
H = $(wildcard *.h) $(wildcard *.hpp)
|
||||
C = $(wildcard *.c) $(wildcard *.cpp)
|
||||
|
||||
INCLUDES = -I/usr/local/include
|
||||
INCLUDES = -I/usr/local/include -I.
|
||||
LIBS = -L/usr/local/lib
|
||||
|
||||
tippecanoe: geojson.o jsonpull.o vector_tile.pb.o tile.o clip.o pool.o mbtiles.o geometry.o projection.o memfile.o clipper/clipper.o
|
||||
$(CXX) $(PG) $(LIBS) -O3 -g -Wall $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lprotobuf-lite -lsqlite3 -lpthread
|
||||
tippecanoe: geojson.o jsonpull/jsonpull.o tile.o pool.o mbtiles.o geometry.o projection.o memfile.o mvt.o serial.o main.o text.o
|
||||
$(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -lpthread
|
||||
|
||||
tippecanoe-enumerate: enumerate.o
|
||||
$(CC) $(PG) $(LIBS) -O3 -g -Wall $(CFLAGS) -o $@ $^ $(LDFLAGS) -lsqlite3
|
||||
$(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CFLAGS) -o $@ $^ $(LDFLAGS) -lsqlite3
|
||||
|
||||
tippecanoe-decode: decode.o vector_tile.pb.o projection.o
|
||||
$(CXX) $(PG) $(LIBS) -O3 -g -Wall $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lprotobuf-lite -lsqlite3
|
||||
tippecanoe-decode: decode.o projection.o mvt.o
|
||||
$(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3
|
||||
|
||||
tile-join: tile-join.o vector_tile.pb.o projection.o pool.o mbtiles.o
|
||||
$(CXX) $(PG) $(LIBS) -O3 -g -Wall $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lprotobuf-lite -lsqlite3
|
||||
tile-join: tile-join.o projection.o pool.o mbtiles.o mvt.o memfile.o
|
||||
$(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -lpthread
|
||||
|
||||
libjsonpull.a: jsonpull.o
|
||||
ar rc $@ $^
|
||||
ranlib $@
|
||||
unit: unit.o text.o
|
||||
$(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -lpthread
|
||||
|
||||
%.o: %.c $(H)
|
||||
$(CC) $(PG) $(INCLUDES) -O3 -g -Wall $(CFLAGS) -c $<
|
||||
-include $(wildcard *.d)
|
||||
|
||||
%.o: %.cc $(H)
|
||||
$(CXX) $(PG) $(INCLUDES) -O3 -g -Wall $(CXXFLAGS) -c $<
|
||||
%.o: %.c
|
||||
$(CC) -MMD $(PG) $(INCLUDES) $(FINAL_FLAGS) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
%.o: %.cpp
|
||||
$(CXX) -MMD $(PG) $(INCLUDES) $(FINAL_FLAGS) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
rm -f tippecanoe *.o
|
||||
rm -f ./tippecanoe ./tippecanoe-* ./tile-join ./unit *.o *.d */*.o */*.d
|
||||
|
||||
indent:
|
||||
clang-format -i -style="{BasedOnStyle: Google, IndentWidth: 8, UseTab: Always, AllowShortIfStatementsOnASingleLine: false, ColumnLimit: 0, ContinuationIndentWidth: 8, SpaceAfterCStyleCast: true, IndentCaseLabels: false, AllowShortBlocksOnASingleLine: false, AllowShortFunctionsOnASingleLine: false}" $(C) $(H)
|
||||
clang-format -i -style="{BasedOnStyle: Google, IndentWidth: 8, UseTab: Always, AllowShortIfStatementsOnASingleLine: false, ColumnLimit: 0, ContinuationIndentWidth: 8, SpaceAfterCStyleCast: true, IndentCaseLabels: false, AllowShortBlocksOnASingleLine: false, AllowShortFunctionsOnASingleLine: false, SortIncludes: false}" $(C) $(H)
|
||||
|
||||
TESTS = $(wildcard tests/*/out/*.json)
|
||||
SPACE = $(NULL) $(NULL)
|
||||
|
||||
test: tippecanoe tippecanoe-decode $(addsuffix .check,$(TESTS)) parallel-test
|
||||
test: tippecanoe tippecanoe-decode $(addsuffix .check,$(TESTS)) parallel-test pbf-test join-test enumerate-test unit
|
||||
./unit
|
||||
|
||||
# Work around Makefile and filename punctuation limits: _ for space, @ for :, % for /
|
||||
%.json.check:
|
||||
./tippecanoe -f -o $@.mbtiles $(subst _, ,$(patsubst %.json.check,%,$(word 4,$(subst /, ,$@)))) $(wildcard $(subst $(SPACE),/,$(wordlist 1,2,$(subst /, ,$@)))/*.json) < /dev/null
|
||||
./tippecanoe -aD -f -o $@.mbtiles $(subst @,:,$(subst %,/,$(subst _, ,$(patsubst %.json.check,%,$(word 4,$(subst /, ,$@)))))) $(wildcard $(subst $(SPACE),/,$(wordlist 1,2,$(subst /, ,$@)))/*.json) < /dev/null
|
||||
./tippecanoe-decode $@.mbtiles > $@.out
|
||||
cmp $(patsubst %.check,%,$@) $@.out
|
||||
rm $@.out $@.mbtiles
|
||||
|
||||
parallel-test:
|
||||
mkdir -p tests/parallel
|
||||
perl -e 'for ($$i = 0; $$i < 20; $$i++) { $$lon = rand(360) - 180; $$lat = rand(180) - 90; print "{ \"type\": \"Feature\", \"properties\": { }, \"geometry\": { \"type\": \"Point\", \"coordinates\": [ $$lon, $$lat ] } }\n"; }' > tests/parallel/in1.json
|
||||
perl -e 'for ($$i = 0; $$i < 20; $$i++) { $$lon = rand(360) - 180; $$lat = rand(180) - 90; print "{ \"type\": \"Feature\", \"properties\": { \"yes\": \"no\", \"who\": 1 }, \"geometry\": { \"type\": \"Point\", \"coordinates\": [ $$lon, $$lat ] } }\n"; }' > tests/parallel/in1.json
|
||||
perl -e 'for ($$i = 0; $$i < 300000; $$i++) { $$lon = rand(360) - 180; $$lat = rand(180) - 90; print "{ \"type\": \"Feature\", \"properties\": { }, \"geometry\": { \"type\": \"Point\", \"coordinates\": [ $$lon, $$lat ] } }\n"; }' > tests/parallel/in2.json
|
||||
perl -e 'for ($$i = 0; $$i < 20; $$i++) { $$lon = rand(360) - 180; $$lat = rand(180) - 90; print "{ \"type\": \"Feature\", \"properties\": { }, \"geometry\": { \"type\": \"Point\", \"coordinates\": [ $$lon, $$lat ] } }\n"; }' > tests/parallel/in3.json
|
||||
./tippecanoe -z5 -f -pi -l test -n test -o tests/parallel/linear-file.mbtiles tests/parallel/in[123].json
|
||||
./tippecanoe -z5 -f -pi -l test -n test -P -o tests/parallel/parallel-file.mbtiles tests/parallel/in[123].json
|
||||
echo -n "" > tests/parallel/empty1.json
|
||||
echo "" > tests/parallel/empty2.json
|
||||
./tippecanoe -z5 -f -pi -l test -n test -o tests/parallel/linear-file.mbtiles tests/parallel/in[123].json tests/parallel/empty[12].json
|
||||
./tippecanoe -z5 -f -pi -l test -n test -P -o tests/parallel/parallel-file.mbtiles tests/parallel/in[123].json tests/parallel/empty[12].json
|
||||
cat tests/parallel/in[123].json | ./tippecanoe -z5 -f -pi -l test -n test -o tests/parallel/linear-pipe.mbtiles
|
||||
cat tests/parallel/in[123].json | ./tippecanoe -z5 -f -pi -l test -n test -P -o tests/parallel/parallel-pipe.mbtiles
|
||||
./tippecanoe -z5 -f -pi -l test -n test -P -o tests/parallel/parallel-pipes.mbtiles <(cat tests/parallel/in1.json) <(cat tests/parallel/empty1.json) <(cat tests/parallel/empty2.json) <(cat tests/parallel/in2.json) /dev/null <(cat tests/parallel/in3.json)
|
||||
./tippecanoe-decode tests/parallel/linear-file.mbtiles > tests/parallel/linear-file.json
|
||||
./tippecanoe-decode tests/parallel/parallel-file.mbtiles > tests/parallel/parallel-file.json
|
||||
./tippecanoe-decode tests/parallel/linear-pipe.mbtiles > tests/parallel/linear-pipe.json
|
||||
./tippecanoe-decode tests/parallel/parallel-pipe.mbtiles > tests/parallel/parallel-pipe.json
|
||||
./tippecanoe-decode tests/parallel/parallel-pipes.mbtiles > tests/parallel/parallel-pipes.json
|
||||
cmp tests/parallel/linear-file.json tests/parallel/parallel-file.json
|
||||
cmp tests/parallel/linear-file.json tests/parallel/linear-pipe.json
|
||||
cmp tests/parallel/linear-file.json tests/parallel/parallel-pipe.json
|
||||
cmp tests/parallel/linear-file.json tests/parallel/parallel-pipes.json
|
||||
rm tests/parallel/*.mbtiles tests/parallel/*.json
|
||||
|
||||
pbf-test:
|
||||
./tippecanoe-decode tests/pbf/11-328-791.vector.pbf 11 328 791 > tests/pbf/11-328-791.vector.pbf.out
|
||||
cmp tests/pbf/11-328-791.json tests/pbf/11-328-791.vector.pbf.out
|
||||
rm tests/pbf/11-328-791.vector.pbf.out
|
||||
./tippecanoe-decode -t EPSG:3857 tests/pbf/11-328-791.vector.pbf 11 328 791 > tests/pbf/11-328-791.3857.vector.pbf.out
|
||||
cmp tests/pbf/11-328-791.3857.json tests/pbf/11-328-791.3857.vector.pbf.out
|
||||
rm tests/pbf/11-328-791.3857.vector.pbf.out
|
||||
|
||||
enumerate-test:
|
||||
./tippecanoe -z5 -f -o tests/ne_110m_admin_0_countries/out/enum.mbtiles tests/ne_110m_admin_0_countries/in.json
|
||||
./tippecanoe-enumerate tests/ne_110m_admin_0_countries/out/enum.mbtiles > tests/ne_110m_admin_0_countries/out/enum.check
|
||||
cmp tests/ne_110m_admin_0_countries/out/enum tests/ne_110m_admin_0_countries/out/enum.check
|
||||
rm tests/ne_110m_admin_0_countries/out/enum.mbtiles tests/ne_110m_admin_0_countries/out/enum.check
|
||||
|
||||
join-test:
|
||||
./tippecanoe -f -z12 -o tests/join-population/tabblock_06001420.mbtiles tests/join-population/tabblock_06001420.json
|
||||
./tippecanoe -f -z12 -o tests/join-population/tabblock_06001420.mbtiles tests/join-population/tabblock_06001420.json
|
||||
./tippecanoe -f -Z5 -z10 -o tests/join-population/macarthur.mbtiles -l macarthur tests/join-population/macarthur.json
|
||||
./tippecanoe -f -d10 -D10 -Z9 -z11 -o tests/join-population/macarthur2.mbtiles -l macarthur tests/join-population/macarthur2.json
|
||||
./tile-join -f -o tests/join-population/joined.mbtiles -x GEOID10 -c tests/join-population/population.csv tests/join-population/tabblock_06001420.mbtiles
|
||||
./tile-join -f -i -o tests/join-population/joined-i.mbtiles -x GEOID10 -c tests/join-population/population.csv tests/join-population/tabblock_06001420.mbtiles
|
||||
./tile-join -f -o tests/join-population/merged.mbtiles tests/join-population/tabblock_06001420.mbtiles tests/join-population/macarthur.mbtiles tests/join-population/macarthur2.mbtiles
|
||||
./tile-join -f -c tests/join-population/windows.csv -o tests/join-population/windows.mbtiles tests/join-population/macarthur.mbtiles
|
||||
./tippecanoe-decode tests/join-population/joined.mbtiles > tests/join-population/joined.mbtiles.json.check
|
||||
./tippecanoe-decode tests/join-population/joined-i.mbtiles > tests/join-population/joined-i.mbtiles.json.check
|
||||
./tippecanoe-decode tests/join-population/merged.mbtiles > tests/join-population/merged.mbtiles.json.check
|
||||
./tippecanoe-decode tests/join-population/windows.mbtiles > tests/join-population/windows.mbtiles.json.check
|
||||
cmp tests/join-population/joined.mbtiles.json.check tests/join-population/joined.mbtiles.json
|
||||
cmp tests/join-population/joined-i.mbtiles.json.check tests/join-population/joined-i.mbtiles.json
|
||||
cmp tests/join-population/merged.mbtiles.json.check tests/join-population/merged.mbtiles.json
|
||||
cmp tests/join-population/windows.mbtiles.json.check tests/join-population/windows.mbtiles.json
|
||||
rm tests/join-population/tabblock_06001420.mbtiles tests/join-population/joined.mbtiles tests/join-population/joined-i.mbtiles tests/join-population/joined.mbtiles.json.check tests/join-population/joined-i.mbtiles.json.check tests/join-population/macarthur.mbtiles tests/join-population/merged.mbtiles tests/join-population/merged.mbtiles.json.check tests/join-population/macarthur2.mbtiles tests/join-population/windows.mbtiles tests/join-population/windows.mbtiles.json.check
|
||||
|
||||
# Use this target to regenerate the standards that the tests are compared against
|
||||
# after making a change that legitimately changes their output
|
||||
|
||||
prep-test: $(TESTS)
|
||||
|
||||
tests/%.json: Makefile tippecanoe tippecanoe-decode
|
||||
./tippecanoe -f -o $@.check.mbtiles $(subst _, ,$(patsubst %.json,%,$(word 4,$(subst /, ,$@)))) $(wildcard $(subst $(SPACE),/,$(wordlist 1,2,$(subst /, ,$@)))/*.json)
|
||||
./tippecanoe -f -o $@.check.mbtiles $(subst @,:,$(subst %,/,$(subst _, ,$(patsubst %.json,%,$(word 4,$(subst /, ,$@)))))) $(wildcard $(subst $(SPACE),/,$(wordlist 1,2,$(subst /, ,$@)))/*.json)
|
||||
./tippecanoe-decode $@.check.mbtiles > $@
|
||||
cmp $(patsubst %.check,%,$@) $@
|
||||
rm $@.check.mbtiles
|
||||
|
164
README.md
164
README.md
@ -1,8 +1,8 @@
|
||||
tippecanoe
|
||||
==========
|
||||
|
||||
Builds [vector tilesets](https://www.mapbox.com/developers/vector-tiles/) from large collections of [GeoJSON](http://geojson.org/)
|
||||
features. This is a tool for [making maps from huge datasets](MADE_WITH.md).
|
||||
Builds [vector tilesets](https://www.mapbox.com/developers/vector-tiles/) from large (or small) collections of [GeoJSON](http://geojson.org/) features,
|
||||
[like these](MADE_WITH.md).
|
||||
|
||||
[](https://travis-ci.org/mapbox/tippecanoe)
|
||||
[](https://coveralls.io/github/mapbox/tippecanoe?branch=master)
|
||||
@ -57,65 +57,95 @@ Options
|
||||
|
||||
### Naming
|
||||
|
||||
* -l _name_: Layer name (default "file" if source is file.json or output is file.mbtiles). If there are multiple input files
|
||||
specified, the files are all merged into the single named layer.
|
||||
* -n _name_: Human-readable name (default file.json)
|
||||
* -l _name_ or --layer=_name_: Layer name (default "file" if source is file.json or output is file.mbtiles). If there are multiple input files
|
||||
specified, the files are all merged into the single named layer, even if they try to specify individual names with -L.
|
||||
* -L _name_:_file.json_ or --named-layer=_name_:_file.json_: Specify layer names for individual files. If your shell supports it, you can use a subshell redirect like -L _name_:<(cat dir/*.json) to specify a layer name for the output of streamed input.
|
||||
* -n _name_ or --name=_name_: Human-readable name for the tileset (default file.json)
|
||||
* -A _text_ or --attribution=_text_: Attribution (HTML) to be shown with maps that use data from this tileset.
|
||||
|
||||
### File control
|
||||
|
||||
* -o _file_.mbtiles: Name the output file.
|
||||
* -f: Delete the mbtiles file if it already exists instead of giving an error
|
||||
* -F: Proceed (without deleting existing data) if the metadata or tiles table already exists
|
||||
* -o _file_.mbtiles or --output=_file_.mbtiles: Name the output file.
|
||||
* -f or --force: Delete the mbtiles file if it already exists instead of giving an error
|
||||
* -F or --allow-existing: Proceed (without deleting existing data) if the metadata or tiles table already exists
|
||||
or if metadata fields can't be set
|
||||
* -t _directory_: Put the temporary files in _directory_.
|
||||
* -P: Use multiple threads to read different parts of each input file at once.
|
||||
* -t _directory_ or --temporary-directory=_directory_: Put the temporary files in _directory_.
|
||||
If you don't specify, it will use `/tmp`.
|
||||
* -P or --read-parallel: Use multiple threads to read different parts of each input file at once.
|
||||
This will only work if the input is line-delimited JSON with each Feature on its
|
||||
own line, because it knows nothing of the top-level structure around the Features.
|
||||
own line, because it knows nothing of the top-level structure around the Features. Spurious "EOF" error
|
||||
messages may result otherwise.
|
||||
Performance will be better if the input is a named file that can be mapped into memory
|
||||
rather than a stream that can only be read sequentially.
|
||||
|
||||
### Zoom levels and resolution
|
||||
|
||||
* -z _zoom_: Maxzoom: the highest zoom level for which tiles are generated (default 14)
|
||||
* -Z _zoom_: Minzoom: the lowest zoom level for which tiles are generated (default 0)
|
||||
* -B _zoom_: Base zoom, the level at and above which all points are included in the tiles (default maxzoom).
|
||||
* -z _zoom_ or --maximum-zoom=_zoom_: Maxzoom: the highest zoom level for which tiles are generated (default 14)
|
||||
* -Z _zoom_ or --minimum-zoom=_zoom_: Minzoom: the lowest zoom level for which tiles are generated (default 0)
|
||||
* -B _zoom_ or --base-zoom=_zoom_: Base zoom, the level at and above which all points are included in the tiles (default maxzoom).
|
||||
If you use -Bg, it will guess a zoom level that will keep at most 50,000 features in the densest tile.
|
||||
You can also specify a marker-width with -Bg*width* to allow fewer features in the densest tile to
|
||||
compensate for the larger marker.
|
||||
* -d _detail_: Detail at max zoom level (default 12, for tile resolution of 4096)
|
||||
* -D _detail_: Detail at lower zoom levels (default 12, for tile resolution of 4096)
|
||||
* -m _detail_: Minimum detail that it will try if tiles are too big at regular detail (default 7)
|
||||
* -b _pixels_: Buffer size where features are duplicated from adjacent tiles. Units are "screen pixels"--1/256th of the tile width or height. (default 5)
|
||||
compensate for the larger marker, or -Bf*number* to allow at most *number* features in the densest tile.
|
||||
* -d _detail_ or --full-detail=_detail_: Detail at max zoom level (default 12, for tile resolution of 4096)
|
||||
* -D _detail_ or --low-detail=_detail_: Detail at lower zoom levels (default 12, for tile resolution of 4096)
|
||||
* -m _detail_ or --minimum-detail=_detail_: Minimum detail that it will try if tiles are too big at regular detail (default 7)
|
||||
* -b _pixels_ or --buffer=_pixels_: Buffer size where features are duplicated from adjacent tiles. Units are "screen pixels"--1/256th of the tile width or height. (default 5)
|
||||
* -s _projection_ or --projection=_projection_: Specify the projection of the input data. Currently supported are EPSG:4326 (WGS84, the default) and EPSG:3857 (Web Mercator).
|
||||
* -M _bytes_ or --maximum-tile-bytes=_bytes_: Use the specified number of _bytes_ as the maximum compressed tile size instead of 500K.
|
||||
|
||||
All internal math is done in terms of a 32-bit tile coordinate system, so 1/(2^32) of the size of Earth,
|
||||
or about 1cm, is the smallest distinguishable distance. If _maxzoom_ + _detail_ > 32, no additional
|
||||
resolution is obtained than by using a smaller _maxzoom_ or _detail_.
|
||||
|
||||
### Properties
|
||||
|
||||
* -x _name_: Exclude the named properties from all features
|
||||
* -y _name_: Include the named properties in all features, excluding all those not explicitly named
|
||||
* -X: Exclude all properties and encode only geometries
|
||||
* -x _name_ or --exclude=_name_: Exclude the named properties from all features
|
||||
* -y _name_ or --include=_name_: Include the named properties in all features, excluding all those not explicitly named
|
||||
* -X or --exclude-all: Exclude all properties and encode only geometries
|
||||
|
||||
### Point simplification
|
||||
|
||||
* -r _rate_: Rate at which dots are dropped at zoom levels below basezoom (default 2.5).
|
||||
* -r _rate_ or --drop-rate=_rate_: Rate at which dots are dropped at zoom levels below basezoom (default 2.5).
|
||||
If you use -rg, it will guess a drop rate that will keep at most 50,000 features in the densest tile.
|
||||
* -g _gamma_: Rate at which especially dense dots are dropped (default 0, for no effect). A gamma of 2 reduces the number of dots less than a pixel apart to the square root of their original number.
|
||||
You can also specify a marker-width with -rg*width* to allow fewer features in the densest tile to
|
||||
compensate for the larger marker, or -rf*number* to allow at most *number* features in the densest tile.
|
||||
* -g _gamma_ or --gamma=_gamma_: Rate at which especially dense dots are dropped (default 0, for no effect). A gamma of 2 reduces the number of dots less than a pixel apart to the square root of their original number.
|
||||
|
||||
### Line and polygon simplification
|
||||
|
||||
* -S _scale_ or --simplification=_scale_: Multiply the tolerance for line and polygon simplification by _scale_. The standard tolerance tries to keep
|
||||
the line or polygon within one tile unit of its proper location. You can probably go up to about 10 without too much visible difference.
|
||||
|
||||
### Doing more
|
||||
|
||||
* -ac: Coalesce adjacent line and polygon features that have the same properties
|
||||
* -ar: Try reversing the directions of lines to make them coalesce and compress better
|
||||
* -ao: Reorder features to put ones with the same properties in sequence, to try to get them to coalesce
|
||||
* -al: Let "dot" dropping at lower zooms apply to lines too
|
||||
* -ac or --coalesce: Coalesce adjacent line and polygon features that have the same properties.
|
||||
Note that when overlapping polygons are coalesced, the overlapping region is treated as a hole,
|
||||
which may not be what you want.
|
||||
* -ar or --reverse: Try reversing the directions of lines to make them coalesce and compress better
|
||||
* -ao or --reorder: Reorder features to put ones with the same properties in sequence, to try to get them to coalesce
|
||||
* -al or --drop-lines: Let "dot" dropping at lower zooms apply to lines too
|
||||
* -ap or --drop-polygons: Let "dot" dropping at lower zooms apply to polygons too
|
||||
* -ag or --calculate-feature-density: Add a new attribute, `tippecanoe_feature_density`, to each feature, to record how densely features are spaced in that area of the tile. You can use this attribute in the style to produce a glowing effect where points are densely packed. It can range from 0 in the sparsest areas to 255 in the densest.
|
||||
* -ab or --detect-shared-borders: In the manner of [TopoJSON](https://github.com/mbostock/topojson/wiki/Introduction), detect borders that are shared between multiple polygons and simplify them identically in each polygon. This takes more time and memory than considering each polygon individually.
|
||||
* -aG or --increase-gamma-as-needed: If a tile is too large, try to reduce it to under 500K by increasing the `-g` gamma. The discovered gamma applies to the entire zoom level.
|
||||
* -as or --drop-densest-as-needed: If a tile is too large, try to reduce it to under 500K by increasing the minimum spacing between features. The discovered spacing applies to the entire zoom level.
|
||||
* -ad or --drop-fraction-as-needed: Dynamically drop some fraction of features from each zoom level to keep large tiles under the 500K size limit. (This is like `-pd` but applies to the entire zoom level, not to each tile.)
|
||||
* -an or --drop-smallest-as-needed: Dynamically drop the smallest features (physically smallest: the shortest lines or the smallest polygons) from each zoom level to keep large tiles under the 500K size limit. This option will not work for point features.
|
||||
* -aL or --grid-low-zooms: At all zoom levels below _maxzoom_, snap all lines and polygons to a stairstep grid instead of allowing diagonals. You will also want to specify a tile resolution, probably `-D8`. This option provides a way to display continuous parcel, gridded, or binned data at low zooms without overwhelming the tiles with tiny polygons, since features will either get stretched out to the grid unit or lost entirely, depending on how they happened to be aligned in the original data.
|
||||
|
||||
### Doing less
|
||||
|
||||
* -ps: Don't simplify lines
|
||||
* -pS: Don't simplify lines at maxzoom (but do simplify at lower zooms)
|
||||
* -pf: Don't limit tiles to 200,000 features
|
||||
* -pk: Don't limit tiles to 500K bytes
|
||||
* -pd: Dynamically drop some fraction of features from large tiles to keep them under the 500K size limit. It will probably look ugly at the tile boundaries.
|
||||
* -pi: Preserve the original input order of features as the drawing order instead of ordering geographically. (This is implemented as a restoration of the original order at the end, so that dot-dropping is still geographic, which means it also undoes -ao).
|
||||
* -pp: Don't split complex polygons (over 700 vertices after simplification) into multiple features.
|
||||
* -q: Work quietly instead of reporting progress
|
||||
* -ps or --no-line-simplification: Don't simplify lines
|
||||
* -pS or --simplify-only-low-zooms: Don't simplify lines at maxzoom (but do simplify at lower zooms)
|
||||
* -pf or --no-feature-limit: Don't limit tiles to 200,000 features
|
||||
* -pk or --no-tile-size-limit: Don't limit tiles to 500K bytes
|
||||
* -pd or --force-feature-limit: Dynamically drop some fraction of features from large tiles to keep them under the 500K size limit. It will probably look ugly at the tile boundaries. (This is like `-ad` but applies to each tile individually, not to the entire zoom level.)
|
||||
* -pi or --preserve-input-order: Preserve the original input order of features as the drawing order instead of ordering geographically. (This is implemented as a restoration of the original order at the end, so that dot-dropping is still geographic, which means it also undoes -ao).
|
||||
* -pp or --no-polygon-splitting: Don't split complex polygons (over 700 vertices after simplification) into multiple features.
|
||||
* -pc or --no-clipping: Don't clip features to the size of the tile. If a feature overlaps the tile's bounds or buffer at all, it is included completely. Be careful: this can produce very large tilesets, especially with large polygons.
|
||||
* -pD or --no-duplication: As with --no-clipping, each feature is included intact instead of cut to tile boundaries. In addition, it is included only in a single tile per zoom level rather than potentially in multiple copies. Clients of the tileset must check adjacent tiles (possibly some distance away) to ensure they have all features.
|
||||
* -pt or --no-tiny-polygon-reduction: Don't combine the area of very small polygons into small squares that represent their combined area.
|
||||
* -q or --quiet: Work quietly instead of reporting progress
|
||||
|
||||
Example
|
||||
-------
|
||||
@ -132,7 +162,7 @@ GeoJSON extension
|
||||
-----------------
|
||||
|
||||
Tippecanoe defines a GeoJSON extension that you can use to specify the minimum and/or maximum zoom level
|
||||
at which an individual feature will be included in the vector tile dataset being produced.
|
||||
at which an individual feature will be included in the vector tileset being produced.
|
||||
If you have a feature like this:
|
||||
|
||||
```
|
||||
@ -149,7 +179,24 @@ If you have a feature like this:
|
||||
|
||||
with a `tippecanoe` object specifiying a `maxzoom` of 9 and a `minzoom` of 4, the feature
|
||||
will only appear in the vector tiles for zoom levels 4 through 9. Note that the `tippecanoe`
|
||||
object belongs to the Feature, not to its `properties`.
|
||||
object belongs to the Feature, not to its `properties`. If you specify a `minzoom` for a feature,
|
||||
it will be preserved down to that zoom level even if dot-dropping with `-r` would otherwise have
|
||||
dropped it.
|
||||
|
||||
You can also specify a layer name in the `tippecanoe` object, which will take precedence over
|
||||
the filename or name specified using `--layer`, like this:
|
||||
|
||||
```
|
||||
{
|
||||
"type" : "Feature",
|
||||
"tippecanoe" : { "layer" : "streets" },
|
||||
"properties" : { "FULLNAME" : "N Vasco Rd" },
|
||||
"geometry" : {
|
||||
"type" : "LineString",
|
||||
"coordinates" : [ [ -121.733350, 37.767671 ], [ -121.733600, 37.767483 ], [ -121.733131, 37.766952 ] ]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Point styling
|
||||
-------------
|
||||
@ -198,7 +245,8 @@ For line features, it drops any features that are too small to draw at all.
|
||||
This still leaves the lower zooms too dark (and too dense for the 500K tile limit,
|
||||
in some places), so I need to figure out an equitable way to throw features away.
|
||||
|
||||
Any polygons that are smaller than a minimum area (currently 4 square subpixels) will
|
||||
Unless you specify `--no-tiny-polygon-reduction`,
|
||||
any polygons that are smaller than a minimum area (currently 4 square subpixels) will
|
||||
have their probability diffused, so that some of them will be drawn as a square of
|
||||
this minimum size and others will not be drawn at all, preserving the total area that
|
||||
all of them should have had together.
|
||||
@ -207,7 +255,7 @@ Any polygons that have over 700 vertices after line simplification will be split
|
||||
multiple features so they can be rendered efficiently, unless you use -pp to prevent this.
|
||||
|
||||
Features in the same tile that share the same type and attributes are coalesced
|
||||
together into a single geometry. You are strongly encouraged to use -x to exclude
|
||||
together into a single geometry if you use `--coalesce`. You are strongly encouraged to use -x to exclude
|
||||
any unnecessary properties to reduce wasted file size.
|
||||
|
||||
If a tile is larger than 500K, it will try encoding that tile at progressively
|
||||
@ -216,18 +264,12 @@ lower resolutions before failing if it still doesn't fit.
|
||||
Development
|
||||
-----------
|
||||
|
||||
Requires protoc and sqlite3. Rebuilding the manpage
|
||||
Requires sqlite3 and zlib (should already be installed on MacOS). Rebuilding the manpage
|
||||
uses md2man (`gem install md2man`).
|
||||
|
||||
MacOS:
|
||||
|
||||
brew install protobuf
|
||||
|
||||
Linux:
|
||||
|
||||
sudo apt-get install libprotobuf-dev
|
||||
sudo apt-get install protobuf-compiler
|
||||
sudo apt-get install libsqlite3-dev
|
||||
sudo apt-get install build-essential libsqlite3-dev zlib1g-dev
|
||||
|
||||
Then build:
|
||||
|
||||
@ -237,6 +279,17 @@ and perhaps
|
||||
|
||||
make install
|
||||
|
||||
Tippecanoe now requires features from the 2014 C++ standard. If your compiler is older than
|
||||
that, you will need to install a newer one. On MacOS, updating to the lastest XCode should
|
||||
get you a new enough version of `clang++`. On Linux, you should be able to upgrade `g++` with
|
||||
|
||||
```
|
||||
sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
|
||||
sudo apt-get update -y
|
||||
sudo apt-get install -y g++-5
|
||||
export CXX=g++-5
|
||||
```
|
||||
|
||||
Examples
|
||||
------
|
||||
|
||||
@ -254,6 +307,10 @@ Tile-join is a tool for joining new attributes from a CSV file to features that
|
||||
have already been tiled with tippecanoe. It reads the tiles from an existing .mbtiles
|
||||
file, matches them against the records of the CSV, and writes out a new tileset.
|
||||
|
||||
If you specify multiple source mbtiles files, they are all read and their combined
|
||||
contents are written to the new mbtiles output. If they define the same layers or
|
||||
the same tiles, the layers or tiles are merged.
|
||||
|
||||
The options are:
|
||||
|
||||
* -o *out.mbtiles*: Write the new tiles to the specified .mbtiles file
|
||||
@ -261,10 +318,12 @@ The options are:
|
||||
* -c *match.csv*: Use *match.csv* as the source for new attributes to join to the features. The first line of the file should be the key names; the other lines are values. The first column is the one to match against the existing features; the other columns are the new data to add.
|
||||
* -x *key*: Remove attributes of type *key* from the output. You can use this to remove the field you are matching against if you no longer need it after joining, or to remove any other attributes you don't want.
|
||||
* -i: Only include features that matched the CSV.
|
||||
* -pk: Don't skip tiles larger than 500K.
|
||||
|
||||
Because tile-join just copies the geometries to the new .mbtiles without processing them,
|
||||
Because tile-join just copies the geometries to the new .mbtiles without processing them
|
||||
(except to rescale the extents if necessary),
|
||||
it doesn't have any of tippecanoe's recourses if the new tiles are bigger than the 500K tile limit.
|
||||
If a tile is too big, it is just left out of the new tileset.
|
||||
If a tile is too big and you haven't specified `-pk`, it is just left out of the new tileset.
|
||||
|
||||
Example
|
||||
-------
|
||||
@ -334,8 +393,13 @@ on an entire file:
|
||||
or on an individual tile:
|
||||
|
||||
tippecanoe-decode file.mbtiles zoom x y
|
||||
tippecanoe-decode file.vector.pbf zoom x y
|
||||
|
||||
If you decode an entire file, you get a nested `FeatureCollection` identifying each
|
||||
tile and layer separately. Note that the same features generally appear at all zooms,
|
||||
so the output for the file will have many copies of the same features at different
|
||||
resolutions.
|
||||
|
||||
### Options
|
||||
|
||||
* -t _projection_: Specify the projection of the output data. Currently supported are EPSG:4326 (WGS84, the default) and EPSG:3857 (Web Mercator).
|
||||
|
@ -1,5 +1,4 @@
|
||||
Boost Software License - Version 1.0 - August 17th, 2003
|
||||
http://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
Permission is hereby granted, free of charge, to any person or organization
|
||||
obtaining a copy of the software and accompanying documentation covered by
|
||||
@ -21,4 +20,4 @@ FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
DEALINGS IN THE SOFTWARE.
|
10524
catch/catch.hpp
Normal file
10524
catch/catch.hpp
Normal file
File diff suppressed because it is too large
Load Diff
84
clip.c
84
clip.c
@ -1,84 +0,0 @@
|
||||
#include "clip.h"
|
||||
|
||||
#define INSIDE 0
|
||||
#define LEFT 1
|
||||
#define RIGHT 2
|
||||
#define BOTTOM 4
|
||||
#define TOP 8
|
||||
|
||||
static int computeOutCode(double x, double y, double xmin, double ymin, double xmax, double ymax) {
|
||||
int code = INSIDE;
|
||||
|
||||
if (x < xmin) {
|
||||
code |= LEFT;
|
||||
} else if (x > xmax) {
|
||||
code |= RIGHT;
|
||||
}
|
||||
|
||||
if (y < ymin) {
|
||||
code |= BOTTOM;
|
||||
} else if (y > ymax) {
|
||||
code |= TOP;
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
int clip(double *x0, double *y0, double *x1, double *y1, double xmin, double ymin, double xmax, double ymax) {
|
||||
int outcode0 = computeOutCode(*x0, *y0, xmin, ymin, xmax, ymax);
|
||||
int outcode1 = computeOutCode(*x1, *y1, xmin, ymin, xmax, ymax);
|
||||
int accept = 0;
|
||||
int changed = 0;
|
||||
|
||||
while (1) {
|
||||
if (!(outcode0 | outcode1)) { // Bitwise OR is 0. Trivially accept and get out of loop
|
||||
accept = 1;
|
||||
break;
|
||||
} else if (outcode0 & outcode1) { // Bitwise AND is not 0. Trivially reject and get out of loop
|
||||
break;
|
||||
} else {
|
||||
// failed both tests, so calculate the line segment to clip
|
||||
// from an outside point to an intersection with clip edge
|
||||
double x = *x0, y = *y0;
|
||||
|
||||
// At least one endpoint is outside the clip rectangle; pick it.
|
||||
int outcodeOut = outcode0 ? outcode0 : outcode1;
|
||||
|
||||
// Now find the intersection point;
|
||||
// use formulas y = y0 + slope * (x - x0), x = x0 + (1 / slope) * (y - y0)
|
||||
if (outcodeOut & TOP) { // point is above the clip rectangle
|
||||
x = *x0 + (*x1 - *x0) * (ymax - *y0) / (*y1 - *y0);
|
||||
y = ymax;
|
||||
} else if (outcodeOut & BOTTOM) { // point is below the clip rectangle
|
||||
x = *x0 + (*x1 - *x0) * (ymin - *y0) / (*y1 - *y0);
|
||||
y = ymin;
|
||||
} else if (outcodeOut & RIGHT) { // point is to the right of clip rectangle
|
||||
y = *y0 + (*y1 - *y0) * (xmax - *x0) / (*x1 - *x0);
|
||||
x = xmax;
|
||||
} else if (outcodeOut & LEFT) { // point is to the left of clip rectangle
|
||||
y = *y0 + (*y1 - *y0) * (xmin - *x0) / (*x1 - *x0);
|
||||
x = xmin;
|
||||
}
|
||||
|
||||
// Now we move outside point to intersection point to clip
|
||||
// and get ready for next pass.
|
||||
if (outcodeOut == outcode0) {
|
||||
*x0 = x;
|
||||
*y0 = y;
|
||||
outcode0 = computeOutCode(*x0, *y0, xmin, ymin, xmax, ymax);
|
||||
changed = 1;
|
||||
} else {
|
||||
*x1 = x;
|
||||
*y1 = y;
|
||||
outcode1 = computeOutCode(*x1, *y1, xmin, ymin, xmax, ymax);
|
||||
changed = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (accept == 0) {
|
||||
return 0;
|
||||
} else {
|
||||
return changed + 1;
|
||||
}
|
||||
}
|
1
clip.h
1
clip.h
@ -1 +0,0 @@
|
||||
int clip(double *x0, double *y0, double *x1, double *y1, double xmin, double ymin, double xmax, double ymax);
|
407
clipper/README
407
clipper/README
@ -1,407 +0,0 @@
|
||||
=====================================================================
|
||||
Clipper Change Log
|
||||
=====================================================================
|
||||
v6.2.1 (31 October 2014) Rev 482
|
||||
* Bugfix in ClipperOffset.Execute where the Polytree.IsHole property
|
||||
was returning incorrect values with negative offsets
|
||||
* Very minor improvement to join rounding in ClipperOffset
|
||||
* Fixed CPP OpenGL demo.
|
||||
|
||||
v6.2.0 (17 October 2014) Rev 477
|
||||
* Numerous minor bugfixes, too many to list.
|
||||
(See revisions 454-475 in Sourceforge Repository)
|
||||
* The ZFillFunction (custom callback function) has had its parameters
|
||||
changed.
|
||||
* Curves demo removed (temporarily).
|
||||
* Deprecated functions have been removed.
|
||||
|
||||
v6.1.5 (26 February 2014) Rev 460
|
||||
* Improved the joining of output polygons sharing a common edge
|
||||
when those common edges are horizontal.
|
||||
* Fixed a bug in ClipperOffset.AddPath() which would produce
|
||||
incorrect solutions when open paths were added before closed paths.
|
||||
* Minor code tidy and performance improvement
|
||||
|
||||
v6.1.4 (6 February 2014)
|
||||
* Fixed bugs in MinkowskiSum
|
||||
* Fixed minor bug when using Clipper.ForceSimplify.
|
||||
* Modified use_xyz callback so that all 4 vertices around an
|
||||
intersection point are now passed to the callback function.
|
||||
|
||||
v6.1.3a (22 January 2014) Rev 453
|
||||
* Fixed buggy PointInPolygon function (C++ and C# only).
|
||||
Note this bug only affected the newly exported function, the
|
||||
internal PointInPolygon function used by Clipper was OK.
|
||||
|
||||
v6.1.3 (19 January 2014) Rev 452
|
||||
* Fixed potential endless loop condition when adding open
|
||||
paths to Clipper.
|
||||
* Fixed missing implementation of SimplifyPolygon function
|
||||
in C++ code.
|
||||
* Fixed incorrect upper range constant for polygon coordinates
|
||||
in Delphi code.
|
||||
* Added PointInPolygon function.
|
||||
* Overloaded MinkowskiSum function to accommodate multi-contour
|
||||
paths.
|
||||
|
||||
v6.1.2 (15 December 2013) Rev 444
|
||||
* Fixed broken C++ header file.
|
||||
* Minor improvement to joining polygons.
|
||||
|
||||
v6.1.1 (13 December 2013) Rev 441
|
||||
* Fixed a couple of bugs affecting open paths that could
|
||||
raise unhandled exceptions.
|
||||
|
||||
v6.1.0 (12 December 2013)
|
||||
* Deleted: Previously deprecated code has been removed.
|
||||
* Modified: The OffsetPaths function is now deprecated as it has
|
||||
been replaced by the ClipperOffset class which is much more
|
||||
flexible.
|
||||
* Bugfixes: Several minor bugs have been fixed including
|
||||
occasionally an incorrect nesting within the PolyTree structure.
|
||||
|
||||
v6.0.0 (30 October 2013)
|
||||
* Added: Open path (polyline) clipping. A new 'Curves' demo
|
||||
application showcases this (see the 'Curves' directory).
|
||||
* Update: Major improvement in the merging of
|
||||
shared/collinear edges in clip solutions (see Execute).
|
||||
* Added: The IntPoint structure now has an optional 'Z' member.
|
||||
(See the precompiler directive use_xyz.)
|
||||
* Added: Users can now force Clipper to use 32bit integers
|
||||
(via the precompiler directive use_int32) instead of using
|
||||
64bit integers.
|
||||
* Modified: To accommodate open paths, the Polygon and Polygons
|
||||
structures have been renamed Path and Paths respectively. The
|
||||
AddPolygon and AddPolygons methods of the ClipperBase class
|
||||
have been renamed AddPath and AddPaths respectively. Several
|
||||
other functions have been similarly renamed.
|
||||
* Modified: The PolyNode Class has a new IsOpen property.
|
||||
* Modified: The Clipper class has a new ZFillFunction property.
|
||||
* Added: MinkowskiSum and MinkowskiDiff functions added.
|
||||
* Added: Several other new functions have been added including
|
||||
PolyTreeToPaths, OpenPathsFromPolyTree and ClosedPathsFromPolyTree.
|
||||
* Added: The Clipper constructor now accepts an optional InitOptions
|
||||
parameter to simplify setting properties.
|
||||
* Bugfixes: Numerous minor bugs have been fixed.
|
||||
* Deprecated: Version 6 is a major upgrade from previous versions
|
||||
and quite a number of changes have been made to exposed structures
|
||||
and functions. To minimize inconvenience to existing library users,
|
||||
some code has been retained and some added to maintain backward
|
||||
compatibility. However, because this code will be removed in a
|
||||
future update, it has been marked as deprecated and a precompiler
|
||||
directive use_deprecated has been defined.
|
||||
|
||||
v5.1.6 (23 May 2013)
|
||||
* BugFix: CleanPolygon function was buggy.
|
||||
* Changed: The behaviour of the 'miter' JoinType has been
|
||||
changed so that when squaring occurs, it's no longer
|
||||
extended up to the miter limit but is squared off at
|
||||
exactly 'delta' units. (This improves the look of mitering
|
||||
with larger limits at acute angles.)
|
||||
* Added: New OffsetPolyLines function
|
||||
* Update: Minor code refactoring and optimisations
|
||||
|
||||
v5.1.5 (5 May 2013)
|
||||
* Added: ForceSimple property to Clipper class
|
||||
* Update: Improved documentation
|
||||
|
||||
v5.1.4 (24 March 2013)
|
||||
* Update: CleanPolygon function enhanced.
|
||||
* Update: Documentation improved.
|
||||
|
||||
v5.1.3 (14 March 2013)
|
||||
* Bugfix: Minor bugfixes.
|
||||
* Update: Documentation significantly improved.
|
||||
|
||||
v5.1.2 (26 February 2013)
|
||||
* Bugfix: PolyNode class was missing a constructor.
|
||||
* Update: The MiterLimit parameter in the OffsetPolygons
|
||||
function has been renamed Limit and can now also be used to
|
||||
limit the number of vertices used to construct arcs when
|
||||
JoinType is set to jtRound.
|
||||
|
||||
v5.1.0 (17 February 2013)
|
||||
* Update: ExPolygons has been replaced with the PolyTree &
|
||||
PolyNode classes to more fully represent the parent-child
|
||||
relationships of the polygons returned by Clipper.
|
||||
* Added: New CleanPolygon and CleanPolygons functions.
|
||||
* Bugfix: Another orientation bug fixed.
|
||||
|
||||
v5.0.2 - 30 December 2012
|
||||
* Bugfix: Significant fixes in and tidy of the internal
|
||||
Int128 class (which is used only when polygon coordinate
|
||||
values are greater than ±0x3FFFFFFF (~1.07e9)).
|
||||
* Update: The Area algorithm has been updated and is faster.
|
||||
* Update: Documentation updates. The newish but undocumented
|
||||
'CheckInputs' parameter of the OffsetPolygons function has been
|
||||
renamed 'AutoFix' and documented too. The comments on rounding
|
||||
have also been improved (ie clearer and expanded).
|
||||
|
||||
v4.10.0 - 25 December 2012
|
||||
* Bugfix: Orientation bugs should now be resolved (finally!).
|
||||
* Bugfix: Bug in Int128 class
|
||||
|
||||
v4.9.8 - 2 December 2012
|
||||
* Bugfix: Further fixes to rare Orientation bug.
|
||||
|
||||
v4.9.7 - 29 November 2012
|
||||
* Bugfix: Bug that very rarely returned the wrong polygon
|
||||
orientation.
|
||||
* Bugfix: Obscure bug affecting OffsetPolygons when using
|
||||
jtRound for the JoinType parameter and when polygons also
|
||||
contain very large coordinate values (> +/-100000000000).
|
||||
|
||||
v4.9.6 - 9 November 2012
|
||||
* Bugfix: Another obscure bug related to joining polygons.
|
||||
|
||||
v4.9.4 - 2 November 2012
|
||||
* Bugfix: Bugs in Int128 class occasionally causing
|
||||
wrong orientations.
|
||||
* Bugfix: Further fixes related to joining polygons.
|
||||
|
||||
v4.9.0 - 9 October 2012
|
||||
* Bugfix: Obscure bug related to joining polygons.
|
||||
|
||||
v4.8.9 - 25 September 2012
|
||||
* Bugfix: Obscure bug related to precision of intersections.
|
||||
|
||||
v4.8.8 - 30 August 2012
|
||||
* Bugfix: Fixed bug in OffsetPolygons function introduced in
|
||||
version 4.8.5.
|
||||
|
||||
v4.8.7 - 24 August 2012
|
||||
* Bugfix: ReversePolygon function in C++ translation was broken.
|
||||
* Bugfix: Two obscure bugs affecting orientation fixed too.
|
||||
|
||||
v4.8.6 - 11 August 2012
|
||||
* Bugfix: Potential for memory overflow errors when using
|
||||
ExPolygons structure.
|
||||
* Bugfix: The polygon coordinate range has been reduced to
|
||||
+/- 0x3FFFFFFFFFFFFFFF (4.6e18).
|
||||
* Update: ReversePolygons function was misnamed ReversePoints in C++.
|
||||
* Update: SimplifyPolygon function now takes a PolyFillType parameter.
|
||||
|
||||
v4.8.5 - 15 July 2012
|
||||
* Bugfix: Potential for memory overflow errors in OffsetPolygons().
|
||||
|
||||
v4.8.4 - 1 June 2012
|
||||
* Bugfix: Another obscure bug affecting ExPolygons structure.
|
||||
|
||||
v4.8.3 - 27 May 2012
|
||||
* Bugfix: Obscure bug causing incorrect removal of a vertex.
|
||||
|
||||
v4.8.2 - 21 May 2012
|
||||
* Bugfix: Obscure bug could cause an exception when using
|
||||
ExPolygon structure.
|
||||
|
||||
v4.8.1 - 12 May 2012
|
||||
* Update: Cody tidy and minor bug fixes.
|
||||
|
||||
v4.8.0 - 30 April 2012
|
||||
* Bugfix: Occasional errors in orientation fixed.
|
||||
* Update: Added notes on rounding to the documentation.
|
||||
|
||||
v4.7.6 - 11 April 2012
|
||||
* Fixed a bug in Orientation function (affecting C# translations only).
|
||||
* Minor documentation update.
|
||||
|
||||
v4.7.5 - 28 March 2012
|
||||
* Bugfix: Fixed a recently introduced bug that occasionally caused an
|
||||
unhandled exception in C++ and C# translations.
|
||||
|
||||
v4.7.4 - 15 March 2012
|
||||
* Bugfix: Another minor bugfix.
|
||||
|
||||
v4.7.2 - 4 March 2012
|
||||
* Bugfix: Fixed bug introduced in ver 4.7 which sometimes caused
|
||||
an exception if ExPolygon structure was passed to Clipper's
|
||||
Execute method.
|
||||
|
||||
v4.7.1 - 3 March 2012
|
||||
* Bugfix: Rare crash when JoinCommonEdges joined polygons that
|
||||
'cancelled' each other.
|
||||
* Bugfix: Clipper's internal Orientation method occasionally
|
||||
returned wrong result.
|
||||
* Update: Improved C# code (thanks to numerous excellent suggestions
|
||||
from David Piepgrass)
|
||||
|
||||
v4.7 - 10 February 2012
|
||||
* Improved the joining of output polygons sharing a common edge.
|
||||
|
||||
v4.6.6 - 3 February 2012
|
||||
* Bugfix: Another obscure bug occasionally causing incorrect
|
||||
polygon orientation.
|
||||
|
||||
v4.6.5 - 17 January 2012
|
||||
* Bugfix: Obscure bug occasionally causing incorrect hole
|
||||
assignment in ExPolygon structure.
|
||||
|
||||
v4.6.4 - 8 November 2011
|
||||
* Added: SimplifyPolygon and SimplifyPolygons functions.
|
||||
|
||||
v4.6.3 - 11 November 2011
|
||||
* Bugfix: Fixed another minor mitering bug in OffsetPolygons.
|
||||
|
||||
v4.6.2 - 10 November 2011
|
||||
* Bugfix: Fixed a rare bug in the orientation of polygons
|
||||
returned by Clipper's Execute() method.
|
||||
* Bugfix: Previous update introduced a mitering bug in the
|
||||
OffsetPolygons function.
|
||||
|
||||
v4.6 - 29 October 2011
|
||||
* Added: Support for Positive and Negative polygon fill
|
||||
types (in addition to the EvenOdd and NonZero fill types).
|
||||
* Bugfix: The OffsetPolygons function was generating the
|
||||
occasional artefact when 'shrinking' polygons.
|
||||
|
||||
v4.5.5 - 8 October 2011
|
||||
* Bugfix: Fixed an obscure bug in Clipper's JoinCommonEdges
|
||||
method.
|
||||
* Update: Replaced IsClockwise function with Orientation
|
||||
function. The orientation issues affecting OffsetPolygons
|
||||
should now be finally resolved.
|
||||
* Change: The Area function once again returns a signed value.
|
||||
|
||||
v4.5.1 - 28 September 2011
|
||||
* Deleted: The UseFullCoordinateRange property has been
|
||||
deleted since integer range is now managed implicitly.
|
||||
* BugFix: Minor bug in OffsetPolygon mitering.
|
||||
* Change: C# JoinType enum moved from Clipper class to
|
||||
ClipperLib namespace.
|
||||
* Change: The Area function now returns the absolute area
|
||||
(irrespective of orientation).
|
||||
* Change: The IsClockwise function now requires a second
|
||||
parameter - YAxisPositiveUpward - to accommodate displays
|
||||
with Y-axis oriented in either direction
|
||||
|
||||
v4.4.4 - 10 September 2011
|
||||
* Change: Deleted jtButt from JoinType (used by the
|
||||
OffsetPolygons function).
|
||||
* BugFix: Fixed another minor bug in OffsetPolygons function.
|
||||
* Update: Further improvements to the help file
|
||||
|
||||
v4.4.3 - 29 August 2011
|
||||
* BugFix: fixed a minor rounding issue in OffsetPolygons
|
||||
function (affected C++ & C# translations).
|
||||
* BugFix: fixed a minor bug in OffsetPolygons' function
|
||||
declaration (affected C++ translation only).
|
||||
* Change: 'clipper' namespace changed to 'ClipperLib'
|
||||
namespace in both C++ and C# code to remove the ambiguity
|
||||
between the Clipper class and the namespace. (This also
|
||||
required numerous updates to the accompanying demos.)
|
||||
|
||||
v4.4.2 - 26 August 2011
|
||||
* BugFix: minor bugfixes in Clipper.
|
||||
* Update: the OffsetPolygons function has been significantly
|
||||
improved by offering 4 different join styles.
|
||||
|
||||
v4.4.0 - 6 August 2011
|
||||
* BugFix: A number of minor bugs have been fixed that mostly
|
||||
affected the new ExPolygons structure.
|
||||
|
||||
v4.3.0 - 17 June 2011
|
||||
* New: ExPolygons structure that explicitly associates 'hole'
|
||||
polygons with their 'outer' container polygons.
|
||||
* New: Execute method overloaded so the solution parameter
|
||||
can now be either Polygons or ExPolygons.
|
||||
* BugFix: Fixed a rare bug in solution polygons orientation.
|
||||
|
||||
v4.2.8 - 21 May 2011
|
||||
* Update: JoinCommonEdges() improved once more.
|
||||
* BugFix: Several minor bugs fixed.
|
||||
|
||||
v4.2.6 - 1 May 2011
|
||||
* Bugfix: minor bug in SlopesEqual function.
|
||||
* Update: Merging of output polygons sharing common edges
|
||||
has been significantly inproved
|
||||
|
||||
v4.2.4 - 26 April 2011
|
||||
Input polygon coordinates can now contain the full range of
|
||||
signed 64bit integers (ie +/-9,223,372,036,854,775,807). This
|
||||
means that floating point values can be converted to and from
|
||||
Clipper's 64bit integer coordinates structure (IntPoint) and
|
||||
still retain a precision of up to 18 decimal places. However,
|
||||
since the large-integer math that supports this expanded range
|
||||
imposes a small cost on performance (~15%), a new property
|
||||
UseFullCoordinateRange has been added to the Clipper class to
|
||||
allow users the choice of whether or not to use this expanded
|
||||
coordinate range. If this property is disabled, coordinate values
|
||||
are restricted to +/-1,500,000,000.
|
||||
|
||||
v4.2 - 12 April 2011
|
||||
JoinCommonEdges() code significantly improved plus other minor
|
||||
improvements.
|
||||
|
||||
v4.1.2 - 9 April 2011
|
||||
* Update: Minor code tidy.
|
||||
* Bugfix: Possible endless loop in JoinCommonEdges() in clipper.pas.
|
||||
|
||||
v4.1.1 - 8 April 2011
|
||||
* Update: All polygon coordinates are now stored as 64bit integers
|
||||
(though they're still restricted to range -1.5e9 to +1.5e9 pending
|
||||
the inclusion of code supporting 64bit math).
|
||||
* Change: AddPolygon and AddPolygons methods now return boolean
|
||||
values.
|
||||
* Bugfix: Bug in JoinCommonEdges() caused potential endless loop.
|
||||
* Bugfix: Bug in IsClockwise(). (C++ code only)
|
||||
|
||||
v4.0 - 5 April 2011
|
||||
* Clipper 4 is a major rewrite of earlier versions. The biggest
|
||||
change is that floating point values are no longer used,
|
||||
except for the storing of edge slope values. The main benefit
|
||||
of this is the issue of numerical robustness has been
|
||||
addressed. Due to other major code improvements Clipper v4
|
||||
is approximately 40% faster than Clipper v3.
|
||||
* The AddPolyPolygon method has been renamed to AddPolygons.
|
||||
* The IgnoreOrientation property has been removed.
|
||||
* The clipper_misc library has been merged back into the
|
||||
main clipper library.
|
||||
|
||||
v3.1.0 - 17 February 2011
|
||||
* Bugfix: Obscure bug in TClipperBase.SetDx method that caused
|
||||
problems with very small edges ( edges <1/1000th pixel in size).
|
||||
|
||||
v3.0.3 - 9 February 2011
|
||||
* Bugfix: Significant bug, but only in C# code.
|
||||
* Update: Minor refactoring.
|
||||
|
||||
v3.0 - 31 January 2011
|
||||
* Update: Major rewrite of the portion of code that calculates
|
||||
the output polygons' orientation.
|
||||
* Update: Help file significantly improved.
|
||||
* Change: Renamed ForceOrientation property to IgnoreOrientation.
|
||||
If the orientation of output polygons is not important, or can
|
||||
be managed separately, clipping routines can be sped up by about
|
||||
60% by setting IgnoreOrientation to true. Defaults to false.
|
||||
* Change: The OffsetPolygon and Area functions have been moved to
|
||||
the new unit - clipper_misc.
|
||||
|
||||
2.99 - 15 January 2011
|
||||
* Bugfix: Obscure bug in AddPolygon method could cause an endless loop.
|
||||
|
||||
2.8 - 20 November 2010
|
||||
* Updated: Output polygons which previously shared a common
|
||||
edge are now merged.
|
||||
* Changed: The orientation of outer polygons is now clockwise
|
||||
when the display's Y axis is positive downwards (as is
|
||||
typical for most Windows applications). Inner polygons
|
||||
(holes) have the opposite orientation.
|
||||
* Added: Support module for Cairo Graphics Library (with demo).
|
||||
* Updated: C# and C++ demos.
|
||||
|
||||
2.522 - 15 October 2010
|
||||
* Added C# translation (thanks to Olivier Lejeune) and
|
||||
a link to Ruby bindings (thanks to Mike Owens).
|
||||
|
||||
2.0 - 30 July 2010
|
||||
* Clipper now clips using both the Even-Odd (alternate) and
|
||||
Non-Zero (winding) polygon filling rules. (Previously Clipper
|
||||
assumed the Even-Odd rule for polygon filling.)
|
||||
|
||||
1.4c - 16 June 2010
|
||||
* Added C++ support for AGG graphics library
|
||||
|
||||
1.2s - 2 June 2010
|
||||
* Added C++ translation of clipper.pas
|
||||
|
||||
1.0 - 9 May 2010
|
4629
clipper/clipper.cpp
4629
clipper/clipper.cpp
File diff suppressed because it is too large
Load Diff
@ -1,435 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* *
|
||||
* Author : Angus Johnson *
|
||||
* Version : 6.4.0 *
|
||||
* Date : 2 July 2015 *
|
||||
* Website : http://www.angusj.com *
|
||||
* Copyright : Angus Johnson 2010-2015 *
|
||||
* *
|
||||
* License: *
|
||||
* Use, modification & distribution is subject to Boost Software License Ver 1. *
|
||||
* http://www.boost.org/LICENSE_1_0.txt *
|
||||
* *
|
||||
* Attributions: *
|
||||
* The code in this library is an extension of Bala Vatti's clipping algorithm: *
|
||||
* "A generic solution to polygon clipping" *
|
||||
* Communications of the ACM, Vol 35, Issue 7 (July 1992) pp 56-63. *
|
||||
* http://portal.acm.org/citation.cfm?id=129906 *
|
||||
* *
|
||||
* Computer graphics and geometric modeling: implementation and algorithms *
|
||||
* By Max K. Agoston *
|
||||
* Springer; 1 edition (January 4, 2005) *
|
||||
* http://books.google.com/books?q=vatti+clipping+agoston *
|
||||
* *
|
||||
* See also: *
|
||||
* "Polygon Offsetting by Computing Winding Numbers" *
|
||||
* Paper no. DETC2005-85513 pp. 565-575 *
|
||||
* ASME 2005 International Design Engineering Technical Conferences *
|
||||
* and Computers and Information in Engineering Conference (IDETC/CIE2005) *
|
||||
* September 24-28, 2005 , Long Beach, California, USA *
|
||||
* http://www.me.berkeley.edu/~mcmains/pubs/DAC05OffsetPolygon.pdf *
|
||||
* *
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef clipper_hpp
|
||||
#define clipper_hpp
|
||||
|
||||
#define CLIPPER_VERSION "6.2.6"
|
||||
|
||||
//use_int32: When enabled 32bit ints are used instead of 64bit ints. This
|
||||
//improve performance but coordinate values are limited to the range +/- 46340
|
||||
//#define use_int32
|
||||
|
||||
//use_xyz: adds a Z member to IntPoint. Adds a minor cost to perfomance.
|
||||
//#define use_xyz
|
||||
|
||||
//use_lines: Enables line clipping. Adds a very minor cost to performance.
|
||||
//#define use_lines
|
||||
|
||||
//use_deprecated: Enables temporary support for the obsolete functions
|
||||
//#define use_deprecated
|
||||
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <set>
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <ostream>
|
||||
#include <functional>
|
||||
#include <queue>
|
||||
#if defined(CLIPPER_IMPL_INCLUDE)
|
||||
#include CLIPPER_IMPL_INCLUDE
|
||||
#endif
|
||||
|
||||
namespace ClipperLib {
|
||||
|
||||
enum ClipType { ctIntersection, ctUnion, ctDifference, ctXor };
|
||||
enum PolyType { ptSubject, ptClip };
|
||||
//By far the most widely used winding rules for polygon filling are
|
||||
//EvenOdd & NonZero (GDI, GDI+, XLib, OpenGL, Cairo, AGG, Quartz, SVG, Gr32)
|
||||
//Others rules include Positive, Negative and ABS_GTR_EQ_TWO (only in OpenGL)
|
||||
//see http://glprogramming.com/red/chapter11.html
|
||||
enum PolyFillType { pftEvenOdd, pftNonZero, pftPositive, pftNegative };
|
||||
|
||||
#ifdef use_int32
|
||||
typedef int cInt;
|
||||
static cInt const loRange = 0x7FFF;
|
||||
static cInt const hiRange = 0x7FFF;
|
||||
#else
|
||||
typedef signed long long cInt;
|
||||
static cInt const loRange = 0x3FFFFFFF;
|
||||
static cInt const hiRange = 0x3FFFFFFFFFFFFFFFLL;
|
||||
typedef signed long long long64; //used by Int128 class
|
||||
typedef unsigned long long ulong64;
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(CLIPPER_INTPOINT_IMPL)
|
||||
|
||||
typedef CLIPPER_INTPOINT_IMPL IntPoint;
|
||||
|
||||
#else
|
||||
|
||||
struct IntPoint {
|
||||
cInt X;
|
||||
cInt Y;
|
||||
#ifdef use_xyz
|
||||
cInt Z;
|
||||
IntPoint(cInt x = 0, cInt y = 0, cInt z = 0): X(x), Y(y), Z(z) {};
|
||||
#else
|
||||
IntPoint(cInt x = 0, cInt y = 0): X(x), Y(y) {};
|
||||
#endif
|
||||
|
||||
friend inline bool operator== (const IntPoint& a, const IntPoint& b)
|
||||
{
|
||||
return a.X == b.X && a.Y == b.Y;
|
||||
}
|
||||
friend inline bool operator!= (const IntPoint& a, const IntPoint& b)
|
||||
{
|
||||
return a.X != b.X || a.Y != b.Y;
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#if defined(CLIPPER_PATH_IMPL)
|
||||
|
||||
typedef CLIPPER_PATH_IMPL Path;
|
||||
|
||||
#else
|
||||
|
||||
typedef std::vector< IntPoint > Path;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(CLIPPER_PATHS_IMPL)
|
||||
|
||||
typedef CLIPPER_PATHS_IMPL Paths;
|
||||
|
||||
#else
|
||||
|
||||
typedef std::vector< Path > Paths;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
inline Path& operator <<(Path& poly, const IntPoint& p) {poly.push_back(p); return poly;}
|
||||
inline Paths& operator <<(Paths& polys, const Path& p) {polys.push_back(p); return polys;}
|
||||
|
||||
std::ostream& operator <<(std::ostream &s, const IntPoint &p);
|
||||
std::ostream& operator <<(std::ostream &s, const Path &p);
|
||||
std::ostream& operator <<(std::ostream &s, const Paths &p);
|
||||
|
||||
struct DoublePoint
|
||||
{
|
||||
double X;
|
||||
double Y;
|
||||
DoublePoint(double x = 0, double y = 0) : X(x), Y(y) {}
|
||||
DoublePoint(IntPoint ip) : X((double)ip.X), Y((double)ip.Y) {}
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#ifdef use_xyz
|
||||
typedef void (*ZFillCallback)(IntPoint& e1bot, IntPoint& e1top, IntPoint& e2bot, IntPoint& e2top, IntPoint& pt);
|
||||
#endif
|
||||
|
||||
enum InitOptions {ioReverseSolution = 1, ioStrictlySimple = 2, ioPreserveCollinear = 4};
|
||||
enum JoinType {jtSquare, jtRound, jtMiter};
|
||||
enum EndType {etClosedPolygon, etClosedLine, etOpenButt, etOpenSquare, etOpenRound};
|
||||
|
||||
class PolyNode;
|
||||
typedef std::vector< PolyNode* > PolyNodes;
|
||||
|
||||
class PolyNode
|
||||
{
|
||||
public:
|
||||
PolyNode();
|
||||
virtual ~PolyNode(){};
|
||||
Path Contour;
|
||||
PolyNodes Childs;
|
||||
PolyNode* Parent;
|
||||
PolyNode* GetNext() const;
|
||||
bool IsHole() const;
|
||||
bool IsOpen() const;
|
||||
int ChildCount() const;
|
||||
private:
|
||||
unsigned Index; //node index in Parent.Childs
|
||||
bool m_IsOpen;
|
||||
JoinType m_jointype;
|
||||
EndType m_endtype;
|
||||
PolyNode* GetNextSiblingUp() const;
|
||||
void AddChild(PolyNode& child);
|
||||
friend class Clipper; //to access Index
|
||||
friend class ClipperOffset;
|
||||
};
|
||||
|
||||
class PolyTree: public PolyNode
|
||||
{
|
||||
public:
|
||||
~PolyTree(){Clear();};
|
||||
PolyNode* GetFirst() const;
|
||||
void Clear();
|
||||
int Total() const;
|
||||
private:
|
||||
PolyNodes AllNodes;
|
||||
friend class Clipper; //to access AllNodes
|
||||
};
|
||||
|
||||
bool Orientation(const Path &poly);
|
||||
double Area(const Path &poly);
|
||||
int PointInPolygon(const IntPoint &pt, const Path &path);
|
||||
|
||||
void SimplifyPolygon(const Path &in_poly, Paths &out_polys, PolyFillType fillType = pftEvenOdd);
|
||||
void SimplifyPolygons(const Paths &in_polys, Paths &out_polys, PolyFillType fillType = pftEvenOdd);
|
||||
void SimplifyPolygons(Paths &polys, PolyFillType fillType = pftEvenOdd);
|
||||
|
||||
void CleanPolygon(const Path& in_poly, Path& out_poly, double distance = 1.415);
|
||||
void CleanPolygon(Path& poly, double distance = 1.415);
|
||||
void CleanPolygons(const Paths& in_polys, Paths& out_polys, double distance = 1.415);
|
||||
void CleanPolygons(Paths& polys, double distance = 1.415);
|
||||
|
||||
void MinkowskiSum(const Path& pattern, const Path& path, Paths& solution, bool pathIsClosed);
|
||||
void MinkowskiSum(const Path& pattern, const Paths& paths, Paths& solution, bool pathIsClosed);
|
||||
void MinkowskiDiff(const Path& poly1, const Path& poly2, Paths& solution);
|
||||
|
||||
void PolyTreeToPaths(const PolyTree& polytree, Paths& paths);
|
||||
void ClosedPathsFromPolyTree(const PolyTree& polytree, Paths& paths);
|
||||
void OpenPathsFromPolyTree(PolyTree& polytree, Paths& paths);
|
||||
|
||||
void ReversePath(Path& p);
|
||||
void ReversePaths(Paths& p);
|
||||
|
||||
struct IntRect { cInt left; cInt top; cInt right; cInt bottom; };
|
||||
|
||||
//enums that are used internally ...
|
||||
enum EdgeSide { esLeft = 1, esRight = 2};
|
||||
|
||||
//forward declarations (for stuff used internally) ...
|
||||
struct TEdge;
|
||||
struct IntersectNode;
|
||||
struct LocalMinimum;
|
||||
struct OutPt;
|
||||
struct OutRec;
|
||||
struct Join;
|
||||
|
||||
typedef std::vector < OutRec* > PolyOutList;
|
||||
typedef std::vector < TEdge* > EdgeList;
|
||||
typedef std::vector < Join* > JoinList;
|
||||
typedef std::vector < IntersectNode* > IntersectList;
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//ClipperBase is the ancestor to the Clipper class. It should not be
|
||||
//instantiated directly. This class simply abstracts the conversion of sets of
|
||||
//polygon coordinates into edge objects that are stored in a LocalMinima list.
|
||||
class ClipperBase
|
||||
{
|
||||
public:
|
||||
ClipperBase();
|
||||
virtual ~ClipperBase();
|
||||
virtual bool AddPath(const Path &pg, PolyType PolyTyp, bool Closed);
|
||||
bool AddPaths(const Paths &ppg, PolyType PolyTyp, bool Closed);
|
||||
virtual void Clear();
|
||||
IntRect GetBounds();
|
||||
bool PreserveCollinear() {return m_PreserveCollinear;};
|
||||
void PreserveCollinear(bool value) {m_PreserveCollinear = value;};
|
||||
protected:
|
||||
void DisposeLocalMinimaList();
|
||||
TEdge* AddBoundsToLML(TEdge *e, bool IsClosed);
|
||||
virtual void Reset();
|
||||
TEdge* ProcessBound(TEdge* E, bool IsClockwise);
|
||||
void InsertScanbeam(const cInt Y);
|
||||
bool PopScanbeam(cInt &Y);
|
||||
bool LocalMinimaPending();
|
||||
bool PopLocalMinima(cInt Y, const LocalMinimum *&locMin);
|
||||
OutRec* CreateOutRec();
|
||||
void DisposeAllOutRecs();
|
||||
void DisposeOutRec(PolyOutList::size_type index);
|
||||
void SwapPositionsInAEL(TEdge *edge1, TEdge *edge2);
|
||||
void DeleteFromAEL(TEdge *e);
|
||||
void UpdateEdgeIntoAEL(TEdge *&e);
|
||||
|
||||
typedef std::vector<LocalMinimum> MinimaList;
|
||||
MinimaList::iterator m_CurrentLM;
|
||||
MinimaList m_MinimaList;
|
||||
|
||||
bool m_UseFullRange;
|
||||
EdgeList m_edges;
|
||||
bool m_PreserveCollinear;
|
||||
bool m_HasOpenPaths;
|
||||
PolyOutList m_PolyOuts;
|
||||
TEdge *m_ActiveEdges;
|
||||
|
||||
typedef std::priority_queue<cInt> ScanbeamList;
|
||||
ScanbeamList m_Scanbeam;
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
class Clipper : public virtual ClipperBase
|
||||
{
|
||||
public:
|
||||
Clipper(int initOptions = 0);
|
||||
bool Execute(ClipType clipType,
|
||||
Paths &solution,
|
||||
PolyFillType fillType = pftEvenOdd);
|
||||
bool Execute(ClipType clipType,
|
||||
Paths &solution,
|
||||
PolyFillType subjFillType,
|
||||
PolyFillType clipFillType);
|
||||
bool Execute(ClipType clipType,
|
||||
PolyTree &polytree,
|
||||
PolyFillType fillType = pftEvenOdd);
|
||||
bool Execute(ClipType clipType,
|
||||
PolyTree &polytree,
|
||||
PolyFillType subjFillType,
|
||||
PolyFillType clipFillType);
|
||||
bool ReverseSolution() { return m_ReverseOutput; };
|
||||
void ReverseSolution(bool value) {m_ReverseOutput = value;};
|
||||
bool StrictlySimple() {return m_StrictSimple;};
|
||||
void StrictlySimple(bool value) {m_StrictSimple = value;};
|
||||
//set the callback function for z value filling on intersections (otherwise Z is 0)
|
||||
#ifdef use_xyz
|
||||
void ZFillFunction(ZFillCallback zFillFunc);
|
||||
#endif
|
||||
protected:
|
||||
virtual bool ExecuteInternal();
|
||||
private:
|
||||
JoinList m_Joins;
|
||||
JoinList m_GhostJoins;
|
||||
IntersectList m_IntersectList;
|
||||
ClipType m_ClipType;
|
||||
typedef std::list<cInt> MaximaList;
|
||||
MaximaList m_Maxima;
|
||||
TEdge *m_SortedEdges;
|
||||
bool m_ExecuteLocked;
|
||||
PolyFillType m_ClipFillType;
|
||||
PolyFillType m_SubjFillType;
|
||||
bool m_ReverseOutput;
|
||||
bool m_UsingPolyTree;
|
||||
bool m_StrictSimple;
|
||||
#ifdef use_xyz
|
||||
ZFillCallback m_ZFill; //custom callback
|
||||
#endif
|
||||
void SetWindingCount(TEdge& edge);
|
||||
bool IsEvenOddFillType(const TEdge& edge) const;
|
||||
bool IsEvenOddAltFillType(const TEdge& edge) const;
|
||||
void InsertLocalMinimaIntoAEL(const cInt botY);
|
||||
void InsertEdgeIntoAEL(TEdge *edge, TEdge* startEdge);
|
||||
void AddEdgeToSEL(TEdge *edge);
|
||||
bool PopEdgeFromSEL(TEdge *&edge);
|
||||
void CopyAELToSEL();
|
||||
void DeleteFromSEL(TEdge *e);
|
||||
void SwapPositionsInSEL(TEdge *edge1, TEdge *edge2);
|
||||
bool IsContributing(const TEdge& edge) const;
|
||||
bool IsTopHorz(const cInt XPos);
|
||||
void DoMaxima(TEdge *e);
|
||||
void ProcessHorizontals();
|
||||
void ProcessHorizontal(TEdge *horzEdge);
|
||||
void AddLocalMaxPoly(TEdge *e1, TEdge *e2, const IntPoint &pt);
|
||||
OutPt* AddLocalMinPoly(TEdge *e1, TEdge *e2, const IntPoint &pt);
|
||||
OutRec* GetOutRec(int idx);
|
||||
void AppendPolygon(TEdge *e1, TEdge *e2);
|
||||
void IntersectEdges(TEdge *e1, TEdge *e2, IntPoint &pt);
|
||||
OutPt* AddOutPt(TEdge *e, const IntPoint &pt);
|
||||
OutPt* GetLastOutPt(TEdge *e);
|
||||
bool ProcessIntersections(const cInt topY);
|
||||
void BuildIntersectList(const cInt topY);
|
||||
void ProcessIntersectList();
|
||||
void ProcessEdgesAtTopOfScanbeam(const cInt topY);
|
||||
void BuildResult(Paths& polys);
|
||||
void BuildResult2(PolyTree& polytree);
|
||||
void SetHoleState(TEdge *e, OutRec *outrec);
|
||||
void DisposeIntersectNodes();
|
||||
bool FixupIntersectionOrder();
|
||||
void FixupOutPolygon(OutRec &outrec);
|
||||
void FixupOutPolyline(OutRec &outrec);
|
||||
bool IsHole(TEdge *e);
|
||||
bool FindOwnerFromSplitRecs(OutRec &outRec, OutRec *&currOrfl);
|
||||
void FixHoleLinkage(OutRec &outrec);
|
||||
void AddJoin(OutPt *op1, OutPt *op2, const IntPoint offPt);
|
||||
void ClearJoins();
|
||||
void ClearGhostJoins();
|
||||
void AddGhostJoin(OutPt *op, const IntPoint offPt);
|
||||
bool JoinPoints(Join *j, OutRec* outRec1, OutRec* outRec2);
|
||||
void JoinCommonEdges();
|
||||
void DoSimplePolygons();
|
||||
void FixupFirstLefts1(OutRec* OldOutRec, OutRec* NewOutRec);
|
||||
void FixupFirstLefts2(OutRec* InnerOutRec, OutRec* OuterOutRec);
|
||||
void FixupFirstLefts3(OutRec* OldOutRec, OutRec* NewOutRec);
|
||||
#ifdef use_xyz
|
||||
void SetZ(IntPoint& pt, TEdge& e1, TEdge& e2);
|
||||
#endif
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
class ClipperOffset
|
||||
{
|
||||
public:
|
||||
ClipperOffset(double miterLimit = 2.0, double roundPrecision = 0.25);
|
||||
~ClipperOffset();
|
||||
void AddPath(const Path& path, JoinType joinType, EndType endType);
|
||||
void AddPaths(const Paths& paths, JoinType joinType, EndType endType);
|
||||
void Execute(Paths& solution, double delta);
|
||||
void Execute(PolyTree& solution, double delta);
|
||||
void Clear();
|
||||
double MiterLimit;
|
||||
double ArcTolerance;
|
||||
private:
|
||||
Paths m_destPolys;
|
||||
Path m_srcPoly;
|
||||
Path m_destPoly;
|
||||
std::vector<DoublePoint> m_normals;
|
||||
double m_delta, m_sinA, m_sin, m_cos;
|
||||
double m_miterLim, m_StepsPerRad;
|
||||
IntPoint m_lowest;
|
||||
PolyNode m_polyNodes;
|
||||
|
||||
void FixOrientations();
|
||||
void DoOffset(double delta);
|
||||
void OffsetPoint(int j, int& k, JoinType jointype);
|
||||
void DoSquare(int j, int k);
|
||||
void DoMiter(int j, int k, double r);
|
||||
void DoRound(int j, int k);
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
class clipperException : public std::exception
|
||||
{
|
||||
public:
|
||||
clipperException(const char* description): m_descr(description) {}
|
||||
virtual ~clipperException() throw() {}
|
||||
virtual const char* what() const throw() {return m_descr.c_str();}
|
||||
private:
|
||||
std::string m_descr;
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
} //ClipperLib namespace
|
||||
|
||||
#endif //clipper_hpp
|
||||
|
||||
|
@ -1,58 +1,20 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sqlite3.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <zlib.h>
|
||||
#include <math.h>
|
||||
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
|
||||
#include <google/protobuf/io/coded_stream.h>
|
||||
#include "vector_tile.pb.h"
|
||||
#include "tile.h"
|
||||
|
||||
extern "C" {
|
||||
#include "projection.h"
|
||||
}
|
||||
|
||||
// https://github.com/mapbox/mapnik-vector-tile/blob/master/src/vector_tile_compression.hpp
|
||||
inline bool is_compressed(std::string const &data) {
|
||||
return data.size() > 2 && (((uint8_t) data[0] == 0x78 && (uint8_t) data[1] == 0x9C) || ((uint8_t) data[0] == 0x1F && (uint8_t) data[1] == 0x8B));
|
||||
}
|
||||
|
||||
// https://github.com/mapbox/mapnik-vector-tile/blob/master/src/vector_tile_compression.hpp
|
||||
inline int decompress(std::string const &input, std::string &output) {
|
||||
z_stream inflate_s;
|
||||
inflate_s.zalloc = Z_NULL;
|
||||
inflate_s.zfree = Z_NULL;
|
||||
inflate_s.opaque = Z_NULL;
|
||||
inflate_s.avail_in = 0;
|
||||
inflate_s.next_in = Z_NULL;
|
||||
if (inflateInit2(&inflate_s, 32 + 15) != Z_OK) {
|
||||
fprintf(stderr, "error: %s\n", inflate_s.msg);
|
||||
}
|
||||
inflate_s.next_in = (Bytef *) input.data();
|
||||
inflate_s.avail_in = input.size();
|
||||
size_t length = 0;
|
||||
do {
|
||||
output.resize(length + 2 * input.size());
|
||||
inflate_s.avail_out = 2 * input.size();
|
||||
inflate_s.next_out = (Bytef *) (output.data() + length);
|
||||
int ret = inflate(&inflate_s, Z_FINISH);
|
||||
if (ret != Z_STREAM_END && ret != Z_OK && ret != Z_BUF_ERROR) {
|
||||
fprintf(stderr, "error: %s\n", inflate_s.msg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
length += (2 * input.size() - inflate_s.avail_out);
|
||||
} while (inflate_s.avail_out == 0);
|
||||
inflateEnd(&inflate_s);
|
||||
output.resize(length);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int dezig(unsigned n) {
|
||||
return (n >> 1) ^ (-(n & 1));
|
||||
}
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/mman.h>
|
||||
#include <protozero/pbf_reader.hpp>
|
||||
#include "mvt.hpp"
|
||||
#include "projection.hpp"
|
||||
#include "geometry.hpp"
|
||||
|
||||
void printq(const char *s) {
|
||||
putchar('"');
|
||||
@ -68,37 +30,33 @@ void printq(const char *s) {
|
||||
putchar('"');
|
||||
}
|
||||
|
||||
struct draw {
|
||||
struct lonlat {
|
||||
int op;
|
||||
double lon;
|
||||
double lat;
|
||||
int x;
|
||||
int y;
|
||||
|
||||
draw(int op, double lon, double lat) {
|
||||
this->op = op;
|
||||
this->lon = lon;
|
||||
this->lat = lat;
|
||||
lonlat(int nop, double nlon, double nlat, int nx, int ny) {
|
||||
this->op = nop;
|
||||
this->lon = nlon;
|
||||
this->lat = nlat;
|
||||
this->x = nx;
|
||||
this->y = ny;
|
||||
}
|
||||
};
|
||||
|
||||
void handle(std::string message, int z, unsigned x, unsigned y, int describe) {
|
||||
GOOGLE_PROTOBUF_VERIFY_VERSION;
|
||||
int within = 0;
|
||||
mvt_tile tile;
|
||||
|
||||
// https://github.com/mapbox/mapnik-vector-tile/blob/master/examples/c%2B%2B/tileinfo.cpp
|
||||
mapnik::vector::tile tile;
|
||||
|
||||
if (is_compressed(message)) {
|
||||
std::string uncompressed;
|
||||
decompress(message, uncompressed);
|
||||
google::protobuf::io::ArrayInputStream stream(uncompressed.c_str(), uncompressed.length());
|
||||
google::protobuf::io::CodedInputStream codedstream(&stream);
|
||||
codedstream.SetTotalBytesLimit(10 * 67108864, 5 * 67108864);
|
||||
if (!tile.ParseFromCodedStream(&codedstream)) {
|
||||
fprintf(stderr, "Couldn't decompress tile %d/%u/%u\n", z, x, y);
|
||||
try {
|
||||
if (!tile.decode(message)) {
|
||||
fprintf(stderr, "Couldn't parse tile %d/%u/%u\n", z, x, y);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
} else if (!tile.ParseFromString(message)) {
|
||||
fprintf(stderr, "Couldn't parse tile %d/%u/%u\n", z, x, y);
|
||||
} catch (protozero::unknown_pbf_wire_type_exception e) {
|
||||
fprintf(stderr, "PBF decoding error in tile %d/%u/%u\n", z, x, y);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@ -106,13 +64,19 @@ void handle(std::string message, int z, unsigned x, unsigned y, int describe) {
|
||||
|
||||
if (describe) {
|
||||
printf(", \"properties\": { \"zoom\": %d, \"x\": %d, \"y\": %d }", z, x, y);
|
||||
|
||||
if (projection != projections) {
|
||||
printf(", \"crs\": { \"type\": \"name\", \"properties\": { \"name\": ");
|
||||
printq(projection->alias);
|
||||
printf(" } }");
|
||||
}
|
||||
}
|
||||
|
||||
printf(", \"features\": [\n");
|
||||
|
||||
for (int l = 0; l < tile.layers_size(); l++) {
|
||||
mapnik::vector::tile_layer layer = tile.layers(l);
|
||||
int extent = layer.extent();
|
||||
for (size_t l = 0; l < tile.layers.size(); l++) {
|
||||
mvt_layer &layer = tile.layers[l];
|
||||
int extent = layer.extent;
|
||||
|
||||
if (describe) {
|
||||
if (l != 0) {
|
||||
@ -121,16 +85,16 @@ void handle(std::string message, int z, unsigned x, unsigned y, int describe) {
|
||||
|
||||
printf("{ \"type\": \"FeatureCollection\"");
|
||||
printf(", \"properties\": { \"layer\": ");
|
||||
printq(layer.name().c_str());
|
||||
printq(layer.name.c_str());
|
||||
printf(", \"version\": %d, \"extent\": %d", layer.version, layer.extent);
|
||||
printf(" }");
|
||||
printf(", \"features\": [\n");
|
||||
|
||||
within = 0;
|
||||
}
|
||||
|
||||
for (int f = 0; f < layer.features_size(); f++) {
|
||||
mapnik::vector::tile_feature feat = layer.features(f);
|
||||
int px = 0, py = 0;
|
||||
for (size_t f = 0; f < layer.features.size(); f++) {
|
||||
mvt_feature &feat = layer.features[f];
|
||||
|
||||
if (within) {
|
||||
printf(",\n");
|
||||
@ -138,86 +102,94 @@ void handle(std::string message, int z, unsigned x, unsigned y, int describe) {
|
||||
within = 1;
|
||||
|
||||
printf("{ \"type\": \"Feature\"");
|
||||
|
||||
if (feat.has_id) {
|
||||
printf(", \"id\": %llu", feat.id);
|
||||
}
|
||||
|
||||
printf(", \"properties\": { ");
|
||||
|
||||
for (int t = 0; t + 1 < feat.tags_size(); t += 2) {
|
||||
for (size_t t = 0; t + 1 < feat.tags.size(); t += 2) {
|
||||
if (t != 0) {
|
||||
printf(", ");
|
||||
}
|
||||
|
||||
const char *key = layer.keys(feat.tags(t)).c_str();
|
||||
mapnik::vector::tile_value const &val = layer.values(feat.tags(t + 1));
|
||||
if (feat.tags[t] >= layer.keys.size()) {
|
||||
fprintf(stderr, "Error: out of bounds feature key\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (feat.tags[t + 1] >= layer.values.size()) {
|
||||
fprintf(stderr, "Error: out of bounds feature value\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (val.has_string_value()) {
|
||||
const char *key = layer.keys[feat.tags[t]].c_str();
|
||||
mvt_value const &val = layer.values[feat.tags[t + 1]];
|
||||
|
||||
if (val.type == mvt_string) {
|
||||
printq(key);
|
||||
printf(": ");
|
||||
printq(val.string_value().c_str());
|
||||
} else if (val.has_int_value()) {
|
||||
printq(val.string_value.c_str());
|
||||
} else if (val.type == mvt_int) {
|
||||
printq(key);
|
||||
printf(": %lld", (long long) val.int_value());
|
||||
} else if (val.has_double_value()) {
|
||||
printf(": %lld", (long long) val.numeric_value.int_value);
|
||||
} else if (val.type == mvt_double) {
|
||||
printq(key);
|
||||
double v = val.double_value();
|
||||
double v = val.numeric_value.double_value;
|
||||
if (v == (long long) v) {
|
||||
printf(": %lld", (long long) v);
|
||||
} else {
|
||||
printf(": %g", v);
|
||||
}
|
||||
} else if (val.has_float_value()) {
|
||||
} else if (val.type == mvt_float) {
|
||||
printq(key);
|
||||
double v = val.float_value();
|
||||
double v = val.numeric_value.float_value;
|
||||
if (v == (long long) v) {
|
||||
printf(": %lld", (long long) v);
|
||||
} else {
|
||||
printf(": %g", v);
|
||||
}
|
||||
} else if (val.has_sint_value()) {
|
||||
} else if (val.type == mvt_sint) {
|
||||
printq(key);
|
||||
printf(": %lld", (long long) val.sint_value());
|
||||
} else if (val.has_uint_value()) {
|
||||
printf(": %lld", (long long) val.numeric_value.sint_value);
|
||||
} else if (val.type == mvt_uint) {
|
||||
printq(key);
|
||||
printf(": %lld", (long long) val.uint_value());
|
||||
} else if (val.has_bool_value()) {
|
||||
printf(": %lld", (long long) val.numeric_value.uint_value);
|
||||
} else if (val.type == mvt_bool) {
|
||||
printq(key);
|
||||
printf(": %s", val.bool_value() ? "true" : "false");
|
||||
printf(": %s", val.numeric_value.bool_value ? "true" : "false");
|
||||
}
|
||||
}
|
||||
|
||||
printf(" }, \"geometry\": { ");
|
||||
|
||||
std::vector<draw> ops;
|
||||
std::vector<lonlat> ops;
|
||||
|
||||
for (int g = 0; g < feat.geometry_size(); g++) {
|
||||
uint32_t geom = feat.geometry(g);
|
||||
uint32_t op = geom & 7;
|
||||
uint32_t count = geom >> 3;
|
||||
for (size_t g = 0; g < feat.geometry.size(); g++) {
|
||||
int op = feat.geometry[g].op;
|
||||
long long px = feat.geometry[g].x;
|
||||
long long py = feat.geometry[g].y;
|
||||
|
||||
if (op == VT_MOVETO || op == VT_LINETO) {
|
||||
for (unsigned k = 0; k < count; k++) {
|
||||
px += dezig(feat.geometry(g + 1));
|
||||
py += dezig(feat.geometry(g + 2));
|
||||
g += 2;
|
||||
long long scale = 1LL << (32 - z);
|
||||
long long wx = scale * x + (scale / extent) * px;
|
||||
long long wy = scale * y + (scale / extent) * py;
|
||||
|
||||
long long scale = 1LL << (32 - z);
|
||||
long long wx = scale * x + (scale / extent) * px;
|
||||
long long wy = scale * y + (scale / extent) * py;
|
||||
double lat, lon;
|
||||
projection->unproject(wx, wy, 32, &lon, &lat);
|
||||
|
||||
double lat, lon;
|
||||
tile2latlon(wx, wy, 32, &lat, &lon);
|
||||
|
||||
ops.push_back(draw(op, lon, lat));
|
||||
}
|
||||
ops.push_back(lonlat(op, lon, lat, px, py));
|
||||
} else {
|
||||
ops.push_back(draw(op, 0, 0));
|
||||
ops.push_back(lonlat(op, 0, 0, 0, 0));
|
||||
}
|
||||
}
|
||||
|
||||
if (feat.type() == VT_POINT) {
|
||||
if (feat.type == VT_POINT) {
|
||||
if (ops.size() == 1) {
|
||||
printf("\"type\": \"Point\", \"coordinates\": [ %f, %f ]", ops[0].lon, ops[0].lat);
|
||||
} else {
|
||||
printf("\"type\": \"MultiPoint\", \"coordinates\": [ ");
|
||||
for (unsigned i = 0; i < ops.size(); i++) {
|
||||
for (size_t i = 0; i < ops.size(); i++) {
|
||||
if (i != 0) {
|
||||
printf(", ");
|
||||
}
|
||||
@ -225,9 +197,9 @@ void handle(std::string message, int z, unsigned x, unsigned y, int describe) {
|
||||
}
|
||||
printf(" ]");
|
||||
}
|
||||
} else if (feat.type() == VT_LINE) {
|
||||
} else if (feat.type == VT_LINE) {
|
||||
int movetos = 0;
|
||||
for (unsigned i = 0; i < ops.size(); i++) {
|
||||
for (size_t i = 0; i < ops.size(); i++) {
|
||||
if (ops[i].op == VT_MOVETO) {
|
||||
movetos++;
|
||||
}
|
||||
@ -235,7 +207,7 @@ void handle(std::string message, int z, unsigned x, unsigned y, int describe) {
|
||||
|
||||
if (movetos < 2) {
|
||||
printf("\"type\": \"LineString\", \"coordinates\": [ ");
|
||||
for (unsigned i = 0; i < ops.size(); i++) {
|
||||
for (size_t i = 0; i < ops.size(); i++) {
|
||||
if (i != 0) {
|
||||
printf(", ");
|
||||
}
|
||||
@ -245,7 +217,7 @@ void handle(std::string message, int z, unsigned x, unsigned y, int describe) {
|
||||
} else {
|
||||
printf("\"type\": \"MultiLineString\", \"coordinates\": [ [ ");
|
||||
int state = 0;
|
||||
for (unsigned i = 0; i < ops.size(); i++) {
|
||||
for (size_t i = 0; i < ops.size(); i++) {
|
||||
if (ops[i].op == VT_MOVETO) {
|
||||
if (state == 0) {
|
||||
printf("[ %f, %f ]", ops[i].lon, ops[i].lat);
|
||||
@ -261,35 +233,39 @@ void handle(std::string message, int z, unsigned x, unsigned y, int describe) {
|
||||
}
|
||||
printf(" ] ]");
|
||||
}
|
||||
} else if (feat.type() == VT_POLYGON) {
|
||||
std::vector<std::vector<draw> > rings;
|
||||
} else if (feat.type == VT_POLYGON) {
|
||||
std::vector<std::vector<lonlat> > rings;
|
||||
std::vector<double> areas;
|
||||
|
||||
for (unsigned i = 0; i < ops.size(); i++) {
|
||||
for (size_t i = 0; i < ops.size(); i++) {
|
||||
if (ops[i].op == VT_MOVETO) {
|
||||
rings.push_back(std::vector<draw>());
|
||||
rings.push_back(std::vector<lonlat>());
|
||||
areas.push_back(0);
|
||||
}
|
||||
|
||||
int n = rings.size() - 1;
|
||||
if (n >= 0) {
|
||||
rings[n].push_back(ops[i]);
|
||||
if (ops[i].op == VT_CLOSEPATH) {
|
||||
rings[n].push_back(rings[n][0]);
|
||||
} else {
|
||||
rings[n].push_back(ops[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int outer = 0;
|
||||
|
||||
for (unsigned i = 0; i < rings.size(); i++) {
|
||||
double area = 0;
|
||||
for (unsigned k = 0; k < rings[i].size(); k++) {
|
||||
for (size_t i = 0; i < rings.size(); i++) {
|
||||
long double area = 0;
|
||||
for (size_t k = 0; k < rings[i].size(); k++) {
|
||||
if (rings[i][k].op != VT_CLOSEPATH) {
|
||||
area += rings[i][k].lon * rings[i][(k + 1) % rings[i].size()].lat;
|
||||
area -= rings[i][k].lat * rings[i][(k + 1) % rings[i].size()].lon;
|
||||
area += rings[i][k].x * rings[i][(k + 1) % rings[i].size()].y;
|
||||
area -= rings[i][k].y * rings[i][(k + 1) % rings[i].size()].x;
|
||||
}
|
||||
}
|
||||
|
||||
areas[i] = area;
|
||||
if (areas[i] <= 0 || i == 0) {
|
||||
if (areas[i] >= 0 || i == 0) {
|
||||
outer++;
|
||||
}
|
||||
|
||||
@ -303,8 +279,8 @@ void handle(std::string message, int z, unsigned x, unsigned y, int describe) {
|
||||
}
|
||||
|
||||
int state = 0;
|
||||
for (unsigned i = 0; i < rings.size(); i++) {
|
||||
if (areas[i] <= 0) {
|
||||
for (size_t i = 0; i < rings.size(); i++) {
|
||||
if (areas[i] >= 0) {
|
||||
if (state != 0) {
|
||||
// new multipolygon
|
||||
printf(" ] ], [ [ ");
|
||||
@ -317,7 +293,7 @@ void handle(std::string message, int z, unsigned x, unsigned y, int describe) {
|
||||
printf(" ], [ ");
|
||||
}
|
||||
|
||||
for (unsigned j = 0; j < rings[i].size(); j++) {
|
||||
for (size_t j = 0; j < rings[i].size(); j++) {
|
||||
if (rings[i][j].op != VT_CLOSEPATH) {
|
||||
if (j != 0) {
|
||||
printf(", ");
|
||||
@ -359,6 +335,35 @@ void decode(char *fname, int z, unsigned x, unsigned y) {
|
||||
int oz = z;
|
||||
unsigned ox = x, oy = y;
|
||||
|
||||
int fd = open(fname, O_RDONLY);
|
||||
if (fd >= 0) {
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) == 0) {
|
||||
if (st.st_size < 50 * 1024 * 1024) {
|
||||
char *map = (char *) mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
if (map != NULL && map != MAP_FAILED) {
|
||||
if (strcmp(map, "SQLite format 3") != 0) {
|
||||
if (z >= 0) {
|
||||
std::string s = std::string(map, st.st_size);
|
||||
handle(s, z, x, y, 1);
|
||||
munmap(map, st.st_size);
|
||||
return;
|
||||
} else {
|
||||
fprintf(stderr, "Must specify zoom/x/y to decode a single pbf file\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
}
|
||||
munmap(map, st.st_size);
|
||||
}
|
||||
} else {
|
||||
perror("fstat");
|
||||
}
|
||||
close(fd);
|
||||
} else {
|
||||
perror(fname);
|
||||
}
|
||||
|
||||
if (sqlite3_open(fname, &db) != SQLITE_OK) {
|
||||
fprintf(stderr, "%s: %s\n", fname, sqlite3_errmsg(db));
|
||||
exit(EXIT_FAILURE);
|
||||
@ -408,13 +413,13 @@ void decode(char *fname, int z, unsigned x, unsigned y) {
|
||||
within = 1;
|
||||
|
||||
int len = sqlite3_column_bytes(stmt, 0);
|
||||
int z = sqlite3_column_int(stmt, 1);
|
||||
int x = sqlite3_column_int(stmt, 2);
|
||||
int y = sqlite3_column_int(stmt, 3);
|
||||
y = (1LL << z) - 1 - y;
|
||||
int tz = sqlite3_column_int(stmt, 1);
|
||||
int tx = sqlite3_column_int(stmt, 2);
|
||||
int ty = sqlite3_column_int(stmt, 3);
|
||||
ty = (1LL << tz) - 1 - ty;
|
||||
const char *s = (const char *) sqlite3_column_blob(stmt, 0);
|
||||
|
||||
handle(std::string(s, len), z, x, y, 1);
|
||||
handle(std::string(s, len), tz, tx, ty, 1);
|
||||
}
|
||||
|
||||
printf("] }\n");
|
||||
@ -461,17 +466,24 @@ void decode(char *fname, int z, unsigned x, unsigned y) {
|
||||
}
|
||||
|
||||
void usage(char **argv) {
|
||||
fprintf(stderr, "Usage: %s file.mbtiles zoom x y\n", argv[0]);
|
||||
fprintf(stderr, "Usage: %s [-t projection] file.mbtiles zoom x y\n", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
extern int optind;
|
||||
// extern char *optarg;
|
||||
extern char *optarg;
|
||||
int i;
|
||||
|
||||
while ((i = getopt(argc, argv, "")) != -1) {
|
||||
usage(argv);
|
||||
while ((i = getopt(argc, argv, "t:")) != -1) {
|
||||
switch (i) {
|
||||
case 't':
|
||||
set_projection_or_exit(optarg);
|
||||
break;
|
||||
|
||||
default:
|
||||
usage(argv);
|
||||
}
|
||||
}
|
||||
|
||||
if (argc == optind + 4) {
|
@ -11,7 +11,7 @@ void enumerate(char *fname) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
char *sql = "SELECT zoom_level, tile_column, tile_row from tiles;";
|
||||
const char *sql = "SELECT zoom_level, tile_column, tile_row from tiles order by zoom_level, tile_column, tile_row;";
|
||||
|
||||
sqlite3_stmt *stmt;
|
||||
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK) {
|
660
geojson.cpp
Normal file
660
geojson.cpp
Normal file
@ -0,0 +1,660 @@
|
||||
#ifdef MTRACE
|
||||
#include <mcheck.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/mman.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <sqlite3.h>
|
||||
#include <stdarg.h>
|
||||
#include <sys/resource.h>
|
||||
#include <pthread.h>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
extern "C" {
|
||||
#include "jsonpull/jsonpull.h"
|
||||
}
|
||||
|
||||
#include "pool.hpp"
|
||||
#include "projection.hpp"
|
||||
#include "memfile.hpp"
|
||||
#include "main.hpp"
|
||||
#include "mbtiles.hpp"
|
||||
#include "geojson.hpp"
|
||||
#include "geometry.hpp"
|
||||
#include "options.hpp"
|
||||
#include "serial.hpp"
|
||||
#include "text.hpp"
|
||||
|
||||
#define GEOM_POINT 0 /* array of positions */
|
||||
#define GEOM_MULTIPOINT 1 /* array of arrays of positions */
|
||||
#define GEOM_LINESTRING 2 /* array of arrays of positions */
|
||||
#define GEOM_MULTILINESTRING 3 /* array of arrays of arrays of positions */
|
||||
#define GEOM_POLYGON 4 /* array of arrays of arrays of positions */
|
||||
#define GEOM_MULTIPOLYGON 5 /* array of arrays of arrays of arrays of positions */
|
||||
#define GEOM_TYPES 6
|
||||
|
||||
static const char *geometry_names[GEOM_TYPES] = {
|
||||
"Point", "MultiPoint", "LineString", "MultiLineString", "Polygon", "MultiPolygon",
|
||||
};
|
||||
|
||||
static int geometry_within[GEOM_TYPES] = {
|
||||
-1, /* point */
|
||||
GEOM_POINT, /* multipoint */
|
||||
GEOM_POINT, /* linestring */
|
||||
GEOM_LINESTRING, /* multilinestring */
|
||||
GEOM_LINESTRING, /* polygon */
|
||||
GEOM_POLYGON, /* multipolygon */
|
||||
};
|
||||
|
||||
static int mb_geometry[GEOM_TYPES] = {
|
||||
VT_POINT, VT_POINT, VT_LINE, VT_LINE, VT_POLYGON, VT_POLYGON,
|
||||
};
|
||||
|
||||
void json_context(json_object *j) {
|
||||
char *s = json_stringify(j);
|
||||
|
||||
if (strlen(s) >= 500) {
|
||||
sprintf(s + 497, "...");
|
||||
}
|
||||
|
||||
fprintf(stderr, "In JSON object %s\n", s);
|
||||
free(s); // stringify
|
||||
}
|
||||
|
||||
long long parse_geometry(int t, json_object *j, long long *bbox, drawvec &out, int op, const char *fname, int line, int *initialized, unsigned *initial_x, unsigned *initial_y, json_object *feature) {
|
||||
long long g = 0;
|
||||
|
||||
if (j == NULL || j->type != JSON_ARRAY) {
|
||||
fprintf(stderr, "%s:%d: expected array for type %d\n", fname, line, t);
|
||||
json_context(feature);
|
||||
return g;
|
||||
}
|
||||
|
||||
int within = geometry_within[t];
|
||||
if (within >= 0) {
|
||||
size_t i;
|
||||
for (i = 0; i < j->length; i++) {
|
||||
if (within == GEOM_POINT) {
|
||||
if (i == 0 || mb_geometry[t] == GEOM_MULTIPOINT) {
|
||||
op = VT_MOVETO;
|
||||
} else {
|
||||
op = VT_LINETO;
|
||||
}
|
||||
}
|
||||
|
||||
g += parse_geometry(within, j->array[i], bbox, out, op, fname, line, initialized, initial_x, initial_y, feature);
|
||||
}
|
||||
} else {
|
||||
if (j->length >= 2 && j->array[0]->type == JSON_NUMBER && j->array[1]->type == JSON_NUMBER) {
|
||||
long long x, y;
|
||||
double lon = j->array[0]->number;
|
||||
double lat = j->array[1]->number;
|
||||
projection->project(lon, lat, 32, &x, &y);
|
||||
|
||||
if (j->length > 2) {
|
||||
static int warned = 0;
|
||||
|
||||
if (!warned) {
|
||||
fprintf(stderr, "%s:%d: ignoring dimensions beyond two\n", fname, line);
|
||||
json_context(j);
|
||||
json_context(feature);
|
||||
warned = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (x < bbox[0]) {
|
||||
bbox[0] = x;
|
||||
}
|
||||
if (y < bbox[1]) {
|
||||
bbox[1] = y;
|
||||
}
|
||||
if (x > bbox[2]) {
|
||||
bbox[2] = x;
|
||||
}
|
||||
if (y > bbox[3]) {
|
||||
bbox[3] = y;
|
||||
}
|
||||
|
||||
if (!*initialized) {
|
||||
if (x < 0 || x >= (1LL << 32) || y < 0 || y >= (1LL < 32)) {
|
||||
*initial_x = 1LL << 31;
|
||||
*initial_y = 1LL << 31;
|
||||
} else {
|
||||
*initial_x = (x >> geometry_scale) << geometry_scale;
|
||||
*initial_y = (y >> geometry_scale) << geometry_scale;
|
||||
}
|
||||
|
||||
*initialized = 1;
|
||||
}
|
||||
|
||||
draw d(op, (x >> geometry_scale), (y >> geometry_scale));
|
||||
out.push_back(d);
|
||||
g++;
|
||||
} else {
|
||||
fprintf(stderr, "%s:%d: malformed point\n", fname, line);
|
||||
json_context(j);
|
||||
json_context(feature);
|
||||
}
|
||||
}
|
||||
|
||||
if (t == GEOM_POLYGON) {
|
||||
// Note that this is not using the correct meaning of closepath.
|
||||
//
|
||||
// We are using it here to close an entire Polygon, to distinguish
|
||||
// the Polygons within a MultiPolygon from each other.
|
||||
//
|
||||
// This will be undone in fix_polygon(), which needs to know which
|
||||
// rings come from which Polygons so that it can make the winding order
|
||||
// of the outer ring be the opposite of the order of the inner rings.
|
||||
|
||||
out.push_back(draw(VT_CLOSEPATH, 0, 0));
|
||||
}
|
||||
|
||||
return g;
|
||||
}
|
||||
|
||||
int serialize_geometry(json_object *geometry, json_object *properties, json_object *id, const char *reading, int line, volatile long long *layer_seq, volatile long long *progress_seq, long long *metapos, long long *geompos, long long *indexpos, std::set<std::string> *exclude, std::set<std::string> *include, int exclude_all, FILE *metafile, FILE *geomfile, FILE *indexfile, struct memfile *poolfile, struct memfile *treefile, const char *fname, int basezoom, int layer, double droprate, long long *file_bbox, json_object *tippecanoe, int segment, int *initialized, unsigned *initial_x, unsigned *initial_y, struct reader *readers, int maxzoom, json_object *feature, std::map<std::string, layermap_entry> *layermap, std::string layername, bool uses_gamma) {
|
||||
json_object *geometry_type = json_hash_get(geometry, "type");
|
||||
if (geometry_type == NULL) {
|
||||
static int warned = 0;
|
||||
if (!warned) {
|
||||
fprintf(stderr, "%s:%d: null geometry (additional not reported)\n", reading, line);
|
||||
json_context(feature);
|
||||
warned = 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (geometry_type->type != JSON_STRING) {
|
||||
fprintf(stderr, "%s:%d: geometry type is not a string\n", reading, line);
|
||||
json_context(feature);
|
||||
return 0;
|
||||
}
|
||||
|
||||
json_object *coordinates = json_hash_get(geometry, "coordinates");
|
||||
if (coordinates == NULL || coordinates->type != JSON_ARRAY) {
|
||||
fprintf(stderr, "%s:%d: feature without coordinates array\n", reading, line);
|
||||
json_context(feature);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int t;
|
||||
for (t = 0; t < GEOM_TYPES; t++) {
|
||||
if (strcmp(geometry_type->string, geometry_names[t]) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (t >= GEOM_TYPES) {
|
||||
fprintf(stderr, "%s:%d: Can't handle geometry type %s\n", reading, line, geometry_type->string);
|
||||
json_context(feature);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int tippecanoe_minzoom = -1;
|
||||
int tippecanoe_maxzoom = -1;
|
||||
std::string tippecanoe_layername;
|
||||
|
||||
if (tippecanoe != NULL) {
|
||||
json_object *min = json_hash_get(tippecanoe, "minzoom");
|
||||
if (min != NULL && min->type == JSON_NUMBER) {
|
||||
tippecanoe_minzoom = min->number;
|
||||
}
|
||||
if (min != NULL && min->type == JSON_STRING) {
|
||||
tippecanoe_minzoom = atoi(min->string);
|
||||
}
|
||||
|
||||
json_object *max = json_hash_get(tippecanoe, "maxzoom");
|
||||
if (max != NULL && max->type == JSON_NUMBER) {
|
||||
tippecanoe_maxzoom = max->number;
|
||||
}
|
||||
if (max != NULL && max->type == JSON_STRING) {
|
||||
tippecanoe_maxzoom = atoi(max->string);
|
||||
}
|
||||
|
||||
json_object *ln = json_hash_get(tippecanoe, "layer");
|
||||
if (ln != NULL && (ln->type == JSON_STRING || ln->type == JSON_NUMBER)) {
|
||||
tippecanoe_layername = std::string(ln->string);
|
||||
}
|
||||
}
|
||||
|
||||
bool has_id = false;
|
||||
unsigned long long id_value = 0;
|
||||
if (id != NULL) {
|
||||
if (id->type == JSON_NUMBER) {
|
||||
if (id->number >= 0) {
|
||||
char *err = NULL;
|
||||
id_value = strtoull(id->string, &err, 10);
|
||||
|
||||
if (err != NULL && *err != '\0') {
|
||||
fprintf(stderr, "Warning: Can't represent non-integer feature ID %s\n", id->string);
|
||||
} else {
|
||||
has_id = true;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "Warning: Can't represent negative feature ID %s\n", id->string);
|
||||
}
|
||||
} else {
|
||||
char *s = json_stringify(id);
|
||||
fprintf(stderr, "Warning: Can't represent non-numeric feature ID %s\n", s);
|
||||
free(s); // stringify
|
||||
}
|
||||
}
|
||||
|
||||
long long bbox[] = {LLONG_MAX, LLONG_MAX, LLONG_MIN, LLONG_MIN};
|
||||
|
||||
if (tippecanoe_layername.size() != 0) {
|
||||
if (layermap->count(tippecanoe_layername) == 0) {
|
||||
layermap->insert(std::pair<std::string, layermap_entry>(tippecanoe_layername, layermap_entry(layermap->size())));
|
||||
}
|
||||
|
||||
auto ai = layermap->find(tippecanoe_layername);
|
||||
if (ai != layermap->end()) {
|
||||
layer = ai->second.id;
|
||||
layername = tippecanoe_layername;
|
||||
} else {
|
||||
fprintf(stderr, "Internal error: can't find layer name %s\n", tippecanoe_layername.c_str());
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
size_t nprop = 0;
|
||||
if (properties != NULL && properties->type == JSON_HASH) {
|
||||
nprop = properties->length;
|
||||
}
|
||||
|
||||
char *metakey[nprop];
|
||||
std::vector<std::string> metaval;
|
||||
metaval.resize(nprop);
|
||||
int metatype[nprop];
|
||||
size_t m = 0;
|
||||
|
||||
for (size_t i = 0; i < nprop; i++) {
|
||||
if (properties->keys[i]->type == JSON_STRING) {
|
||||
std::string s(properties->keys[i]->string);
|
||||
|
||||
if (exclude_all) {
|
||||
if (include->count(s) == 0) {
|
||||
continue;
|
||||
}
|
||||
} else if (exclude->count(s) != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
type_and_string tas;
|
||||
tas.string = s;
|
||||
tas.type = -1;
|
||||
|
||||
metakey[m] = properties->keys[i]->string;
|
||||
|
||||
if (properties->values[i] != NULL && properties->values[i]->type == JSON_STRING) {
|
||||
tas.type = metatype[m] = VT_STRING;
|
||||
metaval[m] = std::string(properties->values[i]->string);
|
||||
std::string err = check_utf8(metaval[m]);
|
||||
if (err != "") {
|
||||
fprintf(stderr, "%s:%d: %s\n", reading, line, err.c_str());
|
||||
json_context(feature);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
m++;
|
||||
} else if (properties->values[i] != NULL && properties->values[i]->type == JSON_NUMBER) {
|
||||
tas.type = metatype[m] = VT_NUMBER;
|
||||
metaval[m] = std::string(properties->values[i]->string);
|
||||
m++;
|
||||
} else if (properties->values[i] != NULL && (properties->values[i]->type == JSON_TRUE || properties->values[i]->type == JSON_FALSE)) {
|
||||
tas.type = metatype[m] = VT_BOOLEAN;
|
||||
metaval[m] = std::string(properties->values[i]->type == JSON_TRUE ? "true" : "false");
|
||||
m++;
|
||||
} else if (properties->values[i] != NULL && (properties->values[i]->type == JSON_NULL)) {
|
||||
;
|
||||
} else {
|
||||
tas.type = metatype[m] = VT_STRING;
|
||||
const char *v = json_stringify(properties->values[i]);
|
||||
metaval[m] = std::string(v);
|
||||
free((void *) v); // stringify
|
||||
m++;
|
||||
}
|
||||
|
||||
if (tas.type >= 0) {
|
||||
auto fk = layermap->find(layername);
|
||||
fk->second.file_keys.insert(tas);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
drawvec dv;
|
||||
long long g = parse_geometry(t, coordinates, bbox, dv, VT_MOVETO, fname, line, initialized, initial_x, initial_y, feature);
|
||||
if (mb_geometry[t] == VT_POLYGON) {
|
||||
dv = fix_polygon(dv);
|
||||
}
|
||||
|
||||
bool inline_meta = true;
|
||||
// Don't inline metadata for features that will span several tiles at maxzoom
|
||||
if (g > 0 && (bbox[2] < bbox[0] || bbox[3] < bbox[1])) {
|
||||
fprintf(stderr, "Internal error: impossible feature bounding box %llx,%llx,%llx,%llx\n", bbox[0], bbox[1], bbox[2], bbox[3]);
|
||||
}
|
||||
if (bbox[2] - bbox[0] > (2LL << (32 - maxzoom)) || bbox[3] - bbox[1] > (2LL << (32 - maxzoom))) {
|
||||
inline_meta = false;
|
||||
|
||||
if (prevent[P_CLIPPING]) {
|
||||
static volatile long long warned = 0;
|
||||
long long extent = ((bbox[2] - bbox[0]) / ((1LL << (32 - maxzoom)) + 1)) * ((bbox[3] - bbox[1]) / ((1LL << (32 - maxzoom)) + 1));
|
||||
if (extent > warned) {
|
||||
fprintf(stderr, "Warning: %s:%d: Large unclipped (-pc) feature may be duplicated across %lld tiles\n", fname, line, extent);
|
||||
warned = extent;
|
||||
|
||||
if (extent > 10000) {
|
||||
fprintf(stderr, "Exiting because this can't be right.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
double extent = 0;
|
||||
if (additional[A_DROP_SMALLEST_AS_NEEDED]) {
|
||||
if (mb_geometry[t] == VT_POLYGON) {
|
||||
for (size_t i = 0; i < dv.size(); i++) {
|
||||
if (dv[i].op == VT_MOVETO) {
|
||||
size_t j;
|
||||
for (j = i + 1; j < dv.size(); j++) {
|
||||
if (dv[j].op != VT_LINETO) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
extent += get_area(dv, i, j);
|
||||
i = j - 1;
|
||||
}
|
||||
}
|
||||
} else if (mb_geometry[t] == VT_LINE) {
|
||||
for (size_t i = 1; i < dv.size(); i++) {
|
||||
if (dv[i].op == VT_LINETO) {
|
||||
double xd = dv[i].x - dv[i - 1].x;
|
||||
double yd = dv[i].y - dv[i - 1].y;
|
||||
extent += sqrt(xd * xd + yd * yd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
long long geomstart = *geompos;
|
||||
long long bbox_index;
|
||||
|
||||
serial_feature sf;
|
||||
sf.layer = layer;
|
||||
sf.segment = segment;
|
||||
sf.t = mb_geometry[t];
|
||||
sf.has_id = has_id;
|
||||
sf.id = id_value;
|
||||
sf.has_tippecanoe_minzoom = (tippecanoe_minzoom != -1);
|
||||
sf.tippecanoe_minzoom = tippecanoe_minzoom;
|
||||
sf.has_tippecanoe_maxzoom = (tippecanoe_maxzoom != -1);
|
||||
sf.tippecanoe_maxzoom = tippecanoe_maxzoom;
|
||||
sf.geometry = dv;
|
||||
sf.m = m;
|
||||
sf.feature_minzoom = 0; // Will be filled in during index merging
|
||||
sf.extent = (long long) extent;
|
||||
|
||||
if (prevent[P_INPUT_ORDER]) {
|
||||
sf.seq = *layer_seq;
|
||||
} else {
|
||||
sf.seq = 0;
|
||||
}
|
||||
|
||||
// Calculate the center even if off the edge of the plane,
|
||||
// and then mask to bring it back into the addressable area
|
||||
long long midx = (bbox[0] / 2 + bbox[2] / 2) & ((1LL << 32) - 1);
|
||||
long long midy = (bbox[1] / 2 + bbox[3] / 2) & ((1LL << 32) - 1);
|
||||
bbox_index = encode(midx, midy);
|
||||
|
||||
if (additional[A_DROP_DENSEST_AS_NEEDED] || additional[A_CALCULATE_FEATURE_DENSITY] || additional[A_INCREASE_GAMMA_AS_NEEDED] || uses_gamma) {
|
||||
sf.index = bbox_index;
|
||||
}
|
||||
|
||||
if (inline_meta) {
|
||||
sf.metapos = -1;
|
||||
for (size_t i = 0; i < m; i++) {
|
||||
sf.keys.push_back(addpool(poolfile, treefile, metakey[i], VT_STRING));
|
||||
sf.values.push_back(addpool(poolfile, treefile, metaval[i].c_str(), metatype[i]));
|
||||
}
|
||||
} else {
|
||||
sf.metapos = *metapos;
|
||||
for (size_t i = 0; i < m; i++) {
|
||||
serialize_long_long(metafile, addpool(poolfile, treefile, metakey[i], VT_STRING), metapos, fname);
|
||||
serialize_long_long(metafile, addpool(poolfile, treefile, metaval[i].c_str(), metatype[i]), metapos, fname);
|
||||
}
|
||||
}
|
||||
|
||||
serialize_feature(geomfile, &sf, geompos, fname, *initial_x >> geometry_scale, *initial_y >> geometry_scale, false);
|
||||
|
||||
struct index index;
|
||||
index.start = geomstart;
|
||||
index.end = *geompos;
|
||||
index.segment = segment;
|
||||
index.seq = *layer_seq;
|
||||
index.t = sf.t;
|
||||
index.index = bbox_index;
|
||||
|
||||
fwrite_check(&index, sizeof(struct index), 1, indexfile, fname);
|
||||
*indexpos += sizeof(struct index);
|
||||
|
||||
for (size_t i = 0; i < 2; i++) {
|
||||
if (bbox[i] < file_bbox[i]) {
|
||||
file_bbox[i] = bbox[i];
|
||||
}
|
||||
}
|
||||
for (size_t i = 2; i < 4; i++) {
|
||||
if (bbox[i] > file_bbox[i]) {
|
||||
file_bbox[i] = bbox[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (*progress_seq % 10000 == 0) {
|
||||
checkdisk(readers, CPUS);
|
||||
if (!quiet) {
|
||||
fprintf(stderr, "Read %.2f million features\r", *progress_seq / 1000000.0);
|
||||
}
|
||||
}
|
||||
(*progress_seq)++;
|
||||
(*layer_seq)++;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void check_crs(json_object *j, const char *reading) {
|
||||
json_object *crs = json_hash_get(j, "crs");
|
||||
if (crs != NULL) {
|
||||
json_object *properties = json_hash_get(crs, "properties");
|
||||
if (properties != NULL) {
|
||||
json_object *name = json_hash_get(properties, "name");
|
||||
if (name->type == JSON_STRING) {
|
||||
if (strcmp(name->string, projection->alias) != 0) {
|
||||
fprintf(stderr, "%s: Warning: GeoJSON specified projection \"%s\", not \"%s\".\n", reading, name->string, projection->alias);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void parse_json(json_pull *jp, const char *reading, volatile long long *layer_seq, volatile long long *progress_seq, long long *metapos, long long *geompos, long long *indexpos, std::set<std::string> *exclude, std::set<std::string> *include, int exclude_all, FILE *metafile, FILE *geomfile, FILE *indexfile, struct memfile *poolfile, struct memfile *treefile, char *fname, int basezoom, int layer, double droprate, long long *file_bbox, int segment, int *initialized, unsigned *initial_x, unsigned *initial_y, struct reader *readers, int maxzoom, std::map<std::string, layermap_entry> *layermap, std::string layername, bool uses_gamma) {
|
||||
long long found_hashes = 0;
|
||||
long long found_features = 0;
|
||||
long long found_geometries = 0;
|
||||
|
||||
while (1) {
|
||||
json_object *j = json_read(jp);
|
||||
if (j == NULL) {
|
||||
if (jp->error != NULL) {
|
||||
fprintf(stderr, "%s:%d: %s\n", reading, jp->line, jp->error);
|
||||
if (jp->root != NULL) {
|
||||
json_context(jp->root);
|
||||
}
|
||||
}
|
||||
|
||||
json_free(jp->root);
|
||||
break;
|
||||
}
|
||||
|
||||
if (j->type == JSON_HASH) {
|
||||
found_hashes++;
|
||||
|
||||
if (found_hashes == 50 && found_features == 0 && found_geometries == 0) {
|
||||
fprintf(stderr, "%s:%d: Warning: not finding any GeoJSON features or geometries in input yet after 50 objects.\n", reading, jp->line);
|
||||
}
|
||||
}
|
||||
|
||||
json_object *type = json_hash_get(j, "type");
|
||||
if (type == NULL || type->type != JSON_STRING) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (found_features == 0) {
|
||||
int i;
|
||||
int is_geometry = 0;
|
||||
for (i = 0; i < GEOM_TYPES; i++) {
|
||||
if (strcmp(type->string, geometry_names[i]) == 0) {
|
||||
is_geometry = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_geometry) {
|
||||
if (j->parent != NULL) {
|
||||
if (j->parent->type == JSON_ARRAY) {
|
||||
if (j->parent->parent->type == JSON_HASH) {
|
||||
json_object *geometries = json_hash_get(j->parent->parent, "geometries");
|
||||
if (geometries != NULL) {
|
||||
// Parent of Parent must be a GeometryCollection
|
||||
is_geometry = 0;
|
||||
}
|
||||
}
|
||||
} else if (j->parent->type == JSON_HASH) {
|
||||
json_object *geometry = json_hash_get(j->parent, "geometry");
|
||||
if (geometry != NULL) {
|
||||
// Parent must be a Feature
|
||||
is_geometry = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (is_geometry) {
|
||||
if (found_features != 0 && found_geometries == 0) {
|
||||
fprintf(stderr, "%s:%d: Warning: found a mixture of features and bare geometries\n", reading, jp->line);
|
||||
}
|
||||
found_geometries++;
|
||||
|
||||
serialize_geometry(j, NULL, NULL, reading, jp->line, layer_seq, progress_seq, metapos, geompos, indexpos, exclude, include, exclude_all, metafile, geomfile, indexfile, poolfile, treefile, fname, basezoom, layer, droprate, file_bbox, NULL, segment, initialized, initial_x, initial_y, readers, maxzoom, j, layermap, layername, uses_gamma);
|
||||
json_free(j);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (strcmp(type->string, "Feature") != 0) {
|
||||
if (strcmp(type->string, "FeatureCollection") == 0) {
|
||||
check_crs(j, reading);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (found_features == 0 && found_geometries != 0) {
|
||||
fprintf(stderr, "%s:%d: Warning: found a mixture of features and bare geometries\n", reading, jp->line);
|
||||
}
|
||||
found_features++;
|
||||
|
||||
json_object *geometry = json_hash_get(j, "geometry");
|
||||
if (geometry == NULL) {
|
||||
fprintf(stderr, "%s:%d: feature with no geometry\n", reading, jp->line);
|
||||
json_context(j);
|
||||
json_free(j);
|
||||
continue;
|
||||
}
|
||||
|
||||
json_object *properties = json_hash_get(j, "properties");
|
||||
if (properties == NULL || (properties->type != JSON_HASH && properties->type != JSON_NULL)) {
|
||||
fprintf(stderr, "%s:%d: feature without properties hash\n", reading, jp->line);
|
||||
json_context(j);
|
||||
json_free(j);
|
||||
continue;
|
||||
}
|
||||
|
||||
json_object *tippecanoe = json_hash_get(j, "tippecanoe");
|
||||
json_object *id = json_hash_get(j, "id");
|
||||
|
||||
json_object *geometries = json_hash_get(geometry, "geometries");
|
||||
if (geometries != NULL) {
|
||||
size_t g;
|
||||
for (g = 0; g < geometries->length; g++) {
|
||||
serialize_geometry(geometries->array[g], properties, id, reading, jp->line, layer_seq, progress_seq, metapos, geompos, indexpos, exclude, include, exclude_all, metafile, geomfile, indexfile, poolfile, treefile, fname, basezoom, layer, droprate, file_bbox, tippecanoe, segment, initialized, initial_x, initial_y, readers, maxzoom, j, layermap, layername, uses_gamma);
|
||||
}
|
||||
} else {
|
||||
serialize_geometry(geometry, properties, id, reading, jp->line, layer_seq, progress_seq, metapos, geompos, indexpos, exclude, include, exclude_all, metafile, geomfile, indexfile, poolfile, treefile, fname, basezoom, layer, droprate, file_bbox, tippecanoe, segment, initialized, initial_x, initial_y, readers, maxzoom, j, layermap, layername, uses_gamma);
|
||||
}
|
||||
|
||||
json_free(j);
|
||||
|
||||
/* XXX check for any non-features in the outer object */
|
||||
}
|
||||
}
|
||||
|
||||
void *run_parse_json(void *v) {
|
||||
struct parse_json_args *pja = (struct parse_json_args *) v;
|
||||
|
||||
parse_json(pja->jp, pja->reading, pja->layer_seq, pja->progress_seq, pja->metapos, pja->geompos, pja->indexpos, pja->exclude, pja->include, pja->exclude_all, pja->metafile, pja->geomfile, pja->indexfile, pja->poolfile, pja->treefile, pja->fname, pja->basezoom, pja->layer, pja->droprate, pja->file_bbox, pja->segment, pja->initialized, pja->initial_x, pja->initial_y, pja->readers, pja->maxzoom, pja->layermap, *pja->layername, pja->uses_gamma);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct jsonmap {
|
||||
char *map;
|
||||
unsigned long long off;
|
||||
unsigned long long end;
|
||||
};
|
||||
|
||||
ssize_t json_map_read(struct json_pull *jp, char *buffer, size_t n) {
|
||||
struct jsonmap *jm = (struct jsonmap *) jp->source;
|
||||
|
||||
if (jm->off + n >= jm->end) {
|
||||
n = jm->end - jm->off;
|
||||
}
|
||||
|
||||
memcpy(buffer, jm->map + jm->off, n);
|
||||
jm->off += n;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
struct json_pull *json_begin_map(char *map, long long len) {
|
||||
struct jsonmap *jm = new jsonmap;
|
||||
if (jm == NULL) {
|
||||
perror("Out of memory");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
jm->map = map;
|
||||
jm->off = 0;
|
||||
jm->end = len;
|
||||
|
||||
return json_begin(json_map_read, jm);
|
||||
}
|
||||
|
||||
void json_end_map(struct json_pull *jp) {
|
||||
delete (struct jsonmap *) jp->source;
|
||||
json_end(jp);
|
||||
}
|
37
geojson.hpp
Normal file
37
geojson.hpp
Normal file
@ -0,0 +1,37 @@
|
||||
struct parse_json_args {
|
||||
json_pull *jp;
|
||||
const char *reading;
|
||||
volatile long long *layer_seq;
|
||||
volatile long long *progress_seq;
|
||||
long long *metapos;
|
||||
long long *geompos;
|
||||
long long *indexpos;
|
||||
std::set<std::string> *exclude;
|
||||
std::set<std::string> *include;
|
||||
int exclude_all;
|
||||
FILE *metafile;
|
||||
FILE *geomfile;
|
||||
FILE *indexfile;
|
||||
struct memfile *poolfile;
|
||||
struct memfile *treefile;
|
||||
char *fname;
|
||||
int basezoom;
|
||||
int layer;
|
||||
double droprate;
|
||||
long long *file_bbox;
|
||||
int segment;
|
||||
int *initialized;
|
||||
unsigned *initial_x;
|
||||
unsigned *initial_y;
|
||||
struct reader *readers;
|
||||
int maxzoom;
|
||||
std::map<std::string, layermap_entry> *layermap;
|
||||
std::string *layername;
|
||||
bool uses_gamma;
|
||||
};
|
||||
|
||||
struct json_pull *json_begin_map(char *map, long long len);
|
||||
void json_end_map(struct json_pull *jp);
|
||||
|
||||
void parse_json(json_pull *jp, const char *reading, volatile long long *layer_seq, volatile long long *progress_seq, long long *metapos, long long *geompos, long long *indexpos, std::set<std::string> *exclude, std::set<std::string> *include, int exclude_all, FILE *metafile, FILE *geomfile, FILE *indexfile, struct memfile *poolfile, struct memfile *treefile, char *fname, int basezoom, int layer, double droprate, long long *file_bbox, int segment, int *initialized, unsigned *initial_x, unsigned *initial_y, struct reader *readers, int maxzoom, std::map<std::string, layermap_entry> *layermap, std::string layername, bool uses_gamma);
|
||||
void *run_parse_json(void *v);
|
1005
geometry.cc
1005
geometry.cc
File diff suppressed because it is too large
Load Diff
1156
geometry.cpp
Normal file
1156
geometry.cpp
Normal file
File diff suppressed because it is too large
Load Diff
32
geometry.hh
32
geometry.hh
@ -1,32 +0,0 @@
|
||||
struct draw {
|
||||
signed char op;
|
||||
long long x;
|
||||
long long y;
|
||||
int necessary;
|
||||
|
||||
draw(int op, long long x, long long y) {
|
||||
this->op = op;
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
}
|
||||
|
||||
draw() {
|
||||
}
|
||||
};
|
||||
|
||||
typedef std::vector<draw> drawvec;
|
||||
|
||||
drawvec decode_geometry(char **meta, int z, unsigned tx, unsigned ty, int detail, long long *bbox, unsigned initial_x, unsigned initial_y);
|
||||
void to_tile_scale(drawvec &geom, int z, int detail);
|
||||
drawvec remove_noop(drawvec geom, int type, int shift);
|
||||
drawvec clip_point(drawvec &geom, int z, int detail, long long buffer);
|
||||
drawvec clean_or_clip_poly(drawvec &geom, int z, int detail, int buffer, bool clip);
|
||||
drawvec simple_clip_poly(drawvec &geom, int z, int detail, int buffer);
|
||||
drawvec close_poly(drawvec &geom);
|
||||
drawvec reduce_tiny_poly(drawvec &geom, int z, int detail, bool *reduced, double *accum_area);
|
||||
drawvec clip_lines(drawvec &geom, int z, int detail, long long buffer);
|
||||
int quick_check(long long *bbox, int z, int detail, long long buffer);
|
||||
drawvec simplify_lines(drawvec &geom, int z, int detail);
|
||||
drawvec reorder_lines(drawvec &geom);
|
||||
drawvec fix_polygon(drawvec &geom);
|
||||
std::vector<drawvec> chop_polygon(std::vector<drawvec> &geoms);
|
73
geometry.hpp
Normal file
73
geometry.hpp
Normal file
@ -0,0 +1,73 @@
|
||||
#define VT_POINT 1
|
||||
#define VT_LINE 2
|
||||
#define VT_POLYGON 3
|
||||
|
||||
#define VT_END 0
|
||||
#define VT_MOVETO 1
|
||||
#define VT_LINETO 2
|
||||
#define VT_CLOSEPATH 7
|
||||
|
||||
#define VT_STRING 1
|
||||
#define VT_NUMBER 2
|
||||
#define VT_BOOLEAN 7
|
||||
|
||||
// The bitfield is to make sizeof(draw) be 16 instead of 24
|
||||
// at the cost, apparently, of a 0.7% increase in running time
|
||||
// for packing and unpacking.
|
||||
struct draw {
|
||||
long long x : 40;
|
||||
signed char op;
|
||||
long long y : 40;
|
||||
signed char necessary;
|
||||
|
||||
draw(int nop, long long nx, long long ny) {
|
||||
this->op = nop;
|
||||
this->x = nx;
|
||||
this->y = ny;
|
||||
this->necessary = 0;
|
||||
}
|
||||
|
||||
draw() {
|
||||
this->op = 0;
|
||||
this->x = 0;
|
||||
this->y = 0;
|
||||
this->necessary = 0;
|
||||
}
|
||||
|
||||
bool operator<(draw const &s) const {
|
||||
if (y < s.y || (y == s.y && x < s.x)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool operator==(draw const &s) const {
|
||||
return y == s.y && x == s.x;
|
||||
}
|
||||
|
||||
bool operator!=(draw const &s) const {
|
||||
return y != s.y || x != s.x;
|
||||
}
|
||||
};
|
||||
|
||||
typedef std::vector<draw> drawvec;
|
||||
|
||||
drawvec decode_geometry(FILE *meta, long long *geompos, int z, unsigned tx, unsigned ty, int detail, long long *bbox, unsigned initial_x, unsigned initial_y);
|
||||
void to_tile_scale(drawvec &geom, int z, int detail);
|
||||
drawvec remove_noop(drawvec geom, int type, int shift);
|
||||
drawvec clip_point(drawvec &geom, int z, int detail, long long buffer);
|
||||
drawvec clean_or_clip_poly(drawvec &geom, int z, int detail, int buffer, bool clip);
|
||||
drawvec simple_clip_poly(drawvec &geom, int z, int detail, int buffer);
|
||||
drawvec close_poly(drawvec &geom);
|
||||
drawvec reduce_tiny_poly(drawvec &geom, int z, int detail, bool *reduced, double *accum_area);
|
||||
drawvec clip_lines(drawvec &geom, int z, int detail, long long buffer);
|
||||
drawvec stairstep(drawvec &geom, int z, int detail);
|
||||
bool point_within_tile(long long x, long long y, int z, int detail, long long buffer);
|
||||
int quick_check(long long *bbox, int z, int detail, long long buffer);
|
||||
drawvec simplify_lines(drawvec &geom, int z, int detail, bool mark_tile_bounds, double simplification, size_t retain);
|
||||
drawvec reorder_lines(drawvec &geom);
|
||||
drawvec fix_polygon(drawvec &geom);
|
||||
std::vector<drawvec> chop_polygon(std::vector<drawvec> &geoms);
|
||||
void check_polygon(drawvec &geom, drawvec &before);
|
||||
double get_area(drawvec &geom, size_t i, size_t j);
|
@ -8,8 +8,12 @@
|
||||
|
||||
#define BUFFER 10000
|
||||
|
||||
json_pull *json_begin(int (*read)(struct json_pull *, char *buffer, int n), void *source) {
|
||||
json_pull *json_begin(ssize_t (*read)(struct json_pull *, char *buffer, size_t n), void *source) {
|
||||
json_pull *j = malloc(sizeof(json_pull));
|
||||
if (j == NULL) {
|
||||
perror("Out of memory");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
j->error = NULL;
|
||||
j->line = 1;
|
||||
@ -18,10 +22,15 @@ json_pull *json_begin(int (*read)(struct json_pull *, char *buffer, int n), void
|
||||
|
||||
j->read = read;
|
||||
j->source = source;
|
||||
j->buffer = malloc(BUFFER);
|
||||
j->buffer_head = 0;
|
||||
j->buffer_tail = 0;
|
||||
|
||||
j->buffer = malloc(BUFFER);
|
||||
if (j->buffer == NULL) {
|
||||
perror("Out of memory");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
@ -40,18 +49,18 @@ static inline int peek(json_pull *j) {
|
||||
|
||||
static inline int next(json_pull *j) {
|
||||
if (j->buffer_head < j->buffer_tail) {
|
||||
return j->buffer[j->buffer_head++];
|
||||
return (unsigned char) j->buffer[j->buffer_head++];
|
||||
} else {
|
||||
j->buffer_head = 0;
|
||||
j->buffer_tail = j->read(j, j->buffer, BUFFER);
|
||||
if (j->buffer_head >= j->buffer_tail) {
|
||||
return EOF;
|
||||
}
|
||||
return j->buffer[j->buffer_head++];
|
||||
return (unsigned char) j->buffer[j->buffer_head++];
|
||||
}
|
||||
}
|
||||
|
||||
static int read_file(json_pull *j, char *buffer, int n) {
|
||||
static ssize_t read_file(json_pull *j, char *buffer, size_t n) {
|
||||
return fread(buffer, 1, n, j->source);
|
||||
}
|
||||
|
||||
@ -59,9 +68,9 @@ json_pull *json_begin_file(FILE *f) {
|
||||
return json_begin(read_file, f);
|
||||
}
|
||||
|
||||
static int read_string(json_pull *j, char *buffer, int n) {
|
||||
static ssize_t read_string(json_pull *j, char *buffer, size_t n) {
|
||||
char *cp = j->source;
|
||||
int out = 0;
|
||||
size_t out = 0;
|
||||
|
||||
while (out < n && cp[out] != '\0') {
|
||||
buffer[out] = cp[out];
|
||||
@ -77,6 +86,7 @@ json_pull *json_begin_string(char *s) {
|
||||
}
|
||||
|
||||
void json_end(json_pull *p) {
|
||||
json_free(p->root);
|
||||
free(p->buffer);
|
||||
free(p);
|
||||
}
|
||||
@ -91,28 +101,41 @@ static inline int read_wrap(json_pull *j) {
|
||||
return c;
|
||||
}
|
||||
|
||||
#define SIZE_FOR(i) (((i) + 31) & ~31)
|
||||
#define SIZE_FOR(i, size) ((size_t)((((i) + 31) & ~31) * size))
|
||||
|
||||
static json_object *fabricate_object(json_object *parent, json_type type) {
|
||||
static json_object *fabricate_object(json_pull *jp, json_object *parent, json_type type) {
|
||||
json_object *o = malloc(sizeof(struct json_object));
|
||||
if (o == NULL) {
|
||||
perror("Out of memory");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
o->type = type;
|
||||
o->parent = parent;
|
||||
o->array = NULL;
|
||||
o->keys = NULL;
|
||||
o->values = NULL;
|
||||
o->length = 0;
|
||||
o->parser = jp;
|
||||
return o;
|
||||
}
|
||||
|
||||
static json_object *add_object(json_pull *j, json_type type) {
|
||||
json_object *c = j->container;
|
||||
json_object *o = fabricate_object(c, type);
|
||||
json_object *o = fabricate_object(j, c, type);
|
||||
|
||||
if (c != NULL) {
|
||||
if (c->type == JSON_ARRAY) {
|
||||
if (c->expect == JSON_ITEM) {
|
||||
if (SIZE_FOR(c->length + 1) != SIZE_FOR(c->length)) {
|
||||
c->array = realloc(c->array, SIZE_FOR(c->length + 1) * sizeof(json_object *));
|
||||
if (SIZE_FOR(c->length + 1, sizeof(json_object *)) != SIZE_FOR(c->length, sizeof(json_object *))) {
|
||||
if (SIZE_FOR(c->length + 1, sizeof(json_object *)) < SIZE_FOR(c->length, sizeof(json_object *))) {
|
||||
fprintf(stderr, "Array size overflow\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
c->array = realloc(c->array, SIZE_FOR(c->length + 1, sizeof(json_object *)));
|
||||
if (c->array == NULL) {
|
||||
perror("Out of memory");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
c->array[c->length++] = o;
|
||||
@ -133,9 +156,17 @@ static json_object *add_object(json_pull *j, json_type type) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (SIZE_FOR(c->length + 1) != SIZE_FOR(c->length)) {
|
||||
c->keys = realloc(c->keys, SIZE_FOR(c->length + 1) * sizeof(json_object *));
|
||||
c->values = realloc(c->values, SIZE_FOR(c->length + 1) * sizeof(json_object *));
|
||||
if (SIZE_FOR(c->length + 1, sizeof(json_object *)) != SIZE_FOR(c->length, sizeof(json_object *))) {
|
||||
if (SIZE_FOR(c->length + 1, sizeof(json_object *)) < SIZE_FOR(c->length, sizeof(json_object *))) {
|
||||
fprintf(stderr, "Hash size overflow\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
c->keys = realloc(c->keys, SIZE_FOR(c->length + 1, sizeof(json_object *)));
|
||||
c->values = realloc(c->values, SIZE_FOR(c->length + 1, sizeof(json_object *)));
|
||||
if (c->keys == NULL || c->values == NULL) {
|
||||
perror("Out of memory");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
c->keys[c->length] = o;
|
||||
@ -149,6 +180,10 @@ static json_object *add_object(json_pull *j, json_type type) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (j->root != NULL) {
|
||||
json_free(j->root);
|
||||
}
|
||||
|
||||
j->root = o;
|
||||
}
|
||||
|
||||
@ -160,7 +195,7 @@ json_object *json_hash_get(json_object *o, const char *s) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int i;
|
||||
size_t i;
|
||||
for (i = 0; i < o->length; i++) {
|
||||
if (o->keys[i] != NULL && o->keys[i]->type == JSON_STRING) {
|
||||
if (strcmp(o->keys[i]->string, s) == 0) {
|
||||
@ -174,21 +209,34 @@ json_object *json_hash_get(json_object *o, const char *s) {
|
||||
|
||||
struct string {
|
||||
char *buf;
|
||||
int n;
|
||||
int nalloc;
|
||||
size_t n;
|
||||
size_t nalloc;
|
||||
};
|
||||
|
||||
static void string_init(struct string *s) {
|
||||
s->nalloc = 500;
|
||||
s->buf = malloc(s->nalloc);
|
||||
if (s->buf == NULL) {
|
||||
perror("Out of memory");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
s->n = 0;
|
||||
s->buf[0] = '\0';
|
||||
}
|
||||
|
||||
static void string_append(struct string *s, char c) {
|
||||
if (s->n + 2 >= s->nalloc) {
|
||||
size_t prev = s->nalloc;
|
||||
s->nalloc += 500;
|
||||
if (s->nalloc <= prev) {
|
||||
fprintf(stderr, "String size overflowed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
s->buf = realloc(s->buf, s->nalloc);
|
||||
if (s->buf == NULL) {
|
||||
perror("Out of memory");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
s->buf[s->n++] = c;
|
||||
@ -196,11 +244,20 @@ static void string_append(struct string *s, char c) {
|
||||
}
|
||||
|
||||
static void string_append_string(struct string *s, char *add) {
|
||||
int len = strlen(add);
|
||||
size_t len = strlen(add);
|
||||
|
||||
if (s->n + len + 1 >= s->nalloc) {
|
||||
size_t prev = s->nalloc;
|
||||
s->nalloc += 500 + len;
|
||||
if (s->nalloc <= prev) {
|
||||
fprintf(stderr, "String size overflowed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
s->buf = realloc(s->buf, s->nalloc);
|
||||
if (s->buf == NULL) {
|
||||
perror("Out of memory");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
for (; *add != '\0'; add++) {
|
||||
@ -219,6 +276,10 @@ json_object *json_read_separators(json_pull *j, json_separator_callback cb, void
|
||||
|
||||
// In case there is an error at the top level
|
||||
if (j->container == NULL) {
|
||||
if (j->root != NULL) {
|
||||
json_free(j->root);
|
||||
}
|
||||
|
||||
j->root = NULL;
|
||||
}
|
||||
|
||||
@ -417,6 +478,11 @@ again:
|
||||
string_append(&val, read_wrap(j));
|
||||
|
||||
c = peek(j);
|
||||
if (c < '0' || c > '9') {
|
||||
j->error = "Decimal point without digits";
|
||||
string_free(&val);
|
||||
return NULL;
|
||||
}
|
||||
while (c >= '0' && c <= '9') {
|
||||
string_append(&val, read_wrap(j));
|
||||
c = peek(j);
|
||||
@ -488,6 +554,11 @@ again:
|
||||
int i;
|
||||
for (i = 0; i < 4; i++) {
|
||||
hex[i] = read_wrap(j);
|
||||
if (hex[i] < '0' || (hex[i] > '9' && hex[i] < 'A') || (hex[i] > 'F' && hex[i] < 'a') || hex[i] > 'f') {
|
||||
j->error = "Invalid \\u hex character";
|
||||
string_free(&val);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
unsigned long ch = strtoul(hex, NULL, 16);
|
||||
if (ch <= 0x7F) {
|
||||
@ -505,10 +576,19 @@ again:
|
||||
string_free(&val);
|
||||
return NULL;
|
||||
}
|
||||
} else if (c < ' ') {
|
||||
j->error = "Found control character in string";
|
||||
string_free(&val);
|
||||
return NULL;
|
||||
} else {
|
||||
string_append(&val, c);
|
||||
}
|
||||
}
|
||||
if (c == EOF) {
|
||||
j->error = "String without closing quote mark";
|
||||
string_free(&val);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
json_object *s = add_object(j, JSON_STRING);
|
||||
if (s != NULL) {
|
||||
@ -541,7 +621,7 @@ json_object *json_read_tree(json_pull *p) {
|
||||
}
|
||||
|
||||
void json_free(json_object *o) {
|
||||
int i;
|
||||
size_t i;
|
||||
|
||||
if (o == NULL) {
|
||||
return;
|
||||
@ -551,7 +631,7 @@ void json_free(json_object *o) {
|
||||
|
||||
if (o->type == JSON_ARRAY) {
|
||||
json_object **a = o->array;
|
||||
int n = o->length;
|
||||
size_t n = o->length;
|
||||
|
||||
o->array = NULL;
|
||||
o->length = 0;
|
||||
@ -564,7 +644,7 @@ void json_free(json_object *o) {
|
||||
} else if (o->type == JSON_HASH) {
|
||||
json_object **k = o->keys;
|
||||
json_object **v = o->values;
|
||||
int n = o->length;
|
||||
size_t n = o->length;
|
||||
|
||||
o->keys = NULL;
|
||||
o->values = NULL;
|
||||
@ -592,7 +672,7 @@ void json_disconnect(json_object *o) {
|
||||
|
||||
if (o->parent != NULL) {
|
||||
if (o->parent->type == JSON_ARRAY) {
|
||||
int i;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < o->parent->length; i++) {
|
||||
if (o->parent->array[i] == o) {
|
||||
@ -607,15 +687,15 @@ void json_disconnect(json_object *o) {
|
||||
}
|
||||
|
||||
if (o->parent->type == JSON_HASH) {
|
||||
int i;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < o->parent->length; i++) {
|
||||
if (o->parent->keys[i] == o) {
|
||||
o->parent->keys[i] = fabricate_object(o->parent, JSON_NULL);
|
||||
o->parent->keys[i] = fabricate_object(o->parser, o->parent, JSON_NULL);
|
||||
break;
|
||||
}
|
||||
if (o->parent->values[i] == o) {
|
||||
o->parent->values[i] = fabricate_object(o->parent, JSON_NULL);
|
||||
o->parent->values[i] = fabricate_object(o->parser, o->parent, JSON_NULL);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -635,6 +715,10 @@ void json_disconnect(json_object *o) {
|
||||
}
|
||||
}
|
||||
|
||||
if (o->parser != NULL && o->parser->root == o) {
|
||||
o->parser->root = NULL;
|
||||
}
|
||||
|
||||
o->parent = NULL;
|
||||
}
|
||||
|
||||
@ -683,7 +767,7 @@ static void json_print(struct string *val, json_object *o) {
|
||||
} else if (o->type == JSON_HASH) {
|
||||
string_append(val, '{');
|
||||
|
||||
int i;
|
||||
size_t i;
|
||||
for (i = 0; i < o->length; i++) {
|
||||
json_print(val, o->keys[i]);
|
||||
string_append(val, ':');
|
||||
@ -695,7 +779,7 @@ static void json_print(struct string *val, json_object *o) {
|
||||
string_append(val, '}');
|
||||
} else if (o->type == JSON_ARRAY) {
|
||||
string_append(val, '[');
|
||||
int i;
|
||||
size_t i;
|
||||
for (i = 0; i < o->length; i++) {
|
||||
json_print(val, o->array[i]);
|
||||
if (i + 1 < o->length) {
|
@ -21,6 +21,7 @@ typedef enum json_type {
|
||||
typedef struct json_object {
|
||||
json_type type;
|
||||
struct json_object *parent;
|
||||
struct json_pull *parser;
|
||||
|
||||
char *string;
|
||||
double number;
|
||||
@ -28,7 +29,7 @@ typedef struct json_object {
|
||||
struct json_object **array;
|
||||
struct json_object **keys;
|
||||
struct json_object **values;
|
||||
int length;
|
||||
size_t length;
|
||||
|
||||
int expect;
|
||||
} json_object;
|
||||
@ -37,11 +38,11 @@ typedef struct json_pull {
|
||||
char *error;
|
||||
int line;
|
||||
|
||||
int (*read)(struct json_pull *, char *buf, int n);
|
||||
ssize_t (*read)(struct json_pull *, char *buf, size_t n);
|
||||
void *source;
|
||||
char *buffer;
|
||||
int buffer_tail;
|
||||
int buffer_head;
|
||||
ssize_t buffer_tail;
|
||||
ssize_t buffer_head;
|
||||
|
||||
json_object *container;
|
||||
json_object *root;
|
||||
@ -50,7 +51,7 @@ typedef struct json_pull {
|
||||
json_pull *json_begin_file(FILE *f);
|
||||
json_pull *json_begin_string(char *s);
|
||||
|
||||
json_pull *json_begin(int (*read)(struct json_pull *, char *buffer, int n), void *source);
|
||||
json_pull *json_begin(ssize_t (*read)(struct json_pull *, char *buffer, size_t n), void *source);
|
||||
void json_end(json_pull *p);
|
||||
|
||||
typedef void (*json_separator_callback)(json_type type, json_pull *j, void *state);
|
18
main.hpp
Normal file
18
main.hpp
Normal file
@ -0,0 +1,18 @@
|
||||
struct index {
|
||||
long long start;
|
||||
long long end;
|
||||
unsigned long long index;
|
||||
short segment;
|
||||
unsigned short t : 2;
|
||||
unsigned long long seq : (64 - 18); // pack with segment and t to stay in 32 bytes
|
||||
};
|
||||
|
||||
void checkdisk(struct reader *r, int nreader);
|
||||
|
||||
extern int geometry_scale;
|
||||
extern int quiet;
|
||||
|
||||
extern size_t CPUS;
|
||||
extern size_t TEMP_FILES;
|
||||
|
||||
extern size_t max_tile_size;
|
213
man/tippecanoe.1
213
man/tippecanoe.1
@ -1,15 +1,10 @@
|
||||
.TH tippecanoe
|
||||
.PP
|
||||
Builds vector tilesets
|
||||
\[la]https://www.mapbox.com/developers/vector-tiles/\[ra] from large collections of GeoJSON
|
||||
\[la]http://geojson.org/\[ra]
|
||||
features. This is a tool for making maps from huge datasets
|
||||
\[la]MADE_WITH.md\[ra]\&.
|
||||
Builds vector tilesets \[la]https://www.mapbox.com/developers/vector-tiles/\[ra] from large (or small) collections of GeoJSON \[la]http://geojson.org/\[ra] features,
|
||||
like these \[la]MADE_WITH.md\[ra]\&.
|
||||
.PP
|
||||
[Build Status](https://travis\-ci.org/mapbox/tippecanoe.svg)
|
||||
\[la]https://travis-ci.org/mapbox/tippecanoe\[ra]
|
||||
[Coverage Status](https://coveralls.io/repos/mapbox/tippecanoe/badge.svg?branch=master&service=github)
|
||||
\[la]https://coveralls.io/github/mapbox/tippecanoe?branch=master\[ra]
|
||||
[Build Status](https://travis\-ci.org/mapbox/tippecanoe.svg) \[la]https://travis-ci.org/mapbox/tippecanoe\[ra]
|
||||
[Coverage Status](https://coveralls.io/repos/mapbox/tippecanoe/badge.svg?branch=master&service=github) \[la]https://coveralls.io/github/mapbox/tippecanoe?branch=master\[ra]
|
||||
.SH Intent
|
||||
.PP
|
||||
The goal of Tippecanoe is to enable making a scale\-independent view of your data,
|
||||
@ -18,8 +13,7 @@ the density and texture of the data rather than a simplification from dropping
|
||||
supposedly unimportant features or clustering or aggregating them.
|
||||
.PP
|
||||
If you give it all of OpenStreetMap and zoom out, it should give you back
|
||||
something that looks like "All Streets
|
||||
\[la]http://benfry.com/allstreets/map5.html\[ra]"
|
||||
something that looks like "All Streets \[la]http://benfry.com/allstreets/map5.html\[ra]"
|
||||
rather than something that looks like an Interstate road atlas.
|
||||
.PP
|
||||
If you give it all the building footprints in Los Angeles and zoom out
|
||||
@ -32,8 +26,7 @@ see the shape and relative popularity of every point of interest and every
|
||||
significant travel corridor.
|
||||
.SH Installation
|
||||
.PP
|
||||
The easiest way to install tippecanoe on OSX is with Homebrew
|
||||
\[la]http://brew.sh/\[ra]:
|
||||
The easiest way to install tippecanoe on OSX is with Homebrew \[la]http://brew.sh/\[ra]:
|
||||
.PP
|
||||
.RS
|
||||
.nf
|
||||
@ -59,95 +52,141 @@ it encounters.
|
||||
.SS Naming
|
||||
.RS
|
||||
.IP \(bu 2
|
||||
\-l \fIname\fP: Layer name (default "file" if source is file.json or output is file.mbtiles). If there are multiple input files
|
||||
specified, the files are all merged into the single named layer.
|
||||
\-l \fIname\fP or \-\-layer=\fIname\fP: Layer name (default "file" if source is file.json or output is file.mbtiles). If there are multiple input files
|
||||
specified, the files are all merged into the single named layer, even if they try to specify individual names with \-L.
|
||||
.IP \(bu 2
|
||||
\-n \fIname\fP: Human\-readable name (default file.json)
|
||||
\-L \fIname\fP:\fIfile.json\fP or \-\-named\-layer=\fIname\fP:\fIfile.json\fP: Specify layer names for individual files. If your shell supports it, you can use a subshell redirect like \-L \fIname\fP:<(cat dir/*.json) to specify a layer name for the output of streamed input.
|
||||
.IP \(bu 2
|
||||
\-n \fIname\fP or \-\-name=\fIname\fP: Human\-readable name for the tileset (default file.json)
|
||||
.IP \(bu 2
|
||||
\-A \fItext\fP or \-\-attribution=\fItext\fP: Attribution (HTML) to be shown with maps that use data from this tileset.
|
||||
.RE
|
||||
.SS File control
|
||||
.RS
|
||||
.IP \(bu 2
|
||||
\-o \fIfile\fP\&.mbtiles: Name the output file.
|
||||
\-o \fIfile\fP\&.mbtiles or \-\-output=\fIfile\fP\&.mbtiles: Name the output file.
|
||||
.IP \(bu 2
|
||||
\-f: Delete the mbtiles file if it already exists instead of giving an error
|
||||
\-f or \-\-force: Delete the mbtiles file if it already exists instead of giving an error
|
||||
.IP \(bu 2
|
||||
\-F: Proceed (without deleting existing data) if the metadata or tiles table already exists
|
||||
\-F or \-\-allow\-existing: Proceed (without deleting existing data) if the metadata or tiles table already exists
|
||||
or if metadata fields can't be set
|
||||
.IP \(bu 2
|
||||
\-t \fIdirectory\fP: Put the temporary files in \fIdirectory\fP\&.
|
||||
\-t \fIdirectory\fP or \-\-temporary\-directory=\fIdirectory\fP: Put the temporary files in \fIdirectory\fP\&.
|
||||
If you don't specify, it will use \fB\fC/tmp\fR\&.
|
||||
.IP \(bu 2
|
||||
\-P: Use multiple threads to read different parts of each input file at once.
|
||||
\-P or \-\-read\-parallel: Use multiple threads to read different parts of each input file at once.
|
||||
This will only work if the input is line\-delimited JSON with each Feature on its
|
||||
own line, because it knows nothing of the top\-level structure around the Features.
|
||||
own line, because it knows nothing of the top\-level structure around the Features. Spurious "EOF" error
|
||||
messages may result otherwise.
|
||||
Performance will be better if the input is a named file that can be mapped into memory
|
||||
rather than a stream that can only be read sequentially.
|
||||
.RE
|
||||
.SS Zoom levels and resolution
|
||||
.RS
|
||||
.IP \(bu 2
|
||||
\-z \fIzoom\fP: Maxzoom: the highest zoom level for which tiles are generated (default 14)
|
||||
\-z \fIzoom\fP or \-\-maximum\-zoom=\fIzoom\fP: Maxzoom: the highest zoom level for which tiles are generated (default 14)
|
||||
.IP \(bu 2
|
||||
\-Z \fIzoom\fP: Minzoom: the lowest zoom level for which tiles are generated (default 0)
|
||||
\-Z \fIzoom\fP or \-\-minimum\-zoom=\fIzoom\fP: Minzoom: the lowest zoom level for which tiles are generated (default 0)
|
||||
.IP \(bu 2
|
||||
\-B \fIzoom\fP: Base zoom, the level at and above which all points are included in the tiles (default maxzoom).
|
||||
\-B \fIzoom\fP or \-\-base\-zoom=\fIzoom\fP: Base zoom, the level at and above which all points are included in the tiles (default maxzoom).
|
||||
If you use \-Bg, it will guess a zoom level that will keep at most 50,000 features in the densest tile.
|
||||
You can also specify a marker\-width with \-Bg\fIwidth\fP to allow fewer features in the densest tile to
|
||||
compensate for the larger marker.
|
||||
compensate for the larger marker, or \-Bf\fInumber\fP to allow at most \fInumber\fP features in the densest tile.
|
||||
.IP \(bu 2
|
||||
\-d \fIdetail\fP: Detail at max zoom level (default 12, for tile resolution of 4096)
|
||||
\-d \fIdetail\fP or \-\-full\-detail=\fIdetail\fP: Detail at max zoom level (default 12, for tile resolution of 4096)
|
||||
.IP \(bu 2
|
||||
\-D \fIdetail\fP: Detail at lower zoom levels (default 12, for tile resolution of 4096)
|
||||
\-D \fIdetail\fP or \-\-low\-detail=\fIdetail\fP: Detail at lower zoom levels (default 12, for tile resolution of 4096)
|
||||
.IP \(bu 2
|
||||
\-m \fIdetail\fP: Minimum detail that it will try if tiles are too big at regular detail (default 7)
|
||||
\-m \fIdetail\fP or \-\-minimum\-detail=\fIdetail\fP: Minimum detail that it will try if tiles are too big at regular detail (default 7)
|
||||
.IP \(bu 2
|
||||
\-b \fIpixels\fP: Buffer size where features are duplicated from adjacent tiles. Units are "screen pixels"\-\-1/256th of the tile width or height. (default 5)
|
||||
\-b \fIpixels\fP or \-\-buffer=\fIpixels\fP: Buffer size where features are duplicated from adjacent tiles. Units are "screen pixels"\-\-1/256th of the tile width or height. (default 5)
|
||||
.IP \(bu 2
|
||||
\-s \fIprojection\fP or \-\-projection=\fIprojection\fP: Specify the projection of the input data. Currently supported are EPSG:4326 (WGS84, the default) and EPSG:3857 (Web Mercator).
|
||||
.IP \(bu 2
|
||||
\-M \fIbytes\fP or \-\-maximum\-tile\-bytes=\fIbytes\fP: Use the specified number of \fIbytes\fP as the maximum compressed tile size instead of 500K.
|
||||
.RE
|
||||
.PP
|
||||
All internal math is done in terms of a 32\-bit tile coordinate system, so 1/(2 of the size of Earth,
|
||||
or about 1cm, is the smallest distinguishable distance. If \fImaxzoom\fP + \fIdetail\fP > 32, no additional
|
||||
resolution is obtained than by using a smaller \fImaxzoom\fP or \fIdetail\fP\&.
|
||||
.SS Properties
|
||||
.RS
|
||||
.IP \(bu 2
|
||||
\-x \fIname\fP: Exclude the named properties from all features
|
||||
\-x \fIname\fP or \-\-exclude=\fIname\fP: Exclude the named properties from all features
|
||||
.IP \(bu 2
|
||||
\-y \fIname\fP: Include the named properties in all features, excluding all those not explicitly named
|
||||
\-y \fIname\fP or \-\-include=\fIname\fP: Include the named properties in all features, excluding all those not explicitly named
|
||||
.IP \(bu 2
|
||||
\-X: Exclude all properties and encode only geometries
|
||||
\-X or \-\-exclude\-all: Exclude all properties and encode only geometries
|
||||
.RE
|
||||
.SS Point simplification
|
||||
.RS
|
||||
.IP \(bu 2
|
||||
\-r \fIrate\fP: Rate at which dots are dropped at zoom levels below basezoom (default 2.5).
|
||||
\-r \fIrate\fP or \-\-drop\-rate=\fIrate\fP: Rate at which dots are dropped at zoom levels below basezoom (default 2.5).
|
||||
If you use \-rg, it will guess a drop rate that will keep at most 50,000 features in the densest tile.
|
||||
You can also specify a marker\-width with \-rg\fIwidth\fP to allow fewer features in the densest tile to
|
||||
compensate for the larger marker, or \-rf\fInumber\fP to allow at most \fInumber\fP features in the densest tile.
|
||||
.IP \(bu 2
|
||||
\-g \fIgamma\fP: Rate at which especially dense dots are dropped (default 0, for no effect). A gamma of 2 reduces the number of dots less than a pixel apart to the square root of their original number.
|
||||
\-g \fIgamma\fP or \-\-gamma=\fIgamma\fP: Rate at which especially dense dots are dropped (default 0, for no effect). A gamma of 2 reduces the number of dots less than a pixel apart to the square root of their original number.
|
||||
.RE
|
||||
.SS Line and polygon simplification
|
||||
.RS
|
||||
.IP \(bu 2
|
||||
\-S \fIscale\fP or \-\-simplification=\fIscale\fP: Multiply the tolerance for line and polygon simplification by \fIscale\fP\&. The standard tolerance tries to keep
|
||||
the line or polygon within one tile unit of its proper location. You can probably go up to about 10 without too much visible difference.
|
||||
.RE
|
||||
.SS Doing more
|
||||
.RS
|
||||
.IP \(bu 2
|
||||
\-ac: Coalesce adjacent line and polygon features that have the same properties
|
||||
\-ac or \-\-coalesce: Coalesce adjacent line and polygon features that have the same properties.
|
||||
Note that when overlapping polygons are coalesced, the overlapping region is treated as a hole,
|
||||
which may not be what you want.
|
||||
.IP \(bu 2
|
||||
\-ar: Try reversing the directions of lines to make them coalesce and compress better
|
||||
\-ar or \-\-reverse: Try reversing the directions of lines to make them coalesce and compress better
|
||||
.IP \(bu 2
|
||||
\-ao: Reorder features to put ones with the same properties in sequence, to try to get them to coalesce
|
||||
\-ao or \-\-reorder: Reorder features to put ones with the same properties in sequence, to try to get them to coalesce
|
||||
.IP \(bu 2
|
||||
\-al: Let "dot" dropping at lower zooms apply to lines too
|
||||
\-al or \-\-drop\-lines: Let "dot" dropping at lower zooms apply to lines too
|
||||
.IP \(bu 2
|
||||
\-ap or \-\-drop\-polygons: Let "dot" dropping at lower zooms apply to polygons too
|
||||
.IP \(bu 2
|
||||
\-ag or \-\-calculate\-feature\-density: Add a new attribute, \fB\fCtippecanoe_feature_density\fR, to each feature, to record how densely features are spaced in that area of the tile. You can use this attribute in the style to produce a glowing effect where points are densely packed. It can range from 0 in the sparsest areas to 255 in the densest.
|
||||
.IP \(bu 2
|
||||
\-ab or \-\-detect\-shared\-borders: In the manner of TopoJSON \[la]https://github.com/mbostock/topojson/wiki/Introduction\[ra], detect borders that are shared between multiple polygons and simplify them identically in each polygon. This takes more time and memory than considering each polygon individually.
|
||||
.IP \(bu 2
|
||||
\-aG or \-\-increase\-gamma\-as\-needed: If a tile is too large, try to reduce it to under 500K by increasing the \fB\fC\-g\fR gamma. The discovered gamma applies to the entire zoom level.
|
||||
.IP \(bu 2
|
||||
\-as or \-\-drop\-densest\-as\-needed: If a tile is too large, try to reduce it to under 500K by increasing the minimum spacing between features. The discovered spacing applies to the entire zoom level.
|
||||
.IP \(bu 2
|
||||
\-ad or \-\-drop\-fraction\-as\-needed: Dynamically drop some fraction of features from each zoom level to keep large tiles under the 500K size limit. (This is like \fB\fC\-pd\fR but applies to the entire zoom level, not to each tile.)
|
||||
.IP \(bu 2
|
||||
\-an or \-\-drop\-smallest\-as\-needed: Dynamically drop the smallest features (physically smallest: the shortest lines or the smallest polygons) from each zoom level to keep large tiles under the 500K size limit. This option will not work for point features.
|
||||
.IP \(bu 2
|
||||
\-aL or \-\-grid\-low\-zooms: At all zoom levels below \fImaxzoom\fP, snap all lines and polygons to a stairstep grid instead of allowing diagonals. You will also want to specify a tile resolution, probably \fB\fC\-D8\fR\&. This option provides a way to display continuous parcel, gridded, or binned data at low zooms without overwhelming the tiles with tiny polygons, since features will either get stretched out to the grid unit or lost entirely, depending on how they happened to be aligned in the original data.
|
||||
.RE
|
||||
.SS Doing less
|
||||
.RS
|
||||
.IP \(bu 2
|
||||
\-ps: Don't simplify lines
|
||||
\-ps or \-\-no\-line\-simplification: Don't simplify lines
|
||||
.IP \(bu 2
|
||||
\-pS: Don't simplify lines at maxzoom (but do simplify at lower zooms)
|
||||
\-pS or \-\-simplify\-only\-low\-zooms: Don't simplify lines at maxzoom (but do simplify at lower zooms)
|
||||
.IP \(bu 2
|
||||
\-pf: Don't limit tiles to 200,000 features
|
||||
\-pf or \-\-no\-feature\-limit: Don't limit tiles to 200,000 features
|
||||
.IP \(bu 2
|
||||
\-pk: Don't limit tiles to 500K bytes
|
||||
\-pk or \-\-no\-tile\-size\-limit: Don't limit tiles to 500K bytes
|
||||
.IP \(bu 2
|
||||
\-pd: Dynamically drop some fraction of features from large tiles to keep them under the 500K size limit. It will probably look ugly at the tile boundaries.
|
||||
\-pd or \-\-force\-feature\-limit: Dynamically drop some fraction of features from large tiles to keep them under the 500K size limit. It will probably look ugly at the tile boundaries. (This is like \fB\fC\-ad\fR but applies to each tile individually, not to the entire zoom level.)
|
||||
.IP \(bu 2
|
||||
\-pi: Preserve the original input order of features as the drawing order instead of ordering geographically. (This is implemented as a restoration of the original order at the end, so that dot\-dropping is still geographic, which means it also undoes \-ao).
|
||||
\-pi or \-\-preserve\-input\-order: Preserve the original input order of features as the drawing order instead of ordering geographically. (This is implemented as a restoration of the original order at the end, so that dot\-dropping is still geographic, which means it also undoes \-ao).
|
||||
.IP \(bu 2
|
||||
\-pp: Don't split complex polygons (over 700 vertices after simplification) into multiple features.
|
||||
\-pp or \-\-no\-polygon\-splitting: Don't split complex polygons (over 700 vertices after simplification) into multiple features.
|
||||
.IP \(bu 2
|
||||
\-q: Work quietly instead of reporting progress
|
||||
\-pc or \-\-no\-clipping: Don't clip features to the size of the tile. If a feature overlaps the tile's bounds or buffer at all, it is included completely. Be careful: this can produce very large tilesets, especially with large polygons.
|
||||
.IP \(bu 2
|
||||
\-pD or \-\-no\-duplication: As with \-\-no\-clipping, each feature is included intact instead of cut to tile boundaries. In addition, it is included only in a single tile per zoom level rather than potentially in multiple copies. Clients of the tileset must check adjacent tiles (possibly some distance away) to ensure they have all features.
|
||||
.IP \(bu 2
|
||||
\-pt or \-\-no\-tiny\-polygon\-reduction: Don't combine the area of very small polygons into small squares that represent their combined area.
|
||||
.IP \(bu 2
|
||||
\-q or \-\-quiet: Work quietly instead of reporting progress
|
||||
.RE
|
||||
.SH Example
|
||||
.PP
|
||||
@ -165,7 +204,7 @@ $ cat tiger/tl_2014_*_roads.json | tippecanoe \-o tiger.mbtiles \-l roads \-n "A
|
||||
.SH GeoJSON extension
|
||||
.PP
|
||||
Tippecanoe defines a GeoJSON extension that you can use to specify the minimum and/or maximum zoom level
|
||||
at which an individual feature will be included in the vector tile dataset being produced.
|
||||
at which an individual feature will be included in the vector tileset being produced.
|
||||
If you have a feature like this:
|
||||
.PP
|
||||
.RS
|
||||
@ -184,7 +223,26 @@ If you have a feature like this:
|
||||
.PP
|
||||
with a \fB\fCtippecanoe\fR object specifiying a \fB\fCmaxzoom\fR of 9 and a \fB\fCminzoom\fR of 4, the feature
|
||||
will only appear in the vector tiles for zoom levels 4 through 9. Note that the \fB\fCtippecanoe\fR
|
||||
object belongs to the Feature, not to its \fB\fCproperties\fR\&.
|
||||
object belongs to the Feature, not to its \fB\fCproperties\fR\&. If you specify a \fB\fCminzoom\fR for a feature,
|
||||
it will be preserved down to that zoom level even if dot\-dropping with \fB\fC\-r\fR would otherwise have
|
||||
dropped it.
|
||||
.PP
|
||||
You can also specify a layer name in the \fB\fCtippecanoe\fR object, which will take precedence over
|
||||
the filename or name specified using \fB\fC\-\-layer\fR, like this:
|
||||
.PP
|
||||
.RS
|
||||
.nf
|
||||
{
|
||||
"type" : "Feature",
|
||||
"tippecanoe" : { "layer" : "streets" },
|
||||
"properties" : { "FULLNAME" : "N Vasco Rd" },
|
||||
"geometry" : {
|
||||
"type" : "LineString",
|
||||
"coordinates" : [ [ \-121.733350, 37.767671 ], [ \-121.733600, 37.767483 ], [ \-121.733131, 37.766952 ] ]
|
||||
}
|
||||
}
|
||||
.fi
|
||||
.RE
|
||||
.SH Point styling
|
||||
.PP
|
||||
To provide a consistent density gradient as you zoom, the Mapbox Studio style needs to be
|
||||
@ -201,6 +259,7 @@ awk 'BEGIN {
|
||||
dotsize = 2; # up to you to decide
|
||||
basezoom = 14; # tippecanoe \-z 14
|
||||
rate = 2.5; # tippecanoe \-r 2.5
|
||||
|
||||
print " marker\-line\-width: 0;";
|
||||
print " marker\-ignore\-placement: true;";
|
||||
print " marker\-allow\-overlap: true;";
|
||||
@ -208,6 +267,7 @@ awk 'BEGIN {
|
||||
for (i = basezoom + 1; i <= 22; i++) {
|
||||
print " [zoom >= " i "] { marker\-width: " (dotsize * exp(log(sqrt(rate)) * (i \- basezoom))) "; }";
|
||||
}
|
||||
|
||||
exit(0);
|
||||
}'
|
||||
.fi
|
||||
@ -231,7 +291,8 @@ For line features, it drops any features that are too small to draw at all.
|
||||
This still leaves the lower zooms too dark (and too dense for the 500K tile limit,
|
||||
in some places), so I need to figure out an equitable way to throw features away.
|
||||
.PP
|
||||
Any polygons that are smaller than a minimum area (currently 4 square subpixels) will
|
||||
Unless you specify \fB\fC\-\-no\-tiny\-polygon\-reduction\fR,
|
||||
any polygons that are smaller than a minimum area (currently 4 square subpixels) will
|
||||
have their probability diffused, so that some of them will be drawn as a square of
|
||||
this minimum size and others will not be drawn at all, preserving the total area that
|
||||
all of them should have had together.
|
||||
@ -240,31 +301,21 @@ Any polygons that have over 700 vertices after line simplification will be split
|
||||
multiple features so they can be rendered efficiently, unless you use \-pp to prevent this.
|
||||
.PP
|
||||
Features in the same tile that share the same type and attributes are coalesced
|
||||
together into a single geometry. You are strongly encouraged to use \-x to exclude
|
||||
together into a single geometry if you use \fB\fC\-\-coalesce\fR\&. You are strongly encouraged to use \-x to exclude
|
||||
any unnecessary properties to reduce wasted file size.
|
||||
.PP
|
||||
If a tile is larger than 500K, it will try encoding that tile at progressively
|
||||
lower resolutions before failing if it still doesn't fit.
|
||||
.SH Development
|
||||
.PP
|
||||
Requires protoc and sqlite3. Rebuilding the manpage
|
||||
Requires sqlite3 and zlib (should already be installed on MacOS). Rebuilding the manpage
|
||||
uses md2man (\fB\fCgem install md2man\fR).
|
||||
.PP
|
||||
MacOS:
|
||||
.PP
|
||||
.RS
|
||||
.nf
|
||||
brew install protobuf
|
||||
.fi
|
||||
.RE
|
||||
.PP
|
||||
Linux:
|
||||
.PP
|
||||
.RS
|
||||
.nf
|
||||
sudo apt\-get install libprotobuf\-dev
|
||||
sudo apt\-get install protobuf\-compiler
|
||||
sudo apt\-get install libsqlite3\-dev
|
||||
sudo apt\-get install build\-essential libsqlite3\-dev zlib1g\-dev
|
||||
.fi
|
||||
.RE
|
||||
.PP
|
||||
@ -283,20 +334,35 @@ and perhaps
|
||||
make install
|
||||
.fi
|
||||
.RE
|
||||
.PP
|
||||
Tippecanoe now requires features from the 2014 C++ standard. If your compiler is older than
|
||||
that, you will need to install a newer one. On MacOS, updating to the lastest XCode should
|
||||
get you a new enough version of \fB\fCclang++\fR\&. On Linux, you should be able to upgrade \fB\fCg++\fR with
|
||||
.PP
|
||||
.RS
|
||||
.nf
|
||||
sudo add\-apt\-repository \-y ppa:ubuntu\-toolchain\-r/test
|
||||
sudo apt\-get update \-y
|
||||
sudo apt\-get install \-y g++\-5
|
||||
export CXX=g++\-5
|
||||
.fi
|
||||
.RE
|
||||
.SH Examples
|
||||
.PP
|
||||
Check out some examples of maps made with tippecanoe
|
||||
\[la]MADE_WITH.md\[ra]
|
||||
Check out some examples of maps made with tippecanoe \[la]MADE_WITH.md\[ra]
|
||||
.SH Name
|
||||
.PP
|
||||
The name is a joking reference
|
||||
\[la]http://en.wikipedia.org/wiki/Tippecanoe_and_Tyler_Too\[ra] to a "tiler" for making map tiles.
|
||||
The name is a joking reference \[la]http://en.wikipedia.org/wiki/Tippecanoe_and_Tyler_Too\[ra] to a "tiler" for making map tiles.
|
||||
.SH tile\-join
|
||||
.PP
|
||||
Tile\-join is a tool for joining new attributes from a CSV file to features that
|
||||
have already been tiled with tippecanoe. It reads the tiles from an existing .mbtiles
|
||||
file, matches them against the records of the CSV, and writes out a new tileset.
|
||||
.PP
|
||||
If you specify multiple source mbtiles files, they are all read and their combined
|
||||
contents are written to the new mbtiles output. If they define the same layers or
|
||||
the same tiles, the layers or tiles are merged.
|
||||
.PP
|
||||
The options are:
|
||||
.RS
|
||||
.IP \(bu 2
|
||||
@ -309,11 +375,14 @@ The options are:
|
||||
\-x \fIkey\fP: Remove attributes of type \fIkey\fP from the output. You can use this to remove the field you are matching against if you no longer need it after joining, or to remove any other attributes you don't want.
|
||||
.IP \(bu 2
|
||||
\-i: Only include features that matched the CSV.
|
||||
.IP \(bu 2
|
||||
\-pk: Don't skip tiles larger than 500K.
|
||||
.RE
|
||||
.PP
|
||||
Because tile\-join just copies the geometries to the new .mbtiles without processing them,
|
||||
Because tile\-join just copies the geometries to the new .mbtiles without processing them
|
||||
(except to rescale the extents if necessary),
|
||||
it doesn't have any of tippecanoe's recourses if the new tiles are bigger than the 500K tile limit.
|
||||
If a tile is too big, it is just left out of the new tileset.
|
||||
If a tile is too big and you haven't specified \fB\fC\-pk\fR, it is just left out of the new tileset.
|
||||
.SH Example
|
||||
.PP
|
||||
Imagine you have a tileset of census blocks:
|
||||
@ -395,6 +464,7 @@ or on an individual tile:
|
||||
.RS
|
||||
.nf
|
||||
tippecanoe\-decode file.mbtiles zoom x y
|
||||
tippecanoe\-decode file.vector.pbf zoom x y
|
||||
.fi
|
||||
.RE
|
||||
.PP
|
||||
@ -402,3 +472,8 @@ If you decode an entire file, you get a nested \fB\fCFeatureCollection\fR identi
|
||||
tile and layer separately. Note that the same features generally appear at all zooms,
|
||||
so the output for the file will have many copies of the same features at different
|
||||
resolutions.
|
||||
.SS Options
|
||||
.RS
|
||||
.IP \(bu 2
|
||||
\-t \fIprojection\fP: Specify the projection of the output data. Currently supported are EPSG:4326 (WGS84, the default) and EPSG:3857 (Web Mercator).
|
||||
.RE
|
||||
|
13
mapbox/LICENSE-geometry.hpp
Normal file
13
mapbox/LICENSE-geometry.hpp
Normal file
@ -0,0 +1,13 @@
|
||||
Copyright (c) 2016, Mapbox
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
25
mapbox/LICENSE-variant
Normal file
25
mapbox/LICENSE-variant
Normal file
@ -0,0 +1,25 @@
|
||||
Copyright (c) MapBox
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
- Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
- Redistributions in binary form must reproduce the above copyright notice, this
|
||||
list of conditions and the following disclaimer in the documentation and/or
|
||||
other materials provided with the distribution.
|
||||
- Neither the name "MapBox" nor the names of its contributors may be
|
||||
used to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
37
mapbox/LICENSE-wagyu
Normal file
37
mapbox/LICENSE-wagyu
Normal file
@ -0,0 +1,37 @@
|
||||
Parts of the code in the Wagyu Library are derived from the version of the
|
||||
Clipper Library by Angus Johnson listed below.
|
||||
|
||||
Author : Angus Johnson
|
||||
Version : 6.4.0
|
||||
Date : 2 July 2015
|
||||
Website : http://www.angusj.com
|
||||
|
||||
Copyright for portions of the derived code in the Wagyu library are held
|
||||
by Angus Johnson, 2010-2015. All other copyright for the Wagyu Library are held by
|
||||
Mapbox, 2016. This code is published in accordance with, and retains the same license
|
||||
as the Clipper Library by Angus Johnson.
|
||||
|
||||
Copyright (c) 2010-2015, Angus Johnson
|
||||
Copyright (c) 2016, Mapbox
|
||||
|
||||
Permission is hereby granted, free of charge, to any person or organization
|
||||
obtaining a copy of the software and accompanying documentation covered by
|
||||
this license (the "Software") to use, reproduce, display, distribute,
|
||||
execute, and transmit the Software, and to prepare derivative works of the
|
||||
Software, and to permit third-parties to whom the Software is furnished to
|
||||
do so, all subject to the following:
|
||||
|
||||
The copyright notices in the Software and this entire statement, including
|
||||
the above license grant, this restriction and the following disclaimer,
|
||||
must be included in all copies of the Software, in whole or in part, and
|
||||
all derivative works of the Software, unless such copies or derivative
|
||||
works are solely in the form of machine-executable object code generated by
|
||||
a source language processor.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
13
mapbox/geometry.hpp
Normal file
13
mapbox/geometry.hpp
Normal file
@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <mapbox/geometry/point.hpp>
|
||||
#include <mapbox/geometry/line_string.hpp>
|
||||
#include <mapbox/geometry/polygon.hpp>
|
||||
#include <mapbox/geometry/multi_point.hpp>
|
||||
#include <mapbox/geometry/multi_line_string.hpp>
|
||||
#include <mapbox/geometry/multi_polygon.hpp>
|
||||
#include <mapbox/geometry/geometry.hpp>
|
||||
#include <mapbox/geometry/feature.hpp>
|
||||
#include <mapbox/geometry/point_arithmetic.hpp>
|
||||
#include <mapbox/geometry/for_each_point.hpp>
|
||||
#include <mapbox/geometry/envelope.hpp>
|
34
mapbox/geometry/box.hpp
Normal file
34
mapbox/geometry/box.hpp
Normal file
@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include <mapbox/geometry/point.hpp>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
|
||||
template <typename T>
|
||||
struct box
|
||||
{
|
||||
using point_type = point<T>;
|
||||
|
||||
constexpr box(point_type const& min_, point_type const& max_)
|
||||
: min(min_), max(max_)
|
||||
{}
|
||||
|
||||
point_type min;
|
||||
point_type max;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
constexpr bool operator==(box<T> const& lhs, box<T> const& rhs)
|
||||
{
|
||||
return lhs.min == rhs.min && lhs.max == rhs.max;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr bool operator!=(box<T> const& lhs, box<T> const& rhs)
|
||||
{
|
||||
return lhs.min != rhs.min || lhs.max != rhs.max;
|
||||
}
|
||||
|
||||
} // namespace geometry
|
||||
} // namespace mapbox
|
33
mapbox/geometry/envelope.hpp
Normal file
33
mapbox/geometry/envelope.hpp
Normal file
@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <mapbox/geometry/box.hpp>
|
||||
#include <mapbox/geometry/for_each_point.hpp>
|
||||
|
||||
#include <limits>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
|
||||
template <typename G, typename T = typename G::coordinate_type>
|
||||
box<T> envelope(G const& geometry)
|
||||
{
|
||||
using limits = std::numeric_limits<T>;
|
||||
|
||||
T min_t = limits::has_infinity ? -limits::infinity() : limits::min();
|
||||
T max_t = limits::has_infinity ? limits::infinity() : limits::max();
|
||||
|
||||
point<T> min(max_t, max_t);
|
||||
point<T> max(min_t, min_t);
|
||||
|
||||
for_each_point(geometry, [&] (point<T> const& point) {
|
||||
if (min.x > point.x) min.x = point.x;
|
||||
if (min.y > point.y) min.y = point.y;
|
||||
if (max.x < point.x) max.x = point.x;
|
||||
if (max.y < point.y) max.y = point.y;
|
||||
});
|
||||
|
||||
return box<T>(min, max);
|
||||
}
|
||||
|
||||
} // namespace geometry
|
||||
} // namespace mapbox
|
81
mapbox/geometry/feature.hpp
Normal file
81
mapbox/geometry/feature.hpp
Normal file
@ -0,0 +1,81 @@
|
||||
#pragma once
|
||||
|
||||
#include <mapbox/geometry/geometry.hpp>
|
||||
|
||||
#include <mapbox/variant.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <experimental/optional>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
|
||||
struct value;
|
||||
|
||||
struct null_value_t
|
||||
{
|
||||
constexpr null_value_t() {}
|
||||
constexpr null_value_t(std::nullptr_t) {}
|
||||
};
|
||||
|
||||
constexpr bool operator==(const null_value_t&, const null_value_t&) { return true; }
|
||||
constexpr bool operator!=(const null_value_t&, const null_value_t&) { return false; }
|
||||
|
||||
constexpr null_value_t null_value = null_value_t();
|
||||
|
||||
// Multiple numeric types (uint64_t, int64_t, double) are present in order to support
|
||||
// the widest possible range of JSON numbers, which do not have a maximum range.
|
||||
// Implementations that produce `value`s should use that order for type preference,
|
||||
// using uint64_t for positive integers, int64_t for negative integers, and double
|
||||
// for non-integers and integers outside the range of 64 bits.
|
||||
using value_base = mapbox::util::variant<null_value_t, bool, uint64_t, int64_t, double, std::string,
|
||||
mapbox::util::recursive_wrapper<std::vector<value>>,
|
||||
mapbox::util::recursive_wrapper<std::unordered_map<std::string, value>>>;
|
||||
|
||||
struct value : value_base
|
||||
{
|
||||
using value_base::value_base;
|
||||
};
|
||||
|
||||
using property_map = std::unordered_map<std::string, value>;
|
||||
|
||||
// The same considerations and requirement for numeric types apply as for `value_base`.
|
||||
using identifier = mapbox::util::variant<uint64_t, int64_t, double, std::string>;
|
||||
|
||||
template <class T>
|
||||
struct feature
|
||||
{
|
||||
using coordinate_type = T;
|
||||
using geometry_type = mapbox::geometry::geometry<T>; // Fully qualified to avoid GCC -fpermissive error.
|
||||
|
||||
geometry_type geometry;
|
||||
property_map properties {};
|
||||
std::experimental::optional<identifier> id {};
|
||||
};
|
||||
|
||||
template <class T>
|
||||
constexpr bool operator==(feature<T> const& lhs, feature<T> const& rhs)
|
||||
{
|
||||
return lhs.id == rhs.id && lhs.geometry == rhs.geometry && lhs.properties == rhs.properties;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
constexpr bool operator!=(feature<T> const& lhs, feature<T> const& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
template <class T, template <typename...> class Cont = std::vector>
|
||||
struct feature_collection : Cont<feature<T>>
|
||||
{
|
||||
using coordinate_type = T;
|
||||
using feature_type = feature<T>;
|
||||
using container_type = Cont<feature_type>;
|
||||
using container_type::container_type;
|
||||
};
|
||||
|
||||
} // namespace geometry
|
||||
} // namespace mapbox
|
45
mapbox/geometry/for_each_point.hpp
Normal file
45
mapbox/geometry/for_each_point.hpp
Normal file
@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include <mapbox/geometry/geometry.hpp>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
|
||||
template <typename Point, typename F>
|
||||
auto for_each_point(Point&& point, F&& f)
|
||||
-> decltype(point.x, point.y, void())
|
||||
{
|
||||
f(std::forward<Point>(point));
|
||||
}
|
||||
|
||||
template <typename Container, typename F>
|
||||
auto for_each_point(Container&& container, F&& f)
|
||||
-> decltype(container.begin(), container.end(), void());
|
||||
|
||||
template <typename...Types, typename F>
|
||||
void for_each_point(mapbox::util::variant<Types...> const& geom, F&& f)
|
||||
{
|
||||
mapbox::util::variant<Types...>::visit(geom, [&] (auto const& g) {
|
||||
for_each_point(g, f);
|
||||
});
|
||||
}
|
||||
|
||||
template <typename...Types, typename F>
|
||||
void for_each_point(mapbox::util::variant<Types...> & geom, F&& f)
|
||||
{
|
||||
mapbox::util::variant<Types...>::visit(geom, [&] (auto & g) {
|
||||
for_each_point(g, f);
|
||||
});
|
||||
}
|
||||
|
||||
template <typename Container, typename F>
|
||||
auto for_each_point(Container&& container, F&& f)
|
||||
-> decltype(container.begin(), container.end(), void())
|
||||
{
|
||||
for (auto& e: container) {
|
||||
for_each_point(e, f);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace geometry
|
||||
} // namespace mapbox
|
53
mapbox/geometry/geometry.hpp
Normal file
53
mapbox/geometry/geometry.hpp
Normal file
@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#include <mapbox/geometry/point.hpp>
|
||||
#include <mapbox/geometry/line_string.hpp>
|
||||
#include <mapbox/geometry/polygon.hpp>
|
||||
#include <mapbox/geometry/multi_point.hpp>
|
||||
#include <mapbox/geometry/multi_line_string.hpp>
|
||||
#include <mapbox/geometry/multi_polygon.hpp>
|
||||
|
||||
#include <mapbox/variant.hpp>
|
||||
|
||||
// stl
|
||||
#include <vector>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
|
||||
template <typename T, template <typename...> class Cont = std::vector>
|
||||
struct geometry_collection;
|
||||
|
||||
template <typename T>
|
||||
using geometry_base = mapbox::util::variant<point<T>,
|
||||
line_string<T>,
|
||||
polygon<T>,
|
||||
multi_point<T>,
|
||||
multi_line_string<T>,
|
||||
multi_polygon<T>,
|
||||
geometry_collection<T>>;
|
||||
|
||||
template <typename T>
|
||||
struct geometry : geometry_base<T>
|
||||
{
|
||||
using coordinate_type = T;
|
||||
using geometry_base<T>::geometry_base;
|
||||
|
||||
/*
|
||||
* The default constructor would create a point geometry with default-constructed coordinates;
|
||||
* i.e. (0, 0). Since this is not particularly useful, and could hide bugs, it is disabled.
|
||||
*/
|
||||
geometry() = delete;
|
||||
};
|
||||
|
||||
template <typename T, template <typename...> class Cont>
|
||||
struct geometry_collection : Cont<geometry<T>>
|
||||
{
|
||||
using coordinate_type = T;
|
||||
using geometry_type = geometry<T>;
|
||||
using container_type = Cont<geometry_type>;
|
||||
using container_type::container_type;
|
||||
};
|
||||
|
||||
} // namespace geometry
|
||||
} // namespace mapbox
|
21
mapbox/geometry/line_string.hpp
Normal file
21
mapbox/geometry/line_string.hpp
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
// mapbox
|
||||
#include <mapbox/geometry/point.hpp>
|
||||
// stl
|
||||
#include <vector>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
|
||||
template <typename T, template <typename...> class Cont = std::vector>
|
||||
struct line_string : Cont<point<T> >
|
||||
{
|
||||
using coordinate_type = T;
|
||||
using point_type = point<T>;
|
||||
using container_type = Cont<point_type>;
|
||||
using container_type::container_type;
|
||||
};
|
||||
|
||||
} // namespace geometry
|
||||
} // namespace mapbox
|
21
mapbox/geometry/multi_line_string.hpp
Normal file
21
mapbox/geometry/multi_line_string.hpp
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
// mapbox
|
||||
#include <mapbox/geometry/line_string.hpp>
|
||||
// stl
|
||||
#include <vector>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
|
||||
template <typename T, template <typename...> class Cont = std::vector>
|
||||
struct multi_line_string : Cont<line_string<T>>
|
||||
{
|
||||
using coordinate_type = T;
|
||||
using line_string_type = line_string<T>;
|
||||
using container_type = Cont<line_string_type>;
|
||||
using container_type::container_type;
|
||||
};
|
||||
|
||||
} // namespace geometry
|
||||
} // namespace mapbox
|
21
mapbox/geometry/multi_point.hpp
Normal file
21
mapbox/geometry/multi_point.hpp
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
// mapbox
|
||||
#include <mapbox/geometry/point.hpp>
|
||||
// stl
|
||||
#include <vector>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
|
||||
template <typename T, template <typename...> class Cont = std::vector>
|
||||
struct multi_point : Cont<point<T>>
|
||||
{
|
||||
using coordinate_type = T;
|
||||
using point_type = point<T>;
|
||||
using container_type = Cont<point_type>;
|
||||
using container_type::container_type;
|
||||
};
|
||||
|
||||
} // namespace geometry
|
||||
} // namespace mapbox
|
21
mapbox/geometry/multi_polygon.hpp
Normal file
21
mapbox/geometry/multi_polygon.hpp
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
// mapbox
|
||||
#include <mapbox/geometry/polygon.hpp>
|
||||
// stl
|
||||
#include <vector>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
|
||||
template <typename T, template <typename...> class Cont = std::vector>
|
||||
struct multi_polygon : Cont<polygon<T>>
|
||||
{
|
||||
using coordinate_type = T;
|
||||
using polygon_type = polygon<T>;
|
||||
using container_type = Cont<polygon_type>;
|
||||
using container_type::container_type;
|
||||
};
|
||||
|
||||
} // namespace geometry
|
||||
} // namespace mapbox
|
35
mapbox/geometry/point.hpp
Normal file
35
mapbox/geometry/point.hpp
Normal file
@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
|
||||
template <typename T>
|
||||
struct point
|
||||
{
|
||||
using coordinate_type = T;
|
||||
|
||||
constexpr point()
|
||||
: x(), y()
|
||||
{}
|
||||
constexpr point(T x_, T y_)
|
||||
: x(x_), y(y_)
|
||||
{}
|
||||
|
||||
T x;
|
||||
T y;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
constexpr bool operator==(point<T> const& lhs, point<T> const& rhs)
|
||||
{
|
||||
return lhs.x == rhs.x && lhs.y == rhs.y;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr bool operator!=(point<T> const& lhs, point<T> const& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
} // namespace geometry
|
||||
} // namespace mapbox
|
119
mapbox/geometry/point_arithmetic.hpp
Normal file
119
mapbox/geometry/point_arithmetic.hpp
Normal file
@ -0,0 +1,119 @@
|
||||
#pragma once
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
|
||||
template <typename T>
|
||||
constexpr point<T> operator+(point<T> const& lhs, point<T> const& rhs)
|
||||
{
|
||||
return point<T>(lhs.x + rhs.x, lhs.y + rhs.y);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr point<T> operator+(point<T> const& lhs, T const& rhs)
|
||||
{
|
||||
return point<T>(lhs.x + rhs, lhs.y + rhs);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr point<T> operator-(point<T> const& lhs, point<T> const& rhs)
|
||||
{
|
||||
return point<T>(lhs.x - rhs.x, lhs.y - rhs.y);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr point<T> operator-(point<T> const& lhs, T const& rhs)
|
||||
{
|
||||
return point<T>(lhs.x - rhs, lhs.y - rhs);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr point<T> operator*(point<T> const& lhs, point<T> const& rhs)
|
||||
{
|
||||
return point<T>(lhs.x * rhs.x, lhs.y * rhs.y);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr point<T> operator*(point<T> const& lhs, T const& rhs)
|
||||
{
|
||||
return point<T>(lhs.x * rhs, lhs.y * rhs);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr point<T> operator/(point<T> const& lhs, point<T> const& rhs)
|
||||
{
|
||||
return point<T>(lhs.x / rhs.x, lhs.y / rhs.y);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr point<T> operator/(point<T> const& lhs, T const& rhs)
|
||||
{
|
||||
return point<T>(lhs.x / rhs, lhs.y / rhs);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr point<T>& operator+=(point<T>& lhs, point<T> const& rhs)
|
||||
{
|
||||
lhs.x += rhs.x;
|
||||
lhs.y += rhs.y;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr point<T>& operator+=(point<T>& lhs, T const& rhs)
|
||||
{
|
||||
lhs.x += rhs;
|
||||
lhs.y += rhs;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr point<T>& operator-=(point<T>& lhs, point<T> const& rhs)
|
||||
{
|
||||
lhs.x -= rhs.x;
|
||||
lhs.y -= rhs.y;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr point<T>& operator-=(point<T>& lhs, T const& rhs)
|
||||
{
|
||||
lhs.x -= rhs;
|
||||
lhs.y -= rhs;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr point<T>& operator*=(point<T>& lhs, point<T> const& rhs)
|
||||
{
|
||||
lhs.x *= rhs.x;
|
||||
lhs.y *= rhs.y;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr point<T>& operator*=(point<T>& lhs, T const& rhs)
|
||||
{
|
||||
lhs.x *= rhs;
|
||||
lhs.y *= rhs;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr point<T>& operator/=(point<T>& lhs, point<T> const& rhs)
|
||||
{
|
||||
lhs.x /= rhs.x;
|
||||
lhs.y /= rhs.y;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr point<T>& operator/=(point<T>& lhs, T const& rhs)
|
||||
{
|
||||
lhs.x /= rhs;
|
||||
lhs.y /= rhs;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
} // namespace geometry
|
||||
} // namespace mapbox
|
31
mapbox/geometry/polygon.hpp
Normal file
31
mapbox/geometry/polygon.hpp
Normal file
@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
// mapbox
|
||||
#include <mapbox/geometry/point.hpp>
|
||||
|
||||
// stl
|
||||
#include <vector>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
|
||||
template <typename T, template <typename...> class Cont = std::vector>
|
||||
struct linear_ring : Cont<point<T>>
|
||||
{
|
||||
using coordinate_type = T;
|
||||
using point_type = point<T>;
|
||||
using container_type = Cont<point_type>;
|
||||
using container_type::container_type;
|
||||
};
|
||||
|
||||
template <typename T, template <typename...> class Cont = std::vector>
|
||||
struct polygon : Cont<linear_ring<T>>
|
||||
{
|
||||
using coordinate_type = T;
|
||||
using linear_ring_type = linear_ring<T>;
|
||||
using container_type = Cont<linear_ring_type>;
|
||||
using container_type::container_type;
|
||||
};
|
||||
|
||||
} // namespace geometry
|
||||
} // namespace mapbox
|
425
mapbox/geometry/wagyu/active_bound_list.hpp
Normal file
425
mapbox/geometry/wagyu/active_bound_list.hpp
Normal file
@ -0,0 +1,425 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef DEBUG
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#endif
|
||||
|
||||
#include <mapbox/geometry/wagyu/bound.hpp>
|
||||
#include <mapbox/geometry/wagyu/config.hpp>
|
||||
#include <mapbox/geometry/wagyu/edge.hpp>
|
||||
#include <mapbox/geometry/wagyu/local_minimum.hpp>
|
||||
#include <mapbox/geometry/wagyu/local_minimum_util.hpp>
|
||||
#include <mapbox/geometry/wagyu/ring.hpp>
|
||||
#include <mapbox/geometry/wagyu/scanbeam.hpp>
|
||||
#include <mapbox/geometry/wagyu/util.hpp>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
|
||||
template <typename T>
|
||||
using active_bound_list = std::list<bound_ptr<T>>;
|
||||
|
||||
template <typename T>
|
||||
using active_bound_list_itr = typename active_bound_list<T>::iterator;
|
||||
|
||||
template <typename T>
|
||||
using active_bound_list_rev_itr = typename active_bound_list<T>::reverse_iterator;
|
||||
|
||||
#ifdef DEBUG
|
||||
|
||||
template <class charT, class traits, typename T>
|
||||
inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& out,
|
||||
const active_bound_list<T>& bnds) {
|
||||
std::size_t c = 0;
|
||||
for (auto const& bnd : bnds) {
|
||||
out << "Index: " << c++ << std::endl;
|
||||
out << *bnd;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::string output_edges(active_bound_list<T> const& bnds) {
|
||||
std::ostringstream out;
|
||||
out << "[";
|
||||
bool first = true;
|
||||
for (auto const& bnd : bnds) {
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
out << ",";
|
||||
}
|
||||
out << "[[" << bnd->current_edge->bot.x << "," << bnd->current_edge->bot.y << "],[";
|
||||
out << bnd->current_edge->top.x << "," << bnd->current_edge->top.y << "]]";
|
||||
}
|
||||
out << "]";
|
||||
return out.str();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
template <typename T>
|
||||
bool is_even_odd_fill_type(bound<T> const& bound,
|
||||
fill_type subject_fill_type,
|
||||
fill_type clip_fill_type) {
|
||||
if (bound.poly_type == polygon_type_subject) {
|
||||
return subject_fill_type == fill_type_even_odd;
|
||||
} else {
|
||||
return clip_fill_type == fill_type_even_odd;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool is_even_odd_alt_fill_type(bound<T> const& bound,
|
||||
fill_type subject_fill_type,
|
||||
fill_type clip_fill_type) {
|
||||
if (bound.poly_type == polygon_type_subject) {
|
||||
return clip_fill_type == fill_type_even_odd;
|
||||
} else {
|
||||
return subject_fill_type == fill_type_even_odd;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool bound2_inserts_before_bound1(bound<T> const& bound1, bound<T> const& bound2) {
|
||||
if (values_are_equal(bound2.current_x, bound1.current_x)) {
|
||||
if (bound2.current_edge->top.y > bound1.current_edge->top.y) {
|
||||
return bound2.current_edge->top.x <
|
||||
get_current_x(*(bound1.current_edge), bound2.current_edge->top.y);
|
||||
} else {
|
||||
return bound1.current_edge->top.x >
|
||||
get_current_x(*(bound2.current_edge), bound1.current_edge->top.y);
|
||||
}
|
||||
} else {
|
||||
return bound2.current_x < bound1.current_x;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
active_bound_list_itr<T> insert_bound_into_ABL(bound<T>& bnd, active_bound_list<T>& active_bounds) {
|
||||
auto itr = active_bounds.begin();
|
||||
while (itr != active_bounds.end() && !bound2_inserts_before_bound1(*(*itr), bnd)) {
|
||||
++itr;
|
||||
}
|
||||
return active_bounds.insert(itr, &bnd);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
active_bound_list_itr<T> insert_bound_into_ABL(bound<T>& bnd,
|
||||
active_bound_list_itr<T> itr,
|
||||
active_bound_list<T>& active_bounds) {
|
||||
while (itr != active_bounds.end() && !bound2_inserts_before_bound1(*(*itr), bnd)) {
|
||||
++itr;
|
||||
}
|
||||
return active_bounds.insert(itr, &bnd);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool is_maxima(bound<T>& bnd, T y) {
|
||||
return bnd.next_edge == bnd.edges.end() && bnd.current_edge->top.y == y;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool is_maxima(active_bound_list_itr<T>& bnd, T y) {
|
||||
return is_maxima(*(*bnd), y);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool is_intermediate(bound<T>& bnd, T y) {
|
||||
return bnd.next_edge != bnd.edges.end() && bnd.current_edge->top.y == y;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool is_intermediate(active_bound_list_itr<T>& bnd, T y) {
|
||||
return is_intermediate(*(*bnd), y);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool current_edge_is_horizontal(active_bound_list_itr<T>& bnd) {
|
||||
return is_horizontal(*((*bnd)->current_edge));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool next_edge_is_horizontal(active_bound_list_itr<T>& bnd) {
|
||||
return is_horizontal(*((*bnd)->next_edge));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void swap_positions_in_ABL(active_bound_list_itr<T>& bnd1,
|
||||
active_bound_list_itr<T>& bnd2,
|
||||
active_bound_list<T>& active_bounds) {
|
||||
if (std::next(bnd2) == bnd1) {
|
||||
active_bounds.splice(bnd2, active_bounds, bnd1);
|
||||
} else {
|
||||
active_bounds.splice(bnd1, active_bounds, bnd2);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void next_edge_in_bound(active_bound_list_itr<T>& bnd, scanbeam_list<T>& scanbeam) {
|
||||
++((*bnd)->current_edge);
|
||||
if ((*bnd)->current_edge != (*bnd)->edges.end()) {
|
||||
++((*bnd)->next_edge);
|
||||
(*bnd)->current_x = static_cast<double>((*bnd)->current_edge->bot.x);
|
||||
if (!current_edge_is_horizontal<T>(bnd)) {
|
||||
scanbeam.push((*bnd)->current_edge->top.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
active_bound_list_itr<T> get_maxima_pair(active_bound_list_itr<T> bnd,
|
||||
active_bound_list<T>& active_bounds) {
|
||||
auto bnd_itr = active_bounds.begin();
|
||||
while (bnd_itr != active_bounds.end()) {
|
||||
if (*bnd_itr == (*bnd)->maximum_bound) {
|
||||
break;
|
||||
}
|
||||
++bnd_itr;
|
||||
}
|
||||
return bnd_itr;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void set_winding_count(active_bound_list_itr<T>& bnd_itr,
|
||||
active_bound_list<T>& active_bounds,
|
||||
fill_type subject_fill_type,
|
||||
fill_type clip_fill_type) {
|
||||
|
||||
auto rev_bnd_itr = active_bound_list_rev_itr<T>(bnd_itr);
|
||||
if (rev_bnd_itr == active_bounds.rend()) {
|
||||
(*bnd_itr)->winding_count = (*bnd_itr)->winding_delta;
|
||||
(*bnd_itr)->winding_count2 = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
// find the edge of the same polytype that immediately preceeds 'edge' in
|
||||
// AEL
|
||||
while (rev_bnd_itr != active_bounds.rend() &&
|
||||
(*rev_bnd_itr)->poly_type != (*bnd_itr)->poly_type) {
|
||||
++rev_bnd_itr;
|
||||
}
|
||||
if (rev_bnd_itr == active_bounds.rend()) {
|
||||
(*bnd_itr)->winding_count = (*bnd_itr)->winding_delta;
|
||||
(*bnd_itr)->winding_count2 = 0;
|
||||
} else if (is_even_odd_fill_type(*(*bnd_itr), subject_fill_type, clip_fill_type)) {
|
||||
// EvenOdd filling ...
|
||||
(*bnd_itr)->winding_count = (*bnd_itr)->winding_delta;
|
||||
(*bnd_itr)->winding_count2 = (*rev_bnd_itr)->winding_count2;
|
||||
} else {
|
||||
// nonZero, Positive or Negative filling ...
|
||||
if ((*rev_bnd_itr)->winding_count * (*rev_bnd_itr)->winding_delta < 0) {
|
||||
// prev edge is 'decreasing' WindCount (WC) toward zero
|
||||
// so we're outside the previous polygon ...
|
||||
if (std::abs(static_cast<int>((*rev_bnd_itr)->winding_count)) > 1) {
|
||||
// outside prev poly but still inside another.
|
||||
// when reversing direction of prev poly use the same WC
|
||||
if ((*rev_bnd_itr)->winding_delta * (*bnd_itr)->winding_delta < 0) {
|
||||
(*bnd_itr)->winding_count = (*rev_bnd_itr)->winding_count;
|
||||
} else {
|
||||
// otherwise continue to 'decrease' WC ...
|
||||
(*bnd_itr)->winding_count =
|
||||
(*rev_bnd_itr)->winding_count + (*bnd_itr)->winding_delta;
|
||||
}
|
||||
} else {
|
||||
// now outside all polys of same polytype so set own WC ...
|
||||
(*bnd_itr)->winding_count = (*bnd_itr)->winding_delta;
|
||||
}
|
||||
} else {
|
||||
// prev edge is 'increasing' WindCount (WC) away from zero
|
||||
// so we're inside the previous polygon ...
|
||||
if ((*rev_bnd_itr)->winding_delta * (*bnd_itr)->winding_delta < 0) {
|
||||
// if wind direction is reversing prev then use same WC
|
||||
(*bnd_itr)->winding_count = (*rev_bnd_itr)->winding_count;
|
||||
} else {
|
||||
// otherwise add to WC ...
|
||||
(*bnd_itr)->winding_count =
|
||||
(*rev_bnd_itr)->winding_count + (*bnd_itr)->winding_delta;
|
||||
}
|
||||
}
|
||||
(*bnd_itr)->winding_count2 = (*rev_bnd_itr)->winding_count2;
|
||||
}
|
||||
|
||||
// update winding_count2 ...
|
||||
auto bnd_itr_forward = rev_bnd_itr.base();
|
||||
if (is_even_odd_alt_fill_type(*(*bnd_itr), subject_fill_type, clip_fill_type)) {
|
||||
// EvenOdd filling ...
|
||||
while (bnd_itr_forward != bnd_itr) {
|
||||
if ((*bnd_itr_forward)->winding_delta != 0) {
|
||||
(*bnd_itr)->winding_count2 = ((*bnd_itr)->winding_count2 == 0 ? 1 : 0);
|
||||
}
|
||||
++bnd_itr_forward;
|
||||
}
|
||||
} else {
|
||||
// nonZero, Positive or Negative filling ...
|
||||
while (bnd_itr_forward != bnd_itr) {
|
||||
(*bnd_itr)->winding_count2 += (*bnd_itr_forward)->winding_delta;
|
||||
++bnd_itr_forward;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool is_contributing(bound<T> const& bnd,
|
||||
clip_type cliptype,
|
||||
fill_type subject_fill_type,
|
||||
fill_type clip_fill_type) {
|
||||
fill_type pft = subject_fill_type;
|
||||
fill_type pft2 = clip_fill_type;
|
||||
if (bnd.poly_type != polygon_type_subject) {
|
||||
pft = clip_fill_type;
|
||||
pft2 = subject_fill_type;
|
||||
}
|
||||
|
||||
switch (pft) {
|
||||
case fill_type_even_odd:
|
||||
break;
|
||||
case fill_type_non_zero:
|
||||
if (std::abs(static_cast<int>(bnd.winding_count)) != 1) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case fill_type_positive:
|
||||
if (bnd.winding_count != 1) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case fill_type_negative:
|
||||
default:
|
||||
if (bnd.winding_count != -1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
switch (cliptype) {
|
||||
case clip_type_intersection:
|
||||
switch (pft2) {
|
||||
case fill_type_even_odd:
|
||||
case fill_type_non_zero:
|
||||
return (bnd.winding_count2 != 0);
|
||||
case fill_type_positive:
|
||||
return (bnd.winding_count2 > 0);
|
||||
case fill_type_negative:
|
||||
default:
|
||||
return (bnd.winding_count2 < 0);
|
||||
}
|
||||
break;
|
||||
case clip_type_union:
|
||||
switch (pft2) {
|
||||
case fill_type_even_odd:
|
||||
case fill_type_non_zero:
|
||||
return (bnd.winding_count2 == 0);
|
||||
case fill_type_positive:
|
||||
return (bnd.winding_count2 <= 0);
|
||||
case fill_type_negative:
|
||||
default:
|
||||
return (bnd.winding_count2 >= 0);
|
||||
}
|
||||
break;
|
||||
case clip_type_difference:
|
||||
if (bnd.poly_type == polygon_type_subject) {
|
||||
switch (pft2) {
|
||||
case fill_type_even_odd:
|
||||
case fill_type_non_zero:
|
||||
return (bnd.winding_count2 == 0);
|
||||
case fill_type_positive:
|
||||
return (bnd.winding_count2 <= 0);
|
||||
case fill_type_negative:
|
||||
default:
|
||||
return (bnd.winding_count2 >= 0);
|
||||
}
|
||||
} else {
|
||||
switch (pft2) {
|
||||
case fill_type_even_odd:
|
||||
case fill_type_non_zero:
|
||||
return (bnd.winding_count2 != 0);
|
||||
case fill_type_positive:
|
||||
return (bnd.winding_count2 > 0);
|
||||
case fill_type_negative:
|
||||
default:
|
||||
return (bnd.winding_count2 < 0);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case clip_type_x_or:
|
||||
return true;
|
||||
break;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void insert_lm_left_and_right_bound(bound<T>& left_bound,
|
||||
bound<T>& right_bound,
|
||||
active_bound_list<T>& active_bounds,
|
||||
ring_manager<T>& rings,
|
||||
scanbeam_list<T>& scanbeam,
|
||||
clip_type cliptype,
|
||||
fill_type subject_fill_type,
|
||||
fill_type clip_fill_type) {
|
||||
|
||||
// Both left and right bound
|
||||
auto lb_abl_itr = insert_bound_into_ABL(left_bound, active_bounds);
|
||||
auto rb_abl_itr = active_bounds.insert(std::next(lb_abl_itr), &right_bound);
|
||||
set_winding_count(lb_abl_itr, active_bounds, subject_fill_type, clip_fill_type);
|
||||
(*rb_abl_itr)->winding_count = (*lb_abl_itr)->winding_count;
|
||||
(*rb_abl_itr)->winding_count2 = (*lb_abl_itr)->winding_count2;
|
||||
if (is_contributing(left_bound, cliptype, subject_fill_type, clip_fill_type)) {
|
||||
add_local_minimum_point(lb_abl_itr, rb_abl_itr, active_bounds,
|
||||
(*lb_abl_itr)->current_edge->bot, rings);
|
||||
}
|
||||
|
||||
// Add top of edges to scanbeam
|
||||
scanbeam.push((*lb_abl_itr)->current_edge->top.y);
|
||||
|
||||
if (!current_edge_is_horizontal<T>(rb_abl_itr)) {
|
||||
scanbeam.push((*rb_abl_itr)->current_edge->top.y);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void insert_local_minima_into_ABL(T const bot_y,
|
||||
local_minimum_ptr_list<T> const& minima_sorted,
|
||||
local_minimum_ptr_list_itr<T>& current_lm,
|
||||
active_bound_list<T>& active_bounds,
|
||||
ring_manager<T>& rings,
|
||||
scanbeam_list<T>& scanbeam,
|
||||
clip_type cliptype,
|
||||
fill_type subject_fill_type,
|
||||
fill_type clip_fill_type) {
|
||||
while (current_lm != minima_sorted.end() && bot_y == (*current_lm)->y) {
|
||||
initialize_lm<T>(current_lm);
|
||||
auto& left_bound = (*current_lm)->left_bound;
|
||||
auto& right_bound = (*current_lm)->right_bound;
|
||||
insert_lm_left_and_right_bound(left_bound, right_bound, active_bounds, rings, scanbeam,
|
||||
cliptype, subject_fill_type, clip_fill_type);
|
||||
++current_lm;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void insert_horizontal_local_minima_into_ABL(T const top_y,
|
||||
local_minimum_ptr_list<T> const& minima_sorted,
|
||||
local_minimum_ptr_list_itr<T>& current_lm,
|
||||
active_bound_list<T>& active_bounds,
|
||||
ring_manager<T>& rings,
|
||||
scanbeam_list<T>& scanbeam,
|
||||
clip_type cliptype,
|
||||
fill_type subject_fill_type,
|
||||
fill_type clip_fill_type) {
|
||||
while (current_lm != minima_sorted.end() && top_y == (*current_lm)->y &&
|
||||
(*current_lm)->minimum_has_horizontal) {
|
||||
initialize_lm<T>(current_lm);
|
||||
auto& left_bound = (*current_lm)->left_bound;
|
||||
auto& right_bound = (*current_lm)->right_bound;
|
||||
insert_lm_left_and_right_bound(left_bound, right_bound, active_bounds, rings, scanbeam,
|
||||
cliptype, subject_fill_type, clip_fill_type);
|
||||
++current_lm;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
95
mapbox/geometry/wagyu/bound.hpp
Normal file
95
mapbox/geometry/wagyu/bound.hpp
Normal file
@ -0,0 +1,95 @@
|
||||
#pragma once
|
||||
|
||||
#include <list>
|
||||
|
||||
#include <mapbox/geometry/point.hpp>
|
||||
|
||||
#include <mapbox/geometry/wagyu/config.hpp>
|
||||
#include <mapbox/geometry/wagyu/edge.hpp>
|
||||
#include <mapbox/geometry/wagyu/ring.hpp>
|
||||
|
||||
#ifdef DEBUG
|
||||
#include <iostream>
|
||||
#endif
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
|
||||
template <typename T>
|
||||
struct bound {
|
||||
|
||||
edge_list<T> edges;
|
||||
edge_list_itr<T> current_edge;
|
||||
edge_list_itr<T> next_edge;
|
||||
mapbox::geometry::point<T> last_point;
|
||||
ring_ptr<T> ring;
|
||||
bound_ptr<T> maximum_bound; // the bound who's maximum connects with this bound
|
||||
double current_x;
|
||||
std::size_t pos;
|
||||
std::int32_t winding_count;
|
||||
std::int32_t winding_count2; // winding count of the opposite polytype
|
||||
std::int8_t winding_delta; // 1 or -1 depending on winding direction - 0 for linestrings
|
||||
polygon_type poly_type;
|
||||
edge_side side; // side only refers to current side of solution poly
|
||||
|
||||
bound() noexcept
|
||||
: edges(),
|
||||
current_edge(edges.end()),
|
||||
last_point({ 0, 0 }),
|
||||
ring(nullptr),
|
||||
maximum_bound(nullptr),
|
||||
current_x(0.0),
|
||||
pos(0),
|
||||
winding_count(0),
|
||||
winding_count2(0),
|
||||
winding_delta(0),
|
||||
poly_type(polygon_type_subject),
|
||||
side(edge_left) {
|
||||
}
|
||||
|
||||
bound(bound<T>&& b) noexcept
|
||||
: edges(std::move(b.edges)),
|
||||
current_edge(std::move(b.current_edge)),
|
||||
last_point(std::move(b.last_point)),
|
||||
ring(std::move(b.ring)),
|
||||
maximum_bound(std::move(b.maximum_bound)),
|
||||
current_x(std::move(b.current_x)),
|
||||
pos(std::move(b.pos)),
|
||||
winding_count(std::move(b.winding_count)),
|
||||
winding_count2(std::move(b.winding_count2)),
|
||||
winding_delta(std::move(b.winding_delta)),
|
||||
poly_type(std::move(b.poly_type)),
|
||||
side(std::move(b.side)) {
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef DEBUG
|
||||
|
||||
template <class charT, class traits, typename T>
|
||||
inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& out,
|
||||
const bound<T>& bnd) {
|
||||
out << " Bound: " << &bnd << std::endl;
|
||||
out << " current_x: " << bnd.current_x << std::endl;
|
||||
out << " last_point: " << bnd.last_point.x << ", " << bnd.last_point.y << std::endl;
|
||||
out << *(bnd.current_edge);
|
||||
out << " winding count: " << bnd.winding_count << std::endl;
|
||||
out << " winding_count2: " << bnd.winding_count2 << std::endl;
|
||||
out << " winding_delta: " << static_cast<int>(bnd.winding_delta) << std::endl;
|
||||
out << " maximum_bound: " << bnd.maximum_bound << std::endl;
|
||||
if (bnd.side == edge_left) {
|
||||
out << " side: left" << std::endl;
|
||||
} else {
|
||||
out << " side: right" << std::endl;
|
||||
}
|
||||
out << " ring: " << bnd.ring << std::endl;
|
||||
if (bnd.ring) {
|
||||
out << " ring index: " << bnd.ring->ring_index << std::endl;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
181
mapbox/geometry/wagyu/build_edges.hpp
Normal file
181
mapbox/geometry/wagyu/build_edges.hpp
Normal file
@ -0,0 +1,181 @@
|
||||
#pragma once
|
||||
|
||||
#include <mapbox/geometry/line_string.hpp>
|
||||
#include <mapbox/geometry/point.hpp>
|
||||
#include <mapbox/geometry/polygon.hpp>
|
||||
|
||||
#include <mapbox/geometry/wagyu/config.hpp>
|
||||
#include <mapbox/geometry/wagyu/edge.hpp>
|
||||
#include <mapbox/geometry/wagyu/util.hpp>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
|
||||
template <typename T>
|
||||
bool point_2_is_between_point_1_and_point_3(mapbox::geometry::point<T> const& pt1,
|
||||
mapbox::geometry::point<T> const& pt2,
|
||||
mapbox::geometry::point<T> const& pt3) {
|
||||
if ((pt1 == pt3) || (pt1 == pt2) || (pt3 == pt2)) {
|
||||
return false;
|
||||
} else if (pt1.x != pt3.x) {
|
||||
return (pt2.x > pt1.x) == (pt2.x < pt3.x);
|
||||
} else {
|
||||
return (pt2.y > pt1.y) == (pt2.y < pt3.y);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool build_edge_list(mapbox::geometry::linear_ring<T> const& path_geometry, edge_list<T>& edges) {
|
||||
|
||||
if (path_geometry.size() < 3) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// As this is a loop, we need to first go backwards from end to try and find
|
||||
// the proper starting point for the iterators before the beginning
|
||||
|
||||
auto itr_rev = path_geometry.rbegin();
|
||||
auto itr = path_geometry.begin();
|
||||
mapbox::geometry::point<T> pt1 = *itr_rev;
|
||||
mapbox::geometry::point<T> pt2 = *itr;
|
||||
|
||||
// Find next non repeated point going backwards from
|
||||
// end for pt1
|
||||
while (pt1 == pt2) {
|
||||
++itr_rev;
|
||||
if (itr_rev == path_geometry.rend()) {
|
||||
return false;
|
||||
}
|
||||
pt1 = *itr_rev;
|
||||
}
|
||||
++itr;
|
||||
mapbox::geometry::point<T> pt3 = *itr;
|
||||
auto itr_last = itr_rev.base();
|
||||
mapbox::geometry::point<T> front_pt;
|
||||
mapbox::geometry::point<T> back_pt;
|
||||
while (true) {
|
||||
if (pt3 == pt2) {
|
||||
// Duplicate point advance itr, but do not
|
||||
// advance other points
|
||||
if (itr == itr_last) {
|
||||
break;
|
||||
}
|
||||
++itr;
|
||||
if (itr == itr_last) {
|
||||
if (edges.empty()) {
|
||||
break;
|
||||
}
|
||||
pt3 = front_pt;
|
||||
} else {
|
||||
pt3 = *itr;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Now check if slopes are equal between two segments - either
|
||||
// a spike or a collinear point - if so drop point number 2.
|
||||
if (slopes_equal(pt1, pt2, pt3)) {
|
||||
// We need to reconsider previously added points
|
||||
// because the point it was using was found to be collinear
|
||||
// or a spike
|
||||
pt2 = pt1;
|
||||
if (!edges.empty()) {
|
||||
edges.pop_back(); // remove previous edge (pt1)
|
||||
}
|
||||
if (!edges.empty()) {
|
||||
if (back_pt == edges.back().top) {
|
||||
pt1 = edges.back().bot;
|
||||
} else {
|
||||
pt1 = edges.back().top;
|
||||
}
|
||||
back_pt = pt1;
|
||||
} else {
|
||||
// If this occurs we must look to the back of the
|
||||
// ring for new points.
|
||||
while (*itr_rev == pt2) {
|
||||
++itr_rev;
|
||||
if ((itr + 1) == itr_rev.base()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
pt1 = *itr_rev;
|
||||
itr_last = itr_rev.base();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (edges.empty()) {
|
||||
front_pt = pt2;
|
||||
}
|
||||
edges.emplace_back(pt2, pt3);
|
||||
back_pt = pt2;
|
||||
if (itr == itr_last) {
|
||||
break;
|
||||
}
|
||||
pt1 = pt2;
|
||||
pt2 = pt3;
|
||||
++itr;
|
||||
if (itr == itr_last) {
|
||||
if (edges.empty()) {
|
||||
break;
|
||||
}
|
||||
pt3 = front_pt;
|
||||
} else {
|
||||
pt3 = *itr;
|
||||
}
|
||||
}
|
||||
|
||||
bool modified = false;
|
||||
do {
|
||||
modified = false;
|
||||
if (edges.size() < 3) {
|
||||
return false;
|
||||
}
|
||||
auto& f = edges.front();
|
||||
auto& b = edges.back();
|
||||
if (slopes_equal(f, b)) {
|
||||
if (f.bot == b.top) {
|
||||
if (f.top == b.bot) {
|
||||
edges.pop_back();
|
||||
edges.erase(edges.begin());
|
||||
} else {
|
||||
f.bot = b.bot;
|
||||
edges.pop_back();
|
||||
}
|
||||
modified = true;
|
||||
} else if (f.top == b.bot) {
|
||||
f.top = b.top;
|
||||
edges.pop_back();
|
||||
modified = true;
|
||||
} else if (f.top == b.top && f.bot == b.bot) {
|
||||
edges.pop_back();
|
||||
edges.erase(edges.begin());
|
||||
modified = true;
|
||||
} else if (f.top == b.top) {
|
||||
if (point_2_is_between_point_1_and_point_3(f.top, f.bot, b.bot)) {
|
||||
b.top = f.bot;
|
||||
edges.erase(edges.begin());
|
||||
} else {
|
||||
f.top = b.bot;
|
||||
edges.pop_back();
|
||||
}
|
||||
modified = true;
|
||||
} else if (f.bot == b.bot) {
|
||||
if (point_2_is_between_point_1_and_point_3(f.bot, f.top, b.top)) {
|
||||
b.bot = f.top;
|
||||
edges.erase(edges.begin());
|
||||
} else {
|
||||
f.bot = b.top;
|
||||
edges.pop_back();
|
||||
}
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
} while (modified);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
39
mapbox/geometry/wagyu/build_local_minima_list.hpp
Normal file
39
mapbox/geometry/wagyu/build_local_minima_list.hpp
Normal file
@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <mapbox/geometry/wagyu/build_edges.hpp>
|
||||
#include <mapbox/geometry/wagyu/config.hpp>
|
||||
#include <mapbox/geometry/wagyu/local_minimum.hpp>
|
||||
#include <mapbox/geometry/wagyu/local_minimum_util.hpp>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
|
||||
template <typename T>
|
||||
bool add_line_string(mapbox::geometry::line_string<T> const& path_geometry,
|
||||
local_minimum_list<T>& minima_list) {
|
||||
bool is_flat = true;
|
||||
edge_list<T> new_edges;
|
||||
new_edges.reserve(path_geometry.size());
|
||||
if (!build_edge_list(path_geometry, new_edges, is_flat) || new_edges.empty()) {
|
||||
return false;
|
||||
}
|
||||
add_line_to_local_minima_list(new_edges, minima_list, polygon_type_subject);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool add_linear_ring(mapbox::geometry::linear_ring<T> const& path_geometry,
|
||||
local_minimum_list<T>& minima_list,
|
||||
polygon_type p_type) {
|
||||
edge_list<T> new_edges;
|
||||
new_edges.reserve(path_geometry.size());
|
||||
if (!build_edge_list(path_geometry, new_edges) || new_edges.empty()) {
|
||||
return false;
|
||||
}
|
||||
add_ring_to_local_minima_list(new_edges, minima_list, p_type);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
68
mapbox/geometry/wagyu/build_result.hpp
Normal file
68
mapbox/geometry/wagyu/build_result.hpp
Normal file
@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
|
||||
#include <mapbox/geometry/wagyu/ring.hpp>
|
||||
#include <mapbox/geometry/wagyu/ring_util.hpp>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
|
||||
template <typename T>
|
||||
void push_ring_to_polygon(mapbox::geometry::polygon<T>& poly, ring_ptr<T>& r, bool reverse_output) {
|
||||
mapbox::geometry::linear_ring<T> lr;
|
||||
lr.reserve(r->size + 1);
|
||||
auto firstPt = r->points;
|
||||
auto ptIt = r->points;
|
||||
if (reverse_output) {
|
||||
do {
|
||||
lr.emplace_back(ptIt->x, ptIt->y);
|
||||
ptIt = ptIt->next;
|
||||
} while (ptIt != firstPt);
|
||||
} else {
|
||||
do {
|
||||
lr.emplace_back(ptIt->x, ptIt->y);
|
||||
ptIt = ptIt->prev;
|
||||
} while (ptIt != firstPt);
|
||||
}
|
||||
lr.emplace_back(firstPt->x, firstPt->y); // close the ring
|
||||
poly.push_back(lr);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void build_result_polygons(std::vector<mapbox::geometry::polygon<T>>& solution,
|
||||
ring_list<T>& rings,
|
||||
bool reverse_output) {
|
||||
|
||||
for (auto& r : rings) {
|
||||
assert(r->points);
|
||||
std::size_t cnt = point_count(r->points);
|
||||
if (cnt < 3) {
|
||||
continue;
|
||||
}
|
||||
solution.emplace_back();
|
||||
push_ring_to_polygon(solution.back(), r, reverse_output);
|
||||
for (auto& c : r->children) {
|
||||
assert(c->points);
|
||||
cnt = point_count(c->points);
|
||||
if (cnt < 3) {
|
||||
continue;
|
||||
}
|
||||
push_ring_to_polygon(solution.back(), c, reverse_output);
|
||||
}
|
||||
for (auto& c : r->children) {
|
||||
if (!c->children.empty()) {
|
||||
build_result_polygons(solution, c->children, reverse_output);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void build_result(std::vector<mapbox::geometry::polygon<T>>& solution,
|
||||
ring_manager<T>& rings,
|
||||
bool reverse_output) {
|
||||
build_result_polygons(solution, rings.children, reverse_output);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
53
mapbox/geometry/wagyu/config.hpp
Normal file
53
mapbox/geometry/wagyu/config.hpp
Normal file
@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <list>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
|
||||
enum clip_type : std::uint8_t {
|
||||
clip_type_intersection = 0,
|
||||
clip_type_union,
|
||||
clip_type_difference,
|
||||
clip_type_x_or
|
||||
};
|
||||
|
||||
enum polygon_type : std::uint8_t { polygon_type_subject = 0, polygon_type_clip };
|
||||
|
||||
enum fill_type : std::uint8_t {
|
||||
fill_type_even_odd = 0,
|
||||
fill_type_non_zero,
|
||||
fill_type_positive,
|
||||
fill_type_negative
|
||||
};
|
||||
|
||||
static double const def_arc_tolerance = 0.25;
|
||||
|
||||
static int const EDGE_UNASSIGNED = -1; // edge not currently 'owning' a solution
|
||||
static int const EDGE_SKIP = -2; // edge that would otherwise close a path
|
||||
static std::int64_t const LOW_RANGE = 0x3FFFFFFF;
|
||||
static std::int64_t const HIGH_RANGE = 0x3FFFFFFFFFFFFFFFLL;
|
||||
|
||||
enum horizontal_direction : std::uint8_t { right_to_left = 0, left_to_right = 1 };
|
||||
|
||||
enum edge_side : std::uint8_t { edge_left = 0, edge_right };
|
||||
|
||||
enum join_type : std::uint8_t { join_type_square = 0, join_type_round, join_type_miter };
|
||||
|
||||
enum end_type {
|
||||
end_type_closed_polygon = 0,
|
||||
end_type_closed_line,
|
||||
end_type_open_butt,
|
||||
end_type_open_square,
|
||||
end_type_open_round
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
using maxima_list = std::list<T>;
|
||||
}
|
||||
}
|
||||
}
|
120
mapbox/geometry/wagyu/edge.hpp
Normal file
120
mapbox/geometry/wagyu/edge.hpp
Normal file
@ -0,0 +1,120 @@
|
||||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <list>
|
||||
|
||||
#include <mapbox/geometry/point.hpp>
|
||||
#include <mapbox/geometry/wagyu/config.hpp>
|
||||
#include <mapbox/geometry/wagyu/util.hpp>
|
||||
|
||||
#ifdef DEBUG
|
||||
#include <iostream>
|
||||
#endif
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
|
||||
template <typename T>
|
||||
struct bound;
|
||||
|
||||
template <typename T>
|
||||
using bound_ptr = bound<T>*;
|
||||
|
||||
template <typename T>
|
||||
struct edge {
|
||||
mapbox::geometry::point<T> bot;
|
||||
mapbox::geometry::point<T> top;
|
||||
double dx;
|
||||
|
||||
edge(edge<T>&& e) noexcept : bot(std::move(e.bot)), top(std::move(e.top)), dx(std::move(e.dx)) {
|
||||
}
|
||||
|
||||
edge& operator=(edge<T>&& e) noexcept {
|
||||
bot = std::move(e.bot);
|
||||
top = std::move(e.top);
|
||||
dx = std::move(e.dx);
|
||||
return *this;
|
||||
}
|
||||
|
||||
edge(mapbox::geometry::point<T> const& current,
|
||||
mapbox::geometry::point<T> const& next_pt) noexcept
|
||||
: bot(current), top(current), dx(0.0) {
|
||||
if (current.y >= next_pt.y) {
|
||||
top = next_pt;
|
||||
} else {
|
||||
bot = next_pt;
|
||||
}
|
||||
double dy = static_cast<double>(top.y - bot.y);
|
||||
if (value_is_zero(dy)) {
|
||||
dx = std::numeric_limits<double>::infinity();
|
||||
} else {
|
||||
dx = static_cast<double>(top.x - bot.x) / dy;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
using edge_ptr = edge<T>*;
|
||||
|
||||
template <typename T>
|
||||
using edge_list = std::vector<edge<T>>;
|
||||
|
||||
template <typename T>
|
||||
using edge_list_itr = typename edge_list<T>::iterator;
|
||||
|
||||
template <typename T>
|
||||
bool slopes_equal(edge<T> const& e1, edge<T> const& e2) {
|
||||
return (e1.top.y - e1.bot.y) * (e2.top.x - e2.bot.x) ==
|
||||
(e1.top.x - e1.bot.x) * (e2.top.y - e2.bot.y);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool is_horizontal(edge<T> const& e) {
|
||||
return std::isinf(e.dx);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline double get_current_x(edge<T> const& edge, const T current_y) {
|
||||
if (current_y == edge.top.y) {
|
||||
return static_cast<double>(edge.top.x);
|
||||
} else {
|
||||
return static_cast<double>(edge.bot.x) +
|
||||
edge.dx * static_cast<double>(current_y - edge.bot.y);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
|
||||
template <class charT, class traits, typename T>
|
||||
inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& out,
|
||||
const edge<T>& e) {
|
||||
out << " Edge: " << std::endl;
|
||||
out << " bot x: " << e.bot.x << " y: " << e.bot.y << std::endl;
|
||||
out << " top x: " << e.top.x << " y: " << e.top.y << std::endl;
|
||||
return out;
|
||||
}
|
||||
|
||||
template <class charT, class traits, typename T>
|
||||
inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& out,
|
||||
edge_list<T> const& edges) {
|
||||
out << "[";
|
||||
bool first = true;
|
||||
for (auto const& e : edges) {
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
out << ",";
|
||||
}
|
||||
out << "[[" << e.bot.x << "," << e.bot.y << "],[";
|
||||
out << e.top.x << "," << e.top.y << "]]";
|
||||
}
|
||||
out << "]";
|
||||
return out;
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
23
mapbox/geometry/wagyu/exceptions.hpp
Normal file
23
mapbox/geometry/wagyu/exceptions.hpp
Normal file
@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
class clipper_exception : public std::exception {
|
||||
private:
|
||||
std::string m_descr;
|
||||
|
||||
public:
|
||||
clipper_exception(const char* description) : m_descr(description) {
|
||||
}
|
||||
virtual ~clipper_exception() noexcept {
|
||||
}
|
||||
virtual const char* what() const noexcept {
|
||||
return m_descr.c_str();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
72
mapbox/geometry/wagyu/intersect.hpp
Normal file
72
mapbox/geometry/wagyu/intersect.hpp
Normal file
@ -0,0 +1,72 @@
|
||||
#pragma once
|
||||
|
||||
#include <set>
|
||||
|
||||
#include <mapbox/geometry/point.hpp>
|
||||
|
||||
#include <mapbox/geometry/wagyu/active_bound_list.hpp>
|
||||
|
||||
#ifdef DEBUG
|
||||
#include <iostream>
|
||||
#endif
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
|
||||
template <typename T>
|
||||
struct intersect_node {
|
||||
|
||||
active_bound_list_itr<T> bound1;
|
||||
active_bound_list_itr<T> bound2;
|
||||
mapbox::geometry::point<double> pt;
|
||||
|
||||
intersect_node(intersect_node<T>&& n)
|
||||
: bound1(std::move(n.bound1)), bound2(std::move(n.bound2)), pt(std::move(n.pt)) {
|
||||
}
|
||||
|
||||
intersect_node& operator=(intersect_node<T>&& n) {
|
||||
bound1 = std::move(n.bound1);
|
||||
bound2 = std::move(n.bound2);
|
||||
pt = std::move(n.pt);
|
||||
return *this;
|
||||
}
|
||||
|
||||
intersect_node(active_bound_list_itr<T> const& bound1_,
|
||||
active_bound_list_itr<T> const& bound2_,
|
||||
mapbox::geometry::point<double> const& pt_)
|
||||
: bound1(bound1_), bound2(bound2_), pt(pt_) {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
using intersect_list = std::vector<intersect_node<T>>;
|
||||
|
||||
#ifdef DEBUG
|
||||
|
||||
template <class charT, class traits, typename T>
|
||||
inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& out,
|
||||
const intersect_node<T>& e) {
|
||||
out << " point x: " << e.pt.x << " y: " << e.pt.y << std::endl;
|
||||
out << " bound 1: " << std::endl;
|
||||
out << *(*e.bound1) << std::endl;
|
||||
out << " bound 2: " << std::endl;
|
||||
out << *(*e.bound2) << std::endl;
|
||||
return out;
|
||||
}
|
||||
|
||||
template <class charT, class traits, typename T>
|
||||
inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& out,
|
||||
const intersect_list<T>& ints) {
|
||||
std::size_t c = 0;
|
||||
for (auto const& i : ints) {
|
||||
out << "Intersection: " << c++ << std::endl;
|
||||
out << i;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
345
mapbox/geometry/wagyu/intersect_util.hpp
Normal file
345
mapbox/geometry/wagyu/intersect_util.hpp
Normal file
@ -0,0 +1,345 @@
|
||||
#pragma once
|
||||
|
||||
#include <mapbox/geometry/wagyu/active_bound_list.hpp>
|
||||
#include <mapbox/geometry/wagyu/bound.hpp>
|
||||
#include <mapbox/geometry/wagyu/config.hpp>
|
||||
#include <mapbox/geometry/wagyu/intersect.hpp>
|
||||
#include <mapbox/geometry/wagyu/ring_util.hpp>
|
||||
#include <mapbox/geometry/wagyu/util.hpp>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
|
||||
template <typename T>
|
||||
struct intersect_list_sorter {
|
||||
inline bool operator()(intersect_node<T> const& node1, intersect_node<T> const& node2) {
|
||||
if (!values_are_equal(node2.pt.y, node1.pt.y)) {
|
||||
return node2.pt.y < node1.pt.y;
|
||||
} else {
|
||||
return ((*node2.bound1)->winding_count2 + (*node2.bound2)->winding_count2) >
|
||||
((*node1.bound1)->winding_count2 + (*node1.bound2)->winding_count2);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline mapbox::geometry::point<T> round_point(mapbox::geometry::point<double> const& pt) {
|
||||
return mapbox::geometry::point<T>(round_towards_max<T>(pt.x), round_towards_max<T>(pt.y));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void swap_rings(bound<T>& b1, bound<T>& b2) {
|
||||
ring_ptr<T> ring = b1.ring;
|
||||
b1.ring = b2.ring;
|
||||
b2.ring = ring;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void swap_sides(bound<T>& b1, bound<T>& b2) {
|
||||
edge_side side = b1.side;
|
||||
b1.side = b2.side;
|
||||
b2.side = side;
|
||||
}
|
||||
|
||||
template <typename T1, typename T2>
|
||||
bool get_edge_intersection(edge<T1> const& e1,
|
||||
edge<T1> const& e2,
|
||||
mapbox::geometry::point<T2>& pt) {
|
||||
T2 p0_x = static_cast<T2>(e1.bot.x);
|
||||
T2 p0_y = static_cast<T2>(e1.bot.y);
|
||||
T2 p1_x = static_cast<T2>(e1.top.x);
|
||||
T2 p1_y = static_cast<T2>(e1.top.y);
|
||||
T2 p2_x = static_cast<T2>(e2.bot.x);
|
||||
T2 p2_y = static_cast<T2>(e2.bot.y);
|
||||
T2 p3_x = static_cast<T2>(e2.top.x);
|
||||
T2 p3_y = static_cast<T2>(e2.top.y);
|
||||
T2 s1_x, s1_y, s2_x, s2_y;
|
||||
s1_x = p1_x - p0_x;
|
||||
s1_y = p1_y - p0_y;
|
||||
s2_x = p3_x - p2_x;
|
||||
s2_y = p3_y - p2_y;
|
||||
|
||||
T2 s, t;
|
||||
s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y);
|
||||
t = (s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y);
|
||||
|
||||
if (s >= 0.0 && s <= 1.0 && t >= 0.0 && t <= 1.0) {
|
||||
pt.x = p0_x + (t * s1_x);
|
||||
pt.y = p0_y + (t * s1_y);
|
||||
return true;
|
||||
}
|
||||
// LCOV_EXCL_START
|
||||
return false;
|
||||
// LCOV_EXCL_END
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void build_intersect_list(active_bound_list<T>& active_bounds, intersect_list<T>& intersects) {
|
||||
// bubblesort ...
|
||||
bool isModified = false;
|
||||
do {
|
||||
isModified = false;
|
||||
auto bnd = active_bounds.begin();
|
||||
auto bnd_next = std::next(bnd);
|
||||
while (bnd_next != active_bounds.end()) {
|
||||
if ((*bnd)->current_x > (*bnd_next)->current_x &&
|
||||
!slopes_equal(*((*bnd)->current_edge), *((*bnd_next)->current_edge))) {
|
||||
mapbox::geometry::point<double> pt;
|
||||
if (!get_edge_intersection<T, double>(*((*bnd)->current_edge),
|
||||
*((*bnd_next)->current_edge), pt)) {
|
||||
// LCOV_EXCL_START
|
||||
throw std::runtime_error(
|
||||
"Trying to find intersection of lines that do not intersect");
|
||||
// LCOV_EXCL_END
|
||||
}
|
||||
intersects.emplace_back(bnd, bnd_next, pt);
|
||||
swap_positions_in_ABL(bnd, bnd_next, active_bounds);
|
||||
bnd_next = std::next(bnd);
|
||||
isModified = true;
|
||||
} else {
|
||||
bnd = bnd_next;
|
||||
++bnd_next;
|
||||
}
|
||||
}
|
||||
} while (isModified);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void intersect_bounds(active_bound_list_itr<T>& b1,
|
||||
active_bound_list_itr<T>& b2,
|
||||
mapbox::geometry::point<T> const& pt,
|
||||
clip_type cliptype,
|
||||
fill_type subject_fill_type,
|
||||
fill_type clip_fill_type,
|
||||
ring_manager<T>& rings,
|
||||
active_bound_list<T>& active_bounds) {
|
||||
bool b1Contributing = ((*b1)->ring != nullptr);
|
||||
bool b2Contributing = ((*b2)->ring != nullptr);
|
||||
|
||||
// update winding counts...
|
||||
// assumes that b1 will be to the Right of b2 ABOVE the intersection
|
||||
if ((*b1)->poly_type == (*b2)->poly_type) {
|
||||
if (is_even_odd_fill_type(*(*b1), subject_fill_type, clip_fill_type)) {
|
||||
std::int32_t oldE1winding_count = (*b1)->winding_count;
|
||||
(*b1)->winding_count = (*b2)->winding_count;
|
||||
(*b2)->winding_count = oldE1winding_count;
|
||||
} else {
|
||||
if ((*b1)->winding_count + (*b2)->winding_delta == 0) {
|
||||
(*b1)->winding_count = -(*b1)->winding_count;
|
||||
} else {
|
||||
(*b1)->winding_count += (*b2)->winding_delta;
|
||||
}
|
||||
if ((*b2)->winding_count - (*b1)->winding_delta == 0) {
|
||||
(*b2)->winding_count = -(*b2)->winding_count;
|
||||
} else {
|
||||
(*b2)->winding_count -= (*b1)->winding_delta;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!is_even_odd_fill_type(*(*b2), subject_fill_type, clip_fill_type)) {
|
||||
(*b1)->winding_count2 += (*b2)->winding_delta;
|
||||
} else {
|
||||
(*b1)->winding_count2 = ((*b1)->winding_count2 == 0) ? 1 : 0;
|
||||
}
|
||||
if (!is_even_odd_fill_type(*(*b1), subject_fill_type, clip_fill_type)) {
|
||||
(*b2)->winding_count2 -= (*b1)->winding_delta;
|
||||
} else {
|
||||
(*b2)->winding_count2 = ((*b2)->winding_count2 == 0) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
fill_type b1FillType, b2FillType, b1FillType2, b2FillType2;
|
||||
if ((*b1)->poly_type == polygon_type_subject) {
|
||||
b1FillType = subject_fill_type;
|
||||
b1FillType2 = clip_fill_type;
|
||||
} else {
|
||||
b1FillType = clip_fill_type;
|
||||
b1FillType2 = subject_fill_type;
|
||||
}
|
||||
if ((*b2)->poly_type == polygon_type_subject) {
|
||||
b2FillType = subject_fill_type;
|
||||
b2FillType2 = clip_fill_type;
|
||||
} else {
|
||||
b2FillType = clip_fill_type;
|
||||
b2FillType2 = subject_fill_type;
|
||||
}
|
||||
|
||||
std::int32_t b1Wc, b2Wc;
|
||||
switch (b1FillType) {
|
||||
case fill_type_positive:
|
||||
b1Wc = (*b1)->winding_count;
|
||||
break;
|
||||
case fill_type_negative:
|
||||
b1Wc = -(*b1)->winding_count;
|
||||
break;
|
||||
case fill_type_even_odd:
|
||||
case fill_type_non_zero:
|
||||
default:
|
||||
b1Wc = std::abs(static_cast<int>((*b1)->winding_count));
|
||||
}
|
||||
switch (b2FillType) {
|
||||
case fill_type_positive:
|
||||
b2Wc = (*b2)->winding_count;
|
||||
break;
|
||||
case fill_type_negative:
|
||||
b2Wc = -(*b2)->winding_count;
|
||||
break;
|
||||
case fill_type_even_odd:
|
||||
case fill_type_non_zero:
|
||||
default:
|
||||
b2Wc = std::abs(static_cast<int>((*b2)->winding_count));
|
||||
}
|
||||
if (b1Contributing && b2Contributing) {
|
||||
if ((b1Wc != 0 && b1Wc != 1) || (b2Wc != 0 && b2Wc != 1) ||
|
||||
((*b1)->poly_type != (*b2)->poly_type && cliptype != clip_type_x_or)) {
|
||||
add_local_maximum_point(b1, b2, pt, rings, active_bounds);
|
||||
} else {
|
||||
add_point(b1, active_bounds, pt, rings);
|
||||
add_point(b2, active_bounds, pt, rings);
|
||||
swap_sides(*(*b1), *(*b2));
|
||||
swap_rings(*(*b1), *(*b2));
|
||||
}
|
||||
} else if (b1Contributing) {
|
||||
if (b2Wc == 0 || b2Wc == 1) {
|
||||
add_point(b1, active_bounds, pt, rings);
|
||||
(*b2)->last_point = pt;
|
||||
swap_sides(*(*b1), *(*b2));
|
||||
swap_rings(*(*b1), *(*b2));
|
||||
}
|
||||
} else if (b2Contributing) {
|
||||
if (b1Wc == 0 || b1Wc == 1) {
|
||||
(*b1)->last_point = pt;
|
||||
add_point(b2, active_bounds, pt, rings);
|
||||
swap_sides(*(*b1), *(*b2));
|
||||
swap_rings(*(*b1), *(*b2));
|
||||
}
|
||||
} else if ((b1Wc == 0 || b1Wc == 1) && (b2Wc == 0 || b2Wc == 1)) {
|
||||
// neither bound is currently contributing ...
|
||||
|
||||
std::int32_t b1Wc2, b2Wc2;
|
||||
switch (b1FillType2) {
|
||||
case fill_type_positive:
|
||||
b1Wc2 = (*b1)->winding_count2;
|
||||
break;
|
||||
case fill_type_negative:
|
||||
b1Wc2 = -(*b1)->winding_count2;
|
||||
break;
|
||||
case fill_type_even_odd:
|
||||
case fill_type_non_zero:
|
||||
default:
|
||||
b1Wc2 = std::abs(static_cast<int>((*b1)->winding_count2));
|
||||
}
|
||||
switch (b2FillType2) {
|
||||
case fill_type_positive:
|
||||
b2Wc2 = (*b2)->winding_count2;
|
||||
break;
|
||||
case fill_type_negative:
|
||||
b2Wc2 = -(*b2)->winding_count2;
|
||||
break;
|
||||
case fill_type_even_odd:
|
||||
case fill_type_non_zero:
|
||||
default:
|
||||
b2Wc2 = std::abs(static_cast<int>((*b2)->winding_count2));
|
||||
}
|
||||
|
||||
if ((*b1)->poly_type != (*b2)->poly_type) {
|
||||
add_local_minimum_point(b1, b2, active_bounds, pt, rings);
|
||||
} else if (b1Wc == 1 && b2Wc == 1) {
|
||||
switch (cliptype) {
|
||||
case clip_type_intersection:
|
||||
if (b1Wc2 > 0 && b2Wc2 > 0) {
|
||||
add_local_minimum_point(b1, b2, active_bounds, pt, rings);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
case clip_type_union:
|
||||
if (b1Wc2 <= 0 && b2Wc2 <= 0) {
|
||||
add_local_minimum_point(b1, b2, active_bounds, pt, rings);
|
||||
}
|
||||
break;
|
||||
case clip_type_difference:
|
||||
if ((((*b1)->poly_type == polygon_type_clip) && (b1Wc2 > 0) && (b2Wc2 > 0)) ||
|
||||
(((*b1)->poly_type == polygon_type_subject) && (b1Wc2 <= 0) && (b2Wc2 <= 0))) {
|
||||
add_local_minimum_point(b1, b2, active_bounds, pt, rings);
|
||||
}
|
||||
break;
|
||||
case clip_type_x_or:
|
||||
add_local_minimum_point(b1, b2, active_bounds, pt, rings);
|
||||
}
|
||||
} else {
|
||||
swap_sides(*(*b1), *(*b2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool bounds_adjacent(intersect_node<T> const& inode) {
|
||||
return (std::next(inode.bound1) == inode.bound2) || (std::next(inode.bound2) == inode.bound1);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void process_intersect_list(intersect_list<T>& intersects,
|
||||
clip_type cliptype,
|
||||
fill_type subject_fill_type,
|
||||
fill_type clip_fill_type,
|
||||
ring_manager<T>& rings,
|
||||
active_bound_list<T>& active_bounds) {
|
||||
for (auto node_itr = intersects.begin(); node_itr != intersects.end(); ++node_itr) {
|
||||
if (!bounds_adjacent(*node_itr)) {
|
||||
auto next_itr = std::next(node_itr);
|
||||
while (next_itr != intersects.end() && !bounds_adjacent(*next_itr)) {
|
||||
++next_itr;
|
||||
}
|
||||
if (next_itr == intersects.end()) {
|
||||
throw std::runtime_error("Could not properly correct intersection order.");
|
||||
}
|
||||
std::iter_swap(node_itr, next_itr);
|
||||
}
|
||||
mapbox::geometry::point<T> pt = round_point<T>(node_itr->pt);
|
||||
intersect_bounds(node_itr->bound1, node_itr->bound2, pt, cliptype, subject_fill_type,
|
||||
clip_fill_type, rings, active_bounds);
|
||||
swap_positions_in_ABL(node_itr->bound1, node_itr->bound2, active_bounds);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void update_current_x(active_bound_list<T>& active_bounds, T top_y) {
|
||||
std::size_t pos = 0;
|
||||
for (auto& bnd : active_bounds) {
|
||||
bnd->pos = pos++;
|
||||
bnd->current_x = get_current_x(*bnd->current_edge, top_y);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void process_intersections(T top_y,
|
||||
active_bound_list<T>& active_bounds,
|
||||
clip_type cliptype,
|
||||
fill_type subject_fill_type,
|
||||
fill_type clip_fill_type,
|
||||
ring_manager<T>& rings) {
|
||||
if (active_bounds.empty()) {
|
||||
return;
|
||||
}
|
||||
update_current_x(active_bounds, top_y);
|
||||
intersect_list<T> intersects;
|
||||
build_intersect_list(active_bounds, intersects);
|
||||
|
||||
if (intersects.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Restore order of active bounds list
|
||||
active_bounds.sort(
|
||||
[](bound_ptr<T> const& b1, bound_ptr<T> const& b2) { return b1->pos < b2->pos; });
|
||||
|
||||
// Sort the intersection list
|
||||
std::stable_sort(intersects.begin(), intersects.end(), intersect_list_sorter<T>());
|
||||
|
||||
process_intersect_list(intersects, cliptype, subject_fill_type, clip_fill_type, rings,
|
||||
active_bounds);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
118
mapbox/geometry/wagyu/local_minimum.hpp
Normal file
118
mapbox/geometry/wagyu/local_minimum.hpp
Normal file
@ -0,0 +1,118 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef DEBUG
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#endif
|
||||
|
||||
#include <queue>
|
||||
|
||||
#include <mapbox/geometry/wagyu/bound.hpp>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
|
||||
template <typename T>
|
||||
struct local_minimum {
|
||||
bound<T> left_bound;
|
||||
bound<T> right_bound;
|
||||
T y;
|
||||
bool minimum_has_horizontal;
|
||||
|
||||
local_minimum(bound<T>&& left_bound_, bound<T>&& right_bound_, T y_, bool has_horz_)
|
||||
: left_bound(std::move(left_bound_)),
|
||||
right_bound(std::move(right_bound_)),
|
||||
y(y_),
|
||||
minimum_has_horizontal(has_horz_) {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
using local_minimum_list = std::deque<local_minimum<T>>;
|
||||
|
||||
template <typename T>
|
||||
using local_minimum_itr = typename local_minimum_list<T>::iterator;
|
||||
|
||||
template <typename T>
|
||||
using local_minimum_ptr = local_minimum<T>*;
|
||||
|
||||
template <typename T>
|
||||
using local_minimum_ptr_list = std::vector<local_minimum_ptr<T>>;
|
||||
|
||||
template <typename T>
|
||||
using local_minimum_ptr_list_itr = typename local_minimum_ptr_list<T>::iterator;
|
||||
|
||||
template <typename T>
|
||||
struct local_minimum_sorter {
|
||||
inline bool operator()(local_minimum_ptr<T> const& locMin1,
|
||||
local_minimum_ptr<T> const& locMin2) {
|
||||
if (locMin2->y == locMin1->y) {
|
||||
return locMin2->minimum_has_horizontal != locMin1->minimum_has_horizontal &&
|
||||
locMin1->minimum_has_horizontal;
|
||||
}
|
||||
return locMin2->y < locMin1->y;
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef DEBUG
|
||||
|
||||
template <class charT, class traits, typename T>
|
||||
inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& out,
|
||||
const local_minimum<T>& lm) {
|
||||
out << " Local Minimum:" << std::endl;
|
||||
out << " y: " << lm.y << std::endl;
|
||||
if (lm.minimum_has_horizontal) {
|
||||
out << " minimum_has_horizontal: true" << std::endl;
|
||||
} else {
|
||||
out << " minimum_has_horizontal: false" << std::endl;
|
||||
}
|
||||
out << " left_bound: " << std::endl;
|
||||
out << lm.left_bound << std::endl;
|
||||
out << " right_bound: " << std::endl;
|
||||
out << lm.right_bound << std::endl;
|
||||
return out;
|
||||
}
|
||||
|
||||
template <class charT, class traits, typename T>
|
||||
inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& out,
|
||||
const local_minimum_ptr_list<T>& lms) {
|
||||
for (auto const& lm : lms) {
|
||||
out << *lm;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::string output_all_edges(local_minimum_ptr_list<T> const& lms) {
|
||||
std::ostringstream out;
|
||||
out << "[";
|
||||
bool first = true;
|
||||
for (auto const& lm : lms) {
|
||||
for (auto const& e : lm->left_bound.edges) {
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
out << ",";
|
||||
}
|
||||
out << "[[" << e.bot.x << "," << e.bot.y << "],[";
|
||||
out << e.top.x << "," << e.top.y << "]]";
|
||||
}
|
||||
for (auto const& e : lm->right_bound.edges) {
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
out << ",";
|
||||
}
|
||||
out << "[[" << e.bot.x << "," << e.bot.y << "],[";
|
||||
out << e.top.x << "," << e.top.y << "]]";
|
||||
}
|
||||
}
|
||||
out << "]";
|
||||
return out.str();
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
321
mapbox/geometry/wagyu/local_minimum_util.hpp
Normal file
321
mapbox/geometry/wagyu/local_minimum_util.hpp
Normal file
@ -0,0 +1,321 @@
|
||||
#pragma once
|
||||
|
||||
#include <mapbox/geometry/wagyu/edge.hpp>
|
||||
#include <mapbox/geometry/wagyu/local_minimum.hpp>
|
||||
|
||||
#ifdef DEBUG
|
||||
#include <stdexcept>
|
||||
#endif
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
|
||||
template <typename T>
|
||||
inline void reverse_horizontal(edge<T>& e) {
|
||||
// swap horizontal edges' top and bottom x's so they follow the natural
|
||||
// progression of the bounds - ie so their xbots will align with the
|
||||
// adjoining lower edge. [Helpful in the process_horizontal() method.]
|
||||
std::swap(e.top.x, e.bot.x);
|
||||
}
|
||||
|
||||
// Make a list start on a local maximum by
|
||||
// shifting all the points not on a local maximum to the
|
||||
template <typename T>
|
||||
void start_list_on_local_maximum(edge_list<T>& edges) {
|
||||
if (edges.size() <= 2) {
|
||||
return;
|
||||
}
|
||||
// Find the first local maximum going forward in the list
|
||||
auto prev_edge = edges.end();
|
||||
--prev_edge;
|
||||
bool prev_edge_is_horizontal = is_horizontal(*prev_edge);
|
||||
auto edge = edges.begin();
|
||||
bool edge_is_horizontal;
|
||||
bool y_decreasing_before_last_horizontal = false; // assume false at start
|
||||
|
||||
while (edge != edges.end()) {
|
||||
edge_is_horizontal = is_horizontal(*edge);
|
||||
if ((!prev_edge_is_horizontal && !edge_is_horizontal && edge->top == prev_edge->top)) {
|
||||
break;
|
||||
}
|
||||
if (!edge_is_horizontal && prev_edge_is_horizontal) {
|
||||
if (y_decreasing_before_last_horizontal &&
|
||||
(edge->top == prev_edge->bot || edge->top == prev_edge->top)) {
|
||||
break;
|
||||
}
|
||||
} else if (!y_decreasing_before_last_horizontal && !prev_edge_is_horizontal &&
|
||||
edge_is_horizontal &&
|
||||
(prev_edge->top == edge->top || prev_edge->top == edge->bot)) {
|
||||
y_decreasing_before_last_horizontal = true;
|
||||
}
|
||||
prev_edge_is_horizontal = edge_is_horizontal;
|
||||
prev_edge = edge;
|
||||
++edge;
|
||||
}
|
||||
std::rotate(edges.begin(), edge, edges.end());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bound<T> create_bound_towards_minimum(edge_list<T>& edges) {
|
||||
if (edges.size() == 1) {
|
||||
if (is_horizontal(edges.front())) {
|
||||
reverse_horizontal(edges.front());
|
||||
}
|
||||
bound<T> bnd;
|
||||
std::swap(bnd.edges, edges);
|
||||
return bnd;
|
||||
}
|
||||
auto next_edge = edges.begin();
|
||||
auto edge = next_edge;
|
||||
++next_edge;
|
||||
bool edge_is_horizontal = is_horizontal(*edge);
|
||||
if (edge_is_horizontal) {
|
||||
reverse_horizontal(*edge);
|
||||
}
|
||||
bool next_edge_is_horizontal;
|
||||
bool y_increasing_before_last_horizontal = false; // assume false at start
|
||||
|
||||
while (next_edge != edges.end()) {
|
||||
next_edge_is_horizontal = is_horizontal(*next_edge);
|
||||
if ((!next_edge_is_horizontal && !edge_is_horizontal && edge->bot == next_edge->bot)) {
|
||||
break;
|
||||
}
|
||||
if (!next_edge_is_horizontal && edge_is_horizontal) {
|
||||
if (y_increasing_before_last_horizontal &&
|
||||
(next_edge->bot == edge->bot || next_edge->bot == edge->top)) {
|
||||
break;
|
||||
}
|
||||
} else if (!y_increasing_before_last_horizontal && !edge_is_horizontal &&
|
||||
next_edge_is_horizontal &&
|
||||
(edge->bot == next_edge->top || edge->bot == next_edge->bot)) {
|
||||
y_increasing_before_last_horizontal = true;
|
||||
}
|
||||
edge_is_horizontal = next_edge_is_horizontal;
|
||||
edge = next_edge;
|
||||
if (edge_is_horizontal) {
|
||||
reverse_horizontal(*edge);
|
||||
}
|
||||
++next_edge;
|
||||
}
|
||||
bound<T> bnd;
|
||||
if (next_edge == edges.end()) {
|
||||
std::swap(edges, bnd.edges);
|
||||
} else {
|
||||
bnd.edges.reserve(std::distance(edges.begin(), next_edge));
|
||||
std::move(edges.begin(), next_edge, std::back_inserter(bnd.edges));
|
||||
edges.erase(edges.begin(), next_edge);
|
||||
}
|
||||
std::reverse(bnd.edges.begin(), bnd.edges.end());
|
||||
return bnd;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bound<T> create_bound_towards_maximum(edge_list<T>& edges) {
|
||||
if (edges.size() == 1) {
|
||||
bound<T> bnd;
|
||||
std::swap(bnd.edges, edges);
|
||||
return bnd;
|
||||
}
|
||||
auto next_edge = edges.begin();
|
||||
auto edge = next_edge;
|
||||
++next_edge;
|
||||
bool edge_is_horizontal = is_horizontal(*edge);
|
||||
bool next_edge_is_horizontal;
|
||||
bool y_decreasing_before_last_horizontal = false; // assume false at start
|
||||
|
||||
while (next_edge != edges.end()) {
|
||||
next_edge_is_horizontal = is_horizontal(*next_edge);
|
||||
if ((!next_edge_is_horizontal && !edge_is_horizontal && edge->top == next_edge->top)) {
|
||||
break;
|
||||
}
|
||||
if (!next_edge_is_horizontal && edge_is_horizontal) {
|
||||
if (y_decreasing_before_last_horizontal &&
|
||||
(next_edge->top == edge->bot || next_edge->top == edge->top)) {
|
||||
break;
|
||||
}
|
||||
} else if (!y_decreasing_before_last_horizontal && !edge_is_horizontal &&
|
||||
next_edge_is_horizontal &&
|
||||
(edge->top == next_edge->top || edge->top == next_edge->bot)) {
|
||||
y_decreasing_before_last_horizontal = true;
|
||||
}
|
||||
edge_is_horizontal = next_edge_is_horizontal;
|
||||
edge = next_edge;
|
||||
++next_edge;
|
||||
}
|
||||
bound<T> bnd;
|
||||
if (next_edge == edges.end()) {
|
||||
std::swap(bnd.edges, edges);
|
||||
} else {
|
||||
bnd.edges.reserve(std::distance(edges.begin(), next_edge));
|
||||
std::move(edges.begin(), next_edge, std::back_inserter(bnd.edges));
|
||||
edges.erase(edges.begin(), next_edge);
|
||||
}
|
||||
return bnd;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void fix_horizontals(bound<T>& bnd) {
|
||||
|
||||
auto edge_itr = bnd.edges.begin();
|
||||
auto next_itr = std::next(edge_itr);
|
||||
if (next_itr == bnd.edges.end()) {
|
||||
return;
|
||||
}
|
||||
if (is_horizontal(*edge_itr) && next_itr->bot != edge_itr->top) {
|
||||
reverse_horizontal(*edge_itr);
|
||||
}
|
||||
auto prev_itr = edge_itr++;
|
||||
while (edge_itr != bnd.edges.end()) {
|
||||
if (is_horizontal(*edge_itr) && prev_itr->top != edge_itr->bot) {
|
||||
reverse_horizontal(*edge_itr);
|
||||
}
|
||||
prev_itr = edge_itr;
|
||||
++edge_itr;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void move_horizontals_on_left_to_right(bound<T>& left_bound, bound<T>& right_bound) {
|
||||
// We want all the horizontal segments that are at the same Y as the minimum to be on the right
|
||||
// bound
|
||||
auto edge_itr = left_bound.edges.begin();
|
||||
while (edge_itr != left_bound.edges.end()) {
|
||||
if (!is_horizontal(*edge_itr)) {
|
||||
break;
|
||||
}
|
||||
reverse_horizontal(*edge_itr);
|
||||
++edge_itr;
|
||||
}
|
||||
if (edge_itr == left_bound.edges.begin()) {
|
||||
return;
|
||||
}
|
||||
std::reverse(left_bound.edges.begin(), edge_itr);
|
||||
auto dist = std::distance(left_bound.edges.begin(), edge_itr);
|
||||
std::move(left_bound.edges.begin(), edge_itr, std::back_inserter(right_bound.edges));
|
||||
left_bound.edges.erase(left_bound.edges.begin(), edge_itr);
|
||||
std::rotate(right_bound.edges.begin(), std::prev(right_bound.edges.end(), dist),
|
||||
right_bound.edges.end());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void add_ring_to_local_minima_list(edge_list<T>& edges,
|
||||
local_minimum_list<T>& minima_list,
|
||||
polygon_type poly_type) {
|
||||
|
||||
if (edges.empty()) {
|
||||
return;
|
||||
}
|
||||
// Adjust the order of the ring so we start on a local maximum
|
||||
// therefore we start right away on a bound.
|
||||
start_list_on_local_maximum(edges);
|
||||
|
||||
bound_ptr<T> first_minimum = nullptr;
|
||||
bound_ptr<T> last_maximum = nullptr;
|
||||
while (!edges.empty()) {
|
||||
bool lm_minimum_has_horizontal = false;
|
||||
auto to_minimum = create_bound_towards_minimum(edges);
|
||||
if (edges.empty()) {
|
||||
throw std::runtime_error("Edges is empty after only creating a single bound.");
|
||||
}
|
||||
auto to_maximum = create_bound_towards_maximum(edges);
|
||||
fix_horizontals(to_minimum);
|
||||
fix_horizontals(to_maximum);
|
||||
auto to_max_first_non_horizontal = to_maximum.edges.begin();
|
||||
auto to_min_first_non_horizontal = to_minimum.edges.begin();
|
||||
bool minimum_is_left = true;
|
||||
while (to_max_first_non_horizontal != to_maximum.edges.end() &&
|
||||
is_horizontal(*to_max_first_non_horizontal)) {
|
||||
lm_minimum_has_horizontal = true;
|
||||
++to_max_first_non_horizontal;
|
||||
}
|
||||
while (to_min_first_non_horizontal != to_minimum.edges.end() &&
|
||||
is_horizontal(*to_min_first_non_horizontal)) {
|
||||
lm_minimum_has_horizontal = true;
|
||||
++to_min_first_non_horizontal;
|
||||
}
|
||||
#ifdef DEBUG
|
||||
if (to_max_first_non_horizontal == to_maximum.edges.end() ||
|
||||
to_min_first_non_horizontal == to_minimum.edges.end()) {
|
||||
throw std::runtime_error("should not have a horizontal only bound for a ring");
|
||||
}
|
||||
#endif
|
||||
if (lm_minimum_has_horizontal) {
|
||||
if (to_max_first_non_horizontal->bot.x > to_min_first_non_horizontal->bot.x) {
|
||||
minimum_is_left = true;
|
||||
move_horizontals_on_left_to_right(to_minimum, to_maximum);
|
||||
} else {
|
||||
minimum_is_left = false;
|
||||
move_horizontals_on_left_to_right(to_maximum, to_minimum);
|
||||
}
|
||||
} else {
|
||||
if (to_max_first_non_horizontal->dx > to_min_first_non_horizontal->dx) {
|
||||
minimum_is_left = false;
|
||||
} else {
|
||||
minimum_is_left = true;
|
||||
}
|
||||
}
|
||||
assert(!to_minimum.edges.empty());
|
||||
assert(!to_maximum.edges.empty());
|
||||
auto const& min_front = to_minimum.edges.front();
|
||||
if (last_maximum) {
|
||||
to_minimum.maximum_bound = last_maximum;
|
||||
}
|
||||
to_minimum.poly_type = poly_type;
|
||||
to_maximum.poly_type = poly_type;
|
||||
if (!minimum_is_left) {
|
||||
to_minimum.side = edge_right;
|
||||
to_maximum.side = edge_left;
|
||||
to_minimum.winding_delta = -1;
|
||||
to_maximum.winding_delta = 1;
|
||||
minima_list.emplace_back(std::move(to_maximum), std::move(to_minimum), min_front.bot.y,
|
||||
lm_minimum_has_horizontal);
|
||||
if (!last_maximum) {
|
||||
first_minimum = &(minima_list.back().right_bound);
|
||||
} else {
|
||||
last_maximum->maximum_bound = &(minima_list.back().right_bound);
|
||||
}
|
||||
last_maximum = &(minima_list.back().left_bound);
|
||||
} else {
|
||||
to_minimum.side = edge_left;
|
||||
to_maximum.side = edge_right;
|
||||
to_minimum.winding_delta = -1;
|
||||
to_maximum.winding_delta = 1;
|
||||
minima_list.emplace_back(std::move(to_minimum), std::move(to_maximum), min_front.bot.y,
|
||||
lm_minimum_has_horizontal);
|
||||
if (!last_maximum) {
|
||||
first_minimum = &(minima_list.back().left_bound);
|
||||
} else {
|
||||
last_maximum->maximum_bound = &(minima_list.back().left_bound);
|
||||
}
|
||||
last_maximum = &(minima_list.back().right_bound);
|
||||
}
|
||||
}
|
||||
last_maximum->maximum_bound = first_minimum;
|
||||
first_minimum->maximum_bound = last_maximum;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void initialize_lm(local_minimum_ptr_list_itr<T>& lm) {
|
||||
if (!(*lm)->left_bound.edges.empty()) {
|
||||
(*lm)->left_bound.current_edge = (*lm)->left_bound.edges.begin();
|
||||
(*lm)->left_bound.next_edge = std::next((*lm)->left_bound.current_edge);
|
||||
(*lm)->left_bound.current_x = static_cast<double>((*lm)->left_bound.current_edge->bot.x);
|
||||
(*lm)->left_bound.winding_count = 0;
|
||||
(*lm)->left_bound.winding_count2 = 0;
|
||||
(*lm)->left_bound.side = edge_left;
|
||||
(*lm)->left_bound.ring = nullptr;
|
||||
}
|
||||
if (!(*lm)->right_bound.edges.empty()) {
|
||||
(*lm)->right_bound.current_edge = (*lm)->right_bound.edges.begin();
|
||||
(*lm)->right_bound.next_edge = std::next((*lm)->right_bound.current_edge);
|
||||
(*lm)->right_bound.current_x = static_cast<double>((*lm)->right_bound.current_edge->bot.x);
|
||||
(*lm)->right_bound.winding_count = 0;
|
||||
(*lm)->right_bound.winding_count2 = 0;
|
||||
(*lm)->right_bound.side = edge_right;
|
||||
(*lm)->right_bound.ring = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
111
mapbox/geometry/wagyu/point.hpp
Normal file
111
mapbox/geometry/wagyu/point.hpp
Normal file
@ -0,0 +1,111 @@
|
||||
#pragma once
|
||||
|
||||
#include <mapbox/geometry/point.hpp>
|
||||
|
||||
#ifdef DEBUG
|
||||
#include <iostream>
|
||||
#endif
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
|
||||
template <typename T>
|
||||
struct point;
|
||||
|
||||
template <typename T>
|
||||
using point_ptr = point<T>*;
|
||||
|
||||
template <typename T>
|
||||
using const_point_ptr = point<T>* const;
|
||||
|
||||
template <typename T>
|
||||
struct ring;
|
||||
|
||||
template <typename T>
|
||||
using ring_ptr = ring<T>*;
|
||||
|
||||
template <typename T>
|
||||
using const_ring_ptr = ring<T>* const;
|
||||
|
||||
template <typename T>
|
||||
struct point {
|
||||
using coordinate_type = T;
|
||||
ring_ptr<T> ring;
|
||||
T x;
|
||||
T y;
|
||||
point_ptr<T> next;
|
||||
point_ptr<T> prev;
|
||||
|
||||
point(point<T>&& p)
|
||||
: ring(std::move(p.ring)),
|
||||
x(std::move(p.x)),
|
||||
y(std::move(p.y)),
|
||||
next(std::move(p.next)),
|
||||
prev(std::move(p.prev)) {
|
||||
}
|
||||
|
||||
point() : ring(nullptr), x(0), y(0), prev(this), next(this) {
|
||||
}
|
||||
point(T x_, T y_) : ring(nullptr), x(x_), y(y_), next(this), prev(this) {
|
||||
}
|
||||
point(ring_ptr<T> ring_, mapbox::geometry::point<T> const& pt)
|
||||
: ring(ring_), x(pt.x), y(pt.y), next(this), prev(this) {
|
||||
}
|
||||
|
||||
point(ring_ptr<T> ring_, mapbox::geometry::point<T> const& pt, point_ptr<T> before_this_point)
|
||||
: ring(ring_), x(pt.x), y(pt.y), next(before_this_point), prev(before_this_point->prev) {
|
||||
before_this_point->prev = this;
|
||||
prev->next = this;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
bool operator==(point<T> const& lhs, point<T> const& rhs) {
|
||||
return lhs.x == rhs.x && lhs.y == rhs.y;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool operator==(mapbox::geometry::point<T> const& lhs, point<T> const& rhs) {
|
||||
return lhs.x == rhs.x && lhs.y == rhs.y;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool operator==(point<T> const& lhs, mapbox::geometry::point<T> const& rhs) {
|
||||
return lhs.x == rhs.x && lhs.y == rhs.y;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool operator!=(point<T> const& lhs, point<T> const& rhs) {
|
||||
return lhs.x != rhs.x || lhs.y != rhs.y;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool operator!=(mapbox::geometry::point<T> const& lhs, point<T> const& rhs) {
|
||||
return lhs.x != rhs.x || lhs.y != rhs.y;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool operator!=(point<T> const& lhs, mapbox::geometry::point<T> const& rhs) {
|
||||
return lhs.x != rhs.x || lhs.y != rhs.y;
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
|
||||
template <class charT, class traits, typename T>
|
||||
inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& out,
|
||||
const point<T>& p) {
|
||||
out << " point at: " << p.x << ", " << p.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
template <class charT, class traits, typename T>
|
||||
inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& out,
|
||||
const mapbox::geometry::point<T>& p) {
|
||||
out << " point at: " << p.x << ", " << p.y;
|
||||
return out;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
273
mapbox/geometry/wagyu/process_horizontal.hpp
Normal file
273
mapbox/geometry/wagyu/process_horizontal.hpp
Normal file
@ -0,0 +1,273 @@
|
||||
#pragma once
|
||||
|
||||
#include <mapbox/geometry/line_string.hpp>
|
||||
#include <mapbox/geometry/point.hpp>
|
||||
#include <mapbox/geometry/polygon.hpp>
|
||||
|
||||
#include <mapbox/geometry/wagyu/config.hpp>
|
||||
#include <mapbox/geometry/wagyu/edge.hpp>
|
||||
#include <mapbox/geometry/wagyu/local_minimum.hpp>
|
||||
#include <mapbox/geometry/wagyu/util.hpp>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
|
||||
template <typename T>
|
||||
active_bound_list_itr<T> process_horizontal_left_to_right(T scanline_y,
|
||||
active_bound_list_itr<T>& horz_bound,
|
||||
active_bound_list<T>& active_bounds,
|
||||
ring_manager<T>& rings,
|
||||
scanbeam_list<T>& scanbeam,
|
||||
clip_type cliptype,
|
||||
fill_type subject_fill_type,
|
||||
fill_type clip_fill_type) {
|
||||
auto horizontal_itr_behind = horz_bound;
|
||||
bool is_maxima_edge = is_maxima(horz_bound, scanline_y);
|
||||
auto bound_max_pair = active_bounds.end();
|
||||
if (is_maxima_edge) {
|
||||
bound_max_pair = get_maxima_pair<T>(horz_bound, active_bounds);
|
||||
}
|
||||
|
||||
auto hp_itr = rings.current_hp_itr;
|
||||
while (hp_itr != rings.hot_pixels.end() &&
|
||||
(hp_itr->y > scanline_y ||
|
||||
(hp_itr->y == scanline_y && hp_itr->x < (*horz_bound)->current_edge->bot.x))) {
|
||||
++hp_itr;
|
||||
}
|
||||
|
||||
auto bnd = std::next(horz_bound);
|
||||
|
||||
while (bnd != active_bounds.end()) {
|
||||
// this code block inserts extra coords into horizontal edges (in output
|
||||
// polygons) wherever hot pixels touch these horizontal edges. This helps
|
||||
//'simplifying' polygons (ie if the Simplify property is set).
|
||||
while (hp_itr != rings.hot_pixels.end() && hp_itr->y == scanline_y &&
|
||||
hp_itr->x < std::llround((*bnd)->current_x) &&
|
||||
hp_itr->x < (*horz_bound)->current_edge->top.x) {
|
||||
if ((*horz_bound)->ring) {
|
||||
add_point_to_ring(*(*horz_bound), *hp_itr, rings);
|
||||
}
|
||||
++hp_itr;
|
||||
}
|
||||
|
||||
if ((*bnd)->current_x > static_cast<double>((*horz_bound)->current_edge->top.x)) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Also break if we've got to the end of an intermediate horizontal edge ...
|
||||
// nb: Smaller Dx's are to the right of larger Dx's ABOVE the horizontal.
|
||||
if (std::llround((*bnd)->current_x) == (*horz_bound)->current_edge->top.x &&
|
||||
(*horz_bound)->next_edge != (*horz_bound)->edges.end() &&
|
||||
(*horz_bound)->current_edge->dx < (*horz_bound)->next_edge->dx) {
|
||||
break;
|
||||
}
|
||||
|
||||
// note: may be done multiple times
|
||||
if ((*horz_bound)->ring) {
|
||||
add_point_to_ring(
|
||||
*(*horz_bound),
|
||||
mapbox::geometry::point<T>(std::llround((*bnd)->current_x), scanline_y), rings);
|
||||
}
|
||||
|
||||
// OK, so far we're still in range of the horizontal Edge but make sure
|
||||
// we're at the last of consec. horizontals when matching with eMaxPair
|
||||
if (is_maxima_edge && bnd == bound_max_pair) {
|
||||
if ((*horz_bound)->ring) {
|
||||
add_local_maximum_point(horz_bound, bound_max_pair,
|
||||
(*horz_bound)->current_edge->top, rings, active_bounds);
|
||||
}
|
||||
active_bounds.erase(bound_max_pair);
|
||||
auto after_horz = active_bounds.erase(horz_bound);
|
||||
if (horizontal_itr_behind != horz_bound) {
|
||||
return horizontal_itr_behind;
|
||||
} else {
|
||||
return after_horz;
|
||||
}
|
||||
}
|
||||
|
||||
intersect_bounds(horz_bound, bnd,
|
||||
mapbox::geometry::point<T>(std::llround((*bnd)->current_x), scanline_y),
|
||||
cliptype, subject_fill_type, clip_fill_type, rings, active_bounds);
|
||||
auto next_bnd = std::next(bnd);
|
||||
swap_positions_in_ABL(horz_bound, bnd, active_bounds);
|
||||
if (current_edge_is_horizontal<T>(bnd) && horizontal_itr_behind == horz_bound) {
|
||||
horizontal_itr_behind = bnd;
|
||||
}
|
||||
bnd = next_bnd;
|
||||
} // end while (bnd != active_bounds.end())
|
||||
|
||||
if ((*horz_bound)->ring) {
|
||||
while (hp_itr != rings.hot_pixels.end() && hp_itr->y == scanline_y &&
|
||||
hp_itr->x < std::llround((*horz_bound)->current_edge->top.x)) {
|
||||
add_point_to_ring(*(*horz_bound), *hp_itr, rings);
|
||||
++hp_itr;
|
||||
}
|
||||
}
|
||||
|
||||
if ((*horz_bound)->next_edge != (*horz_bound)->edges.end()) {
|
||||
if ((*horz_bound)->ring) {
|
||||
add_point_to_ring(*(*horz_bound), (*horz_bound)->current_edge->top, rings);
|
||||
next_edge_in_bound(horz_bound, scanbeam);
|
||||
} else {
|
||||
next_edge_in_bound(horz_bound, scanbeam);
|
||||
}
|
||||
if (horizontal_itr_behind != horz_bound) {
|
||||
return horizontal_itr_behind;
|
||||
} else {
|
||||
return std::next(horz_bound);
|
||||
}
|
||||
} else {
|
||||
if ((*horz_bound)->ring) {
|
||||
add_point_to_ring(*(*horz_bound), (*horz_bound)->current_edge->top, rings);
|
||||
}
|
||||
auto after_horz = active_bounds.erase(horz_bound);
|
||||
if (horizontal_itr_behind != horz_bound) {
|
||||
return horizontal_itr_behind;
|
||||
} else {
|
||||
return after_horz;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
active_bound_list_itr<T> process_horizontal_right_to_left(T scanline_y,
|
||||
active_bound_list_itr<T>& horz_bound,
|
||||
active_bound_list<T>& active_bounds,
|
||||
ring_manager<T>& rings,
|
||||
scanbeam_list<T>& scanbeam,
|
||||
clip_type cliptype,
|
||||
fill_type subject_fill_type,
|
||||
fill_type clip_fill_type) {
|
||||
bool is_maxima_edge = is_maxima(horz_bound, scanline_y);
|
||||
auto bound_max_pair = active_bounds.end();
|
||||
if (is_maxima_edge) {
|
||||
bound_max_pair = get_maxima_pair<T>(horz_bound, active_bounds);
|
||||
}
|
||||
auto hp_itr_fwd = rings.current_hp_itr;
|
||||
while (hp_itr_fwd != rings.hot_pixels.end() &&
|
||||
(hp_itr_fwd->y < scanline_y ||
|
||||
(hp_itr_fwd->y == scanline_y && hp_itr_fwd->x < (*horz_bound)->current_edge->top.x))) {
|
||||
++hp_itr_fwd;
|
||||
}
|
||||
auto hp_itr = hot_pixel_rev_itr<T>(hp_itr_fwd);
|
||||
|
||||
auto bnd = active_bound_list_rev_itr<T>(horz_bound);
|
||||
while (bnd != active_bounds.rend()) {
|
||||
// this code block inserts extra coords into horizontal edges (in output
|
||||
// polygons) wherever hot pixels touch these horizontal edges.
|
||||
while (hp_itr != rings.hot_pixels.rend() && hp_itr->y == scanline_y &&
|
||||
hp_itr->x > std::llround((*bnd)->current_x) &&
|
||||
hp_itr->x > (*horz_bound)->current_edge->top.x) {
|
||||
if ((*horz_bound)->ring) {
|
||||
add_point_to_ring(*(*horz_bound), *hp_itr, rings);
|
||||
}
|
||||
++hp_itr;
|
||||
}
|
||||
|
||||
if ((*bnd)->current_x < static_cast<double>((*horz_bound)->current_edge->top.x)) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Also break if we've got to the end of an intermediate horizontal edge ...
|
||||
// nb: Smaller Dx's are to the right of larger Dx's ABOVE the horizontal.
|
||||
if (std::llround((*bnd)->current_x) == (*horz_bound)->current_edge->top.x &&
|
||||
(*horz_bound)->next_edge != (*horz_bound)->edges.end() &&
|
||||
(*horz_bound)->current_edge->dx < (*horz_bound)->next_edge->dx) {
|
||||
break;
|
||||
}
|
||||
|
||||
// note: may be done multiple times
|
||||
if ((*horz_bound)->ring) {
|
||||
add_point_to_ring(
|
||||
*(*horz_bound),
|
||||
mapbox::geometry::point<T>(std::llround((*bnd)->current_x), scanline_y), rings);
|
||||
}
|
||||
auto bnd_forward = --(bnd.base());
|
||||
|
||||
// OK, so far we're still in range of the horizontal Edge but make sure
|
||||
// we're at the last of consec. horizontals when matching with eMaxPair
|
||||
if (is_maxima_edge && bnd_forward == bound_max_pair) {
|
||||
if ((*horz_bound)->ring) {
|
||||
add_local_maximum_point(horz_bound, bound_max_pair,
|
||||
(*horz_bound)->current_edge->top, rings, active_bounds);
|
||||
}
|
||||
active_bounds.erase(bound_max_pair);
|
||||
return active_bounds.erase(horz_bound);
|
||||
}
|
||||
|
||||
intersect_bounds(bnd_forward, horz_bound,
|
||||
mapbox::geometry::point<T>(std::llround((*bnd)->current_x), scanline_y),
|
||||
cliptype, subject_fill_type, clip_fill_type, rings, active_bounds);
|
||||
swap_positions_in_ABL(horz_bound, bnd_forward, active_bounds);
|
||||
// Why are we not incrementing the bnd iterator here:
|
||||
// It is because reverse iterators point to a `base()` iterator that is a forward
|
||||
// iterator that is one ahead of the reverse bound. This will always be the horizontal
|
||||
// bound,
|
||||
// so what the reverse bound points to will have changed.
|
||||
} // end while (bnd != active_bounds.rend())
|
||||
|
||||
if ((*horz_bound)->ring) {
|
||||
while (hp_itr != rings.hot_pixels.rend() && hp_itr->y == scanline_y &&
|
||||
hp_itr->x > (*horz_bound)->current_edge->top.x) {
|
||||
add_point_to_ring(*(*horz_bound), *hp_itr, rings);
|
||||
++hp_itr;
|
||||
}
|
||||
}
|
||||
|
||||
if ((*horz_bound)->next_edge != (*horz_bound)->edges.end()) {
|
||||
if ((*horz_bound)->ring) {
|
||||
add_point_to_ring(*(*horz_bound), (*horz_bound)->current_edge->top, rings);
|
||||
next_edge_in_bound(horz_bound, scanbeam);
|
||||
} else {
|
||||
next_edge_in_bound(horz_bound, scanbeam);
|
||||
}
|
||||
return std::next(horz_bound);
|
||||
} else {
|
||||
if ((*horz_bound)->ring) {
|
||||
add_point_to_ring(*(*horz_bound), (*horz_bound)->current_edge->top, rings);
|
||||
}
|
||||
return active_bounds.erase(horz_bound);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
active_bound_list_itr<T> process_horizontal(T scanline_y,
|
||||
active_bound_list_itr<T>& horz_bound,
|
||||
active_bound_list<T>& active_bounds,
|
||||
ring_manager<T>& rings,
|
||||
scanbeam_list<T>& scanbeam,
|
||||
clip_type cliptype,
|
||||
fill_type subject_fill_type,
|
||||
fill_type clip_fill_type) {
|
||||
if ((*horz_bound)->current_edge->bot.x < (*horz_bound)->current_edge->top.x) {
|
||||
return process_horizontal_left_to_right(scanline_y, horz_bound, active_bounds, rings,
|
||||
scanbeam, cliptype, subject_fill_type,
|
||||
clip_fill_type);
|
||||
} else {
|
||||
return process_horizontal_right_to_left(scanline_y, horz_bound, active_bounds, rings,
|
||||
scanbeam, cliptype, subject_fill_type,
|
||||
clip_fill_type);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void process_horizontals(T scanline_y,
|
||||
active_bound_list<T>& active_bounds,
|
||||
ring_manager<T>& rings,
|
||||
scanbeam_list<T>& scanbeam,
|
||||
clip_type cliptype,
|
||||
fill_type subject_fill_type,
|
||||
fill_type clip_fill_type) {
|
||||
for (auto bnd_itr = active_bounds.begin(); bnd_itr != active_bounds.end();) {
|
||||
if (current_edge_is_horizontal<T>(bnd_itr)) {
|
||||
bnd_itr = process_horizontal(scanline_y, bnd_itr, active_bounds, rings, scanbeam,
|
||||
cliptype, subject_fill_type, clip_fill_type);
|
||||
} else {
|
||||
++bnd_itr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
125
mapbox/geometry/wagyu/process_maxima.hpp
Normal file
125
mapbox/geometry/wagyu/process_maxima.hpp
Normal file
@ -0,0 +1,125 @@
|
||||
#pragma once
|
||||
|
||||
#include <mapbox/geometry/wagyu/active_bound_list.hpp>
|
||||
#include <mapbox/geometry/wagyu/config.hpp>
|
||||
#include <mapbox/geometry/wagyu/edge.hpp>
|
||||
#include <mapbox/geometry/wagyu/intersect_util.hpp>
|
||||
#include <mapbox/geometry/wagyu/local_minimum.hpp>
|
||||
#include <mapbox/geometry/wagyu/local_minimum_util.hpp>
|
||||
#include <mapbox/geometry/wagyu/process_horizontal.hpp>
|
||||
#include <mapbox/geometry/wagyu/ring.hpp>
|
||||
#include <mapbox/geometry/wagyu/ring_util.hpp>
|
||||
#include <mapbox/geometry/wagyu/topology_correction.hpp>
|
||||
#include <mapbox/geometry/wagyu/util.hpp>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
|
||||
template <typename T>
|
||||
active_bound_list_itr<T> do_maxima(active_bound_list_itr<T>& bnd,
|
||||
active_bound_list_itr<T>& bndMaxPair,
|
||||
clip_type cliptype,
|
||||
fill_type subject_fill_type,
|
||||
fill_type clip_fill_type,
|
||||
ring_manager<T>& rings,
|
||||
active_bound_list<T>& active_bounds) {
|
||||
if (bndMaxPair == active_bounds.end()) {
|
||||
if ((*bnd)->ring) {
|
||||
add_point_to_ring(*(*bnd), (*bnd)->current_edge->top, rings);
|
||||
}
|
||||
return active_bounds.erase(bnd);
|
||||
}
|
||||
auto bnd_next = std::next(bnd);
|
||||
auto return_bnd = bnd_next;
|
||||
bool skipped = false;
|
||||
while (bnd_next != active_bounds.end() && bnd_next != bndMaxPair) {
|
||||
skipped = true;
|
||||
intersect_bounds(bnd, bnd_next, (*bnd)->current_edge->top, cliptype, subject_fill_type,
|
||||
clip_fill_type, rings, active_bounds);
|
||||
swap_positions_in_ABL(bnd, bnd_next, active_bounds);
|
||||
bnd_next = std::next(bnd);
|
||||
}
|
||||
|
||||
if (!(*bnd)->ring && !(*bndMaxPair)->ring) {
|
||||
active_bounds.erase(bndMaxPair);
|
||||
} else if ((*bnd)->ring && (*bndMaxPair)->ring) {
|
||||
add_local_maximum_point(bnd, bndMaxPair, (*bnd)->current_edge->top, rings, active_bounds);
|
||||
active_bounds.erase(bndMaxPair);
|
||||
} else {
|
||||
throw std::runtime_error("DoMaxima error");
|
||||
}
|
||||
auto prev_itr = active_bounds.erase(bnd);
|
||||
if (skipped) {
|
||||
return return_bnd;
|
||||
} else {
|
||||
return prev_itr;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void process_edges_at_top_of_scanbeam(T top_y,
|
||||
active_bound_list<T>& active_bounds,
|
||||
scanbeam_list<T>& scanbeam,
|
||||
local_minimum_ptr_list<T> const& minima_sorted,
|
||||
local_minimum_ptr_list_itr<T>& current_lm,
|
||||
ring_manager<T>& rings,
|
||||
clip_type cliptype,
|
||||
fill_type subject_fill_type,
|
||||
fill_type clip_fill_type) {
|
||||
|
||||
for (auto bnd = active_bounds.begin(); bnd != active_bounds.end();) {
|
||||
// 1. Process maxima, treating them as if they are "bent" horizontal edges,
|
||||
// but exclude maxima with horizontal edges.
|
||||
|
||||
bool is_maxima_edge = is_maxima(bnd, top_y);
|
||||
|
||||
if (is_maxima_edge) {
|
||||
auto bnd_max_pair = get_maxima_pair(bnd, active_bounds);
|
||||
is_maxima_edge = ((bnd_max_pair == active_bounds.end() ||
|
||||
!current_edge_is_horizontal<T>(bnd_max_pair)) &&
|
||||
is_maxima(bnd_max_pair, top_y));
|
||||
if (is_maxima_edge) {
|
||||
bnd = do_maxima(bnd, bnd_max_pair, cliptype, subject_fill_type, clip_fill_type,
|
||||
rings, active_bounds);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Promote horizontal edges.
|
||||
if (is_intermediate(bnd, top_y) && next_edge_is_horizontal<T>(bnd)) {
|
||||
if ((*bnd)->ring) {
|
||||
insert_hot_pixels_in_path(*(*bnd), (*bnd)->current_edge->top, rings, false);
|
||||
}
|
||||
next_edge_in_bound(bnd, scanbeam);
|
||||
if ((*bnd)->ring) {
|
||||
add_point_to_ring(*(*bnd), (*bnd)->current_edge->bot, rings);
|
||||
}
|
||||
} else {
|
||||
(*bnd)->current_x = get_current_x(*((*bnd)->current_edge), top_y);
|
||||
}
|
||||
|
||||
++bnd;
|
||||
}
|
||||
|
||||
insert_horizontal_local_minima_into_ABL(top_y, minima_sorted, current_lm, active_bounds, rings,
|
||||
scanbeam, cliptype, subject_fill_type, clip_fill_type);
|
||||
|
||||
process_horizontals(top_y, active_bounds, rings, scanbeam, cliptype, subject_fill_type,
|
||||
clip_fill_type);
|
||||
|
||||
// 4. Promote intermediate vertices
|
||||
|
||||
for (auto bnd = active_bounds.begin(); bnd != active_bounds.end(); ++bnd) {
|
||||
if (is_intermediate(bnd, top_y)) {
|
||||
if ((*bnd)->ring) {
|
||||
add_point_to_ring(*(*bnd), (*bnd)->current_edge->top, rings);
|
||||
insert_hot_pixels_in_path(*(*bnd), (*bnd)->current_edge->top, rings, false);
|
||||
}
|
||||
next_edge_in_bound(bnd, scanbeam);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
137
mapbox/geometry/wagyu/quick_clip.hpp
Normal file
137
mapbox/geometry/wagyu/quick_clip.hpp
Normal file
@ -0,0 +1,137 @@
|
||||
#pragma once
|
||||
|
||||
#include <mapbox/geometry/box.hpp>
|
||||
#include <mapbox/geometry/multi_polygon.hpp>
|
||||
#include <mapbox/geometry/polygon.hpp>
|
||||
#include <mapbox/geometry/wagyu/wagyu.hpp>
|
||||
|
||||
#include <experimental/optional>
|
||||
|
||||
template <typename T>
|
||||
using optional_linear_ring = std::experimental::optional<mapbox::geometry::linear_ring<T>>;
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
namespace quick_clip {
|
||||
|
||||
template <typename T>
|
||||
mapbox::geometry::point<T> intersect(mapbox::geometry::point<T> a,
|
||||
mapbox::geometry::point<T> b,
|
||||
size_t edge,
|
||||
mapbox::geometry::box<T> const& box) {
|
||||
switch (edge) {
|
||||
case 0:
|
||||
return mapbox::geometry::point<T>(
|
||||
static_cast<T>(a.x + static_cast<double>(b.x - a.x) * (box.min.y - a.y) / (b.y - a.y)),
|
||||
box.min.y);
|
||||
|
||||
case 1:
|
||||
return mapbox::geometry::point<T>(
|
||||
box.max.x,
|
||||
static_cast<T>(a.y + static_cast<double>(b.y - a.y) * (box.max.x - a.x) / (b.x - a.x)));
|
||||
|
||||
case 2:
|
||||
return mapbox::geometry::point<T>(
|
||||
static_cast<T>(a.x + static_cast<double>(b.x - a.x) * (box.max.y - a.y) / (b.y - a.y)),
|
||||
box.max.y);
|
||||
|
||||
default: // case 3
|
||||
return mapbox::geometry::point<T>(
|
||||
box.min.x,
|
||||
static_cast<T>(a.y + static_cast<double>(b.y - a.y) * (box.min.x - a.x) / (b.x - a.x)));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool inside(mapbox::geometry::point<T> p, size_t edge, mapbox::geometry::box<T> const& b) {
|
||||
switch (edge) {
|
||||
case 0:
|
||||
return p.y > b.min.y;
|
||||
|
||||
case 1:
|
||||
return p.x < b.max.x;
|
||||
|
||||
case 2:
|
||||
return p.y < b.max.y;
|
||||
|
||||
default: // case 3
|
||||
return p.x > b.min.x;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
optional_linear_ring<T> quick_lr_clip(mapbox::geometry::linear_ring<T> const& ring,
|
||||
mapbox::geometry::box<T> const& b) {
|
||||
mapbox::geometry::linear_ring<T> out = ring;
|
||||
|
||||
for (size_t edge = 0; edge < 4; edge++) {
|
||||
if (out.size() > 0) {
|
||||
mapbox::geometry::linear_ring<T> in = out;
|
||||
mapbox::geometry::point<T> S = in[in.size() - 1];
|
||||
out.resize(0);
|
||||
|
||||
for (size_t e = 0; e < in.size(); e++) {
|
||||
mapbox::geometry::point<T> E = in[e];
|
||||
|
||||
if (inside(E, edge, b)) {
|
||||
if (!inside(S, edge, b)) {
|
||||
out.push_back(intersect(S, E, edge, b));
|
||||
}
|
||||
out.push_back(E);
|
||||
} else if (inside(S, edge, b)) {
|
||||
out.push_back(intersect(S, E, edge, b));
|
||||
}
|
||||
|
||||
S = E;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (out.size() < 3) {
|
||||
return optional_linear_ring<T>();
|
||||
}
|
||||
// Close the ring if the first/last point was outside
|
||||
if (out[0] != out[out.size() - 1]) {
|
||||
out.push_back(out[0]);
|
||||
}
|
||||
return optional_linear_ring<T>(std::move(out));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
mapbox::geometry::multi_polygon<T> clip(mapbox::geometry::polygon<T> const& poly,
|
||||
mapbox::geometry::box<T> const& b,
|
||||
fill_type subject_fill_type) {
|
||||
mapbox::geometry::multi_polygon<T> result;
|
||||
wagyu<T> clipper;
|
||||
for (auto const& lr : poly) {
|
||||
auto new_lr = quick_clip::quick_lr_clip(lr, b);
|
||||
if (new_lr) {
|
||||
clipper.add_ring(*new_lr, polygon_type_subject);
|
||||
}
|
||||
}
|
||||
clipper.execute(clip_type_union, result, subject_fill_type, fill_type_even_odd);
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
mapbox::geometry::multi_polygon<T> clip(mapbox::geometry::multi_polygon<T> const& mp,
|
||||
mapbox::geometry::box<T> const& b,
|
||||
fill_type subject_fill_type) {
|
||||
mapbox::geometry::multi_polygon<T> result;
|
||||
wagyu<T> clipper;
|
||||
for (auto const& poly : mp) {
|
||||
for (auto const& lr : poly) {
|
||||
auto new_lr = quick_clip::quick_lr_clip(lr, b);
|
||||
if (new_lr) {
|
||||
clipper.add_ring(*new_lr, polygon_type_subject);
|
||||
}
|
||||
}
|
||||
}
|
||||
clipper.execute(clip_type_union, result, subject_fill_type, fill_type_even_odd);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
475
mapbox/geometry/wagyu/ring.hpp
Normal file
475
mapbox/geometry/wagyu/ring.hpp
Normal file
@ -0,0 +1,475 @@
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <cmath>
|
||||
#include <deque>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <mapbox/geometry/wagyu/point.hpp>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
#ifdef DEBUG
|
||||
#include <execinfo.h>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <stdio.h>
|
||||
//
|
||||
// void* callstack[128];
|
||||
// int i, frames = backtrace(callstack, 128);
|
||||
// char** strs = backtrace_symbols(callstack, frames);
|
||||
// for (i = 0; i < frames; ++i) {
|
||||
// printf("%s\n", strs[i]);
|
||||
// }
|
||||
// free(strs);
|
||||
#endif
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
|
||||
// NOTE: ring and ring_ptr are forward declared in wagyu/point.hpp
|
||||
|
||||
template <typename T>
|
||||
using ring_vector = std::vector<ring_ptr<T>>;
|
||||
|
||||
template <typename T>
|
||||
using ring_list = std::list<ring_ptr<T>>;
|
||||
|
||||
template <typename T>
|
||||
struct ring {
|
||||
std::size_t ring_index; // To support unset 0 is undefined and indexes offset by 1
|
||||
std::size_t size;
|
||||
double area;
|
||||
ring_ptr<T> parent;
|
||||
ring_list<T> children;
|
||||
point_ptr<T> points;
|
||||
point_ptr<T> bottom_point;
|
||||
|
||||
ring(ring const&) = delete;
|
||||
ring& operator=(ring const&) = delete;
|
||||
|
||||
ring()
|
||||
: ring_index(0),
|
||||
size(0),
|
||||
area(std::numeric_limits<double>::quiet_NaN()),
|
||||
parent(nullptr),
|
||||
children(),
|
||||
points(nullptr),
|
||||
bottom_point(nullptr) {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
using hot_pixel_vector = std::vector<mapbox::geometry::point<T>>;
|
||||
|
||||
template <typename T>
|
||||
using hot_pixel_itr = typename hot_pixel_vector<T>::iterator;
|
||||
|
||||
template <typename T>
|
||||
using hot_pixel_rev_itr = typename hot_pixel_vector<T>::reverse_iterator;
|
||||
|
||||
template <typename T>
|
||||
struct ring_manager {
|
||||
|
||||
ring_list<T> children;
|
||||
std::vector<point_ptr<T>> all_points;
|
||||
hot_pixel_vector<T> hot_pixels;
|
||||
hot_pixel_itr<T> current_hp_itr;
|
||||
std::deque<point<T>> points;
|
||||
std::deque<ring<T>> rings;
|
||||
std::vector<point<T>> storage;
|
||||
std::size_t index;
|
||||
|
||||
ring_manager(ring_manager const&) = delete;
|
||||
ring_manager& operator=(ring_manager const&) = delete;
|
||||
|
||||
ring_manager()
|
||||
: children(),
|
||||
all_points(),
|
||||
hot_pixels(),
|
||||
current_hp_itr(hot_pixels.end()),
|
||||
points(),
|
||||
rings(),
|
||||
storage(),
|
||||
index(0) {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void preallocate_point_memory(ring_manager<T>& rings, std::size_t size) {
|
||||
rings.storage.reserve(size);
|
||||
rings.all_points.reserve(size);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
ring_ptr<T> create_new_ring(ring_manager<T>& rings) {
|
||||
rings.rings.emplace_back();
|
||||
ring_ptr<T> result = &rings.rings.back();
|
||||
result->ring_index = rings.index++;
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
point_ptr<T>
|
||||
create_new_point(ring_ptr<T> r, mapbox::geometry::point<T> const& pt, ring_manager<T>& rings) {
|
||||
point_ptr<T> point;
|
||||
if (rings.storage.size() < rings.storage.capacity()) {
|
||||
rings.storage.emplace_back(r, pt);
|
||||
point = &rings.storage.back();
|
||||
} else {
|
||||
rings.points.emplace_back(r, pt);
|
||||
point = &rings.points.back();
|
||||
}
|
||||
rings.all_points.push_back(point);
|
||||
return point;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
point_ptr<T> create_new_point(ring_ptr<T> r,
|
||||
mapbox::geometry::point<T> const& pt,
|
||||
point_ptr<T> before_this_point,
|
||||
ring_manager<T>& rings) {
|
||||
point_ptr<T> point;
|
||||
if (rings.storage.size() < rings.storage.capacity()) {
|
||||
rings.storage.emplace_back(r, pt, before_this_point);
|
||||
point = &rings.storage.back();
|
||||
} else {
|
||||
rings.points.emplace_back(r, pt, before_this_point);
|
||||
point = &rings.points.back();
|
||||
}
|
||||
rings.all_points.push_back(point);
|
||||
return point;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void ring1_child_of_ring2(ring_ptr<T> ring1, ring_ptr<T> ring2, ring_manager<T>& manager) {
|
||||
assert(ring1 != ring2);
|
||||
if (ring1->parent == ring2) {
|
||||
return;
|
||||
}
|
||||
if (ring1->parent == nullptr) {
|
||||
manager.children.remove(ring1);
|
||||
} else {
|
||||
ring1->parent->children.remove(ring1);
|
||||
}
|
||||
if (ring2 == nullptr) {
|
||||
ring1->parent = nullptr;
|
||||
manager.children.push_back(ring1);
|
||||
} else {
|
||||
ring1->parent = ring2;
|
||||
ring2->children.push_back(ring1);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void ring1_sibling_of_ring2(ring_ptr<T> ring1, ring_ptr<T> ring2, ring_manager<T>& manager) {
|
||||
assert(ring1 != ring2);
|
||||
if (ring1->parent == ring2->parent) {
|
||||
return;
|
||||
}
|
||||
if (ring1->parent == nullptr) {
|
||||
manager.children.remove(ring1);
|
||||
} else {
|
||||
ring1->parent->children.remove(ring1);
|
||||
}
|
||||
if (ring2->parent == nullptr) {
|
||||
manager.children.push_back(ring1);
|
||||
} else {
|
||||
ring2->parent->children.push_back(ring1);
|
||||
}
|
||||
ring1->parent = ring2->parent;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void ring1_replaces_ring2(ring_ptr<T> ring1, ring_ptr<T> ring2, ring_manager<T>& manager) {
|
||||
assert(ring1 != ring2);
|
||||
if (ring2->parent == nullptr) {
|
||||
manager.children.remove(ring2);
|
||||
} else {
|
||||
ring2->parent->children.remove(ring2);
|
||||
}
|
||||
for (auto& c : ring2->children) {
|
||||
c->parent = ring1;
|
||||
}
|
||||
if (ring1 == nullptr) {
|
||||
manager.children.splice(manager.children.end(), ring2->children);
|
||||
} else {
|
||||
ring1->children.splice(ring1->children.end(), ring2->children);
|
||||
}
|
||||
ring2->parent = nullptr;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void remove_ring(ring_ptr<T> r, ring_manager<T>& manager) {
|
||||
if (r->parent == nullptr) {
|
||||
manager.children.remove(r);
|
||||
for (auto& c : r->children) {
|
||||
c->parent = nullptr;
|
||||
}
|
||||
manager.children.splice(manager.children.end(), r->children);
|
||||
} else {
|
||||
r->parent->children.remove(r);
|
||||
for (auto& c : r->children) {
|
||||
c->parent = r->parent;
|
||||
}
|
||||
r->parent->children.splice(r->parent->children.end(), r->children);
|
||||
r->parent = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline std::size_t ring_depth(ring_ptr<T> r) {
|
||||
std::size_t depth = 0;
|
||||
if (!r) {
|
||||
return depth;
|
||||
}
|
||||
while (r->parent) {
|
||||
depth++;
|
||||
r = r->parent;
|
||||
}
|
||||
return depth;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool ring_is_hole(ring_ptr<T> r) {
|
||||
return ring_depth(r) & 1;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void set_next(const_point_ptr<T>& node, const const_point_ptr<T>& next_node) {
|
||||
node->next = next_node;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
point_ptr<T> get_next(const_point_ptr<T>& node) {
|
||||
return node->next;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
point_ptr<T> get_prev(const_point_ptr<T>& node) {
|
||||
return node->prev;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void set_prev(const_point_ptr<T>& node, const const_point_ptr<T>& prev_node) {
|
||||
node->prev = prev_node;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void init(const_point_ptr<T>& node) {
|
||||
set_next(node, node);
|
||||
set_prev(node, node);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::size_t point_count(const const_point_ptr<T>& orig_node) {
|
||||
std::size_t size = 0;
|
||||
point_ptr<T> n = orig_node;
|
||||
do {
|
||||
n = get_next(n);
|
||||
++size;
|
||||
} while (n != orig_node);
|
||||
return size;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void link_before(point_ptr<T>& node, point_ptr<T>& new_node) {
|
||||
point_ptr<T> prev_node = get_prev(node);
|
||||
set_prev(new_node, prev_node);
|
||||
set_next(new_node, node);
|
||||
set_prev(node, new_node);
|
||||
set_next(prev_node, new_node);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void link_after(point_ptr<T>& node, point_ptr<T>& new_node) {
|
||||
point_ptr<T> next_node = get_next(node);
|
||||
set_prev(new_node, node);
|
||||
set_next(new_node, next_node);
|
||||
set_next(node, new_node);
|
||||
set_prev(next_node, new_node);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void transfer_point(point_ptr<T>& p, point_ptr<T>& b, point_ptr<T>& e) {
|
||||
if (b != e) {
|
||||
point_ptr<T> prev_p = get_prev(p);
|
||||
point_ptr<T> prev_b = get_prev(b);
|
||||
point_ptr<T> prev_e = get_prev(e);
|
||||
set_next(prev_e, p);
|
||||
set_prev(p, prev_e);
|
||||
set_next(prev_b, e);
|
||||
set_prev(e, prev_b);
|
||||
set_next(prev_p, b);
|
||||
set_prev(b, prev_p);
|
||||
} else {
|
||||
link_before(p, b);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void reverse_ring(point_ptr<T> pp) {
|
||||
if (!pp) {
|
||||
return;
|
||||
}
|
||||
point_ptr<T> pp1;
|
||||
point_ptr<T> pp2;
|
||||
pp1 = pp;
|
||||
do {
|
||||
pp2 = pp1->next;
|
||||
pp1->next = pp1->prev;
|
||||
pp1->prev = pp2;
|
||||
pp1 = pp2;
|
||||
} while (pp1 != pp);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
double area_from_point(point_ptr<T> op, std::size_t& size) {
|
||||
point_ptr<T> startOp = op;
|
||||
size = 1;
|
||||
double a = 0.0;
|
||||
do {
|
||||
++size;
|
||||
a += static_cast<double>(op->prev->x + op->x) * static_cast<double>(op->prev->y - op->y);
|
||||
op = op->next;
|
||||
} while (op != startOp);
|
||||
return a * 0.5;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
double area(ring_ptr<T> r) {
|
||||
assert(r != nullptr);
|
||||
if (std::isnan(r->area)) {
|
||||
r->area = area_from_point(r->points, r->size);
|
||||
}
|
||||
return r->area;
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
|
||||
template <class charT, class traits, typename T>
|
||||
inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& out,
|
||||
const ring<T>& r) {
|
||||
out << " ring_index: " << r.ring_index << std::endl;
|
||||
if (!r.parent) {
|
||||
// out << " parent_ring ptr: nullptr" << std::endl;
|
||||
out << " parent_index: -----" << std::endl;
|
||||
} else {
|
||||
// out << " parent_ring ptr: " << r.parent << std::endl;
|
||||
out << " parent_ring idx: " << r.parent->ring_index << std::endl;
|
||||
}
|
||||
ring_ptr<T> n = const_cast<ring_ptr<T>>(&r);
|
||||
if (ring_is_hole(n)) {
|
||||
out << " is_hole: true " << std::endl;
|
||||
} else {
|
||||
out << " is_hole: false " << std::endl;
|
||||
}
|
||||
auto pt_itr = r.points;
|
||||
if (pt_itr) {
|
||||
out << " area: " << r.area << std::endl;
|
||||
out << " points:" << std::endl;
|
||||
out << " [[[" << pt_itr->x << "," << pt_itr->y << "],";
|
||||
pt_itr = pt_itr->next;
|
||||
while (pt_itr != r.points) {
|
||||
out << "[" << pt_itr->x << "," << pt_itr->y << "],";
|
||||
pt_itr = pt_itr->next;
|
||||
}
|
||||
out << "[" << pt_itr->x << "," << pt_itr->y << "]]]" << std::endl;
|
||||
} else {
|
||||
out << " area: NONE" << std::endl;
|
||||
out << " points: NONE" << std::endl;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::string output_as_polygon(ring_ptr<T> r) {
|
||||
std::ostringstream out;
|
||||
|
||||
auto pt_itr = r->points;
|
||||
if (pt_itr) {
|
||||
out << "[";
|
||||
out << "[[" << pt_itr->x << "," << pt_itr->y << "],";
|
||||
pt_itr = pt_itr->next;
|
||||
while (pt_itr != r->points) {
|
||||
out << "[" << pt_itr->x << "," << pt_itr->y << "],";
|
||||
pt_itr = pt_itr->next;
|
||||
}
|
||||
out << "[" << pt_itr->x << "," << pt_itr->y << "]]";
|
||||
for (auto const& c : r->children) {
|
||||
pt_itr = c->points;
|
||||
if (pt_itr) {
|
||||
out << ",[[" << pt_itr->x << "," << pt_itr->y << "],";
|
||||
pt_itr = pt_itr->next;
|
||||
while (pt_itr != c->points) {
|
||||
out << "[" << pt_itr->x << "," << pt_itr->y << "],";
|
||||
pt_itr = pt_itr->next;
|
||||
}
|
||||
out << "[" << pt_itr->x << "," << pt_itr->y << "]]";
|
||||
}
|
||||
}
|
||||
out << "]" << std::endl;
|
||||
} else {
|
||||
out << "[]" << std::endl;
|
||||
}
|
||||
|
||||
return out.str();
|
||||
}
|
||||
|
||||
template <class charT, class traits, typename T>
|
||||
inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& out,
|
||||
const ring_list<T>& rings) {
|
||||
out << "START RING LIST" << std::endl;
|
||||
for (auto& r : rings) {
|
||||
out << " ring: " << r->ring_index << " - " << r << std::endl;
|
||||
out << *r;
|
||||
}
|
||||
out << "END RING LIST" << std::endl;
|
||||
return out;
|
||||
}
|
||||
|
||||
template <class charT, class traits, typename T>
|
||||
inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& out,
|
||||
const ring_vector<T>& rings) {
|
||||
out << "START RING VECTOR" << std::endl;
|
||||
for (auto& r : rings) {
|
||||
if (!r->points) {
|
||||
continue;
|
||||
}
|
||||
out << " ring: " << r->ring_index << " - " << r << std::endl;
|
||||
out << *r;
|
||||
}
|
||||
out << "END RING VECTOR" << std::endl;
|
||||
return out;
|
||||
}
|
||||
|
||||
template <class charT, class traits, typename T>
|
||||
inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& out,
|
||||
const std::deque<ring<T>>& rings) {
|
||||
out << "START RING VECTOR" << std::endl;
|
||||
for (auto& r : rings) {
|
||||
if (!r.points) {
|
||||
continue;
|
||||
}
|
||||
out << " ring: " << r.ring_index << std::endl;
|
||||
out << r;
|
||||
}
|
||||
out << "END RING VECTOR" << std::endl;
|
||||
return out;
|
||||
}
|
||||
|
||||
template <class charT, class traits, typename T>
|
||||
inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& out,
|
||||
const hot_pixel_vector<T>& hp_vec) {
|
||||
out << "Hot Pixels: " << std::endl;
|
||||
for (auto& hp : hp_vec) {
|
||||
out << hp << std::endl;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
906
mapbox/geometry/wagyu/ring_util.hpp
Normal file
906
mapbox/geometry/wagyu/ring_util.hpp
Normal file
@ -0,0 +1,906 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef DEBUG
|
||||
#include <iostream>
|
||||
// Example debug print for backtrace - only works on IOS
|
||||
#include <execinfo.h>
|
||||
#include <stdio.h>
|
||||
//
|
||||
// void* callstack[128];
|
||||
// int i, frames = backtrace(callstack, 128);
|
||||
// char** strs = backtrace_symbols(callstack, frames);
|
||||
// for (i = 0; i < frames; ++i) {
|
||||
// printf("%s\n", strs[i]);
|
||||
// }
|
||||
// free(strs);
|
||||
#endif
|
||||
|
||||
#include <queue>
|
||||
|
||||
#include <mapbox/geometry/wagyu/active_bound_list.hpp>
|
||||
#include <mapbox/geometry/wagyu/config.hpp>
|
||||
#include <mapbox/geometry/wagyu/edge.hpp>
|
||||
#include <mapbox/geometry/wagyu/ring.hpp>
|
||||
#include <mapbox/geometry/wagyu/util.hpp>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
|
||||
template <typename T>
|
||||
void set_hole_state(active_bound_list_itr<T>& bnd,
|
||||
active_bound_list<T>& active_bounds,
|
||||
ring_manager<T>& rings) {
|
||||
auto bnd2 = active_bound_list_rev_itr<T>(bnd);
|
||||
bound_ptr<T> bndTmp = nullptr;
|
||||
// Find first non line ring to the left of current bound.
|
||||
while (bnd2 != active_bounds.rend()) {
|
||||
if ((*bnd2)->ring && (*bnd2)->winding_delta != 0) {
|
||||
if (!bndTmp) {
|
||||
bndTmp = (*bnd2);
|
||||
} else if (bndTmp->ring == (*bnd2)->ring) {
|
||||
bndTmp = nullptr;
|
||||
}
|
||||
}
|
||||
++bnd2;
|
||||
}
|
||||
if (!bndTmp) {
|
||||
(*bnd)->ring->parent = nullptr;
|
||||
rings.children.push_back((*bnd)->ring);
|
||||
} else {
|
||||
(*bnd)->ring->parent = bndTmp->ring;
|
||||
bndTmp->ring->children.push_back((*bnd)->ring);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void set_hole_state(active_bound_list_rev_itr<T>& bnd,
|
||||
active_bound_list<T>& active_bounds,
|
||||
ring_manager<T>& rings) {
|
||||
auto bnd2 = std::next(bnd);
|
||||
bound_ptr<T> bndTmp = nullptr;
|
||||
// Find first non line ring to the left of current bound.
|
||||
while (bnd2 != active_bounds.rend()) {
|
||||
if ((*bnd2)->ring && (*bnd2)->winding_delta != 0) {
|
||||
if (!bndTmp) {
|
||||
bndTmp = (*bnd2);
|
||||
} else if (bndTmp->ring == (*bnd2)->ring) {
|
||||
bndTmp = nullptr;
|
||||
}
|
||||
}
|
||||
++bnd2;
|
||||
}
|
||||
|
||||
if (!bndTmp) {
|
||||
(*bnd)->ring->parent = nullptr;
|
||||
rings.children.push_back((*bnd)->ring);
|
||||
} else {
|
||||
(*bnd)->ring->parent = bndTmp->ring;
|
||||
bndTmp->ring->children.push_back((*bnd)->ring);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void update_current_hp_itr(T scanline_y, ring_manager<T>& rings) {
|
||||
while (rings.current_hp_itr->y > scanline_y) {
|
||||
++rings.current_hp_itr;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct hot_pixel_sorter {
|
||||
inline bool operator()(mapbox::geometry::point<T> const& pt1,
|
||||
mapbox::geometry::point<T> const& pt2) {
|
||||
if (pt1.y == pt2.y) {
|
||||
return pt1.x < pt2.x;
|
||||
} else {
|
||||
return pt1.y > pt2.y;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Due to the nature of floating point calculations
|
||||
// and the high likely hood of values around X.5, we
|
||||
// need to fudge what is X.5 some for our rounding.
|
||||
const double rounding_offset = 1e-12;
|
||||
const double rounding_offset_y = 5e-13;
|
||||
|
||||
template <typename T>
|
||||
T round_towards_min(double val) {
|
||||
// 0.5 rounds to 0
|
||||
// 0.0 rounds to 0
|
||||
// -0.5 rounds to -1
|
||||
return static_cast<T>(std::ceil(val - 0.5 + rounding_offset));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T round_towards_max(double val) {
|
||||
// 0.5 rounds to 1
|
||||
// 0.0 rounds to 0
|
||||
// -0.5 rounds to 0
|
||||
return static_cast<T>(std::floor(val + 0.5 + rounding_offset));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T get_edge_min_x(edge<T> const& edge, const T current_y) {
|
||||
if (is_horizontal(edge)) {
|
||||
if (edge.bot.x < edge.top.x) {
|
||||
return edge.bot.x;
|
||||
} else {
|
||||
return edge.top.x;
|
||||
}
|
||||
} else if (edge.dx > 0.0) {
|
||||
if (current_y == edge.top.y) {
|
||||
return edge.top.x;
|
||||
} else {
|
||||
double lower_range_y = static_cast<double>(current_y - edge.bot.y) - 0.5;
|
||||
double return_val = static_cast<double>(edge.bot.x) + edge.dx * lower_range_y;
|
||||
T value = round_towards_min<T>(return_val);
|
||||
return value;
|
||||
}
|
||||
} else {
|
||||
if (current_y == edge.bot.y) {
|
||||
return edge.bot.x;
|
||||
} else {
|
||||
double return_val =
|
||||
static_cast<double>(edge.bot.x) +
|
||||
edge.dx * (static_cast<double>(current_y - edge.bot.y) + 0.5 - rounding_offset_y);
|
||||
T value = round_towards_min<T>(return_val);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T get_edge_max_x(edge<T> const& edge, const T current_y) {
|
||||
if (is_horizontal(edge)) {
|
||||
if (edge.bot.x > edge.top.x) {
|
||||
return edge.bot.x;
|
||||
} else {
|
||||
return edge.top.x;
|
||||
}
|
||||
} else if (edge.dx < 0.0) {
|
||||
if (current_y == edge.top.y) {
|
||||
return edge.top.x;
|
||||
} else {
|
||||
double lower_range_y = static_cast<double>(current_y - edge.bot.y) - 0.5;
|
||||
double return_val = static_cast<double>(edge.bot.x) + edge.dx * lower_range_y;
|
||||
T value = round_towards_max<T>(return_val);
|
||||
return value;
|
||||
}
|
||||
} else {
|
||||
if (current_y == edge.bot.y) {
|
||||
return edge.bot.x;
|
||||
} else {
|
||||
double return_val =
|
||||
static_cast<double>(edge.bot.x) +
|
||||
edge.dx * (static_cast<double>(current_y - edge.bot.y) + 0.5 - rounding_offset_y);
|
||||
T value = round_towards_max<T>(return_val);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void hot_pixel_set_left_to_right(T y,
|
||||
T start_x,
|
||||
T end_x,
|
||||
bound<T>& bnd,
|
||||
ring_manager<T>& rings,
|
||||
hot_pixel_itr<T>& itr,
|
||||
hot_pixel_itr<T>& end,
|
||||
bool add_end_point) {
|
||||
T x_min = get_edge_min_x(*(bnd.current_edge), y);
|
||||
x_min = std::max(x_min, start_x);
|
||||
T x_max = get_edge_max_x(*(bnd.current_edge), y);
|
||||
x_max = std::min(x_max, end_x);
|
||||
for (; itr != end; ++itr) {
|
||||
if (itr->x < x_min) {
|
||||
continue;
|
||||
}
|
||||
if (itr->x > x_max) {
|
||||
break;
|
||||
}
|
||||
if (!add_end_point && itr->x == end_x) {
|
||||
continue;
|
||||
}
|
||||
point_ptr<T> op = bnd.ring->points;
|
||||
bool to_front = (bnd.side == edge_left);
|
||||
if (to_front && (*itr == *op)) {
|
||||
continue;
|
||||
} else if (!to_front && (*itr == *op->prev)) {
|
||||
continue;
|
||||
}
|
||||
point_ptr<T> new_point = create_new_point(bnd.ring, *itr, op, rings);
|
||||
if (to_front) {
|
||||
bnd.ring->points = new_point;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void hot_pixel_set_right_to_left(T y,
|
||||
T start_x,
|
||||
T end_x,
|
||||
bound<T>& bnd,
|
||||
ring_manager<T>& rings,
|
||||
hot_pixel_rev_itr<T>& itr,
|
||||
hot_pixel_rev_itr<T>& end,
|
||||
bool add_end_point) {
|
||||
T x_min = get_edge_min_x(*(bnd.current_edge), y);
|
||||
x_min = std::max(x_min, end_x);
|
||||
T x_max = get_edge_max_x(*(bnd.current_edge), y);
|
||||
x_max = std::min(x_max, start_x);
|
||||
for (; itr != end; ++itr) {
|
||||
if (itr->x > x_max) {
|
||||
continue;
|
||||
}
|
||||
if (itr->x < x_min) {
|
||||
break;
|
||||
}
|
||||
if (!add_end_point && itr->x == end_x) {
|
||||
continue;
|
||||
}
|
||||
point_ptr<T> op = bnd.ring->points;
|
||||
bool to_front = (bnd.side == edge_left);
|
||||
if (to_front && (*itr == *op)) {
|
||||
continue;
|
||||
} else if (!to_front && (*itr == *op->prev)) {
|
||||
continue;
|
||||
}
|
||||
point_ptr<T> new_point = create_new_point(bnd.ring, *itr, op, rings);
|
||||
if (to_front) {
|
||||
bnd.ring->points = new_point;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void sort_hot_pixels(ring_manager<T>& rings) {
|
||||
std::sort(rings.hot_pixels.begin(), rings.hot_pixels.end(), hot_pixel_sorter<T>());
|
||||
auto last = std::unique(rings.hot_pixels.begin(), rings.hot_pixels.end());
|
||||
rings.hot_pixels.erase(last, rings.hot_pixels.end());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void insert_hot_pixels_in_path(bound<T>& bnd,
|
||||
mapbox::geometry::point<T> const& end_pt,
|
||||
ring_manager<T>& rings,
|
||||
bool add_end_point) {
|
||||
if (end_pt == bnd.last_point) {
|
||||
return;
|
||||
}
|
||||
|
||||
T start_y = bnd.last_point.y;
|
||||
T start_x = bnd.last_point.x;
|
||||
T end_y = end_pt.y;
|
||||
T end_x = end_pt.x;
|
||||
|
||||
auto itr = rings.current_hp_itr;
|
||||
while (itr->y <= start_y && itr != rings.hot_pixels.begin()) {
|
||||
--itr;
|
||||
}
|
||||
if (start_x > end_x) {
|
||||
for (; itr != rings.hot_pixels.end();) {
|
||||
if (itr->y > start_y) {
|
||||
++itr;
|
||||
continue;
|
||||
}
|
||||
if (itr->y < end_y) {
|
||||
break;
|
||||
}
|
||||
T y = itr->y;
|
||||
auto last_itr = hot_pixel_rev_itr<T>(itr);
|
||||
while (itr != rings.hot_pixels.end() && itr->y == y) {
|
||||
++itr;
|
||||
}
|
||||
auto first_itr = hot_pixel_rev_itr<T>(itr);
|
||||
bool add_end_point_itr = (y != end_pt.y || add_end_point);
|
||||
hot_pixel_set_right_to_left(y, start_x, end_x, bnd, rings, first_itr, last_itr,
|
||||
add_end_point_itr);
|
||||
}
|
||||
} else {
|
||||
for (; itr != rings.hot_pixels.end();) {
|
||||
if (itr->y > start_y) {
|
||||
++itr;
|
||||
continue;
|
||||
}
|
||||
if (itr->y < end_y) {
|
||||
break;
|
||||
}
|
||||
T y = itr->y;
|
||||
auto first_itr = itr;
|
||||
while (itr != rings.hot_pixels.end() && itr->y == y) {
|
||||
++itr;
|
||||
}
|
||||
auto last_itr = itr;
|
||||
bool add_end_point_itr = (y != end_pt.y || add_end_point);
|
||||
hot_pixel_set_left_to_right(y, start_x, end_x, bnd, rings, first_itr, last_itr,
|
||||
add_end_point_itr);
|
||||
}
|
||||
}
|
||||
bnd.last_point = end_pt;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void add_to_hot_pixels(mapbox::geometry::point<T> const& pt, ring_manager<T>& rings) {
|
||||
rings.hot_pixels.push_back(pt);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void add_first_point(active_bound_list_itr<T>& bnd,
|
||||
active_bound_list<T>& active_bounds,
|
||||
mapbox::geometry::point<T> const& pt,
|
||||
ring_manager<T>& rings) {
|
||||
|
||||
ring_ptr<T> r = create_new_ring(rings);
|
||||
(*bnd)->ring = r;
|
||||
r->points = create_new_point(r, pt, rings);
|
||||
set_hole_state(bnd, active_bounds, rings);
|
||||
(*bnd)->last_point = pt;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void add_first_point(active_bound_list_rev_itr<T>& bnd,
|
||||
active_bound_list<T>& active_bounds,
|
||||
mapbox::geometry::point<T> const& pt,
|
||||
ring_manager<T>& rings) {
|
||||
ring_ptr<T> r = create_new_ring(rings);
|
||||
// no ring currently set!
|
||||
(*bnd)->ring = r;
|
||||
r->points = create_new_point(r, pt, rings);
|
||||
set_hole_state(bnd, active_bounds, rings);
|
||||
(*bnd)->last_point = pt;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void add_point_to_ring(bound<T>& bnd,
|
||||
mapbox::geometry::point<T> const& pt,
|
||||
ring_manager<T>& rings) {
|
||||
assert(bnd.ring);
|
||||
// Handle hot pixels
|
||||
insert_hot_pixels_in_path(bnd, pt, rings, false);
|
||||
|
||||
// bnd.ring->points is the 'Left-most' point & bnd.ring->points->prev is the
|
||||
// 'Right-most'
|
||||
point_ptr<T> op = bnd.ring->points;
|
||||
bool to_front = (bnd.side == edge_left);
|
||||
if (to_front && (pt == *op)) {
|
||||
return;
|
||||
} else if (!to_front && (pt == *op->prev)) {
|
||||
return;
|
||||
}
|
||||
point_ptr<T> new_point = create_new_point(bnd.ring, pt, bnd.ring->points, rings);
|
||||
if (to_front) {
|
||||
bnd.ring->points = new_point;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void add_point(active_bound_list_itr<T>& bnd,
|
||||
active_bound_list<T>& active_bounds,
|
||||
mapbox::geometry::point<T> const& pt,
|
||||
ring_manager<T>& rings) {
|
||||
if (!(*bnd)->ring) {
|
||||
add_first_point(bnd, active_bounds, pt, rings);
|
||||
} else {
|
||||
add_point_to_ring(*(*bnd), pt, rings);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void add_point(active_bound_list_rev_itr<T>& bnd,
|
||||
active_bound_list<T>& active_bounds,
|
||||
mapbox::geometry::point<T> const& pt,
|
||||
ring_manager<T>& rings) {
|
||||
if (!(*bnd)->ring) {
|
||||
add_first_point(bnd, active_bounds, pt, rings);
|
||||
} else {
|
||||
add_point_to_ring(*(*bnd), pt, rings);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void add_local_minimum_point(active_bound_list_itr<T> b1,
|
||||
active_bound_list_itr<T> b2,
|
||||
active_bound_list<T>& active_bounds,
|
||||
mapbox::geometry::point<T> const& pt,
|
||||
ring_manager<T>& rings) {
|
||||
active_bound_list_itr<T> b;
|
||||
active_bound_list_rev_itr<T> prev_bound;
|
||||
active_bound_list_rev_itr<T> prev_b1(b1);
|
||||
active_bound_list_rev_itr<T> prev_b2(b2);
|
||||
if (is_horizontal(*((*b2)->current_edge)) ||
|
||||
((*b1)->current_edge->dx > (*b2)->current_edge->dx)) {
|
||||
add_point(b1, active_bounds, pt, rings);
|
||||
(*b2)->last_point = pt;
|
||||
(*b2)->ring = (*b1)->ring;
|
||||
(*b1)->side = edge_left;
|
||||
(*b2)->side = edge_right;
|
||||
b = b1;
|
||||
if (prev_b1 != active_bounds.rend() && std::prev(b) == b2) {
|
||||
prev_bound = prev_b2;
|
||||
} else {
|
||||
prev_bound = prev_b1;
|
||||
}
|
||||
} else {
|
||||
add_point(b2, active_bounds, pt, rings);
|
||||
(*b1)->last_point = pt;
|
||||
(*b1)->ring = (*b2)->ring;
|
||||
(*b1)->side = edge_right;
|
||||
(*b2)->side = edge_left;
|
||||
b = b2;
|
||||
if (prev_b2 != active_bounds.rend() && std::prev(b) == b1) {
|
||||
prev_bound = prev_b1;
|
||||
} else {
|
||||
prev_bound = prev_b2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline double get_dx(point<T> const& pt1, point<T> const& pt2) {
|
||||
if (pt1.y == pt2.y) {
|
||||
return std::numeric_limits<double>::infinity();
|
||||
} else {
|
||||
return static_cast<double>(pt2.x - pt2.x) / static_cast<double>(pt2.y - pt1.y);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool first_is_bottom_point(const_point_ptr<T> btmPt1, const_point_ptr<T> btmPt2) {
|
||||
point_ptr<T> p = btmPt1->prev;
|
||||
while ((*p == *btmPt1) && (p != btmPt1)) {
|
||||
p = p->prev;
|
||||
}
|
||||
double dx1p = std::fabs(get_dx(*btmPt1, *p));
|
||||
|
||||
p = btmPt1->next;
|
||||
while ((*p == *btmPt1) && (p != btmPt1)) {
|
||||
p = p->next;
|
||||
}
|
||||
double dx1n = std::fabs(get_dx(*btmPt1, *p));
|
||||
|
||||
p = btmPt2->prev;
|
||||
while ((*p == *btmPt2) && (p != btmPt2)) {
|
||||
p = p->prev;
|
||||
}
|
||||
double dx2p = std::fabs(get_dx(*btmPt2, *p));
|
||||
|
||||
p = btmPt2->next;
|
||||
while ((*p == *btmPt2) && (p != btmPt2)) {
|
||||
p = p->next;
|
||||
}
|
||||
double dx2n = std::fabs(get_dx(*btmPt2, *p));
|
||||
|
||||
if (values_are_equal(std::max(dx1p, dx1n), std::max(dx2p, dx2n)) &&
|
||||
values_are_equal(std::min(dx1p, dx1n), std::min(dx2p, dx2n))) {
|
||||
std::size_t s = 0;
|
||||
return area_from_point(btmPt1, s) > 0.0; // if otherwise identical use orientation
|
||||
} else {
|
||||
return (greater_than_or_equal(dx1p, dx2p) && greater_than_or_equal(dx1p, dx2n)) ||
|
||||
(greater_than_or_equal(dx1n, dx2p) && greater_than_or_equal(dx1n, dx2n));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
point_ptr<T> get_bottom_point(point_ptr<T> pp) {
|
||||
point_ptr<T> dups = nullptr;
|
||||
point_ptr<T> p = pp->next;
|
||||
while (p != pp) {
|
||||
if (p->y > pp->y) {
|
||||
pp = p;
|
||||
dups = nullptr;
|
||||
} else if (p->y == pp->y && p->x <= pp->x) {
|
||||
if (p->x < pp->x) {
|
||||
dups = nullptr;
|
||||
pp = p;
|
||||
} else {
|
||||
if (p->next != pp && p->prev != pp) {
|
||||
dups = p;
|
||||
}
|
||||
}
|
||||
}
|
||||
p = p->next;
|
||||
}
|
||||
if (dups) {
|
||||
// there appears to be at least 2 vertices at bottom_point so ...
|
||||
while (dups != p) {
|
||||
if (!first_is_bottom_point(p, dups)) {
|
||||
pp = dups;
|
||||
}
|
||||
dups = dups->next;
|
||||
while (*dups != *pp) {
|
||||
dups = dups->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
return pp;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
ring_ptr<T> get_lower_most_ring(ring_ptr<T> outRec1, ring_ptr<T> outRec2) {
|
||||
// work out which polygon fragment has the correct hole state ...
|
||||
if (!outRec1->bottom_point) {
|
||||
outRec1->bottom_point = get_bottom_point(outRec1->points);
|
||||
}
|
||||
if (!outRec2->bottom_point) {
|
||||
outRec2->bottom_point = get_bottom_point(outRec2->points);
|
||||
}
|
||||
point_ptr<T> OutPt1 = outRec1->bottom_point;
|
||||
point_ptr<T> OutPt2 = outRec2->bottom_point;
|
||||
if (OutPt1->y > OutPt2->y) {
|
||||
return outRec1;
|
||||
} else if (OutPt1->y < OutPt2->y) {
|
||||
return outRec2;
|
||||
} else if (OutPt1->x < OutPt2->x) {
|
||||
return outRec1;
|
||||
} else if (OutPt1->x > OutPt2->x) {
|
||||
return outRec2;
|
||||
} else if (OutPt1->next == OutPt1) {
|
||||
return outRec2;
|
||||
} else if (OutPt2->next == OutPt2) {
|
||||
return outRec1;
|
||||
} else if (first_is_bottom_point(OutPt1, OutPt2)) {
|
||||
return outRec1;
|
||||
} else {
|
||||
return outRec2;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool ring1_child_below_ring2(ring_ptr<T> ring1, ring_ptr<T> ring2) {
|
||||
do {
|
||||
ring1 = ring1->parent;
|
||||
if (ring1 == ring2) {
|
||||
return true;
|
||||
}
|
||||
} while (ring1);
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void update_points_ring(ring_ptr<T> ring) {
|
||||
point_ptr<T> op = ring->points;
|
||||
do {
|
||||
op->ring = ring;
|
||||
op = op->prev;
|
||||
} while (op != ring->points);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void append_ring(active_bound_list_itr<T>& b1,
|
||||
active_bound_list_itr<T>& b2,
|
||||
active_bound_list<T>& active_bounds,
|
||||
ring_manager<T>& manager) {
|
||||
// get the start and ends of both output polygons ...
|
||||
ring_ptr<T> outRec1 = (*b1)->ring;
|
||||
ring_ptr<T> outRec2 = (*b2)->ring;
|
||||
|
||||
ring_ptr<T> keep_ring;
|
||||
bound_ptr<T> keep_bound;
|
||||
ring_ptr<T> remove_ring;
|
||||
bound_ptr<T> remove_bound;
|
||||
if (ring1_child_below_ring2(outRec1, outRec2)) {
|
||||
keep_ring = outRec2;
|
||||
keep_bound = *b2;
|
||||
remove_ring = outRec1;
|
||||
remove_bound = *b1;
|
||||
} else if (ring1_child_below_ring2(outRec2, outRec1)) {
|
||||
keep_ring = outRec1;
|
||||
keep_bound = *b1;
|
||||
remove_ring = outRec2;
|
||||
remove_bound = *b2;
|
||||
} else if (outRec1 == get_lower_most_ring(outRec1, outRec2)) {
|
||||
keep_ring = outRec1;
|
||||
keep_bound = *b1;
|
||||
remove_ring = outRec2;
|
||||
remove_bound = *b2;
|
||||
} else {
|
||||
keep_ring = outRec2;
|
||||
keep_bound = *b2;
|
||||
remove_ring = outRec1;
|
||||
remove_bound = *b1;
|
||||
}
|
||||
|
||||
// get the start and ends of both output polygons and
|
||||
// join b2 poly onto b1 poly and delete pointers to b2 ...
|
||||
|
||||
point_ptr<T> p1_lft = keep_ring->points;
|
||||
point_ptr<T> p1_rt = p1_lft->prev;
|
||||
point_ptr<T> p2_lft = remove_ring->points;
|
||||
point_ptr<T> p2_rt = p2_lft->prev;
|
||||
|
||||
// join b2 poly onto b1 poly and delete pointers to b2 ...
|
||||
if (keep_bound->side == edge_left) {
|
||||
if (remove_bound->side == edge_left) {
|
||||
// z y x a b c
|
||||
reverse_ring(p2_lft);
|
||||
p2_lft->next = p1_lft;
|
||||
p1_lft->prev = p2_lft;
|
||||
p1_rt->next = p2_rt;
|
||||
p2_rt->prev = p1_rt;
|
||||
keep_ring->points = p2_rt;
|
||||
} else {
|
||||
// x y z a b c
|
||||
p2_rt->next = p1_lft;
|
||||
p1_lft->prev = p2_rt;
|
||||
p2_lft->prev = p1_rt;
|
||||
p1_rt->next = p2_lft;
|
||||
keep_ring->points = p2_lft;
|
||||
}
|
||||
} else {
|
||||
if (remove_bound->side == edge_right) {
|
||||
// a b c z y x
|
||||
reverse_ring(p2_lft);
|
||||
p1_rt->next = p2_rt;
|
||||
p2_rt->prev = p1_rt;
|
||||
p2_lft->next = p1_lft;
|
||||
p1_lft->prev = p2_lft;
|
||||
} else {
|
||||
// a b c x y z
|
||||
p1_rt->next = p2_lft;
|
||||
p2_lft->prev = p1_rt;
|
||||
p1_lft->prev = p2_rt;
|
||||
p2_rt->next = p1_lft;
|
||||
}
|
||||
}
|
||||
|
||||
keep_ring->bottom_point = nullptr;
|
||||
bool keep_is_hole = ring_is_hole(keep_ring);
|
||||
bool remove_is_hole = ring_is_hole(remove_ring);
|
||||
|
||||
remove_ring->points = nullptr;
|
||||
remove_ring->bottom_point = nullptr;
|
||||
if (keep_is_hole != remove_is_hole) {
|
||||
ring1_replaces_ring2(keep_ring->parent, remove_ring, manager);
|
||||
} else {
|
||||
ring1_replaces_ring2(keep_ring, remove_ring, manager);
|
||||
}
|
||||
|
||||
update_points_ring(keep_ring);
|
||||
|
||||
// nb: safe because we only get here via AddLocalMaxPoly
|
||||
keep_bound->ring = nullptr;
|
||||
remove_bound->ring = nullptr;
|
||||
|
||||
for (auto& b : active_bounds) {
|
||||
if (b->ring == remove_ring) {
|
||||
b->ring = keep_ring;
|
||||
b->side = keep_bound->side;
|
||||
break; // Not sure why there is a break here but was transfered logic from angus
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void add_local_maximum_point(active_bound_list_itr<T>& b1,
|
||||
active_bound_list_itr<T>& b2,
|
||||
mapbox::geometry::point<T> const& pt,
|
||||
ring_manager<T>& rings,
|
||||
active_bound_list<T>& active_bounds) {
|
||||
insert_hot_pixels_in_path(*(*b2), pt, rings, false);
|
||||
add_point(b1, active_bounds, pt, rings);
|
||||
if ((*b1)->ring == (*b2)->ring) {
|
||||
(*b1)->ring = nullptr;
|
||||
(*b2)->ring = nullptr;
|
||||
// I am not certain that order is important here?
|
||||
} else if ((*b1)->ring->ring_index < (*b2)->ring->ring_index) {
|
||||
append_ring(b1, b2, active_bounds, rings);
|
||||
} else {
|
||||
append_ring(b2, b1, active_bounds, rings);
|
||||
}
|
||||
}
|
||||
|
||||
enum point_in_polygon_result : std::int8_t {
|
||||
point_on_polygon = -1,
|
||||
point_inside_polygon = 0,
|
||||
point_outside_polygon = 1
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
point_in_polygon_result point_in_polygon(point<T> const& pt, point_ptr<T> op) {
|
||||
// returns 0 if false, +1 if true, -1 if pt ON polygon boundary
|
||||
point_in_polygon_result result = point_outside_polygon;
|
||||
point_ptr<T> startOp = op;
|
||||
do {
|
||||
if (op->next->y == pt.y) {
|
||||
if ((op->next->x == pt.x) ||
|
||||
(op->y == pt.y && ((op->next->x > pt.x) == (op->x < pt.x)))) {
|
||||
return point_on_polygon;
|
||||
}
|
||||
}
|
||||
if ((op->y < pt.y) != (op->next->y < pt.y)) {
|
||||
if (op->x >= pt.x) {
|
||||
if (op->next->x > pt.x) {
|
||||
// Switch between point outside polygon and point inside
|
||||
// polygon
|
||||
if (result == point_outside_polygon) {
|
||||
result = point_inside_polygon;
|
||||
} else {
|
||||
result = point_outside_polygon;
|
||||
}
|
||||
} else {
|
||||
double d =
|
||||
static_cast<double>(op->x - pt.x) *
|
||||
static_cast<double>(op->next->y - pt.y) -
|
||||
static_cast<double>(op->next->x - pt.x) * static_cast<double>(op->y - pt.y);
|
||||
if (value_is_zero(d)) {
|
||||
return point_on_polygon;
|
||||
}
|
||||
if ((d > 0) == (op->next->y > op->y)) {
|
||||
// Switch between point outside polygon and point inside
|
||||
// polygon
|
||||
if (result == point_outside_polygon) {
|
||||
result = point_inside_polygon;
|
||||
} else {
|
||||
result = point_outside_polygon;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (op->next->x > pt.x) {
|
||||
double d =
|
||||
static_cast<double>(op->x - pt.x) *
|
||||
static_cast<double>(op->next->y - pt.y) -
|
||||
static_cast<double>(op->next->x - pt.x) * static_cast<double>(op->y - pt.y);
|
||||
if (value_is_zero(d)) {
|
||||
return point_on_polygon;
|
||||
}
|
||||
if ((d > 0) == (op->next->y > op->y)) {
|
||||
// Switch between point outside polygon and point inside
|
||||
// polygon
|
||||
if (result == point_outside_polygon) {
|
||||
result = point_inside_polygon;
|
||||
} else {
|
||||
result = point_outside_polygon;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
op = op->next;
|
||||
} while (startOp != op);
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
point_in_polygon_result point_in_polygon(mapbox::geometry::point<double> const& pt,
|
||||
point_ptr<T> op) {
|
||||
// returns 0 if false, +1 if true, -1 if pt ON polygon boundary
|
||||
point_in_polygon_result result = point_outside_polygon;
|
||||
point_ptr<T> startOp = op;
|
||||
do {
|
||||
double op_x = static_cast<double>(op->x);
|
||||
double op_y = static_cast<double>(op->y);
|
||||
double op_next_x = static_cast<double>(op->next->x);
|
||||
double op_next_y = static_cast<double>(op->next->y);
|
||||
if (values_are_equal(op_next_y, pt.y)) {
|
||||
if (values_are_equal(op_next_x, pt.x) ||
|
||||
(values_are_equal(op_y, pt.y) && ((op_next_x > pt.x) == (op_x < pt.x)))) {
|
||||
return point_on_polygon;
|
||||
}
|
||||
}
|
||||
if ((op_y < pt.y) != (op_next_y < pt.y)) {
|
||||
if (greater_than_or_equal(op_x, pt.x)) {
|
||||
if (op_next_x > pt.x) {
|
||||
// Switch between point outside polygon and point inside
|
||||
// polygon
|
||||
if (result == point_outside_polygon) {
|
||||
result = point_inside_polygon;
|
||||
} else {
|
||||
result = point_outside_polygon;
|
||||
}
|
||||
} else {
|
||||
double d =
|
||||
(op_x - pt.x) * (op_next_y - pt.y) - (op_next_x - pt.x) * (op_y - pt.y);
|
||||
if (value_is_zero(d)) {
|
||||
return point_on_polygon;
|
||||
}
|
||||
if ((d > 0.0) == (op_next_y > op->y)) {
|
||||
// Switch between point outside polygon and point inside
|
||||
// polygon
|
||||
if (result == point_outside_polygon) {
|
||||
result = point_inside_polygon;
|
||||
} else {
|
||||
result = point_outside_polygon;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (op_next_x > pt.x) {
|
||||
double d =
|
||||
(op_x - pt.x) * (op_next_y - pt.y) - (op_next_x - pt.x) * (op_y - pt.y);
|
||||
if (value_is_zero(d)) {
|
||||
return point_on_polygon;
|
||||
}
|
||||
if ((d > 0.0) == (op_next_y > op->y)) {
|
||||
// Switch between point outside polygon and point inside
|
||||
// polygon
|
||||
if (result == point_outside_polygon) {
|
||||
result = point_inside_polygon;
|
||||
} else {
|
||||
result = point_outside_polygon;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
op = op->next;
|
||||
} while (startOp != op);
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
point_in_polygon_result inside_or_outside_special(point_ptr<T> first_pt, point_ptr<T> other_poly) {
|
||||
|
||||
if (value_is_zero(area(first_pt->ring))) {
|
||||
return point_inside_polygon;
|
||||
}
|
||||
if (value_is_zero(area(other_poly->ring))) {
|
||||
return point_outside_polygon;
|
||||
}
|
||||
point_ptr<T> pt = first_pt;
|
||||
do {
|
||||
if (*pt == *(pt->prev) || *pt == *(pt->next) || *(pt->next) == *(pt->prev) ||
|
||||
slopes_equal(*(pt->prev), *pt, *(pt->next))) {
|
||||
pt = pt->next;
|
||||
continue;
|
||||
}
|
||||
double dx = ((pt->prev->x - pt->x) / 3.0) + ((pt->next->x - pt->x) / 3.0);
|
||||
double dy = ((pt->prev->y - pt->y) / 3.0) + ((pt->next->y - pt->y) / 3.0);
|
||||
mapbox::geometry::point<double> offset_pt(pt->x + dx, pt->y + dy);
|
||||
point_in_polygon_result res = point_in_polygon(offset_pt, pt);
|
||||
if (res != point_inside_polygon) {
|
||||
offset_pt.x = pt->x - dx;
|
||||
offset_pt.y = pt->y - dy;
|
||||
res = point_in_polygon(offset_pt, pt);
|
||||
if (res != point_inside_polygon) {
|
||||
pt = pt->next;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
res = point_in_polygon(offset_pt, other_poly);
|
||||
if (res == point_on_polygon) {
|
||||
pt = pt->next;
|
||||
continue;
|
||||
}
|
||||
return res;
|
||||
} while (pt != first_pt);
|
||||
return point_inside_polygon;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool poly2_contains_poly1(ring_ptr<T> ring1, ring_ptr<T> ring2) {
|
||||
point_ptr<T> outpt1 = ring1->points->next;
|
||||
point_ptr<T> outpt2 = ring2->points->next;
|
||||
point_ptr<T> op = outpt1;
|
||||
do {
|
||||
// nb: PointInPolygon returns 0 if false, +1 if true, -1 if pt on polygon
|
||||
point_in_polygon_result res = point_in_polygon(*op, outpt2);
|
||||
if (res != point_on_polygon) {
|
||||
return res == point_inside_polygon;
|
||||
}
|
||||
op = op->next;
|
||||
} while (op != outpt1);
|
||||
point_in_polygon_result res = inside_or_outside_special(outpt1, outpt2);
|
||||
return res == point_inside_polygon;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void dispose_out_points(point_ptr<T>& pp) {
|
||||
if (pp == nullptr) {
|
||||
return;
|
||||
}
|
||||
pp->prev->next = nullptr;
|
||||
while (pp) {
|
||||
point_ptr<T> tmpPp = pp;
|
||||
pp = pp->next;
|
||||
tmpPp->next = tmpPp;
|
||||
tmpPp->prev = tmpPp;
|
||||
tmpPp->ring = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
37
mapbox/geometry/wagyu/scanbeam.hpp
Normal file
37
mapbox/geometry/wagyu/scanbeam.hpp
Normal file
@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <queue>
|
||||
|
||||
#include <mapbox/geometry/wagyu/config.hpp>
|
||||
#include <mapbox/geometry/wagyu/local_minimum.hpp>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
|
||||
template <typename T>
|
||||
using scanbeam_list = std::priority_queue<T>;
|
||||
|
||||
template <typename T>
|
||||
bool pop_from_scanbeam(T& Y, scanbeam_list<T>& scanbeam) {
|
||||
if (scanbeam.empty()) {
|
||||
return false;
|
||||
}
|
||||
Y = scanbeam.top();
|
||||
scanbeam.pop();
|
||||
while (!scanbeam.empty() && Y == scanbeam.top()) {
|
||||
scanbeam.pop();
|
||||
} // Pop duplicates.
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void setup_scanbeam(local_minimum_list<T>& minima_list, scanbeam_list<T>& scanbeam) {
|
||||
|
||||
for (auto lm = minima_list.begin(); lm != minima_list.end(); ++lm) {
|
||||
scanbeam.push(lm->y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
172
mapbox/geometry/wagyu/snap_rounding.hpp
Normal file
172
mapbox/geometry/wagyu/snap_rounding.hpp
Normal file
@ -0,0 +1,172 @@
|
||||
#pragma once
|
||||
|
||||
#include <mapbox/geometry/wagyu/active_bound_list.hpp>
|
||||
#include <mapbox/geometry/wagyu/bound.hpp>
|
||||
#include <mapbox/geometry/wagyu/config.hpp>
|
||||
#include <mapbox/geometry/wagyu/edge.hpp>
|
||||
#include <mapbox/geometry/wagyu/intersect.hpp>
|
||||
#include <mapbox/geometry/wagyu/intersect_util.hpp>
|
||||
#include <mapbox/geometry/wagyu/ring.hpp>
|
||||
#include <mapbox/geometry/wagyu/ring_util.hpp>
|
||||
#include <mapbox/geometry/wagyu/util.hpp>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
|
||||
template <typename T>
|
||||
void process_hot_pixel_intersections(T top_y,
|
||||
active_bound_list<T>& active_bounds,
|
||||
ring_manager<T>& rings) {
|
||||
if (active_bounds.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
update_current_x(active_bounds, top_y);
|
||||
// bubblesort ...
|
||||
bool isModified;
|
||||
do {
|
||||
isModified = false;
|
||||
auto bnd = active_bounds.begin();
|
||||
auto bnd_next = std::next(bnd);
|
||||
while (bnd_next != active_bounds.end()) {
|
||||
if ((*bnd)->current_x > (*bnd_next)->current_x &&
|
||||
!slopes_equal(*(*bnd)->current_edge, *(*bnd_next)->current_edge)) {
|
||||
mapbox::geometry::point<double> pt;
|
||||
if (!get_edge_intersection<T, double>(*((*bnd)->current_edge),
|
||||
*((*bnd_next)->current_edge), pt)) {
|
||||
// LCOV_EXCL_START
|
||||
throw std::runtime_error("Edges do not intersect!");
|
||||
// LCOV_EXCL_END
|
||||
}
|
||||
add_to_hot_pixels(round_point<T>(pt), rings);
|
||||
swap_positions_in_ABL(bnd, bnd_next, active_bounds);
|
||||
bnd_next = std::next(bnd);
|
||||
isModified = true;
|
||||
} else {
|
||||
bnd = bnd_next;
|
||||
++bnd_next;
|
||||
}
|
||||
}
|
||||
} while (isModified);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void process_hot_pixel_edges_at_top_of_scanbeam(T top_y,
|
||||
scanbeam_list<T>& scanbeam,
|
||||
active_bound_list<T>& active_bounds,
|
||||
ring_manager<T>& rings) {
|
||||
for (auto bnd = active_bounds.begin(); bnd != active_bounds.end();) {
|
||||
auto bnd_2 = std::next(bnd);
|
||||
while ((*bnd)->current_edge != (*bnd)->edges.end() &&
|
||||
(*bnd)->current_edge->top.y == top_y) {
|
||||
add_to_hot_pixels((*bnd)->current_edge->top, rings);
|
||||
if (current_edge_is_horizontal<T>(bnd)) {
|
||||
(*bnd)->current_x = static_cast<double>((*bnd)->current_edge->top.x);
|
||||
if ((*bnd)->current_edge->bot.x < (*bnd)->current_edge->top.x) {
|
||||
// left to right
|
||||
auto bnd_next = std::next(bnd);
|
||||
while (bnd_next != active_bounds.end() &&
|
||||
(*bnd_next)->current_x < (*bnd)->current_x) {
|
||||
if (std::llround((*bnd_next)->current_edge->top.y) != top_y &&
|
||||
std::llround((*bnd_next)->current_edge->bot.y) != top_y) {
|
||||
mapbox::geometry::point<T> pt(std::llround((*bnd_next)->current_x),
|
||||
top_y);
|
||||
add_to_hot_pixels(pt, rings);
|
||||
}
|
||||
swap_positions_in_ABL(bnd, bnd_next, active_bounds);
|
||||
bnd_next = std::next(bnd);
|
||||
}
|
||||
} else {
|
||||
// right to left
|
||||
if (bnd != active_bounds.begin()) {
|
||||
auto bnd_prev = std::prev(bnd);
|
||||
while (bnd != active_bounds.begin() &&
|
||||
(*bnd_prev)->current_x > (*bnd)->current_x) {
|
||||
if (std::llround((*bnd_prev)->current_edge->top.y) != top_y &&
|
||||
std::llround((*bnd_prev)->current_edge->bot.y) != top_y) {
|
||||
mapbox::geometry::point<T> pt(std::llround((*bnd_prev)->current_x),
|
||||
top_y);
|
||||
add_to_hot_pixels(pt, rings);
|
||||
}
|
||||
swap_positions_in_ABL(bnd, bnd_prev, active_bounds);
|
||||
bnd_prev = std::prev(bnd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
next_edge_in_bound(bnd, scanbeam);
|
||||
}
|
||||
if ((*bnd)->current_edge == (*bnd)->edges.end()) {
|
||||
active_bounds.erase(bnd);
|
||||
}
|
||||
bnd = bnd_2;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void insert_local_minima_into_ABL_hot_pixel(T top_y,
|
||||
local_minimum_ptr_list<T>& minima_sorted,
|
||||
local_minimum_ptr_list_itr<T>& lm,
|
||||
active_bound_list<T>& active_bounds,
|
||||
ring_manager<T>& rings,
|
||||
scanbeam_list<T>& scanbeam) {
|
||||
while (lm != minima_sorted.end() && (*lm)->y == top_y) {
|
||||
add_to_hot_pixels((*lm)->left_bound.edges.front().bot, rings);
|
||||
auto& left_bound = (*lm)->left_bound;
|
||||
left_bound.current_edge = left_bound.edges.begin();
|
||||
left_bound.current_x = static_cast<double>(left_bound.current_edge->bot.x);
|
||||
auto lb_abl_itr = insert_bound_into_ABL(left_bound, active_bounds);
|
||||
if (!current_edge_is_horizontal<T>(lb_abl_itr)) {
|
||||
scanbeam.push((*lb_abl_itr)->current_edge->top.y);
|
||||
}
|
||||
auto& right_bound = (*lm)->right_bound;
|
||||
right_bound.current_edge = right_bound.edges.begin();
|
||||
right_bound.current_x = static_cast<double>(right_bound.current_edge->bot.x);
|
||||
auto rb_abl_itr = insert_bound_into_ABL(right_bound, lb_abl_itr, active_bounds);
|
||||
if (!current_edge_is_horizontal<T>(rb_abl_itr)) {
|
||||
scanbeam.push((*rb_abl_itr)->current_edge->top.y);
|
||||
}
|
||||
++lm;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void build_hot_pixels(local_minimum_list<T>& minima_list, ring_manager<T>& rings) {
|
||||
active_bound_list<T> active_bounds;
|
||||
scanbeam_list<T> scanbeam;
|
||||
T scanline_y = std::numeric_limits<T>::max();
|
||||
|
||||
local_minimum_ptr_list<T> minima_sorted;
|
||||
minima_sorted.reserve(minima_list.size());
|
||||
for (auto& lm : minima_list) {
|
||||
minima_sorted.push_back(&lm);
|
||||
}
|
||||
std::stable_sort(minima_sorted.begin(), minima_sorted.end(), local_minimum_sorter<T>());
|
||||
local_minimum_ptr_list_itr<T> current_lm = minima_sorted.begin();
|
||||
|
||||
setup_scanbeam(minima_list, scanbeam);
|
||||
|
||||
// Estimate size for reserving hot pixels
|
||||
std::size_t reserve = 0;
|
||||
for (auto& lm : minima_list) {
|
||||
reserve += lm.left_bound.edges.size() + 2;
|
||||
reserve += lm.right_bound.edges.size() + 2;
|
||||
}
|
||||
rings.hot_pixels.reserve(reserve);
|
||||
|
||||
while (pop_from_scanbeam(scanline_y, scanbeam) || current_lm != minima_sorted.end()) {
|
||||
|
||||
process_hot_pixel_intersections(scanline_y, active_bounds, rings);
|
||||
|
||||
insert_local_minima_into_ABL_hot_pixel(scanline_y, minima_sorted, current_lm, active_bounds,
|
||||
rings, scanbeam);
|
||||
|
||||
process_hot_pixel_edges_at_top_of_scanbeam(scanline_y, scanbeam, active_bounds, rings);
|
||||
}
|
||||
preallocate_point_memory(rings, rings.hot_pixels.size());
|
||||
sort_hot_pixels(rings);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
1987
mapbox/geometry/wagyu/topology_correction.hpp
Normal file
1987
mapbox/geometry/wagyu/topology_correction.hpp
Normal file
File diff suppressed because it is too large
Load Diff
79
mapbox/geometry/wagyu/util.hpp
Normal file
79
mapbox/geometry/wagyu/util.hpp
Normal file
@ -0,0 +1,79 @@
|
||||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include <mapbox/geometry/point.hpp>
|
||||
#include <mapbox/geometry/polygon.hpp>
|
||||
#include <mapbox/geometry/wagyu/point.hpp>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
|
||||
template <typename T>
|
||||
double area(mapbox::geometry::linear_ring<T> const& poly) {
|
||||
std::size_t size = poly.size();
|
||||
if (size < 3) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
double a = 0.0;
|
||||
auto itr = poly.begin();
|
||||
auto itr_prev = poly.end();
|
||||
--itr_prev;
|
||||
a += static_cast<double>(itr_prev->x + itr->x) * static_cast<double>(itr_prev->y - itr->y);
|
||||
++itr;
|
||||
itr_prev = poly.begin();
|
||||
for (; itr != poly.end(); ++itr, ++itr_prev) {
|
||||
a += static_cast<double>(itr_prev->x + itr->x) * static_cast<double>(itr_prev->y - itr->y);
|
||||
}
|
||||
return -a * 0.5;
|
||||
}
|
||||
|
||||
inline bool value_is_zero(double val) {
|
||||
return std::fabs(val) < std::numeric_limits<double>::epsilon();
|
||||
}
|
||||
|
||||
inline bool values_are_equal(double x, double y) {
|
||||
return value_is_zero(x - y);
|
||||
}
|
||||
|
||||
inline bool values_near_equal(double x, double y) {
|
||||
return std::fabs(x - y) < (5.0 * std::numeric_limits<double>::epsilon());
|
||||
}
|
||||
|
||||
inline bool greater_than_or_equal(double x, double y) {
|
||||
return x > y || values_are_equal(x, y);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool slopes_equal(mapbox::geometry::point<T> const& pt1,
|
||||
mapbox::geometry::point<T> const& pt2,
|
||||
mapbox::geometry::point<T> const& pt3) {
|
||||
return (pt1.y - pt2.y) * (pt2.x - pt3.x) == (pt1.x - pt2.x) * (pt2.y - pt3.y);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool slopes_equal(mapbox::geometry::wagyu::point<T> const& pt1,
|
||||
mapbox::geometry::wagyu::point<T> const& pt2,
|
||||
mapbox::geometry::point<T> const& pt3) {
|
||||
return (pt1.y - pt2.y) * (pt2.x - pt3.x) == (pt1.x - pt2.x) * (pt2.y - pt3.y);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool slopes_equal(mapbox::geometry::wagyu::point<T> const& pt1,
|
||||
mapbox::geometry::wagyu::point<T> const& pt2,
|
||||
mapbox::geometry::wagyu::point<T> const& pt3) {
|
||||
return (pt1.y - pt2.y) * (pt2.x - pt3.x) == (pt1.x - pt2.x) * (pt2.y - pt3.y);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool slopes_equal(mapbox::geometry::point<T> const& pt1,
|
||||
mapbox::geometry::point<T> const& pt2,
|
||||
mapbox::geometry::point<T> const& pt3,
|
||||
mapbox::geometry::point<T> const& pt4) {
|
||||
return (pt1.y - pt2.y) * (pt3.x - pt4.x) == (pt1.x - pt2.x) * (pt3.y - pt4.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
74
mapbox/geometry/wagyu/vatti.hpp
Normal file
74
mapbox/geometry/wagyu/vatti.hpp
Normal file
@ -0,0 +1,74 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <set>
|
||||
|
||||
#include <mapbox/geometry/wagyu/active_bound_list.hpp>
|
||||
#include <mapbox/geometry/wagyu/config.hpp>
|
||||
#include <mapbox/geometry/wagyu/intersect_util.hpp>
|
||||
#include <mapbox/geometry/wagyu/local_minimum.hpp>
|
||||
#include <mapbox/geometry/wagyu/local_minimum_util.hpp>
|
||||
#include <mapbox/geometry/wagyu/process_horizontal.hpp>
|
||||
#include <mapbox/geometry/wagyu/process_maxima.hpp>
|
||||
#include <mapbox/geometry/wagyu/ring.hpp>
|
||||
#include <mapbox/geometry/wagyu/ring_util.hpp>
|
||||
#include <mapbox/geometry/wagyu/util.hpp>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
|
||||
template <typename T>
|
||||
bool execute_vatti(local_minimum_list<T>& minima_list,
|
||||
ring_manager<T>& rings,
|
||||
clip_type cliptype,
|
||||
fill_type subject_fill_type,
|
||||
fill_type clip_fill_type) {
|
||||
|
||||
if (minima_list.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
active_bound_list<T> active_bounds;
|
||||
scanbeam_list<T> scanbeam;
|
||||
T scanline_y = std::numeric_limits<T>::max();
|
||||
|
||||
local_minimum_ptr_list<T> minima_sorted;
|
||||
minima_sorted.reserve(minima_list.size());
|
||||
for (auto& lm : minima_list) {
|
||||
minima_sorted.push_back(&lm);
|
||||
}
|
||||
std::stable_sort(minima_sorted.begin(), minima_sorted.end(), local_minimum_sorter<T>());
|
||||
local_minimum_ptr_list_itr<T> current_lm = minima_sorted.begin();
|
||||
// std::clog << output_all_edges(minima_sorted) << std::endl;
|
||||
|
||||
setup_scanbeam(minima_list, scanbeam);
|
||||
rings.current_hp_itr = rings.hot_pixels.begin();
|
||||
|
||||
while (pop_from_scanbeam(scanline_y, scanbeam) || current_lm != minima_sorted.end()) {
|
||||
|
||||
process_intersections(scanline_y, active_bounds, cliptype, subject_fill_type,
|
||||
clip_fill_type, rings);
|
||||
|
||||
update_current_hp_itr(scanline_y, rings);
|
||||
|
||||
// First we process bounds that has already been added to the active bound list --
|
||||
// if the active bound list is empty local minima that are at this scanline_y and
|
||||
// have a horizontal edge at the local minima will be processed
|
||||
process_edges_at_top_of_scanbeam(scanline_y, active_bounds, scanbeam, minima_sorted,
|
||||
current_lm, rings, cliptype, subject_fill_type,
|
||||
clip_fill_type);
|
||||
|
||||
// Next we will add local minima bounds to the active bounds list that are on the local
|
||||
// minima queue at
|
||||
// this current scanline_y
|
||||
insert_local_minima_into_ABL(scanline_y, minima_sorted, current_lm, active_bounds, rings,
|
||||
scanbeam, cliptype, subject_fill_type, clip_fill_type);
|
||||
}
|
||||
// std::clog << rings.rings << std::endl;
|
||||
// std::clog << output_as_polygon(rings.all_rings[0]);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
131
mapbox/geometry/wagyu/wagyu.hpp
Normal file
131
mapbox/geometry/wagyu/wagyu.hpp
Normal file
@ -0,0 +1,131 @@
|
||||
#pragma once
|
||||
|
||||
#include <list>
|
||||
|
||||
#include <mapbox/geometry/box.hpp>
|
||||
#include <mapbox/geometry/line_string.hpp>
|
||||
#include <mapbox/geometry/multi_polygon.hpp>
|
||||
#include <mapbox/geometry/polygon.hpp>
|
||||
|
||||
#include <mapbox/geometry/wagyu/build_local_minima_list.hpp>
|
||||
#include <mapbox/geometry/wagyu/build_result.hpp>
|
||||
#include <mapbox/geometry/wagyu/config.hpp>
|
||||
#include <mapbox/geometry/wagyu/local_minimum.hpp>
|
||||
#include <mapbox/geometry/wagyu/snap_rounding.hpp>
|
||||
#include <mapbox/geometry/wagyu/topology_correction.hpp>
|
||||
#include <mapbox/geometry/wagyu/vatti.hpp>
|
||||
|
||||
namespace mapbox {
|
||||
namespace geometry {
|
||||
namespace wagyu {
|
||||
|
||||
template <typename T>
|
||||
class wagyu {
|
||||
private:
|
||||
using value_type = T;
|
||||
|
||||
local_minimum_list<value_type> minima_list;
|
||||
bool reverse_output;
|
||||
|
||||
wagyu(wagyu const&) = delete;
|
||||
wagyu& operator=(wagyu const&) = delete;
|
||||
|
||||
public:
|
||||
wagyu() : minima_list(), reverse_output(false) {
|
||||
}
|
||||
|
||||
~wagyu() {
|
||||
clear();
|
||||
}
|
||||
|
||||
bool add_ring(mapbox::geometry::linear_ring<value_type> const& pg,
|
||||
polygon_type p_type = polygon_type_subject) {
|
||||
return add_linear_ring(pg, minima_list, p_type);
|
||||
}
|
||||
|
||||
bool add_polygon(mapbox::geometry::polygon<value_type> const& ppg,
|
||||
polygon_type p_type = polygon_type_subject) {
|
||||
bool result = false;
|
||||
for (auto const& r : ppg) {
|
||||
if (add_ring(r, p_type)) {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void reverse_rings(bool value) {
|
||||
reverse_output = value;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
minima_list.clear();
|
||||
}
|
||||
|
||||
mapbox::geometry::box<value_type> get_bounds() {
|
||||
mapbox::geometry::point<value_type> min = { 0, 0 };
|
||||
mapbox::geometry::point<value_type> max = { 0, 0 };
|
||||
if (minima_list.empty()) {
|
||||
return mapbox::geometry::box<value_type>(min, max);
|
||||
}
|
||||
bool first_set = false;
|
||||
for (auto const& lm : minima_list) {
|
||||
if (!lm.left_bound.edges.empty()) {
|
||||
if (!first_set) {
|
||||
min = lm.left_bound.edges.front().top;
|
||||
max = lm.left_bound.edges.back().bot;
|
||||
first_set = true;
|
||||
} else {
|
||||
min.y = std::min(min.y, lm.left_bound.edges.front().top.y);
|
||||
max.y = std::max(max.y, lm.left_bound.edges.back().bot.y);
|
||||
max.x = std::max(max.x, lm.left_bound.edges.back().top.x);
|
||||
min.x = std::min(min.x, lm.left_bound.edges.back().top.x);
|
||||
}
|
||||
for (auto const& e : lm.left_bound.edges) {
|
||||
max.x = std::max(max.x, e.bot.x);
|
||||
min.x = std::min(min.x, e.bot.x);
|
||||
}
|
||||
}
|
||||
if (!lm.right_bound.edges.empty()) {
|
||||
if (!first_set) {
|
||||
min = lm.right_bound.edges.front().top;
|
||||
max = lm.right_bound.edges.back().bot;
|
||||
first_set = true;
|
||||
} else {
|
||||
min.y = std::min(min.y, lm.right_bound.edges.front().top.y);
|
||||
max.y = std::max(max.y, lm.right_bound.edges.back().bot.y);
|
||||
max.x = std::max(max.x, lm.right_bound.edges.back().top.x);
|
||||
min.x = std::min(min.x, lm.right_bound.edges.back().top.x);
|
||||
}
|
||||
for (auto const& e : lm.right_bound.edges) {
|
||||
max.x = std::max(max.x, e.bot.x);
|
||||
min.x = std::min(min.x, e.bot.x);
|
||||
}
|
||||
}
|
||||
}
|
||||
return mapbox::geometry::box<value_type>(min, max);
|
||||
}
|
||||
|
||||
bool execute(clip_type cliptype,
|
||||
mapbox::geometry::multi_polygon<value_type>& solution,
|
||||
fill_type subject_fill_type,
|
||||
fill_type clip_fill_type) {
|
||||
|
||||
ring_manager<T> rings;
|
||||
|
||||
build_hot_pixels(minima_list, rings);
|
||||
|
||||
if (!execute_vatti(minima_list, rings, cliptype, subject_fill_type, clip_fill_type)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
do_simple_polygons(rings);
|
||||
|
||||
build_result(solution, rings, reverse_output);
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
74
mapbox/optional.hpp
Normal file
74
mapbox/optional.hpp
Normal file
@ -0,0 +1,74 @@
|
||||
#ifndef MAPBOX_UTIL_OPTIONAL_HPP
|
||||
#define MAPBOX_UTIL_OPTIONAL_HPP
|
||||
|
||||
#pragma message("This implementation of optional is deprecated. See https://github.com/mapbox/variant/issues/64.")
|
||||
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include <mapbox/variant.hpp>
|
||||
|
||||
namespace mapbox {
|
||||
namespace util {
|
||||
|
||||
template <typename T>
|
||||
class optional
|
||||
{
|
||||
static_assert(!std::is_reference<T>::value, "optional doesn't support references");
|
||||
|
||||
struct none_type
|
||||
{
|
||||
};
|
||||
|
||||
variant<none_type, T> variant_;
|
||||
|
||||
public:
|
||||
optional() = default;
|
||||
|
||||
optional(optional const& rhs)
|
||||
{
|
||||
if (this != &rhs)
|
||||
{ // protect against invalid self-assignment
|
||||
variant_ = rhs.variant_;
|
||||
}
|
||||
}
|
||||
|
||||
optional(T const& v) { variant_ = v; }
|
||||
|
||||
explicit operator bool() const noexcept { return variant_.template is<T>(); }
|
||||
|
||||
T const& get() const { return variant_.template get<T>(); }
|
||||
T& get() { return variant_.template get<T>(); }
|
||||
|
||||
T const& operator*() const { return this->get(); }
|
||||
T operator*() { return this->get(); }
|
||||
|
||||
optional& operator=(T const& v)
|
||||
{
|
||||
variant_ = v;
|
||||
return *this;
|
||||
}
|
||||
|
||||
optional& operator=(optional const& rhs)
|
||||
{
|
||||
if (this != &rhs)
|
||||
{
|
||||
variant_ = rhs.variant_;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
void emplace(Args&&... args)
|
||||
{
|
||||
variant_ = T{std::forward<Args>(args)...};
|
||||
}
|
||||
|
||||
void reset() { variant_ = none_type{}; }
|
||||
|
||||
}; // class optional
|
||||
|
||||
} // namespace util
|
||||
} // namespace mapbox
|
||||
|
||||
#endif // MAPBOX_UTIL_OPTIONAL_HPP
|
122
mapbox/recursive_wrapper.hpp
Normal file
122
mapbox/recursive_wrapper.hpp
Normal file
@ -0,0 +1,122 @@
|
||||
#ifndef MAPBOX_UTIL_RECURSIVE_WRAPPER_HPP
|
||||
#define MAPBOX_UTIL_RECURSIVE_WRAPPER_HPP
|
||||
|
||||
// Based on variant/recursive_wrapper.hpp from boost.
|
||||
//
|
||||
// Original license:
|
||||
//
|
||||
// Copyright (c) 2002-2003
|
||||
// Eric Friedman, Itay Maman
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <cassert>
|
||||
#include <utility>
|
||||
|
||||
namespace mapbox {
|
||||
namespace util {
|
||||
|
||||
template <typename T>
|
||||
class recursive_wrapper
|
||||
{
|
||||
|
||||
T* p_;
|
||||
|
||||
void assign(T const& rhs)
|
||||
{
|
||||
this->get() = rhs;
|
||||
}
|
||||
|
||||
public:
|
||||
using type = T;
|
||||
|
||||
/**
|
||||
* Default constructor default initializes the internally stored value.
|
||||
* For POD types this means nothing is done and the storage is
|
||||
* uninitialized.
|
||||
*
|
||||
* @throws std::bad_alloc if there is insufficient memory for an object
|
||||
* of type T.
|
||||
* @throws any exception thrown by the default constructur of T.
|
||||
*/
|
||||
recursive_wrapper()
|
||||
: p_(new T){}
|
||||
|
||||
~recursive_wrapper() noexcept { delete p_; }
|
||||
|
||||
recursive_wrapper(recursive_wrapper const& operand)
|
||||
: p_(new T(operand.get())) {}
|
||||
|
||||
recursive_wrapper(T const& operand)
|
||||
: p_(new T(operand)) {}
|
||||
|
||||
recursive_wrapper(recursive_wrapper&& operand)
|
||||
: p_(new T(std::move(operand.get()))) {}
|
||||
|
||||
recursive_wrapper(T&& operand)
|
||||
: p_(new T(std::move(operand))) {}
|
||||
|
||||
inline recursive_wrapper& operator=(recursive_wrapper const& rhs)
|
||||
{
|
||||
assign(rhs.get());
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline recursive_wrapper& operator=(T const& rhs)
|
||||
{
|
||||
assign(rhs);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline void swap(recursive_wrapper& operand) noexcept
|
||||
{
|
||||
T* temp = operand.p_;
|
||||
operand.p_ = p_;
|
||||
p_ = temp;
|
||||
}
|
||||
|
||||
recursive_wrapper& operator=(recursive_wrapper&& rhs) noexcept
|
||||
{
|
||||
swap(rhs);
|
||||
return *this;
|
||||
}
|
||||
|
||||
recursive_wrapper& operator=(T&& rhs)
|
||||
{
|
||||
get() = std::move(rhs);
|
||||
return *this;
|
||||
}
|
||||
|
||||
T& get()
|
||||
{
|
||||
assert(p_);
|
||||
return *get_pointer();
|
||||
}
|
||||
|
||||
T const& get() const
|
||||
{
|
||||
assert(p_);
|
||||
return *get_pointer();
|
||||
}
|
||||
|
||||
T* get_pointer() { return p_; }
|
||||
|
||||
const T* get_pointer() const { return p_; }
|
||||
|
||||
operator T const&() const { return this->get(); }
|
||||
|
||||
operator T&() { return this->get(); }
|
||||
|
||||
}; // class recursive_wrapper
|
||||
|
||||
template <typename T>
|
||||
inline void swap(recursive_wrapper<T>& lhs, recursive_wrapper<T>& rhs) noexcept
|
||||
{
|
||||
lhs.swap(rhs);
|
||||
}
|
||||
} // namespace util
|
||||
} // namespace mapbox
|
||||
|
||||
#endif // MAPBOX_UTIL_RECURSIVE_WRAPPER_HPP
|
1013
mapbox/variant.hpp
Normal file
1013
mapbox/variant.hpp
Normal file
File diff suppressed because it is too large
Load Diff
45
mapbox/variant_io.hpp
Normal file
45
mapbox/variant_io.hpp
Normal file
@ -0,0 +1,45 @@
|
||||
#ifndef MAPBOX_UTIL_VARIANT_IO_HPP
|
||||
#define MAPBOX_UTIL_VARIANT_IO_HPP
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
#include <mapbox/variant.hpp>
|
||||
|
||||
namespace mapbox {
|
||||
namespace util {
|
||||
|
||||
namespace detail {
|
||||
// operator<< helper
|
||||
template <typename Out>
|
||||
class printer
|
||||
{
|
||||
public:
|
||||
explicit printer(Out& out)
|
||||
: out_(out) {}
|
||||
printer& operator=(printer const&) = delete;
|
||||
|
||||
// visitor
|
||||
template <typename T>
|
||||
void operator()(T const& operand) const
|
||||
{
|
||||
out_ << operand;
|
||||
}
|
||||
|
||||
private:
|
||||
Out& out_;
|
||||
};
|
||||
}
|
||||
|
||||
// operator<<
|
||||
template <typename CharT, typename Traits, typename... Types>
|
||||
VARIANT_INLINE std::basic_ostream<CharT, Traits>&
|
||||
operator<<(std::basic_ostream<CharT, Traits>& out, variant<Types...> const& rhs)
|
||||
{
|
||||
detail::printer<std::basic_ostream<CharT, Traits>> visitor(out);
|
||||
apply_visitor(visitor, rhs);
|
||||
return out;
|
||||
}
|
||||
} // namespace util
|
||||
} // namespace mapbox
|
||||
|
||||
#endif // MAPBOX_UTIL_VARIANT_IO_HPP
|
38
mapbox/variant_visitor.hpp
Normal file
38
mapbox/variant_visitor.hpp
Normal file
@ -0,0 +1,38 @@
|
||||
#ifndef MAPBOX_UTIL_VARIANT_VISITOR_HPP
|
||||
#define MAPBOX_UTIL_VARIANT_VISITOR_HPP
|
||||
|
||||
namespace mapbox {
|
||||
namespace util {
|
||||
|
||||
template <typename... Fns>
|
||||
struct visitor;
|
||||
|
||||
template <typename Fn>
|
||||
struct visitor<Fn> : Fn
|
||||
{
|
||||
using type = Fn;
|
||||
using Fn::operator();
|
||||
|
||||
visitor(Fn fn) : Fn(fn) {}
|
||||
};
|
||||
|
||||
template <typename Fn, typename... Fns>
|
||||
struct visitor<Fn, Fns...> : Fn, visitor<Fns...>
|
||||
{
|
||||
using type = visitor;
|
||||
using Fn::operator();
|
||||
using visitor<Fns...>::operator();
|
||||
|
||||
visitor(Fn fn, Fns... fns) : Fn(fn), visitor<Fns...>(fns...) {}
|
||||
};
|
||||
|
||||
template <typename... Fns>
|
||||
visitor<Fns...> make_visitor(Fns... fns)
|
||||
{
|
||||
return visitor<Fns...>(fns...);
|
||||
}
|
||||
|
||||
} // namespace util
|
||||
} // namespace mapbox
|
||||
|
||||
#endif // MAPBOX_UTIL_VARIANT_VISITOR_HPP
|
@ -1,13 +1,20 @@
|
||||
// for vasprintf() on Linux
|
||||
#ifndef _GNU_SOURCE
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sqlite3.h>
|
||||
#include "pool.h"
|
||||
#include "tile.h"
|
||||
#include "mbtiles.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include "main.hpp"
|
||||
#include "pool.hpp"
|
||||
#include "mbtiles.hpp"
|
||||
#include "geometry.hpp"
|
||||
|
||||
sqlite3 *mbtiles_open(char *dbname, char **argv, int forcetable) {
|
||||
sqlite3 *outdb;
|
||||
@ -79,7 +86,7 @@ void mbtiles_write_tile(sqlite3 *outdb, int z, int tx, int ty, const char *data,
|
||||
}
|
||||
}
|
||||
|
||||
static void quote(char **buf, const char *s) {
|
||||
static void quote(std::string *buf, const char *s) {
|
||||
char tmp[strlen(s) * 8 + 1];
|
||||
char *out = tmp;
|
||||
|
||||
@ -98,15 +105,10 @@ static void quote(char **buf, const char *s) {
|
||||
}
|
||||
|
||||
*out = '\0';
|
||||
*buf = realloc(*buf, strlen(*buf) + strlen(tmp) + 1);
|
||||
if (*buf == NULL) {
|
||||
perror("realloc");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
strcat(*buf, tmp);
|
||||
buf->append(tmp, strlen(tmp));
|
||||
}
|
||||
|
||||
static void aprintf(char **buf, const char *format, ...) {
|
||||
void aprintf(std::string *buf, const char *format, ...) {
|
||||
va_list ap;
|
||||
char *tmp;
|
||||
|
||||
@ -117,24 +119,21 @@ static void aprintf(char **buf, const char *format, ...) {
|
||||
}
|
||||
va_end(ap);
|
||||
|
||||
*buf = realloc(*buf, strlen(*buf) + strlen(tmp) + 1);
|
||||
strcat(*buf, tmp);
|
||||
buf->append(tmp, strlen(tmp));
|
||||
free(tmp);
|
||||
}
|
||||
|
||||
static int pvcmp(const void *v1, const void *v2) {
|
||||
const struct pool_val *const *pv1 = v1;
|
||||
const struct pool_val *const *pv2 = v2;
|
||||
|
||||
int n = strcmp((*pv1)->s, (*pv2)->s);
|
||||
if (n != 0) {
|
||||
return n;
|
||||
bool type_and_string::operator<(const type_and_string &o) const {
|
||||
if (string < o.string) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return (*pv1)->type - (*pv2)->type;
|
||||
if (string == o.string && type < o.type) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void mbtiles_write_metadata(sqlite3 *outdb, const char *fname, char **layername, int minzoom, int maxzoom, double minlat, double minlon, double maxlat, double maxlon, double midlat, double midlon, struct pool **file_keys, int nlayers, int forcetable) {
|
||||
void mbtiles_write_metadata(sqlite3 *outdb, const char *fname, int minzoom, int maxzoom, double minlat, double minlon, double maxlat, double maxlon, double midlat, double midlon, int forcetable, const char *attribution, std::map<std::string, layermap_entry> const &layermap) {
|
||||
char *sql, *err;
|
||||
|
||||
sql = sqlite3_mprintf("INSERT INTO metadata (name, value) VALUES ('name', %Q);", fname);
|
||||
@ -209,6 +208,17 @@ void mbtiles_write_metadata(sqlite3 *outdb, const char *fname, char **layername,
|
||||
}
|
||||
sqlite3_free(sql);
|
||||
|
||||
if (attribution != NULL) {
|
||||
sql = sqlite3_mprintf("INSERT INTO metadata (name, value) VALUES ('attribution', %Q);", attribution);
|
||||
if (sqlite3_exec(outdb, sql, NULL, NULL, &err) != SQLITE_OK) {
|
||||
fprintf(stderr, "set type: %s\n", err);
|
||||
if (!forcetable) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
sqlite3_free(sql);
|
||||
}
|
||||
|
||||
sql = sqlite3_mprintf("INSERT INTO metadata (name, value) VALUES ('format', %Q);", "pbf");
|
||||
if (sqlite3_exec(outdb, sql, NULL, NULL, &err) != SQLITE_OK) {
|
||||
fprintf(stderr, "set format: %s\n", err);
|
||||
@ -218,50 +228,43 @@ void mbtiles_write_metadata(sqlite3 *outdb, const char *fname, char **layername,
|
||||
}
|
||||
sqlite3_free(sql);
|
||||
|
||||
char *buf = strdup("{");
|
||||
std::string buf("{");
|
||||
aprintf(&buf, "\"vector_layers\": [ ");
|
||||
|
||||
int i;
|
||||
for (i = 0; i < nlayers; i++) {
|
||||
std::vector<std::string> lnames;
|
||||
for (auto ai = layermap.begin(); ai != layermap.end(); ++ai) {
|
||||
lnames.push_back(ai->first);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < lnames.size(); i++) {
|
||||
if (i != 0) {
|
||||
aprintf(&buf, ", ");
|
||||
}
|
||||
|
||||
auto fk = layermap.find(lnames[i]);
|
||||
aprintf(&buf, "{ \"id\": \"");
|
||||
quote(&buf, layername[i]);
|
||||
aprintf(&buf, "\", \"description\": \"\", \"minzoom\": %d, \"maxzoom\": %d, \"fields\": {", minzoom, maxzoom);
|
||||
quote(&buf, lnames[i].c_str());
|
||||
aprintf(&buf, "\", \"description\": \"\", \"minzoom\": %d, \"maxzoom\": %d, \"fields\": {", fk->second.minzoom, fk->second.maxzoom);
|
||||
|
||||
int n = 0;
|
||||
struct pool_val *pv;
|
||||
for (pv = file_keys[i]->head; pv != NULL; pv = pv->next) {
|
||||
n++;
|
||||
}
|
||||
std::set<type_and_string>::iterator j;
|
||||
bool first = true;
|
||||
for (j = fk->second.file_keys.begin(); j != fk->second.file_keys.end(); ++j) {
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
aprintf(&buf, ", ");
|
||||
}
|
||||
|
||||
struct pool_val *vals[n];
|
||||
n = 0;
|
||||
for (pv = file_keys[i]->head; pv != NULL; pv = pv->next) {
|
||||
vals[n++] = pv;
|
||||
}
|
||||
|
||||
qsort(vals, n, sizeof(struct pool_val *), pvcmp);
|
||||
|
||||
int j;
|
||||
for (j = 0; j < n; j++) {
|
||||
pv = vals[j];
|
||||
aprintf(&buf, "\"");
|
||||
quote(&buf, pv->s);
|
||||
quote(&buf, j->string.c_str());
|
||||
|
||||
if (pv->type == VT_NUMBER) {
|
||||
if (j->type == VT_NUMBER) {
|
||||
aprintf(&buf, "\": \"Number\"");
|
||||
} else if (pv->type == VT_BOOLEAN) {
|
||||
} else if (j->type == VT_BOOLEAN) {
|
||||
aprintf(&buf, "\": \"Boolean\"");
|
||||
} else {
|
||||
aprintf(&buf, "\": \"String\"");
|
||||
}
|
||||
|
||||
if (j + 1 < n) {
|
||||
aprintf(&buf, ", ");
|
||||
}
|
||||
}
|
||||
|
||||
aprintf(&buf, "} }");
|
||||
@ -269,7 +272,7 @@ void mbtiles_write_metadata(sqlite3 *outdb, const char *fname, char **layername,
|
||||
|
||||
aprintf(&buf, " ] }");
|
||||
|
||||
sql = sqlite3_mprintf("INSERT INTO metadata (name, value) VALUES ('json', %Q);", buf);
|
||||
sql = sqlite3_mprintf("INSERT INTO metadata (name, value) VALUES ('json', %Q);", buf.c_str());
|
||||
if (sqlite3_exec(outdb, sql, NULL, NULL, &err) != SQLITE_OK) {
|
||||
fprintf(stderr, "set json: %s\n", err);
|
||||
if (!forcetable) {
|
||||
@ -277,7 +280,6 @@ void mbtiles_write_metadata(sqlite3 *outdb, const char *fname, char **layername,
|
||||
}
|
||||
}
|
||||
sqlite3_free(sql);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
void mbtiles_close(sqlite3 *outdb, char **argv) {
|
||||
@ -292,3 +294,37 @@ void mbtiles_close(sqlite3 *outdb, char **argv) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
std::map<std::string, layermap_entry> merge_layermaps(std::vector<std::map<std::string, layermap_entry> > const &maps) {
|
||||
std::map<std::string, layermap_entry> out;
|
||||
|
||||
for (size_t i = 0; i < maps.size(); i++) {
|
||||
for (auto map = maps[i].begin(); map != maps[i].end(); ++map) {
|
||||
if (out.count(map->first) == 0) {
|
||||
out.insert(std::pair<std::string, layermap_entry>(map->first, layermap_entry(out.size())));
|
||||
auto out_entry = out.find(map->first);
|
||||
out_entry->second.minzoom = map->second.minzoom;
|
||||
out_entry->second.maxzoom = map->second.maxzoom;
|
||||
}
|
||||
|
||||
auto out_entry = out.find(map->first);
|
||||
if (out_entry == out.end()) {
|
||||
fprintf(stderr, "Internal error merging layers\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for (auto fk = map->second.file_keys.begin(); fk != map->second.file_keys.end(); ++fk) {
|
||||
out_entry->second.file_keys.insert(*fk);
|
||||
}
|
||||
|
||||
if (map->second.minzoom < out_entry->second.minzoom) {
|
||||
out_entry->second.minzoom = map->second.minzoom;
|
||||
}
|
||||
if (map->second.maxzoom > out_entry->second.maxzoom) {
|
||||
out_entry->second.maxzoom = map->second.maxzoom;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
sqlite3 *mbtiles_open(char *dbname, char **argv, int forcetable);
|
||||
|
||||
void mbtiles_write_tile(sqlite3 *outdb, int z, int tx, int ty, const char *data, int size);
|
||||
|
||||
void mbtiles_write_metadata(sqlite3 *outdb, const char *fname, char **layername, int minzoom, int maxzoom, double minlat, double minlon, double maxlat, double maxlon, double midlat, double midlon, struct pool **file_keys, int nlayers, int forcetable);
|
||||
|
||||
void mbtiles_close(sqlite3 *outdb, char **argv);
|
29
mbtiles.hpp
Normal file
29
mbtiles.hpp
Normal file
@ -0,0 +1,29 @@
|
||||
struct type_and_string {
|
||||
int type;
|
||||
std::string string;
|
||||
|
||||
bool operator<(const type_and_string &o) const;
|
||||
};
|
||||
|
||||
struct layermap_entry {
|
||||
size_t id;
|
||||
std::set<type_and_string> file_keys;
|
||||
int minzoom;
|
||||
int maxzoom;
|
||||
|
||||
layermap_entry(size_t _id) {
|
||||
id = _id;
|
||||
}
|
||||
};
|
||||
|
||||
sqlite3 *mbtiles_open(char *dbname, char **argv, int forcetable);
|
||||
|
||||
void mbtiles_write_tile(sqlite3 *outdb, int z, int tx, int ty, const char *data, int size);
|
||||
|
||||
void mbtiles_write_metadata(sqlite3 *outdb, const char *fname, int minzoom, int maxzoom, double minlat, double minlon, double maxlat, double maxlon, double midlat, double midlon, int forcetable, const char *attribution, std::map<std::string, layermap_entry> const &layermap);
|
||||
|
||||
void mbtiles_close(sqlite3 *outdb, char **argv);
|
||||
|
||||
void aprintf(std::string *buf, const char *format, ...);
|
||||
|
||||
std::map<std::string, layermap_entry> merge_layermaps(std::vector<std::map<std::string, layermap_entry> > const &maps);
|
@ -2,29 +2,30 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
#include "memfile.h"
|
||||
#include "memfile.hpp"
|
||||
|
||||
#define INCREMENT 131072
|
||||
#define INITIAL 256
|
||||
|
||||
struct memfile *memfile_open(int fd) {
|
||||
if (ftruncate(fd, INCREMENT) != 0) {
|
||||
if (ftruncate(fd, INITIAL) != 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *map = mmap(NULL, INCREMENT, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
char *map = (char *) mmap(NULL, INITIAL, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (map == MAP_FAILED) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct memfile *mf = malloc(sizeof(struct memfile));
|
||||
struct memfile *mf = new memfile;
|
||||
if (mf == NULL) {
|
||||
munmap(map, INCREMENT);
|
||||
munmap(map, INITIAL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mf->fd = fd;
|
||||
mf->map = map;
|
||||
mf->len = INCREMENT;
|
||||
mf->len = INITIAL;
|
||||
mf->off = 0;
|
||||
mf->tree = 0;
|
||||
|
||||
@ -42,7 +43,7 @@ int memfile_close(struct memfile *file) {
|
||||
}
|
||||
}
|
||||
|
||||
free(file);
|
||||
delete file;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -58,7 +59,7 @@ int memfile_write(struct memfile *file, void *s, long long len) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
file->map = mmap(NULL, file->len, PROT_READ | PROT_WRITE, MAP_SHARED, file->fd, 0);
|
||||
file->map = (char *) mmap(NULL, file->len, PROT_READ | PROT_WRITE, MAP_SHARED, file->fd, 0);
|
||||
if (file->map == MAP_FAILED) {
|
||||
return -1;
|
||||
}
|
419
mvt.cpp
Normal file
419
mvt.cpp
Normal file
@ -0,0 +1,419 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <zlib.h>
|
||||
#include "mvt.hpp"
|
||||
#include "protozero/varint.hpp"
|
||||
#include "protozero/pbf_reader.hpp"
|
||||
#include "protozero/pbf_writer.hpp"
|
||||
|
||||
mvt_geometry::mvt_geometry(int nop, long long nx, long long ny) {
|
||||
this->op = nop;
|
||||
this->x = nx;
|
||||
this->y = ny;
|
||||
}
|
||||
|
||||
// https://github.com/mapbox/mapnik-vector-tile/blob/master/src/vector_tile_compression.hpp
|
||||
bool is_compressed(std::string const &data) {
|
||||
return data.size() > 2 && (((uint8_t) data[0] == 0x78 && (uint8_t) data[1] == 0x9C) || ((uint8_t) data[0] == 0x1F && (uint8_t) data[1] == 0x8B));
|
||||
}
|
||||
|
||||
// https://github.com/mapbox/mapnik-vector-tile/blob/master/src/vector_tile_compression.hpp
|
||||
int decompress(std::string const &input, std::string &output) {
|
||||
z_stream inflate_s;
|
||||
inflate_s.zalloc = Z_NULL;
|
||||
inflate_s.zfree = Z_NULL;
|
||||
inflate_s.opaque = Z_NULL;
|
||||
inflate_s.avail_in = 0;
|
||||
inflate_s.next_in = Z_NULL;
|
||||
if (inflateInit2(&inflate_s, 32 + 15) != Z_OK) {
|
||||
fprintf(stderr, "error: %s\n", inflate_s.msg);
|
||||
}
|
||||
inflate_s.next_in = (Bytef *) input.data();
|
||||
inflate_s.avail_in = input.size();
|
||||
size_t length = 0;
|
||||
do {
|
||||
output.resize(length + 2 * input.size());
|
||||
inflate_s.avail_out = 2 * input.size();
|
||||
inflate_s.next_out = (Bytef *) (output.data() + length);
|
||||
int ret = inflate(&inflate_s, Z_FINISH);
|
||||
if (ret != Z_STREAM_END && ret != Z_OK && ret != Z_BUF_ERROR) {
|
||||
fprintf(stderr, "error: %s\n", inflate_s.msg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
length += (2 * input.size() - inflate_s.avail_out);
|
||||
} while (inflate_s.avail_out == 0);
|
||||
inflateEnd(&inflate_s);
|
||||
output.resize(length);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// https://github.com/mapbox/mapnik-vector-tile/blob/master/src/vector_tile_compression.hpp
|
||||
int compress(std::string const &input, std::string &output) {
|
||||
z_stream deflate_s;
|
||||
deflate_s.zalloc = Z_NULL;
|
||||
deflate_s.zfree = Z_NULL;
|
||||
deflate_s.opaque = Z_NULL;
|
||||
deflate_s.avail_in = 0;
|
||||
deflate_s.next_in = Z_NULL;
|
||||
deflateInit2(&deflate_s, Z_BEST_COMPRESSION, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY);
|
||||
deflate_s.next_in = (Bytef *) input.data();
|
||||
deflate_s.avail_in = input.size();
|
||||
size_t length = 0;
|
||||
do {
|
||||
size_t increase = input.size() / 2 + 1024;
|
||||
output.resize(length + increase);
|
||||
deflate_s.avail_out = increase;
|
||||
deflate_s.next_out = (Bytef *) (output.data() + length);
|
||||
int ret = deflate(&deflate_s, Z_FINISH);
|
||||
if (ret != Z_STREAM_END && ret != Z_OK && ret != Z_BUF_ERROR) {
|
||||
return -1;
|
||||
}
|
||||
length += (increase - deflate_s.avail_out);
|
||||
} while (deflate_s.avail_out == 0);
|
||||
deflateEnd(&deflate_s);
|
||||
output.resize(length);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool mvt_tile::decode(std::string &message) {
|
||||
layers.clear();
|
||||
std::string src;
|
||||
|
||||
if (is_compressed(message)) {
|
||||
std::string uncompressed;
|
||||
decompress(message, uncompressed);
|
||||
src = uncompressed;
|
||||
} else {
|
||||
src = message;
|
||||
}
|
||||
|
||||
protozero::pbf_reader reader(src);
|
||||
|
||||
while (reader.next()) {
|
||||
switch (reader.tag()) {
|
||||
case 3: /* layer */
|
||||
{
|
||||
protozero::pbf_reader layer_reader(reader.get_message());
|
||||
mvt_layer layer;
|
||||
|
||||
while (layer_reader.next()) {
|
||||
switch (layer_reader.tag()) {
|
||||
case 1: /* name */
|
||||
layer.name = layer_reader.get_string();
|
||||
break;
|
||||
|
||||
case 3: /* key */
|
||||
layer.keys.push_back(layer_reader.get_string());
|
||||
break;
|
||||
|
||||
case 4: /* value */
|
||||
{
|
||||
protozero::pbf_reader value_reader(layer_reader.get_message());
|
||||
mvt_value value;
|
||||
|
||||
while (value_reader.next()) {
|
||||
switch (value_reader.tag()) {
|
||||
case 1: /* string */
|
||||
value.type = mvt_string;
|
||||
value.string_value = value_reader.get_string();
|
||||
break;
|
||||
|
||||
case 2: /* float */
|
||||
value.type = mvt_float;
|
||||
value.numeric_value.float_value = value_reader.get_float();
|
||||
break;
|
||||
|
||||
case 3: /* double */
|
||||
value.type = mvt_double;
|
||||
value.numeric_value.double_value = value_reader.get_double();
|
||||
break;
|
||||
|
||||
case 4: /* int */
|
||||
value.type = mvt_int;
|
||||
value.numeric_value.int_value = value_reader.get_int64();
|
||||
break;
|
||||
|
||||
case 5: /* uint */
|
||||
value.type = mvt_uint;
|
||||
value.numeric_value.uint_value = value_reader.get_uint64();
|
||||
break;
|
||||
|
||||
case 6: /* sint */
|
||||
value.type = mvt_sint;
|
||||
value.numeric_value.sint_value = value_reader.get_sint64();
|
||||
break;
|
||||
|
||||
case 7: /* bool */
|
||||
value.type = mvt_bool;
|
||||
value.numeric_value.bool_value = value_reader.get_bool();
|
||||
break;
|
||||
|
||||
default:
|
||||
value_reader.skip();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
layer.values.push_back(value);
|
||||
break;
|
||||
}
|
||||
|
||||
case 5: /* extent */
|
||||
layer.extent = layer_reader.get_uint32();
|
||||
break;
|
||||
|
||||
case 15: /* version */
|
||||
layer.version = layer_reader.get_uint32();
|
||||
break;
|
||||
|
||||
case 2: /* feature */
|
||||
{
|
||||
protozero::pbf_reader feature_reader(layer_reader.get_message());
|
||||
mvt_feature feature;
|
||||
std::vector<uint32_t> geoms;
|
||||
|
||||
while (feature_reader.next()) {
|
||||
switch (feature_reader.tag()) {
|
||||
case 1: /* id */
|
||||
feature.id = feature_reader.get_uint64();
|
||||
feature.has_id = true;
|
||||
break;
|
||||
|
||||
case 2: /* tag */
|
||||
{
|
||||
auto pi = feature_reader.get_packed_uint32();
|
||||
for (auto it = pi.first; it != pi.second; ++it) {
|
||||
feature.tags.push_back(*it);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 3: /* feature type */
|
||||
feature.type = feature_reader.get_enum();
|
||||
break;
|
||||
|
||||
case 4: /* geometry */
|
||||
{
|
||||
auto pi = feature_reader.get_packed_uint32();
|
||||
for (auto it = pi.first; it != pi.second; ++it) {
|
||||
geoms.push_back(*it);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
feature_reader.skip();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
long long px = 0, py = 0;
|
||||
for (size_t g = 0; g < geoms.size(); g++) {
|
||||
uint32_t geom = geoms[g];
|
||||
uint32_t op = geom & 7;
|
||||
uint32_t count = geom >> 3;
|
||||
|
||||
if (op == mvt_moveto || op == mvt_lineto) {
|
||||
for (size_t k = 0; k < count && g + 2 < geoms.size(); k++) {
|
||||
px += protozero::decode_zigzag32(geoms[g + 1]);
|
||||
py += protozero::decode_zigzag32(geoms[g + 2]);
|
||||
g += 2;
|
||||
|
||||
feature.geometry.push_back(mvt_geometry(op, px, py));
|
||||
}
|
||||
} else {
|
||||
feature.geometry.push_back(mvt_geometry(op, 0, 0));
|
||||
}
|
||||
}
|
||||
|
||||
layer.features.push_back(feature);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
layer_reader.skip();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < layer.keys.size(); i++) {
|
||||
layer.key_map.insert(std::pair<std::string, size_t>(layer.keys[i], i));
|
||||
}
|
||||
for (size_t i = 0; i < layer.values.size(); i++) {
|
||||
layer.value_map.insert(std::pair<mvt_value, size_t>(layer.values[i], i));
|
||||
}
|
||||
|
||||
layers.push_back(layer);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
reader.skip();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string mvt_tile::encode() {
|
||||
std::string data;
|
||||
|
||||
protozero::pbf_writer writer(data);
|
||||
|
||||
for (size_t i = 0; i < layers.size(); i++) {
|
||||
std::string layer_string;
|
||||
protozero::pbf_writer layer_writer(layer_string);
|
||||
|
||||
layer_writer.add_uint32(15, layers[i].version); /* version */
|
||||
layer_writer.add_string(1, layers[i].name); /* name */
|
||||
layer_writer.add_uint32(5, layers[i].extent); /* extent */
|
||||
|
||||
for (size_t j = 0; j < layers[i].keys.size(); j++) {
|
||||
layer_writer.add_string(3, layers[i].keys[j]); /* key */
|
||||
}
|
||||
|
||||
for (size_t v = 0; v < layers[i].values.size(); v++) {
|
||||
std::string value_string;
|
||||
protozero::pbf_writer value_writer(value_string);
|
||||
mvt_value &pbv = layers[i].values[v];
|
||||
|
||||
if (pbv.type == mvt_string) {
|
||||
value_writer.add_string(1, pbv.string_value);
|
||||
} else if (pbv.type == mvt_float) {
|
||||
value_writer.add_float(2, pbv.numeric_value.float_value);
|
||||
} else if (pbv.type == mvt_double) {
|
||||
value_writer.add_double(3, pbv.numeric_value.double_value);
|
||||
} else if (pbv.type == mvt_int) {
|
||||
value_writer.add_int64(4, pbv.numeric_value.int_value);
|
||||
} else if (pbv.type == mvt_uint) {
|
||||
value_writer.add_uint64(5, pbv.numeric_value.uint_value);
|
||||
} else if (pbv.type == mvt_sint) {
|
||||
value_writer.add_sint64(6, pbv.numeric_value.sint_value);
|
||||
} else if (pbv.type == mvt_bool) {
|
||||
value_writer.add_bool(7, pbv.numeric_value.bool_value);
|
||||
}
|
||||
|
||||
layer_writer.add_message(4, value_string);
|
||||
}
|
||||
|
||||
for (size_t f = 0; f < layers[i].features.size(); f++) {
|
||||
std::string feature_string;
|
||||
protozero::pbf_writer feature_writer(feature_string);
|
||||
|
||||
feature_writer.add_enum(3, layers[i].features[f].type);
|
||||
feature_writer.add_packed_uint32(2, std::begin(layers[i].features[f].tags), std::end(layers[i].features[f].tags));
|
||||
|
||||
if (layers[i].features[f].has_id) {
|
||||
feature_writer.add_uint64(1, layers[i].features[f].id);
|
||||
}
|
||||
|
||||
std::vector<uint32_t> geometry;
|
||||
|
||||
int px = 0, py = 0;
|
||||
int cmd_idx = -1;
|
||||
int cmd = -1;
|
||||
int length = 0;
|
||||
|
||||
std::vector<mvt_geometry> &geom = layers[i].features[f].geometry;
|
||||
|
||||
for (size_t g = 0; g < geom.size(); g++) {
|
||||
int op = geom[g].op;
|
||||
|
||||
if (op != cmd) {
|
||||
if (cmd_idx >= 0) {
|
||||
geometry[cmd_idx] = (length << 3) | (cmd & ((1 << 3) - 1));
|
||||
}
|
||||
|
||||
cmd = op;
|
||||
length = 0;
|
||||
cmd_idx = geometry.size();
|
||||
geometry.push_back(0);
|
||||
}
|
||||
|
||||
if (op == mvt_moveto || op == mvt_lineto) {
|
||||
long long wwx = geom[g].x;
|
||||
long long wwy = geom[g].y;
|
||||
|
||||
int dx = wwx - px;
|
||||
int dy = wwy - py;
|
||||
|
||||
geometry.push_back(protozero::encode_zigzag32(dx));
|
||||
geometry.push_back(protozero::encode_zigzag32(dy));
|
||||
|
||||
px = wwx;
|
||||
py = wwy;
|
||||
length++;
|
||||
} else if (op == mvt_closepath) {
|
||||
length++;
|
||||
} else {
|
||||
fprintf(stderr, "\nInternal error: corrupted geometry\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
if (cmd_idx >= 0) {
|
||||
geometry[cmd_idx] = (length << 3) | (cmd & ((1 << 3) - 1));
|
||||
}
|
||||
|
||||
feature_writer.add_packed_uint32(4, std::begin(geometry), std::end(geometry));
|
||||
layer_writer.add_message(2, feature_string);
|
||||
}
|
||||
|
||||
writer.add_message(3, layer_string);
|
||||
}
|
||||
|
||||
std::string compressed;
|
||||
compress(data, compressed);
|
||||
|
||||
return compressed;
|
||||
}
|
||||
|
||||
bool mvt_value::operator<(const mvt_value &o) const {
|
||||
if (type < o.type) {
|
||||
return true;
|
||||
}
|
||||
if (type == o.type) {
|
||||
if ((type == mvt_string && string_value < o.string_value) ||
|
||||
(type == mvt_float && numeric_value.float_value < o.numeric_value.float_value) ||
|
||||
(type == mvt_double && numeric_value.double_value < o.numeric_value.double_value) ||
|
||||
(type == mvt_int && numeric_value.int_value < o.numeric_value.int_value) ||
|
||||
(type == mvt_uint && numeric_value.uint_value < o.numeric_value.uint_value) ||
|
||||
(type == mvt_sint && numeric_value.sint_value < o.numeric_value.sint_value) ||
|
||||
(type == mvt_bool && numeric_value.bool_value < o.numeric_value.bool_value)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void mvt_layer::tag(mvt_feature &feature, std::string key, mvt_value value) {
|
||||
size_t ko, vo;
|
||||
|
||||
std::map<std::string, size_t>::iterator ki = key_map.find(key);
|
||||
std::map<mvt_value, size_t>::iterator vi = value_map.find(value);
|
||||
|
||||
if (ki == key_map.end()) {
|
||||
ko = keys.size();
|
||||
keys.push_back(key);
|
||||
key_map.insert(std::pair<std::string, size_t>(key, ko));
|
||||
} else {
|
||||
ko = ki->second;
|
||||
}
|
||||
|
||||
if (vi == value_map.end()) {
|
||||
vo = values.size();
|
||||
values.push_back(value);
|
||||
value_map.insert(std::pair<mvt_value, size_t>(value, vo));
|
||||
} else {
|
||||
vo = vi->second;
|
||||
}
|
||||
|
||||
feature.tags.push_back(ko);
|
||||
feature.tags.push_back(vo);
|
||||
}
|
88
mvt.hpp
Normal file
88
mvt.hpp
Normal file
@ -0,0 +1,88 @@
|
||||
struct mvt_value;
|
||||
struct mvt_layer;
|
||||
|
||||
enum mvt_operation {
|
||||
mvt_moveto = 1,
|
||||
mvt_lineto = 2,
|
||||
mvt_closepath = 7
|
||||
};
|
||||
|
||||
struct mvt_geometry {
|
||||
int x;
|
||||
int y;
|
||||
int /* mvt_operation */ op;
|
||||
|
||||
mvt_geometry(int op, long long x, long long y);
|
||||
};
|
||||
|
||||
enum mvt_geometry_type {
|
||||
mvt_point = 1,
|
||||
mvt_linestring = 2,
|
||||
mvt_polygon = 3
|
||||
};
|
||||
|
||||
struct mvt_feature {
|
||||
std::vector<unsigned> tags;
|
||||
std::vector<mvt_geometry> geometry;
|
||||
int /* mvt_geometry_type */ type;
|
||||
unsigned long long id;
|
||||
bool has_id;
|
||||
|
||||
mvt_feature() {
|
||||
has_id = false;
|
||||
id = 0;
|
||||
}
|
||||
};
|
||||
|
||||
enum mvt_value_type {
|
||||
mvt_string,
|
||||
mvt_float,
|
||||
mvt_double,
|
||||
mvt_int,
|
||||
mvt_uint,
|
||||
mvt_sint,
|
||||
mvt_bool
|
||||
};
|
||||
|
||||
struct mvt_value {
|
||||
mvt_value_type type;
|
||||
std::string string_value;
|
||||
union {
|
||||
float float_value;
|
||||
double double_value;
|
||||
long long int_value;
|
||||
unsigned long long uint_value;
|
||||
long long sint_value;
|
||||
bool bool_value;
|
||||
} numeric_value;
|
||||
|
||||
bool operator<(const mvt_value &o) const;
|
||||
};
|
||||
|
||||
struct mvt_layer {
|
||||
int version;
|
||||
std::string name;
|
||||
std::vector<mvt_feature> features;
|
||||
std::vector<std::string> keys;
|
||||
std::vector<mvt_value> values;
|
||||
int extent;
|
||||
|
||||
// Add a key-value pair to a feature, using this layer's constant pool
|
||||
void tag(mvt_feature &feature, std::string key, mvt_value value);
|
||||
|
||||
// For tracking the key-value constants already used in this layer
|
||||
std::map<std::string, size_t> key_map;
|
||||
std::map<mvt_value, size_t> value_map;
|
||||
};
|
||||
|
||||
struct mvt_tile {
|
||||
std::vector<mvt_layer> layers;
|
||||
|
||||
std::string encode();
|
||||
bool decode(std::string &message);
|
||||
};
|
||||
|
||||
bool is_compressed(std::string const &data);
|
||||
int decompress(std::string const &input, std::string &output);
|
||||
int compress(std::string const &input, std::string &output);
|
||||
int dezig(unsigned n);
|
29
options.hpp
Normal file
29
options.hpp
Normal file
@ -0,0 +1,29 @@
|
||||
#define A_COALESCE ((int) 'c')
|
||||
#define A_REVERSE ((int) 'r')
|
||||
#define A_REORDER ((int) 'o')
|
||||
#define A_LINE_DROP ((int) 'l')
|
||||
#define A_DEBUG_POLYGON ((int) 'D')
|
||||
#define A_POLYGON_DROP ((int) 'p')
|
||||
#define A_DETECT_SHARED_BORDERS ((int) 'b')
|
||||
#define A_PREFER_RADIX_SORT ((int) 'R')
|
||||
#define A_CALCULATE_FEATURE_DENSITY ((int) 'g')
|
||||
#define A_INCREASE_GAMMA_AS_NEEDED ((int) 'G')
|
||||
#define A_MERGE_POLYGONS_AS_NEEDED ((int) 'm')
|
||||
#define A_DROP_DENSEST_AS_NEEDED ((int) 's')
|
||||
#define A_DROP_FRACTION_AS_NEEDED ((int) 'd')
|
||||
#define A_DROP_SMALLEST_AS_NEEDED ((int) 'n')
|
||||
#define A_GRID_LOW_ZOOMS ((int) 'L')
|
||||
|
||||
#define P_SIMPLIFY ((int) 's')
|
||||
#define P_SIMPLIFY_LOW ((int) 'S')
|
||||
#define P_FEATURE_LIMIT ((int) 'f')
|
||||
#define P_KILOBYTE_LIMIT ((int) 'k')
|
||||
#define P_DYNAMIC_DROP ((int) 'd')
|
||||
#define P_INPUT_ORDER ((int) 'i')
|
||||
#define P_POLYGON_SPLIT ((int) 'p')
|
||||
#define P_CLIPPING ((int) 'c')
|
||||
#define P_DUPLICATION ((int) 'D')
|
||||
#define P_TINY_POLYGON_REDUCTION ((int) 't')
|
||||
|
||||
extern int prevent[256];
|
||||
extern int additional[256];
|
119
pool.c
119
pool.c
@ -1,119 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "pool.h"
|
||||
|
||||
#define POOL_WIDTH 256
|
||||
|
||||
static int hash(const char *s) {
|
||||
int h = 0;
|
||||
for (; *s; s++) {
|
||||
h = h * 37 + *s;
|
||||
}
|
||||
h = h & 0xFF;
|
||||
return h;
|
||||
}
|
||||
|
||||
struct pool_val *pool(struct pool *p, const char *s, int type) {
|
||||
int h = hash(s);
|
||||
struct pool_val **v = &(p->vals[h]);
|
||||
|
||||
while (*v != NULL) {
|
||||
int cmp = strcmp(s, (*v)->s);
|
||||
|
||||
if (cmp == 0) {
|
||||
cmp = type - (*v)->type;
|
||||
}
|
||||
|
||||
if (cmp == 0) {
|
||||
return *v;
|
||||
} else if (cmp < 0) {
|
||||
v = &((*v)->left);
|
||||
} else {
|
||||
v = &((*v)->right);
|
||||
}
|
||||
}
|
||||
|
||||
struct pool_val *nv = malloc(sizeof(struct pool_val));
|
||||
if (nv == NULL) {
|
||||
fprintf(stderr, "out of memory making string pool\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
nv->left = NULL;
|
||||
nv->right = NULL;
|
||||
nv->next = NULL;
|
||||
nv->s = s;
|
||||
nv->type = type;
|
||||
nv->n = p->n++;
|
||||
|
||||
if (p->tail != NULL) {
|
||||
p->tail->next = nv;
|
||||
}
|
||||
p->tail = nv;
|
||||
if (p->head == NULL) {
|
||||
p->head = nv;
|
||||
}
|
||||
|
||||
*v = nv;
|
||||
return *v;
|
||||
}
|
||||
|
||||
int is_pooled(struct pool *p, const char *s, int type) {
|
||||
int h = hash(s);
|
||||
struct pool_val **v = &(p->vals[h]);
|
||||
|
||||
while (*v != NULL) {
|
||||
int cmp = strcmp(s, (*v)->s);
|
||||
|
||||
if (cmp == 0) {
|
||||
cmp = type - (*v)->type;
|
||||
}
|
||||
|
||||
if (cmp == 0) {
|
||||
return 1;
|
||||
} else if (cmp < 0) {
|
||||
v = &((*v)->left);
|
||||
} else {
|
||||
v = &((*v)->right);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void pool_free1(struct pool *p, void (*func)(void *)) {
|
||||
while (p->head != NULL) {
|
||||
if (func != NULL) {
|
||||
func((void *) p->head->s);
|
||||
}
|
||||
|
||||
struct pool_val *next = p->head->next;
|
||||
free(p->head);
|
||||
p->head = next;
|
||||
}
|
||||
|
||||
p->head = NULL;
|
||||
p->tail = NULL;
|
||||
|
||||
free(p->vals);
|
||||
p->vals = NULL;
|
||||
}
|
||||
|
||||
void pool_free(struct pool *p) {
|
||||
pool_free1(p, NULL);
|
||||
}
|
||||
|
||||
void pool_free_strings(struct pool *p) {
|
||||
pool_free1(p, free);
|
||||
}
|
||||
|
||||
void pool_init(struct pool *p, int n) {
|
||||
p->n = n;
|
||||
p->vals = calloc(POOL_WIDTH, sizeof(struct pool_val *));
|
||||
if (p->vals == NULL) {
|
||||
fprintf(stderr, "out of memory creating string pool\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
p->head = NULL;
|
||||
p->tail = NULL;
|
||||
}
|
98
pool.cpp
Normal file
98
pool.cpp
Normal file
@ -0,0 +1,98 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#include "memfile.hpp"
|
||||
#include "pool.hpp"
|
||||
|
||||
static unsigned char swizzle[256] = {
|
||||
0x00, 0xBF, 0x18, 0xDE, 0x93, 0xC9, 0xB1, 0x5E, 0xDF, 0xBE, 0x72, 0x5A, 0xBB, 0x42, 0x64, 0xC6,
|
||||
0xD8, 0xB7, 0x15, 0x74, 0x1C, 0x8B, 0x91, 0xF5, 0x29, 0x46, 0xEC, 0x6F, 0xCA, 0x20, 0xF0, 0x06,
|
||||
0x27, 0x61, 0x87, 0xE0, 0x6E, 0x43, 0x50, 0xC5, 0x1B, 0xB4, 0x37, 0xC3, 0x69, 0xA6, 0xEE, 0x80,
|
||||
0xAF, 0x9B, 0xA1, 0x76, 0x23, 0x24, 0x53, 0xF3, 0x5B, 0x65, 0x19, 0xF4, 0xFC, 0xDD, 0x26, 0xE8,
|
||||
0x10, 0xF7, 0xCE, 0x92, 0x48, 0xF6, 0x94, 0x60, 0x07, 0xC4, 0xB9, 0x97, 0x6D, 0xA4, 0x11, 0x0D,
|
||||
0x1F, 0x4D, 0x13, 0xB0, 0x5D, 0xBA, 0x31, 0xD5, 0x8D, 0x51, 0x36, 0x96, 0x7A, 0x03, 0x7F, 0xDA,
|
||||
0x17, 0xDB, 0xD4, 0x83, 0xE2, 0x79, 0x6A, 0xE1, 0x95, 0x38, 0xFF, 0x28, 0xB2, 0xB3, 0xA7, 0xAE,
|
||||
0xF8, 0x54, 0xCC, 0xDC, 0x9A, 0x6B, 0xFB, 0x3F, 0xD7, 0xBC, 0x21, 0xC8, 0x71, 0x09, 0x16, 0xAC,
|
||||
0x3C, 0x8A, 0x62, 0x05, 0xC2, 0x8C, 0x32, 0x4E, 0x35, 0x9C, 0x5F, 0x75, 0xCD, 0x2E, 0xA2, 0x3E,
|
||||
0x1A, 0xC1, 0x8E, 0x14, 0xA0, 0xD3, 0x7D, 0xD9, 0xEB, 0x5C, 0x70, 0xE6, 0x9E, 0x12, 0x3B, 0xEF,
|
||||
0x1E, 0x49, 0xD2, 0x98, 0x39, 0x7E, 0x44, 0x4B, 0x6C, 0x88, 0x02, 0x2C, 0xAD, 0xE5, 0x9F, 0x40,
|
||||
0x7B, 0x4A, 0x3D, 0xA9, 0xAB, 0x0B, 0xD6, 0x2F, 0x90, 0x2A, 0xB6, 0x1D, 0xC7, 0x22, 0x55, 0x34,
|
||||
0x0A, 0xD0, 0xB5, 0x68, 0xE3, 0x59, 0xFD, 0xFA, 0x57, 0x77, 0x25, 0xA3, 0x04, 0xB8, 0x33, 0x89,
|
||||
0x78, 0x82, 0xE4, 0xC0, 0x0E, 0x8F, 0x85, 0xD1, 0x84, 0x08, 0x67, 0x47, 0x9D, 0xCB, 0x58, 0x4C,
|
||||
0xAA, 0xED, 0x52, 0xF2, 0x4F, 0xF1, 0x66, 0xCF, 0xA5, 0x56, 0xEA, 0x7C, 0xE9, 0x63, 0xE7, 0x01,
|
||||
0xF9, 0xFE, 0x0C, 0x99, 0x2D, 0x0F, 0x3A, 0x41, 0x45, 0xA8, 0x30, 0x2B, 0x73, 0xBD, 0x86, 0x81,
|
||||
};
|
||||
|
||||
int swizzlecmp(const char *a, const char *b) {
|
||||
while (*a || *b) {
|
||||
int aa = swizzle[(unsigned char) *a];
|
||||
int bb = swizzle[(unsigned char) *b];
|
||||
|
||||
int cmp = aa - bb;
|
||||
if (cmp != 0) {
|
||||
return cmp;
|
||||
}
|
||||
|
||||
a++;
|
||||
b++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
long long addpool(struct memfile *poolfile, struct memfile *treefile, const char *s, char type) {
|
||||
long long *sp = &treefile->tree;
|
||||
|
||||
while (*sp != 0) {
|
||||
int cmp = swizzlecmp(s, poolfile->map + ((struct stringpool *) (treefile->map + *sp))->off + 1);
|
||||
|
||||
if (cmp == 0) {
|
||||
cmp = type - (poolfile->map + ((struct stringpool *) (treefile->map + *sp))->off)[0];
|
||||
}
|
||||
|
||||
if (cmp < 0) {
|
||||
sp = &(((struct stringpool *) (treefile->map + *sp))->left);
|
||||
} else if (cmp > 0) {
|
||||
sp = &(((struct stringpool *) (treefile->map + *sp))->right);
|
||||
} else {
|
||||
return ((struct stringpool *) (treefile->map + *sp))->off;
|
||||
}
|
||||
}
|
||||
|
||||
// *sp is probably in the memory-mapped file, and will move if the file grows.
|
||||
long long ssp;
|
||||
if (sp == &treefile->tree) {
|
||||
ssp = -1;
|
||||
} else {
|
||||
ssp = ((char *) sp) - treefile->map;
|
||||
}
|
||||
|
||||
long long off = poolfile->off;
|
||||
if (memfile_write(poolfile, &type, 1) < 0) {
|
||||
perror("memfile write");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (memfile_write(poolfile, (void *) s, strlen(s) + 1) < 0) {
|
||||
perror("memfile write");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
struct stringpool tsp;
|
||||
tsp.left = 0;
|
||||
tsp.right = 0;
|
||||
tsp.off = off;
|
||||
|
||||
long long p = treefile->off;
|
||||
if (memfile_write(treefile, &tsp, sizeof(struct stringpool)) < 0) {
|
||||
perror("memfile write");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (ssp == -1) {
|
||||
treefile->tree = p;
|
||||
} else {
|
||||
*((long long *) (treefile->map + ssp)) = p;
|
||||
}
|
||||
return off;
|
||||
}
|
24
pool.h
24
pool.h
@ -1,24 +0,0 @@
|
||||
struct pool_val {
|
||||
const char *s;
|
||||
int type;
|
||||
int n;
|
||||
|
||||
struct pool_val *left;
|
||||
struct pool_val *right;
|
||||
|
||||
struct pool_val *next;
|
||||
};
|
||||
|
||||
struct pool {
|
||||
struct pool_val **vals;
|
||||
|
||||
struct pool_val *head;
|
||||
struct pool_val *tail;
|
||||
int n;
|
||||
};
|
||||
|
||||
struct pool_val *pool(struct pool *p, const char *s, int type);
|
||||
void pool_free(struct pool *p);
|
||||
void pool_free_strings(struct pool *p);
|
||||
void pool_init(struct pool *p, int n);
|
||||
int is_pooled(struct pool *p, const char *s, int type);
|
7
pool.hpp
Normal file
7
pool.hpp
Normal file
@ -0,0 +1,7 @@
|
||||
struct stringpool {
|
||||
long long left;
|
||||
long long right;
|
||||
long long off;
|
||||
};
|
||||
|
||||
long long addpool(struct memfile *poolfile, struct memfile *treefile, const char *s, char type);
|
63
projection.c
63
projection.c
@ -1,63 +0,0 @@
|
||||
#include <math.h>
|
||||
#include "projection.h"
|
||||
|
||||
// http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
|
||||
void latlon2tile(double lat, double lon, int zoom, long long *x, long long *y) {
|
||||
// Must limit latitude somewhere to prevent overflow.
|
||||
// 89.9 degrees latitude is 0.621 worlds beyond the edge of the flat earth,
|
||||
// hopefully far enough out that there are few expectations about the shape.
|
||||
if (lat < -89.9) {
|
||||
lat = -89.9;
|
||||
}
|
||||
if (lat > 89.9) {
|
||||
lat = 89.9;
|
||||
}
|
||||
|
||||
if (lon < -360) {
|
||||
lon = -360;
|
||||
}
|
||||
if (lon > 360) {
|
||||
lon = 360;
|
||||
}
|
||||
|
||||
double lat_rad = lat * M_PI / 180;
|
||||
unsigned long long n = 1LL << zoom;
|
||||
|
||||
long long llx = n * ((lon + 180) / 360);
|
||||
long long lly = n * (1 - (log(tan(lat_rad) + 1 / cos(lat_rad)) / M_PI)) / 2;
|
||||
|
||||
*x = llx;
|
||||
*y = lly;
|
||||
}
|
||||
|
||||
// http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
|
||||
void tile2latlon(long long x, long long y, int zoom, double *lat, double *lon) {
|
||||
unsigned long long n = 1LL << zoom;
|
||||
*lon = 360.0 * x / n - 180.0;
|
||||
*lat = atan(sinh(M_PI * (1 - 2.0 * y / n))) * 180.0 / M_PI;
|
||||
}
|
||||
|
||||
unsigned long long encode(unsigned int wx, unsigned int wy) {
|
||||
long long out = 0;
|
||||
|
||||
int i;
|
||||
for (i = 0; i < 32; i++) {
|
||||
long long v = ((wx >> (32 - (i + 1))) & 1) << 1;
|
||||
v |= (wy >> (32 - (i + 1))) & 1;
|
||||
v = v << (64 - 2 * (i + 1));
|
||||
|
||||
out |= v;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void decode(unsigned long long index, unsigned *wx, unsigned *wy) {
|
||||
*wx = *wy = 0;
|
||||
|
||||
int i;
|
||||
for (i = 0; i < 32; i++) {
|
||||
*wx |= ((index >> (64 - 2 * (i + 1) + 1)) & 1) << (32 - (i + 1));
|
||||
*wy |= ((index >> (64 - 2 * (i + 1) + 0)) & 1) << (32 - (i + 1));
|
||||
}
|
||||
}
|
112
projection.cpp
Normal file
112
projection.cpp
Normal file
@ -0,0 +1,112 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include "projection.hpp"
|
||||
|
||||
struct projection projections[] = {
|
||||
{"EPSG:4326", lonlat2tile, tile2lonlat, "urn:ogc:def:crs:OGC:1.3:CRS84"},
|
||||
{"EPSG:3857", epsg3857totile, tiletoepsg3857, "urn:ogc:def:crs:EPSG::3857"},
|
||||
{NULL, NULL},
|
||||
};
|
||||
|
||||
struct projection *projection = &projections[0];
|
||||
|
||||
// http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
|
||||
void lonlat2tile(double lon, double lat, int zoom, long long *x, long long *y) {
|
||||
// Must limit latitude somewhere to prevent overflow.
|
||||
// 89.9 degrees latitude is 0.621 worlds beyond the edge of the flat earth,
|
||||
// hopefully far enough out that there are few expectations about the shape.
|
||||
if (lat < -89.9) {
|
||||
lat = -89.9;
|
||||
}
|
||||
if (lat > 89.9) {
|
||||
lat = 89.9;
|
||||
}
|
||||
|
||||
if (lon < -360) {
|
||||
lon = -360;
|
||||
}
|
||||
if (lon > 360) {
|
||||
lon = 360;
|
||||
}
|
||||
|
||||
double lat_rad = lat * M_PI / 180;
|
||||
unsigned long long n = 1LL << zoom;
|
||||
|
||||
long long llx = n * ((lon + 180) / 360);
|
||||
long long lly = n * (1 - (log(tan(lat_rad) + 1 / cos(lat_rad)) / M_PI)) / 2;
|
||||
|
||||
*x = llx;
|
||||
*y = lly;
|
||||
}
|
||||
|
||||
// http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
|
||||
void tile2lonlat(long long x, long long y, int zoom, double *lon, double *lat) {
|
||||
unsigned long long n = 1LL << zoom;
|
||||
*lon = 360.0 * x / n - 180.0;
|
||||
*lat = atan(sinh(M_PI * (1 - 2.0 * y / n))) * 180.0 / M_PI;
|
||||
}
|
||||
|
||||
void epsg3857totile(double ix, double iy, int zoom, long long *x, long long *y) {
|
||||
*x = ix * (1LL << 31) / 6378137.0 / M_PI + (1LL << 31);
|
||||
*y = ((1LL << 32) - 1) - (iy * (1LL << 31) / 6378137.0 / M_PI + (1LL << 31));
|
||||
|
||||
if (zoom != 0) {
|
||||
*x >>= (32 - zoom);
|
||||
*y >>= (32 - zoom);
|
||||
}
|
||||
}
|
||||
|
||||
void tiletoepsg3857(long long ix, long long iy, int zoom, double *ox, double *oy) {
|
||||
if (zoom != 0) {
|
||||
ix <<= (32 - zoom);
|
||||
iy <<= (32 - zoom);
|
||||
}
|
||||
|
||||
*ox = (ix - (1LL << 31)) * M_PI * 6378137.0 / (1LL << 31);
|
||||
*oy = ((1LL << 32) - 1 - iy - (1LL << 31)) * M_PI * 6378137.0 / (1LL << 31);
|
||||
}
|
||||
|
||||
unsigned long long encode(unsigned int wx, unsigned int wy) {
|
||||
unsigned long long out = 0;
|
||||
|
||||
int i;
|
||||
for (i = 0; i < 32; i++) {
|
||||
unsigned long long v = ((wx >> (32 - (i + 1))) & 1) << 1;
|
||||
v |= (wy >> (32 - (i + 1))) & 1;
|
||||
v = v << (64 - 2 * (i + 1));
|
||||
|
||||
out |= v;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void decode(unsigned long long index, unsigned *wx, unsigned *wy) {
|
||||
*wx = *wy = 0;
|
||||
|
||||
int i;
|
||||
for (i = 0; i < 32; i++) {
|
||||
*wx |= ((index >> (64 - 2 * (i + 1) + 1)) & 1) << (32 - (i + 1));
|
||||
*wy |= ((index >> (64 - 2 * (i + 1) + 0)) & 1) << (32 - (i + 1));
|
||||
}
|
||||
}
|
||||
|
||||
void set_projection_or_exit(const char *optarg) {
|
||||
struct projection *p;
|
||||
for (p = projections; p->name != NULL; p++) {
|
||||
if (strcmp(p->name, optarg) == 0) {
|
||||
projection = p;
|
||||
break;
|
||||
}
|
||||
if (strcmp(p->alias, optarg) == 0) {
|
||||
projection = p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (p->name == NULL) {
|
||||
fprintf(stderr, "Unknown projection (-s): %s\n", optarg);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
void latlon2tile(double lat, double lon, int zoom, long long *x, long long *y);
|
||||
void tile2latlon(long long x, long long y, int zoom, double *lat, double *lon);
|
||||
unsigned long long encode(unsigned int wx, unsigned int wy);
|
||||
void decode(unsigned long long index, unsigned *wx, unsigned *wy);
|
17
projection.hpp
Normal file
17
projection.hpp
Normal file
@ -0,0 +1,17 @@
|
||||
void lonlat2tile(double lon, double lat, int zoom, long long *x, long long *y);
|
||||
void epsg3857totile(double ix, double iy, int zoom, long long *x, long long *y);
|
||||
void tile2lonlat(long long x, long long y, int zoom, double *lon, double *lat);
|
||||
void tiletoepsg3857(long long x, long long y, int zoom, double *ox, double *oy);
|
||||
unsigned long long encode(unsigned int wx, unsigned int wy);
|
||||
void decode(unsigned long long index, unsigned *wx, unsigned *wy);
|
||||
void set_projection_or_exit(const char *optarg);
|
||||
|
||||
struct projection {
|
||||
const char *name;
|
||||
void (*project)(double ix, double iy, int zoom, long long *ox, long long *oy);
|
||||
void (*unproject)(long long ix, long long iy, int zoom, double *ox, double *oy);
|
||||
const char *alias;
|
||||
};
|
||||
|
||||
extern struct projection *projection;
|
||||
extern struct projection projections[];
|
71
protozero/byteswap.hpp
Normal file
71
protozero/byteswap.hpp
Normal file
@ -0,0 +1,71 @@
|
||||
#ifndef PROTOZERO_BYTESWAP_HPP
|
||||
#define PROTOZERO_BYTESWAP_HPP
|
||||
|
||||
/*****************************************************************************
|
||||
|
||||
protozero - Minimalistic protocol buffer decoder and encoder in C++.
|
||||
|
||||
This file is from https://github.com/mapbox/protozero where you can find more
|
||||
documentation.
|
||||
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file byteswap.hpp
|
||||
*
|
||||
* @brief Contains functions to swap bytes in values (for different endianness).
|
||||
*/
|
||||
|
||||
#include <cstdint>
|
||||
#include <cassert>
|
||||
|
||||
#include <protozero/config.hpp>
|
||||
|
||||
namespace protozero {
|
||||
|
||||
/**
|
||||
* Swap N byte value between endianness formats. This template function must
|
||||
* be specialized to actually work.
|
||||
*/
|
||||
template <int N>
|
||||
inline void byteswap(const char* /*data*/, char* /*result*/) {
|
||||
static_assert(N == 1, "Can only swap 4 or 8 byte values");
|
||||
}
|
||||
|
||||
/**
|
||||
* Swap 4 byte value (int32_t, uint32_t, float) between endianness formats.
|
||||
*/
|
||||
template <>
|
||||
inline void byteswap<4>(const char* data, char* result) {
|
||||
#ifdef PROTOZERO_USE_BUILTIN_BSWAP
|
||||
*reinterpret_cast<uint32_t*>(result) = __builtin_bswap32(*reinterpret_cast<const uint32_t*>(data));
|
||||
#else
|
||||
result[3] = data[0];
|
||||
result[2] = data[1];
|
||||
result[1] = data[2];
|
||||
result[0] = data[3];
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Swap 8 byte value (int64_t, uint64_t, double) between endianness formats.
|
||||
*/
|
||||
template <>
|
||||
inline void byteswap<8>(const char* data, char* result) {
|
||||
#ifdef PROTOZERO_USE_BUILTIN_BSWAP
|
||||
*reinterpret_cast<uint64_t*>(result) = __builtin_bswap64(*reinterpret_cast<const uint64_t*>(data));
|
||||
#else
|
||||
result[7] = data[0];
|
||||
result[6] = data[1];
|
||||
result[5] = data[2];
|
||||
result[4] = data[3];
|
||||
result[3] = data[4];
|
||||
result[2] = data[5];
|
||||
result[1] = data[6];
|
||||
result[0] = data[7];
|
||||
#endif
|
||||
}
|
||||
|
||||
} // end namespace protozero
|
||||
|
||||
#endif // PROTOZERO_BYTESWAP_HPP
|
59
protozero/config.hpp
Normal file
59
protozero/config.hpp
Normal file
@ -0,0 +1,59 @@
|
||||
#ifndef PROTOZERO_CONFIG_HPP
|
||||
#define PROTOZERO_CONFIG_HPP
|
||||
|
||||
/*****************************************************************************
|
||||
|
||||
protozero - Minimalistic protocol buffer decoder and encoder in C++.
|
||||
|
||||
This file is from https://github.com/mapbox/protozero where you can find more
|
||||
documentation.
|
||||
|
||||
*****************************************************************************/
|
||||
|
||||
#include <cassert>
|
||||
|
||||
/**
|
||||
* @file config.hpp
|
||||
*
|
||||
* @brief Contains macro checks for different configurations.
|
||||
*/
|
||||
|
||||
#define PROTOZERO_LITTLE_ENDIAN 1234
|
||||
#define PROTOZERO_BIG_ENDIAN 4321
|
||||
|
||||
// Find out which byte order the machine has.
|
||||
#if defined(__BYTE_ORDER)
|
||||
# if (__BYTE_ORDER == __LITTLE_ENDIAN)
|
||||
# define PROTOZERO_BYTE_ORDER PROTOZERO_LITTLE_ENDIAN
|
||||
# endif
|
||||
# if (__BYTE_ORDER == __BIG_ENDIAN)
|
||||
# define PROTOZERO_BYTE_ORDER PROTOZERO_BIG_ENDIAN
|
||||
# endif
|
||||
#else
|
||||
// This probably isn't a very good default, but might do until we figure
|
||||
// out something better.
|
||||
# define PROTOZERO_BYTE_ORDER PROTOZERO_LITTLE_ENDIAN
|
||||
#endif
|
||||
|
||||
// On some ARM machines and depending on compiler settings access to unaligned
|
||||
// floating point values will result in a SIGBUS. Do not use the bare pointers
|
||||
// in this case.
|
||||
#if PROTOZERO_BYTE_ORDER == PROTOZERO_LITTLE_ENDIAN
|
||||
# if !defined(__arm__) && !defined(_M_ARM)
|
||||
# ifndef PROTOZERO_DO_NOT_USE_BARE_POINTER
|
||||
# define PROTOZERO_USE_BARE_POINTER_FOR_PACKED_FIXED
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
|
||||
// Check whether __builtin_bswap is available
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
# define PROTOZERO_USE_BUILTIN_BSWAP
|
||||
#endif
|
||||
|
||||
// Wrapper for assert() used for testing
|
||||
#ifndef protozero_assert
|
||||
# define protozero_assert(x) assert(x)
|
||||
#endif
|
||||
|
||||
#endif // PROTOZERO_CONFIG_HPP
|
68
protozero/exception.hpp
Normal file
68
protozero/exception.hpp
Normal file
@ -0,0 +1,68 @@
|
||||
#ifndef PROTOZERO_EXCEPTION_HPP
|
||||
#define PROTOZERO_EXCEPTION_HPP
|
||||
|
||||
/*****************************************************************************
|
||||
|
||||
protozero - Minimalistic protocol buffer decoder and encoder in C++.
|
||||
|
||||
This file is from https://github.com/mapbox/protozero where you can find more
|
||||
documentation.
|
||||
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file exception.hpp
|
||||
*
|
||||
* @brief Contains the exceptions used in the protozero library.
|
||||
*/
|
||||
|
||||
#include <exception>
|
||||
|
||||
/**
|
||||
* @brief All parts of the protozero header-only library are in this namespace.
|
||||
*/
|
||||
namespace protozero {
|
||||
|
||||
/**
|
||||
* All exceptions explicitly thrown by the functions of the protozero library
|
||||
* derive from this exception.
|
||||
*/
|
||||
struct exception : std::exception {
|
||||
/// Returns the explanatory string.
|
||||
const char *what() const noexcept override { return "pbf exception"; }
|
||||
};
|
||||
|
||||
/**
|
||||
* This exception is thrown when parsing a varint thats larger than allowed.
|
||||
* This should never happen unless the data is corrupted.
|
||||
*/
|
||||
struct varint_too_long_exception : exception {
|
||||
/// Returns the explanatory string.
|
||||
const char *what() const noexcept override { return "varint too long exception"; }
|
||||
};
|
||||
|
||||
/**
|
||||
* This exception is thrown when the wire type of a pdf field is unknown.
|
||||
* This should never happen unless the data is corrupted.
|
||||
*/
|
||||
struct unknown_pbf_wire_type_exception : exception {
|
||||
/// Returns the explanatory string.
|
||||
const char *what() const noexcept override { return "unknown pbf field type exception"; }
|
||||
};
|
||||
|
||||
/**
|
||||
* This exception is thrown when we are trying to read a field and there
|
||||
* are not enough bytes left in the buffer to read it. Almost all functions
|
||||
* of the pbf_reader class can throw this exception.
|
||||
*
|
||||
* This should never happen unless the data is corrupted or you have
|
||||
* initialized the pbf_reader object with incomplete data.
|
||||
*/
|
||||
struct end_of_buffer_exception : exception {
|
||||
/// Returns the explanatory string.
|
||||
const char *what() const noexcept override { return "end of buffer exception"; }
|
||||
};
|
||||
|
||||
} // end namespace protozero
|
||||
|
||||
#endif // PROTOZERO_EXCEPTION_HPP
|
139
protozero/pbf_builder.hpp
Normal file
139
protozero/pbf_builder.hpp
Normal file
@ -0,0 +1,139 @@
|
||||
#ifndef PROTOZERO_PBF_BUILDER_HPP
|
||||
#define PROTOZERO_PBF_BUILDER_HPP
|
||||
|
||||
/*****************************************************************************
|
||||
|
||||
protozero - Minimalistic protocol buffer decoder and encoder in C++.
|
||||
|
||||
This file is from https://github.com/mapbox/protozero where you can find more
|
||||
documentation.
|
||||
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file pbf_builder.hpp
|
||||
*
|
||||
* @brief Contains the pbf_builder template class.
|
||||
*/
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include <protozero/types.hpp>
|
||||
#include <protozero/pbf_writer.hpp>
|
||||
|
||||
namespace protozero {
|
||||
|
||||
/**
|
||||
* The pbf_builder is used to write PBF formatted messages into a buffer. It
|
||||
* is based on the pbf_writer class and has all the same methods. The
|
||||
* difference is that while the pbf_writer class takes an integer tag,
|
||||
* this template class takes a tag of the template type T. The idea is that
|
||||
* T will be an enumeration value and this helps reduce the possibility of
|
||||
* programming errors.
|
||||
*
|
||||
* Almost all methods in this class can throw an std::bad_alloc exception if
|
||||
* the std::string used as a buffer wants to resize.
|
||||
*
|
||||
* Read the tutorial to understand how this class is used.
|
||||
*/
|
||||
template <typename T>
|
||||
class pbf_builder : public pbf_writer {
|
||||
|
||||
static_assert(std::is_same<pbf_tag_type, typename std::underlying_type<T>::type>::value,
|
||||
"T must be enum with underlying type protozero::pbf_tag_type");
|
||||
|
||||
public:
|
||||
|
||||
using enum_type = T;
|
||||
|
||||
pbf_builder(std::string& data) noexcept :
|
||||
pbf_writer(data) {
|
||||
}
|
||||
|
||||
template <typename P>
|
||||
pbf_builder(pbf_writer& parent_writer, P tag) noexcept :
|
||||
pbf_writer(parent_writer, pbf_tag_type(tag)) {
|
||||
}
|
||||
|
||||
/// @cond INTERNAL
|
||||
#define PROTOZERO_WRITER_WRAP_ADD_SCALAR(name, type) \
|
||||
inline void add_##name(T tag, type value) { \
|
||||
pbf_writer::add_##name(pbf_tag_type(tag), value); \
|
||||
}
|
||||
|
||||
PROTOZERO_WRITER_WRAP_ADD_SCALAR(bool, bool)
|
||||
PROTOZERO_WRITER_WRAP_ADD_SCALAR(enum, int32_t)
|
||||
PROTOZERO_WRITER_WRAP_ADD_SCALAR(int32, int32_t)
|
||||
PROTOZERO_WRITER_WRAP_ADD_SCALAR(sint32, int32_t)
|
||||
PROTOZERO_WRITER_WRAP_ADD_SCALAR(uint32, uint32_t)
|
||||
PROTOZERO_WRITER_WRAP_ADD_SCALAR(int64, int64_t)
|
||||
PROTOZERO_WRITER_WRAP_ADD_SCALAR(sint64, int64_t)
|
||||
PROTOZERO_WRITER_WRAP_ADD_SCALAR(uint64, uint64_t)
|
||||
PROTOZERO_WRITER_WRAP_ADD_SCALAR(fixed32, uint32_t)
|
||||
PROTOZERO_WRITER_WRAP_ADD_SCALAR(sfixed32, int32_t)
|
||||
PROTOZERO_WRITER_WRAP_ADD_SCALAR(fixed64, uint64_t)
|
||||
PROTOZERO_WRITER_WRAP_ADD_SCALAR(sfixed64, int64_t)
|
||||
PROTOZERO_WRITER_WRAP_ADD_SCALAR(float, float)
|
||||
PROTOZERO_WRITER_WRAP_ADD_SCALAR(double, double)
|
||||
|
||||
#undef PROTOZERO_WRITER_WRAP_ADD_SCALAR
|
||||
/// @endcond
|
||||
|
||||
inline void add_bytes(T tag, const char* value, std::size_t size) {
|
||||
pbf_writer::add_bytes(pbf_tag_type(tag), value, size);
|
||||
}
|
||||
|
||||
inline void add_bytes(T tag, const std::string& value) {
|
||||
pbf_writer::add_bytes(pbf_tag_type(tag), value);
|
||||
}
|
||||
|
||||
inline void add_string(T tag, const char* value, std::size_t size) {
|
||||
pbf_writer::add_string(pbf_tag_type(tag), value, size);
|
||||
}
|
||||
|
||||
inline void add_string(T tag, const std::string& value) {
|
||||
pbf_writer::add_string(pbf_tag_type(tag), value);
|
||||
}
|
||||
|
||||
inline void add_string(T tag, const char* value) {
|
||||
pbf_writer::add_string(pbf_tag_type(tag), value);
|
||||
}
|
||||
|
||||
inline void add_message(T tag, const char* value, std::size_t size) {
|
||||
pbf_writer::add_message(pbf_tag_type(tag), value, size);
|
||||
}
|
||||
|
||||
inline void add_message(T tag, const std::string& value) {
|
||||
pbf_writer::add_message(pbf_tag_type(tag), value);
|
||||
}
|
||||
|
||||
/// @cond INTERNAL
|
||||
#define PROTOZERO_WRITER_WRAP_ADD_PACKED(name) \
|
||||
template <typename InputIterator> \
|
||||
inline void add_packed_##name(T tag, InputIterator first, InputIterator last) { \
|
||||
pbf_writer::add_packed_##name(pbf_tag_type(tag), first, last); \
|
||||
}
|
||||
|
||||
PROTOZERO_WRITER_WRAP_ADD_PACKED(bool)
|
||||
PROTOZERO_WRITER_WRAP_ADD_PACKED(enum)
|
||||
PROTOZERO_WRITER_WRAP_ADD_PACKED(int32)
|
||||
PROTOZERO_WRITER_WRAP_ADD_PACKED(sint32)
|
||||
PROTOZERO_WRITER_WRAP_ADD_PACKED(uint32)
|
||||
PROTOZERO_WRITER_WRAP_ADD_PACKED(int64)
|
||||
PROTOZERO_WRITER_WRAP_ADD_PACKED(sint64)
|
||||
PROTOZERO_WRITER_WRAP_ADD_PACKED(uint64)
|
||||
PROTOZERO_WRITER_WRAP_ADD_PACKED(fixed32)
|
||||
PROTOZERO_WRITER_WRAP_ADD_PACKED(sfixed32)
|
||||
PROTOZERO_WRITER_WRAP_ADD_PACKED(fixed64)
|
||||
PROTOZERO_WRITER_WRAP_ADD_PACKED(sfixed64)
|
||||
PROTOZERO_WRITER_WRAP_ADD_PACKED(float)
|
||||
PROTOZERO_WRITER_WRAP_ADD_PACKED(double)
|
||||
|
||||
#undef PROTOZERO_WRITER_WRAP_ADD_PACKED
|
||||
/// @endcond
|
||||
|
||||
};
|
||||
|
||||
} // end namespace protozero
|
||||
|
||||
#endif // PROTOZERO_PBF_BUILDER_HPP
|
94
protozero/pbf_message.hpp
Normal file
94
protozero/pbf_message.hpp
Normal file
@ -0,0 +1,94 @@
|
||||
#ifndef PROTOZERO_PBF_MESSAGE_HPP
|
||||
#define PROTOZERO_PBF_MESSAGE_HPP
|
||||
|
||||
/*****************************************************************************
|
||||
|
||||
protozero - Minimalistic protocol buffer decoder and encoder in C++.
|
||||
|
||||
This file is from https://github.com/mapbox/protozero where you can find more
|
||||
documentation.
|
||||
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file pbf_message.hpp
|
||||
*
|
||||
* @brief Contains the pbf_message class.
|
||||
*/
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include <protozero/pbf_reader.hpp>
|
||||
#include <protozero/types.hpp>
|
||||
|
||||
namespace protozero {
|
||||
|
||||
/**
|
||||
* This class represents a protobuf message. Either a top-level message or
|
||||
* a nested sub-message. Top-level messages can be created from any buffer
|
||||
* with a pointer and length:
|
||||
*
|
||||
* @code
|
||||
* enum class Message : protozero::pbf_tag_type {
|
||||
* ...
|
||||
* };
|
||||
*
|
||||
* std::string buffer;
|
||||
* // fill buffer...
|
||||
* pbf_message<Message> message(buffer.data(), buffer.size());
|
||||
* @endcode
|
||||
*
|
||||
* Sub-messages are created using get_message():
|
||||
*
|
||||
* @code
|
||||
* enum class SubMessage : protozero::pbf_tag_type {
|
||||
* ...
|
||||
* };
|
||||
*
|
||||
* pbf_message<Message> message(...);
|
||||
* message.next();
|
||||
* pbf_message<SubMessage> submessage = message.get_message();
|
||||
* @endcode
|
||||
*
|
||||
* All methods of the pbf_message class except get_bytes() and get_string()
|
||||
* provide the strong exception guarantee, ie they either succeed or do not
|
||||
* change the pbf_message object they are called on. Use the get_data() method
|
||||
* instead of get_bytes() or get_string(), if you need this guarantee.
|
||||
*
|
||||
* This template class is based on the pbf_reader class and has all the same
|
||||
* methods. The difference is that whereever the pbf_reader class takes an
|
||||
* integer tag, this template class takes a tag of the template type T.
|
||||
*
|
||||
* Read the tutorial to understand how this class is used.
|
||||
*/
|
||||
template <typename T>
|
||||
class pbf_message : public pbf_reader {
|
||||
|
||||
static_assert(std::is_same<pbf_tag_type, typename std::underlying_type<T>::type>::value, "T must be enum with underlying type protozero::pbf_tag_type");
|
||||
|
||||
public:
|
||||
|
||||
using enum_type = T;
|
||||
|
||||
template <typename... Args>
|
||||
pbf_message(Args&&... args) noexcept :
|
||||
pbf_reader(std::forward<Args>(args)...) {
|
||||
}
|
||||
|
||||
inline bool next() {
|
||||
return pbf_reader::next();
|
||||
}
|
||||
|
||||
inline bool next(T tag) {
|
||||
return pbf_reader::next(pbf_tag_type(tag));
|
||||
}
|
||||
|
||||
inline T tag() const noexcept {
|
||||
return T(pbf_reader::tag());
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
} // end namespace protozero
|
||||
|
||||
#endif // PROTOZERO_PBF_MESSAGE_HPP
|
1077
protozero/pbf_reader.hpp
Normal file
1077
protozero/pbf_reader.hpp
Normal file
File diff suppressed because it is too large
Load Diff
837
protozero/pbf_writer.hpp
Normal file
837
protozero/pbf_writer.hpp
Normal file
@ -0,0 +1,837 @@
|
||||
#ifndef PROTOZERO_PBF_WRITER_HPP
|
||||
#define PROTOZERO_PBF_WRITER_HPP
|
||||
|
||||
/*****************************************************************************
|
||||
|
||||
protozero - Minimalistic protocol buffer decoder and encoder in C++.
|
||||
|
||||
This file is from https://github.com/mapbox/protozero where you can find more
|
||||
documentation.
|
||||
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file pbf_writer.hpp
|
||||
*
|
||||
* @brief Contains the pbf_writer class.
|
||||
*/
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
|
||||
#include <protozero/config.hpp>
|
||||
#include <protozero/types.hpp>
|
||||
#include <protozero/varint.hpp>
|
||||
|
||||
#if PROTOZERO_BYTE_ORDER != PROTOZERO_LITTLE_ENDIAN
|
||||
# include <protozero/byteswap.hpp>
|
||||
#endif
|
||||
|
||||
namespace protozero {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename T> class packed_field_varint;
|
||||
template <typename T> class packed_field_svarint;
|
||||
template <typename T> class packed_field_fixed;
|
||||
|
||||
} // end namespace detail
|
||||
|
||||
/**
|
||||
* The pbf_writer is used to write PBF formatted messages into a buffer.
|
||||
*
|
||||
* Almost all methods in this class can throw an std::bad_alloc exception if
|
||||
* the std::string used as a buffer wants to resize.
|
||||
*/
|
||||
class pbf_writer {
|
||||
|
||||
// A pointer to a string buffer holding the data already written to the
|
||||
// PBF message. For default constructed writers or writers that have been
|
||||
// rolled back, this is a nullptr.
|
||||
std::string* m_data;
|
||||
|
||||
// A pointer to a parent writer object if this is a submessage. If this
|
||||
// is a top-level writer, it is a nullptr.
|
||||
pbf_writer* m_parent_writer;
|
||||
|
||||
// This is usually 0. If there is an open submessage, this is set in the
|
||||
// parent to the rollback position, ie. the last position before the
|
||||
// submessage was started. This is the position where the header of the
|
||||
// submessage starts.
|
||||
std::size_t m_rollback_pos = 0;
|
||||
|
||||
// This is usually 0. If there is an open submessage, this is set in the
|
||||
// parent to the position where the data of the submessage is written to.
|
||||
std::size_t m_pos = 0;
|
||||
|
||||
inline void add_varint(uint64_t value) {
|
||||
protozero_assert(m_pos == 0 && "you can't add fields to a parent pbf_writer if there is an existing pbf_writer for a submessage");
|
||||
protozero_assert(m_data);
|
||||
write_varint(std::back_inserter(*m_data), value);
|
||||
}
|
||||
|
||||
inline void add_field(pbf_tag_type tag, pbf_wire_type type) {
|
||||
protozero_assert(((tag > 0 && tag < 19000) || (tag > 19999 && tag <= ((1 << 29) - 1))) && "tag out of range");
|
||||
uint32_t b = (tag << 3) | uint32_t(type);
|
||||
add_varint(b);
|
||||
}
|
||||
|
||||
inline void add_tagged_varint(pbf_tag_type tag, uint64_t value) {
|
||||
add_field(tag, pbf_wire_type::varint);
|
||||
add_varint(value);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void add_fixed(T value) {
|
||||
protozero_assert(m_pos == 0 && "you can't add fields to a parent pbf_writer if there is an existing pbf_writer for a submessage");
|
||||
protozero_assert(m_data);
|
||||
#if PROTOZERO_BYTE_ORDER == PROTOZERO_LITTLE_ENDIAN
|
||||
m_data->append(reinterpret_cast<const char*>(&value), sizeof(T));
|
||||
#else
|
||||
auto size = m_data->size();
|
||||
m_data->resize(size + sizeof(T));
|
||||
byteswap<sizeof(T)>(reinterpret_cast<const char*>(&value), const_cast<char*>(m_data->data() + size));
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename T, typename It>
|
||||
inline void add_packed_fixed(pbf_tag_type tag, It first, It last, std::input_iterator_tag) {
|
||||
if (first == last) {
|
||||
return;
|
||||
}
|
||||
|
||||
pbf_writer sw(*this, tag);
|
||||
|
||||
while (first != last) {
|
||||
sw.add_fixed<T>(*first++);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename It>
|
||||
inline void add_packed_fixed(pbf_tag_type tag, It first, It last, std::forward_iterator_tag) {
|
||||
if (first == last) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto length = std::distance(first, last);
|
||||
add_length_varint(tag, sizeof(T) * pbf_length_type(length));
|
||||
reserve(sizeof(T) * std::size_t(length));
|
||||
|
||||
while (first != last) {
|
||||
add_fixed<T>(*first++);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename It>
|
||||
inline void add_packed_varint(pbf_tag_type tag, It first, It last) {
|
||||
if (first == last) {
|
||||
return;
|
||||
}
|
||||
|
||||
pbf_writer sw(*this, tag);
|
||||
|
||||
while (first != last) {
|
||||
sw.add_varint(uint64_t(*first++));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename It>
|
||||
inline void add_packed_svarint(pbf_tag_type tag, It first, It last) {
|
||||
if (first == last) {
|
||||
return;
|
||||
}
|
||||
|
||||
pbf_writer sw(*this, tag);
|
||||
|
||||
while (first != last) {
|
||||
sw.add_varint(encode_zigzag64(*first++));
|
||||
}
|
||||
}
|
||||
|
||||
// The number of bytes to reserve for the varint holding the length of
|
||||
// a length-delimited field. The length has to fit into pbf_length_type,
|
||||
// and a varint needs 8 bit for every 7 bit.
|
||||
static const int reserve_bytes = sizeof(pbf_length_type) * 8 / 7 + 1;
|
||||
|
||||
// If m_rollpack_pos is set to this special value, it means that when
|
||||
// the submessage is closed, nothing needs to be done, because the length
|
||||
// of the submessage has already been written correctly.
|
||||
static const std::size_t size_is_known = std::numeric_limits<std::size_t>::max();
|
||||
|
||||
inline void open_submessage(pbf_tag_type tag, std::size_t size) {
|
||||
protozero_assert(m_pos == 0);
|
||||
protozero_assert(m_data);
|
||||
if (size == 0) {
|
||||
m_rollback_pos = m_data->size();
|
||||
add_field(tag, pbf_wire_type::length_delimited);
|
||||
m_data->append(std::size_t(reserve_bytes), '\0');
|
||||
} else {
|
||||
m_rollback_pos = size_is_known;
|
||||
add_length_varint(tag, pbf_length_type(size));
|
||||
reserve(size);
|
||||
}
|
||||
m_pos = m_data->size();
|
||||
}
|
||||
|
||||
inline void rollback_submessage() {
|
||||
protozero_assert(m_pos != 0);
|
||||
protozero_assert(m_rollback_pos != size_is_known);
|
||||
protozero_assert(m_data);
|
||||
m_data->resize(m_rollback_pos);
|
||||
m_pos = 0;
|
||||
}
|
||||
|
||||
inline void commit_submessage() {
|
||||
protozero_assert(m_pos != 0);
|
||||
protozero_assert(m_rollback_pos != size_is_known);
|
||||
protozero_assert(m_data);
|
||||
auto length = pbf_length_type(m_data->size() - m_pos);
|
||||
|
||||
protozero_assert(m_data->size() >= m_pos - reserve_bytes);
|
||||
auto n = write_varint(m_data->begin() + long(m_pos) - reserve_bytes, length);
|
||||
|
||||
m_data->erase(m_data->begin() + long(m_pos) - reserve_bytes + n, m_data->begin() + long(m_pos));
|
||||
m_pos = 0;
|
||||
}
|
||||
|
||||
inline void close_submessage() {
|
||||
protozero_assert(m_data);
|
||||
if (m_pos == 0 || m_rollback_pos == size_is_known) {
|
||||
return;
|
||||
}
|
||||
if (m_data->size() - m_pos == 0) {
|
||||
rollback_submessage();
|
||||
} else {
|
||||
commit_submessage();
|
||||
}
|
||||
}
|
||||
|
||||
inline void add_length_varint(pbf_tag_type tag, pbf_length_type length) {
|
||||
add_field(tag, pbf_wire_type::length_delimited);
|
||||
add_varint(length);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Create a writer using the given string as a data store. The pbf_writer
|
||||
* stores a reference to that string and adds all data to it. The string
|
||||
* doesn't have to be empty. The pbf_writer will just append data.
|
||||
*/
|
||||
inline explicit pbf_writer(std::string& data) noexcept :
|
||||
m_data(&data),
|
||||
m_parent_writer(nullptr),
|
||||
m_pos(0) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a writer without a data store. In this form the writer can not
|
||||
* be used!
|
||||
*/
|
||||
inline pbf_writer() noexcept :
|
||||
m_data(nullptr),
|
||||
m_parent_writer(nullptr),
|
||||
m_pos(0) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a pbf_writer for a submessage from the pbf_writer of the
|
||||
* parent message.
|
||||
*
|
||||
* @param parent_writer The pbf_writer
|
||||
* @param tag Tag (field number) of the field that will be written
|
||||
* @param size Optional size of the submessage in bytes (use 0 for unknown).
|
||||
* Setting this allows some optimizations but is only possible in
|
||||
* a few very specific cases.
|
||||
*/
|
||||
inline pbf_writer(pbf_writer& parent_writer, pbf_tag_type tag, std::size_t size=0) :
|
||||
m_data(parent_writer.m_data),
|
||||
m_parent_writer(&parent_writer),
|
||||
m_pos(0) {
|
||||
m_parent_writer->open_submessage(tag, size);
|
||||
}
|
||||
|
||||
/// A pbf_writer object can be copied
|
||||
pbf_writer(const pbf_writer&) noexcept = default;
|
||||
|
||||
/// A pbf_writer object can be copied
|
||||
pbf_writer& operator=(const pbf_writer&) noexcept = default;
|
||||
|
||||
/// A pbf_writer object can be moved
|
||||
inline pbf_writer(pbf_writer&&) noexcept = default;
|
||||
|
||||
/// A pbf_writer object can be moved
|
||||
inline pbf_writer& operator=(pbf_writer&&) noexcept = default;
|
||||
|
||||
inline ~pbf_writer() {
|
||||
if (m_parent_writer) {
|
||||
m_parent_writer->close_submessage();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reserve size bytes in the underlying message store in addition to
|
||||
* whatever the message store already holds. So unlike
|
||||
* the `std::string::reserve()` method this is not an absolute size,
|
||||
* but additional memory that should be reserved.
|
||||
*
|
||||
* @param size Number of bytes to reserve in underlying message store.
|
||||
*/
|
||||
void reserve(std::size_t size) {
|
||||
protozero_assert(m_data);
|
||||
m_data->reserve(m_data->size() + size);
|
||||
}
|
||||
|
||||
inline void rollback() {
|
||||
protozero_assert(m_parent_writer && "you can't call rollback() on a pbf_writer without a parent");
|
||||
protozero_assert(m_pos == 0 && "you can't call rollback() on a pbf_writer that has an open nested submessage");
|
||||
m_parent_writer->rollback_submessage();
|
||||
m_data = nullptr;
|
||||
}
|
||||
|
||||
///@{
|
||||
/**
|
||||
* @name Scalar field writer functions
|
||||
*/
|
||||
|
||||
/**
|
||||
* Add "bool" field to data.
|
||||
*
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param value Value to be written
|
||||
*/
|
||||
inline void add_bool(pbf_tag_type tag, bool value) {
|
||||
add_field(tag, pbf_wire_type::varint);
|
||||
protozero_assert(m_pos == 0 && "you can't add fields to a parent pbf_writer if there is an existing pbf_writer for a submessage");
|
||||
protozero_assert(m_data);
|
||||
m_data->append(1, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "enum" field to data.
|
||||
*
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param value Value to be written
|
||||
*/
|
||||
inline void add_enum(pbf_tag_type tag, int32_t value) {
|
||||
add_tagged_varint(tag, uint64_t(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "int32" field to data.
|
||||
*
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param value Value to be written
|
||||
*/
|
||||
inline void add_int32(pbf_tag_type tag, int32_t value) {
|
||||
add_tagged_varint(tag, uint64_t(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "sint32" field to data.
|
||||
*
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param value Value to be written
|
||||
*/
|
||||
inline void add_sint32(pbf_tag_type tag, int32_t value) {
|
||||
add_tagged_varint(tag, encode_zigzag32(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "uint32" field to data.
|
||||
*
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param value Value to be written
|
||||
*/
|
||||
inline void add_uint32(pbf_tag_type tag, uint32_t value) {
|
||||
add_tagged_varint(tag, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "int64" field to data.
|
||||
*
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param value Value to be written
|
||||
*/
|
||||
inline void add_int64(pbf_tag_type tag, int64_t value) {
|
||||
add_tagged_varint(tag, uint64_t(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "sint64" field to data.
|
||||
*
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param value Value to be written
|
||||
*/
|
||||
inline void add_sint64(pbf_tag_type tag, int64_t value) {
|
||||
add_tagged_varint(tag, encode_zigzag64(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "uint64" field to data.
|
||||
*
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param value Value to be written
|
||||
*/
|
||||
inline void add_uint64(pbf_tag_type tag, uint64_t value) {
|
||||
add_tagged_varint(tag, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "fixed32" field to data.
|
||||
*
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param value Value to be written
|
||||
*/
|
||||
inline void add_fixed32(pbf_tag_type tag, uint32_t value) {
|
||||
add_field(tag, pbf_wire_type::fixed32);
|
||||
add_fixed<uint32_t>(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "sfixed32" field to data.
|
||||
*
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param value Value to be written
|
||||
*/
|
||||
inline void add_sfixed32(pbf_tag_type tag, int32_t value) {
|
||||
add_field(tag, pbf_wire_type::fixed32);
|
||||
add_fixed<int32_t>(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "fixed64" field to data.
|
||||
*
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param value Value to be written
|
||||
*/
|
||||
inline void add_fixed64(pbf_tag_type tag, uint64_t value) {
|
||||
add_field(tag, pbf_wire_type::fixed64);
|
||||
add_fixed<uint64_t>(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "sfixed64" field to data.
|
||||
*
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param value Value to be written
|
||||
*/
|
||||
inline void add_sfixed64(pbf_tag_type tag, int64_t value) {
|
||||
add_field(tag, pbf_wire_type::fixed64);
|
||||
add_fixed<int64_t>(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "float" field to data.
|
||||
*
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param value Value to be written
|
||||
*/
|
||||
inline void add_float(pbf_tag_type tag, float value) {
|
||||
add_field(tag, pbf_wire_type::fixed32);
|
||||
add_fixed<float>(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "double" field to data.
|
||||
*
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param value Value to be written
|
||||
*/
|
||||
inline void add_double(pbf_tag_type tag, double value) {
|
||||
add_field(tag, pbf_wire_type::fixed64);
|
||||
add_fixed<double>(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "bytes" field to data.
|
||||
*
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param value Pointer to value to be written
|
||||
* @param size Number of bytes to be written
|
||||
*/
|
||||
inline void add_bytes(pbf_tag_type tag, const char* value, std::size_t size) {
|
||||
protozero_assert(m_pos == 0 && "you can't add fields to a parent pbf_writer if there is an existing pbf_writer for a submessage");
|
||||
protozero_assert(m_data);
|
||||
protozero_assert(size <= std::numeric_limits<pbf_length_type>::max());
|
||||
add_length_varint(tag, pbf_length_type(size));
|
||||
m_data->append(value, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "bytes" field to data.
|
||||
*
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param value Value to be written
|
||||
*/
|
||||
inline void add_bytes(pbf_tag_type tag, const std::string& value) {
|
||||
add_bytes(tag, value.data(), value.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "string" field to data.
|
||||
*
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param value Pointer to value to be written
|
||||
* @param size Number of bytes to be written
|
||||
*/
|
||||
inline void add_string(pbf_tag_type tag, const char* value, std::size_t size) {
|
||||
add_bytes(tag, value, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "string" field to data.
|
||||
*
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param value Value to be written
|
||||
*/
|
||||
inline void add_string(pbf_tag_type tag, const std::string& value) {
|
||||
add_bytes(tag, value.data(), value.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "string" field to data. Bytes from the value are written until
|
||||
* a null byte is encountered. The null byte is not added.
|
||||
*
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param value Pointer to value to be written
|
||||
*/
|
||||
inline void add_string(pbf_tag_type tag, const char* value) {
|
||||
add_bytes(tag, value, std::strlen(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "message" field to data.
|
||||
*
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param value Pointer to message to be written
|
||||
* @param size Length of the message
|
||||
*/
|
||||
inline void add_message(pbf_tag_type tag, const char* value, std::size_t size) {
|
||||
add_bytes(tag, value, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "message" field to data.
|
||||
*
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param value Value to be written. The value must be a complete message.
|
||||
*/
|
||||
inline void add_message(pbf_tag_type tag, const std::string& value) {
|
||||
add_bytes(tag, value.data(), value.size());
|
||||
}
|
||||
|
||||
///@}
|
||||
|
||||
///@{
|
||||
/**
|
||||
* @name Repeated packed field writer functions
|
||||
*/
|
||||
|
||||
/**
|
||||
* Add "repeated packed bool" field to data.
|
||||
*
|
||||
* @tparam InputIterator An type satisfying the InputIterator concept.
|
||||
* Dereferencing the iterator must yield a type assignable to bool.
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param first Iterator pointing to the beginning of the data
|
||||
* @param last Iterator pointing one past the end of data
|
||||
*/
|
||||
template <typename InputIterator>
|
||||
inline void add_packed_bool(pbf_tag_type tag, InputIterator first, InputIterator last) {
|
||||
add_packed_varint(tag, first, last);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "repeated packed enum" field to data.
|
||||
*
|
||||
* @tparam InputIterator An type satisfying the InputIterator concept.
|
||||
* Dereferencing the iterator must yield a type assignable to int32_t.
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param first Iterator pointing to the beginning of the data
|
||||
* @param last Iterator pointing one past the end of data
|
||||
*/
|
||||
template <typename InputIterator>
|
||||
inline void add_packed_enum(pbf_tag_type tag, InputIterator first, InputIterator last) {
|
||||
add_packed_varint(tag, first, last);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "repeated packed int32" field to data.
|
||||
*
|
||||
* @tparam InputIterator An type satisfying the InputIterator concept.
|
||||
* Dereferencing the iterator must yield a type assignable to int32_t.
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param first Iterator pointing to the beginning of the data
|
||||
* @param last Iterator pointing one past the end of data
|
||||
*/
|
||||
template <typename InputIterator>
|
||||
inline void add_packed_int32(pbf_tag_type tag, InputIterator first, InputIterator last) {
|
||||
add_packed_varint(tag, first, last);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "repeated packed sint32" field to data.
|
||||
*
|
||||
* @tparam InputIterator An type satisfying the InputIterator concept.
|
||||
* Dereferencing the iterator must yield a type assignable to int32_t.
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param first Iterator pointing to the beginning of the data
|
||||
* @param last Iterator pointing one past the end of data
|
||||
*/
|
||||
template <typename InputIterator>
|
||||
inline void add_packed_sint32(pbf_tag_type tag, InputIterator first, InputIterator last) {
|
||||
add_packed_svarint(tag, first, last);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "repeated packed uint32" field to data.
|
||||
*
|
||||
* @tparam InputIterator An type satisfying the InputIterator concept.
|
||||
* Dereferencing the iterator must yield a type assignable to uint32_t.
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param first Iterator pointing to the beginning of the data
|
||||
* @param last Iterator pointing one past the end of data
|
||||
*/
|
||||
template <typename InputIterator>
|
||||
inline void add_packed_uint32(pbf_tag_type tag, InputIterator first, InputIterator last) {
|
||||
add_packed_varint(tag, first, last);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "repeated packed int64" field to data.
|
||||
*
|
||||
* @tparam InputIterator An type satisfying the InputIterator concept.
|
||||
* Dereferencing the iterator must yield a type assignable to int64_t.
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param first Iterator pointing to the beginning of the data
|
||||
* @param last Iterator pointing one past the end of data
|
||||
*/
|
||||
template <typename InputIterator>
|
||||
inline void add_packed_int64(pbf_tag_type tag, InputIterator first, InputIterator last) {
|
||||
add_packed_varint(tag, first, last);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "repeated packed sint64" field to data.
|
||||
*
|
||||
* @tparam InputIterator An type satisfying the InputIterator concept.
|
||||
* Dereferencing the iterator must yield a type assignable to int64_t.
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param first Iterator pointing to the beginning of the data
|
||||
* @param last Iterator pointing one past the end of data
|
||||
*/
|
||||
template <typename InputIterator>
|
||||
inline void add_packed_sint64(pbf_tag_type tag, InputIterator first, InputIterator last) {
|
||||
add_packed_svarint(tag, first, last);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "repeated packed uint64" field to data.
|
||||
*
|
||||
* @tparam InputIterator An type satisfying the InputIterator concept.
|
||||
* Dereferencing the iterator must yield a type assignable to uint64_t.
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param first Iterator pointing to the beginning of the data
|
||||
* @param last Iterator pointing one past the end of data
|
||||
*/
|
||||
template <typename InputIterator>
|
||||
inline void add_packed_uint64(pbf_tag_type tag, InputIterator first, InputIterator last) {
|
||||
add_packed_varint(tag, first, last);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "repeated packed fixed32" field to data.
|
||||
*
|
||||
* @tparam InputIterator An type satisfying the InputIterator concept.
|
||||
* Dereferencing the iterator must yield a type assignable to uint32_t.
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param first Iterator pointing to the beginning of the data
|
||||
* @param last Iterator pointing one past the end of data
|
||||
*/
|
||||
template <typename InputIterator>
|
||||
inline void add_packed_fixed32(pbf_tag_type tag, InputIterator first, InputIterator last) {
|
||||
add_packed_fixed<uint32_t, InputIterator>(tag, first, last,
|
||||
typename std::iterator_traits<InputIterator>::iterator_category());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "repeated packed sfixed32" field to data.
|
||||
*
|
||||
* @tparam InputIterator An type satisfying the InputIterator concept.
|
||||
* Dereferencing the iterator must yield a type assignable to int32_t.
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param first Iterator pointing to the beginning of the data
|
||||
* @param last Iterator pointing one past the end of data
|
||||
*/
|
||||
template <typename InputIterator>
|
||||
inline void add_packed_sfixed32(pbf_tag_type tag, InputIterator first, InputIterator last) {
|
||||
add_packed_fixed<int32_t, InputIterator>(tag, first, last,
|
||||
typename std::iterator_traits<InputIterator>::iterator_category());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "repeated packed fixed64" field to data.
|
||||
*
|
||||
* @tparam InputIterator An type satisfying the InputIterator concept.
|
||||
* Dereferencing the iterator must yield a type assignable to uint64_t.
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param first Iterator pointing to the beginning of the data
|
||||
* @param last Iterator pointing one past the end of data
|
||||
*/
|
||||
template <typename InputIterator>
|
||||
inline void add_packed_fixed64(pbf_tag_type tag, InputIterator first, InputIterator last) {
|
||||
add_packed_fixed<uint64_t, InputIterator>(tag, first, last,
|
||||
typename std::iterator_traits<InputIterator>::iterator_category());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "repeated packed sfixed64" field to data.
|
||||
*
|
||||
* @tparam InputIterator An type satisfying the InputIterator concept.
|
||||
* Dereferencing the iterator must yield a type assignable to int64_t.
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param first Iterator pointing to the beginning of the data
|
||||
* @param last Iterator pointing one past the end of data
|
||||
*/
|
||||
template <typename InputIterator>
|
||||
inline void add_packed_sfixed64(pbf_tag_type tag, InputIterator first, InputIterator last) {
|
||||
add_packed_fixed<int64_t, InputIterator>(tag, first, last,
|
||||
typename std::iterator_traits<InputIterator>::iterator_category());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "repeated packed float" field to data.
|
||||
*
|
||||
* @tparam InputIterator An type satisfying the InputIterator concept.
|
||||
* Dereferencing the iterator must yield a type assignable to float.
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param first Iterator pointing to the beginning of the data
|
||||
* @param last Iterator pointing one past the end of data
|
||||
*/
|
||||
template <typename InputIterator>
|
||||
inline void add_packed_float(pbf_tag_type tag, InputIterator first, InputIterator last) {
|
||||
add_packed_fixed<float, InputIterator>(tag, first, last,
|
||||
typename std::iterator_traits<InputIterator>::iterator_category());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add "repeated packed double" field to data.
|
||||
*
|
||||
* @tparam InputIterator An type satisfying the InputIterator concept.
|
||||
* Dereferencing the iterator must yield a type assignable to double.
|
||||
* @param tag Tag (field number) of the field
|
||||
* @param first Iterator pointing to the beginning of the data
|
||||
* @param last Iterator pointing one past the end of data
|
||||
*/
|
||||
template <typename InputIterator>
|
||||
inline void add_packed_double(pbf_tag_type tag, InputIterator first, InputIterator last) {
|
||||
add_packed_fixed<double, InputIterator>(tag, first, last,
|
||||
typename std::iterator_traits<InputIterator>::iterator_category());
|
||||
}
|
||||
|
||||
///@}
|
||||
|
||||
template <typename T> friend class detail::packed_field_varint;
|
||||
template <typename T> friend class detail::packed_field_svarint;
|
||||
template <typename T> friend class detail::packed_field_fixed;
|
||||
|
||||
}; // class pbf_writer
|
||||
|
||||
namespace detail {
|
||||
|
||||
class packed_field {
|
||||
|
||||
protected:
|
||||
|
||||
pbf_writer m_writer;
|
||||
|
||||
public:
|
||||
|
||||
packed_field(pbf_writer& parent_writer, pbf_tag_type tag) :
|
||||
m_writer(parent_writer, tag) {
|
||||
}
|
||||
|
||||
packed_field(pbf_writer& parent_writer, pbf_tag_type tag, std::size_t size) :
|
||||
m_writer(parent_writer, tag, size) {
|
||||
}
|
||||
|
||||
void rollback() {
|
||||
m_writer.rollback();
|
||||
}
|
||||
|
||||
}; // class packed_field
|
||||
|
||||
template <typename T>
|
||||
class packed_field_fixed : public packed_field {
|
||||
|
||||
public:
|
||||
|
||||
packed_field_fixed(pbf_writer& parent_writer, pbf_tag_type tag) :
|
||||
packed_field(parent_writer, tag) {
|
||||
}
|
||||
|
||||
packed_field_fixed(pbf_writer& parent_writer, pbf_tag_type tag, std::size_t size) :
|
||||
packed_field(parent_writer, tag, size * sizeof(T)) {
|
||||
}
|
||||
|
||||
void add_element(T value) {
|
||||
m_writer.add_fixed<T>(value);
|
||||
}
|
||||
|
||||
}; // class packed_field_fixed
|
||||
|
||||
template <typename T>
|
||||
class packed_field_varint : public packed_field {
|
||||
|
||||
public:
|
||||
|
||||
packed_field_varint(pbf_writer& parent_writer, pbf_tag_type tag) :
|
||||
packed_field(parent_writer, tag) {
|
||||
}
|
||||
|
||||
void add_element(T value) {
|
||||
m_writer.add_varint(uint64_t(value));
|
||||
}
|
||||
|
||||
}; // class packed_field_varint
|
||||
|
||||
template <typename T>
|
||||
class packed_field_svarint : public packed_field {
|
||||
|
||||
public:
|
||||
|
||||
packed_field_svarint(pbf_writer& parent_writer, pbf_tag_type tag) :
|
||||
packed_field(parent_writer, tag) {
|
||||
}
|
||||
|
||||
void add_element(T value) {
|
||||
m_writer.add_varint(encode_zigzag64(value));
|
||||
}
|
||||
|
||||
}; // class packed_field_svarint
|
||||
|
||||
} // end namespace detail
|
||||
|
||||
using packed_field_bool = detail::packed_field_varint<bool>;
|
||||
using packed_field_enum = detail::packed_field_varint<int32_t>;
|
||||
using packed_field_int32 = detail::packed_field_varint<int32_t>;
|
||||
using packed_field_sint32 = detail::packed_field_svarint<int32_t>;
|
||||
using packed_field_uint32 = detail::packed_field_varint<uint32_t>;
|
||||
using packed_field_int64 = detail::packed_field_varint<int64_t>;
|
||||
using packed_field_sint64 = detail::packed_field_svarint<int64_t>;
|
||||
using packed_field_uint64 = detail::packed_field_varint<uint64_t>;
|
||||
using packed_field_fixed32 = detail::packed_field_fixed<uint32_t>;
|
||||
using packed_field_sfixed32 = detail::packed_field_fixed<int32_t>;
|
||||
using packed_field_fixed64 = detail::packed_field_fixed<uint64_t>;
|
||||
using packed_field_sfixed64 = detail::packed_field_fixed<int64_t>;
|
||||
using packed_field_float = detail::packed_field_fixed<float>;
|
||||
using packed_field_double = detail::packed_field_fixed<double>;
|
||||
|
||||
} // end namespace protozero
|
||||
|
||||
#endif // PROTOZERO_PBF_WRITER_HPP
|
49
protozero/types.hpp
Normal file
49
protozero/types.hpp
Normal file
@ -0,0 +1,49 @@
|
||||
#ifndef PROTOZERO_TYPES_HPP
|
||||
#define PROTOZERO_TYPES_HPP
|
||||
|
||||
/*****************************************************************************
|
||||
|
||||
protozero - Minimalistic protocol buffer decoder and encoder in C++.
|
||||
|
||||
This file is from https://github.com/mapbox/protozero where you can find more
|
||||
documentation.
|
||||
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file types.hpp
|
||||
*
|
||||
* @brief Contains the declaration of low-level types used in the pbf format.
|
||||
*/
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace protozero {
|
||||
|
||||
/**
|
||||
* The type used for field tags (field numbers).
|
||||
*/
|
||||
typedef uint32_t pbf_tag_type;
|
||||
|
||||
/**
|
||||
* The type used to encode type information.
|
||||
* See the table on
|
||||
* https://developers.google.com/protocol-buffers/docs/encoding
|
||||
*/
|
||||
enum class pbf_wire_type : uint32_t {
|
||||
varint = 0, // int32/64, uint32/64, sint32/64, bool, enum
|
||||
fixed64 = 1, // fixed64, sfixed64, double
|
||||
length_delimited = 2, // string, bytes, embedded messages,
|
||||
// packed repeated fields
|
||||
fixed32 = 5, // fixed32, sfixed32, float
|
||||
unknown = 99 // used for default setting in this library
|
||||
};
|
||||
|
||||
/**
|
||||
* The type used for length values, such as the length of a field.
|
||||
*/
|
||||
typedef uint32_t pbf_length_type;
|
||||
|
||||
} // end namespace protozero
|
||||
|
||||
#endif // PROTOZERO_TYPES_HPP
|
132
protozero/varint.hpp
Normal file
132
protozero/varint.hpp
Normal file
@ -0,0 +1,132 @@
|
||||
#ifndef PROTOZERO_VARINT_HPP
|
||||
#define PROTOZERO_VARINT_HPP
|
||||
|
||||
/*****************************************************************************
|
||||
|
||||
protozero - Minimalistic protocol buffer decoder and encoder in C++.
|
||||
|
||||
This file is from https://github.com/mapbox/protozero where you can find more
|
||||
documentation.
|
||||
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file varint.hpp
|
||||
*
|
||||
* @brief Contains low-level varint and zigzag encoding and decoding functions.
|
||||
*/
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <protozero/exception.hpp>
|
||||
|
||||
namespace protozero {
|
||||
|
||||
/**
|
||||
* The maximum length of a 64bit varint.
|
||||
*/
|
||||
constexpr const int8_t max_varint_length = sizeof(uint64_t) * 8 / 7 + 1;
|
||||
|
||||
// from https://github.com/facebook/folly/blob/master/folly/Varint.h
|
||||
/**
|
||||
* Decode a 64bit varint.
|
||||
*
|
||||
* Strong exception guarantee: if there is an exception the data pointer will
|
||||
* not be changed.
|
||||
*
|
||||
* @param[in,out] data Pointer to pointer to the input data. After the function
|
||||
* returns this will point to the next data to be read.
|
||||
* @param[in] end Pointer one past the end of the input data.
|
||||
* @returns The decoded integer
|
||||
* @throws varint_too_long_exception if the varint is longer then the maximum
|
||||
* length that would fit in a 64bit int. Usually this means your data
|
||||
* is corrupted or you are trying to read something as a varint that
|
||||
* isn't.
|
||||
* @throws end_of_buffer_exception if the *end* of the buffer was reached
|
||||
* before the end of the varint.
|
||||
*/
|
||||
inline uint64_t decode_varint(const char** data, const char* end) {
|
||||
const int8_t* begin = reinterpret_cast<const int8_t*>(*data);
|
||||
const int8_t* iend = reinterpret_cast<const int8_t*>(end);
|
||||
const int8_t* p = begin;
|
||||
uint64_t val = 0;
|
||||
|
||||
if (iend - begin >= max_varint_length) { // fast path
|
||||
do {
|
||||
int64_t b;
|
||||
b = *p++; val = uint64_t((b & 0x7f) ); if (b >= 0) break;
|
||||
b = *p++; val |= uint64_t((b & 0x7f) << 7); if (b >= 0) break;
|
||||
b = *p++; val |= uint64_t((b & 0x7f) << 14); if (b >= 0) break;
|
||||
b = *p++; val |= uint64_t((b & 0x7f) << 21); if (b >= 0) break;
|
||||
b = *p++; val |= uint64_t((b & 0x7f) << 28); if (b >= 0) break;
|
||||
b = *p++; val |= uint64_t((b & 0x7f) << 35); if (b >= 0) break;
|
||||
b = *p++; val |= uint64_t((b & 0x7f) << 42); if (b >= 0) break;
|
||||
b = *p++; val |= uint64_t((b & 0x7f) << 49); if (b >= 0) break;
|
||||
b = *p++; val |= uint64_t((b & 0x7f) << 56); if (b >= 0) break;
|
||||
b = *p++; val |= uint64_t((b & 0x7f) << 63); if (b >= 0) break;
|
||||
throw varint_too_long_exception();
|
||||
} while (false);
|
||||
} else {
|
||||
int shift = 0;
|
||||
while (p != iend && *p < 0) {
|
||||
val |= uint64_t(*p++ & 0x7f) << shift;
|
||||
shift += 7;
|
||||
}
|
||||
if (p == iend) {
|
||||
throw end_of_buffer_exception();
|
||||
}
|
||||
val |= uint64_t(*p++) << shift;
|
||||
}
|
||||
|
||||
*data = reinterpret_cast<const char*>(p);
|
||||
return val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Varint-encode a 64bit integer.
|
||||
*/
|
||||
template <typename OutputIterator>
|
||||
inline int write_varint(OutputIterator data, uint64_t value) {
|
||||
int n=1;
|
||||
|
||||
while (value >= 0x80) {
|
||||
*data++ = char((value & 0x7f) | 0x80);
|
||||
value >>= 7;
|
||||
++n;
|
||||
}
|
||||
*data++ = char(value);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
/**
|
||||
* ZigZag encodes a 32 bit integer.
|
||||
*/
|
||||
inline uint32_t encode_zigzag32(int32_t value) noexcept {
|
||||
return (static_cast<uint32_t>(value) << 1) ^ (static_cast<uint32_t>(value >> 31));
|
||||
}
|
||||
|
||||
/**
|
||||
* ZigZag encodes a 64 bit integer.
|
||||
*/
|
||||
inline uint64_t encode_zigzag64(int64_t value) noexcept {
|
||||
return (static_cast<uint64_t>(value) << 1) ^ (static_cast<uint64_t>(value >> 63));
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes a 32 bit ZigZag-encoded integer.
|
||||
*/
|
||||
inline int32_t decode_zigzag32(uint32_t value) noexcept {
|
||||
return int32_t(value >> 1) ^ -int32_t(value & 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes a 64 bit ZigZag-encoded integer.
|
||||
*/
|
||||
inline int64_t decode_zigzag64(uint64_t value) noexcept {
|
||||
return int64_t(value >> 1) ^ -int64_t(value & 1);
|
||||
}
|
||||
|
||||
} // end namespace protozero
|
||||
|
||||
#endif // PROTOZERO_VARINT_HPP
|
22
protozero/version.hpp
Normal file
22
protozero/version.hpp
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef PROTOZERO_VERSION_HPP
|
||||
#define PROTOZERO_VERSION_HPP
|
||||
|
||||
/*****************************************************************************
|
||||
|
||||
protozero - Minimalistic protocol buffer decoder and encoder in C++.
|
||||
|
||||
This file is from https://github.com/mapbox/protozero where you can find more
|
||||
documentation.
|
||||
|
||||
*****************************************************************************/
|
||||
|
||||
#define PROTOZERO_VERSION_MAJOR 1
|
||||
#define PROTOZERO_VERSION_MINOR 3
|
||||
#define PROTOZERO_VERSION_PATCH 0
|
||||
|
||||
#define PROTOZERO_VERSION_CODE (PROTOZERO_VERSION_MAJOR * 10000 + PROTOZERO_VERSION_MINOR * 100 + PROTOZERO_VERSION_PATCH)
|
||||
|
||||
#define PROTOZERO_VERSION_STRING "1.3.0"
|
||||
|
||||
|
||||
#endif // PROTOZERO_VERSION_HPP
|
232
serial.cpp
Normal file
232
serial.cpp
Normal file
@ -0,0 +1,232 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <sqlite3.h>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include "protozero/varint.hpp"
|
||||
#include "geometry.hpp"
|
||||
#include "mbtiles.hpp"
|
||||
#include "tile.hpp"
|
||||
#include "serial.hpp"
|
||||
|
||||
size_t fwrite_check(const void *ptr, size_t size, size_t nitems, FILE *stream, const char *fname) {
|
||||
size_t w = fwrite(ptr, size, nitems, stream);
|
||||
if (w != nitems) {
|
||||
fprintf(stderr, "%s: Write to temporary file failed: %s\n", fname, strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return w;
|
||||
}
|
||||
|
||||
void serialize_int(FILE *out, int n, long long *fpos, const char *fname) {
|
||||
serialize_long_long(out, n, fpos, fname);
|
||||
}
|
||||
|
||||
void serialize_long_long(FILE *out, long long n, long long *fpos, const char *fname) {
|
||||
unsigned long long zigzag = protozero::encode_zigzag64(n);
|
||||
|
||||
serialize_ulong_long(out, zigzag, fpos, fname);
|
||||
}
|
||||
|
||||
void serialize_ulong_long(FILE *out, unsigned long long zigzag, long long *fpos, const char *fname) {
|
||||
while (1) {
|
||||
unsigned char b = zigzag & 0x7F;
|
||||
if ((zigzag >> 7) != 0) {
|
||||
b |= 0x80;
|
||||
if (putc(b, out) == EOF) {
|
||||
fprintf(stderr, "%s: Write to temporary file failed: %s\n", fname, strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
*fpos += 1;
|
||||
zigzag >>= 7;
|
||||
} else {
|
||||
if (putc(b, out) == EOF) {
|
||||
fprintf(stderr, "%s: Write to temporary file failed: %s\n", fname, strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
*fpos += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void serialize_byte(FILE *out, signed char n, long long *fpos, const char *fname) {
|
||||
fwrite_check(&n, sizeof(signed char), 1, out, fname);
|
||||
*fpos += sizeof(signed char);
|
||||
}
|
||||
|
||||
void serialize_uint(FILE *out, unsigned n, long long *fpos, const char *fname) {
|
||||
fwrite_check(&n, sizeof(unsigned), 1, out, fname);
|
||||
*fpos += sizeof(unsigned);
|
||||
}
|
||||
|
||||
void deserialize_int(char **f, int *n) {
|
||||
long long ll;
|
||||
deserialize_long_long(f, &ll);
|
||||
*n = ll;
|
||||
}
|
||||
|
||||
void deserialize_long_long(char **f, long long *n) {
|
||||
unsigned long long zigzag = 0;
|
||||
deserialize_ulong_long(f, &zigzag);
|
||||
*n = protozero::decode_zigzag64(zigzag);
|
||||
}
|
||||
|
||||
void deserialize_ulong_long(char **f, unsigned long long *zigzag) {
|
||||
*zigzag = 0;
|
||||
int shift = 0;
|
||||
|
||||
while (1) {
|
||||
if ((**f & 0x80) == 0) {
|
||||
*zigzag |= ((unsigned long long) **f) << shift;
|
||||
*f += 1;
|
||||
shift += 7;
|
||||
break;
|
||||
} else {
|
||||
*zigzag |= ((unsigned long long) (**f & 0x7F)) << shift;
|
||||
*f += 1;
|
||||
shift += 7;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void deserialize_uint(char **f, unsigned *n) {
|
||||
memcpy(n, *f, sizeof(unsigned));
|
||||
*f += sizeof(unsigned);
|
||||
}
|
||||
|
||||
void deserialize_byte(char **f, signed char *n) {
|
||||
memcpy(n, *f, sizeof(signed char));
|
||||
*f += sizeof(signed char);
|
||||
}
|
||||
|
||||
int deserialize_long_long_io(FILE *f, long long *n, long long *geompos) {
|
||||
unsigned long long zigzag = 0;
|
||||
int ret = deserialize_ulong_long_io(f, &zigzag, geompos);
|
||||
*n = protozero::decode_zigzag64(zigzag);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int deserialize_ulong_long_io(FILE *f, unsigned long long *zigzag, long long *geompos) {
|
||||
*zigzag = 0;
|
||||
int shift = 0;
|
||||
|
||||
while (1) {
|
||||
int c = getc(f);
|
||||
if (c == EOF) {
|
||||
return 0;
|
||||
}
|
||||
(*geompos)++;
|
||||
|
||||
if ((c & 0x80) == 0) {
|
||||
*zigzag |= ((unsigned long long) c) << shift;
|
||||
shift += 7;
|
||||
break;
|
||||
} else {
|
||||
*zigzag |= ((unsigned long long) (c & 0x7F)) << shift;
|
||||
shift += 7;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int deserialize_int_io(FILE *f, int *n, long long *geompos) {
|
||||
long long ll = 0;
|
||||
int ret = deserialize_long_long_io(f, &ll, geompos);
|
||||
*n = ll;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int deserialize_uint_io(FILE *f, unsigned *n, long long *geompos) {
|
||||
if (fread(n, sizeof(unsigned), 1, f) != 1) {
|
||||
return 0;
|
||||
}
|
||||
*geompos += sizeof(unsigned);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int deserialize_byte_io(FILE *f, signed char *n, long long *geompos) {
|
||||
int c = getc(f);
|
||||
if (c == EOF) {
|
||||
return 0;
|
||||
}
|
||||
*n = c;
|
||||
(*geompos)++;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void write_geometry(drawvec const &dv, long long *fpos, FILE *out, const char *fname, long long wx, long long wy) {
|
||||
for (size_t i = 0; i < dv.size(); i++) {
|
||||
if (dv[i].op == VT_MOVETO || dv[i].op == VT_LINETO) {
|
||||
serialize_byte(out, dv[i].op, fpos, fname);
|
||||
serialize_long_long(out, dv[i].x - wx, fpos, fname);
|
||||
serialize_long_long(out, dv[i].y - wy, fpos, fname);
|
||||
wx = dv[i].x;
|
||||
wy = dv[i].y;
|
||||
} else {
|
||||
serialize_byte(out, dv[i].op, fpos, fname);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void serialize_feature(FILE *geomfile, serial_feature *sf, long long *geompos, const char *fname, long long wx, long long wy, bool include_minzoom) {
|
||||
serialize_byte(geomfile, sf->t, geompos, fname);
|
||||
|
||||
long long layer = 0;
|
||||
layer |= sf->layer << 6;
|
||||
layer |= (sf->seq != 0) << 5;
|
||||
layer |= (sf->index != 0) << 4;
|
||||
layer |= (sf->extent != 0) << 3;
|
||||
layer |= sf->has_id << 2;
|
||||
layer |= sf->has_tippecanoe_minzoom << 1;
|
||||
layer |= sf->has_tippecanoe_maxzoom << 0;
|
||||
|
||||
serialize_long_long(geomfile, layer, geompos, fname);
|
||||
if (sf->seq != 0) {
|
||||
serialize_long_long(geomfile, sf->seq, geompos, fname);
|
||||
}
|
||||
if (sf->has_tippecanoe_minzoom) {
|
||||
serialize_int(geomfile, sf->tippecanoe_minzoom, geompos, fname);
|
||||
}
|
||||
if (sf->has_tippecanoe_maxzoom) {
|
||||
serialize_int(geomfile, sf->tippecanoe_maxzoom, geompos, fname);
|
||||
}
|
||||
if (sf->has_id) {
|
||||
serialize_ulong_long(geomfile, sf->id, geompos, fname);
|
||||
}
|
||||
|
||||
serialize_int(geomfile, sf->segment, geompos, fname);
|
||||
|
||||
write_geometry(sf->geometry, geompos, geomfile, fname, wx, wy);
|
||||
serialize_byte(geomfile, VT_END, geompos, fname);
|
||||
if (sf->index != 0) {
|
||||
serialize_ulong_long(geomfile, sf->index, geompos, fname);
|
||||
}
|
||||
if (sf->extent != 0) {
|
||||
serialize_long_long(geomfile, sf->extent, geompos, fname);
|
||||
}
|
||||
|
||||
serialize_int(geomfile, sf->m, geompos, fname);
|
||||
if (sf->m != 0) {
|
||||
serialize_long_long(geomfile, sf->metapos, geompos, fname);
|
||||
}
|
||||
|
||||
if (sf->metapos < 0 && sf->m != sf->keys.size()) {
|
||||
fprintf(stderr, "Internal error: %lld doesn't match %lld\n", (long long) sf->m, (long long) sf->keys.size());
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < sf->keys.size(); i++) {
|
||||
serialize_long_long(geomfile, sf->keys[i], geompos, fname);
|
||||
serialize_long_long(geomfile, sf->values[i], geompos, fname);
|
||||
}
|
||||
|
||||
if (include_minzoom) {
|
||||
serialize_byte(geomfile, sf->feature_minzoom, geompos, fname);
|
||||
}
|
||||
}
|
49
serial.hpp
Normal file
49
serial.hpp
Normal file
@ -0,0 +1,49 @@
|
||||
size_t fwrite_check(const void *ptr, size_t size, size_t nitems, FILE *stream, const char *fname);
|
||||
|
||||
void serialize_int(FILE *out, int n, long long *fpos, const char *fname);
|
||||
void serialize_long_long(FILE *out, long long n, long long *fpos, const char *fname);
|
||||
void serialize_ulong_long(FILE *out, unsigned long long n, long long *fpos, const char *fname);
|
||||
void serialize_byte(FILE *out, signed char n, long long *fpos, const char *fname);
|
||||
void serialize_uint(FILE *out, unsigned n, long long *fpos, const char *fname);
|
||||
void serialize_string(FILE *out, const char *s, long long *fpos, const char *fname);
|
||||
|
||||
void deserialize_int(char **f, int *n);
|
||||
void deserialize_long_long(char **f, long long *n);
|
||||
void deserialize_ulong_long(char **f, unsigned long long *n);
|
||||
void deserialize_uint(char **f, unsigned *n);
|
||||
void deserialize_byte(char **f, signed char *n);
|
||||
|
||||
int deserialize_int_io(FILE *f, int *n, long long *geompos);
|
||||
int deserialize_long_long_io(FILE *f, long long *n, long long *geompos);
|
||||
int deserialize_ulong_long_io(FILE *f, unsigned long long *n, long long *geompos);
|
||||
int deserialize_uint_io(FILE *f, unsigned *n, long long *geompos);
|
||||
int deserialize_byte_io(FILE *f, signed char *n, long long *geompos);
|
||||
|
||||
struct serial_feature {
|
||||
long long layer;
|
||||
int segment;
|
||||
long long seq;
|
||||
|
||||
signed char t;
|
||||
signed char feature_minzoom;
|
||||
|
||||
bool has_id;
|
||||
unsigned long long id;
|
||||
|
||||
bool has_tippecanoe_minzoom;
|
||||
int tippecanoe_minzoom;
|
||||
|
||||
bool has_tippecanoe_maxzoom;
|
||||
int tippecanoe_maxzoom;
|
||||
|
||||
drawvec geometry;
|
||||
unsigned long long index;
|
||||
long long extent;
|
||||
|
||||
size_t m;
|
||||
std::vector<long long> keys;
|
||||
std::vector<long long> values;
|
||||
long long metapos;
|
||||
};
|
||||
|
||||
void serialize_feature(FILE *geomfile, serial_feature *sf, long long *geompos, const char *fname, long long wx, long long wy, bool include_minzoom);
|
8
tests/border/in.json
Normal file
8
tests/border/in.json
Normal file
File diff suppressed because one or more lines are too long
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user