- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在将 Ceres 求解器库集成到我的 Android 应用程序中。我在 Android Studio 中使用 CMakeLists.txt 为所有架构创建了预构建的共享库(.so 文件)。现在我想用 OpenCV java 包装器在 Java/Kotlin 中实现 Bundle 调整。
要在 Android 应用程序中使用 Ceres Solver,我们必须使用 C++ 中的 .so 文件和 Ceres Solver 及其依赖库的头文件编写 ceres 求解器逻辑。然后我们必须将包装器写入我们自己的方法,以便在 Java/Kotlin 中使用。
为此,我使用以下 CMakeLists.txt 文件
# 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})
使用 Ceres Solver 类和方法 native-lib.cpp 的示例 C++ 文件
#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;
}
构建应用程序时出现以下错误。
error One of CERES_USE_OPENMP, CERES_USE_CXX11_THREADS or CERES_NO_THREADS must be defined.
我是 NDK、Cmake 和 Ceres Solver 的新手,所以我不知道如何解决这个问题。有人请建议我如何解决问题或指出我做错了什么。提前致谢。
最佳答案
如评论所述,您需要在使用 Ceres 时至少定义这些预处理器定义之一(参见逻辑 here)。您可以使用 target_compile_definitions()
将定义添加到 CMake 的编译中:
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)
...
关于android - 如何修复 Ceres_USE_OPENMP、CERES_USE_CXX11_THREADS 或 CERES_NO_THREADS 中的一个错误必须在 Ceres Solver Android 中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58302815/
我正在将 Ceres 求解器库集成到我的 Android 应用程序中。我在 Android Studio 中使用 CMakeLists.txt 为所有架构创建了预构建的共享库(.so 文件)。现在我想
我是一名优秀的程序员,十分优秀!