forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
depth-quality-model.h
421 lines (355 loc) · 14.2 KB
/
depth-quality-model.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/* License: Apache 2.0. See LICENSE file in root directory. */
/* Copyright(c) 2019 Intel Corporation. All Rights Reserved. */
#pragma once
#include <librealsense2/rs.hpp>
#include "depth-metrics.h"
#include "model-views.h"
#include "viewer.h"
#include "ux-window.h"
#include "os.h"
#include <tuple>
#include <vector>
#include <thread>
#include <mutex>
namespace rs2
{
namespace depth_quality
{
class metrics_model;
struct sample
{
sample(std::vector<single_metric_data> samples, double timestamp, unsigned long long frame_number) :
samples(std::move(samples)), timestamp(timestamp), frame_number(frame_number) {}
std::vector<single_metric_data> samples;
double timestamp;
unsigned long long frame_number;
};
struct metric_definition
{
std::string name;
std::string units;
};
class metrics_recorder
{
public:
metrics_recorder(viewer_model& viewer_model) :
_recording(false), _viewer_model(viewer_model)
{}
void add_metric(const metric_definition& data)
{
std::lock_guard<std::mutex> lock(_m);
_metric_data.push_back(data);
}
void add_sample(rs2::frameset& frames, std::vector<single_metric_data> sample)
{
std::lock_guard<std::mutex> lock(_m);
if (_recording)
{
record_frames(frames);
if (sample.size())
_samples.push_back({ sample, _model_timer.get_elapsed_ms(), frames.get_frame_number() });
}
}
void start_record(metrics_model* metrics)
{
std::lock_guard<std::mutex> lock(_m);
_metrics = metrics;
if (auto ret = file_dialog_open(save_file, NULL, NULL, NULL))
{
_filename_base = ret;
_recording = true;
}
}
void stop_record(device_model* dev)
{
std::lock_guard<std::mutex> lock(_m);
_recording = false;
serialize_to_csv();
if (dev)
{
if (auto adv = dev->dev.as<rs400::advanced_mode>())
{
std::string filename = _filename_base + "_configuration.json";
std::ofstream out(filename);
try
{
out << adv.serialize_json();
}
catch (...)
{
_viewer_model.not_model->add_notification(
notification_data{ "Metrics Recording: JSON Serializaion has failed",
RS2_LOG_SEVERITY_WARN,
RS2_NOTIFICATION_CATEGORY_UNKNOWN_ERROR } );
}
}
}
_samples.clear();
_viewer_model.not_model->add_notification(
notification_data{ "Finished to record frames and matrics data ",
RS2_LOG_SEVERITY_INFO,
RS2_NOTIFICATION_CATEGORY_UNKNOWN_ERROR } );
}
bool is_recording()
{
return _recording;
}
private:
void serialize_to_csv() const;
void record_frames(const frameset & frame);
viewer_model& _viewer_model;
std::vector<metric_definition> _metric_data;
std::vector<sample> _samples;
rsutils::time::stopwatch _model_timer;
std::mutex _m;
bool _recording;
std::string _filename_base;
metrics_model* _metrics;
colorizer _colorize;
pointcloud _pc;
};
class metric_plot : public std::enable_shared_from_this<metric_plot>
{
public:
enum range
{
GREEN_RANGE,
YELLOW_RANGE,
RED_RANGE,
MAX_RANGE
};
std::shared_ptr<metric_plot> set(range r, float from, float to)
{
ranges[r].x = from;
ranges[r].y = to;
return shared_from_this();
}
range get_range(float val) const
{
for (int i = 0; i < MAX_RANGE; i++)
{
if (ranges[i].x < val && val <= ranges[i].y)
return (range)i;
}
return MAX_RANGE;
}
metric_plot(const std::string& name, float min, float max,
const std::string& units, const std::string& description,
const bool with_plane_fit)
: _idx(0), _first_idx(0),_vals(), _min(min), _max(max), _id("##" + name),
_label(name + " = "), _name(name),
_units(units), _description(description),
_enabled(true),
_requires_plane_fit(with_plane_fit),
_trending_up(std::chrono::milliseconds(700)),
_trending_down(std::chrono::milliseconds(700)),
_persistent_visibility(std::chrono::milliseconds(2000)) // The metric's status will be absorbed to make the UI persistent
{
for (int i = 0; i < MAX_RANGE; i++) ranges[i] = { 0.f, 0.f };
}
~metric_plot() {}
void add_value(float val)
{
std::lock_guard<std::mutex> lock(_m);
_vals[_idx] = val;
_timestamps[_idx] = _model_timer.get_elapsed_ms();
_idx = (_idx + 1) % SIZE;
if (_first_idx== _idx)
_first_idx = (_first_idx + 1) % SIZE;
}
void render(ux_window& win);
void visible(bool is_visible)
{
std::lock_guard<std::mutex> lock(_m);
_persistent_visibility.add_value(is_visible);
}
void enable(bool enable)
{
std::lock_guard<std::mutex> lock(_m);
if (enable != _enabled)
{
_persistent_visibility.reset();
_enabled = enable;
}
}
bool enabled() const { return _enabled; }
bool requires_plane_fit() const { return _requires_plane_fit; }
std::string get_name() { return _name; }
private:
bool has_trend(bool positive);
std::mutex _m;
const static size_t SIZE = 200;
size_t _idx, _first_idx;
std::array<float, SIZE> _vals;
std::array<double, SIZE> _timestamps;
float _min, _max;
std::string _id, _label, _units, _name, _description;
bool _enabled;
const bool _requires_plane_fit;
rsutils::time::stopwatch _model_timer;
temporal_event _trending_up;
temporal_event _trending_down;
temporal_event _persistent_visibility; // Control the metric visualization
float2 ranges[MAX_RANGE];
friend class metrics_model; // For CSV export
};
class metrics_model
{
public:
metrics_model(viewer_model& viewer_model);
~metrics_model();
void render(ux_window& win);
std::array<float3, 4> get_plane()
{
std::lock_guard<std::mutex> lock(_m);
return _latest_metrics.plane_corners;
}
void update_stream_attributes(const rs2_intrinsics &intrinsic, float scale_units, float baseline)
{
std::lock_guard<std::mutex> lock(_m);
_depth_intrinsic = intrinsic;
_depth_scale_units = scale_units;
_stereo_baseline_mm = baseline;
};
void update_roi_attributes(const region_of_interest& roi, float roi_percent)
{
std::lock_guard<std::mutex> lock(_m);
_roi = roi;
_roi_percentage = roi_percent;
}
region_of_interest get_roi()
{
std::lock_guard<std::mutex> lock(_m);
return _roi;
}
snapshot_metrics get_last_metrics()
{
std::lock_guard<std::mutex> lock(_m);
return _latest_metrics;
}
void begin_process_frame(rs2::frame f) { _frame_queue.enqueue(std::move(f)); }
void add_metric(std::shared_ptr<metric_plot> metric) { _plots.push_back(metric); }
callback_type callback;
void set_ground_truth(int gt)
{
std::lock_guard<std::mutex> lock(_m);
_ground_truth_mm = gt;
_use_gt = true;
}
void set_plane_fit(bool found)
{
std::lock_guard<std::mutex> lock(_m);
_plane_fit = found;
for (auto&& plot : _plots)
{
if (plot->enabled())
{
bool val = plot->requires_plane_fit() ? found : true;
plot->visible(val);
}
}
}
void disable_ground_truth()
{
std::lock_guard<std::mutex> lock(_m);
_use_gt = false;
_ground_truth_mm = 0;
}
std::tuple<int, bool> get_inputs() const
{
std::lock_guard<std::mutex> lock(_m);
return std::make_tuple(_ground_truth_mm, _plane_fit);
}
void reset()
{
_plane_fit = false;
rs2::frame f;
while (_frame_queue.poll_for_frame(&f));
}
void update_device_data(const std::string& camera_info)
{
_camera_info = camera_info;
}
bool is_recording()
{
return _recorder.is_recording();
}
void start_record()
{
_recorder.start_record(this);
}
void stop_record(device_model* dev)
{
_recorder.stop_record(dev);
}
private:
metrics_model(const metrics_model&);
frame_queue _frame_queue;
std::thread _worker_thread;
rs2_intrinsics _depth_intrinsic;
float _depth_scale_units;
float _stereo_baseline_mm;
int _ground_truth_mm;
bool _use_gt;
bool _plane_fit;
region_of_interest _roi;
float _roi_percentage;
snapshot_metrics _latest_metrics;
bool _active;
std::vector<std::shared_ptr<metric_plot>> _plots;
metrics_recorder _recorder;
std::string _camera_info;
mutable std::mutex _m;
friend class metrics_recorder;
friend class tool_model;
};
using metric = std::shared_ptr<metric_plot>;
class tool_model
{
public:
tool_model(rs2::context& ctx);
bool start(ux_window& win);
void render(ux_window& win);
void update_configuration();
void reset(ux_window& win);
bool draw_instructions(ux_window& win, const rect& viewer_rect, bool& distance, bool& orientation);
void draw_guides(ux_window& win, const rect& viewer_rect, bool distance_guide, bool orientation_guide);
std::shared_ptr<metric_plot> make_metric(
const std::string& name, float min, float max, bool plane_fit,
const std::string& units,
const std::string& description);
void on_frame(callback_type callback) { _metrics_model.callback = callback; }
float get_depth_scale() const { return _metrics_model._depth_scale_units; }
rs2::device get_active_device(void) const;
private:
std::string capture_description();
rs2::context& _ctx;
pipeline _pipe;
std::shared_ptr<device_model> _device_model;
viewer_model _viewer_model;
rs2::points _last_points;
texture_buffer* _last_texture;
std::shared_ptr<subdevice_model> _depth_sensor_model;
metrics_model _metrics_model;
std::string _error_message;
bool _first_frame = true;
rsutils::time::periodic_timer _update_readonly_options_timer;
bool _device_in_use = false;
float _roi_percent = 0.4f;
int _roi_combo_index = 2;
temporal_event _roi_located;
temporal_event _too_far;
temporal_event _too_close;
temporal_event _skew_left;
temporal_event _skew_right;
temporal_event _skew_up;
temporal_event _skew_down;
temporal_event _angle_alert;
std::map<int, temporal_event> _depth_scale_events;
float _min_dist, _max_dist, _max_angle;
std::mutex _mutex;
bool _use_ground_truth = false;
int _ground_truth = 0;
};
}
}