<commit_before>// Copyright eeGeo Ltd (2012-2015), All Rights Reserved

#include "CompassModel.h"
#include "EnvironmentFlatteningService.h"
#include "GpsGlobeCameraController.h"
#include "CameraHelpers.h"
#include "RenderCamera.h"
#include "VectorMath.h"
#include "NavigationService.h"
#include "ILocationService.h"
#include "LatLongAltitude.h"

namespace ExampleApp
{
    namespace Compass
    {
        namespace SdkModel
        {
            CompassModel::CompassModel(Eegeo::Location::NavigationService& navigationService,
                                       Eegeo::Location::ILocationService& locationService,
                                       Eegeo::Camera::GlobeCamera::GpsGlobeCameraController& controller,
                                       Metrics::IMetricsService& metricsService)
                :m_navigationService(navigationService)
                ,m_locationService(locationService)
                ,m_cameraController(controller)
                ,m_metricsService(metricsService)
            {
                m_compassGpsModeToNavigationGpsMode[Eegeo::Location::NavigationService::GpsModeOff] = GpsMode::GpsDisabled;
                m_compassGpsModeToNavigationGpsMode[Eegeo::Location::NavigationService::GpsModeFollow] = GpsMode::GpsFollow;
                m_compassGpsModeToNavigationGpsMode[Eegeo::Location::NavigationService::GpsModeCompass] = GpsMode::GpsCompassMode;

                m_navigationGpsModeToCompassGpsMode[GpsMode::GpsDisabled] = Eegeo::Location::NavigationService::GpsModeOff;
                m_navigationGpsModeToCompassGpsMode[GpsMode::GpsFollow] = Eegeo::Location::NavigationService::GpsModeFollow;
                m_navigationGpsModeToCompassGpsMode[GpsMode::GpsCompassMode] = Eegeo::Location::NavigationService::GpsModeCompass;
                
                m_gpsModeToString[GpsMode::GpsDisabled] = "GpsDisabled";
                m_gpsModeToString[GpsMode::GpsFollow] = "GpsFollow";
                m_gpsModeToString[GpsMode::GpsCompassMode] = "GpsCompassMode";
            }

            CompassModel::~CompassModel()
            {

            }

            bool CompassModel::GetGpsModeActive() const
            {
                return GetGpsMode() != GpsMode::GpsDisabled;
            }

            GpsMode::Values CompassModel::GetGpsMode() const
            {
                return m_gpsMode;
            }

            void CompassModel::CycleToNextGpsMode()
            {
                if(!m_locationService.GetIsAuthorized())
                {
                    DisableGpsMode();
                    m_gpsModeUnauthorizedCallbacks.ExecuteCallbacks();
                    return;
                }
                
                Eegeo::Space::LatLong latlong = Eegeo::Space::LatLong::FromDegrees(0.0, 0.0);
                if(!m_locationService.GetLocation(latlong))
                {
                    DisableGpsMode();
                    return;
                }
                
                int gpsMode = static_cast<int>(m_gpsMode);
                gpsMode = (gpsMode + 1) % static_cast<int>(GpsMode::GpsMode_Max);
                GpsMode::Values newGpsMode = static_cast<GpsMode::Values>(gpsMode);

                if(GetGpsModeActive() && newGpsMode == GpsMode::GpsDisabled)
                {
                    m_gpsMode = newGpsMode;
                    CycleToNextGpsMode();
                }
                else
                {
                    SetGpsMode(newGpsMode);
                }
            }

            void CompassModel::TryUpdateToNavigationServiceGpsMode(Eegeo::Location::NavigationService::GpsMode value)
            {
                if(!m_locationService.GetIsAuthorized())
                {
                    DisableGpsMode();
                    return;
                }
                
                GpsMode::Values gpsModeValueFromNavigationService = m_compassGpsModeToNavigationGpsMode[value];

                if(gpsModeValueFromNavigationService != GetGpsMode())
                {
                    SetGpsMode(gpsModeValueFromNavigationService);
                }
            }

            void CompassModel::DisableGpsMode()
            {
                SetGpsMode(GpsMode::GpsDisabled);
            }

            void CompassModel::SetGpsMode(GpsMode::Values gpsMode)
            {
                m_gpsMode = gpsMode;
                m_navigationService.SetGpsMode(m_navigationGpsModeToCompassGpsMode[gpsMode]);
                
                m_metricsService.SetEvent("SetGpsMode", "GpsMode", m_gpsModeToString[m_gpsMode]);
                
                m_gpsModeChangedCallbacks.ExecuteCallbacks();
            }

