-
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SimpleITK: DICOM JPEG, NRRD, NIFTI (Windows only) (#95)
* SimpleITK-based DICOM importer. * Cleanup: Importer interfaces and factory. * NRRD and NIFTI import (using SimpleITK) * Added script for exporting .unitypackage * Updated documentation. * Don't use EditorDatasetImporter for RAW import. * .gitattributes
- Loading branch information
Showing
54 changed files
with
1,320 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DO NOT REMOVE. This file simply exists so we can get the path of the UnityVolumeRendering project folder (maybe be added as a subfolder to another project). | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
using UnityEditor; | ||
using UnityEngine; | ||
using System.IO.Compression; | ||
|
||
namespace UnityVolumeRendering | ||
{ | ||
/// <summary> | ||
/// Manager for the SimpleITK integration. | ||
/// Since SimpleITK is a native library that requires binaries to be built for your target platform, | ||
/// SimpleITK will be disabled by default and can be enabled through this class. | ||
/// The binaries will be downloaded automatically. | ||
/// </summary> | ||
public class SimpleITKManager | ||
{ | ||
private static string SimpleITKDefinition = "UVR_USE_SIMPLEITK"; | ||
|
||
public static bool IsSITKEnabled() | ||
{ | ||
BuildTarget target = EditorUserBuildSettings.activeBuildTarget; | ||
BuildTargetGroup group = BuildPipeline.GetBuildTargetGroup(target); | ||
|
||
HashSet<string> defines = new HashSet<string>(PlayerSettings.GetScriptingDefineSymbolsForGroup(group).Split(';')); | ||
return defines.Contains(SimpleITKDefinition); | ||
} | ||
|
||
public static void EnableSITK(bool enable) | ||
{ | ||
if (!HasDownloadedBinaries()) | ||
{ | ||
EditorUtility.DisplayDialog("Missing SimpleITK binaries", "You need to download the SimpleITK binaries before you can enable SimpleITK.", "Ok"); | ||
return; | ||
} | ||
|
||
// Enable the UVR_USE_SIMPLEITK preprocessor definition for standalone target | ||
List<BuildTargetGroup> buildTargetGroups = new List<BuildTargetGroup> (){ BuildTargetGroup.Standalone }; | ||
foreach (BuildTargetGroup group in buildTargetGroups) | ||
{ | ||
List<string> defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(group).Split(';').ToList(); | ||
defines.Remove(SimpleITKDefinition); | ||
if (enable) | ||
defines.Add(SimpleITKDefinition); | ||
PlayerSettings.SetScriptingDefineSymbolsForGroup(group, String.Join(";", defines)); | ||
} | ||
|
||
// Save project and recompile scripts | ||
AssetDatabase.SaveAssets(); | ||
AssetDatabase.Refresh(); | ||
#if UNITY_2019_3_OR_NEWER | ||
UnityEditor.Compilation.CompilationPipeline.RequestScriptCompilation(); | ||
#endif | ||
} | ||
|
||
public static bool HasDownloadedBinaries() | ||
{ | ||
string binDir = GetBinaryDirectoryPath(); | ||
return Directory.Exists(binDir) && Directory.GetFiles(binDir).Length > 0; // TODO: Check actual files? | ||
} | ||
|
||
public static void DownloadBinaries() | ||
{ | ||
string extractDirPath = GetBinaryDirectoryPath(); | ||
string zipPath = Path.Combine(Directory.GetParent(extractDirPath).FullName, "SimpleITK.zip"); | ||
if (HasDownloadedBinaries()) | ||
{ | ||
if (!EditorUtility.DisplayDialog("Download SimpleITK binaries", "SimpleITK has already been downloaded. Do you want to delete it and download again?", "Yes", "No")) | ||
{ | ||
return; | ||
} | ||
} | ||
|
||
EditorUtility.DisplayProgressBar("Downloading SimpleITK", "Downloading SimpleITK binaries.", 0); | ||
|
||
// Downlaod binaries zip | ||
using (var client = new WebClient()) | ||
{ | ||
string downloadURL = "https://sourceforge.net/projects/simpleitk/files/SimpleITK/1.2.4/CSharp/SimpleITK-1.2.4-CSharp-win64-x64.zip/download"; | ||
client.DownloadFile(downloadURL, zipPath); | ||
|
||
EditorUtility.DisplayProgressBar("Downloading SimpleITK", "Downloading SimpleITK binaries.", 70); | ||
|
||
if (!File.Exists(zipPath)) | ||
{ | ||
Debug.Log(zipPath); | ||
EditorUtility.DisplayDialog("Error downloadig SimpleITK binaries.", "Failed to download SimpleITK binaries. Please check your internet connection.", "Close"); | ||
Debug.Log($"Failed to download SimpleITK binaries. You can also try to manually download from {downloadURL} and extract it to some folder inside the Assets folder."); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
ExtractZip(zipPath, extractDirPath); | ||
} | ||
catch (Exception ex) | ||
{ | ||
string errorString = $"Extracting binaries failed with error: {ex.Message}\n" | ||
+ $"Please try downloading the zip from: {downloadURL}\nAnd extract it somewhere in the Assets folder.\n\n" | ||
+ "The download URL can be copied from the error log (console)."; | ||
Debug.LogError(ex.ToString()); | ||
Debug.LogError(errorString); | ||
EditorUtility.DisplayDialog("Failed to extract binaries.", errorString, "Close"); | ||
} | ||
} | ||
|
||
File.Delete(zipPath); | ||
|
||
EditorUtility.ClearProgressBar(); | ||
} | ||
|
||
private static void ExtractZip(string zipPath, string extractDirPath) | ||
{ | ||
// Extract zip | ||
using (FileStream zipStream = new FileStream(zipPath, FileMode.Open)) | ||
{ | ||
using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Update)) | ||
{ | ||
if (!Directory.Exists(extractDirPath)) | ||
Directory.CreateDirectory(extractDirPath); | ||
|
||
foreach (ZipArchiveEntry entry in archive.Entries) | ||
{ | ||
if (entry.Name != "" && !entry.Name.EndsWith("/")) | ||
{ | ||
string destFilePath = Path.Combine(extractDirPath, entry.Name); | ||
//TextAsset destAsset = new TextAsset("abc"); | ||
//AssetDatabase.CreateAsset(destAsset, extractDirRelPath + "/" + entry.Name); | ||
Stream inStream = entry.Open(); | ||
|
||
using (Stream outStream = File.OpenWrite(destFilePath)) | ||
{ | ||
inStream.CopyTo(outStream); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static string GetBinaryDirectoryPath() | ||
{ | ||
string dataPath = Application.dataPath; | ||
foreach (string file in Directory.EnumerateFiles(Application.dataPath, "*.*", SearchOption.AllDirectories)) | ||
{ | ||
// Search for magic file stored in Assets directory. | ||
// This is necessary for cases where the UVR plugin is stored in a subfolder (thatæs the case for the asset store version) | ||
if (Path.GetFileName(file) == "DONOTREMOVE-PathSearchFile.txt") | ||
{ | ||
dataPath = Path.GetDirectoryName(file); | ||
} | ||
} | ||
return Path.Combine(dataPath, "3rdparty", "SimpleITK"); // TODO: What is UVR is in a subfolder? | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.