Browse Source

模型文件的访问和

Jimmy 2 years ago
parent
commit
1d2dfb7a25

+ 7 - 1
fis/Managers/FileStorageManager.cs

@@ -7,11 +7,17 @@ using System.Linq;
 using fis.Log;
 using System.Text;
 using System.Threading.Tasks;
+using fis.Win.Dev.Managers.modules.storage;
 
 namespace fis.Win.Dev.Managers
 {
-    internal class FileStorageManager : FisManager,IFileStorageManager
+    internal class FileStorageManager : FisManager, IFileStorageManager
     {
+        public Ultra3DStorage Ultra3DStorage{get;}
+        public FileStorageManager()
+        {
+            Ultra3DStorage = new Ultra3DStorage("Ultra3D");
+        }
         public void Dispose()
         {
             throw new NotImplementedException();

+ 6 - 0
fis/Managers/Interfaces/IFileStorageManager.cs

@@ -1,4 +1,5 @@
 using fis.Managers.Interfaces;
+using fis.Win.Dev.Managers.modules.storage;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -23,5 +24,10 @@ namespace fis.Win.Dev.Managers.Interfaces
         /// <param name="fileName"></param>
         /// <returns></returns>
         Task<string> GetBinaryFileCache(string fileName);
+
+        /// <summary>
+        /// 三维重建数据存储器
+        /// </summary>
+        Ultra3DStorage Ultra3DStorage { get; }
     }
 }

+ 2 - 0
fis/Managers/Interfaces/IUltra3DManager.cs

@@ -13,5 +13,7 @@ namespace fis.Win.Dev.Managers.Interfaces
         Parse3DModelManager Parse3DModelManager { get; set; }
 
         Task RenderUltr3D(string surfacePath,string mdlPath);
+
+        void MDFileReady(string key);
     }
 }

+ 39 - 0
fis/Managers/Modules/Storage/FisStorageBase.cs

@@ -0,0 +1,39 @@
+using fis.Helpers;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace fis.Win.Dev.Managers.modules.storage
+{
+    internal abstract class FisStorageBase
+    {
+        private string BasePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AppStorage");
+        internal FisStorageBase(string storagePath)
+        {
+            StoragePath =Path.Combine(BasePath, storagePath);
+        }
+        public string StoragePath { get; }
+
+        internal async Task<byte[]> ReadCacheFile(string fileName)
+        {
+            var file = Path.Combine(StoragePath, fileName);
+            if (File.Exists(file))
+            {
+                return await File.ReadAllBytesAsync(file);
+               
+            }
+            else {
+                return new byte[0];
+            }
+        }
+        internal async Task<bool> WriteCacehFile(byte[] bytes,string fileName)
+        {
+            var file = Path.Combine(StoragePath, fileName);
+            await File.WriteAllBytesAsync(file, bytes);
+            return true;
+        }
+    }
+}

+ 15 - 0
fis/Managers/Modules/Storage/Ultra3DStorage.cs

@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace fis.Win.Dev.Managers.modules.storage
+{
+    internal class Ultra3DStorage : FisStorageBase
+    {
+        public Ultra3DStorage(string storageName) : base(storageName)
+        {
+        }
+    }
+}

+ 78 - 0
fis/Managers/Modules/Ultra3D/Ultra3DDownloadWorker.cs

