<commit_before>//
//  ARSessionImpl.mm
//  CinderARKit
//
//  Created by <NAME> on 11/08/2017.
//


#import <Foundation/Foundation.h>
#import <ARKit/ARKit.h>

@interface AppleARKitSessionImpl : NSObject<ARSessionDelegate>
{
@public
    ARSession*       mARSession;
}
- (void)session:(ARSession*)session didUpdateFrame:(ARFrame*)frame;
- (void)session:(ARSession*)session didAddAnchors:(NSArray<ARAnchor*>*)anchors;
- (void)session:(ARSession*)session didUpdateAnchors:(NSArray<ARAnchor*>*)anchors;
- (void)session:(ARSession*)session didRemoveAnchors:(NSArray<ARAnchor*>*)anchors;

- (NSUUID*)addAnchorRelativeToWorldWithxPos:(NSNumber*)x yPos:(NSNumber*)y zPos:(NSNumber*)z;
- (NSUUID*)addAnchorRelativeToCameraWithxOffset:(NSNumber*)x yOffset:(NSNumber*)y zOffset:(NSNumber*)z;

@end

#import "ARSessionImpl.h"
#import "CinderARKitUtils.h"

#include "cinder/gl/gl.h"


// Global instances of the AR session to bridge C++ and Obj C
static AppleARKitSessionImpl* appleARKitSession = nil;   // Apple ARKit API
static ARKit::SessionImpl*    ciARKitSession = nullptr;       // Cinder ARKit Bridge


using namespace ARKit;

// Cinder ARKit Session implementation
SessionImpl::SessionImpl()
{
    // You already have an ARSession instance running
    DBG_ASSERT(ciARKitSession == nullptr)
    
    ciARKitSession = this;

    if (appleARKitSession == nil)
    {
        appleARKitSession = [[AppleARKitSessionImpl alloc] init];
        
        DBG_ASSERT(appleARKitSession);
    }
}

SessionImpl::~SessionImpl()
{
    DBG_ASSERT(ciARKitSession == this);
    ciARKitSession = nullptr;
}

void SessionImpl::runConfiguration( TrackingConfiguration config )
{
    ARConfiguration* configuration = getNativeARConfiguration( config );
    [appleARKitSession->mARSession runWithConfiguration:configuration];
}

void SessionImpl::pause()
{
    mIsRunning = false;
    [appleARKitSession->mARSession pause];
}

const AnchorID SessionImpl::addAnchorRelativeToCamera( vec3 offset )
{
    NSUUID* uid = [appleARKitSession addAnchorRelativeToCameraWithxOffset:@(offset.x) yOffset:@(offset.y) zOffset:@(offset.z)];
    return getUidStringFromUUID(uid);
}

bool SessionImpl::isInterfaceInPortraitOrientation() const
{
    return UIInterfaceOrientationIsPortrait( [[UIApplication sharedApplication] statusBarOrientation] );
}




// Apple ARKit API Bridge implementation

@implementation AppleARKitSessionImpl

- (id) init
{
    self = [super init];
    
    mARSession = [ARSession new];
    mARSession.delegate = self;
    
    return self;
}

- (void)session:(ARSession*)session didUpdateFrame:(ARFrame*)frame
{
    ciARKitSession->mIsRunning = true;
    
    auto orientation = [[UIApplication sharedApplication] statusBarOrientation];
    
    // Update view matrix
    ciARKitSession->mViewMatrix = toMat4([frame.camera viewMatrixForOrientation:orientation]);
    
    // Update projection matrix
    auto viewBounds = [[UIScreen mainScreen] bounds];
    CGSize viewportSize = viewBounds.size;
    ciARKitSession->mProjectionMatrix = toMat4([frame.camera projectionMatrixForOrientation:orientation
                                                                       viewportSize:viewportSize
                                                                             zNear:0.001
                                                                              zFar:1000]);
    // Update light estimate
    ciARKitSession->mAmbientLightIntensity = (float)[[frame lightEstimate] ambientIntensity] / 2000.0f;
    ciARKitSession->mAmbientColorTemperature = (float)[[frame lightEstimate] ambientColorTemperature];
    
    // Capture pixel YCbCr
    CVPixelBufferRef pixelBuffer = frame.capturedImage;
    ciARKitSession->mFrameYChannel  = getChannelForCVPixelBuffer( pixelBuffer, 0 );
    ciARKitSession->mFrameCbChannel = getChannelForCVPixelBuffer( pixelBuffer, 1, 2, 0 );
    ciARKitSession->mFrameCrChannel = getChannelForCVPixelBuffer( pixelBuffer, 1, 2, 1 );
    
    ciARKitSession->mCameraSize = vec2( (float)CVPixelBufferGetWidth( pixelBuffer ), (float)CVPixelBufferGetHeight( pixelBuffer ));
}


