diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2014-01-17 16:53:37 +0100 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2014-01-17 16:53:37 +0100 |
commit | c59f62274dcec243048ff5335b7955f195a561a0 (patch) | |
tree | 4395a5e20b57b23baab0c89f4ebc3935fdef22ed /src | |
parent | 2e3b8ed0719d2668c656c8b6e7f868e89347e6e9 (diff) | |
download | qtlocation-mapboxgl-c59f62274dcec243048ff5335b7955f195a561a0.tar.gz |
move geometry reader to header file
Diffstat (limited to 'src')
-rw-r--r-- | src/map/pbf.hpp | 208 | ||||
-rw-r--r-- | src/map/tile.cpp | 52 |
2 files changed, 9 insertions, 251 deletions
diff --git a/src/map/pbf.hpp b/src/map/pbf.hpp deleted file mode 100644 index 41192d7b74..0000000000 --- a/src/map/pbf.hpp +++ /dev/null @@ -1,208 +0,0 @@ -#pragma once - -/* - * Some parts are from upb - a minimalist implementation of protocol buffers. - * - * Copyright (c) 2008-2011 Google Inc. See LICENSE for details. - * Author: Josh Haberman <jhaberman@gmail.com> - */ - -#include <cstdlib> -#include <cstdio> -#include <cstring> -#include <string> -// #include <cassert> - -#define PBF_USE_SETJMP - -#ifdef PBF_USE_SETJMP -#include <csetjmp> -#endif - -namespace llmr { - -struct pbf { - inline pbf(const unsigned char *data, uint32_t length); - inline pbf(const char *data, uint32_t length); - // inline pbf(const std::string& buffer); - - inline bool next(); - template <typename T = uint32_t> inline T varint(); - template <typename T = uint32_t> inline T svarint(); - inline std::string string(); - inline float float32(); - inline double float64(); - // inline int64_t int64(); - inline bool boolean(); - - inline void skip(); - inline void skipValue(uint32_t val); - inline void skipBytes(uint32_t bytes); - - const uint8_t *data; - const uint8_t *end; - uint32_t value; - uint32_t tag; - - enum error_code { - UNTERMINATED_VARINT = 1, - UNKNOWN_TYPE = 2, - END_OF_BUFFER = 3 - }; - - #ifdef PBF_USE_SETJMP - std::jmp_buf jump_buffer; - std::string msg; - #endif -}; - -pbf::pbf(const unsigned char *data, uint32_t length) - : data(data), - end(data + length) -{} - -pbf::pbf(const char *data, uint32_t length) - : data((const unsigned char *)data), - end((const unsigned char *)data + length) -{} - -// pbf::pbf(const std::string& buffer) -// : data((const unsigned char *)buffer.data()), -// end((const unsigned char *)buffer.data() + buffer.size()) -// {} - -bool pbf::next() -{ - if (data < end) { - value = (uint32_t)varint(); - tag = value >> 3; - return true; - } - return false; -} - - -template <typename T> -T pbf::varint() -{ - uint8_t byte = 0x80; - T result = 0; - int bitpos; - for (bitpos = 0; bitpos < 70 && (byte & 0x80); bitpos += 7) { - if (data >= end) { - #ifdef PBF_USE_SETJMP - msg = "unterminated varint, unexpected end of buffer"; - std::longjmp(jump_buffer, UNTERMINATED_VARINT); - #endif - exit(1); - } - result |= ((T)(byte = *data) & 0x7F) << bitpos; - - data++; - } - if (bitpos == 70 && (byte & 0x80)) { - #ifdef PBF_USE_SETJMP - msg = "unterminated varint (too long)"; - std::longjmp(jump_buffer, UNTERMINATED_VARINT); - #endif - exit(1); - } - - return result; -} - -template <typename T> -T pbf::svarint() -{ - T n = varint<T>(); - return (n >> 1) ^ -(T)(n & 1); -} - -std::string pbf::string() -{ - uint32_t bytes = (uint32_t)varint(); - const char *string = (const char *)data; - skipBytes(bytes); - return std::string(string, bytes); -} - -float pbf::float32() -{ - skipBytes(4); - float result; - memcpy(&result, data - 4, 4); - return result; -} - -double pbf::float64() -{ - skipBytes(8); - double result; - memcpy(&result, data - 8, 8); - return result; -} - -// int64_t pbf::int64() -// { -// return (int64_t)varint(); -// } - -bool pbf::boolean() -{ - skipBytes(1); - return *(bool *)(data - 1); -} - -// template <typename T, typename... Args> T *pbf::message(const Args&... args) -// { -// uint32_t bytes = (uint32_t)varint(); -// T *result = new T(data, bytes, args...); -// skipBytes(bytes); -// return result; -// } - -void pbf::skip() -{ - skipValue(value); -} - -void pbf::skipValue(uint32_t val) -{ - switch (val & 0x7) { - case 0: // varint - varint(); - break; - case 1: // 64 bit - skipBytes(8); - break; - case 2: // string/message - skipBytes((uint32_t)varint()); - break; - case 5: // 32 bit - skipBytes(4); - break; - default: - char text[32]; - snprintf(text, 32, "cannot skip unknown type %d", val & 0x7); - #ifdef PBF_USE_SETJMP - msg = text; - std::longjmp(jump_buffer, UNKNOWN_TYPE); - #endif - exit(1); - } -} - -void pbf::skipBytes(uint32_t bytes) -{ - data += bytes; - if (data > end) { - #ifdef PBF_USE_SETJMP - msg = "unexpected end of buffer"; - std::longjmp(jump_buffer, END_OF_BUFFER); - #endif - exit(1); - } -} - -} // end namespace llmr - diff --git a/src/map/tile.cpp b/src/map/tile.cpp index 15894678fa..83ef1d69b8 100644 --- a/src/map/tile.cpp +++ b/src/map/tile.cpp @@ -5,9 +5,10 @@ // #include <iostream> #include <thread> -#include "pbf.hpp" +#include <llmr/util/pbf.hpp> #include <llmr/util/vec2.hpp> #include <llmr/util/string.hpp> +#include <llmr/geometry/geometry.hpp> #include <cmath> using namespace llmr; @@ -175,50 +176,15 @@ void tile::parseFeature(const uint8_t *data, uint32_t bytes) { } void tile::loadGeometry(const uint8_t *data, uint32_t bytes) { - pbf geometry(data, bytes); + geometry geometry(data, bytes); - uint32_t cmd = 1; - uint32_t length = 0; - int32_t x = 0, y = 0; - - // var lines = []; - // var line = null; - int32_t ox = 0, oy = 0; - - while (geometry.data < geometry.end) { - if (!length) { - uint32_t cmd_length = (uint32_t)geometry.varint(); - cmd = cmd_length & 0x7; - length = cmd_length >> 3; + geometry::command cmd; + int32_t x, y; + while ((cmd = geometry.next(x, y)) != geometry::end) { + if (cmd == geometry::move_to) { + lineVertex.addDegenerate(); } - length--; - - if (cmd == 1 || cmd == 2) { - x += geometry.svarint(); - y += geometry.svarint(); - - if (cmd == 1) { - // moveTo - // fprintf(stderr, "[m %d/%d] ", x, y); - // degenerate vertex - lineVertex.addDegenerate(); - ox = x; - oy = y; - } else { - // lineTo - // fprintf(stderr, "[l %d/%d] ", x, y); - } - lineVertex.addCoordinate(x, y); - } else if (cmd == 7) { - // closePolygon - // fprintf(stderr, "[c]\n"); - lineVertex.addCoordinate(ox, oy); - } else { - // throw new Error('unknown command ' + cmd); - // throw std::runtime_error("unknown command"); - fprintf(stderr, "unknown command"); - exit(1); - } + lineVertex.addCoordinate(x, y); } } |