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
/
calib_camera.cpp
1143 lines (994 loc) · 44.1 KB
/
calib_camera.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
/*
* calib_camera.cpp
* ARToolKit6
*
* Camera calibration utility.
*
* Run with "--help" parameter to see usage.
*
* This file is part of ARToolKit.
*
* Copyright 2015-2016 Daqri, LLC.
* Copyright 2002-2015 ARToolworks, Inc.
*
* Author(s): Hirokazu Kato, 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.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#ifdef _WIN32
# include <windows.h>
# define MAXPATHLEN MAX_PATH
# include <direct.h> // getcwd
#else
# include <sys/param.h> // MAXPATHLEN
# include <unistd.h> // getcwd
#endif
#ifdef __APPLE__
# include <OpenGL/gl.h>
#elif defined(__linux) || defined(_WIN32)
# include <GL/gl.h>
#endif
#include <AR6/AR/ar.h>
//#include <AR6/ARVideo/video.h>
#include <AR6/ARVideoSource.h>
#include <AR6/ARView.h>
#include <AR6/ARUtil/system.h>
#include <AR6/ARUtil/thread_sub.h>
#include <AR6/ARUtil/time.h>
#include <AR6/ARUtil/file_utils.h>
#include <AR6/ARG/arg.h>
#include "fileUploader.h"
#include "Calibration.hpp"
#include "flow.hpp"
#include "Eden/EdenMessage.h"
#include "Eden/EdenGLFont.h"
#include "prefs.hpp"
#include "calib_camera.h"
// ============================================================================
// Types
// ============================================================================
// ============================================================================
// Constants
// ============================================================================
#define CHESSBOARD_CORNER_NUM_X 7
#define CHESSBOARD_CORNER_NUM_Y 5
#define CHESSBOARD_PATTERN_WIDTH 30.0
#define CALIB_IMAGE_NUM 10
#define SAVE_FILENAME "camera_para.dat"
// Data upload.
#define QUEUE_DIR "queue"
#define QUEUE_INDEX_FILE_EXTENSION "upload"
#ifdef __APPLE__
# include <CommonCrypto/CommonDigest.h>
# define MD5 CC_MD5
# define MD5_DIGEST_LENGTH CC_MD5_DIGEST_LENGTH
# define MD5_COUNT_t CC_LONG
#else
//#include <openssl/md5.h>
// Rather than including full OpenSSL header tree, just provide prototype for MD5().
// Usage is here: https://www.openssl.org/docs/manmaster/man3/MD5.html .
# define MD5_DIGEST_LENGTH 16
# define MD5_COUNT_t size_t
# ifdef __cplusplus
extern "C" {
# endif
unsigned char *MD5(const unsigned char *d, size_t n, unsigned char *md);
# ifdef __cplusplus
}
# endif
#endif
#define FONT_SIZE 18.0f
#define UPLOAD_STATUS_HIDE_AFTER_SECONDS 9.0f
// ============================================================================
// Global variables.
// ============================================================================
// Prefs.
static void *gPreferences = NULL;
Uint32 gSDLEventPreferencesChanged = 0;
static char *gPreferenceCameraOpenToken = NULL;
static char *gPreferenceCameraResolutionToken = NULL;
static bool gCalibrationSave = false;
static char *gCalibrationSaveDir = NULL;
static char *gCalibrationServerUploadURL = NULL;
static char *gCalibrationServerAuthenticationToken = NULL;
static int gPreferencesCalibImageCountMax = CALIB_IMAGE_NUM;
static Calibration::CalibrationPatternType gCalibrationPatternType;
static cv::Size gCalibrationPatternSize;
static float gCalibrationPatternSpacing;
//
// Calibration.
//
static Calibration *gCalibration = nullptr;
//
// Data upload.
//
static char *gFileUploadQueuePath = NULL;
FILE_UPLOAD_HANDLE_t *fileUploadHandle = NULL;
// Video acquisition and rendering.
static ARVideoSource *vs = nullptr;
static ARView *vv = nullptr;
static bool gPostVideoSetupDone = false;
static bool gCameraIsFrontFacing = false;
static long gFrameCount = 0;
// Window and GL context.
static SDL_GLContext gSDLContext = NULL;
static int contextWidth = 0;
static int contextHeight = 0;
static bool contextWasUpdated = false;
static SDL_Window* gSDLWindow = NULL;
static int32_t gViewport[4] = {0, 0, 0, 0}; // {x, y, width, height}
static int gDisplayOrientation = 1; // range [0-3]. 1=landscape.
static float gDisplayDPI = 72.0f;
// Main state.
static struct timeval gStartTime;
// Corner finder results copy, for display to user.
static ARGL_CONTEXT_SETTINGS_REF gArglSettingsCornerFinderImage = NULL;
// ============================================================================
// Function prototypes
// ============================================================================
static void quit(int rc);
static void reshape(int w, int h);
static void drawView(void);
//static void init(int argc, char *argv[]);
//static void usage(char *com);
static void saveParam(const ARParam *param, ARdouble err_min, ARdouble err_avg, ARdouble err_max, void *userdata);
static void startVideo(void)
{
char buf[256];
snprintf(buf, sizeof(buf), "%s %s", (gPreferenceCameraOpenToken ? gPreferenceCameraOpenToken : ""), (gPreferenceCameraResolutionToken ? gPreferenceCameraResolutionToken : ""));
vs = new ARVideoSource;
if (!vs) {
ARLOGe("Error: Unable to create video source.\n");
quit(-1);
} else {
vs->configure(buf, true, NULL, NULL, 0);
if (!vs->open()) {
ARLOGe("Error: Unable to open video source.\n");
EdenMessageShow((const unsigned char *)"Welcome to ARToolKit Camera Calibrator\n(c)2017 DAQRI LLC.\n\nUnable to open video source.\n\nPress 'p' for settings and help.");
}
}
gPostVideoSetupDone = false;
}
static void stopVideo(void)
{
// Stop calibration flow.
flowStopAndFinal();
if (gCalibration) {
delete gCalibration;
gCalibration = nullptr;
}
if (gArglSettingsCornerFinderImage) {
arglCleanup(gArglSettingsCornerFinderImage); // Clean up any left-over ARGL data.
gArglSettingsCornerFinderImage = NULL;
}
delete vv;
vv = nullptr;
delete vs;
vs = nullptr;
}
static void rereadPreferences(void)
{
// Re-read preferences.
gCalibrationSave = getPreferenceCalibrationSave(gPreferences);
char *csd = getPreferenceCalibSaveDir(gPreferences);
if (csd && gCalibrationSaveDir && strcmp(gCalibrationSaveDir, csd) == 0) {
free(csd);
} else {
free(gCalibrationSaveDir);
gCalibrationSaveDir = csd;
}
char *csuu = getPreferenceCalibrationServerUploadURL(gPreferences);
if (csuu && gCalibrationServerUploadURL && strcmp(gCalibrationServerUploadURL, csuu) == 0) {
free(csuu);
} else {
free(gCalibrationServerUploadURL);
gCalibrationServerUploadURL = csuu;
fileUploaderFinal(&fileUploadHandle);
if (csuu) {
fileUploadHandle = fileUploaderInit(gFileUploadQueuePath, QUEUE_INDEX_FILE_EXTENSION, gCalibrationServerUploadURL, UPLOAD_STATUS_HIDE_AFTER_SECONDS);
if (!fileUploadHandle) {
ARLOGe("Error: Could not initialise fileUploadHandle.\n");
}
}
}
char *csat = getPreferenceCalibrationServerAuthenticationToken(gPreferences);
if (csat && gCalibrationServerAuthenticationToken && strcmp(gCalibrationServerAuthenticationToken, csat) == 0) {
free(csat);
} else {
free(gCalibrationServerAuthenticationToken);
gCalibrationServerAuthenticationToken = csat;
}
bool changedCameraSettings = false;
char *crt = getPreferenceCameraResolutionToken(gPreferences);
if (crt && gPreferenceCameraResolutionToken && strcmp(gPreferenceCameraResolutionToken, crt) == 0) {
free(crt);
} else {
free(gPreferenceCameraResolutionToken);
gPreferenceCameraResolutionToken = crt;
changedCameraSettings = true;
}
char *cot = getPreferenceCameraOpenToken(gPreferences);
if (cot && gPreferenceCameraOpenToken && strcmp(gPreferenceCameraOpenToken, cot) == 0) {
free(cot);
} else {
free(gPreferenceCameraOpenToken);
gPreferenceCameraOpenToken = cot;
changedCameraSettings = true;
}
Calibration::CalibrationPatternType patternType = getPreferencesCalibrationPatternType(gPreferences);
cv::Size patternSize = getPreferencesCalibrationPatternSize(gPreferences);
float patternSpacing = getPreferencesCalibrationPatternSpacing(gPreferences);
if (patternType != gCalibrationPatternType || patternSize != gCalibrationPatternSize || patternSpacing != gCalibrationPatternSpacing) {
gCalibrationPatternType = patternType;
gCalibrationPatternSize = patternSize;
gCalibrationPatternSpacing = patternSpacing;
changedCameraSettings = true;
}
if (changedCameraSettings) {
// Changing camera settings requires complete cancelation of calibration flow,
// closing of video source, and re-init.
stopVideo();
startVideo();
}
}
int main(int argc, char *argv[])
{
#ifdef DEBUG
arLogLevel = AR_LOG_LEVEL_DEBUG;
#endif
// Initialize SDL.
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
ARLOGe("Error: SDL initialisation failed. SDL error: '%s'.\n", SDL_GetError());
return -1;
}
// Preferences.
gPreferences = initPreferences();
gPreferenceCameraOpenToken = getPreferenceCameraOpenToken(gPreferences);
gPreferenceCameraResolutionToken = getPreferenceCameraResolutionToken(gPreferences);
gCalibrationSave = getPreferenceCalibrationSave(gPreferences);
gCalibrationSaveDir = getPreferenceCalibSaveDir(gPreferences);
gCalibrationServerUploadURL = getPreferenceCalibrationServerUploadURL(gPreferences);
gCalibrationServerAuthenticationToken = getPreferenceCalibrationServerAuthenticationToken(gPreferences);
gCalibrationPatternType = getPreferencesCalibrationPatternType(gPreferences);
gCalibrationPatternSize = getPreferencesCalibrationPatternSize(gPreferences);
gCalibrationPatternSpacing = getPreferencesCalibrationPatternSpacing(gPreferences);
gSDLEventPreferencesChanged = SDL_RegisterEvents(1);
// Create a window.
gSDLWindow = SDL_CreateWindow("ARToolKit6 Camera Calibration Utility",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
1280, 720,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI
);
if (!gSDLWindow) {
ARLOGe("Error creating window: %s.\n", SDL_GetError());
quit(-1);
}
// Create an OpenGL context to draw into.
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // This is the default.
SDL_GL_SetSwapInterval(1);
gSDLContext = SDL_GL_CreateContext(gSDLWindow);
if (!gSDLContext) {
ARLOGe("Error creating OpenGL context: %s.\n", SDL_GetError());
return -1;
}
int w, h;
SDL_GL_GetDrawableSize(SDL_GL_GetCurrentWindow(), &w, &h);
reshape(w, h);
asprintf(&gFileUploadQueuePath, "%s/%s", arUtilGetResourcesDirectoryPath(AR_UTIL_RESOURCES_DIRECTORY_BEHAVIOR_USE_APP_CACHE_DIR), QUEUE_DIR);
// Check for QUEUE_DIR and create if not already existing.
if (!fileUploaderCreateQueueDir(gFileUploadQueuePath)) {
ARLOGe("Error: Could not create queue directory.\n");
exit(-1);
}
if (gCalibrationServerUploadURL) {
fileUploadHandle = fileUploaderInit(gFileUploadQueuePath, QUEUE_INDEX_FILE_EXTENSION, gCalibrationServerUploadURL, UPLOAD_STATUS_HIDE_AFTER_SECONDS);
if (!fileUploadHandle) {
ARLOGe("Error: Could not initialise fileUploadHandle.\n");
}
fileUploaderTickle(fileUploadHandle);
}
// Calibration prefs.
ARLOGi("Calbration pattern size X = %d\n", gCalibrationPatternSize.width);
ARLOGi("Calbration pattern size Y = %d\n", gCalibrationPatternSize.height);
ARLOGi("Calbration pattern spacing = %f\n", gCalibrationPatternSpacing);
ARLOGi("Calibration image count maximum = %d\n", gPreferencesCalibImageCountMax);
// Library setup.
int contextsActiveCount = 1;
EdenMessageInit(contextsActiveCount);
EdenGLFontInit(contextsActiveCount);
EdenGLFontSetFont(EDEN_GL_FONT_ID_Stroke_Roman);
EdenGLFontSetSize(FONT_SIZE);
// Get start time.
gettimeofday(&gStartTime, NULL);
startVideo();
// Main loop.
bool done = false;
while (!done) {
SDL_Event ev;
while (SDL_PollEvent(&ev)) {
if (ev.type == SDL_QUIT /*|| (ev.type == SDL_KEYDOWN && ev.key.keysym.sym == SDLK_ESCAPE)*/) {
done = true;
break;
} else if (ev.type == SDL_WINDOWEVENT) {
//ARLOGd("Window event %d.\n", ev.window.event);
if (ev.window.event == SDL_WINDOWEVENT_RESIZED && ev.window.windowID == SDL_GetWindowID(gSDLWindow)) {
//int32_t w = ev.window.data1;
//int32_t h = ev.window.data2;
int w, h;
SDL_GL_GetDrawableSize(gSDLWindow, &w, &h);
reshape(w, h);
}
} else if (ev.type == SDL_KEYDOWN) {
if (gEdenMessageKeyboardRequired) {
EdenMessageInputKeyboard(ev.key.keysym.sym);
} else if (ev.key.keysym.sym == SDLK_ESCAPE) {
flowHandleEvent(EVENT_BACK_BUTTON);
} else if (ev.key.keysym.sym == SDLK_SPACE) {
flowHandleEvent(EVENT_TOUCH);
} else if ((ev.key.keysym.sym == SDLK_COMMA && (ev.key.keysym.mod & KMOD_LGUI)) || ev.key.keysym.sym == SDLK_p) {
showPreferences(gPreferences);
}
} else if (gSDLEventPreferencesChanged != 0 && ev.type == gSDLEventPreferencesChanged) {
rereadPreferences();
}
}
if (vs->isOpen()) {
if (vs->captureFrame()) {
gFrameCount++; // Increment ARToolKit FPS counter.
#ifdef DEBUG
if (gFrameCount % 150 == 0) {
ARLOGi("*** Camera - %f (frame/sec)\n", (double)gFrameCount/arUtilTimer());
gFrameCount = 0;
arUtilTimerReset();
}
#endif
if (!gPostVideoSetupDone) {
gCameraIsFrontFacing = false;
AR2VideoParamT *vid = vs->getAR2VideoParam();
if (vid->module == AR_VIDEO_MODULE_AVFOUNDATION) {
int frontCamera;
if (ar2VideoGetParami(vid, AR_VIDEO_PARAM_AVFOUNDATION_CAMERA_POSITION, &frontCamera) >= 0) {
gCameraIsFrontFacing = (frontCamera == AR_VIDEO_AVFOUNDATION_CAMERA_POSITION_FRONT);
}
}
bool contentRotate90, contentFlipV, contentFlipH;
if (gDisplayOrientation == 1) { // Landscape with top of device at left.
contentRotate90 = false;
contentFlipV = gCameraIsFrontFacing;
contentFlipH = gCameraIsFrontFacing;
} else if (gDisplayOrientation == 2) { // Portrait upside-down.
contentRotate90 = true;
contentFlipV = !gCameraIsFrontFacing;
contentFlipH = true;
} else if (gDisplayOrientation == 3) { // Landscape with top of device at right.
contentRotate90 = false;
contentFlipV = !gCameraIsFrontFacing;
contentFlipH = (!gCameraIsFrontFacing);
} else /*(gDisplayOrientation == 0)*/ { // Portait
contentRotate90 = true;
contentFlipV = gCameraIsFrontFacing;
contentFlipH = false;
}
// Setup a route for rendering the colour background image.
vv = new ARView;
if (!vv) {
ARLOGe("Error: unable to create video view.\n");
quit(-1);
}
vv->setRotate90(contentRotate90);
vv->setFlipH(contentFlipH);
vv->setFlipV(contentFlipV);
vv->setScalingMode(ARView::ScalingMode::SCALE_MODE_FIT);
vv->initWithVideoSource(*vs, contextWidth, contextHeight);
ARLOGi("Content %dx%d (wxh) will display in GL context %dx%d%s.\n", vs->getVideoWidth(), vs->getVideoHeight(), contextWidth, contextHeight, (contentRotate90 ? " rotated" : ""));
vv->getViewport(gViewport);
// Setup a route for rendering the mono background image.
ARParam idealParam;
arParamClear(&idealParam, vs->getVideoWidth(), vs->getVideoHeight(), AR_DIST_FUNCTION_VERSION_DEFAULT);
if ((gArglSettingsCornerFinderImage = arglSetupForCurrentContext(&idealParam, AR_PIXEL_FORMAT_MONO)) == NULL) {
ARLOGe("Unable to setup argl.\n");
quit(-1);
}
if (!arglDistortionCompensationSet(gArglSettingsCornerFinderImage, FALSE)) {
ARLOGe("Unable to setup argl.\n");
quit(-1);
}
arglSetRotate90(gArglSettingsCornerFinderImage, contentRotate90);
arglSetFlipV(gArglSettingsCornerFinderImage, contentFlipV);
arglSetFlipH(gArglSettingsCornerFinderImage, contentFlipH);
//
// Calibration init.
//
gCalibration = new Calibration(gCalibrationPatternType, gPreferencesCalibImageCountMax, gCalibrationPatternSize, gCalibrationPatternSpacing, vs->getVideoWidth(), vs->getVideoHeight());
if (!gCalibration) {
ARLOGe("Error initialising calibration.\n");
quit(-1);
}
if (!flowInitAndStart(gCalibration, saveParam, NULL)) {
ARLOGe("Error: Could not initialise and start flow.\n");
quit(-1);
}
// For FPS statistics.
arUtilTimerReset();
gFrameCount = 0;
gPostVideoSetupDone = true;
} // !gPostVideoSetupDone
if (contextWasUpdated) {
vv->setContextSize({contextWidth, contextHeight});
vv->getViewport(gViewport);
}
FLOW_STATE state = flowStateGet();
if (state == FLOW_STATE_WELCOME || state == FLOW_STATE_DONE || state == FLOW_STATE_CALIBRATING) {
// Upload the frame to OpenGL.
// Now done as part of the draw call.
} else if (state == FLOW_STATE_CAPTURING) {
gCalibration->frame(vs);
}
}
} // vs->isOpen()
// The display has changed.
drawView();
arUtilSleep(1); // 1 millisecond.
}
stopVideo();
quit(0);
}
void reshape(int w, int h)
{
contextWidth = w;
contextHeight = h;
ARLOGd("Resized to %dx%d.\n", w, h);
contextWasUpdated = true;
}
static void quit(int rc)
{
fileUploaderFinal(&fileUploadHandle);
SDL_Quit();
free(gPreferenceCameraOpenToken);
free(gPreferenceCameraResolutionToken);
free(gCalibrationServerUploadURL);
free(gCalibrationServerAuthenticationToken);
preferencesFinal(&gPreferences);
exit(rc);
}
static void usage(char *com)
{
ARLOG("Usage: %s [options]\n", com);
ARLOG("Options:\n");
ARLOG(" --vconf <video parameter for the camera>\n");
ARLOG(" -cornerx=n: specify the number of corners on chessboard in X direction.\n");
ARLOG(" -cornery=n: specify the number of corners on chessboard in Y direction.\n");
ARLOG(" -imagenum=n: specify the number of images captured for calibration.\n");
ARLOG(" -pattwidth=n: specify the square width in the chessbaord.\n");
ARLOG(" -h -help --help: show this message\n");
exit(0);
}
/*
static void init(int argc, char *argv[])
{
ARGViewport viewport;
char *vconf = NULL;
int i;
int gotTwoPartOption;
int screenWidth, screenHeight, screenMargin;
chessboardCornerNumX = 0;
chessboardCornerNumY = 0;
calibImageNum = 0;
patternWidth = 0.0f;
arMalloc(cwd, char, MAXPATHLEN);
if (!getcwd(cwd, MAXPATHLEN)) ARLOGe("Unable to read current working directory.\n");
else ARLOG("Current working directory is '%s'\n", cwd);
i = 1; // argv[0] is name of app, so start at 1.
while (i < argc) {
gotTwoPartOption = FALSE;
// Look for two-part options first.
if ((i + 1) < argc) {
if (strcmp(argv[i], "--vconf") == 0) {
i++;
vconf = argv[i];
gotTwoPartOption = TRUE;
}
}
if (!gotTwoPartOption) {
// Look for single-part options.
if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "-h") == 0) {
usage(argv[0]);
} else if (strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "-version") == 0 || strcmp(argv[i], "-v") == 0) {
ARLOG("%s version %s\n", argv[0], AR_HEADER_VERSION_STRING);
exit(0);
} else if( strncmp(argv[i], "-cornerx=", 9) == 0 ) {
if( sscanf(&(argv[i][9]), "%d", &chessboardCornerNumX) != 1 ) usage(argv[0]);
if( chessboardCornerNumX <= 0 ) usage(argv[0]);
} else if( strncmp(argv[i], "-cornery=", 9) == 0 ) {
if( sscanf(&(argv[i][9]), "%d", &chessboardCornerNumY) != 1 ) usage(argv[0]);
if( chessboardCornerNumY <= 0 ) usage(argv[0]);
} else if( strncmp(argv[i], "-imagenum=", 10) == 0 ) {
if( sscanf(&(argv[i][10]), "%d", &calibImageNum) != 1 ) usage(argv[0]);
if( calibImageNum <= 0 ) usage(argv[0]);
} else if( strncmp(argv[i], "-pattwidth=", 11) == 0 ) {
if( sscanf(&(argv[i][11]), "%f", &patternWidth) != 1 ) usage(argv[0]);
if( patternWidth <= 0 ) usage(argv[0]);
} else {
ARLOGe("Error: invalid command line argument '%s'.\n", argv[i]);
usage(argv[0]);
}
}
i++;
}
if( chessboardCornerNumX == 0 ) chessboardCornerNumX = CHESSBOARD_CORNER_NUM_X;
if( chessboardCornerNumY == 0 ) chessboardCornerNumY = CHESSBOARD_CORNER_NUM_Y;
if( calibImageNum == 0 ) calibImageNum = CALIB_IMAGE_NUM;
if( patternWidth == 0.0f ) patternWidth = (float)CHESSBOARD_PATTERN_WIDTH;
ARLOG("CHESSBOARD_CORNER_NUM_X = %d\n", chessboardCornerNumX);
ARLOG("CHESSBOARD_CORNER_NUM_Y = %d\n", chessboardCornerNumY);
ARLOG("CHESSBOARD_PATTERN_WIDTH = %f\n", patternWidth);
ARLOG("CALIB_IMAGE_NUM = %d\n", calibImageNum);
ARLOG("Video parameter: %s\n", vconf);
*/
static void drawBackground(const float width, const float height, const float x, const float y, const bool drawBorder)
{
GLfloat vertices[4][2];
vertices[0][0] = x; vertices[0][1] = y;
vertices[1][0] = width + x; vertices[1][1] = y;
vertices[2][0] = width + x; vertices[2][1] = height + y;
vertices[3][0] = x; vertices[3][1] = height + y;
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glVertexPointer(2, GL_FLOAT, 0, vertices);
glEnableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glClientActiveTexture(GL_TEXTURE0);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glColor4f(0.0f, 0.0f, 0.0f, 0.5f); // 50% transparent black.
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
if (drawBorder) {
glColor4f(1.0f, 1.0f, 1.0f, 1.0f); // Opaque white.
glLineWidth(1.0f);
glDrawArrays(GL_LINE_LOOP, 0, 4);
}
}
// An animation while we're waiting.
// Designed to be drawn on background of at least 3xsquareSize wide and tall.
static void drawBusyIndicator(int positionX, int positionY, int squareSize, struct timeval *tp)
{
const GLfloat square_vertices [4][2] = { {0.5f, 0.5f}, {squareSize - 0.5f, 0.5f}, {squareSize - 0.5f, squareSize - 0.5f}, {0.5f, squareSize - 0.5f} };
int i;
int hundredthSeconds = (int)tp->tv_usec / 1E4;
// Set up drawing.
glPushMatrix();
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glVertexPointer(2, GL_FLOAT, 0, square_vertices);
glEnableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glClientActiveTexture(GL_TEXTURE0);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
for (i = 0; i < 4; i++) {
glLoadIdentity();
glTranslatef((float)(positionX + ((i + 1)/2 != 1 ? -squareSize : 0.0f)), (float)(positionY + (i / 2 == 0 ? 0.0f : -squareSize)), 0.0f); // Order: UL, UR, LR, LL.
if (i == hundredthSeconds / 25) {
unsigned char r, g, b;
int secDiv255 = (int)tp->tv_usec / 3921;
int secMod6 = tp->tv_sec % 6;
if (secMod6 == 0) {
r = 255; g = secDiv255; b = 0;
} else if (secMod6 == 1) {
r = secDiv255; g = 255; b = 0;
} else if (secMod6 == 2) {
r = 0; g = 255; b = secDiv255;
} else if (secMod6 == 3) {
r = 0; g = secDiv255; b = 255;
} else if (secMod6 == 4) {
r = secDiv255; g = 0; b = 255;
} else {
r = 255; g = 0; b = secDiv255;
}
glColor4ub(r, g, b, 255);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
glColor4ub(255, 255, 255, 255);
glDrawArrays(GL_LINE_LOOP, 0, 4);
}
glPopMatrix();
}
void drawView(void)
{
int i;
struct timeval time;
float left, right, bottom, top;
GLfloat *vertices = NULL;
GLint vertexCount;
// Get frame time.
gettimeofday(&time, NULL);
SDL_GL_MakeCurrent(gSDLWindow, gSDLContext);
// Clean the OpenGL context.
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//
// Setup for drawing video frame.
//
glViewport(gViewport[0], gViewport[1], gViewport[2], gViewport[3]);
FLOW_STATE state = flowStateGet();
if (state == FLOW_STATE_WELCOME || state == FLOW_STATE_DONE || state == FLOW_STATE_CALIBRATING) {
// Display the current frame
vv->draw(vs);
} else if (state == FLOW_STATE_CAPTURING) {
// Grab a lock while we're using the data to prevent it being changed underneath us.
int cornerFoundAllFlag;
std::vector<cv::Point2f> corners;
ARUint8 *videoFrame;
gCalibration->cornerFinderResultsLockAndFetch(&cornerFoundAllFlag, corners, &videoFrame);
// Display the current frame.
if (videoFrame) arglPixelBufferDataUpload(gArglSettingsCornerFinderImage, videoFrame);
arglDispImage(gArglSettingsCornerFinderImage, NULL);
//
// Setup for drawing on top of video frame, in video pixel coordinates.
//
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (vv->rotate90()) glRotatef(90.0f, 0.0f, 0.0f, -1.0f);
if (vv->flipV()) {
bottom = (float)vs->getVideoHeight();
top = 0.0f;
} else {
bottom = 0.0f;
top = (float)vs->getVideoHeight();
}
if (vv->flipH()) {
left = (float)vs->getVideoWidth();
right = 0.0f;
} else {
left = 0.0f;
right = (float)vs->getVideoWidth();
}
glOrtho(left, right, bottom, top, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_BLEND);
glActiveTexture(GL_TEXTURE0);
glDisable(GL_TEXTURE_2D);
// Draw the crosses marking the corner positions.
vertexCount = (GLint)corners.size()*4;
if (vertexCount > 0) {
float fontSizeScaled = FONT_SIZE * (float)vs->getVideoHeight()/(float)(gViewport[(gDisplayOrientation % 2) == 1 ? 3 : 2]);
float colorRed[4] = {1.0f, 0.0f, 0.0f, 1.0f};
float colorGreen[4] = {0.0f, 1.0f, 0.0f, 1.0f};
glColor4fv(cornerFoundAllFlag ? colorRed : colorGreen);
EdenGLFontSetSize(fontSizeScaled);
EdenGLFontSetColor(cornerFoundAllFlag ? colorRed : colorGreen);
arMalloc(vertices, GLfloat, vertexCount*2); // 2 coords per vertex.
for (i = 0; i < corners.size(); i++) {
vertices[i*8 ] = corners[i].x - 5.0f;
vertices[i*8 + 1] = vs->getVideoHeight() - corners[i].y - 5.0f;
vertices[i*8 + 2] = corners[i].x + 5.0f;
vertices[i*8 + 3] = vs->getVideoHeight() - corners[i].y + 5.0f;
vertices[i*8 + 4] = corners[i].x - 5.0f;
vertices[i*8 + 5] = vs->getVideoHeight() - corners[i].y + 5.0f;
vertices[i*8 + 6] = corners[i].x + 5.0f;
vertices[i*8 + 7] = vs->getVideoHeight() - corners[i].y - 5.0f;
unsigned char buf[12]; // 10 digits in INT32_MAX, plus sign, plus null.
sprintf((char *)buf, "%d\n", i);
glPushMatrix();
glLoadIdentity();
glTranslatef(corners[i].x, vs->getVideoHeight() - corners[i].y, 0.0f);
glRotatef((float)(gDisplayOrientation - 1) * -90.0f, 0.0f, 0.0f, 1.0f); // Orient the text to the user.
EdenGLFontDrawLine(0, NULL, buf, 0.0f, 0.0f, H_OFFSET_VIEW_LEFT_EDGE_TO_TEXT_LEFT_EDGE, V_OFFSET_VIEW_BOTTOM_TO_TEXT_BASELINE); // These alignment modes don't require setting of EdenGLFontSetViewSize().
glPopMatrix();
}
EdenGLFontSetSize(FONT_SIZE);
float colorWhite[4] = {1.0f, 1.0f, 1.0f, 1.0f};;
EdenGLFontSetColor(colorWhite);
}
gCalibration->cornerFinderResultsUnlock();
if (vertexCount > 0) {
glVertexPointer(2, GL_FLOAT, 0, vertices);
glEnableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glClientActiveTexture(GL_TEXTURE0);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glLineWidth(2.0f);
glDrawArrays(GL_LINES, 0, vertexCount);
free(vertices);
}
}
//
// Setup for drawing on top of video frame, in viewPort coordinates.
//
#if 0
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
bottom = 0.0f;
top = (float)(viewPort[viewPortIndexHeight]);
left = 0.0f;
right = (float)(viewPort[viewPortIndexWidth]);
glOrthof(left, right, bottom, top, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
EdenGLFontSetViewSize(right, top);
EdenMessageSetViewSize(right, top, gDisplayDPI);
#endif
//
// Setup for drawing on screen, with correct orientation for user.
//
glViewport(0, 0, contextWidth, contextHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
bottom = 0.0f;
top = (float)contextHeight;
left = 0.0f;
right = (float)contextWidth;
glOrtho(left, right, bottom, top, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
EdenGLFontSetViewSize(right, top);
EdenMessageSetViewSize(right, top);
EdenMessageSetBoxParams(600.0f, 20.0f);
float statusBarHeight = EdenGLFontGetHeight() + 4.0f; // 2 pixels above, 2 below.
// Draw status bar with centred status message.
if (statusBarMessage[0]) {
drawBackground(right, statusBarHeight, 0.0f, 0.0f, false);
glDisable(GL_BLEND);
EdenGLFontDrawLine(0, NULL, statusBarMessage, 0.0f, 2.0f, H_OFFSET_VIEW_CENTER_TO_TEXT_CENTER, V_OFFSET_VIEW_BOTTOM_TO_TEXT_BASELINE);
}
// If background tasks are proceeding, draw a status box.
if (fileUploadHandle) {
char uploadStatus[UPLOAD_STATUS_BUFFER_LEN];
int status = fileUploaderStatusGet(fileUploadHandle, uploadStatus, &time);
if (status > 0) {
const int squareSize = (int)(16.0f * (float)gDisplayDPI / 160.f) ;
float x, y, w, h;
float textWidth = EdenGLFontGetLineWidth((unsigned char *)uploadStatus);
w = textWidth + 3*squareSize + 2*4.0f /*text margin*/ + 2*4.0f /* box margin */;
h = MAX(FONT_SIZE, 3*squareSize) + 2*4.0f /* box margin */;
x = right - (w + 2.0f);
y = statusBarHeight + 2.0f;
drawBackground(w, h, x, y, true);
if (status == 1) drawBusyIndicator((int)(x + 4.0f + 1.5f*squareSize), (int)(y + 4.0f + 1.5f*squareSize), squareSize, &time);
EdenGLFontDrawLine(0, NULL, (unsigned char *)uploadStatus, x + 4.0f + 3*squareSize, y + (h - FONT_SIZE)/2.0f, H_OFFSET_VIEW_LEFT_EDGE_TO_TEXT_LEFT_EDGE, V_OFFSET_VIEW_BOTTOM_TO_TEXT_BASELINE);
}
}
// If a message should be onscreen, draw it.
if (gEdenMessageDrawRequired) EdenMessageDraw(0, NULL);
SDL_GL_SwapWindow(gSDLWindow);
}
// Save parameters file and index file with info about it, then signal thread that it's ready for upload.
static void saveParam(const ARParam *param, ARdouble err_min, ARdouble err_avg, ARdouble err_max, void *userdata)
{
int i;
#define SAVEPARAM_PATHNAME_LEN MAXPATHLEN
char indexPathname[SAVEPARAM_PATHNAME_LEN];
char paramPathname[SAVEPARAM_PATHNAME_LEN];
char indexUploadPathname[SAVEPARAM_PATHNAME_LEN];
// Get the current time. It will be used for file IDs, plus a timestamp for the parameters file.
time_t ourClock = time(NULL);
if (ourClock == (time_t)-1) {
ARLOGe("Error reading time and date.\n");
return;
}
//struct tm *timeptr = localtime(&ourClock);
struct tm *timeptr = gmtime(&ourClock);
if (!timeptr) {
ARLOGe("Error converting time and date to UTC.\n");
return;
}
int ID = timeptr->tm_hour*10000 + timeptr->tm_min*100 + timeptr->tm_sec;
// Save the parameter file.
snprintf(paramPathname, SAVEPARAM_PATHNAME_LEN, "%s/%s/%06d-camera_para.dat", arUtilGetResourcesDirectoryPath(AR_UTIL_RESOURCES_DIRECTORY_BEHAVIOR_USE_APP_CACHE_DIR), QUEUE_DIR, ID);
//if (arParamSave(strcat(strcat(docsPath,"/"),paramPathname), 1, param) < 0) {
if (arParamSave(paramPathname, 1, param) < 0) {
ARLOGe("Error writing camera_para.dat file.\n");
} else {
bool goodWrite = true;
// Get main device identifier and focal length from video module.
char *device_id = NULL;
char *focal_length = NULL;
AR2VideoParamT *vid = vs->getAR2VideoParam();
if (ar2VideoGetParams(vid, AR_VIDEO_PARAM_DEVICEID, &device_id) < 0 || !device_id) {
ARLOGe("Error fetching camera device identification.\n");
goodWrite = false;
}
if (goodWrite) {
if (vid->module == AR_VIDEO_MODULE_AVFOUNDATION) {
int focalPreset;
ar2VideoGetParami(vid, AR_VIDEO_PARAM_AVFOUNDATION_FOCUS_PRESET, &focalPreset);
switch (focalPreset) {
case AR_VIDEO_AVFOUNDATION_FOCUS_MACRO:
focal_length = strdup("0.01");
break;
case AR_VIDEO_AVFOUNDATION_FOCUS_0_3M:
focal_length = strdup("0.3");
break;
case AR_VIDEO_AVFOUNDATION_FOCUS_1_0M:
focal_length = strdup("1.0");
break;
case AR_VIDEO_AVFOUNDATION_FOCUS_INF:
focal_length = strdup("1000000.0");
break;
default:
break;
}
}
if (!focal_length) {
// Not known at present, so just send 0.000.
focal_length = strdup("0.000");
}
}
if (goodWrite && gCalibrationSave) {
// Assemble the filename.
char calibrationSavePathname[SAVEPARAM_PATHNAME_LEN];
snprintf(calibrationSavePathname, SAVEPARAM_PATHNAME_LEN, "%s/camera_para-", gCalibrationSaveDir);
size_t len = strlen(calibrationSavePathname);
int i = 0;
while (device_id[i] && (len + i + 2 < SAVEPARAM_PATHNAME_LEN)) {
calibrationSavePathname[len + i] = (device_id[i] == '/' || device_id[i] == '\\' ? '_' : device_id[i]);
i++;
}
calibrationSavePathname[len + i] = '\0';
len = strlen(calibrationSavePathname);
snprintf(&calibrationSavePathname[len], SAVEPARAM_PATHNAME_LEN - len, "-0-%dx%d", vs->getVideoWidth(), vs->getVideoHeight()); // camera_index is always 0 for desktop platforms.
len = strlen(calibrationSavePathname);
if (strcmp(focal_length, "0.000") != 0) {
snprintf(&calibrationSavePathname[len], SAVEPARAM_PATHNAME_LEN - len, "-%s", focal_length);
len = strlen(calibrationSavePathname);
}
snprintf(&calibrationSavePathname[len], SAVEPARAM_PATHNAME_LEN - len, ".dat");
if (cp_f(paramPathname, calibrationSavePathname) != 0) {
ARLOGe("Error saving calibration to '%s'", calibrationSavePathname);
ARLOGperror(NULL);
} else {
ARLOGi("Saved calibration to '%s'.\n", calibrationSavePathname);
}
}
// Check for early exit.
if (!goodWrite || !gCalibrationServerUploadURL) {