@@ -0,0 +1,78 @@
+using fis.Managers;
+using fis.Win.Dev.Managers.Interfaces;
+using fis.Win.Dev.Managers.modules.storage;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace fis.Win.Dev.Managers.Modules.Ultra3D
+{
+    internal class Ultra3DDownloadWorker
+    {
+        public EventHandler<byte[]> MainSurfaceFileReady;
+
+        public EventHandler<byte[]>MainMDFileReady;
+
+        public List<Ultr3dUnionWorkItems> Ultra3DFileWorkItems;
+
+        private Ultra3DStorage _ultra3DStorage;
+
+        public string MainFileID { get; set; }
+
+        public Ultra3DDownloadWorker()
+        {
+            _ultra3DStorage = AppManager.Get<IFileStorageManager>().Ultra3DStorage;
+           Ultra3DFileWorkItems = new List<Ultr3dUnionWorkItems>();
+         
+        }
+
+        public void InitFileWorkItem(List<Ultra3DResourceInfo> ultra3DFileWorkInfos)
+        {
+            var mainItem = ultra3DFileWorkInfos.FirstOrDefault();
+            MainFileID = mainItem?.ID;
+            foreach (var u in ultra3DFileWorkInfos)
+            {
+                if (Ultra3DFileWorkItems.Any(x => x.FileId == u.ID))
+                {
+                    continue;
+                }
+                var surfaceItem = new Ultra3DFileWorkItem(_ultra3DStorage, mainItem.SurfaceUlr, mainItem.ID);
+                var mdItem = new Ultra3DFileWorkItem(_ultra3DStorage, mainItem.MdFileUlr, mainItem.ID);
+                var union = new Ultr3dUnionWorkItems(mainItem.ID, surfaceItem, mdItem);
+                union.SurfaceFileCanbeAccess += OnSurfaceFileReady;
+                union.ModelFileCanbeAccess += OnModelFileReady;
+                Ultra3DFileWorkItems.Add(union);
+            }        
+        }
+
+ 
+
+        public async Task AccessUltraFiles()
+        {
+            foreach (var ulr in Ultra3DFileWorkItems)
+            {
+               await ulr.StartAccessFiles();
+            }
+        }
+
+        private void OnSurfaceFileReady(object? sender,string e)
+        {
+            if (e == MainFileID)
+            {
+                var workUnion = Ultra3DFileWorkItems.FirstOrDefault(x => x.FileId == fileId);
+                MainSurfaceFileReady?.Invoke(this, workUnion.SurfaceWorkItem.FileBytes);
+            }
+        }
+
+        private void OnModelFileReady(object? sender, string e)
+        {
+            if (e == MainFileID)
+            {
+                var workUnion = Ultra3DFileWorkItems.FirstOrDefault(x => x.FileId == e);
+                MainSurfaceFileReady?.Invoke(this, workUnion.MdFileWorkItem.FileBytes);
+            }
+        }
+    }
+}

+ 154 - 0
fis/Managers/Modules/Ultra3D/Ultra3DFileWorkItem.cs

