Skip to content

Commit

Permalink
update CMake and rename source folder
Browse files Browse the repository at this point in the history
  • Loading branch information
pmokeev committed Aug 7, 2021
1 parent 6cdb21d commit 526a898
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 63 deletions.
27 changes: 21 additions & 6 deletions app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,43 @@ cmake_minimum_required(VERSION 3.14)
project(twist-n-sync)

include(FetchContent)

FetchContent_Declare(
cmodule
URL "https://github.com/scapix-com/cmodule/archive/v1.0.29.tar.gz"
URL_HASH SHA256=b49019b355423aebacd927e99541b146c900ef416ae1da6a8343a2a274dd4876
)

FetchContent_Declare(
eigen
GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git
GIT_TAG origin/master
)

FetchContent_Declare(
spline
GIT_REPOSITORY https://github.com/ttk592/spline.git
GIT_TAG origin/master
)

FetchContent_MakeAvailable(cmodule)
FetchContent_MakeAvailable(eigen)
FetchContent_MakeAvailable(spline)

set(SCAPIX_BRIDGE "java" CACHE STRING "java")
set(SCAPIX_PLATFORM "windows" CACHE STRING "one of the folders inside 'source/chat/platform': android, windows")
set(SCAPIX_JAVA_API "android-28" CACHE STRING "one of the folders inside 'scapix/java_api': jdk-11.0.2, android-28, etc.")
set(GENERATED_DIR generated/bridge/java)
set(SOURCE_LIST source/TimeSync.cpp source/util/CubicSpline.cpp source/util/TSUtil.cpp)
set(SOURCE_LIST twist-n-sync-cpp/TimeSync.cpp twist-n-sync-cpp/util/CubicSpline.cpp twist-n-sync-cpp/util/TSUtil.cpp)

add_library(bridge SHARED ${SOURCE_LIST})

find_package(Scapix REQUIRED)
scapix_bridge_headers(bridge "com.googleresearch.capturesync.softwaresync" source/TimeSync.h)
scapix_bridge_headers(bridge "com.googleresearch.capturesync.softwaresync" twist-n-sync-cpp/TimeSync.h)

include_directories(
${GENERATED_DIR}
source
twist-n-sync-cpp
)

