#include "history.hpp" #include #include #include namespace history { namespace { template bool equal(const T& a, const T& b) { return a == b; } bool equal(double a, double b) { return a == b || (std::isnan(a) && std::isnan(b)); } bool equal(const geom::Vec3& a, const geom::Vec3& b) { return equal(a.x, b.x) && equal(a.y, b.y) && equal(a.z, b.z); } template bool equal(const std::vector& a, const std::vector& b) { return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin(), [](const T& x, const T& y) { return equal(x, y); }); } bool equal(const surface::Fit& a, const surface::Fit& b) { return a.ok == b.ok && a.error == b.error && a.nu == b.nu && a.nv == b.nv && a.degree == b.degree && a.npts == b.npts && equal(a.rms, b.rms) && equal(a.max, b.max) && equal(a.pctWithin, b.pctWithin) && equal(a.extend, b.extend) && equal(a.lambda, b.lambda) && equal(a.poles, b.poles) && equal(a.knotsU, b.knotsU) && equal(a.knotsV, b.knotsV) && equal(a.rectOrigin, b.rectOrigin) && equal(a.axisU, b.axisU) && equal(a.axisV, b.axisV) && equal(a.planeNormal, b.planeNormal) && equal(a.dataWidth, b.dataWidth) && equal(a.dataHeight, b.dataHeight) && a.preview.nu == b.preview.nu && a.preview.nv == b.preview.nv && equal(a.preview.positions, b.preview.positions) && equal(a.preview.deviations, b.preview.deviations); } bool equal(const std::unordered_map& a, const std::unordered_map& b) { if (a.size() != b.size()) return false; for (const auto& item : a) { auto it = b.find(item.first); if (it == b.end() || !equal(item.second, it->second)) return false; } return true; } template std::size_t bufferBytes(const std::vector& v) { return v.size() * sizeof(T); } std::size_t bufferBytes(const std::vector& v) { std::size_t n = 0; for (const auto& j : v) n += j.dump().size(); return n; } std::size_t bufferBytes(const std::unordered_map& v) { std::size_t n = 0; for (const auto& item : v) { const auto& f = item.second; n += bufferBytes(f.poles) + bufferBytes(f.knotsU) + bufferBytes(f.knotsV) + bufferBytes(f.preview.positions) + bufferBytes(f.preview.deviations); } return n; } template std::shared_ptr capture(const T& live, const std::shared_ptr& previous, std::size_t oldSize, std::size_t& size) { if (previous && equal(live, *previous)) { size = oldSize; return previous; } size = bufferBytes(live); return std::make_shared(live); } } // namespace History::History(std::size_t budgetBytes) : budget_(budgetBytes) {} void History::reset() { entries_.clear(); sizes_.clear(); cursor_ = -1; dropped_ = 0; } void History::commit(std::string label, std::string step, std::string detail, const Verts& verts, const Tris& tris, const Verts& cloud, const std::vector& sel, const std::vector& prims, const std::unordered_map& surfaces, const align::Mat3& R, const geom::Vec3& T, int nextPrimId, nlohmann::json ui) { const Snapshot previous = cursor_ < 0 ? Snapshot{} : entries_[cursor_].snap; const auto old = cursor_ < 0 ? std::array{} : sizes_[cursor_]; std::array size{}; Snapshot s; s.verts = capture(verts, previous.verts, old[0], size[0]); s.tris = capture(tris, previous.tris, old[1], size[1]); s.cloud = capture(cloud, previous.cloud, old[2], size[2]); s.selVerts = capture(sel, previous.selVerts, old[3], size[3]); s.prims = capture(prims, previous.prims, old[4], size[4]); s.surfaces = capture(surfaces, previous.surfaces, old[5], size[5]); s.frameR = R; s.frameT = T; s.nextPrimId = nextPrimId; s.ui = std::move(ui); size[6] = s.ui.dump().size(); entries_.resize(cursor_ + 1); sizes_.resize(cursor_ + 1); entries_.push_back({std::move(label), std::move(step), std::move(detail), std::move(s)}); sizes_.push_back(size); ++cursor_; while (entries_.size() > 1 && bytes() > budget_) { entries_.erase(entries_.begin()); sizes_.erase(sizes_.begin()); --cursor_; ++dropped_; } } bool History::canUndo() const { return cursor_ > 0; } bool History::canRedo() const { return cursor_ + 1 < static_cast(entries_.size()); } const Snapshot* History::undo() { return canUndo() ? go(cursor_ - 1) : nullptr; } const Snapshot* History::redo() { return canRedo() ? go(cursor_ + 1) : nullptr; } const Snapshot* History::go(int index) { if (index < 0 || index >= static_cast(entries_.size())) return nullptr; cursor_ = index; return &entries_[cursor_].snap; } int History::cursor() const { return cursor_; } const std::vector& History::entries() const { return entries_; } std::size_t History::bytes() const { std::unordered_set seen; std::size_t n = 0; for (std::size_t i = 0; i < entries_.size(); ++i) { const auto& s = entries_[i].snap; const void* buffers[] = {s.verts.get(), s.tris.get(), s.cloud.get(), s.selVerts.get(), s.prims.get(), s.surfaces.get()}; for (int k = 0; k < 6; ++k) if (seen.insert(buffers[k]).second) n += sizes_[i][k]; n += sizes_[i][6]; } return n; } std::size_t History::budget() const { return budget_; } int History::dropped() const { return dropped_; } nlohmann::json History::meta() const { auto list = nlohmann::json::array(); for (const auto& e : entries_) list.push_back({{"label", e.label}, {"step", e.step}, {"detail", e.detail}}); return {{"cursor", cursor_}, {"dropped", dropped_}, {"bytes", bytes()}, {"budget", budget_}, {"canUndo", canUndo()}, {"canRedo", canRedo()}, {"entries", std::move(list)}}; } } // namespace history