-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFraction.cs
More file actions
621 lines (493 loc) · 24.3 KB
/
Fraction.cs
File metadata and controls
621 lines (493 loc) · 24.3 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//---------------------------------------------------------------------------------------------
/// <summary>
/// Eclipse v16 ESAPI script that exports dose for all daily treatment.
/// The code here is modify from Export3D from
/// https://github.com/VarianAPIs/Varian-Code-Samples/blob/master/Eclipse%20Scripting%20API/plugins/Export3D.cs .
/// </summary>
/// <license>
///
// Copyright (c) 2016 Varian Medical Systems, Inc.
// Copyright (c) 2021 44REAM.
//
// 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.
/// </license>
//---------------------------------------------------------------------------------------------
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Reflection;
using VMS.TPS.Common.Model.API;
using VMS.TPS.Common.Model.Types;
using System.IO;
using System.Windows.Media.Media3D;
using System.Windows.Media;
[assembly: AssemblyVersion("1.0.0.1")]
[assembly: AssemblyFileVersion("1.0.0.1")]
[assembly: AssemblyInformationalVersion("1.0")]
namespace Fraction {
public class TreatmentFraction {
public List<PlanSetup> PlanSetupList;
public int PlanNumber;
public int NumberOfFraction;
public TreatmentFraction(List<PlanSetup> planSetupList) {
PlanSetupList = planSetupList;
PlanNumber = planSetupList.Count();
NumberOfFraction = 1;
}
public override bool Equals(object obj) => this.Equals(obj as TreatmentFraction);
public bool Equals(TreatmentFraction p) {
if (p is null) {
return false;
}
if (Object.ReferenceEquals(this, p)) {
return true;
}
if (this.GetType() != p.GetType()) {
return false;
}
if (this.PlanNumber != p.PlanNumber) {
return false;
}
for (int i = 0; i < this.PlanNumber; i++) {
if (this.PlanSetupList[i].Id != p.PlanSetupList[i].Id) {
return false;
}
}
return true;
}
}
public class TreatmentDict {
public Dictionary<string, string> PlanDict = new Dictionary<string, string>();
public int PlanNumber = 1;
public TreatmentDict() {
}
public void AddPlanSetup(PlanSetup planSetup) {
if (!PlanDict.ContainsKey(planSetup.Id)) {
PlanDict.Add(planSetup.Id, $"dose_{PlanNumber}");
PlanNumber++;
}
}
}
class Program {
private static List<string> PatientIDList = new List<string>();
private static bool IsExport = true;
private static string CSVPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\automate_fraction_eso.csv";
private static string ExportFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\export_fraction";
[STAThread]
static void Main(string[] args) {
try {
using (Application app = Application.CreateApplication()) {
ExportFraction(app, "3334289", "C1", "Lungs");
//ExportFractionFromCSV(app);
}
} catch (Exception e) {
}
}
static TreatmentDict AddFraction(List<TreatmentFraction> planFraction, Course course) {
TreatmentDict treatmentDict = new TreatmentDict();
foreach (TreatmentSession treatmentSession in course.TreatmentSessions) {
List<PlanSetup> planSetupList = new List<PlanSetup>(); ;
foreach (PlanTreatmentSession planTreatmentSession in treatmentSession.SessionPlans) {
string status = planTreatmentSession.Status.ToString();
// check if the plan is derivered
// beware CompletedPartially Plan
if (status.Equals("Completed")) {
PlanSetup planSetup = planTreatmentSession.PlanSetup;
// Check if no dose and no image register
if (planSetup.Dose == null || planSetup.StructureSet == null ) {
planFraction.Clear();
throw new Exception("No registered image and no dose data");
}
treatmentDict.AddPlanSetup(planSetup);
planSetupList.Add(planSetup);
} else if (status.Equals("CompletedPartially")) {
throw new Exception("There are CompletedPartially plan");
}
}
// check if there are plansetup
if (planSetupList.Any()) {
TreatmentFraction treatmentFraction = new TreatmentFraction(planSetupList);
int fractionIndex = planFraction.IndexOf(treatmentFraction);
if (fractionIndex == -1) {
planFraction.Add(treatmentFraction);
} else {
planFraction[fractionIndex].NumberOfFraction += 1;
}
}
}
return treatmentDict;
}
static bool IsCTImageOK(Course course) {
HashSet<string> ctNameList = new HashSet<string>();
foreach (PlanSetup planSetup in course.PlanSetups) {
if (planSetup.IsTreated == true) {
if (planSetup.StructureSet == null) {
throw new ArgumentNullException($"StructureSet in {planSetup.Id} is null");
}
if(planSetup.NumberOfFractions == null) {
throw new ArgumentException("null in number of fraction");
}
string ctName = planSetup.StructureSet.Id;
ctNameList.Add(ctName);
}
}
if (ctNameList.Count() != 1) {
return false;
}
return true;
}
static void ExportFractionFromCSV(Application app) {
using (var reader = new StreamReader(CSVPath)) {
while (!reader.EndOfStream) {
var line = reader.ReadLine();
string[] columes = line.Split(',');
try {
ExportFraction(app, columes[0], columes[1], columes[2]);
} catch (Exception e) {
PatientIDList.Add(columes[0]);
Console.Error.WriteLine(e.ToString());
app.ClosePatient();
}
ExportErrorFile(PatientIDList, ExportFolder + "\\error.txt");
Console.WriteLine($"Finish ...");
}
}
}
private static void ExportErrorFile(List<string> list, string path) {
using (TextWriter tw = new StreamWriter(path)) {
foreach (string s in list) {
tw.WriteLine(s);
}
}
}
static void ExportFraction(Application app, string patientId, string courseId, string lungName) {
Console.WriteLine($"Reading {patientId}");
Patient patient = app.OpenPatientById(patientId);
Course course = patient.Courses.Single(c => c.Id == courseId);
if (!IsCTImageOK(course)) {
throw new Exception("There are more than one Registered CT image");
}
List<TreatmentFraction> treatmentFractions = new List<TreatmentFraction>();
TreatmentDict treatmentDict = AddFraction(treatmentFractions, course);
StructureSet structureSet = treatmentFractions[0].PlanSetupList[0].StructureSet;
Structure lungStruct = structureSet.Structures.Single(s => s.Id == lungName);
Image ct = structureSet.Image;
if (IsExport) {
string exportFolder = ExportFolder + "\\" + patient.Id;
CreateFolder(exportFolder);
ExportImage(ct, exportFolder);
ExportDose(treatmentFractions, treatmentDict, exportFolder);
ExportNumberOfFraction(treatmentFractions, treatmentDict, exportFolder);
ExportStructure(lungStruct, exportFolder, "\\lung.ply");
}
app.ClosePatient();
}
static void ExportStructure(Structure structure, string folder, string name) {
if (!structure.HasSegment) {
Console.WriteLine($"NO SEGMENTED");
return;
}
Console.WriteLine($"HAVE SEGMENTED");
Console.WriteLine($"Exporting Structure ...");
//comment out the file output you do not want
SaveTriangleMeshToPlyFile(structure.MeshGeometry, folder + name);
}
static public void ExportNumberOfFraction(List<TreatmentFraction> treatmentFractions, TreatmentDict treatmentDict, string exportFolder) {
string outputFileName = exportFolder + "//fraction.txt";
string name;
if (File.Exists(outputFileName)) {
File.SetAttributes(outputFileName, FileAttributes.Normal);
File.Delete(outputFileName);
}
using (TextWriter writer = new StreamWriter(outputFileName)) {
foreach (TreatmentFraction treatmentFraction in treatmentFractions) {
writer.Write(treatmentFraction.NumberOfFraction.ToString() + " ");
writer.Write(treatmentFraction.PlanSetupList.Count().ToString() + " ");
foreach(PlanSetup planSetup in treatmentFraction.PlanSetupList) {
name = treatmentDict.PlanDict[planSetup.Id];
writer.Write(name + " ");
}
writer.WriteLine();
}
}
}
static public void ExportImage(Image image, string folder) {
Console.WriteLine($"Exporting CT image ...");
string filename = folder + "\\ct.vtk";
SaveDoseOrImageToVTKStructurePoints(null, image, filename);
}
static public void ExportDose(List<TreatmentFraction> treatmentFractions, TreatmentDict treatmentDict, string exportFolder) {
string name;
foreach (TreatmentFraction treatmentFraction in treatmentFractions) {
foreach (PlanSetup planSetup in treatmentFraction.PlanSetupList) {
Dose fractionDose = planSetup.Dose;
if (!planSetup.DoseValuePresentation.ToString().Contains("Absolute")) {
int maxValueForScaling = fractionDose != null ? FindMaxValue(fractionDose) : 0;
double? constant = (double)planSetup.TotalDose.Dose / 100 / planSetup.PlanNormalizationValue / planSetup.NumberOfFractions / planSetup.TreatmentPercentage;
double? test = maxValueForScaling * constant;
if (constant == null) {
throw new Exception("Constant is null");
}
name = treatmentDict.PlanDict[planSetup.Id];
ExportDoseFraction(fractionDose, exportFolder + $"//{name}.vtk", constant);
}
// ---------------------------------------------------------------------------------------------
// I did not written the code for export absolute dose because our data are record in relative dose
//---------------------------------------------------------------------------------------------
name = treatmentDict.PlanDict[planSetup.Id];
ExportDoseFraction(fractionDose, exportFolder + $"//{name}.vtk", 1);
}
}
}
static public void ExportDoseFraction(Dose dose, string outputFileName, double? constant) {
if (File.Exists(outputFileName)) {
File.SetAttributes(outputFileName, FileAttributes.Normal);
File.Delete(outputFileName);
}
int W, H, D;
double sx, sy, sz;
VVector origin, rowDirection, columnDirection;
W = dose.XSize;
H = dose.YSize;
D = dose.ZSize;
sx = dose.XRes;
sy = dose.YRes;
sz = dose.ZRes;
origin = dose.Origin;
rowDirection = dose.XDirection;
columnDirection = dose.YDirection;
using (TextWriter writer = new StreamWriter(outputFileName)) {
writer.WriteLine("# vtk DataFile Version 3.0");
writer.WriteLine("vtk output");
writer.WriteLine("ASCII");
writer.WriteLine("DATASET STRUCTURED_POINTS");
writer.WriteLine("DIMENSIONS " + W + " " + H + " " + D);
int[,] buffer = new int[W, H];
double xsign = rowDirection.x > 0 ? 1.0 : -1.0;
double ysign = columnDirection.y > 0 ? 1.0 : -1.0;
double zsign = GetZDirection(rowDirection, columnDirection).z > 0 ? 1.0 : -1.0;
writer.WriteLine("ORIGIN " + origin.x.ToString() + " " + origin.y.ToString() + " " + origin.z.ToString());
writer.WriteLine("SPACING " + sx * xsign + " " + sy * ysign + " " + sz * zsign);
writer.WriteLine("POINT_DATA " + W * H * D);
writer.WriteLine("SCALARS image_data unsigned_short 1");
writer.WriteLine("LOOKUP_TABLE default");
//int maxValueForScaling = dose != null ? FindMaxValue(dose) : 0;
for (int z = 0; z < D; z++) {
dose.GetVoxels(z, buffer);
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
int value = buffer[x, y];
UInt16 curvalue = 0;
curvalue = (UInt16)((double)(value / 10) * constant);
writer.Write(curvalue + " ");
}
writer.WriteLine();
}
}
}
}
static void SaveTriangleMeshToPlyFile(MeshGeometry3D mesh, string outputFileName) {
if (mesh == null)
return;
if (File.Exists(outputFileName)) {
File.SetAttributes(outputFileName, FileAttributes.Normal);
File.Delete(outputFileName);
}
Point3DCollection vertexes = mesh.Positions;
Int32Collection indexes = mesh.TriangleIndices;
using (TextWriter writer = new StreamWriter(outputFileName)) {
writer.WriteLine("ply");
writer.WriteLine("format ascii 1.0");
writer.WriteLine("element vertex " + vertexes.Count);
writer.WriteLine("property float x");
writer.WriteLine("property float y");
writer.WriteLine("property float z");
writer.WriteLine("property float nx");
writer.WriteLine("property float ny");
writer.WriteLine("property float nz");
writer.WriteLine("element face " + indexes.Count / 3);
writer.WriteLine("property list uchar int vertex_indices");
writer.WriteLine("end_header");
for (int v = 0; v < vertexes.Count(); v++) {
Vector3D normal = CalculateVertexNormal(mesh, v);
writer.Write(vertexes[v].X.ToString("e") + " ");
writer.Write(vertexes[v].Y.ToString("e") + " ");
writer.Write(vertexes[v].Z.ToString("e") + " ");
writer.Write(normal.X.ToString("e") + " ");
writer.Write(normal.Y.ToString("e") + " ");
writer.Write(normal.Z.ToString("e"));
writer.WriteLine();
}
int i = 0;
while (i < indexes.Count) {
writer.Write("3 ");
writer.Write(indexes[i++] + " ");
writer.Write(indexes[i++] + " ");
writer.Write(indexes[i++] + " ");
writer.WriteLine();
}
}
}
static Vector3D CalculateSurfaceNormal(Point3D p1, Point3D p2, Point3D p3) {
Vector3D v1 = new Vector3D(0, 0, 0); // Vector 1 (x,y,z) & Vector 2 (x,y,z)
Vector3D v2 = new Vector3D(0, 0, 0);
Vector3D normal = new Vector3D(0, 0, 0);
// Finds The Vector Between 2 Points By Subtracting
// The x,y,z Coordinates From One Point To Another.
// Calculate The Vector From Point 2 To Point 1
v1.X = p1.X - p2.X; // Vector 1.x=Vertex[0].x-Vertex[1].x
v1.Y = p1.Y - p2.Y; // Vector 1.y=Vertex[0].y-Vertex[1].y
v1.Z = p1.Z - p2.Z; // Vector 1.z=Vertex[0].y-Vertex[1].z
// Calculate The Vector From Point 3 To Point 2
v2.X = p2.X - p3.X; // Vector 1.x=Vertex[0].x-Vertex[1].x
v2.Y = p2.Y - p3.Y; // Vector 1.y=Vertex[0].y-Vertex[1].y
v2.Z = p2.Z - p3.Z; // Vector 1.z=Vertex[0].y-Vertex[1].z
// Compute The Cross Product To Give Us A Surface Normal
normal.X = v1.Y * v2.Z - v1.Z * v2.Y; // Cross Product For Y - Z
normal.Y = v1.Z * v2.X - v1.X * v2.Z; // Cross Product For X - Z
normal.Z = v1.X * v2.Y - v1.Y * v2.X; // Cross Product For X - Y
normal.Normalize();
return normal;
}
//creates a normal for a single vertex by searching all faces it is connected with
//then averaging the surface normal for those faces
static Vector3D CalculateVertexNormal(MeshGeometry3D mesh, int vertex) {
Vector3D normal = new Vector3D(0, 0, 0);
List<Vector3D> normals = new List<Vector3D>();
//foreach triangle
for (int i = 0; i < mesh.TriangleIndices.Count(); i += 3) {
//foreach vertex
for (int ii = 0; ii < 3; ii++) {
//calculates and add the surface normal if the face uses that vertex
if (mesh.TriangleIndices[i + ii] == vertex) {
Vector3D surfaceNormal = CalculateSurfaceNormal(mesh.Positions[mesh.TriangleIndices[i]], mesh.Positions[mesh.TriangleIndices[i + 1]], mesh.Positions[mesh.TriangleIndices[i + 2]]);
normals.Add(surfaceNormal);
}
}
}
//average the normals and normalize
foreach (Vector3D v in normals) {
normal += v;
}
normal = normal / normals.Count();
normal.Normalize();
return normal;
}
static public void SaveDoseOrImageToVTKStructurePoints(Dose dose, Image image, string outputFileName) {
if (File.Exists(outputFileName)) {
File.SetAttributes(outputFileName, FileAttributes.Normal);
File.Delete(outputFileName);
}
int W, H, D;
double sx, sy, sz;
VVector origin, rowDirection, columnDirection;
if (dose != null) {
W = dose.XSize;
H = dose.YSize;
D = dose.ZSize;
sx = dose.XRes;
sy = dose.YRes;
sz = dose.ZRes;
origin = dose.Origin;
rowDirection = dose.XDirection;
columnDirection = dose.YDirection;
} else {
W = image.XSize;
H = image.YSize;
D = image.ZSize;
sx = image.XRes;
sy = image.YRes;
sz = image.ZRes;
origin = image.Origin;
rowDirection = image.XDirection;
columnDirection = image.YDirection;
}
using (TextWriter writer = new StreamWriter(outputFileName)) {
writer.WriteLine("# vtk DataFile Version 3.0");
writer.WriteLine("vtk output");
writer.WriteLine("ASCII");
writer.WriteLine("DATASET STRUCTURED_POINTS");
writer.WriteLine("DIMENSIONS " + W + " " + H + " " + D);
int[,] buffer = new int[W, H];
double xsign = rowDirection.x > 0 ? 1.0 : -1.0;
double ysign = columnDirection.y > 0 ? 1.0 : -1.0;
double zsign = GetZDirection(rowDirection, columnDirection).z > 0 ? 1.0 : -1.0;
writer.WriteLine("ORIGIN " + origin.x.ToString() + " " + origin.y.ToString() + " " + origin.z.ToString());
writer.WriteLine("SPACING " + sx * xsign + " " + sy * ysign + " " + sz * zsign);
writer.WriteLine("POINT_DATA " + W * H * D);
writer.WriteLine("SCALARS image_data unsigned_short 1");
writer.WriteLine("LOOKUP_TABLE default");
// int maxValueForScaling = dose != null ? FindMaxValue(dose) : 0;
for (int z = 0; z < D; z++) {
if (dose != null) dose.GetVoxels(z, buffer);
else image.GetVoxels(z, buffer);
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
int value = buffer[x, y];
UInt16 curvalue = 0;
if (image != null)
curvalue = (UInt16)value;
else
curvalue = (UInt16)((double)(value / 10000));
writer.Write(curvalue + " ");
}
writer.WriteLine();
}
}
}
}
static int FindMaxValue(Dose dose) {
int maxValue = 0;
//float meanValue = 0;
int[,] buffer = new int[dose.XSize, dose.YSize];
if (dose != null) {
for (int z = 0; z < dose.ZSize; z++) {
dose.GetVoxels(z, buffer);
for (int y = 0; y < dose.YSize; y++) {
for (int x = 0; x < dose.XSize; x++) {
int value = buffer[x, y];
//meanValue = meanValue + buffer[x, y];
if (value > maxValue)
maxValue = value;
}
}
}
}
//meanValue = meanValue / (dose.ZSize * dose.XSize * dose.YSize);
//return (int)meanValue;
return maxValue;
}
private static void CreateFolder(string path) {
try {
if (Directory.Exists(path)) {
return;
}
DirectoryInfo di = Directory.CreateDirectory(path);
Console.WriteLine($"CREATE {path}");
} catch (Exception e) {
Console.WriteLine($"process failed {e.ToString()}");
}
}
private static VVector GetZDirection(VVector a, VVector b) {
// return cross product
return new VVector(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
}
}
}