Переглянути джерело

Decoupled app implementation from scaffold work.

jcsyshc 1 рік тому
батько
коміт
472242c011

+ 1 - 0
CMakeLists.txt

@@ -5,6 +5,7 @@ set(CMAKE_CXX_STANDARD 20)
 
 add_executable(${PROJECT_NAME} src/main.cpp
         src/impl/main_impl.cpp
+        src/impl/apps/depth_guide/depth_guide.cpp
         src/core/impl/event_timer.cpp
         src/core/impl/memory_pool.cpp
         src/core/impl/object_manager.cpp

+ 28 - 0
src/impl/app_base.h

@@ -0,0 +1,28 @@
+#ifndef DEPTHGUIDE_APP_BASE_H
+#define DEPTHGUIDE_APP_BASE_H
+
+#include "core/cuda_helper.hpp"
+
+#include <boost/asio/io_context.hpp>
+
+class app_base {
+public:
+
+    using io_context = boost::asio::io_context;
+
+    struct create_config {
+        io_context *asio_ctx = nullptr;
+        CUcontext *cuda_ctx = nullptr;
+    };
+
+    virtual ~app_base() = default;
+
+    virtual const char *window_name() = 0;
+
+    virtual void show_ui() = 0;
+
+    virtual void render_background() = 0;
+
+};
+
+#endif //DEPTHGUIDE_APP_BASE_H

+ 90 - 0
src/impl/apps/depth_guide/depth_guide.cpp

@@ -0,0 +1,90 @@
+#include "depth_guide.h"
+#include "core/image_utility.hpp"
+#include "core/imgui_utility.hpp"
+
+app_depth_guide::app_depth_guide(create_config _conf) {
+    conf = _conf;
+
+    // initialize object manager
+    OBJ_SAVE(img_color, image_u8c3());
+    OBJ_SAVE(img_depth, image_f32c1());
+    OBJ_SAVE(img_bg, image_u8c3());
+    OBJ_SAVE(img_out, image_u8c4());
+
+    OBJ_SIG(img_color)->connect(INT_MIN, [=](obj_name_type _) {
+        OBJ_SAVE(img_bg, OBJ_QUERY(image_u8c3, img_color));
+    });
+
+    // initialize modules
+    auto orb_cam_conf = orb_camera_ui::create_config{
+            .cf_name = img_color, .df_name = img_depth,
+            .ctx = conf.asio_ctx, .stream = default_cuda_stream,
+    };
+    orb_cam = std::make_unique<orb_camera_ui>(orb_cam_conf);
+
+    auto bg_viewer_conf = image_viewer::create_config{
+            .mode = VIEW_COLOR_DEPTH, .flip_y = true,
+            .stream = default_cuda_stream,
+    };
+    auto &bg_extra_conf = bg_viewer_conf.extra.color_depth;
+    bg_extra_conf.c_name = img_color;
+    bg_extra_conf.d_name = img_depth;
+    bg_viewer = std::make_unique<image_viewer>(bg_viewer_conf);
+
+    auto out_down_conf = viewport_downloader::create_config{
+            .type = PIX_RGBA, .stream = default_cuda_stream
+    };
+    out_downloader = std::make_unique<viewport_downloader>(out_down_conf);
+
+    auto out_streamer_conf = image_streamer::create_config{
+            .img_name = img_out, .asio_ctx = conf.asio_ctx,
+            .cuda_ctx = conf.cuda_ctx, .stream = default_cuda_stream
+    };
+    out_streamer = std::make_unique<image_streamer>(out_streamer_conf);
+}
+
+void app_depth_guide::show_ui() {
+    auto ctx = conf.asio_ctx;
+    if (ImGui::Begin("Depth Guide Control")) {
+        ImGui::PushItemWidth(200);
+
+        if (ImGui::CollapsingHeader("Camera")) {
+            auto id_guard = imgui_id_guard("camera");
+            orb_cam->show();
+        }
+
+        if (ImGui::CollapsingHeader("Streamer")) {
+            auto id_guard = imgui_id_guard("streamer");
+            out_streamer->show();
+        }
+
+        if (ImGui::CollapsingHeader("Debug")) {
+            if (ImGui::TreeNode("Background")) {
+                bg_viewer->show();
+                ImGui::TreePop();
+            }
+            if (ImGui::TreeNode("Memory Pool")) {
+                if (ImGui::Button("Purge")) {
+                    post(*ctx, [] { global_mp.purge(); });
+                }
+                ImGui::TreePop();
+            }
+            if (ImGui::TreeNode("Performance")) {
+                ImGui::Text("UI Refresh Rate: %.2fms", perf_timer.query().interval);
+                ImGui::TreePop();
+            }
+        }
+
+        ImGui::PopItemWidth();
+    }
+    ImGui::End();
+    perf_timer.record();
+}
+
+void app_depth_guide::render_background() {
+    bg_viewer->render();
+
+    // TODO: for test
+    auto bg_img = out_downloader->download_rgba();
+    OBJ_SAVE(img_out, bg_img);
+}

+ 54 - 0
src/impl/apps/depth_guide/depth_guide.h

@@ -0,0 +1,54 @@
+#ifndef DEPTHGUIDE_DEPTH_GUIDE_H
+#define DEPTHGUIDE_DEPTH_GUIDE_H
+
+#include "core/event_timer.h"
+#include "core/object_manager.h"
+#include "device/orb_camera_ui.h"
+#include "module/image_streamer.h"
+#include "module/image_viewer.h"
+#include "module/viewport_downloader.hpp"
+#include "impl/app_base.h"
+
+#include <boost/asio/io_context.hpp>
+
+class app_depth_guide : public app_base {
+public:
+
+    explicit app_depth_guide(create_config conf);
+
+    ~app_depth_guide() override = default;
+
+    const char *window_name() override { return "DepthGuide V1.-1"; }
+
+    void show_ui() override;
+
+    void render_background() override;
+
+private:
+
+    enum obj_names : object_manager::name_type {
+
+        // images from device
+        img_color, img_depth,
+
+        // background image
+        img_bg,
+
+        // output image
+        img_out,
+    };
+
+    create_config conf;
+
+    // modules
+    std::unique_ptr<orb_camera_ui> orb_cam;
+    std::unique_ptr<image_viewer> bg_viewer; // background viewer
+    std::unique_ptr<viewport_downloader> out_downloader;
+    std::unique_ptr<image_streamer> out_streamer; // output streamer
+
+    // miscellaneous
+    event_timer perf_timer; // performance timer
+
+};
+
+#endif //DEPTHGUIDE_DEPTH_GUIDE_H

+ 20 - 101
src/impl/main_impl.cpp

@@ -1,11 +1,6 @@
 #include "main_impl.h"
-#include "device/orb_camera_ui.h"
-#include "core/event_timer.h"
-#include "core/image_utility.hpp"
-#include "module/image_streamer.h"
-#include "module/image_viewer.h"
-#include "module/viewport_downloader.hpp"
-#include "object_names.h"
+#include "core/object_manager.h"
+#include "impl/apps/depth_guide/depth_guide.h"
 
 #include <boost/asio/io_context.hpp>
 #include <boost/asio/post.hpp>
@@ -32,15 +27,11 @@ smart_cuda_stream *default_cuda_stream = nullptr;
 io_context *main_ctx;
 object_manager *main_ob;
 
-event_timer perf_timer; // performance timer
+//event_timer perf_timer; // performance timer
 std::unique_ptr<steady_timer> ui_timer;
 std::chrono::milliseconds ui_interval;
 
-// modules
-std::unique_ptr<orb_camera_ui> orb_cam;
-std::unique_ptr<image_viewer> bg_viewer; // background viewer
-std::unique_ptr<viewport_downloader> out_downloader;
-std::unique_ptr<image_streamer> out_streamer; // output streamer
+std::unique_ptr<app_base> app;
 
 void init_cuda() {
     cuInit(0);
@@ -68,7 +59,8 @@ void init_window() {
     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
-    window = glfwCreateWindow(800, 600, "DepthGuide V1.-1", nullptr, nullptr); // TODO: select width and height
+    // TODO: select width and height
+    window = glfwCreateWindow(800, 600, "An not simple platform for visual navigation", nullptr, nullptr);
     assert(window != nullptr);
     glfwMakeContextCurrent(window);
     glfwSwapInterval(0);
@@ -114,47 +106,6 @@ void init_window() {
     });
 }
 
-void init_om() {
-    main_ctx = new io_context();
-    main_ob = new object_manager({.ctx = main_ctx});
-    OBJ_SAVE(img_color, image_u8c3());
-    OBJ_SAVE(img_depth, image_f32c1());
-    OBJ_SAVE(img_bg, image_u8c3());
-    OBJ_SAVE(img_out, image_u8c4());
-
-    OBJ_SIG(img_color)->connect(INT_MIN, [=](obj_name_type _) {
-        OBJ_SAVE(img_bg, OBJ_QUERY(image_u8c3, img_color));
-    });
-}
-
-void init_modules() {
-    auto orb_cam_conf = orb_camera_ui::create_config{
-            .cf_name = img_color, .df_name = img_depth,
-            .ctx = main_ctx, .stream = default_cuda_stream,
-    };
-    orb_cam = std::make_unique<orb_camera_ui>(orb_cam_conf);
-
-    auto bg_viewer_conf = image_viewer::create_config{
-            .mode = VIEW_COLOR_DEPTH, .flip_y = true,
-            .stream = default_cuda_stream,
-    };
-    auto &bg_extra_conf = bg_viewer_conf.extra.color_depth;
-    bg_extra_conf.c_name = img_color;
-    bg_extra_conf.d_name = img_depth;
-    bg_viewer = std::make_unique<image_viewer>(bg_viewer_conf);
-
-    auto out_down_conf = viewport_downloader::create_config{
-            .type = PIX_RGBA, .stream = default_cuda_stream
-    };
-    out_downloader = std::make_unique<viewport_downloader>(out_down_conf);
-
-    auto out_streamer_conf = image_streamer::create_config{
-            .img_name = img_out, .asio_ctx = main_ctx,
-            .cuda_ctx = &cuda_ctx, .stream = default_cuda_stream
-    };
-    out_streamer = std::make_unique<image_streamer>(out_streamer_conf);
-}
-
 void ui_timer_func(error_code ec) {
     if (ec == boost::asio::error::operation_aborted) return;
     assert(ec == error_code());
@@ -166,9 +117,17 @@ void ui_timer_func(error_code ec) {
 void init_all() {
     init_cuda();
     init_window();
-    init_om();
-    init_modules();
 
+    main_ctx = new io_context();
+    main_ob = new object_manager({.ctx = main_ctx});
+
+    auto app_config = app_base::create_config{
+            .asio_ctx = main_ctx, .cuda_ctx = &cuda_ctx
+    };
+    // TODO: switch app here
+    app = std::make_unique<app_depth_guide>(app_config);
+
+    glfwSetWindowTitle(window, app->window_name());
     ui_interval = std::chrono::milliseconds(33); // TODO: select refresh rate
     ui_timer = std::make_unique<steady_timer>(*main_ctx, ui_interval);
     ui_timer->async_wait(ui_timer_func);
@@ -186,39 +145,8 @@ void show_ui() {
         return;
     }
 
-    if (ImGui::Begin("Depth Guide Control")) {
-        ImGui::PushItemWidth(200);
-
-        if (ImGui::CollapsingHeader("Camera")) {
-            auto id_guard = imgui_id_guard("camera");
-            orb_cam->show();
-        }
-
-        if (ImGui::CollapsingHeader("Streamer")) {
-            auto id_guard = imgui_id_guard("streamer");
-            out_streamer->show();
-        }
-
-        if (ImGui::CollapsingHeader("Debug")) {
-            if (ImGui::TreeNode("Background")) {
-                bg_viewer->show();
-                ImGui::TreePop();
-            }
-            if (ImGui::TreeNode("Memory Pool")) {
-                if (ImGui::Button("Purge")) {
-                    post(*main_ctx, [] { global_mp.purge(); });
-                }
-                ImGui::TreePop();
-            }
-            if (ImGui::TreeNode("Performance")) {
-                ImGui::Text("UI Refresh Rate: %.2fms", perf_timer.query().interval);
-                ImGui::TreePop();
-            }
-        }
-
-        ImGui::PopItemWidth();
-    }
-    ImGui::End();
+    assert(app != nullptr);
+    app->show_ui();
     ImGui::Render();
 
     cv::Size frame_size;
@@ -227,24 +155,15 @@ void show_ui() {
     glViewport(0, 0, frame_size.width, frame_size.height);
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
-    bg_viewer->render();
-
-    // TODO: for test
-    auto bg_img = out_downloader->download_rgba();
-    OBJ_SAVE(img_out, bg_img);
+    app->render_background();
 
     ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
     glfwSwapBuffers(window);
-    perf_timer.record();
 }
 
 void cleanup() {
+    app = nullptr;
     ui_timer = nullptr;
-    orb_cam = nullptr;
-    bg_viewer = nullptr;
-    out_downloader = nullptr;
-    out_streamer = nullptr;
-
     delete main_ob;
     delete main_ctx;
 }

+ 0 - 8
src/impl/main_impl.h

@@ -7,14 +7,6 @@
 
 extern boost::asio::io_context *main_ctx;
 
-void init_cuda();
-
-void init_window();
-
-void init_om();
-
-void init_modules();
-
 void init_all();
 
 void show_ui();

+ 3 - 1
src/main.cpp

@@ -7,7 +7,9 @@
 using boost::asio::io_context;
 
 int main() {
-//    spdlog::set_level(spdlog::level::trace);
+#ifndef NDEBUG
+    spdlog::set_level(spdlog::level::trace);
+#endif
     init_all();
     main_ctx->run();
     cleanup();