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 #
28 ###############################################################################
29 */
30 
31 
37 #include "egs_prism.h"
38 #include "egs_input.h"
39 
40 #include <vector>
41 using std::vector;
42 
43 static EGS_PRISM_LOCAL string __prismX("EGS_PrismX");
44 static EGS_PRISM_LOCAL string __prismY("EGS_PrismY");
45 static EGS_PRISM_LOCAL string __prismZ("EGS_PrismZ");
46 static EGS_PRISM_LOCAL string __prism("EGS_Prism");
47 
48 extern "C" {
49 
50  EGS_PRISM_EXPORT EGS_BaseGeometry *createGeometry(EGS_Input *input) {
51 
52  if (!input) {
53  egsWarning("createGeometry(prism): null input?\n");
54  return 0;
55  }
56  string type;
57  int err = input->getInput("type",type);
58  if (err) {
59  egsWarning("createGeometry(prism): missing 'type' input\n");
60  return 0;
61  }
63  vector<EGS_Float> p;
64  err = input->getInput("points",p);
65  if (err) {
66  egsWarning("createGeometry(prism): missing 'points' input\n");
67  return 0;
68  }
69  bool open = true;
70  vector<EGS_Float> tmp;
71  err = input->getInput("closed",tmp);
72  if (!err && tmp.size() == 2) {
73  open = false;
74  }
75  bool p_open = false;
76  int itmp;
77  err = input->getInput("open triangle",itmp);
78  if (!err && itmp == 1) {
79  p_open = true;
80  }
81  if (input->compare(type,__prismX) || input->compare(type,__prismY)
82  || input->compare(type,__prismZ)) {
83  int np = p.size()/2;
84  if (np < 3) {
85  egsWarning("createGeometry(prism): at least 3 points are required "
86  "to construct a prism\n");
87  return 0;
88  }
89  vector<EGS_2DVector> points;
90  for (int j=0; j<np; j++) {
91  points.push_back(EGS_2DVector(p[2*j],p[2*j+1]));
92  }
93  if (open) {
94  if (input->compare(type,__prismX))
95  g=new EGS_PrismX(new EGS_PolygonYZ(points,
96  EGS_XProjector(__prismX),p_open));
97  else if (input->compare(type,__prismY))
98  g=new EGS_PrismY(new EGS_PolygonXZ(points,
99  EGS_YProjector(__prismY),p_open));
100  else
101  g=new EGS_PrismZ(new EGS_PolygonXY(points,
102  EGS_ZProjector(__prismZ),p_open));
103  }
104  else {
105  if (input->compare(type,__prismX))
106  g=new EGS_PrismX(new EGS_PolygonYZ(points,
107  EGS_XProjector(__prismX),p_open),tmp[0],tmp[1]);
108  else if (input->compare(type,__prismY))
109  g=new EGS_PrismY(new EGS_PolygonXZ(points,
110  EGS_YProjector(__prismY),p_open),tmp[0],tmp[1]);
111  else
112  g=new EGS_PrismZ(new EGS_PolygonXY(points,
113  EGS_ZProjector(__prismZ),p_open),tmp[0],tmp[1]);
114  }
115  }
116  else {
117  int np = p.size()/3;
118  if (np < 3) {
119  egsWarning("createGeometry(prism): at least 3 points are required"
120  " to construct a prism\n");
121  return 0;
122  }
123  vector<EGS_Vector> points;
124  for (int j=0; j<np; j++) {
125  points.push_back(EGS_Vector(p[3*j],p[3*j+1],p[3*j+2]));
126  }
127  EGS_Vector aux(points[np-1]-points[0]);
128  if (aux.length2() > epsilon) {
129  points.push_back(points[0]);
130  }
131  np = points.size();
132  EGS_Projector pro(points[0],points[1],points[np-2],__prism);
133  vector<EGS_2DVector> p2;
134  {
135  for (int j=0; j<np; j++) {
136  p2.push_back(pro.getProjection(points[j]));
137  EGS_Float d = pro.distance(points[j]);
138  if (fabs(d) > epsilon) {
139  egsWarning("createGeometry(prism): "
140  "points are not on a plane\n");
141  return 0;
142  }
143  }
144  }
145  if (open) {
146  g = new EGS_Prism(new EGS_Polygon(p2,pro,p_open));
147  }
148  else {
149  g = new EGS_Prism(new EGS_Polygon(p2,pro,p_open),tmp[0],tmp[1]);
150  }
151  }
152  g->setName(input);
153  g->setBoundaryTolerance(input);
154  g->setMedia(input);
155  g->setLabels(input);
156  return g;
157  }
158 
159 }
A class representing 2D vectors.
EGS_Input class header file.
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_PolygonT< EGS_XProjector > EGS_PolygonYZ
A 3D polygon in the x-plane.
Definition: egs_polygon.h:478
EGS_PrismT< EGS_PolygonYZ > EGS_PrismX
A prism with base in the X-plane.
Definition: egs_prism.h:347
EGS_PolygonT< EGS_Projector > EGS_Polygon
A 3D polygon in any plane.
Definition: egs_polygon.h:484
void setMedia(EGS_Input *inp)
Set the media in the geometry from the input pointed to by inp.
A projector into any plane.
void setName(EGS_Input *inp)
Set the name of the geometry from the input inp.
EGS_PrismT< EGS_PolygonXZ > EGS_PrismY
A prism with base in the Y-plane.
Definition: egs_prism.h:349
A projector into the z-plane.
A projector into the y-plane.
EGS_PrismT< EGS_PolygonXY > EGS_PrismZ
A prism with base in the Z-plane.
Definition: egs_prism.h:351
EGS_PolygonT< EGS_ZProjector > EGS_PolygonXY
A 3D polygon in the z-plane.
Definition: egs_polygon.h:482
A projector into the x-plane.
EGS_PrismT< EGS_Polygon > EGS_Prism
A prism with base in an arbitrary plane.
Definition: egs_prism.h:353
const EGS_Float epsilon
The epsilon constant for floating point comparisons.
Definition: egs_functions.h:61
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:1170
A prism geometry: header.
EGS_PolygonT< EGS_YProjector > EGS_PolygonXZ
A 3D polygon in the y-plane.
Definition: egs_polygon.h:480
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.