Something went wrong. Try again.
Builds CAD models from 3d scan data
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105#include "history.hpp"#include <cstdio>#include <limits>
namespace {int failures = 0;void check(const char* name, bool passed) { std::printf("%-48s %s\n", name, passed ? "PASS" : "FAIL"); if (!passed) ++failures;}struct Document { history::Verts verts{{0, 0, 0}, {1, 0, 0}, {0, 1, 0}}; history::Tris tris{{0, 1, 2}}; history::Verts cloud{{2, 3, 4}}; std::vector<int> sel{1}; std::vector<nlohmann::json> prims{{{"id", 1}}}; std::unordered_map<int, surface::Fit> surfaces; align::Mat3 R{1, 0, 0, 0, 1, 0, 0, 0, 1}; geom::Vec3 T{3, 4, 5}; int next = 2; nlohmann::json ui{{"step", "load"}}; void commit(history::History& h, const std::string& label) { h.commit(label, "step", "detail", verts, tris, cloud, sel, prims, surfaces, R, T, next, ui); }};}int main() { history::History h(1024 * 1024); Document d; check("empty undo/redo/goto", !h.undo() && !h.redo() && !h.go(0) && h.cursor() == -1); d.commit(h, "one"); check("undo at zero", !h.undo() && h.cursor() == 0); d.verts[0].x = 2; d.commit(h, "two"); d.verts[0].x = 3; d.commit(h, "three"); const auto* s = h.undo(); check("first undo", s && s->verts->at(0).x == 2 && h.cursor() == 1); s = h.undo(); check("second undo", s && s->verts->at(0).x == 0 && h.cursor() == 0); s = h.redo(); check("redo", s && s->verts->at(0).x == 2 && h.cursor() == 1); d.verts[0].x = 4; d.commit(h, "branch"); check("commit truncates redo", !h.canRedo() && h.entries().size() == 3 && h.entries().back().label == "branch" && h.dropped() == 0); check("go backward", h.go(0) && h.cursor() == 0); check("go forward", h.go(2) && h.cursor() == 2); check("invalid go preserves cursor", !h.go(-1) && !h.go(3) && h.cursor() == 2); h.reset(); surface::Fit f; f.poles = {{1, 2, 3}}; f.knotsU = {0, 1}; f.knotsV = {0, 1}; f.preview.positions = {{4, 5, 6}}; f.preview.deviations = {std::numeric_limits<double>::quiet_NaN()}; d.surfaces[1] = f; d.commit(h, "base"); auto base = h.entries()[0].snap; auto baseBytes = h.bytes(); auto expected = d.verts.size() * sizeof(geom::Vec3) + d.tris.size() * 12 + d.cloud.size() * sizeof(geom::Vec3) + d.sel.size() * 4 + d.prims[0].dump().size() + 2 * sizeof(geom::Vec3) + 5 * sizeof(double) + d.ui.dump().size(); check("byte accounting includes all vectors", baseBytes == expected); d.commit(h, "unchanged"); auto same = h.entries().back().snap; check("unchanged buffers share pointers", same.verts == base.verts && same.tris == base.tris && same.cloud == base.cloud && same.selVerts == base.selVerts && same.prims == base.prims && same.surfaces == base.surfaces); check("unchanged commit only adds UI bytes", h.bytes() == baseBytes + d.ui.dump().size()); d.verts[0].x += 1; d.commit(h, "moved"); auto moved = h.entries().back().snap; check("changed verts share only unchanged buffers", moved.verts != same.verts && moved.tris == same.tris && moved.cloud == same.cloud); d.cloud[0].x += 1; d.sel.push_back(2); d.prims[0]["id"] = 2; d.surfaces[1].preview.deviations[0] = 0.5; d.next = 3; d.T.x = 9; d.R[0] = -1; d.ui = {{"step", "fit"}}; d.commit(h, "document"); auto full = h.entries().back().snap; check("all changed buffers captured", full.cloud != moved.cloud && full.selVerts != moved.selVerts && full.prims != moved.prims && full.surfaces != moved.surfaces); check("frame ids and UI captured", full.nextPrimId == 3 && full.frameT.x == 9 && full.frameR[0] == -1 && full.ui == d.ui); check("old snapshot immutable", base.cloud->at(0).x == 2 && base.selVerts->size() == 1 && base.prims->at(0)["id"] == 1 && base.nextPrimId == 2 && base.frameT.x == 3); auto meta = h.meta(); check("metadata", meta["cursor"] == 3 && meta["bytes"] == h.bytes() && meta["budget"] == h.budget() && meta["canUndo"] == true && meta["canRedo"] == false && meta["entries"][3] == nlohmann::json({{"label", "document"}, {"step", "step"}, {"detail", "detail"}})); history::History bounded(baseBytes + d.verts.size() * sizeof(geom::Vec3)); for (int i = 0; i < 6; ++i) { d.verts[0].x = i; d.commit(bounded, std::to_string(i)); } check("budget evicts oldest and retains cursor", bounded.dropped() > 0 && bounded.dropped() + static_cast<int>(bounded.entries().size()) == 6 && bounded.cursor() == static_cast<int>(bounded.entries().size()) - 1 && bounded.entries().back().label == "5" && bounded.bytes() <= bounded.budget()); history::History tiny(0); d.commit(tiny, "a"); d.commit(tiny, "b"); check("oversized cursor retained", tiny.entries().size() == 1 && tiny.cursor() == 0 && tiny.dropped() == 1 && tiny.bytes() > tiny.budget()); tiny.reset(); check("reset clears entries bytes cursor dropped", tiny.entries().empty() && tiny.bytes() == 0 && tiny.cursor() == -1 && tiny.dropped() == 0 && tiny.budget() == 0); std::printf("\n%d failure(s)\n", failures); return failures ? 1 : 0;}