Illustrated C/C++ source sheets, symbol index, API relationship graph, compiler console and debug trace

LIVE AI / FLEXIBLE SENSING / FLIGHT CONTEXT

Sensoraia API

Live AI and sensing, connected to the flight.
See, listen, inspect and return supported results while airborne.

Loading...
Searching...
No Matches
ortho_map.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2026 OpenUAS
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/**
19 * @file ortho_map.cpp
20 * @brief Load fixed-session photos and render Simple/Great map pixels.
21 * @details Great computes seam ownership once from a bounded global overview,
22 * then reuses that plan for every output tile. Global decisions avoid seams
23 * changing at tile borders; full-resolution footprint fallback preserves
24 * narrow source coverage that a coarse overview might miss.
25 */
26#include "ortho_map.h"
27#include "jpeg_encoder.h"
28#include "parallel_rows.h"
29#include "jpeg_stream.h"
30#include "usher_sources.h"
31#include <opencv2/core.hpp>
32#include <opencv2/imgproc.hpp>
33#include <opencv2/calib3d.hpp>
34#include <opencv2/stitching/detail/seam_finders.hpp>
35#include <opencv2/stitching/detail/exposure_compensate.hpp>
36#include <opencv2/stitching/detail/blenders.hpp>
37#include <array>
38#include <cstdint>
39#include <iomanip>
40#include <iostream>
41#include <limits>
42#include <numeric>
43#include <thread>
44#include <atomic>
45#include <mutex>
46#include <chrono>
47#if defined(__GLIBC__)
48#include <malloc.h>
49#endif
50
51namespace pangea_survey {
52namespace {
53using fsPath = std::filesystem::path;
54std::vector<std::string> csvFields(const std::string& line) {
55 std::vector<std::string> result;
56 std::string value;
57 bool quoted = false, closed = false;
58 for (std::size_t i = 0; i < line.size(); ++i) {
59 const char c = line[i];
60 if (quoted) {
61 if (c == '"' && i + 1 < line.size() && line[i + 1] == '"') { value += '"'; ++i; }
62 else if (c == '"') { quoted = false; closed = true; }
63 else value += c;
64 } else if (c == ',') { result.push_back(value); value.clear(); closed = false; }
65 else if (c == '"' && value.empty() && !closed) quoted = true;
66 else if (closed || c == '"') throw std::runtime_error("Malformed source CSV quoting");
67 else if (c != '\r') value += c;
68 }
69 if (quoted) throw std::runtime_error("Unclosed source CSV quote");
70 result.push_back(value);
71 return result;
72}
73std::vector<OrthoSource> readSources(const fsPath& path, const fsPath& sourceDirectory = {}) {
74 std::ifstream input(path);
75 if (!input) throw std::runtime_error("Cannot open orthorectification source CSV: " + path.u8string());
76 std::string line;
77 if (!std::getline(input, line)) throw std::runtime_error("Empty source CSV");
78 const auto header = csvFields(line);
79 std::map<std::string, std::size_t> columns;
80 for (std::size_t i = 0; i < header.size(); ++i)
81 if (!columns.emplace(header[i], i).second) throw std::runtime_error("Duplicate source CSV column");
82 const bool haveUtc = columns.count("capture_utc") != 0;
83 for (const auto& group : {std::vector<const char*>{"fx_px", "fy_px", "cx_px", "cy_px"},
84 std::vector<const char*>{"lens_k1", "lens_k2", "lens_blue_scale", "lens_red_scale"}}) {
85 const auto present = std::count_if(group.begin(), group.end(),
86 [&](const char* key) { return columns.count(key) != 0; });
87 if (present != 0 && present != static_cast<std::ptrdiff_t>(group.size()))
88 throw std::runtime_error("Source CSV has incomplete optical calibration columns");
89 }
90 if ((columns.count("utc_absolute_seconds") != 0) != haveUtc ||
91 (columns.count("utc_read_qpc_100ns") != 0) != haveUtc)
92 throw std::runtime_error("Source CSV has incomplete capture UTC columns");
93 if ((columns.count("tile_row") != 0) != (columns.count("tile_column") != 0))
94 throw std::runtime_error("Source CSV has incomplete capture grid columns");
95 std::vector<OrthoSource> sources;
96 std::map<fsPath, bool> paths;
97 // One fixed camera/altitude/time session keeps the intrinsic and map scale valid for every row.
98 while (std::getline(input, line)) {
99 const auto fields = csvFields(line);
100 if (fields.size() != header.size()) throw std::runtime_error("Truncated/malformed source CSV row");
101 const auto text = [&](const char* key) -> const std::string& {
102 const auto it = columns.find(key);
103 if (it == columns.end()) throw std::runtime_error(std::string("Source CSV lacks ") + key);
104 return fields[it->second];
105 };
106 const auto get = [&](const char* key) { return number(text(key), key); };
107 if (text("trigger") != "box_tile") continue;
108 const fsPath name = std::filesystem::u8path(text("filename"));
109 if (name.empty() || name.has_parent_path() || name.extension() != ".jpg")
110 throw std::runtime_error("Source CSV filename must be a local JPEG basename");
111 OrthoSource s;
112 s.path = (sourceDirectory.empty() ? path.parent_path() : sourceDirectory) / name;
113 if (!paths.emplace(s.path, true).second) throw std::runtime_error("Duplicate source photo");
114 s.latitude = get("observed_latitude"); s.longitude = get("observed_longitude");
115 s.altitude = get("observed_altitude_m");
116 s.pitch = get("observed_pitch_deg"); s.bank = get("observed_bank_deg");
117 s.heading = get("observed_heading_deg"); s.fov = get("sdk_fov_deg");
118 range(s.latitude, -80, 84, "source latitude"); range(s.longitude, -180, 180, "source longitude");
119 range(s.fov, 10, 120, "source vertical FOV");
120 if (text("projection") != "perspective" || !text("map_epsg").empty() ||
121 std::abs(s.pitch - 90) > 0.00002 || std::abs(std::remainder(s.heading - s.bank, 360.0)) > 0.001 ||
122 get("commanded_pitch_deg") != 90 || get("commanded_bank_deg") != 0 ||
123 get("commanded_heading_deg") != 0)
124 throw std::runtime_error("Orthorectification requires unrotated, confirmed true-north nadir source photos");
125 const auto integer = [&](const char* key, double low, double high) {
126 const double n = get(key); range(n, low, high, key);
127 if (std::floor(n) != n) throw std::runtime_error(std::string("Noninteger ") + key);
128 return n;
129 };
130 s.width = static_cast<int>(integer("width", 64, 8192));
131 s.height = static_cast<int>(integer("height", 64, 8192));
132 if (columns.count("tile_row"))
133 s.captureCell = std::make_pair(static_cast<int>(integer("tile_row", 0, 100000)),
134 static_cast<int>(integer("tile_column", 0, 100000)));
135 const double sw = integer("source_width", 64, 16384);
136 const double sh = integer("source_height", 64, 16384);
137 const double cw = get("crop_width"), ch = get("crop_height");
138 const double x = get("crop_x"), y = get("crop_y");
139 if (cw <= 0 || ch <= 0 || x < 0 || y < 0 || x + cw > sw + 1e-6 || y + ch > sh + 1e-6 ||
140 std::abs(cw / s.width - ch / s.height) > 1e-6)
141 throw std::runtime_error("Invalid source crop/intrinsics");
142 s.focal = sh / (2 * std::tan(s.fov * pi / 360)) * s.width / cw;
143 s.cx = (sw / 2 - x) * s.width / cw - 0.5;
144 s.cy = (sh / 2 - y) * s.height / ch - 0.5;
145 if (columns.count("lens_profile")) {
146 const auto& profile = text("lens_profile");
147 if (profile != "simconnect" && profile != "raspberry_pi_ai")
148 throw std::runtime_error("Unsupported source lens profile");
149 s.distorted = profile == "raspberry_pi_ai";
150 }
151 if (columns.count("start_ground_m") && !text("start_ground_m").empty())
152 s.referencePlane = get("start_ground_m");
153 if (columns.count("fx_px")) {
154 const double fx = get("fx_px"), fy = get("fy_px");
155 if (std::abs(fx - s.focal) > .01 || std::abs(fy - fx) > .01 ||
156 std::abs(get("cx_px") - s.cx) > .01 || std::abs(get("cy_px") - s.cy) > .01)
157 throw std::runtime_error("Source saved-image intrinsics disagree with capture framing");
158 }
159 if (columns.count("lens_k1")) {
160 s.lens = {get("lens_k1"), get("lens_k2"), get("lens_blue_scale"), get("lens_red_scale")};
161 range(s.lens.k1, -.05, 0, "source lens_k1"); range(s.lens.k2, 0, .01, "source lens_k2");
162 range(s.lens.blueScale, .999, 1.001, "source lens_blue_scale");
163 range(s.lens.redScale, .999, 1.001, "source lens_red_scale");
164 if (!s.distorted && (s.lens.k1 != 0 || s.lens.k2 != 0 ||
165 s.lens.blueScale != 1 || s.lens.redScale != 1))
166 throw std::runtime_error("Rectilinear source has nonidentity lens effects");
167 }
168 const auto timestamp = [&](const char* key) {
169 const auto n = integer(key, 1, 9e15);
170 return static_cast<long long>(n);
171 };
172 s.poseTime = timestamp("pose_received_qpc_100ns"); s.frameTime = timestamp("frame_qpc_100ns");
173 const auto requested = timestamp("request_qpc_100ns");
174 if (s.poseTime > s.frameTime || requested > s.frameTime || s.frameTime - s.poseTime > 10000000)
175 throw std::runtime_error("Source camera/frame timestamps are missing, stale or reversed");
176 if (haveUtc && !text("capture_utc").empty()) {
177 s.captureUtc = parseUtcDateTime(text("capture_utc"));
178 s.utcAbsoluteSeconds = get("utc_absolute_seconds");
179 s.utcRead100ns = timestamp("utc_read_qpc_100ns");
180 if (!captureUtcSampleValid(*s.captureUtc, s.utcAbsoluteSeconds, s.utcRead100ns, s.frameTime))
181 throw std::runtime_error("Source capture UTC readback is stale, reversed or outside the locked second");
182 }
183 if (haveUtc && !s.captureUtc &&
184 (!text("utc_absolute_seconds").empty() || !text("utc_read_qpc_100ns").empty()))
185 throw std::runtime_error("Source capture UTC evidence lacks its target date/time");
186 if (!std::filesystem::is_regular_file(s.path)) throw std::runtime_error("Missing source JPEG: " + s.path.u8string());
187 if (!sources.empty() && (std::abs(s.altitude - sources[0].altitude) > 0.01 ||
188 s.width != sources[0].width || s.height != sources[0].height ||
189 std::abs(s.focal / sources[0].focal - 1) > 1e-6 ||
190 std::abs(s.cx - sources[0].cx) > .01 || std::abs(s.cy - sources[0].cy) > .01 ||
191 s.distorted != sources[0].distorted ||
192 (s.distorted && (s.lens.k1 != sources[0].lens.k1 || s.lens.k2 != sources[0].lens.k2 ||
193 s.lens.blueScale != sources[0].lens.blueScale || s.lens.redScale != sources[0].lens.redScale))))
194 throw std::runtime_error("Source set changed altitude, lens or viewport; use one fixed-MSL capture session");
195 if (!sources.empty() && (s.captureUtc.has_value() != sources[0].captureUtc.has_value() ||
196 (s.captureUtc && s.captureUtc->text != sources[0].captureUtc->text)))
197 throw std::runtime_error("Source set changed capture UTC; use one fixed-time capture session");
198 sources.push_back(s);
199 }
200 if (input.bad()) throw std::runtime_error("Cannot read source CSV");
201 if (sources.empty()) throw std::runtime_error("No box photographs in source CSV");
202 return sources;
203}
204struct RectificationCache {
205 std::array<double, 9> calibration{};
206 std::array<cv::Mat, 4> mapX, mapY;
207 std::array<int, 4> inset{};
208
209 bool prepare(const OrthoSource& source, std::size_t pixels) {
210 constexpr std::size_t maximumBytes = 96 * 1024 * 1024;
211 if (pixels > maximumBytes / (8 * sizeof(float))) {
212 *this = {};
213 return false;
214 }
215 const std::array<double, 9> current{double(source.width), double(source.height),
216 source.focal, source.cx, source.cy, source.lens.k1, source.lens.k2,
217 source.lens.blueScale, source.lens.redScale};
218 if (current != calibration) {
219 *this = {};
220 calibration = current;
221 }
222 return true;
223 }
224};
225
226struct Photo {
227 OrthoSource info;
228 int validInset = 0;
229 double east = 0, north = 0, c = 1, s = 0, scale = 1;
230 std::vector<unsigned char> bgra;
231 fsPath raster;
232 void load(int maxSide = 0, RectificationCache* rectification = nullptr) {
233 JpegStream decoder;
234 auto image = decoder.read(info.path, info.width, info.height, info.distorted ? 0 : maxSide);
235 info.width = image.width; info.height = image.height;
236 info.focal *= image.scale;
237 info.cx = (info.cx + .5) * image.scale - .5;
238 info.cy = (info.cy + .5) * image.scale - .5;
239 const auto count = static_cast<std::size_t>(info.width) * info.height;
240 bgra = std::move(image.pixels);
241 if (info.distorted) {
242 if (rectification && !rectification->prepare(info, count)) rectification = nullptr;
243 cv::Mat input(info.height, info.width, CV_8UC4, bgra.data());
244 std::vector<cv::Mat> channels;
245 cv::split(input, channels);
246 const double scales[] = {info.lens.blueScale, 1, info.lens.redScale, 1};
247 const cv::Matx33d ideal(info.focal, 0, info.cx, 0, info.focal, info.cy, 0, 0, 1);
248 for (int channel = 0; channel < 4; ++channel) {
249 const double focal = info.focal * scales[channel];
250 const cv::Matx33d sensor(focal, 0, info.cx, 0, focal, info.cy, 0, 0, 1);
251 cv::Mat mx, my, corrected;
252 int channelInset = 0;
253 if (rectification) {
254 if (rectification->mapX[channel].empty()) {
255 for (int previous = 0; previous < channel; ++previous) {
256 if (scales[previous] == scales[channel]) {
257 rectification->mapX[channel] = rectification->mapX[previous];
258 rectification->mapY[channel] = rectification->mapY[previous];
259 rectification->inset[channel] = rectification->inset[previous];
260 break;
261 }
262 }
263 }
264 mx = rectification->mapX[channel]; my = rectification->mapY[channel];
265 channelInset = rectification->inset[channel];
266 }
267 if (mx.empty()) {
268 cv::initUndistortRectifyMap(sensor, cv::Vec<double, 5>(info.lens.k1, info.lens.k2, 0, 0, 0),
269 cv::noArray(), ideal, cv::Size(info.width, info.height), CV_32FC1, mx, my);
270 for (int y = 0; y < info.height; ++y) for (int x = 0; x < info.width; ++x) {
271 const double u = mx.at<float>(y, x), v = my.at<float>(y, x);
272 if (!std::isfinite(u) || !std::isfinite(v))
273 throw std::runtime_error("Nonfinite rectified camera ray");
274 if (u < 0 || v < 0 || u > info.width - 1 || v > info.height - 1)
275 channelInset = std::max(channelInset,
276 std::min({x, y, info.width - 1 - x, info.height - 1 - y}) + 1);
277 }
278 if (rectification) {
279#if defined(__x86_64__)
280 cv::convertMaps(mx, my, rectification->mapX[channel], rectification->mapY[channel],
281 CV_16SC2, false);
282 mx = rectification->mapX[channel]; my = rectification->mapY[channel];
283#else
284 rectification->mapX[channel] = mx; rectification->mapY[channel] = my;
285#endif
286 rectification->inset[channel] = channelInset;
287 }
288 }
289 validInset = std::max(validInset, channelInset);
290 cv::remap(channels[channel], corrected, mx, my, cv::INTER_LINEAR, cv::BORDER_CONSTANT);
291 channels[channel] = corrected;
292 }
293 cv::Mat corrected;
294 cv::merge(channels, corrected);
295 if (!corrected.isContinuous() || corrected.total() * corrected.elemSize() != bgra.size())
296 throw std::runtime_error("Rectified photo storage differs from the decoded image");
297 for (std::size_t offset = 0; offset < bgra.size(); ++offset)
298 bgra[offset] = corrected.data[offset];
299 if (2 * validInset > std::min(info.width, info.height) - 64)
300 throw std::runtime_error("Lens rectification leaves too few fully supported image pixels");
301 }
302 }
303 bool project(double e, double n, double z, double& x, double& y, int border = 5) const {
304 border += validInset;
305 const double depth = info.altitude - z;
306 if (depth <= 5) return false;
307 const double de = (e - east) / scale, dn = (n - north) / scale;
308 if (info.camera) {
309 const auto pixel = info.camera->groundToPixel(depth) * cv::Vec3d(c * de + s * dn, -s * de + c * dn, 1);
310 if (pixel[2] <= 1e-9) return false;
311 x = pixel[0] / pixel[2]; y = pixel[1] / pixel[2];
312 return std::isfinite(x) && std::isfinite(y) && x >= border && y >= border &&
313 x < info.width - border - 1 && y < info.height - border - 1;
314 }
315 x = info.cx + info.focal * (c * de + s * dn) / depth;
316 y = info.cy - info.focal * (-s * de + c * dn) / depth;
317 return x >= border && y >= border && x < info.width - border - 1 && y < info.height - border - 1;
318 }
319 void color(double x, double y, unsigned char* pixel) const {
320 const int ix = static_cast<int>(x), iy = static_cast<int>(y);
321 const double dx = x - ix, dy = y - iy;
322 const auto i = (static_cast<std::size_t>(iy) * info.width + ix) * 4;
323 for (std::size_t k = 0; k < 3; ++k)
324 pixel[k] = static_cast<unsigned char>(std::clamp(std::lround((1 - dy) * ((1 - dx) * bgra[i + k] +
325 dx * bgra[i + k + 4]) + dy * ((1 - dx) * bgra[i + info.width * 4 + k] +
326 dx * bgra[i + info.width * 4 + k + 4])), 0L, 255L));
327 pixel[3] = 255;
328 }
329};
330using Photos = std::vector<std::shared_ptr<Photo>>;
331
332}
333
335 validateJpegSize(config.jpegWidth, config.jpegHeight);
336 if (config.jpegWidth > 4096 || config.jpegHeight > 4096)
337 throw std::runtime_error("Orthorectified tile dimensions must be at most 4096");
338 range(config.mapPixelSize, .05, 2, "map_pixel_size_m");
339 const MapProjection projection((config.start.lat + config.end.lat) / 2,
340 (config.start.lon + config.end.lon) / 2);
341 const auto corner = projection.forward(config.start.lat, config.start.lon);
342 OrthoLayout result{corner.first, corner.second, corner.first, corner.second, 0, 0, projection.epsg()};
343 for (const auto lat : {config.start.lat, config.end.lat}) for (const auto lon : {config.start.lon, config.end.lon}) {
344 const auto p = projection.forward(lat, lon);
345 result.west = std::min(result.west, p.first); result.east = std::max(result.east, p.first);
346 result.north = std::max(result.north, p.second); result.south = std::min(result.south, p.second);
347 }
348 const double columns = std::ceil((result.east - result.west) / (config.jpegWidth * config.mapPixelSize));
349 const double rows = std::ceil((result.north - result.south) / (config.jpegHeight * config.mapPixelSize));
350 if (!std::isfinite(columns) || !std::isfinite(rows) || columns < 1 || rows < 1 || columns * rows > 100000)
351 throw std::runtime_error("Invalid/excessive orthophoto tile count");
352 result.columns = static_cast<std::size_t>(columns); result.rows = static_cast<std::size_t>(rows);
353 result.east = result.west + columns * config.jpegWidth * config.mapPixelSize;
354 result.south = result.north - rows * config.jpegHeight * config.mapPixelSize;
355 return result;
356}
357
358/** @brief Fill unsupported areas using a global push-pull colour pyramid.
359 * @param premultiplied Four-channel float overview with colour weighted by alpha.
360 * @param workers Worker count for row processing.
361 * @param stopped Cooperative cancellation predicate.
362 * @return Completed four-channel float overview with alpha set to one.
363 * @details Downsample weighted colour and support together; pull coarser
364 * estimates into gaps without introducing tile-local fill boundaries.
365 * @throws std::runtime_error On invalid input, absent support, or cancellation. */
366static cv::Mat completeMosaicPyramid(const cv::Mat& premultiplied, unsigned workers,
367 const std::function<bool()>& stopped) {
368 if (premultiplied.empty() || premultiplied.type() != CV_32FC4)
369 throw std::runtime_error("Mosaic completion requires premultiplied float colour/alpha");
370 // Push/pull from one global pyramid so completion crosses source gaps, not tile boundaries.
371 std::vector<cv::Mat> levels{premultiplied.clone()};
372 while (levels.back().cols > 1 || levels.back().rows > 1) {
373 if (stopped()) throw std::runtime_error("Mosaic completion canceled");
374 cv::Mat reduced;
375 cv::pyrDown(levels.back(), reduced);
376 levels.push_back(std::move(reduced));
377 }
378 auto& root = levels.back().at<cv::Vec4f>(0, 0);
379 if (!std::isfinite(root[3]) || root[3] <= 0)
380 throw std::runtime_error("Cannot complete a mosaic without any source colour");
381 for (int channel = 0; channel < 3; ++channel) root[channel] /= root[3];
382 root[3] = 1;
383 for (std::size_t level = levels.size() - 1; level > 0; --level) {
384 cv::Mat expanded;
385 cv::pyrUp(levels[level], expanded, levels[level - 1].size());
386 auto& destination = levels[level - 1];
387 parallelRows(destination.rows, [&](int row) {
388 if (stopped()) throw std::runtime_error("Mosaic completion canceled");
389 auto* values = destination.ptr<cv::Vec4f>(row);
390 const auto* coarse = expanded.ptr<cv::Vec4f>(row);
391 for (int column = 0; column < destination.cols; ++column) {
392 const float alpha = std::clamp(values[column][3], 0.0f, 1.0f);
393 for (int channel = 0; channel < 3; ++channel)
394 values[column][channel] += (1 - alpha) * coarse[column][channel];
395 values[column][3] = 1;
396 }
397 }, workers);
398 }
399 return levels.front();
400}
401
405 std::vector<OrthoSource> sources;
406 double west = 0, north = 0;
407 std::size_t cols = 0, rows = 0;
408 struct CachedBlock { OrthoImage image; std::size_t touched = 0; };
409 std::map<std::pair<std::size_t, std::size_t>, CachedBlock> blocks;
410 std::size_t blockClock = 0;
411 struct SourcePhoto { std::shared_ptr<Photo> photo; std::size_t touched; };
412 std::map<std::size_t, SourcePhoto> sourcePhotos;
413 std::size_t sourcePhotoBytes = 0, sourcePhotoClock = 0;
414 std::shared_ptr<Photo> sourcePhoto(std::size_t index);
415 struct MosaicView {
416 std::size_t source;
417 cv::Rect rectangle;
418 cv::Mat seam;
419 double gain = 1;
420 cv::Rect footprint;
421 };
422 struct CachedPhoto { Photo photo; std::size_t touched = 0; };
423 std::vector<MosaicView> mosaicViews;
424 std::map<std::size_t, CachedPhoto> mosaicPhotos;
425 std::size_t photoClock = 0, photoBytes = 0;
426 int seamScale = 0;
428 std::vector<std::filesystem::path> sourceRasters;
429 bool mosaicPrepared = false, diskSourcesPrepared = false;
430 Photo& mosaicPhoto(std::size_t index);
431 void prepareDiskSources(const std::function<bool()>& stopped);
432 cv::Rect mosaicRectangle(const OrthoSource& source, int scale, bool seamPlan = false) const;
433 cv::Matx33d mosaicWarp(const Photo& photo, const cv::Rect& rectangle, int scale, cv::Mat& image, cv::Mat& mask) const;
434 void prepareMosaic(const std::function<bool()>& stopped);
435 void prepareMosaicCompletion(const std::vector<cv::UMat>& images, const std::vector<cv::UMat>& originalMasks,
436 const std::vector<std::size_t>& seamViews, const std::function<bool()>& stopped);
437 OrthoImage renderMosaicRegion(const MapTile& map, int width, int height, const std::function<bool()>& stopped);
438 OrthoImage renderRegion(const MapTile& map, int width, int height, const std::function<bool()>& stopped);
439 /** @brief Admit a source set and establish the shared projection and grid.
440 * @param c Requested configuration.
441 * @param csv Source manifest used by the selected input path.
442 * @details Reconcile the manifest count and choose a reference plane before
443 * rendering; camera heights must leave clearance above that plane.
444 * @throws std::runtime_error On invalid input, engine, or layout. */
445 Impl(const MapConfig& c, const std::filesystem::path& csv) : config(c),
446 projection((c.start.lat + c.end.lat) / 2, (c.start.lon + c.end.lon) / 2),
447 sources(c.photoDirectory.empty() ? readSources(csv, c.sourceDirectory) : readUsherSources(c, csv)) {
448 if (c.expectedSaved && sources.size() != *c.expectedSaved)
449 throw std::runtime_error("Capture manifest saved count disagrees with source CSV box photographs");
450 const bool mosaic = c.orthoEngine == "mosaic";
451 if (c.orthoEngine != "simple" && !mosaic)
452 throw std::runtime_error("Unsupported orthomap engine");
453 if (mosaic) config.referencePlane = c.mosaicReferencePlane;
454 if ((config.orthoEngine == "simple" || mosaic) && !std::isfinite(config.referencePlane))
455 config.referencePlane = sources[0].referencePlane;
456 if ((config.orthoEngine == "simple" || mosaic) && (!std::isfinite(config.referencePlane) ||
457 config.referencePlane >= sources[0].altitude - 5))
458 throw std::runtime_error("Mosaicking requires a valid capture reference plane or the mode's reference_plane_amsl_m");
459 const auto layout = orthoLayout(c);
460 west = layout.west; north = layout.north; cols = layout.columns; rows = layout.rows;
461 }
462};
463Orthorectifier::Orthorectifier(const MapConfig& config, const std::filesystem::path& csv)
464 : impl_(std::make_unique<Impl>(config, csv)) {}
466std::size_t Orthorectifier::columns() const { return impl_->cols; }
467std::size_t Orthorectifier::rows() const { return impl_->rows; }
468std::size_t Orthorectifier::size() const { return columns() * rows(); }
469std::optional<UtcDateTime> Orthorectifier::captureUtc() const { return impl_->sources.front().captureUtc; }
470std::size_t Orthorectifier::sourceCount() const { return impl_->sources.size(); }
471std::string Orthorectifier::sourceFingerprint(const std::function<bool()>& stopped) const {
472 std::ostringstream identity;
473 for (const auto& source : impl_->sources)
474 identity << std::filesystem::canonical(source.path).generic_string() << '\n'
475 << fileDigest(source.path, stopped) << '\n';
476 const auto text = identity.str();
477 return dataDigest(text.data(), text.size());
478}
479
480/** @brief Get a pinned decoded photo for the simple renderer.
481 * @param index Index in the admitted source list.
482 * @return Shared ownership of the decoded, projection-ready photo.
483 * @details Evict least-recently-used entries only when the cache is their sole
484 * owner; active renders must retain their pixel storage.
485 * @throws std::runtime_error If the decoded-photo budget cannot be met. */
486std::shared_ptr<Photo> Orthorectifier::Impl::sourcePhoto(std::size_t index) {
487 auto& cache = sourcePhotos;
488 auto found = cache.find(index);
489 if (found == cache.end()) {
490 const auto& source = sources.at(index);
491 const auto bytes = static_cast<std::size_t>(source.width) * source.height * 4;
492 const std::size_t budget = std::size_t{512} * 1024 * 1024;
493 if (bytes > budget) throw std::runtime_error("Source photo exceeds decoded-photo cache budget");
494 while (sourcePhotoBytes + bytes > budget) {
495 auto oldest = cache.end();
496 for (auto candidate = cache.begin(); candidate != cache.end(); ++candidate)
497 if (candidate->second.photo.use_count() == 1 &&
498 (oldest == cache.end() || candidate->second.touched < oldest->second.touched))
499 oldest = candidate;
500 if (oldest == cache.end()) throw std::runtime_error("Active source photos exceed decoded-photo cache budget");
501 const auto& photo = *oldest->second.photo;
502 sourcePhotoBytes -= photo.bgra.size();
503 cache.erase(oldest);
504 }
505 auto photo = std::make_shared<Photo>();
506 photo->info = source;
507 const auto coordinate = projection.forward(source.latitude, source.longitude);
508 photo->east = coordinate.first; photo->north = coordinate.second;
509 const double heading = projection.convergence(source.latitude, source.longitude) * pi / 180;
510 photo->c = std::cos(heading); photo->s = std::sin(heading);
511 photo->scale = projection.scale(source.latitude, source.longitude);
512 photo->load();
513 sourcePhotoBytes += photo->bgra.size();
514 found = cache.emplace(index, SourcePhoto{std::move(photo), 0}).first;
515 }
516 found->second.touched = ++sourcePhotoClock;
517 return found->second.photo;
518}
519
520/** @brief Prepare disk-backed rasters for low-memory rendering.
521 * @param stopped Cooperative cancellation predicate.
522 * @details Identify each raster with its input digest so reusable prepared
523 * data cannot quietly stand in for changed source imagery.
524 * @throws std::runtime_error On cancellation or invalid source data or storage. */
525void Orthorectifier::Impl::prepareDiskSources(const std::function<bool()>& stopped) {
526 if (diskSourcesPrepared) return;
527 if (config.workingDirectory.empty()) throw std::runtime_error("Low-memory processing requires a working directory");
528 const auto directory = config.preparedSourceDirectory.empty() ? config.workingDirectory / "sources" :
529 config.preparedSourceDirectory;
530 std::filesystem::create_directories(directory);
531 if (sources.size() > 4096) throw std::runtime_error("Low-memory source count exceeds 4096");
532 sourceRasters.resize(sources.size());
533 for (std::size_t index = 0; index < sources.size(); ++index) {
534 if (stopped()) throw std::runtime_error("Source preparation canceled");
535 auto& info = sources[index];
536 const auto sourceSize = std::filesystem::file_size(info.path);
537 if (!sourceSize || sourceSize > 128ull * 1024 * 1024)
538 throw std::runtime_error("Invalid streamed JPEG file size");
539 const auto sourceHash = fileDigest(info.path, stopped);
540 std::ostringstream identity;
541 identity.imbue(std::locale::classic());
542 identity << std::setprecision(17) << sourceHash << ' ' << info.width << ' ' << info.height << ' '
543 << info.focal << ' ' << info.cx << ' ' << info.cy << ' ' << info.distorted << ' '
544 << info.lens.k1 << ' ' << info.lens.k2 << ' ' << info.lens.blueScale << ' ' << info.lens.redScale;
545 if (info.camera) identity << " fy=" << info.focalY;
546 const auto cacheIdentity = "PANGEA_PREPARED_SOURCE_V1\n" + identity.str();
547 const auto key = config.preparedSourceDirectory.empty() ? std::to_string(index) :
548 dataDigest(cacheIdentity.data(), cacheIdentity.size());
549 const auto path = directory / (key + ".bgra");
550 const auto metadata = path.string() + ".verified";
551 int inset = 0;
552 if (std::filesystem::exists(metadata)) {
553 std::ifstream record(metadata);
554 std::string saved, digest, trailing;
555 std::getline(record, saved);
556 if (saved != identity.str() || !(record >> inset >> digest) || (record >> trailing) ||
557 inset < 0 || 2 * inset > std::min(info.width, info.height) - 64 || fileDigest(path, stopped) != digest)
558 throw std::runtime_error("Source checkpoint identity or checksum mismatch; originals retained");
559 DiskRaster checked(path, info.width - 2 * inset, info.height - 2 * inset);
560 } else {
561 storageHeadroom(directory, static_cast<std::uint64_t>(info.width) * info.height * (info.distorted ? 12 : 4));
562 const auto decoded = info.distorted ? directory / (key + ".decoded") : path;
563 { JpegStream decoder; decoder.decode(info.path, decoded, info.width, info.height, stopped); }
564 if (info.distorted) {
565 const DiskRaster raster(decoded, info.width, info.height);
566 const auto correctedPath = directory / (key + ".corrected");
567 std::ofstream output(correctedPath, std::ios::binary | std::ios::trunc);
568 const double scales[] = {info.lens.blueScale, 1, info.lens.redScale, 1};
569 for (int firstRow = 0; firstRow < info.height; firstRow += 32) {
570 if (stopped()) throw std::runtime_error("Strip rectification canceled");
571 const int rows = std::min(32, info.height - firstRow);
572 cv::Mat stripe(rows, info.width, CV_8UC4);
573 for (int firstColumn = 0; firstColumn < info.width; firstColumn += 256) {
574 const int columns = std::min(256, info.width - firstColumn);
575 for (int channel = 0; channel < 4; ++channel) {
576 const double focalY = info.focalY > 0 ? info.focalY : info.focal;
577 const cv::Matx33d sensor(info.focal * scales[channel], 0, info.cx,
578 0, focalY * scales[channel], info.cy, 0, 0, 1);
579 const cv::Matx33d ideal(info.focal, 0, info.cx - firstColumn,
580 0, focalY, info.cy - firstRow, 0, 0, 1);
581 cv::Mat mx, my;
582 cv::initUndistortRectifyMap(sensor, cv::Vec<double, 5>(info.lens.k1, info.lens.k2, 0, 0, 0),
583 cv::noArray(), ideal, {columns, rows}, CV_32FC1, mx, my);
584 double minX = info.width - 1, maxX = 0, minY = info.height - 1, maxY = 0;
585 for (int row = 0; row < rows; ++row) for (int column = 0; column < columns; ++column) {
586 const double sx = mx.at<float>(row, column), sy = my.at<float>(row, column);
587 if (!std::isfinite(sx) || !std::isfinite(sy))
588 throw std::runtime_error("Nonfinite rectified camera ray");
589 if (sx < 0 || sy < 0 || sx > info.width - 1 || sy > info.height - 1)
590 inset = std::max(inset, std::min({firstColumn + column, firstRow + row,
591 info.width - 1 - firstColumn - column, info.height - 1 - firstRow - row}) + 1);
592 minX = std::min(minX, std::clamp(sx, 0.0, double(info.width - 1)));
593 maxX = std::max(maxX, std::clamp(sx, 0.0, double(info.width - 1)));
594 minY = std::min(minY, std::clamp(sy, 0.0, double(info.height - 1)));
595 maxY = std::max(maxY, std::clamp(sy, 0.0, double(info.height - 1)));
596 }
597 const int left = std::max(0, static_cast<int>(std::floor(minX)) - 1);
598 const int top = std::max(0, static_cast<int>(std::floor(minY)) - 1);
599 const int right = std::min(info.width, static_cast<int>(std::ceil(maxX)) + 2);
600 const int bottom = std::min(info.height, static_cast<int>(std::ceil(maxY)) + 2);
601 const auto input = raster.read({left, top, right - left, bottom - top});
602 cv::Mat plane, corrected;
603 cv::extractChannel(input, plane, channel);
604 mx -= left; my -= top;
605 cv::remap(plane, corrected, mx, my, cv::INTER_LINEAR, cv::BORDER_CONSTANT);
606 for (int row = 0; row < rows; ++row) for (int column = 0; column < columns; ++column)
607 stripe.at<cv::Vec4b>(row, firstColumn + column)[channel] = corrected.at<unsigned char>(row, column);
608 }
609 }
610 output.write(reinterpret_cast<const char*>(stripe.data), static_cast<std::streamsize>(stripe.total() * 4));
611 }
612 output.close();
613 if (!output || 2 * inset > std::min(info.width, info.height) - 64)
614 throw std::runtime_error("Rectification write failed or unsupported camera crop");
615 const DiskRaster corrected(correctedPath, info.width, info.height);
616 const auto temporary = path.string() + ".partial";
617 std::ofstream cropped(temporary, std::ios::binary | std::ios::trunc);
618 for (int row = inset; row < info.height - inset; ++row) {
619 if (stopped()) throw std::runtime_error("Source crop canceled");
620 const auto pixels = corrected.read({inset, row, info.width - 2 * inset, 1});
621 cropped.write(reinterpret_cast<const char*>(pixels.data), pixels.cols * 4);
622 }
623 cropped.close();
624 if (!cropped) throw std::runtime_error("Cannot write rectified raster");
625 publishWorkingFile(temporary, path);
626 std::filesystem::remove(decoded); std::filesystem::remove(correctedPath);
627 }
628 if (fileDigest(info.path, stopped) != sourceHash)
629 throw std::runtime_error("Photo changed during preparation; no source checkpoint committed");
630 saveWorkingText(metadata, identity.str() + "\n" + std::to_string(inset) + " " + fileDigest(path, stopped) + "\n");
631 }
632 sourceRasters[index] = path;
633 info.width -= 2 * inset; info.height -= 2 * inset; info.cx -= inset; info.cy -= inset;
634 if (info.camera) info.camera->crop(inset);
635 info.distorted = false;
636 }
637 diskSourcesPrepared = true;
638}
639
640void Orthorectifier::prepareSources(const std::function<bool()>& stopped) {
641 if (impl_->config.lowMemory) impl_->prepareDiskSources(stopped);
642}
643
644/** @brief Get the mosaic's cached projection and image source.
645 * @param index Index in the admitted source list.
646 * @return Borrowed entry that can be invalidated by cache eviction.
647 * @details Low-memory mode retains a raster path instead of decoded pixels;
648 * otherwise evict by recency before allocating the next image. */
649Photo& Orthorectifier::Impl::mosaicPhoto(std::size_t index) {
650 auto cached = mosaicPhotos.find(index);
651 if (cached == mosaicPhotos.end()) {
652 const auto& info = sources.at(index);
653 const auto bytes = static_cast<std::size_t>(info.width) * info.height * 4;
654 const auto budget = static_cast<std::size_t>(config.mosaicPhotoCacheMiB) * 1024 * 1024;
655 if (!config.lowMemory && bytes > budget) throw std::runtime_error("Mosaic source exceeds its configured photo cache budget");
656 while (photoBytes + bytes > budget && !mosaicPhotos.empty()) {
657 const auto oldest = std::min_element(mosaicPhotos.begin(), mosaicPhotos.end(),
658 [](const auto& a, const auto& b) { return a.second.touched < b.second.touched; });
659 photoBytes -= oldest->second.photo.bgra.size();
660 mosaicPhotos.erase(oldest);
661 }
662 CachedPhoto entry; entry.photo.info = info;
663 const auto coordinate = projection.forward(info.latitude, info.longitude);
664 entry.photo.east = coordinate.first; entry.photo.north = coordinate.second;
665 const double heading = projection.convergence(info.latitude, info.longitude) * pi / 180;
666 entry.photo.c = std::cos(heading); entry.photo.s = std::sin(heading);
667 entry.photo.scale = projection.scale(info.latitude, info.longitude);
668 if (config.lowMemory) entry.photo.raster = sourceRasters.at(index);
669 else { entry.photo.load(); photoBytes += bytes; }
670 cached = mosaicPhotos.emplace(index, std::move(entry)).first;
671 }
672 cached->second.touched = ++photoClock;
673 return cached->second.photo;
674}
675
676/** @brief Bound the projected source footprint on a mosaic grid.
677 * @param source Source pose and camera geometry.
678 * @param scale Number of base pixels per grid pixel.
679 * @param seamPlan True when computing the seam-planning footprint.
680 * @return Pixel rectangle in the selected mosaic grid.
681 * @details Project image corners to the common reference plane so the
682 * renderer can avoid processing unrelated regions. */
683cv::Rect Orthorectifier::Impl::mosaicRectangle(const OrthoSource& source, int scale, bool seamPlan) const {
684 const auto position = projection.forward(source.latitude, source.longitude);
685 const double heading = projection.convergence(source.latitude, source.longitude) * pi / 180;
686 const double c = std::cos(heading), s = std::sin(heading);
687 const double ray = (source.altitude - config.referencePlane) / source.focal *
688 projection.scale(source.latitude, source.longitude);
689 const double pixel = config.mapPixelSize * scale;
690 double minX = std::numeric_limits<double>::infinity(), minY = minX;
691 double maxX = -minX, maxY = -minX;
692 for (const double u : {1.0, double(source.width - 2)}) for (const double v : {1.0, double(source.height - 2)}) {
693 const auto local = source.camera ? source.camera->ground(u, v, source.altitude - config.referencePlane) :
694 cv::Point2d((u - source.cx) * ray, -(v - source.cy) * ray);
695 const double unit = source.camera ? projection.scale(source.latitude, source.longitude) : 1;
696 const double x = (position.first + unit * (c * local.x - s * local.y) - west) / pixel;
697 const double y = (north - position.second - unit * (s * local.x + c * local.y)) / pixel;
698 minX = std::min(minX, x); maxX = std::max(maxX, x);
699 minY = std::min(minY, y); maxY = std::max(maxY, y);
700 }
701 const int pad = (64 + scale - 1) / scale + 2;
702 double endX = std::ceil(double(cols) * config.jpegWidth / scale) + pad;
703 double endY = std::ceil(double(rows) * config.jpegHeight / scale) + pad;
704 if (seamPlan) {
705 double actualEast = west, actualSouth = north;
706 for (const auto latitude : {config.start.lat, config.end.lat})
707 for (const auto longitude : {config.start.lon, config.end.lon}) {
708 const auto corner = projection.forward(latitude, longitude);
709 actualEast = std::max(actualEast, corner.first);
710 actualSouth = std::min(actualSouth, corner.second);
711 }
712 endX = std::ceil((actualEast - west) / pixel) + pad;
713 endY = std::ceil((north - actualSouth) / pixel) + pad;
714 }
715 const double x0 = std::max(std::floor(minX), -double(pad)), y0 = std::max(std::floor(minY), -double(pad));
716 const double x1 = std::min(std::ceil(maxX), endX), y1 = std::min(std::ceil(maxY), endY);
717 if (x1 <= x0 || y1 <= y0) return {};
718 if (std::max({std::abs(x0), std::abs(y0), std::abs(x1), std::abs(y1)}) > 1e8)
719 throw std::runtime_error("Great mosaic extent exceeds supported grid coordinates");
720 return {static_cast<int>(x0), static_cast<int>(y0), static_cast<int>(x1 - x0), static_cast<int>(y1 - y0)};
721}
722
723/** @brief Resample one source over a mosaic rectangle.
724 * @param photo Projection-ready source with decoded or disk-backed pixels.
725 * @param rectangle Output window in mosaic-grid pixels.
726 * @param scale Number of base pixels per grid pixel.
727 * @param[out] image Warped source colour.
728 * @param[out] mask Support for the warped colour.
729 * @return Transform mapping local destination coordinates into the source.
730 * @details Evaluate the mapping at pixel centres; use the calibrated camera
731 * when present and process in strips to bound source access. */
732cv::Matx33d Orthorectifier::Impl::mosaicWarp(const Photo& photo, const cv::Rect& rectangle, int scale,
733 cv::Mat& image, cv::Mat& mask) const {
734 const double pixel = config.mapPixelSize * scale;
735 const double e = west + (rectangle.x + .5) * pixel, n = north - (rectangle.y + .5) * pixel;
736 double u = 0, v = 0;
737 if (!photo.info.camera) photo.project(e, n, config.referencePlane, u, v, 1);
738 const double factor = pixel * photo.info.focal / ((photo.info.altitude - config.referencePlane) * photo.scale);
739 cv::Matx33d transform(factor * photo.c, -factor * photo.s, u, factor * photo.s, factor * photo.c, v, 0, 0, 1);
740 if (photo.info.camera) {
741 const double de = (e - photo.east) / photo.scale, dn = (n - photo.north) / photo.scale;
742 const double step = pixel / photo.scale;
743 transform = photo.info.camera->groundToPixel(photo.info.altitude - config.referencePlane) *
744 cv::Matx33d(step * photo.c, -step * photo.s, photo.c * de + photo.s * dn,
745 -step * photo.s, -step * photo.c, -photo.s * de + photo.c * dn, 0, 0, 1);
746 }
747 const cv::Mat source = photo.raster.empty() ? cv::Mat(photo.info.height, photo.info.width, CV_8UC4,
748 const_cast<unsigned char*>(photo.bgra.data())) : cv::Mat();
749 cv::Mat warped;
750 warped.create(rectangle.size(), CV_8UC4);
751 constexpr int stripRows = 32;
752 parallelRows((rectangle.height + stripRows - 1) / stripRows, [&](int strip) {
753 const int first = strip * stripRows, last = std::min(rectangle.height, first + stripRows);
754 auto local = transform;
755 for (int axis = 0; axis < 3; ++axis) local(axis, 2) += local(axis, 1) * first;
756 cv::Mat destination = warped.rowRange(first, last);
757 const auto warp = [&](auto&& self, cv::Mat output, cv::Matx33d mapping) -> void {
758 cv::Mat input = source;
759 if (!photo.raster.empty()) {
760 double minX = photo.info.width - 1, maxX = 0, minY = photo.info.height - 1, maxY = 0;
761 double minDepth = std::numeric_limits<double>::infinity(), maxDepth = -minDepth;
762 for (int row : {0, output.rows - 1}) for (int column : {0, output.cols - 1}) {
763 const double depth = mapping(2, 0) * column + mapping(2, 1) * row + mapping(2, 2);
764 minDepth = std::min(minDepth, depth); maxDepth = std::max(maxDepth, depth);
765 const double denominator = std::abs(depth) > 1e-12 ? depth : 1e-12;
766 const double sx = std::clamp((mapping(0, 0) * column + mapping(0, 1) * row + mapping(0, 2)) / denominator, 0.0, double(photo.info.width - 1));
767 const double sy = std::clamp((mapping(1, 0) * column + mapping(1, 1) * row + mapping(1, 2)) / denominator, 0.0, double(photo.info.height - 1));
768 minX = std::min(minX, sx); maxX = std::max(maxX, sx); minY = std::min(minY, sy); maxY = std::max(maxY, sy);
769 }
770 if (maxDepth <= 1e-9) { output.setTo(cv::Scalar(0)); return; }
771 // Across the projective horizon, corner extrema cannot safely bound the source read.
772 if (minDepth <= 1e-9) { minX = minY = 0; maxX = photo.info.width - 1; maxY = photo.info.height - 1; }
773 const int left = std::max(0, static_cast<int>(std::floor(minX)) - 2);
774 const int top = std::max(0, static_cast<int>(std::floor(minY)) - 2);
775 const int right = std::min(photo.info.width, static_cast<int>(std::ceil(maxX)) + 3);
776 const int bottom = std::min(photo.info.height, static_cast<int>(std::ceil(maxY)) + 3);
777 if (static_cast<std::uint64_t>(right - left) * (bottom - top) * 4 >
778 static_cast<std::uint64_t>(config.mosaicPhotoCacheMiB) * 1024 * 1024) {
779 // Split the destination before loading either source window; keep the warp origin aligned.
780 const bool splitRows = output.rows >= output.cols && output.rows > 1;
781 const int split = (splitRows ? output.rows : output.cols) / 2;
782 if (!split) throw std::runtime_error("Single warp sample exceeds raster budget");
783 const cv::Rect first(0, 0, splitRows ? output.cols : split, splitRows ? split : output.rows);
784 const cv::Rect second(splitRows ? 0 : split, splitRows ? split : 0,
785 splitRows ? output.cols : output.cols - split, splitRows ? output.rows - split : output.rows);
786 self(self, output(first), mapping);
787 for (int axis = 0; axis < 3; ++axis)
788 mapping(axis, 2) += mapping(axis, 0) * second.x + mapping(axis, 1) * second.y;
789 self(self, output(second), mapping);
790 return;
791 }
792 input = DiskRaster(photo.raster, photo.info.width, photo.info.height).read({left, top, right - left, bottom - top});
793 for (int axis = 0; axis < 3; ++axis) {
794 mapping(0, axis) -= left * mapping(2, axis);
795 mapping(1, axis) -= top * mapping(2, axis);
796 }
797 }
798 if (photo.info.camera)
799 cv::warpPerspective(input, output, mapping, output.size(), cv::INTER_LINEAR | cv::WARP_INVERSE_MAP,
800 cv::BORDER_REPLICATE);
801 else
802 cv::warpAffine(input, output, cv::Matx23d(mapping(0, 0), mapping(0, 1), mapping(0, 2),
803 mapping(1, 0), mapping(1, 1), mapping(1, 2)), output.size(),
804 cv::INTER_LINEAR | cv::WARP_INVERSE_MAP, cv::BORDER_REPLICATE);
805 };
806 warp(warp, destination, local);
807 }, config.mosaicThreads);
808 cv::cvtColor(warped, image, cv::COLOR_BGRA2BGR);
809 mask.create(rectangle.size(), CV_8U);
810 const int border = 1 + photo.validInset;
811 parallelRows(rectangle.height, [&](int y) {
812 auto* row = mask.ptr<unsigned char>(y);
813 for (int x = 0; x < rectangle.width; ++x) {
814 const double depth = transform(2, 0) * x + transform(2, 1) * y + transform(2, 2);
815 const double sx = (transform(0, 0) * x + transform(0, 1) * y + transform(0, 2)) / depth;
816 const double sy = (transform(1, 0) * x + transform(1, 1) * y + transform(1, 2)) / depth;
817 row[x] = depth > 1e-9 && sx >= border && sy >= border && sx < photo.info.width - border - 1 &&
818 sy < photo.info.height - border - 1 ? 255 : 0;
819 }
820 }, config.mosaicThreads);
821 return transform;
822}
823
824/** @brief Build the shared seam and exposure plan on first use.
825 * @param stopped Cooperative cancellation predicate.
826 * @details Coarsen the overview until the global planning workspace fits its
827 * limits. Reuse that plan across windows so adjacent renders agree. */
828void Orthorectifier::Impl::prepareMosaic(const std::function<bool()>& stopped) {
829 if (mosaicPrepared) return;
830 if (config.lowMemory) prepareDiskSources(stopped);
831 const auto prepareStarted = std::chrono::steady_clock::now();
832 seamScale = config.mosaicSeamScale;
833 // Increase overview scale until source work and the global grid fit their memory budgets.
834 for (;;) {
835 std::size_t pixels = 0;
836 for (const auto& source : sources) {
837 const auto rectangle = mosaicRectangle(source, seamScale, true);
838 pixels += static_cast<std::size_t>(rectangle.width) * rectangle.height;
839 }
840 const auto globalPixels = std::ceil(double(cols) * config.jpegWidth / seamScale) *
841 std::ceil(double(rows) * config.jpegHeight / seamScale);
842 if (pixels <= static_cast<std::size_t>(config.mosaicOverviewPixels) &&
843 (!config.lowMemory || globalPixels <= config.mosaicOverviewPixels)) break;
844 seamScale *= 2;
845 if (seamScale > (config.lowMemory ? 8192 : 256))
846 throw std::runtime_error("Mosaic overview exceeds its memory budget; split the map area");
847 }
848 std::cout << config.mode << ": camera-calibrated mosaic, " << config.mosaicSeamMethod << " seams at 1/" << seamScale
849 << ", " << config.mosaicBlendBands << " blending bands; no stereo cost volume.\n";
850 std::vector<cv::UMat> images, masks, originalMasks;
851 std::vector<cv::Point> corners;
852 std::vector<std::size_t> seamViews;
853 std::vector<cv::Mat> overviewImages(sources.size());
854 std::vector<cv::Mat> overviewMasks(sources.size());
855 parallelRows(static_cast<int>(sources.size()), [&](int index) {
856 if (stopped()) throw std::runtime_error("Mosaic overview canceled");
857 const auto rectangle = mosaicRectangle(sources[index], seamScale, true);
858 if (rectangle.empty()) return;
859 Photo photo;
860 photo.info = sources[index];
861 const auto coordinate = projection.forward(photo.info.latitude, photo.info.longitude);
862 photo.east = coordinate.first; photo.north = coordinate.second;
863 const double heading = projection.convergence(photo.info.latitude, photo.info.longitude) * pi / 180;
864 photo.c = std::cos(heading); photo.s = std::sin(heading);
865 photo.scale = projection.scale(photo.info.latitude, photo.info.longitude);
866 if (config.lowMemory) photo.raster = sourceRasters.at(index);
867 else photo.load(config.mosaicOverviewSourceMaxSide);
868 mosaicWarp(photo, rectangle, seamScale, overviewImages[index], overviewMasks[index]);
869 }, config.mosaicThreads);
870 // Seam ownership is global; per-output-tile seam finding would make adjacent tiles disagree.
871 for (std::size_t i = 0; i < sources.size(); ++i) {
872 if (stopped()) throw std::runtime_error("Great overview canceled");
873 const auto rectangle = mosaicRectangle(sources[i], seamScale, true);
874 if (mosaicRectangle(sources[i], seamScale).empty()) continue;
875 if (mosaicViews.size() == 4096)
876 throw std::runtime_error("Great seam plan exceeds 4096 overlapping sources; split the map area");
877 // A coarse overview can miss a narrow valid footprint; retain its full-resolution colour fallback.
878 mosaicViews.push_back({i, rectangle, {}, 1, mosaicRectangle(sources[i], 1)});
879 if (rectangle.empty()) continue;
880 cv::Mat image = overviewImages[i], mask = overviewMasks[i];
881 if (cv::countNonZero(mask) == 0) continue;
882 images.emplace_back(); masks.emplace_back();
883 image.copyTo(images.back()); mask.copyTo(masks.back());
884 if (config.mosaicFillGaps) { originalMasks.emplace_back(); mask.copyTo(originalMasks.back()); }
885 corners.push_back(rectangle.tl());
886 seamViews.push_back(mosaicViews.size() - 1);
887 }
888 if (mosaicViews.empty())
889 throw std::runtime_error("Great has no source-photo coverage in the requested area");
890 const auto exposureStarted = std::chrono::steady_clock::now();
891 if (config.mosaicExposure && images.size() > 1) {
892 cv::detail::GainCompensator exposure;
893 std::vector<std::pair<cv::UMat, unsigned char>> tagged;
894 for (const auto& mask : masks) tagged.emplace_back(mask, static_cast<unsigned char>(255));
895 exposure.feed(corners, images, tagged);
896 const auto gains = exposure.gains();
897 if (gains.size() != images.size()) throw std::runtime_error("Great exposure solver returned wrong gain count");
898 for (std::size_t i = 0; i < gains.size(); ++i) {
899 if (!std::isfinite(gains[i]) || gains[i] <= 0)
900 throw std::runtime_error("Great exposure solver returned invalid gain");
901 mosaicViews[seamViews[i]].gain = std::clamp(gains[i], .8, 1.25);
902 }
903 }
904 const auto seamsStarted = std::chrono::steady_clock::now();
905 if (config.mosaicSeamMethod != "voronoi")
906 for (std::size_t i = 0; i < images.size(); ++i)
907 images[i].convertTo(images[i], CV_32F, mosaicViews[seamViews[i]].gain);
908 if (stopped()) throw std::runtime_error("Great seam optimization canceled");
909 if (!images.empty()) {
910 if (config.mosaicSeamMethod == "dp") {
911 cv::detail::DpSeamFinder finder(cv::detail::DpSeamFinder::COLOR_GRAD);
912 finder.find(images, corners, masks);
913 } else if (config.mosaicSeamMethod == "voronoi") {
914 cv::detail::VoronoiSeamFinder finder;
915 finder.find(images, corners, masks);
916 } else {
917 cv::detail::GraphCutSeamFinder finder(cv::detail::GraphCutSeamFinderBase::COST_COLOR_GRAD);
918 finder.find(images, corners, masks);
919 }
920 }
921 for (std::size_t i = 0; i < masks.size(); ++i) {
922 cv::dilate(masks[i], mosaicViews[seamViews[i]].seam, cv::Mat(), cv::Point(-1, -1), 1);
923 }
924 if (config.mosaicFillGaps) prepareMosaicCompletion(images, originalMasks, seamViews, stopped);
925 if (stopped()) throw std::runtime_error("Great seam optimization canceled");
926 mosaicPrepared = true;
927 const auto finished = std::chrono::steady_clock::now();
928 const auto elapsed = [](auto first, auto last) { return std::chrono::duration<double>(last - first).count(); };
929 std::cout << "TIMING mosaic_prepare photos_seconds=" << elapsed(prepareStarted, exposureStarted)
930 << " exposure_seconds=" << elapsed(exposureStarted, seamsStarted)
931 << " seams_seconds=" << elapsed(seamsStarted, finished)
932 << " total_seconds=" << elapsed(prepareStarted, finished) << '\n';
933 std::cout << config.mode << " overview ready: " << mosaicViews.size()
934 << " source footprints; global seams reused for every output tile.\n";
935}
936
937/** @brief Build a shared fill overview while retaining observed support.
938 * @param images Warped colour overviews.
939 * @param originalMasks Matching support masks before seam selection.
940 * @param seamViews Source indices corresponding to the overview entries.
941 * @param stopped Cooperative cancellation predicate.
942 * @details Keep the observed mask separate from inferred fill so later
943 * renders need not report extrapolated colours as measured coverage. */
944void Orthorectifier::Impl::prepareMosaicCompletion(const std::vector<cv::UMat>& images,
945 const std::vector<cv::UMat>& originalMasks, const std::vector<std::size_t>& seamViews,
946 const std::function<bool()>& stopped) {
947 double actualEast = west, actualSouth = north;
948 for (const auto latitude : {config.start.lat, config.end.lat})
949 for (const auto longitude : {config.start.lon, config.end.lon}) {
950 const auto corner = projection.forward(latitude, longitude);
951 actualEast = std::max(actualEast, corner.first); actualSouth = std::min(actualSouth, corner.second);
952 }
953 const int width = std::max(1, static_cast<int>(std::ceil((actualEast - west) / (config.mapPixelSize * seamScale))));
954 const int height = std::max(1, static_cast<int>(std::ceil((north - actualSouth) / (config.mapPixelSize * seamScale))));
955 if (static_cast<std::size_t>(width) * height > static_cast<std::size_t>(config.lowMemory ? config.mosaicOverviewPixels : 8000000))
956 throw std::runtime_error("Global completion overview exceeds 8 million pixels; increase mosaic.seam_scale");
957 cv::Mat premultiplied(height, width, CV_32FC4, cv::Scalar(0));
958 cv::Mat scores(height, width, CV_32F, cv::Scalar(std::numeric_limits<float>::infinity()));
959 cv::Mat observed(height, width, CV_8U, cv::Scalar(0));
960 for (std::size_t index = 0; index < images.size(); ++index) {
961 if (stopped()) throw std::runtime_error("Global mosaic source fusion canceled");
962 const auto& view = mosaicViews[seamViews[index]];
963 const auto rectangle = view.rectangle & cv::Rect(0, 0, width, height);
964 if (rectangle.empty()) continue;
965 cv::Mat colour;
966 images[index].convertTo(colour, CV_32F, config.mosaicSeamMethod == "voronoi" ? view.gain : 1);
967 const auto original = originalMasks[index].getMat(cv::ACCESS_READ);
968 for (int row = rectangle.y; row < rectangle.y + rectangle.height; ++row) {
969 auto* destination = premultiplied.ptr<cv::Vec4f>(row);
970 auto* best = scores.ptr<float>(row);
971 auto* coverage = observed.ptr<unsigned char>(row);
972 const int sourceRow = row - view.rectangle.y;
973 const auto* pixels = colour.ptr<cv::Vec3f>(sourceRow);
974 const auto* valid = original.ptr<unsigned char>(sourceRow);
975 const auto* seam = view.seam.ptr<unsigned char>(sourceRow);
976 for (int column = rectangle.x; column < rectangle.x + rectangle.width; ++column) {
977 const int sourceColumn = column - view.rectangle.x;
978 if (!valid[sourceColumn]) continue;
979 const double dx = (sourceColumn + .5) / view.rectangle.width - .5;
980 const double dy = (sourceRow + .5) / view.rectangle.height - .5;
981 const float score = static_cast<float>(dx * dx + dy * dy + (seam[sourceColumn] ? 0 : 10));
982 if (score >= best[column]) continue;
983 best[column] = score; coverage[column] = 255;
984 const auto& pixel = pixels[sourceColumn];
985 destination[column] = cv::Vec4f(pixel[0], pixel[1], pixel[2], 1);
986 }
987 }
988 }
989 cv::distanceTransform(observed, mosaicSourceDistance, cv::DIST_L2, 3);
990 completedMosaic = completeMosaicPyramid(premultiplied, config.mosaicThreads, stopped);
991 std::cout << "Global source-derived completion ready: " << width << " x " << height
992 << "; completed pixels are tracked separately from observed photo colour.\n";
993}
994
995/** @brief Render a mosaic window using the session-wide seam plan.
996 * @param map Projected centre and georeference of the window.
997 * @param width Output columns.
998 * @param height Output rows.
999 * @param stopped Cooperative cancellation predicate.
1000 * @return Owned pixels, confidence, and rendering provenance.
1001 * @details Combine planned contributions in the local window while tracking
1002 * fallback colour and coverage independently of multiband blending. */
1004 const std::function<bool()>& stopped) {
1005 prepareMosaic(stopped);
1007 result.map = map; result.width = width; result.height = height; result.mode = config.mode;
1008 result.referencePlane = config.referencePlane;
1009 result.seamScale = seamScale; result.blendBands = config.mosaicBlendBands;
1010 result.seamMethod = config.mosaicSeamMethod;
1011 result.exposureCompensated = config.mosaicExposure;
1012 const auto count = static_cast<std::size_t>(width) * height;
1013 result.bgra.assign(count * 4, 0); result.confidence.assign(count, 0);
1014 const cv::Rect region(static_cast<int>(std::llround((map.east - west) / config.mapPixelSize - width / 2.0)),
1015 static_cast<int>(std::llround((north - map.north) / config.mapPixelSize - height / 2.0)),
1016 width, height);
1017 cv::Mat best(height, width, CV_32F, cv::Scalar(std::numeric_limits<float>::infinity()));
1018 cv::Mat fallback(height, width, CV_8UC3, cv::Scalar(0));
1019 cv::Mat coverage(height, width, CV_8U, cv::Scalar(0));
1020 cv::detail::MultiBandBlender blender(false, config.mosaicBlendBands);
1021 blender.prepare(cv::Rect(0, 0, width, height));
1022 bool fed = false;
1023 for (const auto& view : mosaicViews) {
1024 if (stopped()) throw std::runtime_error("Great blending canceled");
1025 const cv::Rect footprint = view.footprint;
1026 if ((footprint & region).empty()) continue;
1027 auto& photo = mosaicPhoto(view.source);
1028 cv::Mat image, mask;
1029 const auto transform = mosaicWarp(photo, region, 1, image, mask);
1030 if (cv::countNonZero(mask) == 0) continue;
1031 image.convertTo(image, CV_8U, view.gain);
1032 result.sources.push_back(photo.info);
1033 cv::Mat selected(height, width, CV_8U, cv::Scalar(0));
1034 parallelRows(height, [&](int y) {
1035 const auto* valid = mask.ptr<unsigned char>(y);
1036 const auto* colours = image.ptr<cv::Vec3b>(y);
1037 auto* scores = best.ptr<float>(y);
1038 auto* backup = fallback.ptr<cv::Vec3b>(y);
1039 auto* covered = coverage.ptr<unsigned char>(y);
1040 auto* owned = selected.ptr<unsigned char>(y);
1041 const double gy = (region.y + y + .5) / seamScale - .5 - view.rectangle.y;
1042 const int sy = view.seam.empty() ? 0 :
1043 std::clamp(static_cast<int>(std::lround(gy)), 0, view.seam.rows - 1);
1044 for (int x = 0; x < width; ++x) {
1045 if (!valid[x]) continue;
1046 covered[x] = 255;
1047 const double depth = transform(2, 0) * x + transform(2, 1) * y + transform(2, 2);
1048 const double u = (transform(0, 0) * x + transform(0, 1) * y + transform(0, 2)) / depth;
1049 const double v = (transform(1, 0) * x + transform(1, 1) * y + transform(1, 2)) / depth;
1050 const double score = std::pow((u - photo.info.cx) / photo.info.width, 2) +
1051 std::pow((v - photo.info.cy) / photo.info.height, 2);
1052 if (score < scores[x]) { scores[x] = static_cast<float>(score); backup[x] = colours[x]; }
1053 if (view.seam.empty()) continue;
1054 const double gx = (region.x + x + .5) / seamScale - .5 - view.rectangle.x;
1055 const int sx = std::clamp(static_cast<int>(std::lround(gx)), 0, view.seam.cols - 1);
1056 if (view.seam.at<unsigned char>(sy, sx)) owned[x] = 255;
1057 }
1058 }, config.mosaicThreads);
1059 if (cv::countNonZero(selected)) {
1060 cv::Mat signedImage; image.convertTo(signedImage, CV_16S);
1061 blender.feed(signedImage, selected, cv::Point(0, 0));
1062 fed = true;
1063 }
1064 }
1065 cv::Mat blended, blendedMask;
1066 if (fed) blender.blend(blended, blendedMask);
1067 std::atomic<std::size_t> reconstructed{0}, reconstructionBlend{0};
1068 parallelRows(height, [&](int y) {
1069 if (stopped()) throw std::runtime_error("Great mosaic finalization canceled");
1070 std::size_t rowReconstructed = 0, rowBlended = 0;
1071 for (int x = 0; x < width; ++x) {
1072 const bool observed = coverage.at<unsigned char>(y, x) != 0;
1073 if (!observed && completedMosaic.empty()) continue;
1074 const auto index = static_cast<std::size_t>(y) * width + x;
1075 auto* pixel = result.bgra.data() + index * 4;
1076 if (observed && fed && blendedMask.at<unsigned char>(y, x)) {
1077 const auto value = blended.at<cv::Vec3s>(y, x);
1078 for (int channel = 0; channel < 3; ++channel) pixel[channel] = cv::saturate_cast<unsigned char>(value[channel]);
1079 } else if (observed) {
1080 const auto value = fallback.at<cv::Vec3b>(y, x);
1081 for (int channel = 0; channel < 3; ++channel) pixel[channel] = value[channel];
1082 }
1083 pixel[3] = 255;
1084 result.confidence[index] = observed ? 64 : 16;
1085 if (!completedMosaic.empty()) {
1086 const double sourceX = std::clamp((region.x + x + .5) / seamScale - .5,
1087 0.0, double(completedMosaic.cols - 1));
1088 const double sourceY = std::clamp((region.y + y + .5) / seamScale - .5,
1089 0.0, double(completedMosaic.rows - 1));
1090 const int left = static_cast<int>(sourceX), top = static_cast<int>(sourceY);
1091 const int right = std::min(left + 1, completedMosaic.cols - 1);
1092 const int bottom = std::min(top + 1, completedMosaic.rows - 1);
1093 const double fractionX = sourceX - left, fractionY = sourceY - top;
1094 const auto sample = [&](const cv::Mat& image, int channel) {
1095 const int channels = image.channels();
1096 const auto* first = image.ptr<float>(top); const auto* last = image.ptr<float>(bottom);
1097 return (1 - fractionY) * ((1 - fractionX) * first[left * channels + channel] +
1098 fractionX * first[right * channels + channel]) + fractionY *
1099 ((1 - fractionX) * last[left * channels + channel] + fractionX * last[right * channels + channel]);
1100 };
1101 double weight = observed ? std::clamp(sample(mosaicSourceDistance, 0) / 2, 0.0, 1.0) : 0;
1102 weight = weight * weight * (3 - 2 * weight);
1103 if (weight < 1) {
1104 for (int channel = 0; channel < 3; ++channel)
1105 pixel[channel] = cv::saturate_cast<unsigned char>(weight * pixel[channel] +
1106 (1 - weight) * sample(completedMosaic, channel));
1107 if (observed) { result.confidence[index] = 48; ++rowBlended; }
1108 else ++rowReconstructed;
1109 }
1110 }
1111 }
1112 reconstructed += rowReconstructed; reconstructionBlend += rowBlended;
1113 }, config.mosaicThreads);
1114 result.validFraction = completedMosaic.empty() ? static_cast<double>(cv::countNonZero(coverage)) / count : 1;
1115 result.reconstructedFraction = static_cast<double>(reconstructed.load()) / count;
1116 result.reconstructionBlendFraction = static_cast<double>(reconstructionBlend.load()) / count;
1117 return result;
1118}
1119
1120std::filesystem::path Orthorectifier::normalizeSources(const fsPath& csv, const fsPath& directory,
1121 const std::function<bool()>& stopped,
1122 unsigned workers, unsigned memoryMiB) {
1123 const auto started = std::chrono::steady_clock::now();
1124 const auto sources = readSources(csv);
1125 if (std::none_of(sources.begin(), sources.end(), [](const OrthoSource& source) { return source.distorted; }))
1126 return csv;
1127 if (std::filesystem::exists(directory)) throw std::runtime_error("Rectified working directory already exists");
1128 std::filesystem::create_directories(directory);
1129 std::ifstream input(csv);
1130 std::string line;
1131 std::getline(input, line);
1132 const auto header = csvFields(line);
1133 std::map<std::string, std::size_t> columns;
1134 for (std::size_t i = 0; i < header.size(); ++i) columns.emplace(header[i], i);
1135 const auto result = directory / "photos.csv";
1136 std::ofstream output(result, std::ios::binary);
1137 output << line << '\n';
1138 std::uint64_t bytesPerWorker = 1;
1139 for (const auto& source : sources) {
1140 const auto required = static_cast<std::uint64_t>(source.width) * source.height * 128 +
1141 std::filesystem::file_size(source.path) + 4 * 1024 * 1024;
1142 bytesPerWorker = std::max(bytesPerWorker, required);
1143 }
1144 const auto memoryWorkers = std::max<std::uint64_t>(1,
1145 static_cast<std::uint64_t>(memoryMiB) * 1024 * 1024 / bytesPerWorker);
1146 workers = static_cast<unsigned>(std::min<std::uint64_t>({std::max(1u, workers),
1147 std::max(1u, std::thread::hardware_concurrency()), sources.size(), memoryWorkers}));
1148 std::vector<int> insets(sources.size());
1149 parallelRows(static_cast<int>(workers), [&](int worker) {
1151 RectificationCache rectification;
1152 for (std::size_t index = static_cast<unsigned>(worker); index < sources.size(); index += workers) {
1153 if (stopped()) throw std::runtime_error("Camera rectification canceled");
1154 const auto& source = sources[index];
1155 Photo photo; photo.info = source; photo.load(0, &rectification);
1156 const int inset = insets[index] = photo.validInset;
1157 const auto* pixels = photo.bgra.data() + (static_cast<std::size_t>(inset) * source.width + inset) * 4;
1158 const auto image = encoder.encode(pixels, source.width - 2 * inset, source.height - 2 * inset,
1159 source.width * 4, 100);
1160 JpegEncoder::save(image, directory / source.path.filename());
1161 }
1162 }, workers);
1163 const int inset = insets.front();
1164 if (!std::all_of(insets.begin(), insets.end(), [&](int value) { return value == inset; }))
1165 throw std::runtime_error("Source lens rectification changed valid crop");
1166 while (std::getline(input, line)) {
1167 auto fields = csvFields(line);
1168 if (fields.size() != header.size()) throw std::runtime_error("Source CSV changed during rectification");
1169 if (fields[columns.at("trigger")] != "box_tile") continue;
1170 if (inset > 0) {
1171 const auto value = [&](const char* key) { return number(fields.at(columns.at(key)), key); };
1172 const double width = value("width"), height = value("height");
1173 const double cropWidth = value("crop_width"), cropHeight = value("crop_height");
1174 const auto set = [&](const char* key, double numberValue) {
1175 std::ostringstream text;
1176 text.imbue(std::locale::classic()); text << std::setprecision(17) << numberValue;
1177 fields.at(columns.at(key)) = text.str();
1178 };
1179 set("crop_x", value("crop_x") + inset * cropWidth / width);
1180 set("crop_y", value("crop_y") + inset * cropHeight / height);
1181 set("crop_width", cropWidth * (width - 2 * inset) / width);
1182 set("crop_height", cropHeight * (height - 2 * inset) / height);
1183 set("width", width - 2 * inset); set("height", height - 2 * inset);
1184 if (columns.count("cx_px")) { set("cx_px", value("cx_px") - inset); set("cy_px", value("cy_px") - inset); }
1185 }
1186 fields[columns.at("lens_profile")] = "simconnect";
1187 for (const auto* key : {"lens_k1", "lens_k2", "lens_blue_scale", "lens_red_scale"}) {
1188 const auto column = columns.find(key);
1189 if (column != columns.end()) fields[column->second] = std::string(key).find("scale") != std::string::npos ? "1" : "0";
1190 }
1191 for (std::size_t i = 0; i < fields.size(); ++i) {
1192 if (i) output << ',';
1193 output << '"';
1194 for (const auto ch : fields[i]) { if (ch == '"') output << '"'; output << ch; }
1195 output << '"';
1196 }
1197 output << '\n';
1198 }
1199 if (input.bad()) throw std::runtime_error("Cannot read source CSV during rectification");
1200 output.close();
1201 if (!output) throw std::runtime_error("Cannot finalize rectified source CSV");
1202#if defined(__GLIBC__)
1203 if (workers > 1) malloc_trim(0);
1204#endif
1205 if (inset > 0)
1206 std::cout << "Rectified working images exclude " << inset
1207 << " unsupported border pixels per edge; cropped intrinsics recorded.\n";
1208 std::cout << "Lens-aware working images saved locally; originals unchanged: " << directory.u8string() << '\n';
1209 std::cout << "TIMING source_prepare photos=" << sources.size() << " workers=" << workers << " wall_seconds="
1210 << std::chrono::duration<double>(std::chrono::steady_clock::now() - started).count() << '\n';
1211 return result;
1212}
1213
1214/** @brief Render a window with the selected orthorectification engine.
1215 * @param map Projected centre and georeference of the window.
1216 * @param outputWidth Output columns.
1217 * @param outputHeight Output rows.
1218 * @param stopped Cooperative cancellation predicate.
1219 * @return Owned pixels and coverage information.
1220 * @details The simple engine ranks sources by distance and bounds the
1221 * candidate set; the mosaic engine uses the shared seam plan instead. */
1222OrthoImage Orthorectifier::Impl::renderRegion(const MapTile& map, int outputWidth, int outputHeight,
1223 const std::function<bool()>& stopped) {
1224 if (config.orthoEngine == "mosaic") return renderMosaicRegion(map, outputWidth, outputHeight, stopped);
1225 const auto& state = *this;
1227 result.width = outputWidth; result.height = outputHeight;
1228 const double tileWidth = result.width * config.mapPixelSize, tileHeight = result.height * config.mapPixelSize;
1229 result.map = map;
1230 std::vector<std::pair<double, std::size_t>> nearest;
1231 for (std::size_t i = 0; i < state.sources.size(); ++i) {
1232 const auto p = state.projection.forward(state.sources[i].latitude, state.sources[i].longitude);
1233 nearest.emplace_back(std::hypot(p.first - result.map.east, p.second - result.map.north), i);
1234 }
1235 std::sort(nearest.begin(), nearest.end());
1236 Photos photos;
1237 for (const auto& location : nearest) {
1238 if (photos.size() == 12) break;
1239 const auto& info = state.sources[location.second];
1240 const double maxFootprint = (info.altitude - config.referencePlane) / info.focal * std::hypot(info.width, info.height);
1241 if (location.first > maxFootprint / 2 + std::hypot(tileWidth, tileHeight) / 2) break;
1242 const auto decoded = static_cast<std::size_t>(info.width) * info.height * 4;
1243 std::size_t loaded = 0;
1244 for (const auto& photo : photos) loaded += photo->bgra.size();
1245 if (loaded + decoded > 512 * 1024 * 1024)
1246 throw std::runtime_error("Source view memory exceeds 512 MB; reduce source viewport dimensions");
1247 result.sources.push_back(info);
1248 photos.push_back(sourcePhoto(location.second));
1249 }
1250 const auto count = static_cast<std::size_t>(result.width) * result.height;
1251 result.bgra.assign(count * 4, 0);
1252 result.confidence.assign(count, 0);
1253 const double left = map.east - (result.width - 1) * config.mapPixelSize / 2;
1254 const double top = map.north + (result.height - 1) * config.mapPixelSize / 2;
1255 parallelRows(result.height, [&](int y) {
1256 if (stopped()) throw std::runtime_error("Simple mosaic canceled");
1257 for (int x = 0; x < result.width; ++x) {
1258 const double e = left + x * config.mapPixelSize, n = top - y * config.mapPixelSize;
1259 double best = std::numeric_limits<double>::infinity(), weights = 0;
1260 std::array<double, 3> sum{};
1261 const auto index = static_cast<std::size_t>(y) * result.width + x;
1262 auto* pixel = result.bgra.data() + index * 4;
1263 for (const auto& owner : photos) {
1264 const auto& photo = *owner;
1265 double u, v;
1266 if (!photo.project(e, n, config.referencePlane, u, v, 1)) continue;
1267 const double score = std::pow((u - photo.info.cx) / photo.info.width, 2) +
1268 std::pow((v - photo.info.cy) / photo.info.height, 2);
1269 unsigned char colour[4]{};
1270 photo.color(u, v, colour);
1271 if (config.blend == "feather") {
1272 const double edge = std::min({u, v, photo.info.width - 1 - u, photo.info.height - 1 - v});
1273 const double weight = edge * edge / (1 + score);
1274 for (std::size_t channel = 0; channel < 3; ++channel) sum[channel] += colour[channel] * weight;
1275 weights += weight;
1276 } else if (score < best) {
1277 best = score;
1278 std::copy(colour, colour + 4, pixel);
1279 }
1280 }
1281 if (weights > 0) {
1282 for (std::size_t channel = 0; channel < 3; ++channel)
1283 pixel[channel] = static_cast<unsigned char>(std::clamp(std::lround(sum[channel] / weights), 0L, 255L));
1284 pixel[3] = 255;
1285 }
1286 if (pixel[3]) result.confidence[index] = 64;
1287 }
1288 });
1289 result.validFraction = static_cast<double>(std::count(result.confidence.begin(), result.confidence.end(), 64)) / count;
1290 return result;
1291}
1292
1293OrthoImage Orthorectifier::render(std::size_t index, const std::function<bool()>& stopped,
1294 const std::function<void(double)>& progress) {
1295 if (index >= size()) throw std::runtime_error("Orthophoto tile index out of range");
1296 const auto& config = impl_->config;
1297 return renderWindow(index % columns() * config.jpegWidth, index / columns() * config.jpegHeight,
1298 config.jpegWidth, config.jpegHeight, stopped, progress);
1299}
1300
1301OrthoImage Orthorectifier::renderWindow(std::size_t originX, std::size_t originY, int width, int height,
1302 const std::function<bool()>& stopped,
1303 const std::function<void(double)>& progress) {
1304 auto& state = *impl_;
1305 const auto& config = state.config;
1306 if (config.lowMemory) state.prepareDiskSources(stopped);
1307 if (width <= 0 || height <= 0 || width > 4096 || height > 4096 ||
1308 originX > columns() * config.jpegWidth || originY > rows() * config.jpegHeight ||
1309 static_cast<std::size_t>(width) > columns() * config.jpegWidth - originX ||
1310 static_cast<std::size_t>(height) > rows() * config.jpegHeight - originY)
1311 throw std::runtime_error("Requested render window is outside the working grid");
1313 result.width = width; result.height = height;
1314 result.mode = config.mode;
1315 result.referencePlane = state.config.referencePlane;
1316 result.map = {state.west + (originX + result.width / 2.0) * config.mapPixelSize,
1317 state.north - (originY + result.height / 2.0) * config.mapPixelSize,
1318 config.mapPixelSize, 0, state.projection.epsg()};
1319 const auto count = static_cast<std::size_t>(result.width) * result.height;
1320 result.bgra.resize(count * 4); result.confidence.resize(count);
1321 const std::size_t blockSize = config.orthoEngine == "mosaic" ? 512 : 500;
1322 constexpr std::size_t halo = 64;
1323 const std::size_t regionSize = blockSize + 2 * halo;
1324 std::size_t valid = 0, reconstructed = 0, reconstructionBlend = 0;
1325 const auto blocks = ((originX + result.width - 1) / blockSize - originX / blockSize + 1) *
1326 ((originY + result.height - 1) / blockSize - originY / blockSize + 1);
1327 std::size_t completedBlocks = 0;
1328 if (progress) progress(0);
1329 for (auto by = originY / blockSize; by <= (originY + result.height - 1) / blockSize; ++by)
1330 for (auto bx = originX / blockSize; bx <= (originX + result.width - 1) / blockSize; ++bx) {
1331 if (stopped()) throw std::runtime_error("Orthorectification canceled");
1332 const auto key = std::make_pair(bx, by);
1333 auto cached = state.blocks.find(key);
1334 if (cached == state.blocks.end()) {
1335 const MapTile map{state.west + (bx * blockSize + blockSize / 2.0) * config.mapPixelSize,
1336 state.north - (by * blockSize + blockSize / 2.0) * config.mapPixelSize,
1337 config.mapPixelSize, 0, state.projection.epsg()};
1338 const bool webTiles = config.outputFormat == "xyz";
1339 const auto capacity = config.lowMemory ? std::max<std::size_t>(1,
1340 static_cast<std::size_t>(config.blockCacheMiB) * 1024 * 1024 / (regionSize * regionSize * 5)) :
1341 (webTiles ? 32u : 4u);
1342 if (state.blocks.size() >= capacity) {
1343 auto oldest = state.blocks.begin();
1344 if (webTiles) oldest = std::min_element(state.blocks.begin(), state.blocks.end(),
1345 [](const auto& a, const auto& b) { return a.second.touched < b.second.touched; });
1346 state.blocks.erase(oldest);
1347 }
1349 const auto blockPath = config.workingDirectory / "blocks" /
1350 (std::to_string(bx) + "_" + std::to_string(by));
1351 const auto recordPath = blockPath.string() + ".verified";
1352 if (config.lowMemory && std::filesystem::exists(recordPath)) {
1353 std::ifstream record(recordPath);
1354 std::string digest, trailing;
1355 if (!(record >> image.seamScale >> digest) || (record >> trailing) ||
1356 image.seamScale < 1 || fileDigest(blockPath) != digest ||
1357 std::filesystem::file_size(blockPath) != regionSize * regionSize * 5)
1358 throw std::runtime_error("Working block checksum or metadata mismatch");
1359 image.width = image.height = static_cast<int>(regionSize);
1360 image.bgra.resize(regionSize * regionSize * 4); image.confidence.resize(regionSize * regionSize);
1361 std::ifstream payload(blockPath, std::ios::binary);
1362 payload.read(reinterpret_cast<char*>(image.bgra.data()), static_cast<std::streamsize>(image.bgra.size()));
1363 payload.read(reinterpret_cast<char*>(image.confidence.data()), static_cast<std::streamsize>(image.confidence.size()));
1364 if (!payload) throw std::runtime_error("Cannot load working block");
1365 image.blendBands = config.mosaicBlendBands; image.seamMethod = config.mosaicSeamMethod;
1366 image.exposureCompensated = config.mosaicExposure;
1367 } else {
1368 image = state.renderRegion(map, static_cast<int>(regionSize), static_cast<int>(regionSize), stopped);
1369 if (config.lowMemory) {
1370 std::filesystem::create_directories(blockPath.parent_path());
1371 storageHeadroom(blockPath.parent_path(), image.bgra.size() + image.confidence.size());
1372 const auto temporary = blockPath.string() + ".partial";
1373 std::ofstream payload(temporary, std::ios::binary | std::ios::trunc);
1374 payload.write(reinterpret_cast<const char*>(image.bgra.data()), static_cast<std::streamsize>(image.bgra.size()));
1375 payload.write(reinterpret_cast<const char*>(image.confidence.data()), static_cast<std::streamsize>(image.confidence.size()));
1376 payload.close();
1377 if (!payload) throw std::runtime_error("Cannot save working block");
1378 publishWorkingFile(temporary, blockPath);
1379 saveWorkingText(recordPath, std::to_string(image.seamScale) + " " + fileDigest(blockPath) + "\n");
1380 }
1381 }
1382 Impl::CachedBlock block{std::move(image), 0};
1383 cached = state.blocks.emplace(key, std::move(block)).first;
1384 }
1385 cached->second.touched = ++state.blockClock;
1386 const auto& block = cached->second.image;
1387 result.seamScale = block.seamScale; result.blendBands = block.blendBands;
1388 result.seamMethod = block.seamMethod;
1389 result.exposureCompensated = block.exposureCompensated;
1390 for (const auto& source : block.sources)
1391 if (std::none_of(result.sources.begin(), result.sources.end(),
1392 [&](const OrthoSource& existing) { return existing.path == source.path; }))
1393 result.sources.push_back(source);
1394 const auto x0 = std::max(originX, bx * blockSize), x1 = std::min(originX + result.width, (bx + 1) * blockSize);
1395 const auto y0 = std::max(originY, by * blockSize), y1 = std::min(originY + result.height, (by + 1) * blockSize);
1396 for (auto y = y0; y < y1; ++y) for (auto x = x0; x < x1; ++x) {
1397 const auto input = (y - by * blockSize + halo) * regionSize + x - bx * blockSize + halo;
1398 const auto output = (y - originY) * result.width + x - originX;
1399 std::copy_n(block.bgra.data() + input * 4, 4, result.bgra.data() + output * 4);
1400 result.confidence[output] = block.confidence[input];
1401 if (block.confidence[input]) ++valid;
1402 if (config.orthoEngine == "mosaic") {
1403 reconstructed += block.confidence[input] == 16;
1404 reconstructionBlend += block.confidence[input] == 48;
1405 }
1406 }
1407 if (progress) progress(static_cast<double>(++completedBlocks) / blocks);
1408 }
1409 result.validFraction = static_cast<double>(valid) / count;
1410 result.reconstructedFraction = static_cast<double>(reconstructed) / count;
1411 result.reconstructionBlendFraction = static_cast<double>(reconstructionBlend) / count;
1412 result.corroboratedFraction = 0;
1413 std::cout << "Working region " << originX << ',' << originY << " shared-grid coverage: "
1414 << std::fixed << std::setprecision(3) << result.validFraction * 100 << "%.\n";
1415 return result;
1416}
1417
1418/** @brief Run synthetic orthorectification regression cases.
1419 * @details Use controlled source geometry and imagery to check render
1420 * behavior independently of simulator or camera availability.
1421 * @throws std::runtime_error If a fixture invariant fails. */
1423 const auto require = [](bool value, const char* message) {
1424 if (!value) throw std::runtime_error(std::string("Orthorectification test: ") + message);
1425 };
1426 {
1427 const auto directory = std::filesystem::temp_directory_path() /
1428 ("PangeaRectification_" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
1429 require(std::filesystem::create_directory(directory), "Create rectification parity fixture");
1430 OrthoSource source;
1431 source.path = directory / "source.jpg";
1432 source.width = 96; source.height = 80; source.focal = 90;
1433 source.cx = 47.5; source.cy = 39.5; source.distorted = true;
1434 source.lens.k1 = .03; source.lens.k2 = .005;
1435 source.lens.blueScale = .9996; source.lens.redScale = 1.0004;
1436 std::vector<unsigned char> pixels(96 * 80 * 4);
1437 for (std::size_t offset = 0; offset < pixels.size(); ++offset)
1438 pixels[offset] = static_cast<unsigned char>((offset * 37 + offset / 96) % 251);
1440 JpegEncoder::save(encoder.encode(pixels.data(), 96, 80, 96 * 4, 100), source.path);
1441 RectificationCache cache;
1442 for (int scenario = 0; scenario < 10; ++scenario) {
1443 auto camera = source;
1444 if (scenario == 2) camera.focal += 5;
1445 if (scenario == 3) camera.cx += 1;
1446 if (scenario == 4) camera.cy -= 1;
1447 if (scenario == 5) camera.lens.k1 += .01;
1448 if (scenario == 6) camera.lens.k2 += .001;
1449 if (scenario == 7) camera.lens.blueScale -= .001;
1450 if (scenario == 8) camera.lens.redScale += .001;
1451 for (int repeated = 0; repeated < 2; ++repeated) {
1452 Photo original, cached;
1453 original.info = cached.info = camera;
1454 original.load(); cached.load(0, &cache);
1455 require(original.bgra == cached.bgra && original.validInset == cached.validInset,
1456 "Cached rectification preserves every BGRA byte and crop across calibration changes");
1457 }
1458 }
1459 require(cache.mapX[1].data == cache.mapX[3].data && cache.mapY[1].data == cache.mapY[3].data,
1460 "Identical green and alpha calibration shares immutable maps");
1461 for (const bool changeWidth : {false, true}) {
1462 auto camera = source;
1463 if (changeWidth) ++camera.width; else ++camera.height;
1464 require(cache.prepare(camera, 96 * 80) && cache.mapX[0].empty(),
1465 "Changed dimensions invalidate cached maps");
1466 }
1467 require(!cache.prepare(source, 96 * 1024 * 1024) && cache.mapX[0].empty(),
1468 "Oversized calibration releases the cache and uses the uncached path");
1469 std::filesystem::remove(source.path);
1470 std::filesystem::remove(directory);
1471 }
1472 {
1473 cv::Mat sparse(13, 9, CV_32FC4, cv::Scalar(0));
1474 sparse.at<cv::Vec4f>(3, 3) = cv::Vec4f(32, 64, 128, 1);
1475 sparse.at<cv::Vec4f>(7, 5) = cv::Vec4f(0, 0, 0, 1);
1476 const auto completed = completeMosaicPyramid(sparse, 32, [] { return false; });
1477 for (int row = 0; row < completed.rows; ++row) for (int column = 0; column < completed.cols; ++column) {
1478 const auto value = completed.at<cv::Vec4f>(row, column);
1479 require(value[3] == 1 && std::isfinite(value[0]) && value[0] >= 0 && value[0] <= 32.001,
1480 "Odd-size push-pull completion has finite opaque colour everywhere");
1481 if (sparse.at<cv::Vec4f>(row, column)[3])
1482 require(value == sparse.at<cv::Vec4f>(row, column), "Completion preserves observed colour, including genuine black");
1483 }
1484 bool emptyRejected = false, canceled = false;
1485 try { completeMosaicPyramid(cv::Mat::zeros(9, 13, CV_32FC4), 1, [] { return false; }); }
1486 catch (const std::runtime_error&) { emptyRejected = true; }
1487 try { completeMosaicPyramid(sparse, 1, [] { return true; }); }
1488 catch (const std::runtime_error&) { canceled = true; }
1489 require(emptyRejected && canceled, "Global completion rejects no-source images and honors cancellation");
1490 }
1491 MapConfig plan;
1492 plan.start = {52.00455, 4.35870, 120}; plan.end = {52.00365, 4.36065, 120};
1493 plan.jpegWidth = 1400; plan.jpegHeight = 1100; plan.mapPixelSize = .1;
1494 const auto whole = orthoLayout(plan);
1495 require(whole.columns == 1 && whole.rows == 1 && whole.epsg == 32631 &&
1496 std::abs(whole.east - whole.west - 140) < 1e-8 &&
1497 std::abs(whole.north - whole.south - 110) < 1e-8, "Exact final map extent");
1498 plan.jpegWidth = 700; plan.jpegHeight = 550;
1499 const auto cuts = orthoLayout(plan);
1500 require(cuts.columns == 2 && cuts.rows == 2 && cuts.west == whole.west &&
1501 cuts.north == whole.north && cuts.east == whole.east && cuts.south == whole.south,
1502 "Tile cuts preserve exact map origin and extent");
1503 plan.mapPixelSize = 2;
1504 require(orthoLayout(plan).columns == 1 && orthoLayout(plan).rows == 1,
1505 "Existing two-metre pixel-size setting remains supported");
1506 require(csvFields("a,\"b,c\",\"d\"\"e\"") == std::vector<std::string>{"a", "b,c", "d\"e"}, "CSV quoting");
1507 {
1508 const auto directory = std::filesystem::temp_directory_path() /
1509 ("PangeaUtcSourceTest_" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
1510 require(std::filesystem::create_directory(directory), "Create isolated UTC source CSV fixture");
1511 for (const auto* name : {"one.jpg", "two.jpg", "three.jpg"}) {
1512 std::ofstream file(directory / name);
1513 file << 'x';
1514 file.close();
1515 require(static_cast<bool>(file), "Create local source existence fixture");
1516 }
1517 const auto target = parseUtcDateTime("2026-11-01T14:56Z");
1518 const auto csv = directory / "capture.csv";
1519 for (int mode = 0; mode <= 7; ++mode) {
1520 std::ofstream fixture(csv);
1521 fixture << "filename,trigger,observed_latitude,observed_longitude,observed_altitude_m,"
1522 "observed_pitch_deg,observed_bank_deg,observed_heading_deg,sdk_fov_deg,projection,map_epsg,"
1523 "commanded_pitch_deg,commanded_bank_deg,commanded_heading_deg,width,height,source_width,"
1524 "source_height,crop_width,crop_height,crop_x,crop_y,pose_received_qpc_100ns,"
1525 "frame_qpc_100ns,request_qpc_100ns";
1526 if (mode) {
1527 fixture << ",capture_utc,utc_absolute_seconds";
1528 if (mode != 7) fixture << ",utc_read_qpc_100ns";
1529 }
1530 fixture << '\n';
1531 int index = 0;
1532 for (const auto* name : {"one.jpg", "two.jpg", "three.jpg"}) {
1533 fixture << name << ",box_tile,52,4,120,90,-90,-90,60,perspective,,90,0,0,"
1534 "64,64,64,64,64,64,0,0,11000000,13000000,12000000";
1535 if (mode == 6) fixture << ",,,";
1536 else if (mode) {
1537 auto utc = target;
1538 if (mode == 2 && index == 2) utc = parseUtcDateTime("2026-11-01T14:57Z");
1539 fixture << ',' << (mode == 5 && index == 2 ? "" : utc.text) << ','
1540 << std::setprecision(17) << utc.absoluteSeconds() + (mode == 3 ? 1 : 0.125);
1541 if (mode != 7) fixture << ',' << (mode == 4 ? 3000000 : 12000000);
1542 }
1543 fixture << '\n';
1544 ++index;
1545 }
1546 fixture.close();
1547 require(static_cast<bool>(fixture), "Write UTC source CSV fixture");
1548 bool rejected = false;
1549 try {
1550 const auto sources = readSources(csv);
1551 require(sources.size() == 3 &&
1552 sources.front().captureUtc.has_value() == (mode != 0 && mode != 6),
1553 "Legacy and fixed-time CSVs retain correct UTC verification status");
1554 if (mode == 1) require(sources.back().captureUtc->text == target.text,
1555 "All source photos retain the same locked UTC");
1556 } catch (const std::runtime_error& error) {
1557 rejected = std::string(error.what()).find("Source") == 0;
1558 if (!rejected) throw;
1559 }
1560 require(rejected == (mode >= 2 && mode != 6),
1561 "Reject changed, drifting, stale, mixed or incomplete source UTC evidence");
1562 }
1563 for (const auto* name : {"capture.csv", "one.jpg", "two.jpg", "three.jpg"})
1564 std::filesystem::remove(directory / name);
1565 std::filesystem::remove(directory);
1566 }
1567 Photo p;
1568 p.info = {};
1569 p.info.latitude = 52; p.info.longitude = 4; p.info.altitude = 120;
1570 p.info.focal = 1000; p.info.cx = 500; p.info.cy = 500; p.info.pitch = 90;
1571 p.info.fov = 60; p.info.poseTime = 1; p.info.frameTime = 2;
1572 p.info.width = 1001; p.info.height = 1001;
1573 p.east = 10; p.north = 20;
1574 double x, y;
1575 require(p.project(22, 26, 0, x, y) && std::abs(x - 600) < 1e-9 && std::abs(y - 450) < 1e-9,
1576 "Ground pinhole projection");
1577 require(p.project(22, 26, 60, x, y) && std::abs(x - 700) < 1e-9 && std::abs(y - 400) < 1e-9,
1578 "Roof height projection");
1579 std::vector<int> visited(11);
1580 parallelRows(11, [&](int row) { visited[static_cast<std::size_t>(row)] = row + 1; });
1581 require(std::accumulate(visited.begin(), visited.end(), 0) == 66, "Every parallel row runs exactly once");
1582 std::vector<int> nestedRows(40, 0);
1583 parallelRows(8, [&](int outer) {
1584 parallelRows(5, [&](int inner) { nestedRows[static_cast<std::size_t>(outer) * 5 + inner] = 1; }, 32);
1585 }, 32);
1586 require(std::accumulate(nestedRows.begin(), nestedRows.end(), 0) == 40,
1587 "Nested row work completes without nested worker teams or lost rows");
1588 bool workerFailed = false;
1589 try { parallelRows(11, [](int row) { if (row == 3) throw std::runtime_error("test worker failure"); }); }
1590 catch (const std::runtime_error& error) { workerFailed = std::string(error.what()) == "test worker failure"; }
1591 require(workerFailed, "Parallel worker errors propagate after joining");
1592 std::cout << "PASS: orthorectification CSV, plane projection, source planning, "
1593 "parallel errors, discontinuities and cancellation.\n";
1594}
1595}
static bool project(const union dc_shot_union *shot, unsigned camera_id, double uncertainty, struct coverage_candidate *candidate)
Convert FC units and estimate geometric detail using this camera's calibration.
static struct coverage_state * covered
static unsigned capacity[COVERAGE_CAMERAS]
static double latitude
static double longitude
size_t size
Definition chdk_pipe.c:143
uint8_t bytes[CHDK_CONTROL_REPLY_SIZE]
Definition chdk_pipe.c:144
Reusable IJG compressor and JPEG publisher.
static void save(const Image &image, const std::filesystem::path &path, const unsigned char *exif=nullptr, std::size_t exifSize=0, bool durable=false)
Publish encoded JPEG bytes, optionally adding one EXIF APP1 segment.
Read bounded rectangular regions from a lossless BGRA disk raster.
cv::Mat read(const cv::Rect &rectangle) const
Read a rectangle into a new BGRA matrix.
Convert one baseline JPEG into a row-major BGRA working raster.
Definition jpeg_stream.h:52
Forward and inverse WGS-84 Transverse Mercator conversion for one UTM zone.
int epsg() const
Get the CRS identifier for this WGS-84 UTM zone.
std::pair< double, double > forward(double latitude, double longitude) const
Project a geographic position into UTM easting and northing.
std::size_t size() const
Return total requested map tiles.
~Orthorectifier()
Release the render state and its caches.
std::optional< UtcDateTime > captureUtc() const
Return the validated common capture UTC target, when present.
void prepareSources(const std::function< bool()> &stopped)
Prepare shared source rasters/mosaic state before repeated rendering.
std::size_t rows() const
Return output tile rows.
Orthorectifier(const MapConfig &config, const std::filesystem::path &csv)
Validate source CSV rows and derive the requested output grid.
std::unique_ptr< Impl > impl_
Definition ortho_map.h:100
OrthoImage renderWindow(std::size_t originX, std::size_t originY, int width, int height, const std::function< bool()> &stopped, const std::function< void(double)> &progress={})
Render an arbitrary bounded window of the configured map grid.
static std::filesystem::path normalizeSources(const std::filesystem::path &csv, const std::filesystem::path &directory, const std::function< bool()> &stopped, unsigned workers=1, unsigned memoryMiB=128)
Rectify JPEG sources into working JPEGs and write a new CSV.
std::size_t sourceCount() const
Return the validated source count without decoding pixels.
static void selfTest()
Run synthetic orthorectification regression cases.
OrthoImage render(std::size_t index, const std::function< bool()> &stopped, const std::function< void(double)> &progress={})
Render one configured output tile.
std::size_t columns() const
Return output tile columns.
std::string sourceFingerprint(const std::function< bool()> &stopped) const
Hash canonical source paths and current JPEG contents in source order.
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 bool started
static struct ear_inference_result queued_evidence completed
Encode BGRA map pixels and publish JPEGs with optional EXIF.
Decode JPEGs with IJG scanlines into BGRA previews or disk-backed rasters.
struct jpeg_compress_struct encoder
Definition lwir_image.c:59
uint8_t row[LWIR_MAX_WIDTH *3U]
Definition lwir_image.c:63
struct jpeg_error_mgr error
Definition lwir_image.c:60
struct jpeg_decompress_struct decoder
Definition lwir_mock.c:55
static int32_t corrected(int32_t raw, const struct lwir_profile *profile)
Apply the profile's linear display correction.
static void color(unsigned intensity, enum lwir_palette palette, unsigned *red, unsigned *green, unsigned *blue)
Map a window intensity to RGB for the selected palette.
static struct usher_camera_profile profile
OrthoLayout orthoLayout(const MapConfig &config)
Compute a north-up, non-overlapping image grid for the requested bounds.
void storageHeadroom(const std::filesystem::path &directory, std::uint64_t bytes)
Require requested free space plus a fixed safety reserve.
static cv::Mat completeMosaicPyramid(const cv::Mat &premultiplied, unsigned workers, const std::function< bool()> &stopped)
Fill unsupported areas using a global push-pull colour pyramid.
void parallelRows(int rows, Function function, unsigned requested=4)
Apply a row operation in parallel without creating nested teams.
std::string fileDigest(const std::filesystem::path &path, const std::function< bool()> &stopped={})
Hash a file in bounded chunks, optionally honoring cancellation.
Definition map_storage.h:89
void publishWorkingFile(const std::filesystem::path &temporary, const std::filesystem::path &target, bool replace=true)
Fsync a completed working file, rename it, then sync its directory.
std::string dataDigest(const void *data, std::size_t size)
Compute a lowercase SHA-256 digest for an in-memory byte range.
Definition map_storage.h:77
void validateJpegSize(int width, int height)
Validate encoded-image dimensions before allocating pixel buffers.
void range(double value, double minimum, double maximum, const std::string &name)
Enforce the inclusive policy bounds for a parsed setting.
Definition route.h:160
void saveWorkingText(const std::filesystem::path &target, const std::string &text)
Save checkpoint text through the durable working-file publisher.
bool captureUtcSampleValid(const UtcDateTime &target, double actual, long long read100ns, long long frame100ns)
Validate that a frame's UTC readback is recent and in the locked second.
Definition utc_time.h:202
constexpr double pi
Definition route.h:44
UtcDateTime parseUtcDateTime(std::string_view text)
Parse a strict Gregorian UTC minute in YYYY-MM-DDTHH:MMZ form.
Definition utc_time.h:115
std::vector< OrthoSource > readUsherSources(const MapConfig &config, const std::filesystem::path &manifest)
Read a photo snapshot and verify each source against profile/evidence.
double number(const std::string &text, const std::string &name)
Read a complete finite numeric route field.
Definition route.h:141
static bool checked(OrtStatus *status, char *error, size_t capacity)
Consume every ORT error object exactly once while copying its diagnostic before release.
Describe the validated photo set and render projected map windows.
Run independent row work with a bounded native thread team.
static const char header[]
Definition pose_log.c:87
static uint64_t rejected
Definition pose_log.c:81
static size_t count
Definition pose_log.c:79
static FILE * output
Definition pose_log.c:77
Fully parsed settings shared by orthomap and XYZ export.
Definition map_config.h:41
std::filesystem::path workingDirectory
Definition map_config.h:57
std::optional< std::size_t > expectedSaved
Definition map_config.h:72
std::string mosaicSeamMethod
Definition map_config.h:50
Centre and pixel geometry for a projected image tile.
Rendered BGRA map plus coverage, source list, and method provenance.
Definition ortho_map.h:78
UTM-aligned extent and tile count for one requested map area.
Definition ortho_map.h:38
One fixed-session source image and its camera/pose evidence.
Definition ortho_map.h:55
std::filesystem::path path
Definition ortho_map.h:56
std::optional< CameraPlane > camera
Definition ortho_map.h:57
OrthoImage renderMosaicRegion(const MapTile &map, int width, int height, const std::function< bool()> &stopped)
Render a mosaic window using the session-wide seam plan.
cv::Matx33d mosaicWarp(const Photo &photo, const cv::Rect &rectangle, int scale, cv::Mat &image, cv::Mat &mask) const
Resample one source over a mosaic rectangle.
cv::Rect mosaicRectangle(const OrthoSource &source, int scale, bool seamPlan=false) const
Bound the projected source footprint on a mosaic grid.
std::vector< OrthoSource > sources
void prepareDiskSources(const std::function< bool()> &stopped)
Prepare disk-backed rasters for low-memory rendering.
Photo & mosaicPhoto(std::size_t index)
Get the mosaic's cached projection and image source.
std::vector< std::filesystem::path > sourceRasters
void prepareMosaicCompletion(const std::vector< cv::UMat > &images, const std::vector< cv::UMat > &originalMasks, const std::vector< std::size_t > &seamViews, const std::function< bool()> &stopped)
Build a shared fill overview while retaining observed support.
Impl(const MapConfig &c, const std::filesystem::path &csv)
Admit a source set and establish the shared projection and grid.
std::vector< MosaicView > mosaicViews
void prepareMosaic(const std::function< bool()> &stopped)
Build the shared seam and exposure plan on first use.
OrthoImage renderRegion(const MapTile &map, int width, int height, const std::function< bool()> &stopped)
Render a window with the selected orthorectification engine.
std::shared_ptr< Photo > sourcePhoto(std::size_t index)
Get a pinned decoded photo for the simple renderer.
std::map< std::pair< std::size_t, std::size_t >, CachedBlock > blocks
std::map< std::size_t, SourcePhoto > sourcePhotos
std::map< std::size_t, CachedPhoto > mosaicPhotos
static struct camera_backend camera
Definition usher.c:521
bool valid
Definition usher.c:537
struct usher_control_result result
Definition usher.c:377
struct captured_image image
Definition usher.c:371
Convert validated USHER AI Camera evidence into GREAT source records.