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 <opencv2/core/mat.hpp>
9#include <pcl/PolygonMesh.h>
10#include <pcl/TextureMesh.h>
11#include <pcl/point_cloud.h>
12#include <pcl/point_types.h>
13#include <string>
14#include <string_view>
15#include <vector>
16
17namespace reusex {
18
19// Forward declarations
20namespace core {
21struct MaterialPassport;
23struct SensorIntrinsics;
24} // namespace core
25namespace geometry {
27enum class ComponentType;
28} // namespace geometry
29
30class ProjectDB {
31 public:
54 explicit ProjectDB(std::filesystem::path dbPath, bool readOnly = false);
59
60 // Non-copyable (RAII resource management)
61 ProjectDB(const ProjectDB &) = delete;
62 ProjectDB &operator=(const ProjectDB &) = delete;
63
64 // Movable
65 ProjectDB(ProjectDB &&) noexcept;
66 ProjectDB &operator=(ProjectDB &&) noexcept;
67
68 // --- Core Database Operations ---
69
70 bool is_open() const noexcept;
71 const std::filesystem::path &path() const noexcept;
72 int schema_version() const;
73 void validate_schema() const;
74
75 // --- Sensor Frame Operations ---
76
77 void save_sensor_frame(int nodeId, const cv::Mat &colorImage);
78
79 void save_sensor_frame(int nodeId, const cv::Mat &color, const cv::Mat &depth,
80 const cv::Mat &confidence,
81 const std::array<double, 16> &worldPose,
82 const core::SensorIntrinsics &intrinsics,
83 double timestamp = -1.0);
84
85 std::vector<int> sensor_frame_ids() const;
86 cv::Mat sensor_frame_image(int nodeId) const;
87 cv::Mat sensor_frame_depth(int nodeId) const;
88 cv::Mat sensor_frame_confidence(int nodeId) const;
89 std::array<double, 16> sensor_frame_pose(int nodeId) const;
90 core::SensorIntrinsics sensor_frame_intrinsics(int nodeId) const;
91 bool has_sensor_frame(int nodeId) const;
92
94 double sensor_frame_timestamp(int nodeId) const;
95
98 int nearest_sensor_frame_by_timestamp(double timestamp) const;
99
100 // --- Panoramic Image Operations ---
101
103 int id;
104 std::string filename;
105 double timestamp; // -1.0 if unknown
106 int node_id; // -1 if unmatched
107 };
108
109 void save_panoramic_image(const std::string &filename,
110 const std::vector<uint8_t> &jpeg_data,
111 double timestamp = -1.0, int nodeId = -1);
112
113 cv::Mat panoramic_image(int id) const;
114 cv::Mat panoramic_image(std::string_view filename) const;
115 bool has_panoramic_image(std::string_view filename) const;
117 void delete_panoramic_image(std::string_view filename);
118 std::vector<PanoramicImage> list_panoramic_images() const;
120
121 // --- Segmentation Image Operations ---
122
123 bool has_segmentation_image(int nodeId) const;
124 cv::Mat segmentation_image(int nodeId) const;
125 void save_segmentation_image(int nodeId, const cv::Mat &labels);
126 void save_segmentation_images(const std::vector<int> &nodeIds,
127 const std::vector<cv::Mat> &labels);
128
129 // --- Point Cloud Operations ---
130
131 void save_point_cloud(std::string_view name, const Cloud &cloud,
132 std::string_view stage = "",
133 std::string_view paramsJson = "");
134
135 void save_point_cloud(std::string_view name, const CloudN &cloud,
136 std::string_view stage = "",
137 std::string_view paramsJson = "");
138
139 void save_point_cloud(std::string_view name, const CloudL &cloud,
140 std::string_view stage = "",
141 std::string_view paramsJson = "");
142
143 void save_point_cloud(std::string_view name,
144 const pcl::PointCloud<pcl::PointXYZ> &cloud,
145 std::string_view stage = "",
146 std::string_view paramsJson = "");
147
148 CloudPtr point_cloud_xyzrgb(std::string_view name) const;
149 CloudNPtr point_cloud_normal(std::string_view name) const;
150 CloudLPtr point_cloud_label(std::string_view name) const;
151 pcl::PointCloud<pcl::PointXYZ>::Ptr
152 point_cloud_xyz(std::string_view name) const;
153
154 bool has_point_cloud(std::string_view name) const;
155 void delete_point_cloud(std::string_view name);
156 std::vector<std::string> list_point_clouds() const;
157 std::string point_cloud_type(std::string_view name) const;
158
159 // --- Label Definitions ---
160
161 void save_label_definitions(std::string_view cloudName,
162 const std::map<int, std::string> &labelMap);
163
164 std::map<int, std::string>
165 label_definitions(std::string_view cloudName) const;
166
167 // --- Mesh Operations ---
168
169 void save_mesh(std::string_view name, const pcl::PolygonMesh &mesh,
170 std::string_view stage = "", std::string_view paramsJson = "");
171
172 void save_mesh(std::string_view name, const pcl::TextureMesh &mesh,
173 std::string_view stage = "", std::string_view paramsJson = "");
174
175 pcl::PolygonMesh::Ptr mesh(std::string_view name) const;
176 pcl::TextureMesh::Ptr texture_mesh(std::string_view name) const;
177 bool has_mesh(std::string_view name) const;
178 std::vector<std::string> list_meshes() const;
179
180 // --- Building Component Operations ---
181
184 bool has_building_component(std::string_view name) const;
185 void delete_building_component(std::string_view name);
186 std::vector<std::string> list_building_components() const;
187 std::vector<std::string>
190
191 // --- Pipeline Log ---
192
193 int log_pipeline_start(std::string_view stage,
194 std::string_view paramsJson = "");
195
196 void log_pipeline_end(int logId, bool success,
197 std::string_view errorMsg = "");
198
200 int id;
201 std::string stage;
202 std::string started_at;
203 std::string finished_at; // Empty if still running
204 std::string parameters; // JSON string
205 std::string status; // "running", "success", "failed"
206 std::string error_msg; // Empty if no error
207 };
208
209 std::vector<PipelineLogEntry> pipeline_log(int limit = 0) const;
210
211 // --- Project Summary ---
212
214 struct CloudInfo {
215 std::string name;
216 std::string type; // "PointXYZRGB", "Normal", "Label", "PointXYZ"
218 size_t width;
219 size_t height;
220 bool organized; // height > 1
221 std::map<int, std::string> labels; // Only for Label clouds
222 };
223
224 struct MeshInfo {
225 std::string name;
228 };
229
232 int width; // 0 if no frames
233 int height; // 0 if no frames
234 int segmented_count; // frames with segmentation
235 };
236
238 int total_count = 0;
239 int matched_count = 0; // images linked to a sensor frame
240 };
241
243 int total_count = 0;
244 std::map<std::string, int> count_by_type;
245 };
246
248 std::string id;
249 std::string guid;
251 std::string created_at; // ISO 8601 timestamp
252 std::string version_number; // Semver (e.g., "0.1.1")
253 };
254
255 struct ProjectInfo {
256 std::string id;
257 std::string name;
258 std::string building_address;
259 int year_of_construction = 0; // 0 = not set
260 std::string survey_date;
262 std::string notes;
263 };
264
265 std::filesystem::path path;
267 std::vector<ProjectInfo> projects;
268 std::vector<CloudInfo> clouds;
269 std::vector<MeshInfo> meshes;
273 std::vector<MaterialInfo> materials;
274 };
275
277
278 // --- Material Passport Operations ---
279
280 core::MaterialPassport material_passport(std::string_view documentGuid) const;
281 std::vector<core::MaterialPassport> all_material_passports() const;
282
284 std::string_view projectId);
285
298 std::string_view projectId,
299 std::string_view id);
300
306 void delete_material_passport(std::string_view documentGuid);
307
312 std::vector<std::string> list_passport_guids() const;
313
325 std::map<std::string, std::string>
326 passport_stored_properties(std::string_view documentGuid) const;
327
335 std::string passport_property_value(std::string_view documentGuid,
336 std::string_view fieldName) const;
337
345 passport_metadata(std::string_view documentGuid) const;
346
358 void set_passport_metadata_field(std::string_view documentGuid,
359 std::string_view column,
360 std::string_view value);
361
374 void set_passport_property(std::string_view documentGuid,
375 std::string_view fieldName,
376 std::string_view value);
377
384 void delete_passport_property(std::string_view documentGuid,
385 std::string_view fieldName);
386
387 // --- Project Metadata Operations ---
388
390 std::string id;
391 std::string name;
392 std::string building_address;
393 int year_of_construction = 0; // 0 = not set
394 std::string survey_date;
396 std::string notes;
397 };
398
405 ProjectMetadata get_project_metadata(std::string_view projectId) const;
406
413
418 std::vector<std::string> list_project_ids() const;
419
420 private:
421 class Impl;
422 std::unique_ptr<Impl> impl_;
423};
424} // namespace reusex
double sensor_frame_timestamp(int nodeId) const
Get the timestamp (epoch seconds) of a sensor frame. Returns -1.0 if not set.
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
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
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="")
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
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::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
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)
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::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, ...).