-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcameramodel-parser.re
More file actions
791 lines (710 loc) · 24 KB
/
cameramodel-parser.re
File metadata and controls
791 lines (710 loc) · 24 KB
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
/* -*- c -*- */
// Copyright (c) 2017-2023 California Institute of Technology ("Caltech"). U.S.
// Government sponsorship acknowledged. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Apparently I need this in MSVC to get constants
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <math.h>
#include "mrcal.h"
#include "_util.h"
#define DEBUG 0
// string defined by an explicit length. Instead of being 0-terminated
typedef struct
{
const char* s;
int len;
} string_t;
/*!re2c
re2c:define:YYCTYPE = char;
// No filling. All the data is available at the start
re2c:yyfill:enable = 0;
// I use @ tags
re2c:flags:tags = 1;
re2c:sentinel = 0;
SPACE = [ \t\n\r]*;
IGNORE = (SPACE | "#" [^\x00\n]* "\n")*;
// This FLOAT definition will erroneously accept "." and "" as a valid float,
// but the strtod converter will then reject it
FLOAT = "-"?[0-9]*("."[0-9]*)?([eE][+-]?[0-9]+)?;
UNSIGNED_INT = "0"|[1-9][0-9]*;
*/
/*!stags:re2c format = 'static const char *@@;'; */
static bool string_is(const char* ref, string_t key)
{
int ref_strlen = strlen(ref);
return
ref_strlen == key.len &&
0 == strncmp(key.s, ref, ref_strlen);
}
static bool read_string( // output stored here. If NULL, we try to read off
// a string, but do not store it; success in the
// return value, as usual
string_t* out,
const char** pYYCURSOR,
const char* start_file,
const char* what)
{
const char* YYMARKER;
const char* YYCURSOR = *pYYCURSOR;
while(true)
{
const char* s;
const char* e;
/*!re2c
IGNORE "b"? ["'] @s [^"'\x00]* @e ["']
{
if(out != NULL)
{
out->s = s;
out->len = (int)(e-s);
}
*pYYCURSOR = YYCURSOR;
return true;
}
* { break; }
*/
}
if(out != NULL)
MSG("Didn't see the %s string at position %ld. Giving up.",
what, (long int)(*pYYCURSOR - start_file));
return false;
}
static bool read_value( const char** pYYCURSOR,
const char* start_file)
{
const char* YYMARKER;
const char* YYCURSOR = *pYYCURSOR;
const char* s, *e;
while(true)
{
/*!re2c
IGNORE @s FLOAT @e
{
// FLOAT regex erroneously matches empty strings and ".". I
// explicitly check for those, and return a failure
if( e == s )
return false;
if( e == &s[1] && *s == '.')
return false;
break;
}
*
{
return false;
}
*/
}
*pYYCURSOR = YYCURSOR;
return true;
}
typedef bool (ingest_generic_consume_ignorable_t)(void* out0, int i,
const char** pYYCURSOR,
const char* start_file,
const char* what);
static bool ingest_double_consume_ignorable(void* out0, int i,
const char** pYYCURSOR,
const char* start_file,
const char* what)
{
const char* YYMARKER;
const char* YYCURSOR = *pYYCURSOR;
const char* s;
const char* e;
while(true)
{
/*!re2c
IGNORE @s FLOAT @e IGNORE
{
break;
}
*
{
MSG("Error parsing double-precision value for %s at %ld",
what, (long int)(*pYYCURSOR-start_file));
return false;
}
*/
}
if(out0 != NULL)
{
int N = e-s;
char tok[N+1];
memcpy(tok, s, N);
tok[N] = '\0';
char* endptr = NULL;
((double*)out0)[i] = strtod(tok, &endptr);
if( N == 0 || endptr == NULL || endptr != &tok[N] ||
!isfinite(((double*)out0)[i]))
{
MSG("Error parsing double-precision value for %s at %ld. String: '%s'",
what, (long int)(*pYYCURSOR-start_file), tok);
return false;
}
}
*pYYCURSOR = YYCURSOR;
return true;
}
static bool ingest_uint_consume_ignorable(void* out0, int i,
const char** pYYCURSOR,
const char* start_file,
const char* what)
{
const char* YYMARKER;
const char* YYCURSOR = *pYYCURSOR;
const char* s;
const char* e;
while(true)
{
/*!re2c
IGNORE @s UNSIGNED_INT @e IGNORE
{
break;
}
*
{
MSG("Error parsing unsigned integer for %s at %ld",
what, (long int)(*pYYCURSOR-start_file));
return false;
}
*/
}
if(out0 != NULL)
{
int N = e-s;
char tok[N+1];
memcpy(tok, s, N);
tok[N] = '\0';
int si = atoi(tok);
if( N == 0 || si < 0 )
{
MSG("Error parsing unsigned int for %s at %ld. String: '%s'",
what, (long int)(*pYYCURSOR-start_file), tok);
return false;
}
((unsigned int*)out0)[i] = (unsigned int)si;
}
*pYYCURSOR = YYCURSOR;
return true;
}
static bool read_list_values_generic( // output stored here. If NULL, we try to
// read off the values, but do not store
// them; success in the return value, as
// usual
void* out,
ingest_generic_consume_ignorable_t* f,
const char** pYYCURSOR, const char* start_file,
const char* what,
int Nvalues)
{
const char* YYMARKER;
const char* YYCURSOR = *pYYCURSOR;
while(true)
{
/*!re2c
IGNORE [[(] { break; }
*
{
MSG("Didn't see the opening [/( for the %s at position %ld. Giving up.",
what, (long int)(YYCURSOR - start_file));
return false;
}
*/
}
int i;
for(i=0; i<Nvalues-1; i++)
{
if(!(*f)(out, i, &YYCURSOR, start_file, what))
return false;
if(*YYCURSOR == ',')
YYCURSOR++;
else
{
MSG("Didn't see expected ',' at %ld while parsing %s",
(long int)(YYCURSOR-start_file), what);
return false;
}
}
// one more, but the trailing , is optional
{
if(!(*f)(out, i, &YYCURSOR, start_file, what))
return false;
if(*YYCURSOR == ',')
YYCURSOR++;
}
while(true)
{
/*!re2c
IGNORE [\])] { break; }
*
{
MSG("Didn't see the closing )/] for the %s at position %ld. Expected %d values, but the given list has more. Giving up.",
what, (long int)(YYCURSOR - start_file), Nvalues);
return false;
}
*/
}
*pYYCURSOR = YYCURSOR;
return true;
}
static bool read_balanced_list( const char** pYYCURSOR, const char* start_file )
{
const char* YYMARKER;
const char* YYCURSOR = *pYYCURSOR;
int level = 0;
while(true)
{
/*!re2c
( IGNORE | [0-9eE.,-]*)* [[(]
{
level++;
continue;
}
( IGNORE | [0-9eE.,-]*)* [\])] IGNORE ","?
{
level--;
if(level < 0)
{
MSG("Error reading a balanced list at %ld (list not balanced)",
(long int)(YYCURSOR-start_file));
return false;
}
if(level==0)
{
// closed last ]. Leave trailing , unprocessed. Caller will deal
// with it
if(YYCURSOR[-1] == ',')
YYCURSOR--;
*pYYCURSOR = YYCURSOR;
return true;
}
if(level > 0)
{
// closed inner ]. Trailing , afterwards is optional. I don't
// bother checking for this thoroughly, so I end up erroneously
// accepting expressions like [1,2,3][3,4,5]. But that's OK
;
}
continue;
}
*
{
MSG("Error reading a balanced list at %ld (unexpected value)",
(long int)(YYCURSOR-start_file));
return false;
}
*/
}
MSG("Getting here is a bug");
return false;
}
// Internal routine that does all the work
static
bool read_cameramodel_from_string(// output buffer. If it should be allocated,
// *model == NULL at the start. Otherwise
// *model is the preallocated buffer
mrcal_cameramodel_VOID_t** model,
// if the buffer is preallocated
// (*model!=NULL), the number of the
// intrinsics available in this buffer is in
// *Nintrinsics_max. If this is insufficient,
// we fail, set the number of intrinsics
// needed in *Nintrinsics_max, and return
// false. If we fail for any other reason, we
// set *Nintrinsics_max=0. If the buffer
// should be allocated, this isn't used
int* Nintrinsics_max,
// in
// if len>0, the string doesn't need to be
// 0-terminated. If len<=0, the end of the
// buffer IS indicated by a 0 byte
const char* string,
const int len)
{
bool model_need_dealloc = false;
bool did_read_intrinsics = false;
bool did_set_Nintrinsics_max = false;
// Set the output structure to invalid values that I can check later
// Everything except the intrinsics will be read here, and moved to *model
// at the end. The intrinsics are read directly into *model. This allows us
// to read in non-intrinsics before the full array is allocated
mrcal_cameramodel_VOID_t model_not_intrinsics =
{.rt_cam_ref[0] = DBL_MAX,
.imagersize = {},
.lensmodel.type = MRCAL_LENSMODEL_INVALID };
bool finished = false;
const char* YYMARKER;
const char* start_file = NULL;
// This is lame. If the end of the buffer is indicated by the buffer length
// only, I allocate a new padded buffer, and copy into it. Then this code
// looks for a terminated 0 always. I should instead use the re2c logic for
// fixed-length buffers (YYLIMIT), but that looks complicated
const char* YYCURSOR = NULL;
char* malloced_buf = NULL;
if(len > 0)
{
malloced_buf = malloc(len+1);
if(malloced_buf == NULL)
{
MSG("malloc() failed");
goto done;
}
memcpy(malloced_buf, string, len);
malloced_buf[len] = '\0';
YYCURSOR = malloced_buf;
}
else
YYCURSOR = string;
start_file = YYCURSOR;
///////// leading {
while(true)
{
/*!re2c
IGNORE "{" IGNORE { break; }
*
{
MSG("Didn't see leading '{'. Giving up.");
goto done;
}
*/
}
///////// key: value
while(true)
{
string_t key = {};
///////// key:
if(!read_string(&key, &YYCURSOR, start_file, "key"))
goto done;
while(true)
{
/*!re2c
IGNORE ":" { break; }
*
{
MSG("Didn't see expected ':' at %ld. Giving up.",
(long int)(YYCURSOR-start_file));
goto done;
}
*/
}
#if defined DEBUG && DEBUG
MSG("key: '%.*s'", key.len, key.s);
#endif
if( string_is("lensmodel", key) )
{
if(model_not_intrinsics.lensmodel.type >= 0)
{
MSG("lensmodel defined more than once");
goto done;
}
// "lensmodel" has string values
string_t lensmodel;
if(!read_string(&lensmodel,
&YYCURSOR, start_file, "lensmodel"))
goto done;
char lensmodel_string[lensmodel.len+1];
memcpy(lensmodel_string, lensmodel.s, lensmodel.len);
lensmodel_string[lensmodel.len] = '\0';
if( !mrcal_lensmodel_from_name(&model_not_intrinsics.lensmodel, lensmodel_string) )
{
MSG("Could not parse lensmodel '%s'", lensmodel_string);
goto done;
}
}
else if(string_is("intrinsics", key))
{
if(did_read_intrinsics)
{
MSG("intrinsics defined more than once");
goto done;
}
if(model_not_intrinsics.lensmodel.type < 0)
{
MSG("Saw 'intrinsics' key, before a 'lensmodel' key. Make sure that a 'lensmodel' key exists, and that it appears in the file before the 'intrinsics'");
goto done;
}
const int Nintrinsics = mrcal_lensmodel_num_params(&model_not_intrinsics.lensmodel);
if(*model == NULL)
{
// we need to allocate the model
*model = malloc(sizeof(mrcal_cameramodel_VOID_t) +
Nintrinsics*sizeof(double));
if(NULL == *model)
{
MSG("malloc() failed");
goto done;
}
model_need_dealloc = true;
}
else
{
// we read the data into a static buffer
if(Nintrinsics > *Nintrinsics_max)
{
*Nintrinsics_max = Nintrinsics;
did_set_Nintrinsics_max = true;
goto done;
}
}
if( !read_list_values_generic((*model)->intrinsics,
ingest_double_consume_ignorable,
&YYCURSOR, start_file,
"intrinsics", Nintrinsics) )
goto done;
did_read_intrinsics = true;
}
else if(string_is("extrinsics", key) ||
string_is("rt_cam_ref", key))
{
if(model_not_intrinsics.rt_cam_ref[0] != DBL_MAX)
{
// Receiving another one; must be identical
double rt_cam_ref[6];
if( !read_list_values_generic(rt_cam_ref,
ingest_double_consume_ignorable,
&YYCURSOR, start_file, "extrinsics", 6) )
goto done;
for(int i=0; i<6; i++)
if(1e-9 < fabs(rt_cam_ref[i] - model_not_intrinsics.rt_cam_ref[i]))
{
MSG("extrinsics defined more than once and aren't identical");
goto done;
}
}
else if( !read_list_values_generic(model_not_intrinsics.rt_cam_ref,
ingest_double_consume_ignorable,
&YYCURSOR, start_file, "extrinsics", 6) )
goto done;
}
else if(string_is("imagersize", key))
{
if(model_not_intrinsics.imagersize[0] > 0)
{
MSG("imagersize defined more than once");
goto done;
}
if( !read_list_values_generic(model_not_intrinsics.imagersize,
ingest_uint_consume_ignorable,
&YYCURSOR, start_file, "imagersize", 2) )
goto done;
}
else
{
// Some unknown key. Read off the data and continue
// try to read a string...
if(!read_value(&YYCURSOR, start_file) &&
!read_string(NULL, &YYCURSOR, start_file, "unknown") &&
!read_balanced_list(&YYCURSOR, start_file))
{
MSG("Error parsing value for key '%.*s' at %ld",
key.len, key.s,
(long int)(YYCURSOR-start_file));
goto done;
}
}
// Done with key:value. Look for optional trailing , and/or a }. We must
// see at least some of those things. k:v k:v without a , in-between is
// illegal
bool closing_brace = false;
const char* f;
while(true)
{
/*!re2c
IGNORE ("," | "}" | ("," IGNORE "}")) @f
{
if(f[-1] == '}') closing_brace = true;
break;
}
*
{
MSG("Didn't see trailing , at %ld",
(long int)(YYCURSOR-start_file));
goto done;
}
*/
}
if(closing_brace)
{
while(true)
{
/*!re2c
IGNORE [\x00]
{
finished = true;
goto done;
}
*
{
MSG("Garbage after trailing } at %ld. Giving up",
(long int)(f-1 - start_file));
goto done;
}
*/
}
}
}
done:
if(Nintrinsics_max != NULL && !did_set_Nintrinsics_max)
*Nintrinsics_max = 0;
free(malloced_buf);
if(!finished)
{
if(model_need_dealloc)
{
free(*model);
*model = NULL;
}
return false;
}
// Done parsing everything! Validate and finalize
if(!(model_not_intrinsics.lensmodel.type >= 0 &&
did_read_intrinsics &&
model_not_intrinsics.rt_cam_ref[0] != DBL_MAX &&
model_not_intrinsics.imagersize[0] > 0))
{
MSG("Incomplete cameramodel. Need keys: lensmodel, intrinsics, rt_cam_ref/extrinsics, imagersize");
if(model_need_dealloc)
{
free(*model);
*model = NULL;
}
return false;
}
memcpy(*model, &model_not_intrinsics, sizeof(model_not_intrinsics));
return true;
}
static
bool read_cameramodel_from_file(// output buffer. If it should be allocated,
// *model == NULL at the start. Otherwise *model
// is the preallocated buffer
mrcal_cameramodel_VOID_t** model,
// if the buffer is preallocated (*model!=NULL),
// the number of the intrinsics available in
// this buffer is in *Nintrinsics_max. If this
// is insufficient, we fail, set the number of
// intrinsics needed in *Nintrinsics_max, and
// return false. If we fail for any other
// reason, we set *Nintrinsics_max=0. If the
// buffer should be allocated, this isn't used
int* Nintrinsics_max,
// in
const char* filename)
{
int fd = -1;
char* string = NULL;
bool result = false;
bool did_set_Nintrinsics_max = false;
fd = open(filename, O_RDONLY);
if(fd < 0)
{
MSG("Couldn't open(\"%s\")", filename);
goto done;
}
struct stat st;
if(0 != fstat(fd, &st))
{
MSG("Couldn't stat(\"%s\")", filename);
goto done;
}
// I mmap twice:
//
// 1. anonymous mapping slightly larger than the file size. These are all 0
// 2. mmap of the file. The trailing 0 are preserved, and the parser can use
// the trailing 0 to indicate the end of file
//
// This is only needed if the file size is exactly a multiple of the page
// size. If it isn't, then the remains of the last page are 0 anyway.
string = mmap(NULL,
st.st_size + 1, // one extra byte
PROT_READ,
MAP_ANONYMOUS | MAP_PRIVATE,
-1, 0);
if(string == MAP_FAILED)
{
MSG("Couldn't mmap(anonymous) right before mmap(\"%s\")", filename);
goto done;
}
string = mmap(string, st.st_size,
PROT_READ,
MAP_FIXED | MAP_PRIVATE,
fd, 0);
if(string == MAP_FAILED)
{
MSG("Couldn't mmap(\"%s\")", filename);
goto done;
}
did_set_Nintrinsics_max = true;
result = read_cameramodel_from_string(model, Nintrinsics_max,
string,
// passing len==0 to use the \0 as the
// EOF marker. This is the more
// efficient path in
// read_cameramodel_from_string()
0);
done:
if(string != NULL && string != MAP_FAILED)
munmap(string, st.st_size+1);
if(fd >= 0)
close(fd);
if(Nintrinsics_max != NULL && !did_set_Nintrinsics_max)
*Nintrinsics_max = 0;
return result;
}
// if len>0, the string doesn't need to be 0-terminated. If len<=0, the end of
// the buffer IS indicated by a 0 byte
mrcal_cameramodel_VOID_t* mrcal_read_cameramodel_string(const char *string,
const int len)
{
mrcal_cameramodel_VOID_t* model = NULL;
bool result = read_cameramodel_from_string(&model, NULL,
string, len);
if(result) return model;
else return NULL;
}
mrcal_cameramodel_VOID_t* mrcal_read_cameramodel_file(const char* filename)
{
mrcal_cameramodel_VOID_t* model = NULL;
bool result = read_cameramodel_from_file(&model, NULL,
filename);
if(result) return model;
else return NULL;
}
void mrcal_free_cameramodel(mrcal_cameramodel_VOID_t** cameramodel)
{
free(*cameramodel);
*cameramodel = NULL;
}
bool mrcal_read_cameramodel_string_into(// out
mrcal_cameramodel_VOID_t* model,
// in,out
int* Nintrinsics_max,
// in
const char* string,
const int len)
{
return read_cameramodel_from_string(&model, Nintrinsics_max,
string, len);
}
bool mrcal_read_cameramodel_file_into (// out
mrcal_cameramodel_VOID_t* model,
// in,out
int* Nintrinsics_max,
// in
const char* filename)
{
return read_cameramodel_from_file(&model, Nintrinsics_max,
filename);
}