reorganize to use .bat files instead of .sh. Move demo to an example project

This commit is contained in:
2025-09-15 10:58:01 -05:00
parent 530dea9728
commit e77dfde1bc
18 changed files with 115 additions and 39 deletions

View File

12
examples/demo/build.bat Normal file
View File

@@ -0,0 +1,12 @@
SET flags=-Od -ZI -MD
IF "%1" == "release" (
SET flags=-O2 -MT
)
SET srcs=src\*
mkdir bin obj
(
PUSHD ..\..
CALL build.bat %1
POPD
COPY /Y ..\..\bin\* bin && cl %srcs% bin\LivePlotter.lib -I ..\..\inc -I ..\..\ext\glm %flags% -std:c++20 -MP -Fo:obj\\ -Fe:bin\\
)

22
examples/demo/export.bat Normal file
View File

@@ -0,0 +1,22 @@
@ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO [ > compile_commands.json
FOR /r "src\" %%F IN (*.cpp) DO (
SET "file=%%F"
SET "file=!file:\=/!"
SET "directory=%~dp0"
SET "directory=!directory:\=/!"
ECHO { >> compile_commands.json
ECHO "directory": "!directory!", >> compile_commands.json
ECHO "command": "cl !file! -I inc -I ../../ext/glm -I ../../ext/glfw/include %flags% -std:c++20 -MD -MP -Fo:obj\\ ", >> compile_commands.json
ECHO "file": "!file!" >> compile_commands.json
ECHO }, >> compile_commands.json
)
ECHO ] >> compile_commands.json

View File

@@ -0,0 +1,93 @@
#include <iostream>
#include <string>
#include <map>
#include "glm/ext/vector_float3.hpp"
#include "util.hpp"
#include "live_plotter.hpp"
#pragma comment(lib, "LivePlotter.lib")
using namespace std;
static map<string, pointid> name_to_id;
#define LINE_BUF_SIZE 256
#define NUM_SPHERES_PER_AXE 25
#define SPACE_PER_SPHERE 10.0f
bool parse_poses(std::vector<glm::mat2x3>& locs_out, const char* filepath) {
ifstream file(filepath);
if (!file.is_open()) {
return false;
}
string line;
getline(file, line);
while (getline(file, line)) {
std::vector<std::string> words = split_str(line, ',');
float x = stof(words[0]);
float y = stof(words[1]);
float z = stof(words[2]);
float w = stof(words[3]);
float i = stof(words[4]);
float j = stof(words[5]);
float k = stof(words[6]);
glm::mat4 pose = quat_to_mat4(glm::quat(w, i, j, k));
pose[3] = glm::vec4(x, y, z, 1);
// Generate axis spheres
for (int m = 0; m < 3; m++) {
for (int n = 0; n < NUM_SPHERES_PER_AXE; n++) {
glm::mat2x3 loc_color = glm::mat2x3(0.0);
loc_color[0][m] = n * SPACE_PER_SPHERE;
loc_color[0] = pose * glm::vec4(loc_color[0], 1);
loc_color[1] = glm::vec3(0);
loc_color[1][m] = 1.0f;
locs_out.push_back(loc_color);
}
}
}
return true;
}
int main() {
vector<glm::mat2x3> camera_pose_axes;
if (!parse_poses(camera_pose_axes, "poses.csv")) {
return -1;
}
start(800, 800);
for (int i = 0; i < camera_pose_axes.size(); i++) {
glm::vec3 p = camera_pose_axes[i][0];
glm::vec3 color = camera_pose_axes[i][1];
pointid id = create_point(p.x, p.y, p.z);
set_color(id, color.x, color.y, color.z);
set_scale(id, 10);
}
while (true) {
string line;
getline(cin, line);
vector<string> words = split_str(line, ' ');
if (words.size() != 4)
return NULL;
printf("Received: %s, %s, %s, %s\n", words[0].c_str(), words[1].c_str(), words[2].c_str(),
words[3].c_str()); // echo for debugging
string name = words[0];
float x = stof(words[1]);
float y = stof(words[2]);
float z = stof(words[3]);
if (auto it = name_to_id.find(name); it != name_to_id.end()) {
update_point(it->second, x, y, z);
} else {
name_to_id[name] = create_point(x, y, z);
set_lifetime(name_to_id[name], 0.2);
set_scale(name_to_id[name], 15);
}
}
}

View File

@@ -0,0 +1,40 @@
#include "util.hpp"
using namespace std;
bool read_file(string* s, const char* filepath) {
ifstream file(filepath);
if (!file.is_open()) {
return false;
}
*s = { istreambuf_iterator<char>(file), istreambuf_iterator<char>() };
return true;
}
vector<string> split_str(string s, char delim) {
vector<string> res;
string cur_word;
istringstream ss(s);
while (getline(ss, cur_word, delim)) {
res.push_back(cur_word);
}
return res;
}
// https://songho.ca/opengl/gl_quaternion.html
glm::mat4 quat_to_mat4(glm::quat q) {
glm::vec4 col0 = glm::vec4(
1 - 2 * q.y * q.y - 2 * q.z * q.z, 2 * q.x * q.y + 2 * q.w * q.z, 2 * q.x * q.z - 2 * q.w * q.y, 0);
glm::vec4 col1 = glm::vec4(
2 * q.x * q.y - 2 * q.w * q.z, 1 - 2 * q.x * q.x - 2 * q.z * q.z, 2 * q.y * q.z + 2 * q.w * q.x, 0);
glm::vec4 col2 = glm::vec4(
2 * q.x * q.z + 2 * q.w * q.y, 2 * q.y * q.z - 2 * q.w * q.x, 1 - 2 * q.x * q.x - 2 * q.y * q.y, 0);
glm::vec4 col3 = glm::vec4(0, 0, 0, 1);
return glm::mat4(col0, col1, col2, col3);
}
float randf() { return (float)abs(rand()) / (float)RAND_MAX; }