-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCollisionCheck.cs
More file actions
2038 lines (1619 loc) · 105 KB
/
CollisionCheck.cs
File metadata and controls
2038 lines (1619 loc) · 105 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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Media;
using System.Numerics;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media.Media3D;
using g3;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace CollisionCheck
{
public class CollisionCheckMethods
{
public static double ABSDISTANCE(Vector3d V1, Vector3d V2)
{
double distance = 0.0;
double xdiff = 0.0;
double ydiff = 0.0;
double zdiff = 0.0;
xdiff = V1.x - V2.x;
ydiff = V1.y - V2.y;
zdiff = V1.z - V2.z;
distance = Math.Sqrt(Math.Pow(Math.Abs(xdiff), 2.0) + Math.Pow(Math.Abs(ydiff), 2.0) + Math.Pow(Math.Abs(zdiff), 2.0));
return distance;
}
public static async Task<StreamReader> GantryRetrievalAsync(string pid, string cid, string lid, BEAM beam)
{
StreamReader GantryAngleRetrieveOutput = await Task.Run(() => ActualRetrieval(pid, cid, lid, beam));
return GantryAngleRetrieveOutput;
}
public static StreamReader ActualRetrieval(string pid, string cid, string lid, BEAM beam)
{
ProcessStartInfo processinfo = new ProcessStartInfo(@"\\wvvrnimbp01ss\va_data$\filedata\ProgramData\Vision\Stand-alone Programs\CollisionCheck_InfoRetrieval\CollisionCheck_InfoRetrieval.exe", pid + " " + cid + " " + beam.beamId + " " + lid); // path name of the Collision retrieval program
processinfo.UseShellExecute = false;
processinfo.ErrorDialog = false;
processinfo.RedirectStandardOutput = true;
processinfo.WindowStyle = ProcessWindowStyle.Hidden;
Process GantryAngleRetrieve = new Process();
GantryAngleRetrieve.StartInfo = processinfo;
GantryAngleRetrieve.EnableRaisingEvents = true;
GantryAngleRetrieve.Start();
StreamReader Gstream = GantryAngleRetrieve.StandardOutput;
GantryAngleRetrieve.WaitForExit();
return Gstream;
}
//=========================================================================================================================================================================================================================================
public static List<CollisionAlert> BeamCollisionAnalysis(BEAM beam, TextBox ProgOutput)
{
ProgOutput.AppendText(Environment.NewLine);
ProgOutput.AppendText("Beam " + beam.beamId + " analysis running....");
List<CollisionAlert> collist = new List<CollisionAlert>();
double INTERSECTDIST = 50.0;
Index2i snear_tids = new Index2i(-1, -1);
DistTriangle3Triangle3 STriDist;
double ZABSDIST;
double reportCang;
DMesh3 PBodyContour = new DMesh3();
DMeshAABBTree3 PBodyContourSpatial = new DMeshAABBTree3(PBodyContour);
DMesh3 PCouchInterior = new DMesh3();
DMeshAABBTree3 PCouchInteriorSpatial = new DMeshAABBTree3(PCouchInterior);
DMesh3 PProne_Brst_Board = new DMesh3();
DMeshAABBTree3 PProne_Brst_BoardSpatial = new DMeshAABBTree3(PProne_Brst_Board);
DMesh3 PATBOX = new DMesh3();
DMeshAABBTree3 spatial = new DMeshAABBTree3(PATBOX);
// ANGLES ARE IN DEGREES
List<double> GAngleList = new List<double>();
int gantrylistCNT = 0;
double CouchStartAngle = beam.ControlPoints[0].Couchangle;
double CouchEndAngle = beam.ControlPoints[beam.ControlPoints.Count - 1].Couchangle; // count - 1 is the end becuase the index starts at 0
List<WriteMesh> tempbeam = new List<WriteMesh>();
//these variables are used in the collision anlysis
double? MRGPATBOX = null;
double? MRGCSURFACE = null;
double? MRGCINTERIOR = null;
double? MRGBBOARD = null;
bool lastcontigrealPATBOX = false;
bool lastcontigrealCouch = false;
bool lastcontigrealBoard = false;
if (CouchStartAngle != CouchEndAngle)
{
System.Windows.Forms.MessageBox.Show("WARNING: The patient couch has a different rotation angle at the end of beam " + beam.beamId + " in plan " + beam.planId + " than what the beam starts with.");
// just in case the start and stop of an beam have different couch angles for some reason. not sure if this is actually possible.
}
else if (CouchStartAngle == CouchEndAngle)
{
//ProgOutput.AppendText(Environment.NewLine);
//ProgOutput.AppendText("Starting analysis of beam " + beam.Id + " in plan " + plan.Id + ".");
// MessageBox.Show("Starting analysis of beam " + beam.Id + " in plan " + plan.Id + " .");
Vector3d ISO = beam.Isocenter;
Vector3d UserOrigin = beam.imageuserorigin;
Vector3d Origin = beam.imageorigin;
bool cp = false; //this represents whether the program is using MLC control points or not
// ProgOutput.AppendText(Environment.NewLine);
// ProgOutput.AppendText("Beam " + beam.Id + " calling BOXMAKER....");
List<DMesh3> PatMeshList = new List<DMesh3>();
// calls the boxmaker method which makes all the patient realated 3D meshes.
try
{
// System.Windows.Forms.MessageBox.Show("Before Patbox");
PatMeshList = BOXMAKER(beam, PBodyContour, PBodyContourSpatial, PCouchInterior, PCouchInteriorSpatial, PATBOX, spatial, PProne_Brst_Board, PProne_Brst_BoardSpatial);
}
catch(Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
}
// System.Windows.Forms.MessageBox.Show("after Patbox");
// System.Windows.Forms.MessageBox.Show("PatMeshListSize: " + PatMeshList.Count);
if (beam.couchexists == true & beam.breastboardexists == true)
{
PBodyContour = PatMeshList[0];
tempbeam.Add(new WriteMesh(PBodyContour));
PBodyContourSpatial = new DMeshAABBTree3(PBodyContour);
PBodyContourSpatial.Build();
PCouchInterior = PatMeshList[1];
tempbeam.Add(new WriteMesh(PCouchInterior));
PCouchInteriorSpatial = new DMeshAABBTree3(PCouchInterior);
PCouchInteriorSpatial.Build();
PProne_Brst_Board = PatMeshList[2];
tempbeam.Add(new WriteMesh(PProne_Brst_Board));
PProne_Brst_BoardSpatial = new DMeshAABBTree3(PProne_Brst_Board);
PProne_Brst_BoardSpatial.Build();
PATBOX = PatMeshList[3];
spatial = new DMeshAABBTree3(PATBOX);
spatial.Build();
}
else if(beam.couchexists == false & beam.breastboardexists == false)
{
PBodyContour = PatMeshList[0];
tempbeam.Add(new WriteMesh(PBodyContour));
PBodyContourSpatial = new DMeshAABBTree3(PBodyContour);
PBodyContourSpatial.Build();
PATBOX = PatMeshList[1];
spatial = new DMeshAABBTree3(PATBOX);
spatial.Build();
}
else if(beam.couchexists == true & beam.breastboardexists == false)
{
PBodyContour = PatMeshList[0];
tempbeam.Add(new WriteMesh(PBodyContour));
PBodyContourSpatial = new DMeshAABBTree3(PBodyContour);
PBodyContourSpatial.Build();
PCouchInterior = PatMeshList[1];
tempbeam.Add(new WriteMesh(PCouchInterior));
PCouchInteriorSpatial = new DMeshAABBTree3(PCouchInterior);
PCouchInteriorSpatial.Build();
PATBOX = PatMeshList[2];
spatial = new DMeshAABBTree3(PATBOX);
spatial.Build();
}
else if(beam.couchexists == false & beam.breastboardexists == true)
{
PBodyContour = PatMeshList[0];
tempbeam.Add(new WriteMesh(PBodyContour));
PBodyContourSpatial = new DMeshAABBTree3(PBodyContour);
PBodyContourSpatial.Build();
PProne_Brst_Board = PatMeshList[1];
tempbeam.Add(new WriteMesh(PProne_Brst_Board));
PProne_Brst_BoardSpatial = new DMeshAABBTree3(PProne_Brst_Board);
PProne_Brst_BoardSpatial.Build();
PATBOX = PatMeshList[2];
spatial = new DMeshAABBTree3(PATBOX);
spatial.Build();
}
// System.Windows.Forms.MessageBox.Show("Isocenter point is at: (" + ISO.x + " ," + ISO.y + " ," + ISO.z + ")");
// MessageBox.Show("Image origin is at: (" + Origin.x + " ," + Origin.y + " ," + Origin.z + ")");
// System.Windows.Forms.MessageBox.Show("User Origin at: (" + UserOrigin.x + " ," + UserOrigin.y + " ," + UserOrigin.z + ")");
// source position creation
double myZ = 0.0;
double myX = 0.0;
double myY = 0.0;
double ANGLE = 0.0;
double Gangle = 0.0;
string gantryXISOshift = null; //not an iso shift, this variable is used in the intial construction of the gantry disk model
// initial gantry head point construction
double gfxp = 0.0;
double gfyp = 0.0;
double gantrycenterxtrans = 0.0;
double gantrycenterztrans = 0.0;
double xpp = 0.0;
double ypp = 0.0;
// coordinate system transform
double thetap = 0.0;
double Backxtrans = 0.0;
double Frontxtrans = 0.0;
double Leftxtrans = 0.0;
double Rightxtrans = 0.0;
double Backztrans = 0.0;
double Frontztrans = 0.0;
double Leftztrans = 0.0;
double Rightztrans = 0.0;
// DMeshAABBTree3.IntersectionsQueryResult intersectlist = new DMeshAABBTree3.IntersectionsQueryResult();
// double GantryAngle = 5000.5;
if (beam.MLCtype == "Static")
{
double GantryStartAngle = 500.0; //initially set to 500 so that they are clearly outside of the appropriate domain, not a real angle
double GantryEndAngle = 500.0;
string ArcDirection = null;
//ProgOutput.AppendText(Environment.NewLine);
//ProgOutput.AppendText("This is a static MLC beam with no control points. Attempting to get Gantry information for this beam from the ARIA database (this might take a minute)... ");
//System.Windows.Forms.MessageBox.Show("This is a static MLC beam with no control points. The program will get the gantry information it needs for this beam from the ARIA database.\nA blank terminal window will appear while it does this. A dialogue box will appear that will tell you that the program is busy because it is waiting for the other program to query the database.\nYou will have to click on 'switch to' several times until it is done. The GUI window will reappear when the program is finished.");
Task<StreamReader> TGantryAngleRetrieveOutput = GantryRetrievalAsync(beam.patientId, beam.courseId, beam.planId, beam);
StreamReader GantryAngleRetrieveOutput = TGantryAngleRetrieveOutput.Result;
// This is high-level .NET multithreading using the Task Parallel Library included in .NET 4.0
//ProgOutput.AppendText(Environment.NewLine);
//ProgOutput.AppendText("Aria retrieval complete! Building list of gantry angles...");
ArcDirection = GantryAngleRetrieveOutput.ReadLine();
GantryStartAngle = Convert.ToDouble(GantryAngleRetrieveOutput.ReadLine());
GantryEndAngle = Convert.ToDouble(GantryAngleRetrieveOutput.ReadLine());
if (ArcDirection == "NONE")
{
GAngleList.Add(GantryStartAngle);
}
else if (ArcDirection == "CW")
{
double tempangle = GantryStartAngle;
GAngleList.Add(GantryStartAngle);
while (tempangle != GantryEndAngle)
{
tempangle++;
if (tempangle == 360)
{
tempangle = 0;
}
GAngleList.Add(tempangle);
}
}
else if (ArcDirection == "CC")
{
double tempangle = GantryStartAngle;
GAngleList.Add(GantryStartAngle);
while (tempangle != GantryEndAngle)
{
tempangle--;
if (tempangle == -1)
{
tempangle = 359;
}
GAngleList.Add(tempangle);
}
}
}
else
{
//System.Windows.Forms.MessageBox.Show("Arc Length: " + beam.ArcLength);
if (beam.arclength == 0)
{
//ProgOutput.AppendText(Environment.NewLine);
//ProgOutput.AppendText("Static gantry IMRT beam. Retrieving gantry angle from first MLC control point ... ");
GAngleList.Add(beam.ControlPoints.First().Gantryangle);
}
else
{
//ProgOutput.AppendText(Environment.NewLine);
//ProgOutput.AppendText("Moving gantry IMRT beam. Building list of gantry angles from control points ... ");
cp = true;
foreach (CONTROLPOINT point in beam.ControlPoints)
{
GAngleList.Add(point.Gantryangle);
}
}
// System.Windows.Forms.MessageBox.Show("Trigger 1");
}
// System.Windows.Forms.MessageBox.Show("Trigger 2");
foreach (double GantryAngle in GAngleList)
{
gantrylistCNT++;
// System.Windows.Forms.MessageBox.Show("Gantry ANGLE : " + GantryAngle + " ");
// MessageBox.Show("couch ANGLE : " + CouchEndAngle + " ");
if (GAngleList.Count <= 10)
{
//ProgOutput.AppendText(Environment.NewLine);
//ProgOutput.AppendText("Gantry Angle: " + gantrylistCNT + "/" + GAngleList.Count);
}
else if (cp == false)
{
if (gantrylistCNT % 4 == 0)
{
//ProgOutput.AppendText(Environment.NewLine);
//ProgOutput.AppendText("Gantry Angle: " + gantrylistCNT + "/" + GAngleList.Count);
// System.Windows.Forms.MessageBox.Show("Current (not from cp) Gangle: " + GantryAngle);
}
else
{
continue;
}
}
else if (cp == true)
{
if (gantrylistCNT % 3 == 0)
{
//ProgOutput.AppendText(Environment.NewLine);
// ProgOutput.AppendText("Gantry Angle: " + gantrylistCNT + "/" + GAngleList.Count);
//System.Windows.Forms.MessageBox.Show("Current (cp) Gangle: " + GantryAngle);
// ProgBar.PerformStep();
}
else
{
continue;
}
}
//System.Windows.Forms.MessageBox.Show("Trigger 3");
// ProgOutput.AppendText(Environment.NewLine);
//ProgOutput.AppendText("Conducting Collision analysis and writing STL files to disk...");
// MessageBox.Show("real couch ANGLE : " + realangle + " ");
//VVector APISOURCE = beam.GetSourceLocation(GantryAngle); // negative Y
// MessageBox.Show("SOURCE (already transformed by API): (" + APISOURCE.x + " ," + APISOURCE.y + " ," + APISOURCE.z + ")");
/* So, the issue with the Source position is that it actually does change in accordance with the couch angle,
* in other words, the position returned by the source location method has already had a coordinate transformation
* performed on it so that it is in the coordinate system of the patient's image. And it is in the Eclipse coord. system.
*
* Because the gantry center point and other points of the gantry need to first be constructed with everything at couch zero,
* the position of the Source at couch zero needs to be determined, otherwise the gantrycenter point calculated from the source can be wrong,
* because it may operate on the wrong position components (because it assumes the couch is at zero).
*
* It is safer to calculate the position of all the gantry points as if they were at couch zero first, and then do a coord. transform for the couch angle.
*
* Anyway, in order to find the correct position of the gantry center point we need to find the couch zero position of source, using the following equation.
*/
// need to come up with something to set polarity ISO.x and ISO.y based on patient orientation and where the ISO is.
// ISO coords can be negative !!!!!
// this just innately handles iso shifts. No artifical shifts are neccessary. The only thing is the gantry disks are rotated at the end if a prone orientation. the patient structures are never shifted.
myZ = ISO.z;
myX = 1000 * Math.Cos((((GantryAngle - 90.0) * Math.PI) / 180.0)) + ISO.x; // - 90 degrees is because the polar coordinate system has 0 degrees on the right side
myY = 1000 * Math.Sin((((-GantryAngle - 90.0) * Math.PI) / 180.0)) + ISO.y; // need negative because y axis is inverted
// THIS WORKS!
Vector3d mySOURCE = new Vector3d(myX, myY, myZ);
// MessageBox.Show("mySOURCE: (" + mySOURCE.x + " ," + mySOURCE.y + " ," + mySOURCE.z + ")");
// MessageBox.Show("SOURCE : (" + convSOURCE.x + " ," + convSOURCE.y + " ," + convSOURCE.z + ")");
// this determines the position of gantrycenterpoint (from mySOURCE) at all gantry angles at couch 0 degrees
if (GantryAngle > 270.0)
{
Gangle = 90.0 - (GantryAngle - 270.0);
gfxp = 585.0 * Math.Sin((Gangle * Math.PI) / 180.0);
gfyp = 585.0 * Math.Cos((Gangle * Math.PI) / 180.0);
}
else if (GantryAngle >= 0.0 & GantryAngle <= 90.0)
{
Gangle = GantryAngle;
gfxp = 585.0 * Math.Sin((Gangle * Math.PI) / 180.0);
gfyp = 585.0 * Math.Cos((Gangle * Math.PI) / 180.0);
}
else if (GantryAngle > 90.0 & GantryAngle <= 180.0)
{
Gangle = 90.0 - (GantryAngle - 90.0);
gfxp = 585.0 * Math.Sin((Gangle * Math.PI) / 180.0);
gfyp = 585.0 * Math.Cos((Gangle * Math.PI) / 180.0);
}
else if (GantryAngle > 180.0 & GantryAngle <= 270.0)
{
// MessageBox.Show("Trig 5");
Gangle = GantryAngle - 180.0;
gfxp = 585.0 * Math.Sin((Gangle * Math.PI) / 180.0);
gfyp = 585.0 * Math.Cos((Gangle * Math.PI) / 180.0);
}
thetap = 90.0 - Gangle;
// MessageBox.Show("thetap is: " + thetap);
Vector3d gantrycenter = mySOURCE; // this will represent the center of the gantry head's surface once the transforms below are performed
// For couch zero degrees
if (GantryAngle >= 270.0 | (GantryAngle >= 0.0 & GantryAngle <= 90.0))
{
gantrycenter.y = gantrycenter.y + gfyp;
// MessageBox.Show("gf.y is: " + gf.y);
}
else if (GantryAngle < 270.0 & GantryAngle > 90.0)
{
gantrycenter.y = gantrycenter.y - gfyp;
}
// this just determines if the original xshift to gf is positive or negative
if (GantryAngle >= 0.0 & GantryAngle <= 180.0)
{
gantrycenter.x = gantrycenter.x - gfxp;
gantryXISOshift = "POS";
}
else if (GantryAngle > 180.0)
{
gantrycenter.x = gantrycenter.x + gfxp;
gantryXISOshift = "NEG";
}
Vector3d origgantrycenter = gantrycenter;
//System.Windows.Forms.MessageBox.Show("Trigger 4");
// MessageBox.Show("gantrycenter before transform is: (" + gantrycenter.x + " ," + gantrycenter.y + " ," + gantrycenter.z + ")");
//gantrycenter now represents the center point of the gantry for all gantry angles at 0 degrees couch angle
// a coordinate transformation for couch angle is performed next
//once the gantry centerpoint and the patient are in the same coordinate system, the edges of the gantry are found from there.
// GANTRY CENTER POINT COORDINATE SYSTEM TRANSFORMATION FOR COUCH ANGLE
if (CouchEndAngle >= 270.0 & CouchEndAngle <= 360.0)
{
// this is really for 0 to 90 couch angle
// MessageBox.Show("REAL couch ANGLE : " + realangle + " ");
// MessageBox.Show("TRIGGER ROT 0 to 270");
ANGLE = 360.0 - CouchEndAngle;
gantrycenterxtrans = (gantrycenter.x * Math.Cos(((-ANGLE * Math.PI) / 180.0))) + (gantrycenter.z * Math.Sin(((-ANGLE * Math.PI) / 180.0)));
gantrycenterztrans = (-gantrycenter.x * Math.Sin(((-ANGLE * Math.PI) / 180.0))) + (gantrycenter.z * Math.Cos(((-ANGLE * Math.PI) / 180.0)));
}
else if (CouchEndAngle >= 0.0 & CouchEndAngle <= 90.0)
{
ANGLE = CouchEndAngle;
gantrycenterxtrans = (gantrycenter.x * Math.Cos(((ANGLE * Math.PI) / 180.0))) + (gantrycenter.z * Math.Sin(((ANGLE * Math.PI) / 180.0)));
gantrycenterztrans = (-gantrycenter.x * Math.Sin(((ANGLE * Math.PI) / 180.0))) + (gantrycenter.z * Math.Cos(((ANGLE * Math.PI) / 180.0)));
}
// that is just the first part of the coordinate system. we now need to account for shifts to the respective ccordinate axes for the ISO position. This is dependent on couch and gantry rotation.
if (CouchEndAngle >= 0.0 & CouchEndAngle <= 90.0)
{
if (gantryXISOshift == "NEG") // gantry on left side
{
gantrycenter.x = gantrycenterxtrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.z + ISO.x));
gantrycenter.z = gantrycenterztrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (ISO.x + ISO.z));
}
else if (gantryXISOshift == "POS") // gantry on right side
{
gantrycenter.x = gantrycenterxtrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.z + ISO.x));
gantrycenter.z = gantrycenterztrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.x + ISO.z));
}
}
else if (CouchEndAngle >= 270.0 & CouchEndAngle <= 360.0)
{
if (gantryXISOshift == "NEG") // gantry on left side
{
gantrycenter.x = gantrycenterxtrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.z + ISO.x));
gantrycenter.z = gantrycenterztrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (ISO.x + ISO.z));
}
else if (gantryXISOshift == "POS") // gantry on right side
{
gantrycenter.x = gantrycenterxtrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.z + ISO.x));
gantrycenter.z = gantrycenterztrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.x + ISO.z));
}
}
// MessageBox.Show("gantrycenter after transform is: (" + gantrycenter.x + " ," + gantrycenter.y + " ," + gantrycenter.z + ")");
ypp = 384.0 * Math.Cos((thetap * Math.PI) / 180.0); // gantry head diameter is 76.5 cm
xpp = 384.0 * Math.Sin((thetap * Math.PI) / 180.0);
// calaulate the left, right, front, back points of the gantry head for couch at 0 deg, for all gantry angles
// these 4 points represent the gantry head
Vector3d RIGHTEDGE = origgantrycenter;
Vector3d LEFTEDGE = origgantrycenter;
Vector3d BACKEDGE = origgantrycenter;
Vector3d FRONTEDGE = origgantrycenter;
FRONTEDGE.z = FRONTEDGE.z - 384.0;
BACKEDGE.z = BACKEDGE.z + 384.0;
if (GantryAngle >= 0.0 & GantryAngle <= 90.0)
{
// MessageBox.Show("Trigger gantry angle between 0 and 90 gantry points calculation");
RIGHTEDGE.y = RIGHTEDGE.y + ypp;
LEFTEDGE.y = LEFTEDGE.y - ypp;
RIGHTEDGE.x = RIGHTEDGE.x + xpp;
LEFTEDGE.x = LEFTEDGE.x - xpp;
}
else if (GantryAngle > 270.0)
{
//MessageBox.Show("Trigger gantry angle > 270 gantry points calculation");
RIGHTEDGE.y = RIGHTEDGE.y - ypp;
LEFTEDGE.y = LEFTEDGE.y + ypp;
RIGHTEDGE.x = RIGHTEDGE.x + xpp;
LEFTEDGE.x = LEFTEDGE.x - xpp;
}
else if (GantryAngle > 90.0 & GantryAngle <= 180.0)
{
RIGHTEDGE.y = RIGHTEDGE.y + ypp;
LEFTEDGE.y = LEFTEDGE.y - ypp;
RIGHTEDGE.x = RIGHTEDGE.x - xpp;
LEFTEDGE.x = LEFTEDGE.x + xpp;
}
else if (GantryAngle > 180.0 & GantryAngle <= 270.0)
{
RIGHTEDGE.y = RIGHTEDGE.y - ypp;
LEFTEDGE.y = LEFTEDGE.y + ypp;
RIGHTEDGE.x = RIGHTEDGE.x - xpp;
LEFTEDGE.x = LEFTEDGE.x + xpp;
}
// System.Windows.Forms.MessageBox.Show("Trigger 5");
// GANTRY EDGE POINTS COORDINATE SYSTEM TRANSFORMATION FOR COUCH ANGLE
if (CouchEndAngle >= 270.0 & CouchEndAngle <= 360.0)
{
ANGLE = 360.0 - CouchEndAngle;
Backxtrans = (BACKEDGE.x * Math.Cos(((-ANGLE * Math.PI) / 180.0))) + (BACKEDGE.z * Math.Sin(((-ANGLE * Math.PI) / 180.0)));
Backztrans = (-BACKEDGE.x * Math.Sin(((-ANGLE * Math.PI) / 180.0))) + (BACKEDGE.z * Math.Cos(((-ANGLE * Math.PI) / 180.0)));
Frontxtrans = (FRONTEDGE.x * Math.Cos(((-ANGLE * Math.PI) / 180.0))) + (FRONTEDGE.z * Math.Sin(((-ANGLE * Math.PI) / 180.0)));
Frontztrans = (-FRONTEDGE.x * Math.Sin(((-ANGLE * Math.PI) / 180.0))) + (FRONTEDGE.z * Math.Cos(((-ANGLE * Math.PI) / 180.0)));
Leftxtrans = (LEFTEDGE.x * Math.Cos(((-ANGLE * Math.PI) / 180.0))) + (LEFTEDGE.z * Math.Sin(((-ANGLE * Math.PI) / 180.0)));
Leftztrans = (-LEFTEDGE.x * Math.Sin(((-ANGLE * Math.PI) / 180.0))) + (LEFTEDGE.z * Math.Cos(((-ANGLE * Math.PI) / 180.0)));
Rightxtrans = (RIGHTEDGE.x * Math.Cos(((-ANGLE * Math.PI) / 180.0))) + (RIGHTEDGE.z * Math.Sin(((-ANGLE * Math.PI) / 180.0)));
Rightztrans = (-RIGHTEDGE.x * Math.Sin(((-ANGLE * Math.PI) / 180.0))) + (RIGHTEDGE.z * Math.Cos(((-ANGLE * Math.PI) / 180.0)));
}
else if (CouchEndAngle >= 0.0 & CouchEndAngle <= 90.0)
{
ANGLE = CouchEndAngle;
Backxtrans = (BACKEDGE.x * Math.Cos(((ANGLE * Math.PI) / 180.0))) + (BACKEDGE.z * Math.Sin(((ANGLE * Math.PI) / 180.0)));
Backztrans = (-BACKEDGE.x * Math.Sin(((ANGLE * Math.PI) / 180.0))) + (BACKEDGE.z * Math.Cos(((ANGLE * Math.PI) / 180.0)));
Frontxtrans = (FRONTEDGE.x * Math.Cos(((ANGLE * Math.PI) / 180.0))) + (FRONTEDGE.z * Math.Sin(((ANGLE * Math.PI) / 180.0)));
Frontztrans = (-FRONTEDGE.x * Math.Sin(((ANGLE * Math.PI) / 180.0))) + (FRONTEDGE.z * Math.Cos(((ANGLE * Math.PI) / 180.0)));
Leftxtrans = (LEFTEDGE.x * Math.Cos(((ANGLE * Math.PI) / 180.0))) + (LEFTEDGE.z * Math.Sin(((ANGLE * Math.PI) / 180.0)));
Leftztrans = (-LEFTEDGE.x * Math.Sin(((ANGLE * Math.PI) / 180.0))) + (LEFTEDGE.z * Math.Cos(((ANGLE * Math.PI) / 180.0)));
Rightxtrans = (RIGHTEDGE.x * Math.Cos(((ANGLE * Math.PI) / 180.0))) + (RIGHTEDGE.z * Math.Sin(((ANGLE * Math.PI) / 180.0)));
Rightztrans = (-RIGHTEDGE.x * Math.Sin(((ANGLE * Math.PI) / 180.0))) + (RIGHTEDGE.z * Math.Cos(((ANGLE * Math.PI) / 180.0)));
}
if (CouchEndAngle >= 0.0 & CouchEndAngle <= 90.0)
{
if (gantryXISOshift == "NEG") // gantry on left side
{
BACKEDGE.x = Backxtrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.z + ISO.x));
BACKEDGE.z = Backztrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (ISO.x + ISO.z));
FRONTEDGE.x = Frontxtrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.z + ISO.x));
FRONTEDGE.z = Frontztrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (ISO.x + ISO.z));
LEFTEDGE.x = Leftxtrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.z + ISO.x));
LEFTEDGE.z = Leftztrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (ISO.x + ISO.z));
RIGHTEDGE.x = Rightxtrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.z + ISO.x));
RIGHTEDGE.z = Rightztrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (ISO.x + ISO.z));
}
else if (gantryXISOshift == "POS") // gantry on right side
{
BACKEDGE.x = Backxtrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.z + ISO.x));
BACKEDGE.z = Backztrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.x + ISO.z));
FRONTEDGE.x = Frontxtrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.z + ISO.x));
FRONTEDGE.z = Frontztrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.x + ISO.z));
LEFTEDGE.x = Leftxtrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.z + ISO.x));
LEFTEDGE.z = Leftztrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.x + ISO.z));
RIGHTEDGE.x = Rightxtrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.z + ISO.x));
RIGHTEDGE.z = Rightztrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.x + ISO.z));
}
}
else if (CouchEndAngle >= 270.0 & CouchEndAngle <= 360.0)
{
if (gantryXISOshift == "NEG") // gantry on left side
{
BACKEDGE.x = Backxtrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.z + ISO.x));
BACKEDGE.z = Backztrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (ISO.x + ISO.z));
FRONTEDGE.x = Frontxtrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.z + ISO.x));
FRONTEDGE.z = Frontztrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (ISO.x + ISO.z));
LEFTEDGE.x = Leftxtrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.z + ISO.x));
LEFTEDGE.z = Leftztrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (ISO.x + ISO.z));
RIGHTEDGE.x = Rightxtrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.z + ISO.x));
RIGHTEDGE.z = Rightztrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (ISO.x + ISO.z));
}
else if (gantryXISOshift == "POS") // gantry on right side
{
BACKEDGE.x = Backxtrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.z + ISO.x));
BACKEDGE.z = Backztrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.x + ISO.z));
FRONTEDGE.x = Frontxtrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.z + ISO.x));
FRONTEDGE.z = Frontztrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.x + ISO.z));
LEFTEDGE.x = Leftxtrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.z + ISO.x));
LEFTEDGE.z = Leftztrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.x + ISO.z));
RIGHTEDGE.x = Rightxtrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.z + ISO.x));
RIGHTEDGE.z = Rightztrans + (Math.Sin(((ANGLE * Math.PI) / 180.0)) * (-ISO.x + ISO.z));
}
}
// MessageBox.Show("backedge after transform is: (" + BACKEDGE.x + " ," + BACKEDGE.y + " ," + BACKEDGE.z + ")");
// MessageBox.Show("frontedge after transform is: (" + FRONTEDGE.x + " ," + FRONTEDGE.y + " ," + FRONTEDGE.z + ")");
// MessageBox.Show("leftedge after transform is: (" + LEFTEDGE.x + " ," + LEFTEDGE.y + " ," + LEFTEDGE.z + ")");
// MessageBox.Show("rightedge after transform is: (" + RIGHTEDGE.x + " ," + RIGHTEDGE.y + " ," + RIGHTEDGE.z + ")");
Vector3d Ri = new Vector3d(RIGHTEDGE.x, RIGHTEDGE.y, RIGHTEDGE.z); //0 5 9 13
Vector3d Le = new Vector3d(LEFTEDGE.x, LEFTEDGE.y, LEFTEDGE.z); //1 6 10 14
Vector3d Ba = new Vector3d(BACKEDGE.x, BACKEDGE.y, BACKEDGE.z); //2 7 11 15
Vector3d Fr = new Vector3d(FRONTEDGE.x, FRONTEDGE.y, FRONTEDGE.z); //3 8 12 16
Vector3d Ce = new Vector3d(gantrycenter.x, gantrycenter.y, gantrycenter.z); //4
//System.Windows.Forms.MessageBox.Show("Trigger 6");
// MessageBox.Show("Trig");
List<Index3i> Grangle = new List<Index3i>();
Index3i shangle = new Index3i(3, 4, 0);
Grangle.Add(shangle);
shangle = new Index3i(4, 2, 0);
Grangle.Add(shangle);
shangle = new Index3i(4, 1, 2);
Grangle.Add(shangle);
shangle = new Index3i(1, 3, 4);
Grangle.Add(shangle);
DMesh3 GANTRY = new DMesh3(MeshComponents.VertexNormals);
GANTRY.AppendVertex(new NewVertexInfo(Ri));
GANTRY.AppendVertex(new NewVertexInfo(Le));
GANTRY.AppendVertex(new NewVertexInfo(Ba));
GANTRY.AppendVertex(new NewVertexInfo(Fr));
GANTRY.AppendVertex(new NewVertexInfo(Ce));
foreach (Index3i tri in Grangle)
{
GANTRY.AppendTriangle(tri);
}
Vector3d gantrynormal = GANTRY.GetTriNormal(0);
TrivialDiscGenerator makegantryhead = new TrivialDiscGenerator();
makegantryhead.Radius = 382.5f;
makegantryhead.StartAngleDeg = 0.0f;
makegantryhead.EndAngleDeg = 360.0f;
makegantryhead.Slices = 72;
makegantryhead.Generate();
DMesh3 diskgantry = makegantryhead.MakeDMesh();
MeshTransforms.Translate(diskgantry, Ce);
// IOWriteResult result40 = StandardMeshWriter.WriteFile(@"C:\Users\ztm00\Desktop\STL Files\Test\diskGantryi" + beam.Id + point.Index + ".stl", new List<WriteMesh>() { new WriteMesh(diskgantry) }, WriteOptions.Defaults);
Vector3d diskgantrynormal = diskgantry.GetTriNormal(0);
double gantrydotprod = Vector3d.Dot(diskgantrynormal.Normalized, gantrynormal.Normalized);
double anglebetweengantrynormals = Math.Acos(gantrydotprod); // in radians
// MessageBox.Show("angle between: " + anglebetweengantrynormals);
// this changes with couch rotation. (0,0,1) at couch zero degrees. implement coordinate system transformation.
Vector3d zaxisgd = new Vector3d(0, 0, 1);
double zaxisgd_xp = 0.0;
double zaxisgd_zp = 0.0;
if (CouchEndAngle >= 270.0 & CouchEndAngle <= 360.0)
{
ANGLE = 360.0 - CouchEndAngle;
// counterclockwise rotation
zaxisgd_xp = (zaxisgd.x * Math.Cos(((-ANGLE * Math.PI) / 180.0))) + (zaxisgd.z * Math.Sin(((-ANGLE * Math.PI) / 180.0)));
zaxisgd_zp = (zaxisgd.z * Math.Cos(((-ANGLE * Math.PI) / 180.0))) - (zaxisgd.x * Math.Sin(((-ANGLE * Math.PI) / 180.0)));
}
else if (CouchEndAngle >= 0.0 & CouchEndAngle <= 90.0)
{
ANGLE = CouchEndAngle;
//clockwise rotation
zaxisgd_xp = (zaxisgd.x * Math.Cos(((ANGLE * Math.PI) / 180.0))) + (zaxisgd.z * Math.Sin(((ANGLE * Math.PI) / 180.0)));
zaxisgd_zp = (zaxisgd.z * Math.Cos(((ANGLE * Math.PI) / 180.0))) - (zaxisgd.x * Math.Sin(((ANGLE * Math.PI) / 180.0)));
}
zaxisgd.x = zaxisgd_xp;
zaxisgd.z = zaxisgd_zp;
if (GantryAngle > 180.0)
{
anglebetweengantrynormals = -1 * anglebetweengantrynormals;
}
Vector3d ISOV = new Vector3d(Ce.x, Ce.y, Ce.z);
Quaterniond diskrot = new Quaterniond(zaxisgd, (anglebetweengantrynormals * MathUtil.Rad2Deg));
MeshTransforms.Rotate(diskgantry, ISOV, diskrot);
if (beam.TreatmentOrientation == "HeadFirstProne" || beam.TreatmentOrientation == "FeetFirstProne")
{
Vector3d g3ISO = new Vector3d(ISO.x, ISO.y, ISO.z);
Vector3d ZaxisPatOrientRot = new Vector3d(0, 0, 1);
Quaterniond PatOrientRot = new Quaterniond(ZaxisPatOrientRot, 180.0);
MeshTransforms.Rotate(diskgantry, g3ISO, PatOrientRot);
}
IOWriteResult result42 = StandardMeshWriter.WriteFile(@"C:\Users\ztm00\Desktop\STL Files\CollisionCheck\DiskGantry\diskgantry" + beam.beamId + GantryAngle + ".stl", new List<WriteMesh>() { new WriteMesh(diskgantry) }, WriteOptions.Defaults);
tempbeam.Add(new WriteMesh(diskgantry));
// System.Windows.Forms.MessageBox.Show("Trigger 7");
// IOWriteResult result5 = StandardMeshWriter.WriteFile(@"C:\Users\ztm00\Desktop\STL Files\CollisionCheck\SquareGantry\Gantry" + beam.Id + GantryAngle + ".stl", new List<WriteMesh>() { new WriteMesh(GANTRY) }, WriteOptions.Defaults);
DMeshAABBTree3 diskgantryspatial = new DMeshAABBTree3(diskgantry);
diskgantryspatial.Build();
reportCang = 360.0 - CouchEndAngle;
if (reportCang == 360.0)
{
reportCang = 0.0;
}
// Generation of geometric model is no complete for both the patient structures and the diskgantry. The collision analysis is below
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
// couch surface check - Couch surface structure does not generate properly (its empty) because it is hollow, so ignoring it for now. Hopefully we can merge the interior and surface to get one couch structure that we can use in the future.
if (findCouchSurf == true)
{
snear_tids = diskgantryspatial.FindNearestTriangles(CouchSurfSpatial, null, out INTERSECTDIST);
System.Windows.Forms.MessageBox.Show("Tri IDs: " + snear_tids.a + ", " + snear_tids.b);
if (snear_tids.a == -1 || snear_tids.b == -1)
{
// out of range, do nothing
System.Windows.Forms.MessageBox.Show("Trigger 3");
}
else
{
System.Windows.Forms.MessageBox.Show("Trigger 3.1");
STriDist = MeshQueries.TrianglesDistance(diskgantry, snear_tids.a, PCouchsurf, snear_tids.b);
System.Windows.Forms.MessageBox.Show("Trigger 3.2");
ZABSDIST = ABSDISTANCE(STriDist.Triangle0Closest, STriDist.Triangle1Closest);
System.Windows.Forms.MessageBox.Show("Trigger 4");
if (ZABSDIST <= 50.0)
{
//System.Windows.Forms.MessageBox.Show("PATBOX collision");
System.Windows.Forms.MessageBox.Show("Trigger 5");
if (MRGPATBOX == null)
{
collist.Add(new CollisionAlert { beam = beam.Id, gantryangle = Math.Round(GantryAngle, 0, MidpointRounding.AwayFromZero), couchangle = Math.Round(CouchEndAngle, 1, MidpointRounding.AwayFromZero), distance = Math.Round(ZABSDIST, 0, MidpointRounding.AwayFromZero), type = "Couch Surface", contiguous = false });
MRGPATBOX = GantryAngle;
lastcontigreal = false;
}
else if ((MRGPATBOX >= GantryAngle - 15.0) & (MRGPATBOX <= GantryAngle + 15.0))
{
// contiguous collisions, do not report
collist.Add(new CollisionAlert { beam = beam.Id, gantryangle = Math.Round((double)MRGPATBOX, 0, MidpointRounding.AwayFromZero), couchangle = Math.Round(CouchEndAngle, 1, MidpointRounding.AwayFromZero), distance = Math.Round(ZABSDIST, 0, MidpointRounding.AwayFromZero), type = "Couch Surface", contiguous = true });
MRGPATBOX = GantryAngle;
lastcontigreal = true;
// System.Windows.Forms.MessageBox.Show("Lastcontigreal set true");
}
else
{
if (lastcontigreal == true)
{
// System.Windows.Forms.MessageBox.Show("Last contig fire");
collist.Add(new CollisionAlert { beam = beam.Id, gantryangle = Math.Round(GantryAngle, 0, MidpointRounding.AwayFromZero), couchangle = Math.Round(CouchEndAngle, 1, MidpointRounding.AwayFromZero), distance = Math.Round(ZABSDIST, 0, MidpointRounding.AwayFromZero), type = "Couch Surface", contiguous = false, lastcontig = true });
MRGPATBOX = GantryAngle;
lastcontigreal = false;
}
else
{
collist.Add(new CollisionAlert { beam = beam.Id, gantryangle = Math.Round(GantryAngle, 0, MidpointRounding.AwayFromZero), couchangle = Math.Round(CouchEndAngle, 1, MidpointRounding.AwayFromZero), distance = Math.Round(ZABSDIST, 0, MidpointRounding.AwayFromZero), type = "Couch Surface", contiguous = false });
MRGPATBOX = GantryAngle;
lastcontigreal = false;
}
}
}
else if (lastcontigreal == true)
{
collist.Add(new CollisionAlert { beam = beam.Id, gantryangle = Math.Round(GantryAngle, 0, MidpointRounding.AwayFromZero), couchangle = Math.Round(CouchEndAngle, 1, MidpointRounding.AwayFromZero), distance = Math.Round(ZABSDIST, 0, MidpointRounding.AwayFromZero), type = "Couch Surface", contiguous = false, lastcontig = true });
lastcontigreal = false;
}
}
}
*/
//lock (ProgOutput)
//{
// ProgOutput.AppendText(Environment.NewLine);
// ProgOutput.AppendText("Beam " + beam.Id + " collision reporting....");
//}
// System.Windows.Forms.MessageBox.Show("Trigger 6");
// couch interior collision check-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
bool couchex = beam.couchexists;
bool boardex = beam.breastboardexists;
if (couchex == true)
{
snear_tids = diskgantryspatial.FindNearestTriangles(PCouchInteriorSpatial, null, out INTERSECTDIST);
// System.Windows.Forms.MessageBox.Show("Trigger 7");
if (snear_tids.a == DMesh3.InvalidID || snear_tids.b == DMesh3.InvalidID)
{
// out of range, do nothing
// System.Windows.Forms.MessageBox.Show("Trigger 8");
}
else
{
STriDist = MeshQueries.TrianglesDistance(diskgantry, snear_tids.a, PCouchInterior, snear_tids.b);
ZABSDIST = ABSDISTANCE(STriDist.Triangle0Closest, STriDist.Triangle1Closest);
// System.Windows.Forms.MessageBox.Show("Trigger 9");
if (ZABSDIST <= 50.0)
{
//System.Windows.Forms.MessageBox.Show("Couch less than 50");
if (MRGCINTERIOR == null)
{
collist.Add(new CollisionAlert { beam = beam.beamId, gantryangle = Math.Round(GantryAngle, 0, MidpointRounding.AwayFromZero), rotationdirection = beam.gantrydirection, couchangle = Math.Round(reportCang, 0, MidpointRounding.AwayFromZero), distance = Math.Round(ZABSDIST, 0, MidpointRounding.AwayFromZero), type = "Couch Interior", contiguous = false, lastcontig = false, endoflist = false });
MRGCINTERIOR = GantryAngle;
lastcontigrealCouch = true;
//System.Windows.Forms.MessageBox.Show("First couch collision Angle: " + GantryAngle);
}
else if ((MRGCINTERIOR >= GantryAngle - 15.0) & (MRGCINTERIOR <= GantryAngle + 15.0))
{
if (cp == true & ((GAngleList.Count - gantrylistCNT) <= 3))
{
// if at the end of the gantry angle list,
collist.Add(new CollisionAlert { beam = beam.beamId, gantryangle = Math.Round(GAngleList.Last(), 0, MidpointRounding.AwayFromZero), rotationdirection = beam.gantrydirection, couchangle = Math.Round(reportCang, 0, MidpointRounding.AwayFromZero), distance = Math.Round(ZABSDIST, 0, MidpointRounding.AwayFromZero), type = "Couch Interior", contiguous = false, lastcontig = false, endoflist = true });
//System.Windows.Forms.MessageBox.Show("Couch: End of gantry angle list Angle: " + GantryAngle);
}
else if (cp == false & ((GAngleList.Count - gantrylistCNT) <= 4))
{
collist.Add(new CollisionAlert { beam = beam.beamId, gantryangle = Math.Round(GAngleList.Last(), 0, MidpointRounding.AwayFromZero), rotationdirection = beam.gantrydirection, couchangle = Math.Round(reportCang, 0, MidpointRounding.AwayFromZero), distance = Math.Round(ZABSDIST, 0, MidpointRounding.AwayFromZero), type = "Couch Interior", contiguous = false, lastcontig = false, endoflist = true });
//System.Windows.Forms.MessageBox.Show("Couch: End of gantry angle list Angle: " + GantryAngle);
}
else
{
// contiguous collisions, do not report
collist.Add(new CollisionAlert { beam = beam.beamId, gantryangle = Math.Round((double)MRGCINTERIOR, 0, MidpointRounding.AwayFromZero), rotationdirection = beam.gantrydirection, couchangle = Math.Round(reportCang, 0, MidpointRounding.AwayFromZero), distance = Math.Round(ZABSDIST, 0, MidpointRounding.AwayFromZero), type = "Couch Interior", contiguous = true, lastcontig = false, endoflist = false });
MRGCINTERIOR = GantryAngle;
lastcontigrealCouch = true;
//System.Windows.Forms.MessageBox.Show("Couch: Within 15 degrees of last collision, meaning it is contiguous, don't report Angle: " + GantryAngle);
}
}
else
{
collist.Add(new CollisionAlert { beam = beam.beamId, gantryangle = Math.Round(GantryAngle, 0, MidpointRounding.AwayFromZero), rotationdirection = beam.gantrydirection, couchangle = Math.Round(reportCang, 0, MidpointRounding.AwayFromZero), distance = Math.Round(ZABSDIST, 0, MidpointRounding.AwayFromZero), type = "Couch Interior", contiguous = false, lastcontig = false, endoflist = false });
MRGCINTERIOR = GantryAngle;
lastcontigrealCouch = true;
//System.Windows.Forms.MessageBox.Show("Couch: NOT Within 15 degrees of last collision, not contiguous, start of new collision area Angle: " + GantryAngle);
}
}
else if (ZABSDIST >= 50.0 & lastcontigrealCouch == true)
{
collist.Add(new CollisionAlert { beam = beam.beamId, gantryangle = Math.Round(GantryAngle, 0, MidpointRounding.AwayFromZero), rotationdirection = beam.gantrydirection, couchangle = Math.Round(reportCang, 0, MidpointRounding.AwayFromZero), distance = Math.Round(ZABSDIST, 0, MidpointRounding.AwayFromZero), type = "Couch Interior", contiguous = false, lastcontig = true, endoflist = false });
lastcontigrealCouch = false;
//System.Windows.Forms.MessageBox.Show("Couch: greater than 50, but last angle was a collison, so end of collision area Angle: " + GantryAngle);
}
}
}
// System.Windows.Forms.MessageBox.Show("Trigger 10");
//prone breast board collision check-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if (boardex == true)
{
snear_tids = diskgantryspatial.FindNearestTriangles(PProne_Brst_BoardSpatial, null, out INTERSECTDIST);
// System.Windows.Forms.MessageBox.Show("Trigger 11");
if (snear_tids.a == DMesh3.InvalidID || snear_tids.b == DMesh3.InvalidID)
{
// out of range, do nothing
// System.Windows.Forms.MessageBox.Show("Trigger 12");
}
else
{
STriDist = MeshQueries.TrianglesDistance(diskgantry, snear_tids.a, PProne_Brst_Board, snear_tids.b);
ZABSDIST = ABSDISTANCE(STriDist.Triangle0Closest, STriDist.Triangle1Closest);
// System.Windows.Forms.MessageBox.Show("Trigger 13");
if (ZABSDIST <= 50.0)
{
//System.Windows.Forms.MessageBox.Show("PATBOX collision");
if (MRGBBOARD == null)
{
collist.Add(new CollisionAlert { beam = beam.beamId, gantryangle = Math.Round(GantryAngle, 0, MidpointRounding.AwayFromZero), rotationdirection = beam.gantrydirection, couchangle = Math.Round(reportCang, 0, MidpointRounding.AwayFromZero), distance = Math.Round(ZABSDIST, 0, MidpointRounding.AwayFromZero), type = "Prone Breast Board", contiguous = false, lastcontig = false, endoflist = false });
MRGBBOARD = GantryAngle;
lastcontigrealBoard = true;
}
else if ((MRGBBOARD >= GantryAngle - 15.0) & (MRGBBOARD <= GantryAngle + 15.0))
{
if (cp == true & ((GAngleList.Count - gantrylistCNT) <= 3))
{
// if at the end of the gantry angle list,
collist.Add(new CollisionAlert { beam = beam.beamId, gantryangle = Math.Round(GAngleList.Last(), 0, MidpointRounding.AwayFromZero), rotationdirection = beam.gantrydirection, couchangle = Math.Round(reportCang, 0, MidpointRounding.AwayFromZero), distance = Math.Round(ZABSDIST, 0, MidpointRounding.AwayFromZero), type = "Prone Breast Board", contiguous = false, lastcontig = false, endoflist = true });
//System.Windows.Forms.MessageBox.Show("Couch: End of gantry angle list Angle: " + GantryAngle);
}
else if (cp == false & ((GAngleList.Count - gantrylistCNT) <= 4))
{
collist.Add(new CollisionAlert { beam = beam.beamId, gantryangle = Math.Round(GAngleList.Last(), 0, MidpointRounding.AwayFromZero), rotationdirection = beam.gantrydirection, couchangle = Math.Round(reportCang, 0, MidpointRounding.AwayFromZero), distance = Math.Round(ZABSDIST, 0, MidpointRounding.AwayFromZero), type = "Prone Breast Board", contiguous = false, lastcontig = false, endoflist = true });
//System.Windows.Forms.MessageBox.Show("Couch: End of gantry angle list Angle: " + GantryAngle);
}
else
{
// contiguous collisions, do not report
collist.Add(new CollisionAlert { beam = beam.beamId, gantryangle = Math.Round((double)MRGBBOARD, 0, MidpointRounding.AwayFromZero), rotationdirection = beam.gantrydirection, couchangle = Math.Round(reportCang, 0, MidpointRounding.AwayFromZero), distance = Math.Round(ZABSDIST, 0, MidpointRounding.AwayFromZero), type = "Prone Breast Board", contiguous = true, lastcontig = false, endoflist = false });
MRGBBOARD = GantryAngle;
lastcontigrealBoard = true;
// System.Windows.Forms.MessageBox.Show("Lastcontigreal set true");
}