From 9deded95a3cb19c20e3881e5a536d9f569ee5bab Mon Sep 17 00:00:00 2001 From: David OK Date: Wed, 11 Oct 2023 12:32:04 +0100 Subject: [PATCH 1/3] MAINT: remove duplicate code. --- build.py | 40 +++++++++-- .../Kalpana/Emscripten/CMakeLists.txt | 1 - cpp/examples/Kalpana/Emscripten/Geometry.hpp | 69 ------------------- .../Emscripten/test_image_dewarp_renderer.cpp | 21 +++--- .../Emscripten/test_metric_grid_renderer.cpp | 20 ++++-- 5 files changed, 60 insertions(+), 91 deletions(-) delete mode 100644 cpp/examples/Kalpana/Emscripten/Geometry.hpp diff --git a/build.py b/build.py index ba6690399..b2695a80e 100755 --- a/build.py +++ b/build.py @@ -43,15 +43,20 @@ SYSTEM = platform.system() +FORCE_COMPILE_WITH_GCC = False + # Third-party libraries that makes Sara faster, stronger, cooler... if SYSTEM == "Linux": OPT_PATH = pathlib.Path("/opt") HALIDE_ROOT_PATH = OPT_PATH / (f"Halide-{HALIDE_VERSION}-x86-64-linux") ONNXRUNTIME_ROOT_PATH = OPT_PATH / "onnxruntime-linux-x64-gpu-1.14.0" NVIDIA_CODEC_SDK_ROOT_PATH = OPT_PATH / "Video_Codec_SDK_12.1.14" - SWIFT_TOOLCHAIN_DIR = OPT_PATH / f"swift-{SWIFT_VERSION}-RELEASE-ubuntu{UBUNTU_VERSION}" - SWIFT_TOOLCHAIN_BIN_DIR = SWIFT_TOOLCHAIN_DIR / "usr/bin" - SWIFTC_PATH = SWIFT_TOOLCHAIN_BIN_DIR / "swiftc" + if not FORCE_COMPILE_WITH_GCC: + SWIFT_TOOLCHAIN_DIR = OPT_PATH / f"swift-{SWIFT_VERSION}-RELEASE-ubuntu{UBUNTU_VERSION}" + SWIFT_TOOLCHAIN_BIN_DIR = SWIFT_TOOLCHAIN_DIR / "usr/bin" + SWIFTC_PATH = SWIFT_TOOLCHAIN_BIN_DIR / "swiftc" + else: + SWIFTC_PATH = "" elif SYSTEM == "Darwin": NVIDIA_CODEC_SDK_ROOT_PATH = None SWIFT_PATH = subprocess.check_output(["which", "swift"]) @@ -69,6 +74,31 @@ PYBIND11_DIR = None +class BuildConfiguration: + + def __init__(self): + self._os_name = "ubuntu" + self._os_version = UBUNTU_VERSION + self._cuda_version = CUDA_VERSION + self._trt_version = TRT_VERSION + self._halide_version = HALIDE_VERSION + self._source_dir = SARA_SOURCE_DIR + self._compiler = None + self._build_type = "Release" + + config_list = [ + self._build_type, + f"self._compiler", + f"{self._os_name}{self._os_version}", + f"cuda-{self._cuda_version}", + f"trt-{self._trt_version}", + f"halide-{self._halide_version}", + ] + + stringified_config = "-".join(config_list) + self._build_dir = f"sara-build-{stringified_config}" + + def execute(cmd, cwd): with subprocess.Popen( cmd, @@ -112,7 +142,7 @@ def generate_project( if PROJECT_TYPE != "Xcode": cmake_options.append(f"-D CMAKE_BUILD_TYPE={build_type}") - if SYSTEM == "Linux": + if SYSTEM == "Linux" and not FORCE_COMPILE_WITH_GCC: cxx_compiler = SWIFT_TOOLCHAIN_BIN_DIR / "clang++" c_compiler = SWIFT_TOOLCHAIN_BIN_DIR / "clang" swift_bridging_include_dirs = SWIFT_TOOLCHAIN_DIR / "usr/include" @@ -181,7 +211,7 @@ def generate_project( # Setup Swift bindings. if SYSTEM == "Darwin": cmake_options.append("-D CMAKE_Swift_COMPILER=/usr/bin/swiftc") - elif SYSTEM == "Linux" and pathlib.Path(SWIFTC_PATH).exists(): + elif SYSTEM == "Linux" and pathlib.Path(SWIFTC_PATH).exists() and not FORCE_COMPILE_WITH_GCC: cmake_options.append(f"-D CMAKE_Swift_COMPILER={SWIFTC_PATH}") # Setup Python bindings. diff --git a/cpp/examples/Kalpana/Emscripten/CMakeLists.txt b/cpp/examples/Kalpana/Emscripten/CMakeLists.txt index 6793610c5..b66b2c697 100644 --- a/cpp/examples/Kalpana/Emscripten/CMakeLists.txt +++ b/cpp/examples/Kalpana/Emscripten/CMakeLists.txt @@ -4,7 +4,6 @@ set(SARA_EMSCRIPTEN_BUILD_HTML YES) list( APPEND GL_UTILS - Geometry.hpp ImagePlaneRenderer.hpp ImagePlaneRenderer.cpp ImageDewarpRenderer.hpp diff --git a/cpp/examples/Kalpana/Emscripten/Geometry.hpp b/cpp/examples/Kalpana/Emscripten/Geometry.hpp deleted file mode 100644 index 83b318fc9..000000000 --- a/cpp/examples/Kalpana/Emscripten/Geometry.hpp +++ /dev/null @@ -1,69 +0,0 @@ -#pragma once - -#include - -template -inline auto frustum(T l, T r, T b, T t, T n, T f) -> Eigen::Matrix -{ - auto proj = Eigen::Matrix{}; - - // clang-format off - proj << - 2*n/(r-l), 0, (r+l)/(r-l), 0, - 0, 2*n/(t-b), (t+b)/(t-b), 0, - 0, 0, -(f+n)/(f-n), -2*f*n/(f-n), - 0, 0, -1, 0; - // clang-format on - - return proj; -} - -template -inline auto orthographic(T l, T r, T b, T t, T n, T f) -> Eigen::Matrix -{ - auto proj = Eigen::Matrix{}; - - // clang-format off - proj << - 2/(r-l), 0, 0, -(r+l)/(r-l), - 0, 2/(t-b), 0, -(t+b)/(t-b), - 0, 0, 0, -(f+n)/(f-n), - 0, 0, 0, 1; - // clang-format on - - return proj; -} - -template -inline auto perspective(T fov, T aspect, T z_near, T z_far) - -> Eigen::Matrix -{ - static constexpr auto k = static_cast(M_PI / 360.); - const auto t = z_near * std::tan(fov * k); - const auto b = -t; - const auto l = aspect * b; - const auto r = aspect * t; - return frustum(l, r, b, t, z_near, z_far); -} - -template -inline auto look_at(const Eigen::Matrix& eye, // - const Eigen::Matrix& center, // - const Eigen::Matrix& up) -> Eigen::Matrix -{ - const Eigen::Matrix f = (center - eye).normalized(); - Eigen::Matrix u = up.normalized(); - const Eigen::Matrix s = f.cross(u).normalized(); - u = s.cross(f); - - Eigen::Matrix res; - // clang-format off - res << - s.x(), s.y(), s.z(), -s.dot(eye), - u.x(), u.y(), u.z(), -u.dot(eye), - -f.x(), -f.y(), -f.z(), f.dot(eye), - 0, 0, 0, 1; - // clang-format on - - return res; -} diff --git a/cpp/examples/Kalpana/Emscripten/test_image_dewarp_renderer.cpp b/cpp/examples/Kalpana/Emscripten/test_image_dewarp_renderer.cpp index 7a5082d8f..01476d26b 100644 --- a/cpp/examples/Kalpana/Emscripten/test_image_dewarp_renderer.cpp +++ b/cpp/examples/Kalpana/Emscripten/test_image_dewarp_renderer.cpp @@ -12,6 +12,7 @@ //! @file #include +#include #include #include @@ -26,13 +27,13 @@ #include -#include "Geometry.hpp" #include "ImageDewarpRenderer.hpp" #include "ImagePlaneRenderer.hpp" namespace fs = std::filesystem; namespace sara = DO::Sara; +namespace k = DO::Kalpana; class GLFWApp; @@ -62,7 +63,7 @@ class GLFWApp glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); #endif - // clang-format off + // clang-format off // #ifdef __APPLE__ // // GL 3.2 + GLSL 150 // MyGLFW::glsl_version = "#version 150"; @@ -81,7 +82,7 @@ class GLFWApp // glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); // #endif - // clang-format on + // clang-format on #if defined(_WIN32) // if it's a HighDPI monitor, try to scale everything @@ -209,10 +210,10 @@ class GLFWApp image_texture._model_view.setIdentity(); const auto aspect_ratio = static_cast(_window_sizes.x()) / _window_sizes.y(); - image_texture._projection = - orthographic(-0.5f * aspect_ratio, 0.5f * aspect_ratio, // - -0.5f, 0.5f, // - -0.5f, 0.5f); + image_texture._projection = k::orthographic( // + -0.5f * aspect_ratio, 0.5f * aspect_ratio, // + -0.5f, 0.5f, // + -0.5f, 0.5f); } auto initialize_camera_parameters() -> void @@ -271,8 +272,10 @@ class GLFWApp const auto aspect_ratio = static_cast(width) / height; auto& image = app._image_plane_renderer._textures.front(); - image._projection = orthographic(-0.5f * aspect_ratio, 0.5f * aspect_ratio, - -0.5f, 0.5f, -0.5f, 0.5f); + image._projection = k::orthographic( // + -0.5f * aspect_ratio, 0.5f * aspect_ratio, // + -0.5f, 0.5f, // + -0.5f, 0.5f); } static auto key_callback(GLFWwindow* window, int key, int /* scancode */, diff --git a/cpp/examples/Kalpana/Emscripten/test_metric_grid_renderer.cpp b/cpp/examples/Kalpana/Emscripten/test_metric_grid_renderer.cpp index 038f8d4bf..27b1197fe 100644 --- a/cpp/examples/Kalpana/Emscripten/test_metric_grid_renderer.cpp +++ b/cpp/examples/Kalpana/Emscripten/test_metric_grid_renderer.cpp @@ -12,6 +12,7 @@ //! @file #include +#include #include #include @@ -29,13 +30,13 @@ #include "imgui_impl_glfw.h" #include "imgui_impl_opengl3.h" -#include "Geometry.hpp" #include "ImagePlaneRenderer.hpp" #include "MetricGridRenderer.hpp" namespace fs = std::filesystem; namespace sara = DO::Sara; +namespace k = DO::Kalpana; class GLFWApp; @@ -65,7 +66,7 @@ class GLFWApp glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); #endif - // clang-format off + // clang-format off // #if defined(__APPLE__) // // GL 3.2 + GLSL 150 // MyGLFW::glsl_version = "#version 150"; @@ -84,7 +85,7 @@ class GLFWApp // glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); // #endif - // clang-format on + // clang-format on #if defined(_WIN32) // if it's a HighDPI monitor, try to scale everything @@ -301,8 +302,11 @@ class GLFWApp const auto aspect_ratio = static_cast(_window_sizes.x()) / // _window_sizes.y(); image_texture._model_view.setIdentity(); - image_texture._projection = orthographic( - -0.5f * aspect_ratio, 0.5f * aspect_ratio, -0.5f, 0.5f, -0.5f, 0.5f); + image_texture._projection = k::orthographic( // + -0.5f * aspect_ratio, 0.5f * aspect_ratio, // + -0.5f, 0.5f, // + -0.5f, 0.5f // + ); } auto initialize_camera_parameters(MetricGridRenderer::LineShaderData& lines) @@ -419,8 +423,10 @@ class GLFWApp const auto aspect_ratio = static_cast(width) / height; auto& image = app._image_plane_renderer._textures.front(); - image._projection = orthographic(-0.5f * aspect_ratio, 0.5f * aspect_ratio, - -0.5f, 0.5f, -0.5f, 0.5f); + image._projection = k::orthographic( // + -0.5f * aspect_ratio, 0.5f * aspect_ratio, -0.5f, 0.5f, // + -0.5f, 0.5f // + ); } static auto key_callback(GLFWwindow* window, int key, int /* scancode */, From 7dc52120c3d693d971d7abbe80a4aeb1ad44ed7b Mon Sep 17 00:00:00 2001 From: David OK Date: Wed, 11 Oct 2023 13:37:26 +0100 Subject: [PATCH 2/3] MAINT: tweak build script. --- build.py | 58 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/build.py b/build.py index b2695a80e..be45e3d22 100755 --- a/build.py +++ b/build.py @@ -76,27 +76,43 @@ class BuildConfiguration: - def __init__(self): + def __init__(self, args): self._os_name = "ubuntu" self._os_version = UBUNTU_VERSION self._cuda_version = CUDA_VERSION self._trt_version = TRT_VERSION self._halide_version = HALIDE_VERSION self._source_dir = SARA_SOURCE_DIR - self._compiler = None - self._build_type = "Release" - - config_list = [ - self._build_type, - f"self._compiler", - f"{self._os_name}{self._os_version}", - f"cuda-{self._cuda_version}", - f"trt-{self._trt_version}", - f"halide-{self._halide_version}", - ] + + # Quick'n'dirty + if FORCE_COMPILE_WITH_GCC: + self._compiler = "gcc" + else: + self._compiler = "clang" + self._build_type = args.build_type + + config_list = [self._build_type] + if FORCE_COMPILE_WITH_GCC: + config_list += [ + f"{self._compiler}", + # f"{self._os_name}{self._os_version}", + # f"cuda-{self._cuda_version}", + # f"trt-{self._trt_version}", + # f"halide-{self._halide_version}", + ] stringified_config = "-".join(config_list) - self._build_dir = f"sara-build-{stringified_config}" + self._build_dir = f"{SARA_SOURCE_DIR.name}-build-{stringified_config}" + + @staticmethod + def infer_project_type(system: str): + if system == "Linux": + return "Ninja" + elif system == "Darwin": + return "Xcode" + + +PROJECT_TYPE = BuildConfiguration.infer_project_type(SYSTEM) def execute(cmd, cwd): @@ -114,16 +130,6 @@ def execute(cmd, cwd): raise subprocess.CalledProcessError(p.returncode, p.args) -def infer_project_type(system: str): - if system == "Linux": - return "Ninja" - elif system == "Darwin": - return "Xcode" - - -PROJECT_TYPE = infer_project_type(SYSTEM) - - def generate_project( source_dir: str, build_dir: str, @@ -429,10 +435,8 @@ def build_emsdk_docker(): f"{SARA_SOURCE_DIR.name}-build-Xcode" ) else: - build_dir = ( - SARA_SOURCE_DIR.parent / - f"{SARA_SOURCE_DIR.name}-build-{args.build_type}" - ) + build_config = BuildConfiguration(args) + build_dir = SARA_SOURCE_DIR.parent / build_config._build_dir generate_project( SARA_SOURCE_DIR, build_dir, args.build_type, From 4519183baf88386a0c8b466cf611aaa18decde4c Mon Sep 17 00:00:00 2001 From: David OK Date: Wed, 11 Oct 2023 13:46:26 +0100 Subject: [PATCH 3/3] STY: reformat code. --- .../Emscripten/test_metric_grid_renderer.cpp | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/cpp/examples/Kalpana/Emscripten/test_metric_grid_renderer.cpp b/cpp/examples/Kalpana/Emscripten/test_metric_grid_renderer.cpp index 27b1197fe..2708f56c7 100644 --- a/cpp/examples/Kalpana/Emscripten/test_metric_grid_renderer.cpp +++ b/cpp/examples/Kalpana/Emscripten/test_metric_grid_renderer.cpp @@ -316,11 +316,11 @@ class GLFWApp // convention. // // clang-format off - const Eigen::Matrix3f P = (Eigen::Matrix3f{} << - 0, 0, 1, - -1, 0, 0, - 0, -1, 0 - ).finished(); + const Eigen::Matrix3f P = (Eigen::Matrix3f{} << + 0, 0, 1, + -1, 0, 0, + 0, -1, 0 + ).finished(); // clang-format on auto& C = lines._extrinsics; @@ -331,19 +331,19 @@ class GLFWApp auto& intrinsics = lines._intrinsics; // clang-format off - const auto K = (Eigen::Matrix3f{} << - 1041.55762f, -2.31719828f, 942.885742f, - 0.f, 1041.53857f, 589.198425f, - 0.f, 0.f, 1.f - ).finished(); - intrinsics.set_calibration_matrix(K); - intrinsics.radial_distortion_coefficients << - 0.442631334f, - -0.156340882f, - 0; - intrinsics.tangential_distortion_coefficients << - -0.000787709199f, - -0.000381082471f; + const auto K = (Eigen::Matrix3f{} << + 1041.55762f, -2.31719828f, 942.885742f, + 0.f, 1041.53857f, 589.198425f, + 0.f, 0.f, 1.f + ).finished(); + intrinsics.set_calibration_matrix(K); + intrinsics.radial_distortion_coefficients << + 0.442631334f, + -0.156340882f, + 0; + intrinsics.tangential_distortion_coefficients << + -0.000787709199f, + -0.000381082471f; // clang-format on intrinsics.xi = 1.43936455f; } @@ -423,9 +423,10 @@ class GLFWApp const auto aspect_ratio = static_cast(width) / height; auto& image = app._image_plane_renderer._textures.front(); - image._projection = k::orthographic( // - -0.5f * aspect_ratio, 0.5f * aspect_ratio, -0.5f, 0.5f, // - -0.5f, 0.5f // + image._projection = k::orthographic( // + -0.5f * aspect_ratio, 0.5f * aspect_ratio, // + -0.5f, 0.5f, // + -0.5f, 0.5f // ); }