Something went wrong. Try again.
Builds CAD models from 3d scan data
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128#include "history.hpp"#include <algorithm>#include <cmath>#include <unordered_set>
namespace history {namespace {template<class T> 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<class T> bool equal(const std::vector<T>& a, const std::vector<T>& 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<int, surface::Fit>& a, const std::unordered_map<int, surface::Fit>& 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<class T> std::size_t bufferBytes(const std::vector<T>& v) { return v.size() * sizeof(T);}std::size_t bufferBytes(const std::vector<nlohmann::json>& 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<int, surface::Fit>& 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<class T> std::shared_ptr<const T> capture(const T& live, const std::shared_ptr<const T>& 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<const T>(live);}} // namespaceHistory::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<int>& sel, const std::vector<nlohmann::json>& prims, const std::unordered_map<int, surface::Fit>& 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<std::size_t, 7>{} : sizes_[cursor_]; std::array<std::size_t, 7> 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<int>(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<int>(entries_.size())) return nullptr; cursor_ = index; return &entries_[cursor_].snap;}int History::cursor() const { return cursor_; }const std::vector<Entry>& History::entries() const { return entries_; }std::size_t History::bytes() const { std::unordered_set<const void*> 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