EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_planes.cpp
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ planes geometry
5 # Copyright (C) 2015 National Research Council Canada
6 #
7 # This file is part of EGSnrc.
8 #
9 # EGSnrc is free software: you can redistribute it and/or modify it under
10 # the terms of the GNU Affero General Public License as published by the
11 # Free Software Foundation, either version 3 of the License, or (at your
12 # option) any later version.
13 #
14 # EGSnrc is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
17 # more details.
18 #
19 # You should have received a copy of the GNU Affero General Public License
20 # along with EGSnrc. If not, see <http://www.gnu.org/licenses/>.
21 #
22 ###############################################################################
23 #
24 # Author: Iwan Kawrakow, 2005
25 #
26 # Contributors: Frederic Tessier
27 #
28 ###############################################################################
29 */
30 
31 
38 #include "egs_planes.h"
39 #include "egs_input.h"
40 
41 //string XProjector::type = "EGS_Xplanes";
42 //string YProjector::type = "EGS_Yplanes";
43 //string ZProjector::type = "EGS_Zplanes";
44 //string Projector::type = "EGS_Planes";
45 const string EGS_PLANES_LOCAL xproj_type("EGS_Xplanes");
46 const string EGS_PLANES_LOCAL yproj_type("EGS_Yplanes");
47 const string EGS_PLANES_LOCAL zproj_type("EGS_Zplanes");
48 const string EGS_PLANES_LOCAL proj_type("EGS_Planes");
49 
50 string EGS_PlaneCollection::type = "EGS_PlaneCollection";
51 
52 EGS_PlaneCollection::EGS_PlaneCollection(int Np, const EGS_Float *pos,
53  const EGS_Vector *norm, const string &Name) : EGS_BaseGeometry(Name) {
54  if (Np < 2) egsFatal("EGS_PlaneCollection::EGS_PlaneCollection: "
55  " you nead at least 2 planes\n");
56  np = Np;
57  nreg=np-1;
58  planes = new EGS_Planes* [np];
59  for (int j=0; j<np; j++) {
60  planes[j] = new EGS_Planes(1,&pos[j],"",
61  EGS_Projector(norm[j],proj_type));
62  planes[j]->ref();
63  }
64 }
65 
66 EGS_PlaneCollection::~EGS_PlaneCollection() {
67  //egsWarning("Deleting ~EGS_PlaneCollection at 0x%x\n",this);
68  for (int j=0; j<np; j++) if (!planes[j]->deref()) {
69  delete planes[j];
70  }
71  delete [] planes;
72 }
73 
74 void EGS_PlaneCollection::printInfo() const {
76  int j;
77  egsInformation(" plane positions: ");
78  for (j=0; j<np; j++) {
79  egsInformation(" %g ",planes[j]->position(0));
80  }
81  egsInformation(" \nplane normals: ");
82  for (j=0; j<np; j++) {
83  EGS_Vector a = planes[j]->normal();
84  egsInformation(" (%g,%g,%g) ",a.x,a.y,a.z);
85  }
86  egsInformation("\n=====================================================\n");
87 }
88 
89 extern "C" {
90 
91  EGS_PLANES_EXPORT EGS_BaseGeometry *createGeometry(EGS_Input *input) {
92  string type;
93  int err = input->getInput("type",type);
94  if (err) {
95  egsWarning("createGeometry(planes): missing type key\n");
96  return 0;
97  }
98  EGS_Float first;
99  vector<EGS_Float> thick;
100  vector<int> nthick;
101  vector<EGS_Float> pos;
102  int err_first = input->getInput("first plane",first);
103  int err_thick = input->getInput("slab thickness",thick);
104  int err_nthick = input->getInput("number of slabs",nthick);
105  if (!err_first && !err_thick && !err_nthick) {
106  if (thick.size() != nthick.size() || !thick.size()) {
107  egsWarning("createGeometry(planes): number of 'slab thickness' and"
108  "\n 'number of slabs' inputs must be the same and not zero\n");
109  egsWarning(" got %d and %d inputs --> input ignored\n",
110  thick.size(),nthick.size());
111  }
112  else {
113  pos.push_back(first);
114  int j=0;
115  for (int i=0; i<thick.size(); i++) {
116  for (int l=0; l<nthick[i]; ++l) {
117  pos.push_back(pos[j++]+thick[i]);
118  }
119  }
120  }
121  }
122  if (!pos.size()) {
123  err = input->getInput("positions",pos);
124  if (err) {
125  egsWarning("createGeometry(planes): missing/wrong 'positions' "
126  "input and missing/wrong multiple plane input\n");
127  return 0;
128  }
129  }
130  EGS_BaseGeometry *g;
131  if (type == "EGS_Xplanes") g = new EGS_PlanesX(pos,"",
132  EGS_XProjector(xproj_type));
133  else if (type == "EGS_Yplanes") g = new EGS_PlanesY(pos,"",
134  EGS_YProjector(yproj_type));
135  else if (type == "EGS_Zplanes") g = new EGS_PlanesZ(pos,"",
136  EGS_ZProjector(zproj_type));
137  else if (type == "EGS_Planes") {
138  vector<EGS_Float> a;
139  err = input->getInput("normal",a);
140  if (err || a.size() != 3) {
141  egsWarning("createGeometry(planes): missing/wrong normal input\n");
142  return 0;
143  }
144  g = new EGS_Planes(pos,"",EGS_Projector(EGS_Vector(a[0],a[1],a[2]),
145  proj_type));
146  }
147  else if (type == "EGS_PlaneCollection") {
148  vector<EGS_Float> a;
149  err = input->getInput("normals",a);
150  if (err || a.size() < 6) {
151  egsWarning("createGeometry(planes): missing/wrong normal input\n");
152  return 0;
153  }
154  int np = a.size()/3;
155  if (np != pos.size()) {
156  egsWarning("createGeometry(planes): number of plane normals (%d)\n"
157  " is not the same as number of plane positions (%d) for a"
158  " plane collection\n",np,pos.size());
159  return 0;
160  }
161  EGS_Float *p = new EGS_Float [np];
162  EGS_Vector *normal = new EGS_Vector [np];
163  for (int j=0; j<np; j++) {
164  p[j] = pos[j];
165  normal[j] = EGS_Vector(a[3*j],a[3*j+1],a[3*j+2]);
166  }
167  g = new EGS_PlaneCollection(np,p,normal);
168  }
169  else {
170  egsWarning("createGeometry(planes): unknown type %s\n",type.c_str());
171  return 0;
172  }
173  g->setName(input);
174  g->setBoundaryTolerance(input);
175  g->setMedia(input);
176  g->setLabels(input);
177  return g;
178  }
179 
180 }
int deref()
Decrease the reference count to this geometry.
virtual void printInfo() const
Print information about this geometry.
Sets of parallel planes and a plane collection.
EGS_Input class header file.
EGS_Float y
y-component
Definition: egs_vector.h:61
A class representing 3D vectors.
Definition: egs_vector.h:56
int setLabels(EGS_Input *input)
Set the labels from an input block.
EGS_GLIB_EXPORT EGS_BaseGeometry * createGeometry(EGS_Input *input)
Definition: egs_glib.cpp:57
Base geometry class. Every geometry class must be derived from EGS_BaseGeometry.
void setBoundaryTolerance(EGS_Input *inp)
Set the value of the boundary tolerance from the input inp.
EGS_Float z
z-component
Definition: egs_vector.h:62
EGS_InfoFunction EGS_EXPORT egsFatal
Always use this function for reporting fatal errors.
void setMedia(EGS_Input *inp)
Set the media in the geometry from the input pointed to by inp.
EGS_InfoFunction EGS_EXPORT egsInformation
Always use this function for reporting the progress of a simulation and any other type of information...
A set of parallel planes.
Definition: egs_planes.h:166
A projector into any plane.
void setName(EGS_Input *inp)
Set the name of the geometry from the input inp.
A collection of non-parallel planes.
Definition: egs_planes.h:574
A projector into the z-plane.
A projector into the y-plane.
EGS_Float x
x-component
Definition: egs_vector.h:60
A projector into the x-plane.
A class for storing information in a tree-like structure of key-value pairs. This class is used throu...
Definition: egs_input.h:182
int nreg
Number of local regions in this geometry.
int getInput(const string &key, vector< string > &values) const
Assign values to an array of strings from an input identified by key.
Definition: egs_input.cpp:338
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.