From 4bd5f62f868a9f69d0a1c6beb5fc8efd1e6f8016 Mon Sep 17 00:00:00 2001 From: squishyhuman Date: Sat, 30 Sep 2023 06:54:01 -0700 Subject: [PATCH] Import code for optical flow in the browser --- .gitignore | 8 ++ lib/CMakeLists.txt | 174 +++++++++++++++++++++++++++++++++++++++++ lib/build-ci.sh | 67 ++++++++++++++++ tools/build-depends.sh | 3 + tools/build-paths.sh | 3 + 5 files changed, 255 insertions(+) create mode 100644 lib/CMakeLists.txt create mode 100755 lib/build-ci.sh diff --git a/.gitignore b/.gitignore index 96fc2d984..c7898eb6e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ # Generated dependencies /src/generated +# Generated libraries +/frontend/public/motion_tracker + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] @@ -143,3 +146,8 @@ Temporary # libzip build files /third-party/libzip + +# Eclipse project files +.cproject +.project +.settings diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt new file mode 100644 index 000000000..3bab98918 --- /dev/null +++ b/lib/CMakeLists.txt @@ -0,0 +1,174 @@ +################################################################################ +# +# Copyright (C) 2020-2023 retro.ai +# This file is part of retro-dapp - https://github.com/RetroAI/retro-dapp +# +# SPDX-License-Identifier: Apache-2.0 +# See the file LICENSE.md for more information. +# +################################################################################ + +################################################################################ +# +# Build system for C++ libraries +# +# Required CMake variables: +# +# CMAKE_FIND_ROOT_PATH - Point this to dependencies compiled with Emscripten +# CMAKE_INSTALL_PREFIX - Point this to the "public" folder +# +################################################################################ + +################################################################################ +# +# Project settings +# +################################################################################ + +project(retroai) + +cmake_minimum_required(VERSION 3.0.0) + +set(CMAKE_CXX_STANDARD 17) + +################################################################################ +# +# Dependencies +# +################################################################################ + +find_package(Ceres REQUIRED) +find_package(Eigen3 REQUIRED) +find_package(Glog REQUIRED) +find_package(OpenCV REQUIRED) + +add_definitions(-DCERES_FOUND=1) + +################################################################################ +# +# Define sources +# +################################################################################ + +# +# Motion tracker +# + +set(MOTION_TRACKER_SOURCES + api/imgproc.cpp + api/reconstruction.cpp + api/scene.cpp + api/video.cpp + kernels/cpu/cpu_imgproc.cpp + kernels/cpu/cpu_reconstruction.cpp + kernels/cpu/cpu_scene.cpp + kernels/cpu/cpu_video.cpp + motion_tracker/motion_tracker.cpp + motion_tracker/motion_tracker_embinder.cpp + motion_tracker/vision_graph.cpp + utils/emscripten_utils.cpp + utils/frame_pool.cpp + utils/image_utils.cpp + utils/math_utils.cpp +) + +################################################################################ +# +# Build libraries +# +# TODO: +# +# * Build properly instead of shelling out +# * Could refactor this into macros +# +################################################################################ + +include_directories( + ${CMAKE_SOURCE_DIR} +) + +string(APPEND EMSCRIPTEN_LINK_FLAGS + "--bind " + # "-o dist/engine.js " + # " -std=c++11 " + # " -O2 " + # " --preload-file textures " + # " --preload-file shaders " + # " --preload-file fonts " + # " --pre-js pre-module.j " + # " --post-js post-module.j " + "-s ALLOW_MEMORY_GROWTH=1 " + "-s ASSERTIONS=1 " + # " -s DEMANGLE_SUPPORT=1 " + # " -s DISABLE_EXCEPTION_CATCHING=0 " + "-s ERROR_ON_UNDEFINED_SYMBOLS=0 " + # " -s FULL_ES3=1 " + # " -s GL_ASSERTIONS=1 " + # " -s GL_UNSAFE_OPTS=0 " + # " -s INVOKE_RUN=0 " + # " -s LEGACY_GL_EMULATION=0 " + #"-s LLD_REPORT_UNDEFINED " + # " -s OFFSCREENCANVAS_SUPPORT=1 " + # " -s SAFE_HEAP=1 " + #"-s TOTAL_MEMORY=67108864 " + # " -s USE_FREETYPE=1 " + # " -s USE_GLFW=3 " + # " -s USE_WEBGL2=1 " + "-s USE_ZLIB=1 " + # " -s WASM=1 " +) + +# +# Motion tracker +# + +add_executable(motion_tracker + ${MOTION_TRACKER_SOURCES} +) + +target_include_directories(motion_tracker PRIVATE + ${OpenCV_INCLUDE_DIRS} +) + +target_link_libraries(motion_tracker PRIVATE + ${OpenCV_LIBS} +) + +if (${CMAKE_SYSTEM_NAME} MATCHES "Emscripten") + set_target_properties(motion_tracker PROPERTIES + COMPILE_FLAGS " \ + -O0 \ + -g4 \ + -s DISABLE_EXCEPTION_CATCHING=0 \ + -s INITIAL_MEMORY=26214400 \ + " + # 26214400 is 25 MiB + LINK_FLAGS " \ + --bind \ + --source-map-base https://retro.ai/ \ + -O0 \ + -g4 \ + -s DISABLE_EXCEPTION_CATCHING=0 \ + -s INITIAL_MEMORY=26214400 \ + " + ) +endif () + +################################################################################ +# +# Install libraries +# +################################################################################ + +# +# Motion tracker +# + +INSTALL( + FILES + "${CMAKE_BINARY_DIR}/motion_tracker.js" + "${CMAKE_BINARY_DIR}/motion_tracker.wasm" + "${CMAKE_BINARY_DIR}/motion_tracker.wasm.map" + DESTINATION + motion_tracker +) diff --git a/lib/build-ci.sh b/lib/build-ci.sh new file mode 100755 index 000000000..4219ffc4d --- /dev/null +++ b/lib/build-ci.sh @@ -0,0 +1,67 @@ +#!/bin/bash +################################################################################ +# +# Copyright (C) 2000-2023 retro.ai +# This file is part of retro-dapp - https://github.com/RetroAI/retro-dapp +# +# SPDX-License-Identifier: Apache-2.0 +# See the file LICENSE.md for more information. +# +################################################################################ + +################################################################################ +# +# Helper for CI infrastructure. Sets the appropriate paths and calls CMake. +# +################################################################################ + +# Enable strict shell mode +set -o errexit +set -o nounset +set -o pipefail + +# +# Environment paths +# + +# Get the absolute path to this script +SOURCE_DIRECTORY="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +# Directory of the depends build system +TOOL_DIRECTORY="${SOURCE_DIRECTORY}/../tools" + +# Directory for intermediate build files +BUILD_DIRECTORY="${TOOL_DIRECTORY}/build/cpp-libs" + +# Directory of the Emscripten SDK +EMSDK_DIRECTORY="${TOOL_DIRECTORY}/repos/emsdk" + +# Directory of the installed dependency files +DEPENDS_DIRECTORY="${TOOL_DIRECTORY}/dist" + +# Directory to place the generated libraries +INSTALL_DIRECTORY="${SOURCE_DIRECTORY}/../frontend/public" + +# Ensure directories exist +mkdir -p "${BUILD_DIRECTORY}" +mkdir -p "${INSTALL_DIRECTORY}" + +# +# Setup environment +# + +source "${EMSDK_DIRECTORY}/emsdk_set_env.sh" + +# +# Call CMake +# + +cd "${BUILD_DIRECTORY}" + +emcmake cmake \ + "${SOURCE_DIRECTORY}" \ + -DCMAKE_FIND_ROOT_PATH="${DEPENDS_DIRECTORY}" \ + -DCMAKE_INSTALL_PREFIX="${INSTALL_DIRECTORY}" \ + $(! command -v ccache &> /dev/null || echo "-DCMAKE_CXX_COMPILER_LAUNCHER=ccache") \ + +cmake --build "${BUILD_DIRECTORY}" -j$(shell getconf _NPROCESSORS_ONLN) --target install diff --git a/tools/build-depends.sh b/tools/build-depends.sh index 84ea9ab1e..4b77c8bc3 100755 --- a/tools/build-depends.sh +++ b/tools/build-depends.sh @@ -72,6 +72,9 @@ function depends-all() { fi make -C "${TOOL_DIR}" -j$(getconf _NPROCESSORS_ONLN) ${BUILD_DEPENDS} + + # Build C++ libraries + "${CPP_LIBRARY_DIR}/build-ci.sh" } function depends-checkout() { diff --git a/tools/build-paths.sh b/tools/build-paths.sh index 7c7923e99..ad3f2aabe 100755 --- a/tools/build-paths.sh +++ b/tools/build-paths.sh @@ -32,6 +32,9 @@ SOURCE_DIR="src" # Directory for generated source GENERATED_SOURCE_DIR="${SOURCE_DIR}/generated" +# C++ libraries directory +CPP_LIBRARY_DIR="lib" + # Tooling directory TOOL_DIR="tools"