@@ -0,0 +1,154 @@
+using fis.Helpers;
+using fis.Win.Dev.Managers.modules.storage;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace fis.Win.Dev.Managers.Modules.Ultra3D
+{
+    internal class Ultra3DFileWorkItem :IDisposable
+    {
+        public string FileUri { get; set; }
+
+        public string FileID { get; set; }
+
+        public string FileName { get; set; }
+
+        public byte[] FileBytes { get; set; }
+
+        public FileAccessStatus FileAccessStatus { get; set; }
+
+        private Ultra3DStorage ultra3DStorage;
+
+        public Ultra3DFileWorkItem(Ultra3DStorage ultraStorage,string fileUri,string fileId)
+        {
+            FileUri = fileUri;
+            FileID = fileId;           
+            var fileInfo = GetVidNameFromUrl(fileUri);
+            ultra3DStorage = ultraStorage;
+            FileName = $"{fileId}{fileInfo.Item2}";
+            var filePath = Path.Combine(ultra3DStorage.StoragePath, FileName);
+            if (File.Exists(filePath))
+            {
+                FileAccessStatus = FileAccessStatus.CachedInDiskOnly;
+            }
+            else {
+                FileAccessStatus = FileAccessStatus.ReadyToDownload;
+            }
+        }
+
+        public async Task<byte[]> ReadFromDisk()
+        {
+            FileAccessStatus = FileAccessStatus.ReadingCachedFile;
+            FileBytes = await ultra3DStorage.ReadCacheFile(FileName);
+            FileAccessStatus = FileAccessStatus.ReadCacheedFileInMemory;
+            return FileBytes;
+        }
+
+        public async Task<byte[]> ReadFromNetwork()
+        {
+            FileAccessStatus = FileAccessStatus.Downloading;
+            var bytes = await HttpHelper.httpClient.GetByteArrayAsync(FileUri);
+            FileAccessStatus = FileAccessStatus.CachedInMemoryOnly;
+            return bytes;
+        }
+
+
+        public async Task<byte[]> CacheFromNetwork()
+        {
+            FileAccessStatus = FileAccessStatus.Downloading;
+            FileBytes = await HttpHelper.httpClient.GetByteArrayAsync(FileUri);
+            FileAccessStatus = FileAccessStatus.CachedInMemoryOnly;
+            return FileBytes;
+        }
+
+        public async void CacheToDisk()
+        {
+            await ultra3DStorage.WriteCacehFile(FileBytes, FileName);
+            FileAccessStatus = FileAccessStatus.CachedInMemoryAndDisk;
+        }
+
+        private Tuple<string,string> GetVidNameFromUrl(string url)
+        {
+            var fragments = url.Split('/');
+            var lastFragment = fragments.Last();
+            var fileInfo = lastFragment.Split('.');
+            var name = fileInfo.First();
+            var ext = fileInfo.Last();
+            return new Tuple<string, string>(name, ext);
+        }
+
+        public void Dispose()
+        {
+            throw new NotImplementedException();
+        }
+    }
+
+    internal enum FileAccessStatus
+    {
+        ReadyToDownload,
+        Downloading,
+        CachedInMemoryOnly,
+        CachedInMemoryAndDisk,
+        
+        ///AlreadyCached
+        CachedInDiskOnly,
+        ReadingCachedFile,
+        ReadCacheedFileInMemory
+    }
+
+    internal class Ultr3dUnionWorkItems
+    {
+        public string FileId { get; set; }
+
+        public Ultra3DFileWorkItem SurfaceWorkItem { get; set; }
+
+        public Ultra3DFileWorkItem MdFileWorkItem { get; set; }
+
+        public EventHandler<string> SurfaceFileCanbeAccess;
+
+        public EventHandler<string> ModelFileCanbeAccess;
+
+        public async Task StartAccessFiles()
+        {
+            //访问表面数据
+            if (SurfaceWorkItem.FileAccessStatus == FileAccessStatus.ReadyToDownload)
+            {
+                await SurfaceWorkItem.CacheFromNetwork();
+                SurfaceFileCanbeAccess?.Invoke(this,FileId);
+                SurfaceWorkItem.CacheToDisk();
+            }
+
+            if (SurfaceWorkItem.FileAccessStatus == FileAccessStatus.CachedInDiskOnly)
+            {
+                await SurfaceWorkItem.ReadFromDisk();
+                SurfaceFileCanbeAccess?.Invoke(this, FileId);
+            }
+            //访问模型数据
+
+            if (SurfaceWorkItem.FileAccessStatus == FileAccessStatus.ReadyToDownload)
+            {
+                await MdFileWorkItem.CacheFromNetwork();
+                ModelFileCanbeAccess?.Invoke(this, FileId);
+                MdFileWorkItem.CacheToDisk();
+            }
+
+            if (SurfaceWorkItem.FileAccessStatus == FileAccessStatus.CachedInDiskOnly)
+            {
+                await MdFileWorkItem.ReadFromDisk();
+                ModelFileCanbeAccess?.Invoke(this, FileId);
+            }
+        }
+
+        public Ultr3dUnionWorkItems(string fileId, Ultra3DFileWorkItem surface, Ultra3DFileWorkItem model)
+        {
+            FileId = fileId;
+            SurfaceWorkItem=surface;
+            MdFileWorkItem = model;
+        }
+    }
+
+}

+ 17 - 0
fis/Managers/Modules/Ultra3D/Ultra3DResourceInfo.cs

@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace fis.Win.Dev.Managers.Modules.Ultra3D
+{
+    internal class Ultra3DResourceInfo
+    {
+        public string ID { get; set; }
+
+        public string SurfaceUlr { get; set; }
+
+        public string MdFileUlr { get; set; }
+    }
+}

