ENH: Replace GLM with Eigen

This commit is contained in:
David Allemang
2023-01-27 22:28:39 -05:00
parent b228f76658
commit 1f284ed349
6 changed files with 47 additions and 25 deletions

View File

@@ -4,12 +4,12 @@ project(toddcox-faster)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
include(FetchContent) include(FetchContent)
include(vendor/eigen.cmake)
include(vendor/fmt.cmake) include(vendor/fmt.cmake)
include(vendor/peglib.cmake)
include(vendor/glad.cmake) include(vendor/glad.cmake)
include(vendor/glfw.cmake) include(vendor/glfw.cmake)
include(vendor/glm.cmake)
include(vendor/gtest.cmake) include(vendor/gtest.cmake)
include(vendor/peglib.cmake)
include(vendor/yaml-cpp.cmake) include(vendor/yaml-cpp.cmake)
include(vendor/embed.cmake) include(vendor/embed.cmake)

10
vendor/eigen.cmake vendored Normal file
View File

@@ -0,0 +1,10 @@
FetchContent_Declare(
eigen
GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git
GIT_TAG 3.4
GIT_PROGRESS TRUE
)
set(EIGEN_BUILD_DOC OFF CACHE INTERNAL "")
set(BUILD_TESTING OFF CACHE INTERNAL "")
set(EIGEN_BUILD_PKGCONFIG OFF CACHE INTERNAL "")
FetchContent_MakeAvailable(eigen)

8
vendor/glm.cmake vendored
View File

@@ -1,8 +0,0 @@
FetchContent_Declare(
glm
GIT_REPOSITORY https://github.com/g-truc/glm.git
GIT_TAG 0.9.9.8
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(glm)

View File

@@ -19,5 +19,5 @@ target_include_directories(vis-util INTERFACE include)
add_executable(vis src/main.cpp) add_executable(vis src/main.cpp)
target_include_directories(vis PRIVATE include) target_include_directories(vis PRIVATE include)
target_link_libraries(vis PRIVATE tc glad glm glfw yaml-cpp shaders) target_link_libraries(vis PRIVATE tc glad eigen glfw yaml-cpp shaders fmt)
add_dependencies(vis presets) add_dependencies(vis presets)

View File

@@ -6,6 +6,8 @@
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#include <Eigen/Eigen>
template<unsigned N> template<unsigned N>
using vec = std::array<float, N>; using vec = std::array<float, N>;
@@ -184,11 +186,29 @@ std::vector<V> plane_intersections(std::vector<V> normals) {
return results; return results;
} }
glm::mat4 utilRotate(const int u, const int v, const float theta) { Eigen::Matrix4f utilRotate(const int u, const int v, const float theta) {
auto res = glm::identity<glm::mat4>(); Eigen::Matrix4f res;
res[u][u] = std::cos(theta); res.setIdentity();
res[u][v] = std::sin(theta); res(u, u) = std::cos(theta);
res[v][u] = -std::sin(theta); res(u, v) = std::sin(theta);
res[v][v] = std::cos(theta); res(v, u) = -std::sin(theta);
res(v, v) = std::cos(theta);
return res; return res;
} }
Eigen::Matrix4f ortho(
float l,
float r,
float b,
float t,
float n,
float f
) {
Eigen::Matrix4f res;
res <<
2 / (r - l), 0, 0, -(r + l) / (r - l),
0, 2 / (t - b), 0, -(t + b) / (t - b),
0, 0, -2 / (f - n), -(f + n) / (f - n),
0, 0, 0, 1;
return res;
}

View File

@@ -3,8 +3,6 @@
#include <cmath> #include <cmath>
#include <iostream> #include <iostream>
#include <glm/gtc/type_ptr.hpp>
#include <tc/groups.hpp> #include <tc/groups.hpp>
#include "util.hpp" #include "util.hpp"
@@ -28,10 +26,10 @@ __attribute__((unused)) __declspec(dllexport) int NvOptimusEnablement = 0x000000
#endif #endif
struct Matrices { struct Matrices {
glm::mat4 proj; Eigen::Matrix4f proj;
glm::mat4 view; Eigen::Matrix4f view;
Matrices(const glm::mat4 &proj, const glm::mat4 &view) Matrices(const Eigen::Matrix4f &proj, const Eigen::Matrix4f &view)
: proj(proj), view(view) { : proj(proj), view(view) {
} }
}; };
@@ -52,13 +50,15 @@ Matrices build(GLFWwindow *window, State &state) {
auto aspect = (float) width / (float) height; auto aspect = (float) width / (float) height;
auto pheight = 1.4f; auto pheight = 1.4f;
auto pwidth = aspect * pheight; auto pwidth = aspect * pheight;
glm::mat4 proj = glm::ortho(-pwidth, pwidth, -pheight, pheight, -10.0f, 10.0f); Eigen::Matrix4f proj = ortho(-pwidth, pwidth, -pheight, pheight, -10.0f, 10.0f);
if (!glfwGetKey(window, GLFW_KEY_LEFT_SHIFT)) { if (!glfwGetKey(window, GLFW_KEY_LEFT_SHIFT)) {
state.st += state.time_delta / 8; state.st += state.time_delta / 8;
} }
auto view = glm::identity<glm::mat4>(); Eigen::Matrix4f view;
view.setIdentity();
if (state.dimension < 4) { if (state.dimension < 4) {
view *= utilRotate(2, 3, M_PI_2f32 + 0.01f); view *= utilRotate(2, 3, M_PI_2f32 + 0.01f);
} }