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 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
|
/************************************************************************
*
* Copyright (C) 2019-2024 IRCAD France
* Copyright (C) 2019-2020 IHU Strasbourg
*
* This file is part of Sight.
*
* Sight is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sight 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
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sight. If not, see <https://www.gnu.org/licenses/>.
*
***********************************************************************/
#include "scan.hpp"
#include <core/base.hpp>
#include <core/com/signal.hxx>
#include <core/com/slot.hxx>
#include <core/com/slots.hxx>
#include <core/location/single_file.hpp>
#include <core/runtime/path.hpp>
#include <core/type.hpp>
#include <data/matrix4.hpp>
#include <ui/__/dialog/location.hpp>
#include <ui/__/dialog/message.hpp>
#include <ui/__/dialog/selector.hpp>
#include <librealsense2/rs_advanced_mode.hpp>
#include <algorithm>
#include <fstream>
#include <thread>
namespace sight::module::io::realsense
{
namespace fwClock = core::clock;
static const std::string s_FPS = "fps";
static const std::string s_COLOR_FRAME_H = "colorH";
static const std::string s_COLOR_FRAME_W = "colorW";
static const std::string s_DEPTH_FRAME_H = "depthH";
static const std::string s_DEPTH_FRAME_W = "depthW";
static const std::string s_PRESET = "preset";
static const std::string s_ALIGNMENT = "alignTo";
static const std::string s_IREMITTER = "IREmitter";
static const std::string s_SWITCH_TO_IR = "switchToIR";
static const std::string s_ENABLE_SPACIAL_FILTER = "enableSpacial";
static const std::string s_ENABLE_TEMPORAL_FILTER = "enableTemporal";
static const std::string s_ENABLE_HOLES_FILLING_FILTER = "enableHolesFilling";
static const std::string s_SPACIAL_MAGNITUDE = "spacialMagnitude";
static const std::string s_SPACIAL_SMOOTH_ALPHA = "spacialSmoothAlpha";
static const std::string s_SPACIAL_SMOOTH_DELTA = "spacialSmoothDelta";
static const std::string s_SPACIAL_HOLE_FILLING = "spacialHoleFilling";
static const std::string s_TEMPORAL_SMOOTH_ALPHA = "temporalSmoothAlpha";
static const std::string s_TEMPORAL_SMOOTH_DELTA = "temporalSmoothDelta";
static const std::string s_TEMPORAL_PERSISTENCY = "temporalPersistency";
static const std::string s_HOLE_FILLING = "holeFilling";
static const core::com::slots::key_t CONFIGURE_RECORDING_PATH_SLOT = "configure_recording_path";
static const core::com::slots::key_t RECORD = "record";
static const core::com::signals::key_t DISTANCE_COMPUTED_SIG = "distance_computed";
static const core::com::signals::key_t DEVICE_PLAYED_SIG = "device_played";
static const core::com::signals::key_t FILE_PLAYED_SIG = "file_played";
// Determine depth value corresponding to one meter
static const float s_METERS_TO_MMS = 1000.F;
//-----------------------------------------------------------------------------
scan::scan() noexcept
{
new_slot(CONFIGURE_RECORDING_PATH_SLOT, &scan::configureRecordingPath, this);
new_slot(RECORD, &scan::record, this);
new_signal<distance_computed_t>(DISTANCE_COMPUTED_SIG);
new_signal<device_played_signal_t>(DEVICE_PLAYED_SIG);
new_signal<file_played_signal_t>(FILE_PLAYED_SIG);
}
//-----------------------------------------------------------------------------
scan::~scan() noexcept
{
this->scan::stopCamera();
}
//-----------------------------------------------------------------------------
void scan::starting()
{
m_grabbingStarted = false;
}
//-----------------------------------------------------------------------------
void scan::stopping()
{
this->stopCamera();
// Clear the preset
m_cameraSettings.reset();
}
//-----------------------------------------------------------------------------
void scan::configuring()
{
const config_t configTree = this->get_config();
const auto cfg = configTree.get_child_optional("config.<xmlattr>.");
if(cfg)
{
m_cameraSettings.fps = cfg->get<int>(s_FPS, m_cameraSettings.fps);
m_cameraSettings.colorW = cfg->get<int>(s_COLOR_FRAME_W, m_cameraSettings.colorW);
m_cameraSettings.colorH = cfg->get<int>(s_COLOR_FRAME_H, m_cameraSettings.colorH);
m_cameraSettings.depthW = cfg->get<int>(s_DEPTH_FRAME_W, m_cameraSettings.depthW);
m_cameraSettings.depthH = cfg->get<int>(s_DEPTH_FRAME_H, m_cameraSettings.depthH);
m_switchInfra2Color = cfg->get<bool>(s_SWITCH_TO_IR, m_switchInfra2Color);
m_cameraSettings.irEmitter = cfg->get<bool>(s_IREMITTER, m_cameraSettings.irEmitter);
const std::string alignTo = cfg->get<std::string>(s_ALIGNMENT, "None");
this->updateAlignment(alignTo);
}
static const auto s_modulePath = core::runtime::get_module_resource_path("sight::module::io::realsense");
// Parse presets files
this->loadPresets(s_modulePath / "presets");
}
//-----------------------------------------------------------------------------
void scan::updating()
{
this->stopCamera();
this->startCamera();
}
//-----------------------------------------------------------------------------
std::string scan::selectDevice()
{
// TODO: Check if a device is already selected (via Qt).
// Obtain a list of devices currently present on the system
rs2::context ctx;
const auto devices = ctx.query_devices();
const uint32_t device_count = devices.size();
std::string selectedDevice;
// Only one device found -> select it.
if(device_count == 1)
{
selectedDevice = devices[0].get_info(RS2_CAMERA_INFO_SERIAL_NUMBER);
}
// Several devices found -> open a selector dialog.
else if(device_count > 1)
{
sight::ui::dialog::selector dial;
dial.setTitle("Select realsense camera");
std::vector<std::string> selections;
selections.resize(devices.size());
for(std::uint32_t i = 0 ; i < devices.size() ; ++i)
{
const std::string name = devices[i].get_info(RS2_CAMERA_INFO_NAME);
const std::string serial = devices[i].get_info(RS2_CAMERA_INFO_SERIAL_NUMBER);
// Display index + name + serial (ex: 1.Intel RealSense D415(839112062452) ).
const std::string nameserial = std::to_string(i + 1) + ". " + name + " (" + serial + ")";
selections[i] = nameserial;
}
dial.set_choices(selections);
const std::string selected = dial.show();
// Get the index of selected camera.
const std::size_t dot = selected.find('.');
const auto index = std::stoi(selected.substr(0, dot)) - 1;
// Get associated serial numbers.
selectedDevice = devices[static_cast<uint32_t>(index)].get_info(RS2_CAMERA_INFO_SERIAL_NUMBER);
SIGHT_DEBUG("selecting camera: " << index);
}
return selectedDevice;
}
//-----------------------------------------------------------------------------
void scan::initialize(const rs2::pipeline_profile& _profile)
{
const auto depthStream = _profile.get_stream(RS2_STREAM_DEPTH).as<rs2::video_stream_profile>();
const auto colorStream = _profile.get_stream(RS2_STREAM_COLOR).as<rs2::video_stream_profile>();
const auto infraredStream = _profile.get_stream(RS2_STREAM_INFRARED).as<rs2::video_stream_profile>();
std::stringstream str;
str << "-- fps : " << depthStream.fps() << std::endl;
str << "-- depth w : " << depthStream.width() << std::endl;
str << "-- depth h : " << depthStream.height() << std::endl;
str << "-- depth format : " << depthStream.format() << std::endl;
str << "-- color w : " << colorStream.width() << std::endl;
str << "-- color h : " << colorStream.height() << std::endl;
str << "-- color format : " << colorStream.format() << std::endl;
SIGHT_DEBUG("Actual mode : \n" + str.str());
const auto depthStreamW = static_cast<data::mesh::size_t>(depthStream.width());
const auto depthStreamH = static_cast<data::mesh::size_t>(depthStream.height());
const auto colorStreamW = static_cast<data::mesh::size_t>(colorStream.width());
const auto colorStreamH = static_cast<data::mesh::size_t>(colorStream.height());
{
const auto colorTimeline = m_frame.lock();
colorTimeline->init_pool_size(
colorStreamW,
colorStreamH,
core::type::UINT8,
data::frame_tl::PixelFormat::RGBA
);
colorTimeline->setMaximumSize(50);
}
{
const auto depthTimeline = m_depth.lock();
if(depthTimeline)
{
depthTimeline->init_pool_size(
depthStreamW,
depthStreamH,
core::type::UINT16,
data::frame_tl::PixelFormat::GRAY_SCALE
);
depthTimeline->setMaximumSize(50);
}
}
{
// Get camera information.
const auto camera_set = m_camera_set.lock();
if(camera_set)
{
data::camera::sptr colorCamera;
data::camera::sptr depthCamera;
// check if there is camera
if(camera_set->size() == 0)
{
depthCamera = std::make_shared<data::camera>();
colorCamera = std::make_shared<data::camera>();
camera_set->add_camera(depthCamera);
camera_set->add_camera(colorCamera);
auto sig = camera_set->signal<data::camera_set::added_camera_signal_t>(
data::camera_set::ADDED_CAMERA_SIG
);
sig->async_emit(depthCamera);
sig->async_emit(colorCamera);
}
else if(camera_set->size() == 1) // missing one camera
{
depthCamera = camera_set->get_camera(0);
colorCamera = std::make_shared<data::camera>();
camera_set->add_camera(colorCamera);
auto sig = camera_set->signal<data::camera_set::added_camera_signal_t>(
data::camera_set::ADDED_CAMERA_SIG
);
sig->async_emit(colorCamera);
}
else
{
depthCamera = camera_set->get_camera(0);
colorCamera = camera_set->get_camera(1);
}
if(!depthCamera->getIsCalibrated() || !colorCamera->getIsCalibrated())
{
// copy device calibration into the camera series
data::matrix4::sptr matrix = std::make_shared<data::matrix4>();
const rs2_intrinsics depthIntrinsics = depthStream.get_intrinsics();
const rs2_intrinsics colorIntrinsics = colorStream.get_intrinsics();
const rs2_extrinsics extrinsic = depthStream.get_extrinsics_to(colorStream);
// Construct an explicit camera name: Intel RealSense D415(839112062452)
const std::string cameraName = std::string(m_currentDevice.get_info(RS2_CAMERA_INFO_NAME)) + "("
+ std::string(m_currentDevice.get_info(RS2_CAMERA_INFO_SERIAL_NUMBER))
+ ")";
depthCamera->setDescription(cameraName + " Depth");
depthCamera->setWidth(depthStreamW);
depthCamera->setHeight(depthStreamH);
depthCamera->setFx(static_cast<double>(depthIntrinsics.fx));
depthCamera->setFy(static_cast<double>(depthIntrinsics.fy));
depthCamera->setCx(static_cast<double>(depthIntrinsics.ppx));
depthCamera->setCy(static_cast<double>(depthIntrinsics.ppy));
depthCamera->setDistortionCoefficient(
static_cast<double>(depthIntrinsics.coeffs[0]),
static_cast<double>(depthIntrinsics.coeffs[1]),
static_cast<double>(depthIntrinsics.coeffs[2]),
static_cast<double>(depthIntrinsics.coeffs[3]),
static_cast<double>(depthIntrinsics.coeffs[4])
);
depthCamera->setIsCalibrated(true);
colorCamera->setDescription(cameraName + "Color");
colorCamera->setWidth(colorStreamW);
colorCamera->setHeight(colorStreamH);
colorCamera->setFx(static_cast<double>(colorIntrinsics.fx));
colorCamera->setFy(static_cast<double>(colorIntrinsics.fy));
colorCamera->setCx(static_cast<double>(colorIntrinsics.ppx));
colorCamera->setCy(static_cast<double>(colorIntrinsics.ppy));
colorCamera->setDistortionCoefficient(
static_cast<double>(colorIntrinsics.coeffs[0]),
static_cast<double>(colorIntrinsics.coeffs[1]),
static_cast<double>(colorIntrinsics.coeffs[2]),
static_cast<double>(colorIntrinsics.coeffs[3]),
static_cast<double>(colorIntrinsics.coeffs[4])
);
colorCamera->setIsCalibrated(true);
std::size_t index = 0;
for(std::size_t i = 0 ; i < 3 ; ++i)
{
(*matrix)(i, 3) = static_cast<double>(extrinsic.translation[i] * s_METERS_TO_MMS);
for(std::size_t j = 0 ; j < 3 ; ++j)
{
(*matrix)(i, j) = static_cast<double>(extrinsic.rotation[index]);
++index;
}
}
camera_set->set_extrinsic_matrix(1, matrix);
auto sig = camera_set->signal<data::camera_set::modified_signal_t>(
data::camera_set::MODIFIED_SIG
);
sig->async_emit();
}
}
}
//Only create the pointer one time.
auto pointcloud = std::make_shared<data::mesh>();
const auto dumpLock = pointcloud->dump_lock();
const data::mesh::size_t nbPoints = depthStreamW * depthStreamH;
// Allocate mesh.
pointcloud->resize(nbPoints, nbPoints, data::mesh::cell_type_t::POINT, data::mesh::attribute::point_colors);
// to display the mesh, we need to create cells with one point.
data::mesh::cell_t i = 0;
for(auto& cell : pointcloud->range<data::iterator::cell::point>())
{
cell.pt = i++;
}
pointcloud->truncate(nbPoints, nbPoints);
m_pointCloudOutput = pointcloud;
}
//-----------------------------------------------------------------------------
void scan::startCamera()
{
if(m_running)
{
SIGHT_WARN("Camera is still running. Nothing is done.");
return;
}
{
const auto camera_set = m_camera_set.lock();
const auto setPlaybackMode =
[&](data::camera::csptr camera)
{
if(camera->getCameraSource() == data::camera::file)
{
m_playbackMode = true;
m_playbackFileName = camera->getVideoFile().string();
this->signal<file_played_signal_t>(FILE_PLAYED_SIG)->async_emit();
}
else if(camera->getCameraSource() == data::camera::STREAM)
{
sight::module::io::realsense::scan::popMessageDialog(
"RealSense grabber cannot open STREAM type, please select DEVICE or FILE."
);
return;
}
// CameraSource is unknow or DEVICE we open a device. This allows to work
// without camera selector.
else
{
m_playbackMode = false;
// Test if previous device was kept (changing presets cases).
if(m_deviceID.empty())
{
m_deviceID = this->selectDevice();
}
this->signal<device_played_signal_t>(DEVICE_PLAYED_SIG)->async_emit();
}
};
if(camera_set)
{
// Extract the first camera (source should be the same).
data::mt::locked_ptr<data::camera> locked(camera_set->get_camera(0));
setPlaybackMode(locked.get_shared());
}
else // No camera_set (called by grabberProxy for ex.).
{
const auto locked = m_camera.lock();
setPlaybackMode(locked.get_shared());
SIGHT_ASSERT(
"Camera should not be null, check if '"
<< s_CAMERA_SET_INOUT
<< "' or '"
<< s_CAMERA_INPUT
<< "' is present.",
locked
);
}
}
rs2::config cfg;
m_pipe = std::make_unique<rs2::pipeline>();
rs2::pipeline_profile profile;
try
{
if(m_deviceID.empty() && !m_playbackMode)
{
throw std::runtime_error("No RealSense device was found.");
}
// Enable selected device if playback is disabled.
if(!m_playbackMode)
{
cfg.enable_device(m_deviceID);
}
// Enable depth stream (16 bit values)
cfg.enable_stream(
RS2_STREAM_DEPTH,
m_cameraSettings.depthW,
m_cameraSettings.depthH,
RS2_FORMAT_Z16,
m_cameraSettings.fps
);
// Enable color stream (RGBA8 is choosen to avoid conversion to sight).
cfg.enable_stream(
RS2_STREAM_COLOR,
m_cameraSettings.colorW,
m_cameraSettings.colorH,
RS2_FORMAT_RGBA8,
m_cameraSettings.fps
);
// Enable Infrared with the same parameters as the color one.
cfg.enable_stream(
RS2_STREAM_INFRARED,
m_cameraSettings.colorW,
m_cameraSettings.colorH,
RS2_FORMAT_RGBA8,
m_cameraSettings.fps
);
// Enable recording if needed.
if(m_record)
{
cfg.enable_record_to_file(m_recordingFileName);
}
// Enable playback if needed.
if(m_playbackMode)
{
cfg.enable_device_from_file(m_playbackFileName);
}
profile = m_pipe->start(cfg);
m_currentDevice = profile.get_device();
// Test if device support advanced mode.
if(m_currentDevice.is< ::rs400::advanced_mode>())
{
auto advanced_mode_dev = m_currentDevice.as< ::rs400::advanced_mode>();
if(!advanced_mode_dev.is_enabled())
{
// Enable advanced-mode.
advanced_mode_dev.toggle_advanced_mode(true);
SIGHT_DEBUG("Enable advanced mode on realsense device.");
}
}
else
{
// It's Ok if device is in playback mode.
if(!m_playbackMode)
{
throw std::runtime_error("The selected device doesn't support advanced mode. This is required!");
}
}
// Set a preset if there is one (can overwrite resolutions values).
if(!m_cameraSettings.presetPath.empty())
{
// load json content
std::ifstream json(m_cameraSettings.presetPath.string(), std::ifstream::in);
std::string json_content((std::istreambuf_iterator<char>(json)),
std::istreambuf_iterator<char>());
// Camera needs to be in "advanced mode"
auto advanced_mode_dev = m_currentDevice.as< ::rs400::advanced_mode>();
advanced_mode_dev.load_json(json_content);
}
auto depthSensor = m_currentDevice.first<rs2::depth_sensor>();
// Get the depth scale: depth in mm corresponding to a depth value of 1.
m_depthScale = depthSensor.get_depth_scale() * s_METERS_TO_MMS;
// Options are read-only when playing from files.
if(!m_playbackMode)
{
depthSensor.set_option(RS2_OPTION_EMITTER_ENABLED, (m_cameraSettings.irEmitter ? 1.F : 0.F));
}
}
catch(const std::exception& e)
{
sight::module::io::realsense::scan::popMessageDialog(
"RealSense device is not available. Please check if it is plugged in. Error : "
+ std::string(e.what())
);
return;
}
// Initialize calibration/pointcloud.
this->initialize(profile);
// Launch the grabbing thread.
m_running = true;
m_thread = std::thread(&scan::grab, this);
// Set min/max range if they parameters has changed (slot called before startCamera).
// This works only when grabbing thread is running.
if(m_cameraSettings.minRange > s_MIN_DEPTH_RANGE || m_cameraSettings.maxRange < s_MAX_DEPTH_RANGE)
{
this->setMinMaxRange();
}
auto sigStarted = this->signal<grabber::camera_started_signal_t>(
grabber::CAMERA_STARTED_SIG
);
sigStarted->async_emit();
}
//-----------------------------------------------------------------------------
void scan::stopCamera()
{
// Grabbing thread is running
if(m_running.exchange(false))
{
if(m_pause)
{
std::lock_guard<std::mutex> lock(m_pauseMutex);
m_pause = false;
}
m_pauseConditionVariable.notify_all();
m_thread.join();
// If a preset was loaded we should hard-reset the camera,
// If we don't some parameters are stored on the Camera ROM.
if(m_cameraSettings.needHardReset)
{
// Reset the device (if preset was loaded, ...)
m_currentDevice.hardware_reset();
// Wait until hardware_reset as been sent to the camera.
std::this_thread::sleep_for(std::chrono::seconds(1));
m_deviceID.clear();
}
m_pipe->stop();
m_pipe.reset();
auto sig = this->signal<grabber::camera_stopped_signal_t>(grabber::CAMERA_STOPPED_SIG);
sig->async_emit();
}
}
//-----------------------------------------------------------------------------
void scan::pauseCamera()
{
if(m_running)
{
// Enable/disable pause mode.
{
std::lock_guard<std::mutex> lock(m_pauseMutex);
m_pause = !m_pause;
}
m_pauseConditionVariable.notify_all();
// Also pause the recording if needed.
if(m_record)
{
if(m_pause)
{
m_currentDevice.as<rs2::recorder>().pause();
}
else
{
m_currentDevice.as<rs2::recorder>().resume();
}
}
}
}
//-----------------------------------------------------------------------------
void scan::record()
{
// Cannot record when playback a file.
if(m_playbackMode)
{
sight::module::io::realsense::scan::popMessageDialog("Cannot record when grabber playback a file !");
return;
}
// If already recording, stop it.
if(m_record)
{
m_record = false;
if(m_running)
{
// Restart camera to stop recording pipeline.
this->stopCamera();
this->startCamera();
}
return;
}
// Check recording file first.
bool erase = true;
// If file already exists, should we erase it ?
if(std::filesystem::exists(m_recordingFileName))
{
sight::ui::dialog::message warnDial;
warnDial.setIcon(ui::dialog::message::warning);
warnDial.setTitle("File already exists");
warnDial.setMessage(
"File: " + m_recordingFileName
+ " already exists, are you sure you want to erase it ?"
);
warnDial.addButton(ui::dialog::message::Buttons::YES);
warnDial.addButton(ui::dialog::message::Buttons::NO);
warnDial.setDefaultButton(ui::dialog::message::Buttons::NO);
const auto res = warnDial.show();
if(res == sight::ui::dialog::message::Buttons::NO)
{
erase = false;
}
}
// Ask user for a new file if filename is empty OR if filename exists but user don't want to erase it.
if(m_recordingFileName.empty() || (!erase && !m_recordingFileName.empty()))
{
// Configure recording path.
this->configureRecordingPath();
}
// If filename is still empty at this point = user cancel the location dialog or location is not valid, so skip
// recording.
if(m_recordingFileName.empty())
{
return;
}
// Everything is ok at this point, we can start recording.
m_record = true;
// If grabbing thread is running.
if(m_running)
{
// restart camera to enable recording pipeline.
this->stopCamera();
this->startCamera();
}
}
//-----------------------------------------------------------------------------
void scan::configureRecordingPath()
{
// Ask user for a new file name.
sight::ui::dialog::location dial;
dial.setTitle("Name of recording file");
dial.setType(ui::dialog::location::single_file);
dial.setOption(ui::dialog::location::WRITE);
dial.addFilter("Bag files", "*.bag");
auto result = std::dynamic_pointer_cast<core::location::single_file>(dial.show());
// If filename is ok.
if(result)
{
m_recordingFileName = result->get_file().string();
}
}
//------------------------------------------------------------------------------
void scan::set_parameter(ui::parameter_t _value, std::string _key)
{
try
{
// BOOL
// IR key
if(_key == s_IREMITTER)
{
// Save the value in cameraSettings.
m_cameraSettings.irEmitter = std::get<bool>(_value);
// Change the parameter live if grabber is running, otherwise it will be changed on next startCamera.
if(!m_deviceID.empty() && m_running)
{
auto depthSensor = m_currentDevice.first<rs2::depth_sensor>();
depthSensor.set_option(RS2_OPTION_EMITTER_ENABLED, (std::get<bool>(_value) ? 1.F : 0.F));
}
}
else if(_key == s_SWITCH_TO_IR)
{
m_switchInfra2Color = std::get<bool>(_value);
}
else if(_key == s_ENABLE_SPACIAL_FILTER)
{
m_filterSettings.enableSpacial = std::get<bool>(_value);
}
else if(_key == s_ENABLE_TEMPORAL_FILTER)
{
m_filterSettings.enableTemporal = std::get<bool>(_value);
}
else if(_key == s_ENABLE_HOLES_FILLING_FILTER)
{
m_filterSettings.enableHolesFilling = std::get<bool>(_value);
}
else if(_key == "minRange")
{
if(std::get<int>(_value) < s_MIN_DEPTH_RANGE)
{
throw std::runtime_error("cannot set value < 0");
}
m_cameraSettings.minRange = std::get<int>(_value);
m_cameraSettings.needHardReset = true;
}
else if(_key == "maxRange")
{
if(std::get<int>(_value) > s_MAX_DEPTH_RANGE)
{
throw std::runtime_error("cannot set value > 65535");
}
m_cameraSettings.maxRange = std::get<int>(_value);
m_cameraSettings.needHardReset = true;
}
else if(_key == s_SPACIAL_MAGNITUDE)
{
if(std::get<int>(_value) < 1 || std::get<int>(_value) > 5)
{
throw std::runtime_error(s_SPACIAL_MAGNITUDE + " value must be in [1-5].");
}
m_filterSettings.spacialMagnitude = static_cast<std::uint8_t>(std::get<int>(_value));
}
else if(_key == s_SPACIAL_SMOOTH_DELTA)
{
if(std::get<int>(_value) < 1 || std::get<int>(_value) > 50)
{
throw std::runtime_error(s_SPACIAL_SMOOTH_DELTA + " value must be in [1-50].");
}
m_filterSettings.spacialSmoothDelta = static_cast<std::uint8_t>(std::get<int>(_value));
}
else if(_key == s_SPACIAL_HOLE_FILLING)
{
// Skip if we have the value as a string, only use the index.
if(std::holds_alternative<std::string>(_value))
{
return;
}
if(std::get<int>(_value) < 0 || std::get<int>(_value) > 5)
{
throw std::runtime_error(s_SPACIAL_HOLE_FILLING + " value must be in [0-5].");
}
m_filterSettings.spacialHoleFilling = static_cast<std::uint8_t>(std::get<int>(_value));
}
else if(_key == s_TEMPORAL_SMOOTH_DELTA)
{
if(std::get<int>(_value) < 1 || std::get<int>(_value) > 100)
{
throw std::runtime_error(s_TEMPORAL_SMOOTH_DELTA + " value must be in [1-100].");
}
m_filterSettings.temporalSmoothDelta = static_cast<std::uint8_t>(std::get<int>(_value));
}
else if(_key == s_TEMPORAL_PERSISTENCY)
{
// Skip if we have the value as a string, only use the index.
if(std::holds_alternative<std::string>(_value))
{
return;
}
if(std::get<int>(_value) < 0 || std::get<int>(_value) > 8)
{
throw std::runtime_error(s_TEMPORAL_PERSISTENCY + " value must be in [0-8].");
}
m_filterSettings.temporalPersistency = static_cast<std::uint8_t>(std::get<int>(_value));
}
else if(_key == s_HOLE_FILLING)
{
// Skip if we have the value as a string, only use the index.
if(std::holds_alternative<std::string>(_value))
{
return;
}
if(std::get<int>(_value) < 0 || std::get<int>(_value) > 2)
{
throw std::runtime_error(s_HOLE_FILLING + " value must be in [0-2].");
}
m_filterSettings.holeFilling = static_cast<std::uint8_t>(std::get<int>(_value));
}
// ENUM
// Change preset (advanced option).
else if(_key == s_PRESET)
{
// Skip value is stored as index.
if(std::holds_alternative<int>(_value))
{
return;
}
const auto presetPathToLoad = m_jsonPresets[std::get<std::string>(_value)];
if(!presetPathToLoad.empty())
{
m_cameraSettings.presetPath = presetPathToLoad;
if(!m_deviceID.empty() && m_running)
{
// We need to restart the same camera.
// Make sure no hard-reset is performed in this particular case.
m_cameraSettings.needHardReset = false;
this->stopCamera();
this->startCamera();
}
// Ok now we should hard-reset if stopCamera() is called.
m_cameraSettings.needHardReset = true;
}
else
{
SIGHT_ERROR("Cannot load preset named: " + std::get<std::string>(_value) + ". Nothing append");
}
}
else if(_key == s_ALIGNMENT)
{
// Skip value is stored as index.
if(std::holds_alternative<int>(_value))
{
return;
}
if(this->updateAlignment(std::get<std::string>(_value)))
{
this->stopCamera();
this->startCamera();
}
}
// DOUBLE
else if(_key == s_SPACIAL_SMOOTH_ALPHA)
{
if(std::get<double>(_value) < 0.25 || std::get<double>(_value) > 1)
{
throw std::runtime_error(s_SPACIAL_SMOOTH_ALPHA + " must be in [0.25-1]");
}
m_filterSettings.spacialSmoothAlpha = static_cast<float>(std::get<double>(_value));
}
else if(_key == s_TEMPORAL_SMOOTH_ALPHA)
{
if(std::get<double>(_value) < 0 || std::get<double>(_value) > 1)
{
throw std::runtime_error(s_TEMPORAL_SMOOTH_ALPHA + " must be in [0-1]");
}
m_filterSettings.temporalSmoothAlpha = static_cast<float>(std::get<double>(_value));
}
else
{
SIGHT_ERROR("Key '" + _key + "' is not recognized.");
}
// Change parameters live if grabber is running, otherwise it will be changed on next call to startCamera.
if(!m_deviceID.empty() && m_running)
{
this->setMinMaxRange();
}
}
catch(const std::exception& e)
{
sight::module::io::realsense::scan::popMessageDialog("RealSense device error:" + std::string(e.what()));
return;
}
}
//-----------------------------------------------------------------------------
void scan::popMessageDialog(const std::string& _message)
{
sight::ui::dialog::message::show(
"RealSense Error",
_message,
sight::ui::dialog::message::CRITICAL
);
}
//-----------------------------------------------------------------------------
void scan::grab()
{
// Declare pointcloud object, for calculating pointclouds and texture mappings
rs2::pointcloud pc;
rs2::spatial_filter spatialFilter; // Spatial - edge-preserving spatial smoothing
rs2::temporal_filter temporalFilter; // Temporal - reduces temporal noise
rs2::hole_filling_filter holesFilter; // Holes filling
rs2::disparity_transform depthToDisparity(true); // transform depth to disparity
rs2::disparity_transform disparityToDepth(false); // transform disparity to depth
bool needAlignment = false;
rs2::align alignFrames(RS2_STREAM_COLOR);
// Since RS2_STREAM_* is an enum, and DEPTH, COLOR & INFRARED are stored in this order, we can eliminate other
// value, and default one.
if(m_cameraSettings.streamToAlignTo != RS2_STREAM_ANY && m_cameraSettings.streamToAlignTo < RS2_STREAM_FISHEYE)
{
alignFrames = rs2::align(m_cameraSettings.streamToAlignTo);
needAlignment = true;
}
// We want the points object to be persistent so we can display the last cloud when a frame drops
rs2::points points;
while(m_running)
{
while(m_pause && m_running)
{
std::unique_lock<std::mutex> lock(m_pauseMutex);
m_pauseConditionVariable.wait(lock);
}
try
{
// Wait for the next set of frames from the camera
auto frames = m_pipe->wait_for_frames();
// Align each frames in the chosen coordinate frames.
if(needAlignment)
{
frames = alignFrames.process(frames);
}
auto depth = frames.get_depth_frame();
auto color = frames.get_color_frame();
auto infra = frames.get_infrared_frame();
rs2::frame mapframe;
rs2::frame colorOrInfra;
// push infrared in color TL if needed.
m_switchInfra2Color ? colorOrInfra = infra : colorOrInfra = color;
// Generate the pointcloud and texture mappings
if(depth)
{
// transform depth to disparity to apply spacial and temporal filter
if(m_filterSettings.enableSpacial || m_filterSettings.enableTemporal)
{
depth = depthToDisparity.process(depth);
}
if(m_filterSettings.enableSpacial)
{
spatialFilter.set_option(RS2_OPTION_FILTER_MAGNITUDE, m_filterSettings.spacialMagnitude);
spatialFilter.set_option(RS2_OPTION_FILTER_SMOOTH_ALPHA, m_filterSettings.spacialSmoothAlpha);
spatialFilter.set_option(RS2_OPTION_FILTER_SMOOTH_DELTA, m_filterSettings.spacialSmoothDelta);
spatialFilter.set_option(RS2_OPTION_HOLES_FILL, m_filterSettings.spacialHoleFilling);
depth = spatialFilter.process(depth);
}
if(m_filterSettings.enableTemporal)
{
temporalFilter.set_option(RS2_OPTION_FILTER_SMOOTH_ALPHA, m_filterSettings.temporalSmoothAlpha);
temporalFilter.set_option(RS2_OPTION_FILTER_SMOOTH_DELTA, m_filterSettings.temporalSmoothDelta);
temporalFilter.set_option(RS2_OPTION_HOLES_FILL, m_filterSettings.temporalPersistency);
depth = temporalFilter.process(depth);
}
// transform disparity to depth
if(m_filterSettings.enableSpacial || m_filterSettings.enableTemporal)
{
depth = disparityToDepth.process(depth);
}
if(m_filterSettings.enableHolesFilling)
{
holesFilter.set_option(RS2_OPTION_HOLES_FILL, m_filterSettings.holeFilling);
depth = holesFilter.process(depth);
}
points = pc.calculate(depth);
if(m_pointcloudColorMap == PointcloudColormap::COLOR)
{
mapframe = color;
pc.map_to(color);
}
else if(m_pointcloudColorMap == PointcloudColormap::DEPTH)
{
mapframe = depth;
pc.map_to(depth);
}
else if(m_pointcloudColorMap == PointcloudColormap::INFRARED)
{
mapframe = infra;
pc.map_to(infra);
}
this->onPointCloud(points, mapframe);
}
if(m_depth.lock().get_shared())
{
this->onCameraImageDepth(reinterpret_cast<const std::uint16_t*>(depth.get_data()));
}
this->onCameraImage(reinterpret_cast<const std::uint8_t*>(colorOrInfra.get_data()));
// Compute the z value of the center pixel, to give the distance "object-camera" in mm.
const auto distanceToCenter = depth.get_distance(depth.get_width() / 2, depth.get_height() / 2);
this->signal<distance_computed_t>(DISTANCE_COMPUTED_SIG)->async_emit(
static_cast<double>(distanceToCenter * s_METERS_TO_MMS)
);
}
catch(const std::exception& e)
{
this->popMessageDialog("RealSense device error:" + std::string(e.what()));
return;
}
}
}
//-----------------------------------------------------------------------------
void scan::loadPresets(const ::fs::path& _path)
{
// 1. List all .json files in the folder.
::fs::directory_iterator end_itr;
// 1.1. Cycle through the directory
for(::fs::directory_iterator itr(_path) ; itr != end_itr ; ++itr)
{
// 1.2. Check only files with .json extension.
if(::fs::is_regular_file(itr->path()) && itr->path().extension() == ".json")
{
// 2. Generate "readable name" by removing "Preset*.json".
const std::string current_file = itr->path().filename().string();
const auto p = current_file.find("Preset");
const std::string name = current_file.substr(0, p);
// 3. Push in Map [Name, path].
m_jsonPresets[name] = itr->path();
}
}
}
//-----------------------------------------------------------------------------
bool scan::updateAlignment(const std::string& _alignTo)
{
if(_alignTo == "None")
{
m_cameraSettings.streamToAlignTo = RS2_STREAM_COUNT; // Equivalent to No alignment.
}
else if(_alignTo == "Color")
{
m_cameraSettings.streamToAlignTo = RS2_STREAM_COLOR;
}
else if(_alignTo == "Depth")
{
m_cameraSettings.streamToAlignTo = RS2_STREAM_DEPTH;
}
else if(_alignTo == "Infrared")
{
m_cameraSettings.streamToAlignTo = RS2_STREAM_INFRARED;
}
else
{
SIGHT_ERROR("'" + _alignTo + "' is not a valid alignment option (None, Color, Depth or Infrared).");
return false;
}
return true;
}
//-----------------------------------------------------------------------------
void scan::setMinMaxRange()
{
if(!m_playbackMode)
{
try
{
// Use the "advanced_mode" to set DepthClampMax value.
auto advanced_mode_dev = m_currentDevice.as<rs400::advanced_mode>();
auto depth_table = advanced_mode_dev.get_depth_table();
depth_table.depthClampMin = m_cameraSettings.minRange;
depth_table.depthClampMax = m_cameraSettings.maxRange;
advanced_mode_dev.set_depth_table(depth_table);
}
catch(const std::exception& e)
{
this->popMessageDialog("RealSense device error:" + std::string(e.what()));
return;
}
}
}
//-----------------------------------------------------------------------------
void scan::onCameraImage(const uint8_t* _buffer)
{
// Filling timeline's buffer
const fwClock::type timestamp(fwClock::get_time_in_milli_sec());
data::timeline::signals::pushed_t::sptr sig;
{
const auto colorTimeline = m_frame.lock();
const auto colorBuffer = colorTimeline->create_buffer(timestamp);
auto* destColorBuffer = reinterpret_cast<uint8_t*>(colorBuffer->addElement(0));
memcpy(destColorBuffer, _buffer, colorBuffer->size());
// Push buffer to timeline and notify
colorTimeline->pushObject(colorBuffer);
sig = colorTimeline->signal<data::timeline::signals::pushed_t>(data::timeline::signals::PUSHED);
}
sig->async_emit(timestamp);
}
//-----------------------------------------------------------------------------
void scan::onCameraImageDepth(const std::uint16_t* _buffer)
{
// Filling the depth image buffer in the timeline
const fwClock::type timestamp(fwClock::get_time_in_milli_sec());
data::timeline::signals::pushed_t::sptr sig;
{
const auto depthTimeline = m_depth.lock();
const auto depthTL = depthTimeline->create_buffer(timestamp);
const auto width = depthTimeline->getWidth();
const auto height = depthTimeline->getHeight();
auto* depthBuffer = reinterpret_cast<std::uint16_t*>(depthTL->addElement(0));
const auto sizeBuffer = width * height;
// Re-map depth frame in mm.
for(std::size_t i = 0 ; i < sizeBuffer ; ++i)
{
*depthBuffer++ = static_cast<std::uint16_t>(static_cast<float>(*_buffer++) * m_depthScale);
}
// Push buffer to timeline and notify
depthTimeline->pushObject(depthTL);
sig = depthTimeline->signal<data::timeline::signals::pushed_t>(data::timeline::signals::PUSHED);
}
sig->async_emit(timestamp);
}
//-----------------------------------------------------------------------------
void scan::onPointCloud(const rs2::points& _pc, const rs2::video_frame& _texture)
{
auto pointcloud = m_pointCloudOutput.lock();
if(pointcloud)
{
// Get Width and Height coordinates of texture
const int textureW = _texture.get_width(); // Frame width in pixels
const int textureH = _texture.get_height(); // Frame height in pixels
const int textureBytePerPix = _texture.get_bytes_per_pixel();
const int textureStrides = _texture.get_stride_in_bytes();
const auto* const textureBuff = reinterpret_cast<const uint8_t*>(_texture.get_data());
/* this segment actually prints the pointcloud */
const auto* const vertices = _pc.get_vertices();
const auto* const textureCoord = _pc.get_texture_coordinates();
const std::size_t pcSize = _pc.size();
// Parallelization of the loop is possible since each element is independent.
auto points = pointcloud->begin<data::iterator::point::xyz>();
auto colors = pointcloud->begin<data::iterator::point::rgba>();
for(std::int64_t i = 0 ; i < static_cast<std::int64_t>(pcSize) ; ++i)
{
// Fill the point buffer (x = +0, y = +1, z = +2).
points->x = static_cast<float>(vertices[i].x) * s_METERS_TO_MMS;
points->y = static_cast<float>(vertices[i].y) * s_METERS_TO_MMS;
points->z = static_cast<float>(vertices[i].z) * s_METERS_TO_MMS * m_depthScale; // Re-map to mm.
// Normals to texture Coordinates conversion
const int x_value = std::min(
std::max(
static_cast<int>(std::lround(textureCoord[i].u * static_cast<float>(textureW))),
0
),
textureW - 1
);
const int y_value = std::min(
std::max(
static_cast<int>(std::lround(textureCoord[i].v * static_cast<float>(textureH))),
0
),
textureH - 1
);
const int bytes = x_value * textureBytePerPix; // Get # of bytes per pixel
const int strides = y_value * textureStrides; // Get line width in bytes
const int index = (bytes + strides);
// Fill the color buffer (R = +0, G = +1, B = +2).
colors->r = textureBuff[index + 0];
colors->g = textureBuff[index + 1];
colors->b = textureBuff[index + 2];
colors->a = 255;
++points;
++colors;
}
const auto sigVertex = pointcloud->signal<data::mesh::signal_t>(data::mesh::VERTEX_MODIFIED_SIG);
sigVertex->async_emit();
const auto sigcolor = pointcloud->signal<data::mesh::signal_t>(data::mesh::POINT_COLORS_MODIFIED_SIG);
sigcolor->async_emit();
}
}
//-----------------------------------------------------------------------------
} // namespace sight::module::io::realsense
|