private IEnumerator SaveReconstructionCoroutine(string environmentProfileName, bool saveChangesInProfile)
        {
            Validate();

            if (_currentState == ReconstructionState.Scanning && _pause)
            {
                IEnvironmentProfile environmentProfile = GetEnvironmentByName(environmentProfileName);

                int    index = 0;
                string path  = _environmentProfileRepository.GetPath(environmentProfile.Id);

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                List <string> meshesName = new List <string>();
                List <Thread> threads    = new List <Thread>();
                _saveFinished = false;

                // Delete old reconstruction meshes
                DeleteReconstructionMeshFiles(environmentProfileName, false);

                foreach (MeshFilter meshFilter in _reconstruction.GetComponentsInChildren <MeshFilter>())
                {
                    Mesh      mesh      = meshFilter.mesh;
                    Vector3[] vertices  = mesh.vertices;
                    int[]     triangles = mesh.triangles;

                    string meshName = Path.Combine(path, (++index).ToString());
                    meshesName.Add(meshName + ".obj");

                    Thread thread = new Thread(() => _modelFileManipulator.SaveMeshToFile(meshName, vertices, triangles));
                    thread.Start();
                    threads.Add(thread);
                    yield return(null);
                }

                Thread invokeOnFinished = new Thread(() => ScanThreads(threads, out _saveFinished));
                invokeOnFinished.Start();
                yield return(new WaitWhile(() => !_saveFinished));

                _environmentProfileRepository.SetMeshes(environmentProfile.Id, meshesName);
                if (saveChangesInProfile)
                {
                    yield return(StartCoroutine(SaveReconstructionChangesInProfile()));

                    _reconstructionSaved.Invoke();
                }
                else
                {
                    // Invoked only after the meshes are completly saved on disk
                    _reconstructionSaved.Invoke();
                }
            }
            else
            {
                Debug.LogWarning("Trying to save a mesh of a non initialized or ongoing reconstruction.");
            }
        }
        private IEnumerator LoadReconstructionCoroutine(string environmentProfileName)
        {
            Validate();

            IEnvironmentProfile environmentProfile   = GetEnvironmentByName(environmentProfileName);
            GameObject          loadedReconstruction = new GameObject("LoadedReconstruction");

            List <MeshData> meshesData = new List <MeshData>();
            List <Thread>   threads    = new List <Thread>();

            _loadFinished = false;

            for (int i = 0; i < environmentProfile.Meshes.Count; i++)
            {
                string meshPath = environmentProfile.Meshes[i];
                Thread thread   = new Thread(
                    () =>
                {
                    lock (meshesData)
                    {
                        meshesData.Add(_modelFileManipulator.LoadMeshFromFile(meshPath));
                    }
                });
                thread.Start();
                threads.Add(thread);
                yield return(null);
            }

            Thread invokeOnFinished = new Thread(() => ScanThreads(threads, out _loadFinished));

            invokeOnFinished.Start();

            // wait for the end of the loading threads
            yield return(new WaitWhile(() => !_loadFinished));

            foreach (MeshData meshData in meshesData)
            {
                GameObject newMesh    = new GameObject(meshData.Name);
                MeshFilter meshFilter = newMesh.AddComponent <MeshFilter>();
                meshFilter.mesh.vertices  = meshData.Vertices.ToArray();
                meshFilter.mesh.triangles = meshData.Triangles.ToArray();
                newMesh.AddComponent <MeshRenderer>().material = _scanningMaterial;

                Rigidbody rigidbody = newMesh.AddComponent <Rigidbody>();
                rigidbody.isKinematic = true;
                rigidbody.useGravity  = false;
                newMesh.AddComponent <MeshCollider>();
                newMesh.transform.SetParent(loadedReconstruction.transform);
                meshFilter.mesh.RecalculateNormals();
            }

            // Remove object from the scene if there is no saved mesh for this environment profile
            if (loadedReconstruction.transform.childCount == 0)
            {
                Destroy(loadedReconstruction);
            }

            _loadedReconstruction = loadedReconstruction;
            _reconstructionLoaded.Invoke(loadedReconstruction);
        }
Пример #3
0
        /// <summary>
        /// Reads the list of environment profiles.
        /// </summary>
        public void Read()
        {
            Debug.Assert(_iOStream != null);
            Debug.Assert(_profileParser != null);

            SelectedEnvironment = null;
            string content = _iOStream.Read();

            _environmentProfileCollection = content == null ? new EnvironmentProfileCollection() : _profileParser.DeserializeEnvironmentProfiles(content);
        }
        private IEnvironmentProfile GetEnvironmentByName(string name)
        {
            IEnvironmentProfile environmentProfile = string.IsNullOrEmpty(name) ? _environmentProfileRepository.SelectedEnvironment : _environmentProfileRepository.FindByName(name);

            if (environmentProfile == null)
            {
                throw new KeyNotFoundException("There is no environment specified by name " + name);
            }

            return(environmentProfile);
        }
Пример #5
0
 private void SelectEnvironment(IEnvironmentProfile environmentProfile)
 {
     _slamChecker.TryLocalizeMap(environmentProfile.MapName, (ok) =>
     {
         if (ok)
         {
             _environmentProfileRepository.Select(environmentProfile.Id);
         }
         _environmentSelected.Invoke(ok ? EnvironmentSelectionResultTypeEvent.EnvironmentSelectionResultType.SelectedEnvironment : EnvironmentSelectionResultTypeEvent.EnvironmentSelectionResultType.NewEnvironment);
     });
 }
