2026-04-21 18:05:19 +10:00
|
|
|
/********************************************************************************
|
|
|
|
|
* *
|
|
|
|
|
* This file is part of IfcOpenShell. *
|
|
|
|
|
* *
|
|
|
|
|
* IfcOpenShell is free software: you can redistribute it and/or modify *
|
|
|
|
|
* it under the terms of the Lesser GNU General Public License as published by *
|
|
|
|
|
* the Free Software Foundation, either version 3.0 of the License, or *
|
|
|
|
|
* (at your option) any later version. *
|
|
|
|
|
* *
|
|
|
|
|
* IfcOpenShell is distributed in the hope that it will be useful, *
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
|
* Lesser GNU General Public License for more details. *
|
|
|
|
|
* *
|
|
|
|
|
* You should have received a copy of the Lesser GNU General Public License *
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
|
* *
|
|
|
|
|
********************************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifndef SCENELOADER_H
|
|
|
|
|
#define SCENELOADER_H
|
|
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
|
#include <QString>
|
|
|
|
|
#include <QStringList>
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
#include <QElapsedTimer>
|
|
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <deque>
|
|
|
|
|
#include <map>
|
2026-05-16 21:11:27 +10:00
|
|
|
#include <memory>
|
2026-04-21 18:05:19 +10:00
|
|
|
#include <string>
|
|
|
|
|
#include <thread>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2026-05-01 17:31:08 +10:00
|
|
|
#include "Federation.h"
|
2026-06-04 11:11:14 +10:00
|
|
|
#include "../ifcviewer/ViewportWindow.h"
|
|
|
|
|
#include "../ifcviewer/StreamingLoader.h"
|
2026-04-21 18:05:19 +10:00
|
|
|
#include "GeometryStreamer.h"
|
2026-05-16 21:11:27 +10:00
|
|
|
#include "SidecarBuilder.h"
|
2026-04-21 18:05:19 +10:00
|
|
|
#include "SidecarCache.h"
|
|
|
|
|
|
2026-06-04 11:11:14 +10:00
|
|
|
// Drives IFC file loading into a ViewportWindow. Owns the per-model
|
2026-04-21 18:05:19 +10:00
|
|
|
// GeometryStreamer, the load queue, the sidecar read thread, and the
|
|
|
|
|
// next-free object_id counter used to rebase cached models onto the
|
|
|
|
|
// current session's ID space.
|
|
|
|
|
//
|
|
|
|
|
// Consumers (MainWindow, MinimalWindow) observe progress through signals
|
|
|
|
|
// and never touch the streamer, sidecar thread, or queue directly.
|
2026-05-16 21:11:27 +10:00
|
|
|
// Sidecar *writes* happen automatically as a side effect of stream loads —
|
|
|
|
|
// the SidecarBuilder accumulates from the streamer chunks alongside the
|
|
|
|
|
// viewport upload, and SceneLoader finalizes + writes the result when the
|
|
|
|
|
// stream finishes. No GPU readback involved.
|
2026-04-21 18:05:19 +10:00
|
|
|
class SceneLoader : public QObject {
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
2026-06-04 11:11:14 +10:00
|
|
|
explicit SceneLoader(ViewportWindow* viewport, QObject* parent = nullptr);
|
2026-04-21 18:05:19 +10:00
|
|
|
~SceneLoader();
|
|
|
|
|
|
2026-05-16 21:11:27 +10:00
|
|
|
// Sidecar cache use is opt-in per direction. Embedders that don't care
|
|
|
|
|
// about .ifcview can leave both off (default) and SceneLoader will never
|
|
|
|
|
// probe for or produce one. Toggles only affect *subsequent* loads;
|
|
|
|
|
// a load already in flight finishes with whatever was set when it
|
|
|
|
|
// started. Opening a `.ifcview` file directly always reads it,
|
|
|
|
|
// regardless of these flags.
|
|
|
|
|
void setShouldReadSidecar(bool enabled);
|
|
|
|
|
void setShouldWriteSidecar(bool enabled);
|
|
|
|
|
bool shouldReadSidecar() const { return should_read_sidecar_; }
|
|
|
|
|
bool shouldWriteSidecar() const { return should_write_sidecar_; }
|
|
|
|
|
|
2026-04-21 18:05:19 +10:00
|
|
|
// Returns the model_ids assigned to the enqueued paths, in order.
|
|
|
|
|
// Callers can use these to set up per-model UI state (tree roots, etc.)
|
|
|
|
|
// before any load signal fires.
|
|
|
|
|
std::vector<uint32_t> addFiles(const QStringList& paths);
|
2026-04-22 11:27:53 +10:00
|
|
|
void cancelCurrentLoad();
|
2026-04-21 18:05:19 +10:00
|
|
|
bool isLoading() const { return loading_model_id_ != 0 || !load_queue_.empty(); }
|
2026-05-04 12:22:14 +10:00
|
|
|
bool isLoadingModel(uint32_t mid) const { return loading_model_id_ == mid; }
|
2026-04-21 18:05:19 +10:00
|
|
|
size_t modelCount() const { return models_.size(); }
|
|
|
|
|
|
2026-05-04 12:22:14 +10:00
|
|
|
// Drop the loader's tracking for `mid` — its streamer, file path, georef
|
|
|
|
|
// cache, and queue slot if still pending. Caller is responsible for the
|
|
|
|
|
// viewport / UI cleanup; this only releases the loader's own state.
|
|
|
|
|
// Refuses while the model is the active load (use cancelCurrentLoad first).
|
|
|
|
|
void removeModel(uint32_t mid);
|
|
|
|
|
|
2026-04-21 18:05:19 +10:00
|
|
|
QString filePath(uint32_t mid) const;
|
|
|
|
|
QString displayName(uint32_t mid) const;
|
|
|
|
|
ifcopenshell::file* ifcFile(uint32_t mid) const;
|
|
|
|
|
|
2026-05-01 17:31:08 +10:00
|
|
|
// Lazily computes the model's georef matrix + unit scales the first
|
|
|
|
|
// time it's asked for, caches the result, and returns a pointer into the
|
|
|
|
|
// cache. Returns nullptr when the IFC file isn't available yet (e.g.
|
|
|
|
|
// sidecar-hit path before the data-source thread populates the streamer).
|
|
|
|
|
const ModelGeoref* modelGeoref(uint32_t mid);
|
|
|
|
|
|
2026-04-21 18:05:19 +10:00
|
|
|
signals:
|
|
|
|
|
void progressChanged(int percent);
|
|
|
|
|
void loadStarted(uint32_t mid, QString display_name);
|
|
|
|
|
|
|
|
|
|
// Fired once per sidecar hit, before loadedFromSidecar, with the full
|
|
|
|
|
// packed element set. Consumer is responsible for decoding + tree/
|
|
|
|
|
// property-map population. Moved arguments — avoid unnecessary copies.
|
|
|
|
|
void sidecarElementsReady(uint32_t mid,
|
|
|
|
|
std::vector<PackedElementInfo> elements,
|
|
|
|
|
std::string string_table);
|
|
|
|
|
void loadedFromSidecar(uint32_t mid, qint64 elapsed_ms);
|
|
|
|
|
|
2026-04-24 13:45:04 +10:00
|
|
|
// Fired after a sidecar-hit model has its .rdb/.ifc opened as a
|
|
|
|
|
// property data source in the background. Consumers can refresh
|
|
|
|
|
// any UI that queries ifcFile(mid) for attributes/properties.
|
|
|
|
|
void dataSourceReady(uint32_t mid);
|
|
|
|
|
|
2026-04-21 18:05:19 +10:00
|
|
|
// Fired repeatedly while streaming, as the worker thread produces
|
|
|
|
|
// elements. Each batch contains whatever accumulated since the last
|
|
|
|
|
// poll tick.
|
|
|
|
|
void streamedElementsReady(uint32_t mid, std::vector<ElementInfo> elements);
|
|
|
|
|
|
|
|
|
|
// Fired once after the streamer finishes and the viewport has been
|
|
|
|
|
// finalized. Consumer may synchronously perform work that needs all
|
|
|
|
|
// elements to be known (e.g. sidecar write) — SceneLoader will only
|
|
|
|
|
// start the next queued load after all slots return.
|
|
|
|
|
void loadedFromStream(uint32_t mid, qint64 elapsed_ms);
|
2026-04-22 11:27:53 +10:00
|
|
|
void loadCancelled(uint32_t mid);
|
2026-04-21 18:05:19 +10:00
|
|
|
|
|
|
|
|
void loadError(uint32_t mid, QString message);
|
|
|
|
|
void allLoadsFinished();
|
|
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
|
void onStreamerProgressChanged(int percent);
|
|
|
|
|
void onStreamerMeshReady(MeshChunk chunk);
|
|
|
|
|
void onStreamerInstanceReady(InstanceChunk chunk);
|
|
|
|
|
void onStreamerFinished();
|
2026-04-22 11:27:53 +10:00
|
|
|
void onStreamerCancelled();
|
2026-04-21 18:05:19 +10:00
|
|
|
void onStreamerError(const QString& msg);
|
|
|
|
|
void onElementPollTick();
|
|
|
|
|
|
|
|
|
|
private:
|
2026-04-28 19:31:48 +10:00
|
|
|
struct Model {
|
2026-04-21 18:05:19 +10:00
|
|
|
uint32_t id = 0;
|
|
|
|
|
QString file_path;
|
|
|
|
|
QString display_name;
|
|
|
|
|
GeometryStreamer* streamer = nullptr;
|
|
|
|
|
QElapsedTimer load_timer;
|
2026-05-01 17:31:08 +10:00
|
|
|
|
|
|
|
|
// Cached on first SceneLoader::modelGeoref(mid) call once the
|
|
|
|
|
// streamer has its IFC file loaded.
|
|
|
|
|
ModelGeoref georef;
|
|
|
|
|
bool has_georef = false;
|
2026-05-03 10:04:39 +10:00
|
|
|
|
2026-05-16 21:11:27 +10:00
|
|
|
// Live-load sidecar accumulator. Constructed at the start of a
|
|
|
|
|
// stream load when shouldWriteSidecar is on; null otherwise.
|
|
|
|
|
std::unique_ptr<SidecarBuilder> sidecar_builder;
|
|
|
|
|
// Element batches accumulated as the streamer yields, mirrored from
|
|
|
|
|
// what's emitted via streamedElementsReady so finalize() has the
|
|
|
|
|
// full set without re-draining.
|
|
|
|
|
std::vector<ElementInfo> streamed_elements;
|
2026-04-21 18:05:19 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void startNextLoad();
|
2026-05-16 21:11:27 +10:00
|
|
|
void startStreamLoadFor(uint32_t mid);
|
2026-04-21 18:05:19 +10:00
|
|
|
void connectStreamer(GeometryStreamer* streamer);
|
|
|
|
|
void joinSidecarThread();
|
2026-04-24 13:45:04 +10:00
|
|
|
void joinDataSourceThreads();
|
2026-06-01 17:38:33 +10:00
|
|
|
void applySidecarData(uint32_t mid, StreamingSidecar metadata);
|
2026-04-24 13:45:04 +10:00
|
|
|
void startDataSourceLoad(uint32_t mid);
|
2026-04-21 18:05:19 +10:00
|
|
|
|
2026-06-04 11:11:14 +10:00
|
|
|
ViewportWindow* viewport_ = nullptr;
|
2026-05-16 21:11:27 +10:00
|
|
|
bool should_read_sidecar_ = false;
|
|
|
|
|
bool should_write_sidecar_ = false;
|
2026-04-28 19:31:48 +10:00
|
|
|
std::map<uint32_t, Model> models_;
|
2026-04-21 18:05:19 +10:00
|
|
|
std::deque<uint32_t> load_queue_;
|
|
|
|
|
uint32_t next_model_id_ = 1;
|
|
|
|
|
uint32_t next_object_id_ = 1;
|
|
|
|
|
uint32_t loading_model_id_ = 0;
|
|
|
|
|
std::thread sidecar_read_thread_;
|
2026-04-24 13:45:04 +10:00
|
|
|
// One thread per sidecar-hit model while its .rdb/.ifc opens in the
|
|
|
|
|
// background. Joined only at destruction so a slow SPF parse on model
|
|
|
|
|
// A never blocks the sidecar-hit path of model B.
|
|
|
|
|
std::vector<std::thread> data_source_threads_;
|
2026-04-21 18:05:19 +10:00
|
|
|
QTimer element_poll_timer_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif // SCENELOADER_H
|