EGS Brachy
An egs++ user code for rapid brachytherapy calculations
Loading...
Searching...
No Matches
/Users/marc/Developer/EGSnrc/HEN_HOUSE/user_codes/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
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 map<int, vector<int> > regions_corrected;
223
225 success(0),
226 status(""),
227 time(0),
228 density(0),
229 npoints(0),
230 bounds_volume(0),
231 other_volume(0) {};
232
234 success(0),
235 status(""),
236 time(0),
237 other_volume(0) {
238 density = opts->density;
239 npoints = opts->npoints;
241 }
242
243 void outputResults(string extra="") {
244
245 if (success == -1){
246 egsFatal("%s volume correction requested but failed. %s\n", extra.c_str(), status.c_str());
247 return;
248 }else if (success == 0){
249 egsInformation("%s volume correction not requested.\n", extra.c_str());
250 return;
251 }
252
253 egsInformation("Time taken = %.2G s\n", time);
254 egsInformation("Density of points used = %.0E points/cm^-3\n", density);
255 egsInformation("Number of source points used = %G\n", npoints);
256 egsInformation("Bounding shape volume = %.4E cm^3\n", bounds_volume);
257 if (extra != "") {
258 egsInformation("Volume of %-10s = %.4E cm^3\n", extra.c_str(), other_volume);
259 }
260
261 }
262
263};
264
266
268 string status;
269 EGS_Float time;
270 map<string, string> phantom_files;
271 map<string, int> nreg;
272
274 success(0),
275 status(""),
276 time(0) {};
277
278 FileResults(map<string, string> phant_files):
279 success(0),
280 status(""),
281 time(0),
282 phantom_files(phant_files) {};
283
285 if (success == -1){
286 egsFatal("File volume correction requested but failed: %s.\n", status.c_str());
287 return;
288 }else if (success == 0){
289 egsInformation("File volume correction not requested.\n");
290 return;
291 }
292 egsInformation("Time taken = %.2G s\n", time);
293 for (map<string, int>::iterator it=nreg.begin(); it !=nreg.end(); it++) {
294 string phant_name = it->first;
295 int npoints = it->second;
296 string file_name = phantom_files[phant_name];
297
298 egsInformation("Read %d voxel volumes for '%s' from %s\n", npoints, phant_name.c_str(), file_name.c_str());
299
300 }
301 }
302
303};
304
305
312
313 EGS_Input *input;
316 map<string, string> phantom_files;
317 vector<EB_Phantom *> phantoms;
318 EGS_BaseGeometry *base_geom;
319 //EGS_BaseGeometry *source_geom;
321 vector<EGS_AffineTransform *> transforms;
322 EGS_AffineTransform *base_transform;
323 EGS_AffineTransform *base_transform_inv;
324
325 void setupOptions();
326
328 double correctGeneralVolumes();
329 void applyVolumeCorrections(Options *options, HitCounterT hit_counter);
330 map<string, int> loadFileVolumeCorrections();
331
332public:
333 VolumeCorrector(EGS_Input *volcor_input,
334 vector<EB_Phantom *> phantoms, EGS_BaseGeometry *base_geom,
335 GeomInfo *geom_info, vector<EGS_AffineTransform *> transforms = vector<EGS_AffineTransform *>()):
336 input(volcor_input),
339 ginfo(geom_info),
340
342
343 setupOptions();
344
345 if (transforms.size()>0) {
347 base_transform_inv = new EGS_AffineTransform(transforms[0]->inverse());
348 }
349
350
351 };
352
354 if (source_opts) {
355 delete source_opts;
356 }
357
358 if (gen_opts) {
359 delete gen_opts;
360 }
361 if (base_transform_inv) {
362 delete base_transform_inv;
363 }
364 }
365
367 timer.addTimer("VolumeCorrector::runSourceCorrection");
368 Results results(source_opts);
369
371 results.success = -1;
372 results.status = "Invalid source correction options";
373 timer.stopTimer();
374 return results;
375 }else if (source_opts->mode == NO_CORRECTION) {
376 results.success = 0;
377 results.status = "Not requested";
378 timer.stopTimer();
379 return results;
380 }
381
382 clock_t start_time = clock();
384 clock_t end_time = clock();
385 results.time = (end_time-start_time)/(double)CLOCKS_PER_SEC;
386
387 results.success = 1;
388 results.status = "Completed";
389 timer.stopTimer();
390
391 return results;
392 }
393
395 timer.addTimer("VolumeCorrector::runGeneralCorrection");
396 Results results(gen_opts);
397
399 results.success = -1;
400 results.status = "Invalid general correction options";
401 timer.stopTimer();
402 return results;
403 }else if (gen_opts->mode == NO_CORRECTION) {
404 results.success = 0;
405 results.status = "Not requested";
406 timer.stopTimer();
407 return results;
408 }
409
410 clock_t start_time = clock();
412 clock_t end_time = clock();
413
414 results.time = (end_time-start_time)/(double)CLOCKS_PER_SEC;
415 results.status = "Completed";
416 results.success = 1;
417
418 timer.stopTimer();
419 return results;
420 }
421
423 timer.addTimer("VolumeCorrector::runFileCorrection");
424 vector<int> nreg_corrected;
425 FileResults results(phantom_files);
426
427 if (phantom_files.size()==0) {
428 results.success = 0;
429 results.status = "Not requested";
430 timer.stopTimer();
431 return results;
432 }
433
434 clock_t start_time = clock();
436 clock_t end_time = clock();
437 results.time = (end_time-start_time)/(double)CLOCKS_PER_SEC;
438
439 results.status = "Completed";
440 results.success = 1;
441 timer.stopTimer();
442
443 return results;
444 };
445
446};
447
448}
449
450#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
An object for controlling the volume correction routine.
Definition eb_volcor.h:311
map< string, int > loadFileVolumeCorrections()
Results runSourceCorrection(EB_TimingTree &timer)
Definition eb_volcor.h:366
EGS_BaseGeometry * base_geom
Definition eb_volcor.h:318
vector< EGS_AffineTransform * > transforms
Definition eb_volcor.h:321
void applyVolumeCorrections(Options *options, HitCounterT hit_counter)
EGS_AffineTransform * base_transform_inv
Definition eb_volcor.h:323
Results runGeneralCorrection(EB_TimingTree &timer)
Definition eb_volcor.h:394
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:333
vector< EB_Phantom * > phantoms
Definition eb_volcor.h:317
FileResults runFileCorrection(EB_TimingTree &timer)
Definition eb_volcor.h:422
EGS_AffineTransform * base_transform
Definition eb_volcor.h:322
map< string, string > phantom_files
Definition eb_volcor.h:316
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:270
FileResults(map< string, string > phant_files)
Definition eb_volcor.h:278
map< string, int > nreg
Definition eb_volcor.h:271
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:233
EGS_Float other_volume
Definition eb_volcor.h:220
EGS_Float bounds_volume
Definition eb_volcor.h:219
void outputResults(string extra="")
Definition eb_volcor.h:243
map< int, vector< int > > regions_corrected
Definition eb_volcor.h:222