forked from mikocml/jsartoolkit5
-
Notifications
You must be signed in to change notification settings - Fork 20
/
ARToolKitJS.cpp
1132 lines (893 loc) · 30.5 KB
/
ARToolKitJS.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <AR/ar.h>
//#include <AR/gsub_lite.h>
// #include <AR/gsub_es2.h>
#include <AR/arMulti.h>
#include <emscripten.h>
#include <string>
#include <vector>
#include <unordered_map>
#include <AR/config.h>
#include <AR/arFilterTransMat.h>
#include <AR2/tracking.h>
#include <AR/paramGL.h>
#include <AR/video.h>
#include <KPM/kpm.h>
#include "ARMarkerNFT.h"
#include "trackingSub.h"
struct multi_marker {
int id;
ARMultiMarkerInfoT *multiMarkerHandle;
};
struct arController {
int id;
ARParam param;
ARParamLT *paramLT = NULL;
ARUint8 *videoFrame = NULL;
int videoFrameSize;
ARUint8 *videoLuma = NULL;
int width = 0;
int height = 0;
ARHandle *arhandle = NULL;
ARPattHandle *arPattHandle = NULL;
ARMultiMarkerInfoT *arMultiMarkerHandle = NULL;
AR3DHandle* ar3DHandle;
KpmHandle* kpmHandle;
AR2HandleT* ar2Handle;
THREAD_HANDLE_T *threadHandle = NULL;
int surfaceSetCount = 0; // Running NFT marker id
std::unordered_map<int, AR2SurfaceSetT*> surfaceSets;
ARdouble nearPlane = 0.0001;
ARdouble farPlane = 1000.0;
std::vector<multi_marker> multi_markers;
int patt_id = 0; // Running pattern marker id
ARdouble cameraLens[16];
AR_PIXEL_FORMAT pixFormat = AR_PIXEL_FORMAT_RGBA;
};
std::unordered_map<int, arController> arControllers;
std::unordered_map<int, ARParam> cameraParams;
// ============================================================================
// Global variables
// ============================================================================
static ARdouble gTransform[3][4];
static int gARControllerID = 0;
static int gCameraID = 0;
static int ARCONTROLLER_NOT_FOUND = -1;
static int MULTIMARKER_NOT_FOUND = -2;
static int MARKER_INDEX_OUT_OF_BOUNDS = -3;
static ARMarkerInfo gMarkerInfo;
extern "C" {
/**
NFT API bindings
*/
int getNFTMarkerInfo(int id, int markerIndex) {
if (arControllers.find(id) == arControllers.end()) { return ARCONTROLLER_NOT_FOUND; }
arController *arc = &(arControllers[id]);
if (arc->surfaceSetCount <= markerIndex) {
return MARKER_INDEX_OUT_OF_BOUNDS;
}
KpmResult *kpmResult = NULL;
int kpmResultNum = -1;
int pageNo;
//kpmGetResult( arc->kpmHandle, &kpmResult, &kpmResultNum );
int i, j, k;
int flag = -1;
float err = -1;
float trans[3][4];
float trackingTrans[3][4];
kpmResultNum = trackingInitGetResult( arc->threadHandle, trackingTrans, &pageNo);
ARLOGi("kpmResultNum is: %d\n", kpmResultNum);
for( i = 0; i < kpmResultNum; i++ ) {
//if (kpmResult[i].pageNo == markerIndex && kpmResult[i].camPoseF == 0 ) {
if (pageNo == markerIndex ) {
// if( flag == -1 || err > kpmResult[i].error ) { // Take the first or best result.
if( flag == -1 ) { // Take the first or best result.
flag = i;
err = kpmResult[i].error;
ARLOGe("error in the tracking");
}
}
}
flag = kpmResultNum;
ARLOGi("flag is: %d\n", flag);
if (flag > -1) {
for (j = 0; j < 3; j++) {
for (k = 0; k < 4; k++) {
trans[j][k] = trackingTrans[j][k];
}
}
//ARLOGi("trackingTrans %d\n", trans);
EM_ASM_({
var $a = arguments;
var i = 0;
if (!artoolkit["NFTMarkerInfo"]) {
artoolkit["NFTMarkerInfo"] = ({
id: 0,
error: -1,
found: 0,
pose: [0,0,0,0, 0,0,0,0, 0,0,0,0]
});
}
var markerInfo = artoolkit["NFTMarkerInfo"];
markerInfo["id"] = $a[i++];
markerInfo["error"] = $a[i++];
markerInfo["found"] = 1;
markerInfo["pose"][0] = $a[i++];
markerInfo["pose"][1] = $a[i++];
markerInfo["pose"][2] = $a[i++];
markerInfo["pose"][3] = $a[i++];
markerInfo["pose"][4] = $a[i++];
markerInfo["pose"][5] = $a[i++];
markerInfo["pose"][6] = $a[i++];
markerInfo["pose"][7] = $a[i++];
markerInfo["pose"][8] = $a[i++];
markerInfo["pose"][9] = $a[i++];
markerInfo["pose"][10] = $a[i++];
markerInfo["pose"][11] = $a[i++];
},
markerIndex,
err,
trans[0][0],
trans[0][1],
trans[0][2],
trans[0][3],
trans[1][0],
trans[1][1],
trans[1][2],
trans[1][3],
trans[2][0],
trans[2][1],
trans[2][2],
trans[2][3]
);
} else {
EM_ASM_({
var $a = arguments;
var i = 0;
if (!artoolkit["NFTMarkerInfo"]) {
artoolkit["NFTMarkerInfo"] = ({
id: 0,
error: -1,
found: 0,
pose: [0,0,0,0, 0,0,0,0, 0,0,0,0]
});
}
var markerInfo = artoolkit["NFTMarkerInfo"];
markerInfo["id"] = $a[i++];
markerInfo["error"] = -1;
markerInfo["found"] = 0;
markerInfo["pose"][0] = 0;
markerInfo["pose"][1] = 0;
markerInfo["pose"][2] = 0;
markerInfo["pose"][3] = 0;
markerInfo["pose"][4] = 0;
markerInfo["pose"][5] = 0;
markerInfo["pose"][6] = 0;
markerInfo["pose"][7] = 0;
markerInfo["pose"][8] = 0;
markerInfo["pose"][9] = 0;
markerInfo["pose"][10] = 0;
markerInfo["pose"][11] = 0;
},
markerIndex
);
}
return 0;
}
THREAD_HANDLE_T *trackingInit(KpmHandle* kpmHandle){
// Start the KPM tracking thread.
THREAD_HANDLE_T *threadHandle;
threadHandle = trackingInitInit(kpmHandle);
if (!threadHandle) exit(-1);
return threadHandle;
}
int detectNFTMarker(int id) {
if (arControllers.find(id) == arControllers.end()) { return -1; }
arController *arc = &(arControllers[id]);
KpmResult *kpmResult = NULL;
// NFT results.
int detectedPage = -2; // -2 Tracking not inited, -1 tracking inited OK, >= 0 tracking online on page.
float trackingTrans[3][4];
int kpmResultNum = -1;
//kpmMatching( arc->kpmHandle, arc->videoLuma );
//kpmGetResult( arc->kpmHandle, &kpmResult, &kpmResultNum );
if (arc->threadHandle) {
// Perform NFT tracking.
float err;
int ret;
int pageNo;
if( detectedPage == -2 ) {
trackingInitStart( arc->threadHandle, arc->videoLuma );
detectedPage = -1;
}
if( detectedPage == -1 ) {
kpmResultNum = trackingInitGetResult( arc->threadHandle, trackingTrans, &pageNo);
}
}
return kpmResultNum;
}
KpmHandle *createKpmHandle(ARParamLT *cparamLT) {
KpmHandle *kpmHandle;
kpmHandle = kpmCreateHandle(cparamLT);
return kpmHandle;
}
int getKpmImageWidth(KpmHandle *kpmHandle) {
return kpmHandleGetXSize(kpmHandle);
}
int getKpmImageHeight(KpmHandle *kpmHandle) {
return kpmHandleGetYSize(kpmHandle);
}
// disbling this; maybe a very old implementation?
//int getKpmPixelSize(KpmHandle *kpmHandle) {
// return arUtilGetPixelSize(GetPixelFormat(kpmHandle));
//}
int setupAR2(int id) {
if (arControllers.find(id) == arControllers.end()) { return -1; }
arController *arc = &(arControllers[id]);
//arc->pixFormat = arVideoGetPixelFormat();
arc->kpmHandle = createKpmHandle(arc->paramLT);
// AR2 init.
if( (arc->ar2Handle = ar2CreateHandle(arc->paramLT, arc->pixFormat, AR2_TRACKING_DEFAULT_THREAD_NUM)) == NULL ) {
ARLOGe("Error: ar2CreateHandle.\n");
//kpmDeleteHandle(arc->kpmHandle);
return (FALSE);
}
if (threadGetCPU() <= 1) {
ARLOGi("Using NFT tracking settings for a single CPU.\n");
ar2SetTrackingThresh(arc->ar2Handle, 5.0);
ar2SetSimThresh(arc->ar2Handle, 0.50);
ar2SetSearchFeatureNum(arc->ar2Handle, 16);
ar2SetSearchSize(arc->ar2Handle, 6);
ar2SetTemplateSize1(arc->ar2Handle, 6);
ar2SetTemplateSize2(arc->ar2Handle, 6);
} else {
ARLOGi("Using NFT tracking settings for more than one CPU.\n");
ar2SetTrackingThresh(arc->ar2Handle, 5.0);
ar2SetSimThresh(arc->ar2Handle, 0.50);
ar2SetSearchFeatureNum(arc->ar2Handle, 16);
ar2SetSearchSize(arc->ar2Handle, 12);
ar2SetTemplateSize1(arc->ar2Handle, 6);
ar2SetTemplateSize2(arc->ar2Handle, 6);
}
return 0;
}
int loadNFTMarker(arController *arc, int surfaceSetCount, const char* datasetPathname) {
int i, pageNo;
AR2SurfaceSetT *surfaceSet;
KpmRefDataSet *refDataSet;
KpmHandle *kpmHandle = arc->kpmHandle;
arc->threadHandle = trackingInit(arc->kpmHandle);
refDataSet = NULL;
// Load KPM data.
KpmRefDataSet *refDataSet2;
ARLOGi("Reading %s.fset3\n", datasetPathname);
if (kpmLoadRefDataSet(datasetPathname, "fset3", &refDataSet2) < 0 ) {
ARLOGe("Error reading KPM data from %s.fset3\n", datasetPathname);
pageNo = -1;
return (FALSE);
}
pageNo = surfaceSetCount;
ARLOGi(" Assigned page no. %d.\n", surfaceSetCount);
if (kpmChangePageNoOfRefDataSet(refDataSet2, KpmChangePageNoAllPages, surfaceSetCount) < 0) {
ARLOGe("Error: kpmChangePageNoOfRefDataSet\n");
return (FALSE);
}
if (kpmMergeRefDataSet(&refDataSet, &refDataSet2) < 0) {
ARLOGe("Error: kpmMergeRefDataSet\n");
return (FALSE);
}
ARLOGi(" Done.\n");
// Load AR2 data.
ARLOGi("Reading %s.fset\n", datasetPathname);
if ((surfaceSet = ar2ReadSurfaceSet(datasetPathname, "fset", NULL)) == NULL ) {
ARLOGe("Error reading data from %s.fset\n", datasetPathname);
}
ARLOGi(" Done.\n");
arc->surfaceSets[surfaceSetCount] = surfaceSet;
if (kpmSetRefDataSet(kpmHandle, refDataSet) < 0) {
ARLOGe("Error: kpmSetRefDataSet\n");
return (FALSE);
}
kpmDeleteRefDataSet(&refDataSet);
ARLOGi("Loading of NFT data complete.\n");
return (TRUE);
}
/***************
* Set Log Level
****************/
void setLogLevel(int level) {
arLogLevel = level;
}
int getLogLevel() {
return arLogLevel;
}
/***********
* Teardown *
***********/
void deleteHandle(arController *arc) {
if (arc->arhandle != NULL) {
arPattDetach(arc->arhandle);
arDeleteHandle(arc->arhandle);
arc->arhandle = NULL;
}
if (arc->ar3DHandle != NULL) {
ar3DDeleteHandle(&(arc->ar3DHandle));
arc->ar3DHandle = NULL;
}
if (arc->paramLT != NULL) {
arParamLTFree(&(arc->paramLT));
arc->paramLT = NULL;
}
}
int teardown(int id) {
if (arControllers.find(id) == arControllers.end()) { return -1; }
arController *arc = &(arControllers[id]);
if (arc->videoFrame) {
free(arc->videoFrame);
arc->videoFrame = NULL;
arc->videoFrameSize = 0;
}
deleteHandle(arc);
arPattDeleteHandle(arc->arPattHandle);
arControllers.erase(id);
for (int i=0; i<arc->multi_markers.size(); i++) {
arMultiFreeConfig(arc->multi_markers[i].multiMarkerHandle);
}
delete &arc->multi_markers;
delete arc;
return 0;
}
/*****************
* Camera loading *
*****************/
int loadCamera(std::string cparam_name) {
ARParam param;
if (arParamLoad(cparam_name.c_str(), 1, ¶m) < 0) {
ARLOGe("loadCamera(): Error loading parameter file %s for camera.\n", cparam_name.c_str());
return -1;
}
int cameraID = gCameraID++;
cameraParams[cameraID] = param;
return cameraID;
}
int setCamera(int id, int cameraID) {
if (arControllers.find(id) == arControllers.end()) { return -1; }
arController *arc = &(arControllers[id]);
if (cameraParams.find(cameraID) == cameraParams.end()) { return -1; }
arc->param = cameraParams[cameraID];
if (arc->param.xsize != arc->width || arc->param.ysize != arc->height) {
ARLOGw("*** Camera Parameter resized from %d, %d. ***\n", arc->param.xsize, arc->param.ysize);
arParamChangeSize(&(arc->param), arc->width, arc->height, &(arc->param));
}
// ARLOGi("*** Camera Parameter ***\n");
// arParamDisp(&(arc->param));
deleteHandle(arc);
if ((arc->paramLT = arParamLTCreate(&(arc->param), AR_PARAM_LT_DEFAULT_OFFSET)) == NULL) {
ARLOGe("setCamera(): Error: arParamLTCreate.\n");
return -1;
}
// ARLOGi("setCamera(): arParamLTCreated\n..%d, %d\n", (arc->paramLT->param).xsize, (arc->paramLT->param).ysize);
// setup camera
if ((arc->arhandle = arCreateHandle(arc->paramLT)) == NULL) {
ARLOGe("setCamera(): Error: arCreateHandle.\n");
return -1;
}
// AR_DEFAULT_PIXEL_FORMAT
int set = arSetPixelFormat(arc->arhandle, arc->pixFormat);
// ARLOGi("setCamera(): arCreateHandle done\n");
arc->ar3DHandle = ar3DCreateHandle(&(arc->param));
if (arc->ar3DHandle == NULL) {
ARLOGe("setCamera(): Error creating 3D handle");
return -1;
}
// ARLOGi("setCamera(): ar3DCreateHandle done\n");
arPattAttach(arc->arhandle, arc->arPattHandle);
// ARLOGi("setCamera(): Pattern handler attached.\n");
arglCameraFrustumRH(&((arc->paramLT)->param), arc->nearPlane, arc->farPlane, arc->cameraLens);
arc->kpmHandle = createKpmHandle(arc->paramLT);
return 0;
}
/*****************
* Marker loading *
*****************/
static int loadMarker(const char *patt_name, int *patt_id, ARHandle *arhandle, ARPattHandle **pattHandle_p) {
// Loading only 1 pattern in this example.
if ((*patt_id = arPattLoad(*pattHandle_p, patt_name)) < 0) {
ARLOGe("loadMarker(): Error loading pattern file %s.\n", patt_name);
arPattDeleteHandle(*pattHandle_p);
return (FALSE);
}
return (TRUE);
}
static int loadMultiMarker(const char *patt_name, ARHandle *arHandle, ARPattHandle **pattHandle_p, ARMultiMarkerInfoT **arMultiConfig) {
if( (*arMultiConfig = arMultiReadConfigFile(patt_name, *pattHandle_p)) == NULL ) {
ARLOGe("config data load error !!\n");
arPattDeleteHandle(*pattHandle_p);
return (FALSE);
}
if( (*arMultiConfig)->patt_type == AR_MULTI_PATTERN_DETECTION_MODE_TEMPLATE ) {
arSetPatternDetectionMode( arHandle, AR_TEMPLATE_MATCHING_COLOR );
} else if( (*arMultiConfig)->patt_type == AR_MULTI_PATTERN_DETECTION_MODE_MATRIX ) {
arSetPatternDetectionMode( arHandle, AR_MATRIX_CODE_DETECTION );
} else { // AR_MULTI_PATTERN_DETECTION_MODE_TEMPLATE_AND_MATRIX
arSetPatternDetectionMode( arHandle, AR_TEMPLATE_MATCHING_COLOR_AND_MATRIX );
}
return (TRUE);
}
int addMarker(int id, std::string patt_name) {
if (arControllers.find(id) == arControllers.end()) { return -1; }
arController *arc = &(arControllers[id]);
// const char *patt_name
// Load marker(s).
if (!loadMarker(patt_name.c_str(), &(arc->patt_id), arc->arhandle, &(arc->arPattHandle))) {
ARLOGe("ARToolKitJS(): Unable to set up AR marker.\n");
return -1;
}
return arc->patt_id;
}
int addNFTMarker(int id, std::string datasetPathname) {
if (arControllers.find(id) == arControllers.end()) { return -1; }
arController *arc = &(arControllers[id]);
// Load marker(s).
int patt_id = arc->surfaceSetCount;
if (!loadNFTMarker(arc, patt_id, datasetPathname.c_str())) {
ARLOGe("ARToolKitJS(): Unable to set up NFT marker.\n");
return -1;
}
arc->surfaceSetCount++;
return patt_id;
}
int addMultiMarker(int id, std::string patt_name) {
if (arControllers.find(id) == arControllers.end()) { return -1; }
arController *arc = &(arControllers[id]);
// const char *patt_name
// Load marker(s).
if (!loadMultiMarker(patt_name.c_str(), arc->arhandle, &(arc->arPattHandle), &(arc->arMultiMarkerHandle))) {
ARLOGe("ARToolKitJS(): Unable to set up AR multimarker.\n");
return -1;
}
int multiMarker_id = arc->multi_markers.size();
multi_marker marker = multi_marker();
marker.id = multiMarker_id;
marker.multiMarkerHandle = arc->arMultiMarkerHandle;
arc->multi_markers.push_back(marker);
return marker.id;
}
int getMultiMarkerNum(int id, int multiMarker_id) {
if (arControllers.find(id) == arControllers.end()) { return -1; }
arController *arc = &(arControllers[id]);
int mId = multiMarker_id;
if (mId < 0 || arc->multi_markers.size() <= mId) {
return -1;
}
return (arc->multi_markers[mId].multiMarkerHandle)->marker_num;
}
int getMultiMarkerCount(int id) {
if (arControllers.find(id) == arControllers.end()) { return -1; }
arController *arc = &(arControllers[id]);
return arc->multi_markers.size();
}
/**********************
* Setters and getters *
**********************/
void setProjectionNearPlane(int id, const ARdouble projectionNearPlane) {
if (arControllers.find(id) == arControllers.end()) { return; }
arController *arc = &(arControllers[id]);
arc->nearPlane = projectionNearPlane;
}
ARdouble getProjectionNearPlane(int id) {
if (arControllers.find(id) == arControllers.end()) { return -1; }
arController *arc = &(arControllers[id]);
return arc->nearPlane;
}
void setProjectionFarPlane(int id, const ARdouble projectionFarPlane) {
if (arControllers.find(id) == arControllers.end()) { return; }
arController *arc = &(arControllers[id]);
arc->farPlane = projectionFarPlane;
}
ARdouble getProjectionFarPlane(int id) {
if (arControllers.find(id) == arControllers.end()) { return -1; }
arController *arc = &(arControllers[id]);
return arc->farPlane;
}
void setPatternDetectionMode(int id, int mode) {
if (arControllers.find(id) == arControllers.end()) { return; }
arController *arc = &(arControllers[id]);
if (arSetPatternDetectionMode(arc->arhandle, mode) == 0) {
ARLOGi("Pattern detection mode set to %d.\n", mode);
}
}
int getPatternDetectionMode(int id) {
if (arControllers.find(id) == arControllers.end()) { return -1; }
int mode;
arController *arc = &(arControllers[id]);
if (arGetPatternDetectionMode(arc->arhandle, &mode) == 0) {
return mode;
}
return -1;
}
void setPattRatio(int id, float ratio) {
if (arControllers.find(id) == arControllers.end()) { return; }
arController *arc = &(arControllers[id]);
if (ratio <= 0.0f || ratio >= 1.0f) return;
ARdouble pattRatio = (ARdouble)ratio;
if (arc->arhandle) {
if (arSetPattRatio(arc->arhandle, pattRatio) == 0) {
ARLOGi("Pattern ratio size set to %f.\n", pattRatio);
}
}
}
ARdouble getPattRatio(int id) {
if (arControllers.find(id) == arControllers.end()) { return -1; }
arController *arc = &(arControllers[id]);
ARdouble pattRatio;
if (arc->arhandle) {
if (arGetPattRatio(arc->arhandle, &pattRatio) == 0) {
return pattRatio;
}
}
return -1;
}
void setMatrixCodeType(int id, int type) {
if (arControllers.find(id) == arControllers.end()) { return; }
arController *arc = &(arControllers[id]);
AR_MATRIX_CODE_TYPE matrixType = (AR_MATRIX_CODE_TYPE)type;
arSetMatrixCodeType(arc->arhandle, matrixType);
}
int getMatrixCodeType(int id) {
if (arControllers.find(id) == arControllers.end()) { return -1; }
arController *arc = &(arControllers[id]);
AR_MATRIX_CODE_TYPE matrixType;
arGetMatrixCodeType(arc->arhandle, &matrixType);
return matrixType;
}
void setLabelingMode(int id, int mode) {
if (arControllers.find(id) == arControllers.end()) { return; }
arController *arc = &(arControllers[id]);
int labelingMode = mode;
if (arSetLabelingMode(arc->arhandle, labelingMode) == 0) {
ARLOGi("Labeling mode set to %d\n", labelingMode);
}
}
int getLabelingMode(int id) {
if (arControllers.find(id) == arControllers.end()) { return -1; }
arController *arc = &(arControllers[id]);
int labelingMode;
if (arGetLabelingMode(arc->arhandle, &labelingMode) == 0) {
return labelingMode;
}
return -1;
}
void setThreshold(int id, int threshold) {
if (arControllers.find(id) == arControllers.end()) { return; }
arController *arc = &(arControllers[id]);
if (threshold < 0 || threshold > 255) return;
if (arSetLabelingThresh(arc->arhandle, threshold) == 0) {
ARLOGi("Threshold set to %d\n", threshold);
};
// default 100
// arSetLabelingThreshMode
// AR_LABELING_THRESH_MODE_MANUAL, AR_LABELING_THRESH_MODE_AUTO_MEDIAN, AR_LABELING_THRESH_MODE_AUTO_OTSU, AR_LABELING_THRESH_MODE_AUTO_ADAPTIVE
}
int getThreshold(int id) {
if (arControllers.find(id) == arControllers.end()) { return -1; }
arController *arc = &(arControllers[id]);
int threshold;
if (arGetLabelingThresh(arc->arhandle, &threshold) == 0) {
return threshold;
};
return -1;
}
void setThresholdMode(int id, int mode) {
if (arControllers.find(id) == arControllers.end()) { return; }
arController *arc = &(arControllers[id]);
AR_LABELING_THRESH_MODE thresholdMode = (AR_LABELING_THRESH_MODE)mode;
if (arSetLabelingThreshMode(arc->arhandle, thresholdMode) == 0) {
ARLOGi("Threshold mode set to %d\n", (int)thresholdMode);
}
}
int getThresholdMode(int id) {
if (arControllers.find(id) == arControllers.end()) { return -1; }
arController *arc = &(arControllers[id]);
AR_LABELING_THRESH_MODE thresholdMode;
if (arGetLabelingThreshMode(arc->arhandle, &thresholdMode) == 0) {
return thresholdMode;
}
return -1;
}
int setDebugMode(int id, int enable) {
if (arControllers.find(id) == arControllers.end()) { return NULL; }
arController *arc = &(arControllers[id]);
arSetDebugMode(arc->arhandle, enable ? AR_DEBUG_ENABLE : AR_DEBUG_DISABLE);
ARLOGi("Debug mode set to %s\n", enable ? "on." : "off.");
return enable;
}
int getProcessingImage(int id) {
if (arControllers.find(id) == arControllers.end()) { return NULL; }
arController *arc = &(arControllers[id]);
return (int)arc->arhandle->labelInfo.bwImage;
}
int getDebugMode(int id) {
if (arControllers.find(id) == arControllers.end()) { return NULL; }
arController *arc = &(arControllers[id]);
int enable;
arGetDebugMode(arc->arhandle, &enable);
return enable;
}
void setImageProcMode(int id, int mode) {
if (arControllers.find(id) == arControllers.end()) { return; }
arController *arc = &(arControllers[id]);
int imageProcMode = mode;
if (arSetImageProcMode(arc->arhandle, mode) == 0) {
ARLOGi("Image proc. mode set to %d.\n", imageProcMode);
}
}
int getImageProcMode(int id) {
if (arControllers.find(id) == arControllers.end()) { return -1; }
arController *arc = &(arControllers[id]);
int imageProcMode;
if (arGetImageProcMode(arc->arhandle, &imageProcMode) == 0) {
return imageProcMode;
}
return -1;
}
/*
* Marker processing
*/
void matrixCopy(ARdouble src[3][4], ARdouble dst[3][4]) {
for (int i=0; i<3; i++) {
for (int j=0; j<4; j++) {
dst[i][j] = src[i][j];
}
}
}
int getTransMatSquare(int id, int markerIndex, int markerWidth) {
if (arControllers.find(id) == arControllers.end()) { return ARCONTROLLER_NOT_FOUND; }
arController *arc = &(arControllers[id]);
if (arc->arhandle->marker_num <= markerIndex) {
return MARKER_INDEX_OUT_OF_BOUNDS;
}
ARMarkerInfo* marker = markerIndex < 0 ? &gMarkerInfo : &((arc->arhandle)->markerInfo[markerIndex]);
arGetTransMatSquare(arc->ar3DHandle, marker, markerWidth, gTransform);
return 0;
}
int getTransMatSquareCont(int id, int markerIndex, int markerWidth) {
if (arControllers.find(id) == arControllers.end()) { return ARCONTROLLER_NOT_FOUND; }
arController *arc = &(arControllers[id]);
if (arc->arhandle->marker_num <= markerIndex) {
return MARKER_INDEX_OUT_OF_BOUNDS;
}
ARMarkerInfo* marker = markerIndex < 0 ? &gMarkerInfo : &((arc->arhandle)->markerInfo[markerIndex]);
arGetTransMatSquareCont(arc->ar3DHandle, marker, gTransform, markerWidth, gTransform);
return 0;
}
int setMarkerInfoDir(int id, int markerIndex, int dir) {
if (arControllers.find(id) == arControllers.end()) { return ARCONTROLLER_NOT_FOUND; }
arController *arc = &(arControllers[id]);
if (arc->arhandle->marker_num <= markerIndex) {
return MARKER_INDEX_OUT_OF_BOUNDS;
}
ARMarkerInfo* marker = markerIndex < 0 ? &gMarkerInfo : &((arc->arhandle)->markerInfo[markerIndex]);
marker->dir = dir;
return 0;
}
int setMarkerInfoVertex(int id, int markerIndex) {
if (arControllers.find(id) == arControllers.end()) { return ARCONTROLLER_NOT_FOUND; }
arController *arc = &(arControllers[id]);
if (arc->arhandle->marker_num <= markerIndex) {
return MARKER_INDEX_OUT_OF_BOUNDS;
}
ARMarkerInfo* marker = markerIndex < 0 ? &gMarkerInfo : &((arc->arhandle)->markerInfo[markerIndex]);
auto v = marker->vertex;
v[0][0] = gTransform[0][0];
v[0][1] = gTransform[0][1];
v[1][0] = gTransform[0][2];
v[1][1] = gTransform[0][3];
v[2][0] = gTransform[1][0];
v[2][1] = gTransform[1][1];
v[3][0] = gTransform[1][2];
v[3][1] = gTransform[1][3];
marker->pos[0] = (v[0][0] + v[1][0] + v[2][0] + v[3][0]) * 0.25;
marker->pos[1] = (v[0][1] + v[1][1] + v[2][1] + v[3][1]) * 0.25;
return 0;
}
int getTransMatMultiSquareRobust(int id, int multiMarkerId) {
if (arControllers.find(id) == arControllers.end()) { return ARCONTROLLER_NOT_FOUND; }
arController *arc = &(arControllers[id]);
if (arc->multi_markers.size() <= multiMarkerId || multiMarkerId < 0) {
return MULTIMARKER_NOT_FOUND;
}
multi_marker *multiMatch = &(arc->multi_markers[multiMarkerId]);
ARMultiMarkerInfoT *arMulti = multiMatch->multiMarkerHandle;
arGetTransMatMultiSquareRobust( arc->ar3DHandle, arc->arhandle->markerInfo, arc->arhandle->marker_num, arMulti );
matrixCopy(arMulti->trans, gTransform);
return 0;
}
int getTransMatMultiSquare(int id, int multiMarkerId) {
if (arControllers.find(id) == arControllers.end()) { return ARCONTROLLER_NOT_FOUND; }
arController *arc = &(arControllers[id]);
if (arc->multi_markers.size() <= multiMarkerId || multiMarkerId < 0) {
return MULTIMARKER_NOT_FOUND;
}
multi_marker *multiMatch = &(arc->multi_markers[multiMarkerId]);
ARMultiMarkerInfoT *arMulti = multiMatch->multiMarkerHandle;
arGetTransMatMultiSquare( arc->ar3DHandle, arc->arhandle->markerInfo, arc->arhandle->marker_num, arMulti );
matrixCopy(arMulti->trans, gTransform);
return 0;
}
int detectMarker(int id) {
if (arControllers.find(id) == arControllers.end()) { return ARCONTROLLER_NOT_FOUND; }
arController *arc = &(arControllers[id]);
// Convert video frame to AR2VideoBufferT
AR2VideoBufferT buff = {0};
buff.buff = arc->videoFrame;
buff.fillFlag = 1;
buff.buffLuma = arc->videoLuma;
return arDetectMarker( arc->arhandle, &buff);
}
int getMarkerNum(int id) {
if (arControllers.find(id) == arControllers.end()) { return ARCONTROLLER_NOT_FOUND; }
arController *arc = &(arControllers[id]);
return arc->arhandle->marker_num;
}
int getMultiEachMarkerInfo(int id, int multiMarkerId, int markerIndex) {
if (arControllers.find(id) == arControllers.end()) { return ARCONTROLLER_NOT_FOUND; }
arController *arc = &(arControllers[id]);
if (arc->multi_markers.size() <= multiMarkerId || multiMarkerId < 0) {
return MULTIMARKER_NOT_FOUND;
}
multi_marker *multiMatch = &(arc->multi_markers[multiMarkerId]);
ARMultiMarkerInfoT *arMulti = multiMatch->multiMarkerHandle;
if (arMulti->marker_num <= markerIndex || markerIndex < 0) {
return MARKER_INDEX_OUT_OF_BOUNDS;
}
ARMultiEachMarkerInfoT *marker = &(arMulti->marker[markerIndex]);
matrixCopy(marker->trans, gTransform);
EM_ASM_({
if (!artoolkit["multiEachMarkerInfo"]) {
artoolkit["multiEachMarkerInfo"] = ({});
}
var multiEachMarker = artoolkit["multiEachMarkerInfo"];
multiEachMarker['visible'] = $0;
multiEachMarker['pattId'] = $1;
multiEachMarker['pattType'] = $2;
multiEachMarker['width'] = $3;
},
marker->visible,
marker->patt_id,
marker->patt_type,
marker->width
);
return 0;
}
int getMarkerInfo(int id, int markerIndex) {
if (arControllers.find(id) == arControllers.end()) { return ARCONTROLLER_NOT_FOUND; }
arController *arc = &(arControllers[id]);
if (arc->arhandle->marker_num <= markerIndex) {
return MARKER_INDEX_OUT_OF_BOUNDS;
}
ARMarkerInfo* markerInfo = markerIndex < 0 ? &gMarkerInfo : &((arc->arhandle)->markerInfo[markerIndex]);
EM_ASM_({
var $a = arguments;
var i = 12;
if (!artoolkit["markerInfo"]) {
artoolkit["markerInfo"] = ({
pos: [0,0],
line: [[0,0,0], [0,0,0], [0,0,0], [0,0,0]],
vertex: [[0,0], [0,0], [0,0], [0,0]]
});
}
var markerInfo = artoolkit["markerInfo"];
markerInfo["area"] = $0;
markerInfo["id"] = $1;
markerInfo["idPatt"] = $2;
markerInfo["idMatrix"] = $3;
markerInfo["dir"] = $4;
markerInfo["dirPatt"] = $5;
markerInfo["dirMatrix"] = $6;
markerInfo["cf"] = $7;
markerInfo["cfPatt"] = $8;