Add MeshPocket message board and worldwide map
This commit is contained in:
1 parent
7d0709bf03
commit
5d31a839c5
41 files changed
+6852
-7
No files matched your search
@@ -86,6 +86,7 @@ public:
|
||||
uint64_t heard_millis;
|
||||
int queue_index;
|
||||
bool unread;
|
||||
bool own_sent;
|
||||
int channel_idx;
|
||||
char channel_name[32];
|
||||
char origin[62];
|
||||
@@ -135,14 +136,15 @@ public:
|
||||
|
||||
void add(uint64_t heard_millis, int channel_idx,
|
||||
const char* channel_name, const char* origin,
|
||||
const char* message, int queue_index = -1) {
|
||||
const char* message, int queue_index = -1, bool own_sent = false) {
|
||||
_head = (_head + 1) % Capacity;
|
||||
if (_count < Capacity) ++_count;
|
||||
|
||||
Entry& entry = _entries[_head];
|
||||
entry.heard_millis = heard_millis;
|
||||
entry.queue_index = queue_index;
|
||||
entry.unread = true;
|
||||
entry.unread = !own_sent;
|
||||
entry.own_sent = own_sent;
|
||||
entry.channel_idx = channel_idx;
|
||||
copyField(entry.channel_name, sizeof(entry.channel_name), channel_name);
|
||||
copyField(entry.origin, sizeof(entry.origin), origin);
|
||||
|
||||
@@ -138,12 +138,17 @@ void GxEPDDisplay::startFrame(ColorVal bkg) {
|
||||
if (_cycles_before_full_refresh != 0) {
|
||||
display.setPartialWindow(0, 0, display.width(), display.height());
|
||||
} else {
|
||||
#if UI_POCKET_GUI
|
||||
display.setFullWindow();
|
||||
_full_frame = true;
|
||||
#else
|
||||
// forces a full wipe of the screen ...
|
||||
display.clearScreen(0xFF); // Clears microcontroller side RAM
|
||||
display.writeScreenBuffer(0xFF); // Forces 0xFF (White) into the display controller's history registers
|
||||
// we'll need a partial refresh after that (whatever crc value is)
|
||||
last_display_crc_value = 0;
|
||||
resetPartialRefreshCounter();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,11 +252,25 @@ uint16_t GxEPDDisplay::getTextWidth(const char* str) {
|
||||
void GxEPDDisplay::endFrame() {
|
||||
if (_isOn == false) return;
|
||||
uint32_t crc = display_crc.finalize();
|
||||
#if UI_POCKET_GUI
|
||||
if (_full_frame) {
|
||||
display.display(false);
|
||||
display.setPartialWindow(0, 0, display.width(), display.height());
|
||||
resetPartialRefreshCounter();
|
||||
_full_frame = false;
|
||||
display.powerOff();
|
||||
} else if (crc != last_display_crc_value) {
|
||||
display.display(true);
|
||||
if (_cycles_before_full_refresh > 0) --_cycles_before_full_refresh;
|
||||
display.powerOff();
|
||||
}
|
||||
#else
|
||||
if (crc != last_display_crc_value) {
|
||||
display.display(true);
|
||||
if (_cycles_before_full_refresh > 0) {
|
||||
_cycles_before_full_refresh--;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
last_display_crc_value = crc;
|
||||
}
|
||||
@@ -15,6 +15,7 @@
|
||||
#include <CRC32.h>
|
||||
|
||||
#include "DisplayDriver.h"
|
||||
#include "PocketGuiConfig.h"
|
||||
|
||||
#ifndef EINK_MAX_PARTIAL_REFRESH
|
||||
// 60 to prevent ghosting when refreshing every minute ...
|
||||
@@ -58,6 +59,9 @@ class GxEPDDisplay : public DisplayDriver {
|
||||
int last_display_crc_value = 0;
|
||||
int _cycles_before_full_refresh;
|
||||
int max_partial_refresh = EINK_MAX_PARTIAL_REFRESH; // so this can be changed by an option after
|
||||
#if UI_POCKET_GUI
|
||||
bool _full_frame = false;
|
||||
#endif
|
||||
|
||||
public:
|
||||
#if defined(EINK_DISPLAY_MODEL)
|
||||
@@ -67,6 +71,29 @@ public:
|
||||
#endif
|
||||
|
||||
bool begin();
|
||||
#if UI_POCKET_GUI
|
||||
Adafruit_GFX& gfx() { return display; }
|
||||
void crcAdd(const void* bytes, size_t length) {
|
||||
display_crc.update(static_cast<const uint8_t*>(bytes), length);
|
||||
}
|
||||
void setMaxPartialRefresh(int count) {
|
||||
if (count == max_partial_refresh) return;
|
||||
max_partial_refresh = count;
|
||||
resetPartialRefreshCounter();
|
||||
}
|
||||
void setNativeRotation(int rotation) {
|
||||
rotation &= 3;
|
||||
if (display.getRotation() == rotation) return;
|
||||
display.setRotation(rotation);
|
||||
setDimensions(display.width(), display.height());
|
||||
display.setFullWindow();
|
||||
_full_frame = true;
|
||||
forceFullRefresh();
|
||||
}
|
||||
void setFlipped(bool flipped) override {
|
||||
setNativeRotation((DISPLAY_ROTATION + (flipped ? 2 : 0)) & 3);
|
||||
}
|
||||
#endif
|
||||
|
||||
bool isOn() override { return _isOn; }
|
||||
bool isEink() override { return true; }
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef UI_POCKET_GUI
|
||||
#if defined(HELTEC_MESH_POCKET) && defined(COMPANION_RADIO_FULL)
|
||||
#define UI_POCKET_GUI 1
|
||||
#else
|
||||
#define UI_POCKET_GUI 0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if UI_POCKET_GUI && !(defined(HELTEC_MESH_POCKET) && defined(COMPANION_RADIO_FULL))
|
||||
#error "The Pocket GUI requires MeshPocket Full Companion"
|
||||
#endif
|
||||
|
||||
#if UI_POCKET_GUI
|
||||
#ifndef UI_MSG_PREVIEW_SIZE
|
||||
#define UI_MSG_PREVIEW_SIZE 170
|
||||
#endif
|
||||
#ifndef MESH_COMPANION_SCREEN_STARTUP_BYTES
|
||||
#define MESH_COMPANION_SCREEN_STARTUP_BYTES 12288
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,213 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace mesh {
|
||||
namespace ui {
|
||||
namespace pocket {
|
||||
|
||||
// Store an explicit wire record rather than compiler-specific structure padding.
|
||||
struct Settings {
|
||||
uint8_t channel = 0;
|
||||
uint8_t text_large = 0;
|
||||
uint8_t name_len = 1;
|
||||
uint8_t flip = 0;
|
||||
uint8_t dark = 0;
|
||||
uint8_t idle = 1;
|
||||
uint8_t refresh = 1;
|
||||
uint8_t map_zoom = 3;
|
||||
};
|
||||
|
||||
constexpr size_t SETTINGS_RECORD_SIZE = 13;
|
||||
constexpr unsigned SETTINGS_COUNT = 8;
|
||||
|
||||
inline bool settingsValid(const Settings& value, unsigned channel_count = 40) {
|
||||
return channel_count != 0 && channel_count <= 256 && value.channel < channel_count
|
||||
&& value.text_large <= 1 && value.name_len <= 3
|
||||
&& value.flip <= 1 && value.dark <= 1
|
||||
&& value.idle <= 3 && value.refresh <= 3 && value.map_zoom <= 8;
|
||||
}
|
||||
|
||||
inline uint16_t settingsChecksum(const uint8_t* bytes, size_t length) {
|
||||
uint16_t crc = 0xFFFF;
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
crc ^= (uint16_t)bytes[i] << 8;
|
||||
for (unsigned bit = 0; bit < 8; ++bit)
|
||||
crc = crc & 0x8000 ? (uint16_t)((crc << 1) ^ 0x1021)
|
||||
: (uint16_t)(crc << 1);
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
inline bool encodeSettings(const Settings& value, uint8_t* bytes, size_t size,
|
||||
unsigned channel_count = 40) {
|
||||
if (bytes == nullptr || size != SETTINGS_RECORD_SIZE || !settingsValid(value, channel_count)) return false;
|
||||
bytes[0] = 'P'; bytes[1] = 'G'; bytes[2] = 1;
|
||||
bytes[3] = value.channel;
|
||||
bytes[4] = value.text_large;
|
||||
bytes[5] = value.name_len;
|
||||
bytes[6] = value.flip | (value.dark << 1);
|
||||
bytes[7] = value.idle;
|
||||
bytes[8] = value.refresh;
|
||||
bytes[9] = value.map_zoom;
|
||||
bytes[10] = 0;
|
||||
const uint16_t crc = settingsChecksum(bytes, 11);
|
||||
bytes[11] = (uint8_t)crc;
|
||||
bytes[12] = (uint8_t)(crc >> 8);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool decodeSettings(Settings& value, const uint8_t* bytes, size_t size,
|
||||
unsigned channel_count = 40) {
|
||||
if (bytes == nullptr || size != SETTINGS_RECORD_SIZE
|
||||
|| bytes[0] != 'P' || bytes[1] != 'G' || bytes[2] != 1
|
||||
|| (bytes[6] & ~3U) != 0 || bytes[10] != 0
|
||||
|| settingsChecksum(bytes, 11) != (uint16_t)(bytes[11] | (uint16_t)bytes[12] << 8)) return false;
|
||||
Settings next;
|
||||
next.channel = bytes[3]; next.text_large = bytes[4]; next.name_len = bytes[5];
|
||||
next.flip = bytes[6] & 1; next.dark = (bytes[6] >> 1) & 1;
|
||||
next.idle = bytes[7]; next.refresh = bytes[8]; next.map_zoom = bytes[9];
|
||||
if (!settingsValid(next, channel_count)) return false;
|
||||
value = next;
|
||||
return true;
|
||||
}
|
||||
|
||||
inline uint32_t idleMillis(const Settings& value) {
|
||||
static const uint32_t values[] = {30000, 60000, 300000, 0};
|
||||
return values[value.idle <= 3 ? value.idle : 1];
|
||||
}
|
||||
|
||||
inline int refreshCount(const Settings& value) {
|
||||
static const int values[] = {10, 30, 60, -1};
|
||||
return values[value.refresh <= 3 ? value.refresh : 1];
|
||||
}
|
||||
|
||||
inline unsigned nameCharacters(const Settings& value) {
|
||||
static const uint8_t values[] = {3, 5, 8, 0};
|
||||
return values[value.name_len <= 3 ? value.name_len : 1];
|
||||
}
|
||||
|
||||
inline double mapHeightKm(const Settings& value) {
|
||||
static const uint16_t values[] = {20000, 5000, 1000, 200, 100, 50, 20, 10, 5};
|
||||
return values[value.map_zoom <= 8 ? value.map_zoom : 3];
|
||||
}
|
||||
|
||||
inline bool cycleSetting(Settings& value, unsigned row, unsigned channel_count = 40) {
|
||||
switch (row) {
|
||||
case 0:
|
||||
if (channel_count == 0 || channel_count > 256) return false;
|
||||
value.channel = (uint8_t)((value.channel + 1U) % channel_count); break;
|
||||
case 1: value.text_large = !value.text_large; break;
|
||||
case 2: value.name_len = (value.name_len + 1) % 4; break;
|
||||
case 3: value.flip = !value.flip; break;
|
||||
case 4: value.dark = !value.dark; break;
|
||||
case 5: value.idle = (value.idle + 1) % 4; break;
|
||||
case 6: value.refresh = (value.refresh + 1) % 4; break;
|
||||
case 7: value.map_zoom = (value.map_zoom + 1) % 9; break;
|
||||
default: return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
struct BoardState {
|
||||
unsigned scroll = 0;
|
||||
void clamp(unsigned count) { if (scroll >= count) scroll = count == 0 ? 0 : count - 1; }
|
||||
bool scrollOlder(unsigned count) {
|
||||
clamp(count);
|
||||
if (count == 0 || scroll + 1 >= count) return false;
|
||||
++scroll;
|
||||
return true;
|
||||
}
|
||||
bool scrollNewer() {
|
||||
if (scroll == 0) return false;
|
||||
--scroll;
|
||||
return true;
|
||||
}
|
||||
void messageAdded() { if (scroll != 0 && scroll < 31) ++scroll; }
|
||||
};
|
||||
|
||||
inline uint32_t messageAgeSeconds(uint64_t now_millis, uint64_t heard_millis) {
|
||||
// MeshPocket uses Arduino's 32-bit millis(), widened in shared history.
|
||||
return (uint32_t)((uint32_t)now_millis - (uint32_t)heard_millis) / 1000;
|
||||
}
|
||||
|
||||
// Never leave a partial UTF-8 character at the end of a displayed name.
|
||||
inline size_t nextUtf8Character(const char* value, size_t offset, size_t length) {
|
||||
if (offset >= length) return length;
|
||||
const uint8_t lead = (uint8_t)value[offset];
|
||||
size_t count = lead < 0x80 ? 1 : lead >= 0xC2 && lead <= 0xDF ? 2
|
||||
: lead >= 0xE0 && lead <= 0xEF ? 3 : lead >= 0xF0 && lead <= 0xF4 ? 4 : 1;
|
||||
if (count > length - offset) return offset + 1;
|
||||
for (size_t i = 1; i < count; ++i)
|
||||
if (((uint8_t)value[offset + i] & 0xC0) != 0x80) return offset + 1;
|
||||
return offset + count;
|
||||
}
|
||||
|
||||
inline void truncateCharacters(char* text, unsigned count) {
|
||||
if (text == nullptr || count == 0) return;
|
||||
const size_t length = strlen(text);
|
||||
size_t position = 0;
|
||||
for (unsigned i = 0; i < count && position < length; ++i)
|
||||
position = nextUtf8Character(text, position, length);
|
||||
text[position] = 0;
|
||||
}
|
||||
|
||||
// The compact fonts include Latin-1. Replace unsupported code points without
|
||||
// splitting UTF-8 or reading beyond the supplied input length.
|
||||
inline void sanitizeText(char* output, size_t capacity, const char* input, size_t length) {
|
||||
if (output == nullptr || capacity == 0) return;
|
||||
size_t source = 0, destination = 0;
|
||||
if (input != nullptr) while (source < length && input[source] != 0) {
|
||||
const uint8_t lead = (uint8_t)input[source];
|
||||
const size_t next = nextUtf8Character(input, source, length);
|
||||
const size_t bytes = next - source;
|
||||
if (lead < 0x80) {
|
||||
if (destination + 1 >= capacity) break;
|
||||
output[destination++] = lead < 0x20 || lead == 0x7F ? ' ' : (char)lead;
|
||||
} else if (bytes == 2 && lead == 0xC2 && (uint8_t)input[source + 1] < 0xA0) {
|
||||
if (destination + 1 >= capacity) break;
|
||||
output[destination++] = ' ';
|
||||
} else if (bytes == 2 && (lead == 0xC2 || lead == 0xC3)) {
|
||||
if (destination + 2 >= capacity) break;
|
||||
output[destination++] = input[source];
|
||||
output[destination++] = input[source + 1];
|
||||
} else {
|
||||
if (destination + 1 >= capacity) break;
|
||||
output[destination++] = '*';
|
||||
}
|
||||
source = next;
|
||||
}
|
||||
output[destination] = 0;
|
||||
}
|
||||
|
||||
struct BoardMessageView {
|
||||
const char* sender;
|
||||
size_t sender_length;
|
||||
const char* text;
|
||||
};
|
||||
|
||||
inline void parseBoardMessage(const char* origin, const char* body, bool own_sent,
|
||||
BoardMessageView& result) {
|
||||
result.sender = origin == nullptr ? "" : origin;
|
||||
result.sender_length = strlen(result.sender);
|
||||
result.text = body == nullptr ? "" : body;
|
||||
if (!own_sent) {
|
||||
const char* separator = strstr(result.text, ": ");
|
||||
if (separator != nullptr && separator != result.text
|
||||
&& (size_t)(separator - result.text) <= 61) {
|
||||
result.sender = result.text;
|
||||
result.sender_length = (size_t)(separator - result.text);
|
||||
result.text = separator + 2;
|
||||
} else {
|
||||
// The old preview's origin includes a route suffix, never show it as a name.
|
||||
const char* route = strstr(result.sender, " [");
|
||||
if (route != nullptr) result.sender_length = (size_t)(route - result.sender);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace pocket
|
||||
} // namespace ui
|
||||
} // namespace mesh
|
||||
@@ -0,0 +1,227 @@
|
||||
#include "PocketWorldMap.h"
|
||||
#include "PocketGuiConfig.h"
|
||||
|
||||
#if UI_POCKET_GUI || defined(POCKET_WORLD_MAP_TEST)
|
||||
|
||||
#include "PocketWorldMapData.generated.h"
|
||||
#include "../ota/tinf/tinf.h"
|
||||
#include <math.h>
|
||||
|
||||
namespace mesh { namespace ui { namespace pocket {
|
||||
namespace {
|
||||
|
||||
constexpr float kPi = 3.14159265358979323846f;
|
||||
constexpr float kRadians = kPi / 180.0f;
|
||||
constexpr float kEarthKm = 6371.0088f;
|
||||
|
||||
struct Projection {
|
||||
float latitude, longitude, sine, cosine, pixels_per_km, radius;
|
||||
int width, height;
|
||||
bool global;
|
||||
};
|
||||
|
||||
bool makeProjection(const MapView& view, Projection& p) {
|
||||
if (!isfinite(view.latitude) || !isfinite(view.longitude) || !isfinite(view.height_km)
|
||||
|| view.latitude < -90.0 || view.latitude > 90.0 || view.longitude < -180.0
|
||||
|| view.longitude > 180.0 || view.height_km < 1.0 || view.height_km > 20000.0
|
||||
|| view.width < 2 || view.height < 2 || view.width > 4096 || view.height > 4096) return false;
|
||||
p.latitude = float(view.latitude) * kRadians;
|
||||
p.longitude = float(view.longitude) * kRadians;
|
||||
p.width = view.width;
|
||||
p.height = view.height;
|
||||
p.global = view.height_km == 20000.0;
|
||||
if (p.global) {
|
||||
// The overview includes every longitude and latitude, independent of the
|
||||
// local centre's latitude. Preserve 2:1 map proportions with blank margins.
|
||||
p.pixels_per_km = fminf((view.width - 1) / 360.0f, (view.height - 1) / 180.0f);
|
||||
return true;
|
||||
}
|
||||
p.sine = sinf(p.latitude);
|
||||
p.cosine = fabsf(float(view.latitude)) == 90.0f ? 0.0f : cosf(p.latitude);
|
||||
p.pixels_per_km = (view.height - 1) / float(view.height_km);
|
||||
p.radius = hypotf(view.width - 1, view.height - 1) * 0.5f / p.pixels_per_km / kEarthKm;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool project(const Projection& p, float latitude, float longitude, float& x, float& y) {
|
||||
if (!isfinite(latitude) || !isfinite(longitude) || latitude < -90.0f || latitude > 90.0f) return false;
|
||||
if (p.global) {
|
||||
float degrees = longitude - p.longitude / kRadians;
|
||||
while (degrees > 180.0f) degrees -= 360.0f;
|
||||
while (degrees < -180.0f) degrees += 360.0f;
|
||||
x = (p.width - 1) * 0.5f + degrees * p.pixels_per_km;
|
||||
y = (p.height - 1) * 0.5f - latitude * p.pixels_per_km;
|
||||
return true;
|
||||
}
|
||||
const float lat = latitude * kRadians;
|
||||
float delta = longitude * kRadians - p.longitude;
|
||||
// Decoded longitudes are bounded to +/-540 degrees, so at most two wraps.
|
||||
while (delta > kPi) delta -= 2.0f * kPi;
|
||||
while (delta < -kPi) delta += 2.0f * kPi;
|
||||
const float sin_lat = sinf(lat);
|
||||
const float cos_lat = fabsf(latitude) == 90.0f ? 0.0f : cosf(lat);
|
||||
const float cos_delta = cosf(delta);
|
||||
float cos_distance = p.sine * sin_lat + p.cosine * cos_lat * cos_delta;
|
||||
if (cos_distance > 1.0f) cos_distance = 1.0f;
|
||||
// At the far hemisphere the azimuthal singularity can join unrelated edges.
|
||||
// The Pocket's local map never needs that hemisphere to cover its viewport.
|
||||
if (cos_distance < 0.0f) return false;
|
||||
// c/sin(c) has a stable expansion at zero. It avoids acosf cancellation
|
||||
// (and two transcendental calls) for nearby nodes and coastline vertices.
|
||||
const float scale = cos_distance > 0.9999f
|
||||
? kEarthKm * (1.0f + (1.0f - cos_distance) / 3.0f)
|
||||
: kEarthKm * acosf(cos_distance) / sqrtf(1.0f - cos_distance * cos_distance);
|
||||
x = (p.width - 1) * 0.5f + scale * cos_lat * sinf(delta) * p.pixels_per_km;
|
||||
y = (p.height - 1) * 0.5f - scale * (p.cosine * sin_lat - p.sine * cos_lat * cos_delta) * p.pixels_per_km;
|
||||
return isfinite(x) && isfinite(y);
|
||||
}
|
||||
|
||||
// Conservative spherical-cap/bounding-box test; it may decode extra chunks,
|
||||
// but never discards visible land at the antimeridian or either pole.
|
||||
bool visibleChunk(const Projection& p, const data::Chunk& chunk) {
|
||||
if (p.global) return true;
|
||||
const float latitude = p.latitude / kRadians;
|
||||
const float degrees = p.radius / kRadians + 0.01f;
|
||||
if (chunk.north * 0.001f < latitude - degrees || chunk.south * 0.001f > latitude + degrees) return false;
|
||||
if (p.radius >= kPi * 0.5f || latitude + degrees >= 90.0f || latitude - degrees <= -90.0f) return true;
|
||||
const float ratio = sinf(p.radius) / p.cosine;
|
||||
if (ratio >= 1.0f || ratio <= -1.0f) return true;
|
||||
const float half_longitude = fabsf(asinf(ratio)) / kRadians + 0.01f;
|
||||
const float center = p.longitude / kRadians;
|
||||
// Source fragments are unwrapped; test adjacent copies of the view.
|
||||
for (int copy = -1; copy <= 1; ++copy) {
|
||||
const float longitude = center + copy * 360.0f;
|
||||
if (chunk.east * 0.001f >= longitude - half_longitude
|
||||
&& chunk.west * 0.001f <= longitude + half_longitude) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool clipAxis(float start, float delta, float minimum, float maximum, float& first, float& last) {
|
||||
if (delta == 0.0f) return start >= minimum && start <= maximum;
|
||||
float a = (minimum - start) / delta, b = (maximum - start) / delta;
|
||||
if (a > b) { const float swap = a; a = b; b = swap; }
|
||||
if (a > first) first = a;
|
||||
if (b < last) last = b;
|
||||
return first <= last;
|
||||
}
|
||||
|
||||
void drawClipped(const Projection& p, float ax, float ay, float bx, float by,
|
||||
MapLine line, void* context) {
|
||||
float first = 0.0f, last = 1.0f;
|
||||
const float margin = p.global ? ((p.width - 1) * 0.5f - 180.0f * p.pixels_per_km) : 0.0f;
|
||||
if (!clipAxis(ax, bx - ax, margin, p.width - 1 - margin, first, last)
|
||||
|| !clipAxis(ay, by - ay, 0.0f, p.height - 1, first, last)) return;
|
||||
const int x0 = static_cast<int>(lroundf(ax + first * (bx - ax)));
|
||||
const int y0 = static_cast<int>(lroundf(ay + first * (by - ay)));
|
||||
const int x1 = static_cast<int>(lroundf(ax + last * (bx - ax)));
|
||||
const int y1 = static_cast<int>(lroundf(ay + last * (by - ay)));
|
||||
line(context, x0, y0, x1, y1);
|
||||
}
|
||||
|
||||
void drawSegment(const Projection& p, float ax, float ay, float bx, float by,
|
||||
MapLine line, void* context) {
|
||||
if (p.global) {
|
||||
const float span = 360.0f * p.pixels_per_km;
|
||||
if (fabsf(bx - ax) > span * 0.5f) {
|
||||
// Split a seam crossing at the two map edges, never across the middle.
|
||||
if (bx > ax) ax += span;
|
||||
else bx += span;
|
||||
drawClipped(p, ax, ay, bx, by, line, context);
|
||||
drawClipped(p, ax - span, ay, bx - span, by, line, context);
|
||||
return;
|
||||
}
|
||||
}
|
||||
drawClipped(p, ax, ay, bx, by, line, context);
|
||||
}
|
||||
|
||||
uint16_t read16(const uint8_t* p) { return uint16_t(p[0]) | (uint16_t(p[1]) << 8); }
|
||||
int32_t read32(const uint8_t* p) {
|
||||
return static_cast<int32_t>(uint32_t(p[0]) | (uint32_t(p[1]) << 8)
|
||||
| (uint32_t(p[2]) << 16) | (uint32_t(p[3]) << 24));
|
||||
}
|
||||
|
||||
bool readDelta(const uint8_t*& cursor, const uint8_t* end, int32_t& delta) {
|
||||
uint32_t value = 0;
|
||||
// Coordinates fit signed 32-bit, but a map delta needs at most 21 bits.
|
||||
for (unsigned shift = 0; shift < 28; shift += 7) {
|
||||
if (cursor == end) return false;
|
||||
const uint8_t byte = *cursor++;
|
||||
value |= uint32_t(byte & 127) << shift;
|
||||
if (!(byte & 128)) {
|
||||
delta = (value & 1) ? -static_cast<int32_t>((value >> 1) + 1) : static_cast<int32_t>(value >> 1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool drawChunk(const Projection& p, const uint8_t* bytes, size_t size, MapLine line, void* context) {
|
||||
const uint8_t* cursor = bytes;
|
||||
const uint8_t* end = bytes + size;
|
||||
while (cursor != end) {
|
||||
if (size_t(end - cursor) < 10) return false;
|
||||
const unsigned count = read16(cursor);
|
||||
int32_t longitude = read32(cursor + 2), latitude = read32(cursor + 6);
|
||||
cursor += 10;
|
||||
if (count < 2 || count > 96 || longitude < -540000 || longitude > 540000
|
||||
|| latitude < -90000 || latitude > 90000) return false;
|
||||
float ax = 0.0f, ay = 0.0f;
|
||||
bool previous = project(p, latitude * 0.001f, longitude * 0.001f, ax, ay);
|
||||
for (unsigned point = 1; point < count; ++point) {
|
||||
int32_t dx, dy;
|
||||
if (!readDelta(cursor, end, dx) || !readDelta(cursor, end, dy)
|
||||
|| dx < -360000 || dx > 360000 || dy < -180000 || dy > 180000) return false;
|
||||
// Bounds checked in 64 bits before updating decoded coordinates.
|
||||
const int64_t next_lon = int64_t(longitude) + dx, next_lat = int64_t(latitude) + dy;
|
||||
if (next_lon < -540000 || next_lon > 540000 || next_lat < -90000 || next_lat > 90000) return false;
|
||||
longitude = int32_t(next_lon);
|
||||
latitude = int32_t(next_lat);
|
||||
float bx = 0.0f, by = 0.0f;
|
||||
const bool current = project(p, latitude * 0.001f, longitude * 0.001f, bx, by);
|
||||
if (previous && current) drawSegment(p, ax, ay, bx, by, line, context);
|
||||
previous = current;
|
||||
ax = bx;
|
||||
ay = by;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool projectWorldPoint(const MapView& view, double latitude, double longitude, int& x, int& y) {
|
||||
Projection p;
|
||||
float px, py;
|
||||
if (longitude < -180.0 || longitude > 180.0 || !makeProjection(view, p) || !project(p, latitude, longitude, px, py)
|
||||
|| px < -0.5f || py < -0.5f || px >= p.width - 0.5f || py >= p.height - 0.5f) return false;
|
||||
x = int(lroundf(px));
|
||||
y = int(lroundf(py));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool renderWorldMap(const MapView& view, MapLine line, void* context) {
|
||||
Projection p;
|
||||
if (!line || !makeProjection(view, p)) return false;
|
||||
uint8_t scratch[data::kScratchBytes];
|
||||
for (const auto& chunk : data::kChunks) {
|
||||
if (!visibleChunk(p, chunk)) continue;
|
||||
if (chunk.raw > sizeof(scratch) || chunk.offset > sizeof(data::kDeflate)
|
||||
|| chunk.packed > sizeof(data::kDeflate) - chunk.offset) return false;
|
||||
unsigned int size = chunk.raw;
|
||||
if (tinf_uncompress_exact(scratch, &size, data::kDeflate + chunk.offset, chunk.packed) != TINF_OK
|
||||
|| size != chunk.raw || !drawChunk(p, scratch, size, line, context)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#if defined(POCKET_WORLD_MAP_TEST)
|
||||
bool renderWorldMapChunkForTest(const MapView& view, const uint8_t* bytes, size_t size,
|
||||
MapLine line, void* context) {
|
||||
Projection p;
|
||||
return bytes && line && makeProjection(view, p) && drawChunk(p, bytes, size, line, context);
|
||||
}
|
||||
#endif
|
||||
|
||||
}}}
|
||||
#endif
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
namespace mesh { namespace ui { namespace pocket {
|
||||
|
||||
// Local azimuthal equidistant view. Height is the full vertical extent in km.
|
||||
// At 20,000 km use a complete equirectangular world overview, centred at the
|
||||
// requested longitude and the equator, with blank margins to preserve aspect.
|
||||
// The coarse land outline is useful around 100-200 km; close zooms locate nodes.
|
||||
struct MapView {
|
||||
double latitude, longitude, height_km;
|
||||
int width, height;
|
||||
};
|
||||
|
||||
using MapLine = void (*)(void* context, int x0, int y0, int x1, int y1);
|
||||
|
||||
// Coordinates are relative to this viewport, including its clipped boundary.
|
||||
// Returns false for an invalid view or corrupt built-in map data.
|
||||
bool renderWorldMap(const MapView& view, MapLine line, void* context);
|
||||
// Same projection as the coastline. False means invalid or outside the view.
|
||||
bool projectWorldPoint(const MapView& view, double latitude, double longitude, int& x, int& y);
|
||||
|
||||
#if defined(POCKET_WORLD_MAP_TEST)
|
||||
// Exercise the production bounded parser with malformed/truncated raw chunks.
|
||||
bool renderWorldMapChunkForTest(const MapView& view, const uint8_t* bytes, size_t size,
|
||||
MapLine line, void* context);
|
||||
#endif
|
||||
|
||||
}}}
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user