            float CompassModel::GetHeadingRadians() const
            {
                const Eegeo::Camera::RenderCamera renderCamera = m_cameraController.GetRenderCamera();
                const Eegeo::m44& cameraModelMatrix = renderCamera.GetModelMatrix();

                const Eegeo::v3& viewDirection = cameraModelMatrix.GetRow(2);

                Eegeo::v3 ecefUp = renderCamera.GetEcefLocation().ToSingle().Norm();
                const float epsilon = 0.001f;
                Eegeo::v3 heading;
                if (Eegeo::Math::Abs(Eegeo::v3::Dot(viewDirection, ecefUp)) > (1.f - epsilon))
                {
                    const Eegeo::v3& viewUp = cameraModelMatrix.GetRow(1);
                    heading = viewUp;
                }
                else
                {<commit_msg>Slight change to CompassModel. When we don't have auth for GPS we constantly keep setting mode to disabled, which triggers a Flurry metric event. This'll spam Flurry with false GPS update events. Wrapping the call to set GPS mode to disabled to only occur if it isn't already disabled. Buddy: Scott<commit_after>// Copyright eeGeo Ltd (2012-2015), All Rights Reserved

#include "CompassModel.h"
#include "EnvironmentFlatteningService.h"
#include "GpsGlobeCameraController.h"
#include "CameraHelpers.h"
#include "RenderCamera.h"
#include "VectorMath.h"
#include "NavigationService.h"
#include "ILocationService.h"
#include "LatLongAltitude.h"

namespace ExampleApp
{
    namespace Compass
    {
        namespace SdkModel
        {
            CompassModel::CompassModel(Eegeo::Location::NavigationService& navigationService,
                                       Eegeo::Location::ILocationService& locationService,
                                       Eegeo::Camera::GlobeCamera::GpsGlobeCameraController& controller,
                                       Metrics::IMetricsService& metricsService)
                :m_navigationService(navigationService)
                ,m_locationService(locationService)
                ,m_cameraController(controller)
                ,m_metricsService(metricsService)
            {
                m_compassGpsModeToNavigationGpsMode[Eegeo::Location::NavigationService::GpsModeOff] = GpsMode::GpsDisabled;
                m_compassGpsModeToNavigationGpsMode[Eegeo::Location::NavigationService::GpsModeFollow] = GpsMode::GpsFollow;
                m_compassGpsModeToNavigationGpsMode[Eegeo::Location::NavigationService::GpsModeCompass] = GpsMode::GpsCompassMode;

                m_navigationGpsModeToCompassGpsMode[GpsMode::GpsDisabled] = Eegeo::Location::NavigationService::GpsModeOff;
                m_navigationGpsModeToCompassGpsMode[GpsMode::GpsFollow] = Eegeo::Location::NavigationService::GpsModeFollow;
                m_navigationGpsModeToCompassGpsMode[GpsMode::GpsCompassMode] = Eegeo::Location::NavigationService::GpsModeCompass;
                
                m_gpsModeToString[GpsMode::GpsDisabled] = "GpsDisabled";
                m_gpsModeToString[GpsMode::GpsFollow] = "GpsFollow";
                m_gpsModeToString[GpsMode::GpsCompassMode] = "GpsCompassMode";
            }

            CompassModel::~CompassModel()
            {

            }

            bool CompassModel::GetGpsModeActive() const
            {
                return GetGpsMode() != GpsMode::GpsDisabled;
            }

            GpsMode::Values CompassModel::GetGpsMode() const
            {
                return m_gpsMode;
            }

            void CompassModel::CycleToNextGpsMode()
            {
                if(!m_locationService.GetIsAuthorized())
                {
                    DisableGpsMode();
                    m_gpsModeUnauthorizedCallbacks.ExecuteCallbacks();
                    return;
                }
                
                Eegeo::Space::LatLong latlong = Eegeo::Space::LatLong::FromDegrees(0.0, 0.0);
                if(!m_locationService.GetLocation(latlong))
                {
                    DisableGpsMode();
                    return;
                }
                
                int gpsMode = static_cast<int>(m_gpsMode);
                gpsMode = (gpsMode + 1) % static_cast<int>(GpsMode::GpsMode_Max);
                GpsMode::Values newGpsMode = static_cast<GpsMode::Values>(gpsMode);

                if(GetGpsModeActive() && newGpsMode == GpsMode::GpsDisabled)
                {
                    m_gpsMode = newGpsMode;
                    CycleToNextGpsMode();
                }
                else
                {
                    SetGpsMode(newGpsMode);
                }
            }

            void CompassModel::TryUpdateToNavigationServiceGpsMode(Eegeo::Location::NavigationService::GpsMode value)
            {
                if(!m_locationService.GetIsAuthorized())
                {
                    DisableGpsMode();
                    return;
                }
                
                GpsMode::Values gpsModeValueFromNavigationService = m_compassGpsModeToNavigationGpsMode[value];

                if(gpsModeValueFromNavigationService != GetGpsMode())
                {
                    SetGpsMode(gpsModeValueFromNavigationService);
                }
            }

            void CompassModel::DisableGpsMode()
            {
            	if (GetGpsMode() != GpsMode::GpsDisabled)
            	{
            		SetGpsMode(GpsMode::GpsDisabled);
            	}
            }

            void CompassModel::SetGpsMode(GpsMode::Values gpsMode)
            {
                m_gpsMode = gpsMode;
                m_navigationService.SetGpsMode(m_navigationGpsModeToCompassGpsMode[gpsMode]);
                
                m_metricsService.SetEvent("SetGpsMode", "GpsMode", m_gpsModeToString[m_gpsMode]);
                
                m_gpsModeChangedCallbacks.ExecuteCallbacks();
            }

            float CompassModel::GetHeadingRadians() const
            {
                const Eegeo::Camera::RenderCamera renderCamera = m_cameraController.GetRenderCamera();
                const Eegeo::m44& cameraModelMatrix = renderCamera.GetModelMatrix();

                const Eegeo::v3& viewDirection = cameraModelMatrix.GetRow(2);

                Eegeo::v3 ecefUp = renderCamera.GetEcefLocation().ToSingle().Norm();
                const float epsilon = 0.001f;
                Eegeo::v3 heading;
                if (Eegeo::Math::Abs(Eegeo::v3::Dot(viewDirection, ecefUp)) > (1.f - epsilon))
                {
                    const Eegeo::v3& viewUp = cameraModelMatrix.GetRow(1);
                    heading = viewUp;
                }
                else
                {
{'fraction_non_alphanumeric': 0.08682952734997344, 'fraction_numerical': 0.004779607010090282, 'mean_word_length': 1.8163010967098705, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2749068', 'n_tokens_mistral': 3184, 'n_tokens_neox': 2980, 'n_words': 385}