+ 22 - 5
fis/Managers/Ultra3DManager.cs

@@ -1,6 +1,7 @@
 using fis.Log;
 using fis.Win.Dev.Helpers;
 using fis.Win.Dev.Managers.Interfaces;
+using fis.Win.Dev.Managers.Modules.Ultra3D;
 using fis.Win.Dev.Utilities;
 using fis.Win.Dev.Win.Ultra3D.manager;
 using fis.Win.Dev.Win.Ultra3D.Utils;
@@ -17,12 +18,25 @@ namespace fis.Win.Dev.Managers
     internal class Ultra3DManager : IUltra3DManager
     {
         public Parse3DModelManager Parse3DModelManager { get; set; }
-
+        private Ultra3DDownloadWorker _ultr3DFileWorker;
         internal Ultra3DManager()
         {
-            Parse3DModelManager = new Parse3DModelManager();          
+            Parse3DModelManager = new Parse3DModelManager();
+            _ultr3DFileWorker = new Ultra3DDownloadWorker();
+            _ultr3DFileWorker.MainSurfaceFileReady += OnMainSurfaceReady;
+            _ultr3DFileWorker.MainMDFileReady += OnMainMDFileReady;
+        }
+
+        private void OnMainMDFileReady(object? sender, byte[] e)
+        {
+            throw new NotImplementedException();
+        }
+
+        private void OnMainSurfaceReady(object? sender, byte[] e)
+        {
+            throw new NotImplementedException();
         }
-        
+
         public void Dispose()
         {
             throw new NotImplementedException();
@@ -33,8 +47,6 @@ namespace fis.Win.Dev.Managers
             Parse3DModelManager.ParseSurface(surfacePath);
             Parse3DModelManager.ParseMdl(mdlPath,CarotidScanType.CarotidLeft,CarotidScanDirection.TopToBottom);
             await LoadSurfaceFile();
-            var callString = PlatFormHelper.GetMethodStr("externalNotification", TargetMethodName.MdlFileLoaded,new List<string>());
-            AvaloniaCefBrowserHelper.browser?.ExecuteJavaScript(callString, null, 0);
         }
 
         private async Task LoadSurfaceFile()
@@ -72,5 +84,10 @@ namespace fis.Win.Dev.Managers
             }
         }
 
+        public void MDFileReady(string key)
+        {
+            var callString = PlatFormHelper.GetMethodStr("externalNotification", TargetMethodName.MdlFileLoaded, new List<string>());
+            AvaloniaCefBrowserHelper.browser?.ExecuteJavaScript(callString, null, 0);
+        }
     }
 }

+ 9 - 1
fis/Utilities/FisBrowserScriptObject.cs

@@ -23,6 +23,7 @@ namespace fis.Win.Dev.Utilities
         TextBlock _title;
         private string _lastClipData;
         private readonly SliceHelper _sliceHelper;
+        public event EventHandler<bool> MeasureStateChanged;
         internal FisBrowserScriptObject(TextBlock title, IPlatformService platformService)
         {
             _title = title;
@@ -84,6 +85,14 @@ namespace fis.Win.Dev.Utilities
             return JsonConvert.SerializeObject(new ClipPlanePoints(null, 0));
         }
 
+        /// <summary>
+        /// Measure state is changed by wheter has active mesh or not.
+        /// </summary>
+        /// <param name="e">True indicate any mesh is actived false indicate not.</param>
+        public void MeshActiveStatusChanged(bool e)
+        {
+            MeasureStateChanged?.Invoke(this, e);
+        }
 
         private string CaculateClipPlaneData(string cefInputDataStr)
         {
@@ -94,7 +103,6 @@ namespace fis.Win.Dev.Utilities
 
                 if (_sliceHelper.Calculate3DSliceImage(cefInputData, clipImagePath))
                 {
-
                     var para1 = _sliceHelper.GetModelSliceEndPoint3D().ToArray();
                     var para2 = _sliceHelper.GetModelSliceEndPointPolar2D().ToArray();
                     var para3 = _sliceHelper.CurrentClipImage;