Browse Source

Implemented mesh no transfer.

jcsyshc 1 year ago
parent
commit
855148a900

+ 7 - 1
data/config_scene_player.yaml

@@ -1 +1,7 @@
-app_name: scene_player
+app_name: scene_player
+
+mesh_list:
+  - name: Femur
+    path: /home/tpx/project/DepthGuide/data/model/Femur_3.stl
+  - name: Tibia
+    path: /home/tpx/project/DepthGuide/data/model/Tibia_3.stl

+ 1 - 0
src/codec/mesh_codec.hpp

@@ -13,6 +13,7 @@ namespace mesh_codec {
     static constexpr flag_type FLAG_NEW = 0x00;
     static constexpr flag_type FLAG_CACHED = 0x01;
     static constexpr flag_type FLAG_NO_CACHE = 0x02;
+    static constexpr flag_type FLAG_NO_TRANSFER = 0x03;
 
     static constexpr ptrdiff_t pre_size =
             sizeof(flag_type) + sizeof(hash_type);

+ 44 - 0
src/codec/mesh_decoder.cpp

@@ -1,10 +1,34 @@
 #include "mesh_decoder.h"
 #include "mesh_codec.hpp"
+#include "third_party/static_block.hpp"
+#include "impl/main_impl.h"
 
 #include <unordered_map>
 
 using namespace mesh_codec;
 
+namespace mesh_decoder_impl {
+
+    using mesh_map_type =
+            std::unordered_map<size_t, mesh_ptr>;
+    mesh_map_type mesh_pool;
+
+    static_block {
+        register_cleanup_func([] { mesh_pool.clear(); });
+    }
+
+    using hasher_type = std::hash<std::string>;
+    hasher_type name_hasher;
+
+    mesh_ptr get_no_transfer_mesh(size_t name) {
+        assert(mesh_pool.contains(name));
+        return mesh_pool.at(name);
+    }
+
+}
+
+using namespace mesh_decoder_impl;
+
 struct mesh_decoder::impl {
 
     using cache_map_type =
@@ -26,6 +50,12 @@ struct mesh_decoder::impl {
     mesh_ptr decode(const data_type &data) {
         auto reader = network_reader(data);
         auto flag = reader.read_value<flag_type>();
+
+        if (flag == FLAG_NO_TRANSFER) {
+            auto name = reader.read_value<size_t>();
+            return get_no_transfer_mesh(name);
+        }
+
         auto id = reader.read_value<hash_type>();
         if (cache_map.contains(id)) {
             return cache_map.at(id);
@@ -52,4 +82,18 @@ mesh_decoder::~mesh_decoder() = default;
 
 mesh_ptr mesh_decoder::decode(const data_type &data) {
     return pimpl->decode(data);
+}
+
+void register_mesh_file(const std::string &name,
+                        const std::string &path) {
+    auto mesh = mesh_type::create({.path = path});
+    auto name_id = name_hasher(name);
+    assert(!mesh_pool.contains(name_id));
+    mesh_pool.emplace(name_id, mesh);
+}
+
+void register_mesh_list(const YAML::Node &conf_list) {
+    for (auto &conf: conf_list) {
+        register_mesh_file(LOAD_STR("name"), LOAD_STR("path"));
+    }
 }

+ 5 - 0
src/codec/mesh_decoder.h

@@ -1,6 +1,7 @@
 #ifndef DEPTHGUIDE_MESH_DECODER_H
 #define DEPTHGUIDE_MESH_DECODER_H
 
+#include "core/yaml_utility.hpp"
 #include "render/render_mesh.h"
 
 #include <memory>
@@ -22,5 +23,9 @@ private:
     std::unique_ptr<impl> pimpl;
 };
 
+void register_mesh_file(const std::string &name,
+                        const std::string &path);
+
+void register_mesh_list(const YAML::Node &conf);
 
 #endif //DEPTHGUIDE_MESH_DECODER_H

+ 9 - 0
src/codec/mesh_encoder.cpp

@@ -28,6 +28,15 @@ struct mesh_encoder::impl {
     }
 
     data_type encode(const mesh_ptr &mesh) {
+        // handle no transfer situation
+        if (auto no_transfer_any = mesh->get_meta_any(META_MESH_NO_TRANSFER);
+                boost::any_cast<bool>(no_transfer_any)) {
+            auto name = mesh->get_meta_ext<size_t>(META_SERIES_NAME);
+            auto writer = network_writer();
+            writer << FLAG_NO_TRANSFER << name;
+            return writer.current_data();
+        }
+
         static constexpr auto hash_seed = 0;
         auto ret = encode_mesh(mesh);
         auto id = XXH64(ret.start_ptr(), ret.size, hash_seed);

+ 1 - 1
src/core/image_utility_v2.h

@@ -161,7 +161,7 @@ private:
 
 public:
 
-    explicit generic_image(std::unique_ptr<impl> &&pimpl);
+    explicit generic_image(std::unique_ptr<impl> pimpl);
 };
 
 using image_ptr = generic_image::pointer;

+ 4 - 2
src/core/impl/image_utility_v2.cpp

@@ -324,8 +324,10 @@ bool generic_image::basic_info_type::operator==(const basic_info_type &o) const
     return true;
 }
 
-generic_image::generic_image(std::unique_ptr<impl> &&_pimpl)
-        : meta_base(_pimpl.get()), pimpl(std::move(_pimpl)) {
+generic_image::generic_image(std::unique_ptr<impl> _pimpl)
+        : meta_base(_pimpl.get()) {
+    pimpl = std::move(_pimpl);
+    assert(pimpl != nullptr);
     pimpl->q_this = this;
 }
 

+ 4 - 2
src/core/impl/pc_utility.cpp

@@ -169,8 +169,10 @@ void generic_pc::impl::cuda_modified(smart_cuda_stream *stream) {
     REC_CREATE(store_cuda.ptr, stream);
 }
 
-generic_pc::generic_pc(std::unique_ptr<impl> &&_pimpl)
-        : meta_base(_pimpl.get()), pimpl(std::move(_pimpl)) {
+generic_pc::generic_pc(std::unique_ptr<impl> _pimpl)
+        : meta_base(_pimpl.get()) {
+    pimpl = std::move(_pimpl);
+    assert(pimpl != nullptr);
     pimpl->q_this = this;
 }
 

+ 1 - 1
src/core/pc_utility.h

@@ -90,7 +90,7 @@ private:
     friend class pc_memory;
 
 public:
-    explicit generic_pc(std::unique_ptr<impl> &&pimpl);
+    explicit generic_pc(std::unique_ptr<impl> pimpl);
 
 };
 

+ 7 - 3
src/impl/apps/scene_player/scene_player.cpp

@@ -1,15 +1,17 @@
 #include "scene_player.h"
+#include "codec/mesh_decoder.h"
 #include "core/imgui_utility.hpp"
 
 app_scene_player::app_scene_player(const create_config &_conf) {
-    conf = _conf;
+    main_conf = _conf;
+    auto &conf = main_conf.ext_conf;
 
     // initialize object manager
     OBJ_SAVE(scene_in, scene_ptr());
 
     // initialize modules
     auto in_conf = image_player::create_config{
-            .img_name = scene_in, .ctx = conf.asio_ctx,
+            .img_name = scene_in, .ctx = main_conf.asio_ctx,
             .decode_scene = true, .stream = default_cuda_stream,
     };
     in_streamer = std::make_unique<image_player>(in_conf);
@@ -23,10 +25,12 @@ app_scene_player::app_scene_player(const create_config &_conf) {
     auto view_conf = camera_augment_helper_v2::create_config{
             .camera = camera_augment_helper_v2::create_config::freedom_camera_config{},
             .manager = aug_manager.get(),
-            .ctx = conf.asio_ctx,
+            .ctx = main_conf.asio_ctx,
     };
     aug_helper = std::make_unique<camera_augment_helper_v2>(view_conf);
     aug_helper->set_interactive(true);
+
+    register_mesh_list(LOAD_LIST("mesh_list"));
 }
 
 void app_scene_player::show_ui() {

+ 1 - 1
src/impl/apps/scene_player/scene_player.h

@@ -29,7 +29,7 @@ private:
         scene_in,
     };
 
-    create_config conf;
+    create_config main_conf;
 
     // modules
     std::unique_ptr<image_player> in_streamer;

+ 14 - 0
src/impl/main_impl.cpp

@@ -27,6 +27,10 @@ smart_cuda_stream *default_cuda_stream = nullptr;
 io_context *main_ctx;
 object_manager *main_ob;
 
+using cleanup_list_type =
+        std::vector<cleanup_func_type>;
+cleanup_list_type cleanup_list;
+
 //event_timer perf_timer; // performance timer
 std::unique_ptr<steady_timer> ui_timer;
 std::chrono::milliseconds ui_interval;
@@ -175,9 +179,19 @@ void show_ui() {
     glfwSwapBuffers(window);
 }
 
+void register_cleanup_func(cleanup_func_type func) {
+    cleanup_list.push_back(func);
+}
+
 void cleanup() {
     app = nullptr;
     ui_timer = nullptr;
+
+    // custom cleanup funcs
+    for (auto func: cleanup_list) {
+        func();
+    }
+
     delete main_ob;
     delete main_ctx;
 }

+ 4 - 0
src/impl/main_impl.h

@@ -11,6 +11,10 @@ void init_all();
 
 void show_ui();
 
+using cleanup_func_type = void (*)();
+
+void register_cleanup_func(cleanup_func_type func);
+
 void cleanup();
 
 #endif //DEPTHGUIDE_MAIN_IMPL_H

+ 3 - 0
src/module/impl/augment_manager_v2.cpp

@@ -197,6 +197,7 @@ augment_manager_v2::item_list_from_v1(const item_list_v1_type &items,
     auto ret = item_list_type();
     ret.reserve(items.size());
 
+    auto hasher = std::hash<std::string>();
     using item_type = create_config::item_type;
     for (auto &v1: items) {
         auto item = item_type{
@@ -210,6 +211,8 @@ augment_manager_v2::item_list_from_v1(const item_list_v1_type &items,
 
         // create mesh
         auto mesh = mesh_type::create({.path = v1.model_path});
+        mesh->set_meta_any(META_SERIES_NAME, (size_t) hasher(v1.name));
+        mesh->set_meta_any(META_MESH_NO_TRANSFER, true);
         OBJ_SAVE(item.name, mesh);
     }
     return ret;

+ 1 - 1
src/module/impl/image_streamer.cpp

@@ -13,7 +13,7 @@ void image_streamer::impl::create_scene_encoder() {
 
     auto pc_enc_conf = pc_encoder::create_config{
             .method = pc_codec::PC_RGB_DEPTH,
-            .depth_method = FAKE_800P,
+            .depth_method = FAKE_555P,
             .stream = conf.stream,
             .img_enc = i_enc.get(),
     };

+ 12 - 8
src/render/impl/render_mesh.cpp

@@ -173,16 +173,15 @@ mesh_type::pointer mesh_type::create(const create_config &conf) {
 
     auto pimpl = impl::create(conf);
     if (pimpl == nullptr) return nullptr;
-    auto ret = std::make_shared<this_type>();
-    ret->pimpl = std::move(pimpl);
+    auto ret = std::make_shared<this_type>(std::move(pimpl));
     mesh_cache.emplace(conf.path, ret);
     return ret;
 }
 
 mesh_type::pointer mesh_type::from_vtk(vtkPolyData *poly) {
-    auto ret = std::make_shared<this_type>();
-    ret->pimpl = std::make_unique<impl>();
-    ret->pimpl->upload_data_vtk(poly);
+    auto pimpl = std::make_unique<impl>();
+    pimpl->upload_data_vtk(poly);
+    auto ret = std::make_shared<this_type>(std::move(pimpl));
     return ret;
 }
 
@@ -191,12 +190,11 @@ mesh_type::pointer mesh_type::from_file(const std::string &path) {
 }
 
 mesh_type::pointer mesh_type::from_raw(const raw_info_type &raw) {
-    auto ret = std::make_shared<this_type>();
-    ret->pimpl = std::make_unique<impl>();
-    auto &pimpl = ret->pimpl;
+    auto pimpl = std::make_unique<impl>();
     pimpl->vbo_data = raw.vbo_data;
     pimpl->ebo_data = raw.ebo_data;
     pimpl->num_triangles = raw.num_triangles;
+    auto ret = std::make_shared<this_type>(std::move(pimpl));
     return ret;
 }
 
@@ -208,6 +206,12 @@ mesh_type::raw_info_type mesh_type::get_raw_info() {
     return pimpl->get_raw_info();
 }
 
+mesh_type::mesh_type(std::unique_ptr<impl> _pimpl)
+        : meta_base(_pimpl.get()) {
+    pimpl = std::move(_pimpl);
+    assert(pimpl != nullptr);
+}
+
 namespace render_mesh_impl {
 
     using pg_type = std::unique_ptr<smart_program>;

+ 1 - 1
src/render/impl/render_mesh_impl.h

@@ -24,7 +24,7 @@ namespace render_mesh_impl {
 
 using namespace render_mesh_impl;
 
-struct mesh_type::impl {
+struct mesh_type::impl : public meta_base::impl {
 
     struct v_type { // vertex type
         glm::vec3 pos;

+ 9 - 2
src/render/render_mesh.h

@@ -2,16 +2,17 @@
 #define DEPTHGUIDE_RENDER_MESH_H
 
 #include "render_utility.h"
+#include "core/meta_helper.hpp"
 #include "network/binary_utility.hpp"
 
 #include <vtkPolyData.h>
 
 #include <memory>
 
-class mesh_type {
+class mesh_type : public meta_base {
 public:
 
-    ~mesh_type();
+    ~mesh_type() override;
 
     struct create_config {
         std::string path;
@@ -50,10 +51,16 @@ public:
 private:
     struct impl;
     std::unique_ptr<impl> pimpl;
+
+public:
+    explicit mesh_type(std::unique_ptr<impl> pimpl);
+
 };
 
 using mesh_ptr = mesh_type::pointer;
 
+static constexpr auto META_MESH_NO_TRANSFER = meta_hash("mesh_no_transfer"); // bool
+
 enum mesh_render_mode {
     MESH_NORMAL,
     MESH_DEPTH_ALPHA