-
-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathTransferFunctionUpgraderWindow.cs
More file actions
145 lines (127 loc) · 5.79 KB
/
Copy pathTransferFunctionUpgraderWindow.cs
File metadata and controls
145 lines (127 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System.IO;
using UnityEditor;
using UnityEngine;
namespace UnityVolumeRendering
{
public class TransferFunctionUpgraderWindow : EditorWindow
{
private enum TFConversionState
{
ReadyToImport,
ReadyToConvert,
ReadyToSave
}
private TransferFunction transferFunction = null;
private VolumeRenderedObject targetObject = null;
private string infoText = "";
private string errorText = "";
private string tfFilePath = "";
private TFConversionState state = TFConversionState.ReadyToImport;
public static void ShowWindow()
{
TransferFunctionUpgraderWindow wnd = new TransferFunctionUpgraderWindow();
wnd.Show();
}
private void OnGUI()
{
if (state == TFConversionState.ReadyToImport)
{
targetObject = null;
transferFunction = null;
}
GUIStyle headerStyle = new GUIStyle(EditorStyles.label);
headerStyle.fontSize = 20;
GUIStyle groupHeaderStyle = new GUIStyle(EditorStyles.label);
groupHeaderStyle.fontSize = 16;
groupHeaderStyle.fontStyle = FontStyle.Bold;
GUIStyle errorStyle = new GUIStyle(EditorStyles.label);
errorStyle.normal.textColor = Color.red;
GUIStyle infoStyle = new GUIStyle(EditorStyles.label);
infoStyle.wordWrap = true;
EditorGUILayout.LabelField("Transfer function upgrader tool", headerStyle);
EditorGUILayout.Space();
if (infoText != "")
{
EditorGUILayout.LabelField(infoText);
EditorGUILayout.Space();
}
if (errorText != "")
{
EditorGUILayout.LabelField(errorText, errorStyle);
EditorGUILayout.Space();
}
GUILayout.Label("Import", groupHeaderStyle);
if (GUILayout.Button("Load transfer function"))
{
tfFilePath = EditorUtility.OpenFilePanel("Load transfer function", "", "tf");
transferFunction = TransferFunctionDatabase.LoadTransferFunction(tfFilePath);
if (transferFunction == null)
errorText = "Invalid transfer function";
else
{
if (transferFunction.relativeScale)
{
infoText = "Old transfer function with relative scale detected. Please convert it.";
state = TFConversionState.ReadyToConvert;
}
else
{
infoText = "Transfer function already up-to-date. Nothing was done.";
}
}
}
EditorGUILayout.Space();
if (state == TFConversionState.ReadyToConvert || state == TFConversionState.ReadyToSave)
{
GUILayout.Label("Select target object (dataset)", groupHeaderStyle);
targetObject = EditorGUILayout.ObjectField("Target object", targetObject, typeof(VolumeRenderedObject), true) as VolumeRenderedObject;
if (targetObject != null && targetObject.dataset != null)
{
ConvertToAbsoluteTF(transferFunction, targetObject.dataset);
infoText = "The transfer function has been converted. Please save it.";
state = TFConversionState.ReadyToSave;
}
GUILayout.Label("Since the transfer function was using data values relative to a dataset," +
"we need a reference to this dataset in order to convert the values to Hounsfield scale, which is now the default.", infoStyle);
EditorGUILayout.Space();
}
if (state == TFConversionState.ReadyToSave)
{
GUILayout.Label("Save converted dataset", groupHeaderStyle);
if (targetObject == null || transferFunction == null)
{
state = TFConversionState.ReadyToImport;
return;
}
if (GUILayout.Button("Save transfer function"))
{
string filepath = EditorUtility.SaveFilePanel("Save transfer function", Path.GetDirectoryName(tfFilePath), Path.GetFileName(tfFilePath), "tf");
TransferFunctionDatabase.SaveTransferFunction(transferFunction, filepath);
infoText = "Transfer function saved. You can now import a new one.";
state = TFConversionState.ReadyToImport;
}
}
}
private void ConvertToAbsoluteTF(TransferFunction transferFunction, VolumeDataset dataset)
{
if (transferFunction.relativeScale)
{
float minValue = dataset.GetMinDataValue();
float maxValue = dataset.GetMaxDataValue();
for (int i = 0; i < transferFunction.colourControlPoints.Count; i++)
{
TFColourControlPoint point = transferFunction.colourControlPoints[i];
point.dataValue = Mathf.Lerp(minValue, maxValue, point.dataValue);
transferFunction.colourControlPoints[i] = point;
}
for (int i = 0; i < transferFunction.alphaControlPoints.Count; i++)
{
TFAlphaControlPoint point = transferFunction.alphaControlPoints[i];
point.dataValue = Mathf.Lerp(minValue, maxValue, point.dataValue);
transferFunction.alphaControlPoints[i] = point;
}
transferFunction.relativeScale = false;
}
}
}
}