-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.main.cpp
123 lines (97 loc) · 3.12 KB
/
test.main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//#define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING
#include <httplib.h>
#include <spdlog/spdlog.h>
#include <filesystem>
#include "dimcli/cli.h"
#include "ply/ply_reader.h"
namespace fs = std::filesystem;
bool readPLY(const std::string& path, std::vector<float3>& myverts, std::vector<Color>& myColors)
{
std::ifstream infile(path);
if (infile.is_open() && !infile.fail())
{
read_ply_file(path, myverts, myColors);
return true;
}
return false;
}
int main(int argc, char** argv)
{
Dim::Cli cli;
auto& host_name = cli.opt<std::string>("host <host_name>").desc("like. 192.168.x.x");
auto& host_port = cli.opt<int>("port <host_port>").desc("range (1:65535)");
auto& pcg_path = cli.opt<std::string>("pcg <pcg_file>").desc("xxx.ply");
if (!cli.parse(argc, argv))
{
auto ret = cli.printError(std::cerr);
if (!*cli.helpOpt())
{
std::cerr << "--help for more info.\n";
}
return ret;
}
if (!fs::exists(*pcg_path))
{
spdlog::error("unable to find {}", *pcg_path);
return -1;
}
{
fs::path pcgPath(*pcg_path);
if (!pcgPath.has_extension() || pcgPath.extension() != ".ply")
{
spdlog::error("point cloud file must be ply.");
return -1;
}
}
httplib::Server svr;
//if (!svr.set_mount_point("/unity", unity_dir->c_str()))
//{
// spdlog::error("unable to bind Unity WebGl build folder: {}", *unity_dir);
// return EXIT_FAILURE;
//}
//if (!svr.set_mount_point("/workdir", work_dir->c_str()))
//{
// spdlog::error("unable to bind Unity WebGl build folder: {}", *work_dir);
// return EXIT_FAILURE;
//}
svr.Options(R"(\*)", [](const auto& req, auto& res)
{
res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
});
svr.Options("/", [](const auto& req, auto& res)
{
res.set_header("Access-Control-Allow-Origin", req.get_header_value("Origin").c_str());
res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
res.set_header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Accept, Origin, Authorization");
res.set_header("Access-Control-Allow-Methods", "OPTIONS, GET, POST, HEAD");
});
svr.Get("/test", [&pcg_path](const httplib::Request& req, httplib::Response& res)
{
res.set_content("hello world", "text/html");
});
svr.Get("/download_pcg", [&pcg_path](const httplib::Request& req, httplib::Response& res)
{
//std::string ply_text;
std::string pcg_path(*pcg_path);
std::vector<float3> myverts;
std::vector<Color> myColors;
std::string buffer;
if (fs::exists(pcg_path) && readPLY(pcg_path, myverts, myColors))
{
auto bytes1 = myverts.size() * sizeof(float3);
auto bytes2 = myColors.size() * sizeof(Color);
buffer.resize(4 + bytes1 + bytes2);
const int32_t size = myverts.size();
spdlog::info("size: {}", size);
memcpy((char*)buffer.c_str(), &size, sizeof(int32_t));
memcpy(((char*)buffer.c_str()) + 4, myverts.data(), bytes1);
memcpy(((char*)buffer.c_str()) + 4 + bytes1, myColors.data(), bytes2);
res.set_content(buffer, "text/plain");
return;
}
res.set_content("pcg does not exist", "text/html");
});
spdlog::info("server is running @{}:{}", *host_name, *host_port);
svr.listen((*host_name).c_str(), (*host_port));
return 0;
}