77//
88
99#import " RCTARKit.h"
10- #import " Plane.h"
11-
10+ #import " RCTConvert+ARKit.h"
1211
1312@import CoreLocation;
1413
@@ -77,7 +76,7 @@ - (instancetype)initWithARView:(ARSCNView *)arView {
7776
7877 arView.scene .rootNode .name = @" root" ;
7978
80- self. planes = [ NSMutableDictionary new ];
79+
8180
8281 // start ARKit
8382 [self addSubview: arView];
@@ -150,6 +149,29 @@ - (void)setPlaneDetection:(BOOL)planeDetection {
150149 [self resume ];
151150}
152151
152+ -(NSDictionary *)origin {
153+ return @{
154+ @" position" : vectorToJson (self.nodeManager .localOrigin .position )
155+ };
156+ }
157+
158+ -(void )setOrigin : (NSDictionary *)json {
159+
160+ if (json[@" transition" ]) {
161+ NSDictionary * transition =json[@" transition" ];
162+ if (transition[@" duration" ]) {
163+ [SCNTransaction setAnimationDuration: [transition[@" duration" ] floatValue ]];
164+ } else {
165+ [SCNTransaction setAnimationDuration: 0.0 ];
166+ }
167+
168+ } else {
169+ [SCNTransaction setAnimationDuration: 0.0 ];
170+ }
171+ SCNVector3 position = [RCTConvert SCNVector3: json[@" position" ]];
172+ [self .nodeManager.localOrigin setPosition: position];
173+ }
174+
153175- (BOOL )lightEstimationEnabled {
154176 ARConfiguration *configuration = self.configuration ;
155177 return configuration.lightEstimationEnabled ;
@@ -195,6 +217,9 @@ - (NSDictionary *)readCameraPosition {
195217static NSDictionary * vectorToJson (const SCNVector3 v) {
196218 return @{ @" x" : @(v.x ), @" y" : @(v.y ), @" z" : @(v.z ) };
197219}
220+ static NSDictionary * vector_float3ToJson (const simd_float3 v) {
221+ return @{ @" x" : @(v.x ), @" y" : @(v.y ), @" z" : @(v.z ) };
222+ }
198223static NSDictionary * vector4ToJson (const SCNVector4 v) {
199224 return @{ @" x" : @(v.x ), @" y" : @(v.y ), @" z" : @(v.z ), @" w" : @(v.w ) };
200225}
@@ -216,7 +241,8 @@ - (NSDictionary *)readCamera {
216241}
217242
218243- (SCNVector3)projectPoint : (SCNVector3)point {
219- return [self .arView projectPoint: point];
244+ return [self .arView projectPoint: [self .nodeManager getAbsolutePositionToOrigin: point]];
245+
220246}
221247
222248
@@ -302,7 +328,7 @@ - (UIImage *)cropImage:(UIImage *)imageToCrop toRect:(CGRect)rect
302328 UIGraphicsBeginImageContext (src.size );
303329
304330 CGContextRef context = UIGraphicsGetCurrentContext ();
305- [src drawAtPoint: CGPointMake (0 , 0 )];
331+ [src drawAtPoint: CGPointMake (0 , 0 )];
306332 if (orientation == UIImageOrientationRight) {
307333 CGContextRotateCTM (context, radians (90 ));
308334 } else if (orientation == UIImageOrientationLeft) {
@@ -313,7 +339,7 @@ - (UIImage *)cropImage:(UIImage *)imageToCrop toRect:(CGRect)rect
313339 CGContextRotateCTM (context, radians (90 ));
314340 }
315341
316-
342+
317343
318344 UIImage *image = UIGraphicsGetImageFromCurrentImageContext ();
319345 UIGraphicsEndImageContext ();
@@ -382,27 +408,6 @@ - (void)hitTestPlane:(const CGPoint)tapPoint types:(ARHitTestResultType)types re
382408 resolve ([self getPlaneHitResult: tapPoint types: types]);
383409}
384410
385- static NSMutableArray * mapHitResults (NSArray <ARHitTestResult *> *results) {
386- NSMutableArray *resultsMapped = [NSMutableArray arrayWithCapacity: [results count ]];
387- [results enumerateObjectsUsingBlock: ^(id obj, NSUInteger index, BOOL *stop) {
388- ARHitTestResult *result = (ARHitTestResult *) obj;
389-
390- [resultsMapped addObject: (@{
391- @" distance" : @(result.distance ),
392- @" id" : result.anchor .identifier .UUIDString ,
393- @" point" : @{
394- @" x" : @(result.worldTransform .columns [3 ].x ),
395- @" y" : @(result.worldTransform .columns [3 ].y ),
396- @" z" : @(result.worldTransform .columns [3 ].z )
397- }
398-
399- } )];
400- }];
401- return resultsMapped;
402- }
403-
404-
405-
406411
407412
408413static NSDictionary * getPlaneHitResult (NSMutableArray *resultsMapped, const CGPoint tapPoint) {
@@ -418,7 +423,7 @@ - (void)hitTestPlane:(const CGPoint)tapPoint types:(ARHitTestResultType)types re
418423
419424- (NSDictionary *)getPlaneHitResult : (const CGPoint)tapPoint types : (ARHitTestResultType)types ; {
420425 NSArray <ARHitTestResult *> *results = [self .arView hitTest: tapPoint types: types];
421- NSMutableArray * resultsMapped = mapHitResults ( results) ;
426+ NSMutableArray * resultsMapped = [ self .nodeManager mapHitResults: results] ;
422427 NSDictionary *planeHitResult = getPlaneHitResult (resultsMapped, tapPoint);
423428 return planeHitResult;
424429}
@@ -461,70 +466,45 @@ - (void)renderer:(id <SCNSceneRenderer>)renderer didRenderScene:(SCNScene *)scen
461466}
462467
463468
469+ - (NSDictionary *)makePlaneDetectionResult : (SCNNode *)node planeAnchor : (ARPlaneAnchor *)planeAnchor {
470+
471+ return @{
472+ @" id" : planeAnchor.identifier .UUIDString ,
473+ @" alignment" : @(planeAnchor.alignment ),
474+ @" position" : vectorToJson ([self .nodeManager getRelativePositionToOrigin: node.position]),
475+ @" positionAbsolute" : vectorToJson (node.position ),
476+ // node is deprecated
477+ @" node" : vectorToJson (node.position ),
478+ @" center" : vector_float3ToJson (planeAnchor.center ),
479+ @" extent" : vector_float3ToJson (planeAnchor.extent )
480+ };
481+ }
482+
483+ - (void )renderer : (id <SCNSceneRenderer>)renderer willUpdateNode : (SCNNode *)node forAnchor : (ARAnchor *)anchor {
484+ }
485+
486+
464487- (void )renderer : (id <SCNSceneRenderer>)renderer didAddNode : (SCNNode *)node forAnchor : (ARAnchor *)anchor {
465488 if (![anchor isKindOfClass: [ARPlaneAnchor class ]]) {
466489 return ;
467490 }
468-
469- SCNNode *parent = [node parentNode ];
470- NSLog (@" plane detected" );
471- // NSLog(@"%f %f %f", parent.position.x, parent.position.y, parent.position.z);
472-
491+
473492 ARPlaneAnchor *planeAnchor = (ARPlaneAnchor *)anchor;
474-
475- // NSLog(@"%@", @{
476- // @"id": planeAnchor.identifier.UUIDString,
477- // @"alignment": @(planeAnchor.alignment),
478- // @"node": @{ @"x": @(node.position.x), @"y": @(node.position.y), @"z": @(node.position.z) },
479- // @"center": @{ @"x": @(planeAnchor.center.x), @"y": @(planeAnchor.center.y), @"z": @(planeAnchor.center.z) },
480- // @"extent": @{ @"x": @(planeAnchor.extent.x), @"y": @(planeAnchor.extent.y), @"z": @(planeAnchor.extent.z) },
481- // @"camera": @{ @"x": @(self.cameraOrigin.position.x), @"y": @(self.cameraOrigin.position.y), @"z": @(self.cameraOrigin.position.z) }
482- // });
483-
484493 if (self.onPlaneDetected ) {
485- self.onPlaneDetected (@{
486- @" id" : planeAnchor.identifier .UUIDString ,
487- @" alignment" : @(planeAnchor.alignment ),
488- @" node" : @{ @" x" : @(node.position .x ), @" y" : @(node.position .y ), @" z" : @(node.position .z ) },
489- @" center" : @{ @" x" : @(planeAnchor.center .x ), @" y" : @(planeAnchor.center .y ), @" z" : @(planeAnchor.center .z ) },
490- @" extent" : @{ @" x" : @(planeAnchor.extent .x ), @" y" : @(planeAnchor.extent .y ), @" z" : @(planeAnchor.extent .z ) },
491- // @"camera": @{ @"x": @(self.cameraOrigin.position.x), @"y": @(self.cameraOrigin.position.y), @"z": @(self.cameraOrigin.position.z) }
492- });
494+ self.onPlaneDetected ([self makePlaneDetectionResult: node planeAnchor: planeAnchor]);
493495 }
494496
495- // Plane *plane = [[Plane alloc] initWithAnchor: (ARPlaneAnchor *)anchor isHidden: NO];
496- // [self.planes setObject:plane forKey:anchor.identifier];
497- // [node addChildNode:plane];
498- }
499-
500- - (void )renderer : (id <SCNSceneRenderer>)renderer willUpdateNode : (SCNNode *)node forAnchor : (ARAnchor *)anchor {
501497}
502498
503499- (void )renderer : (id <SCNSceneRenderer>)renderer didUpdateNode : (SCNNode *)node forAnchor : (ARAnchor *)anchor {
504500 ARPlaneAnchor *planeAnchor = (ARPlaneAnchor *)anchor;
505501
506502 if (self.onPlaneUpdate ) {
507- self.onPlaneUpdate (@{
508- @" id" : planeAnchor.identifier .UUIDString ,
509- @" alignment" : @(planeAnchor.alignment ),
510- @" node" : @{ @" x" : @(node.position .x ), @" y" : @(node.position .y ), @" z" : @(node.position .z ) },
511- @" center" : @{ @" x" : @(planeAnchor.center .x ), @" y" : @(planeAnchor.center .y ), @" z" : @(planeAnchor.center .z ) },
512- @" extent" : @{ @" x" : @(planeAnchor.extent .x ), @" y" : @(planeAnchor.extent .y ), @" z" : @(planeAnchor.extent .z ) },
513- // @"camera": @{ @"x": @(self.cameraOrigin.position.x), @"y": @(self.cameraOrigin.position.y), @"z": @(self.cameraOrigin.position.z) }
514- });
503+ self.onPlaneUpdate ([self makePlaneDetectionResult: node planeAnchor: planeAnchor]);
515504 }
516505
517- Plane *plane = [self .planes objectForKey: anchor.identifier];
518- if (plane == nil ) {
519- return ;
520- }
521-
522- [plane update: (ARPlaneAnchor *)anchor];
523506}
524507
525- - (void )renderer : (id <SCNSceneRenderer>)renderer didRemoveNode : (SCNNode *)node forAnchor : (ARAnchor *)anchor {
526- // [self.planes removeObjectForKey:anchor.identifier];
527- }
528508
529509
530510
@@ -541,14 +521,14 @@ - (NSDictionary *)getCurrentLightEstimation {
541521- (NSMutableArray *)getCurrentDetectedFeaturePoints {
542522 NSMutableArray * featurePoints = [NSMutableArray array ];
543523 for (int i = 0 ; i < [self currentFrame ].rawFeaturePoints .count ; i++) {
544- vector_float3 point = [self currentFrame ].rawFeaturePoints .points [i];
545-
524+ vector_float3 positionV = [self currentFrame ].rawFeaturePoints .points [i];
525+ SCNVector3 position = [ self .nodeManager getRelativePositionToOrigin: SCNVector3Make (positionV[ 0 ],positionV[ 1 ],positionV[ 2 ])];
546526 NSString * pointId = [NSString stringWithFormat: @" featurepoint_%lld " ,[self currentFrame ].rawFeaturePoints.identifiers[i]];
547527
548528 [featurePoints addObject: @{
549- @" x" : @(point[ 0 ] ),
550- @" y" : @(point[ 1 ] ),
551- @" z" : @(point[ 2 ] ),
529+ @" x" : @(position. x ),
530+ @" y" : @(position. y ),
531+ @" z" : @(position. z ),
552532 @" id" :pointId,
553533 }];
554534
@@ -563,7 +543,7 @@ - (void)session:(ARSession *)session didUpdateFrame:(ARFrame *)frame {
563543 }
564544 }
565545 if (self.onFeaturesDetected ) {
566- NSMutableArray * featurePoints = [self getCurrentDetectedFeaturePoints ];
546+ NSArray * featurePoints = [self getCurrentDetectedFeaturePoints ];
567547 dispatch_async (dispatch_get_main_queue (), ^{
568548
569549
0 commit comments