ifcviewer: viewport overlay subsystem (highlight tris + HUD text)

New OverlayRenderer module owns every client-supplied overlay primitive
drawn after the main pass: tinted, depth-aware highlight triangles via
its own GL shader, and top-left HUD text via QPainter on a
QOpenGLPaintDevice.  Public surface on ViewportWindow is just two
forwarders (setHighlightTriangles, setHudText).

ViewportWindow's MeshLocalPick now exposes the instance's composed
transform so consumers can map mesh-local geometry back to world
space without re-querying.  AreaMeasurement uses both: its selection
key is now (object_id, tri) so per-instance highlighting works for
two distinct walls sharing a mesh, and on every pick it rebuilds the
world-space tri list and the HUD readout.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Dion Moult
2026-05-07 16:43:38 +10:00
parent d565dc3ff3
commit ad62dd6d91
7 changed files with 414 additions and 21 deletions
+11 -2
View File
@@ -210,11 +210,20 @@ void MainWindow::setupUi() {
[this](int x, int y, int modifiers) {
const bool alt = (modifiers & Qt::AltModifier) != 0;
area_measurement_.onPick(*viewport_, x, y, alt);
viewport_->setHudText(QString("Area: %1 m² (%2 tris)")
.arg(area_measurement_.totalArea(), 0, 'f', 4)
.arg(area_measurement_.triangleCount()));
});
connect(viewport_, &ViewportWindow::areaToolToggled, this,
[this](bool active) {
area_measurement_.clear();
qInfo("Area tool %s", active ? "on (LMB to add patch, Alt+LMB single tri, click again to remove, Esc exits)" : "off");
area_measurement_.clear(*viewport_);
if (active) {
viewport_->setHudText("Area: 0.0000 m² (0 tris)");
status_label_->setText("Area tool: LMB add, Alt+LMB single tri, click again to remove, Esc exits");
} else {
viewport_->setHudText(QString());
status_label_->setText("Ready");
}
});
auto* tree_dock = new QDockWidget("Elements", this);
+49 -5
View File
@@ -201,10 +201,40 @@ constexpr double kCoplanarDot = 0.9999; // ~0.81° tolerance
AreaMeasurement::AreaMeasurement() = default;
void AreaMeasurement::clear() {
void AreaMeasurement::clear(ViewportWindow& vp) {
mesh_cache_.clear();
selected_.clear();
total_area_m2_ = 0.0;
vp.setHighlightTriangles({}, 0, 0, 0, 0);
}
void AreaMeasurement::rebuildHighlight(ViewportWindow& vp) {
// Push every selected triangle's three world-space vertices to the
// overlay. Mesh-local positions × per-instance composed transform.
std::vector<float> world_xyz;
world_xyz.reserve(selected_.size() * 9);
for (const auto& [key, sel] : selected_) {
const uint64_t cache_key = (uint64_t(sel.model_id) << 32)
| uint64_t(sel.mesh_id);
auto cit = mesh_cache_.find(cache_key);
if (cit == mesh_cache_.end()) continue;
const MeshCache& c = cit->second;
if (size_t(sel.tri) * 3 + 2 >= c.indices.size()) continue;
const float* M = sel.composed_transform; // column-major
for (int e = 0; e < 3; ++e) {
const uint32_t vi = c.indices[3 * sel.tri + e];
const float* p = &c.positions[3 * vi];
// World = M * (p, 1). Column-major: M[col*4 + row].
const float wx = M[0]*p[0] + M[4]*p[1] + M[8]*p[2] + M[12];
const float wy = M[1]*p[0] + M[5]*p[1] + M[9]*p[2] + M[13];
const float wz = M[2]*p[0] + M[6]*p[1] + M[10]*p[2] + M[14];
world_xyz.push_back(wx);
world_xyz.push_back(wy);
world_xyz.push_back(wz);
}
}
// Translucent cyan-ish tint — readable on both light and dark surfaces.
vp.setHighlightTriangles(world_xyz, 0.20f, 0.85f, 1.00f, 0.45f);
}
AreaMeasurement::MeshCache* AreaMeasurement::meshCache(ViewportWindow& vp,
@@ -304,19 +334,33 @@ void AreaMeasurement::onPick(ViewportWindow& vp, int x, int y, bool alt) {
// Toggle: if the seed was already in the set, remove the patch;
// otherwise add it.
const uint64_t seed_key = triKey(pick.model_id, pick.mesh_id, seed);
const uint64_t seed_key = triKey(pick.object_id, seed);
const bool removing = selected_.count(seed_key) > 0;
double delta = 0.0;
for (uint32_t t : patch) {
const uint64_t k = triKey(pick.model_id, pick.mesh_id, t);
const uint64_t k = triKey(pick.object_id, t);
if (removing) {
if (selected_.erase(k) > 0) delta -= cache->tri_areas[t];
auto it = selected_.find(k);
if (it != selected_.end()) {
delta -= cache->tri_areas[t];
selected_.erase(it);
}
} else {
if (selected_.insert(k).second) delta += cache->tri_areas[t];
SelectedTri sel;
sel.model_id = pick.model_id;
sel.mesh_id = pick.mesh_id;
sel.tri = t;
std::memcpy(sel.composed_transform, pick.composed_transform,
sizeof(sel.composed_transform));
if (selected_.emplace(k, sel).second) {
delta += cache->tri_areas[t];
}
}
}
total_area_m2_ += delta;
rebuildHighlight(vp);
qInfo("Area %s%.6f m^2 (total: %.6f m^2, %zu tris)",
delta >= 0.0 ? "+" : "", delta,
total_area_m2_, selected_.size());
+29 -14
View File
@@ -22,7 +22,6 @@
#include <cstdint>
#include <unordered_map>
#include <unordered_set>
#include <vector>
class ViewportWindow;
@@ -38,14 +37,16 @@ double volumeOfObjects(ViewportWindow& vp,
const std::vector<uint32_t>& object_ids);
// Click-to-accumulate area measurement. Each pick resolves the screen
// click to a (model, mesh, triangle) using ViewportWindow's primitives,
// click to a (instance, triangle) using ViewportWindow's primitives,
// expands it into the connected coplanar patch (BFS over shared edges,
// dot(normal, seed_normal) > 0.9999), then either adds or removes that
// patch from the running set depending on whether the seed triangle was
// already in. Alt-click skips the BFS expansion (single-triangle).
// Picks across different meshes are kept as separate patches and their
// areas are summed.
// Picks on different instances (even of the same mesh) are kept as
// separate patches and their areas are summed.
//
// On every pick the world-space triangles of the running set are pushed
// to ViewportWindow::setHighlightTriangles for in-viewport shading.
// State is cleared on construction, on clear(), and is expected to be
// reset by the host (e.g. when the viewport's area tool toggles off).
class AreaMeasurement {
@@ -57,8 +58,9 @@ public:
// via qInfo. Misses are silent.
void onPick(ViewportWindow& vp, int x, int y, bool alt);
// Wipe all accumulated triangles and per-mesh adjacency caches.
void clear();
// Wipe all accumulated triangles, per-mesh adjacency caches, and the
// viewport overlay.
void clear(ViewportWindow& vp);
double totalArea() const { return total_area_m2_; }
size_t triangleCount() const { return selected_.size(); }
@@ -77,16 +79,29 @@ private:
};
MeshCache* meshCache(ViewportWindow& vp, uint32_t model_id, uint32_t mesh_id);
// Selection key: (uint64) packing model_id (high 24), mesh_id (mid 24),
// triangle index (low 16). 16 bits is enough — meshes with > 65k tris
// are rare and the streamer chunks them anyway.
static uint64_t triKey(uint32_t model_id, uint32_t mesh_id, uint32_t tri) {
return (uint64_t(model_id) << 40) | (uint64_t(mesh_id) << 16) | uint64_t(tri);
// Per-selected-triangle record. The composed transform is captured at
// pick time so the overlay rebuild doesn't have to re-query the
// viewport for it (and so the overlay keeps working if the picked
// instance later goes hidden).
struct SelectedTri {
uint32_t model_id;
uint32_t mesh_id;
uint32_t tri;
float composed_transform[16];
};
// Selection key: object_id (high 32) | tri index (low 32). Packing
// by object_id rather than mesh_id means two distinct instances of
// the same mesh contribute independently, as the user spec'd.
static uint64_t triKey(uint32_t object_id, uint32_t tri) {
return (uint64_t(object_id) << 32) | uint64_t(tri);
}
std::unordered_map<uint64_t, MeshCache> mesh_cache_;
std::unordered_set<uint64_t> selected_;
double total_area_m2_ = 0.0;
void rebuildHighlight(ViewportWindow& vp);
std::unordered_map<uint64_t, MeshCache> mesh_cache_;
std::unordered_map<uint64_t, SelectedTri> selected_;
double total_area_m2_ = 0.0;
};
#endif // IFCVIEWER_FULL_MEASUREMENT_H