/// <summary> /// Run IMRT optimization for a given plan. /// </summary> public static void Optimize(ExternalPlanSetup plan) { plan.SetCalculationModel(CalculationType.PhotonIMRTOptimization, OptimizationAlgorithm); var opt = new OptimizationOptionsIMRT(NumberOfIterationsForIMRTOptimization, OptimizationOption.RestartOptimization, OptimizationConvergenceOption.TerminateIfConverged, MlcId); Trace.WriteLine("\nOptimizing...\n"); var res = plan.Optimize(opt); if (!res.Success) { var message = string.Format("Optimization failed for plan '{0}'", plan.Id); throw new Exception(message); } plan.SetCalculationModel(CalculationType.PhotonVolumeDose, DoseCalculationAlgorithm); plan.SetCalculationModel(CalculationType.PhotonLeafMotions, LeafMotionCalculator); Trace.WriteLine("\nCalculating leaf motions...\n"); var calcRes = plan.CalculateLeafMotions(); if (!res.Success) { var message = string.Format("Leaf motion calculation failed for plan '{0}'. Output:\n{1}", plan.Id, calcRes); throw new Exception(message); } }
private void createMlcField(float[,] flncMtx, float rotAng) { int height = flncMtx.GetLength(0); int width = flncMtx.GetLength(1); double xOrg = -0.5 * ((double)width * flncRes - flncRes); double yOrg = 0.5 * ((double)height * flncRes - flncRes); // Create fluence Fluence mlcFlnc = new Fluence(flncMtx, xOrg, yOrg); // Add beam Beam currBm = currPln.AddMLCBeam(currMachParam, new float[2, 60], new VRect <double>(-50.0, -50.0, 50.0, 50.0), rotAng, 0.0, 0.0, currIsoCtr); currBm.SetOptimalFluence(mlcFlnc); currPln.CalculateLeafMotions(vlmcOpt); }
public static string calcularMovimientoLaminas(ExternalPlanSetup plan) { var watch = Stopwatch.StartNew(); plan.SetCalculationModel(CalculationType.PhotonVolumeDose, Configuracion.DoseCalculationAlgorithm); plan.SetCalculationModel(CalculationType.PhotonLeafMotions, Configuracion.LeafMotionCalculator); var res = plan.CalculateLeafMotions(); if (!res.Success) { return(string.Format("Falló el cálculo de movimiento de láminas para el plan '{0}'. Output:\n{1}", plan.Id, res)); } else { watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; return(string.Format("Finalizó el cálculo de movimiento de láminas para el plan '{0}'. Demoró " + elapsedMs.ToString() + "ms", plan.Id)); } }
public void Execute(ScriptContext context /*, System.Windows.Window window*/) { Patient patient = context.Patient; if (patient == null) { throw new ApplicationException("Please load a patient."); } // Get active structureset StructureSet ss = context.StructureSet; if (ss == null) { throw new ApplicationException("Please load a structure set."); } // Check that unique body structure, PTV and Rectum structures exist Structure body = ss.Structures.Where(o => o.Id == "BODY").FirstOrDefault(); Structure ptv = ss.Structures.Where(o => o.Id == "PTV").FirstOrDefault(); Structure rectum = ss.Structures.Where(o => o.Id == "Rectum1").FirstOrDefault(); if (body == null || ptv == null || rectum == null) { throw new ApplicationException(string.Format("Cannot find required structures (BODY, PTV, Rectum) from Structureset '{0}'", ss.Id)); } // Enable modifications patient.BeginModifications(); // Get or create course with Id 'Superplan' const string courseId = "Superplan"; Course course = patient.Courses.Where(o => o.Id == courseId).SingleOrDefault(); if (course == null) { course = patient.AddCourse(); course.Id = courseId; } // Create a new plan ExternalPlanSetup plan = course.AddExternalPlanSetup(ss); int fractions = 20; double prescribedPercentage = 1.0; DoseValue fractiondose = new DoseValue(2.50, DoseValue.DoseUnit.Gy); // TODO: if needed change to cGy to match your system configuration plan.UniqueFractionation.SetPrescription(fractions, fractiondose, prescribedPercentage); // Add fields const int nfields = 5; ExternalBeamMachineParameters parameters = new ExternalBeamMachineParameters("Varian 23EX", "6X", 600, "STATIC", null); // TODO: change machine id to yours VVector isocenter = ptv.CenterPoint; for (int n = 0; n < nfields; n++) { // add a 10 cm x 10 cm field Beam beam = plan.AddStaticBeam(parameters, new VRect <double>(-50, -50, 50, 50), 0, Math.Round(360.0 / nfields * n, 0), 0, isocenter); } // end of first part. MessageBox.Show("Plan created, open course SuperPlan, Choose Plan1 to view results.", "Varian Developer"); // Set optimization constraints OptimizationSetup optimizationSetup = plan.OptimizationSetup; optimizationSetup.AddPointObjective(ptv, OptimizationObjectiveOperator.Lower, new DoseValue(49.50, DoseValue.DoseUnit.Gy), 100, 100.0); optimizationSetup.AddPointObjective(ptv, OptimizationObjectiveOperator.Upper, new DoseValue(52.00, DoseValue.DoseUnit.Gy), 0, 100.0); optimizationSetup.AddPointObjective(rectum, OptimizationObjectiveOperator.Upper, new DoseValue(20.00, DoseValue.DoseUnit.Gy), 40.0, 100.0); MessageBox.Show("Plan '" + plan.Id + "' in course '" + course.Id + "' for patient '" + patient.Id + "' has been created"); // optimize for 30 iterations if (!plan.Optimize(30).Success) { MessageBox.Show("Optimization failed", "Varian Developer", MessageBoxButton.OK, MessageBoxImage.Error); return; } // run LMC if (!plan.CalculateLeafMotions().Success) { MessageBox.Show("LMC failed", "Varian Developer", MessageBoxButton.OK, MessageBoxImage.Error); return; } // calculate final dose CalculationResult doseCalc = plan.CalculateDose(); if (!doseCalc.Success) { MessageBox.Show("Dose calculation failed, logs shown next.", "Varian Developer", MessageBoxButton.OK, MessageBoxImage.Error); // write calculate logs to a file and show them to the user. string filename = writeCalculationLogs(plan); // 'Start' generated TXT file to launch Notepad window System.Diagnostics.Process.Start(filename); // Sleep for a few seconds to let Excel window start System.Threading.Thread.Sleep(TimeSpan.FromSeconds(3)); return; } MessageBox.Show("Done"); }