@@ -45,6 +45,7 @@ struct arController {
4545 KpmHandle* kpmHandle;
4646 AR2HandleT* ar2Handle;
4747 THREAD_HANDLE_T *threadHandle = NULL ;
48+ int detectedPage = -2 ; // -2 Tracking not inited, -1 tracking inited OK, >= 0 tracking online on page.
4849
4950 int surfaceSetCount = 0 ; // Running NFT marker id
5051 std::unordered_map<int , AR2SurfaceSetT*> surfaceSets;
@@ -97,23 +98,41 @@ extern "C" {
9798 KpmResult *kpmResult = NULL ;
9899 int kpmResultNum = -1 ;
99100 int pageNo;
100-
101- // kpmGetResult( arc->kpmHandle, &kpmResult, &kpmResultNum );
102-
103-
104101 int i, j, k;
105102 int flag = -1 ;
106- float err = -1 ;
107- float trans[3 ][4 ];
108- float trackingTrans[3 ][4 ];
109-
110- kpmResultNum = trackingInitGetResult ( arc->threadHandle , trackingTrans, &pageNo);
111- ar2SetInitTrans (arc->surfaceSet [pageNo], trackingTrans);
112- if ( ar2Tracking (arc->ar2Handle , arc->surfaceSet [pageNo], arc->videoLuma , trackingTrans, &err) < 0 ) {
103+ float err = -1 ;
104+ float trans[3 ][4 ];
105+ float trackingTrans[3 ][4 ] = {0.0 ,0.0 ,0.0 ,0.0 ,0.0 ,0.0 ,0.0 ,0.0 ,0.0 ,0.0 ,0.0 ,0.0 };
106+ if (arc->threadHandle ) {
107+ int ret;
108+ if ( arc->detectedPage == -1 ) {
109+ ret = trackingInitGetResult ( arc->threadHandle , trackingTrans, &pageNo);
110+ if ( ret == 1 ) {
111+ if (pageNo >= 0 && pageNo < arc->surfaceSetCount ) {
112+ ARLOGd (" Detected page %d.\n " , pageNo);
113+ arc->detectedPage = pageNo;
114+ ar2SetInitTrans (arc->surfaceSet [pageNo], trackingTrans);
115+ } else {
116+ ARLOGe (" Detected bad page %d.\n " , pageNo);
117+ arc->detectedPage = -2 ;
118+ }
119+ } else if ( ret < 0 ) {
120+ ARLOGd (" No page detected.\n " );
121+ arc->detectedPage = -2 ;
122+ }
123+ }
124+ if ( arc->detectedPage >= 0 && arc->detectedPage < arc->surfaceSetCount ) {
125+ if ( ar2Tracking (arc->ar2Handle , arc->surfaceSet [pageNo], arc->videoLuma , trackingTrans, &err) < 0 ) {
113126 ARLOGd (" Tracking lost.\n " );
127+ arc->detectedPage = -2 ;
128+ } else {
129+ ARLOGd (" Tracked page %d (max %d).\n " , arc->detectedPage , arc->surfaceSetCount - 1 );
114130 }
115- // ar2SetInitTrans(surfaceSet[detectedPage], trackingTrans);
116- ARLOGi (" kpmResultNum is: %d\n " , kpmResultNum);
131+ }
132+ } else {
133+ ARLOGe (" Error: threadHandle\n " );
134+ arc->detectedPage = -2 ;
135+ }
117136
118137 for ( i = 0 ; i < kpmResultNum; i++ ) {
119138 // if (kpmResult[i].pageNo == markerIndex && kpmResult[i].camPoseF == 0 ) {
@@ -122,19 +141,19 @@ extern "C" {
122141 if ( flag == -1 ) { // Take the first or best result.
123142 flag = i;
124143 err = kpmResult[i].error ;
125- ARLOGe (" error in the tracking" );
144+ ARLOGe (" error in the tracking: %d \n " , err );
126145 }
127146 }
128147 }
129- flag = kpmResultNum;
130- ARLOGi (" flag is: %d\n " , flag );
131- if (flag > - 1 ) {
148+
149+ // ARLOGi("flag is: %d\n", arc->detectedPage );
150+ if (pageNo >= 0 && pageNo == arc-> detectedPage ) {
132151 for (j = 0 ; j < 3 ; j++) {
133152 for (k = 0 ; k < 4 ; k++) {
134153 trans[j][k] = trackingTrans[j][k];
135154 }
136155 }
137- // ARLOGi("trackingTrans %d\n", trans);
156+
138157 EM_ASM_ ({
139158 var $a = arguments;
140159 var i = 0 ;
@@ -231,7 +250,6 @@ extern "C" {
231250
232251 KpmResult *kpmResult = NULL ;
233252 // NFT results.
234- int detectedPage = -2 ; // -2 Tracking not inited, -1 tracking inited OK, >= 0 tracking online on page.
235253 float trackingTrans[3 ][4 ];
236254 int kpmResultNum = -1 ;
237255
@@ -243,12 +261,9 @@ extern "C" {
243261 int ret;
244262 int pageNo;
245263
246- if ( detectedPage == -2 ) {
264+ if ( arc-> detectedPage == -2 ) {
247265 trackingInitStart ( arc->threadHandle , arc->videoLuma );
248- detectedPage = -1 ;
249- }
250- if ( detectedPage == -1 ) {
251- kpmResultNum = trackingInitGetResult ( arc->threadHandle , trackingTrans, &pageNo);
266+ arc->detectedPage = -1 ;
252267 }
253268 }
254269 return kpmResultNum;
@@ -281,7 +296,7 @@ extern "C" {
281296 // AR2 init.
282297 if ( (arc->ar2Handle = ar2CreateHandle (arc->paramLT , arc->pixFormat , AR2_TRACKING_DEFAULT_THREAD_NUM)) == NULL ) {
283298 ARLOGe (" Error: ar2CreateHandle.\n " );
284- // kpmDeleteHandle(arc->kpmHandle);
299+ kpmDeleteHandle (& arc->kpmHandle );
285300 return (FALSE );
286301 }
287302 if (threadGetCPU () <= 1 ) {
0 commit comments