38#include <opencv2/core/persistence.hpp>
39#include <opencv2/calib3d.hpp>
40#include <opencv2/features2d.hpp>
41#include <opencv2/imgproc.hpp>
44#include <linux/ioprio.h>
50#include <sys/resource.h>
52#include <sys/statvfs.h>
53#include <sys/syscall.h>
62#include <condition_variable>
92namespace fs = std::filesystem;
95constexpr uint64_t
mib = 1024 * 1024;
100constexpr double pi = 3.14159265358979323846;
110static_assert(
decltype(
stopping)::is_always_lock_free,
111 "Signal cancellation requires lock-free atomic access");
120void check(
bool condition,
const char* message) {
121 if (!condition)
throw std::runtime_error(message);
129 check(clock_gettime(clock, &value) == 0,
"clock unavailable");
130 return value.tv_sec + value.tv_nsec * 1e-9;
137uint64_t
proc_kib(
const char* path,
const char* wanted) {
138 std::ifstream input(path);
140 while (std::getline(input, line)) {
141 std::istringstream values(line);
142 std::string key, unit;
144 if (values >> key >> amount >> unit && key == wanted && unit ==
"kB")
145 return amount * 1024;
154 std::function<void(
size_t)>
task;
165 std::unique_lock<std::mutex> lock(
mutex);
194 sigset_t blocked, previous;
195 sigemptyset(&blocked);
196 sigaddset(&blocked, SIGINT);
197 sigaddset(&blocked, SIGTERM);
198 check(pthread_sigmask(SIG_BLOCK, &blocked, &previous) == 0,
199 "cannot mask desktop worker signals");
201 for (
size_t index = 0; index <
count; ++index)
202 workers.emplace_back([
this] { work(); });
203 check(pthread_sigmask(SIG_SETMASK, &previous,
nullptr) == 0,
204 "cannot restore coordinator signals");
207 std::lock_guard<std::mutex> lock(
mutex);
211 for (
auto& worker :
workers) worker.join();
212 pthread_sigmask(SIG_SETMASK, &previous,
nullptr);
220 std::lock_guard<std::mutex> lock(
mutex);
224 for (
auto& worker :
workers) worker.join();
232 std::lock_guard<std::mutex> lock(
mutex);
246 void run(
size_t count, std::function<
void(
size_t)> operation,
247 const std::function<
void()>& checkpoint) {
248 std::unique_lock<std::mutex> lock(
mutex);
249 task = std::move(operation);
256 completed.wait_for(lock, std::chrono::milliseconds(20));
291 std::unique_ptr<DesktopPool>
pool;
300 Resources(uint64_t reserve_mb,
int cpu_percent, fs::path pause = {},
303 duty(cpu_percent / 100.0),
305 check(reserve_mb >= 192 && reserve_mb <= 4096 && cpu_percent >= 1 &&
307 "reserve must be 192..4096 MiB; CPU duty must be 1..20 percent of "
312 "--desktop requires at least 2 GiB RAM; MORA must use default mode");
313 check(getrlimit(RLIMIT_AS, &address) == 0,
314 "cannot read inherited address-space limit");
315 address_limit = address.rlim_cur == RLIM_INFINITY ? 0 : address.rlim_cur;
319 check(setrlimit(RLIMIT_AS, &address) == 0,
320 "cannot install resource limits");
322 check(setrlimit(RLIMIT_CORE, &core) == 0 &&
323 setrlimit(RLIMIT_NOFILE, &files) == 0,
324 "cannot install resource limits");
325 sched_param parameters{};
327 check(setpriority(PRIO_PROCESS, 0, 19) == 0 &&
328 sched_setscheduler(0, SCHED_IDLE, ¶meters) == 0,
329 "cannot enable idle CPU scheduling");
330 check(syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, 0,
331 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0)) == 0,
332 "cannot enable idle I/O scheduling");
334 check(prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == 0,
335 "cannot disable privilege escalation");
336 cv::setNumThreads(1);
340 check(sched_getaffinity(0,
sizeof(affinity), &affinity) == 0,
341 "desktop CPU affinity unavailable");
342 const uint64_t available_memory =
343 proc_kib(
"/proc/meminfo",
"MemAvailable:");
345 "insufficient RAM headroom for desktop workers");
346 const uint64_t memory_workers =
348 const size_t count = std::min<uint64_t>(
349 std::min(CPU_COUNT(&affinity), 64), memory_workers);
350 check(
count > 0,
"no desktop worker capacity");
351 pool = std::make_unique<DesktopPool>(
count);
362 if (
pool)
check(!
pool->cancelled(),
"desktop tile work cancelled");
370 check(
bool(
pool),
"desktop worker pool unavailable");
381 const uint64_t available_memory =
382 proc_kib(
"/proc/meminfo",
"MemAvailable:");
384 if (available_memory <= overhead)
return 1;
385 return std::max<size_t>(1, std::min<uint64_t>(
386 pool->size(), (available_memory - overhead) / (photo_budget_mib *
mib)));
393 struct stat status{};
394 if (lstat(
pause_file.c_str(), &status) == 0 || errno != ENOENT)
397 const double now =
seconds(CLOCK_MONOTONIC);
403 std::ifstream input(
"/proc/stat");
405 std::array<uint64_t, 8> ticks{};
408 for (
auto& value : ticks) {
412 check(input.good() && label ==
"cpu",
"CPU headroom unavailable");
422 const uint64_t resident =
proc_kib(
"/proc/self/status",
"VmRSS:");
423 const uint64_t available_memory =
424 proc_kib(
"/proc/meminfo",
"MemAvailable:");
427 return resident > 0 &&
444 const double now_cpu =
seconds(CLOCK_PROCESS_CPUTIME_ID);
445 const double now_wall =
seconds(CLOCK_MONOTONIC);
448 const double deadline = now_wall + std::max(0.0, rest);
451 static_cast<int>(std::clamp(
452 std::ceil((deadline -
seconds(CLOCK_MONOTONIC)) * 1000), 1.0,
457 poll(
nullptr, 0, 1000);
470 {
"cpu_duty_percent",
duty * 100 * (
pool ?
pool->size() : 1)},
471 {
"scheduler",
desktop_mode ?
"inherited" :
"SCHED_IDLE"},
472 {
"threads",
pool ?
static_cast<int>(
pool->size()) : cv::getNumThreads()},
474 {
"peak_rss_bytes",
proc_kib(
"/proc/self/status",
"VmHWM:")}});
476 result.set(
"mode",
"desktop");
477 result.set(
"cpu_throttled",
false);
480 result.set(
"elapsed_wall_seconds", elapsed);
481 result.set(
"process_cpu_seconds", cpu);
482 result.set(
"average_cpu_percent", elapsed > 0 ? cpu / elapsed * 100 : 0);
483 result.set(
"peak_active_workers",
static_cast<int>(
pool->peak_workers()));
496using File = std::unique_ptr<FILE, FileCloser>;
501 void operator()(DIR* directory)
const { closedir(directory); }
507 using std::runtime_error::runtime_error;
522 open(path.c_str(), O_RDONLY | O_NONBLOCK | O_NOFOLLOW | O_CLOEXEC);
524 struct stat status{};
525 if (fstat(
descriptor, &status) != 0 || !S_ISREG(status.st_mode) ||
526 status.st_size < 0 ||
static_cast<uint64_t
>(status.st_size) > maximum) {
528 throw InvalidPhoto(
"input exceeds regular-file limits");
539 const int descriptor =
open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
543 check(
result == 0,
"cannot synchronize checkpoint directory");
555 std::string pattern = destination.string() +
".tmp-XXXXXX";
556 const int descriptor = mkstemp(pattern.data());
564 check(
bool(
file),
"cannot open checkpoint stream");
571 void write(
const void* data,
size_t length) {
572 check(fwrite(data, 1, length,
file.get()) == length,
573 "checkpoint write failed");
577 void text(
const std::string& value) {
write(value.data(), value.size()); }
580 check(fflush(
file.get()) == 0 && fsync(fileno(
file.get())) == 0,
581 "checkpoint flush failed");
582 check(fclose(
file.release()) == 0,
"checkpoint close failed");
584 "checkpoint publish failed");
604 std::array<char, 16385>
bytes{};
606 check(!ferror(input.get()) &&
count <= 16384,
"invalid checkpoint length");
610 throw std::runtime_error(
"invalid JSON file " + path.string() +
": " +
error.what());
616 std::array<uint8_t, 32> hash{};
618 std::ostringstream
result;
619 for (uint8_t
byte : hash)
620 result << std::hex << std::setw(2) << std::setfill(
'0') << unsigned(
byte);
631std::string
identity(
const fs::path& path,
bool allow_missing =
false) {
632 struct stat status{};
633 const int result = lstat(path.c_str(), &status);
634 if (allow_missing && ((
result != 0 && (errno == ENOENT || errno == ESTALE)) ||
635 (
result == 0 && !S_ISREG(status.st_mode))))
638 "source disappeared or is not regular");
639 std::ostringstream value;
640 value << path.filename().string() <<
':' << status.st_dev <<
':'
641 << status.st_ino <<
':' << status.st_size <<
':'
642 << status.st_mtim.tv_sec <<
':' << status.st_mtim.tv_nsec <<
':'
643 << status.st_ctim.tv_sec <<
':' << status.st_ctim.tv_nsec;
644 const std::string text = value.str();
645 return digest(text.data(), text.size());
652 if (!
exif_get(metadata, directory, tag, &entry))
return {};
655 std::string text(
reinterpret_cast<const char*
>(entry.
data), entry.
size);
657 text.compare(0, 8, std::string(
"ASCII\0\0\0", 8)) == 0)
659 while (!text.empty() && text.back() ==
'\0') text.pop_back();
665double numeric(
const Json& values,
const char* key,
double minimum,
668 (std::string(
"missing numeric geometry: ") + key).c_str());
669 const double result = values[key].
get<
double>();
671 "geometry out of range");
677cv::Matx33d
rotation(
double roll,
double pitch,
double yaw) {
678 const cv::Matx33d roll_matrix(1, 0, 0, 0, cos(roll), -sin(roll), 0, sin(roll),
680 const cv::Matx33d pitch_matrix(cos(pitch), 0, sin(pitch), 0, 1, 0,
681 -sin(pitch), 0, cos(pitch));
682 const cv::Matx33d yaw_matrix(cos(yaw), -sin(yaw), 0, sin(yaw), cos(yaw), 0, 0,
684 return yaw_matrix * pitch_matrix * roll_matrix;
692 if (path.empty())
return nullptr;
695 "invalid flat real-camera profile");
700 {
"height",
camera.height},
705 {
"mount_roll_deg",
camera.mount_roll_deg},
706 {
"mount_pitch_deg",
camera.mount_pitch_deg},
707 {
"mount_yaw_deg",
camera.mount_yaw_deg},
708 {
"filename_prefix", prefix}});
723 value[3].get<int>()};
727 result.bottom < (1 << 18) &&
730 "map exceeds configured pixel side or Mercator limits");
740 if (
empty())
return other;
754 std::array<cv::Point2f, 4>
corners(
const cv::Matx33d& transform)
const {
755 std::array<cv::Point2f, 4>
result{};
756 const std::array<cv::Point, 4> pixels{{{0, 0},
759 {0,
image.rows - 1}}};
760 for (
size_t index = 0; index < pixels.size(); ++index) {
761 const cv::Vec3d
point =
762 transform * cv::Vec3d(pixels[index].x, pixels[index].y, 1);
771 for (
const cv::Point& pixel :
772 std::array<cv::Point, 4>{{{0, 0},
775 {0,
image.rows - 1}}}) {
776 const cv::Vec3d
point =
to_world * cv::Vec3d(pixel.x, pixel.y, 1);
787 bool standard_profile) {
789 const std::string document =
791 standard_profile = standard_profile &&
792 document.find(
"USHER_MOCK_MAP_V1") == std::string::npos;
793 if (
camera.is_null() && !standard_profile) {
798 "missing MIMIC geometry; real JPEGs require --camera-profile");
800 const bool xyz_geometry = provenance.
value(
"tile_scheme",
"") ==
"XYZ";
802 const bool legacy_cache_geometry = provenance.
value(
"provider",
"") ==
"GOOGLE";
805 provenance.
value(
"schema",
"") ==
"USHER_MOCK_MAP_V1" &&
806 provenance.
value(
"projection",
"") ==
"pinhole_flat_ground" &&
807 (xyz_geometry || legacy_cache_geometry) &&
808 provenance.
value(
"zoom", 0) == 18,
809 "only clean USHER MIMIC pinhole photos are supported");
810 }
else if (!
camera.is_null()) {
811 photo_check(document.find(
"USHER_MOCK_MAP_V1") == std::string::npos,
812 "mock photo in real-camera input");
814 "JPEG dimensions differ from real-camera profile");
817 .find(
"USHER_LENS_V1") == std::string::npos,
818 "lens derivative cannot be inverted without its shutter/lens "
819 "model; use clean photo");
821 uint32_t orientation_value = 0;
825 "unsupported EXIF orientation");
826 std::istringstream description(
829 std::getline(description, field,
';');
831 const bool flyeye = (standard_profile || !
camera.is_null()) &&
832 field.find(
"FlyEye simulated camera") == 0;
837 pose.
value(
"datum",
"") ==
"WGS-84",
838 "unsupported FlyEye position metadata");
839 shot.set(
"lat_deg", pose.
at(
"latitude_deg"));
840 shot.set(
"lon_deg", pose.
at(
"longitude_deg"));
841 shot.set(
"groundalt_m", pose.
at(
"agl_m"));
842 shot.set(
"roll_deg", 0);
843 shot.set(
"pitch_deg", 0);
844 shot.set(
"yaw_deg", 0);
847 "missing USHER shot metadata");
848 std::map<std::string, std::string> fields;
849 while (std::getline(description, field,
';')) {
850 const size_t start = field.find_first_not_of(
" \t\r\n"),
851 end = field.find_last_not_of(
" \t\r\n");
854 const size_t separator = field.find(
'=');
857 .emplace(field.substr(0, separator),
858 field.substr(separator + 1))
860 "duplicate or invalid metadata field");
862 for (
const char* key : {
"lat_deg",
"lon_deg",
"roll_deg",
"pitch_deg",
863 "yaw_deg",
"groundalt_m"}) {
864 std::string selected = key;
866 (selected ==
"lat_deg" || selected ==
"lon_deg") &&
867 fields.count(
"original_" + selected))
868 selected =
"original_" + selected;
869 photo_check(fields.count(selected),
"missing shot field");
872 shot.set(key, std::stod(fields.at(selected), &consumed));
873 photo_check(consumed == fields.at(selected).size(),
874 "invalid shot number");
875 }
catch (
const std::logic_error&) {
882 const double altitude =
numeric(
shot,
"groundalt_m", 0.1, 10000) /
884 const double roll =
numeric(
shot,
"roll_deg", -180, 180) *
pi / 180;
885 const double pitch =
numeric(
shot,
"pitch_deg", -180, 180) *
pi / 180;
886 const double yaw =
numeric(
shot,
"yaw_deg", -360, 360) *
pi / 180;
887 cv::Matx33d rays, mount = cv::Matx33d::eye();
889 const double horizontal =
890 standard_profile ? tan(60 *
pi / 360) :
891 tan(
numeric(provenance,
"horizontal_fov_deg", 1, 170) *
pi / 360);
892 const double vertical =
893 standard_profile ? horizontal * (height - 1) / (width - 1) :
894 tan(
numeric(provenance,
"vertical_fov_deg", 1, 170) *
pi / 360);
895 rays = cv::Matx33d(0, -2 * vertical / (height - 1), vertical,
896 2 * horizontal / (width - 1), 0, -horizontal, 0, 0, 1);
898 const double fx =
camera.at(
"fx").get<
double>(), fy =
camera.at(
"fy").get<
double>(),
899 cx =
camera.at(
"cx").get<
double>(), cy =
camera.at(
"cy").get<
double>();
900 rays = cv::Matx33d(0, -1 / fy, cy / fy, 1 / fx, 0, -cx / fx, 0, 0, 1);
902 camera.at(
"mount_pitch_deg").get<
double>() *
pi / 180,
903 camera.at(
"mount_yaw_deg").get<
double>() *
pi / 180);
905 const cv::Matx33d ground =
rotation(roll, pitch, yaw) * mount * rays;
907 const double center_y =
910 cv::Matx33d(0, altitude, center_x, -altitude, 0, center_y, 0, 0, 1) *
922 double minimum_x = INFINITY, minimum_y = INFINITY, maximum_x = -INFINITY,
923 maximum_y = -INFINITY;
924 for (
int row : {0, height - 1})
925 for (
int column : {0, width - 1}) {
930 photo_check(std::isfinite(world_x) && std::isfinite(world_y) &&
931 world_x >= 0 && world_y >= 0 &&
933 "footprint crosses Mercator/dateline boundary");
934 minimum_x = std::min(minimum_x, world_x);
935 maximum_x = std::max(maximum_x, world_x);
936 minimum_y = std::min(minimum_y, world_y);
937 maximum_y = std::max(maximum_y, world_y);
947 "photo footprint exceeds 64-tile work limit");
954 void (*
emit)(j_common_ptr, int) =
nullptr;
967 reinterpret_cast<JpegError*
>(state->err)->emit(state, level);
991 const Json&
camera,
bool standard_profile =
false,
992 bool parallel_decode =
false) {
994 static thread_local std::unique_ptr<Decoder> cached_decoder;
995 std::unique_ptr<Decoder> local_decoder;
996 if (parallel_decode) {
997 if (!cached_decoder) cached_decoder = std::make_unique<Decoder>();
998 }
else local_decoder = std::make_unique<Decoder>();
999 Decoder*
const volatile decoder = parallel_decode ? cached_decoder.get() : local_decoder.get();
1004 if (setjmp(
decoder->error.recovery)) {
1005 if (parallel_decode) cached_decoder.reset();
1009 jpeg_abort_decompress(&
decoder->state);
1014 jpeg_create_decompress(&
decoder->state);
1016 jpeg_stdio_src(&
decoder->state, input.get());
1017 jpeg_save_markers(&
decoder->state, JPEG_APP0 + 1, 65533);
1019 "invalid JPEG header");
1020 const unsigned width =
decoder->state.image_width,
1021 height =
decoder->state.image_height;
1022 photo_check(width >= 2 && height >= 2 && width <= 4096 && height <= 4096 &&
1023 uint64_t(width) * height <=
1024 (
camera.is_null() ? 8388608u : 16777216u) &&
1025 !
decoder->state.progressive_mode,
1026 "unsupported JPEG size or progressive encoding");
1029 if (
marker->marker != JPEG_APP0 + 1 ||
marker->data_length < 6 ||
1030 std::string(
reinterpret_cast<const char*
>(
marker->data), 6) !=
1031 std::string(
"Exif\0\0", 6))
1044 if (parallel_decode)
resources.tile_checkpoint();
1046 decoder->state.out_color_space = JCS_RGB;
1047 jpeg_start_decompress(&
decoder->state);
1048 decoder->photo.image.create(height, width, CV_8UC3);
1049 while (
decoder->state.output_scanline < height) {
1050 if (
decoder->state.output_scanline % 32 == 0) {
1051 if (parallel_decode)
resources.tile_checkpoint();
1056 "incomplete JPEG scanline");
1058 jpeg_finish_decompress(&
decoder->state);
1060 return std::move(
decoder->photo);
1072 if (!fs::exists(path))
return;
1074 std::array<char, 512>
header{};
1076 "tile header missing");
1081 throw std::runtime_error(
"invalid tile header " + path.string() +
": " +
error.what());
1083 check(
record.value(
"schema",
"") ==
"PANGEA_TILE_V1",
1084 "invalid tile format");
1087 const size_t length =
sums.total() *
sums.elemSize();
1088 check(fread(
sums.data, 1, length, input.get()) == length &&
1089 fgetc(input.get()) == EOF && !ferror(input.get()),
1090 "invalid tile data length");
1092 "tile checksum mismatch");
1094 "invalid tile accumulation");
1099 const size_t length =
sums.total() *
sums.elemSize();
1113 return root /
"tiles" /
1114 (std::to_string(column) +
"-" + std::to_string(
row) +
".tile");
1122 struct statvfs status{};
1123 check(statvfs(root.c_str(), &status) == 0,
"disk headroom unavailable");
1126 check(uint64_t(status.f_bavail) * status.f_frsize >= 512 *
mib + required,
1127 "disk reserve reached; checkpoint retained, free space and resume");
1143 const cv::Matx33d local = cv::Matx33d(1, 0, -tile_column *
tile_size, 0,
1146 const auto footprint = photo.
corners(local);
1147 const std::array<cv::Point2f, 4> rectangle{
1152 std::vector<cv::Point2f> intersection;
1153 if (cv::intersectConvexConvex(footprint, rectangle, intersection) <= 0)
1155 const fs::path path =
tile_path(root, tile_column, tile_row);
1158 check(tile.
sequence <= sequence,
"tile newer than pending photo");
1159 if (tile.
sequence == sequence)
return;
1160 bool contributed =
false;
1163 for (
int start_row = 0; start_row <
tile_size; start_row += 16) {
1165 const cv::Matx33d transform =
1166 cv::Matx33d(1, 0, -tile_column *
tile_size, 0, 1,
1167 -tile_row *
tile_size - start_row, 0, 0, 1) *
1169 const cv::Matx33d inverse = transform.inv();
1171 cv::warpPerspective(photo.
image, warped, transform,
1172 cv::Size(
tile_size, 16), cv::INTER_LINEAR,
1173 cv::BORDER_CONSTANT);
1174 for (
int row = 0;
row < warped.rows; ++
row) {
1175 auto* sums = tile.
sums.ptr<cv::Vec4f>(start_row +
row);
1176 const auto* colors = warped.ptr<cv::Vec3b>(
row);
1177 for (
int column = 0; column <
tile_size; ++column) {
1178 const cv::Vec3d source = inverse * cv::Vec3d(column,
row, 1);
1179 if (source[2] <= 0)
continue;
1180 const double source_x = source[0] / source[2],
1181 source_y = source[1] / source[2];
1183 std::min({source_x, source_y, photo.
image.cols - 1 - source_x,
1184 photo.
image.rows - 1 - source_y});
1185 if (edge < 1)
continue;
1188 const float weight = std::min(
1190 edge / (0.125 * std::min(photo.
image.cols, photo.
image.rows)));
1192 for (
int channel = 0; channel < 3; ++channel)
1193 sums[column][channel] += colors[column][channel] * weight;
1194 sums[column][3] += weight;
1211void blend(
const Photo& photo,
const fs::path& root, uint64_t sequence,
1216 resources.parallel_tiles(
static_cast<size_t>(columns) * rows,
1219 photo.
bounds.
left +
static_cast<int>(index % columns),
1220 photo.
bounds.
top +
static_cast<int>(index / columns));
1260 static thread_local std::vector<cv::Point2f> intersection;
1265 const cv::Matx33d local =
1266 cv::Matx33d(1, 0, -tile_column *
tile_size, 0, 1,
1268 const auto footprint = photo.
corners(local);
1269 const std::array<cv::Point2f, 4> rectangle{{{0, 0}, {
tile_size - 1, 0},
1271 intersection.clear();
1272 if (cv::intersectConvexConvex(footprint, rectangle, intersection) <= 0)
1275 tile.
column = tile_column;
1276 tile.
row = tile_row;
1279 bool contributed =
false;
1280 for (
int start_row = 0; start_row <
tile_size; start_row += 16) {
1282 const cv::Matx33d transform =
1283 cv::Matx33d(1, 0, -tile_column *
tile_size, 0, 1,
1285 const cv::Matx33d inverse = transform.inv();
1286 cv::Mat colors = tile.
colors.rowRange(start_row, start_row + 16);
1287 cv::warpPerspective(photo.
image, colors, transform, cv::Size(
tile_size, 16),
1288 cv::INTER_LINEAR, cv::BORDER_CONSTANT);
1290 auto* weights = tile.
weights.ptr<
float>(start_row +
row);
1291 for (
int column = 0; column <
tile_size; ++column) {
1292 const cv::Vec3d source = inverse * cv::Vec3d(column,
row, 1);
1293 if (source[2] <= 0)
continue;
1294 const double source_x = source[0] / source[2];
1295 const double source_y = source[1] / source[2];
1296 const double edge = std::min({source_x, source_y,
1297 photo.
image.cols - 1 - source_x, photo.
image.rows - 1 - source_y});
1298 if (edge < 1)
continue;
1299 weights[column] = std::min(1.0,
1300 edge / (0.125 * std::min(photo.
image.cols, photo.
image.rows)));
1305 if (contributed) prepared.
tiles.push_back(std::move(tile));
1319 resources.parallel_tiles(prepared.
tiles.size(), [&](
size_t index) {
1320 resources.tile_checkpoint();
1321 disk_headroom(root, 2 * mib);
1322 const ProjectedTile& projected = prepared.tiles[index];
1323 const fs::path path = tile_path(root, projected.column, projected.row);
1326 check(tile.sequence <= sequence,
"tile newer than pending photo");
1327 if (tile.sequence == sequence) return;
1328 for (int row = 0; row < tile_size; ++row) {
1329 if (row % 16 == 0) resources.tile_checkpoint();
1330 auto* sums = tile.sums.ptr<cv::Vec4f>(row);
1331 const auto* colors = projected.colors.ptr<cv::Vec3b>(row);
1332 const auto* weights = projected.weights.ptr<float>(row);
1333 for (int column = 0; column < tile_size; ++column) {
1334 if (weights[column] <= 0) continue;
1335 for (int channel = 0; channel < 3; ++channel)
1336 sums[column][channel] += colors[column][channel] * weights[column];
1337 sums[column][3] += weights[column];
1340 tile.sequence = sequence;
1365 if (
bounds.empty())
return;
1369 const std::string name =
1370 "snapshot-" + std::to_string(state.
at(
"sequence").
get<uint64_t>());
1371 const fs::path destination = root / name;
1372 if (fs::is_symlink(root /
"latest") &&
1373 fs::read_symlink(root /
"latest") == name) {
1375 check(published.
at(
"sequence") == state.
at(
"sequence") &&
1376 published.
at(
"bounds") == state.
at(
"bounds"),
1377 "published snapshot disagrees with checkpoint");
1380 fs::create_directory(destination);
1382 mask(destination /
"map-mask.pgm");
1383 mask.
text(
"P5\n" + std::to_string(width) +
" " + std::to_string(height) +
1385 auto encoder = std::make_unique<Encoder>();
1388 if (setjmp(
encoder->error.recovery))
1389 throw std::runtime_error(
"map JPEG encoding failed");
1390 jpeg_create_compress(&
encoder->state);
1393 encoder->state.image_width = width;
1394 encoder->state.image_height = height;
1395 encoder->state.input_components = 3;
1396 encoder->state.in_color_space = JCS_RGB;
1397 jpeg_set_defaults(&
encoder->state);
1398 jpeg_set_quality(&
encoder->state, 95, TRUE);
1399 jpeg_start_compress(&
encoder->state, TRUE);
1402 stripe_top < (
bounds.bottom + 1) *
tile_size; stripe_top += stripe_rows) {
1403 const int tile_row = stripe_top /
tile_size;
1404 const int start_row = stripe_top %
tile_size;
1406 encoder->stripe = cv::Mat(stripe_rows, width, CV_8UC3, cv::Scalar::all(0));
1407 encoder->covered = cv::Mat(stripe_rows, width, CV_8UC1, cv::Scalar::all(0));
1408 const auto fill_tile = [&](
int tile_column) {
1412 for (
int row = 0;
row < stripe_rows; ++
row) {
1414 auto* colors =
encoder->stripe.ptr<cv::Vec3b>(
row) +
1418 const auto* sums = tile.
sums.ptr<cv::Vec4f>(start_row +
row);
1419 for (
int column = 0; column <
tile_size; ++column)
1420 if (sums[column][3] > 0) {
1421 for (
int channel = 0; channel < 3; ++channel)
1422 colors[column][channel] = cv::saturate_cast<uint8_t>(
1423 sums[column][channel] / sums[column][3]);
1424 valid[column] = 255;
1431 fill_tile(bounds.left + static_cast<int>(index));
1434 for (
int tile_column =
bounds.left; tile_column <=
bounds.right;
1436 fill_tile(tile_column);
1438 for (
int row = 0;
row < stripe_rows; ++
row) {
1441 check(jpeg_write_scanlines(&
encoder->state, &pixels, 1) == 1,
1442 "map scanline write failed");
1446 jpeg_finish_compress(&
encoder->state);
1449 const double origin_x =
1451 const double origin_y =
1453 std::ostringstream world;
1457 << origin_y <<
'\n';
1458 AtomicFile world_file(destination /
"map.jgw");
1459 world_file.
text(world.str());
1461 std::ostringstream vrt;
1462 vrt << std::setprecision(17) <<
"<VRTDataset rasterXSize=\"" << width
1463 <<
"\" rasterYSize=\"" << height
1464 <<
"\"><SRS>EPSG:3857</SRS><GeoTransform>"
1467 <<
"</GeoTransform>";
1468 for (
int band = 1; band <= 4; ++band) {
1469 vrt <<
"<VRTRasterBand dataType=\"Byte\" band=\"" << band
1470 <<
"\"><ColorInterp>"
1471 << std::array<const char*, 4>{
"Red",
"Green",
"Blue",
"Alpha"}[band - 1]
1472 <<
"</ColorInterp><SimpleSource><SourceFilename relativeToVRT=\"1\">"
1473 << (band == 4 ?
"map-mask.pgm" :
"map.jpg")
1474 <<
"</SourceFilename><SourceBand>" << (band == 4 ? 1 : band)
1475 <<
"</SourceBand></SimpleSource></VRTRasterBand>";
1477 vrt <<
"</VRTDataset>\n";
1478 AtomicFile vrt_file(destination /
"map.vrt");
1479 vrt_file.
text(vrt.str());
1481 Json metadata = state;
1483 metadata.
set(
"crs",
"EPSG:3857");
1485 metadata.
set(
"width", width);
1486 metadata.
set(
"height", height);
1487 metadata.
set(
"model",
"pose_pinhole_flat_ground_feather_v1");
1488 metadata.
set(
"source_mode",
1489 state.
value(
"standard_profile",
false) ?
"assumed_camera" :
1490 state.
value(
"camera",
Json()).is_null() ?
"mimic" :
"real_camera");
1491 metadata.
set(
"limitations",
1492 "Flat-ground pinhole projection of input JPEGs; no terrain/3D "
1493 "reconstruction, lens correction, added effects or accuracy "
1495 if (state.
value(
"align_images",
false)) {
1496 metadata.
set(
"model",
"neighbor_similarity_registered_visual_map_v1");
1497 metadata.
set(
"limitations",
1498 "Visual neighbor registration with pose-selected candidates and pose "
1499 "fallbacks; first-anchor scale/position and accumulated drift are not "
1500 "surveyed accuracy. Source overlays, parallax and renderer artifacts "
1501 "remain; no GPS correction or terrain reconstruction.");
1503 write_json(destination /
"map.json", metadata);
1504 const fs::path temporary = root /
".latest-next";
1505 unlink(temporary.c_str());
1506 check(symlink(name.c_str(), temporary.c_str()) == 0 &&
1507 rename(temporary.c_str(), (root /
"latest").c_str()) == 0,
1508 "cannot publish completed map snapshot");
1510 for (
const auto& entry : fs::directory_iterator(root)) {
1511 if (!entry.is_directory() || entry.is_symlink() ||
1512 entry.path().filename().string().rfind(
"snapshot-", 0) != 0 ||
1513 entry.path().filename() == name)
1515 for (
const char* file :
1516 {
"map.jpg",
"map-mask.pgm",
"map.jgw",
"map.vrt",
"map.json"})
1517 fs::remove(entry.path() / file);
1518 std::error_code
error;
1519 fs::remove(entry.path(),
error);
1521 std::cout <<
"PANGEA: map published " << (root /
"latest/map.jpg") <<
'\n';
1546 cv::Mat resized, gray, mask;
1547 cv::Ptr<cv::SIFT> detector = cv::SIFT::create(4000);
1549 static thread_local Workspace
workspace;
1550 const double scale = std::min(1.0, 1024.0 / std::max(photo.
image.cols, photo.
image.rows));
1551 cv::resize(photo.
image,
workspace.resized, cv::Size(), scale, scale, cv::INTER_AREA);
1555 const int margin = std::max(1,
int(40 * scale));
1557 workspace.gray.rows - 3 * margin)).setTo(255);
1561 current.
name = name;
1579 const int margin = 40;
1580 if (photo.
image.cols <= 2 * margin || photo.
image.rows <= 3 * margin)
1583 size_t best_inliers = 0;
1584 cv::Matx33d selected = photo.
to_world;
1585 const cv::Vec3d center_pixel((photo.
image.cols - 1) / 2.0,
1586 (photo.
image.rows - 1) / 2.0, 1);
1587 const cv::Vec3d predicted_center = photo.
to_world * center_pixel;
1590 cv::Matx33d to_world;
1593 std::vector<Candidate> candidates(
references.size());
1595 const auto* reference = &references[references.size() - 1 - candidate_index];
1596 if (current.descriptors.empty() || reference->descriptors.empty()) return;
1597 const cv::Vec3d relative = reference->position_prior.inv() * predicted_center;
1598 if (relative[2] <= 0 || relative[0] / relative[2] < -reference->width * 0.25 ||
1599 relative[0] / relative[2] > reference->width * 1.25 ||
1600 relative[1] / relative[2] < -reference->height * 0.25 ||
1601 relative[1] / relative[2] > reference->height * 1.25) return;
1602 resources.tile_checkpoint();
1604 cv::BFMatcher matcher{cv::NORM_L2};
1605 std::vector<std::vector<cv::DMatch>> matches;
1606 std::vector<cv::Point2f> source, target, supported;
1607 std::vector<double> errors;
1610 static thread_local Workspace
workspace;
1616 matches.clear(); source.clear(); target.clear();
1618 workspace.matcher.knnMatch(current.descriptors, reference->descriptors, matches, 2);
1619 for (
const auto& pair : matches) {
1620 if (pair.size() != 2 || pair[0].distance >= 0.7f * pair[1].distance)
continue;
1621 source.push_back(current.keypoints[pair[0].queryIdx].pt);
1622 target.push_back(reference->keypoints[pair[0].trainIdx].pt);
1624 if (source.size() < 30)
return;
1626 const cv::Mat affine = cv::estimateAffinePartial2D(
1627 source, target, inliers, cv::RANSAC, 3, 2000, 0.995, 10);
1628 const int count = cv::countNonZero(inliers);
1629 if (affine.empty() ||
count < 30 ||
count < source.size() * 0.25 ||
1631 const double scale = std::hypot(affine.at<
double>(0, 0), affine.at<
double>(1, 0));
1632 const double angle = std::atan2(affine.at<
double>(1, 0),
1633 affine.at<
double>(0, 0)) * 180 /
pi;
1634 if (!std::isfinite(scale) || scale < 0.9 || scale > 1.1 || std::abs(angle) > 5)
return;
1635 const cv::Matx33d transform(
1636 affine.at<
double>(0, 0), affine.at<
double>(0, 1), affine.at<
double>(0, 2),
1637 affine.at<
double>(1, 0), affine.at<
double>(1, 1), affine.at<
double>(1, 2),
1639 for (
size_t index = 0; index < source.size(); ++index) {
1640 if (!inliers.at<uint8_t>(
static_cast<int>(index)))
continue;
1641 const cv::Vec3d
point = transform * cv::Vec3d(source[index].x, source[index].y, 1);
1643 errors.push_back(std::hypot(
point[0] - target[index].x,
point[1] - target[index].y));
1645 const cv::Rect support = cv::boundingRect(
supported);
1646 if (support.width < photo.
image.cols * 0.15 ||
1647 support.height < photo.
image.rows * 0.15)
return;
1648 std::sort(errors.begin(), errors.end());
1649 if (errors[errors.size() / 2] > 2)
return;
1650 const cv::Matx33d candidate = reference->to_world * transform;
1651 const cv::Vec3d candidate_center = candidate * center_pixel;
1652 const double correction = std::hypot(candidate_center[0] - predicted_center[0],
1653 candidate_center[1] - predicted_center[1]);
1654 const cv::Vec3d matched_center = transform * center_pixel;
1655 const double local_distance = std::hypot(
1656 matched_center[0] - (reference->width - 1) / 2.0,
1657 matched_center[1] - (reference->height - 1) / 2.0);
1658 if (!std::isfinite(correction) ||
1659 local_distance > std::hypot(reference->width, reference->height) * 1.25)
1661 Candidate& match = candidates[candidate_index];
1662 match.inliers =
count;
1663 match.to_world = candidate;
1664 match.evidence =
Json::object({{
"status",
"registered"}, {
"reference", reference->name},
1665 {
"inliers",
count}, {
"median_error_px", errors[errors.size() / 2]},
1666 {
"center_correction_map_px", correction}, {
"relative_scale", scale}});
1668 for (
const auto& candidate : candidates) {
1669 if (candidate.inliers <= best_inliers)
continue;
1670 best_inliers = candidate.inliers;
1671 selected = candidate.to_world;
1672 result = candidate.evidence;
1674 photo.to_world = selected;
1675 photo.update_bounds(photo.image.cols, photo.image.rows);
1676 current.to_world = photo.to_world;
1692 if (
identity(source,
true) ==
job.at(
"identity"))
return true;
1694 "pending source changed; retain original photos to resume this map");
1695 std::cout <<
"PANGEA: deferred changing photo " << source.filename()
1703 std::exception_ptr prepared_error =
nullptr) {
1704 const fs::path source =
input /
job.at(
"name").get<std::string>();
1705 check(source.parent_path() ==
input,
"invalid source path");
1710 if (prepared) photo = std::move(prepared->photo);
1711 else if (prepared_error) std::rethrow_exception(prepared_error);
1713 state.value(
"standard_profile",
false));
1716 std::string(
error.what()) !=
1717 "missing MIMIC geometry; real JPEGs require --camera-profile")
1720 std::cerr <<
"PANGEA: " <<
error.what() <<
'\n'
1721 <<
"Standard profile assumes 60-degree horizontal FOV, "
1722 "centered square pixels and nadir mounting; FlyEye "
1723 "images assume north-up, zero roll/pitch. "
1724 "Map geometry is approximate, not calibrated.\n"
1725 <<
"use standard profile Y/n: " << std::flush;
1727 if (!std::getline(std::cin, answer) ||
1728 !(answer.empty() || answer ==
"Y" || answer ==
"y"))
1730 state.set(
"standard_profile",
true);
1732 {
"horizontal_fov_deg", 60}, {
"mount",
"nadir"},
1733 {
"flyeye_roll_deg", 0}, {
"flyeye_pitch_deg", 0},
1734 {
"flyeye_yaw_deg", 0}, {
"accuracy",
"uncalibrated"}}));
1738 }
catch (
const std::exception&
error) {
1742 "pending photo no longer decodes; checkpoint retained");
1744 root /
"done" / (
job.at(
"identity").get<std::string>() +
".json"),
1745 Json::object({{
"status",
"rejected"}, {
"source", job}, {
"reason", error.what()}}));
1746 state.set(
"rejected",
state.at(
"rejected").get<
unsigned>() + 1);
1748 std::cerr <<
"PANGEA: skipped " << source.filename() <<
": "
1749 <<
error.what() <<
'\n';
1752 if (!unchanged(source,
job))
return;
1756 state.
at(
"pending").
contains(
"registered_to_world")) {
1757 const Json saved = state.
at(
"pending").
at(
"registered_to_world");
1758 const cv::Matx33d position_prior = photo.to_world;
1760 for (
int index = 0; index < 9; ++index) {
1761 photo.to_world.val[index] = saved[index].
get<
double>();
1762 check(std::isfinite(photo.to_world.val[index]),
"nonfinite saved alignment");
1764 photo.update_bounds(photo.image.cols, photo.image.rows);
1765 alignment = state.
at(
"pending").
at(
"alignment");
1766 if (photo.image.cols > 80 && photo.image.rows > 120) {
1767 if (references.size() == 8) references.erase(references.begin());
1768 auto reference = features(photo,
job.at(
"name").get<std::string>());
1769 reference.position_prior = position_prior;
1770 references.push_back(std::move(reference));
1772 }
else alignment = align(photo,
job.at(
"name").get<std::string>(),
1773 prepared ? prepared->features.get() :
nullptr);
1775 prepared->photo = photo;
1776 prepared->tiles.clear();
1783 .
include(photo.bounds, max_map_side);
1784 }
catch (
const std::exception&) {
1785 throw std::runtime_error(
1786 "map extent limit reached; resume with a larger --max-map-side "
1787 "for standalone stitching, or use a new output for another area");
1789 const uint64_t sequence = state.
at(
"sequence").
get<uint64_t>() + 1;
1790 check(sequence <=
max_photos,
"10000-photo limit reached; start a new map");
1795 state.
set(
"pending",
job);
1798 for (
double value : photo.to_world.val) transform.
push_back(value);
1800 pending.set(
"registered_to_world", transform);
1801 pending.set(
"alignment", alignment);
1810 {
"sequence", sequence},
1811 {
"corners_lon_lat", photo.geographic_corners()}});
1812 if (align_images)
completed.set(
"alignment", alignment);
1815 state.
set(
"sequence", sequence);
1816 state.
set(
"pending",
nullptr);
1819 for (
const auto& reference : references) {
1821 for (
double value : reference.to_world.val) transform.
push_back(value);
1823 {
"identity",
identity(input / reference.name)}, {
"to_world", transform}}));
1825 state.
set(
"alignment_references", saved);
1828 std::cout <<
"PANGEA: assembled " << source.filename() <<
" (#" << sequence
1836 const Json&
camera,
bool interactive,
int side_limit,
bool image_alignment)
1839 input = fs::canonical(source);
1840 check(fs::is_directory(
input),
"input must be a photo directory");
1841 fs::create_directories(
output);
1846 const std::string lock_path =
1847 "/tmp/usher-pangea-" + std::to_string(getuid()) +
".lock";
1849 O_CREAT | O_RDWR | O_NOFOLLOW | O_CLOEXEC, 0600));
1851 "another PANGEA worker is active for this user");
1853 O_CREAT | O_RDWR | O_NOFOLLOW | O_CLOEXEC, 0600));
1855 "output already has a PANGEA worker");
1856 if (fs::exists(
root /
"state.json")) {
1858 check(
state.value(
"schema",
"") ==
"PANGEA_STATE_V1" &&
1860 "checkpoint belongs to another input or format");
1862 "camera profile differs from checkpoint; use original profile or a "
1866 "alignment mode differs from checkpoint; use a new output directory");
1869 "invalid checkpoint counts");
1871 for (
const auto& entry : fs::directory_iterator(
root))
1872 check(entry.path().filename() ==
".lock",
1873 "output must be empty or a PANGEA checkpoint");
1875 {
"input",
input.string()},
1882 {
"pending",
nullptr}});
1886 fs::create_directory(
root /
"tiles");
1887 fs::create_directory(
root /
"done");
1889 const Json saved =
state.at(
"alignment_references");
1891 for (
size_t saved_index = 0; saved_index < saved.
size(); ++saved_index) {
1893 const std::string name =
record.at(
"name").get<std::string>();
1894 const fs::path source =
input / name;
1897 "alignment reference changed; retain original photos");
1899 state.value(
"standard_profile",
false));
1900 const cv::Matx33d position_prior = photo.
to_world;
1901 const Json transform =
record.at(
"to_world");
1902 check(transform.
is_array() && transform.
size() == 9,
"invalid reference transform");
1903 for (
int index = 0; index < 9; ++index) {
1904 photo.
to_world.val[index] = transform[index].
get<
double>();
1905 check(std::isfinite(photo.
to_world.val[index]),
"nonfinite reference transform");
1908 auto reference =
features(photo, name);
1909 reference.position_prior = position_prior;
1917 double exported_at =
seconds(CLOCK_MONOTONIC);
1918 const auto export_due = [&](
bool force) {
1919 if (
state.at(
"exported") !=
state.at(
"sequence") &&
1920 (force || (watch &&
seconds(CLOCK_MONOTONIC) - exported_at >= 10))) {
1924 exported_at =
seconds(CLOCK_MONOTONIC);
1927 std::map<std::string, std::pair<std::string, double>> observed;
1930 if (!
state.at(
"pending").is_null()) {
1936 check(
bool(directory),
"input directory unavailable");
1937 std::vector<Json> jobs;
1939 std::map<std::string, std::pair<std::string, double>> next_observed;
1941 while (dirent* entry = readdir(directory.get())) {
1944 if (++entries % 64 == 0)
resources.checkpoint();
1945 check(entries <= 100000,
"input directory entry limit reached");
1946 const std::string name = entry->d_name;
1947 if (fs::path(name).extension() !=
".jpg" ||
1948 name.find(
".lens.") != std::string::npos)
1950 if (!
state.value(
"camera",
Json()).is_null() &&
1952 state.at(
"camera").at(
"filename_prefix").get<std::string>(),
1955 struct stat status{};
1956 if (lstat((
input / name).c_str(), &status) != 0 ||
1957 !S_ISREG(status.st_mode) ||
1958 status.st_mtim.tv_sec <
1959 static_cast<time_t
>(
state.at(
"since").get<uint64_t>()))
1962 if (key.empty())
continue;
1963 if (fs::exists(
root /
"done" / (key +
".json")))
continue;
1965 const auto previous = observed.find(name);
1966 const double now =
seconds(CLOCK_MONOTONIC);
1967 const double stable_since =
1968 previous != observed.end() && previous->second.first == key
1969 ? previous->second.second
1971 next_observed.emplace(name, std::make_pair(key, stable_since));
1974 if (now - stable_since < 2) {
1975 if (next_observed.size() == 64)
break;
1979 jobs.push_back(
Json::object({{
"name", name}, {
"identity", key}}));
1980 if (jobs.size() == 64 || next_observed.size() == 64)
break;
1982 observed = std::move(next_observed);
1983 if (!jobs.empty()) {
1984 std::sort(jobs.begin(), jobs.end(),
1985 [](
const Json& first,
const Json& second) {
1986 return first.at(
"name") < second.at(
"name");
1989 for (
const Json&
job : jobs) {
1992 "photo record limit reached; start a new output");
1997 for (
size_t batch_start = 0; batch_start < jobs.size();) {
1998 const size_t batch_size =
2000 jobs.size() - batch_start);
2001 bool standard_profile =
state.value(
"standard_profile",
false);
2002 std::vector<std::unique_ptr<PreparedPhoto>> prepared(batch_size);
2003 std::vector<std::exception_ptr> errors(batch_size);
2005 const auto prepare_one = [&](
size_t index) {
2006 prepared[index].reset();
2007 errors[index] =
nullptr;
2009 auto decoded = std::make_unique<PreparedPhoto>();
2011 input / jobs[batch_start + index].at(
"name").get<std::string>(),
2014 else if (decoded->photo.image.cols > 80 && decoded->photo.image.rows > 120)
2015 decoded->features = std::make_unique<ImageFeatures>(
features(
2016 decoded->photo, jobs[batch_start + index].at(
"name").get<std::string>()));
2017 prepared[index] = std::move(decoded);
2019 errors[index] = std::current_exception();
2022 resources.parallel_tiles(batch_size, prepare_one);
2023 for (
size_t index = 0; index < batch_size; ++index) {
2024 const Json&
job = jobs[batch_start + index];
2026 state.at(
"rejected").get<
unsigned>() <
2028 "photo record limit reached; start a new output");
2029 if (standard_profile !=
state.value(
"standard_profile",
false)) {
2030 standard_profile =
state.value(
"standard_profile",
false);
2031 resources.parallel_tiles(batch_size - index, [&](
size_t remaining) {
2032 prepare_one(index + remaining);
2035 process(
job, prepared[index].get(), errors[index]);
2037 batch_start += batch_size;
2045 poll(
nullptr, 0, 1000);
2070 if (argc == 2 && (std::string(argv[1]) ==
"--version" || std::string(argv[1]) ==
"-v")) {
2076 const std::string command = argv[1];
2077 if (command ==
"--run" || command ==
"--check" || command ==
"--diagnostic" ||
2078 command ==
"--update-great" || command ==
"--follow-great" || command ==
"--finalize-great" ||
2079 command ==
"--stop" || command ==
"--self-test")
2082 signal(SIGINT,
stop);
2083 signal(SIGTERM,
stop);
2085 std::cout.setf(std::ios::unitbuf);
2086 if (argc == 2 && std::string(argv[1]) ==
"--check-resources") {
2088 std::cout <<
resources.report().dump() <<
'\n';
2091 fs::path input,
output, profile_path, pause_file;
2092 uint64_t reserve = 192, since = 0;
2093 int cpu_percent = 20, max_map_side = 8192;
2094 bool watch =
false, desktop =
false, cpu_option =
false, align_images =
false;
2095 for (
int index = 1; index < argc; ++index) {
2096 const std::string option = argv[index];
2097 if (option ==
"--help" || option ==
"-h") {
2099 <<
"PANGEA: Photo-Assisted Neighboring Geographic Edge Assembly\n"
2100 "Usage: pangea --input PHOTOS --output MAP [--watch] "
2101 "[--reserve-mb 192] [--cpu-percent 20]\n"
2102 " Incremental GREAT XYZ: --update-great INI, --follow-great INI, --finalize-great INI.\n"
2103 " Add --camera-profile FILE for real USHER JPEGs; no "
2104 "synthetic images or effects.\n"
2105 " --pause-file FILE defers work while FILE exists; remove "
2107 " --max-map-side N permits larger standalone maps; "
2108 "default 8192, multiples of 256 up to 65280.\n"
2109 " --desktop enables parallel, unthrottled replay on "
2110 "hosts with at least 2 GiB RAM; never for MORA.\n"
2111 " --align-images registers overlapping photos in desktop "
2112 "mode; requires a new output directory.\n"
2113 " pangea --check-resources\n"
2114 " pangea --check SURVEY.ini\n"
2115 " pangea --run SURVEY.ini [--mode simple|great]\n"
2116 " pangea --diagnostic SURVEY.ini [--mode MODE]\n"
2118#if SENSORAIA_BUILD_TESTING
2119 " pangea --self-test\n"
2121 " pangea --version\n"
2122 "Default: clean MIMIC input. Real mode requires explicit "
2123 "camera geometry. Standalone replay offers an approximate "
2124 "standard profile when MIMIC geometry is missing. "
2125 "No realtime deadline.\n";
2128 if (option ==
"--watch") {
2132 if (option ==
"--desktop") {
2136 if (option ==
"--align-images") {
2137 align_images =
true;
2140 check(index + 1 < argc,
"option needs a value");
2141 const std::string value = argv[++index];
2142 if (option ==
"--input")
2144 else if (option ==
"--output")
2146 else if (option ==
"--camera-profile")
2147 profile_path = value;
2148 else if (option ==
"--pause-file") {
2149 check(!value.empty(),
"pause file must not be empty");
2152 check(!value.empty() &&
2153 value.find_first_not_of(
"0123456789") == std::string::npos,
2154 "numeric option must be unsigned");
2155 const uint64_t parsed = std::stoull(value);
2156 if (option ==
"--reserve-mb")
2158 else if (option ==
"--max-map-side") {
2159 check(parsed >= 8192 && parsed <= 65280 && parsed %
tile_size == 0,
2160 "max-map-side must be a multiple of 256 in 8192..65280");
2161 max_map_side =
static_cast<int>(parsed);
2163 else if (option ==
"--cpu-percent") {
2164 check(parsed <= 20,
"CPU duty exceeds limit");
2165 cpu_percent = parsed;
2167 }
else if (option ==
"--since")
2169 else if (option ==
"--parent-pid") {
2170 check(parsed > 1 && parsed <= INT32_MAX,
"invalid parent PID");
2173 throw std::runtime_error(
"unknown option");
2177 "--input PHOTOS and --output MAP are required");
2179 "--desktop is standalone replay only; incompatible with --watch, "
2180 "--parent-pid and --cpu-percent");
2181 check(!align_images || desktop,
"--align-images requires --desktop");
2185 while (watch && !fs::exists(input)) {
2187 poll(
nullptr, 0, 1000);
2192 std::cout <<
resources.report().dump() <<
'\n';
2194 }
catch (
const std::exception&
error) {
2195 std::cerr <<
"PANGEA: " <<
error.what() <<
'\n';
bool usher_camera_profile_read(const char *filename, struct usher_camera_profile *profile)
Read a bounded PANGEA_CAMERA_V1 key=value calibration file.
Public C API for reading PANGEA camera calibration profiles.
uint8_t bytes[CHDK_CONTROL_REPLY_SIZE]
void(* close)(struct usbfs_handle *)
int(* open)(const struct usbfs_device *, struct usbfs_handle *)
size_t peak_workers()
Read the lifetime concurrency high-water mark.
~DesktopPool()
Wake and join the workers before destroying shared batch state.
DesktopPool(size_t count)
Start the desktop worker pool.
std::condition_variable ready
std::exception_ptr failure
void work()
Claim and execute jobs until the coordinator shuts down the pool.
std::condition_variable completed
bool cancelled() const
Check whether the current batch has failed or been interrupted.
std::vector< std::thread > workers
size_t size() const
Report the fixed worker capacity.
void run(size_t count, std::function< void(size_t)> operation, const std::function< void()> &checkpoint)
Run indexed work and wait for every active callback to finish.
std::atomic< bool > cancel
std::function< void(size_t)> task
size_t photo_batch_capacity(uint64_t photo_budget_mib=128) const
Estimate how many photos can be prepared in one desktop batch.
bool desktop() const
Check which execution policy was selected.
std::unique_ptr< DesktopPool > pool
void tile_checkpoint()
Check whether tile processing may continue.
void parallel_tiles(size_t count, std::function< void(size_t)> task)
Run a desktop batch under the coordinator's resource policy.
Resources(uint64_t reserve_mb, int cpu_percent, fs::path pause={}, bool desktop=false)
Establish mapping limits for onboard or desktop execution.
const double started_wall
bool unchanged(const fs::path &source, const Json &job)
Recheck source identity before modifying map state.
pangea_survey::StorageDescriptor process_lock
Worker(fs::path source, fs::path output, Resources &budget, uint64_t since, const Json &camera, bool interactive, int side_limit, bool image_alignment)
void process(const Json &job, PreparedPhoto *prepared=nullptr, std::exception_ptr prepared_error=nullptr)
void save()
Publish the current resume checkpoint through the atomic JSON writer.
Json align(Photo &photo, const std::string &name, ImageFeatures *cached=nullptr)
Try to refine a photo's georeferenced transform using recent photos.
ImageFeatures AlignmentReference
pangea_survey::StorageDescriptor output_lock
std::vector< AlignmentReference > references
AlignmentReference features(const Photo &photo, const std::string &name)
Extract bounded SIFT features for a possible alignment reference.
Own a working-file descriptor through success and exception paths.
Adapter errors exposed independently of OpenCV's exception type.
C++ value interface with explicit conversions and OpenCV JSON I/O.
static Value parse(Iterator first, Iterator last)
Copy a byte range and delegate parsing to the string overload.
Type get() const
Convert explicitly while preserving the distinction between types.
Value at(const char *key) const
Require an object member instead of substituting null.
void push_back(const Value &value)
Clone an element, append it in order and select array kind.
bool is_array() const
Inspect the array tag.
void set(const std::string &key, const Value &value)
Clone a member value, replace any matching key and select object kind.
size_t size() const
Count container entries, not string bytes or scalar payload size.
bool is_number() const
Test all three numeric categories without treating bool as numeric.
std::string dump(int indent=-1, char=' ', bool=false) const
Serialize with OpenCV and restore native JSON scalar tokens.
Type value(const char *key, Type fallback) const
Use a default only for absence, never to hide an invalid present value.
static Value array()
Make an empty array.
static Value object()
Make an empty object.
bool is_null() const
Inspect the null tag.
bool contains(const char *key) const
Check member presence without interpreting a null value as missing.
bool is_object() const
Inspect the object tag.
RAII guard installing the backend at startup and joining workers before main() exits.
static bool bounds(const struct coverage_state *state, const struct coverage_candidate *candidate, struct coverage_bounds *box)
Reject unusable polygons before converting coordinates or scanning cells.
static bool start(uint32_t session)
Admit a nonzero session and discard correlation from the previous one.
static int record(const union dc_shot_union *shot)
Retrieve a point and admit a new observation only when its credit is free.
static struct ear_inference_result queued_evidence completed
static bool supported(enum exif_directory directory, uint16_t tag)
Select standard fields retained by the default import policy.
Bounded C11 EXIF metadata and JPEG streaming interfaces.
bool exif_value_unsigned(const struct exif_value *value, size_t index, uint32_t *result)
Decode one BYTE, SHORT or LONG component without an unaligned load.
int exif_parse(struct exif_view *view, const unsigned char *payload, size_t size)
Validate an EXIF APP1 payload and create a borrowed view.
exif_directory
Directory identities, not on-disk tag numbers or offsets.
bool exif_get(const struct exif_view *view, enum exif_directory directory, uint16_t tag, struct exif_value *value)
Look up a directory-local tag without copying its bytes.
#define EXIF_MAX_PAYLOAD
APP1 payload ceiling: the JPEG length includes its own two bytes.
Decode JPEGs with IJG scanlines into BGRA previews or disk-backed rasters.
struct jpeg_compress_struct encoder
uint8_t marker[LWIR_THERMAL_HEADER_BYTES+LWIR_THERMAL_CHUNK_CAPACITY]
uint8_t row[LWIR_MAX_WIDTH *3U]
struct jpeg_error_mgr error
struct jpeg_decompress_struct decoder
static uint8_t combined[256U *192U *4U]
Integrity checks and bounded disk-backed intermediates for surveys.
cv::Matx33d rotation(double roll, double pitch, double yaw)
Json camera_profile(const fs::path &path)
constexpr uint64_t memory_limit
void blend_tile(const Photo &photo, const fs::path &root, uint64_t sequence, Resources &resources, int tile_column, int tile_row)
Photo read_photo(const fs::path &path, Resources &resources, const Json &camera, bool standard_profile=false, bool parallel_decode=false)
Admit and decode one source JPEG into a projection-ready photo.
usher_cv_json::Value Json
void photo_check(bool condition, const char *message)
void write_json(const fs::path &path, const Json &value)
std::atomic< sig_atomic_t > stopping
constexpr double projected_pixel_m
std::unique_ptr< FILE, FileCloser > File
fs::path tile_path(const fs::path &root, int column, int row)
void jpeg_message(j_common_ptr state, int level)
Screen scan diagnostics before dispatching to libjpeg's usual handler.
void blend(const Photo &photo, const fs::path &root, uint64_t sequence, Resources &resources)
Apply one photo to the tiles enclosed by its footprint.
void blend_prepared(const PreparedPhoto &prepared, const fs::path &root, uint64_t sequence, Resources &resources)
Commit a prepared photo's contributions to independent tile files.
double seconds(clockid_t clock)
uint64_t proc_kib(const char *path, const char *wanted)
std::unique_ptr< DIR, DirectoryCloser > Directory
std::string digest(const void *data, size_t size)
void jpeg_failure(j_common_ptr state)
Route a libjpeg fatal callback to the active recovery point.
std::string identity(const fs::path &path, bool allow_missing=false)
void prepare_projection(PreparedPhoto &prepared, Resources &resources)
Stage projected colour and weights without modifying durable tiles.
void export_map(const fs::path &root, const Json &state, Resources &resources, int max_map_side)
void sync_directory(const fs::path &path)
void disk_headroom(const fs::path &root, uint64_t required)
Json read_json(const fs::path &path)
constexpr double world_pixels
void check(bool condition, const char *message)
double numeric(const Json &values, const char *key, double minimum, double maximum)
std::string exif_text(const exif_view *metadata, exif_directory directory, uint16_t tag)
File regular_file(const fs::path &path, uint64_t maximum)
void validateJpegScanMessage(j_common_ptr state)
Apply the shared JPEG marker bounds to the current decoder state.
Header-only C++17 value adapter around OpenCV's JSON persistence layer.
Header-only C++17 cv::parallel::ParallelForAPI backend for Sensoraia.
int main(int argc, char **argv)
Dispatch PANGEA survey commands or run incremental photo mapping.
int pangeaSurveyMain(int argc, char **argv)
static const char header[]
void sha256_bytes(const void *data, size_t length, void *output)
Hash one memory region to its standard 32-byte binary digest.
Allocation-free, portable SHA-256 checksums for memory and streams.
#define PANGEA_BUILD_VERSION
Measured pinhole calibration for the delivered image, including its mount.
Borrowed bytes plus enough type information for safe interpretation.
const unsigned char * data
Borrowed view of a validated APP1 payload.
AtomicFile(const fs::path &destination)
void write(const void *data, size_t length)
void text(const std::string &value)
Append serialized text without adding a terminator or newline.
~AtomicFile()
Remove abandoned staging data without removing committed output.
static Bounds from(const Json &value, int max_map_side=8192)
Bounds include(const Bounds &other, int max_map_side=8192) const
Extend the committed tile envelope without changing either input.
~Decoder()
Release codec allocations only if creation succeeded.
jpeg_decompress_struct state
void operator()(DIR *directory) const
Release an owned, non-null directory stream.
jpeg_compress_struct state
~Encoder()
Release initialized compressor state on success or recovery.
void operator()(FILE *file) const
Release an owned, non-null file stream.
cv::Matx33d position_prior
std::vector< cv::KeyPoint > keypoints
void(* emit)(j_common_ptr, int)
Json geographic_corners() const
void geometry(const exif_view *metadata, int width, int height, const Json &camera, bool standard_profile)
std::array< cv::Point2f, 4 > corners(const cv::Matx33d &transform) const
void update_bounds(int width, int height)
Recompute the tile footprint after setting the image homography.
std::unique_ptr< ImageFeatures > features
std::vector< ProjectedTile > tiles
void save(const fs::path &path)
void load(const fs::path &path)
One timestamped geographic position for a replay annotation.
One measured camera calibration loaded from the flat PANGEA profile format.
struct coverage_camera calibration
static std::unique_ptr< pangea_survey::MapResources > resources
static struct camera_backend camera
struct usher_control_result result