Example #1
0
        /// <summary>
        /// Update the surface
        /// </summary>
        /// <param name="handler">The handler to receive the surface information</param>
        /// <param name="change">The surface change</param>
        /// <param name="surface">The surface to update</param>
        /// <param name="trianglesPerCubicMeter">The max triangles per cubic meter</param>
        /// <param name="surfaceInfo">The surface info</param>
        /// <param name="surfaceOptions">The mesh options</param>
        private async void UpdateSurface(OnSurfaceChangedHandler handler, SurfaceChange change, SpatialMappingSurfaceInternal surface, float trianglesPerCubicMeter, SpatialSurfaceInfo surfaceInfo, SpatialSurfaceMeshOptions surfaceOptions)
        {
            if (surface.IsProcessing)
            {
                return;
            }

            try
            {
                surface.IsProcessing = true;

                // Generate the mesh from the MixedReality surface info
                SpatialSurfaceMesh surfaceMesh = await surfaceInfo.TryComputeLatestMeshAsync(trianglesPerCubicMeter, surfaceOptions);

                if (surfaceMesh != null && surface.UpdateTime < surfaceMesh.SurfaceInfo.UpdateTime)
                {
                    // Update the surface mesh
                    surface.UpdateSurfaceMesh(surfaceMesh);

                    if (handler != null)
                    {
                        handler(surface.Id, surface, change, surface.UpdateTime);
                    }
                }
            }
            finally
            {
                surface.IsProcessing = false;
            }
        }
        /// <summary>
        /// Process all spatial surfaces
        /// </summary>
        /// <param name="handler">The handler to receive the surface information</param>
        /// <param name="ignorePrevious">Ignore previous surfaces</param>
        public void UpdateSurfaces(OnSurfaceChangedHandler handler, bool ignorePrevious = false)
        {
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }

            if (!this.IsConnected)
            {
                throw new InvalidOperationException("MixedReality is not available");
            }

            this.spatialMappingManager.UpdateSurfaces(handler, ignorePrevious);
        }
Example #3
0
        /// <summary>
        /// Process all spatial surfaces
        /// </summary>
        /// <param name="handler">The handler to receive the surface information</param>
        /// <param name="ignorePrevious">Ignore previous surfaces</param>
        public void UpdateSurfaces(OnSurfaceChangedHandler handler, bool ignorePrevious)
        {
            ////if (!this.pendingChanges && !ignorePrevious)
            ////{
            ////    return;
            ////}

            this.pendingChanges = false;

            lock (this)
            {
                if (this.updateSurfacesTask != null)
                {
                    return;
                }

                this.updateSurfacesTask = System.Threading.Tasks.Task.Run(() =>
                {
                    try
                    {
                        this.surfaceMeshOptions.IncludeVertexNormals = this.ObtainNormals;

                        var surfaceCollection = this.surfaceObserver.GetObservedSurfaces();

                        foreach (var pair in surfaceCollection)
                        {
                            // Gets the MixedReality surface
                            var id          = pair.Key;
                            var surfaceInfo = pair.Value;
                            SpatialMappingSurface surface;
                            SurfaceChange change = SurfaceChange.Updated;

                            if (!this.surfaces.TryGetValue(id, out surface))
                            {
                                surface = new SpatialMappingSurfaceInternal()
                                {
                                    Id = id
                                };

                                this.surfaces.Add(id, surface);
                                change = SurfaceChange.Added;
                            }

                            if (surface.UpdateTime < surfaceInfo.UpdateTime)
                            {
                                this.UpdateSurface(handler, change, surface as SpatialMappingSurfaceInternal, this.TrianglesPerCubicMeter, surfaceInfo, this.surfaceMeshOptions);
                            }
                            else if (ignorePrevious)
                            {
                                if (handler != null)
                                {
                                    handler(id, surface, SurfaceChange.Removed, surface.UpdateTime);
                                }
                            }
                        }

                        var surafacesToRemove = this.surfaces.Where(id => !surfaceCollection.ContainsKey(id.Key)).ToList();
                        foreach (var pair in surafacesToRemove)
                        {
                            var id      = pair.Key;
                            var surface = pair.Value;

                            this.surfaces.Remove(id);
                            surface.Dispose();

                            if (handler != null)
                            {
                                handler(id, surface, SurfaceChange.Removed, surface.UpdateTime);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e);
                    }

                    this.updateSurfacesTask = null;
                });
            }
        }