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 # Marc Chamberland
28 # Reid Townson
29 #
30 ###############################################################################
31 */
32 
33 
40 #include "egs_planes.h"
41 #include "egs_input.h"
42 
43 //string XProjector::type = "EGS_Xplanes";
44 //string YProjector::type = "EGS_Yplanes";
45 //string ZProjector::type = "EGS_Zplanes";
46 //string Projector::type = "EGS_Planes";
47 const string EGS_PLANES_LOCAL xproj_type("EGS_Xplanes");
48 const string EGS_PLANES_LOCAL yproj_type("EGS_Yplanes");
49 const string EGS_PLANES_LOCAL zproj_type("EGS_Zplanes");
50 const string EGS_PLANES_LOCAL proj_type("EGS_Planes");
51 
52 string EGS_PlaneCollection::type = "EGS_PlaneCollection";
53 
54 EGS_PlaneCollection::EGS_PlaneCollection(int Np, const EGS_Float *pos,
55  const EGS_Vector *norm, const string &Name) : EGS_BaseGeometry(Name) {
56  if (Np < 2) egsFatal("EGS_PlaneCollection::EGS_PlaneCollection: "
57  " you nead at least 2 planes\n");
58  np = Np;
59  nreg=np-1;
60  planes = new EGS_Planes* [np];
61  for (int j=0; j<np; j++) {
62  planes[j] = new EGS_Planes(1,&pos[j],"",
63  EGS_Projector(norm[j],proj_type));
64  planes[j]->ref();
65  }
66 }
67 
68 EGS_PlaneCollection::~EGS_PlaneCollection() {
69  //egsWarning("Deleting ~EGS_PlaneCollection at 0x%x\n",this);
70  for (int j=0; j<np; j++) if (!planes[j]->deref()) {
71  delete planes[j];
72  }
73  delete [] planes;
74 }
75 
76 void EGS_PlaneCollection::printInfo() const {
78  int j;
79  egsInformation(" plane positions: ");
80  for (j=0; j<np; j++) {
81  egsInformation(" %g ",planes[j]->position(0));
82  }
83  egsInformation(" \nplane normals: ");
84  for (j=0; j<np; j++) {
85  EGS_Vector a = planes[j]->normal();
86  egsInformation(" (%g,%g,%g) ",a.x,a.y,a.z);
87  }
88  egsInformation("\n=====================================================\n");
89 }
90 
91 extern "C" {
92 
93  EGS_PLANES_EXPORT EGS_BaseGeometry *createGeometry(EGS_Input *input) {
94  string type;
95  int err = input->getInput("type",type);
96  if (err) {
97  egsWarning("createGeometry(planes): missing type key\n");
98  return 0;
99  }
100  EGS_Float first;
101  vector<EGS_Float> thick;
102  vector<int> nthick;
103  vector<EGS_Float> pos;
104  int err_first = input->getInput("first plane",first);
105  int err_thick = input->getInput("slab thickness",thick);
106  int err_nthick = input->getInput("number of slabs",nthick);
107  if (!err_first && !err_thick && !err_nthick) {
108  if (thick.size() != nthick.size() || !thick.size()) {
109  egsWarning("createGeometry(planes): number of 'slab thickness' and"
110  "\n 'number of slabs' inputs must be the same and not zero\n");
111  egsWarning(" got %d and %d inputs --> input ignored\n",
112  thick.size(),nthick.size());
113  }
114  else {
115  pos.push_back(first);
116  int j=0;
117  for (int i=0; i<thick.size(); i++) {
118  for (int l=0; l<nthick[i]; ++l) {
119  pos.push_back(pos[j++]+thick[i]);
120  }
121  }
122  }
123  }
124  if (!pos.size()) {
125  err = input->getInput("positions",pos);
126  if (err) {
127  egsWarning("createGeometry(planes): missing/wrong 'positions' "
128  "input and missing/wrong multiple plane input\n");
129  return 0;
130  }
131  }
132  EGS_BaseGeometry *g;
133  if (type == "EGS_Xplanes") g = new EGS_PlanesX(pos,"",
134  EGS_XProjector(xproj_type));
135  else if (type == "EGS_Yplanes") g = new EGS_PlanesY(pos,"",
136  EGS_YProjector(yproj_type));
137  else if (type == "EGS_Zplanes") g = new EGS_PlanesZ(pos,"",
138  EGS_ZProjector(zproj_type));
139  else if (type == "EGS_Planes") {
140  vector<EGS_Float> a;
141  err = input->getInput("normal",a);
142  if (err || a.size() != 3) {
143  egsWarning("createGeometry(planes): missing/wrong normal input\n");
144  return 0;
145  }
146  g = new EGS_Planes(pos,"",EGS_Projector(EGS_Vector(a[0],a[1],a[2]),
147  proj_type));
148  }
149  else if (type == "EGS_PlaneCollection") {
150  vector<EGS_Float> a;
151  err = input->getInput("normals",a);
152  if (err || a.size() < 6) {
153  egsWarning("createGeometry(planes): missing/wrong normal input\n");
154  return 0;
155  }
156  int np = a.size()/3;
157  if (np != pos.size()) {
158  egsWarning("createGeometry(planes): number of plane normals (%d)\n"
159  " is not the same as number of plane positions (%d) for a"
160  " plane collection\n",np,pos.size());
161  return 0;
162  }
163  EGS_Float *p = new EGS_Float [np];
164  EGS_Vector *normal = new EGS_Vector [np];
165  for (int j=0; j<np; j++) {
166  p[j] = pos[j];
167  normal[j] = EGS_Vector(a[3*j],a[3*j+1],a[3*j+2]);
168  }
169  g = new EGS_PlaneCollection(np,p,normal);
170  }
171  else {
172  egsWarning("createGeometry(planes): unknown type %s\n",type.c_str());
173  return 0;
174  }
175  g->setName(input);
176  g->setBoundaryTolerance(input);
177  g->setMedia(input);
178  g->setLabels(input);
179  return g;
180  }
181 
182 }
Base geometry class. Every geometry class must be derived from EGS_BaseGeometry.
int deref()
Decrease the reference count to this geometry.
void setMedia(EGS_Input *inp)
Set the media in the geometry from the input pointed to by inp.
void setName(EGS_Input *inp)
Set the name of the geometry from the input inp.
int setLabels(EGS_Input *input)
Set the labels from an input block.
virtual void printInfo() const
Print information about this geometry.
void setBoundaryTolerance(EGS_Input *inp)
Set the value of the boundary tolerance from the input inp.
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 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
A collection of non-parallel planes.
Definition: egs_planes.h:575
A set of parallel planes.
Definition: egs_planes.h:167
A projector into any plane.
A class representing 3D vectors.
Definition: egs_vector.h:57
EGS_Float y
y-component
Definition: egs_vector.h:62
EGS_Float z
z-component
Definition: egs_vector.h:63
EGS_Float x
x-component
Definition: egs_vector.h:61
A projector into the x-plane.
A projector into the y-plane.
A projector into the z-plane.
EGS_GLIB_EXPORT EGS_BaseGeometry * createGeometry(EGS_Input *input)
Definition: egs_glib.cpp:57
EGS_Input class header file.
Sets of parallel planes and a plane collection.
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.
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.