// ===== ANCHOR HANDLING ===========================================================================================

- (void)session:(ARSession *)session didAddAnchors:(NSArray<ARAnchor*>*)anchors
{
    for (ARAnchor* anchor in anchors)
    {
        const auto uid = getUidStringFromAnchor( anchor );
        
        if ([anchor isKindOfClass:[ARPlaneAnchor class]])
        {
            ARPlaneAnchor* pa = (ARPlaneAnchor*)anchor;
            updateOrAddAnchor(ciARKitSession->mPlaneAnchors, PlaneAnchor( uid, toMat4( pa.transform ), toVec3( pa.center ), toVec3( pa.extent )));
        }
        else
        {
            updateOrAddAnchor(ciARKitSession->mAnchors, Anchor( uid, toMat4( anchor.transform )));
        }
    }
}

- (void)session:(ARSession *)session didUpdateAnchors:(NSArray<ARAnchor*>*)anchors
{
    for (ARAnchor* anchor in anchors)
    {
        const auto uid = getUidStringFromAnchor( anchor );
        
        if ( [anchor isKindOfClass:[ARPlaneAnchor class]] )<commit_msg>enabled disabling of camera<commit_after>//
//  ARSessionImpl.mm
//  CinderARKit
//
//  Created by <NAME> on 11/08/2017.
//


#import <Foundation/Foundation.h>
#import <ARKit/ARKit.h>

@interface AppleARKitSessionImpl : NSObject<ARSessionDelegate>
{
@public
    ARSession*       mARSession;
}
- (void)session:(ARSession*)session didUpdateFrame:(ARFrame*)frame;
- (void)session:(ARSession*)session didAddAnchors:(NSArray<ARAnchor*>*)anchors;
- (void)session:(ARSession*)session didUpdateAnchors:(NSArray<ARAnchor*>*)anchors;
- (void)session:(ARSession*)session didRemoveAnchors:(NSArray<ARAnchor*>*)anchors;

- (NSUUID*)addAnchorRelativeToWorldWithxPos:(NSNumber*)x yPos:(NSNumber*)y zPos:(NSNumber*)z;
- (NSUUID*)addAnchorRelativeToCameraWithxOffset:(NSNumber*)x yOffset:(NSNumber*)y zOffset:(NSNumber*)z;

@end

#import "ARSessionImpl.h"
#import "CinderARKitUtils.h"

#include "cinder/gl/gl.h"


// Global instances of the AR session to bridge C++ and Obj C
static AppleARKitSessionImpl* appleARKitSession = nil;   // Apple ARKit API
static ARKit::SessionImpl*    ciARKitSession = nullptr;       // Cinder ARKit Bridge


using namespace ARKit;

// Cinder ARKit Session implementation
SessionImpl::SessionImpl()
{
    // You already have an ARSession instance running
    DBG_ASSERT(ciARKitSession == nullptr)
    
    ciARKitSession = this;

    if (appleARKitSession == nil)
    {
        appleARKitSession = [[AppleARKitSessionImpl alloc] init];
        
        DBG_ASSERT(appleARKitSession);
    }
}

SessionImpl::~SessionImpl()
{
    DBG_ASSERT(ciARKitSession == this);
    ciARKitSession = nullptr;
}

void SessionImpl::runConfiguration( TrackingConfiguration config )
{
    ARConfiguration* configuration = getNativeARConfiguration( config );
    [appleARKitSession->mARSession runWithConfiguration:configuration];
}

void SessionImpl::pause()
{
    mIsRunning = false;
    [appleARKitSession->mARSession pause];
}

const AnchorID SessionImpl::addAnchorRelativeToCamera( vec3 offset )
{
    NSUUID* uid = [appleARKitSession addAnchorRelativeToCameraWithxOffset:@(offset.x) yOffset:@(offset.y) zOffset:@(offset.z)];
    return getUidStringFromUUID(uid);
}

bool SessionImpl::isInterfaceInPortraitOrientation() const
{
    return UIInterfaceOrientationIsPortrait( [[UIApplication sharedApplication] statusBarOrientation] );
}




// Apple ARKit API Bridge implementation

@implementation AppleARKitSessionImpl

- (id) init
{
    self = [super init];
    
    mARSession = [ARSession new];
    mARSession.delegate = self;
    
    return self;
}

- (void)session:(ARSession*)session didUpdateFrame:(ARFrame*)frame
{
    ciARKitSession->mIsRunning = true;
    
    auto orientation = [[UIApplication sharedApplication] statusBarOrientation];
    
    // Update view matrix
    ciARKitSession->mViewMatrix = toMat4([frame.camera viewMatrixForOrientation:orientation]);
    
    // Update projection matrix
    auto viewBounds = [[UIScreen mainScreen] bounds];
    CGSize viewportSize = viewBounds.size;
    ciARKitSession->mProjectionMatrix = toMat4([frame.camera projectionMatrixForOrientation:orientation
                                                                       viewportSize:viewportSize
                                                                             zNear:0.001
                                                                              zFar:1000]);
    // Update light estimate
    ciARKitSession->mAmbientLightIntensity = (float)[[frame lightEstimate] ambientIntensity] / 2000.0f;
    ciARKitSession->mAmbientColorTemperature = (float)[[frame lightEstimate] ambientColorTemperature];
    
    // Capture pixel YCbCr
    if (ciARKitSession->mRGBCaptureEnabled)
    {
        CVPixelBufferRef pixelBuffer = frame.capturedImage;
        ciARKitSession->mFrameYChannel  = getChannelForCVPixelBuffer( pixelBuffer, 0 );
        ciARKitSession->mFrameCbChannel = getChannelForCVPixelBuffer( pixelBuffer, 1, 2, 0 );
        ciARKitSession->mFrameCrChannel = getChannelForCVPixelBuffer( pixelBuffer, 1, 2, 1 );
        
        ciARKitSession->mCameraSize = vec2( (float)CVPixelBufferGetWidth( pixelBuffer ), (float)CVPixelBufferGetHeight( pixelBuffer ));
    }
}


// ===== ANCHOR HANDLING ===========================================================================================

- (void)session:(ARSession *)session didAddAnchors:(NSArray<ARAnchor*>*)anchors
{
    for (ARAnchor* anchor in anchors)
    {
        const auto uid = getUidStringFromAnchor( anchor );
        
        if ([anchor isKindOfClass:[ARPlaneAnchor class]])
        {
            ARPlaneAnchor* pa = (ARPlaneAnchor*)anchor;
            updateOrAddAnchor(ciARKitSession->mPlaneAnchors, PlaneAnchor( uid, toMat4( pa.transform ), toVec3( pa.center ), toVec3( pa.extent )));
        }
        else
        {
            updateOrAddAnchor(ciARKitSession->mAnchors, Anchor( uid, toMat4( anchor.transform )));
        }
    }
}

- (void)session:(ARSession *)session didUpdateAnchors:(NSArray<ARAnchor*>*)anchors
{
    for (ARAnchor* anchor in anchors)
    {
        const auto uid = getUidStringFromAnchor( anchor );
        
        if ( [anchor isKindOfClass:[ARPlaneAnchor class]] )
{'fraction_non_alphanumeric': 0.12337030550690796, 'fraction_numerical': 0.0070052539404553416, 'mean_word_length': 3.5583148558758313, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '618358', 'n_tokens_mistral': 3110, 'n_tokens_neox': 2949, 'n_words': 563}