Free, open-source 3D sculpting for Linux, macOS and Windows: dense meshes, remeshing, painting, built-in AI (image/text to 3D), Send to Unreal, game-ready LODs, Gaussian splats and OpenUSD.
C++ 3d3d-sculptingcppdear-imguidigital-sculptingfbxgame-developmentgaussian-splattinggltfimage-to-3dmcpopenglopenusdsculptingtext-to-3dunreal-engine
Phone link core: LAN HTTP server, pairing, resumable uploads into the Captures inbox, face feed (WebRTC via libdatachannel + Mbed TLS, optional), AR export (GLB + USDZ), QR codes, capture and face pages
islandmed73aab79f3332
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 79828697..218f0a7c 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -97,6 +97,7 @@ add_library(sculptforge_core STATICsrc/io/BlenderBridge.cppsrc/io/BlenderCommands.cppsrc/io/StlIO.cpp+ src/io/ArExport.cpp)target_include_directories(sculptforge_core PUBLIC src PRIVATE third_party/stb third_party/ufbx third_party/xatlas third_party/mikktspace third_party/tinyexr)# Third-party C code: compile quietly.@@ -141,6 +142,31 @@ else()target_compile_options(sculptforge_ai PRIVATE -Wall -Wextra -Wno-unused-parameter)endif()+# --- Phone link (src/link/PhoneLink.h, docs/phone-link.md): the capture page server, AR export, live face feed ---+option(SCULPTFORGE_WEBRTC "Live face feed over WebRTC (fetches libdatachannel and Mbed TLS; OFF: capture and AR only)" ON)+include(cmake/EmbedFile.cmake)+embed_files(${CMAKE_CURRENT_BINARY_DIR}/EmbeddedPhone.cpp sf::phone_assets+ capturePage ${CMAKE_CURRENT_SOURCE_DIR}/assets/phone/capture.html+ facePage ${CMAKE_CURRENT_SOURCE_DIR}/website/link/index.html)+add_library(sculptforge_phone STATIC ${CMAKE_CURRENT_BINARY_DIR}/EmbeddedPhone.cpp src/link/HttpServer.cpp+ src/link/PhoneLink.cpp src/link/FaceFeed.cpp src/link/PhoneRtc.cpp src/link/Qr.cpp third_party/qrcodegen/qrcodegen.cpp)+target_include_directories(sculptforge_phone PRIVATE third_party/qrcodegen)+target_link_libraries(sculptforge_phone PUBLIC sculptforge_ai)+if(SCULPTFORGE_WEBRTC)+ include(cmake/WebRTC.cmake)+ target_link_libraries(sculptforge_phone PRIVATE LibDataChannel::LibDataChannelStatic MbedTLS::MbedTLS)+ target_compile_definitions(sculptforge_phone PRIVATE SF_HAVE_WEBRTC=1)+else()+ target_compile_definitions(sculptforge_phone PRIVATE SF_HAVE_WEBRTC=0)+endif()+if(MSVC)+ target_compile_options(sculptforge_phone PRIVATE /W4 /utf-8 /permissive-)+ set_source_files_properties(third_party/qrcodegen/qrcodegen.cpp PROPERTIES COMPILE_OPTIONS "/w")+else()+ target_compile_options(sculptforge_phone PRIVATE -Wall -Wextra -Wno-unused-parameter)+ set_source_files_properties(third_party/qrcodegen/qrcodegen.cpp PROPERTIES COMPILE_OPTIONS "-w")+endif()+# --- Headless command-line tool (pipelines, build farms, AI agents) ---add_executable(sculptforge-cli src/cli/main.cpp src/cli/McpServer.cpp)target_compile_definitions(sculptforge-cli PRIVATE SCULPTFORGE_VERSION="${PROJECT_VERSION}")@@ -205,7 +231,7 @@ if(SCULPTFORGE_BUILD_APP)src/render/Clusters.cppsrc/render/Renderer.cpp)- target_link_libraries(sculptforge PRIVATE sculptforge_core sculptforge_ai imgui SDL3::SDL3)+ target_link_libraries(sculptforge PRIVATE sculptforge_core sculptforge_ai sculptforge_phone imgui SDL3::SDL3)target_include_directories(sculptforge PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/third_party/lucide)target_compile_definitions(sculptforge PRIVATE SCULPTFORGE_VERSION="${PROJECT_VERSION}"SF_UPDATE_CHECK=$<BOOL:${SCULPTFORGE_UPDATE_CHECK}>)@@ -385,7 +411,7 @@ if(SCULPTFORGE_BUILD_TESTS)src/render/Clusters.cpp)target_include_directories(sculptforge_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/tests)target_compile_definitions(sculptforge_tests PRIVATE SF_TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/tests/data")- target_link_libraries(sculptforge_tests PRIVATE sculptforge_core sculptforge_ai)+ target_link_libraries(sculptforge_tests PRIVATE sculptforge_core sculptforge_ai sculptforge_phone)if(SCULPTFORGE_BUILD_APP) # the action registry (key chords, shortcut overrides) needs SDL's key namestarget_sources(sculptforge_tests PRIVATE src/app/Actions.cpp)target_link_libraries(sculptforge_tests PRIVATE SDL3::SDL3)diff --git a/assets/phone/capture.html b/assets/phone/capture.htmlnew file mode 100644index 00000000..48361e07--- /dev/null+++ b/assets/phone/capture.html@@ -0,0 +1,537 @@+<!doctype html>+<!-- SculptForge phone link: the capture page the desktop serves on the local network (src/link/PhoneLink.cpp).+ Self-contained (no external files): scan videos and photo bursts, one photo for image to 3D, splat .ply files,+ View in AR, and the way into the live face feed. GPL-3.0-or-later. -->+<html lang="en">+<head>+<meta charset="utf-8">+<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">+<meta name="theme-color" content="#0f0f12">+<meta name="color-scheme" content="dark">+<title>SculptForge capture</title>+<style>+:root { --bg:#0f0f12; --bg2:#15151a; --panel:#1b1b21; --panel2:#22222a; --border:#2a2a31; --text:#ececf1; --muted:#a3a3ad;+ --accent:#f28c3c; --accent2:#ffb06a; --ok:#4ad295; --warn:#ffcf5a; --bad:#ff6b6b; --r:16px; }+* { box-sizing:border-box; -webkit-tap-highlight-color:transparent; }+html,body { margin:0; background:var(--bg); color:var(--text); }+body { font:400 16px/1.5 -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;+ -webkit-font-smoothing:antialiased; min-height:100vh; padding:env(safe-area-inset-top) 0 env(safe-area-inset-bottom); }+header { position:sticky; top:0; z-index:5; display:flex; align-items:center; gap:10px; padding:14px 18px;+ background:rgba(15,15,18,.88); backdrop-filter:blur(12px); -webkit-backdrop-filter:blur(12px); border-bottom:1px solid var(--border); }+header .brand { font-weight:650; letter-spacing:-.01em; flex:1; display:flex; align-items:center; gap:9px; }+.pill { font-size:13px; padding:4px 10px 4px 8px; border-radius:999px; background:var(--panel); border:1px solid var(--border);+ color:var(--muted); display:flex; align-items:center; gap:6px; white-space:nowrap; max-width:55vw; overflow:hidden; text-overflow:ellipsis; }+.dot { width:8px; height:8px; border-radius:50%; background:var(--muted); flex:none; }+.pill.ok .dot { background:var(--ok); box-shadow:0 0 8px var(--ok); } .pill.bad .dot { background:var(--bad); }+main { padding:18px; max-width:560px; margin:0 auto; }+h1 { font-size:26px; line-height:1.15; margin:6px 0 6px; letter-spacing:-.02em; }+h2 { font-size:20px; margin:0 0 6px; letter-spacing:-.01em; }+p { margin:0 0 12px; } .muted { color:var(--muted); } .small { font-size:14px; }+.cards { display:grid; gap:12px; margin-top:16px; }+.card { display:flex; gap:14px; align-items:center; padding:16px; border-radius:var(--r); background:var(--panel);+ border:1px solid var(--border); color:var(--text); text-align:left; width:100%; font:inherit; cursor:pointer; }+.card:active { transform:scale(.985); background:var(--panel2); }+.card .icon { width:46px; height:46px; border-radius:12px; flex:none; display:grid; place-items:center;+ background:linear-gradient(160deg, rgba(242,140,60,.25), rgba(242,140,60,.08)); color:var(--accent2); }+.card b { display:block; font-weight:620; font-size:17px; } .card span { color:var(--muted); font-size:14px; }+.card .chev { margin-left:auto; color:var(--muted); }+.panel { background:var(--panel); border:1px solid var(--border); border-radius:var(--r); padding:16px; margin:14px 0; }+.back { background:none; border:none; color:var(--accent2); font:inherit; padding:6px 0; margin:0 0 4px; cursor:pointer; }+button.primary, a.primary, label.primary { display:flex; align-items:center; justify-content:center; gap:8px; width:100%; min-height:52px;+ border:none; border-radius:14px; font:600 17px/1 inherit; font-family:inherit; color:#1a0d04; text-decoration:none;+ background:linear-gradient(180deg, #f6994d, #e2742a); cursor:pointer; margin-top:10px; }+button.secondary, a.secondary, label.secondary { display:flex; align-items:center; justify-content:center; gap:8px; width:100%; min-height:50px;+ border:1px solid var(--border); border-radius:14px; font:600 16px/1 inherit; font-family:inherit; color:var(--text);+ background:var(--panel2); cursor:pointer; margin-top:10px; text-decoration:none; }+button:disabled { opacity:.45; }+input[type=file] { display:none; }+ul.tips { list-style:none; padding:0; margin:8px 0 4px; display:grid; gap:9px; }+ul.tips li { display:flex; gap:10px; align-items:flex-start; font-size:15px; }+ul.tips li::before { content:""; width:20px; height:20px; flex:none; margin-top:1px; border-radius:50%;+ background:rgba(74,210,149,.16) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'%3E%3Cpath d='M5.5 10.5l3 3 6-7' fill='none' stroke='%234ad295' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center/20px no-repeat; }+.orbit { display:block; width:100%; max-width:300px; margin:6px auto 2px; }+.orbit .cam { animation:orbit 6s linear infinite; transform-origin:150px 95px; }+@keyframes orbit { to { transform:rotate(360deg); } }+.seg { display:flex; background:var(--bg2); border:1px solid var(--border); border-radius:12px; padding:3px; gap:3px; margin:8px 0; }+.seg button { flex:1; border:none; background:none; color:var(--muted); font:600 14px inherit; font-family:inherit; padding:9px 4px; border-radius:9px; cursor:pointer; }+.seg button.on { background:var(--panel2); color:var(--text); box-shadow:0 1px 0 rgba(255,255,255,.05) inset; }+.bar { height:10px; border-radius:999px; background:var(--bg2); border:1px solid var(--border); overflow:hidden; margin:12px 0 8px; }+.bar i { display:block; height:100%; width:0; background:linear-gradient(90deg, #e2742a, #ffb06a); transition:width .25s; }+.row { display:flex; justify-content:space-between; gap:10px; font-size:14px; color:var(--muted); }+.note { border-radius:12px; padding:11px 13px; font-size:14px; margin:10px 0; background:rgba(255,207,90,.08); border:1px solid rgba(255,207,90,.25); color:#ffe2a0; }+.note.bad { background:rgba(255,107,107,.08); border-color:rgba(255,107,107,.3); color:#ffc2c2; }+.note.ok { background:rgba(74,210,149,.08); border-color:rgba(74,210,149,.3); color:#bff3db; }+.preview { width:100%; border-radius:12px; margin:10px 0; background:#000; max-height:46vh; object-fit:contain; }+.code { display:flex; gap:8px; justify-content:center; margin:14px 0; }+.code input { width:100%; max-width:260px; text-align:center; font:600 30px/1 ui-monospace, Menlo, monospace; letter-spacing:.3em;+ padding:12px; border-radius:14px; border:1px solid var(--border); background:var(--bg2); color:var(--text); }+.hidden { display:none !important; }+footer { text-align:center; color:#6f6f78; font-size:12px; padding:22px 18px 30px; }+.big-check { width:72px; height:72px; border-radius:50%; margin:10px auto 14px; display:grid; place-items:center; background:rgba(74,210,149,.14); }+.arlink { display:block; } .arlink img { width:100%; border-radius:12px; display:block; }+</style>+</head>+<body>+<header>+ <div class="brand">+ <svg width="26" height="26" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 2.5l8.5 4.8v9.4L12 21.5l-8.5-4.8V7.3z" fill="none" stroke="#f28c3c" stroke-width="1.6" stroke-linejoin="round"/><path d="M3.8 7.4L12 12l8.2-4.6M12 12v9.2" fill="none" stroke="#ffb06a" stroke-width="1.6" stroke-linejoin="round"/></svg>+ SculptForge+ </div>+ <div id="conn" class="pill"><span class="dot"></span><span id="connText">Connecting...</span></div>+</header>++<main>+ <!-- Pairing: when the page was opened without the QR code's link -->+ <section id="pair" class="hidden">+ <h1>Pair with your computer</h1>+ <p class="muted">Type the 6-digit code shown in SculptForge (File > Capture from phone).</p>+ <div class="code"><input id="codeInput" inputmode="numeric" autocomplete="one-time-code" maxlength="7" placeholder="000000"></div>+ <button class="primary" id="pairBtn">Pair</button>+ <div id="pairMsg"></div>+ </section>++ <!-- Home -->+ <section id="home" class="hidden">+ <h1>Capture for SculptForge</h1>+ <p class="muted" id="homeSub">What would you like to send?</p>+ <div class="cards">+ <button class="card" data-go="scan"><div class="icon"><svg width="26" height="26" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="3"/><path d="M3 12a9 4 0 0 0 18 0M21 12a9 4 0 0 0-18 0" opacity=".55"/><path d="M17 5l2-1.5M7 19l-2 1.5"/></svg></div>+ <div><b>Scan an object</b><span>Walk around it for 20-60 s. Becomes a live splat.</span></div><span class="chev">›</span></button>+ <button class="card" data-go="photo"><div class="icon"><svg width="26" height="26" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="6" width="18" height="14" rx="3"/><circle cx="12" cy="13" r="3.5"/><path d="M8 6l1.5-2.5h5L16 6"/></svg></div>+ <div><b>One photo to 3D</b><span>AI turns a single picture into a model.</span></div><span class="chev">›</span></button>+ <button class="card" data-go="ply"><div class="icon"><svg width="26" height="26" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="7" cy="8" r="1.6"/><circle cx="15" cy="6" r="1.2"/><circle cx="17" cy="13" r="2"/><circle cx="9" cy="15" r="2.4"/><circle cx="13" cy="19" r="1.2"/></svg></div>+ <div><b>Send a splat file</b><span>A .ply from Scaniverse, Polycam, Luma...</span></div><span class="chev">›</span></button>+ <button class="card" data-go="ar"><div class="icon"><svg width="26" height="26" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M12 3l7 4v8l-7 4-7-4V7z"/><path d="M5 7l7 4 7-4M12 11v8"/><path d="M2 21h20" opacity=".55"/></svg></div>+ <div><b>View in AR</b><span>Your sculpt, standing on your real desk.</span></div><span class="chev">›</span></button>+ <button class="card" data-go="face"><div class="icon"><svg width="26" height="26" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="9"/><path d="M8.5 14.5c1 1.3 2.2 2 3.5 2s2.5-.7 3.5-2"/><path d="M9 9.5h.01M15 9.5h.01"/></svg></div>+ <div><b>Face capture</b><span>Your expressions drive the character, live.</span></div><span class="chev">›</span></button>+ </div>+ </section>++ <!-- Scan -->+ <section id="scan" class="hidden">+ <button class="back" data-go="home">‹ Back</button>+ <h1>Scan an object</h1>+ <svg class="orbit" viewBox="0 0 300 190" aria-hidden="true">+ <ellipse cx="150" cy="120" rx="118" ry="44" fill="none" stroke="#2f2f38" stroke-width="2" stroke-dasharray="5 7"/>+ <ellipse cx="150" cy="150" rx="60" ry="10" fill="#000" opacity=".35"/>+ <path d="M150 62c22 0 34 20 34 42s-10 44-34 44-34-22-34-44 12-42 34-42z" fill="#c9a78b"/>+ <path d="M150 62c22 0 34 20 34 42s-10 44-34 44" fill="#a78468"/>+ <g class="cam"><g transform="translate(268 120)"><rect x="-11" y="-17" width="22" height="34" rx="5" fill="#f28c3c"/><circle cx="0" cy="-8" r="3.2" fill="#1a0d04"/></g></g>+ </svg>+ <p class="muted small" style="text-align:center">Walk slowly all the way round, then once more a little lower.</p>+ <ul class="tips">+ <li>20-60 seconds, slow and steady (no quick turns)</li>+ <li>Even, soft light; no harsh shadows or reflections</li>+ <li>Keep the object in the middle of the screen and the whole of it in view</li>+ <li>Don't zoom; keep about the same distance all the way</li>+ <li>A plain table or floor around it helps</li>+ </ul>+ <label class="primary" for="scanVideo">Record a video</label>+ <input id="scanVideo" type="file" accept="video/*" capture="environment">+ <label class="secondary" for="scanPhotos">Choose photos (20-200)</label>+ <input id="scanPhotos" type="file" accept="image/*" multiple>+ <label class="secondary" for="scanLibrary">Choose a video you already took</label>+ <input id="scanLibrary" type="file" accept="video/*">+ <div id="scanCheck" class="panel hidden">+ <video id="scanPreview" class="preview hidden" playsinline muted></video>+ <div id="scanSummary"></div>+ <button class="primary" id="scanSend">Send to SculptForge</button>+ </div>+ </section>++ <!-- One photo -->+ <section id="photo" class="hidden">+ <button class="back" data-go="home">‹ Back</button>+ <h1>One photo to 3D</h1>+ <ul class="tips">+ <li>One object, the whole of it in the picture</li>+ <li>A plain background (a wall, a sheet of paper)</li>+ <li>Soft light; a three-quarter view shows the most</li>+ </ul>+ <p class="muted small">Your computer sends it to the image-to-3D service chosen in SculptForge (Generate > Image to 3D; free Hugging Face by default).</p>+ <label class="primary" for="photoCam">Take a photo</label>+ <input id="photoCam" type="file" accept="image/*" capture="environment">+ <label class="secondary" for="photoLib">Choose from your photos</label>+ <input id="photoLib" type="file" accept="image/*">+ <div id="photoCheck" class="panel hidden">+ <img id="photoPreview" class="preview" alt="">+ <button class="primary" id="photoSend">Send to SculptForge</button>+ </div>+ </section>++ <!-- PLY -->+ <section id="ply" class="hidden">+ <button class="back" data-go="home">‹ Back</button>+ <h1>Send a splat file</h1>+ <p class="muted">Already scanned it with a splat app? Export a Gaussian splat <b>.ply</b> and send it here:</p>+ <ul class="tips">+ <li><b>Scaniverse</b>: open the scan, Share, Export Model, PLY (splat)</li>+ <li><b>Polycam</b>: open the capture, Export, Gaussian splat, PLY</li>+ <li><b>Luma</b>: open the capture, download, Gaussian splat (.ply)</li>+ </ul>+ <p class="muted small">Save it to Files first if the app can't share straight to the browser. .splat and .spz work too.</p>+ <label class="primary" for="plyFile">Choose the file</label>+ <input id="plyFile" type="file">+ <div id="plyMsg"></div>+ </section>++ <!-- Upload progress (all kinds) -->+ <section id="upload" class="hidden">+ <h1 id="upTitle">Sending...</h1>+ <p class="muted" id="upSub">Keep this page open and the screen on.</p>+ <div class="bar"><i id="upBar"></i></div>+ <div class="row"><span id="upLeft">0%</span><span id="upRight"></span></div>+ <div id="upNote"></div>+ <button class="secondary" id="upCancel">Cancel</button>+ </section>++ <!-- Done -->+ <section id="done" class="hidden" style="text-align:center">+ <div class="big-check"><svg width="38" height="38" viewBox="0 0 20 20"><path d="M5 10.5l3 3 7-8" fill="none" stroke="#4ad295" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"/></svg></div>+ <h1>Sent</h1>+ <p class="muted" id="doneText"></p>+ <button class="primary" data-go="home">Capture something else</button>+ <button class="secondary" data-go="ar">View the sculpt in AR</button>+ </section>++ <!-- AR -->+ <section id="ar" class="hidden">+ <button class="back" data-go="home">‹ Back</button>+ <h1>View in AR</h1>+ <p class="muted">Point your phone at a desk or the floor; the sculpt stands on it at real size.</p>+ <div class="panel">+ <div class="small muted">Show</div>+ <div class="seg" id="arScope"><button data-v="subtool" class="on">Active SubTool</button><button data-v="scene">Whole scene</button></div>+ <div class="small muted">Size on the desk</div>+ <div class="seg" id="arSize"><button data-v="10">10 cm</button><button data-v="20" class="on">20 cm</button><button data-v="40">40 cm</button><button data-v="100">1 m</button></div>+ <button class="primary" id="arPrepare">Get the model</button>+ <div id="arState" class="small muted" style="margin-top:10px"></div>+ <div id="arOpen" class="hidden">+ <a id="arIos" class="arlink hidden" rel="ar" href="#"><img alt="View in AR" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='600' height='120'%3E%3Crect width='600' height='120' rx='26' fill='%23f28c3c'/%3E%3Ctext x='300' y='74' font-family='-apple-system,system-ui,sans-serif' font-size='40' font-weight='600' text-anchor='middle' fill='%231a0d04'%3EView in AR%3C/text%3E%3C/svg%3E"></a>+ <a id="arAndroid" class="primary hidden" href="#">View in AR</a>+ <a id="arDownload" class="secondary" href="#" download>Download the model</a>+ </div>+ <div id="arLive" class="note ok hidden">Updated from your sculpt: tap View in AR again to see the change.</div>+ </div>+ <p class="muted small">iPhone and iPad use AR Quick Look (USDZ); Android uses Google's Scene Viewer (GLB, needs Google Play Services for AR).</p>+ </section>++ <!-- Face -->+ <section id="face" class="hidden">+ <button class="back" data-go="home">‹ Back</button>+ <h1>Face capture</h1>+ <p class="muted">Your phone's front camera tracks 52 facial expressions (the ARKit set) and sends them to SculptForge about 30 times a second.</p>+ <ul class="tips">+ <li>The video never leaves your phone: only the expression numbers do</li>+ <li>Prop the phone up at eye level, about an arm's length away</li>+ <li>Light on your face, not behind you</li>+ </ul>+ <p class="muted small">The camera needs a secure page, so this opens SculptForge's face page on its website, which then talks straight to your computer over Wi-Fi.</p>+ <button class="primary" id="faceStart">Start face capture</button>+ <div id="faceMsg"></div>+ </section>++ <!-- Can't reach the computer -->+ <section id="lost" class="hidden">+ <h1>Can't reach your computer</h1>+ <div class="note bad" id="lostWhy">SculptForge isn't answering.</div>+ <ul class="tips">+ <li>Is the Capture from phone window still open in SculptForge?</li>+ <li>Phone and computer on the same Wi-Fi (not a guest network, not mobile data)</li>+ <li>A VPN on either device can block local connections</li>+ <li>On Windows, allow SculptForge on private networks in the firewall</li>+ </ul>+ <button class="primary" id="retry">Try again</button>+ </section>+</main>+<footer>SculptForge phone link · nothing is uploaded anywhere but your computer</footer>++<script>+"use strict";+const $ = (id) => document.getElementById(id);+const params = new URLSearchParams(location.search);+let token = params.get("k") || sessionStorage.getItem("sfToken") || "";+let status = null, statusFails = 0, current = "home", busyUpload = null;+let limits = { chunk: 4 * 1024 * 1024 };++function show(id) {+ for (const s of document.querySelectorAll("main > section")) s.classList.toggle("hidden", s.id !== id);+ current = id;+ window.scrollTo(0, 0);+}+function note(el, text, kind) { el.innerHTML = text ? `<div class="note ${kind || ""}">${text}</div>` : ""; }+function mb(n) { return n >= 1e9 ? (n / 1e9).toFixed(2) + " GB" : n >= 1e6 ? (n / 1e6).toFixed(1) + " MB" : Math.max(1, Math.round(n / 1e3)) + " KB"; }+function esc(s) { return String(s).replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ }[c])); }+const isIOS = /iPhone|iPad|iPod/.test(navigator.userAgent) || (/Macintosh/.test(navigator.userAgent) && navigator.maxTouchPoints > 1);+const isAndroid = /Android/.test(navigator.userAgent);+const sleep = (ms) => new Promise((r) => setTimeout(r, ms));++async function api(method, path, body, raw) {+ const opts = { method, headers: { "X-SF-Token": token } };+ if (body !== undefined) {+ if (raw) { opts.body = body; opts.headers["Content-Type"] = "application/octet-stream"; }+ else { opts.body = JSON.stringify(body); opts.headers["Content-Type"] = "application/json"; }+ }+ const r = await fetch(path, opts);+ let data = {};+ try { data = await r.json(); } catch (e) {}+ if (!r.ok) { const err = new Error(data.error || ("HTTP " + r.status)); err.status = r.status; err.data = data; throw err; }+ return data;+}++// --- Connection ---+function setConn(ok, text) {+ $("conn").className = "pill " + (ok ? "ok" : "bad");+ $("connText").textContent = text;+}+async function poll() {+ if (!token) return;+ try {+ status = await api("GET", "/api/status");+ statusFails = 0;+ limits = status.limits || limits;+ setConn(true, status.desktop || "Connected");+ $("homeSub").textContent = "Connected to " + (status.desktop || "your computer") + ". What would you like to send?";+ if (current === "lost") show("home");+ arStatus();+ } catch (e) {+ if (e.status === 403) { sessionStorage.removeItem("sfToken"); token = ""; setConn(false, "Not paired"); show("pair"); return; }+ if (++statusFails >= 3 && !busyUpload) {+ setConn(false, "Not connected");+ $("lostWhy").textContent = "No answer from your computer for a few seconds.";+ if (current !== "pair") show("lost");+ }+ }+}+setInterval(poll, 2000);++// --- Pairing ---+$("pairBtn").onclick = async () => {+ const code = $("codeInput").value.replace(/\D/g, "");+ if (code.length !== 6) { note($("pairMsg"), "The code has 6 digits.", "bad"); return; }+ try {+ const r = await fetch("/api/pair", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ code }) });+ const data = await r.json();+ if (!r.ok) throw new Error(data.error || "Pairing failed");+ token = data.token;+ sessionStorage.setItem("sfToken", token);+ history.replaceState(null, "", "/?k=" + encodeURIComponent(token));+ note($("pairMsg"), "");+ show("home");+ poll();+ } catch (e) { note($("pairMsg"), esc(e.message), "bad"); }+};++for (const b of document.querySelectorAll("[data-go]")) b.addEventListener("click", () => show(b.dataset.go));+$("retry").onclick = () => { statusFails = 0; poll(); };++// --- Uploads: resumable, chunked ---+let cancelUpload = false;+$("upCancel").onclick = () => { cancelUpload = true; };++async function sendCapture(kind, files, doneText) {+ if (busyUpload) return;+ cancelUpload = false;+ busyUpload = kind;+ show("upload");+ $("upTitle").textContent = files.length > 1 ? `Sending ${files.length} files...` : "Sending...";+ const total = files.reduce((s, f) => s + f.size, 0);+ let sent = 0, capture = null;+ const t0 = performance.now();+ const progress = (extra) => {+ const pct = total ? Math.min(100, (sent / total) * 100) : 0;+ $("upBar").style.width = pct.toFixed(1) + "%";+ const secs = (performance.now() - t0) / 1000, rate = secs > 0.5 ? sent / secs : 0;+ $("upLeft").textContent = `${Math.floor(pct)}% · ${mb(sent)} of ${mb(total)}`;+ $("upRight").textContent = rate > 0 ? `${mb(rate)}/s · ${Math.max(1, Math.round((total - sent) / Math.max(rate, 1)))} s left` : "";+ if (extra !== undefined) note($("upNote"), extra, "");+ };+ try {+ capture = await withRetry(() => api("POST", "/api/capture", { kind }));+ for (let i = 0; i < files.length; i++) {+ const file = files[i];+ if (files.length > 1) $("upSub").textContent = `File ${i + 1} of ${files.length} · keep this page open.`;+ const entry = await withRetry(() => api("POST", `/api/capture/${capture.id}/file`, { name: file.name || ("file" + i), size: file.size }));+ const chunk = entry.chunk || limits.chunk;+ let offset = 0;+ while (offset < file.size) {+ if (cancelUpload) throw new Error("cancelled");+ const end = Math.min(file.size, offset + chunk);+ const r = await withRetry(async () => {+ try { return await api("PUT", `/api/file/${entry.id}?offset=${offset}`, file.slice(offset, end), true); }+ catch (e) {+ if (e.status === 409 && e.data && typeof e.data.received === "number") return e.data; // resume where it stopped+ throw e;+ }+ }, (n) => progress(`Connection hiccup, retrying (${n})... Same Wi-Fi as your computer?`));+ sent += r.received - offset;+ offset = r.received;+ progress("");+ }+ }+ const fin = await withRetry(() => api("POST", `/api/capture/${capture.id}/finish`));+ $("doneText").textContent = doneText + (fin.name ? ` (Captures/${fin.name})` : "");+ show("done");+ } catch (e) {+ if (capture) { try { await api("POST", `/api/capture/${capture.id}/cancel`); } catch (e2) {} }+ if (e.message === "cancelled") { show("home"); }+ else { show("upload"); $("upTitle").textContent = "Not sent"; note($("upNote"), esc(e.message), "bad"); $("upCancel").textContent = "Back"; $("upCancel").onclick = () => { $("upCancel").textContent = "Cancel"; $("upCancel").onclick = () => { cancelUpload = true; }; show("home"); }; }+ } finally {+ busyUpload = null;+ }+}++// Retries network failures with backoff (2, 4, 8 ... 30 s); errors the computer answered with are final.+async function withRetry(fn, onRetry) {+ for (let attempt = 1; ; attempt++) {+ try { return await fn(); }+ catch (e) {+ if (e.status && e.status < 500 && e.status !== 429 && e.status !== 409) throw e;+ if (cancelUpload) throw new Error("cancelled");+ if (attempt >= 12) throw new Error("Lost the connection to your computer. Check that both are on the same Wi-Fi and the SculptForge window is still open, then try again.");+ if (onRetry) onRetry(attempt);+ await sleep(Math.min(30000, 1000 * 2 ** attempt));+ }+ }+}++// --- Scan ---+let scanFiles = [];+function scanChosen(files, isVideo) {+ scanFiles = Array.from(files || []);+ if (!scanFiles.length) return;+ const total = scanFiles.reduce((s, f) => s + f.size, 0);+ $("scanCheck").classList.remove("hidden");+ const v = $("scanPreview");+ if (isVideo) {+ v.classList.remove("hidden");+ v.src = URL.createObjectURL(scanFiles[0]);+ v.onloadedmetadata = () => {+ const d = v.duration;+ let msg = `<b>${isFinite(d) ? Math.round(d) + " s video" : "Video"}</b> · ${mb(total)}`;+ if (isFinite(d) && d < 15) msg += `<div class="note">That's short: 20-60 s all the way round gives a much better scan. Send anyway, or record again.</div>`;+ else if (isFinite(d) && d > 120) msg += `<div class="note">That's long: the upload and training take longer. 20-60 s is plenty.</div>`;+ $("scanSummary").innerHTML = msg;+ };+ $("scanSummary").innerHTML = `<b>Video</b> · ${mb(total)}`;+ } else {+ v.classList.add("hidden");+ let msg = `<b>${scanFiles.length} photos</b> · ${mb(total)}`;+ if (scanFiles.length < 20) msg += `<div class="note">Splats need many views: 20 photos at least, 50-150 is better.</div>`;+ $("scanSummary").innerHTML = msg;+ }+ $("scanCheck").scrollIntoView({ behavior: "smooth" });+}+$("scanVideo").onchange = (e) => scanChosen(e.target.files, true);+$("scanLibrary").onchange = (e) => scanChosen(e.target.files, true);+$("scanPhotos").onchange = (e) => scanChosen(e.target.files, false);+$("scanSend").onclick = () => sendCapture("splat", scanFiles, "SculptForge is training a splat from your scan: watch it sharpen in the viewport.");++// --- One photo ---+let photoFile = null;+async function photoChosen(files) {+ const f = files && files[0];+ if (!f) return;+ photoFile = f;+ $("photoPreview").src = URL.createObjectURL(f);+ $("photoCheck").classList.remove("hidden");+ $("photoCheck").scrollIntoView({ behavior: "smooth" });+}+// Phones take 12-48 MP photos; image to 3D uses about 1 MP. Send at most 2048 px as JPEG (faster on slow Wi-Fi).+async function shrink(file) {+ try {+ const bmp = await createImageBitmap(file, { imageOrientation: "from-image" });+ const scale = Math.min(1, 2048 / Math.max(bmp.width, bmp.height));+ if (scale >= 1 && /jpe?g|png/i.test(file.type)) return file;+ const c = document.createElement("canvas");+ c.width = Math.round(bmp.width * scale); c.height = Math.round(bmp.height * scale);+ c.getContext("2d").drawImage(bmp, 0, 0, c.width, c.height);+ const blob = await new Promise((r) => c.toBlob(r, "image/jpeg", 0.92));+ if (!blob) return file;+ return new File([blob], (file.name || "photo").replace(/\.[^.]*$/, "") + ".jpg", { type: "image/jpeg" });+ } catch (e) { return file; }+}+$("photoCam").onchange = (e) => photoChosen(e.target.files);+$("photoLib").onchange = (e) => photoChosen(e.target.files);+$("photoSend").onclick = async () => {+ if (!photoFile) return;+ const f = await shrink(photoFile);+ sendCapture("photo", [f], "SculptForge is generating a 3D model from your photo (Generate > Image to 3D).");+};++// --- PLY ---+$("plyFile").onchange = (e) => {+ const f = e.target.files && e.target.files[0];+ if (!f) return;+ if (!/\.(ply|splat|spz)$/i.test(f.name)) { note($("plyMsg"), "That's not a .ply, .splat or .spz file.", "bad"); return; }+ note($("plyMsg"), "");+ sendCapture("ply", [f], "The splat opens in SculptForge next to your sculpt.");+};++// --- AR ---+let arScope = "subtool", arSize = 20, arWanted = 0, arShown = 0;+function segment(id, set) {+ for (const b of $(id).querySelectorAll("button")) b.onclick = () => {+ for (const o of $(id).querySelectorAll("button")) o.classList.toggle("on", o === b);+ set(b.dataset.v);+ };+}+segment("arScope", (v) => { arScope = v; });+segment("arSize", (v) => { arSize = Number(v); });+$("arPrepare").onclick = async () => {+ try {+ const r = await api("POST", "/api/ar", { scope: arScope, size: arSize });+ arWanted = (r.version || 0) + 1;+ $("arState").textContent = "Preparing the model on your computer...";+ $("arLive").classList.add("hidden");+ $("arOpen").classList.add("hidden");+ } catch (e) { $("arState").textContent = e.message; }+};+function arStatus() {+ if (!status || !status.ar) return;+ const ar = status.ar;+ if (ar.error && arWanted) { $("arState").textContent = ar.error; return; }+ if (!ar.version || ar.version < arWanted) { if (arWanted) $("arState").textContent = "Preparing the model on your computer..."; return; }+ if (!arWanted && !arShown) return;+ const v = ar.version;+ const base = location.origin;+ const q = `?k=${encodeURIComponent(token)}&v=${v}`;+ const usdz = `${base}/ar/model.usdz${q}`, glb = `${base}/ar/model.glb${q}`;+ $("arIos").href = usdz + "#allowsContentScaling=0";+ $("arAndroid").href = `intent://arvr.google.com/scene-viewer/1.0?file=${encodeURIComponent(glb)}&mode=ar_preferred&title=SculptForge#Intent;scheme=https;package=com.google.android.googlequicksearchbox;action=android.intent.action.VIEW;S.browser_fallback_url=${encodeURIComponent(glb)};end;`;+ $("arDownload").href = isIOS ? usdz : glb;+ $("arIos").classList.toggle("hidden", !isIOS);+ $("arAndroid").classList.toggle("hidden", isIOS);+ $("arOpen").classList.remove("hidden");+ $("arState").textContent = ar.summary ? "Ready: " + ar.summary : "Ready";+ if (arShown && v > arShown) $("arLive").classList.remove("hidden");+ arShown = v;+}++// --- Face ---+$("faceStart").onclick = async () => {+ if (status && status.face && !status.face.webrtc) {+ note($("faceMsg"), "This SculptForge was built without WebRTC, so live face capture isn't available in it.", "bad");+ return;+ }+ note($("faceMsg"), "Preparing a secure connection...", "");+ try {+ const r = await api("GET", "/api/face-link");+ location.href = r.url;+ } catch (e) { note($("faceMsg"), esc(e.message), "bad"); }+};++// --- Start ---+if (token) {+ sessionStorage.setItem("sfToken", token);+ if (params.get("k")) history.replaceState(null, "", "/?k=" + encodeURIComponent(token));+ show(location.hash === "#face" ? "face" : "home");+ poll();+} else {+ setConn(false, "Not paired");+ show("pair");+}+</script>+</body>+</html>diff --git a/cmake/WebRTC.cmake b/cmake/WebRTC.cmakenew file mode 100644index 00000000..8add16d1--- /dev/null+++ b/cmake/WebRTC.cmake@@ -0,0 +1,87 @@+# WebRTC data channels for the phone link's live face feed (src/link/PhoneRtc.cpp), optional:+# -DSCULPTFORGE_WEBRTC=OFF builds without it (the phone link then offers capture and AR only).+# libdatachannel (MPL-2.0) with libjuice (MPL-2.0), usrsctp (BSD-3-Clause), plog (MIT) and Mbed TLS (Apache-2.0),+# all static and fetched at configure time like the other dependencies. No media (SRTP) and no WebSocket parts.+include(FetchContent)++FetchContent_Declare(mbedtls+ URL https://github.com/Mbed-TLS/mbedtls/releases/download/mbedtls-3.6.7/mbedtls-3.6.7.tar.bz2+ DOWNLOAD_EXTRACT_TIMESTAMP TRUE)+FetchContent_Declare(libdatachannel+ URL https://github.com/paullouisageneau/libdatachannel/archive/refs/tags/v0.24.6.tar.gz+ DOWNLOAD_EXTRACT_TIMESTAMP TRUE)+# libdatachannel's submodules, at the commits its v0.24.6 tag pins (GitHub archives leave submodules out).+FetchContent_Declare(ldc_usrsctp+ URL https://github.com/paullouisageneau/usrsctp/archive/fec583d54493f879d2ae44a743423bf8a04371ab.tar.gz+ DOWNLOAD_EXTRACT_TIMESTAMP TRUE)+FetchContent_Declare(ldc_libjuice+ URL https://github.com/paullouisageneau/libjuice/archive/b89c792e3612faf2f12cf35bcc56857313a06be3.tar.gz+ DOWNLOAD_EXTRACT_TIMESTAMP TRUE)+FetchContent_Declare(ldc_plog+ URL https://github.com/SergiusTheBest/plog/archive/94899e0b926ac1b0f4750bfbd495167b4a6ae9ef.tar.gz+ DOWNLOAD_EXTRACT_TIMESTAMP TRUE)++foreach(dep mbedtls libdatachannel ldc_usrsctp ldc_libjuice ldc_plog)+ FetchContent_GetProperties(${dep})+ if(NOT ${dep}_POPULATED)+ if(POLICY CMP0169)+ cmake_policy(PUSH)+ cmake_policy(SET CMP0169 OLD) # FetchContent_Populate: we add the projects ourselves, EXCLUDE_FROM_ALL+ endif()+ FetchContent_Populate(${dep})+ if(POLICY CMP0169)+ cmake_policy(POP)+ endif()+ endif()+endforeach()++# The dependencies' own install rules (headers, static libraries, CMake packages) stay out of our packages:+# install() does nothing while SF_SKIP_INSTALL is set.+function(install)+ if(NOT SF_SKIP_INSTALL)+ _install(${ARGV})+ endif()+endfunction()+set(SF_SKIP_INSTALL ON)++# Mbed TLS: the libraries only, static.+set(ENABLE_TESTING OFF CACHE BOOL "" FORCE)+set(ENABLE_PROGRAMS OFF CACHE BOOL "" FORCE)+set(USE_SHARED_MBEDTLS_LIBRARY OFF CACHE BOOL "" FORCE)+set(USE_STATIC_MBEDTLS_LIBRARY ON CACHE BOOL "" FORCE)+set(MBEDTLS_FATAL_WARNINGS OFF CACHE BOOL "" FORCE)+set(GEN_FILES OFF CACHE BOOL "" FORCE)+set(MBEDTLS_USER_CONFIG_FILE ${CMAKE_CURRENT_LIST_DIR}/mbedtls_user_config.h CACHE FILEPATH "" FORCE)+add_subdirectory(${mbedtls_SOURCE_DIR} ${CMAKE_BINARY_DIR}/_webrtc/mbedtls EXCLUDE_FROM_ALL) # (sources may be shared between build trees)+add_library(sf_mbedtls INTERFACE)+target_link_libraries(sf_mbedtls INTERFACE mbedtls mbedx509 mbedcrypto)+add_library(MbedTLS::MbedTLS ALIAS sf_mbedtls)++# libdatachannel looks for its submodules under deps/.+foreach(sub usrsctp libjuice plog)+ if(NOT EXISTS ${libdatachannel_SOURCE_DIR}/deps/${sub}/CMakeLists.txt)+ file(REMOVE_RECURSE ${libdatachannel_SOURCE_DIR}/deps/${sub})+ file(COPY ${ldc_${sub}_SOURCE_DIR}/ DESTINATION ${libdatachannel_SOURCE_DIR}/deps/${sub})+ endif()+endforeach()+set(USE_MBEDTLS ON CACHE BOOL "" FORCE)+set(NO_MEDIA ON CACHE BOOL "" FORCE)+set(NO_WEBSOCKET ON CACHE BOOL "" FORCE)+set(NO_EXAMPLES ON CACHE BOOL "" FORCE)+set(NO_TESTS ON CACHE BOOL "" FORCE)+set(BUILD_SHARED_DEPS_LIBS OFF CACHE BOOL "" FORCE)+set(_sf_shared ${BUILD_SHARED_LIBS})+set(BUILD_SHARED_LIBS OFF)+add_subdirectory(${libdatachannel_SOURCE_DIR} ${CMAKE_BINARY_DIR}/_webrtc/libdatachannel EXCLUDE_FROM_ALL)+set(BUILD_SHARED_LIBS ${_sf_shared})+set(SF_SKIP_INSTALL OFF)+# Third-party code: compile quietly.+foreach(t datachannel-static juice-static usrsctp mbedtls mbedx509 mbedcrypto)+ if(TARGET ${t})+ if(MSVC)+ target_compile_options(${t} PRIVATE /w)+ else()+ target_compile_options(${t} PRIVATE -w)+ endif()+ endif()+endforeach()diff --git a/cmake/mbedtls_user_config.h b/cmake/mbedtls_user_config.hnew file mode 100644index 00000000..05a61fd2--- /dev/null+++ b/cmake/mbedtls_user_config.h@@ -0,0 +1,3 @@+/* SculptForge additions to Mbed TLS's default configuration (cmake/WebRTC.cmake). libdatachannel's DTLS code+ refers to the DTLS-SRTP API even without media, so it must be compiled in. */+#define MBEDTLS_SSL_DTLS_SRTPdiff --git a/src/io/ArExport.cpp b/src/io/ArExport.cppnew file mode 100644index 00000000..29662f5f--- /dev/null+++ b/src/io/ArExport.cpp@@ -0,0 +1,335 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+#include "io/ArExport.h"++#include "core/AutoUv.h"+#include "core/Decimate.h"+#include "core/Texture.h"+#include "io/GltfIO.h"+#include "io/UsdIO.h"++#include <algorithm>+#include <atomic>+#include <chrono>+#include <cmath>+#include <cstdio>+#include <filesystem>+#include <fstream>+#include <functional>+#include <iterator>+#include <unordered_map>++namespace sf {++namespace fs = std::filesystem;++namespace {++constexpr size_t kClusterAbove = 3'000'000; // triangles: cluster vertices first (QEM on more needs gigabytes)+constexpr int kPadding = 4; // texels around each UV island++// One part's source of colour for a merged triangle.+struct Part {+ const PaintTexture* texture = nullptr; // sampled at the carried corner UVs+ bool colors = false; // polypaint (per vertex, linear)+};++glm::vec3 sampleBaked(const PaintTexture& tex, glm::vec2 uv) {+ // Bilinear on the sRGB bytes (rows bottom-up, v = 0 at the bottom), clamped to the edge.+ const float x = std::clamp(uv.x, 0.0f, 1.0f) * float(tex.width) - 0.5f;+ const float y = std::clamp(uv.y, 0.0f, 1.0f) * float(tex.height) - 0.5f;+ const int x0 = std::clamp(int(std::floor(x)), 0, tex.width - 1), y0 = std::clamp(int(std::floor(y)), 0, tex.height - 1);+ const int x1 = std::min(x0 + 1, tex.width - 1), y1 = std::min(y0 + 1, tex.height - 1);+ const float fx = std::clamp(x - float(x0), 0.0f, 1.0f), fy = std::clamp(y - float(y0), 0.0f, 1.0f);+ auto at = [&](int px, int py) {+ const uint8_t* p = tex.pixels.data() + (size_t(py) * size_t(tex.width) + size_t(px)) * 4;+ return glm::vec3(p[0], p[1], p[2]) / 255.0f;+ };+ return glm::mix(glm::mix(at(x0, y0), at(x1, y0), fx), glm::mix(at(x0, y1), at(x1, y1), fx), fy);+}++// Rasterises every triangle into the texture at its UVs, colour from `colorAt(triangle, barycentrics)` (sRGB+// 0..1), then pads the islands ring by ring.+std::vector<uint8_t> bake(const std::vector<glm::uvec3>& triangles, const std::vector<glm::vec2>& uvs, int size,+ const std::function<glm::vec3(size_t, glm::vec3)>& colorAt) {+ std::vector<uint8_t> rgba(size_t(size) * size_t(size) * 4, 255);+ std::vector<uint8_t> filled(size_t(size) * size_t(size), 0);+ for (size_t t = 0; t < triangles.size(); ++t) {+ const glm::vec2 a = uvs[t * 3] * float(size), b = uvs[t * 3 + 1] * float(size), c = uvs[t * 3 + 2] * float(size);+ const float area = (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y);+ if (std::abs(area) < 1e-12f) continue;+ const int x0 = std::max(0, int(std::floor(std::min({a.x, b.x, c.x}))));+ const int x1 = std::min(size - 1, int(std::ceil(std::max({a.x, b.x, c.x}))));+ const int y0 = std::max(0, int(std::floor(std::min({a.y, b.y, c.y}))));+ const int y1 = std::min(size - 1, int(std::ceil(std::max({a.y, b.y, c.y}))));+ for (int y = y0; y <= y1; ++y)+ for (int x = x0; x <= x1; ++x) {+ const glm::vec2 p(float(x) + 0.5f, float(y) + 0.5f);+ float w0 = ((b.x - p.x) * (c.y - p.y) - (c.x - p.x) * (b.y - p.y)) / area;+ float w1 = ((c.x - p.x) * (a.y - p.y) - (a.x - p.x) * (c.y - p.y)) / area;+ float w2 = 1.0f - w0 - w1;+ // Texels whose centre lies just outside still get the triangle's colour (thin slivers, edges).+ constexpr float eps = -0.02f;+ if (w0 < eps || w1 < eps || w2 < eps) continue;+ w0 = std::max(w0, 0.0f);+ w1 = std::max(w1, 0.0f);+ w2 = std::max(w2, 0.0f);+ const float sum = w0 + w1 + w2;+ const glm::vec3 col = glm::clamp(colorAt(t, glm::vec3(w0, w1, w2) / sum), glm::vec3(0.0f), glm::vec3(1.0f));+ const size_t i = size_t(y) * size_t(size) + size_t(x);+ rgba[i * 4 + 0] = uint8_t(std::lround(col.x * 255.0f));+ rgba[i * 4 + 1] = uint8_t(std::lround(col.y * 255.0f));+ rgba[i * 4 + 2] = uint8_t(std::lround(col.z * 255.0f));+ filled[i] = 1;+ }+ }+ // Padding: each empty texel next to filled ones takes their average, ring after ring.+ for (int ring = 0; ring < kPadding; ++ring) {+ std::vector<uint8_t> next = filled;+ for (int y = 0; y < size; ++y)+ for (int x = 0; x < size; ++x) {+ const size_t i = size_t(y) * size_t(size) + size_t(x);+ if (filled[i]) continue;+ int sum[3] = {0, 0, 0}, n = 0;+ for (int dy = -1; dy <= 1; ++dy)+ for (int dx = -1; dx <= 1; ++dx) {+ const int nx = x + dx, ny = y + dy;+ if (nx < 0 || ny < 0 || nx >= size || ny >= size) continue;+ const size_t j = size_t(ny) * size_t(size) + size_t(nx);+ if (!filled[j]) continue;+ for (int k = 0; k < 3; ++k) sum[k] += rgba[j * 4 + size_t(k)];+ ++n;+ }+ if (!n) continue;+ for (int k = 0; k < 3; ++k) rgba[i * 4 + size_t(k)] = uint8_t(sum[k] / n);+ next[i] = 1;+ }+ filled.swap(next);+ }+ return rgba;+}++// A copy with only what AR needs: current positions, visible triangles, polypaint, and the texture's corner UVs.+Mesh leanCopy(const Mesh& src, bool keepUvs) {+ Mesh m;+ m.positions = src.positions;+ if (src.hasColors()) m.colors = src.colors;+ m.triangles.reserve(src.triangleCount());+ if (keepUvs) m.uvs.reserve(src.triangleCount() * 3);+ for (uint32_t t = 0; t < src.triangleCount(); ++t) {+ if (src.triangleHidden(t)) continue;+ m.triangles.push_back(src.triangles[t]);+ if (keepUvs)+ for (int c = 0; c < 3; ++c) m.uvs.push_back(src.uvs[size_t(t) * 3 + size_t(c)]);+ }+ return m;+}++// Vertex clustering on a grid: a quick first reduction for meshes too big for quadric decimation. Colours are+// averaged; a texture is sampled into vertex colours first (its corner UVs can't survive clustering).+Mesh cluster(const Mesh& src, const PaintTexture* texture, size_t targetVertices) {+ double area = 0.0;+ for (const auto& t : src.triangles)+ area += 0.5 * double(glm::length(glm::cross(src.positions[t[1]] - src.positions[t[0]], src.positions[t[2]] - src.positions[t[0]])));+ const float h = float(std::sqrt(std::max(area, 1e-12) / double(std::max<size_t>(targetVertices, 1000))));+ std::vector<glm::vec3> vertexColor;+ const bool colored = texture || src.hasColors();+ if (texture) {+ vertexColor.assign(src.vertexCount(), glm::vec3(1.0f));+ for (size_t t = 0; t < src.triangleCount(); ++t)+ for (int c = 0; c < 3; ++c)+ vertexColor[src.triangles[t][c]] = srgbToLinear(sampleBaked(*texture, src.uvs[t * 3 + size_t(c)]));+ }+ struct Key {+ int x, y, z;+ bool operator==(const Key& o) const { return x == o.x && y == o.y && z == o.z; }+ };+ struct KeyHash {+ size_t operator()(const Key& k) const {+ return size_t(uint64_t(uint32_t(k.x)) * 73856093u ^ uint64_t(uint32_t(k.y)) * 19349663u ^ uint64_t(uint32_t(k.z)) * 83492791u);+ }+ };+ std::unordered_map<Key, uint32_t, KeyHash> cells;+ std::vector<uint32_t> remap(src.vertexCount());+ std::vector<glm::dvec3> sumP;+ std::vector<glm::dvec3> sumC;+ std::vector<uint32_t> count;+ for (uint32_t v = 0; v < src.vertexCount(); ++v) {+ const glm::vec3 p = src.positions[v] / h;+ const Key k{int(std::floor(p.x)), int(std::floor(p.y)), int(std::floor(p.z))};+ auto [it, added] = cells.try_emplace(k, uint32_t(sumP.size()));+ if (added) {+ sumP.emplace_back(0.0);+ sumC.emplace_back(0.0);+ count.push_back(0);+ }+ const uint32_t c = it->second;+ remap[v] = c;+ sumP[c] += glm::dvec3(src.positions[v]);+ if (colored) sumC[c] += glm::dvec3(texture ? vertexColor[v] : src.colors[v]);+ ++count[c];+ }+ Mesh out;+ out.positions.resize(sumP.size());+ if (colored) out.colors.resize(sumP.size());+ for (size_t c = 0; c < sumP.size(); ++c) {+ out.positions[c] = glm::vec3(sumP[c] / double(count[c]));+ if (colored) out.colors[c] = glm::vec3(sumC[c] / double(count[c]));+ }+ for (const auto& t : src.triangles) {+ const glm::uvec3 n(remap[t[0]], remap[t[1]], remap[t[2]]);+ if (n[0] != n[1] && n[1] != n[2] && n[0] != n[2]) out.triangles.push_back(n);+ }+ return out;+}++std::string readFile(const fs::path& path) {+ std::ifstream in(path, std::ios::binary);+ return std::string(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());+}++std::string withThousands(size_t n) {+ std::string s = std::to_string(n);+ for (int i = int(s.size()) - 3; i > 0; i -= 3) s.insert(size_t(i), ",");+ return s;+}++} // namespace++std::vector<uint8_t> bakeCornerColors(const std::vector<glm::uvec3>& triangles, const std::vector<glm::vec2>& uvs,+ const std::vector<glm::vec3>& cornerColors, int size) {+ return bake(triangles, uvs, size, [&](size_t t, glm::vec3 w) {+ return cornerColors[t * 3] * w.x + cornerColors[t * 3 + 1] * w.y + cornerColors[t * 3 + 2] * w.z;+ });+}++bool exportArModel(const std::vector<const Mesh*>& meshes, const ArExportOptions& options, ArExportResult& out,+ std::string& error) {+ out = {};+ size_t total = 0;+ std::vector<const Mesh*> parts;+ for (const Mesh* m : meshes)+ if (m && m->triangleCount() > 0) {+ parts.push_back(m);+ total += m->triangleCount();+ }+ if (parts.empty()) {+ error = "Nothing to show: the model is empty";+ return false;+ }+ const size_t budget = std::max<size_t>(options.maxTriangles, 200);++ // Reduce each part to its share of the budget, keeping colour sources.+ std::vector<Mesh> reducedParts;+ std::vector<Part> partInfo;+ bool anyColor = false;+ for (const Mesh* src : parts) {+ const bool textured = hasTexture(*src);+ const size_t share = std::max<size_t>(64, size_t(double(budget) * double(src->triangleCount()) / double(total)));+ Mesh lean = leanCopy(*src, textured);+ Part info;+ if (lean.triangleCount() > kClusterAbove) {+ lean = cluster(lean, textured ? src->texture.get() : nullptr,+ std::min<size_t>(lean.triangleCount() / 2, std::max<size_t>(share * 4, 500'000)));+ } else if (textured) {+ info.texture = src->texture.get();+ }+ Mesh reduced = lean.triangleCount() > share ? decimate(lean, share) : std::move(lean);+ if (info.texture && reduced.uvs.size() != reduced.triangleCount() * 3) info.texture = nullptr;+ info.colors = reduced.hasColors();+ anyColor = anyColor || info.colors || info.texture;+ partInfo.push_back(info);+ reducedParts.push_back(std::move(reduced));+ }+ // One mesh: per triangle its part, per corner the part's texture UV, per vertex polypaint (white elsewhere).+ Mesh merged;+ std::vector<uint32_t> triPart;+ std::vector<glm::vec2> sourceUvs;+ const bool anyPolypaint = std::any_of(partInfo.begin(), partInfo.end(), [](const Part& p) { return p.colors; });+ for (size_t i = 0; i < reducedParts.size(); ++i) {+ const Mesh& reduced = reducedParts[i];+ const uint32_t base = uint32_t(merged.positions.size());+ merged.positions.insert(merged.positions.end(), reduced.positions.begin(), reduced.positions.end());+ if (anyPolypaint) {+ if (partInfo[i].colors) merged.colors.insert(merged.colors.end(), reduced.colors.begin(), reduced.colors.end());+ else merged.colors.resize(merged.positions.size(), glm::vec3(1.0f));+ }+ for (size_t t = 0; t < reduced.triangleCount(); ++t) {+ merged.triangles.push_back(reduced.triangles[t] + glm::uvec3(base));+ triPart.push_back(uint32_t(i));+ for (int c = 0; c < 3; ++c)+ sourceUvs.push_back(partInfo[i].texture ? reduced.uvs[t * 3 + size_t(c)] : glm::vec2(0.0f));+ }+ }+ reducedParts.clear();+ if (merged.triangleCount() == 0) {+ error = "Nothing to show: every polygon is hidden";+ return false;+ }++ // Stand it on the desk: centred, base at y = 0, largest side = sizeMeters.+ AABB box;+ for (const auto& p : merged.positions) box.expand(p);+ const glm::vec3 extent = box.extent();+ const float largest = std::max({extent.x, extent.y, extent.z, 1e-6f});+ const float scale = std::max(options.sizeMeters, 0.001f) / largest;+ const glm::vec3 offset(-box.center().x, -box.min.y, -box.center().z);+ for (auto& p : merged.positions) p = (p + offset) * scale;++ // Colour: new UVs for the merged mesh and one texture baked from each part's polypaint or texture.+ if (anyColor) {+ AutoUvResult atlas;+ std::string uvError;+ if (!computeAutoUv(merged.positions, merged.triangles, options.textureSize, atlas, uvError)) {+ error = "Couldn't lay out the colours (" + uvError + ")";+ return false;+ }+ std::vector<uint8_t> rgba = bake(merged.triangles, atlas.uvs, options.textureSize, [&](size_t t, glm::vec3 w) {+ const Part& part = partInfo[triPart[t]];+ if (part.texture)+ return sampleBaked(*part.texture, sourceUvs[t * 3] * w.x + sourceUvs[t * 3 + 1] * w.y + sourceUvs[t * 3 + 2] * w.z);+ if (part.colors) {+ const glm::uvec3 tri = merged.triangles[t];+ return linearToSrgb(merged.colors[tri[0]] * w.x + merged.colors[tri[1]] * w.y + merged.colors[tri[2]] * w.z);+ }+ return glm::vec3(1.0f);+ });+ auto tex = makeTexture(options.textureSize, options.textureSize);+ tex->pixels = std::move(rgba);+ merged.texture = tex;+ merged.uvs = std::move(atlas.uvs);+ merged.clearColors(); // the texture carries them (glTF would multiply vertex colours in again)+ out.colored = true;+ }+ merged.rebuildTopology();+ merged.recomputeAllNormals();++ // Write both files through the regular exporters.+ std::error_code ec;+ const std::string stamp = std::to_string(std::chrono::steady_clock::now().time_since_epoch().count());+ const fs::path dir = fs::temp_directory_path(ec);+ const fs::path glbPath = dir / ("sculptforge_ar_" + stamp + ".glb");+ const fs::path usdzPath = dir / ("sculptforge_ar_" + stamp + ".usdz");+ const auto text = [](const fs::path& p) {+ const auto u = p.u8string();+ return std::string(u.begin(), u.end());+ };+ bool ok = saveGlb(text(glbPath), merged, options.name, error) &&+ saveUsd(text(usdzPath), &merged, nullptr, options.name, error);+ if (ok) {+ out.glb = readFile(glbPath);+ out.usdz = readFile(usdzPath);+ ok = !out.glb.empty() && !out.usdz.empty();+ if (!ok) error = "Couldn't read the exported model back";+ }+ fs::remove(glbPath, ec);+ fs::remove(usdzPath, ec);+ if (!ok) return false;+ out.triangles = merged.triangleCount();+ char size[32];+ std::snprintf(size, sizeof size, "%.0f cm", double(options.sizeMeters) * 100.0);+ out.summary = withThousands(out.triangles) + " triangles" + (out.colored ? ", in colour" : "") + ", " + size;+ return true;+}++} // namespace sfdiff --git a/src/io/ArExport.h b/src/io/ArExport.hnew file mode 100644index 00000000..82dad33e--- /dev/null+++ b/src/io/ArExport.h@@ -0,0 +1,44 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// "View in AR" for the phone link (link/PhoneLink.h): the sculpt as a phone-friendly model standing on a real+// desk. The visible parts are reduced to a budget of triangles (vertex clustering first for very big meshes, then+// quadric decimation), polypaint and textures are baked into one texture on new UVs (AR Quick Look ignores vertex+// colours), and the model is scaled to a chosen size with its base at y = 0 and centred, so it stands on the+// detected surface. Written as GLB (Android Scene Viewer, WebXR viewers) and USDZ (iPhone and iPad AR Quick Look),+// in metres.+#pragma once++#include "core/Mesh.h"++#include <cstdint>+#include <string>+#include <vector>++namespace sf {++struct ArExportOptions {+ size_t maxTriangles = 80000; // phones show this smoothly; the files stay a few MB+ float sizeMeters = 0.20f; // the model's largest side on the desk+ int textureSize = 1024;+ std::string name = "SculptForge";+};++struct ArExportResult {+ std::string glb, usdz; // file contents+ size_t triangles = 0;+ bool colored = false; // polypaint or a texture was baked+ std::string summary; // "62,410 triangles, polypaint, 20 cm"+};++// `meshes`: the parts to show (null entries are skipped); hidden triangles are left out.+bool exportArModel(const std::vector<const Mesh*>& meshes, const ArExportOptions& options, ArExportResult& out,+ std::string& error);++// Pieces exposed for tests.+// Each corner's colour (sRGB 0..1) on new per-corner `uvs` baked into a size x size texture (rows bottom-up),+// with the islands padded by a few texels. `cornerColors` has 3 per triangle.+std::vector<uint8_t> bakeCornerColors(const std::vector<glm::uvec3>& triangles, const std::vector<glm::vec2>& uvs,+ const std::vector<glm::vec3>& cornerColors, int size);++} // namespace sfdiff --git a/src/link/FaceFeed.cpp b/src/link/FaceFeed.cppnew file mode 100644index 00000000..3538a30c--- /dev/null+++ b/src/link/FaceFeed.cpp@@ -0,0 +1,233 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+#include "link/FaceFeed.h"++#include "core/Json.h"++#include <algorithm>+#include <cmath>+#include <cstdio>+#include <cstring>+#include <filesystem>+#include <vector>++namespace sf::link {++namespace {++std::filesystem::path u8path(const std::string& s) { return std::filesystem::path(std::u8string(s.begin(), s.end())); }++void appendNumber(std::string& out, double v, int decimals) {+ char buf[32];+ std::snprintf(buf, sizeof buf, "%.*f", decimals, v);+ // Trim trailing zeros ("0.3100" -> "0.31", "1.0000" -> "1").+ std::string s = buf;+ if (s.find('.') != std::string::npos) {+ while (!s.empty() && s.back() == '0') s.pop_back();+ if (!s.empty() && s.back() == '.') s.pop_back();+ }+ if (s == "-0") s = "0";+ out += s;+}++} // namespace++const std::array<const char*, kArkitShapeCount>& arkitShapeNames() {+ static const std::array<const char*, kArkitShapeCount> names = {+ "eyeBlinkLeft", "eyeLookDownLeft", "eyeLookInLeft", "eyeLookOutLeft", "eyeLookUpLeft",+ "eyeSquintLeft", "eyeWideLeft", "eyeBlinkRight", "eyeLookDownRight", "eyeLookInRight",+ "eyeLookOutRight", "eyeLookUpRight", "eyeSquintRight", "eyeWideRight", "jawForward",+ "jawLeft", "jawRight", "jawOpen", "mouthClose", "mouthFunnel",+ "mouthPucker", "mouthLeft", "mouthRight", "mouthSmileLeft", "mouthSmileRight",+ "mouthFrownLeft", "mouthFrownRight", "mouthDimpleLeft", "mouthDimpleRight", "mouthStretchLeft",+ "mouthStretchRight", "mouthRollLower", "mouthRollUpper", "mouthShrugLower", "mouthShrugUpper",+ "mouthPressLeft", "mouthPressRight", "mouthLowerDownLeft", "mouthLowerDownRight", "mouthUpperUpLeft",+ "mouthUpperUpRight", "browDownLeft", "browDownRight", "browInnerUp", "browOuterUpLeft",+ "browOuterUpRight", "cheekPuff", "cheekSquintLeft", "cheekSquintRight", "noseSneerLeft",+ "noseSneerRight", "tongueOut"};+ return names;+}++int arkitShapeIndex(const std::string& name) {+ const auto& names = arkitShapeNames();+ for (int i = 0; i < kArkitShapeCount; ++i)+ if (name == names[size_t(i)]) return i;+ return -1;+}++int arkitMirrorIndex(int index) {+ if (index < 0 || index >= kArkitShapeCount) return index;+ std::string name = arkitShapeNames()[size_t(index)];+ std::string other;+ if (name.size() > 4 && name.compare(name.size() - 4, 4, "Left") == 0) other = name.substr(0, name.size() - 4) + "Right";+ else if (name.size() > 5 && name.compare(name.size() - 5, 5, "Right") == 0) other = name.substr(0, name.size() - 5) + "Left";+ else if (name == "jawLeft") other = "jawRight";+ else if (name == "jawRight") other = "jawLeft";+ else if (name == "mouthLeft") other = "mouthRight";+ else if (name == "mouthRight") other = "mouthLeft";+ else return index;+ const int i = arkitShapeIndex(other);+ return i >= 0 ? i : index;+}++float FaceFrame::weight(const char* name) const {+ const int i = arkitShapeIndex(name);+ return i >= 0 && present[size_t(i)] ? weights[size_t(i)] : 0.0f;+}++int FaceFrame::presentCount() const { return int(std::count(present.begin(), present.end(), true)); }++FaceFrame FaceFrame::mirrored() const {+ FaceFrame m = *this;+ for (int i = 0; i < kArkitShapeCount; ++i) {+ const int j = arkitMirrorIndex(i);+ m.weights[size_t(j)] = weights[size_t(i)];+ m.present[size_t(j)] = present[size_t(i)];+ }+ // Mirroring across the YZ plane: x stays, the rotation's y and z flip.+ m.head = glm::quat(head.w, head.x, -head.y, -head.z);+ return m;+}++std::string FaceFrame::weightsJson() const {+ std::string out = "{";+ bool first = true;+ for (int i = 0; i < kArkitShapeCount; ++i) {+ if (!present[size_t(i)]) continue;+ if (!first) out += ',';+ first = false;+ out += '"';+ out += arkitShapeNames()[size_t(i)];+ out += "\":";+ appendNumber(out, weights[size_t(i)], 4);+ }+ return out + "}";+}++std::string FaceFrame::messageJson() const {+ std::string out = "{\"type\":\"face\",\"t\":";+ appendNumber(out, t, 1);+ out += ",\"weights\":" + weightsJson();+ if (hasHead) {+ out += ",\"head\":[";+ const float q[4] = {head.x, head.y, head.z, head.w};+ for (int i = 0; i < 4; ++i) {+ if (i) out += ',';+ appendNumber(out, q[i], 5);+ }+ out += ']';+ }+ return out + "}";+}++bool parseFaceMessage(const std::string& text, FaceFrame& frame, std::string& error) {+ if (text.size() > kMaxFaceMessageBytes) {+ error = "Face message too large";+ return false;+ }+ Json msg;+ if (!Json::parse(text, msg, error)) return false;+ if (!msg.isObject() || msg["type"].asString() != "face") {+ error = "Not a face message";+ return false;+ }+ FaceFrame f;+ if (msg.has("t")) {+ if (!msg["t"].isNumber() || !std::isfinite(msg["t"].asNumber())) {+ error = "Face message: t must be a number";+ return false;+ }+ f.t = msg["t"].asNumber();+ }+ const Json& weights = msg["weights"];+ if (!weights.isObject()) {+ error = "Face message: weights must be an object";+ return false;+ }+ for (const auto& [name, value] : weights.members()) {+ if (!value.isNumber() || !std::isfinite(value.asNumber())) {+ error = "Face message: weight " + name + " is not a number";+ return false;+ }+ const int i = arkitShapeIndex(name);+ if (i < 0) {+ if (name != "_neutral") ++f.unknownNames;+ continue;+ }+ f.weights[size_t(i)] = std::clamp(float(value.asNumber()), 0.0f, 1.0f);+ f.present[size_t(i)] = true;+ }+ if (msg.has("head") && !msg["head"].isNull()) {+ const Json& h = msg["head"];+ if (!h.isArray() || h.size() != 4) {+ error = "Face message: head must be [qx, qy, qz, qw]";+ return false;+ }+ float q[4];+ for (size_t i = 0; i < 4; ++i) {+ if (!h[i].isNumber() || !std::isfinite(h[i].asNumber())) {+ error = "Face message: head must be four numbers";+ return false;+ }+ q[i] = float(h[i].asNumber());+ }+ const float len = std::sqrt(q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]);+ if (len < 1e-6f) {+ error = "Face message: head quaternion is zero";+ return false;+ }+ f.head = glm::quat(q[3] / len, q[0] / len, q[1] / len, q[2] / len);+ f.hasHead = true;+ }+ frame = f;+ return true;+}++bool FaceTakeWriter::open(const std::string& path, std::string& error) {+ close();+ out_.open(u8path(path), std::ios::binary | std::ios::trunc);+ if (!out_) {+ error = "Cannot write " + path;+ return false;+ }+ frames_ = 0;+ firstT_ = lastT_ = 0.0;+ return true;+}++void FaceTakeWriter::write(const FaceFrame& frame) {+ if (!out_.is_open()) return;+ if (frames_ == 0) firstT_ = frame.t;+ lastT_ = frame.t;+ ++frames_;+ out_ << frame.messageJson() << '\n';+}++void FaceTakeWriter::close() {+ if (out_.is_open()) out_.close();+}++bool readFaceTake(const std::string& path, std::vector<FaceFrame>& frames, std::string& error) {+ std::ifstream in(u8path(path), std::ios::binary);+ if (!in) {+ error = "Cannot read " + path;+ return false;+ }+ frames.clear();+ std::string line;+ int number = 0;+ while (std::getline(in, line)) {+ ++number;+ if (!line.empty() && line.back() == '\r') line.pop_back();+ if (line.empty()) continue;+ FaceFrame f;+ if (!parseFaceMessage(line, f, error)) {+ error = path + " line " + std::to_string(number) + ": " + error;+ return false;+ }+ frames.push_back(f);+ }+ return true;+}++} // namespace sf::linkdiff --git a/src/link/FaceFeed.h b/src/link/FaceFeed.hnew file mode 100644index 00000000..fcd1b6b7--- /dev/null+++ b/src/link/FaceFeed.h@@ -0,0 +1,74 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// The live face feed from the phone (link/PhoneLink.h): the phone runs MediaPipe Face Landmarker on its front+// camera and sends one JSON message per frame,+// {"type":"face","t":<ms>,"weights":{"jawOpen":0.31,...},"head":[qx,qy,qz,qw]}+// with the blend shape names of Apple ARKit (which MediaPipe's blendshape categories use too; MediaPipe has no+// tongueOut and adds "_neutral", which is ignored). The desktop hands the weights to the `face_weights` command+// (facial shapes on the active SubTool) and can record a take: one such message per line (JSON Lines).+#pragma once++#include <array>+#include <cstdint>+#include <fstream>+#include <string>+#include <vector>++#include <glm/gtc/quaternion.hpp>++namespace sf::link {++constexpr int kArkitShapeCount = 52;+// The 52 ARKit blend shape names, in Apple's documentation order.+const std::array<const char*, kArkitShapeCount>& arkitShapeNames();+// Index of an ARKit name, or -1.+int arkitShapeIndex(const std::string& name);+// The same shape on the other side ("eyeBlinkLeft" <-> "eyeBlinkRight"); itself for centre shapes.+int arkitMirrorIndex(int index);++struct FaceFrame {+ double t = 0.0; // the phone's clock, milliseconds+ std::array<float, kArkitShapeCount> weights{}; // 0..1+ std::array<bool, kArkitShapeCount> present{}; // sent in this message+ glm::quat head{1.0f, 0.0f, 0.0f, 0.0f}; // w, x, y, z (glm order); the message sends x, y, z, w+ bool hasHead = false;+ int unknownNames = 0; // weights whose names aren't ARKit's (ignored)++ float weight(const char* name) const;+ int presentCount() const;+ // Left and right swapped (shapes and the head's yaw and roll), for a feed that comes mirrored.+ FaceFrame mirrored() const;+ // {"jawOpen":0.31,...}: the `json` argument of face_weights (present shapes only, 4 decimals).+ std::string weightsJson() const;+ // The whole message again (as a take stores it).+ std::string messageJson() const;+};++constexpr size_t kMaxFaceMessageBytes = 16 * 1024;++// Parses one face message. False with `error` for anything else: not JSON, another type, a weight that isn't a+// number, a head that isn't four numbers. Weights are clamped to 0..1, the head quaternion is normalised.+bool parseFaceMessage(const std::string& text, FaceFrame& frame, std::string& error);++// Records a take: one message per line, written as frames arrive (thread-safe use is the caller's).+class FaceTakeWriter {+public:+ bool open(const std::string& path, std::string& error);+ void write(const FaceFrame& frame);+ void close();+ bool isOpen() const { return out_.is_open(); }+ int frames() const { return frames_; }+ // Milliseconds between the first and the last frame (phone clock).+ double durationMs() const { return frames_ > 0 ? lastT_ - firstT_ : 0.0; }++private:+ std::ofstream out_;+ int frames_ = 0;+ double firstT_ = 0.0, lastT_ = 0.0;+};++// Reads a take back (for tests and playback); false if a line doesn't parse.+bool readFaceTake(const std::string& path, std::vector<FaceFrame>& frames, std::string& error);++} // namespace sf::linkdiff --git a/src/link/HttpServer.cpp b/src/link/HttpServer.cppnew file mode 100644index 00000000..3b6651af--- /dev/null+++ b/src/link/HttpServer.cpp@@ -0,0 +1,529 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+#include "link/HttpServer.h"++#include <algorithm>+#include <cctype>+#include <chrono>+#include <cstdio>+#include <cstdlib>++namespace sf::link {++namespace {++std::string lower(std::string s) {+ for (char& c : s) c = char(std::tolower(static_cast<unsigned char>(c)));+ return s;+}++std::string trim(const std::string& s) {+ size_t a = 0, b = s.size();+ while (a < b && (s[a] == ' ' || s[a] == '\t')) ++a;+ while (b > a && (s[b - 1] == ' ' || s[b - 1] == '\t')) --b;+ return s.substr(a, b - a);+}++int hexValue(char c) {+ if (c >= '0' && c <= '9') return c - '0';+ if (c >= 'a' && c <= 'f') return c - 'a' + 10;+ if (c >= 'A' && c <= 'F') return c - 'A' + 10;+ return -1;+}++bool isTokenChar(char c) { // RFC 9110 tchar+ return std::isalnum(static_cast<unsigned char>(c)) || std::string_view("!#$%&'*+-.^_`|~").find(c) != std::string_view::npos;+}++bool parseIpv4(const std::string& text, uint32_t& out) {+ uint32_t value = 0;+ int parts = 0;+ size_t i = 0;+ while (parts < 4) {+ if (i >= text.size() || !std::isdigit(static_cast<unsigned char>(text[i]))) return false;+ uint32_t part = 0;+ size_t digits = 0;+ while (i < text.size() && std::isdigit(static_cast<unsigned char>(text[i]))) {+ part = part * 10 + uint32_t(text[i++] - '0');+ if (++digits > 3 || part > 255) return false;+ }+ value = (value << 8) | part;+ if (++parts < 4) {+ if (i >= text.size() || text[i] != '.') return false;+ ++i;+ }+ }+ if (i != text.size()) return false;+ out = value;+ return true;+}++} // namespace++// --- Requests and responses ---++const std::string& HttpRequest::header(const std::string& name) const {+ static const std::string empty;+ for (const auto& [k, v] : headers)+ if (k == name) return v;+ return empty;+}++std::string HttpRequest::param(const std::string& name, const std::string& fallback) const {+ return queryParameter(query, name, fallback);+}++bool HttpRequest::keepAlive() const {+ const std::string connection = lower(header("connection"));+ if (version == "HTTP/1.0") return connection.find("keep-alive") != std::string::npos;+ return connection.find("close") == std::string::npos;+}++HttpResponse HttpResponse::text(int status, std::string body) {+ HttpResponse r;+ r.status = status;+ r.body = std::move(body);+ return r;+}++HttpResponse HttpResponse::json(int status, std::string body) {+ HttpResponse r;+ r.status = status;+ r.contentType = "application/json";+ r.body = std::move(body);+ return r;+}++HttpResponse HttpResponse::html(std::string body) {+ HttpResponse r;+ r.contentType = "text/html; charset=utf-8";+ r.body = std::move(body);+ return r;+}++HttpResponse HttpResponse::bytes(std::string contentType, std::string body) {+ HttpResponse r;+ r.contentType = std::move(contentType);+ r.body = std::move(body);+ return r;+}++const char* httpReasonPhrase(int status) {+ switch (status) {+ case 200: return "OK";+ case 201: return "Created";+ case 204: return "No Content";+ case 302: return "Found";+ case 400: return "Bad Request";+ case 403: return "Forbidden";+ case 404: return "Not Found";+ case 405: return "Method Not Allowed";+ case 409: return "Conflict";+ case 411: return "Length Required";+ case 413: return "Content Too Large";+ case 416: return "Range Not Satisfiable";+ case 429: return "Too Many Requests";+ case 431: return "Request Header Fields Too Large";+ case 500: return "Internal Server Error";+ case 501: return "Not Implemented";+ case 503: return "Service Unavailable";+ case 505: return "HTTP Version Not Supported";+ default: return "Status";+ }+}++std::string httpResponseHead(const HttpResponse& response, bool keepAlive) {+ std::string head = "HTTP/1.1 " + std::to_string(response.status) + " " + httpReasonPhrase(response.status) + "\r\n";+ head += "Content-Type: " + response.contentType + "\r\n";+ head += "Content-Length: " + std::to_string(response.body.size()) + "\r\n";+ head += keepAlive ? "Connection: keep-alive\r\n" : "Connection: close\r\n";+ head += "Cache-Control: no-store\r\nX-Content-Type-Options: nosniff\r\nReferrer-Policy: no-referrer\r\n";+ for (const auto& [k, v] : response.headers) head += k + ": " + v + "\r\n";+ return head + "\r\n";+}++// --- Parser ---++HttpRequestParser::State HttpRequestParser::fail(int status, std::string text) {+ state_ = State::Error;+ errorStatus_ = status;+ errorText_ = std::move(text);+ return state_;+}++HttpRequestParser::State HttpRequestParser::feed(const char* data, size_t size) {+ if (state_ == State::Error || state_ == State::Done) {+ if (state_ == State::Done) buffer_.append(data, size); // the next request's bytes+ return state_;+ }+ buffer_.append(data, size);+ return parse();+}++HttpRequestParser::State HttpRequestParser::next() {+ if (state_ != State::Done) return state_;+ request_ = {};+ contentLength_ = 0;+ state_ = State::Head;+ return parse();+}++HttpRequestParser::State HttpRequestParser::parse() {+ if (state_ == State::Head) {+ // Tolerate blank lines before a request (RFC 9112 2.2).+ size_t skip = 0;+ while (skip + 1 < buffer_.size() && buffer_[skip] == '\r' && buffer_[skip + 1] == '\n') skip += 2;+ if (skip) buffer_.erase(0, skip);+ const size_t end = buffer_.find("\r\n\r\n");+ if (end == std::string::npos) {+ if (buffer_.size() > limits_.maxHeadBytes) return fail(431, "Request headers too large");+ return state_;+ }+ if (end + 4 > limits_.maxHeadBytes) return fail(431, "Request headers too large");+ if (!parseHead(buffer_.substr(0, end))) return state_;+ buffer_.erase(0, end + 4);+ state_ = State::Body;+ }+ if (state_ == State::Body) {+ if (buffer_.size() < contentLength_) return state_;+ request_.body.assign(buffer_, 0, contentLength_);+ buffer_.erase(0, contentLength_);+ state_ = State::Done;+ }+ return state_;+}++bool HttpRequestParser::parseHead(const std::string& head) {+ size_t lineEnd = head.find("\r\n");+ const std::string requestLine = head.substr(0, lineEnd);+ // method SP target SP version+ const size_t s1 = requestLine.find(' ');+ const size_t s2 = s1 == std::string::npos ? std::string::npos : requestLine.find(' ', s1 + 1);+ if (s1 == std::string::npos || s2 == std::string::npos || requestLine.find(' ', s2 + 1) != std::string::npos) {+ fail(400, "Malformed request line");+ return false;+ }+ request_.method = requestLine.substr(0, s1);+ request_.target = requestLine.substr(s1 + 1, s2 - s1 - 1);+ request_.version = requestLine.substr(s2 + 1);+ if (request_.method.empty() || !std::all_of(request_.method.begin(), request_.method.end(), isTokenChar)) {+ fail(400, "Malformed method");+ return false;+ }+ if (request_.version != "HTTP/1.1" && request_.version != "HTTP/1.0") {+ fail(505, "Unsupported HTTP version");+ return false;+ }+ if (request_.target.empty() || request_.target[0] != '/') {+ fail(400, "Only origin-form targets are accepted");+ return false;+ }+ for (unsigned char c : request_.target)+ if (c <= 0x20 || c >= 0x7f) {+ fail(400, "Invalid character in the target");+ return false;+ }+ const size_t q = request_.target.find('?');+ request_.path = percentDecode(request_.target.substr(0, q));+ request_.query = q == std::string::npos ? std::string() : request_.target.substr(q + 1);+ if (request_.path.find('\0') != std::string::npos) {+ fail(400, "Invalid path");+ return false;+ }++ bool sawLength = false;+ while (lineEnd != std::string::npos) {+ const size_t start = lineEnd + 2;+ lineEnd = head.find("\r\n", start);+ const std::string line = head.substr(start, lineEnd == std::string::npos ? std::string::npos : lineEnd - start);+ if (line.empty()) continue;+ if (line[0] == ' ' || line[0] == '\t') {+ fail(400, "Folded headers are not accepted");+ return false;+ }+ const size_t colon = line.find(':');+ if (colon == std::string::npos || colon == 0) {+ fail(400, "Malformed header");+ return false;+ }+ std::string name = line.substr(0, colon);+ if (!std::all_of(name.begin(), name.end(), isTokenChar)) {+ fail(400, "Malformed header name");+ return false;+ }+ std::string value = trim(line.substr(colon + 1));+ for (unsigned char c : value)+ if ((c < 0x20 && c != '\t') || c == 0x7f) {+ fail(400, "Invalid character in a header");+ return false;+ }+ if (request_.headers.size() >= limits_.maxHeaders) {+ fail(431, "Too many headers");+ return false;+ }+ name = lower(name);+ if (name == "transfer-encoding") {+ fail(501, "Chunked bodies are not accepted; send a Content-Length");+ return false;+ }+ if (name == "content-length") {+ if (value.empty() || value.size() > 15 || !std::all_of(value.begin(), value.end(), [](char c) {+ return std::isdigit(static_cast<unsigned char>(c));+ })) {+ fail(400, "Invalid Content-Length");+ return false;+ }+ const size_t length = size_t(std::stoull(value));+ if (sawLength && length != contentLength_) {+ fail(400, "Conflicting Content-Length headers");+ return false;+ }+ sawLength = true;+ contentLength_ = length;+ if (contentLength_ > limits_.maxBodyBytes) {+ fail(413, "Request body too large");+ return false;+ }+ }+ request_.headers.emplace_back(std::move(name), std::move(value));+ }+ return true;+}++// --- Helpers ---++std::string percentDecode(const std::string& text, bool plusIsSpace) {+ std::string out;+ out.reserve(text.size());+ for (size_t i = 0; i < text.size(); ++i) {+ const char c = text[i];+ if (c == '%' && i + 2 < text.size()) {+ const int hi = hexValue(text[i + 1]), lo = hexValue(text[i + 2]);+ if (hi >= 0 && lo >= 0) {+ out += char(hi * 16 + lo);+ i += 2;+ continue;+ }+ }+ out += plusIsSpace && c == '+' ? ' ' : c;+ }+ return out;+}++std::string percentEncode(const std::string& text) {+ static const char* hex = "0123456789ABCDEF";+ std::string out;+ for (unsigned char c : text) {+ if (std::isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') out += char(c);+ else {+ out += '%';+ out += hex[c >> 4];+ out += hex[c & 15];+ }+ }+ return out;+}++std::string queryParameter(const std::string& query, const std::string& name, const std::string& fallback) {+ size_t pos = 0;+ while (pos <= query.size()) {+ size_t amp = query.find('&', pos);+ if (amp == std::string::npos) amp = query.size();+ const std::string pair = query.substr(pos, amp - pos);+ const size_t eq = pair.find('=');+ if (percentDecode(pair.substr(0, eq), true) == name)+ return eq == std::string::npos ? std::string() : percentDecode(pair.substr(eq + 1), true);+ pos = amp + 1;+ }+ return fallback;+}++bool isLocalNetworkAddress(const std::string& ipv4) {+ uint32_t a = 0;+ if (!parseIpv4(ipv4, a)) return false;+ const auto in = [a](uint32_t net, int bits) { return (a >> (32 - bits)) == (net >> (32 - bits)); };+ return in(0x7F000000u, 8) // 127/8 loopback+ || in(0x0A000000u, 8) // 10/8+ || in(0xAC100000u, 12) // 172.16/12+ || in(0xC0A80000u, 16) // 192.168/16+ || in(0xA9FE0000u, 16) // 169.254/16 link-local+ || in(0x64400000u, 10); // 100.64/10 shared (carrier NAT, Tailscale)+}++std::vector<std::string> localNetworkAddresses() {+ // The address the system would use toward a few typical destinations: the default route first, then the+ // usual home and office ranges. Connecting a UDP socket sends nothing.+ std::vector<std::string> out;+ for (const char* probe : {"8.8.8.8", "192.168.1.1", "192.168.0.1", "10.0.0.1", "172.16.0.1", "100.100.100.100"}) {+ const std::string a = localAddressToward(probe);+ if (a.empty() || a.rfind("127.", 0) == 0 || !isLocalNetworkAddress(a)) continue;+ if (std::find(out.begin(), out.end(), a) == out.end()) out.push_back(a);+ }+ return out;+}++HttpClientResponse httpLocalRequest(const std::string& address, uint16_t port, const std::string& method,+ const std::string& target, const std::string& body,+ const std::vector<std::pair<std::string, std::string>>& headers, int timeoutMs) {+ HttpClientResponse out;+ Socket s;+ if (!s.connect(address, port, out.error)) return out;+ std::string req = method + " " + target + " HTTP/1.1\r\nHost: " + address + ":" + std::to_string(port) ++ "\r\nConnection: close\r\nContent-Length: " + std::to_string(body.size()) + "\r\n";+ for (const auto& [k, v] : headers) req += k + ": " + v + "\r\n";+ req += "\r\n" + body;+ if (!s.sendAll(req, out.error)) return out;+ std::string data;+ std::vector<char> buffer(64 * 1024);+ const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeoutMs);+ size_t headEnd = std::string::npos, length = std::string::npos;+ while (std::chrono::steady_clock::now() < deadline) {+ const long long n = s.readSome(buffer.data(), buffer.size(), 100);+ if (n < 0) break;+ if (n > 0) data.append(buffer.data(), size_t(n));+ if (headEnd == std::string::npos && (headEnd = data.find("\r\n\r\n")) != std::string::npos) {+ const std::string head = data.substr(0, headEnd);+ if (head.size() > 12) out.status = std::atoi(head.c_str() + 9);+ size_t pos = head.find("\r\n");+ while (pos != std::string::npos) {+ const size_t start = pos + 2, end = head.find("\r\n", start);+ const std::string line = head.substr(start, end == std::string::npos ? std::string::npos : end - start);+ const size_t colon = line.find(':');+ if (colon != std::string::npos) {+ out.headers.emplace_back(lower(line.substr(0, colon)), trim(line.substr(colon + 1)));+ if (out.headers.back().first == "content-length") length = size_t(std::atoll(out.headers.back().second.c_str()));+ }+ pos = end;+ }+ }+ if (headEnd != std::string::npos && length != std::string::npos && data.size() >= headEnd + 4 + length) break;+ }+ if (headEnd == std::string::npos) {+ out.status = 0;+ out.error = "No answer";+ return out;+ }+ out.body = data.substr(headEnd + 4, length == std::string::npos ? std::string::npos : length);+ return out;+}++// --- Server ---++bool HttpServer::start(const Options& options, Handler handler, std::string& error) {+ stop();+ options_ = options;+ handler_ = std::move(handler);+ uint16_t bound = 0;+ bool ok = false;+ std::string err;+ if (options.port != 0) {+ for (int i = 0; i < std::max(1, options.portTries) && !ok; ++i)+ ok = listener_.listen(options.bindAddress, uint16_t(options.port + i), bound, err, 16);+ }+ if (!ok) ok = listener_.listen(options.bindAddress, 0, bound, err, 16);+ if (!ok) {+ error = err;+ return false;+ }+ port_ = bound;+ running_ = true;+ acceptThread_ = std::thread([this] { acceptLoop(); });+ return true;+}++void HttpServer::stop() {+ if (!running_ && !acceptThread_.joinable()) return;+ running_ = false;+ if (acceptThread_.joinable()) acceptThread_.join();+ reapFinished(true);+ listener_.close();+ port_ = 0;+}++void HttpServer::reapFinished(bool all) {+ for (auto it = connections_.begin(); it != connections_.end();) {+ if (all || *it->done) {+ if (it->thread.joinable()) it->thread.join();+ it = connections_.erase(it);+ } else {+ ++it;+ }+ }+}++void HttpServer::acceptLoop() {+ while (running_) {+ reapFinished(false);+ auto client = std::make_shared<Socket>();+ std::string peer;+ if (!listener_.accept(*client, 200, &peer)) continue;+ std::string err;+ if (options_.localNetworkOnly && !isLocalNetworkAddress(peer)) {+ HttpResponse r = HttpResponse::text(403, "SculptForge only answers devices on the local network.\n");+ client->sendAll(httpResponseHead(r, false) + r.body, err);+ continue;+ }+ if (active_ >= options_.maxConnections) {+ HttpResponse r = HttpResponse::text(503, "Too many connections, try again.\n");+ r.headers.emplace_back("Retry-After", "1");+ client->sendAll(httpResponseHead(r, false) + r.body, err);+ continue;+ }+ auto done = std::make_shared<std::atomic<bool>>(false);+ ++active_;+ connections_.push_back({std::thread([this, client, peer, done] {+ serve(*client, peer);+ client->close();+ --active_;+ *done = true;+ }),+ done});+ }+}++void HttpServer::serve(Socket& client, const std::string& peer) {+ HttpRequestParser parser(options_.limits);+ std::string err;+ std::vector<char> buffer(256 * 1024);+ auto idleSince = std::chrono::steady_clock::now();+ while (running_) {+ if (!parser.done() && !parser.failed()) {+ const long long n = client.readSome(buffer.data(), buffer.size(), 250);+ if (n < 0) return; // closed+ if (n == 0) {+ if (std::chrono::steady_clock::now() - idleSince > std::chrono::milliseconds(options_.idleTimeoutMs)) return;+ continue;+ }+ idleSince = std::chrono::steady_clock::now();+ parser.feed(buffer.data(), size_t(n));+ }+ if (parser.failed()) {+ HttpResponse r = HttpResponse::text(parser.errorStatus(), parser.errorText() + "\n");+ client.sendAll(httpResponseHead(r, false) + r.body, err);+ return;+ }+ if (!parser.done()) continue;+ HttpRequest& request = parser.request();+ request.peer = peer;+ HttpResponse response;+ try {+ response = handler_ ? handler_(request) : HttpResponse::text(404, "Not found\n");+ } catch (const std::exception& e) {+ response = HttpResponse::text(500, std::string("Internal error: ") + e.what() + "\n");+ }+ const bool keep = request.keepAlive() && running_;+ std::string head = httpResponseHead(response, keep);+ if (request.method == "HEAD") response.body.clear();+ // Small responses go out in one send; big ones (models) after their head.+ if (response.body.size() < 64 * 1024) {+ if (!client.sendAll(head + response.body, err)) return;+ } else if (!client.sendAll(head, err) || !client.sendAll(response.body, err)) {+ return;+ }+ if (!keep) return;+ idleSince = std::chrono::steady_clock::now();+ parser.next();+ }+}++} // namespace sf::linkdiff --git a/src/link/HttpServer.h b/src/link/HttpServer.hnew file mode 100644index 00000000..277e84ec--- /dev/null+++ b/src/link/HttpServer.h@@ -0,0 +1,169 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// A small HTTP/1.1 server for the phone link (link/PhoneLink.h): the phone's browser loads the capture page and+// talks to the desktop over plain HTTP on the local network. Requests are parsed incrementally with hard limits+// (head size, body size, header count), bodies need a Content-Length (no chunked uploads), connections are kept+// alive, and only clients with local-network addresses are served unless told otherwise. One thread accepts,+// one thread serves each connection (at most `maxConnections`).+#pragma once++#include "link/Socket.h"++#include <atomic>+#include <cstdint>+#include <functional>+#include <memory>+#include <string>+#include <thread>+#include <utility>+#include <vector>++namespace sf::link {++struct HttpRequest {+ std::string method; // "GET", "POST", "PUT", ...+ std::string target; // as sent: "/api/file/3?offset=0"+ std::string path; // percent-decoded path without the query: "/api/file/3"+ std::string query; // raw query without '?'+ std::string version; // "HTTP/1.1"+ std::vector<std::pair<std::string, std::string>> headers; // names lower-cased, in order+ std::string body;+ std::string peer; // client IPv4 address++ // The first header called `name` (lower case), or "".+ const std::string& header(const std::string& name) const;+ // A query parameter, percent-decoded ('+' is a space); `fallback` when absent.+ std::string param(const std::string& name, const std::string& fallback = {}) const;+ bool keepAlive() const;+};++struct HttpResponse {+ int status = 200;+ std::string contentType = "text/plain; charset=utf-8";+ std::string body;+ std::vector<std::pair<std::string, std::string>> headers; // extra headers++ static HttpResponse text(int status, std::string body);+ static HttpResponse json(int status, std::string body);+ static HttpResponse html(std::string body);+ static HttpResponse bytes(std::string contentType, std::string body);+};++// The status line and headers (Content-Length, Connection, no-store caching, nosniff) for a response.+std::string httpResponseHead(const HttpResponse& response, bool keepAlive);+const char* httpReasonPhrase(int status);++struct HttpLimits {+ size_t maxHeadBytes = 16 * 1024; // request line + headers+ size_t maxHeaders = 64;+ size_t maxBodyBytes = 17 * 1024 * 1024; // the largest upload chunk plus room+};++// Incremental request parser: feed bytes as they arrive. After `done()`, `request()` holds the request and any+// bytes past it stay buffered for the next one (pipelining); call `next()` to start parsing it.+class HttpRequestParser {+public:+ enum class State { Head, Body, Done, Error };++ explicit HttpRequestParser(HttpLimits limits = {}) : limits_(limits) {}+ // Appends `size` bytes and parses as far as possible. Returns the state.+ State feed(const char* data, size_t size);+ State state() const { return state_; }+ bool done() const { return state_ == State::Done; }+ bool failed() const { return state_ == State::Error; }+ // On Error: the status to answer with (400, 413, 431, 501, 505) and why.+ int errorStatus() const { return errorStatus_; }+ const std::string& errorText() const { return errorText_; }+ // The declared body length (valid once the head is parsed), for early rejection of oversized uploads.+ size_t contentLength() const { return contentLength_; }+ HttpRequest& request() { return request_; }+ // Starts the next request with the bytes left over from the last one.+ State next();++private:+ State fail(int status, std::string text);+ State parse();+ bool parseHead(const std::string& head);++ HttpLimits limits_;+ State state_ = State::Head;+ std::string buffer_;+ HttpRequest request_;+ size_t contentLength_ = 0;+ int errorStatus_ = 0;+ std::string errorText_;+};++// Percent-decoding ('+' as space when `plusIsSpace`); invalid escapes are kept as they are.+std::string percentDecode(const std::string& text, bool plusIsSpace = false);+std::string percentEncode(const std::string& text);+// The `name` parameter of a query string ("a=1&b=2"), decoded.+std::string queryParameter(const std::string& query, const std::string& name, const std::string& fallback = {});++// Private (RFC 1918), loopback, link-local and shared (100.64/10: carrier NAT, Tailscale) IPv4 addresses.+bool isLocalNetworkAddress(const std::string& ipv4);+// This computer's IPv4 addresses on its local networks (the one with the default route first).+std::vector<std::string> localNetworkAddresses();++// A minimal client for this server (tests, the UI tests' scripted phone): one request on a new connection,+// waiting at most `timeoutMs` for the whole answer. `status` 0 = no answer.+struct HttpClientResponse {+ int status = 0;+ std::string body;+ std::vector<std::pair<std::string, std::string>> headers; // names lower-cased+ std::string error;+};+HttpClientResponse httpLocalRequest(const std::string& address, uint16_t port, const std::string& method,+ const std::string& target, const std::string& body = {},+ const std::vector<std::pair<std::string, std::string>>& headers = {},+ int timeoutMs = 10000);++class HttpServer {+public:+ using Handler = std::function<HttpResponse(const HttpRequest&)>;++ struct Options {+ uint16_t port = 0; // 0 = any free port+ int portTries = 10; // try port, port + 1, ... before taking any free one+ std::string bindAddress = "0.0.0.0";+ bool localNetworkOnly = true; // refuse clients outside the local network (403)+ int maxConnections = 16;+ int idleTimeoutMs = 15000; // a kept-alive connection with no request closes after this+ HttpLimits limits;+ };++ HttpServer() = default;+ ~HttpServer() { stop(); }+ HttpServer(const HttpServer&) = delete;+ HttpServer& operator=(const HttpServer&) = delete;++ // Starts listening and serving on background threads. `handler` runs on connection threads (concurrently).+ bool start(const Options& options, Handler handler, std::string& error);+ // Closes the listener and every connection, and waits for their threads.+ void stop();+ bool running() const { return running_; }+ uint16_t port() const { return port_; }+ int activeConnections() const { return active_; }++private:+ void acceptLoop();+ void serve(Socket& client, const std::string& peer);+ void reapFinished(bool all);++ struct Connection {+ std::thread thread;+ std::shared_ptr<std::atomic<bool>> done;+ };++ Options options_;+ Handler handler_;+ Socket listener_;+ uint16_t port_ = 0;+ std::atomic<bool> running_{false};+ std::atomic<int> active_{0};+ std::thread acceptThread_;+ std::vector<Connection> connections_; // accept thread only (and stop, after it has ended)+};++} // namespace sf::linkdiff --git a/src/link/PhoneLink.cpp b/src/link/PhoneLink.cppnew file mode 100644index 00000000..d1fcb0d4--- /dev/null+++ b/src/link/PhoneLink.cpp@@ -0,0 +1,1086 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+#include "link/PhoneLink.h"++#include "ai/Http.h"+#include "core/Json.h"+#include "link/PhoneRtc.h"++#include <algorithm>+#include <cctype>+#include <cstdlib>+#include <ctime>+#include <filesystem>+#include <fstream>++namespace sf::phone_assets {+// The pages the server hands out (cmake: embed_files, assets/phone/capture.html and website/link/index.html).+extern const unsigned char capturePage[];+extern const unsigned long long capturePage_size;+extern const unsigned char facePage[];+extern const unsigned long long facePage_size;+} // namespace sf::phone_assets++namespace sf::link {++namespace fs = std::filesystem;++namespace {++constexpr auto kUnusedTokenLifetime = std::chrono::minutes(10);+constexpr int kMaxPairFailures = 5;+constexpr int kMaxOpenCaptures = 8;++fs::path u8path(const std::string& s) { return fs::path(std::u8string(s.begin(), s.end())); }+std::string pathString(const fs::path& p) {+ const auto u = p.u8string();+ return std::string(u.begin(), u.end());+}++std::string hex(const std::string& bytes) {+ static const char* digits = "0123456789abcdef";+ std::string out;+ for (unsigned char c : bytes) {+ out += digits[c >> 4];+ out += digits[c & 15];+ }+ return out;+}++std::string newId() { return hex(randomBytes(8)); }++std::string newCode() {+ const std::string r = randomBytes(4);+ const uint32_t v = uint32_t(uint8_t(r[0])) | uint32_t(uint8_t(r[1])) << 8 | uint32_t(uint8_t(r[2])) << 16 |+ uint32_t(uint8_t(r[3])) << 24;+ char buf[8];+ std::snprintf(buf, sizeof buf, "%06u", v % 1000000u);+ return buf;+}++// Compares without stopping at the first difference (the pairing code and token).+bool sameSecret(const std::string& a, const std::string& b) {+ if (a.size() != b.size() || a.empty()) return false;+ unsigned char diff = 0;+ for (size_t i = 0; i < a.size(); ++i) diff |= uint8_t(a[i]) ^ uint8_t(b[i]);+ return diff == 0;+}++HttpResponse jsonError(int status, const std::string& message, const std::string& code = {}) {+ Json j = Json::object();+ j.set("error", message);+ if (!code.empty()) j.set("code", code);+ return HttpResponse::json(status, j.dump());+}++HttpResponse jsonOk(const Json& j) { return HttpResponse::json(200, j.dump()); }++std::string lowerExtension(const std::string& name) {+ const size_t dot = name.rfind('.');+ if (dot == std::string::npos) return {};+ std::string e = name.substr(dot);+ for (char& c : e) c = char(std::tolower(static_cast<unsigned char>(c)));+ return e;+}++bool parseUnsigned(const std::string& text, uint64_t& out) {+ if (text.empty() || text.size() > 19) return false;+ uint64_t v = 0;+ for (char c : text) {+ if (!std::isdigit(static_cast<unsigned char>(c))) return false;+ v = v * 10 + uint64_t(c - '0');+ }+ out = v;+ return true;+}++std::string isoTime(std::chrono::system_clock::time_point t) {+ const std::time_t tt = std::chrono::system_clock::to_time_t(t);+ std::tm tm{};+#if defined(_WIN32)+ localtime_s(&tm, &tt);+#else+ localtime_r(&tt, &tm);+#endif+ char buf[32];+ std::strftime(buf, sizeof buf, "%Y-%m-%dT%H:%M:%S", &tm);+ return buf;+}++std::string envOr(const char* name, const std::string& fallback) {+ const char* v = std::getenv(name);+ return v && *v ? std::string(v) : fallback;+}++// The path after `prefix` up to the next '/', and what follows: "/api/capture/<id>/finish".+bool splitId(const std::string& path, const std::string& prefix, std::string& id, std::string& rest) {+ if (path.rfind(prefix, 0) != 0) return false;+ const std::string tail = path.substr(prefix.size());+ const size_t slash = tail.find('/');+ id = tail.substr(0, slash);+ rest = slash == std::string::npos ? std::string() : tail.substr(slash);+ return !id.empty() && std::all_of(id.begin(), id.end(), [](char c) { return std::isxdigit(static_cast<unsigned char>(c)); });+}++} // namespace++// --- Free helpers ---++const char* captureKindName(CaptureKind kind) {+ switch (kind) {+ case CaptureKind::Splat: return "splat";+ case CaptureKind::Photo: return "photo";+ case CaptureKind::Ply: return "ply";+ case CaptureKind::Face: return "face";+ }+ return "splat";+}++bool parseCaptureKind(const std::string& text, CaptureKind& kind) {+ if (text == "splat") kind = CaptureKind::Splat;+ else if (text == "photo") kind = CaptureKind::Photo;+ else if (text == "ply") kind = CaptureKind::Ply;+ else return false; // (face takes are recorded on the desktop, not uploaded)+ return true;+}++std::string sanitizeUploadName(const std::string& name) {+ // Only the last path component (browsers send bare names, but be sure).+ std::string base = name;+ const size_t slash = base.find_last_of("/\\");+ if (slash != std::string::npos) base = base.substr(slash + 1);+ std::string out;+ for (unsigned char c : base) {+ if (std::isalnum(c) || c == '.' || c == '-' || c == '_') out += char(c);+ else if (c >= 0x80) continue; // UTF-8 bytes: dropped rather than doubled into underscores+ else out += '_';+ }+ while (!out.empty() && (out[0] == '.' || out[0] == '_' || out[0] == '-')) out.erase(0, 1);+ while (!out.empty() && (out.back() == '.' || out.back() == ' ')) out.pop_back();+ if (out.size() > 80) {+ const std::string ext = lowerExtension(out);+ out = out.substr(0, 80 - std::min<size_t>(ext.size(), 16)) + ext.substr(0, 16);+ }+ if (out.empty() || out.find_first_not_of('.') == std::string::npos) return {};+ // Names Windows reserves, with or without an extension.+ std::string stem = out.substr(0, out.find('.'));+ for (char& c : stem) c = char(std::toupper(static_cast<unsigned char>(c)));+ for (const char* reserved : {"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7",+ "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"})+ if (stem == reserved) return "file_" + out;+ if (out == "capture.json") return "capture_file.json"; // ours+ return out;+}++bool captureExtensionAllowed(CaptureKind kind, const std::string& extension) {+ static const char* video[] = {".mp4", ".mov", ".m4v", ".webm", ".3gp"};+ static const char* image[] = {".jpg", ".jpeg", ".png", ".heic", ".heif", ".webp"};+ static const char* splat[] = {".ply", ".splat", ".spz"};+ auto in = [&](const auto& list) {+ return std::any_of(std::begin(list), std::end(list), [&](const char* e) { return extension == e; });+ };+ switch (kind) {+ case CaptureKind::Splat: return in(video) || in(image);+ case CaptureKind::Photo: return in(image);+ case CaptureKind::Ply: return in(splat);+ case CaptureKind::Face: return extension == ".jsonl";+ }+ return false;+}++std::string describeDevice(const std::string& ua) {+ auto has = [&](const char* s) { return ua.find(s) != std::string::npos; };+ std::string device = has("iPhone") ? "iPhone"+ : has("iPad") ? "iPad"+ : has("Android") ? "Android"+ : has("Macintosh") && has("Mobile") ? "iPad"+ : has("Macintosh") ? "Mac"+ : has("Windows") ? "Windows PC"+ : has("Linux") ? "Linux PC"+ : "Device";+ std::string browser = has("CriOS") || (has("Chrome") && !has("Edg") && !has("SamsungBrowser") && !has("OPR"))+ ? "Chrome"+ : has("FxiOS") || has("Firefox") ? "Firefox"+ : has("EdgiOS") || has("EdgA") || has("Edg/") ? "Edge"+ : has("SamsungBrowser") ? "Samsung Internet"+ : has("OPR") || has("Opera") ? "Opera"+ : has("Safari") ? "Safari"+ : "";+ return browser.empty() ? device : device + " \xC2\xB7 " + browser;+}++std::string captureTimestamp(std::chrono::system_clock::time_point t) {+ const std::time_t tt = std::chrono::system_clock::to_time_t(t);+ std::tm tm{};+#if defined(_WIN32)+ localtime_s(&tm, &tt);+#else+ localtime_r(&tt, &tm);+#endif+ char buf[32];+ std::strftime(buf, sizeof buf, "%Y-%m-%d_%H%M%S", &tm);+ return buf;+}++// --- Session state ---++struct PhoneLink::Upload {+ std::string id, captureId, name, path;+ CaptureKind kind = CaptureKind::Splat;+ uint64_t size = 0, received = 0;+ Clock::time_point started{}, lastChunk{};+ double bytesPerSecond = 0.0;+ bool finished = false;+ std::mutex write; // one chunk at a time+};++struct PhoneLink::Capture {+ std::string id, device, dir; // dir: <inbox>/.incoming/<id> until finished+ CaptureKind kind = CaptureKind::Splat;+ std::vector<std::shared_ptr<Upload>> files;+ uint64_t bytes = 0;+ std::chrono::system_clock::time_point started{};+ bool finished = false;+ std::string folder; // final folder once finished+};++PhoneLink::PhoneLink() = default;++PhoneLink::~PhoneLink() { stop(); }++void PhoneLink::newToken() {+ token_ = hex(randomBytes(16));+ code_ = newCode();+ tokenIssued_ = Clock::now();+ paired_ = false;+ failedPairs_ = 0;+}++bool PhoneLink::start(const PhoneLinkOptions& options, std::string& error) {+ stop();+ options_ = options;+ if (options_.siteUrl.empty()) options_.siteUrl = envOr("SCULPTFORGE_LINK_URL", kPhoneLinkSiteUrl);+ if (options_.relayUrl.empty()) options_.relayUrl = envOr("SCULPTFORGE_LINK_RELAY", kPhoneLinkRelayUrl);+ if (options_.inboxRoot.empty()) {+ error = "No Captures folder (the user library is unavailable)";+ return false;+ }+ std::error_code ec;+ fs::create_directories(u8path(options_.inboxRoot) / ".incoming", ec);+ if (ec) {+ error = "Cannot create " + options_.inboxRoot + ": " + ec.message();+ return false;+ }+ {+ std::lock_guard lock(mutex_);+ newToken();+ addresses_ = localNetworkAddresses();+ address_ = addresses_.empty() ? "127.0.0.1" : addresses_.front();+ lastError_.clear();+ }+ if (options_.startServer) {+ HttpServer::Options so;+ so.port = options_.port;+ so.localNetworkOnly = options_.localNetworkOnly;+ so.limits.maxBodyBytes = kMaxUploadChunkBytes + 64 * 1024;+ if (!server_.start(so, [this](const HttpRequest& r) { return handle(r); }, error)) {+ error = "Cannot open the phone link's network port: " + error;+ return false;+ }+ }+ running_ = true;+ return true;+}++void PhoneLink::stop() {+ if (!running_ && !server_.running()) return;+ server_.stop();+ stopRelay();+ {+ std::lock_guard lock(rtcMutex_);+ if (rtc_) rtc_->close();+ rtc_.reset();+ }+ stopTake();+ std::lock_guard lock(mutex_);+ // Unfinished uploads can't resume in another session (the token changes): remove their files.+ std::error_code ec;+ for (const auto& [id, capture] : captures_)+ if (!capture->finished) fs::remove_all(u8path(capture->dir), ec);+ captures_.clear();+ uploads_.clear();+ faceConnected_ = false;+ seen_ = false;+ paired_ = false;+ arGlb_.clear();+ arUsdz_.clear();+ arBusy_ = false;+ running_ = false;+}++std::vector<std::string> PhoneLink::addresses() const {+ std::lock_guard lock(mutex_);+ return addresses_;+}++void PhoneLink::setAddress(const std::string& address) {+ std::lock_guard lock(mutex_);+ address_ = address;+}++uint16_t PhoneLink::port() const { return options_.startServer ? server_.port() : options_.port; }++std::string PhoneLink::url() const {+ std::lock_guard lock(mutex_);+ return "http://" + address_ + ":" + std::to_string(port()) + "/?k=" + token_;+}++std::string PhoneLink::code() const {+ std::lock_guard lock(mutex_);+ return code_;+}++void PhoneLink::pushEvent(PhoneEvent event) {+ // (mutex_ held by the caller)+ if (event.type == PhoneEvent::Type::Error) lastError_ = event.text;+ events_.push_back(std::move(event));+ while (events_.size() > 256) events_.pop_front();+}++std::vector<PhoneEvent> PhoneLink::takeEvents() {+ std::lock_guard lock(mutex_);+ std::vector<PhoneEvent> out(events_.begin(), events_.end());+ events_.clear();+ return out;+}++PhoneLinkStatus PhoneLink::status() const {+ PhoneLinkStatus s;+ const auto now = Clock::now();+ const auto seconds = [now](Clock::time_point t) { return std::chrono::duration<double>(now - t).count(); };+ std::lock_guard lock(mutex_);+ s.running = running_;+ s.address = address_ + ":" + std::to_string(port());+ s.url = "http://" + s.address + "/?k=" + token_;+ s.code = code_;+ s.paired = paired_;+ s.tokenExpiresIn = paired_ ? 0.0 : std::max(0.0, std::chrono::duration<double>(kUnusedTokenLifetime).count() - seconds(tokenIssued_));+ s.device = device_;+ s.deviceAddress = deviceAddress_;+ s.secondsSinceSeen = seen_ ? seconds(lastSeen_) : -1.0;+ for (const auto& [id, u] : uploads_) {+ PhoneUploadInfo info;+ info.captureId = u->captureId;+ info.name = u->name;+ info.kind = u->kind;+ info.size = u->size;+ info.received = u->received;+ info.bytesPerSecond = u->bytesPerSecond;+ info.finished = u->finished;+ s.uploads.push_back(info);+ }+ s.capturesReceived = capturesReceived_;+ s.arVersion = arVersion_;+ s.arBusy = arBusy_;+ s.arSummary = arSummary_;+ s.arError = arError_;+ s.secondsSinceArViewed = arViewedEver_ ? seconds(arViewed_) : -1.0;+ s.faceConnected = faceConnected_;+ s.faceTransport = faceTransport_;+ s.faceFrames = faceSerial_;+ s.secondsSinceFace = faceSerial_ ? seconds(lastFace_) : -1.0;+ size_t recent = 0;+ for (const auto& t : faceTimes_)+ if (now - t <= std::chrono::seconds(1)) ++recent;+ s.faceFps = double(recent);+ s.recording = take_.isOpen();+ s.takeFrames = take_.frames();+ s.takeSeconds = take_.durationMs() / 1000.0;+ s.lastError = lastError_;+ return s;+}++// --- Requests ---++HttpResponse PhoneLink::handle(const HttpRequest& request) {+ HttpResponse r = route(request);+ return r;+}++bool PhoneLink::authorised(const HttpRequest& request) {+ std::string token = request.param("k");+ if (token.empty()) token = request.header("x-sf-token");+ std::lock_guard lock(mutex_);+ if (!paired_ && Clock::now() - tokenIssued_ > kUnusedTokenLifetime) {+ newToken(); // nobody used it in time: the QR code shows a fresh one+ return false;+ }+ if (!sameSecret(token, token_)) return false;+ paired_ = true;+ return true;+}++void PhoneLink::noteDevice(const HttpRequest& request) {+ const std::string device = describeDevice(request.header("user-agent"));+ std::lock_guard lock(mutex_);+ const bool fresh = !seen_ || device != device_ || request.peer != deviceAddress_ || Clock::now() - lastSeen_ > std::chrono::seconds(30);+ device_ = device;+ deviceAddress_ = request.peer;+ lastSeen_ = Clock::now();+ seen_ = true;+ if (fresh) pushEvent(PhoneEvent::make(PhoneEvent::Type::DeviceConnected, device));+}++HttpResponse PhoneLink::route(const HttpRequest& request) {+ const std::string& path = request.path;+ const std::string& method = request.method;+ const bool get = method == "GET" || method == "HEAD";++ if (get && (path == "/" || path == "/index.html")) {+ HttpResponse r = HttpResponse::html(std::string(reinterpret_cast<const char*>(phone_assets::capturePage),+ size_t(phone_assets::capturePage_size)));+ r.headers.emplace_back("Content-Security-Policy",+ "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; "+ "img-src 'self' data: blob:; media-src 'self' blob:; connect-src 'self'; frame-ancestors 'none'");+ return r;+ }+ if (get && path == "/face") // the face page from this computer (works where its address counts as secure: localhost)+ return HttpResponse::html(std::string(reinterpret_cast<const char*>(phone_assets::facePage), size_t(phone_assets::facePage_size)));+ if (get && (path == "/favicon.ico" || path == "/apple-touch-icon.png" || path == "/apple-touch-icon-precomposed.png")) {+ HttpResponse r = HttpResponse::text(404, "");+ return r;+ }+ if (method == "POST" && path == "/api/pair") return pair(request);++ const bool api = path.rfind("/api/", 0) == 0 || path.rfind("/ar/", 0) == 0 || path == "/rtc-answer";+ if (!api) return HttpResponse::text(404, "Not found\n");+ if (!authorised(request))+ return jsonError(403, "This link has expired or is not for this session. Scan the QR code on your computer again.",+ "token");+ noteDevice(request);++ if (get && path == "/api/status") return HttpResponse::json(200, statusJson());+ if (method == "POST" && path == "/api/capture") return startCapture(request);+ std::string id, rest;+ if (splitId(path, "/api/capture/", id, rest)) {+ if (method == "POST" && rest == "/file") return addFile(id, request);+ if (method == "POST" && rest == "/finish") return finishCapture(id);+ if (method == "POST" && rest == "/cancel") return cancelCapture(id);+ return jsonError(404, "Unknown capture request");+ }+ if (splitId(path, "/api/file/", id, rest) && rest.empty()) {+ if (method == "PUT" || method == "POST") return putChunk(id, request);+ if (get) return fileStatus(id);+ return jsonError(405, "Use PUT to send data");+ }+ if (method == "POST" && path == "/api/ar") return requestAr(request);+ if (get && path == "/ar/model.glb") return serveArModel(false);+ if (get && path == "/ar/model.usdz") return serveArModel(true);+ if (method == "POST" && path == "/api/face") {+ // One message, or several (one per line) from a phone that batches.+ size_t pos = 0, accepted = 0;+ std::string error;+ while (pos < request.body.size()) {+ size_t end = request.body.find('\n', pos);+ if (end == std::string::npos) end = request.body.size();+ const std::string line = request.body.substr(pos, end - pos);+ pos = end + 1;+ if (line.find_first_not_of(" \r\t") == std::string::npos) continue;+ if (receiveFaceMessage(line, "Wi-Fi (HTTP)", error)) ++accepted;+ else return jsonError(400, error);+ }+ Json j = Json::object();+ j.set("accepted", double(accepted));+ return jsonOk(j);+ }+ if (get && path == "/api/face-link") return faceLink();+ if (get && path == "/rtc-answer") return directAnswer(request);+ return jsonError(404, "Unknown request");+}++HttpResponse PhoneLink::pair(const HttpRequest& request) {+ Json body;+ std::string err;+ std::string code;+ if (Json::parse(request.body, body, err)) code = body["code"].asString();+ code.erase(std::remove_if(code.begin(), code.end(), [](char c) { return !std::isdigit(static_cast<unsigned char>(c)); }),+ code.end());+ std::lock_guard lock(mutex_);+ const auto now = Clock::now();+ if (now - lastPairAttempt_ < std::chrono::milliseconds(800)) return jsonError(429, "Too fast - wait a second and try again.");+ lastPairAttempt_ = now;+ if (!paired_ && now - tokenIssued_ > kUnusedTokenLifetime) newToken();+ if (!sameSecret(code, code_)) {+ if (++failedPairs_ >= kMaxPairFailures) {+ code_ = newCode(); // too many guesses: the code on the computer changes+ failedPairs_ = 0;+ pushEvent(PhoneEvent::make(PhoneEvent::Type::Error, "Several wrong pairing codes were typed on a phone; the code changed."));+ return jsonError(403, "Too many wrong codes. Use the new code shown on your computer.", "code");+ }+ return jsonError(403, "That's not the code shown on your computer.", "code");+ }+ paired_ = true;+ Json j = Json::object();+ j.set("token", token_);+ return jsonOk(j);+}++std::string PhoneLink::statusJson() {+ Json j = Json::object();+ std::lock_guard lock(mutex_);+ j.set("desktop", options_.desktopName.empty() ? std::string("SculptForge") : options_.desktopName);+ j.set("version", options_.version);+ j.set("device", device_);+ Json limits = Json::object();+ limits.set("chunk", double(kUploadChunkBytes));+ limits.set("file", double(kMaxCaptureFileBytes));+ limits.set("capture", double(kMaxCaptureBytes));+ limits.set("files", double(kMaxCaptureFiles));+ j.set("limits", limits);+ Json ar = Json::object();+ ar.set("version", double(arVersion_));+ ar.set("busy", arBusy_);+ ar.set("summary", arSummary_);+ ar.set("error", arError_);+ j.set("ar", ar);+ Json face = Json::object();+ face.set("webrtc", webrtcAvailable());+ face.set("connected", faceConnected_);+ face.set("recording", take_.isOpen());+ j.set("face", face);+ j.set("captures", double(capturesReceived_));+ return j.dump();+}++HttpResponse PhoneLink::startCapture(const HttpRequest& request) {+ Json body;+ std::string err;+ if (!Json::parse(request.body, body, err) || !body.isObject()) return jsonError(400, "Expected JSON: {\"kind\": ...}");+ CaptureKind kind;+ if (!parseCaptureKind(body["kind"].asString(), kind)) return jsonError(400, "Unknown capture kind");+ auto capture = std::make_shared<Capture>();+ capture->id = newId();+ capture->kind = kind;+ capture->started = std::chrono::system_clock::now();+ std::lock_guard lock(mutex_);+ capture->device = device_;+ const auto open = std::count_if(captures_.begin(), captures_.end(), [](const auto& c) { return !c.second->finished; });+ if (open >= kMaxOpenCaptures) return jsonError(429, "Too many uploads at once; finish or cancel one first.");+ capture->dir = pathString(u8path(options_.inboxRoot) / ".incoming" / capture->id);+ std::error_code ec;+ fs::create_directories(u8path(capture->dir), ec);+ if (ec) {+ pushEvent(PhoneEvent::make(PhoneEvent::Type::Error, "Cannot write to the Captures folder: " + ec.message()));+ return jsonError(500, "Your computer can't save the capture (" + ec.message() + ").");+ }+ captures_[capture->id] = capture;+ Json j = Json::object();+ j.set("id", capture->id);+ j.set("chunk", double(kUploadChunkBytes));+ return jsonOk(j);+}++HttpResponse PhoneLink::addFile(const std::string& captureId, const HttpRequest& request) {+ Json body;+ std::string err;+ if (!Json::parse(request.body, body, err) || !body.isObject()) return jsonError(400, "Expected JSON: {\"name\": ..., \"size\": ...}");+ const double sizeNumber = body["size"].asNumber(-1);+ if (!(sizeNumber >= 1) || sizeNumber > double(kMaxCaptureFileBytes))+ return jsonError(413, "That file is too big (the limit is 4 GB per file).");+ const uint64_t size = uint64_t(sizeNumber);+ std::lock_guard lock(mutex_);+ const auto it = captures_.find(captureId);+ if (it == captures_.end() || it->second->finished) return jsonError(404, "Unknown or finished capture");+ Capture& capture = *it->second;+ if (capture.files.size() >= kMaxCaptureFiles) return jsonError(413, "Too many files in one capture (500 at most).");+ if (capture.bytes + size > kMaxCaptureBytes) return jsonError(413, "This capture is too big (8 GB at most).");+ std::string name = sanitizeUploadName(body["name"].asString());+ const std::string ext = lowerExtension(name);+ if (name.empty() || !captureExtensionAllowed(capture.kind, ext))+ return jsonError(400, "SculptForge can't use this kind of file here (" + (ext.empty() ? std::string("no extension") : ext) + ").");+ // Unique within the capture ("IMG_0001.jpg", "IMG_0001-2.jpg").+ const std::string stem = name.substr(0, name.size() - ext.size());+ for (int n = 2; std::any_of(capture.files.begin(), capture.files.end(), [&](const auto& f) { return f->name == name; }); ++n)+ name = stem + "-" + std::to_string(n) + ext;+ auto upload = std::make_shared<Upload>();+ upload->id = newId();+ upload->captureId = captureId;+ upload->name = name;+ upload->kind = capture.kind;+ upload->size = size;+ upload->path = pathString(u8path(capture.dir) / (name + ".part"));+ upload->started = upload->lastChunk = Clock::now();+ {+ std::ofstream create(u8path(upload->path), std::ios::binary | std::ios::trunc);+ if (!create) return jsonError(500, "Your computer can't save the file.");+ }+ capture.files.push_back(upload);+ capture.bytes += size;+ uploads_[upload->id] = upload;+ Json j = Json::object();+ j.set("id", upload->id);+ j.set("name", name);+ j.set("received", 0.0);+ j.set("chunk", double(kUploadChunkBytes));+ return jsonOk(j);+}++HttpResponse PhoneLink::putChunk(const std::string& fileId, const HttpRequest& request) {+ std::shared_ptr<Upload> upload;+ {+ std::lock_guard lock(mutex_);+ const auto it = uploads_.find(fileId);+ if (it == uploads_.end()) return jsonError(404, "Unknown upload (the computer may have closed the link)");+ upload = it->second;+ }+ uint64_t offset = 0;+ if (!parseUnsigned(request.param("offset"), offset)) return jsonError(400, "Missing offset");+ if (request.body.size() > kMaxUploadChunkBytes) return jsonError(413, "Chunk too large");+ std::lock_guard write(upload->write);+ Json j = Json::object();+ if (upload->finished) {+ j.set("received", double(upload->received));+ return jsonOk(j);+ }+ if (offset > upload->received) { // a chunk went missing: the phone resumes from what arrived+ j.set("error", "Out of order");+ j.set("received", double(upload->received));+ return HttpResponse::json(409, j.dump());+ }+ if (offset + request.body.size() > upload->size) return jsonError(400, "More data than the file's size");+ const uint64_t skip = upload->received - offset; // a resent chunk overlapping what we have+ if (skip < request.body.size()) {+ std::ofstream out(u8path(upload->path), std::ios::binary | std::ios::app);+ out.write(request.body.data() + skip, std::streamsize(request.body.size() - skip));+ out.close();+ if (!out) {+ std::lock_guard lock(mutex_);+ pushEvent(PhoneEvent::make(PhoneEvent::Type::Error, "Saving " + upload->name + " failed: is the disk full?"));+ return jsonError(500, "Your computer couldn't save the data (disk full?).");+ }+ const uint64_t added = request.body.size() - skip;+ const auto now = Clock::now();+ const double dt = std::max(1e-3, std::chrono::duration<double>(now - upload->lastChunk).count());+ const double rate = double(added) / dt;+ {+ std::lock_guard lock(mutex_);+ upload->received += added;+ upload->bytesPerSecond = upload->bytesPerSecond > 0 ? upload->bytesPerSecond * 0.7 + rate * 0.3 : rate;+ upload->lastChunk = now;+ }+ }+ j.set("received", double(upload->received));+ return jsonOk(j);+}++HttpResponse PhoneLink::fileStatus(const std::string& fileId) {+ std::lock_guard lock(mutex_);+ const auto it = uploads_.find(fileId);+ if (it == uploads_.end()) return jsonError(404, "Unknown upload");+ Json j = Json::object();+ j.set("received", double(it->second->received));+ j.set("size", double(it->second->size));+ return jsonOk(j);+}++HttpResponse PhoneLink::finishCapture(const std::string& captureId) {+ std::shared_ptr<Capture> capture;+ {+ std::lock_guard lock(mutex_);+ const auto it = captures_.find(captureId);+ if (it == captures_.end()) return jsonError(404, "Unknown capture");+ capture = it->second;+ if (capture->finished) {+ Json j = Json::object();+ j.set("folder", capture->folder);+ return jsonOk(j);+ }+ if (capture->files.empty()) return jsonError(400, "Nothing was uploaded");+ Json missing = Json::array();+ for (const auto& f : capture->files)+ if (f->received != f->size) missing.push(f->name);+ if (missing.size()) {+ Json j = Json::object();+ j.set("error", "Some files are incomplete");+ j.set("missing", missing);+ return HttpResponse::json(409, j.dump());+ }+ capture->finished = true; // (no more chunks are accepted from here on)+ for (const auto& f : capture->files) f->finished = true;+ }+ // Move the files into their final folder.+ std::error_code ec;+ const fs::path root = u8path(options_.inboxRoot);+ const std::string base = captureTimestamp(capture->started) + "-" + captureKindName(capture->kind);+ fs::path folder = root / base;+ for (int n = 2; fs::exists(folder, ec); ++n) folder = root / (base + "-" + std::to_string(n));+ fs::create_directories(folder, ec);+ std::vector<std::string> paths;+ Json files = Json::array();+ bool ok = !ec;+ for (const auto& f : capture->files) {+ const fs::path to = folder / u8path(f->name);+ if (ok) fs::rename(u8path(f->path), to, ec);+ if (ec) ok = false;+ paths.push_back(pathString(to));+ Json entry = Json::object();+ entry.set("name", f->name);+ entry.set("size", double(f->size));+ files.push(entry);+ }+ if (!ok) {+ std::lock_guard lock(mutex_);+ capture->finished = false;+ for (const auto& f : capture->files) f->finished = false;+ pushEvent(PhoneEvent::make(PhoneEvent::Type::Error, "Couldn't move the capture into " + pathString(folder) + ": " + ec.message()));+ return jsonError(500, "Your computer couldn't store the capture (" + ec.message() + ").");+ }+ fs::remove_all(u8path(capture->dir), ec);+ Json info = Json::object();+ info.set("kind", captureKindName(capture->kind));+ info.set("device", capture->device);+ info.set("files", files);+ info.set("started", isoTime(capture->started));+ info.set("finished", isoTime(std::chrono::system_clock::now()));+ info.set("source", "SculptForge phone link");+ info.set("app", "SculptForge " + options_.version);+ {+ std::ofstream out(folder / "capture.json", std::ios::binary);+ out << info.dump(true) << '\n';+ }+ const std::string folderText = pathString(folder);+ {+ std::lock_guard lock(mutex_);+ capture->folder = folderText;+ ++capturesReceived_;+ PhoneEvent e = PhoneEvent::make(PhoneEvent::Type::CaptureReady);+ e.kind = capture->kind;+ e.folder = folderText;+ e.files = paths;+ e.device = capture->device;+ pushEvent(std::move(e));+ }+ Json j = Json::object();+ j.set("folder", folderText);+ j.set("name", pathString(folder.filename()));+ return jsonOk(j);+}++HttpResponse PhoneLink::cancelCapture(const std::string& captureId) {+ std::lock_guard lock(mutex_);+ const auto it = captures_.find(captureId);+ if (it == captures_.end()) return jsonError(404, "Unknown capture");+ if (!it->second->finished) {+ std::error_code ec;+ fs::remove_all(u8path(it->second->dir), ec);+ for (const auto& f : it->second->files) uploads_.erase(f->id);+ }+ captures_.erase(it);+ return jsonOk(Json::object());+}++// --- AR ---++HttpResponse PhoneLink::requestAr(const HttpRequest& request) {+ Json body;+ std::string err;+ Json::parse(request.body.empty() ? std::string("{}") : request.body, body, err);+ PhoneEvent e = PhoneEvent::make(PhoneEvent::Type::ArRequested);+ e.wholeScene = body["scope"].asString() == "scene";+ e.sizeCm = std::clamp(float(body["size"].asNumber(20.0)), 2.0f, 200.0f);+ std::lock_guard lock(mutex_);+ arViewed_ = Clock::now();+ arViewedEver_ = true;+ pushEvent(e);+ Json j = Json::object();+ j.set("version", double(arVersion_));+ return jsonOk(j);+}++HttpResponse PhoneLink::serveArModel(bool usdz) {+ std::lock_guard lock(mutex_);+ arViewed_ = Clock::now();+ arViewedEver_ = true;+ const std::string& data = usdz ? arUsdz_ : arGlb_;+ if (data.empty()) return jsonError(503, arBusy_ ? "The model is still being prepared" : "No model yet");+ HttpResponse r = HttpResponse::bytes(usdz ? "model/vnd.usdz+zip" : "model/gltf-binary", data);+ r.headers.emplace_back("Content-Disposition", usdz ? "inline; filename=\"SculptForge.usdz\"" : "inline; filename=\"SculptForge.glb\"");+ r.headers.emplace_back("Access-Control-Allow-Origin", "*"); // Scene Viewer and model viewers fetch it themselves+ return r;+}++void PhoneLink::setArBusy(bool busy) {+ std::lock_guard lock(mutex_);+ arBusy_ = busy;+}++void PhoneLink::publishArModel(std::string glb, std::string usdz, std::string summary) {+ std::lock_guard lock(mutex_);+ if (glb.size() > kMaxArModelBytes || usdz.size() > kMaxArModelBytes) {+ arError_ = "The model is too big for a phone";+ arBusy_ = false;+ return;+ }+ arGlb_ = std::move(glb);+ arUsdz_ = std::move(usdz);+ arSummary_ = std::move(summary);+ arError_.clear();+ arBusy_ = false;+ ++arVersion_;+}++void PhoneLink::setArError(std::string error) {+ std::lock_guard lock(mutex_);+ arError_ = std::move(error);+ arBusy_ = false;+}++double PhoneLink::secondsSinceArViewed() const {+ std::lock_guard lock(mutex_);+ return arViewedEver_ ? std::chrono::duration<double>(Clock::now() - arViewed_).count() : -1.0;+}++// --- Face ---++bool PhoneLink::receiveFaceMessage(const std::string& text, const std::string& transport, std::string& error) {+ if (text.find("\"hello\"") != std::string::npos || text.find("\"bye\"") != std::string::npos) {+ Json msg;+ std::string err;+ if (Json::parse(text, msg, err) && (msg["type"].asString() == "hello" || msg["type"].asString() == "bye")) {+ std::lock_guard lock(mutex_);+ if (msg["type"].asString() == "bye") {+ if (faceConnected_) pushEvent(PhoneEvent::make(PhoneEvent::Type::FaceDisconnected, "The phone stopped the face feed"));+ faceConnected_ = false;+ }+ return true;+ }+ }+ FaceFrame frame;+ if (!parseFaceMessage(text, frame, error)) return false;+ const auto now = Clock::now();+ std::lock_guard lock(mutex_);+ face_ = frame;+ ++faceSerial_;+ lastFace_ = now;+ faceTimes_.push_back(now);+ while (!faceTimes_.empty() && now - faceTimes_.front() > std::chrono::seconds(1)) faceTimes_.pop_front();+ if (!faceConnected_ || faceTransport_ != transport) {+ faceConnected_ = true;+ faceTransport_ = transport;+ pushEvent(PhoneEvent::make(PhoneEvent::Type::FaceConnected, transport));+ }+ if (take_.isOpen()) take_.write(frame);+ return true;+}++bool PhoneLink::latestFace(FaceFrame& frame, uint64_t& serial) const {+ std::lock_guard lock(mutex_);+ if (faceSerial_ == 0 || faceSerial_ == serial) return false;+ frame = face_;+ serial = faceSerial_;+ return true;+}++bool PhoneLink::startTake(std::string& folder, std::string& error) {+ std::lock_guard lock(mutex_);+ if (take_.isOpen()) {+ folder = takeFolder_;+ return true;+ }+ takeStarted_ = std::chrono::system_clock::now();+ const fs::path root = u8path(options_.inboxRoot);+ const std::string base = captureTimestamp(takeStarted_) + "-face";+ std::error_code ec;+ fs::path dir = root / base;+ for (int n = 2; fs::exists(dir, ec); ++n) dir = root / (base + "-" + std::to_string(n));+ fs::create_directories(dir, ec);+ if (ec) {+ error = "Cannot create " + pathString(dir) + ": " + ec.message();+ return false;+ }+ if (!take_.open(pathString(dir / "take.jsonl"), error)) return false;+ takeFolder_ = folder = pathString(dir);+ return true;+}++std::string PhoneLink::stopTake(std::string* summary) {+ std::lock_guard lock(mutex_);+ if (!take_.isOpen()) return {};+ const int frames = take_.frames();+ const double seconds = take_.durationMs() / 1000.0;+ take_.close();+ Json info = Json::object();+ info.set("kind", "face");+ info.set("device", device_);+ Json files = Json::array();+ Json entry = Json::object();+ entry.set("name", "take.jsonl");+ entry.set("frames", double(frames));+ files.push(entry);+ info.set("files", files);+ info.set("frames", double(frames));+ info.set("seconds", seconds);+ info.set("format", "JSON Lines: one face message per line, {\"type\":\"face\",\"t\":ms,\"weights\":{ARKit name: 0..1},\"head\":[qx,qy,qz,qw]}");+ info.set("started", isoTime(takeStarted_));+ info.set("finished", isoTime(std::chrono::system_clock::now()));+ info.set("source", "SculptForge phone link");+ info.set("app", "SculptForge " + options_.version);+ {+ std::ofstream out(u8path(takeFolder_) / "capture.json", std::ios::binary);+ out << info.dump(true) << '\n';+ }+ if (summary) {+ char buf[64];+ std::snprintf(buf, sizeof buf, "%d frames, %.1f s", frames, seconds);+ *summary = buf;+ }+ std::string folder = takeFolder_;+ takeFolder_.clear();+ return folder;+}++void PhoneLink::onFaceState(int state) {+ const auto s = FaceRtc::State(state);+ std::lock_guard lock(mutex_);+ if (s == FaceRtc::State::Connected) {+ if (!faceConnected_ || faceTransport_ != "WebRTC") pushEvent(PhoneEvent::make(PhoneEvent::Type::FaceConnected, "WebRTC"));+ faceConnected_ = true;+ faceTransport_ = "WebRTC";+ } else if ((s == FaceRtc::State::Closed || s == FaceRtc::State::Failed) && faceTransport_ == "WebRTC") {+ if (faceConnected_)+ pushEvent(PhoneEvent::make(PhoneEvent::Type::FaceDisconnected, s == FaceRtc::State::Failed ? "The face link failed (network)" : "The phone closed the face link"));+ faceConnected_ = false;+ }+}++bool PhoneLink::createFaceLink(std::string& faceUrl, std::string& error) {+ if (!webrtcAvailable()) {+ error = "This SculptForge was built without WebRTC, so the live face feed is not available.";+ return false;+ }+ std::lock_guard linkLock(faceLinkMutex_); // one new face link at a time+ stopRelay(); // (outside rtcMutex_: the relay thread takes it to answer)+ std::lock_guard rtcLock(rtcMutex_);+ if (rtc_) rtc_->close();+ rtc_ = std::make_unique<FaceRtc>();+ rtc_->onMessage = [this](const std::string& text) {+ std::string err;+ receiveFaceMessage(text, "WebRTC", err);+ };+ rtc_->onState = [this](FaceRtc::State s) { onFaceState(int(s)); };+ std::string sdp;+ if (!rtc_->createOffer(sdp, error)) {+ rtc_.reset();+ return false;+ }+ const std::string topic = "sculptforge-" + hex(randomBytes(12));+ const std::string key = randomBytes(32);+ std::string desktop, token;+ {+ std::lock_guard lock(mutex_);+ desktop = "http://" + address_ + ":" + std::to_string(port());+ token = token_;+ relayTopic_ = topic;+ relayKey_ = key;+ }+ const bool relay = options_.relayUrl != "off";+ // Everything after '#' stays on the phone: the website never sees the offer, the token or the key.+ faceUrl = options_.siteUrl + "#v=1&k=" + token + "&d=" + percentEncode(desktop) + "&o=" + base64UrlEncode(sdp) ++ "&n=" + percentEncode(options_.desktopName) + "&x=" + base64UrlEncode(key) ++ (relay ? "&r=" + percentEncode(options_.relayUrl + topic) : std::string());+ if (relay) {+ const int64_t since = int64_t(std::chrono::duration_cast<std::chrono::seconds>(+ std::chrono::system_clock::now().time_since_epoch()).count()) - 5;+ relayStop_ = false;+ relayThread_ = std::thread([this, topic, key, since] { relayLoop(topic, key, since); });+ }+ return true;+}++HttpResponse PhoneLink::faceLink() {+ std::string url, error;+ if (!createFaceLink(url, error)) return jsonError(webrtcAvailable() ? 500 : 501, error);+ Json j = Json::object();+ j.set("url", url);+ return jsonOk(j);+}++bool PhoneLink::acceptFaceAnswer(const std::string& sdp, std::string& error) {+ std::lock_guard rtcLock(rtcMutex_);+ if (!rtc_) {+ error = "No face link is waiting (open Face on the phone again)";+ return false;+ }+ if (sdp.find("v=0") == std::string::npos || sdp.size() > 16 * 1024) {+ error = "Not a WebRTC answer";+ return false;+ }+ return rtc_->setAnswer(sdp, error);+}++bool PhoneLink::acceptRelayMessage(const std::string& packed, std::string& error) {+ std::string key, token;+ {+ std::lock_guard lock(mutex_);+ key = relayKey_;+ token = token_;+ }+ std::string plain;+ if (!aesGcmDecrypt(key, packed, plain, error)) return false;+ Json msg;+ if (!Json::parse(plain, msg, error)) return false;+ if (!sameSecret(msg["k"].asString(), token)) {+ error = "The answer is for another session";+ return false;+ }+ return acceptFaceAnswer(msg["sdp"].asString(), error);+}++HttpResponse PhoneLink::directAnswer(const HttpRequest& request) {+ std::string sdp, error;+ const bool ok = base64UrlDecode(request.param("a"), sdp) && acceptFaceAnswer(sdp, error);+ std::string page =+ "<!doctype html><meta charset=utf-8><meta name=viewport content=\"width=device-width,initial-scale=1\">"+ "<title>SculptForge</title><body style=\"background:#14161a;color:#e8e8ea;font:17px -apple-system,system-ui,sans-serif;"+ "display:flex;align-items:center;justify-content:center;height:90vh;text-align:center\"><div>";+ page += ok ? "<h2>Connected</h2><p>Go back to the previous tab: your face drives SculptForge now.</p>"+ : "<h2>Not connected</h2><p>" + error + "</p>";+ page += "</div><script>setTimeout(function(){window.close()},700)</script></body>";+ return HttpResponse::html(page);+}++void PhoneLink::relayLoop(std::string topic, std::string key, int64_t sinceUnix) {+ const auto started = Clock::now();+ std::string seen;+ while (!relayStop_ && Clock::now() - started < std::chrono::minutes(5)) {+ const ai::HttpResponse r = ai::httpRequest(+ "GET", options_.relayUrl + topic + "/json?poll=1&since=" + std::to_string(sinceUnix), {}, {}, 10);+ if (relayStop_) return;+ if (r.status == 200) {+ size_t pos = 0;+ while (pos < r.body.size()) {+ size_t end = r.body.find('\n', pos);+ if (end == std::string::npos) end = r.body.size();+ Json event;+ std::string err;+ if (Json::parse(r.body.substr(pos, end - pos), event, err) && event["event"].asString() == "message") {+ const std::string id = event["id"].asString();+ if (seen.find("|" + id + "|") == std::string::npos) {+ seen += "|" + id + "|";+ if (acceptRelayMessage(event["message"].asString(), err)) return;+ }+ }+ pos = end + 1;+ }+ }+ for (int i = 0; i < 15 && !relayStop_; ++i) std::this_thread::sleep_for(std::chrono::milliseconds(100));+ }+}++void PhoneLink::stopRelay() {+ relayStop_ = true;+ if (relayThread_.joinable()) relayThread_.join();+ relayStop_ = false;+}++} // namespace sf::linkdiff --git a/src/link/PhoneLink.h b/src/link/PhoneLink.hnew file mode 100644index 00000000..287d6df8--- /dev/null+++ b/src/link/PhoneLink.h@@ -0,0 +1,262 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// The phone link (File > Capture from phone; docs/phone-link.md): while its window is open, a small HTTP server+// on the local network serves the capture page to a phone that scanned the window's QR code, and takes:+// - captures: a scan video or photo burst (for splat training), one photo (for image to 3D) or a splat .ply from+// a scanning app, uploaded in resumable chunks into the Captures inbox,+// <user library>/Captures/<yyyy-mm-dd_hhmmss>-<kind>/ (kind = splat | photo | ply)+// with a capture.json (kind, device, files, times); the app then runs the command for that kind;+// - AR requests: the active SubTool (or the scene), decimated and coloured, as USDZ (iPhone AR Quick Look) and+// GLB (Android Scene Viewer), re-exported after strokes while the phone is looking;+// - the live face feed: link/FaceFeed.h messages over WebRTC (link/PhoneRtc.h) or HTTP, and face takes.+// Security: everything but the page itself needs the session token (in the QR code's URL; the page also pairs+// with a 6-digit code typed on the phone). The token changes every time the window opens and expires if no+// phone uses it within 10 minutes. Only local-network clients are answered, uploads have size limits, file names+// are reduced to safe characters and written only inside the session's own capture folders. Nothing listens+// while the window is closed.+#pragma once++#include "link/FaceFeed.h"+#include "link/HttpServer.h"++#include <atomic>+#include <chrono>+#include <cstdint>+#include <deque>+#include <map>+#include <memory>+#include <mutex>+#include <string>+#include <thread>+#include <vector>++namespace sf::link {++class FaceRtc;++// Where the face page lives (HTTPS, needed for the camera): our website. One constant; SCULPTFORGE_LINK_URL+// overrides it (e.g. a local HTTPS test server).+inline constexpr const char* kPhoneLinkSiteUrl = "https://islandmed.github.io/SculptForge/link/";+// The relay that carries the phone's (encrypted) WebRTC answer when the desktop isn't reachable from the HTTPS+// page; SCULPTFORGE_LINK_RELAY overrides it.+inline constexpr const char* kPhoneLinkRelayUrl = "https://ntfy.sh/";+inline constexpr uint16_t kPhoneLinkPort = 8765;++inline constexpr uint64_t kMaxCaptureFileBytes = 4ull << 30; // one video or .ply+inline constexpr uint64_t kMaxCaptureBytes = 8ull << 30; // all files of one capture+inline constexpr size_t kMaxCaptureFiles = 500; // a photo burst+inline constexpr size_t kUploadChunkBytes = 4u << 20; // what the page sends per request+inline constexpr size_t kMaxUploadChunkBytes = 16u << 20;+inline constexpr size_t kMaxArModelBytes = 64u << 20;++struct PhoneLinkOptions {+ std::string inboxRoot; // the Captures folder (created on first use)+ uint16_t port = kPhoneLinkPort;+ bool localNetworkOnly = true;+ std::string desktopName; // shown on the phone ("Tony's PC")+ std::string version; // SculptForge version, shown on the phone+ std::string siteUrl; // face page; empty = kPhoneLinkSiteUrl (or SCULPTFORGE_LINK_URL)+ std::string relayUrl; // empty = kPhoneLinkRelayUrl (or SCULPTFORGE_LINK_RELAY); "off" = none+ bool startServer = true; // false: handle() only (unit tests)+};++enum class CaptureKind { Splat, Photo, Ply, Face };+const char* captureKindName(CaptureKind kind);+bool parseCaptureKind(const std::string& text, CaptureKind& kind);++// A safe file name for an upload: letters, digits, '.', '-', '_' (others become '_'), no leading dot, at most+// 80 characters, keeping the extension. Empty when nothing usable is left.+std::string sanitizeUploadName(const std::string& name);+// Whether an upload of `kind` may have this (lower-case) extension.+bool captureExtensionAllowed(CaptureKind kind, const std::string& extension);+// "iPhone · Safari" from a User-Agent.+std::string describeDevice(const std::string& userAgent);+// "2026-10-26_195803" for local time `t`.+std::string captureTimestamp(std::chrono::system_clock::time_point t);++struct PhoneEvent {+ enum class Type;+ static PhoneEvent make(Type type, std::string text = {}) {+ PhoneEvent e;+ e.type = type;+ e.text = std::move(text);+ return e;+ }+ enum class Type {+ DeviceConnected, // a phone opened the page (text: device)+ CaptureReady, // an upload finished: kind, folder, files+ ArRequested, // the phone asked for an AR model (scope, sizeCm)+ FaceConnected, // the face feed started (text: how)+ FaceDisconnected,+ Error, // something went wrong (text, in plain words)+ };+ Type type = Type::Error;+ std::string text;+ CaptureKind kind = CaptureKind::Splat;+ std::string folder;+ std::vector<std::string> files; // full paths+ std::string device;+ bool wholeScene = false;+ float sizeCm = 20.0f;+};++struct PhoneUploadInfo {+ std::string captureId, name;+ CaptureKind kind = CaptureKind::Splat;+ uint64_t size = 0, received = 0;+ double bytesPerSecond = 0.0;+ bool finished = false;+};++struct PhoneLinkStatus {+ bool running = false;+ std::string url; // what the QR code holds+ std::string address; // ip:port+ std::string code; // 6-digit pairing code+ double tokenExpiresIn = 0; // seconds until an unused token expires (0 once a phone used it)+ bool paired = false; // a phone used the token+ std::string device; // the last phone seen ("iPhone · Safari")+ std::string deviceAddress;+ double secondsSinceSeen = -1; // -1 = never+ std::vector<PhoneUploadInfo> uploads; // in progress and recently finished+ int capturesReceived = 0;+ // AR+ uint64_t arVersion = 0; // models published so far+ bool arBusy = false;+ std::string arSummary, arError;+ double secondsSinceArViewed = -1;+ // Face+ bool faceConnected = false;+ std::string faceTransport; // "WebRTC", "Wi-Fi (HTTP)"+ std::string faceState;+ double faceFps = 0;+ uint64_t faceFrames = 0;+ double secondsSinceFace = -1;+ bool recording = false;+ int takeFrames = 0;+ double takeSeconds = 0;+ std::string lastError;+};++class PhoneLink {+public:+ PhoneLink();+ ~PhoneLink();+ PhoneLink(const PhoneLink&) = delete;+ PhoneLink& operator=(const PhoneLink&) = delete;++ // Starts a session: new token and code, and (unless options.startServer is false) the server.+ bool start(const PhoneLinkOptions& options, std::string& error);+ // Stops listening, closes the face link, drops unfinished uploads.+ void stop();+ bool running() const { return running_; }++ // Local addresses the QR code can use (default route first) and the one it uses.+ std::vector<std::string> addresses() const;+ void setAddress(const std::string& address);+ std::string url() const;+ PhoneLinkStatus status() const;+ const std::string& token() const { return token_; }+ std::string code() const;+ uint16_t port() const;+ std::string inboxRoot() const { return options_.inboxRoot; }++ // Events for the main thread, oldest first.+ std::vector<PhoneEvent> takeEvents();++ // The newest face frame (false when there is none newer than `serial`; updates `serial`).+ bool latestFace(FaceFrame& frame, uint64_t& serial) const;+ // Records every face frame into <inbox>/<stamp>-face/take.jsonl (with a capture.json at stop).+ bool startTake(std::string& folder, std::string& error);+ // Stops recording; the take's folder, or "" when nothing was recording.+ std::string stopTake(std::string* summary = nullptr);++ // AR: the app publishes models (GLB for Android, USDZ for iOS) for the phone to fetch.+ void setArBusy(bool busy);+ void publishArModel(std::string glb, std::string usdz, std::string summary);+ void setArError(std::string error);+ // Seconds since the phone last fetched or asked for an AR model (-1 = never): while it is looking, edits are+ // re-exported.+ double secondsSinceArViewed() const;++ // One request, as the server hands it over (public for tests: no sockets needed).+ HttpResponse handle(const HttpRequest& request);+ // A face message from any transport (public for tests).+ bool receiveFaceMessage(const std::string& text, const std::string& transport, std::string& error);+ // Applies a WebRTC answer for the pending face link (the relay and the direct /rtc-answer path use it).+ bool acceptFaceAnswer(const std::string& sdp, std::string& error);+ // The face page URL with the pending offer (creates one); for tests too.+ bool createFaceLink(std::string& faceUrl, std::string& error);+ // Relay message handling, exposed for tests: decrypts and applies an answer posted to the relay.+ bool acceptRelayMessage(const std::string& packed, std::string& error);++private:+ struct Upload;+ struct Capture;+ using Clock = std::chrono::steady_clock;++ HttpResponse route(const HttpRequest& request);+ bool authorised(const HttpRequest& request);+ void noteDevice(const HttpRequest& request);+ void pushEvent(PhoneEvent event);+ void newToken();+ std::string statusJson();+ HttpResponse startCapture(const HttpRequest& request);+ HttpResponse addFile(const std::string& captureId, const HttpRequest& request);+ HttpResponse putChunk(const std::string& fileId, const HttpRequest& request);+ HttpResponse fileStatus(const std::string& fileId);+ HttpResponse finishCapture(const std::string& captureId);+ HttpResponse cancelCapture(const std::string& captureId);+ HttpResponse pair(const HttpRequest& request);+ HttpResponse requestAr(const HttpRequest& request);+ HttpResponse serveArModel(bool usdz);+ HttpResponse faceLink();+ HttpResponse directAnswer(const HttpRequest& request);+ void relayLoop(std::string topic, std::string key, int64_t sinceUnix);+ void stopRelay();+ void onFaceState(int state);++ PhoneLinkOptions options_;+ std::atomic<bool> running_{false};+ HttpServer server_;+ mutable std::mutex mutex_;+ std::string token_, code_, address_;+ std::vector<std::string> addresses_;+ Clock::time_point tokenIssued_;+ bool paired_ = false;+ int failedPairs_ = 0;+ Clock::time_point lastPairAttempt_{};+ std::string device_, deviceAddress_;+ Clock::time_point lastSeen_{};+ bool seen_ = false;+ std::map<std::string, std::shared_ptr<Capture>> captures_;+ std::map<std::string, std::shared_ptr<Upload>> uploads_;+ int capturesReceived_ = 0;+ std::deque<PhoneEvent> events_;+ std::string lastError_;+ // AR+ std::string arGlb_, arUsdz_, arSummary_, arError_;+ uint64_t arVersion_ = 0;+ bool arBusy_ = false;+ Clock::time_point arViewed_{};+ bool arViewedEver_ = false;+ // Face+ std::unique_ptr<FaceRtc> rtc_;+ std::mutex faceLinkMutex_; // createFaceLink+ std::mutex rtcMutex_; // rtc_: creating / answering the peer connection+ std::string faceTransport_;+ bool faceConnected_ = false;+ FaceFrame face_;+ uint64_t faceSerial_ = 0;+ Clock::time_point lastFace_{};+ std::deque<Clock::time_point> faceTimes_; // the last second's frames, for the rate+ FaceTakeWriter take_;+ std::string takeFolder_;+ std::chrono::system_clock::time_point takeStarted_{};+ std::string relayTopic_, relayKey_;+ std::thread relayThread_;+ std::atomic<bool> relayStop_{false};+};++} // namespace sf::linkdiff --git a/src/link/PhoneRtc.cpp b/src/link/PhoneRtc.cppnew file mode 100644index 00000000..cf20814b--- /dev/null+++ b/src/link/PhoneRtc.cpp@@ -0,0 +1,390 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+#include "link/PhoneRtc.h"++#include <condition_variable>+#include <future>+#include <mutex>+#include <random>++#if SF_HAVE_WEBRTC+#include <mbedtls/gcm.h>+#include <rtc/rtc.hpp>+#endif++namespace sf::link {++// --- Encoding and randomness (always available) ---++std::string base64UrlEncode(const std::string& bytes) {+ static const char* table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";+ std::string out;+ out.reserve((bytes.size() + 2) / 3 * 4);+ size_t i = 0;+ for (; i + 2 < bytes.size(); i += 3) {+ const uint32_t v = uint32_t(uint8_t(bytes[i])) << 16 | uint32_t(uint8_t(bytes[i + 1])) << 8 | uint8_t(bytes[i + 2]);+ out += table[v >> 18];+ out += table[(v >> 12) & 63];+ out += table[(v >> 6) & 63];+ out += table[v & 63];+ }+ if (i + 1 == bytes.size()) {+ const uint32_t v = uint32_t(uint8_t(bytes[i])) << 16;+ out += table[v >> 18];+ out += table[(v >> 12) & 63];+ } else if (i + 2 == bytes.size()) {+ const uint32_t v = uint32_t(uint8_t(bytes[i])) << 16 | uint32_t(uint8_t(bytes[i + 1])) << 8;+ out += table[v >> 18];+ out += table[(v >> 12) & 63];+ out += table[(v >> 6) & 63];+ }+ return out;+}++bool base64UrlDecode(const std::string& text, std::string& bytes) {+ bytes.clear();+ uint32_t acc = 0;+ int bits = 0;+ for (char c : text) {+ int v;+ if (c >= 'A' && c <= 'Z') v = c - 'A';+ else if (c >= 'a' && c <= 'z') v = c - 'a' + 26;+ else if (c >= '0' && c <= '9') v = c - '0' + 52;+ else if (c == '-' || c == '+') v = 62;+ else if (c == '_' || c == '/') v = 63;+ else if (c == '=') break;+ else return false;+ acc = (acc << 6) | uint32_t(v);+ bits += 6;+ if (bits >= 8) {+ bits -= 8;+ bytes += char((acc >> bits) & 0xFF);+ }+ }+ return true;+}++std::string randomBytes(size_t n) {+ static std::mutex mutex;+ static std::random_device device;+ std::lock_guard lock(mutex);+ std::string out(n, '\0');+ for (size_t i = 0; i < n; i += 4) {+ const uint32_t v = device();+ for (size_t j = 0; j < 4 && i + j < n; ++j) out[i + j] = char((v >> (8 * j)) & 0xFF);+ }+ return out;+}++const char* faceRtcStateName(FaceRtc::State state) {+ switch (state) {+ case FaceRtc::State::Idle: return "idle";+ case FaceRtc::State::Offered: return "waiting for the phone";+ case FaceRtc::State::Connecting: return "connecting";+ case FaceRtc::State::Connected: return "connected";+ case FaceRtc::State::Closed: return "closed";+ case FaceRtc::State::Failed: return "failed";+ }+ return "?";+}++#if SF_HAVE_WEBRTC++bool webrtcAvailable() { return true; }++namespace {++bool gcm(bool encrypt, const std::string& key, const std::string& iv, const std::string& in, std::string& out,+ std::string& tag, std::string& error) {+ if (key.size() != 32) {+ error = "The key must be 32 bytes";+ return false;+ }+ mbedtls_gcm_context ctx;+ mbedtls_gcm_init(&ctx);+ int rc = mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, reinterpret_cast<const unsigned char*>(key.data()), 256);+ out.assign(in.size(), '\0');+ if (rc == 0) {+ if (encrypt) {+ tag.assign(16, '\0');+ rc = mbedtls_gcm_crypt_and_tag(&ctx, MBEDTLS_GCM_ENCRYPT, in.size(),+ reinterpret_cast<const unsigned char*>(iv.data()), iv.size(), nullptr, 0,+ reinterpret_cast<const unsigned char*>(in.data()),+ reinterpret_cast<unsigned char*>(out.data()), 16,+ reinterpret_cast<unsigned char*>(tag.data()));+ } else {+ rc = mbedtls_gcm_auth_decrypt(&ctx, in.size(), reinterpret_cast<const unsigned char*>(iv.data()), iv.size(),+ nullptr, 0, reinterpret_cast<const unsigned char*>(tag.data()), tag.size(),+ reinterpret_cast<const unsigned char*>(in.data()),+ reinterpret_cast<unsigned char*>(out.data()));+ }+ }+ mbedtls_gcm_free(&ctx);+ if (rc != 0) {+ error = encrypt ? "Encryption failed" : "The message doesn't decrypt with this session's key";+ out.clear();+ return false;+ }+ return true;+}++} // namespace++bool aesGcmEncrypt(const std::string& key, const std::string& plain, std::string& packed, std::string& error) {+ const std::string iv = randomBytes(12);+ std::string cipher, tag;+ if (!gcm(true, key, iv, plain, cipher, tag, error)) return false;+ packed = base64UrlEncode(iv + cipher + tag);+ return true;+}++bool aesGcmDecrypt(const std::string& key, const std::string& packed, std::string& plain, std::string& error) {+ std::string bytes;+ if (!base64UrlDecode(packed, bytes) || bytes.size() < 12 + 16) {+ error = "Not an encrypted message";+ return false;+ }+ std::string tag = bytes.substr(bytes.size() - 16);+ return gcm(false, key, bytes.substr(0, 12), bytes.substr(12, bytes.size() - 28), plain, tag, error);+}++struct FaceRtc::Impl {+ std::mutex mutex;+ std::condition_variable gathered;+ bool gatheringDone = false;+ State state = State::Idle;+ std::shared_ptr<rtc::PeerConnection> pc;+ std::shared_ptr<rtc::DataChannel> channel;+ // The test peer.+ std::shared_ptr<rtc::PeerConnection> testPc;+ std::shared_ptr<rtc::DataChannel> testChannel;+};++namespace {++rtc::DataChannelInit faceChannelInit() {+ rtc::DataChannelInit init;+ init.negotiated = true; // both sides open channel 0 themselves: no in-band announcement to lose+ init.id = 0;+ init.reliability.unordered = true;+ init.reliability.maxRetransmits = 0;+ return init;+}++rtc::Configuration localConfiguration() {+ return rtc::Configuration(); // no STUN or TURN servers: the phone and this computer share a network+}++} // namespace++FaceRtc::FaceRtc() : impl_(std::make_unique<Impl>()) {+ static std::once_flag logging;+ std::call_once(logging, [] { rtc::InitLogger(rtc::LogLevel::None); });+}++FaceRtc::~FaceRtc() { close(); }++FaceRtc::State FaceRtc::state() const {+ std::lock_guard lock(impl_->mutex);+ return impl_->state;+}++bool FaceRtc::createOffer(std::string& sdp, std::string& error, int timeoutMs) {+ close();+ try {+ auto pc = std::make_shared<rtc::PeerConnection>(localConfiguration());+ Impl* impl = impl_.get();+ auto setState = [this, impl](State s) {+ {+ std::lock_guard lock(impl->mutex);+ if (impl->state == s) return;+ impl->state = s;+ }+ if (onState) onState(s);+ };+ pc->onGatheringStateChange([impl](rtc::PeerConnection::GatheringState s) {+ if (s != rtc::PeerConnection::GatheringState::Complete) return;+ std::lock_guard lock(impl->mutex);+ impl->gatheringDone = true;+ impl->gathered.notify_all();+ });+ pc->onStateChange([setState](rtc::PeerConnection::State s) {+ using S = rtc::PeerConnection::State;+ if (s == S::Connecting) setState(State::Connecting);+ else if (s == S::Failed) setState(State::Failed);+ else if (s == S::Disconnected || s == S::Closed) setState(State::Closed);+ });+ auto channel = pc->createDataChannel("face", faceChannelInit());+ channel->onOpen([setState] { setState(State::Connected); });+ channel->onClosed([setState] { setState(State::Closed); });+ channel->onMessage([this](rtc::message_variant m) {+ if (const auto* text = std::get_if<std::string>(&m); text && onMessage) onMessage(*text);+ else if (const auto* binary = std::get_if<rtc::binary>(&m); binary && onMessage)+ onMessage(std::string(reinterpret_cast<const char*>(binary->data()), binary->size()));+ });+ {+ std::lock_guard lock(impl->mutex);+ impl->pc = pc;+ impl->channel = channel;+ impl->gatheringDone = false;+ }+ pc->setLocalDescription(rtc::Description::Type::Offer);+ std::unique_lock lock(impl->mutex);+ impl->gathered.wait_for(lock, std::chrono::milliseconds(timeoutMs), [impl] { return impl->gatheringDone; });+ const auto description = pc->localDescription();+ if (!description) {+ error = "WebRTC made no offer";+ return false;+ }+ sdp = std::string(*description);+ if (sdp.find("a=candidate:") == std::string::npos) {+ error = "This computer has no network address the phone could reach";+ return false;+ }+ impl->state = State::Offered;+ return true;+ } catch (const std::exception& e) {+ error = std::string("WebRTC: ") + e.what();+ return false;+ }+}++bool FaceRtc::setAnswer(const std::string& sdp, std::string& error) {+ std::shared_ptr<rtc::PeerConnection> pc;+ {+ std::lock_guard lock(impl_->mutex);+ pc = impl_->pc;+ if (!pc || impl_->state != State::Offered) {+ error = pc ? "This face link is already answered" : "No face link is waiting for an answer";+ return false;+ }+ impl_->state = State::Connecting;+ }+ try {+ pc->setRemoteDescription(rtc::Description(sdp, rtc::Description::Type::Answer));+ return true;+ } catch (const std::exception& e) {+ std::lock_guard lock(impl_->mutex);+ impl_->state = State::Offered; // a bad answer doesn't spoil the offer+ error = std::string("The phone's answer is not valid: ") + e.what();+ return false;+ }+}++bool FaceRtc::send(const std::string& text) {+ std::shared_ptr<rtc::DataChannel> channel;+ {+ std::lock_guard lock(impl_->mutex);+ channel = impl_->channel;+ }+ try {+ return channel && channel->isOpen() && channel->send(text);+ } catch (const std::exception&) {+ return false;+ }+}++void FaceRtc::close() {+ std::shared_ptr<rtc::PeerConnection> pc, testPc;+ std::shared_ptr<rtc::DataChannel> channel, testChannel;+ {+ std::lock_guard lock(impl_->mutex);+ pc = std::move(impl_->pc);+ channel = std::move(impl_->channel);+ testPc = std::move(impl_->testPc);+ testChannel = std::move(impl_->testChannel);+ impl_->state = State::Idle;+ }+ try {+ if (channel) {+ channel->onMessage(nullptr);+ channel->onOpen(nullptr);+ channel->onClosed(nullptr);+ }+ if (pc) {+ pc->onStateChange(nullptr);+ pc->onGatheringStateChange(nullptr);+ pc->close();+ }+ if (testPc) testPc->close();+ } catch (const std::exception&) {+ }+}++bool FaceRtc::answerForTest(const std::string& offerSdp, std::string& answerSdp, std::string& error, int timeoutMs) {+ try {+ auto pc = std::make_shared<rtc::PeerConnection>(localConfiguration());+ auto done = std::make_shared<std::promise<void>>();+ auto once = std::make_shared<std::once_flag>();+ pc->onGatheringStateChange([done, once](rtc::PeerConnection::GatheringState s) {+ if (s == rtc::PeerConnection::GatheringState::Complete) std::call_once(*once, [&] { done->set_value(); });+ });+ auto channel = pc->createDataChannel("face", faceChannelInit());+ pc->setRemoteDescription(rtc::Description(offerSdp, rtc::Description::Type::Offer));+ done->get_future().wait_for(std::chrono::milliseconds(timeoutMs));+ const auto description = pc->localDescription();+ if (!description) {+ error = "No answer";+ return false;+ }+ answerSdp = std::string(*description);+ std::lock_guard lock(impl_->mutex);+ impl_->testPc = pc;+ impl_->testChannel = channel;+ return true;+ } catch (const std::exception& e) {+ error = e.what();+ return false;+ }+}++bool FaceRtc::sendFromTestPeer(const std::string& text) {+ std::shared_ptr<rtc::DataChannel> channel;+ {+ std::lock_guard lock(impl_->mutex);+ channel = impl_->testChannel;+ }+ try {+ return channel && channel->isOpen() && channel->send(text);+ } catch (const std::exception&) {+ return false;+ }+}++#else // without WebRTC++bool webrtcAvailable() { return false; }++struct FaceRtc::Impl {};++FaceRtc::FaceRtc() : impl_(std::make_unique<Impl>()) {}+FaceRtc::~FaceRtc() = default;+FaceRtc::State FaceRtc::state() const { return State::Idle; }++bool FaceRtc::createOffer(std::string&, std::string& error, int) {+ error = "This SculptForge was built without WebRTC (SCULPTFORGE_WEBRTC=OFF), so the live face feed can't connect";+ return false;+}+bool FaceRtc::setAnswer(const std::string&, std::string& error) {+ error = "Built without WebRTC";+ return false;+}+bool FaceRtc::send(const std::string&) { return false; }+void FaceRtc::close() {}+bool FaceRtc::answerForTest(const std::string&, std::string&, std::string& error, int) {+ error = "Built without WebRTC";+ return false;+}+bool FaceRtc::sendFromTestPeer(const std::string&) { return false; }+bool aesGcmEncrypt(const std::string&, const std::string&, std::string&, std::string& error) {+ error = "Built without WebRTC";+ return false;+}+bool aesGcmDecrypt(const std::string&, const std::string&, std::string&, std::string& error) {+ error = "Built without WebRTC";+ return false;+}++#endif++} // namespace sf::linkdiff --git a/src/link/PhoneRtc.h b/src/link/PhoneRtc.hnew file mode 100644index 00000000..3f7a7f8b--- /dev/null+++ b/src/link/PhoneRtc.h@@ -0,0 +1,71 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// The live face feed's transport (docs/phone-link.md): a WebRTC data channel between the phone's face page,+// which must be served over HTTPS for camera access (it lives on our website, website/link/), and this desktop.+// No server of ours is involved:+// - desktop -> phone: the desktop's offer (SDP) travels in the face page URL's fragment, which the browser never+// sends to the website;+// - phone -> desktop: the answer goes through ntfy.sh, a free public message relay, encrypted with AES-256-GCM+// under a key that is also only in the fragment (or, without internet, in a tab the phone opens on this+// desktop's own page);+// - then the data channel runs directly over the local network (unordered, no retransmits: a late face frame is+// useless).+// Built with libdatachannel and Mbed TLS (cmake/WebRTC.cmake, -DSCULPTFORGE_WEBRTC=ON); without them+// webrtcAvailable() is false and FaceRtc refuses to start.+#pragma once++#include <cstdint>+#include <functional>+#include <memory>+#include <string>++namespace sf::link {++bool webrtcAvailable();++class FaceRtc {+public:+ enum class State { Idle, Offered, Connecting, Connected, Closed, Failed };++ FaceRtc();+ ~FaceRtc();+ FaceRtc(const FaceRtc&) = delete;+ FaceRtc& operator=(const FaceRtc&) = delete;++ // Called on WebRTC threads: each text message, and each state change.+ std::function<void(const std::string&)> onMessage;+ std::function<void(State)> onState;++ // Creates the peer connection and its "face" data channel (negotiated, id 0) and waits (up to `timeoutMs`)+ // for the host candidates. The offer SDP.+ bool createOffer(std::string& sdp, std::string& error, int timeoutMs = 3000);+ bool setAnswer(const std::string& sdp, std::string& error);+ // Sends a text message on the data channel (when connected).+ bool send(const std::string& text);+ State state() const;+ void close();++ // A second peer for tests: answers `offerSdp` like the phone's page does.+ bool answerForTest(const std::string& offerSdp, std::string& answerSdp, std::string& error, int timeoutMs = 3000);+ bool sendFromTestPeer(const std::string& text); // once its channel is open++private:+ struct Impl;+ std::unique_ptr<Impl> impl_;+};++const char* faceRtcStateName(FaceRtc::State state);++// base64url without padding (RFC 4648 section 5); decoding also accepts '+', '/' and padding.+std::string base64UrlEncode(const std::string& bytes);+bool base64UrlDecode(const std::string& text, std::string& bytes);++// AES-256-GCM as the face page's WebCrypto does it: `packed` is base64url(iv[12] | ciphertext | tag[16]).+// Needs the WebRTC build (Mbed TLS); false otherwise.+bool aesGcmEncrypt(const std::string& key, const std::string& plain, std::string& packed, std::string& error);+bool aesGcmDecrypt(const std::string& key, const std::string& packed, std::string& plain, std::string& error);+// `n` cryptographically random bytes (the system's generator).+std::string randomBytes(size_t n);++} // namespace sf::linkdiff --git a/src/link/Qr.cpp b/src/link/Qr.cppnew file mode 100644index 00000000..0f43487d--- /dev/null+++ b/src/link/Qr.cpp@@ -0,0 +1,26 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+#include "link/Qr.h"++#include "qrcodegen.hpp"++#include <exception>++namespace sf::link {++bool makeQrCode(const std::string& text, QrCode& out, std::string& error) {+ try {+ const qrcodegen::QrCode qr = qrcodegen::QrCode::encodeText(text.c_str(), qrcodegen::QrCode::Ecc::MEDIUM);+ out.size = qr.getSize();+ out.modules.assign(size_t(out.size) * size_t(out.size), 0);+ for (int y = 0; y < out.size; ++y)+ for (int x = 0; x < out.size; ++x) out.modules[size_t(y * out.size + x)] = qr.getModule(x, y) ? 1 : 0;+ return true;+ } catch (const std::exception& e) {+ error = std::string("QR code: ") + e.what();+ out = {};+ return false;+ }+}++} // namespace sf::linkdiff --git a/src/link/Qr.h b/src/link/Qr.hnew file mode 100644index 00000000..1f9a0e82--- /dev/null+++ b/src/link/Qr.h@@ -0,0 +1,22 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// QR codes for the phone link's pairing window (third_party/qrcodegen, MIT).+#pragma once++#include <cstdint>+#include <string>+#include <vector>++namespace sf::link {++struct QrCode {+ int size = 0; // modules per side (without the quiet zone)+ std::vector<uint8_t> modules; // size * size, row-major, 1 = dark+ bool dark(int x, int y) const { return x >= 0 && y >= 0 && x < size && y < size && modules[size_t(y * size + x)] != 0; }+};++// Encodes `text` (UTF-8) with medium error correction (a phone reads it from a screen at an angle).+bool makeQrCode(const std::string& text, QrCode& out, std::string& error);++} // namespace sf::linkdiff --git a/src/link/Socket.cpp b/src/link/Socket.cppindex 3000e83a..950bc012 100644--- a/src/link/Socket.cpp+++ b/src/link/Socket.cpp@@ -202,7 +202,7 @@ std::string hostName() {return gethostname(name, sizeof name - 1) == 0 ? std::string(name) : std::string();}-bool Socket::listen(const std::string& address, uint16_t port, uint16_t& boundPort, std::string& error) {+bool Socket::listen(const std::string& address, uint16_t port, uint16_t& boundPort, std::string& error, int backlog) {close();if (!initSockets()) {error = "Networking is unavailable";@@ -214,9 +214,15 @@ bool Socket::listen(const std::string& address, uint16_t port, uint16_t& boundPoreturn false;}fd_ = int64_t(s);+#if !defined(_WIN32)+ // A port whose last connections are still in TIME_WAIT can be listened on again at once (on Windows the+ // option would let another program take the port over, and isn't needed there).+ int yes = 1;+ setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes);+#endifsockaddr_in local;if (!parseAddress(address, port, local) || bind(s, reinterpret_cast<sockaddr*>(&local), sizeof local) != 0 ||- ::listen(s, 1) != 0) {+ ::listen(s, backlog) != 0) {error = "Cannot listen on " + address + ": " + lastError();close();return false;@@ -227,10 +233,17 @@ bool Socket::listen(const std::string& address, uint16_t port, uint16_t& boundPoreturn true;}-bool Socket::accept(Socket& client, int timeoutMs) {+bool Socket::accept(Socket& client, int timeoutMs, std::string* from) {if (!valid() || !waitReadable(timeoutMs)) return false;- Native c = ::accept(native(fd_), nullptr, nullptr);+ sockaddr_in peer{};+ SockLen len = sizeof peer;+ Native c = ::accept(native(fd_), reinterpret_cast<sockaddr*>(&peer), &len);if (invalid(c)) return false;+ if (from) {+ char text[INET_ADDRSTRLEN] = {};+ inet_ntop(AF_INET, &peer.sin_addr, text, sizeof text);+ *from = text;+ }client.close();client.fd_ = int64_t(c);#if defined(SO_NOSIGPIPE)@@ -240,6 +253,35 @@ bool Socket::accept(Socket& client, int timeoutMs) {return true;}+bool Socket::connect(const std::string& address, uint16_t port, std::string& error) {+ close();+ if (!initSockets()) {+ error = "Networking is unavailable";+ return false;+ }+ sockaddr_in to;+ if (!parseAddress(address, port, to)) {+ error = "Invalid address " + address;+ return false;+ }+ Native s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);+ if (invalid(s)) {+ error = lastError();+ return false;+ }+ fd_ = int64_t(s);+ if (::connect(s, reinterpret_cast<sockaddr*>(&to), sizeof to) != 0) {+ error = "Cannot connect to " + address + ":" + std::to_string(port) + ": " + lastError();+ close();+ return false;+ }+#if defined(SO_NOSIGPIPE)+ int yes = 1;+ setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &yes, sizeof yes);+#endif+ return true;+}+bool Socket::sendAll(const std::string& data, std::string& error) {size_t sent = 0;while (sent < data.size()) {@@ -265,4 +307,11 @@ bool Socket::receiveSome(std::string& data, size_t maxBytes, int timeoutMs) {return true;}+long long Socket::readSome(char* buffer, size_t size, int timeoutMs) {+ if (!valid()) return -1;+ if (!waitReadable(timeoutMs)) return 0;+ const auto n = recv(native(fd_), buffer, int(size), 0);+ return n > 0 ? (long long)n : -1;+}+} // namespace sf::linkdiff --git a/src/link/Socket.h b/src/link/Socket.hindex fb298f68..20842b3d 100644--- a/src/link/Socket.h+++ b/src/link/Socket.h@@ -27,12 +27,18 @@ public:// Receives one datagram; false on timeout or error. `from` gets the sender's IPv4 address.bool receive(std::string& data, int timeoutMs, std::string* from = nullptr);- // TCP listener on `address` (port 0 = any free port); `boundPort` reports the port.- bool listen(const std::string& address, uint16_t port, uint16_t& boundPort, std::string& error);- bool accept(Socket& client, int timeoutMs);+ // TCP listener on `address` (port 0 = any free port); `boundPort` reports the port. `backlog`: connections+ // the system queues before accept.+ bool listen(const std::string& address, uint16_t port, uint16_t& boundPort, std::string& error, int backlog = 1);+ // `from` gets the client's IPv4 address.+ bool accept(Socket& client, int timeoutMs, std::string* from = nullptr);+ // TCP connection to `address`:`port` (blocking; meant for this machine and the local network).+ bool connect(const std::string& address, uint16_t port, std::string& error);bool sendAll(const std::string& data, std::string& error);// Reads whatever is available (up to `maxBytes`), waiting at most `timeoutMs`.bool receiveSome(std::string& data, size_t maxBytes, int timeoutMs);+ // Reads up to `size` bytes into `buffer`: the byte count, 0 on timeout, -1 when the peer closed or on error.+ long long readSome(char* buffer, size_t size, int timeoutMs);bool valid() const { return fd_ >= 0; }void close();diff --git a/tests/test_phone_link.cpp b/tests/test_phone_link.cppnew file mode 100644index 00000000..5f59ade5--- /dev/null+++ b/tests/test_phone_link.cpp@@ -0,0 +1,674 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// The phone link (src/link): the HTTP request parser (with a fuzz pass), the server on a real socket, pairing,+// resumable uploads into the Captures inbox, the face message parser and takes, the WebRTC face link (when+// built with it), the QR code and the AR export (GLB and USDZ validity).+#include "TestHarness.h"++#include "core/Json.h"+#include "core/Primitives.h"+#include "core/Texture.h"+#include "io/ArExport.h"+#include "io/GltfIO.h"+#include "io/Zip.h"+#include "link/FaceFeed.h"+#include "link/HttpServer.h"+#include "link/PhoneLink.h"+#include "link/PhoneRtc.h"+#include "link/Qr.h"+#include "link/Socket.h"++#include <chrono>+#include <cmath>+#include <cstring>+#include <filesystem>+#include <fstream>+#include <iterator>+#include <random>+#include <thread>++using namespace sf;+using namespace sf::link;+namespace fs = std::filesystem;++namespace {++HttpRequestParser::State parseAll(HttpRequestParser& p, const std::string& text) { return p.feed(text.data(), text.size()); }++std::string readText(const fs::path& p) {+ std::ifstream in(p, std::ios::binary);+ return std::string(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());+}++fs::path freshDir(const char* name) {+ const fs::path dir = fs::temp_directory_path() / name;+ std::error_code ec;+ fs::remove_all(dir, ec);+ fs::create_directories(dir, ec);+ return dir;+}++fs::path upath(const std::string& s) { return fs::path(std::u8string(s.begin(), s.end())); }++std::string str(const fs::path& p) {+ const auto u = p.u8string();+ return std::string(u.begin(), u.end());+}++HttpRequest request(const std::string& method, const std::string& target, const std::string& token = {},+ const std::string& body = {}) {+ HttpRequest r;+ r.method = method;+ r.target = target;+ const size_t q = target.find('?');+ r.path = target.substr(0, q);+ r.query = q == std::string::npos ? std::string() : target.substr(q + 1);+ r.version = "HTTP/1.1";+ if (!token.empty()) r.headers.emplace_back("x-sf-token", token);+ r.headers.emplace_back("user-agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 Version/18.0 Mobile/15E148 Safari/604.1");+ r.body = body;+ r.peer = "192.168.1.33";+ return r;+}++Json parseJson(const std::string& text) {+ Json j;+ std::string err;+ Json::parse(text, j, err);+ return j;+}++PhoneLinkOptions testOptions(const fs::path& inbox) {+ PhoneLinkOptions o;+ o.inboxRoot = str(inbox);+ o.startServer = false;+ o.desktopName = "Test PC";+ o.version = "0.0.0";+ o.relayUrl = "off";+ o.siteUrl = "https://example.org/link/";+ return o;+}++std::string faceJson(float jaw, float t = 0) {+ return "{\"type\":\"face\",\"t\":" + std::to_string(t) + ",\"weights\":{\"jawOpen\":" + std::to_string(jaw) ++ ",\"eyeBlinkLeft\":0.25,\"_neutral\":0.1},\"head\":[0,0,0,1]}";+}++} // namespace++// --- HTTP parser ---++TEST(http_parser_reads_a_request) {+ HttpRequestParser p;+ CHECK(parseAll(p, "GET /api/status?k=ab%20c&x=1+2 HTTP/1.1\r\nHost: 10.0.0.2:8765\r\nUser-Agent: Phone\r\n\r\n") ==+ HttpRequestParser::State::Done);+ const HttpRequest& r = p.request();+ CHECK(r.method == "GET");+ CHECK(r.path == "/api/status");+ CHECK(r.param("k") == "ab c");+ CHECK(r.param("x") == "1 2");+ CHECK(r.param("missing", "none") == "none");+ CHECK(r.header("user-agent") == "Phone");+ CHECK(r.header("host") == "10.0.0.2:8765");+ CHECK(r.keepAlive());+ CHECK(r.body.empty());+}++TEST(http_parser_handles_split_bodies_and_pipelining) {+ const std::string two = "PUT /api/file/ab12?offset=0 HTTP/1.1\r\nContent-Length: 5\r\n\r\nhello"+ "POST /api/pair HTTP/1.1\r\nContent-Length: 2\r\nConnection: close\r\n\r\n{}";+ // Byte by byte gives the same as all at once.+ HttpRequestParser p;+ size_t i = 0;+ while (i < two.size() && !p.done()) p.feed(&two[i++], 1);+ CHECK(p.done());+ CHECK(p.request().method == "PUT");+ CHECK(p.request().body == "hello");+ // The rest of the stream is the next request.+ p.feed(two.data() + i, two.size() - i);+ CHECK(p.next() == HttpRequestParser::State::Done);+ CHECK(p.request().method == "POST");+ CHECK(p.request().body == "{}");+ CHECK(!p.request().keepAlive());+}++TEST(http_parser_rejects_bad_requests) {+ struct Case {+ std::string text;+ int status;+ };+ const std::string bigHeader = "GET / HTTP/1.1\r\nX: " + std::string(20000, 'a') + "\r\n\r\n";+ const Case cases[] = {+ {"GET /\r\n\r\n", 400}, // no version+ {"GET / HTTP/2.0\r\n\r\n", 505}, // unsupported version+ {"GET http://evil/ HTTP/1.1\r\n\r\n", 400}, // absolute form+ {"G(T / HTTP/1.1\r\n\r\n", 400}, // bad method+ {"GET / HTTP/1.1\r\nBad Header: x\r\n\r\n", 400}, // space in a name+ {"GET / HTTP/1.1\r\nA: b\r\n folded\r\n\r\n", 400}, // obsolete folding+ {"POST / HTTP/1.1\r\nContent-Length: -1\r\n\r\n", 400},+ {"POST / HTTP/1.1\r\nContent-Length: 5\r\nContent-Length: 6\r\n\r\n", 400}, // smuggling attempt+ {"POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n", 501},+ {"POST / HTTP/1.1\r\nContent-Length: 999999999999\r\n\r\n", 413},+ {bigHeader, 431},+ {"GET /a\x01 HTTP/1.1\r\n\r\n", 400},+ };+ for (const Case& c : cases) {+ HttpRequestParser p;+ p.feed(c.text.data(), c.text.size());+ CHECK(p.failed());+ CHECK(p.errorStatus() == c.status);+ }+ // An endless head without a blank line fails once it passes the limit.+ HttpRequestParser p;+ const std::string line = "X-Fill: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n";+ p.feed("GET / HTTP/1.1\r\n", 16);+ for (int i = 0; i < 1000 && !p.failed(); ++i) p.feed(line.data(), line.size());+ CHECK(p.failed() && p.errorStatus() == 431);+}++TEST(http_parser_survives_fuzzing) {+ // Mutated valid requests and random bytes, fed in random pieces: the parser must end in a defined state,+ // never read past what it was given, and agree with itself whatever the split.+ const std::string seeds[] = {+ "GET /api/status?k=0123 HTTP/1.1\r\nHost: x\r\n\r\n",+ "PUT /api/file/00ff?offset=4194304 HTTP/1.1\r\nContent-Length: 8\r\nX-SF-Token: abc\r\n\r\n\x00\x01\x02\x03\x04\x05\x06\x07",+ "POST /api/capture HTTP/1.1\r\nContent-Type: application/json\r\nContent-Length: 16\r\n\r\n{\"kind\":\"splat\"}",+ };+ std::mt19937 rng(1234);+ int done = 0, failed = 0;+ for (int iter = 0; iter < 4000; ++iter) {+ std::string text = seeds[iter % 3];+ if (iter % 3 == 2 && iter % 7 == 0) text.resize(rng() % text.size());+ const int mutations = int(rng() % 6);+ for (int m = 0; m < mutations && !text.empty(); ++m) {+ const size_t at = rng() % text.size();+ switch (rng() % 4) {+ case 0: text[at] = char(rng() & 0xFF); break;+ case 1: text.erase(at, 1); break;+ case 2: text.insert(at, 1, char(rng() & 0xFF)); break;+ default: text.insert(at, "\r\n"); break;+ }+ }+ if (iter % 50 == 0) { // pure noise+ text.resize(rng() % 300);+ for (char& c : text) c = char(rng() & 0xFF);+ }+ HttpLimits limits;+ limits.maxHeadBytes = 512;+ limits.maxBodyBytes = 1024;+ HttpRequestParser whole(limits), pieces(limits);+ whole.feed(text.data(), text.size());+ size_t pos = 0;+ while (pos < text.size()) {+ const size_t n = std::min(text.size() - pos, size_t(1 + rng() % 17));+ pieces.feed(text.data() + pos, n);+ pos += n;+ }+ CHECK(whole.state() == pieces.state());+ if (whole.done()) {+ ++done;+ CHECK(whole.request().method == pieces.request().method);+ CHECK(whole.request().body == pieces.request().body);+ CHECK(whole.request().body.size() == whole.contentLength());+ }+ if (whole.failed()) {+ ++failed;+ CHECK(whole.errorStatus() >= 400 && whole.errorStatus() < 600);+ }+ }+ CHECK(done > 100);+ CHECK(failed > 100);+}++TEST(http_helpers) {+ CHECK(percentDecode("a%2Fb%zz%4") == "a/b%zz%4");+ CHECK(percentDecode("a+b", true) == "a b");+ CHECK(percentEncode("http://1.2.3.4:5/?a=b c") == "http%3A%2F%2F1.2.3.4%3A5%2F%3Fa%3Db%20c");+ CHECK(queryParameter("a=1&b=&c", "b", "x").empty());+ CHECK(queryParameter("a=1&b=&c", "c", "x").empty());+ CHECK(queryParameter("a=1&b=&c", "d", "x") == "x");+ CHECK(isLocalNetworkAddress("192.168.1.20"));+ CHECK(isLocalNetworkAddress("10.0.0.117"));+ CHECK(isLocalNetworkAddress("172.31.255.1"));+ CHECK(!isLocalNetworkAddress("172.32.0.1"));+ CHECK(isLocalNetworkAddress("127.0.0.1"));+ CHECK(isLocalNetworkAddress("100.99.23.78"));+ CHECK(!isLocalNetworkAddress("8.8.8.8"));+ CHECK(!isLocalNetworkAddress("192.168.1"));+ CHECK(!isLocalNetworkAddress("192.168.1.256"));+ CHECK(!isLocalNetworkAddress("::1"));+ const std::string head = httpResponseHead(HttpResponse::json(200, "{}"), true);+ CHECK(head.rfind("HTTP/1.1 200 OK\r\n", 0) == 0);+ CHECK(head.find("Content-Length: 2\r\n") != std::string::npos);+ CHECK(head.find("Connection: keep-alive") != std::string::npos);+}++TEST(http_server_answers_on_a_socket) {+ HttpServer server;+ HttpServer::Options o;+ o.port = 0;+ o.bindAddress = "127.0.0.1";+ std::string err;+ CHECK(server.start(o, [](const HttpRequest& r) { return HttpResponse::text(200, r.method + " " + r.path + " " + r.body); }, err));+ CHECK(server.port() != 0);+ // Two requests on separate connections, one of them with a body.+ HttpClientResponse a = httpLocalRequest("127.0.0.1", server.port(), "GET", "/hello?x=1");+ CHECK(a.status == 200);+ CHECK(a.body == "GET /hello ");+ HttpClientResponse b = httpLocalRequest("127.0.0.1", server.port(), "POST", "/data", std::string(100000, 'z'));+ CHECK(b.status == 200);+ CHECK(b.body.size() == std::string("POST /data ").size() + 100000);+ // Keep-alive: two requests on one connection.+ Socket s;+ CHECK(s.connect("127.0.0.1", server.port(), err));+ CHECK(s.sendAll("GET /one HTTP/1.1\r\n\r\nGET /two HTTP/1.1\r\nConnection: close\r\n\r\n", err));+ std::string all;+ char buf[4096];+ for (int i = 0; i < 50; ++i) {+ const long long n = s.readSome(buf, sizeof buf, 100);+ if (n < 0) break;+ all.append(buf, size_t(std::max<long long>(n, 0)));+ }+ CHECK(all.find("GET /one") != std::string::npos);+ CHECK(all.find("GET /two") != std::string::npos);+ // A malformed request gets its error status and the connection closes.+ Socket bad;+ CHECK(bad.connect("127.0.0.1", server.port(), err));+ CHECK(bad.sendAll("POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n", err));+ std::string reply;+ for (int i = 0; i < 50; ++i) {+ const long long n = bad.readSome(buf, sizeof buf, 100);+ if (n < 0) break;+ reply.append(buf, size_t(std::max<long long>(n, 0)));+ }+ CHECK(reply.rfind("HTTP/1.1 501", 0) == 0);+ server.stop();+ CHECK(!server.running());+}++// --- Session: pairing, uploads, the inbox ---++TEST(phone_upload_names_are_safe) {+ CHECK(sanitizeUploadName("IMG_0001.JPG") == "IMG_0001.JPG");+ CHECK(sanitizeUploadName("../../etc/passwd") == "passwd");+ CHECK(sanitizeUploadName("..\\..\\Windows\\scan.ply") == "scan.ply");+ CHECK(sanitizeUploadName(".hidden.ply") == "hidden.ply");+ CHECK(sanitizeUploadName("my scan (1).mov") == "my_scan__1_.mov");+ CHECK(sanitizeUploadName("CON.ply") == "file_CON.ply");+ CHECK(sanitizeUploadName("capture.json") == "capture_file.json");+ CHECK(sanitizeUploadName("....") == "");+ CHECK(sanitizeUploadName("") == "");+ CHECK(sanitizeUploadName("caf\xC3\xA9.jpg") == "caf.jpg");+ CHECK(sanitizeUploadName(std::string(300, 'a') + ".mp4").size() <= 80);+ CHECK(sanitizeUploadName(std::string(300, 'a') + ".mp4").find(".mp4") != std::string::npos);+ CHECK(captureExtensionAllowed(CaptureKind::Splat, ".mov"));+ CHECK(captureExtensionAllowed(CaptureKind::Splat, ".jpg"));+ CHECK(!captureExtensionAllowed(CaptureKind::Photo, ".mov"));+ CHECK(captureExtensionAllowed(CaptureKind::Ply, ".ply"));+ CHECK(!captureExtensionAllowed(CaptureKind::Ply, ".exe"));+ CHECK(describeDevice("Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 Version/18.0 Mobile/15E148 Safari/604.1") ==+ "iPhone \xC2\xB7 Safari");+ CHECK(describeDevice("Mozilla/5.0 (Linux; Android 15; Pixel 9) AppleWebKit/537.36 Chrome/131.0 Mobile Safari/537.36") ==+ "Android \xC2\xB7 Chrome");+}++TEST(phone_pairing_and_tokens) {+ const fs::path inbox = freshDir("sf_phone_pair");+ PhoneLink link;+ std::string err;+ CHECK(link.start(testOptions(inbox), err));+ // Without the token only the page itself answers.+ CHECK(link.handle(request("GET", "/")).status == 200);+ CHECK(link.handle(request("GET", "/")).body.find("SculptForge capture") != std::string::npos);+ CHECK(link.handle(request("GET", "/api/status")).status == 403);+ CHECK(link.handle(request("GET", "/api/status", "0123456789abcdef0123456789abcdef")).status == 403);+ CHECK(link.handle(request("GET", "/ar/model.glb?k=nope")).status == 403);+ CHECK(link.handle(request("GET", "/etc/passwd")).status == 404);+ // The QR code's URL carries the token.+ CHECK(link.url().find("/?k=" + link.token()) != std::string::npos);+ CHECK(link.handle(request("GET", "/api/status?k=" + link.token())).status == 200);+ const Json st = parseJson(link.handle(request("GET", "/api/status", link.token())).body);+ CHECK(st["desktop"].asString() == "Test PC");+ CHECK(st["device"].asString() == "iPhone \xC2\xB7 Safari");+ // Typing the code gives the token; a wrong code doesn't.+ std::this_thread::sleep_for(std::chrono::milliseconds(850));+ CHECK(link.handle(request("POST", "/api/pair", {}, "{\"code\":\"999999x\"}")).status == 403 || link.code() == "999999");+ std::this_thread::sleep_for(std::chrono::milliseconds(850));+ HttpResponse ok = link.handle(request("POST", "/api/pair", {}, "{\"code\":\"" + link.code() + "\"}"));+ CHECK(ok.status == 200);+ CHECK(parseJson(ok.body)["token"].asString() == link.token());+ // Too fast: refused without counting.+ CHECK(link.handle(request("POST", "/api/pair", {}, "{\"code\":\"" + link.code() + "\"}")).status == 429);+ bool connected = false;+ for (const PhoneEvent& e : link.takeEvents()) connected = connected || e.type == PhoneEvent::Type::DeviceConnected;+ CHECK(connected);+ link.stop();+}++TEST(phone_upload_resumes_and_lands_in_the_inbox) {+ const fs::path inbox = freshDir("sf_phone_upload");+ PhoneLink link;+ std::string err;+ CHECK(link.start(testOptions(inbox), err));+ const std::string k = link.token();+ // A bad kind and a bad file are refused.+ CHECK(link.handle(request("POST", "/api/capture", k, "{\"kind\":\"virus\"}")).status == 400);+ const Json cap = parseJson(link.handle(request("POST", "/api/capture", k, "{\"kind\":\"splat\"}")).body);+ const std::string cid = cap["id"].asString();+ CHECK(!cid.empty());+ CHECK(link.handle(request("POST", "/api/capture/" + cid + "/file", k, "{\"name\":\"x.exe\",\"size\":10}")).status == 400);+ CHECK(link.handle(request("POST", "/api/capture/" + cid + "/file", k, "{\"name\":\"x.mov\",\"size\":9e12}")).status == 413);+ CHECK(link.handle(request("POST", "/api/capture/" + cid + "/finish", k)).status == 400); // nothing yet++ std::string data(3 * 1000 + 17, '\0');+ for (size_t i = 0; i < data.size(); ++i) data[i] = char((i * 131) & 0xFF);+ const Json file = parseJson(link.handle(request("POST", "/api/capture/" + cid + "/file", k,+ "{\"name\":\"../scan video.mov\",\"size\":" + std::to_string(data.size()) + "}")).body);+ const std::string fid = file["id"].asString();+ CHECK(file["name"].asString() == "scan_video.mov");+ // First chunk; then a chunk from the future (a lost one in between) is refused with what arrived.+ CHECK(link.handle(request("PUT", "/api/file/" + fid + "?offset=0", k, data.substr(0, 1000))).status == 200);+ HttpResponse gap = link.handle(request("PUT", "/api/file/" + fid + "?offset=2000", k, data.substr(2000, 1000)));+ CHECK(gap.status == 409);+ CHECK(parseJson(gap.body)["received"].asNumber() == 1000);+ // Finishing early lists the incomplete file.+ CHECK(link.handle(request("POST", "/api/capture/" + cid + "/finish", k)).status == 409);+ // A resent chunk that overlaps what arrived (the phone didn't see the answer) only adds the new part.+ CHECK(link.handle(request("PUT", "/api/file/" + fid + "?offset=500", k, data.substr(500, 1500))).status == 200);+ CHECK(parseJson(link.handle(request("GET", "/api/file/" + fid, k)).body)["received"].asNumber() == 2000);+ CHECK(link.handle(request("PUT", "/api/file/" + fid + "?offset=2000", k, data.substr(2000) + "extra")).status == 400);+ CHECK(link.handle(request("PUT", "/api/file/" + fid + "?offset=2000", k, data.substr(2000))).status == 200);+ // Status shows the upload.+ const PhoneLinkStatus st = link.status();+ CHECK(st.uploads.size() == 1);+ CHECK(st.uploads[0].received == data.size());+ HttpResponse fin = link.handle(request("POST", "/api/capture/" + cid + "/finish", k));+ CHECK(fin.status == 200);+ const fs::path folder = upath(parseJson(fin.body)["folder"].asString());+ CHECK(folder.parent_path() == inbox);+ CHECK(folder.filename().string().size() == std::string("2026-10-26_195803-splat").size());+ CHECK(folder.filename().string().find("-splat") != std::string::npos);+ CHECK(readText(folder / "scan_video.mov") == data);+ const Json info = parseJson(readText(folder / "capture.json"));+ CHECK(info["kind"].asString() == "splat");+ CHECK(info["device"].asString() == "iPhone \xC2\xB7 Safari");+ CHECK(info["files"].size() == 1);+ CHECK(info["files"][size_t(0)]["name"].asString() == "scan_video.mov");+ CHECK(info["files"][size_t(0)]["size"].asNumber() == double(data.size()));+ // Nothing is left in the staging folder, and the app hears about it.+ CHECK(fs::is_empty(inbox / ".incoming"));+ bool ready = false;+ for (const PhoneEvent& e : link.takeEvents())+ if (e.type == PhoneEvent::Type::CaptureReady) {+ ready = true;+ CHECK(e.kind == CaptureKind::Splat);+ CHECK(e.files.size() == 1);+ CHECK(upath(e.files[0]).filename() == "scan_video.mov");+ }+ CHECK(ready);+ // Finishing again just answers with the folder; more chunks aren't taken.+ CHECK(link.handle(request("POST", "/api/capture/" + cid + "/finish", k)).status == 200);++ // A second capture in the same second gets its own folder.+ const std::string cid2 = parseJson(link.handle(request("POST", "/api/capture", k, "{\"kind\":\"ply\"}")).body)["id"].asString();+ const std::string fid2 = parseJson(link.handle(request("POST", "/api/capture/" + cid2 + "/file", k, "{\"name\":\"s.ply\",\"size\":3}")).body)["id"].asString();+ CHECK(link.handle(request("PUT", "/api/file/" + fid2 + "?offset=0", k, "ply")).status == 200);+ const fs::path folder2 = upath(parseJson(link.handle(request("POST", "/api/capture/" + cid2 + "/finish", k)).body)["folder"].asString());+ CHECK(fs::exists(folder2 / "s.ply"));+ CHECK(folder2 != folder);++ // An unfinished capture is removed when the link stops.+ const std::string cid3 = parseJson(link.handle(request("POST", "/api/capture", k, "{\"kind\":\"photo\"}")).body)["id"].asString();+ link.handle(request("POST", "/api/capture/" + cid3 + "/file", k, "{\"name\":\"p.jpg\",\"size\":30}"));+ CHECK(fs::exists(inbox / ".incoming" / cid3));+ link.stop();+ CHECK(!fs::exists(inbox / ".incoming" / cid3));+}++// --- Face ---++TEST(face_messages_parse) {+ FaceFrame f;+ std::string err;+ CHECK(parseFaceMessage(faceJson(0.5f, 1234.5f), f, err));+ CHECK(std::abs(f.t - 1234.5) < 1e-6);+ CHECK(std::abs(f.weight("jawOpen") - 0.5f) < 1e-6f);+ CHECK(std::abs(f.weight("eyeBlinkLeft") - 0.25f) < 1e-6f);+ CHECK(f.presentCount() == 2); // _neutral is MediaPipe's, not ARKit's: ignored without counting as unknown+ CHECK(f.unknownNames == 0);+ CHECK(f.hasHead);+ CHECK(std::abs(f.head.w - 1.0f) < 1e-6f);+ // Clamped, normalised, unknown names counted.+ CHECK(parseFaceMessage(R"({"type":"face","weights":{"jawOpen":1.7,"mouthSmileLeft":-1,"wiggle":0.3},"head":[0,0,2,0]})", f, err));+ CHECK(f.weight("jawOpen") == 1.0f);+ CHECK(f.weight("mouthSmileLeft") == 0.0f);+ CHECK(f.unknownNames == 1);+ CHECK(std::abs(f.head.z - 1.0f) < 1e-6f);+ // Rejected.+ CHECK(!parseFaceMessage("not json", f, err));+ CHECK(!parseFaceMessage(R"({"type":"hand","weights":{}})", f, err));+ CHECK(!parseFaceMessage(R"({"type":"face"})", f, err));+ CHECK(!parseFaceMessage(R"({"type":"face","weights":{"jawOpen":"wide"}})", f, err));+ CHECK(!parseFaceMessage(R"({"type":"face","weights":{},"head":[1,2,3]})", f, err));+ CHECK(!parseFaceMessage(R"({"type":"face","weights":{},"head":[0,0,0,0]})", f, err));+ CHECK(!parseFaceMessage(R"({"type":"face","t":"now","weights":{}})", f, err));+ CHECK(!parseFaceMessage("{\"type\":\"face\",\"weights\":{\"jawOpen\":" + std::string(20000, '1') + "}}", f, err));+ // All 52 names, each mirror pair, and the weights round trip.+ CHECK(arkitShapeNames().size() == 52);+ CHECK(arkitShapeIndex("tongueOut") == 51);+ CHECK(arkitShapeIndex("_neutral") < 0);+ for (int i = 0; i < kArkitShapeCount; ++i) CHECK(arkitMirrorIndex(arkitMirrorIndex(i)) == i);+ CHECK(arkitShapeNames()[size_t(arkitMirrorIndex(arkitShapeIndex("eyeBlinkLeft")))] == std::string("eyeBlinkRight"));+ CHECK(arkitShapeNames()[size_t(arkitMirrorIndex(arkitShapeIndex("jawLeft")))] == std::string("jawRight"));+ CHECK(arkitMirrorIndex(arkitShapeIndex("jawOpen")) == arkitShapeIndex("jawOpen"));+ CHECK(parseFaceMessage(faceJson(0.3125f), f, err));+ const FaceFrame m = f.mirrored();+ CHECK(m.weight("eyeBlinkRight") == 0.25f);+ CHECK(m.weight("eyeBlinkLeft") == 0.0f);+ CHECK(f.weightsJson() == "{\"eyeBlinkLeft\":0.25,\"jawOpen\":0.3125}");+ FaceFrame back;+ CHECK(parseFaceMessage(f.messageJson(), back, err));+ CHECK(back.weightsJson() == f.weightsJson());+}++TEST(face_feed_over_http_and_takes) {+ const fs::path inbox = freshDir("sf_phone_face");+ PhoneLink link;+ std::string err;+ CHECK(link.start(testOptions(inbox), err));+ const std::string k = link.token();+ FaceFrame f;+ uint64_t serial = 0;+ CHECK(!link.latestFace(f, serial));+ CHECK(link.handle(request("POST", "/api/face", k, faceJson(0.1f, 0))).status == 200);+ CHECK(link.latestFace(f, serial));+ CHECK(std::abs(f.weight("jawOpen") - 0.1f) < 1e-5f);+ CHECK(!link.latestFace(f, serial)); // nothing newer+ CHECK(link.status().faceConnected);+ CHECK(link.status().faceTransport == "Wi-Fi (HTTP)");+ CHECK(link.handle(request("POST", "/api/face", k, "{\"type\":\"face\",\"weights\":5}")).status == 400);+ CHECK(link.handle(request("POST", "/api/face", "", faceJson(0.2f))).status == 403);+ // A take records every frame, batched or not.+ std::string folder;+ CHECK(link.startTake(folder, err));+ CHECK(fs::path(upath(folder)).filename().string().find("-face") != std::string::npos);+ CHECK(link.handle(request("POST", "/api/face", k, faceJson(0.2f, 1000) + "\n" + faceJson(0.3f, 1033))).status == 200);+ CHECK(link.receiveFaceMessage(faceJson(0.4f, 1066), "WebRTC", err));+ CHECK(link.status().recording);+ CHECK(link.status().takeFrames == 3);+ std::string summary;+ CHECK(link.stopTake(&summary) == folder);+ CHECK(summary.find("3 frames") != std::string::npos);+ std::vector<FaceFrame> frames;+ CHECK(readFaceTake(str(upath(folder) / "take.jsonl"), frames, err));+ CHECK(frames.size() == 3);+ CHECK(std::abs(frames[2].weight("jawOpen") - 0.4f) < 1e-4f);+ CHECK(std::abs(frames[1].t - 1033) < 1e-3);+ const Json info = parseJson(readText(upath(folder) / "capture.json"));+ CHECK(info["kind"].asString() == "face");+ CHECK(info["frames"].asNumber() == 3);+ CHECK(std::abs(info["seconds"].asNumber() - 0.066) < 1e-6);+ CHECK(link.stopTake().empty()); // not recording any more+ link.stop();+}++TEST(face_link_over_webrtc) {+ if (!webrtcAvailable()) {+ PhoneLink link;+ std::string url, err;+ CHECK(!link.createFaceLink(url, err));+ CHECK(err.find("without WebRTC") != std::string::npos);+ return;+ }+ // base64url and AES-GCM as the face page uses them.+ CHECK(base64UrlEncode("\xff\xfe") == "__4");+ std::string bytes;+ CHECK(base64UrlDecode("__4", bytes) && bytes == "\xff\xfe");+ const std::string key = randomBytes(32);+ std::string packed, plain, err;+ CHECK(aesGcmEncrypt(key, "hello phone", packed, err));+ CHECK(aesGcmDecrypt(key, packed, plain, err) && plain == "hello phone");+ packed[20] = packed[20] == 'A' ? 'B' : 'A';+ CHECK(!aesGcmDecrypt(key, packed, plain, err)); // tampered++ const fs::path inbox = freshDir("sf_phone_rtc");+ PhoneLink link;+ CHECK(link.start(testOptions(inbox), err));+ // The capture page asks for the face link: a URL on the website with everything in the fragment.+ const Json linkJson = parseJson(link.handle(request("GET", "/api/face-link", link.token())).body);+ const std::string url = linkJson["url"].asString();+ CHECK(url.rfind("https://example.org/link/#v=1&", 0) == 0);+ const std::string fragment = url.substr(url.find('#') + 1);+ std::string offer, faceKey;+ CHECK(base64UrlDecode(queryParameter(fragment, "o"), offer));+ CHECK(offer.find("webrtc-datachannel") != std::string::npos);+ CHECK(base64UrlDecode(queryParameter(fragment, "x"), faceKey) && faceKey.size() == 32);+ CHECK(queryParameter(fragment, "k") == link.token());+ // The phone (a second peer here) answers; the answer comes back encrypted through the relay.+ FaceRtc phone;+ std::string answer;+ CHECK(phone.answerForTest(offer, answer, err));+ std::string wrongToken;+ CHECK(aesGcmEncrypt(faceKey, "{\"k\":\"nope\",\"sdp\":\"v=0\"}", wrongToken, err));+ CHECK(!link.acceptRelayMessage(wrongToken, err));+ Json msg = Json::object();+ msg.set("k", link.token());+ msg.set("sdp", answer);+ std::string relayed;+ CHECK(aesGcmEncrypt(faceKey, msg.dump(), relayed, err));+ CHECK(link.acceptRelayMessage(relayed, err));+ // Frames flow over the data channel.+ FaceFrame f;+ uint64_t serial = 0;+ bool got = false;+ for (int i = 0; i < 100 && !got; ++i) {+ phone.sendFromTestPeer(faceJson(0.75f));+ std::this_thread::sleep_for(std::chrono::milliseconds(50));+ got = link.latestFace(f, serial);+ }+ CHECK(got);+ CHECK(std::abs(f.weight("jawOpen") - 0.75f) < 1e-5f);+ CHECK(link.status().faceConnected);+ CHECK(link.status().faceTransport == "WebRTC");+ phone.close();+ link.stop();+}++// --- QR code ---++TEST(qr_code_encodes_the_pairing_url) {+ QrCode qr;+ std::string err;+ CHECK(makeQrCode("http://192.168.1.20:8765/?k=0123456789abcdef0123456789abcdef", qr, err));+ CHECK(qr.size >= 29 && qr.size <= 45);+ CHECK(qr.modules.size() == size_t(qr.size * qr.size));+ // Finder patterns in three corners: dark outer ring, light ring, dark centre.+ for (auto [x, y] : {std::pair{0, 0}, std::pair{qr.size - 7, 0}, std::pair{0, qr.size - 7}}) {+ CHECK(qr.dark(x, y) && qr.dark(x + 6, y + 6) && qr.dark(x + 3, y + 3));+ CHECK(!qr.dark(x + 1, y + 1) && !qr.dark(x + 5, y + 5));+ }+ CHECK(!qr.dark(-1, 0));+ CHECK(!makeQrCode(std::string(5000, 'x'), qr, err));+}++// --- AR export ---++TEST(ar_export_writes_valid_glb_and_usdz) {+ Mesh sphere = makeIcosphere(5); // 20,480 triangles+ // Polypaint: red top, blue bottom.+ sphere.ensureColors();+ for (size_t v = 0; v < sphere.vertexCount(); ++v)+ sphere.colors[v] = sphere.positions[v].y > 0 ? glm::vec3(0.9f, 0.05f, 0.05f) : glm::vec3(0.05f, 0.05f, 0.9f);+ ArExportOptions o;+ o.maxTriangles = 4000;+ o.sizeMeters = 0.2f;+ o.textureSize = 256;+ ArExportResult r;+ std::string err;+ CHECK(exportArModel({&sphere}, o, r, err));+ CHECK(r.colored);+ CHECK(r.triangles > 2000 && r.triangles <= 4000);+ CHECK(r.summary.find("in colour") != std::string::npos);+ // GLB: header, a JSON chunk naming an image and a texture, total length matching.+ CHECK(r.glb.size() > 20);+ CHECK(r.glb.compare(0, 4, "glTF") == 0);+ uint32_t version = 0, length = 0, jsonLength = 0;+ std::memcpy(&version, r.glb.data() + 4, 4);+ std::memcpy(&length, r.glb.data() + 8, 4);+ std::memcpy(&jsonLength, r.glb.data() + 12, 4);+ CHECK(version == 2);+ CHECK(length == r.glb.size());+ CHECK(r.glb.compare(16, 4, "JSON") == 0);+ const Json gltf = parseJson(r.glb.substr(20, jsonLength));+ CHECK(gltf["images"].size() == 1);+ CHECK(gltf["materials"].size() >= 1);+ // It loads back at desk size: 20 cm across, standing on y = 0.+ const fs::path dir = freshDir("sf_phone_ar");+ {+ std::ofstream(dir / "m.glb", std::ios::binary) << r.glb;+ std::ofstream(dir / "m.usdz", std::ios::binary) << r.usdz;+ }+ Mesh back;+ CHECK(loadGltf(str(dir / "m.glb"), back, err));+ const AABB box = back.bounds();+ const glm::vec3 e = box.extent();+ CHECK(std::abs(std::max({e.x, e.y, e.z}) - 0.2f) < 1e-3f);+ CHECK(std::abs(box.min.y) < 1e-4f);+ CHECK(std::abs(box.center().x) < 1e-3f && std::abs(box.center().z) < 1e-3f);+ CHECK(back.hasUvs()); // (the glTF reader doesn't load textures; the material must name one)+ CHECK(gltf["materials"][size_t(0)]["pbrMetallicRoughness"].has("baseColorTexture"));+ // USDZ: a stored zip whose first file is the USD layer, with the texture inside, 64-byte aligned.+ std::vector<ZipFileEntry> entries;+ CHECK(readStoredZipIndex(str(dir / "m.usdz"), entries, err));+ CHECK(!entries.empty());+ CHECK(entries[0].name.size() > 5 && entries[0].name.substr(entries[0].name.size() - 5) == ".usda");+ bool png = false;+ for (const auto& en : entries) {+ CHECK(en.offset % 64 == 0);+ png = png || en.name.find(".png") != std::string::npos;+ }+ CHECK(png);+ // Without colour there is no texture; hidden triangles are left out; an empty scene is an error.+ Mesh plain = makeIcosphere(3);+ CHECK(exportArModel({&plain}, o, r, err));+ CHECK(!r.colored);+ CHECK(parseJson(r.glb.substr(20, [&] { uint32_t n; std::memcpy(&n, r.glb.data() + 12, 4); return n; }()))["images"].size() == 0);+ Mesh empty;+ CHECK(!exportArModel({&empty, nullptr}, o, r, err));+ plain.ensureHidden();+ std::fill(plain.hidden.begin(), plain.hidden.end(), uint8_t(1));+ CHECK(!exportArModel({&plain}, o, r, err));+}++TEST(ar_export_bakes_corner_colours) {+ // One triangle covering the lower-left half of the texture, red; the padding spills a few texels over.+ const std::vector<glm::uvec3> tris = {{0, 1, 2}};+ const std::vector<glm::vec2> uvs = {{0.0f, 0.0f}, {1.0f, 0.0f}, {0.0f, 1.0f}};+ const std::vector<glm::vec3> cols(3, glm::vec3(1.0f, 0.0f, 0.0f));+ const std::vector<uint8_t> rgba = bakeCornerColors(tris, uvs, cols, 32);+ CHECK(rgba.size() == 32 * 32 * 4);+ auto px = [&](int x, int y) { return &rgba[(size_t(y) * 32 + size_t(x)) * 4]; };+ CHECK(px(2, 2)[0] == 255 && px(2, 2)[1] == 0); // inside+ CHECK(px(31, 31)[0] == 255 && px(31, 31)[1] == 255); // far outside: white+}diff --git a/third_party/qrcodegen/SOURCE.txt b/third_party/qrcodegen/SOURCE.txtnew file mode 100644index 00000000..45012ed1--- /dev/null+++ b/third_party/qrcodegen/SOURCE.txt@@ -0,0 +1,3 @@+QR Code generator library (C++), by Project Nayuki - MIT License (see the header of each file).+https://github.com/nayuki/QR-Code-generator, tag v1.8.0, files cpp/qrcodegen.cpp and cpp/qrcodegen.hpp, unmodified.+Used by src/link/Qr.cpp for the phone link's pairing QR code.diff --git a/third_party/qrcodegen/qrcodegen.cpp b/third_party/qrcodegen/qrcodegen.cppnew file mode 100644index 00000000..0957b79b--- /dev/null+++ b/third_party/qrcodegen/qrcodegen.cpp@@ -0,0 +1,830 @@+/*+ * QR Code generator library (C++)+ *+ * Copyright (c) Project Nayuki. (MIT License)+ * https://www.nayuki.io/page/qr-code-generator-library+ *+ * Permission is hereby granted, free of charge, to any person obtaining a copy of+ * this software and associated documentation files (the "Software"), to deal in+ * the Software without restriction, including without limitation the rights to+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of+ * the Software, and to permit persons to whom the Software is furnished to do so,+ * subject to the following conditions:+ * - The above copyright notice and this permission notice shall be included in+ * all copies or substantial portions of the Software.+ * - The Software is provided "as is", without warranty of any kind, express or+ * implied, including but not limited to the warranties of merchantability,+ * fitness for a particular purpose and noninfringement. In no event shall the+ * authors or copyright holders be liable for any claim, damages or other+ * liability, whether in an action of contract, tort or otherwise, arising from,+ * out of or in connection with the Software or the use or other dealings in the+ * Software.+ */++#include <algorithm>+#include <cassert>+#include <climits>+#include <cstddef>+#include <cstdlib>+#include <cstring>+#include <sstream>+#include <utility>+#include "qrcodegen.hpp"++using std::int8_t;+using std::uint8_t;+using std::size_t;+using std::vector;+++namespace qrcodegen {++/*---- Class QrSegment ----*/++QrSegment::Mode::Mode(int mode, int cc0, int cc1, int cc2) :+ modeBits(mode) {+ numBitsCharCount[0] = cc0;+ numBitsCharCount[1] = cc1;+ numBitsCharCount[2] = cc2;+}+++int QrSegment::Mode::getModeBits() const {+ return modeBits;+}+++int QrSegment::Mode::numCharCountBits(int ver) const {+ return numBitsCharCount[(ver + 7) / 17];+}+++const QrSegment::Mode QrSegment::Mode::NUMERIC (0x1, 10, 12, 14);+const QrSegment::Mode QrSegment::Mode::ALPHANUMERIC(0x2, 9, 11, 13);+const QrSegment::Mode QrSegment::Mode::BYTE (0x4, 8, 16, 16);+const QrSegment::Mode QrSegment::Mode::KANJI (0x8, 8, 10, 12);+const QrSegment::Mode QrSegment::Mode::ECI (0x7, 0, 0, 0);+++QrSegment QrSegment::makeBytes(const vector<uint8_t> &data) {+ if (data.size() > static_cast<unsigned int>(INT_MAX))+ throw std::length_error("Data too long");+ BitBuffer bb;+ for (uint8_t b : data)+ bb.appendBits(b, 8);+ return QrSegment(Mode::BYTE, static_cast<int>(data.size()), std::move(bb));+}+++QrSegment QrSegment::makeNumeric(const char *digits) {+ BitBuffer bb;+ int accumData = 0;+ int accumCount = 0;+ int charCount = 0;+ for (; *digits != '\0'; digits++, charCount++) {+ char c = *digits;+ if (c < '0' || c > '9')+ throw std::domain_error("String contains non-numeric characters");+ accumData = accumData * 10 + (c - '0');+ accumCount++;+ if (accumCount == 3) {+ bb.appendBits(static_cast<uint32_t>(accumData), 10);+ accumData = 0;+ accumCount = 0;+ }+ }+ if (accumCount > 0) // 1 or 2 digits remaining+ bb.appendBits(static_cast<uint32_t>(accumData), accumCount * 3 + 1);+ return QrSegment(Mode::NUMERIC, charCount, std::move(bb));+}+++QrSegment QrSegment::makeAlphanumeric(const char *text) {+ BitBuffer bb;+ int accumData = 0;+ int accumCount = 0;+ int charCount = 0;+ for (; *text != '\0'; text++, charCount++) {+ const char *temp = std::strchr(ALPHANUMERIC_CHARSET, *text);+ if (temp == nullptr)+ throw std::domain_error("String contains unencodable characters in alphanumeric mode");+ accumData = accumData * 45 + static_cast<int>(temp - ALPHANUMERIC_CHARSET);+ accumCount++;+ if (accumCount == 2) {+ bb.appendBits(static_cast<uint32_t>(accumData), 11);+ accumData = 0;+ accumCount = 0;+ }+ }+ if (accumCount > 0) // 1 character remaining+ bb.appendBits(static_cast<uint32_t>(accumData), 6);+ return QrSegment(Mode::ALPHANUMERIC, charCount, std::move(bb));+}+++vector<QrSegment> QrSegment::makeSegments(const char *text) {+ // Select the most efficient segment encoding automatically+ vector<QrSegment> result;+ if (*text == '\0'); // Leave result empty+ else if (isNumeric(text))+ result.push_back(makeNumeric(text));+ else if (isAlphanumeric(text))+ result.push_back(makeAlphanumeric(text));+ else {+ vector<uint8_t> bytes;+ for (; *text != '\0'; text++)+ bytes.push_back(static_cast<uint8_t>(*text));+ result.push_back(makeBytes(bytes));+ }+ return result;+}+++QrSegment QrSegment::makeEci(long assignVal) {+ BitBuffer bb;+ if (assignVal < 0)+ throw std::domain_error("ECI assignment value out of range");+ else if (assignVal < (1 << 7))+ bb.appendBits(static_cast<uint32_t>(assignVal), 8);+ else if (assignVal < (1 << 14)) {+ bb.appendBits(2, 2);+ bb.appendBits(static_cast<uint32_t>(assignVal), 14);+ } else if (assignVal < 1000000L) {+ bb.appendBits(6, 3);+ bb.appendBits(static_cast<uint32_t>(assignVal), 21);+ } else+ throw std::domain_error("ECI assignment value out of range");+ return QrSegment(Mode::ECI, 0, std::move(bb));+}+++QrSegment::QrSegment(const Mode &md, int numCh, const std::vector<bool> &dt) :+ mode(&md),+ numChars(numCh),+ data(dt) {+ if (numCh < 0)+ throw std::domain_error("Invalid value");+}+++QrSegment::QrSegment(const Mode &md, int numCh, std::vector<bool> &&dt) :+ mode(&md),+ numChars(numCh),+ data(std::move(dt)) {+ if (numCh < 0)+ throw std::domain_error("Invalid value");+}+++int QrSegment::getTotalBits(const vector<QrSegment> &segs, int version) {+ int result = 0;+ for (const QrSegment &seg : segs) {+ int ccbits = seg.mode->numCharCountBits(version);+ if (seg.numChars >= (1L << ccbits))+ return -1; // The segment's length doesn't fit the field's bit width+ if (4 + ccbits > INT_MAX - result)+ return -1; // The sum will overflow an int type+ result += 4 + ccbits;+ if (seg.data.size() > static_cast<unsigned int>(INT_MAX - result))+ return -1; // The sum will overflow an int type+ result += static_cast<int>(seg.data.size());+ }+ return result;+}+++bool QrSegment::isNumeric(const char *text) {+ for (; *text != '\0'; text++) {+ char c = *text;+ if (c < '0' || c > '9')+ return false;+ }+ return true;+}+++bool QrSegment::isAlphanumeric(const char *text) {+ for (; *text != '\0'; text++) {+ if (std::strchr(ALPHANUMERIC_CHARSET, *text) == nullptr)+ return false;+ }+ return true;+}+++const QrSegment::Mode &QrSegment::getMode() const {+ return *mode;+}+++int QrSegment::getNumChars() const {+ return numChars;+}+++const std::vector<bool> &QrSegment::getData() const {+ return data;+}+++const char *QrSegment::ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";++++/*---- Class QrCode ----*/++int QrCode::getFormatBits(Ecc ecl) {+ switch (ecl) {+ case Ecc::LOW : return 1;+ case Ecc::MEDIUM : return 0;+ case Ecc::QUARTILE: return 3;+ case Ecc::HIGH : return 2;+ default: throw std::logic_error("Unreachable");+ }+}+++QrCode QrCode::encodeText(const char *text, Ecc ecl) {+ vector<QrSegment> segs = QrSegment::makeSegments(text);+ return encodeSegments(segs, ecl);+}+++QrCode QrCode::encodeBinary(const vector<uint8_t> &data, Ecc ecl) {+ vector<QrSegment> segs{QrSegment::makeBytes(data)};+ return encodeSegments(segs, ecl);+}+++QrCode QrCode::encodeSegments(const vector<QrSegment> &segs, Ecc ecl,+ int minVersion, int maxVersion, int mask, bool boostEcl) {+ if (!(MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= MAX_VERSION) || mask < -1 || mask > 7)+ throw std::invalid_argument("Invalid value");++ // Find the minimal version number to use+ int version, dataUsedBits;+ for (version = minVersion; ; version++) {+ int dataCapacityBits = getNumDataCodewords(version, ecl) * 8; // Number of data bits available+ dataUsedBits = QrSegment::getTotalBits(segs, version);+ if (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits)+ break; // This version number is found to be suitable+ if (version >= maxVersion) { // All versions in the range could not fit the given data+ std::ostringstream sb;+ if (dataUsedBits == -1)+ sb << "Segment too long";+ else {+ sb << "Data length = " << dataUsedBits << " bits, ";+ sb << "Max capacity = " << dataCapacityBits << " bits";+ }+ throw data_too_long(sb.str());+ }+ }+ assert(dataUsedBits != -1);++ // Increase the error correction level while the data still fits in the current version number+ for (Ecc newEcl : {Ecc::MEDIUM, Ecc::QUARTILE, Ecc::HIGH}) { // From low to high+ if (boostEcl && dataUsedBits <= getNumDataCodewords(version, newEcl) * 8)+ ecl = newEcl;+ }++ // Concatenate all segments to create the data bit string+ BitBuffer bb;+ for (const QrSegment &seg : segs) {+ bb.appendBits(static_cast<uint32_t>(seg.getMode().getModeBits()), 4);+ bb.appendBits(static_cast<uint32_t>(seg.getNumChars()), seg.getMode().numCharCountBits(version));+ bb.insert(bb.end(), seg.getData().begin(), seg.getData().end());+ }+ assert(bb.size() == static_cast<unsigned int>(dataUsedBits));++ // Add terminator and pad up to a byte if applicable+ size_t dataCapacityBits = static_cast<size_t>(getNumDataCodewords(version, ecl)) * 8;+ assert(bb.size() <= dataCapacityBits);+ bb.appendBits(0, std::min(4, static_cast<int>(dataCapacityBits - bb.size())));+ bb.appendBits(0, (8 - static_cast<int>(bb.size() % 8)) % 8);+ assert(bb.size() % 8 == 0);++ // Pad with alternating bytes until data capacity is reached+ for (uint8_t padByte = 0xEC; bb.size() < dataCapacityBits; padByte ^= 0xEC ^ 0x11)+ bb.appendBits(padByte, 8);++ // Pack bits into bytes in big endian+ vector<uint8_t> dataCodewords(bb.size() / 8);+ for (size_t i = 0; i < bb.size(); i++)+ dataCodewords.at(i >> 3) |= (bb.at(i) ? 1 : 0) << (7 - (i & 7));++ // Create the QR Code object+ return QrCode(version, ecl, dataCodewords, mask);+}+++QrCode::QrCode(int ver, Ecc ecl, const vector<uint8_t> &dataCodewords, int msk) :+ // Initialize fields and check arguments+ version(ver),+ errorCorrectionLevel(ecl) {+ if (ver < MIN_VERSION || ver > MAX_VERSION)+ throw std::domain_error("Version value out of range");+ if (msk < -1 || msk > 7)+ throw std::domain_error("Mask value out of range");+ size = ver * 4 + 17;+ size_t sz = static_cast<size_t>(size);+ modules = vector<vector<bool> >(sz, vector<bool>(sz)); // Initially all light+ isFunction = vector<vector<bool> >(sz, vector<bool>(sz));++ // Compute ECC, draw modules+ drawFunctionPatterns();+ const vector<uint8_t> allCodewords = addEccAndInterleave(dataCodewords);+ drawCodewords(allCodewords);++ // Do masking+ if (msk == -1) { // Automatically choose best mask+ long minPenalty = LONG_MAX;+ for (int i = 0; i < 8; i++) {+ applyMask(i);+ drawFormatBits(i);+ long penalty = getPenaltyScore();+ if (penalty < minPenalty) {+ msk = i;+ minPenalty = penalty;+ }+ applyMask(i); // Undoes the mask due to XOR+ }+ }+ assert(0 <= msk && msk <= 7);+ mask = msk;+ applyMask(msk); // Apply the final choice of mask+ drawFormatBits(msk); // Overwrite old format bits++ isFunction.clear();+ isFunction.shrink_to_fit();+}+++int QrCode::getVersion() const {+ return version;+}+++int QrCode::getSize() const {+ return size;+}+++QrCode::Ecc QrCode::getErrorCorrectionLevel() const {+ return errorCorrectionLevel;+}+++int QrCode::getMask() const {+ return mask;+}+++bool QrCode::getModule(int x, int y) const {+ return 0 <= x && x < size && 0 <= y && y < size && module(x, y);+}+++void QrCode::drawFunctionPatterns() {+ // Draw horizontal and vertical timing patterns+ for (int i = 0; i < size; i++) {+ setFunctionModule(6, i, i % 2 == 0);+ setFunctionModule(i, 6, i % 2 == 0);+ }++ // Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)+ drawFinderPattern(3, 3);+ drawFinderPattern(size - 4, 3);+ drawFinderPattern(3, size - 4);++ // Draw numerous alignment patterns+ const vector<int> alignPatPos = getAlignmentPatternPositions();+ size_t numAlign = alignPatPos.size();+ for (size_t i = 0; i < numAlign; i++) {+ for (size_t j = 0; j < numAlign; j++) {+ // Don't draw on the three finder corners+ if (!((i == 0 && j == 0) || (i == 0 && j == numAlign - 1) || (i == numAlign - 1 && j == 0)))+ drawAlignmentPattern(alignPatPos.at(i), alignPatPos.at(j));+ }+ }++ // Draw configuration data+ drawFormatBits(0); // Dummy mask value; overwritten later in the constructor+ drawVersion();+}+++void QrCode::drawFormatBits(int msk) {+ // Calculate error correction code and pack bits+ int data = getFormatBits(errorCorrectionLevel) << 3 | msk; // errCorrLvl is uint2, msk is uint3+ int rem = data;+ for (int i = 0; i < 10; i++)+ rem = (rem << 1) ^ ((rem >> 9) * 0x537);+ int bits = (data << 10 | rem) ^ 0x5412; // uint15+ assert(bits >> 15 == 0);++ // Draw first copy+ for (int i = 0; i <= 5; i++)+ setFunctionModule(8, i, getBit(bits, i));+ setFunctionModule(8, 7, getBit(bits, 6));+ setFunctionModule(8, 8, getBit(bits, 7));+ setFunctionModule(7, 8, getBit(bits, 8));+ for (int i = 9; i < 15; i++)+ setFunctionModule(14 - i, 8, getBit(bits, i));++ // Draw second copy+ for (int i = 0; i < 8; i++)+ setFunctionModule(size - 1 - i, 8, getBit(bits, i));+ for (int i = 8; i < 15; i++)+ setFunctionModule(8, size - 15 + i, getBit(bits, i));+ setFunctionModule(8, size - 8, true); // Always dark+}+++void QrCode::drawVersion() {+ if (version < 7)+ return;++ // Calculate error correction code and pack bits+ int rem = version; // version is uint6, in the range [7, 40]+ for (int i = 0; i < 12; i++)+ rem = (rem << 1) ^ ((rem >> 11) * 0x1F25);+ long bits = static_cast<long>(version) << 12 | rem; // uint18+ assert(bits >> 18 == 0);++ // Draw two copies+ for (int i = 0; i < 18; i++) {+ bool bit = getBit(bits, i);+ int a = size - 11 + i % 3;+ int b = i / 3;+ setFunctionModule(a, b, bit);+ setFunctionModule(b, a, bit);+ }+}+++void QrCode::drawFinderPattern(int x, int y) {+ for (int dy = -4; dy <= 4; dy++) {+ for (int dx = -4; dx <= 4; dx++) {+ int dist = std::max(std::abs(dx), std::abs(dy)); // Chebyshev/infinity norm+ int xx = x + dx, yy = y + dy;+ if (0 <= xx && xx < size && 0 <= yy && yy < size)+ setFunctionModule(xx, yy, dist != 2 && dist != 4);+ }+ }+}+++void QrCode::drawAlignmentPattern(int x, int y) {+ for (int dy = -2; dy <= 2; dy++) {+ for (int dx = -2; dx <= 2; dx++)+ setFunctionModule(x + dx, y + dy, std::max(std::abs(dx), std::abs(dy)) != 1);+ }+}+++void QrCode::setFunctionModule(int x, int y, bool isDark) {+ size_t ux = static_cast<size_t>(x);+ size_t uy = static_cast<size_t>(y);+ modules .at(uy).at(ux) = isDark;+ isFunction.at(uy).at(ux) = true;+}+++bool QrCode::module(int x, int y) const {+ return modules.at(static_cast<size_t>(y)).at(static_cast<size_t>(x));+}+++vector<uint8_t> QrCode::addEccAndInterleave(const vector<uint8_t> &data) const {+ if (data.size() != static_cast<unsigned int>(getNumDataCodewords(version, errorCorrectionLevel)))+ throw std::invalid_argument("Invalid argument");++ // Calculate parameter numbers+ int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[static_cast<int>(errorCorrectionLevel)][version];+ int blockEccLen = ECC_CODEWORDS_PER_BLOCK [static_cast<int>(errorCorrectionLevel)][version];+ int rawCodewords = getNumRawDataModules(version) / 8;+ int numShortBlocks = numBlocks - rawCodewords % numBlocks;+ int shortBlockLen = rawCodewords / numBlocks;++ // Split data into blocks and append ECC to each block+ vector<vector<uint8_t> > blocks;+ const vector<uint8_t> rsDiv = reedSolomonComputeDivisor(blockEccLen);+ for (int i = 0, k = 0; i < numBlocks; i++) {+ vector<uint8_t> dat(data.cbegin() + k, data.cbegin() + (k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1)));+ k += static_cast<int>(dat.size());+ const vector<uint8_t> ecc = reedSolomonComputeRemainder(dat, rsDiv);+ if (i < numShortBlocks)+ dat.push_back(0);+ dat.insert(dat.end(), ecc.cbegin(), ecc.cend());+ blocks.push_back(std::move(dat));+ }++ // Interleave (not concatenate) the bytes from every block into a single sequence+ vector<uint8_t> result;+ for (size_t i = 0; i < blocks.at(0).size(); i++) {+ for (size_t j = 0; j < blocks.size(); j++) {+ // Skip the padding byte in short blocks+ if (i != static_cast<unsigned int>(shortBlockLen - blockEccLen) || j >= static_cast<unsigned int>(numShortBlocks))+ result.push_back(blocks.at(j).at(i));+ }+ }+ assert(result.size() == static_cast<unsigned int>(rawCodewords));+ return result;+}+++void QrCode::drawCodewords(const vector<uint8_t> &data) {+ if (data.size() != static_cast<unsigned int>(getNumRawDataModules(version) / 8))+ throw std::invalid_argument("Invalid argument");++ size_t i = 0; // Bit index into the data+ // Do the funny zigzag scan+ for (int right = size - 1; right >= 1; right -= 2) { // Index of right column in each column pair+ if (right == 6)+ right = 5;+ for (int vert = 0; vert < size; vert++) { // Vertical counter+ for (int j = 0; j < 2; j++) {+ size_t x = static_cast<size_t>(right - j); // Actual x coordinate+ bool upward = ((right + 1) & 2) == 0;+ size_t y = static_cast<size_t>(upward ? size - 1 - vert : vert); // Actual y coordinate+ if (!isFunction.at(y).at(x) && i < data.size() * 8) {+ modules.at(y).at(x) = getBit(data.at(i >> 3), 7 - static_cast<int>(i & 7));+ i++;+ }+ // If this QR Code has any remainder bits (0 to 7), they were assigned as+ // 0/false/light by the constructor and are left unchanged by this method+ }+ }+ }+ assert(i == data.size() * 8);+}+++void QrCode::applyMask(int msk) {+ if (msk < 0 || msk > 7)+ throw std::domain_error("Mask value out of range");+ size_t sz = static_cast<size_t>(size);+ for (size_t y = 0; y < sz; y++) {+ for (size_t x = 0; x < sz; x++) {+ bool invert;+ switch (msk) {+ case 0: invert = (x + y) % 2 == 0; break;+ case 1: invert = y % 2 == 0; break;+ case 2: invert = x % 3 == 0; break;+ case 3: invert = (x + y) % 3 == 0; break;+ case 4: invert = (x / 3 + y / 2) % 2 == 0; break;+ case 5: invert = x * y % 2 + x * y % 3 == 0; break;+ case 6: invert = (x * y % 2 + x * y % 3) % 2 == 0; break;+ case 7: invert = ((x + y) % 2 + x * y % 3) % 2 == 0; break;+ default: throw std::logic_error("Unreachable");+ }+ modules.at(y).at(x) = modules.at(y).at(x) ^ (invert & !isFunction.at(y).at(x));+ }+ }+}+++long QrCode::getPenaltyScore() const {+ long result = 0;++ // Adjacent modules in row having same color, and finder-like patterns+ for (int y = 0; y < size; y++) {+ bool runColor = false;+ int runX = 0;+ std::array<int,7> runHistory = {};+ for (int x = 0; x < size; x++) {+ if (module(x, y) == runColor) {+ runX++;+ if (runX == 5)+ result += PENALTY_N1;+ else if (runX > 5)+ result++;+ } else {+ finderPenaltyAddHistory(runX, runHistory);+ if (!runColor)+ result += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;+ runColor = module(x, y);+ runX = 1;+ }+ }+ result += finderPenaltyTerminateAndCount(runColor, runX, runHistory) * PENALTY_N3;+ }+ // Adjacent modules in column having same color, and finder-like patterns+ for (int x = 0; x < size; x++) {+ bool runColor = false;+ int runY = 0;+ std::array<int,7> runHistory = {};+ for (int y = 0; y < size; y++) {+ if (module(x, y) == runColor) {+ runY++;+ if (runY == 5)+ result += PENALTY_N1;+ else if (runY > 5)+ result++;+ } else {+ finderPenaltyAddHistory(runY, runHistory);+ if (!runColor)+ result += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;+ runColor = module(x, y);+ runY = 1;+ }+ }+ result += finderPenaltyTerminateAndCount(runColor, runY, runHistory) * PENALTY_N3;+ }++ // 2*2 blocks of modules having same color+ for (int y = 0; y < size - 1; y++) {+ for (int x = 0; x < size - 1; x++) {+ bool color = module(x, y);+ if ( color == module(x + 1, y) &&+ color == module(x, y + 1) &&+ color == module(x + 1, y + 1))+ result += PENALTY_N2;+ }+ }++ // Balance of dark and light modules+ int dark = 0;+ for (const vector<bool> &row : modules) {+ for (bool color : row) {+ if (color)+ dark++;+ }+ }+ int total = size * size; // Note that size is odd, so dark/total != 1/2+ // Compute the smallest integer k >= 0 such that (45-5k)% <= dark/total <= (55+5k)%+ int k = static_cast<int>((std::abs(dark * 20L - total * 10L) + total - 1) / total) - 1;+ assert(0 <= k && k <= 9);+ result += k * PENALTY_N4;+ assert(0 <= result && result <= 2568888L); // Non-tight upper bound based on default values of PENALTY_N1, ..., N4+ return result;+}+++vector<int> QrCode::getAlignmentPatternPositions() const {+ if (version == 1)+ return vector<int>();+ else {+ int numAlign = version / 7 + 2;+ int step = (version == 32) ? 26 :+ (version * 4 + numAlign * 2 + 1) / (numAlign * 2 - 2) * 2;+ vector<int> result;+ for (int i = 0, pos = size - 7; i < numAlign - 1; i++, pos -= step)+ result.insert(result.begin(), pos);+ result.insert(result.begin(), 6);+ return result;+ }+}+++int QrCode::getNumRawDataModules(int ver) {+ if (ver < MIN_VERSION || ver > MAX_VERSION)+ throw std::domain_error("Version number out of range");+ int result = (16 * ver + 128) * ver + 64;+ if (ver >= 2) {+ int numAlign = ver / 7 + 2;+ result -= (25 * numAlign - 10) * numAlign - 55;+ if (ver >= 7)+ result -= 36;+ }+ assert(208 <= result && result <= 29648);+ return result;+}+++int QrCode::getNumDataCodewords(int ver, Ecc ecl) {+ return getNumRawDataModules(ver) / 8+ - ECC_CODEWORDS_PER_BLOCK [static_cast<int>(ecl)][ver]+ * NUM_ERROR_CORRECTION_BLOCKS[static_cast<int>(ecl)][ver];+}+++vector<uint8_t> QrCode::reedSolomonComputeDivisor(int degree) {+ if (degree < 1 || degree > 255)+ throw std::domain_error("Degree out of range");+ // Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.+ // For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array {255, 8, 93}.+ vector<uint8_t> result(static_cast<size_t>(degree));+ result.at(result.size() - 1) = 1; // Start off with the monomial x^0++ // Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),+ // and drop the highest monomial term which is always 1x^degree.+ // Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).+ uint8_t root = 1;+ for (int i = 0; i < degree; i++) {+ // Multiply the current product by (x - r^i)+ for (size_t j = 0; j < result.size(); j++) {+ result.at(j) = reedSolomonMultiply(result.at(j), root);+ if (j + 1 < result.size())+ result.at(j) ^= result.at(j + 1);+ }+ root = reedSolomonMultiply(root, 0x02);+ }+ return result;+}+++vector<uint8_t> QrCode::reedSolomonComputeRemainder(const vector<uint8_t> &data, const vector<uint8_t> &divisor) {+ vector<uint8_t> result(divisor.size());+ for (uint8_t b : data) { // Polynomial division+ uint8_t factor = b ^ result.at(0);+ result.erase(result.begin());+ result.push_back(0);+ for (size_t i = 0; i < result.size(); i++)+ result.at(i) ^= reedSolomonMultiply(divisor.at(i), factor);+ }+ return result;+}+++uint8_t QrCode::reedSolomonMultiply(uint8_t x, uint8_t y) {+ // Russian peasant multiplication+ int z = 0;+ for (int i = 7; i >= 0; i--) {+ z = (z << 1) ^ ((z >> 7) * 0x11D);+ z ^= ((y >> i) & 1) * x;+ }+ assert(z >> 8 == 0);+ return static_cast<uint8_t>(z);+}+++int QrCode::finderPenaltyCountPatterns(const std::array<int,7> &runHistory) const {+ int n = runHistory.at(1);+ assert(n <= size * 3);+ bool core = n > 0 && runHistory.at(2) == n && runHistory.at(3) == n * 3 && runHistory.at(4) == n && runHistory.at(5) == n;+ return (core && runHistory.at(0) >= n * 4 && runHistory.at(6) >= n ? 1 : 0)+ + (core && runHistory.at(6) >= n * 4 && runHistory.at(0) >= n ? 1 : 0);+}+++int QrCode::finderPenaltyTerminateAndCount(bool currentRunColor, int currentRunLength, std::array<int,7> &runHistory) const {+ if (currentRunColor) { // Terminate dark run+ finderPenaltyAddHistory(currentRunLength, runHistory);+ currentRunLength = 0;+ }+ currentRunLength += size; // Add light border to final run+ finderPenaltyAddHistory(currentRunLength, runHistory);+ return finderPenaltyCountPatterns(runHistory);+}+++void QrCode::finderPenaltyAddHistory(int currentRunLength, std::array<int,7> &runHistory) const {+ if (runHistory.at(0) == 0)+ currentRunLength += size; // Add light border to initial run+ std::copy_backward(runHistory.cbegin(), runHistory.cend() - 1, runHistory.end());+ runHistory.at(0) = currentRunLength;+}+++bool QrCode::getBit(long x, int i) {+ return ((x >> i) & 1) != 0;+}+++/*---- Tables of constants ----*/++const int QrCode::PENALTY_N1 = 3;+const int QrCode::PENALTY_N2 = 3;+const int QrCode::PENALTY_N3 = 40;+const int QrCode::PENALTY_N4 = 10;+++const int8_t QrCode::ECC_CODEWORDS_PER_BLOCK[4][41] = {+ // Version: (note that index 0 is for padding, and is set to an illegal value)+ //0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level+ {-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // Low+ {-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28}, // Medium+ {-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // Quartile+ {-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // High+};++const int8_t QrCode::NUM_ERROR_CORRECTION_BLOCKS[4][41] = {+ // Version: (note that index 0 is for padding, and is set to an illegal value)+ //0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level+ {-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25}, // Low+ {-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49}, // Medium+ {-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68}, // Quartile+ {-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81}, // High+};+++data_too_long::data_too_long(const std::string &msg) :+ std::length_error(msg) {}++++/*---- Class BitBuffer ----*/++BitBuffer::BitBuffer()+ : std::vector<bool>() {}+++void BitBuffer::appendBits(std::uint32_t val, int len) {+ if (len < 0 || len > 31 || val >> len != 0)+ throw std::domain_error("Value out of range");+ for (int i = len - 1; i >= 0; i--) // Append bit by bit+ this->push_back(((val >> i) & 1) != 0);+}++}diff --git a/third_party/qrcodegen/qrcodegen.hpp b/third_party/qrcodegen/qrcodegen.hppnew file mode 100644index 00000000..94489826--- /dev/null+++ b/third_party/qrcodegen/qrcodegen.hpp@@ -0,0 +1,549 @@+/*+ * QR Code generator library (C++)+ *+ * Copyright (c) Project Nayuki. (MIT License)+ * https://www.nayuki.io/page/qr-code-generator-library+ *+ * Permission is hereby granted, free of charge, to any person obtaining a copy of+ * this software and associated documentation files (the "Software"), to deal in+ * the Software without restriction, including without limitation the rights to+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of+ * the Software, and to permit persons to whom the Software is furnished to do so,+ * subject to the following conditions:+ * - The above copyright notice and this permission notice shall be included in+ * all copies or substantial portions of the Software.+ * - The Software is provided "as is", without warranty of any kind, express or+ * implied, including but not limited to the warranties of merchantability,+ * fitness for a particular purpose and noninfringement. In no event shall the+ * authors or copyright holders be liable for any claim, damages or other+ * liability, whether in an action of contract, tort or otherwise, arising from,+ * out of or in connection with the Software or the use or other dealings in the+ * Software.+ */++#pragma once++#include <array>+#include <cstdint>+#include <stdexcept>+#include <string>+#include <vector>+++namespace qrcodegen {++/*+ * A segment of character/binary/control data in a QR Code symbol.+ * Instances of this class are immutable.+ * The mid-level way to create a segment is to take the payload data+ * and call a static factory function such as QrSegment::makeNumeric().+ * The low-level way to create a segment is to custom-make the bit buffer+ * and call the QrSegment() constructor with appropriate values.+ * This segment class imposes no length restrictions, but QR Codes have restrictions.+ * Even in the most favorable conditions, a QR Code can only hold 7089 characters of data.+ * Any segment longer than this is meaningless for the purpose of generating QR Codes.+ */+class QrSegment final {++ /*---- Public helper enumeration ----*/++ /*+ * Describes how a segment's data bits are interpreted. Immutable.+ */+ public: class Mode final {++ /*-- Constants --*/++ public: static const Mode NUMERIC;+ public: static const Mode ALPHANUMERIC;+ public: static const Mode BYTE;+ public: static const Mode KANJI;+ public: static const Mode ECI;+++ /*-- Fields --*/++ // The mode indicator bits, which is a uint4 value (range 0 to 15).+ private: int modeBits;++ // Number of character count bits for three different version ranges.+ private: int numBitsCharCount[3];+++ /*-- Constructor --*/++ private: Mode(int mode, int cc0, int cc1, int cc2);+++ /*-- Methods --*/++ /*+ * (Package-private) Returns the mode indicator bits, which is an unsigned 4-bit value (range 0 to 15).+ */+ public: int getModeBits() const;++ /*+ * (Package-private) Returns the bit width of the character count field for a segment in+ * this mode in a QR Code at the given version number. The result is in the range [0, 16].+ */+ public: int numCharCountBits(int ver) const;++ };++++ /*---- Static factory functions (mid level) ----*/++ /*+ * Returns a segment representing the given binary data encoded in+ * byte mode. All input byte vectors are acceptable. Any text string+ * can be converted to UTF-8 bytes and encoded as a byte mode segment.+ */+ public: static QrSegment makeBytes(const std::vector<std::uint8_t> &data);+++ /*+ * Returns a segment representing the given string of decimal digits encoded in numeric mode.+ */+ public: static QrSegment makeNumeric(const char *digits);+++ /*+ * Returns a segment representing the given text string encoded in alphanumeric mode.+ * The characters allowed are: 0 to 9, A to Z (uppercase only), space,+ * dollar, percent, asterisk, plus, hyphen, period, slash, colon.+ */+ public: static QrSegment makeAlphanumeric(const char *text);+++ /*+ * Returns a list of zero or more segments to represent the given text string. The result+ * may use various segment modes and switch modes to optimize the length of the bit stream.+ */+ public: static std::vector<QrSegment> makeSegments(const char *text);+++ /*+ * Returns a segment representing an Extended Channel Interpretation+ * (ECI) designator with the given assignment value.+ */+ public: static QrSegment makeEci(long assignVal);+++ /*---- Public static helper functions ----*/++ /*+ * Tests whether the given string can be encoded as a segment in numeric mode.+ * A string is encodable iff each character is in the range 0 to 9.+ */+ public: static bool isNumeric(const char *text);+++ /*+ * Tests whether the given string can be encoded as a segment in alphanumeric mode.+ * A string is encodable iff each character is in the following set: 0 to 9, A to Z+ * (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.+ */+ public: static bool isAlphanumeric(const char *text);++++ /*---- Instance fields ----*/++ /* The mode indicator of this segment. Accessed through getMode(). */+ private: const Mode *mode;++ /* The length of this segment's unencoded data. Measured in characters for+ * numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.+ * Always zero or positive. Not the same as the data's bit length.+ * Accessed through getNumChars(). */+ private: int numChars;++ /* The data bits of this segment. Accessed through getData(). */+ private: std::vector<bool> data;+++ /*---- Constructors (low level) ----*/++ /*+ * Creates a new QR Code segment with the given attributes and data.+ * The character count (numCh) must agree with the mode and the bit buffer length,+ * but the constraint isn't checked. The given bit buffer is copied and stored.+ */+ public: QrSegment(const Mode &md, int numCh, const std::vector<bool> &dt);+++ /*+ * Creates a new QR Code segment with the given parameters and data.+ * The character count (numCh) must agree with the mode and the bit buffer length,+ * but the constraint isn't checked. The given bit buffer is moved and stored.+ */+ public: QrSegment(const Mode &md, int numCh, std::vector<bool> &&dt);+++ /*---- Methods ----*/++ /*+ * Returns the mode field of this segment.+ */+ public: const Mode &getMode() const;+++ /*+ * Returns the character count field of this segment.+ */+ public: int getNumChars() const;+++ /*+ * Returns the data bits of this segment.+ */+ public: const std::vector<bool> &getData() const;+++ // (Package-private) Calculates the number of bits needed to encode the given segments at+ // the given version. Returns a non-negative number if successful. Otherwise returns -1 if a+ // segment has too many characters to fit its length field, or the total bits exceeds INT_MAX.+ public: static int getTotalBits(const std::vector<QrSegment> &segs, int version);+++ /*---- Private constant ----*/++ /* The set of all legal characters in alphanumeric mode, where+ * each character value maps to the index in the string. */+ private: static const char *ALPHANUMERIC_CHARSET;++};++++/*+ * A QR Code symbol, which is a type of two-dimension barcode.+ * Invented by Denso Wave and described in the ISO/IEC 18004 standard.+ * Instances of this class represent an immutable square grid of dark and light cells.+ * The class provides static factory functions to create a QR Code from text or binary data.+ * The class covers the QR Code Model 2 specification, supporting all versions (sizes)+ * from 1 to 40, all 4 error correction levels, and 4 character encoding modes.+ *+ * Ways to create a QR Code object:+ * - High level: Take the payload data and call QrCode::encodeText() or QrCode::encodeBinary().+ * - Mid level: Custom-make the list of segments and call QrCode::encodeSegments().+ * - Low level: Custom-make the array of data codeword bytes (including+ * segment headers and final padding, excluding error correction codewords),+ * supply the appropriate version number, and call the QrCode() constructor.+ * (Note that all ways require supplying the desired error correction level.)+ */+class QrCode final {++ /*---- Public helper enumeration ----*/++ /*+ * The error correction level in a QR Code symbol.+ */+ public: enum class Ecc {+ LOW = 0 , // The QR Code can tolerate about 7% erroneous codewords+ MEDIUM , // The QR Code can tolerate about 15% erroneous codewords+ QUARTILE, // The QR Code can tolerate about 25% erroneous codewords+ HIGH , // The QR Code can tolerate about 30% erroneous codewords+ };+++ // Returns a value in the range 0 to 3 (unsigned 2-bit integer).+ private: static int getFormatBits(Ecc ecl);++++ /*---- Static factory functions (high level) ----*/++ /*+ * Returns a QR Code representing the given Unicode text string at the given error correction level.+ * As a conservative upper bound, this function is guaranteed to succeed for strings that have 2953 or fewer+ * UTF-8 code units (not Unicode code points) if the low error correction level is used. The smallest possible+ * QR Code version is automatically chosen for the output. The ECC level of the result may be higher than+ * the ecl argument if it can be done without increasing the version.+ */+ public: static QrCode encodeText(const char *text, Ecc ecl);+++ /*+ * Returns a QR Code representing the given binary data at the given error correction level.+ * This function always encodes using the binary segment mode, not any text mode. The maximum number of+ * bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.+ * The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.+ */+ public: static QrCode encodeBinary(const std::vector<std::uint8_t> &data, Ecc ecl);+++ /*---- Static factory functions (mid level) ----*/++ /*+ * Returns a QR Code representing the given segments with the given encoding parameters.+ * The smallest possible QR Code version within the given range is automatically+ * chosen for the output. Iff boostEcl is true, then the ECC level of the result+ * may be higher than the ecl argument if it can be done without increasing the+ * version. The mask number is either between 0 to 7 (inclusive) to force that+ * mask, or -1 to automatically choose an appropriate mask (which may be slow).+ * This function allows the user to create a custom sequence of segments that switches+ * between modes (such as alphanumeric and byte) to encode text in less space.+ * This is a mid-level API; the high-level API is encodeText() and encodeBinary().+ */+ public: static QrCode encodeSegments(const std::vector<QrSegment> &segs, Ecc ecl,+ int minVersion=1, int maxVersion=40, int mask=-1, bool boostEcl=true); // All optional parameters++++ /*---- Instance fields ----*/++ // Immutable scalar parameters:++ /* The version number of this QR Code, which is between 1 and 40 (inclusive).+ * This determines the size of this barcode. */+ private: int version;++ /* The width and height of this QR Code, measured in modules, between+ * 21 and 177 (inclusive). This is equal to version * 4 + 17. */+ private: int size;++ /* The error correction level used in this QR Code. */+ private: Ecc errorCorrectionLevel;++ /* The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive).+ * Even if a QR Code is created with automatic masking requested (mask = -1),+ * the resulting object still has a mask value between 0 and 7. */+ private: int mask;++ // Private grids of modules/pixels, with dimensions of size*size:++ // The modules of this QR Code (false = light, true = dark).+ // Immutable after constructor finishes. Accessed through getModule().+ private: std::vector<std::vector<bool> > modules;++ // Indicates function modules that are not subjected to masking. Discarded when constructor finishes.+ private: std::vector<std::vector<bool> > isFunction;++++ /*---- Constructor (low level) ----*/++ /*+ * Creates a new QR Code with the given version number,+ * error correction level, data codeword bytes, and mask number.+ * This is a low-level API that most users should not use directly.+ * A mid-level API is the encodeSegments() function.+ */+ public: QrCode(int ver, Ecc ecl, const std::vector<std::uint8_t> &dataCodewords, int msk);++++ /*---- Public instance methods ----*/++ /*+ * Returns this QR Code's version, in the range [1, 40].+ */+ public: int getVersion() const;+++ /*+ * Returns this QR Code's size, in the range [21, 177].+ */+ public: int getSize() const;+++ /*+ * Returns this QR Code's error correction level.+ */+ public: Ecc getErrorCorrectionLevel() const;+++ /*+ * Returns this QR Code's mask, in the range [0, 7].+ */+ public: int getMask() const;+++ /*+ * Returns the color of the module (pixel) at the given coordinates, which is false+ * for light or true for dark. The top left corner has the coordinates (x=0, y=0).+ * If the given coordinates are out of bounds, then false (light) is returned.+ */+ public: bool getModule(int x, int y) const;++++ /*---- Private helper methods for constructor: Drawing function modules ----*/++ // Reads this object's version field, and draws and marks all function modules.+ private: void drawFunctionPatterns();+++ // Draws two copies of the format bits (with its own error correction code)+ // based on the given mask and this object's error correction level field.+ private: void drawFormatBits(int msk);+++ // Draws two copies of the version bits (with its own error correction code),+ // based on this object's version field, iff 7 <= version <= 40.+ private: void drawVersion();+++ // Draws a 9*9 finder pattern including the border separator,+ // with the center module at (x, y). Modules can be out of bounds.+ private: void drawFinderPattern(int x, int y);+++ // Draws a 5*5 alignment pattern, with the center module+ // at (x, y). All modules must be in bounds.+ private: void drawAlignmentPattern(int x, int y);+++ // Sets the color of a module and marks it as a function module.+ // Only used by the constructor. Coordinates must be in bounds.+ private: void setFunctionModule(int x, int y, bool isDark);+++ // Returns the color of the module at the given coordinates, which must be in range.+ private: bool module(int x, int y) const;+++ /*---- Private helper methods for constructor: Codewords and masking ----*/++ // Returns a new byte string representing the given data with the appropriate error correction+ // codewords appended to it, based on this object's version and error correction level.+ private: std::vector<std::uint8_t> addEccAndInterleave(const std::vector<std::uint8_t> &data) const;+++ // Draws the given sequence of 8-bit codewords (data and error correction) onto the entire+ // data area of this QR Code. Function modules need to be marked off before this is called.+ private: void drawCodewords(const std::vector<std::uint8_t> &data);+++ // XORs the codeword modules in this QR Code with the given mask pattern.+ // The function modules must be marked and the codeword bits must be drawn+ // before masking. Due to the arithmetic of XOR, calling applyMask() with+ // the same mask value a second time will undo the mask. A final well-formed+ // QR Code needs exactly one (not zero, two, etc.) mask applied.+ private: void applyMask(int msk);+++ // Calculates and returns the penalty score based on state of this QR Code's current modules.+ // This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.+ private: long getPenaltyScore() const;++++ /*---- Private helper functions ----*/++ // Returns an ascending list of positions of alignment patterns for this version number.+ // Each position is in the range [0,177), and are used on both the x and y axes.+ // This could be implemented as lookup table of 40 variable-length lists of unsigned bytes.+ private: std::vector<int> getAlignmentPatternPositions() const;+++ // Returns the number of data bits that can be stored in a QR Code of the given version number, after+ // all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.+ // The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.+ private: static int getNumRawDataModules(int ver);+++ // Returns the number of 8-bit data (i.e. not error correction) codewords contained in any+ // QR Code of the given version number and error correction level, with remainder bits discarded.+ // This stateless pure function could be implemented as a (40*4)-cell lookup table.+ private: static int getNumDataCodewords(int ver, Ecc ecl);+++ // Returns a Reed-Solomon ECC generator polynomial for the given degree. This could be+ // implemented as a lookup table over all possible parameter values, instead of as an algorithm.+ private: static std::vector<std::uint8_t> reedSolomonComputeDivisor(int degree);+++ // Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials.+ private: static std::vector<std::uint8_t> reedSolomonComputeRemainder(const std::vector<std::uint8_t> &data, const std::vector<std::uint8_t> &divisor);+++ // Returns the product of the two given field elements modulo GF(2^8/0x11D).+ // All inputs are valid. This could be implemented as a 256*256 lookup table.+ private: static std::uint8_t reedSolomonMultiply(std::uint8_t x, std::uint8_t y);+++ // Can only be called immediately after a light run is added, and+ // returns either 0, 1, or 2. A helper function for getPenaltyScore().+ private: int finderPenaltyCountPatterns(const std::array<int,7> &runHistory) const;+++ // Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore().+ private: int finderPenaltyTerminateAndCount(bool currentRunColor, int currentRunLength, std::array<int,7> &runHistory) const;+++ // Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore().+ private: void finderPenaltyAddHistory(int currentRunLength, std::array<int,7> &runHistory) const;+++ // Returns true iff the i'th bit of x is set to 1.+ private: static bool getBit(long x, int i);+++ /*---- Constants and tables ----*/++ // The minimum version number supported in the QR Code Model 2 standard.+ public: static constexpr int MIN_VERSION = 1;++ // The maximum version number supported in the QR Code Model 2 standard.+ public: static constexpr int MAX_VERSION = 40;+++ // For use in getPenaltyScore(), when evaluating which mask is best.+ private: static const int PENALTY_N1;+ private: static const int PENALTY_N2;+ private: static const int PENALTY_N3;+ private: static const int PENALTY_N4;+++ private: static const std::int8_t ECC_CODEWORDS_PER_BLOCK[4][41];+ private: static const std::int8_t NUM_ERROR_CORRECTION_BLOCKS[4][41];++};++++/*---- Public exception class ----*/++/*+ * Thrown when the supplied data does not fit any QR Code version. Ways to handle this exception include:+ * - Decrease the error correction level if it was greater than Ecc::LOW.+ * - If the encodeSegments() function was called with a maxVersion argument, then increase+ * it if it was less than QrCode::MAX_VERSION. (This advice does not apply to the other+ * factory functions because they search all versions up to QrCode::MAX_VERSION.)+ * - Split the text data into better or optimal segments in order to reduce the number of bits required.+ * - Change the text or binary data to be shorter.+ * - Change the text to fit the character set of a particular segment mode (e.g. alphanumeric).+ * - Propagate the error upward to the caller/user.+ */+class data_too_long : public std::length_error {++ public: explicit data_too_long(const std::string &msg);++};++++/*+ * An appendable sequence of bits (0s and 1s). Mainly used by QrSegment.+ */+class BitBuffer final : public std::vector<bool> {++ /*---- Constructor ----*/++ // Creates an empty bit buffer (length 0).+ public: BitBuffer();++++ /*---- Method ----*/++ // Appends the given number of low-order bits of the given value+ // to this buffer. Requires 0 <= len <= 31 and val < 2^len.+ public: void appendBits(std::uint32_t val, int len);++};++}diff --git a/website/link/index.html b/website/link/index.htmlnew file mode 100644index 00000000..6fbc3f3c--- /dev/null+++ b/website/link/index.html@@ -0,0 +1,266 @@+<!doctype html>+<!-- SculptForge face capture (docs/phone-link.md). Served over HTTPS from the SculptForge website because phone+ browsers only allow the camera on secure pages. MediaPipe Face Landmarker (Apache-2.0) runs here, on the phone;+ only the 52 ARKit expression weights and the head rotation go to the computer, over a WebRTC data channel on+ the local network. The connection details arrive in this URL's #fragment, which browsers never send to the+ website. The same page is served by SculptForge itself at /face (it then posts frames over HTTP: for+ localhost, e.g. Android with `adb reverse`, and tests). GPL-3.0-or-later. -->+<html lang="en">+<head>+<meta charset="utf-8">+<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">+<meta name="theme-color" content="#0f0f12">+<meta name="color-scheme" content="dark">+<meta name="robots" content="noindex">+<title>SculptForge face capture</title>+<style>+:root { --bg:#0f0f12; --bg2:#15151a; --panel:#1b1b21; --panel2:#22222a; --border:#2a2a31; --text:#ececf1; --muted:#a3a3ad;+ --accent:#f28c3c; --accent2:#ffb06a; --ok:#4ad295; --bad:#ff6b6b; }+* { box-sizing:border-box; -webkit-tap-highlight-color:transparent; }+html,body { margin:0; background:var(--bg); color:var(--text); }+body { font:400 16px/1.5 -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif; -webkit-font-smoothing:antialiased;+ min-height:100vh; padding:env(safe-area-inset-top) 0 env(safe-area-inset-bottom); }+header { display:flex; align-items:center; gap:10px; padding:14px 18px; border-bottom:1px solid var(--border); }+header .brand { font-weight:650; flex:1; display:flex; gap:9px; align-items:center; }+.pill { font-size:13px; padding:4px 10px 4px 8px; border-radius:999px; background:var(--panel); border:1px solid var(--border);+ color:var(--muted); display:flex; align-items:center; gap:6px; white-space:nowrap; max-width:58vw; overflow:hidden; text-overflow:ellipsis; }+.dot { width:8px; height:8px; border-radius:50%; background:var(--muted); flex:none; }+.pill.ok .dot { background:var(--ok); box-shadow:0 0 8px var(--ok); } .pill.bad .dot { background:var(--bad); } .pill.wait .dot { background:#ffcf5a; }+main { padding:16px 18px; max-width:560px; margin:0 auto; }+h1 { font-size:26px; line-height:1.15; margin:4px 0 8px; letter-spacing:-.02em; }+p { margin:0 0 12px; } .muted { color:var(--muted); } .small { font-size:14px; }+.stage { position:relative; border-radius:18px; overflow:hidden; background:#000; aspect-ratio:3/4; border:1px solid var(--border); }+.stage video, .stage canvas { position:absolute; inset:0; width:100%; height:100%; object-fit:cover; transform:scaleX(-1); }+.stage .hud { position:absolute; left:10px; top:10px; right:10px; display:flex; justify-content:space-between; font-size:13px; }+.stage .hud span { background:rgba(0,0,0,.55); padding:3px 9px; border-radius:999px; }+.meters { display:grid; grid-template-columns:1fr 1fr; gap:8px 14px; margin:14px 0; }+.meter { font-size:13px; color:var(--muted); }+.meter i { display:block; height:6px; border-radius:99px; background:var(--bg2); margin-top:3px; overflow:hidden; }+.meter i b { display:block; height:100%; width:0; background:linear-gradient(90deg,#e2742a,#ffb06a); }+button.primary, a.primary { display:flex; align-items:center; justify-content:center; width:100%; min-height:52px; border:none; border-radius:14px;+ font:600 17px/1 inherit; font-family:inherit; color:#1a0d04; background:linear-gradient(180deg,#f6994d,#e2742a); margin-top:10px; cursor:pointer; text-decoration:none; }+button.secondary, a.secondary { display:flex; align-items:center; justify-content:center; width:100%; min-height:48px; border:1px solid var(--border);+ border-radius:14px; font:600 16px/1 inherit; font-family:inherit; color:var(--text); background:var(--panel2); margin-top:10px; cursor:pointer; text-decoration:none; }+.note { border-radius:12px; padding:11px 13px; font-size:14px; margin:10px 0; background:rgba(255,207,90,.08); border:1px solid rgba(255,207,90,.25); color:#ffe2a0; }+.note.bad { background:rgba(255,107,107,.08); border-color:rgba(255,107,107,.3); color:#ffc2c2; }+.hidden { display:none !important; }+footer { text-align:center; color:#6f6f78; font-size:12px; padding:20px 18px 30px; }+</style>+</head>+<body>+<header>+ <div class="brand"><svg width="26" height="26" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 2.5l8.5 4.8v9.4L12 21.5l-8.5-4.8V7.3z" fill="none" stroke="#f28c3c" stroke-width="1.6" stroke-linejoin="round"/><path d="M3.8 7.4L12 12l8.2-4.6M12 12v9.2" fill="none" stroke="#ffb06a" stroke-width="1.6" stroke-linejoin="round"/></svg>Face capture</div>+ <div id="conn" class="pill"><span class="dot"></span><span id="connText">Not connected</span></div>+</header>+<main>+ <section id="intro">+ <h1>Drive a face with yours</h1>+ <p class="muted" id="introText">Prop the phone up at eye level, light on your face, then start. The video stays on this phone.</p>+ <button class="primary" id="start">Start the camera</button>+ <div id="msg"></div>+ </section>+ <section id="live" class="hidden">+ <div class="stage"><video id="video" playsinline muted autoplay></video><canvas id="overlay"></canvas>+ <div class="hud"><span id="fps">-- fps</span><span id="track">Looking for a face...</span></div></div>+ <div class="meters" id="meters"></div>+ <div id="liveMsg"></div>+ <button class="secondary hidden" id="direct">Connect without internet</button>+ <button class="secondary" id="pause">Pause</button>+ </section>+ <a class="secondary hidden" id="back" href="#">Back to capture</a>+</main>+<footer>Face tracking: MediaPipe Face Landmarker (Apache-2.0), running on this phone. Only 52 numbers and a rotation per frame are sent, to your computer.</footer>++<script type="module">+"use strict";+const MP_VERSION = "1.1.0";+const MP_BASE = `https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@${MP_VERSION}`;+const MODEL = "https://storage.googleapis.com/mediapipe-models/face_landmarker/face_landmarker/float16/1/face_landmarker.task";+const $ = (id) => document.getElementById(id);+const esc = (s) => String(s).replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ }[c]));+const note = (el, text, kind) => { el.innerHTML = text ? `<div class="note ${kind || ""}">${text}</div>` : ""; };+const setConn = (state, text) => { $("conn").className = "pill " + state; $("connText").textContent = text; };++// Connection details: #v=1&k=token&d=desktop&o=offer&n=name&x=key&r=relay (from the desktop's capture page), or+// ?k=token when SculptForge serves this page itself at /face.+const frag = new URLSearchParams(location.hash.slice(1));+const query = new URLSearchParams(location.search);+const served = location.pathname.endsWith("/face") && query.get("k"); // from the desktop: HTTP transport+const token = served ? query.get("k") : frag.get("k");+const desktop = served ? location.origin : frag.get("d");+const desktopName = frag.get("n") || "your computer";+history.replaceState(null, "", location.pathname + (served ? location.search : "")); // keep the secrets out of history++if (desktop && token) {+ $("back").href = `${desktop}/?k=${encodeURIComponent(token)}`;+ $("back").classList.remove("hidden");+}+if (!token || (!served && !frag.get("o"))) {+ $("introText").textContent = "Open this page from SculptForge: File > Capture from phone on your computer, scan the QR code, then tap Face capture.";+ $("start").classList.add("hidden");+}+if (!window.isSecureContext || !navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {+ note($("msg"), "This browser only allows the camera on secure (https) pages. Open face capture from the capture page's Face capture button.", "bad");+ $("start").disabled = true;+}++const b64u = {+ enc: (bytes) => btoa(String.fromCharCode(...new Uint8Array(bytes))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""),+ dec: (text) => Uint8Array.from(atob(text.replace(/-/g, "+").replace(/_/g, "/") + "===".slice((text.length + 3) % 4)), (c) => c.charCodeAt(0)),+ str: (text) => new TextDecoder().decode(b64u.dec(text)),+};++// --- Transport ---+let send = () => false;+let pc = null, channel = null, answerSdp = "";++async function connectHttp() {+ // Frames are posted to the page's own computer; one request in flight at a time, the newest frame wins.+ let pending = null, inFlight = false;+ const pump = async () => {+ if (inFlight || !pending) return;+ inFlight = true;+ const body = pending; pending = null;+ try {+ const r = await fetch("/api/face", { method: "POST", headers: { "X-SF-Token": token, "Content-Type": "application/json" }, body });+ setConn(r.ok ? "ok" : "bad", r.ok ? "Sending" : "Refused");+ } catch (e) { setConn("bad", "Not connected"); }+ inFlight = false;+ pump();+ };+ send = (text) => { pending = text; pump(); return true; };+ setConn("ok", "Wi-Fi");+}++async function connectRtc() {+ setConn("wait", "Connecting...");+ pc = new RTCPeerConnection({ iceServers: [] });+ channel = pc.createDataChannel("face", { negotiated: true, id: 0, ordered: false, maxRetransmits: 0 });+ channel.onopen = () => { setConn("ok", "Connected to " + desktopName); $("direct").classList.add("hidden"); note($("liveMsg"), ""); };+ channel.onclose = () => setConn("bad", "Disconnected");+ pc.onconnectionstatechange = () => {+ if (pc.connectionState === "failed") {+ setConn("bad", "Couldn't connect");+ note($("liveMsg"), "The phone couldn't reach your computer over Wi-Fi. Both must be on the same network (no guest Wi-Fi, no VPN), and the computer's firewall must allow SculptForge.", "bad");+ }+ };+ await pc.setRemoteDescription({ type: "offer", sdp: b64u.str(frag.get("o")) });+ await pc.setLocalDescription(await pc.createAnswer());+ await new Promise((resolve) => {+ if (pc.iceGatheringState === "complete") return resolve();+ const done = () => { if (pc.iceGatheringState === "complete") resolve(); };+ pc.addEventListener("icegatheringstatechange", done);+ setTimeout(resolve, 2500); // what we have by then is enough on a local network+ });+ answerSdp = pc.localDescription.sdp;+ send = (text) => {+ if (!channel || channel.readyState !== "open" || channel.bufferedAmount > 32768) return false;+ channel.send(text);+ return true;+ };+ // The answer goes back through the relay, encrypted with the key from the fragment (the relay can't read it).+ const relay = frag.get("r");+ if (relay) {+ try {+ const key = await crypto.subtle.importKey("raw", b64u.dec(frag.get("x")), "AES-GCM", false, ["encrypt"]);+ const iv = crypto.getRandomValues(new Uint8Array(12));+ const plain = new TextEncoder().encode(JSON.stringify({ k: token, sdp: answerSdp }));+ const cipher = new Uint8Array(await crypto.subtle.encrypt({ name: "AES-GCM", iv }, key, plain));+ const packed = new Uint8Array(iv.length + cipher.length);+ packed.set(iv); packed.set(cipher, iv.length);+ await fetch(relay, { method: "POST", body: b64u.enc(packed) });+ } catch (e) {+ note($("liveMsg"), "No internet to set up the connection: tap Connect without internet.", "");+ }+ }+ // Without internet (or if the relay is slow): a tab on the computer's own page delivers the answer.+ setTimeout(() => { if (!channel || channel.readyState !== "open") $("direct").classList.remove("hidden"); }, relay ? 6000 : 0);+}++$("direct").onclick = () => {+ const url = `${desktop}/rtc-answer?k=${encodeURIComponent(token)}&a=${b64u.enc(new TextEncoder().encode(answerSdp))}`;+ window.open(url, "_blank");+};++// --- Tracking ---+let landmarker = null, video = $("video"), running = false, paused = false, lastSent = 0, frames = 0, fpsT = performance.now();+const SHOWN = [["jawOpen", "Jaw open"], ["mouthSmileLeft", "Smile L"], ["mouthSmileRight", "Smile R"], ["eyeBlinkLeft", "Blink L"],+ ["eyeBlinkRight", "Blink R"], ["browInnerUp", "Brows up"], ["mouthPucker", "Pucker"], ["cheekPuff", "Cheek puff"]];+$("meters").innerHTML = SHOWN.map(([k, label]) => `<div class="meter">${label}<i><b id="m_${k}"></b></i></div>`).join("");++// Rotation part of MediaPipe's column-major 4x4 face transform, as [x, y, z, w].+function quatFromMatrix(m) {+ const m00 = m[0], m10 = m[1], m20 = m[2], m01 = m[4], m11 = m[5], m21 = m[6], m02 = m[8], m12 = m[9], m22 = m[10];+ const sx = Math.hypot(m00, m10, m20) || 1, sy = Math.hypot(m01, m11, m21) || 1, sz = Math.hypot(m02, m12, m22) || 1;+ const a = m00 / sx, b = m01 / sy, c = m02 / sz, d = m10 / sx, e = m11 / sy, f = m12 / sz, g = m20 / sx, h = m21 / sy, i = m22 / sz;+ const tr = a + e + i;+ let x, y, z, w;+ if (tr > 0) { const s = Math.sqrt(tr + 1) * 2; w = s / 4; x = (h - f) / s; y = (c - g) / s; z = (d - b) / s; }+ else if (a > e && a > i) { const s = Math.sqrt(1 + a - e - i) * 2; w = (h - f) / s; x = s / 4; y = (b + d) / s; z = (c + g) / s; }+ else if (e > i) { const s = Math.sqrt(1 + e - a - i) * 2; w = (c - g) / s; x = (b + d) / s; y = s / 4; z = (f + h) / s; }+ else { const s = Math.sqrt(1 + i - a - e) * 2; w = (d - b) / s; x = (c + g) / s; y = (f + h) / s; z = s / 4; }+ return [x, y, z, w].map((v) => Math.round(v * 1e5) / 1e5);+}++function tick() {+ if (!running) return;+ const now = performance.now();+ if (!paused && video.readyState >= 2 && now - lastSent >= 30) { // about 30 frames a second+ const result = landmarker.detectForVideo(video, now);+ const shapes = result.faceBlendshapes && result.faceBlendshapes[0];+ if (shapes) {+ const weights = {};+ for (const c of shapes.categories) if (c.categoryName !== "_neutral") weights[c.categoryName] = Math.round(c.score * 1e4) / 1e4;+ const msg = { type: "face", t: Math.round(now * 10) / 10, weights };+ const m = result.facialTransformationMatrixes && result.facialTransformationMatrixes[0];+ if (m && m.data && m.data.length >= 16) msg.head = quatFromMatrix(m.data);+ send(JSON.stringify(msg));+ lastSent = now;+ frames++;+ $("track").textContent = "Tracking";+ for (const [k] of SHOWN) { const el = $("m_" + k); if (el) el.style.width = ((weights[k] || 0) * 100).toFixed(0) + "%"; }+ } else {+ $("track").textContent = "Looking for a face...";+ }+ }+ if (now - fpsT > 1000) { $("fps").textContent = frames + " fps"; frames = 0; fpsT = now; }+ (video.requestVideoFrameCallback ? video.requestVideoFrameCallback.bind(video) : requestAnimationFrame)(tick);+}++$("start").onclick = async () => {+ $("start").disabled = true;+ note($("msg"), "Starting the camera...", "");+ try {+ // The camera first: browsers then also share this phone's local address, which helps WebRTC.+ const stream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: "user", width: { ideal: 640 }, height: { ideal: 480 } }, audio: false });+ video.srcObject = stream;+ await video.play();+ note($("msg"), "Loading face tracking (about 5 MB, once)...", "");+ const { FaceLandmarker, FilesetResolver } = await import(`${MP_BASE}/vision_bundle.mjs`);+ const files = await FilesetResolver.forVisionTasks(`${MP_BASE}/wasm`);+ const options = (delegate) => ({ baseOptions: { modelAssetPath: MODEL, delegate }, runningMode: "VIDEO", numFaces: 1,+ outputFaceBlendshapes: true, outputFacialTransformationMatrixes: true });+ try { landmarker = await FaceLandmarker.createFromOptions(files, options("GPU")); }+ catch (e) { landmarker = await FaceLandmarker.createFromOptions(files, options("CPU")); }+ $("intro").classList.add("hidden");+ $("live").classList.remove("hidden");+ if (served) await connectHttp(); else await connectRtc();+ running = true;+ tick();+ if (navigator.wakeLock) { try { await navigator.wakeLock.request("screen"); } catch (e) {} }+ } catch (e) {+ $("start").disabled = false;+ const denied = e && (e.name === "NotAllowedError" || e.name === "SecurityError");+ note($("msg"), denied ? "Camera access was refused. Allow the camera for this page in the browser's settings and try again."+ : "Couldn't start: " + esc(e.message || e), "bad");+ }+};+$("pause").onclick = () => {+ paused = !paused;+ $("pause").textContent = paused ? "Resume" : "Pause";+ $("track").textContent = paused ? "Paused" : "Tracking";+};+window.addEventListener("pagehide", () => { try { send(JSON.stringify({ type: "bye" })); } catch (e) {} });+</script>+</body>+</html>