depth_guide_v2.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include "depth_guide_v2.h"
  2. #include "core/imgui_utility.hpp"
  3. #include "image_process/process_funcs.h"
  4. #include <glm/gtx/transform.hpp>
  5. app_depth_guide_v2::app_depth_guide_v2(const create_config &_conf) {
  6. main_conf = _conf;
  7. auto conf = main_conf.ext_conf;
  8. // initialize object manager
  9. OBJ_SAVE(img_color, image_u8c3());
  10. OBJ_SAVE(img_depth, image_f32c1());
  11. OBJ_SAVE(img_remap, image_ptr());
  12. OBJ_SAVE(pc_raw, pc_ptr());
  13. OBJ_SAVE(scene_out, scene_ptr());
  14. OBJ_SAVE(img_out_left, image_ptr());
  15. OBJ_SAVE(img_out_right, image_ptr());
  16. OBJ_SAVE(img_out, image_ptr());
  17. // initialize meta
  18. OBJ_PIN_META(pc_raw, META_SERIES_NAME, meta_hash("pc_raw"));
  19. OBJ_PIN_META(img_remap, META_SERIES_NAME, meta_hash("img_remap"));
  20. OBJ_PIN_META(pc_raw, META_DEPTH_MIN, (float) 0.0f); // TODO: make adjustable
  21. OBJ_PIN_META(pc_raw, META_DEPTH_MAX, (float) 3000.0f);
  22. OBJ_SIG(img_color)->connect([](auto _) {
  23. gen_pc_rgbd::call(
  24. {img_color, img_depth, img_remap,
  25. pc_raw, default_cuda_stream});
  26. });
  27. OBJ_SIG(pc_raw)->connect([this](auto _) {
  28. aug_manager->update(aug_helper->get_camera());
  29. auto out_conf = camera_augment_helper_v2::output_config{
  30. .size = aug_stereo->get_suggest_size(), .stream = default_cuda_stream,
  31. };
  32. out_conf.img_name = img_out_left;
  33. aug_left->render_image(out_conf);
  34. out_conf.img_name = img_out_right;
  35. aug_right->render_image(out_conf);
  36. });
  37. // initialize sophiar
  38. sophiar_thread = std::make_unique<std::thread>([conf_path = LOAD_STR("sophiar_config")] {
  39. sophiar::run_sophiar(conf_path);
  40. });
  41. sophiar_conn = std::make_unique<sophiar_conn_type>();
  42. sophiar_start_var = LOAD_STR("sophiar_start_var");
  43. // initialize modules
  44. auto orb_cam_conf = orb_camera_ui::create_config{
  45. .cf_name = img_color, .df_name = img_depth,
  46. .remap_name = img_remap,
  47. .ctx = main_conf.asio_ctx, .stream = default_cuda_stream,
  48. };
  49. orb_cam = std::make_unique<orb_camera_ui>(orb_cam_conf);
  50. auto aug_list_v1 = augment_manager::item_list_from_yaml(LOAD_LIST("augment_list"));
  51. auto aug_list = augment_manager_v2::item_list_from_v1(aug_list_v1);
  52. auto aug_conf = augment_manager_v2::create_config{
  53. .scene_name = scene_out,
  54. .sophiar_conn = sophiar_conn.get(),
  55. .stream = default_cuda_stream,
  56. };
  57. aug_conf.item_list.push_back(
  58. {.type = augment_manager_v2::ITEM_PC,
  59. .disp_name = "PC Raw", .name = pc_raw,
  60. .transform_var = LOAD_STR("camera_transform_var")});
  61. std::copy(aug_list.begin(), aug_list.end(), std::back_inserter(aug_conf.item_list));
  62. aug_manager = std::make_unique<augment_manager_v2>(aug_conf);
  63. auto guide_conf = cursor_guide::create_config{
  64. .item_list = cursor_guide::item_list_from_aug(aug_list),
  65. .sophiar_conn = sophiar_conn.get(),
  66. };
  67. aug_guide = std::make_unique<cursor_guide>(guide_conf);
  68. auto view_conf = camera_augment_helper_v2::create_config{
  69. .camera = camera_augment_helper_v2::create_config::freedom_camera_config{
  70. .transform_var = LOAD_STR("camera_transform_var")},
  71. .sophiar_conn = sophiar_conn.get(),
  72. .manager = aug_manager.get(),
  73. .act_guide = aug_guide.get(),
  74. .ctx = main_conf.asio_ctx,
  75. };
  76. aug_helper = std::make_unique<camera_augment_helper_v2>(view_conf);
  77. aug_helper->set_active_guidance(true);
  78. view_conf.camera = camera_augment_helper_v2::create_config::relative_camera_config{
  79. .parent = aug_helper.get(),
  80. .transform = glm::translate(glm::vec3(-baseline_dis / 2, 0, 0))};
  81. aug_left = std::make_unique<camera_augment_helper_v2>(view_conf);
  82. aug_left->sync_with(aug_helper.get());
  83. view_conf.camera = camera_augment_helper_v2::create_config::relative_camera_config{
  84. .parent = aug_helper.get(),
  85. .transform = glm::translate(glm::vec3(baseline_dis / 2, 0, 0))};
  86. aug_right = std::make_unique<camera_augment_helper_v2>(view_conf);
  87. aug_right->sync_with(aug_helper.get());
  88. auto output_size = cv::Size(
  89. LOAD_NUMBER(int, "output_width"),
  90. LOAD_NUMBER(int, "output_height"));
  91. auto stereo_conf = stereo_augment_helper::create_config{
  92. .left_name = img_out_left, .right_name = img_out_right,
  93. .out_name = img_out, .fbo_size = output_size,
  94. .flip_image = true, .stream = default_cuda_stream,
  95. };
  96. aug_stereo = std::make_unique<stereo_augment_helper>(stereo_conf);
  97. auto out_conf = image_streamer::create_config{
  98. .img_name = img_out, .encode_scene = false,
  99. .asio_ctx = main_conf.asio_ctx,
  100. .cuda_ctx = main_conf.cuda_ctx, .stream = default_cuda_stream
  101. };
  102. out_streamer = std::make_unique<image_streamer>(out_conf);
  103. auto reg_conf = registration_config{
  104. .conn = sophiar_conn.get(),
  105. .probe_model_path = LOAD_STR("probe_model"),
  106. };
  107. reg.reset(registration::create(reg_conf));
  108. for (auto reg_item: LOAD_LIST("registration_list")) {
  109. auto item_conf = registration_target::from_yaml(reg_item);
  110. reg->add_target(item_conf);
  111. }
  112. }
  113. void app_depth_guide_v2::show_ui() {
  114. if (ImGui::Begin("Depth Guide Control")) {
  115. ImGui::PushItemWidth(200);
  116. if (ImGui::CollapsingHeader("Camera")) {
  117. auto id_guard = imgui_id_guard("camera");
  118. orb_cam->show();
  119. }
  120. if (ImGui::CollapsingHeader("Augment")) {
  121. ImGui::SeparatorText("Tracker");
  122. if (ImGui::Button("Start")) {
  123. start_tracking();
  124. }
  125. ImGui::SameLine();
  126. ImGui::Checkbox("Registration", &enable_reg);
  127. {
  128. ImGui::SeparatorText("Scene");
  129. auto id_guard = imgui_id_guard("augment_scene");
  130. aug_manager->show();
  131. }
  132. {
  133. ImGui::SeparatorText("Camera");
  134. auto id_guard = imgui_id_guard("augment_camera");
  135. aug_helper->show();
  136. }
  137. {
  138. ImGui::SeparatorText("Stereo");
  139. auto id_guard = imgui_id_guard("stereo");
  140. aug_stereo->show();
  141. }
  142. }
  143. if (ImGui::CollapsingHeader("Streamer")) {
  144. auto id_guard = imgui_id_guard("streamer");
  145. out_streamer->show();
  146. }
  147. ImGui::PopItemWidth();
  148. }
  149. ImGui::End();
  150. if (enable_reg) {
  151. reg->process();
  152. reg->show();
  153. }
  154. }
  155. void app_depth_guide_v2::render_background() {
  156. aug_helper->render();
  157. }
  158. void app_depth_guide_v2::start_tracking() {
  159. // sophiar_conn->start_object(sophiar_start_var);
  160. // work in another thread to prevent blocking
  161. auto t = std::thread([this] {
  162. auto conn = sophiar::local_connection();
  163. conn.start_object(sophiar_start_var);
  164. });
  165. t.detach();
  166. }
  167. app_depth_guide_v2::~app_depth_guide_v2() {
  168. // sophiar
  169. sophiar::stop_sophiar();
  170. sophiar_thread->join();
  171. }