Skip to content

Commit

Permalink
Update mrcal_jni.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
mcm001 committed May 9, 2024
1 parent fdd4f2b commit 741b1ad
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/mrcal_jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <stdexcept>
#include <vector>

#include <exception>

#include "mrcal_wrapper.h"

// JClass helper from wpilib
Expand Down Expand Up @@ -101,6 +103,17 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {

} // extern "C"

static std::string what(const std::exception_ptr &eptr = std::current_exception())
{
if (!eptr) { throw std::bad_exception(); }

try { std::rethrow_exception(eptr); }
catch (const std::exception &e) { return e.what() ; }
catch (const std::string &e) { return e ; }
catch (const char *e) { return e ; }
catch (...) { return "who knows"; }
}

/*
* Class: org_photonvision_mrcal_MrCalJNI_mrcal_1calibrate
* Method: 1camera
Expand All @@ -112,6 +125,9 @@ Java_org_photonvision_mrcal_MrCalJNI_mrcal_1calibrate_1camera
jint boardHeight, jdouble boardSpacing, jint imageWidth, jint imageHeight,
jdouble focalLenGuessMM)
{
try {


// Pull out arrays. We rely on data being packed and aligned to make this
// work! Observations should be [x, y, level]
std::span<mrcal_point3_t> observations{
Expand Down Expand Up @@ -197,6 +213,14 @@ Java_org_photonvision_mrcal_MrCalJNI_mrcal_1calibrate_1camera
rms_err, residuals, warp_x, warp_y, Noutliers);

return ret;

} catch (...) {
std::cerr << "Calibration exception: " << what() << std::endl;

static char buff[512];
strcpy(buff, what().c_str());
env->ThrowNew(env->FindClass("java/lang/Exception"), buff);
}
}

/*
Expand Down

0 comments on commit 741b1ad

Please sign in to comment.