|
| 1 | +#include <filesystem> |
| 2 | +#include <liteviz/viewer.h> |
| 3 | + |
| 4 | +struct SLAMData{ // for visualization |
| 5 | + |
| 6 | +struct Frame { |
| 7 | + cv::Mat image; |
| 8 | + Eigen::Vector4f intrinsics; |
| 9 | + Eigen::Matrix4f pose; |
| 10 | + |
| 11 | + Frame(const cv::Mat &img, |
| 12 | + const Eigen::Vector4f &intrin, |
| 13 | + const Eigen::Matrix4f &p) |
| 14 | + : image(img), intrinsics(intrin), pose(p) {} |
| 15 | +}; |
| 16 | + |
| 17 | +public: |
| 18 | + std::mutex frame_mutex; |
| 19 | + std::mutex point_mutex; |
| 20 | + std::mutex image_mutex; |
| 21 | + std::mutex poses_mutex; |
| 22 | + |
| 23 | + std::vector<Frame> frames; |
| 24 | + std::vector<Eigen::Vector3f> points; |
| 25 | + std::vector<Eigen::Matrix4f> poses; |
| 26 | + cv::Mat feature_tracker_cvimage; |
| 27 | + |
| 28 | + void update_poses(const Eigen::Matrix4f _pose) { |
| 29 | + std::lock_guard<std::mutex> lock(poses_mutex); |
| 30 | + poses.push_back(_pose); |
| 31 | + } |
| 32 | + |
| 33 | + void update_frames(std::vector<Frame> _frames) { |
| 34 | + std::lock_guard<std::mutex> lock(frame_mutex); |
| 35 | + frames = _frames; |
| 36 | + } |
| 37 | + |
| 38 | + void update_points(const std::vector<Eigen::Vector3f> &_points) { |
| 39 | + std::lock_guard<std::mutex> lock(point_mutex); |
| 40 | + points = _points; |
| 41 | + } |
| 42 | + |
| 43 | + void update_image(const cv::Mat &image) { |
| 44 | + std::lock_guard<std::mutex> lock(image_mutex); |
| 45 | + feature_tracker_cvimage = image.clone(); |
| 46 | + } |
| 47 | + |
| 48 | + std::vector<Eigen::Vector3f> get_points() { |
| 49 | + std::lock_guard<std::mutex> lock(point_mutex); |
| 50 | + return points; |
| 51 | + } |
| 52 | + std::vector<Frame> get_frames() { |
| 53 | + std::lock_guard<std::mutex> lock(frame_mutex); |
| 54 | + return frames; |
| 55 | + } |
| 56 | + |
| 57 | + std::vector<Eigen::Matrix4f> get_poses() { |
| 58 | + std::lock_guard<std::mutex> lock(poses_mutex); |
| 59 | + return poses; |
| 60 | + } |
| 61 | + |
| 62 | + cv::Mat get_image() { |
| 63 | + std::lock_guard<std::mutex> lock(image_mutex); |
| 64 | + return feature_tracker_cvimage.clone(); |
| 65 | + } |
| 66 | + |
| 67 | +}; |
| 68 | + |
| 69 | +class SLAMViewer: public ViewerDetail { |
| 70 | + |
| 71 | +public: |
| 72 | + SLAMViewer(std::string title, int width, int height): |
| 73 | + ViewerDetail(title, width, height) { |
| 74 | + std::cout << "SLAMViewer initialized." << std::endl; |
| 75 | + |
| 76 | + data = std::make_shared<SLAMData>(); |
| 77 | + } |
| 78 | + |
| 79 | + ~SLAMViewer() { |
| 80 | + std::cout << "SLAMViewer destroyed." << std::endl; |
| 81 | + } |
| 82 | + |
| 83 | + void drawData() override { |
| 84 | + |
| 85 | + grid->draw(gridShader, viewport); |
| 86 | + |
| 87 | + Eigen::Vector4f cameraColor = Eigen::Vector4f(0.8f, 0.2f, 0.2f, 1.0f); |
| 88 | + Eigen::Vector4f pointsColor = Eigen::Vector4f(1.0f, 1.0f, 1.0f, 1.0f); |
| 89 | + Eigen::Vector4f trajColor = Eigen::Vector4f(0.7f, 0.2f, 0.2f, 1.0f); |
| 90 | + |
| 91 | + auto frames = data->get_frames(); |
| 92 | + for(auto & frame : frames) { |
| 93 | + frustum->setup(frame.intrinsics, 0.001); |
| 94 | + frustum->setColor(cameraColor); |
| 95 | + frustum->transform(frame.pose); |
| 96 | + frustum->draw(pointShader, viewer->viewport); |
| 97 | + } |
| 98 | + |
| 99 | + auto points = data->get_points(); |
| 100 | + pointCloud->setup(points, pointsColor); |
| 101 | + pointCloud->draw(pointShader, viewport); |
| 102 | + |
| 103 | + auto poses = data->get_poses(); |
| 104 | + if(!poses.empty()) { |
| 105 | + std::vector<Eigen::Vector3f> traj_points; |
| 106 | + traj_points.reserve(poses.size() * 2); |
| 107 | + for(size_t i = 0; i < poses.size() - 1; ++i) { |
| 108 | + traj_points.push_back(poses[i + 0].block<3, 1>(0, 3)); |
| 109 | + traj_points.push_back(poses[i + 1].block<3, 1>(0, 3)); |
| 110 | + } |
| 111 | + line->setup(traj_points, trajColor); |
| 112 | + line->draw(pointShader, viewport); |
| 113 | + } |
| 114 | + |
| 115 | + drawImage(); |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + void drawImage() { |
| 120 | + |
| 121 | + ImGui_ImplOpenGL3_NewFrame(); |
| 122 | + ImGui_ImplGlfw_NewFrame(); |
| 123 | + ImGui::NewFrame(); |
| 124 | + |
| 125 | + any_window_active = ImGui::IsAnyItemActive() ; |
| 126 | + |
| 127 | + ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0.1f, 0.1f, 0.1f, 0.3f)); |
| 128 | + ImGui::Begin("Configuration", nullptr, window_flags); |
| 129 | + ImGui::SetWindowSize(ImVec2(300, 0)); |
| 130 | + |
| 131 | + if(notifier){ |
| 132 | + std::lock_guard<std::mutex> lock(notifier->mtx); |
| 133 | + const char* buttonText = isRunning ? "Running..." : "Paused"; |
| 134 | + if (ImGui::Button(buttonText, ImVec2(300, 0))){ |
| 135 | + isRunning = !isRunning; |
| 136 | + notifier->ready = isRunning; |
| 137 | + } |
| 138 | + notifier->cv.notify_one(); |
| 139 | + } |
| 140 | + |
| 141 | + auto image = data->get_image(); |
| 142 | + if(!image.empty()) { |
| 143 | + size_t w = 300; |
| 144 | + size_t h = w * image.rows / image.cols; |
| 145 | + colorTexture->setup(image.data, Eigen::Vector2i(image.cols, image.rows)); |
| 146 | + ImGui::Image(colorTexture->getTextureID(), ImVec2(w, h)); |
| 147 | + } |
| 148 | + |
| 149 | + ImGui::End(); |
| 150 | + ImGui::PopStyleColor(); |
| 151 | + |
| 152 | + ImGui::Render(); |
| 153 | + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | + void start(){ |
| 158 | + std::thread viewer_thread([this]() { this->run(); }); |
| 159 | + viewer_thread.detach(); |
| 160 | + } |
| 161 | + |
| 162 | + std::shared_ptr<SLAMData> data = nullptr; |
| 163 | +}; |
0 commit comments