ReUseX  0.0.5
3D Point Cloud Processing for Building Reuse
Loading...
Searching...
No Matches
ProjectDB.hpp
Go to the documentation of this file.
1#pragma once
2#include "reusex/types.hpp"
3
4#include <array>
5#include <filesystem>
6#include <map>
7#include <memory>
8#include <optional>
9#include <opencv2/core/mat.hpp>
10#include <pcl/PolygonMesh.h>
11#include <pcl/TextureMesh.h>
12#include <pcl/point_cloud.h>
13#include <pcl/point_types.h>
14#include <string>
15#include <string_view>
16#include <vector>
17
18namespace reusex {
19
20// Forward declarations
21namespace core {
22struct MaterialPassport;
24struct SensorIntrinsics;
25} // namespace core
26namespace geometry {
28enum class ComponentType;
29} // namespace geometry
30
31class ProjectDB {
32 public:
55 explicit ProjectDB(std::filesystem::path dbPath, bool readOnly = false);
60
61 // Non-copyable (RAII resource management)
62 ProjectDB(const ProjectDB &) = delete;
63 ProjectDB &operator=(const ProjectDB &) = delete;
64
65 // Movable
66 ProjectDB(ProjectDB &&) noexcept;
67 ProjectDB &operator=(ProjectDB &&) noexcept;
68
69 // --- Core Database Operations ---
70
71 bool is_open() const noexcept;
72 const std::filesystem::path &path() const noexcept;
73 int schema_version() const;
74 void validate_schema() const;
75
76 // --- Sensor Frame Operations ---
77
78 void save_sensor_frame(int nodeId, const cv::Mat &colorImage);
79
80 void save_sensor_frame(int nodeId, const cv::Mat &color, const cv::Mat &depth,
81 const cv::Mat &confidence,
82 const std::array<double, 16> &worldPose,
83 const core::SensorIntrinsics &intrinsics,
84 double timestamp = -1.0);
85
86 std::vector<int> sensor_frame_ids() const;
87 cv::Mat sensor_frame_image(int nodeId) const;
88 cv::Mat sensor_frame_depth(int nodeId) const;
89 cv::Mat sensor_frame_confidence(int nodeId) const;
90 std::array<double, 16> sensor_frame_pose(int nodeId) const;
91 core::SensorIntrinsics sensor_frame_intrinsics(int nodeId) const;
92 bool has_sensor_frame(int nodeId) const;
93
96 double sensor_frame_timestamp(int nodeId) const;
97
100 int nearest_sensor_frame_by_timestamp(double timestamp) const;
101
102 // --- Panoramic Image Operations ---
103
105 int id;
106 std::string filename;
107 double timestamp; // -1.0 if unknown
108 int node_id; // -1 if unmatched
109 };
110
111 void save_panoramic_image(const std::string &filename,
112 const std::vector<uint8_t> &jpeg_data,
113 double timestamp = -1.0, int nodeId = -1);
114
115 cv::Mat panoramic_image(int id) const;
116 cv::Mat panoramic_image(std::string_view filename) const;
117 bool has_panoramic_image(std::string_view filename) const;
119 void delete_panoramic_image(std::string_view filename);
120 std::vector<PanoramicImage> list_panoramic_images() const;
122
123 // --- Segmentation Image Operations ---
124
125 bool has_segmentation_image(int nodeId) const;
126 cv::Mat segmentation_image(int nodeId) const;
127 std::vector<int> segmentation_image_ids() const;
128 void save_segmentation_image(int nodeId, const cv::Mat &labels);
129 void save_segmentation_images(const std::vector<int> &nodeIds,
130 const std::vector<cv::Mat> &labels);
131
132 // --- Point Cloud Operations ---
133
134 void save_point_cloud(std::string_view name, const Cloud &cloud,
135 std::string_view stage = "",
136 std::string_view paramsJson = "");
137
138 void save_point_cloud(std::string_view name, const CloudN &cloud,
139 std::string_view stage = "",
140 std::string_view paramsJson = "");
141
142 void save_point_cloud(std::string_view name, const CloudL &cloud,
143 std::string_view stage = "",
144 std::string_view paramsJson = "");
145
146 void save_point_cloud(std::string_view name,
147 const pcl::PointCloud<pcl::PointXYZ> &cloud,
148 std::string_view stage = "",
149 std::string_view paramsJson = "");
150
151 CloudPtr point_cloud_xyzrgb(std::string_view name) const;
152 CloudNPtr point_cloud_normal(std::string_view name) const;
153 CloudLPtr point_cloud_label(std::string_view name) const;
154 pcl::PointCloud<pcl::PointXYZ>::Ptr
155 point_cloud_xyz(std::string_view name) const;
156
157 bool has_point_cloud(std::string_view name) const;
158 void delete_point_cloud(std::string_view name);
159 std::vector<std::string> list_point_clouds() const;
160 std::string point_cloud_type(std::string_view name) const;
161
162 // --- Label Definitions ---
163
164 void save_label_definitions(std::string_view cloudName,
165 const std::map<int, std::string> &labelMap);
166
167 std::map<int, std::string>
168 label_definitions(std::string_view cloudName) const;
169
170 // --- Mesh Operations ---
171
172 void save_mesh(std::string_view name, const pcl::PolygonMesh &mesh,
173 std::string_view stage = "", std::string_view paramsJson = "");
174
175 void save_mesh(std::string_view name, const pcl::TextureMesh &mesh,
176 std::string_view stage = "", std::string_view paramsJson = "");
177
178 pcl::PolygonMesh::Ptr mesh(std::string_view name) const;
179 pcl::TextureMesh::Ptr texture_mesh(std::string_view name) const;
180 bool has_mesh(std::string_view name) const;
181 std::vector<std::string> list_meshes() const;
182 std::string mesh_format(std::string_view name) const;
183
187 };
188 MeshMetadata mesh_metadata(std::string_view name) const;
189
190 std::vector<uint8_t> mesh_data_blob(std::string_view name) const;
191
193 std::string tex_name, format;
194 std::vector<uint8_t> image_data;
196 };
197 std::vector<MeshTextureInfo> mesh_texture_blobs(std::string_view name) const;
198
200 std::string tex_name, format;
202 };
203 std::vector<MeshTextureMetadata>
204 mesh_texture_metadata(std::string_view name) const;
205
206 // --- Building Component Operations ---
207
210 bool has_building_component(std::string_view name) const;
211 void delete_building_component(std::string_view name);
212 std::vector<std::string> list_building_components() const;
213 std::vector<std::string>
216
217 // --- Pipeline Log ---
218
219 int log_pipeline_start(std::string_view stage,
220 std::string_view paramsJson = "");
221
222 void log_pipeline_end(int logId, bool success,
223 std::string_view errorMsg = "");
224
226 int id;
227 std::string stage;
228 std::string started_at;
229 std::string finished_at; // Empty if still running
230 std::string parameters; // JSON string
231 std::string status; // "running", "success", "failed"
232 std::string error_msg; // Empty if no error
233 };
234
235 std::vector<PipelineLogEntry> pipeline_log(int limit = 0) const;
236
237 // --- Project Summary ---
238
240 struct CloudInfo {
241 std::string name;
242 std::string type; // "PointXYZRGB", "Normal", "Label", "PointXYZ"
244 size_t width;
245 size_t height;
246 bool organized; // height > 1
247 std::map<int, std::string> labels; // Only for Label clouds
248 };
249
250 struct MeshInfo {
251 std::string name;
254 };
255
258 int width; // 0 if no frames
259 int height; // 0 if no frames
260 int segmented_count; // frames with segmentation
261 };
262
264 int total_count = 0;
265 int matched_count = 0; // images linked to a sensor frame
266 };
267
269 int total_count = 0;
270 std::map<std::string, int> count_by_type;
271 };
272
274 std::string id;
275 std::string guid;
277 std::string created_at; // ISO 8601 timestamp
278 std::string version_number; // Semver (e.g., "0.1.1")
279 };
280
281 struct ProjectInfo {
282 std::string id;
283 std::string name;
284 std::string building_address;
285 int year_of_construction = 0; // 0 = not set
286 std::string survey_date;
288 std::string notes;
289 };
290
291 std::filesystem::path path;
293 std::vector<ProjectInfo> projects;
294 std::vector<CloudInfo> clouds;
295 std::vector<MeshInfo> meshes;
299 std::vector<MaterialInfo> materials;
300 };
301
303
304 // --- Material Passport Operations ---
305
306 core::MaterialPassport material_passport(std::string_view documentGuid) const;
307 std::vector<core::MaterialPassport> all_material_passports() const;
308
310 std::string_view projectId);
311
324 std::string_view projectId, std::string_view id);
325
331 void delete_material_passport(std::string_view documentGuid);
332
337 std::vector<std::string> list_passport_guids() const;
338
350 std::map<std::string, std::string>
351 passport_stored_properties(std::string_view documentGuid) const;
352
360 std::string passport_property_value(std::string_view documentGuid,
361 std::string_view fieldName) const;
362
377 std::optional<int>
378 passport_linked_node_id(std::string_view documentGuid) const;
379
387 passport_metadata(std::string_view documentGuid) const;
388
400 void set_passport_metadata_field(std::string_view documentGuid,
401 std::string_view column,
402 std::string_view value);
403
416 void set_passport_property(std::string_view documentGuid,
417 std::string_view fieldName,
418 std::string_view value);
419
426 void delete_passport_property(std::string_view documentGuid,
427 std::string_view fieldName);
428
429 // --- Project Metadata Operations ---
430
432 std::string id;
433 std::string name;
434 std::string building_address;
435 int year_of_construction = 0; // 0 = not set
436 std::string survey_date;
438 std::string notes;
439 };
440
447 ProjectMetadata get_project_metadata(std::string_view projectId) const;
448
455
460 std::vector<std::string> list_project_ids() const;
461
462 private:
463 class Impl;
464 std::unique_ptr<Impl> impl_;
465};
466} // namespace reusex
double sensor_frame_timestamp(int nodeId) const
Get the timestamp (epoch seconds) of a sensor frame.
int log_pipeline_start(std::string_view stage, std::string_view paramsJson="")
std::string point_cloud_type(std::string_view name) const
bool has_point_cloud(std::string_view name) const
std::vector< uint8_t > mesh_data_blob(std::string_view name) const
void add_material_passport(const core::MaterialPassport &passport, std::string_view projectId)
ProjectDB(const ProjectDB &)=delete
void save_point_cloud(std::string_view name, const pcl::PointCloud< pcl::PointXYZ > &cloud, std::string_view stage="", std::string_view paramsJson="")
bool has_segmentation_image(int nodeId) const
void log_pipeline_end(int logId, bool success, std::string_view errorMsg="")
std::vector< std::string > list_building_components() const
CloudLPtr point_cloud_label(std::string_view name) const
pcl::PolygonMesh::Ptr mesh(std::string_view name) const
ProjectDB & operator=(const ProjectDB &)=delete
int nearest_sensor_frame_by_timestamp(double timestamp) const
Find the sensor frame with the closest timestamp to the given value.
std::array< double, 16 > sensor_frame_pose(int nodeId) const
std::vector< PipelineLogEntry > pipeline_log(int limit=0) const
std::vector< MeshTextureMetadata > mesh_texture_metadata(std::string_view name) const
void add_material_passport(const core::MaterialPassport &passport, std::string_view projectId, std::string_view id)
Add a material passport with a custom row ID.
void save_panoramic_image(const std::string &filename, const std::vector< uint8_t > &jpeg_data, double timestamp=-1.0, int nodeId=-1)
bool has_mesh(std::string_view name) const
std::vector< std::string > list_passport_guids() const
List document GUIDs ordered by created_at.
void set_passport_metadata_field(std::string_view documentGuid, std::string_view column, std::string_view value)
Set a metadata column on a material passport.
void delete_panoramic_image(std::string_view filename)
void save_point_cloud(std::string_view name, const Cloud &cloud, std::string_view stage="", std::string_view paramsJson="")
std::vector< int > sensor_frame_ids() const
ProjectSummary project_summary() const
std::vector< core::MaterialPassport > all_material_passports() const
core::MaterialPassport material_passport(std::string_view documentGuid) const
void save_segmentation_image(int nodeId, const cv::Mat &labels)
int schema_version() const
void save_segmentation_images(const std::vector< int > &nodeIds, const std::vector< cv::Mat > &labels)
int building_component_count() const
ProjectDB(std::filesystem::path dbPath, bool readOnly=false)
Opens a ReUseX project database and validates its schema.
bool is_open() const noexcept
void delete_building_component(std::string_view name)
void save_mesh(std::string_view name, const pcl::TextureMesh &mesh, std::string_view stage="", std::string_view paramsJson="")
std::optional< int > passport_linked_node_id(std::string_view documentGuid) const
Return the sensor frame node_id this passport was linked to.
cv::Mat sensor_frame_confidence(int nodeId) const
void delete_point_cloud(std::string_view name)
cv::Mat sensor_frame_image(int nodeId) const
int panoramic_image_count() const
cv::Mat panoramic_image(std::string_view filename) const
std::map< int, std::string > label_definitions(std::string_view cloudName) const
bool has_panoramic_image(std::string_view filename) const
cv::Mat segmentation_image(int nodeId) const
std::vector< std::string > list_project_ids() const
List all project IDs in the database.
void save_point_cloud(std::string_view name, const CloudL &cloud, std::string_view stage="", std::string_view paramsJson="")
std::string passport_property_value(std::string_view documentGuid, std::string_view fieldName) const
Get a single passport property value by field name.
std::vector< PanoramicImage > list_panoramic_images() const
void save_point_cloud(std::string_view name, const CloudN &cloud, std::string_view stage="", std::string_view paramsJson="")
CloudPtr point_cloud_xyzrgb(std::string_view name) const
MeshMetadata mesh_metadata(std::string_view name) const
CloudNPtr point_cloud_normal(std::string_view name) const
bool has_sensor_frame(int nodeId) const
void validate_schema() const
void update_project_metadata(const ProjectMetadata &metadata)
Update project metadata.
geometry::BuildingComponent building_component(std::string_view name) const
std::vector< MeshTextureInfo > mesh_texture_blobs(std::string_view name) const
std::map< std::string, std::string > passport_stored_properties(std::string_view documentGuid) const
Get stored property field_name→string pairs for a passport.
bool has_building_component(std::string_view name) const
const std::filesystem::path & path() const noexcept
std::vector< std::string > list_point_clouds() const
void delete_panoramic_image(int id)
void set_passport_property(std::string_view documentGuid, std::string_view fieldName, std::string_view value)
Set a single property value by field name (upsert).
std::vector< std::string > list_building_components(geometry::ComponentType type) const
std::string mesh_format(std::string_view name) const
void save_mesh(std::string_view name, const pcl::PolygonMesh &mesh, std::string_view stage="", std::string_view paramsJson="")
~ProjectDB()
Destructor closes database connection.
core::SensorIntrinsics sensor_frame_intrinsics(int nodeId) const
cv::Mat panoramic_image(int id) const
core::MaterialPassportMetadata passport_metadata(std::string_view documentGuid) const
Get passport metadata without loading all properties.
pcl::PointCloud< pcl::PointXYZ >::Ptr point_cloud_xyz(std::string_view name) const
void delete_passport_property(std::string_view documentGuid, std::string_view fieldName)
Delete a single property value by field name.
pcl::TextureMesh::Ptr texture_mesh(std::string_view name) const
void save_building_component(const geometry::BuildingComponent &component)
std::vector< int > segmentation_image_ids() const
cv::Mat sensor_frame_depth(int nodeId) const
std::vector< std::string > list_meshes() const
void save_sensor_frame(int nodeId, const cv::Mat &colorImage)
ProjectDB(ProjectDB &&) noexcept
ProjectMetadata get_project_metadata(std::string_view projectId) const
Get project metadata by project ID.
void delete_material_passport(std::string_view documentGuid)
Delete a material passport by GUID.
void save_label_definitions(std::string_view cloudName, const std::map< int, std::string > &labelMap)
ComponentType
Discriminator for building component types.
typename CloudN::Ptr CloudNPtr
Definition types.hpp:31
pcl::PointCloud< PointT > Cloud
Definition types.hpp:26
typename CloudL::Ptr CloudLPtr
Definition types.hpp:35
typename Cloud::Ptr CloudPtr
Definition types.hpp:27
pcl::PointCloud< LabelT > CloudL
Definition types.hpp:34
pcl::PointCloud< NormalT > CloudN
Definition types.hpp:30
std::vector< uint8_t > image_data
std::map< int, std::string > labels
std::vector< CloudInfo > clouds
std::vector< ProjectInfo > projects
std::vector< MaterialInfo > materials
std::vector< MeshInfo > meshes
Document-level metadata for the material passport.
A complete material passport for a reused building material.
Lightweight camera intrinsics replacing rtabmap::CameraModel in downstream code.
A detected or manual building component (window, door, wall, ...).