EGS Brachy
An egs++ user code for rapid brachytherapy calculations
Loading...
Searching...
No Matches
/home/runner/work/egs_brachy/egs_brachy/egs_brachy/eb_volcor.h
Go to the documentation of this file.
1/*
2################################################################################
3#
4# egs_brachy eb_volcor.h
5# Copyright (C) 2016 Rowan Thomson, Dave Rogers, Randle Taylor, and Marc
6# Chamberland
7#
8# This file is part of egs_brachy
9#
10# egs_brachy is free software: you can redistribute it and/or modify it
11# under the terms of the GNU Affero General Public License as published
12# by the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# egs_brachy is distributed in the hope that it will be useful, but
16# WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18# Affero General Public License for more details:
19# <http://www.gnu.org/licenses/>.
20#
21################################################################################
22#
23# When egs_brachy is used for publications, please cite our paper:
24# M. J. P. Chamberland, R. E. P. Taylor, D. W. O. Rogers, and R. M. Thomson,
25# egs brachy: a versatile and fast Monte Carlo code for brachytherapy,
26# Phys. Med. Biol. 61, 8214-8231 (2016).
27#
28################################################################################
29#
30# Author: Randle Taylor, 2016
31#
32# Contributors: Marc Chamberland
33# Dave Rogers
34# Rowan Thomson
35#
36################################################################################
37*/
38
56
57
58#ifndef EB_VOLCOR_
59#define EB_VOLCOR_
60
61#include <map>
62#include <set>
63#include <cstdlib>
64
65#include "egs_functions.h"
66#include "egs_input.h"
67#include "egs_rndm.h"
68#include "egs_shapes.h"
69
70#include "phantom.h"
71#include "ginfo.h"
72#include "timing.h"
73
74#include "egs_autoenvelope/egs_sobol.h" // required for Superposition mode
75
76namespace ebvolcor {
77
79
81EGS_Float getShapeVolume(EGS_Input *shape_inp);
82
86typedef pair<int, int> PhantRegT;
87
89struct RegVolume {
90 int ir;
91 EGS_Float vol;
92 EGS_Float unc;
93};
94
97typedef std::map<PhantRegT, EGS_I64> HitCounterT;
98
99
145class Options {
146
147 const static unsigned long DEFAULT_RAND_POINT_DENSITY = 100000000;
148
149 EGS_Input *input;
150
151
152 EGS_BaseShape *bounds;
154
155
156 EGS_RandomGenerator *rng;
157
158 void setMode();
159 int setBoundsShape();
160 void setDensity();
161 void setRNG();
162 void setCoveredThreshold();
163
164public:
165 Options(EGS_Input *inp):
166
167 input(inp), bounds(NULL), sobolAllowed(false), rng(NULL) {
168
169 valid = true;
170
171 setMode();
172
173 if (!inp) {
174 valid = false;
175 return;
176 }
177
178 int err = setBoundsShape();
179 if (err) {
180 valid = false;
181 return;
182 }
183
184 setDensity();
185 setRNG();
187
188 }
189
191 if (rng) {
192 delete rng;
193 }
194 if (bounds) {
195 delete bounds;
196 }
197 }
198
199 bool valid;
200
201 EGS_Float bounds_volume;
202 EGS_Float density;
203 EGS_Float npoints;
206
207 EGS_Vector getRandomPoint();
208};
209
210
212struct Results {
213
215 string status;
216 EGS_Float time;
217 double density;
218 double npoints;
219 EGS_Float bounds_volume;
220 EGS_Float other_volume;
222
223 map<int, vector<int> > regions_corrected;
224
226 success(0),
227 status(""),
228 time(0),
229 density(0),
230 npoints(0),
231 bounds_volume(0),
232 other_volume(0),
234
236 success(0),
237 status(""),
238 time(0),
239 other_volume(0) {
240 density = opts->density;
241 npoints = opts->npoints;
244 opts->covered_threshold * 100. : opts->covered_threshold;
245 }
246
247 void outputResults(string extra="") {
248
249 if (success == -1){
250 egsFatal("%s volume correction requested but failed. %s\n", extra.c_str(), status.c_str());
251 return;
252 }else if (success == 0){
253 egsInformation("%s volume correction not requested.\n", extra.c_str());
254 return;
255 }
256
257 egsInformation("Time taken = %.2G s\n", time);
258 egsInformation("Total coverage threshold %% = %.4G\n", covered_threshold_pct);
259 egsInformation("Density of points used = %.0E points/cm^-3\n", density);
260 egsInformation("Number of source points used = %G\n", npoints);
261 egsInformation("Bounding shape volume = %.4E cm^3\n", bounds_volume);
262 if (extra != "") {
263 egsInformation("Volume of %-10s = %.4E cm^3\n", extra.c_str(), other_volume);
264 }
265
266 }
267
268};
269
271
273 string status;
274 EGS_Float time;
275 map<string, string> phantom_files;
276 map<string, int> nreg;
277
279 success(0),
280 status(""),
281 time(0) {};
282
283 FileResults(map<string, string> phant_files):
284 success(0),
285 status(""),
286 time(0),
287 phantom_files(phant_files) {};
288
290 if (success == -1){
291 egsFatal("File volume correction requested but failed: %s.\n", status.c_str());
292 return;
293 }else if (success == 0){
294 egsInformation("File volume correction not requested.\n");
295 return;
296 }
297 egsInformation("Time taken = %.2G s\n", time);
298 for (map<string, int>::iterator it=nreg.begin(); it !=nreg.end(); it++) {
299 string phant_name = it->first;
300 int npoints = it->second;
301 string file_name = phantom_files[phant_name];
302
303 egsInformation("Read %d voxel volumes for '%s' from %s\n", npoints, phant_name.c_str(), file_name.c_str());
304
305 }
306 }
307
308};
309
310
317
318 EGS_Input *input;
321 map<string, string> phantom_files;
322 vector<EB_Phantom *> phantoms;
323 EGS_BaseGeometry *base_geom;
324 //EGS_BaseGeometry *source_geom;
326 vector<EGS_AffineTransform *> transforms;
327 EGS_AffineTransform *base_transform;
328 EGS_AffineTransform *base_transform_inv;
329
330 void setupOptions();
331
333 double correctGeneralVolumes();
334 void applyVolumeCorrections(Options *options, HitCounterT hit_counter);
335 map<string, int> loadFileVolumeCorrections();
336
337public:
338 VolumeCorrector(EGS_Input *volcor_input,
339 vector<EB_Phantom *> phantoms, EGS_BaseGeometry *base_geom,
340 GeomInfo *geom_info, vector<EGS_AffineTransform *> transforms = vector<EGS_AffineTransform *>()):
341 input(volcor_input),
344 ginfo(geom_info),
345
347
348 setupOptions();
349
350 if (transforms.size()>0) {
352 base_transform_inv = new EGS_AffineTransform(transforms[0]->inverse());
353 }
354
355
356 };
357
359 if (source_opts) {
360 delete source_opts;
361 }
362
363 if (gen_opts) {
364 delete gen_opts;
365 }
366 if (base_transform_inv) {
367 delete base_transform_inv;
368 }
369 }
370
372 timer.addTimer("VolumeCorrector::runSourceCorrection");
373 Results results(source_opts);
374
375 if (source_opts->mode != NO_CORRECTION && !source_opts->valid){
376 results.success = -1;
377 results.status = "Invalid source correction options";
378 timer.stopTimer();
379 return results;
380 }else if (source_opts->mode == NO_CORRECTION) {
381 results.success = 0;
382 results.status = "Not requested";
383 timer.stopTimer();
384 return results;
385 }
386
387 clock_t start_time = clock();
389 clock_t end_time = clock();
390 results.time = (end_time-start_time)/(double)CLOCKS_PER_SEC;
391
392 results.success = 1;
393 results.status = "Completed";
394 timer.stopTimer();
395
396 return results;
397 }
398
400 timer.addTimer("VolumeCorrector::runGeneralCorrection");
401 Results results(gen_opts);
402
403 if (gen_opts->mode != NO_CORRECTION && !gen_opts->valid){
404 results.success = -1;
405 results.status = "Invalid general correction options";
406 timer.stopTimer();
407 return results;
408 }else if (gen_opts->mode == NO_CORRECTION) {
409 results.success = 0;
410 results.status = "Not requested";
411 timer.stopTimer();
412 return results;
413 }
414
415 clock_t start_time = clock();
417 clock_t end_time = clock();
418
419 results.time = (end_time-start_time)/(double)CLOCKS_PER_SEC;
420 results.status = "Completed";
421 results.success = 1;
422
423 timer.stopTimer();
424 return results;
425 }
426
428 timer.addTimer("VolumeCorrector::runFileCorrection");
429 vector<int> nreg_corrected;
430 FileResults results(phantom_files);
431
432 if (phantom_files.size()==0) {
433 results.success = 0;
434 results.status = "Not requested";
435 timer.stopTimer();
436 return results;
437 }
438
439 clock_t start_time = clock();
441 clock_t end_time = clock();
442 results.time = (end_time-start_time)/(double)CLOCKS_PER_SEC;
443
444 results.status = "Completed";
445 results.success = 1;
446 timer.stopTimer();
447
448 return results;
449 };
450
451};
452
453}
454
455#endif
void stopTimer()
Definition timing.h:139
void addTimer(string name)
Definition timing.h:132
a container for organizing meta data about the geometries
Definition ginfo.h:99
Volume correction initialization helper class.
Definition eb_volcor.h:145
EGS_Float density
Definition eb_volcor.h:202
static const unsigned long DEFAULT_RAND_POINT_DENSITY
Definition eb_volcor.h:147
EGS_Float covered_threshold
Definition eb_volcor.h:205
EGS_BaseShape * bounds
Definition eb_volcor.h:152
EGS_Input * input
Definition eb_volcor.h:149
int setBoundsShape()
create bounding shape from the shape input and calculate its volume
Options(EGS_Input *inp)
Definition eb_volcor.h:165
EGS_Float bounds_volume
Definition eb_volcor.h:201
void setCoveredThreshold()
EGS_Vector getRandomPoint()
VolCorMode mode
Definition eb_volcor.h:204
void setMode()
read mode from input
EGS_Float npoints
Definition eb_volcor.h:203
EGS_RandomGenerator * rng
Definition eb_volcor.h:156
map< string, int > loadFileVolumeCorrections()
Results runSourceCorrection(EB_TimingTree &timer)
Definition eb_volcor.h:371
EGS_BaseGeometry * base_geom
Definition eb_volcor.h:323
vector< EGS_AffineTransform * > transforms
Definition eb_volcor.h:326
void applyVolumeCorrections(Options *options, HitCounterT hit_counter)
EGS_AffineTransform * base_transform_inv
Definition eb_volcor.h:328
Results runGeneralCorrection(EB_TimingTree &timer)
Definition eb_volcor.h:399
double correctPhantomVolumesForSources()
VolumeCorrector(EGS_Input *volcor_input, vector< EB_Phantom * > phantoms, EGS_BaseGeometry *base_geom, GeomInfo *geom_info, vector< EGS_AffineTransform * > transforms=vector< EGS_AffineTransform * >())
Definition eb_volcor.h:338
vector< EB_Phantom * > phantoms
Definition eb_volcor.h:322
FileResults runFileCorrection(EB_TimingTree &timer)
Definition eb_volcor.h:427
EGS_AffineTransform * base_transform
Definition eb_volcor.h:327
map< string, string > phantom_files
Definition eb_volcor.h:321
ginfo contains classes for organizing information about the geometries present in an egs_brachy simul...
pair< int, int > PhantRegT
PhantRegT is a pair of the form (PhantomNumber, PhantomRegion) e.g. a pair of (2, 12) would represent...
Definition eb_volcor.h:86
std::map< PhantRegT, EGS_I64 > HitCounterT
HitCounterT is used for counting how many random points land in a given phantoms region.
Definition eb_volcor.h:97
@ NO_CORRECTION
Definition eb_volcor.h:78
@ CORRECT_VOLUME
Definition eb_volcor.h:78
@ ZERO_DOSE
Definition eb_volcor.h:78
EGS_Float getShapeVolume(EGS_Input *shape_inp)
get shape volume from a shape input item
Definition eb_volcor.cpp:77
Header file for phantom objects.
map< string, string > phantom_files
Definition eb_volcor.h:275
FileResults(map< string, string > phant_files)
Definition eb_volcor.h:283
map< string, int > nreg
Definition eb_volcor.h:276
RegVolumeT sruct with members (ir=RegionNumber, vol=Volume, unc=Unc)
Definition eb_volcor.h:89
Struct used to collect and output results about a volume correction run.
Definition eb_volcor.h:212
Results(Options *opts)
Definition eb_volcor.h:235
EGS_Float other_volume
Definition eb_volcor.h:220
EGS_Float bounds_volume
Definition eb_volcor.h:219
EGS_Float covered_threshold_pct
Definition eb_volcor.h:221
void outputResults(string extra="")
Definition eb_volcor.h:247
map< int, vector< int > > regions_corrected
Definition eb_volcor.h:223