-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathDvhLookups.cs
347 lines (298 loc) · 15.1 KB
/
DvhLookups.cs
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
////////////////////////////////////////////////////////////////////////////////
// DvhLookups.cs
//
// Simple DVH Lookup tool.
//
// Applies to: ESAPI v11 and later.
//
// Copyright (c) 2015 Varian Medical Systems, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Linq;
using System.Text;
using System.Windows;
using System.Collections.Generic;
using VMS.TPS.Common.Model.API;
using VMS.TPS.Common.Model.Types;
using System.Windows.Controls;
namespace VMS.TPS
{
public static class DvhExtensions
{
public static DoseValue GetDoseAtVolume(this PlanningItem pitem, Structure structure, double volume, VolumePresentation volumePresentation, DoseValuePresentation requestedDosePresentation)
{
if (pitem is PlanSetup)
{
return ((PlanSetup)pitem).GetDoseAtVolume(structure, volume, volumePresentation, requestedDosePresentation);
}
else
{
if (requestedDosePresentation != DoseValuePresentation.Absolute)
throw new ApplicationException("Only absolute dose supported for Plan Sums");
DVHData dvh = pitem.GetDVHCumulativeData(structure, DoseValuePresentation.Absolute, volumePresentation, 0.001);
return DvhExtensions.DoseAtVolume(dvh, volume);
}
}
public static double GetVolumeAtDose(this PlanningItem pitem, Structure structure, DoseValue dose, VolumePresentation requestedVolumePresentation)
{
if (pitem is PlanSetup)
{
return ((PlanSetup)pitem).GetVolumeAtDose(structure, dose, requestedVolumePresentation);
}
else
{
DVHData dvh = pitem.GetDVHCumulativeData(structure, DoseValuePresentation.Absolute, requestedVolumePresentation, 0.001);
return DvhExtensions.VolumeAtDose(dvh, dose.Dose);
}
}
public static DoseValue DoseAtVolume(DVHData dvhData, double volume)
{
if (dvhData == null || dvhData.CurveData.Count() == 0)
return DoseValue.UndefinedDose();
double absVolume = dvhData.CurveData[0].VolumeUnit == "%" ? volume * dvhData.Volume * 0.01 : volume;
if (volume < 0.0 || absVolume > dvhData.Volume)
return DoseValue.UndefinedDose();
DVHPoint[] hist = dvhData.CurveData;
for (int i = 0; i < hist.Length; i++)
{
if (hist[i].Volume < volume)
return hist[i].DoseValue;
}
return DoseValue.UndefinedDose();
}
public static double VolumeAtDose(DVHData dvhData, double dose)
{
if (dvhData == null)
return Double.NaN;
DVHPoint[] hist = dvhData.CurveData;
int index = (int)(hist.Length * dose / dvhData.MaxDose.Dose);
if (index < 0 || index > hist.Length)
return 0.0;//Double.NaN;
else
return hist[index].Volume;
}
}
public class Script
{
public Script()
{
}
//---------------------------------------------------------------------------------------------
public void Execute(ScriptContext context, Window window)
{
PlanSetup plan = context.PlanSetup;
PlanSum psum = context.PlanSumsInScope.FirstOrDefault();
if (plan == null && psum == null)
return;
window.Closing += new System.ComponentModel.CancelEventHandler(OnWindowClosing);
window.Background = System.Windows.Media.Brushes.Cornsilk;
window.Height = 120;
window.Width = 1024;
SelectedPlanningItem = plan != null ? (PlanningItem)plan : (PlanningItem)psum;
// Plans in plansum can have different structuresets but here we only use structureset to allow chosing one structure
SelectedStructureSet = plan != null ? plan.StructureSet : psum.PlanSetups.First().StructureSet;
window.Title = "DVH Lookups for " + SelectedPlanningItem.Id + " / " + SelectedStructureSet.Id;
if (SelectedPlanningItem.Dose == null)
return;
InitializeUI(window);
}
PlanningItem SelectedPlanningItem { get; set; }
StructureSet SelectedStructureSet { get; set; }
Structure SelectedStructure { get; set; }
bool m_closing = false;
//---------------------------------------------------------------------------------------------
void OnWindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
m_closing = true;
}
//---------------------------------------------------------------------------------------------
void InitializeUI(Window window)
{
StackPanel rootPanel = new StackPanel();
rootPanel.Orientation = Orientation.Horizontal;
// Structure selection and info
{
GroupBox structureGroup = new GroupBox();
structureGroup.Header = "Structure";
rootPanel.Children.Add(structureGroup);
StackPanel structurePanel = new StackPanel();
structurePanel.Orientation = Orientation.Horizontal;
ComboBox structureCombo = new ComboBox();
structureCombo.ItemsSource = SelectedStructureSet.Structures;
structureCombo.SelectionChanged += new SelectionChangedEventHandler(OnComboSelectionChanged);
structureCombo.MinWidth = 160.0;
Label volumeLabel = new Label();
volumeLabel.Content = "Volume (cm3)";
m_structureVolume.Height = 25.0;
m_structureVolume.VerticalAlignment = System.Windows.VerticalAlignment.Center;
structureGroup.Content = structurePanel;
structurePanel.Children.Add(structureCombo);
structurePanel.Children.Add(volumeLabel);
structurePanel.Children.Add(m_structureVolume);
m_absDoseCheckbox.Content = "AbsDose";
m_absDoseCheckbox.VerticalAlignment = VerticalAlignment.Center;
m_absDoseCheckbox.Checked += new RoutedEventHandler(CheckBoxChanged);
m_absDoseCheckbox.Unchecked += new RoutedEventHandler(CheckBoxChanged);
if (SelectedPlanningItem is PlanSum)
{
// only absolute dose for plansums
m_absDoseCheckbox.IsChecked = true;
m_absDoseCheckbox.IsEnabled = false;
}
structurePanel.Children.Add(m_absDoseCheckbox);
m_absVolCheckbox.Content = "AbsVol";
m_absVolCheckbox.VerticalAlignment = VerticalAlignment.Center;
m_absVolCheckbox.Checked += new RoutedEventHandler(CheckBoxChanged);
m_absVolCheckbox.Unchecked += new RoutedEventHandler(CheckBoxChanged);
structurePanel.Children.Add(m_absVolCheckbox);
}
// DVH lookup controls
{
GroupBox dvhGroup = new GroupBox();
dvhGroup.Header = "DVH";
rootPanel.Children.Add(dvhGroup);
StackPanel dvhPanel = new StackPanel();
dvhPanel.Orientation = Orientation.Horizontal;
m_volumeTextBox.TextChanged += new TextChangedEventHandler(OnInputChanged);
m_doseTextBox.TextChanged += new TextChangedEventHandler(OnInputChanged);
dvhGroup.Content = dvhPanel;
m_volumeAtDoseLabel.Content = "Volume at Dose";
m_doseAtVolumeLabel.Content = "Dose at Volume";
dvhPanel.Children.Add(m_volumeAtDoseLabel);
dvhPanel.Children.Add(m_doseTextBox);
dvhPanel.Children.Add(m_volumeAtDoseResultLabel);
dvhPanel.Children.Add(m_resultVolumeAtDose);
dvhPanel.Children.Add(m_doseAtVolumeLabel);
dvhPanel.Children.Add(m_volumeTextBox);
dvhPanel.Children.Add(m_doseAtVolumeResultLabel);
dvhPanel.Children.Add(m_resultDoseAtVolume);
}
// Layout
{
m_structureVolume.MinWidth = 60.0;
m_volumeTextBox.MinWidth = 60.0;
m_doseTextBox.MinWidth = 60.0;
m_resultVolumeAtDose.MinWidth = 60.0;
m_resultDoseAtVolume.MinWidth = 60.0;
rootPanel.Height = 60.0;
}
window.Content = rootPanel;
}
void CheckBoxChanged(object sender, RoutedEventArgs e)
{
UpdateDvhLookup();
}
TextBlock m_structureVolume = new TextBlock();
TextBox m_volumeTextBox = new TextBox();
Label m_resultVolumeAtDose = new Label();
TextBox m_doseTextBox = new TextBox();
Label m_resultDoseAtVolume = new Label();
CheckBox m_absDoseCheckbox = new CheckBox();
CheckBox m_absVolCheckbox = new CheckBox();
Label m_doseAtVolumeLabel = new Label();
Label m_volumeAtDoseLabel = new Label();
Label m_doseAtVolumeResultLabel = new Label();
Label m_volumeAtDoseResultLabel = new Label();
//---------------------------------------------------------------------------------------------
void OnInputChanged(object sender, TextChangedEventArgs e)
{
UpdateDvhLookup();
}
static double s_binWidth = 0.001;
//---------------------------------------------------------------------------------------------
void UpdateDvhLookup()
{
if (m_closing || SelectedStructure == null)
return;
bool doseAbsolute = m_absDoseCheckbox.IsChecked.Value;
bool volAbsolute = m_absVolCheckbox.IsChecked.Value;
m_volumeAtDoseResultLabel.Content = "";
m_doseAtVolumeResultLabel.Content = "";
m_resultVolumeAtDose.Content = "";
m_resultDoseAtVolume.Content = "";
m_structureVolume.Text = "";
double inputVolume = Double.NaN;
if (m_volumeTextBox.Text != null)
{
Double.TryParse(m_volumeTextBox.Text, out inputVolume);
}
double inputDose = Double.NaN;
if (m_doseTextBox.Text != null)
{
Double.TryParse(m_doseTextBox.Text, out inputDose);
}
DoseValuePresentation dosePres = doseAbsolute ? DoseValuePresentation.Absolute : DoseValuePresentation.Relative;
VolumePresentation volPres = volAbsolute ? VolumePresentation.AbsoluteCm3 : VolumePresentation.Relative;
DVHData dvhData = SelectedPlanningItem.GetDVHCumulativeData(SelectedStructure, dosePres, volPres, s_binWidth);
m_structureVolume.Text = dvhData.Volume.ToString("F5");
if (SelectedPlanningItem != null && SelectedPlanningItem.Dose != null && SelectedStructure != null)
{
if (!Double.IsNaN(inputVolume))
{
DoseValue val = SelectedPlanningItem.GetDoseAtVolume(SelectedStructure, inputVolume, volPres, dosePres);
DoseValue controlVal = DvhExtensions.DoseAtVolume(dvhData, inputVolume);
double err = Math.Abs((val.Dose - controlVal.Dose) / val.Dose);
if (err > 0.001)
{
MessageBox.Show("Value : " + val.ToString() + " Control Val : " + controlVal.ToString());
}
m_resultDoseAtVolume.Content = val.ToString();
string doseAtVolumeResultType = volAbsolute ? "cm3 D(" : "% D(";
doseAtVolumeResultType += inputVolume.ToString("F1") + (volAbsolute ? "cm3" : "%") + ") =";
m_doseAtVolumeResultLabel.Content = doseAtVolumeResultType;
}
if (!Double.IsNaN(inputDose))
{
DoseValue.DoseUnit doseUnit = dvhData.MaxDose.Unit;
double vol = SelectedPlanningItem.GetVolumeAtDose(SelectedStructure, new DoseValue(inputDose, doseUnit), volPres);
double controlVal = DvhExtensions.VolumeAtDose(dvhData, inputDose);
double err = Math.Abs((vol - controlVal) / vol);
if (err > 0.001)
{
MessageBox.Show("Value : " + vol.ToString("F3") + " Control Val : " + controlVal.ToString("F3"));
}
m_resultVolumeAtDose.Content = vol.ToString("F5") + (volAbsolute ? "cm3" : "%");
string volumeAtDoseResultType = doseUnit.ToString() + " V(";
volumeAtDoseResultType += inputDose.ToString("F1") + doseUnit.ToString() + " ) =";
m_volumeAtDoseResultLabel.Content = volumeAtDoseResultType;
}
}
}
//---------------------------------------------------------------------------------------------
private void OnComboSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems != null && e.AddedItems.Count == 1)
{
Structure structure = e.AddedItems[0] as Structure;
if (structure != null)
{
SelectedStructure = structure;
UpdateDvhLookup();
}
}
}
//---------------------------------------------------------------------------------------------
private void OnPercentageTextChanged(object sender, TextChangedEventArgs e)
{
UpdateDvhLookup();
}
}
}