EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_prism.cpp
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ prism 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 # Hannah Gallop
30 #
31 ###############################################################################
32 */
33 
34 
40 #include "egs_prism.h"
41 #include "egs_input.h"
42 
43 #include <vector>
44 using std::vector;
45 
46 static EGS_PRISM_LOCAL string __prismX("EGS_PrismX");
47 static EGS_PRISM_LOCAL string __prismY("EGS_PrismY");
48 static EGS_PRISM_LOCAL string __prismZ("EGS_PrismZ");
49 static EGS_PRISM_LOCAL string __prism("EGS_Prism");
50 
51 static bool EGS_PRISM_LOCAL inputSet = false;
52 
53 extern "C" {
54 
55  static void setInputs() {
56  inputSet = true;
57 
58  setBaseGeometryInputs();
59 
60  geomBlockInput->getSingleInput("library")->setValues({"egs_prism"});
61 
62  // Format: name, isRequired, description, vector string of allowed values
63  auto typePtr = geomBlockInput->addSingleInput("type", true, "The type of prism", {"EGS_PrismX", "EGS_PrismY", "EGS_PrismZ", "EGS_Prism"});
64 
65  geomBlockInput->addSingleInput("closed", false, "Two inputs that define the distance from the top and bottom prism plane to the plane used to define the polygon");
66  geomBlockInput->addSingleInput("points", true, "A list of 2D or 3D (for type=EGS_Prism) positions. For 3D points, they must reside on a plane. These create a polygon that define the base of the prism.");
67  }
68 
69  EGS_PRISM_EXPORT string getExample() {
70  string example;
71  example = {
72  R"(
73  # Example of egs_prisms
74 
75  # EGS_PrismZ example
76  #:start geometry:
77  name = my_prism
78  library = egs_prism
79  type = EGS_PrismZ
80  points = 1 1 -1 1 -1 -1 4 -1
81  closed = 1 4
82  :start media input:
83  media = air
84  :stop media input:
85  :stop geometry:
86 )"};
87  return example;
88  }
89 
90  EGS_PRISM_EXPORT shared_ptr<EGS_BlockInput> getInputs() {
91  if(!inputSet) {
92  setInputs();
93  }
94  return geomBlockInput;
95  }
96 
97  EGS_PRISM_EXPORT EGS_BaseGeometry *createGeometry(EGS_Input *input) {
98 
99  if (!input) {
100  egsWarning("createGeometry(prism): null input?\n");
101  return 0;
102  }
103  string type;
104  int err = input->getInput("type",type);
105  if (err) {
106  egsWarning("createGeometry(prism): missing 'type' input\n");
107  return 0;
108  }
109  EGS_BaseGeometry *g;
110  vector<EGS_Float> p;
111  err = input->getInput("points",p);
112  if (err) {
113  egsWarning("createGeometry(prism): missing 'points' input\n");
114  return 0;
115  }
116  bool open = true;
117  vector<EGS_Float> tmp;
118  err = input->getInput("closed",tmp);
119  if (!err && tmp.size() == 2) {
120  open = false;
121  }
122  bool p_open = false;
123  int itmp;
124  err = input->getInput("open triangle",itmp);
125  if (!err && itmp == 1) {
126  p_open = true;
127  }
128  if (input->compare(type,__prismX) || input->compare(type,__prismY)
129  || input->compare(type,__prismZ)) {
130  int np = p.size()/2;
131  if (np < 3) {
132  egsWarning("createGeometry(prism): at least 3 points are required "
133  "to construct a prism\n");
134  return 0;
135  }
136  vector<EGS_2DVector> points;
137  for (int j=0; j<np; j++) {
138  points.push_back(EGS_2DVector(p[2*j],p[2*j+1]));
139  }
140  if (open) {
141  if (input->compare(type,__prismX))
142  g=new EGS_PrismX(new EGS_PolygonYZ(points,
143  EGS_XProjector(__prismX),p_open));
144  else if (input->compare(type,__prismY))
145  g=new EGS_PrismY(new EGS_PolygonXZ(points,
146  EGS_YProjector(__prismY),p_open));
147  else
148  g=new EGS_PrismZ(new EGS_PolygonXY(points,
149  EGS_ZProjector(__prismZ),p_open));
150  }
151  else {
152  if (input->compare(type,__prismX))
153  g=new EGS_PrismX(new EGS_PolygonYZ(points,
154  EGS_XProjector(__prismX),p_open),tmp[0],tmp[1]);
155  else if (input->compare(type,__prismY))
156  g=new EGS_PrismY(new EGS_PolygonXZ(points,
157  EGS_YProjector(__prismY),p_open),tmp[0],tmp[1]);
158  else
159  g=new EGS_PrismZ(new EGS_PolygonXY(points,
160  EGS_ZProjector(__prismZ),p_open),tmp[0],tmp[1]);
161  }
162  }
163  else {
164  int np = p.size()/3;
165  if (np < 3) {
166  egsWarning("createGeometry(prism): at least 3 points are required"
167  " to construct a prism\n");
168  return 0;
169  }
170  vector<EGS_Vector> points;
171  for (int j=0; j<np; j++) {
172  points.push_back(EGS_Vector(p[3*j],p[3*j+1],p[3*j+2]));
173  }
174  EGS_Vector aux(points[np-1]-points[0]);
175  if (aux.length2() > epsilon) {
176  points.push_back(points[0]);
177  }
178  np = points.size();
179  EGS_Projector pro(points[0],points[1],points[np-2],__prism);
180  vector<EGS_2DVector> p2;
181  {
182  for (int j=0; j<np; j++) {
183  p2.push_back(pro.getProjection(points[j]));
184  EGS_Float d = pro.distance(points[j]);
185  if (fabs(d) > epsilon) {
186  egsWarning("createGeometry(prism): "
187  "points are not on a plane\n");
188  return 0;
189  }
190  }
191  }
192  if (open) {
193  g = new EGS_Prism(new EGS_Polygon(p2,pro,p_open));
194  }
195  else {
196  g = new EGS_Prism(new EGS_Polygon(p2,pro,p_open),tmp[0],tmp[1]);
197  }
198  }
199  g->setName(input);
200  g->setBoundaryTolerance(input);
201  g->setMedia(input);
202  g->setLabels(input);
203  return g;
204  }
205 
206 }
A class representing 2D vectors.
Base geometry class. Every geometry class must be derived from EGS_BaseGeometry.
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.
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
static bool compare(const string &s1, const string &s2)
Definition: egs_input.cpp:1173
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:341
A projector into any plane.
A class representing 3D vectors.
Definition: egs_vector.h:57
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:84
EGS_Input class header file.
EGS_PolygonT< EGS_XProjector > EGS_PolygonYZ
A 3D polygon in the x-plane.
Definition: egs_polygon.h:478
EGS_PolygonT< EGS_Projector > EGS_Polygon
A 3D polygon in any plane.
Definition: egs_polygon.h:484
EGS_PolygonT< EGS_ZProjector > EGS_PolygonXY
A 3D polygon in the z-plane.
Definition: egs_polygon.h:482
EGS_PolygonT< EGS_YProjector > EGS_PolygonXZ
A 3D polygon in the y-plane.
Definition: egs_polygon.h:480
A prism geometry: header.
EGS_PrismT< EGS_PolygonYZ > EGS_PrismX
A prism with base in the X-plane.
Definition: egs_prism.h:347
EGS_PrismT< EGS_Polygon > EGS_Prism
A prism with base in an arbitrary plane.
Definition: egs_prism.h:353
EGS_PrismT< EGS_PolygonXZ > EGS_PrismY
A prism with base in the Y-plane.
Definition: egs_prism.h:349
EGS_PrismT< EGS_PolygonXY > EGS_PrismZ
A prism with base in the Z-plane.
Definition: egs_prism.h:351
const EGS_Float epsilon
The epsilon constant for floating point comparisons.
Definition: egs_functions.h:62
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.