forked from ScanNet/ScanNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController+Sensor.mm
More file actions
880 lines (721 loc) · 32.9 KB
/
ViewController+Sensor.mm
File metadata and controls
880 lines (721 loc) · 32.9 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
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
/*
This file is part of the Structure SDK.
Copyright © 2015 Occipital, Inc. All rights reserved.
http://structure.io
*/
#import "ViewController.h"
#import "ViewController+Camera.h"
#import "ViewController+Sensor.h"
#import "ViewController+OpenGL.h"
#import <Structure/Structure.h>
#include <string>
#include "EncoderConfig.h"
#include "Encoder.h"
#include "uplinksimple_image-codecs.h"
static const std::string BUILD_ID = [[[NSBundle mainBundle] objectForInfoDictionaryKey: (NSString*)kCFBundleVersionKey] UTF8String];
static const std::string APP_VERSION_ID = "Scanner.v1." + BUILD_ID;
static Encoder *g_encoder = nullptr;
static NSTimer *g_timer;
static unsigned g_frameCount = 0;
static unsigned g_encodedFrameCount = 0;
static bool g_running = false;
static bool g_receivedIntrinsics = false;
// TODO: Split up writing logic to a separate class
#ifdef WRITE_LOCAL
#define WRITE fwrite
#else
#define WRITE sendNalu
#endif
static std::string g_dataDir;
static std::string g_filePrefix;
static FILE *g_h264File;
static FILE *g_depthFile;
static FILE *g_metaFile;
static NSLock* g_h264Lock = [NSLock new];
static NSLock* g_depthLock = [NSLock new];
// 4-byte NALU start code
static uint8_t g_startCode[4] = {0, 0, 0, 1};
uint8_t* g_depthCompressed = nullptr;
void writeIntrinsics(const Options& options)
{
unsigned int colorWidth = options.useHalfResColor ? options.colorWidth / 2 : options.colorWidth;
unsigned int colorHeight = options.useHalfResColor ? options.colorHeight / 2 : options.colorHeight;
fprintf(g_metaFile, "colorWidth = %d\r\n", colorWidth);
fprintf(g_metaFile, "colorHeight = %d\r\n", colorHeight);
fprintf(g_metaFile, "depthWidth = %d\r\n", options.depthWidth);
fprintf(g_metaFile, "depthHeight = %d\r\n", options.depthHeight);
fprintf(g_metaFile, "fx_color = %f\r\n", options.colorFocalX);
fprintf(g_metaFile, "fy_color = %f\r\n", options.colorFocalY);
fprintf(g_metaFile, "mx_color = %f\r\n", options.colorCenterX);
fprintf(g_metaFile, "my_color = %f\r\n", options.colorCenterY);
fprintf(g_metaFile, "fx_depth = %f\r\n", options.depthFocalX);
fprintf(g_metaFile, "fy_depth = %f\r\n", options.depthFocalY);
fprintf(g_metaFile, "mx_depth = %f\r\n", options.depthCenterX);
fprintf(g_metaFile, "my_depth = %f\r\n", options.depthCenterY);
std::string colorToDepthExt = "";
for(int i = 0; i < 16; i++) {
colorToDepthExt += std::to_string(options.colorToDepthExtrinsics[i]) + " ";
}
fprintf(g_metaFile, "colorToDepthExtrinsics = %s\r\n", colorToDepthExt.c_str());
fprintf(g_metaFile, "deviceId = %s\r\n", options.deviceId.c_str());
fprintf(g_metaFile, "deviceName = %s\r\n", options.deviceName.c_str());
fprintf(g_metaFile, "sceneLabel = %s\r\n", options.sceneLabel.c_str());
fprintf(g_metaFile, "sceneType = %s\r\n", options.specifiedSceneType.c_str());
fprintf(g_metaFile, "userName = %s\r\n", options.userName.c_str());
fprintf(g_metaFile, "appVersionId = %s\r\n", APP_VERSION_ID.c_str());
fflush(g_metaFile);
}
void writeDepthFrame(STDepthFrame* depthFrame)
{
uint32_t size = uplinksimple::encode([depthFrame shiftData],
[depthFrame height] * [depthFrame width],
g_depthCompressed, sizeof(uint8_t)*[depthFrame height] * (2*[depthFrame width]+1));
[g_depthLock lock];
WRITE((uint8_t *) &size, sizeof(uint32_t), 1, g_depthFile);
WRITE(g_depthCompressed, sizeof(uint8_t), size, g_depthFile); //size is #bytes
[g_depthLock unlock];
}
void sendNalu(const void *src, size_t chunkSize, size_t numChunks, FILE *unused)
{
static int i = 0;
// Assumes chunkSize is always 1 (byte)
// Does not use FILE*. Included for fwrite compatibility
// [g_client sendNalU:(UInt8 *)src length:numChunks];
fwrite(src, chunkSize, numChunks, unused);
NSLog(@"Sent %d %zu chunks", i++, numChunks);
}
void writeToFile(CMSampleBufferRef sampleBuffer)
{
if (!CMSampleBufferDataIsReady(sampleBuffer))
{
NSLog(@"sampleBuffer is not ready ");
return;
}
bool keyFrame = !CFDictionaryContainsKey((CFDictionaryRef) CFArrayGetValueAtIndex(CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, true), 0), kCMSampleAttachmentKey_NotSync);
// SPS, PPS are their own NALUs
if (keyFrame)
{
CMFormatDescriptionRef format = CMSampleBufferGetFormatDescription(sampleBuffer);
size_t sparameterSetSize, sparameterSetCount;
const uint8_t *sparameterSet;
OSStatus statusCode = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format, 0, &sparameterSet, &sparameterSetSize, &sparameterSetCount, 0 );
if (statusCode == noErr)
{
// Found sps and now check for pps
size_t pparameterSetSize, pparameterSetCount;
const uint8_t *pparameterSet;
OSStatus statusCode = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format, 1, &pparameterSet, &pparameterSetSize, &pparameterSetCount, 0 );
if (statusCode == noErr)
{
WRITE(g_startCode, 1, 4, g_h264File);
WRITE(sparameterSet, 1, sparameterSetSize, g_h264File);
WRITE(g_startCode, 1, 4, g_h264File);
WRITE(pparameterSet, 1, pparameterSetSize, g_h264File);
}
}
}
CMBlockBufferRef buf = CMSampleBufferGetDataBuffer(sampleBuffer);
size_t length, totalLength;
char *dataPointer;
OSStatus statusCodeRet = CMBlockBufferGetDataPointer(buf, 0, &length, &totalLength, &dataPointer);
if (statusCodeRet == noErr)
{
size_t bufferOffset = 0;
static const int AVCCHeaderLength = 4;
unsigned counter = 0;
// Iterate through the NALUs
while (bufferOffset < totalLength - AVCCHeaderLength) {
// Read the NAL unit length
uint32_t NALUnitLength = 0;
memcpy(&NALUnitLength, dataPointer + bufferOffset, AVCCHeaderLength);
// Convert the length value from Big-endian to Little-endian
NALUnitLength = CFSwapInt32BigToHost(NALUnitLength);
WRITE(g_startCode, 1, 4, g_h264File);
WRITE(dataPointer + bufferOffset + AVCCHeaderLength, 1, NALUnitLength, g_h264File);
bufferOffset += AVCCHeaderLength + NALUnitLength;
}
}
}
void h264EncodedFrameCallback(CMSampleBufferRef sampleBuffer, void *userData)
{
if (g_running) {
[g_h264Lock lock];
writeToFile(sampleBuffer);
[g_h264Lock unlock];
dispatch_async(dispatch_get_main_queue(), ^{
g_encodedFrameCount++;
});
}
}
@implementation ViewController (Sensor)
#pragma mark - Structure Sensor delegates
- (void)setupStructureSensor
{
// Get the sensor controller singleton
_sensorController = [STSensorController sharedController];
// Set ourself as the delegate to receive sensor data.
_sensorController.delegate = self;
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingString:@"/"];
g_dataDir = [documentsDirectory UTF8String];
}
- (BOOL)isStructureConnectedAndCharged
{
return [_sensorController isConnected] && ![_sensorController isLowPower];
}
- (void)sensorDidConnect
{
NSLog(@"[Structure] Sensor connected!");
if (![self currentStateNeedsSensor])
{
[self enterSceneLabellingState];
//[self enterCubePlacementState];
}
[self connectToStructureSensorAndStartStreaming];
}
- (void)sensorDidLeaveLowPowerMode
{
_appStatus.sensorStatus = AppStatus::SensorStatusNeedsUserToConnect;
[self updateAppStatusMessage];
}
- (void)sensorBatteryNeedsCharging
{
[self cleanUpAndReset];
// Notify the user that the sensor needs to be charged.
_appStatus.sensorStatus = AppStatus::SensorStatusNeedsUserToCharge;
[self updateAppStatusMessage];
}
- (void)cleanUpFileWriting
{
if (g_encoder) { delete g_encoder; g_encoder = nullptr; }
if (g_depthCompressed) { delete [] g_depthCompressed; g_depthCompressed = nullptr; }
}
- (void)sensorDidStopStreaming:(STSensorControllerDidStopStreamingReason)reason
{
if (reason == STSensorControllerDidStopStreamingReasonAppWillResignActive)
{
[self stopColorCamera];
NSLog(@"[Structure] Stopped streaming because the app will resign its active state.");
}
else
{
NSLog(@"[Structure] Stopped streaming for an unknown reason.");
[self enterViewingState];
}
}
- (void)sensorDidDisconnect
{
// If we receive the message while in background, do nothing. We'll check the status when we
// become active again.
if ([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive)
return;
NSLog(@"[Structure] Sensor disconnected!");
// Reset the scan on disconnect, since we won't be able to recover afterwards.
if (_slamState.scannerState == ScannerStateScanning)
{
[self resetButtonPressed:self];
[self enterViewingState];
}
if (_useColorCamera)
[self stopColorCamera];
[self updateIdleTimer];
}
- (STSensorControllerInitStatus)connectToStructureSensorAndStartStreaming
{
// Try connecting to a Structure Sensor.
STSensorControllerInitStatus result = [_sensorController initializeSensorConnection];
if (result == STSensorControllerInitStatusSuccess || result == STSensorControllerInitStatusAlreadyInitialized)
{
// Even though _useColorCamera was set in viewDidLoad by asking if an approximate calibration is guaranteed,
// it's still possible that the Structure Sensor that has just been plugged in has a custom or approximate calibration
// that we couldn't have known about in advance.
STCalibrationType calibrationType = [_sensorController calibrationType];
if(calibrationType == STCalibrationTypeApproximate || calibrationType == STCalibrationTypeDeviceSpecific)
{
_useColorCamera = true;
}
else
{
_useColorCamera = false;
}
// If we can't use the color camera, then don't try to use registered depth.
if (!_useColorCamera)
_options.useHardwareRegisteredDepth = false;
_appStatus.sensorStatus = AppStatus::SensorStatusOk;
[self updateAppStatusMessage];
// Start streaming depth data.
[self startStructureSensorStreaming];
}
else
{
switch (result)
{
case STSensorControllerInitStatusSensorNotFound:
NSLog(@"[Structure] No sensor found"); break;
case STSensorControllerInitStatusOpenFailed:
NSLog(@"[Structure] Error: Open failed."); break;
case STSensorControllerInitStatusSensorIsWakingUp:
NSLog(@"[Structure] Error: Sensor still waking up."); break;
default: {}
}
_appStatus.sensorStatus = AppStatus::SensorStatusNeedsUserToConnect;
[self updateAppStatusMessage];
}
[self updateIdleTimer];
return result;
}
- (void)startStructureSensorStreaming
{
if (![self isStructureConnectedAndCharged])
return;
// Tell the driver to start streaming.
NSError *error = nil;
BOOL optionsAreValid = FALSE;
if (_useColorCamera)
{
// We can use either registered or unregistered depth.
_structureStreamConfig = _options.useHardwareRegisteredDepth ? STStreamConfigRegisteredDepth640x480 : STStreamConfigDepth640x480;
if (_options.useHardwareRegisteredDepth)
{
// We are using the color camera, so let's make sure the depth gets synchronized with it.
// If we use registered depth, we also need to specify a fixed lens position value for the color camera.
optionsAreValid = [_sensorController startStreamingWithOptions:@{kSTStreamConfigKey : @(_structureStreamConfig),
kSTFrameSyncConfigKey : @(STFrameSyncDepthAndRgb),
kSTColorCameraFixedLensPositionKey: @(_options.lensPosition)}
error:&error];
}
else
{
// We are using the color camera, so let's make sure the depth gets synchronized with it.
optionsAreValid = [_sensorController startStreamingWithOptions:@{kSTStreamConfigKey : @(_structureStreamConfig),
kSTFrameSyncConfigKey : @(STFrameSyncDepthAndRgb)}
error:&error];
}
[self startColorCamera];
}
else
{
_structureStreamConfig = STStreamConfigDepth640x480;
optionsAreValid = [_sensorController startStreamingWithOptions:@{kSTStreamConfigKey : @(_structureStreamConfig),
kSTFrameSyncConfigKey : @(STFrameSyncOff)} error:&error];
}
if (!optionsAreValid)
{
NSLog(@"Error during streaming start: %s", [[error localizedDescription] UTF8String]);
return;
}
NSLog(@"[Structure] Streaming started.");
// Notify and initialize streaming dependent objects.
[self onStructureSensorStartedStreaming];
}
- (void)onStructureSensorStartedStreaming
{
NSLog(@"onStructureSensorStartedStreaming");
if (!_slamState.initialized)
{
[self enterSceneLabellingState];
_depthAsRgbaVisualizer = [[STDepthToRgba alloc] initWithOptions:@{kSTDepthToRgbaStrategyKey: @(STDepthToRgbaStrategyRedToBlueGradient)}
error:nil];
_slamState.initialized = true;
}
}
- (void)sensorDidOutputDeviceMotion:(CMDeviceMotion*)motion
{
//[self processDeviceMotion:motion withError:nil];
}
GLKVector4 getIntrinsicsFromGlProj(const GLKMatrix4& matrix, unsigned int width, unsigned int height, bool useHalf)
{
float fov = 2.0f * atan(1.0f / matrix.m00);
float aspect = (float)width / height;
float t = tan(0.5f * fov);
float fx = 0.5f * width / t;
float fy = 0.5f * height / t * aspect;
float mx = (float)(width - 1.0f) / 2.0f;
float my = (float)(height - 1.0f) / 2.0f;
if (useHalf) {
fx *= 0.5f; fy *= 0.5f;
mx *= 0.5f; my *= 0.5f;
}
GLKVector4 ret = GLKVector4Make(fx, fy, mx, my);
return ret;
}
-(UIImage *) imageFromFrame:(STColorFrame*) colorFrame{
CMSampleBufferRef sampleBuffer = colorFrame.sampleBuffer;
@autoreleasepool {
CVImageBufferRef imBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imBuffer, 0);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef context = CGBitmapContextCreate(CVPixelBufferGetBaseAddressOfPlane(imBuffer, 0), CVPixelBufferGetWidth(imBuffer), CVPixelBufferGetHeight(imBuffer), 8, CVPixelBufferGetBytesPerRowOfPlane(imBuffer,0), colorSpace, kCGImageAlphaNone);
CGImageRef cgImage = CGBitmapContextCreateImage(context);
CVPixelBufferUnlockBaseAddress(imBuffer,0);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
UIImage *image = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
return image;
}
}
- (bool)sensorTooClose:(STDepthFrame*)depthFrame
{
float *depth = depthFrame.depthInMillimeters;
int totalSize = depthFrame.width * depthFrame.height;
int numNotNan = 0;
int numValidDistance = 0;
const double distanceThreshold = 750.0;
const double threshold = 0.45;
for(int i = 0; i < totalSize; i++)
{
if(!isnan(depth[i]))
{
numNotNan++;
if(depth[i] < distanceThreshold)
{
numValidDistance++;
}
}
}
double percentGood = ((double) numValidDistance) / numNotNan;
return (percentGood > threshold) || isnan(percentGood);
}
-(void)numFeatures:(UIImage*)inputImage
{
//NSLog(@"Beginning corner detection");
GPUImagePicture *pictureInput = [[GPUImagePicture alloc] initWithImage:inputImage];
GPUImageHarrisCornerDetectionFilter *cornerDetector = [[GPUImageHarrisCornerDetectionFilter alloc] init];
[cornerDetector forceProcessingAtSize:inputImage.size];
[cornerDetector setThreshold:0.12];
[cornerDetector setSensitivity:25.0];
[pictureInput removeAllTargets];
[cornerDetector setCornersDetectedBlock:^(GLfloat* cornerArray, NSUInteger cornersDetected, CMTime frameTime) {
//NSLog(@"Number of corners: %ld", (unsigned long)cornersDetected);
/*dispatch_async(dispatch_get_main_queue(), ^{
if(g_frameCount % 5 == 0)
{
bool hasNoCorners = cornersDetected < numCornerThreshold;
[[self losingTrackingLabel] setHidden:!(hasNoCorners && prevFrameNoFeatures && prevFrame2NoFeatures)];
prevFrame2NoFeatures = prevFrameNoFeatures;
prevFrameNoFeatures = hasNoCorners;
}
});*/
const unsigned int numCornerThreshold = 3;
for(int i = 0; i < cornersDetected; i++)
{
[corners addObject:[NSNumber numberWithFloat:cornerArray[2 * i]]];
[corners addObject:[NSNumber numberWithFloat:cornerArray[2 * i + 1]]];
}
bool hasNoCorners = cornersDetected < numCornerThreshold;
const float maxCorners = 25;
dispatch_async(dispatch_get_main_queue(), ^{
int numCornersForColor = (int) MIN(cornersDetected, maxCorners);
for(int i = 0; i < numPrevFramesTracked - 1; i++) {
numPreviousCorners[i] = numPreviousCorners[i + 1];
}
numPreviousCorners[numPrevFramesTracked - 1] = numCornersForColor;
numCornersForColor = *std::max_element(numPreviousCorners, numPreviousCorners + numPrevFramesTracked);
float progress = 0;
if(numCornersForColor <= 2) {
progress = 0.2;
}
else if(numCornersForColor >= maxCorners) {
progress = 1.0;
}
else {
progress = 0.2 + log2(numCornersForColor) / (log2(maxCorners) * 1.25);
}
/*
CGFloat redColor = CGFloat( ( (float) (maxCorners - numCornersForColor)) / maxCorners );
CGFloat greenColor = CGFloat( ( (float) (numCornersForColor) / maxCorners));
UIColor *color = [[UIColor alloc] initWithRed:redColor green:greenColor blue:0 alpha:1];
*/
float hue = 1.0f / 3 * log2(numCornersForColor) / log2(maxCorners);
//NSLog(@"Hue: %f", hue);
UIColor *color = [[UIColor alloc] initWithHue:hue saturation:1 brightness:1 alpha:1];
/*
if(cornersDetected <= 3) {
color = [UIColor redColor];
//progress = 0.4;
}
else if(cornersDetected < 6) {
//progress = 0.5;
color = [UIColor orangeColor];
}
else {
//progress = 1.0;
color = [UIColor greenColor];
}*/
//[[self losingTrackingLabel] setHidden:!hasNoCorners];
[[self numCornersProgressView] setProgressTintColor:color];
[[self numCornersProgressView] setProgress:progress animated:YES];
});
// });
}];
[pictureInput addTarget:cornerDetector];
[cornerDetector useNextFrameForImageCapture];
[pictureInput processImage];
//NSLog(@"XCorners: %lu", [corners count]);
}
- (void)processDepthFrame:(STDepthFrame *)depthFrame
colorFrameOrNil:(STColorFrame*)colorFrame
{
// Upload the new color image for next rendering.
if (_useColorCamera && colorFrame != nil)
{
[self uploadGLColorTexture: colorFrame];
}
//else if(!_useColorCamera)
//{
[self uploadGLColorTextureFromDepth:depthFrame];
//}
// Update the projection matrices since we updated the frames.
{
_display.depthCameraGLProjectionMatrix = [depthFrame glProjectionMatrix];
if (colorFrame)
_display.colorCameraGLProjectionMatrix = [colorFrame glProjectionMatrix];
}
}
- (void)sensorDidOutputSynchronizedDepthFrame:(STDepthFrame*)depthFrame
andColorFrame:(STColorFrame*)colorFrame
{
if (_slamState.initialized)
{
//NSLog(@"process depth/color frame");
[self processDepthFrame:depthFrame colorFrameOrNil:colorFrame];
if ((_slamState.scannerState == ScannerStateSceneLabelling || _slamState.scannerState == ScannerStateCubePlacement) && colorFrame && depthFrame) {
GLKVector4 colorIntrinsics = getIntrinsicsFromGlProj(colorFrame.glProjectionMatrix, _options.colorWidth, _options.colorHeight, _options.useHalfResColor);
GLKVector4 depthIntrinsics = getIntrinsicsFromGlProj(depthFrame.glProjectionMatrix, _options.depthWidth, _options.depthHeight, false);
//NSLog(@"color intrinsics: %f %f %f %f (%d,%d)\n", colorIntrinsics.x, colorIntrinsics.y, colorIntrinsics.z, colorIntrinsics.w, _options.colorWidth, _options.colorHeight);
//NSLog(@"depth intrinsics: %f %f %f %f (%d,%d)\n", depthIntrinsics.x, depthIntrinsics.y, depthIntrinsics.z, depthIntrinsics.w, _options.depthWidth, _options.depthHeight);
_options.colorFocalX = colorIntrinsics.x;
_options.colorFocalY = colorIntrinsics.y;
_options.colorCenterX = colorIntrinsics.z;
_options.colorCenterY = colorIntrinsics.w;
_options.depthFocalX = depthIntrinsics.x;
_options.depthFocalY = depthIntrinsics.y;
_options.depthCenterX = depthIntrinsics.z;
_options.depthCenterY = depthIntrinsics.w;
/*
float *m4 = (float*) malloc(16 * sizeof(float));
[depthFrame colorCameraPoseInDepthCoordinateFrame:m4];
for(int i = 0; i < 4; i++) {
NSLog(@"%f %f %f %f", m4[i], m4[i + 4], m4[i + 8], m4[i + 12]);
}
free(m4);
*/
if(!_options.useHardwareRegisteredDepth) {
float *m4 = (float*) malloc(16 * sizeof(float));
[_sensorController colorCameraPoseInSensorCoordinateFrame:m4];
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
_options.colorToDepthExtrinsics[4 * i + j] = m4[i + 4 * j];
}
//NSLog(@"%f %f %f %f", m4[i], m4[i + 4], m4[i + 8], m4[i + 12]);
}
free(m4);
}
else {
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
_options.colorToDepthExtrinsics[4 * i + j] = (i == j);
}
}
}
g_receivedIntrinsics = true;
}
if (_slamState.scannerState == ScannerStateScanning && g_running && colorFrame && depthFrame)
{
//if(g_frameCount % 100 == 0)
// NSLog(@"Frame Count: %d", g_frameCount);
colorFrame = colorFrame.halfResolutionColorFrame;
// Save first frame out as image
if(g_frameCount == 1)
{
UIImage *thumbnail = [self imageFromFrame:colorFrame];
std::string curFilePrefix = g_dataDir + g_filePrefix;
NSString *filePath = [NSString stringWithUTF8String:(curFilePrefix + ".jpg").c_str()];
//NSLog(filePath);
[UIImageJPEGRepresentation(thumbnail, 1) writeToFile: filePath atomically:YES];
}
g_encoder->encodeFrame(colorFrame);
writeDepthFrame(depthFrame);
_colorTimestamps.push_back([colorFrame timestamp]);
_depthTimestamps.push_back([depthFrame timestamp]);
//#ifndef DEBUG_MODE
//if((g_frameCount % 5) == 0) // Check if too close or losing tracking every 5 frames
//{
/*
if((g_frameCount % 10) == 0)
[[self tooCloseLabel] setHidden:(![self sensorTooClose:depthFrame])];
*/
UIImage* im = (colorFrame.height > 500) ? [self imageFromFrame:colorFrame.halfResolutionColorFrame] : [self imageFromFrame:colorFrame];
//UIImage* im = [self imageFromFrame:colorFrame];
[self numFeatures:im];
NSLog(@"Exposure Duration: %f, ISO: %f", CMTimeGetSeconds([self.videoDevice exposureDuration]), self.videoDevice.ISO);
AVCaptureWhiteBalanceGains wb = [self.videoDevice deviceWhiteBalanceGains];
float redGain = wb.redGain;
float greenGain = wb.greenGain;
float blueGain = wb.blueGain;
float exposureDuration = CMTimeGetSeconds([self.videoDevice exposureDuration]);
float ISO = self.videoDevice.ISO;
//NSLog(@"Blue Gain: %f Green Gain: %f Red Gain: %f", wb.blueGain, wb.greenGain, wb.redGain);
fwrite((float*) &redGain, sizeof(float), 1, g_cameraFile);
fwrite((float*) &greenGain, sizeof(float), 1, g_cameraFile);
fwrite((float*) &blueGain, sizeof(float), 1, g_cameraFile);
fwrite((float*) &exposureDuration, sizeof(float), 1, g_cameraFile);
fwrite((float*) &ISO, sizeof(float), 1, g_cameraFile);
fflush(g_cameraFile);
//#endif
dispatch_async(dispatch_get_main_queue(), ^{
g_frameCount++;
});
}
// Scene rendering is triggered by new frames to avoid rendering the same view several times.
[self renderSceneForDepthFrame:depthFrame colorFrameOrNil:colorFrame];
}
}
- (void)stopScanningAndWrite
{
if (g_running) {
g_running = false;
[self closeFile];
[self cleanUpFileWriting];
g_frameCount = 0;
g_encodedFrameCount = 0;
_colorTimestamps.clear();
_depthTimestamps.clear();
}
}
- (void)startScanningAndOpen
{
if (!g_running) {
unsigned int colorWidth = _options.useHalfResColor ? _options.colorWidth / 2 : _options.colorWidth;
unsigned int colorHeight = _options.useHalfResColor ? _options.colorHeight / 2 : _options.colorHeight;
NSLog(@"startScanningAndOpen (%d,%d, bitrate %d)", colorWidth, colorHeight, _options.colorEncodeBitrate);
EncoderConfig config(colorWidth, colorHeight, EncoderConfig::H264, 30, _options.colorEncodeBitrate); //30fps
g_encoder = new Encoder(config);
g_encoder->registerEncodedFrameCallback(h264EncodedFrameCallback, 0);
if (!g_depthCompressed) g_depthCompressed = new uint8_t[(2*_options.depthWidth+1) * _options.depthHeight];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd_hh-mm-ss"];
std::string dateString = [[dateFormatter stringFromDate:[NSDate date]] UTF8String];
if (!g_receivedIntrinsics || _options.deviceId.empty()) {
NSLog(@"ERROR: no intrinsics/device id!");
_appStatus.sensorStatus = AppStatus::SensorStatusNeedsIntrinsics;
[self updateAppStatusMessage];
g_running = false;
return;
}
else {
NSLog(@"color intrinsics: %f %f, %f %f", _options.colorFocalX, _options.colorFocalY, _options.colorCenterX, _options.colorCenterY);
NSLog(@"depth intrinsics: %f %f, %f %f", _options.depthFocalX, _options.depthFocalY, _options.depthCenterX, _options.depthCenterY);
}
[self openFile:(dateString + "__" + _options.deviceId)];
NSLog(@"[Structure] opened file");
writeIntrinsics(_options);
g_running = true;
}
}
- (void)sensorDidOutputDepthFrame:(STDepthFrame *)depthFrame
{
if (_slamState.initialized)
{
[self processDepthFrame:depthFrame colorFrameOrNil:nil];
// Scene rendering is triggered by new frames to avoid rendering the same view several times.
[self renderSceneForDepthFrame:depthFrame colorFrameOrNil:nil];
}
}
- (void) openFile:(std::string) filePref
{
g_h264File = fopen((g_dataDir + filePref + ".h264").c_str(), "wb");
g_depthFile = fopen((g_dataDir + filePref + ".depth").c_str(), "wb");
g_metaFile = fopen((g_dataDir + filePref + ".txt").c_str(), "w");
g_imuFile = fopen((g_dataDir + filePref + ".imu").c_str(), "wb");
g_cameraFile = fopen((g_dataDir + filePref + ".camera").c_str(), "wb");
std::string versionNum = "v1";
fwrite(versionNum.c_str(), versionNum.size() * sizeof(char), 1, g_cameraFile);
g_filePrefix = filePref;
}
- (void) closeFile
{
if (g_frameCount > _colorTimestamps.size() || g_frameCount > _depthTimestamps.size())
NSLog(@"ERROR: inconsistent #frames/#timestamps (color, depth, frames) = %lu, %lu, %d\n", _colorTimestamps.size(), _depthTimestamps.size(), g_frameCount);
NSLog(@"framecount = %d, encodedcount = %d, #imu = %d\n", g_frameCount, g_encodedFrameCount, g_numIMUmeasurements);
//get created at time
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMdd'T'hhmmssZZZ"];
std::string dateString = [[dateFormatter stringFromDate:[NSDate date]] UTF8String];
//write #frames
fprintf(g_metaFile, "scanSavedAt = %s\r\n", dateString.c_str());
fprintf(g_metaFile, "numDepthFrames = %d\r\n", g_frameCount);
fprintf(g_metaFile, "numColorFrames = %d\r\n", g_encodedFrameCount);
fprintf(g_metaFile, "numIMUmeasurements = %d\r\n", g_numIMUmeasurements);
fclose(g_metaFile);
//write timestamps to end of depth file
[g_depthLock lock];
WRITE(_depthTimestamps.data(), sizeof(NSTimeInterval), g_frameCount, g_depthFile);
WRITE(_colorTimestamps.data(), sizeof(NSTimeInterval), g_frameCount, g_depthFile);
fclose(g_depthFile);
[g_depthLock unlock];
[g_h264Lock lock];
fclose(g_h264File);
[g_h264Lock unlock];
[g_imuLock lock];
fclose(g_imuFile);
[g_imuLock unlock];
[g_cameraLock lock];
fclose(g_cameraFile);
[g_cameraLock unlock];
[self verifyFiles];
NSLog(@"[Structure] closed file");
}
- (void) verifyFiles
{
bool remove = false;
std::string curFilePrefix = g_dataDir + g_filePrefix;
NSError *error = nil;
NSString *absH264File = [NSString stringWithUTF8String:(curFilePrefix + ".h264").c_str()];
NSString *h264FileSize = [NSString stringWithFormat:@"%llu",
[[[NSFileManager defaultManager] attributesOfItemAtPath:absH264File error:&error] fileSize]];
if (!h264FileSize || error)
{
NSLog(@"H264 file %@, size %@, error: %@", absH264File, h264FileSize, error);
remove = true;
}
error = nil;
NSString *absDepthFile = [NSString stringWithUTF8String:(curFilePrefix + ".h264").c_str()];
NSString *depthFileSize = [NSString stringWithFormat:@"%llu",
[[[NSFileManager defaultManager] attributesOfItemAtPath:absDepthFile error:&error] fileSize]];
if (!depthFileSize || error)
{
NSLog(@"Depth file %@, size %@, error: %@", absDepthFile, depthFileSize, error);
remove = true;
}
error = nil;
NSString *absMetaFile = [NSString stringWithUTF8String:(curFilePrefix + ".h264").c_str()];
NSString *metaFileSize = [NSString stringWithFormat:@"%llu",
[[[NSFileManager defaultManager] attributesOfItemAtPath:absMetaFile error:&error] fileSize]];
if (!metaFileSize || error)
{
NSLog(@"Meta file %@, size %@, error: %@", absMetaFile, metaFileSize, error);
remove = true;
}
error = nil;
NSString *absImuFile = [NSString stringWithUTF8String:(curFilePrefix + ".h264").c_str()];
NSString *imuFileSize = [NSString stringWithFormat:@"%llu",
[[[NSFileManager defaultManager] attributesOfItemAtPath:absImuFile error:&error] fileSize]];
if (!imuFileSize || error)
{
NSLog(@"imu file %@, size %@, error: %@", absImuFile, imuFileSize, error);
remove = true;
}
if (remove)
{
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:absH264File error:nil];
[fileManager removeItemAtPath:absDepthFile error:nil];
[fileManager removeItemAtPath:absMetaFile error:nil];
[fileManager removeItemAtPath:absImuFile error:nil];
}
}
@end