This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
flow.cpp
317 lines (251 loc) · 8.05 KB
/
flow.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
/*
* flow.cpp
* ARToolKit6
*
* Copyright 2015-2017 Daqri LLC. All Rights Reserved.
* Copyright 2013-2015 ARToolworks, Inc. All Rights Reserved.
*
* Author(s): Philip Lamb
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE // asprintf()
#endif
#include "flow.hpp"
#include <stdio.h> // asprintf()
#include <pthread.h>
#include <Eden/EdenMessage.h>
#include <AR6/AR/ar.h>
//
// Globals.
//
static bool gInited = false;
static FLOW_STATE gState = FLOW_STATE_NOT_INITED;
static pthread_mutex_t gStateLock;
static pthread_mutex_t gEventLock;
static pthread_cond_t gEventCond;
static EVENT_t gEvent = EVENT_NONE;
static EVENT_t gEventMask = EVENT_NONE;
static pthread_t gThread;
static int gThreadExitStatus;
static bool gStop;
// Completion callback.
static FLOW_CALLBACK_t gCallback = NULL;
static void *gCallbackUserdata = NULL;
// Logging macros
#define LOG_TAG "flow"
// Status bar.
#define STATUS_BAR_MESSAGE_BUFFER_LEN 128
unsigned char statusBarMessage[STATUS_BAR_MESSAGE_BUFFER_LEN] = "";
// Calibration inputs.
static Calibration *gFlowCalib = nullptr;
//
// Function prototypes.
//
static void *flowThread(void *arg);
static void flowSetEventMask(const EVENT_t eventMask);
//
// Functions.
//
bool flowInitAndStart(Calibration *calib, FLOW_CALLBACK_t callback, void *callback_userdata)
{
pthread_mutex_init(&gStateLock, NULL);
pthread_mutex_init(&gEventLock, NULL);
pthread_cond_init(&gEventCond, NULL);
// Calibration inputs.
gFlowCalib = calib;
// Completion callback.
gCallback = callback;
gCallbackUserdata = callback_userdata;
gStop = false;
pthread_create(&gThread, NULL, flowThread, NULL);
gInited = true;
return (true);
}
bool flowStopAndFinal()
{
void *exit_status_p; // Pointer to return value from thread, will be filled in by pthread_join().
if (!gInited) return (false);
// Request stop and wait for join.
gStop = true;
#ifndef ANDROID
pthread_cancel(gThread); // Not implemented on Android.
#endif
#ifdef DEBUG
ARLOGi("flowStopAndFinal(): Waiting for flowThread() to exit...\n");
#endif
pthread_join(gThread, &exit_status_p);
#ifdef DEBUG
# ifndef ANDROID
ARLOGi(" done. Exit status was %d.\n",((exit_status_p == PTHREAD_CANCELED) ? 0 : *(int *)(exit_status_p))); // Contents of gThreadExitStatus.
# else
ARLOGi(" done. Exit status was %d.\n", *(int *)(exit_status_p)); // Contents of gThreadExitStatus.
# endif
#endif
gFlowCalib = nullptr;
// Clean up.
pthread_mutex_destroy(&gStateLock);
pthread_mutex_destroy(&gEventLock);
pthread_cond_destroy(&gEventCond);
gState = FLOW_STATE_NOT_INITED;
gInited = false;
return true;
}
FLOW_STATE flowStateGet()
{
FLOW_STATE ret;
if (!gInited) return (FLOW_STATE_NOT_INITED);
pthread_mutex_lock(&gStateLock);
ret = gState;
pthread_mutex_unlock(&gStateLock);
return (ret);
}
static void flowStateSet(FLOW_STATE state)
{
if (!gInited) return;
pthread_mutex_lock(&gStateLock);
gState = state;
pthread_mutex_unlock(&gStateLock);
}
static void flowSetEventMask(const EVENT_t eventMask)
{
pthread_mutex_lock(&gEventLock);
gEventMask = eventMask;
pthread_mutex_unlock(&gEventLock);
}
bool flowHandleEvent(const EVENT_t event)
{
bool ret;
if (!gInited) return false;
pthread_mutex_lock(&gEventLock);
if ((event & gEventMask) == EVENT_NONE) {
ret = false; // not handled (discarded).
} else {
gEvent = event;
pthread_cond_signal(&gEventCond);
ret = true;
}
pthread_mutex_unlock(&gEventLock);
return (ret);
}
static EVENT_t flowWaitForEvent(void)
{
EVENT_t ret;
pthread_mutex_lock(&gEventLock);
while (gEvent == EVENT_NONE && !gStop) {
#ifdef ANDROID
// Android "Bionic" libc doesn't implement cancelation, so need to let wait expire somewhat regularly.
const struct timespec twoSeconds = {2, 0};
pthread_cond_timedwait_relative_np(&gEventCond, &gEventLock, &twoSeconds);
#else
pthread_cond_wait(&gEventCond, &gEventLock);
#endif
}
ret = gEvent;
gEvent = EVENT_NONE; // Clear wait state.
pthread_mutex_unlock(&gEventLock);
return (ret);
}
static void flowThreadCleanup(void *arg)
{
pthread_mutex_unlock(&gStateLock);
// Clear status bar.
statusBarMessage[0] = '\0';
}
static void *flowThread(void *arg)
{
bool captureDoneSinceBackButtonLastPressed;
EVENT_t event;
// TYPE* TYPE_INSTANCE = (TYPE *)arg; // Cast the thread start arg to the correct type.
ARLOGi("Start flow thread.\n");
// Register our cleanup function, with no arg.
pthread_cleanup_push(flowThreadCleanup, NULL);
// Welcome.
flowStateSet(FLOW_STATE_WELCOME);
while (!gStop) {
if (flowStateGet() == FLOW_STATE_WELCOME) {
EdenMessageShow((const unsigned char *)"Welcome to ARToolKit Camera Calibrator\n(c)2017 DAQRI LLC.\n\nPress 'space' to begin a calibration run.\n\nPress 'p' for settings and help.");
} else {
EdenMessageShow((const unsigned char *)"Press 'space' to begin a calibration run.\n\nPress 'p' for settings and help.");
}
flowSetEventMask((EVENT_t)(EVENT_TOUCH | EVENT_MODAL));
event = flowWaitForEvent();
if (gStop) break;
if (event == EVENT_MODAL) {
flowSetEventMask(EVENT_MODAL);
event = flowWaitForEvent();
continue;
} else {
EdenMessageHide();
}
// Start capturing.
captureDoneSinceBackButtonLastPressed = false;
flowStateSet(FLOW_STATE_CAPTURING);
flowSetEventMask((EVENT_t)(EVENT_TOUCH|EVENT_BACK_BUTTON));
do {
snprintf((char *)statusBarMessage, STATUS_BAR_MESSAGE_BUFFER_LEN, "Capturing image %d/%d", gFlowCalib->calibImageCount() + 1, gFlowCalib->calibImageCountMax());
event = flowWaitForEvent();
if (gStop) break;
if (event == EVENT_TOUCH) {
if (gFlowCalib->capture()) {
captureDoneSinceBackButtonLastPressed = true;
}
} else if (event == EVENT_BACK_BUTTON) {
if (!captureDoneSinceBackButtonLastPressed) {
gFlowCalib->uncaptureAll();
break;
} else {
gFlowCalib->uncapture();
}
captureDoneSinceBackButtonLastPressed = false;
}
} while (gFlowCalib->calibImageCount() < gFlowCalib->calibImageCountMax());
// Clear status bar.
statusBarMessage[0] = '\0';
if (gFlowCalib->calibImageCount() < gFlowCalib->calibImageCountMax()) {
flowSetEventMask(EVENT_TOUCH);
flowStateSet(FLOW_STATE_DONE);
EdenMessageShow((const unsigned char *)"Calibration canceled");
flowWaitForEvent();
if (gStop) break;
EdenMessageHide();
} else {
ARParam param;
ARdouble err_min, err_avg, err_max;
flowSetEventMask(EVENT_NONE);
flowStateSet(FLOW_STATE_CALIBRATING);
EdenMessageShow((const unsigned char *)"Calculating camera parameters...");
gFlowCalib->calib(¶m, &err_min, &err_avg, &err_max);
EdenMessageHide();
if (gCallback) (*gCallback)(¶m, err_min, err_avg, err_max, gCallbackUserdata);
gFlowCalib->uncaptureAll(); // prepare for next run.
// Calibration complete. Post results as status.
flowSetEventMask(EVENT_TOUCH);
flowStateSet(FLOW_STATE_DONE);
unsigned char *buf;
asprintf((char **)&buf, "Camera parameters calculated (error min=%.3f, avg=%.3f, max=%.3f)", err_min, err_avg, err_max);
EdenMessageShow(buf);
free(buf);
flowWaitForEvent();
if (gStop) break;
EdenMessageHide();
}
//pthread_testcancel(); // Not implemented on Android.
} // while (!gStop);
pthread_cleanup_pop(1); // Unlocks gStateLock.
ARLOGi("End flow thread.\n");
gThreadExitStatus = 1; // Put the exit status into a global
return (&gThreadExitStatus); // Pass a pointer to the global as our exit status.
}