-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetProfile.cs
More file actions
171 lines (135 loc) · 4.65 KB
/
GetProfile.cs
File metadata and controls
171 lines (135 loc) · 4.65 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using VMS.TPS.Common.Model.API;
using VMS.TPS.Common.Model.Types;
namespace VMS.TPS
{
public class Script
{
public Script() { }
private DoseProfile CalculateProfile(
Double x_start, Double y_start, Double z_start, Double x_stop, Double y_stop, Double z_stop, Double step, Dose dose
)
{
// 始点と終点を定義
var start = new VVector(x_start, y_start, z_start);
var stop = new VVector(x_stop, y_stop, z_stop);
// 始点・終点間距離を定義
var distance = VVector.Distance(start, stop);
if (distance == 0)
{
throw new ApplicationException("Distance between two points is 0.");
}
// 距離をステップサイズで割ってサンプリング点数を決定(切り捨て)
Double point = Math.Floor(distance / step);
if (point < 1.0)
{
throw new ApplicationException("Step size is largar than distance between two points.");
}
// 始点から終点へ向かう長さ1のベクトルを得る
var dir = stop - start;
dir.ScaleToUnitLength();
// stepサイズに合うように終点位置を調整
var stop_new = start + dir * point * step;
// DoseProfile用の配列を準備
double[] profile = new double[(int) (point + 1)];
// DoseProfileを取得
var doseProfile = dose.GetDoseProfile(start, stop_new, profile);
return doseProfile;
}
public void Execute(ScriptContext context /*, System.Windows.Window window*/ )
{
String inputDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
String inputFile = inputDir + "\\input.csv";
var patient = context.Patient;
var planSetup = context.PlanSetup;
if (planSetup == null)
{
throw new ApplicationException("Please Open the plan.");
}
var planDose = planSetup.Dose;
if (planDose == null)
{
throw new ApplicationException("Please Calculate the plan.");
}
bool CanGetBeamDose = true;
// BeamDoseを取得できるかどうか確認する。
Type t = typeof(Beam);
System.Reflection.PropertyInfo info = t.GetProperty("Dose");
if (info == null)
{
CanGetBeamDose = false;
}
Double x_start;
Double y_start;
Double z_start;
Double x_stop;
Double y_stop;
Double z_stop;
Double step;
int i = 1;
string msg = null;
try
{
using(var sr = new StreamReader(inputFile))
{
while (!sr.EndOfStream)
{
var profileList = new List<DoseProfile>();
var line = sr.ReadLine();
var values = line.Split(',');
if (values[0].StartsWith("x"))
{
continue;
}
x_start = double.Parse(values[0]);
y_start = double.Parse(values[1]);
z_start = double.Parse(values[2]);
x_stop = double.Parse(values[3]);
y_stop = double.Parse(values[4]);
z_stop = double.Parse(values[5]);
step = double.Parse(values[6]);
var outputFile = inputDir + String.Format("\\Profile{0:00}.csv", i);
string header = "x, y, z, plan";
profileList.Add(CalculateProfile(x_start, y_start, z_start, x_stop, y_stop, z_stop, step, planDose));
var count = profileList[0].Count;
if (CanGetBeamDose)
{
foreach (var beam in planSetup.Beams)
{
if (!beam.IsSetupField)
{
header += String.Format(", {0}", beam.Id);
profileList.Add(CalculateProfile(x_start, y_start, z_start, x_stop, y_stop, z_stop, step, beam.Dose));
}
}
}
using(var sw = new StreamWriter(outputFile))
{
sw.WriteLine(header);
for (int j = 0; j < count; j++)
{
string data = String.Format("{0}, {1}, {2}", profileList[0][j].Position.x, profileList[0][j].Position.y, profileList[0][j].Position.z);
foreach (var profile in profileList)
{
data += String.Format(", {0}", profile[j].Value);
}
sw.WriteLine(data);
}
}
msg += String.Format("Profile{0:00} is written to {1}.\n", i, outputFile);
i++;
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
MessageBox.Show(msg);
}
}
}