44constexpr double pi = 3.14159265358979323846;
97 return interval > 0 && elapsed >= nextPhoto;
130inline std::string
trim(
const std::string& text) {
131 const auto first = text.find_first_not_of(
" \t\r\n");
132 if (first == std::string::npos)
return {};
133 return text.substr(first, text.find_last_not_of(
" \t\r\n") - first + 1);
141inline double number(
const std::string& text,
const std::string& name) {
142 std::size_t used = 0;
145 value = std::stod(text, &used);
146 }
catch (
const std::exception&) {
147 throw std::runtime_error(
"Invalid number for " + name +
": " + text);
149 if (used != text.size() || !std::isfinite(value))
150 throw std::runtime_error(
"Invalid number for " + name +
": " + text);
160inline void range(
double value,
double minimum,
double maximum,
const std::string& name) {
161 if (value < minimum || value > maximum)
162 throw std::runtime_error(name +
" must be between " + std::to_string(minimum) +
163 " and " + std::to_string(maximum));
171 std::istringstream input(address);
172 int octet[4]{}, port = 0;
174 if (!(input >> octet[0] >> separator[0] >> octet[1] >> separator[1] >>
175 octet[2] >> separator[2] >> octet[3] >> separator[3] >> port) ||
176 separator[0] !=
'.' || separator[1] !=
'.' || separator[2] !=
'.' ||
177 separator[3] !=
':' || input.peek() != std::char_traits<char>::eof() ||
178 address.find_first_not_of(
"0123456789.:") != std::string::npos ||
179 port < 1 || port > 65535 ||
180 std::any_of(std::begin(octet), std::end(octet), [](
int value) { return value < 0 || value > 255; }))
181 throw std::runtime_error(
"ivy.bus_address must be a full IPv4 broadcast/multicast address:port (1-65535)");
189 return std::remainder(end -
start, 360.0);
199 const double lat1 = a.
lat *
pi / 180;
200 const double lat2 = b.
lat *
pi / 180;
201 const double dlat = lat2 - lat1;
203 const double h = std::pow(std::sin(dlat / 2), 2) +
204 std::cos(lat1) * std::cos(lat2) * std::pow(std::sin(dlon / 2), 2);
205 return 2 *
earthRadius * std::asin(std::sqrt(std::clamp(h, 0.0, 1.0)));
216 std::map<std::string, std::string> values;
217 std::string section, line;
218 std::size_t lineNumber = 0;
219 while (std::getline(input, line)) {
221 line =
trim(line.substr(0, line.find_first_of(
";#")));
222 if (line.empty())
continue;
223 if (line.front() ==
'[' && line.back() ==
']') {
224 section =
trim(line.substr(1, line.size() - 2));
225 if (section !=
"start" && section !=
"end" && section !=
"camera" &&
226 section !=
"ivy" && section !=
"simulation")
227 throw std::runtime_error(
"Unknown INI section on line " + std::to_string(lineNumber));
230 const auto equal = line.find(
'=');
231 if (section.empty() || equal == std::string::npos)
232 throw std::runtime_error(
"Expected section/key=value on INI line " +
233 std::to_string(lineNumber));
234 const std::string key = section +
"." +
trim(line.substr(0, equal));
235 const std::string value =
trim(line.substr(equal + 1));
236 if (value.empty() || !values.emplace(key, value).second)
237 throw std::runtime_error(
"Empty or duplicate INI key: " + key);
239 if (input.bad())
throw std::runtime_error(
"Failed while reading INI file");
240 const auto required = [&](
const std::string& key) {
241 const auto found = values.find(key);
242 if (found == values.end())
throw std::runtime_error(
"Missing INI key: " + key);
243 return number(found->second, key);
245 const auto optional = [&](
const std::string& key,
double fallback) {
246 const auto found = values.find(key);
247 return found == values.end() ? fallback :
number(found->second, key);
249 const auto boolean = [&](
const std::string& key,
bool fallback) {
250 const double value = optional(key, fallback ? 1 : 0);
251 if (value != 0 && value != 1)
throw std::runtime_error(key +
" must be 0 or 1");
254 const std::string allowed[] = {
255 "start.lat",
"start.lon",
"start.agl",
"end.lat",
"end.lon",
"end.agl",
256 "camera.speed_mps",
"camera.photo_interval_seconds",
"camera.settle_seconds",
257 "camera.fov_degrees",
"camera.jpeg_quality",
"camera.output_directory",
"camera.startup_flight",
258 "camera.lock_start_altitude",
"camera.jpeg_width",
"camera.jpeg_height",
259 "camera.grab",
"camera.tile_settle_seconds",
"camera.tile_overlap",
"camera.map_camera_agl_m",
260 "camera.capture_inset_pixels",
"camera.lens_profile",
261 "camera.shutter_speed_seconds",
"camera.iso",
"camera.hardware_identifier",
262 "camera.capture_margin",
263 "camera.lens_k1",
"camera.lens_k2",
"camera.lens_blue_scale",
"camera.lens_red_scale",
264 "camera.auto_enter_flight",
"camera.startup_timeout_seconds",
"camera.startup_airport",
265 "camera.startup_maximize_minimized",
266 "ivy.enabled",
"ivy.bus_address",
"ivy.aircraft_id",
"simulation.utc_datetime",
267 "simulation.weather_preset"
269 for (
const auto& entry : values)
270 if (std::find(std::begin(allowed), std::end(allowed), entry.first) == std::end(allowed))
271 throw std::runtime_error(
"Unknown INI key: " + entry.first);
274 config.
start = {required(
"start.lat"), required(
"start.lon"), required(
"start.agl")};
275 config.
end = {required(
"end.lat"), required(
"end.lon"), required(
"end.agl")};
281 config.
speed = optional(
"camera.speed_mps", config.
speed);
282 config.
interval = optional(
"camera.photo_interval_seconds", config.
interval);
283 config.
settle = optional(
"camera.settle_seconds", config.
settle);
284 config.
fov = optional(
"camera.fov_degrees", config.
fov);
285 const auto lens = values.find(
"camera.lens_profile");
286 if (lens != values.end()) config.
lensProfile = lens->second;
288 throw std::runtime_error(
"camera.lens_profile must be simconnect or raspberry_pi_ai");
289 if (config.
piCamera() && values.count(
"camera.fov_degrees"))
290 throw std::runtime_error(
"Pi AI lens fixes the optical FOV; omit camera.fov_degrees");
294 range(iso, 1, 65535,
"camera.iso");
295 if (std::floor(iso) != iso)
296 throw std::runtime_error(
"camera.iso must be an integer");
298 const auto hardware = values.find(
"camera.hardware_identifier");
301 config.
lens.
k1 = optional(
"camera.lens_k1", config.
lens.
k1);
302 config.
lens.
k2 = optional(
"camera.lens_k2", config.
lens.
k2);
309 if (!config.
piCamera() && (values.count(
"camera.lens_k1") || values.count(
"camera.lens_k2") ||
310 values.count(
"camera.lens_blue_scale") || values.count(
"camera.lens_red_scale")))
311 throw std::runtime_error(
"Lens effect overrides require lens_profile=raspberry_pi_ai");
312 const double grab = optional(
"camera.grab", config.
grab);
313 if (grab != 2 && grab != 3)
throw std::runtime_error(
"camera.grab must be 2 (line flight) or 3 (box tiles)");
314 config.
grab =
static_cast<int>(grab);
317 config.
captureMargin = boolean(
"camera.capture_margin",
false);
318 config.
mapCameraAgl = optional(
"camera.map_camera_agl_m", 0);
319 const double inset = optional(
"camera.capture_inset_pixels", 0);
320 range(inset, 0, 256,
"camera.capture_inset_pixels");
321 if (std::floor(inset) != inset)
322 throw std::runtime_error(
"camera.capture_inset_pixels must be an integer");
330 throw std::runtime_error(
"map_camera_agl_m requires grab=3 and lock_start_altitude=1");
336 const auto airport = values.find(
"camera.startup_airport");
337 if (airport != values.end()) config.
startupAirport = airport->second;
340 return (value >=
'A' && value <=
'Z') || (value >=
'0' && value <=
'9');
342 throw std::runtime_error(
"camera.startup_airport must be a four-character uppercase ICAO airport code");
344 const auto ivyAddress = values.find(
"ivy.bus_address");
345 if (ivyAddress != values.end()) config.
ivyAddress = ivyAddress->second;
347 const double aircraftId = optional(
"ivy.aircraft_id", config.
ivyAircraftId);
348 range(aircraftId, -1, 255,
"ivy.aircraft_id");
349 if (std::floor(aircraftId) != aircraftId)
350 throw std::runtime_error(
"ivy.aircraft_id must be an integer (-1 = any)");
352 const auto utc = values.find(
"simulation.utc_datetime");
353 if (utc != values.end() && utc->second !=
"unchanged") {
356 }
catch (
const std::invalid_argument&
error) {
357 throw std::runtime_error(std::string(
"simulation.utc_datetime: ") +
error.what());
360 const auto weather = values.find(
"simulation.weather_preset");
361 if (weather != values.end()) config.
weatherPreset = weather->second;
363 throw std::runtime_error(
"simulation.weather_preset must be unchanged or clear_sky");
364 const double quality = optional(
"camera.jpeg_quality", config.
quality);
365 const double jpegWidth = optional(
"camera.jpeg_width", config.
jpegWidth);
366 const double jpegHeight = optional(
"camera.jpeg_height", config.
jpegHeight);
367 range(jpegWidth, 1, 16384,
"camera.jpeg_width");
368 range(jpegHeight, 1, 16384,
"camera.jpeg_height");
369 if (std::floor(jpegWidth) != jpegWidth || std::floor(jpegHeight) != jpegHeight)
370 throw std::runtime_error(
"camera.jpeg_width and camera.jpeg_height must be integer pixel counts");
371 config.
jpegWidth =
static_cast<int>(jpegWidth);
372 config.
jpegHeight =
static_cast<int>(jpegHeight);
376 throw std::runtime_error(
"Pi AI lens requires grab=3, ordinary camera height and 2028x1520 JPEGs");
379 throw std::runtime_error(
"photo_interval_seconds must be -1 (disabled) or a positive number of seconds");
381 range(config.
fov, 10, 120,
"fov_degrees");
383 throw std::runtime_error(
"Mapping camera needs at least 1.05 degrees FOV; reduce map_camera_agl_m "
384 "or increase start AGL/fov_degrees (MSFS clamps narrower lenses)");
385 range(quality, 1, 100,
"jpeg_quality");
386 if (std::floor(quality) != quality)
throw std::runtime_error(
"jpeg_quality must be an integer");
387 config.
quality =
static_cast<unsigned long>(quality);
388 const auto output = values.find(
"camera.output_directory");
390 const auto startup = values.find(
"camera.startup_flight");
391 if (startup != values.end()) config.
startupFlight = startup->second;
392 config.
output = base / (
output == values.end() ? std::filesystem::path(config.
piCamera() ? L
"pictures" : L
"mytiles") :
393 std::filesystem::u8path(
output->second));
395 range(length, 1, 100000,
"route length in meters");
398 throw std::runtime_error(
"grab=3 needs opposite corners of a nonzero-width, nonzero-height bounding box");
409 std::ifstream input(path);
410 if (!input)
throw std::runtime_error(
"Cannot open INI file: " + path.u8string());
411 return parseConfig(input, std::filesystem::absolute(path).parent_path());
436 double at(
double ground,
double routeAgl,
bool settled) {
437 if (!std::isfinite(ground) || !std::isfinite(routeAgl))
438 throw std::runtime_error(
"Nonfinite camera terrain/AGL height");
470 throw std::runtime_error(
"Route time must be finite and nonnegative");
472 const double t = phase <= 1 ? phase : 2 - phase;
Optical profiles and EXIF settings for simulated survey cameras.
double at(double ground, double routeAgl, bool settled)
Compute camera AMSL altitude for one terrain sample.
bool locked() const
Report whether fixed-start AMSL has been latched.
std::optional< double > lockedMsl_
CameraAltitude(const Config &config)
Initialize altitude policy from a validated route configuration.
double legSeconds() const
Return one-way travel duration at the configured speed.
double aglAt(const Point &point) const
Interpolate route AGL from distance along the endpoint segment.
Point at(double seconds) const
Return the interpolated endpoint-to-endpoint route position.
Route(const Config &config)
Construct a route and cache its great-circle endpoint distance.
double length() const
Return route length in metres.
static bool start(uint32_t session)
Admit a nonzero session and discard correlation from the previous one.
Crop, resample, and rotate BGRA images into the requested camera frame.
struct jpeg_error_mgr error
Config parseConfig(std::istream &input, const std::filesystem::path &base)
Parse and validate a route INI stream relative to a base directory.
std::string trim(const std::string &text)
Strip the ASCII whitespace allowed around route INI tokens.
Config loadConfig(const std::filesystem::path &path)
Open and parse a route configuration file.
double distance(const Point &a, const Point &b)
Estimate surface distance between two route points.
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.
UtcDateTime parseUtcDateTime(std::string_view text)
Parse a strict Gregorian UTC minute in YYYY-MM-DDTHH:MMZ form.
constexpr double earthRadius
void validateIvyAddress(const std::string &address)
Validate the syntax of an Ivy IPv4 bus endpoint.
double number(const std::string &text, const std::string &name)
Read a complete finite numeric route field.
double longitudeDelta(double start, double end)
Compute the shortest signed longitude change across the antimeridian.
User-supplied exposure labels written to simulated-photo EXIF.
void validate() const
Reject values that cannot be represented as bounded EXIF metadata.
std::string hardwareIdentifier
Validated camera, route, simulator-startup, and output settings.
bool startupMaximizeMinimized
std::string startupFlight
CameraExifSettings exifSettings
std::string startupAirport
double cameraFov() const
Compute field of view at the selected map-camera height.
std::string weatherPreset
double viewportFov(int width, int height) const
Choose the simulator viewport field of view.
double cameraAgl(double nominalAgl) const
Resolve the camera's height above ground.
bool piCamera() const
Check for the calibrated Raspberry Pi AI camera optics path.
std::filesystem::path weatherReadback
std::filesystem::path iniDirectory
std::optional< UtcDateTime > startupUtc
bool timedPhotoDue(double elapsed, double nextPhoto) const
Check whether an enabled timed capture has reached its deadline.
std::filesystem::path output
Engineering lens-distortion and lateral colour-scale parameters.
static double viewportFov(int sourceWidth, int sourceHeight, int inset)
Find the simulator vertical FOV that yields this captured viewport.
static constexpr double clearDayShutterSeconds
static constexpr int height
static constexpr int width
Geographic route point; latitude/longitude are degrees and AGL is metres.
One timestamped geographic position for a replay annotation.
static double seconds(Clock::duration duration)
Convert a monotonic interval without truncating fractional seconds.
Validate Gregorian UTC targets and qualify simulator time samples.