forked from AprilRobotics/apriltag
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAprilTagJNI.cpp
363 lines (303 loc) · 10.5 KB
/
AprilTagJNI.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "AprilTagJNI.h"
#include "apriltag.h"
#include "tag36h11.h"
#include "tag25h9.h"
#include "tag16h5.h"
#include "tagCircle21h7.h"
#include "tagCircle49h12.h"
#include "tagCustom48h12.h"
#include "tagStandard41h12.h"
#include "tagStandard52h13.h"
#include "apriltag_pose.h"
#include <vector>
#include <algorithm>
#include <math.h>
struct DetectorState
{
int id;
apriltag_detector_t *td;
apriltag_family_t *tf;
void (*tf_destroy)(apriltag_family_t *);
};
std::vector<DetectorState> detectors;
extern "C"
{
JNIEXPORT jlong JNICALL Java_org_photonvision_vision_apriltag_AprilTagJNI_AprilTag_1Create(JNIEnv *env,
jclass cls, jstring jstr, jdouble decimate, jdouble blur, jint threads, jboolean debug, jboolean refine_edges)
{
// Initialize tag detector with options
apriltag_family_t *tf = NULL;
// const char *famname = fam;
const char *famname = env->GetStringUTFChars(jstr, 0);
void (*tf_destroy_func)(apriltag_family_t *);
if (!strcmp(famname, "tag36h11"))
{
tf = tag36h11_create();
tf_destroy_func = tag36h11_destroy;
}
else if (!strcmp(famname, "tag25h9"))
{
tf = tag25h9_create();
tf_destroy_func = tag25h9_destroy;
}
else if (!strcmp(famname, "tag16h5"))
{
tf = tag16h5_create();
tf_destroy_func = tag16h5_destroy;
}
else if (!strcmp(famname, "tagCircle21h7"))
{
tf = tagCircle21h7_create();
tf_destroy_func = tagCircle21h7_destroy;
}
else if (!strcmp(famname, "tagCircle49h12"))
{
tf = tagCircle49h12_create();
tf_destroy_func = tagCircle49h12_destroy;
}
else if (!strcmp(famname, "tagStandard41h12"))
{
tf = tagStandard41h12_create();
tf_destroy_func = tagStandard41h12_destroy;
}
else if (!strcmp(famname, "tagStandard52h13"))
{
tf = tagStandard52h13_create();
tf_destroy_func = tagStandard52h13_destroy;
}
else if (!strcmp(famname, "tagCustom48h12"))
{
tf = tagCustom48h12_create();
tf_destroy_func = tagCustom48h12_destroy;
}
else
{
printf("Unrecognized tag family name. Use e.g. \"tag36h11\".\n");
env->ReleaseStringUTFChars(jstr, famname);
return 0;
}
apriltag_detector_t *td = apriltag_detector_create();
apriltag_detector_add_family(td, tf);
td->quad_decimate = (float)decimate;
td->quad_sigma = (float)blur;
td->nthreads = threads;
td->debug = debug;
td->refine_edges = refine_edges;
env->ReleaseStringUTFChars(jstr, famname);
// printf("Looking for max\n");
auto max = std::max_element(detectors.begin(), detectors.end(), [](DetectorState &a, DetectorState &b)
{ return a.id < b.id; }); // detectors.size();
int index = 0;
if(max != detectors.end()) index = max->id + 1;
detectors.push_back({index, td, tf, tf_destroy_func});
printf("Created detector at idx %i\n", index);
return (jlong)index;
}
#define WPI_JNI_MAKEJARRAY(T, F) \
inline T##Array MakeJ##F##Array(JNIEnv *env, T *data, size_t size) \
{ \
T##Array jarr = env->New##F##Array(size); \
if (!jarr) \
{ \
return nullptr; \
} \
env->Set##F##ArrayRegion(jarr, 0, size, data); \
return jarr; \
}
WPI_JNI_MAKEJARRAY(jboolean, Boolean)
WPI_JNI_MAKEJARRAY(jbyte, Byte)
WPI_JNI_MAKEJARRAY(jshort, Short)
WPI_JNI_MAKEJARRAY(jlong, Long)
WPI_JNI_MAKEJARRAY(jfloat, Float)
WPI_JNI_MAKEJARRAY(jdouble, Double)
#undef WPI_JNI_MAKEJARRAY
/**
* Finds a class and keeps it as a global reference.
*
* Use with caution, as the destructor does NOT call DeleteGlobalRef due to
* potential shutdown issues with doing so.
*/
class JClass
{
public:
JClass() = default;
JClass(JNIEnv *env, const char *name)
{
jclass local = env->FindClass(name);
if (!local)
{
return;
}
m_cls = static_cast<jclass>(env->NewGlobalRef(local));
env->DeleteLocalRef(local);
}
void free(JNIEnv *env)
{
if (m_cls)
{
env->DeleteGlobalRef(m_cls);
}
m_cls = nullptr;
}
explicit operator bool() const { return m_cls; }
operator jclass() const { return m_cls; }
protected:
jclass m_cls = nullptr;
};
JClass detectionClass;
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
{
JNIEnv *env;
if (vm->GetEnv((void **)(&env), JNI_VERSION_1_6) != JNI_OK)
{
return JNI_ERR;
}
detectionClass = JClass(env, "org/photonvision/vision/apriltag/DetectionResult");
if (!detectionClass)
{
printf("Couldn't find class!");
return JNI_ERR;
}
return JNI_VERSION_1_6;
}
static jobject MakeJObject(JNIEnv *env, const apriltag_detection_t *detect,
apriltag_pose_t& pose1, apriltag_pose_t& pose2,
double error1, double error2)
{
// Constructor signature must match Java! I = int, F = float, [D = double array
static jmethodID constructor =
env->GetMethodID(detectionClass, "<init>", "(IIF[DDD[D[D[DD[D[DD)V");
if (!constructor)
{
return nullptr;
}
if (!detect)
{
return nullptr;
}
// We have to copy the homography matrix and coners into jdoubles
jdouble h[9]; // = new jdouble[9]{};
for (int i = 0; i < 9; i++) {
h[i] = detect->H->data[i];
}
jdouble corners[8]; // = new jdouble[8]{};
for (int i = 0; i < 4; i++)
{
corners[i * 2] = detect->p[i][0];
corners[i * 2 + 1] = detect->p[i][1];
}
jdoubleArray harr = MakeJDoubleArray(env, h, 9);
jdoubleArray carr = MakeJDoubleArray(env, corners, 8);
// The rotation of the target is encoded as a 3 by 3 rotation matrix, we'll convert to a row-major array
jdouble pose1RotMat[9] = {0};
jdouble pose2RotMat[9] = {0};
for (int i = 0; i < 9; i++) {
if (pose1.R) {
pose1RotMat[i] = pose1.R->data[i];
}
if (pose2.R) {
pose2RotMat[i] = pose2.R->data[i];
}
}
// And translation a 3x1 vector (todo check axis order)
jdouble pose1Trans[3] = {0};
jdouble pose2Trans[3] = {0};
for (int i = 0; i < 3; i++) {
if (pose1.t && pose1.t->data) {
pose1Trans[i] = pose1.t->data[i];
}
if (pose2.t && pose2.t->data) {
pose2Trans[i] = pose2.t->data[i];
}
}
jdoubleArray pose1rotArr = MakeJDoubleArray(env, pose1RotMat, 9);
jdoubleArray pose2rotArr = MakeJDoubleArray(env, pose2RotMat, 9);
jdoubleArray pose1transArr = MakeJDoubleArray(env, pose1Trans, 3);
jdoubleArray pose2transArr = MakeJDoubleArray(env, pose2Trans, 3);
jdouble err1 = error1;
jdouble err2 = error2;
// Actually call the constructor
auto ret = env->NewObject(
detectionClass, constructor,
(jint)detect->id, (jint)detect->hamming, (jfloat)detect->decision_margin,
harr, (jdouble)detect->c[0], (jdouble)detect->c[1], carr,
pose1transArr, pose1rotArr, err1,
pose2transArr, pose2rotArr, err2);
// TODO we don't seem to need this... or at least, it doesnt leak rn
// env->ReleaseDoubleArrayElements(harr, h, 0);
// env->ReleaseDoubleArrayElements(carr, corners, 0);
return ret;
}
JNIEXPORT jobjectArray JNICALL Java_org_photonvision_vision_apriltag_AprilTagJNI_AprilTag_1Detect(JNIEnv *env,
jclass cls, jlong detectIdx, jlong pData,
jint rows, jint cols,
jboolean doPoseEstimation, jdouble tagWidthMeters, jdouble fx, jdouble fy, jdouble cx, jdouble cy, jint nIters)
{
// No image, can't do anything
if (!pData) {
return nullptr;
}
// Make an image_u8_t header for the Mat data
image_u8_t im = {(int32_t)cols,
(int32_t)rows,
(int32_t)cols,
(uint8_t *)pData};
// Get our detector
auto state = std::find_if(detectors.begin(), detectors.end(), [&](DetectorState& s) { return s.id == detectIdx; });
if (state == detectors.end()) {
return nullptr;
}
// And run the detector on our new image
zarray_t *detections = apriltag_detector_detect(state->td, &im);
int size = zarray_size(detections);
// Object array to return to Java
jobjectArray jarr = env->NewObjectArray(size, detectionClass, nullptr);
if (!jarr)
{
printf("Couldn't make array\n");
return nullptr;
}
// Global pose
apriltag_pose_t pose1 = { 0 };
apriltag_pose_t pose2 = { 0 };
// printf("Created array %llu! Got %i targets!\n", &jarr, size);
// Add our detected targets to the array
for (size_t i = 0; i < size; ++i)
{
apriltag_detection_t *det = {0};
zarray_get(detections, i, &det);
if (det != nullptr)
{
double err1 = HUGE_VAL; //Should get overwritten if pose estimation is happening
double err2 = HUGE_VAL;
if (doPoseEstimation) {
// Feed results to the pose estimator
apriltag_detection_info_t info { det, tagWidthMeters, fx, fy, cx, cy };
estimate_tag_pose_orthogonal_iteration(&info, &err1, &pose1, &err2, &pose2, nIters);
}
jobject obj = MakeJObject(env, det, pose1, pose2, err1, err2);
env->SetObjectArrayElement(jarr, i, obj);
}
}
// Now that stuff's in our Java-side array, we can clean up native memory
apriltag_detections_destroy(detections);
return jarr;
}
JNIEXPORT void JNICALL Java_org_photonvision_vision_apriltag_AprilTagJNI_AprilTag_1Destroy(JNIEnv *env, jclass clazz, jlong detectIdx)
{
printf("Destroying detector at idx %li\n", (long)detectIdx);
auto state = std::find_if(detectors.begin(), detectors.end(), [&](DetectorState& s) { return s.id == detectIdx; });
if(state == detectors.end()) return;
if (state->td)
{
apriltag_detector_destroy(state->td);
state->td = NULL;
}
if (state->tf)
{
state->tf_destroy(state->tf);
state->tf = NULL;
}
detectors.erase(detectors.begin() + detectIdx);
}
}