Ejemplo n.º 1
0
    /// <summary>
    /// For Unity Simulator
    /// Checks if the current camera pose is within the range for localization.
    /// </summary>
    public void checkLocalization()
    {
        PNTransformUnity currCameraPose = GetPose();
        Vector3          currPosition   = new Vector3(currCameraPose.position.x, currCameraPose.position.y, currCameraPose.position.z);
        Quaternion       currRotation   = new Quaternion(currCameraPose.rotation.x, currCameraPose.rotation.y,
                                                         currCameraPose.rotation.z, currCameraPose.rotation.w);

        /// Iterate through each saved cameraPose in the map to find the one that matches
        /// the current cameraPose.
        for (int i = 0; i < simCameraPoses.cameraPoses.Count; i++)
        {
            PNTransformUnity localizeCameraPose = simCameraPoses.cameraPoses[i];
            Vector3          localizePosition   = new Vector3(localizeCameraPose.position.x, localizeCameraPose.position.y, localizeCameraPose.position.z);
            Quaternion       localizeRotation   = new Quaternion(localizeCameraPose.rotation.x, localizeCameraPose.rotation.y,
                                                                 localizeCameraPose.rotation.z, localizeCameraPose.rotation.w);
            float positionDiffNorm = Vector3.Distance(currPosition, localizePosition);
            float angleDiffNorm    = Quaternion.Angle(localizeRotation, currRotation);

            if (positionDiffNorm < SIM_LOCAL_DISTANCE_THRESHOLD && angleDiffNorm < SIM_LOCAL_ANGLE_THRESHOLD)
            {
                mCurrStatus = MappingStatus.RUNNING;
                break;
            }
            else
            {
                mCurrStatus = MappingStatus.LOST;
            }
        }
    }
Ejemplo n.º 2
0
    static void OnPose(ref PNTransformUnity outputPose, ref PNTransformUnity arkitPose, IntPtr context)
    {
        Matrix4x4 outputPoseMat = PNUtility.MatrixOps.PNPose2Matrix4x4(outputPose);
        Matrix4x4 arkitPoseMat  = PNUtility.MatrixOps.PNPose2Matrix4x4(arkitPose);

        MappingStatus status = Instance.GetStatus();

        var listeners = Instance.listeners;

        if (status == MappingStatus.RUNNING)
        {
            MainThreadTaskQueue.InvokeOnMainThread(() => {
                foreach (var listener in listeners)
                {
                    listener.OnPose(outputPoseMat, arkitPoseMat);
                }
            });
            Instance.mCurrentTransform = outputPoseMat * arkitPoseMat.inverse;
        }

        if (status != Instance.mPrevStatus)
        {
            MainThreadTaskQueue.InvokeOnMainThread(() => {
                foreach (var listener in listeners)
                {
                    listener.OnStatusChange(Instance.mPrevStatus, status);
                }
                Instance.mPrevStatus = status;
            });
        }
    }
