summaryrefslogtreecommitdiff
path: root/codegen
diff options
context:
space:
mode:
authorJohannes Hofmann <Johannes.Hofmann@gmx.de>2026-10-26 20:05:20 +0100
committerJohannes Hofmann <Johannes.Hofmann@gmx.de>2026-10-26 20:05:20 +0100
commit375d87a9b0ec1cf2fffd705adb870c86144d2400 (patch)
tree1e8e05e7cd58686628d0f4622622456613a42e4d /codegen
parent45425dd047b791fe63f13a46a3251c529cfffc94 (diff)
Replace the C++ implementation with the Rust port
The Rust port (formerly gipfel-rs) is now the official successor to the FLTK/GSL C++ code base. This squashes the port into a single commit and removes the C++ sources (they remain in the git history below). Layout: crates/gipfel-core headless core: hills, panorama geometry, projections (maxima-generated derivatives), pure-Rust LM fit, gipfel: JPEG comment + EXIF I/O, profiles, stitching crates/gipfel-cli command-line tools (export, info, stitch, profiles) crates/gipfel-app Cosmic DE (libcosmic) GUI, binary name `gipfel` codegen/ maxima scripts + regen.sh for the projection code tests/ golden files + C++ reference harnesses (historical) The original README is kept for now; it will be updated to describe the Rust build. gipfel.dat, AUTHORS, ChangeLog and NEWS are unchanged.
Diffstat (limited to 'codegen')
-rw-r--r--codegen/expr2rust.mac31
-rw-r--r--codegen/fixup.py14
-rw-r--r--codegen/lsq_cylindrical.mac38
-rw-r--r--codegen/lsq_rectilinear.mac60
-rwxr-xr-xcodegen/regen.sh16
5 files changed, 159 insertions, 0 deletions
diff --git a/codegen/expr2rust.mac b/codegen/expr2rust.mac
new file mode 100644
index 0000000..e4e6be8
--- /dev/null
+++ b/codegen/expr2rust.mac
@@ -0,0 +1,31 @@
+/*
+ * Rust code generation for gipfel-rs.
+ *
+ * Port of gipfel's expr2c.mac (Copyright 2006-2009 Johannes Hofmann).
+ * Emits one Rust function per line (grep "^pub fn"); integer literals are
+ * converted to float literals by fixup.py afterwards.
+ */
+
+load("stringproc")$
+
+varsubst(v) := ssubst("_", "%", string(v))$
+
+printdecl(vars) := (
+ sprint("let mut", varsubst(first(vars)), ": f64 = 0.0;"),
+ for v in rest(vars) do sprint("let mut", varsubst(v), ": f64 = 0.0;"))$
+
+printdef(expr) := sprint(varsubst(first(expr)), "=", varsubst(second(expr)), ";")$
+
+block2rust(expr) :=
+ if ?equal(op(expr), block) then (
+ printdecl(first(expr)),
+ for d in reverse(rest(reverse(rest(expr)))) do printdef(d),
+ sprint("return", varsubst(last(expr)), ";"))
+ else
+ sprint("return", varsubst(expr), ";")$
+
+expr2rust(funcname, argstr, expr) := (
+ sprint("pub fn", funcname, "(", argstr, ") -> f64 {"),
+ block2rust(subst(pow, "^", optimize(expr))),
+ sprint("}", "
+"))$
diff --git a/codegen/fixup.py b/codegen/fixup.py
new file mode 100644
index 0000000..1131f11
--- /dev/null
+++ b/codegen/fixup.py
@@ -0,0 +1,14 @@
+#!/usr/bin/env python3
+"""Post-process maxima output for Rust:
+ - convert bare integer literals to float literals (1 -> 1.0)
+ - drop maxima noise lines (keep only 'pub fn' lines)
+"""
+import re
+import sys
+
+pat = re.compile(r"(?<![\w.])-?\d+(?![\w.])")
+
+for line in sys.stdin:
+ if not line.startswith("pub fn"):
+ continue
+ sys.stdout.write(pat.sub(lambda m: m.group(0) + ".0", line))
diff --git a/codegen/lsq_cylindrical.mac b/codegen/lsq_cylindrical.mac
new file mode 100644
index 0000000..316d3f9
--- /dev/null
+++ b/codegen/lsq_cylindrical.mac
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2006-2009 Johannes Hofmann <Johannes.Hofmann@gmx.de>
+ */
+
+/*
+ * cylindrical projection model
+ */
+
+x : m_view - c_view$
+y : tan(c_pitch - m_elev)$
+x_rot : y * sin(c_roll) + x * cos(c_roll)$
+y_rot : y * cos(c_roll) - x * sin(c_roll)$
+x_scale : x_rot * scale$
+y_scale : y_rot * scale$
+
+/*
+ * Some mangling for C code generation
+ */
+
+load("expr2rust.mac")$
+
+x_expand : trigexpand(x_scale)$
+y_expand : trigexpand(y_scale)$
+
+args: "c_view: f64, c_pitch: f64, c_roll: f64, scale: f64, k0: f64, k1: f64, x0: f64, m_view: f64, m_elev: f64"$
+
+expr2rust("mac_x", args, x_expand)$
+expr2rust("mac_y", args, y_expand)$
+
+expr2rust("mac_x_dc_view", args, diff(x_expand, c_view))$
+expr2rust("mac_x_dc_pitch", args, diff(x_expand, c_pitch))$
+expr2rust("mac_x_dc_roll", args, diff(x_expand, c_roll))$
+expr2rust("mac_x_dscale", args, diff(x_expand, scale))$
+expr2rust("mac_y_dc_view", args, diff(y_expand, c_view))$
+expr2rust("mac_y_dc_pitch", args, diff(y_expand, c_pitch))$
+expr2rust("mac_y_dc_roll", args, diff(y_expand, c_roll))$
+expr2rust("mac_y_dscale", args, diff(y_expand, scale))$
+
diff --git a/codegen/lsq_rectilinear.mac b/codegen/lsq_rectilinear.mac
new file mode 100644
index 0000000..4dbb36a
--- /dev/null
+++ b/codegen/lsq_rectilinear.mac
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2006-2009 Johannes Hofmann <Johannes.Hofmann@gmx.de>
+ */
+
+/*
+ * rectilinear (pinhole) projection model with distortion correction
+ */
+
+/* switch to cartesian coordinates */
+c_x : cos(m_elev) * cos(m_view - c_view)$
+c_y : cos(m_elev) * sin(m_view - c_view)$
+c_z : sin(m_elev)$
+
+/* pitch rotation */
+c_x_rot : cos(c_pitch) * c_x + sin(c_pitch) * c_z$
+c_y_rot : c_y$
+c_z_rot : -sin(c_pitch) * c_x + cos(c_pitch) * c_z$
+
+/* pinhole projection */
+x : c_y_rot / c_x_rot$
+y : -c_z_rot / c_x_rot$
+
+/* rotation around view axis */
+x_rot : y * sin(c_roll) + x * cos(c_roll) + x0$
+y_rot : y * cos(c_roll) - x * sin(c_roll)$
+
+/* distortion correction */
+d : sqrt(x_rot ^ 2 + y_rot ^ 2)$
+dist_fact : d ^ 2 * k1 + d * k0$
+x_dist : x_rot * (1 + dist_fact) * scale$
+y_dist : y_rot * (1 + dist_fact) * scale$
+
+/*
+ * Some mangling for C code generation
+ */
+
+load("expr2rust.mac")$
+
+x_expand : trigexpand(x_dist)$
+y_expand : trigexpand(y_dist)$
+
+args: "c_view: f64, c_pitch: f64, c_roll: f64, scale: f64, k0: f64, k1: f64, x0: f64, m_view: f64, m_elev: f64"$
+
+expr2rust("mac_x", args, x_expand)$
+expr2rust("mac_y", args, y_expand)$
+
+expr2rust("mac_x_dc_view", args, diff(x_expand, c_view))$
+expr2rust("mac_x_dc_pitch", args, diff(x_expand, c_pitch))$
+expr2rust("mac_x_dc_roll", args, diff(x_expand, c_roll))$
+expr2rust("mac_x_dscale", args, diff(x_expand, scale))$
+expr2rust("mac_x_dk0", args, diff(x_expand, k0))$
+expr2rust("mac_x_dk1", args, diff(x_expand, k1))$
+expr2rust("mac_x_dx0", args, diff(x_expand, x0))$
+expr2rust("mac_y_dc_view", args, diff(y_expand, c_view))$
+expr2rust("mac_y_dc_pitch", args, diff(y_expand, c_pitch))$
+expr2rust("mac_y_dc_roll", args, diff(y_expand, c_roll))$
+expr2rust("mac_y_dscale", args, diff(y_expand, scale))$
+expr2rust("mac_y_dk0", args, diff(y_expand, k0))$
+expr2rust("mac_y_dk1", args, diff(y_expand, k1))$
+expr2rust("mac_y_dx0", args, diff(y_expand, x0))$
diff --git a/codegen/regen.sh b/codegen/regen.sh
new file mode 100755
index 0000000..dbf9f6b
--- /dev/null
+++ b/codegen/regen.sh
@@ -0,0 +1,16 @@
+#!/bin/sh
+# Regenerate the symbolic projection functions with maxima.
+# Run from anywhere; requires maxima (and rustfmt if available).
+set -e
+cd "$(dirname "$0")"
+OUT=../crates/gipfel-core/src/projection/generated
+
+for p in rectilinear cylindrical; do
+ maxima -b lsq_${p}.mac | python3 fixup.py > ${OUT}/${p}_funcs.rs
+done
+
+if command -v rustfmt >/dev/null; then
+ rustfmt ${OUT}/rectilinear_funcs.rs ${OUT}/cylindrical_funcs.rs 2>/dev/null || true
+fi
+echo "generated:"
+wc -l ${OUT}/*_funcs.rs