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>
53using fsPath = std::filesystem::path;
54std::vector<std::string> csvFields(
const std::string& line) {
55 std::vector<std::string>
result;
57 bool quoted =
false, closed =
false;
58 for (std::size_t i = 0; i < line.size(); ++i) {
59 const char c = line[i];
61 if (c ==
'"' && i + 1 < line.size() && line[i + 1] ==
'"') { value +=
'"'; ++i; }
62 else if (c ==
'"') { quoted =
false; closed =
true; }
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;
69 if (quoted)
throw std::runtime_error(
"Unclosed source CSV quote");
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());
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");
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;
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];
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");
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);
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");
148 throw std::runtime_error(
"Unsupported source lens profile");
149 s.distorted =
profile ==
"raspberry_pi_ai";
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");
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");
168 const auto timestamp = [&](
const char* key) {
169 const auto n = integer(key, 1, 9e15);
170 return static_cast<long long>(n);
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()) {
178 s.utcAbsoluteSeconds = get(
"utc_absolute_seconds");
179 s.utcRead100ns = timestamp(
"utc_read_qpc_100ns");
181 throw std::runtime_error(
"Source capture UTC readback is stale, reversed or outside the locked second");
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);
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");
204struct RectificationCache {
205 std::array<double, 9> calibration{};
206 std::array<cv::Mat, 4> mapX, mapY;
207 std::array<int, 4> inset{};
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))) {
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) {
220 calibration = current;
229 double east = 0, north = 0, c = 1, s = 0, scale = 1;
230 std::vector<unsigned char> bgra;
232 void load(
int maxSide = 0, RectificationCache* rectification =
nullptr) {
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);
252 int channelInset = 0;
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];
264 mx = rectification->mapX[channel]; my = rectification->mapY[channel];
265 channelInset = rectification->inset[channel];
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);
279#if defined(__x86_64__)
280 cv::convertMaps(mx, my, rectification->mapX[channel], rectification->mapY[channel],
282 mx = rectification->mapX[channel]; my = rectification->mapY[channel];
284 rectification->mapX[channel] = mx; rectification->mapY[channel] = my;
286 rectification->inset[channel] = channelInset;
289 validInset = std::max(validInset, channelInset);
290 cv::remap(channels[channel],
corrected, mx, my, cv::INTER_LINEAR, cv::BORDER_CONSTANT);
296 throw std::runtime_error(
"Rectified photo storage differs from the decoded image");
297 for (std::size_t offset = 0; offset < bgra.size(); ++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");
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;
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;
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;
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));
330using Photos = std::vector<std::shared_ptr<Photo>>;
337 throw std::runtime_error(
"Orthorectified tile dimensions must be at most 4096");
342 OrthoLayout result{corner.first, corner.second, corner.first, corner.second, 0, 0, projection.
epsg()};
344 const auto p = projection.
forward(lat, lon);
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);
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");
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");
375 cv::pyrDown(levels.back(), reduced);
376 levels.push_back(std::move(reduced));
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];
383 for (std::size_t level = levels.size() - 1; level > 0; --level) {
385 cv::pyrUp(levels[level], expanded, levels[level - 1].
size());
386 auto& destination = levels[level - 1];
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;
399 return levels.front();
414 std::shared_ptr<Photo>
sourcePhoto(std::size_t index);
433 cv::Matx33d
mosaicWarp(
const Photo& photo,
const cv::Rect& rectangle,
int scale, cv::Mat&
image, cv::Mat& mask)
const;
436 const std::vector<std::size_t>& seamViews,
const std::function<
bool()>& stopped);
449 throw std::runtime_error(
"Capture manifest saved count disagrees with source CSV box photographs");
452 throw std::runtime_error(
"Unsupported orthomap engine");
454 if ((
config.orthoEngine ==
"simple" || mosaic) && !std::isfinite(
config.referencePlane))
456 if ((
config.orthoEngine ==
"simple" || mosaic) && (!std::isfinite(
config.referencePlane) ||
458 throw std::runtime_error(
"Mosaicking requires a valid capture reference plane or the mode's reference_plane_amsl_m");
460 west = layout.west;
north = layout.north;
cols = layout.columns;
rows = layout.rows;
464 :
impl_(std::make_unique<
Impl>(config, csv)) {}
472 std::ostringstream identity;
473 for (
const auto& source :
impl_->sources)
474 identity << std::filesystem::canonical(source.path).generic_string() <<
'\n'
476 const auto text = identity.str();
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");
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))
500 if (oldest == cache.end())
throw std::runtime_error(
"Active source photos exceed decoded-photo cache budget");
501 const auto& photo = *oldest->second.photo;
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);
514 found = cache.emplace(index,
SourcePhoto{std::move(photo), 0}).first;
517 return found->second.photo;
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");
533 for (std::size_t index = 0; index <
sources.size(); ++index) {
534 if (stopped())
throw std::runtime_error(
"Source preparation canceled");
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";
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");
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;
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);
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;
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)));
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});
603 cv::extractChannel(input, plane, channel);
604 mx -= left; my -= top;
605 cv::remap(plane,
corrected, mx, my, cv::INTER_LINEAR, cv::BORDER_CONSTANT);
607 stripe.at<cv::Vec4b>(
row, firstColumn + column)[channel] =
corrected.at<
unsigned char>(
row, column);
610 output.write(
reinterpret_cast<const char*
>(stripe.data),
static_cast<std::streamsize
>(stripe.total() * 4));
613 if (!
output || 2 * inset > std::min(info.width, info.height) - 64)
614 throw std::runtime_error(
"Rectification write failed or unsupported camera crop");
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);
624 if (!cropped)
throw std::runtime_error(
"Cannot write rectified raster");
626 std::filesystem::remove(decoded); std::filesystem::remove(correctedPath);
628 if (
fileDigest(info.path, stopped) != sourceHash)
629 throw std::runtime_error(
"Photo changed during preparation; no source checkpoint committed");
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;
641 if (
impl_->config.lowMemory)
impl_->prepareDiskSources(stopped);
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");
658 [](
const auto& a,
const auto& b) { return a.second.touched < b.second.touched; });
659 photoBytes -= oldest->second.photo.bgra.size();
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);
670 cached =
mosaicPhotos.emplace(index, std::move(entry)).first;
673 return cached->second.photo;
686 const double c = std::cos(heading), s = std::sin(heading);
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)}) {
694 cv::Point2d((u - source.
cx) * ray, -(v - source.
cy) * ray);
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);
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;
705 double actualEast =
west, actualSouth =
north;
709 actualEast = std::max(actualEast, corner.first);
710 actualSouth = std::min(actualSouth, corner.second);
712 endX = std::ceil((actualEast -
west) / pixel) + pad;
713 endY = std::ceil((
north - actualSouth) / pixel) + pad;
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)};
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;
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);
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();
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);
770 if (maxDepth <= 1e-9) { output.setTo(cv::Scalar(0)); return; }
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) {
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);
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);
798 if (photo.info.camera)
799 cv::warpPerspective(input, output, mapping, output.size(), cv::INTER_LINEAR | cv::WARP_INVERSE_MAP,
800 cv::BORDER_REPLICATE);
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);
806 warp(warp, destination, local);
808 cv::cvtColor(warped,
image, cv::COLOR_BGRA2BGR);
809 mask.create(rectangle.size(), CV_8U);
810 const int border = 1 + photo.validInset;
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;
831 const auto prepareStarted = std::chrono::steady_clock::now();
835 std::size_t pixels = 0;
836 for (
const auto& source :
sources) {
838 pixels +=
static_cast<std::size_t
>(rectangle.width) * rectangle.height;
842 if (pixels <=
static_cast<std::size_t
>(
config.mosaicOverviewPixels) &&
843 (!
config.lowMemory || globalPixels <=
config.mosaicOverviewPixels))
break;
846 throw std::runtime_error(
"Mosaic overview exceeds its memory budget; split the map area");
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());
856 if (stopped()) throw std::runtime_error(
"Mosaic overview canceled");
857 const auto rectangle = mosaicRectangle(sources[index], seamScale, true);
858 if (rectangle.empty()) return;
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]);
871 for (std::size_t i = 0; i <
sources.size(); ++i) {
872 if (stopped())
throw std::runtime_error(
"Great overview canceled");
876 throw std::runtime_error(
"Great seam plan exceeds 4096 overlapping sources; split the map area");
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());
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);
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);
917 cv::detail::GraphCutSeamFinder finder(cv::detail::GraphCutSeamFinderBase::COST_COLOR_GRAD);
918 finder.find(images, corners, masks);
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);
925 if (stopped())
throw std::runtime_error(
"Great seam optimization canceled");
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';
934 <<
" source footprints; global seams reused for every output tile.\n";
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;
951 actualEast = std::max(actualEast, corner.first); actualSouth = std::min(actualSouth, corner.second);
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");
963 const auto rectangle = view.rectangle & cv::Rect(0, 0, width, height);
964 if (rectangle.empty())
continue;
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);
991 std::cout <<
"Global source-derived completion ready: " << width <<
" x " << height
992 <<
"; completed pixels are tracked separately from observed photo colour.\n";
1004 const std::function<
bool()>& stopped) {
1012 const auto count =
static_cast<std::size_t
>(width) * height;
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)),
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));
1024 if (stopped())
throw std::runtime_error(
"Great blending canceled");
1025 const cv::Rect footprint = view.footprint;
1026 if ((footprint & region).empty())
continue;
1028 cv::Mat
image, mask;
1030 if (cv::countNonZero(mask) == 0)
continue;
1032 result.sources.push_back(photo.info);
1033 cv::Mat selected(height, width, CV_8U, cv::Scalar(0));
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;
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;
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));
1065 cv::Mat blended, blendedMask;
1066 if (fed) blender.blend(blended, blendedMask);
1067 std::atomic<std::size_t> reconstructed{0}, reconstructionBlend{0};
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;
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];
1084 result.confidence[index] = observed ? 64 : 16;
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]);
1102 weight = weight * weight * (3 - 2 * weight);
1104 for (
int channel = 0; channel < 3; ++channel)
1105 pixel[channel] = cv::saturate_cast<unsigned char>(weight * pixel[channel] +
1107 if (observed) {
result.confidence[index] = 48; ++rowBlended; }
1108 else ++rowReconstructed;
1112 reconstructed += rowReconstructed; reconstructionBlend += rowBlended;
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;
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; }))
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);
1131 std::getline(input, line);
1132 const auto header = csvFields(line);
1133 std::map<std::string, std::size_t>
columns;
1135 const auto result = directory /
"photos.csv";
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);
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;
1159 source.
width * 4, 100);
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;
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();
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); }
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";
1191 for (std::size_t i = 0; i < fields.size(); ++i) {
1194 for (
const auto ch : fields[i]) {
if (ch ==
'"')
output <<
'"';
output << ch; }
1199 if (input.bad())
throw std::runtime_error(
"Cannot read source CSV during rectification");
1201 if (!
output)
throw std::runtime_error(
"Cannot finalize rectified source CSV");
1202#if defined(__GLIBC__)
1203 if (workers > 1) malloc_trim(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';
1223 const std::function<
bool()>& stopped) {
1225 const auto& state = *
this;
1227 result.width = outputWidth;
result.height = outputHeight;
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);
1235 std::sort(nearest.begin(), nearest.end());
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);
1253 const double left = map.
east - (
result.width - 1) *
config.mapPixelSize / 2;
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;
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;
1276 } else if (score < best) {
1278 std::copy(colour, colour + 4, pixel);
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));
1286 if (pixel[3]) result.confidence[index] = 64;
1289 result.validFraction =
static_cast<double>(std::count(
result.confidence.begin(),
result.confidence.end(), 64)) /
count;
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;
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 ||
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");
1315 result.referencePlane = state.config.referencePlane;
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()) {
1336 state.north - (by * blockSize + blockSize / 2.0) * config.
mapPixelSize,
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);
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;
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");
1368 image = state.renderRegion(map,
static_cast<int>(regionSize),
static_cast<int>(regionSize), stopped);
1370 std::filesystem::create_directories(blockPath.parent_path());
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()));
1377 if (!payload)
throw std::runtime_error(
"Cannot save working block");
1383 cached = state.blocks.emplace(key, std::move(block)).first;
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);
1401 if (block.confidence[input]) ++
valid;
1403 reconstructed += block.confidence[input] == 16;
1404 reconstructionBlend += block.confidence[input] == 48;
1407 if (progress) progress(
static_cast<double>(++completedBlocks) / blocks);
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";
1423 const auto require = [](
bool value,
const char* message) {
1424 if (!value)
throw std::runtime_error(std::string(
"Orthorectification test: ") + message);
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");
1431 source.
path = directory /
"source.jpg";
1433 source.
cx = 47.5; source.
cy = 39.5; source.
distorted =
true;
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);
1441 RectificationCache cache;
1442 for (
int scenario = 0; scenario < 10; ++scenario) {
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");
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}) {
1464 require(cache.prepare(
camera, 96 * 80) && cache.mapX[0].empty(),
1465 "Changed dimensions invalidate cached maps");
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);
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);
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");
1484 bool emptyRejected =
false, canceled =
false;
1486 catch (
const std::runtime_error&) { emptyRejected =
true; }
1488 catch (
const std::runtime_error&) { canceled =
true; }
1489 require(emptyRejected && canceled,
"Global completion rejects no-source images and honors cancellation");
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;
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;
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;
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");
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);
1515 require(
static_cast<bool>(file),
"Create local source existence fixture");
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";
1527 fixture <<
",capture_utc,utc_absolute_seconds";
1528 if (mode != 7) fixture <<
",utc_read_qpc_100ns";
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 <<
",,,";
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);
1547 require(
static_cast<bool>(fixture),
"Write UTC source CSV fixture");
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) {
1560 require(
rejected == (mode >= 2 && mode != 6),
1561 "Reject changed, drifting, stale, mixed or incomplete source UTC evidence");
1563 for (
const auto* name : {
"capture.csv",
"one.jpg",
"two.jpg",
"three.jpg"})
1564 std::filesystem::remove(directory / name);
1565 std::filesystem::remove(directory);
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;
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);
1581 require(std::accumulate(visited.begin(), visited.end(), 0) == 66,
"Every parallel row runs exactly once");
1582 std::vector<int> nestedRows(40, 0);
1584 parallelRows(5, [&](
int inner) { nestedRows[
static_cast<std::size_t
>(outer) * 5 + inner] = 1; }, 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";
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]
uint8_t bytes[CHDK_CONTROL_REPLY_SIZE]
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.
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_
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 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
uint8_t row[LWIR_MAX_WIDTH *3U]
struct jpeg_error_mgr error
struct jpeg_decompress_struct decoder
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.
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.
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.
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.
UtcDateTime parseUtcDateTime(std::string_view text)
Parse a strict Gregorian UTC minute in YYYY-MM-DDTHH:MMZ form.
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.
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[]
Fully parsed settings shared by orthomap and XYZ export.
std::filesystem::path workingDirectory
double mosaicReferencePlane
std::optional< std::size_t > expectedSaved
std::string mosaicSeamMethod
Centre and pixel geometry for a projected image tile.
Rendered BGRA map plus coverage, source list, and method provenance.
UTM-aligned extent and tile count for one requested map area.
One fixed-session source image and its camera/pose evidence.
std::filesystem::path path
std::optional< CameraPlane > camera
std::shared_ptr< Photo > photo
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::Mat mosaicSourceDistance
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
std::size_t sourcePhotoClock
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::size_t sourcePhotoBytes
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
struct usher_control_result result
struct captured_image image
Convert validated USHER AI Camera evidence into GREAT source records.