Ejemplo n.º 3
0
    /// <summary>
    /// Starts a mapping/localization session. If a map is loaded before <see cref="StartSession"/> is called,
    /// the session will operate in localization mode, and will not add more points. If a map
    /// is not loaded, a mapping session will be started to create a map that can be saved with <see cref="SaveMap"/>
    /// </summary>
    public void StartSession()
    {
                #if !UNITY_EDITOR
        PNStartSession(OnPose, IntPtr.Zero);
                #else
        if (mLocalization)
        {
            /// Set MappingStatus to LOST if status is localization
            mCurrStatus = MappingStatus.LOST;
            /// Stops the relocalization (checkLocalization) or the mapping (saving cameraPoses) invoke
            sInstance.CancelInvoke();
            /// Start checking for localization
            sInstance.InvokeRepeating("checkLocalization", 0f, 0.5f);
        }
        else
        {
            /// Set MappingStatus to RUNNING if status is mapping (ie. not localization)
            mCurrStatus = MappingStatus.RUNNING;
            /// Stops the relocalization (checkLocalization) or the mapping (saving cameraPoses) invoke
            sInstance.CancelInvoke();
            /// Start saving camera poses to create a map
            simCameraPoses.cameraPoses = new List <PNTransformUnity> ();
            sInstance.InvokeRepeating("SaveCameraPose", 0f, 0.5f);
        }

        /// A coroutine that simulates the InvokeRepeating of OnPose
        StartCoroutine(OnPoseInvokeRepeat());
                #endif
    }
        /// <summary>
        /// Arguments used to create a point mapping.
        /// </summary>
        /// <param name="point">Point value being mapped.</param>
        /// <param name="shard">Shard used as the mapping target.</param>
        /// <param name="status">Status of the mapping.</param>
        public PointMappingCreationInfo(TKey point, Shard shard, MappingStatus status)
        {
            ExceptionUtils.DisallowNullArgument(shard, "shard");
            this.Value  = point;
            this.Shard  = shard;
            this.Status = status;

            this.Key = new ShardKey(ShardKey.ShardKeyTypeFromType(typeof(TKey)), point);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Gets the status of the mapping session
 /// </summary>
 /// <returns>The status of the mapping session.</returns>
 public MappingStatus GetStatus()
 {
             #if !UNITY_EDITOR
     MappingStatus status = (MappingStatus)PNGetStatus();
     return(status);
             #else
     return(mCurrStatus);
             #endif
 }
        public RangeMappingCreationInfo(Range <TKey> value, Shard shard, MappingStatus status)
        {
            ExceptionUtils.DisallowNullArgument(value, "value");
            ExceptionUtils.DisallowNullArgument(shard, "shard");
            this.Value  = value;
            this.Shard  = shard;
            this.Status = status;

            this.Range = new ShardRange(
                new ShardKey(ShardKey.ShardKeyTypeFromType(typeof(TKey)), value.Low),
                new ShardKey(ShardKey.ShardKeyTypeFromType(typeof(TKey)), value.HighIsMax ? null : (object)value.High));
        }
Ejemplo n.º 7
0
    /// <summary>
    /// Stops the running mapping/localization session.
    /// </summary>
    public void StopSession()
    {
        mCurrentTransform = null;         //transform is again, meaningless
                #if !UNITY_EDITOR
        PNStopSession();
                #else
        /// Stops the current OnPose coroutine
        StopCoroutine(OnPoseInvokeRepeat());

        /// Stops the relocalization or the mapping (saving cameraPoses) invoke
        sInstance.CancelInvoke();

        mCurrStatus   = MappingStatus.WAITING;
        mLocalization = false;
                #endif
    }
Ejemplo n.º 8
0
    void Awake()
    {
        var keyMapping = NoteLayoutOptionsHandler.KeyMapping;

        length        = keyMapping.Count;
        mappingStatus = new MappingStatus[length];
        int i = 0;

        foreach (var kv in keyMapping)
        {
            mappingStatus[i++] = new MappingStatus {
                keyCode   = kv.Value,
                channelId = kv.Key
            };
        }
    }
Ejemplo n.º 9
0
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
        private void RefreshThread()
        {
            lock(multiThreadLock)
            {
                IncreaseWorkCount();

                mappingProtocol = MappingProtocol.None;
                externalIPAddress = null;

                lock (portMappings)
                {
                    for (int i = 0; i < portMappings.Count; i++)
                    {
                        PortMapping pm = (PortMapping)portMappings[i];
                        if (pm.MappingStatus == PortMappingStatus.Mapped)
                        {
                            pm.SetMappingStatus(PortMappingStatus.Unmapped);
                        }
                    }
                }

                OnWillStartSearchForRouter();

                DebugLog.WriteLine("RefreshThread");

                // Update all the following variables:
                // - routerIPAddress
                // - routerManufacturer
                // - localIPAddress
                // - localIPAddressOnSubnet
                //
                // Note: The order in which we call the following methods matters.
                // We must update the routerIPAddress before the others.
                UpdateRouterIPAddress();
                UpdateRouterManufacturer();
                UpdateLocalIPAddress();

                DebugLog.WriteLine("routerIPAddress       : {0}", routerIPAddress);
                DebugLog.WriteLine("routerManufacturer    : {0}", routerManufacturer);
                DebugLog.WriteLine("localIPAddress        : {0}", localIPAddress);
                DebugLog.WriteLine("localIPAddressOnSubnet: {0}", localIPOnRouterSubnet);

                if (routerIPAddress != null)
                {
                    if ((localIPAddress != null) && localIPOnRouterSubnet)
                    {
                        externalIPAddress = null;

                        if (IsIPv4AddressInPrivateSubnet(routerIPAddress))
                        {
                            natpmpStatus = MappingStatus.Trying;
                            upnpStatus = MappingStatus.Trying;

                            natpmpPortMapper.Refresh();
                            upnpPortMapper.Refresh();
                        }
                        else
                        {
                            natpmpStatus = MappingStatus.Failed;
                            upnpStatus = MappingStatus.Failed;
                            externalIPAddress = localIPAddress;
                            mappingProtocol = MappingProtocol.None;

                            // Set all mappings to be mapped with their local port number being the external one
                            lock (portMappings)
                            {
                                for (int i = 0; i < portMappings.Count; i++)
                                {
                                    PortMapping pm = (PortMapping)portMappings[i];
                                    pm.SetExternalPort(pm.LocalPort);
                                    pm.SetMappingStatus(PortMappingStatus.Mapped);
                                }
                            }

                            OnDidFinishSearchForRouter();
                        }
                    }
                    else
                    {
                        OnDidFinishSearchForRouter();
                    }
                }
                else
                {
                    OnDidFinishSearchForRouter();
                }

                // If we call DecreaseWorkCount right now, then it will likely result in a call to DidFinishWork.
                // This is because we the background threads that get called likely haven't started yet.
                // Therefore we delay this call, so as to allow the background threads to start first.
                new Timer(new TimerCallback(DecreaseWorkCount), null, (1 * 1000), Timeout.Infinite);
            }
        }
Ejemplo n.º 10
0
        private void natpmpPortMapper_DidFail(NATPMPPortMapper sender)
        {
            DebugLog.WriteLine("natpmpPortMapper_DidFail");

            if (natpmpStatus == MappingStatus.Trying)
            {
                natpmpStatus = MappingStatus.Failed;
            }
            else if (natpmpStatus == MappingStatus.Works)
            {
                externalIPAddress = null;
            }

            if (upnpStatus == MappingStatus.Failed)
            {
                OnDidFinishSearchForRouter();
            }
        }
Ejemplo n.º 11
0
        private void natpmpPortMapper_DidGetExternalIPAddress(NATPMPPortMapper sender, System.Net.IPAddress ip)
        {
            bool shouldNotify = false;

            if (natpmpStatus == MappingStatus.Trying)
            {
                natpmpStatus = MappingStatus.Works;
                mappingProtocol = MappingProtocol.NATPMP;
                shouldNotify = true;
            }

            externalIPAddress = ip;
            OnExternalIPAddressDidChange();

            if (shouldNotify)
            {
                OnDidFinishSearchForRouter();
            }
        }
Ejemplo n.º 12
0
        private void upnpPortMapper_DidGetExternalIPAddress(UPnPPortMapper sender, IPAddress ip)
        {
            DebugLog.WriteLine("upnpPortMapper_DidGetExternalIPAddress: {0}", ip);

            bool shouldNotify = false;

            if (upnpStatus == MappingStatus.Trying)
            {
                upnpStatus = MappingStatus.Works;
                mappingProtocol = MappingProtocol.UPnP;
                shouldNotify = true;
            }

            externalIPAddress = ip;
            OnExternalIPAddressDidChange();

            if (shouldNotify)
            {
                OnDidFinishSearchForRouter();
            }
        }
Ejemplo n.º 13
0
        private void TryExecuteCommand(string input)
        {
            if (input.ToLower() == "exitmapping")
            {
                Session.PopState();
                return;
            }

            var room = RoomHelper.GetPlayerRoom(Session.Player);

            if (_status == MappingStatus.Walking)
            {
                var direction = DirectionHelper.GetDirectionWord(input);

                if (string.IsNullOrEmpty(direction))
                {
                    Session.WriteLine("That's not a direction.");
                    return;
                }

                if (room.HasExit(direction))
                {
                    var commandInfo = Server.Current.CommandLookup.FindCommand(direction, Session.Player);
                    commandInfo.Command.Execute(Session, CommandContext.Create(direction));
                    return;
                }

                // set mode to request title
                Session.WriteLine("`RCreating exit, Please enter a title...");
                _status    = MappingStatus.NeedsTitle;
                _direction = direction;
            }
            else
            {
                // user is inputting a title
                var checkRoom     = Server.Current.Database.Get <Room>(input.ToLower());
                var calculatedKey = input.ToLower();

                if (checkRoom != null)
                {
                    calculatedKey = RoomHelper.GenerateKey(input);
                    Session.WriteLine("`RRoom already exists. Using title: `G{0}`R.", calculatedKey);
                }

                var newRoom = new Room()
                {
                    Area        = room.Area,
                    Description = RoomHelper.GetDefaultRoomDescription(),
                    Key         = calculatedKey.ToLower(),
                    Title       = input,
                };

                newRoom.Exits.Add(DirectionHelper.GetOppositeDirection(_direction), new RoomExit()
                {
                    IsDoor  = false,
                    IsOpen  = true,
                    LeadsTo = room.Key
                });

                Server.Current.Database.Save(newRoom);

                room.Exits.Add(_direction, new RoomExit()
                {
                    IsDoor  = false,
                    IsOpen  = true,
                    LeadsTo = newRoom.Key
                });

                Server.Current.Database.Save(room);

                var commandInfo = Server.Current.CommandLookup.FindCommand(_direction, Session.Player);
                commandInfo.Command.Execute(Session, CommandContext.Create(_direction));
                _status = MappingStatus.Walking;
            }
        }
Ejemplo n.º 14
0
 public MappingState()
 {
     _status = MappingStatus.Walking;
 }
Ejemplo n.º 15
0
 public static string GetStatusString(MappingStatus? s)
 {
     switch (s)
     {
         case MappingStatus.Mapped:
             return "OK";
         case MappingStatus.Questions:
             return "?";
         case MappingStatus.CustomSource:
             return "CS";
         case MappingStatus.Ignored:
             return "I";
         default:
             return string.Empty;
     }
 }
Ejemplo n.º 16
0
 public MapResult(MappingStatus status, string errorMessage = null)
 {
     Status       = status;
     ErrorMessage = errorMessage;
 }