target_include_directories(bridge PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/lib/eigen")
target_include_directories(bridge PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/lib/spline/src")
target_include_directories(bridge PUBLIC "${CMAKE_BINARY_DIR}/_deps/eigen-src")
target_include_directories(bridge PUBLIC "${CMAKE_BINARY_DIR}/_deps/spline-src/src")
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
//
// Created by achains on 18.07.2021.
//

#include "TimeSync.h"
#include "util/TSUtil.h"
#include "util/CubicSpline.h"
Expand All @@ -17,19 +13,19 @@ TimeSync::TimeSync(std::vector<std::vector<double>> const & gyro_first,
std::vector<double> const & ts_first,
std::vector<double> const & ts_second,
bool const & do_resample):
gyro_first_(TSUtil::vectorToEigMatrixX3d(gyro_first)),
gyro_second_(TSUtil::vectorToEigMatrixX3d(gyro_second)),
ts_first_(TSUtil::vectorToEigVectorXd(ts_first)),
ts_second_(TSUtil::vectorToEigVectorXd(ts_second)),
gyro_first_(tsutil::vectorToEigMatrixX3d(gyro_first)),
gyro_second_(tsutil::vectorToEigMatrixX3d(gyro_second)),
ts_first_(tsutil::vectorToEigVectorXd(ts_first)),
ts_second_(tsutil::vectorToEigVectorXd(ts_second)),
do_resample_(do_resample) {}


TSUtil::CorrData TimeSync::getInitialIndex() const {
tsutil::CorrData TimeSync::getInitialIndex() const {

Eigen::VectorXd norm_first = TSUtil::getNormOfRows(gyro_first_);
Eigen::VectorXd norm_second = TSUtil::getNormOfRows(gyro_second_);
Eigen::VectorXd norm_first = tsutil::getNormOfRows(gyro_first_);
Eigen::VectorXd norm_second = tsutil::getNormOfRows(gyro_second_);

Eigen::VectorXd cross_cor = TSUtil::eigenCrossCor(norm_first, norm_second);
Eigen::VectorXd cross_cor = tsutil::eigenCrossCor(norm_second, norm_first);

return {cross_cor, std::distance(cross_cor.begin(), std::max_element(cross_cor.begin(), cross_cor.end()))};
}
Expand All @@ -38,21 +34,21 @@ Eigen::MatrixX3d & TimeSync::interpolateGyro(Eigen::VectorXd const & ts_old, Eig
Eigen::VectorXd const & ts_new, Eigen::MatrixX3d & gyro_new) {
assert (gyro_old.rows() == gyro_new.rows());

gyro_new << TSUtil::interpolate(ts_old, gyro_old(Eigen::all, 0), ts_new),
TSUtil::interpolate(ts_old, gyro_old(Eigen::all, 1), ts_new),
TSUtil::interpolate(ts_old, gyro_old(Eigen::all, 2), ts_new);
gyro_new << tsutil::interpolate(ts_old, gyro_old(Eigen::all, 0), ts_new),
tsutil::interpolate(ts_old, gyro_old(Eigen::all, 1), ts_new),
tsutil::interpolate(ts_old, gyro_old(Eigen::all, 2), ts_new);
return gyro_new;
}

void TimeSync::resample(double const & accuracy){
double time_first_mean = TSUtil::adjDiffEigen(ts_first_).mean();
double time_second_mean = TSUtil::adjDiffEigen(ts_second_).mean();
double time_first_mean = tsutil::adjDiffEigen(ts_first_).mean();
double time_second_mean = tsutil::adjDiffEigen(ts_second_).mean();

dt_ = std::min({accuracy, time_first_mean, time_second_mean});

if (do_resample_){
Eigen::VectorXd ts_first_new = TSUtil::arangeEigen(ts_first_[0], *ts_first_.end() + dt_, dt_);
Eigen::VectorXd ts_second_new = TSUtil::arangeEigen(ts_second_[0], *ts_second_.end() + dt_, dt_);
Eigen::VectorXd ts_first_new = tsutil::arangeEigen(ts_first_[0], *ts_first_.end() + dt_, dt_);
Eigen::VectorXd ts_second_new = tsutil::arangeEigen(ts_second_[0], *ts_second_.end() + dt_, dt_);

TimeSync::interpolateGyro(ts_first_, gyro_first_, ts_first_new, gyro_first_);
TimeSync::interpolateGyro(ts_second_, gyro_second_, ts_second_new, gyro_second_);
Expand All @@ -64,7 +60,7 @@ Eigen::Vector2d TimeSync::obtainRoots(Eigen::VectorXd const & coeffs, Eigen::Ind
for (auto i = 0; i < order; ++i){
equation[i] = static_cast<double>(order - i) * coeffs[i];
}
return TSUtil::quadraticRoots(equation.reverse());
return tsutil::quadraticRoots(equation.reverse());
}

void TimeSync::obtainDelay(){
Expand All @@ -73,7 +69,7 @@ void TimeSync::obtainDelay(){
Eigen::Index shift = -gyro_first_.rows() + 1;

// Cross-cor estimation
TSUtil::CorrData corr_data = TimeSync::getInitialIndex();
tsutil::CorrData corr_data = TimeSync::getInitialIndex();
corr_data.initial_index += shift;

Eigen::MatrixX3d tmp_xx1;
Expand Down Expand Up @@ -105,8 +101,8 @@ void TimeSync::obtainDelay(){
corr_data = TimeSync::getInitialIndex();

// Cross-cor, based cubic spline coefficients
CubicSpline cubic_spline(TSUtil::arangeEigen(0., static_cast<double>(corr_data.cross_cor.size())),
corr_data.cross_cor);
CubicSpline cubic_spline(tsutil::arangeEigen(0., static_cast<double>(corr_data.cross_cor.size())),
corr_data.cross_cor);

Eigen::Matrix4Xd spline_coefficients = cubic_spline.getCoefficients();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
//
// Created by achains on 18.07.2021.
//

#ifndef TWIST_N_SYNC_CPP_MODULE_TIMESYNC_H
#define TWIST_N_SYNC_CPP_MODULE_TIMESYNC_H

Expand Down Expand Up @@ -31,7 +27,7 @@ class TimeSync : public scapix::bridge::object<TimeSync> {

static Eigen::Vector2d obtainRoots(Eigen::VectorXd const & coeffs, Eigen::Index const & order);

TSUtil::CorrData getInitialIndex() const;
tsutil::CorrData getInitialIndex() const;

// 3D angular velocities from devices' gyros
Eigen::MatrixX3d gyro_first_;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
//
// Created by achains on 26.07.2021.
//

#include "CubicSpline.h"
#include "TSUtil.h"

Expand Down Expand Up @@ -39,8 +35,8 @@ Eigen::Matrix4Xd CubicSpline::getCoefficients() {
calculateDerivative();
}
std::vector<double> Y = m_spline_.get_y();
Eigen::VectorXd A = TSUtil::vectorToEigVectorXd(Y);
Eigen::VectorXd B = TSUtil::vectorToEigVectorXd(derivative_);
Eigen::VectorXd A = tsutil::vectorToEigVectorXd(Y);
Eigen::VectorXd B = tsutil::vectorToEigVectorXd(derivative_);

double dx = m_spline_.get_x()[1] - m_spline_.get_x()[0];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
//
// Created by achains on 26.07.2021.
//

#ifndef TWIST_N_SYNC_CPP_MODULE_CUBICSPLINE_H
#define TWIST_N_SYNC_CPP_MODULE_CUBICSPLINE_H

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
//
// Created by achains on 23.07.2021.
//

#include "TSUtil.h"
#include "unsupported/Eigen/FFT"
#include "unsupported/Eigen/Polynomials"
#include "util/CubicSpline.h"

#include <numeric>

namespace TSUtil {
namespace tsutil {

Eigen::VectorXd arangeEigen(double start, double const & stop, double const & step){
Eigen::Index size = std::ceil((stop - start) / step);
Expand Down Expand Up @@ -63,20 +59,22 @@ namespace TSUtil {
Eigen::VectorXd eigenCrossCor(Eigen::VectorXd & data_1, Eigen::VectorXd & data_2){
// Cross-cor(x, y) = iFFT(FFT(x) * conj(FFT(y)))

Eigen::Index shift_size_data1 = data_1.size();
Eigen::Index shift_size_data2 = data_2.size();
Eigen::Index old_data1_size = data_1.size();
Eigen::Index old_data2_size = data_2.size();

// Length of Discrete Fourier Transform
Eigen::Index N = shift_size_data1 + shift_size_data2 - 1;
Eigen::Index N = old_data1_size + old_data2_size - 1;

N = static_cast<Eigen::Index> (std::pow(2, std::ceil(std::log2(N))));

Eigen::Index shift = N - (old_data1_size + old_data2_size - 1);

// Zero padding both vectors

data_1.conservativeResize(N);
data_2.conservativeResize(N);
for (Eigen::Index i = shift_size_data1; i < N; ++i) data_1[i] = 0.0;
for (Eigen::Index i = shift_size_data2; i < N; ++i) data_2[i] = 0.0;
for (Eigen::Index i = old_data1_size; i < N; ++i) data_1[i] = 0.0;
for (Eigen::Index i = old_data2_size; i < N; ++i) data_2[i] = 0.0;

Eigen::FFT <double> fft;

Expand All @@ -92,11 +90,11 @@ namespace TSUtil {
Eigen::VectorXd unbiased_result(N);
fft.inv(unbiased_result, fft_result);

// Rotating cross-correlation vector on shift_size_data1
// Rotating cross-correlation vector on shift
Eigen::VectorXd cross_cor(N);
cross_cor << unbiased_result(Eigen::seq(shift_size_data1, Eigen::last)),
unbiased_result(Eigen::seq(0, shift_size_data1 - 1));
cross_cor << unbiased_result(Eigen::seq(old_data1_size, Eigen::last)),
unbiased_result(Eigen::seq(0, old_data1_size - 1));

return cross_cor.reverse();
return cross_cor(Eigen::seq(shift, Eigen::last));
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
//
// Created by achains on 23.07.2021.
//

#ifndef TWIST_N_SYNC_CPP_MODULE_TSUTIL_H
#define TWIST_N_SYNC_CPP_MODULE_TSUTIL_H

#include "Eigen/Core"

#include <vector>

namespace TSUtil {
namespace tsutil {
// Implementation of numpy.arange function
Eigen::VectorXd arangeEigen(double start, double const & stop, double const & step = 1.0);

Expand Down

0 comments on commit 526a898

Please sign in to comment.