Skip to content

Commit

Permalink
Merge pull request #351 from oddkiva/maint-remove-duplicate-code
Browse files Browse the repository at this point in the history
MAINT: remove duplicate code
  • Loading branch information
oddkiva authored Oct 11, 2023
2 parents 122f244 + 4519183 commit 7142333
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 123 deletions.
72 changes: 53 additions & 19 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand All @@ -69,6 +74,47 @@
PYBIND11_DIR = None


class BuildConfiguration:

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

# 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_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):
with subprocess.Popen(
cmd,
Expand All @@ -84,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,
Expand All @@ -112,7 +148,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"
Expand Down Expand Up @@ -181,7 +217,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.
Expand Down Expand Up @@ -399,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,
Expand Down
1 change: 0 additions & 1 deletion cpp/examples/Kalpana/Emscripten/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ set(SARA_EMSCRIPTEN_BUILD_HTML YES)
list(
APPEND
GL_UTILS
Geometry.hpp
ImagePlaneRenderer.hpp
ImagePlaneRenderer.cpp
ImageDewarpRenderer.hpp
Expand Down
69 changes: 0 additions & 69 deletions cpp/examples/Kalpana/Emscripten/Geometry.hpp

This file was deleted.

21 changes: 12 additions & 9 deletions cpp/examples/Kalpana/Emscripten/test_image_dewarp_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//! @file

#include <DO/Kalpana/EasyGL.hpp>
#include <DO/Kalpana/Math/Projection.hpp>

#include <DO/Sara/Core/Math/Rotation.hpp>
#include <DO/Sara/ImageIO.hpp>
Expand All @@ -26,13 +27,13 @@

#include <filesystem>

#include "Geometry.hpp"
#include "ImageDewarpRenderer.hpp"
#include "ImagePlaneRenderer.hpp"


namespace fs = std::filesystem;
namespace sara = DO::Sara;
namespace k = DO::Kalpana;


class GLFWApp;
Expand Down Expand Up @@ -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";
Expand All @@ -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
Expand Down Expand Up @@ -209,10 +210,10 @@ class GLFWApp
image_texture._model_view.setIdentity();
const auto aspect_ratio =
static_cast<float>(_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
Expand Down Expand Up @@ -271,8 +272,10 @@ class GLFWApp
const auto aspect_ratio = static_cast<float>(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 */,
Expand Down
57 changes: 32 additions & 25 deletions cpp/examples/Kalpana/Emscripten/test_metric_grid_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//! @file

#include <DO/Kalpana/EasyGL.hpp>
#include <DO/Kalpana/Math/Projection.hpp>

#include <DO/Sara/Core/Math/Rotation.hpp>
#include <DO/Sara/ImageIO.hpp>
Expand All @@ -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;
Expand Down Expand Up @@ -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";
Expand All @@ -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
Expand Down Expand Up @@ -301,8 +302,11 @@ class GLFWApp
const auto aspect_ratio = static_cast<float>(_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)
Expand All @@ -312,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;
Expand All @@ -327,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;
}
Expand Down Expand Up @@ -419,8 +423,11 @@ class GLFWApp
const auto aspect_ratio = static_cast<float>(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 */,
Expand Down

0 comments on commit 7142333

Please sign in to comment.