Files
LivePlotter/src/camera_poses.cpp

74 lines
2.4 KiB
C++

#include <body.hpp>
#include <stdio.h>
#include <stdlib.h>
#include <glm/glm.hpp>
#include <glm/ext/matrix_clip_space.hpp>
#include <glm/ext/matrix_transform.hpp>
#include <glm/ext/vector_float3.hpp>
#include <glm/matrix.hpp>
#include <util.hpp>
#include "camera_poses.hpp"
#define LINE_BUF_SIZE 256
#define NUM_SPHERES_PER_AXE 25
#define SPACE_PER_SPHERE 10.0f
glm::vec4 red_color = glm::vec4(1, 0, 0, 1);
glm::vec4 green_color = glm::vec4(0, 1, 0, 1);
glm::vec4 blue_color = glm::vec4(0, 0, 1, 1);
bool parse_poses(Array<Body>* bodies_out, const char* filepath) {
FILE* fp;
if (fopen_s(&fp, filepath, "rb")) {
printf("Error parsing %s\n", filepath);
return false;
}
char delim = ',';
char line[LINE_BUF_SIZE];
// read in header
fgets(line, LINE_BUF_SIZE, fp);
fgets(line, LINE_BUF_SIZE, fp);
for (int camera_i = 0; !feof(fp); camera_i++) {
Array<char*> words = split_str(line, delim);
float x = atof(words[0])/100.0f;
float y = atof(words[1])/100.0f;
float z = atof(words[2])/100.0f;
float w = atof(words[3]);
float i = atof(words[4]);
float j = atof(words[5]);
float k = atof(words[6]);
glm::mat4 pose = glm::translate(quat_to_mat4(glm::quat(w, i, j, k)), glm::vec3(x, y, z));
// because clang refuses to cooperate with my append implementation and generate the function code...
*bodies_out
= { (Body*)malloc(sizeof(Body) * 3 * NUM_SPHERES_PER_AXE * 14), 3 * NUM_SPHERES_PER_AXE * 14 };
// Generate axis spheres
for (int i = 0; i < 3; i++) {
for (int j = 0; j < NUM_SPHERES_PER_AXE; j++) {
Body b;
create_new_sphere(&b);
// How far along the axis is this ball
glm::vec3 trans = glm::vec3(0, 0, 0);
trans[i] = (float)j * SPACE_PER_SPHERE;
// Now move the translated pose via the camera's pose
b.pose = pose * glm::translate(b.pose, trans);
b.color = glm::vec4(i == 0 ? 1 : 0, i == 1 ? 1 : 0, i == 2 ? 1 : 0, 1);
b.scale = 0.2;
printf("%d, ", camera_i * 3 * NUM_SPHERES_PER_AXE + i * NUM_SPHERES_PER_AXE + j);
bodies_out->data[camera_i*3*NUM_SPHERES_PER_AXE + i*NUM_SPHERES_PER_AXE + j] = b;
}
}
fgets(line, LINE_BUF_SIZE, fp);
}
}