63 for (
int j=0; j<
N; j++) {
75 const string &Name,
bool O) :
EGS_BaseGeometry(Name), N(G.size()), ortho(O) {
78 for (
int j=0; j<
N; j++) {
89 EGS_NDGeometry::~EGS_NDGeometry() {
90 for (
int j=0; j<
N; j++) {
100 void EGS_NDGeometry::setup() {
105 void EGS_NDGeometry::printInfo()
const {
108 for (
int j=0; j<
N; j++)
110 j+1,
g[j]->
getName().c_str(),
g[j]->getType().c_str());
112 "=======================================================\n");
120 int err = i->
getInput(
"set medium",inp);
123 if (inp.size() == 2) {
126 else if (inp.size() == 3) {
129 else if (inp.size() == 4)
setMedium(inp[0],inp[1],mind[inp[2]],
131 else if (inp.size() == 2*
N+1) {
132 setM(0,0,inp,mind[inp[2*
N]]);
134 else egsWarning(
"EGS_NDGeometry::setMedia(): found %dinputs\n"
135 "in a 'set medium' input. 2, 3, 4, or %d are allowed\n",2*
N+1);
137 else egsWarning(
"EGS_NDGeometry::setMedia(): wrong 'set medium'"
142 void EGS_NDGeometry::setM(
int ibase,
int idim,
143 const vector<int> &ranges,
int medium) {
144 int istart = ranges[2*idim], iend = ranges[2*idim+1];
148 int ndim =
n[idim+1] /
n[idim];
152 if (idim <
N-1)
for (
int j=istart; j<=iend; j++) {
153 setM(ibase+j*
n[idim],idim+1,ranges,
medium);
155 else for (
int j=istart; j<=iend; j++) {
162 string EGS_XYZGeometry::type =
"EGS_XYZGeometry";
166 xp(Xp), yp(Yp), zp(Zp) {
175 xpos = xp->getPositions();
176 ypos = yp->getPositions();
177 zpos = zp->getPositions();
180 EGS_XYZGeometry::~EGS_XYZGeometry() {
192 void EGS_XYZGeometry::printInfo()
const {
198 "=======================================================\n");
201 int EGS_XYZGeometry::medIndex(
char medium) {
202 std::string medIdx =
"123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
203 size_t pos = medIdx.find(
medium);
204 if (pos == std::string::npos) {
207 return static_cast<int>(pos);
210 void EGS_XYZGeometry::setMedia(
EGS_Input *input,
int nmed,
const int *mind) {
215 int err = i->
getInput(
"set medium",inp);
218 if (inp.size() == 2) {
221 else if (inp.size() == 3) {
224 else if (inp.size() == 4)
setMedium(inp[0],inp[1],mind[inp[2]],
226 else if (inp.size() == 7) {
227 int is = inp[0], ie = inp[1];
234 int js = inp[2], je = inp[3];
241 int ks = inp[4], ke = inp[5];
245 if (ke >
nreg/nxy-1) {
250 for (
int i=is; i<=ie; i++) {
251 for (
int j=js; j<=je; j++) {
252 for (
int k=ks; k<=ke; k++) {
253 int ireg = i + j*nx + k*nxy;
261 else if (inp.size() == 10) {
262 int is = inp[0], ie = inp[1], idelta = inp[2];
272 int js = inp[3], je = inp[4], jdelta = inp[5];
282 int ks = inp[6], ke = inp[7], kdelta = inp[8];
286 if (ke >
nreg/nxy-1) {
294 for (
int i=is; i<=ie; i+=idelta) {
295 for (
int j=js; j<=je; j+=jdelta) {
296 for (
int k=ks; k<=ke; k+=kdelta) {
297 int ireg = i + j*nx + k*nxy;
305 else egsWarning(
"EGS_XYZGeometry::setMedia(): found %dinputs\n"
306 "in a 'set medium' input. 2, 3, 4, 7, or 10 are allowed\n");
308 else egsWarning(
"EGS_XYZGeometry::setMedia(): wrong 'set medium'"
313 EGS_XYZGeometry *EGS_XYZGeometry::constructGeometry(
const char *dens_file,
314 const char *ramp_file,
int dens_or_egsphant_or_interfile) {
315 const static char *func =
"EGS_XYZGeometry::constructGeometry";
317 if (!dens_file || (dens_or_egsphant_or_interfile != 1 && !ramp_file)) {
321 ifstream ramp(ramp_file);
322 if (dens_or_egsphant_or_interfile != 1 && !ramp) {
323 egsWarning(
"%s: failed to open CT ramp file %s\n",func,ramp_file);
326 vector<string> med_names;
327 vector<EGS_Float> rho_min,rho_max,rho_def;
329 if (dens_or_egsphant_or_interfile == 2) {
333 for (
int i=0; i < meds; i++) {
335 EGS_Float rmin,rmax,rdef;
336 ramp >> rmin >> rmax >> medname;
337 rdef=(rmin+rmax) / 2.0;
338 if (ramp.eof() || ramp.fail() || !ramp.good()) {
341 egsInformation(
"Using medium %s for rho=%g...%g\n",medname.c_str(),
343 med_names.push_back(medname);
344 rho_min.push_back(rmin);
345 rho_max.push_back(rmax+1);
346 rho_def.push_back(rdef);
348 if (med_names.size() < 1) {
349 egsWarning(
"%s: no media defined in the CT ramp "
350 "file %s!\n",func,ramp_file);
354 else if (dens_or_egsphant_or_interfile == 0) {
356 for (EGS_I64 loopCount=0; loopCount<=
loopMax; ++loopCount) {
358 egsFatal(
"EGS_XYZGeometry::constructGeometry: Too many iterations were required! Input may be invalid, or consider increasing loopMax.");
363 EGS_Float rmin,rmax,rdef;
364 ramp >> medname >> rmin >> rmax >> rdef;
365 if (ramp.eof() || ramp.fail() || !ramp.good()) {
368 egsInformation(
"Using medium %s for rho=%g...%g\n",medname.c_str(),
370 med_names.push_back(medname);
371 rho_min.push_back(rmin);
372 rho_max.push_back(rmax);
373 rho_def.push_back(rdef);
375 if (med_names.size() < 1) {
376 egsWarning(
"%s: no media defined in the CT ramp "
377 "file %s!\n",func,ramp_file);
383 EGS_Float *xx, *yy, *zz;
385 if (dens_or_egsphant_or_interfile == 0) {
387 if (my_endian < 0)
egsFatal(
"%s: machine has an unknown"
388 " endianess!\n",func);
389 ifstream dens(dens_file,ios::binary);
391 egsWarning(
"%s: failed to open density matrix file "
392 "%s\n",func,dens_file);
396 dens.read(&their_endian,1);
397 if (their_endian != 0 && their_endian != 1)
398 egsFatal(
"%s: density data created on a machine with"
399 " unknown endianess!\n",func);
400 bool swap = my_endian != their_endian;
401 dens.read((
char *) &Nx,
sizeof(
int));
405 dens.read((
char *) &Ny,
sizeof(
int));
409 dens.read((
char *) &Nz,
sizeof(
int));
413 if (Nx < 1 || Ny < 1 || Nz < 1) {
414 egsWarning(
"%s: invalid density matrix file: "
415 " Nx=%d Ny=%d Nz=%d\n",func,Nx,Ny,Nz);
418 float *x =
new float [Nx+1];
419 float *y =
new float [Ny+1];
420 float *z =
new float [Nz+1];
421 dens.read((
char *)x, (Nx+1)*
sizeof(
float));
422 if (dens.eof() || dens.fail() || !dens.good()) {
423 egsWarning(
"%s: error while reading %d x-planes from"
424 " file %s\n",func,Nx+1,dens_file);
430 dens.read((
char *)y, (Ny+1)*
sizeof(
float));
431 if (dens.eof() || dens.fail() || !dens.good()) {
432 egsWarning(
"%s: error while reading %d y-planes from"
433 " file %s\n",func,Ny+1,dens_file);
439 dens.read((
char *)z, (Nz+1)*
sizeof(
float));
440 if (dens.eof() || dens.fail() || !dens.good()) {
441 egsWarning(
"%s: error while reading %d z-planes from"
442 " file %s\n",func,Nz+1,dens_file);
450 for (j=0; j<Nx; j++) {
453 for (j=0; j<Ny; j++) {
456 for (j=0; j<Nz; j++) {
462 xx =
new EGS_Float [Nx+1];
463 for (j=0; j<=Nx; j++) {
466 yy =
new EGS_Float [Ny+1];
467 for (j=0; j<=Ny; j++) {
470 zz =
new EGS_Float [Nz+1];
471 for (j=0; j<=Nz; j++) {
478 rho =
new float [Nx*Ny*Nz];
479 dens.read((
char *)rho, Nx*Ny*Nz*
sizeof(
float));
481 egsWarning(
"%s: failed reading mass densities from density matrux file\n",func);
489 for (
int j=0; j<Nx*Ny*Nz; j++) {
494 else if (dens_or_egsphant_or_interfile == 2) {
495 ifstream h_file(dens_file);
497 egsWarning(
"%s: failed to open the interfile header %s\n",func,dens_file);
500 string data_file(
"");
502 float scale_x=0.0, scale_y=0.0, scale_z=0.0;
503 for (EGS_I64 loopCount=0; loopCount<=
loopMax; ++loopCount) {
505 egsFatal(
"EGS_XYZGeometry::constructGeometry: Too many iterations were required! Input may be invalid, or consider increasing loopMax.");
508 string line, key, value;
510 getline(h_file, line);
511 if (h_file.eof() || h_file.fail() || !h_file.good()) {
515 pos = line.find(
":=");
516 if (pos == string::npos) {
519 key = line.substr(0,
int(pos));
520 value = line.substr(
int(pos)+2);
521 while ((key[0] ==
'!') || (key[0] ==
' ')) {
524 while (key[key.length()-1] ==
' ') {
525 key.erase(key.length()-1, 1);
527 while (value[0] ==
' ') {
530 while (value[value.length()-1] ==
' ') {
531 value.erase(value.length()-1, 1);
534 if (key ==
"matrix size [1]") {
535 sscanf(value.c_str(),
"%u", &Nx);
537 else if (key ==
"matrix size [2]") {
538 sscanf(value.c_str(),
"%u", &Ny);
540 else if ((key ==
"number of slices") || (key ==
"number of images")) {
541 sscanf(value.c_str(),
"%u", &Nz);
543 else if (key ==
"scaling factor (mm/pixel) [1]") {
544 sscanf(value.c_str(),
"%f", &scale_x);
546 else if (key ==
"scaling factor (mm/pixel) [2]") {
547 sscanf(value.c_str(),
"%f", &scale_y);
549 else if (key ==
"slice thickness (pixels)") {
550 sscanf(value.c_str(),
"%f", &scale_z);
552 else if (key ==
"name of data file") {
555 else if (key ==
"number format") {
556 if ((value ==
"float") || (value ==
"FLOAT")) {
559 else if ((value ==
"unsigned integer") || (value ==
"UNSIGNED INTEGER")) {
563 egsWarning(
"%s: unrecognised 'number format' type: %s \n",func,value.c_str());
567 if (Nx < 1 || Ny < 1 || Nz < 1 || scale_x <= 0.0 || scale_y <= 0.0 || scale_z <= 0.0 || data_file ==
"" || data_type == -1) {
568 egsWarning(
"%s: invalid interfile header information: "
569 "Nx=%d Ny=%d Nz=%d scale_x=%f scale_y=%f scale_z=%f "
570 "data_file='%s' number_format=%d\n",func,Nx,Ny,Nz,scale_x,scale_y,scale_z,data_file.c_str(),data_type);
573 ifstream i_file(data_file.c_str(),ios::binary);
575 egsWarning(
"%s: failed to open interfile data "
576 "%s\n",func,data_file.c_str());
584 xx =
new EGS_Float [Nx+1];
585 for (j=0; j<=Nx; j++) {
586 xx[j] = -(float)Nx*scale_x/2.0 + j*scale_x;
588 yy =
new EGS_Float [Ny+1];
589 for (j=0; j<=Ny; j++) {
590 yy[j] = -(float)Ny*scale_y/2.0 + j*scale_y;
592 zz =
new EGS_Float [Nz+1];
593 for (j=0; j<=Nz; j++) {
594 zz[j] = -(float)Nz*scale_z/2.0 + j*scale_z;
597 rho =
new float [Nx*Ny*Nz];
598 if (data_type == 0) {
599 i_file.read((
char *)rho, Nx*Ny*Nz*
sizeof(float));
602 unsigned short int *rho_tmp =
new unsigned short int [Nx*Ny*Nz];
603 i_file.read((
char *)rho_tmp, (Nx*Ny*Nz)*
sizeof(
unsigned short int));
604 for (
int cc = 0; cc<Nx*Ny*Nz; cc++) {
605 rho[cc] = (float)(rho_tmp[cc]);
610 egsWarning(
"%s: failed reading interfile data\n",func);
620 ifstream tempf(dens_file, ios::binary);
628 egsWarning(
"%s: failed to open .egsphant file %s\n",func,dens_file);
632 bool is_gzip = (tempf.get() == 0x1f && tempf.get() == 0x8b);
637 binf.open(dens_file);
640 egsWarning(
"Tried to read gzipped egsphant but egs_ndgeometry was not compiled with gzip support\n");
645 textf.open(dens_file);
652 if ((*data).fail()) {
653 egsWarning(
"%s: failed reading number of media\n",func);
658 (*data).getline(buf,1023);
659 for (imed=0; imed<nmed; ++imed) {
660 (*data).getline(buf,1023);
662 if ((*data).fail()) {
663 egsWarning(
"%s: failed reading medium name for %d'th medium\n",func,imed+1);
666 med_names.push_back(
string(buf));
669 med_names.back().c_str(),med_names.size());
673 for (imed=0; imed<nmed; ++imed) {
676 (*data) >> Nx >> Ny >> Nz;
677 if ((*data).fail()) {
678 egsWarning(
"%s: failed reading number of voxels\n",func);
681 xx =
new EGS_Float [Nx+1];
682 yy =
new EGS_Float [Ny+1];
683 zz =
new EGS_Float [Nz+1];
685 for (j=0; j<=Nx; ++j) {
688 if ((*data).fail()) {
689 egsWarning(
"%s: failed reading x-planes\n",func);
695 for (j=0; j<=Ny; ++j) {
698 if ((*data).fail()) {
699 egsWarning(
"%s: failed reading y-planes\n",func);
705 for (j=0; j<=Nz; ++j) {
708 if ((*data).fail()) {
709 egsWarning(
"%s: failed reading z-planes\n",func);
722 for (
int i=0; i<Nz; ++i) {
723 for (j=0; j<Ny; ++j) {
728 if (medLine.size() < Nx) {
729 egsWarning(
"%s: Number of media per line in correct.\n",func);
736 for (
int k=0; k<Nx; ++k) {
737 int index = i * (Ny * Nx) + j * Nx + k;
740 int medIndex = result->medIndex(medLine[k]);
742 medName = med_names[medIndex];
754 (*data).getline(buf,1023);
766 if ((*data).fail()) {
767 egsWarning(
"%s: failed reading media indeces matrix\n",func);
784 EGS_Float rhomin=1e30,rhomax=0;
786 for (j=0; j<Nx*Ny*Nz; j++) {
787 if (rho[j] > rhomax) {
790 if (rho[j] < rhomin) {
796 int nmed = med_names.size();
797 int *imed =
new int [nmed];
798 for (j=0; j<nmed; j++) {
801 for (j=0; j<Nx*Ny*Nz; j++) {
803 for (i=0; i<nmed; i++) {
804 if (rho[j] >= rho_min[i] && rho[j] < rho_max[i]) {
809 egsWarning(
"%s: no material defined for density %g, leaving voxel %d"
810 " as vacuum\n",func,rho[j],j);
817 med_names[i].c_str(),imed[i]);
820 if (rho_def[i] > 0) {
821 EGS_Float rrho = rho[j]/rho_def[i];
833 void EGS_XYZGeometry::finishInitialization() {
836 if (dens_or_egsphant_or_interfile != 1 || useEgsphantDensities ==
false) {
839 const static char *func =
"EGS_XYZGeometry::finishInitialization";
841 ifstream tempf(dens_file, ios::binary);
849 egsWarning(
"%s: failed to open .egsphant file %s\n",func,dens_file.c_str());
853 bool is_gzip = (tempf.get() == 0x1f && tempf.get() == 0x8b);
858 binf.open(dens_file);
861 egsWarning(
"Tried to read gzipped egsphant but egs_ndgeometry was not compiled with gzip support\n");
866 textf.open(dens_file);
872 if ((*data).fail()) {
873 egsWarning(
"%s: failed reading number of media\n",func);
879 (*data).getline(buf,1023);
880 for (imed=0; imed<nmed; ++imed) {
881 (*data).getline(buf,1023);
883 if ((*data).fail()) {
884 egsWarning(
"%s: failed reading medium name for %d'th medium\n",func,imed+1);
890 for (imed=0; imed<nmed; ++imed) {
894 (*data) >> Nx >> Ny >> Nz;
895 if ((*data).fail()) {
896 egsWarning(
"%s: failed reading number of voxels\n",func);
901 for (j=0; j<=Nx; ++j) {
904 if ((*data).fail()) {
905 egsWarning(
"%s: failed reading x-planes\n",func);
908 for (j=0; j<=Ny; ++j) {
911 if ((*data).fail()) {
912 egsWarning(
"%s: failed reading y-planes\n",func);
915 for (j=0; j<=Nz; ++j) {
918 if ((*data).fail()) {
919 egsWarning(
"%s: failed reading z-planes\n",func);
924 for (
int i=0; i<Nz; ++i) {
925 for (j=0; j<Ny; ++j) {
927 (*data).getline(buf,1023);
931 (*data).getline(buf,1023);
934 for (j=0; j<nr; ++j) {
938 EGS_Float rrho = rho / getMediumRho(
medium(j));
943 if ((*data).fail()) {
944 egsWarning(
"%s: failed reading mass density matrix\n",func);
951 string EGS_DeformedXYZ::def_type =
"EGS_DeformedXYZ";
953 char EGS_DeformedXYZ::tetrahedra[] = {0,2,3,6, 0,6,3,7, 0,4,6,7,
954 7,0,1,3, 7,1,0,4, 7,5,1,4
957 char EGS_DeformedXYZ::plane_order[] = {0,1,2, 0,2,3, 0,3,1, 1,3,2};
959 char EGS_DeformedXYZ::enter_tetra1[] = {0, 3, 4, 0, 0, 2};
960 char EGS_DeformedXYZ::enter_plane1[] = {1, 1, 1, 3, 0, 3};
961 char EGS_DeformedXYZ::enter_tetra2[] = {2, 5, 5, 1, 3, 5};
962 char EGS_DeformedXYZ::enter_plane2[] = {0, 0, 2, 3, 2, 2};
965 char EGS_DeformedXYZ::tetra_data[] = {
999 EGS_PlanesZ *Zp,
const char *defFile,
const string &Name) :
1001 setDeformations(defFile);
1005 nxyp1 = nx + ny + 1;
1009 ifstream data(defFile,ios::binary);
1011 egsWarning(
"Failed to open deformations file %s\n",defFile);
1015 data.read((
char *)&nvec,
sizeof(
int));
1016 int nneed = (nx+1)*(ny+1)*(nz+1);
1017 if (nvec != nneed) {
1018 egsWarning(
"Inconsistent deformations file: %d instead of %d vectors\n",
1027 for (j=0; j<nvec; ++j) {
1028 data.read((
char *)tmp,3*
sizeof(
float));
1030 egsWarning(
"Error while readinf vector %d from file\n",j+1);
1033 vectors[j] =
EGS_Vector(tmp[0],tmp[1],tmp[2]);
1037 for (k=0; k<=nz; ++k)
for (j=0; j<=ny; ++j)
for (i=0; i<=nx; ++i) {
1038 int index = i + j*(nx+1) + k*(nx+1)*(ny+1);
1039 vectors[index] +=
EGS_Vector(xpos[i],ypos[j],zpos[k]);
1042 for (j=0; j<24; ++j) {
1043 int i = tetrahedra[j];
1058 tnodes[j] = (nx+1)*(ny+1);
1061 tnodes[j] = (nx+1)*(ny+1)+1;
1064 tnodes[j] = (nx+1)*(ny+1)+nx+1;
1067 tnodes[j] = (nx+1)*(ny+1)+nx+2;
1077 EGS_DeformedXYZ::~EGS_DeformedXYZ() {
1083 string EGS_XYZRepeater::type =
"EGS_XYZRepeater";
1085 EGS_XYZRepeater::EGS_XYZRepeater(EGS_Float xmin, EGS_Float xmax,
1086 EGS_Float ymin, EGS_Float ymax,
1087 EGS_Float zmin, EGS_Float zmax,
1088 int Nx,
int Ny,
int Nz,
1090 const string &Name) :
1092 dx = (xmax - xmin)/nx;
1095 dy = (ymax - ymin)/ny;
1098 dz = (zmax - zmin)/nz;
1115 for (
int ix=0; ix<nx; ix++)
1116 for (
int iy=0; iy<ny; iy++)
1117 for (
int iz=0; iz<nz; iz++)
1118 translation[ix+iy*nx+iz*nxy] =
EGS_Vector(xmin + dx*(0.5+ix),
1120 zmin + dz*(0.5+iz));
1124 EGS_XYZRepeater::~EGS_XYZRepeater() {
1128 if (!xyz->
deref()) {
1131 delete [] translation;
1134 void EGS_XYZRepeater::printInfo()
const {
1137 nx,xyz->getXPositions()[0],xyz->getXPositions()[nx]);
1139 ny,xyz->getYPositions()[0],xyz->getYPositions()[ny]);
1141 nz,xyz->getZPositions()[0],xyz->getZPositions()[nz]);
1153 void EGS_XYZGeometry::voxelizeGeometry(
EGS_Input *input) {
1155 int err = input->
getInput(
"voxelize geometry",gname);
1160 egsInformation(
" setting media from geometry %s\n",gname.c_str());
1163 egsInformation(
" this geometry does not exist -> media will be not set.\n");
1198 EGS_Vector v1(xp->position(0),yp->position(0),zp->position(0));
1199 EGS_Vector v2(xp->position(nx),yp->position(ny),zp->position(nz));
1200 egsInformation(
" top/left/front corner : (%g,%g,%g)\n",v1.x,v1.y,v1.z);
1201 egsInformation(
" bottom/right/back corner: (%g,%g,%g)\n",v2.x,v2.y,v2.z);
1203 egsInformation(
" the above are transformed as follows before looking up media:\n");
1206 egsInformation(
" top/left/front corner : (%g,%g,%g)\n",v1.x,v1.y,v1.z);
1207 egsInformation(
" bottom/right/back corner: (%g,%g,%g)\n",v2.x,v2.y,v2.z);
1209 for (
int ix=0; ix<nx; ++ix)
for (
int iy=0; iy<ny; iy++)
for (
int iz=0; iz<nz; ++iz) {
1210 int ir = ix + iy*nx + iz*nxy;
1211 EGS_Vector v(0.5*(xp->position(ix)+xp->position(ix+1)),
1212 0.5*(yp->position(iy)+yp->position(iy+1)),
1213 0.5*(zp->position(iz)+zp->position(iz+1)));
1217 int ireg = geometry->
isWhere(v);
1237 vector<string> options;
1238 options.push_back(
"no");
1239 options.push_back(
"yes");
1240 bool delete_geometry = input->
getInput(
"delete geometry",options,0);
1241 if (delete_geometry) {
1242 if (geometry->
deref() == -1) {
1247 egsInformation(
" not deleting geometry %s as requested due to remaining references\n",
1255 const char *err_msg1 =
"createGeometry(EGS_XYZRepeater)";
1259 static string EGS_NDG_LOCAL typeStr(
"EGS_NDGeometry");
1262 static bool EGS_NDG_LOCAL inputSet =
false;
1265 static void setInputs() {
1268 setBaseGeometryInputs();
1270 geomBlockInput->getSingleInput(
"library")->setValues({
"egs_ndgeometry"});
1273 auto typePtr = geomBlockInput->addSingleInput(
"type",
false,
"Alternative types of nd_geometry. Neglect this input to use a standard egs_nd_geometry.", {
"EGS_XYZGeometry",
"EGS_XYZRepeater"});
1275 auto dimPtr = geomBlockInput->addSingleInput(
"dimensions",
true,
"A list of previously defined geometries.");
1276 dimPtr->addDependency(typePtr,
"",
true);
1277 auto hownPtr = geomBlockInput->addSingleInput(
"hownear method",
false,
"0(for orthogonal constituent geometries) or 1");
1278 hownPtr->addDependency(typePtr,
"",
true);
1282 auto xPtr = geomBlockInput->addSingleInput(
"x-planes",
false,
"A list of the x-plane positions");
1283 xPtr->addDependency(typePtr,
"EGS_XYZGeometry");
1284 auto yPtr = geomBlockInput->addSingleInput(
"y-planes",
false,
"A list of the y-plane positions");
1285 yPtr->addDependency(typePtr,
"EGS_XYZGeometry");
1286 auto zPtr = geomBlockInput->addSingleInput(
"z-planes",
false,
"A list of the z-plane positions");
1287 zPtr->addDependency(typePtr,
"EGS_XYZGeometry");
1290 auto densityPtr = geomBlockInput->addSingleInput(
"density matrix",
false,
"A binary file containing the density for every region. See the documentation for format details.");
1291 densityPtr->addDependency(typePtr,
"EGS_XYZGeometry");
1292 auto ctPtr = geomBlockInput->addSingleInput(
"ct ramp",
false,
"A ramp file for converting density to medium.");
1293 ctPtr->addDependency(typePtr,
"EGS_XYZGeometry");
1294 auto phantPtr = geomBlockInput->addSingleInput(
"egsphant file",
false,
"An egsphant file, as defined in the DOSXYZnrc documentation.");
1295 phantPtr->addDependency(typePtr,
"EGS_XYZGeometry");
1298 densityPtr->addDependency(phantPtr,
"",
true);
1299 phantPtr->addDependency(densityPtr,
"",
true);
1300 ctPtr->addDependency(densityPtr);
1303 auto xslabPtr = geomBlockInput->addSingleInput(
"x-slabs",
false,
"Xo Dx Nx");
1304 xslabPtr->addDependency(typePtr,
"EGS_XYZGeometry");
1305 auto yslabPtr = geomBlockInput->addSingleInput(
"y-slabs",
false,
"Yo Dy Ny");
1306 yslabPtr->addDependency(typePtr,
"EGS_XYZGeometry");
1307 auto zslabPtr = geomBlockInput->addSingleInput(
"z-slabs",
false,
"Zo Dz Nz");
1308 zslabPtr->addDependency(typePtr,
"EGS_XYZGeometry");
1311 xPtr->addDependency(densityPtr,
"",
true);
1312 yPtr->addDependency(densityPtr,
"",
true);
1313 zPtr->addDependency(densityPtr,
"",
true);
1314 xPtr->addDependency(ctPtr,
"",
true);
1315 yPtr->addDependency(ctPtr,
"",
true);
1316 zPtr->addDependency(ctPtr,
"",
true);
1317 xPtr->addDependency(phantPtr,
"",
true);
1318 yPtr->addDependency(phantPtr,
"",
true);
1319 zPtr->addDependency(phantPtr,
"",
true);
1320 xPtr->addDependency(xslabPtr,
"",
true);
1321 yPtr->addDependency(xslabPtr,
"",
true);
1322 zPtr->addDependency(xslabPtr,
"",
true);
1323 xPtr->addDependency(yslabPtr,
"",
true);
1324 yPtr->addDependency(yslabPtr,
"",
true);
1325 zPtr->addDependency(yslabPtr,
"",
true);
1326 xPtr->addDependency(zslabPtr,
"",
true);
1327 yPtr->addDependency(zslabPtr,
"",
true);
1328 zPtr->addDependency(zslabPtr,
"",
true);
1329 densityPtr->addDependency(xPtr,
"",
true);
1330 ctPtr->addDependency(xPtr,
"",
true);
1331 phantPtr->addDependency(xPtr,
"",
true);
1332 densityPtr->addDependency(yPtr,
"",
true);
1333 ctPtr->addDependency(yPtr,
"",
true);
1334 phantPtr->addDependency(yPtr,
"",
true);
1335 densityPtr->addDependency(zPtr,
"",
true);
1336 ctPtr->addDependency(zPtr,
"",
true);
1337 phantPtr->addDependency(zPtr,
"",
true);
1338 densityPtr->addDependency(xslabPtr,
"",
true);
1339 ctPtr->addDependency(xslabPtr,
"",
true);
1340 phantPtr->addDependency(xslabPtr,
"",
true);
1341 densityPtr->addDependency(yslabPtr,
"",
true);
1342 ctPtr->addDependency(yslabPtr,
"",
true);
1343 phantPtr->addDependency(yslabPtr,
"",
true);
1344 densityPtr->addDependency(zslabPtr,
"",
true);
1345 ctPtr->addDependency(zslabPtr,
"",
true);
1346 phantPtr->addDependency(zslabPtr,
"",
true);
1347 xslabPtr->addDependency(xPtr,
"",
true);
1348 xslabPtr->addDependency(yPtr,
"",
true);
1349 xslabPtr->addDependency(zPtr,
"",
true);
1350 xslabPtr->addDependency(densityPtr,
"",
true);
1351 xslabPtr->addDependency(ctPtr,
"",
true);
1352 xslabPtr->addDependency(phantPtr,
"",
true);
1353 yslabPtr->addDependency(xPtr,
"",
true);
1354 yslabPtr->addDependency(yPtr,
"",
true);
1355 yslabPtr->addDependency(zPtr,
"",
true);
1356 yslabPtr->addDependency(densityPtr,
"",
true);
1357 yslabPtr->addDependency(ctPtr,
"",
true);
1358 yslabPtr->addDependency(phantPtr,
"",
true);
1359 zslabPtr->addDependency(xPtr,
"",
true);
1360 zslabPtr->addDependency(yPtr,
"",
true);
1361 zslabPtr->addDependency(zPtr,
"",
true);
1362 zslabPtr->addDependency(densityPtr,
"",
true);
1363 zslabPtr->addDependency(ctPtr,
"",
true);
1364 zslabPtr->addDependency(phantPtr,
"",
true);
1368 auto regeomPtr = geomBlockInput->addSingleInput(
"repeated geometry",
true,
"The name of a previously defined geometry");
1369 regeomPtr->addDependency(typePtr,
"EGS_XYZRepeater");
1370 auto medPtr = geomBlockInput->addSingleInput(
"medium",
false,
"The medium the space between xmin..xmax, ymin..ymax, and zmin..zmax is filled with");
1371 medPtr->addDependency(typePtr,
"EGS_XYZRepeater");
1372 auto rexPtr = geomBlockInput->addSingleInput(
"repeat x",
true,
"xmin xmax Nx");
1373 rexPtr->addDependency(typePtr,
"EGS_XYZRepeater");
1374 auto reyPtr = geomBlockInput->addSingleInput(
"repeat y",
true,
"ymin ymax Ny");
1375 reyPtr->addDependency(typePtr,
"EGS_XYZRepeater");
1376 auto rezPtr = geomBlockInput->addSingleInput(
"repeat z",
true,
"zmin zmax Nz");
1377 rezPtr->addDependency(typePtr,
"EGS_XYZRepeater");
1380 EGS_NDG_EXPORT
string getExample() {
1384 # Example of egs_ndgeometry
1386 library = EGS_NDGeometry
1387 name = my_ndgeometry
1388 dimensions = geom1 geom2
1397 EGS_NDG_EXPORT shared_ptr<EGS_BlockInput> getInputs() {
1401 return geomBlockInput;
1406 const static char *func =
"createGeometry(XYZ)";
1408 int is_xyz = input->
getInput(
"type",type);
1409 if (!is_xyz && input->
compare(
"EGS_XYZRepeater",type)) {
1411 vector<EGS_Float> xr, yr, zr;
1412 int err1 = input->
getInput(
"repeated geometry",base);
1413 int err2 = input->
getInput(
"repeat x",xr);
1414 int err3 = input->
getInput(
"repeat y",yr);
1415 int err4 = input->
getInput(
"repeat z",zr);
1416 int err5 = input->
getInput(
"medium",medium);
1419 egsWarning(
"%s: missing 'repeated geometry' input\n",err_msg1);
1424 egsWarning(
"%s: no geometry named %s exists\n",err_msg1,
1429 if (err2 || xr.size() != 3) {
1431 egsWarning(
"%s: wrong/missing 'repeat x' input\n",err_msg1);
1433 if (err3 || yr.size() != 3) {
1435 egsWarning(
"%s: wrong/missing 'repeat y' input\n",err_msg1);
1437 if (err4 || zr.size() != 3) {
1439 egsWarning(
"%s: wrong/missing 'repeat z' input\n",err_msg1);
1444 EGS_Float xmin = xr[0], xmax = xr[1];
1445 int nx = (int)(xr[2]+0.1);
1446 if (xmin >= xmax || nx < 1) {
1447 egsWarning(
"%s: wrong 'repeat x' input xmin=%g xmax=%g nx=%d\n",
1451 EGS_Float ymin = yr[0], ymax = yr[1];
1452 int ny = (int)(yr[2]+0.1);
1453 if (ymin >= ymax || ny < 1) {
1454 egsWarning(
"%s: wrong 'repeat y' input ymin=%g ymax=%g ny=%d\n",
1458 EGS_Float zmin = zr[0], zmax = zr[1];
1459 int nz = (int)(zr[2]+0.1);
1460 if (zmin >= zmax || nz < 1) {
1461 egsWarning(
"%s: wrong 'repeat z' input zmin=%g zmax=%g nz=%d\n",
1476 result->setXYZLabels(input);
1480 else if (!is_xyz && input->
compare(
"EGS_XYZGeometry",type)) {
1481 string dens_file, ramp_file, egsphant_file, interfile_file;
1482 int ierr1 = input->
getInput(
"density matrix",dens_file);
1483 int ierr2 = input->
getInput(
"ct ramp",ramp_file);
1484 int ierr3 = input->
getInput(
"egsphant file",egsphant_file);
1485 int ierr4 = input->
getInput(
"interfile header",interfile_file);
1486 int dens_or_egsphant_or_interfile = -1;
1488 dens_or_egsphant_or_interfile = 0;
1491 dens_or_egsphant_or_interfile = 1;
1492 dens_file = egsphant_file;
1495 dens_or_egsphant_or_interfile = 2;
1496 dens_file = interfile_file;
1498 if (dens_or_egsphant_or_interfile >= 0 || !ierr2) {
1499 if (dens_or_egsphant_or_interfile < 0) {
1500 egsWarning(
"%s: no 'density matrix', 'egsphant file' or 'interfile header' input\n",func);
1503 if (dens_or_egsphant_or_interfile != 1 && ierr2) {
1508 EGS_XYZGeometry::constructGeometry(dens_file.c_str(),ramp_file.c_str(),dens_or_egsphant_or_interfile);
1511 result->dens_file = dens_file;
1512 result->dens_or_egsphant_or_interfile = dens_or_egsphant_or_interfile;
1517 if (dens_or_egsphant_or_interfile == 1) {
1518 vector<string> options;
1519 options.push_back(
"no");
1520 options.push_back(
"yes");
1521 bool useEgsphantDensities = input->
getInput(
"use egsphant densities",options,1);
1522 result->useEgsphantDensities = useEgsphantDensities;
1528 vector<EGS_Float> xpos, ypos, zpos, xslab, yslab, zslab;
1529 int ix = input->
getInput(
"x-planes",xpos);
1530 int iy = input->
getInput(
"y-planes",ypos);
1531 int iz = input->
getInput(
"z-planes",zpos);
1532 int ix1 = input->
getInput(
"x-slabs",xslab);
1533 int iy1 = input->
getInput(
"y-slabs",yslab);
1534 int iz1 = input->
getInput(
"z-slabs",zslab);
1535 int nx=0, ny=0, nz=0;
1537 if (xslab.size() != 3) {
1538 egsWarning(
"createGeometry(XYZ): exactly 3 inputs are required"
1539 " when using 'x-slabs' input method\n");
1543 nx = (int)(xslab[2]+0.1);
1545 egsWarning(
"createGeometry(XYZ): number of slabs must be"
1549 if (xslab[1] <= 0) {
1550 egsWarning(
"createGeometry(XYZ): slab thickness must be"
1557 egsWarning(
"createGeometry(XYZ): wrong/missing 'x-planes' "
1558 "and 'x-slabs' input\n");
1562 if (yslab.size() != 3) {
1563 egsWarning(
"createGeometry(XYZ): exactly 3 inputs are required"
1564 " when using 'y-slabs' input method\n");
1568 ny = (int)(yslab[2]+0.1);
1570 egsWarning(
"createGeometry(XYZ): number of slabs must be"
1574 if (yslab[1] <= 0) {
1575 egsWarning(
"createGeometry(XYZ): slab thickness must be"
1582 egsWarning(
"createGeometry(XYZ): wrong/missing 'y-planes' "
1583 "and 'y-slabs' input\n");
1587 if (zslab.size() != 3) {
1588 egsWarning(
"createGeometry(XYZ): exactly 3 inputs are required"
1589 " when using 'z-slabs' input method\n");
1593 nz = (int)(zslab[2]+0.1);
1595 egsWarning(
"createGeometry(XYZ): number of slabs must be"
1599 if (zslab[1] <= 0) {
1600 egsWarning(
"createGeometry(XYZ): slab thickness must be"
1607 egsWarning(
"createGeometry(XYZ): wrong/missing 'z-planes' "
1608 "and 'z-slabs' input\n");
1623 egsWarning(
"**********************************************\n");
1628 result->voxelizeGeometry(input);
1631 result->setXYZLabels(input);
1637 vector<EGS_BaseGeometry *> dims;
1640 while ((ij = input->
takeInputItem(
"geometry",
false)) != 0) {
1651 vector<string> gnames;
1652 int err1 = input->
getInput(
"dimensions",gnames);
1654 for (
unsigned int j=0; j<gnames.size(); j++) {
1661 egsWarning(
"Geometry %s does not exist\n",gnames[j].c_str());
1667 egsWarning(
"createGeometry(ND_Geometry): %d errors while "
1668 "creating/geting geometries defining individual dimensions\n",error);
1671 if (dims.size() < 2) {
1672 egsWarning(
"createGeometry(ND_Geometry): why do you want to "
1673 "construct a ND geometry with a single dimension?\n");
1674 input->
print(0,cerr);
1675 for (
int j=0; j<gnames.size(); j++)
egsWarning(
"dimension %d: %s\n",
1676 j+1,gnames[j].c_str());
1679 for (
int j=0; j<dims.size(); j++)
if (!dims[j]->isConvex()) {
1684 egsWarning(
"createGeometry(ND_Geometry): a ND geometry can not have "
1685 " more than one non-convex dimension, yours has %d\n",n_concav);
1688 else if (n_concav == 1) {
1689 if (dims[dims.size()-1]->isConvex()) {
1690 egsWarning(
"createGeometry(ND_Geometry): the non-convex "
1691 "dimension must be the last dimension\n");
1696 for (
int j=0; j<dims.size(); j++)
1697 if (dims[j]->deref()) {
1703 err1 = input->
getInput(
"hownear method",hn_method);
1705 if (!err1 && hn_method == 1) {
1719 void EGS_XYZGeometry::setXYZLabels(
EGS_Input *input) {
1725 err = input->
getInput(
"set x label", inp);
1730 err = input->
getInput(
"set y label", inp);
1735 err = input->
getInput(
"set z label", inp);
1742 void EGS_NDGeometry::ndRegions(
int r,
int dim,
int dimk,
int k, vector<int> ®s) {
1751 ndRegions(r, dim+1, dimk, k, regs);
1756 else if (dim ==
N-1) {
1757 for (
int j=0; j<
g[dim]->
regions(); j++) {
1758 regs.push_back(r+j*
n[dim]);
1764 for (
int j=0; j<
g[dim]->
regions(); j++) {
1765 ndRegions(r + j*
n[dim], dim+1, dimk, k, regs);
1771 void EGS_NDGeometry::getLabelRegions(
const string &str, vector<int> ®s,
bool sanitize) {
1774 vector<int> local_regs;
1775 for (
int i=0; i<
N; i++) {
1780 if (local_regs.size() == 0) {
1785 for (
int j=0; j<local_regs.size(); j++) {
1786 ndRegions(0, 0, i, local_regs[j], regs);
1796 void EGS_XYZGeometry::getLabelRegions(
const string &str, vector<int> ®s,
bool sanitize) {
1798 vector<int> local_regs;
1803 for (
int i=0; i<local_regs.size(); i++) {
1804 for (
int j=0; j<ny; j++) {
1805 for (
int k=0; k<nz; k++) {
1806 regs.push_back(local_regs[i] + nx*j + nxy*k);
1814 for (
int j=0; j<local_regs.size(); j++) {
1815 for (
int i=0; i<nx; i++) {
1816 for (
int k=0; k<nz; k++) {
1817 regs.push_back(i + nx*local_regs[j] + nxy*k);
1825 for (
int k=0; k<local_regs.size(); k++) {
1826 for (
int i=0; i<nx; i++) {
1827 for (
int j=0; j<ny; j++) {
1828 regs.push_back(i + nx*j + nxy*local_regs[k]);
1839 void EGS_XYZRepeater::getLabelRegions(
const string &str, vector<int> ®s,
bool sanitize) {
1841 vector<int> local_regs;
1846 for (
int i=0; i<nx; i++) {
1847 for (
int j=0; j<ny; j++) {
1848 for (
int k=0; k<nz; k++) {
1849 for (
int r=0; r<local_regs.size(); r++) {
1850 regs.push_back(ng*(i + nx*j + nxy*k) + local_regs[r]);
1858 xyz->getXLabelRegions(str, local_regs, sanitize);
1859 for (
int i=0; i<local_regs.size(); i++) {
1860 for (
int j=0; j<ny; j++) {
1861 for (
int k=0; k<nz; k++) {
1862 for (
int r=0; r<ng; r++) {
1863 regs.push_back(ng*(local_regs[i] + nx*j + nxy*k) + r);
1871 xyz->getYLabelRegions(str, local_regs, sanitize);
1872 for (
int j=0; j<local_regs.size(); j++) {
1873 for (
int i=0; i<nx; i++) {
1874 for (
int k=0; k<nz; k++) {
1875 for (
int r=0; r<ng; r++) {
1876 regs.push_back(ng*(i + nx*local_regs[j] + nxy*k) + r);
1884 xyz->getZLabelRegions(str, local_regs, sanitize);
1885 for (
int k=0; k<local_regs.size(); k++) {
1886 for (
int i=0; i<nx; i++) {
1887 for (
int j=0; j<ny; j++) {
1888 for (
int r=0; r<ng; r++) {
1889 regs.push_back(ng*(i + nx*j + nxy*local_regs[k]) + r);
Base geometry class. Every geometry class must be derived from EGS_BaseGeometry.
int deref()
Decrease the reference count to this geometry.
virtual EGS_Float getBScaling(int ireg) const
Get the B field scaling factor in region ireg.
virtual void setBScaling(int start, int end, EGS_Float bf)
Set the B field scaling factor in regions.
void setMedia(EGS_Input *inp)
Set the media in the geometry from the input pointed to by inp.
static EGS_BaseGeometry * createSingleGeometry(EGS_Input *inp)
Create a single geometry from the input inp.
int nreg
Number of local regions in this geometry.
bool has_B_scaling
Does this geometry has B field scaling factor?
bool is_convex
Is this geometry convex?
bool has_rho_scaling
Does this geometry have relative mass density scvaling?
bool hasBScaling() const
Does this geometry object have a B field scaling feature?
short * region_media
Array of media indeces.
virtual bool hasRhoScaling()
Does this geometry object have a mass density scaling feature?
void setName(EGS_Input *inp)
Set the name of the geometry from the input inp.
void setMedium(const string &Name)
Set all regions to a medium with name Name.
const string & getName() const
Get the name of this geometry.
EGS_Float * rhor
Array with relative mass densities.
virtual EGS_Float getRelativeRho(int ireg) const
Get the relative mass density in region ireg.
EGS_Float * bfactor
Array with B field scaling factors.
bool isConvex() const
Is the geometry convex?
virtual int medium(int ireg) const
Returns the medium index in region ireg.
string name
Name of this geometry.
int regions() const
Returns the number of local regions in this geometry.
int setLabels(EGS_Input *input)
Set the labels from an input block.
virtual void setRelativeRho(int start, int end, EGS_Float rho)
Set the relative mass density in regions.
virtual void printInfo() const
Print information about this geometry.
int ref()
Increase the reference count to this geometry.
static EGS_BaseGeometry * getGeometry(const string &Name)
Get a pointer to the geometry named Name.
static int addMedium(const string &medname)
Add a medium or get the index of an existing medium.
static const char * getMediumName(int ind)
Get the name of medium with index ind.
void setBoundaryTolerance(EGS_Input *inp)
Set the value of the boundary tolerance from the input inp.
virtual void getLabelRegions(const string &str, vector< int > ®s, bool sanitize=true)
Get the list of all regions labeled with str.
virtual int isWhere(const EGS_Vector &x)=0
In which region is poisition x?
A class modeling a N-dimensional geometry.
static string type
The geometry type.
EGS_NDGeometry(int ng, EGS_BaseGeometry **G, const string &Name="", bool O=true)
int * n
Used for calculating region indeces.
void setMedia(EGS_Input *inp, int nmed, const int *med_ind)
Define media.
EGS_BaseGeometry ** g
The dimensions.
int N
Number of dimensions.
A set of parallel planes.
A class representing 3D vectors.
A projector into the x-plane.
A geometry repeated on a regular XYZ grid.
A projector into the y-plane.
A projector into the z-plane.
Global egspp functions header file.
EGS_GLIB_EXPORT EGS_BaseGeometry * createGeometry(EGS_Input *input)
N-dimensional geometries: header.
int egsGetEndian()
Get the endianess of the machine.
EGS_InfoFunction EGS_EXPORT egsInformation
Always use this function for reporting the progress of a simulation and any other type of information...
EGS_InfoFunction EGS_EXPORT egsFatal
Always use this function for reporting fatal errors.
const EGS_I64 loopMax
The maximum number of iterations for near-infinite loops.
const EGS_Float epsilon
The epsilon constant for floating point comparisons.
void egsSwapBytes(int *n)
Swap the bytes of 32 bit integers.
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.