3

I am integrating Ceres Solver Library in my Android Application. I have created the prebuilt shared library (.so files) for all the architecture using CMakeLists.txt in Android Studio. Now I want to implement the Bundle adjustment in Java/Kotlin with OpenCV java wrappers.

To consume Ceres Solver in Android application we have to write the ceres solver logic in C++ using .so file and header files of the Ceres Solver and its dependency libraries. Then we have to write the wrappers to our own methods to consume in Java / Kotlin.

For this I am using the following CMakeLists.txt file

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             native-lib.cpp )


find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )


include_directories( C:/eigen-eigen-323c052e1731 )
include_directories( C:/My_Data/Ceres-Solver-Builder/ceres-solver/include )
include_directories( C:/My_Data/Ceres-Solver-Builder/ceres-solver/config )
include_directories( C:/My_Data/Ceres-Solver-Builder/ceres-solver/internal/ceres/miniglog )
include_directories( C:/My_Data/Ceres-Solver-Builder/ceres-solver/internal )

add_library( ceres-lib SHARED IMPORTED)
set_target_properties( # Specifies the target library.
        ceres-lib

        # Specifies the parameter you want to define.
        PROPERTIES IMPORTED_LOCATION

        # Provides the path to the library you want to import.
        C:/My_Data/AS_Workspace/JNIApplication/app/src/main/jniLibs/${ANDROID_ABI}/libceres.so )
target_link_libraries( native-lib ceres-lib ${log-lib})

Sample C++ file that uses Ceres Solver classes and methods native-lib.cpp

#include <jni.h>
#include <string>
#include <bitset>

#include "ceres/ceres.h"
#include "glog/logging.h"

using ceres::AutoDiffCostFunction;
using ceres::CostFunction;
using ceres::Problem;
using ceres::Solver;
using ceres::Solve;
int test();

extern "C" JNIEXPORT jstring JNICALL
Java_com_trimble_jniapplication_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";

    test();

    return env->NewStringUTF(hello.c_str());
}



// A templated cost functor that implements the residual r = 10 -
// x. The method operator() is templated so that we can then use an
// automatic differentiation wrapper around it to generate its
// derivatives.
struct CostFunctor {
    template<typename T>
    bool operator()(const T *const x, T *residual) const {
        residual[0] = 10.0 - x[0];
        return true;
    }
};

int test() {
    //google::InitGoogleLogging(argv[0]);

    // The variable to solve for with its initial value. It will be
    // mutated in place by the solver.
    double x = 0.5;
    const double initial_x = x;

    // Build the problem.
    Problem problem;

    // Set up the only cost function (also known as residual). This uses
    // auto-differentiation to obtain the derivative (jacobian).
    CostFunction *cost_function = new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
    problem.AddResidualBlock(cost_function, NULL, &x);

    // Run the solver!
    Solver::Options options;
    options.minimizer_progress_to_stdout = true;
    Solver::Summary summary;
    Solve(options, &problem, &summary);

    std::cout << summary.BriefReport() << "\n";
    std::cout << "x : " << initial_x
              << " -> " << x << "\n";
    return 0;
}

When I build the application I am getting the following error.

error One of CERES_USE_OPENMP, CERES_USE_CXX11_THREADS or CERES_NO_THREADS must be defined.

I am new to NDK, Cmake, and Ceres Solver, So I don't know how to fix this problem. Someone please suggest me how to fix the problem or point me what I am doing wrong. Thanks in advance.

Kevin
  • 16,549
  • 8
  • 60
  • 74
Kumar M
  • 994
  • 6
  • 21
  • You need to define one of these preprocessor macros before you include the `ceres/ceres.h` header. Either with `#define` in your C++ code or with `add_definitions(-DCERES...)` in CMake. – Botje Oct 09 '19 at 11:14
  • @Botje Thanks for your comment. Now I have added add_definitions( -DCERES_NO_THREADS ) in CMakeLists.txt and the error is gone. Thanks again. – Kumar M Oct 09 '19 at 11:31

1 Answers1

1

As commented, you need to define at least one of these preprocessor definitions when using Ceres (see logic here). You can add definitions to the compilation with CMake using target_compile_definitions():

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             native-lib.cpp )

target_compile_definitions(native-lib PRIVATE CERES_USE_CXX11_THREADS=1)

...
Kevin
  • 16,549
  • 8
  • 60
  • 74