Skip to content

Commit 87402df

Browse files
committed
add backend main&system
1 parent f807298 commit 87402df

16 files changed

Lines changed: 613 additions & 338 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ add_subdirectory(xrslam-localization)
3333

3434
if(XRSLAM_PC)
3535
add_subdirectory(xrslam-pc)
36+
add_subdirectory(xrslam-backend)
3637
endif()
3738
if(XRSLAM_IOS)
3839
add_subdirectory(xrslam-ios)

xrslam-backend/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
cmake_minimum_required(VERSION 3.11 FATAL_ERROR)
2+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")
3+
project(xrslam-backend LANGUAGES CXX)
4+
5+
include(SuperBuildDepends)
6+
superbuild_depend(liteviz)
7+
8+
add_executable(xrslam-backend
9+
${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp
10+
)
11+
12+
target_include_directories(xrslam-backend
13+
PRIVATE
14+
${CMAKE_CURRENT_SOURCE_DIR}/src
15+
)
16+
17+
target_link_libraries(xrslam-backend
18+
PRIVATE
19+
xrslam
20+
depends::opencv
21+
depends::liteviz
22+
)

xrslam-backend/src/main.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
#include <thread>
3+
#include "visualizer.h"
4+
#include "system.h"
5+
6+
int main() {
7+
8+
std::shared_ptr<SLAMViewer> viewer = std::make_shared<SLAMViewer>("SLAM Viewer", 1280, 720);
9+
viewer->start();
10+
11+
std::shared_ptr<xrslam::System> server = std::make_shared<xrslam::System>(5);
12+
server->start();
13+
14+
return 0;
15+
}

xrslam-backend/src/system.h

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#ifndef __SYSTEM_H__
2+
#define __SYSTEM_H__
3+
4+
#include <iostream>
5+
#include <string>
6+
#include <tuple>
7+
#include <thread>
8+
#include <mutex>
9+
#include <xrslam/utility/logger.h>
10+
#include <xrslam/backend/connector.h>
11+
12+
namespace xrslam {
13+
14+
struct DataBundle{
15+
DataBundle(DataType dtype, std::shared_ptr<VizServer> s)
16+
{
17+
this->server = s;
18+
this->dtype = dtype;
19+
}
20+
21+
std::string name;
22+
std::shared_ptr<VizServer> server;
23+
DataType dtype;
24+
};
25+
26+
27+
class System{
28+
29+
struct ListenThread{
30+
std::thread thread;
31+
std::shared_ptr<VizServer> server; // maybe send to client (virtual object)
32+
};
33+
34+
public:
35+
36+
std::mutex servers_mutex;
37+
std::vector<std::shared_ptr<VizServer>> servers;
38+
39+
std::vector<ListenThread> listen_threads;
40+
41+
int _port = 12000; // default server port
42+
43+
System(size_t max_agent = 5){
44+
45+
log_message(XRSLAM_LOG_INFO, "\n------------------ RCO-SLAM ------------------\n");
46+
47+
listen_threads.resize(max_agent);
48+
for (size_t i = 0; i < max_agent; ++i) {
49+
listen_threads[i].thread = std::thread(&System::listen, this, i);
50+
}
51+
}
52+
53+
~System(){
54+
for (auto& l : listen_threads) {
55+
if (l.thread.joinable()) {
56+
l.thread.join();
57+
}
58+
}
59+
}
60+
61+
void listen(int id){
62+
63+
std::string port = std::to_string(_port + id);
64+
auto& listener = listen_threads[id].server;
65+
listener = std::make_shared<VizServer>(port);
66+
listener->getClientInfo();
67+
68+
while(true){
69+
sleep(1);
70+
std::cout << "Listening on port: " << port << std::endl;
71+
}
72+
}
73+
74+
void add_element(std::shared_ptr<DataBundle> data){
75+
76+
}
77+
78+
void issue_message(){
79+
sleep(1);
80+
std::cout << "Issuing message..." << std::endl;
81+
}
82+
83+
84+
void start(){
85+
while(true){
86+
issue_message();
87+
}
88+
}
89+
90+
};
91+
92+
}
93+
94+
95+
96+
#endif

xrslam-backend/src/visualizer.h

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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+
};

xrslam-pc/player/src/visualizer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class SLAMViewer: public ViewerDetail {
7777
}
7878

7979
~SLAMViewer() {
80-
std::cout << "SLAMViewer destroyed." << std::endl;
80+
viewer_thread.join();
8181
}
8282

8383
void drawData() override {
@@ -155,9 +155,9 @@ class SLAMViewer: public ViewerDetail {
155155
}
156156

157157
void start(){
158-
std::thread viewer_thread([this]() { this->run(); });
159-
viewer_thread.detach();
158+
viewer_thread = std::thread([this]() { this->run(); });
160159
}
161160

161+
std::thread viewer_thread;
162162
std::shared_ptr<SLAMData> data = nullptr;
163163
};

xrslam/CMakeLists.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,19 @@ set(PRIVATE_HEADERS
6464
${XRSLAM_SOURCE_DIR}/xrslam/utility/worker.h
6565
${XRSLAM_SOURCE_DIR}/xrslam/utility/logger.h
6666
${XRSLAM_SOURCE_DIR}/xrslam/utility/parsac.h
67-
${XRSLAM_SOURCE_DIR}/xrslam/utility/serialize.h
68-
${XRSLAM_SOURCE_DIR}/xrslam/utility/potocal.h
6967
${XRSLAM_SOURCE_DIR}/xrslam/utility/imu_parsac.h
70-
${XRSLAM_SOURCE_DIR}/xrslam/utility/orb_extractor.h
71-
${XRSLAM_SOURCE_DIR}/xrslam/utility/client.h
7268
${XRSLAM_SOURCE_DIR}/xrslam/ar/virtual_object_manager.h
7369
${XRSLAM_SOURCE_DIR}/xrslam/localizer/base64.h
7470
${XRSLAM_SOURCE_DIR}/xrslam/localizer/base64_convert.h
7571
${XRSLAM_SOURCE_DIR}/xrslam/localizer/localizer.h
7672
${XRSLAM_SOURCE_DIR}/xrslam/localizer/httplib.h
7773
${XRSLAM_SOURCE_DIR}/xrslam/localizer/json.h
74+
75+
${XRSLAM_SOURCE_DIR}/xrslam/backend/serialize.h
76+
${XRSLAM_SOURCE_DIR}/xrslam/backend/potocal.h
77+
${XRSLAM_SOURCE_DIR}/xrslam/backend/orb_extractor.h
78+
${XRSLAM_SOURCE_DIR}/xrslam/backend/connector.h
79+
${XRSLAM_SOURCE_DIR}/xrslam/backend/socket.h
7880
)
7981

8082
set(PRIVATE_SOURCES
@@ -97,9 +99,10 @@ set(PRIVATE_SOURCES
9799
${XRSLAM_SOURCE_DIR}/xrslam/xrslam.cpp
98100
${XRSLAM_SOURCE_DIR}/xrslam/utility/debug.cpp
99101
${XRSLAM_SOURCE_DIR}/xrslam/utility/worker.cpp
100-
${XRSLAM_SOURCE_DIR}/xrslam/utility/orb_extractor.cpp
101102
${XRSLAM_SOURCE_DIR}/xrslam/localizer/localizer.cpp
102103
${XRSLAM_SOURCE_DIR}/xrslam/ar/virtual_object_manager.cpp
104+
105+
${XRSLAM_SOURCE_DIR}/xrslam/backend/orb_extractor.cpp
103106
)
104107

105108
add_compile_options(-fPIC)

0 commit comments

Comments
 (0)