Пример #6
0
        /// <summary>
        /// Whether the environment profile specified by id is valid or not.
        /// </summary>
        /// <param name="id">The environment profile id.</param>
        /// <returns><c>true</c> if the environment profile specified by id is valid; otherwise, <c>false</c>.</returns>
        public bool Verify(int id)
        {
            ValidateProfiles();
            IEnvironmentProfile profile = _environmentProfileCollection.TryGetById(id);

            if (profile != null)
            {
                return(_profileVerifier.IsValid(profile));
            }
            return(false);
        }
Пример #7
0
        /// <summary>
        /// Selects the environment profile defined by Id.
        /// </summary>
        /// <param name="id">The id of the environment profile to be selected.</param>
        public void Select(int id)
        {
            ValidateProfiles();
            EnvironmentProfile profile = _environmentProfileCollection.GetById(id);

            if (profile != null)
            {
                profile.UpdateLastTimeUsed();
                SelectedEnvironment = profile;
            }
        }
        protected override void Initialize()
        {
            IEnvironmentProfile currentEnvironment = _environmentProfileRepository.SelectedEnvironment;

            if (currentEnvironment != null)
            {
                _metaReconstruction.ReconstructionSaved.AddListener(FinishSave);
                _metaReconstruction.SaveReconstruction(currentEnvironment.Name, false);
            }
            else
            {
                Finish();
            }
        }
Пример #9
0
        /// <summary>
        /// Deletes the environment profile with the given id.
        /// </summary>
        /// <param name="id">The id of the environment profile to be deleted.</param>
        public void Delete(int id)
        {
            ValidateProfiles();
            EnvironmentProfile profile = _environmentProfileCollection.GetById(id);

            if (profile != null)
            {
                if (profile == SelectedEnvironment)
                {
                    SelectedEnvironment = null;
                }

                _environmentProfileCollection.Remove(id);
            }
        }
Пример #10
0
        public IEnvironmentProfile ForceCreation(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            ValidateProfiles();
            ValidateName(name);

            EnvironmentProfile newEnvironment = new EnvironmentProfile(GetNewId(), name);

            _environmentProfileCollection.Add(newEnvironment);
            SelectedEnvironment = newEnvironment;
            return(newEnvironment);
        }
Пример #11
0
        protected override void Initialize()
        {
            IEnvironmentProfile environmentProfile = _environmentProfileRepository.SelectedEnvironment;

            if (_slamLocalizer.IsFinished && environmentProfile != null)
            {
                string mapName = environmentProfile.MapName;
                if (string.IsNullOrEmpty(mapName))
                {
                    mapName = string.Format("{0}\\{1}", _environmentProfileRepository.GetPath(environmentProfile.Id), environmentProfile.Id);
                    _environmentProfileRepository.SetMapName(environmentProfile.Id, mapName);
                }
                _slamLocalizer.SaveSlamMap(mapName);
            }

            Finish();
        }
        /// <summary>
        /// Delete meshes related to the given map.
        /// </summary>
        /// <param name="profileName">The slam map name</param>
        /// <param name="saveChangesInProfile">Whether to save changes in profile or not</param>
        public void DeleteReconstructionMeshFiles(string environmentProfileName, bool saveChangesInProfile = true)
        {
            Validate();
            IEnvironmentProfile environmentProfile = GetEnvironmentByName(environmentProfileName);

            for (int i = 0; i < environmentProfile.Meshes.Count; i++)
            {
                string meshPath = environmentProfile.Meshes[i];
                if (File.Exists(meshPath))
                {
                    File.Delete(meshPath);
                }
            }

            _environmentProfileRepository.SetMeshes(environmentProfile.Id, new List <string>());
            if (saveChangesInProfile)
            {
                StartCoroutine(SaveReconstructionChangesInProfile());
            }
        }
        private void DeleteDefaultEnvironments()
        {
            IEnvironmentProfile defaultEnvironment = _environmentProfileRepository.FindByName(EnvironmentConstants.DefaultEnvironmentName);

            if (defaultEnvironment == null)
            {
                return;
            }

            string environmentPath = _environmentProfileRepository.GetPath(defaultEnvironment.Id);

            _environmentProfileRepository.Delete(defaultEnvironment.Id);

            if (Directory.Exists(environmentPath))
            {
                Directory.Delete(environmentPath, true);
            }

            _environmentProfileRepository.Save();
        }
        protected override void Initialize()
        {
            IEnvironmentProfile defaultEnvironment = _environmentProfileRepository.GetDefault();

            if (defaultEnvironment == null)
            {
                Finish();
                return;
            }

            string environmentPath = _environmentProfileRepository.GetPath(defaultEnvironment.Id);

            _environmentProfileRepository.Delete(defaultEnvironment.Id);

            if (Directory.Exists(environmentPath))
            {
                Directory.Delete(environmentPath, true);
            }

            Finish();
        }
Пример #15
0
 private void Read()
 {
     _environmentProfileRepository.Read();
     _defaultEnvironmentProfile = _environmentProfileRepository.GetDefault();
 }
 /// <summary>
 /// Whether the environment profile is valid or not.
 /// </summary>
 /// <param name="environmentProfile"></param>
 /// <returns><c>true</c> if the environment profile is valid; otherwise, <c>false</c>.</returns>
 public bool IsValid(IEnvironmentProfile environmentProfile)
 {
     return(AreMeshesValid(environmentProfile.Meshes) && IsFileValid(environmentProfile.MapName + ".mmf"));
 }