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 #
30 ###############################################################################
31 */
32 
33 
39 #include "egs_prism.h"
40 #include "egs_input.h"
41 
42 #include <vector>
43 using std::vector;
44 
45 static EGS_PRISM_LOCAL string __prismX("EGS_PrismX");
46 static EGS_PRISM_LOCAL string __prismY("EGS_PrismY");
47 static EGS_PRISM_LOCAL string __prismZ("EGS_PrismZ");
48 static EGS_PRISM_LOCAL string __prism("EGS_Prism");
49 
50 extern "C" {
51 
52  EGS_PRISM_EXPORT EGS_BaseGeometry *createGeometry(EGS_Input *input) {
53 
54  if (!input) {
55  egsWarning("createGeometry(prism): null input?\n");
56  return 0;
57  }
58  string type;
59  int err = input->getInput("type",type);
60  if (err) {
61  egsWarning("createGeometry(prism): missing 'type' input\n");
62  return 0;
63  }
65  vector<EGS_Float> p;
66  err = input->getInput("points",p);
67  if (err) {
68  egsWarning("createGeometry(prism): missing 'points' input\n");
69  return 0;
70  }
71  bool open = true;
72  vector<EGS_Float> tmp;
73  err = input->getInput("closed",tmp);
74  if (!err && tmp.size() == 2) {
75  open = false;
76  }
77  bool p_open = false;
78  int itmp;
79  err = input->getInput("open triangle",itmp);
80  if (!err && itmp == 1) {
81  p_open = true;
82  }
83  if (input->compare(type,__prismX) || input->compare(type,__prismY)
84  || input->compare(type,__prismZ)) {
85  int np = p.size()/2;
86  if (np < 3) {
87  egsWarning("createGeometry(prism): at least 3 points are required "
88  "to construct a prism\n");
89  return 0;
90  }
91  vector<EGS_2DVector> points;
92  for (int j=0; j<np; j++) {
93  points.push_back(EGS_2DVector(p[2*j],p[2*j+1]));
94  }
95  if (open) {
96  if (input->compare(type,__prismX))
97  g=new EGS_PrismX(new EGS_PolygonYZ(points,
98  EGS_XProjector(__prismX),p_open));
99  else if (input->compare(type,__prismY))
100  g=new EGS_PrismY(new EGS_PolygonXZ(points,
101  EGS_YProjector(__prismY),p_open));
102  else
103  g=new EGS_PrismZ(new EGS_PolygonXY(points,
104  EGS_ZProjector(__prismZ),p_open));
105  }
106  else {
107  if (input->compare(type,__prismX))
108  g=new EGS_PrismX(new EGS_PolygonYZ(points,
109  EGS_XProjector(__prismX),p_open),tmp[0],tmp[1]);
110  else if (input->compare(type,__prismY))
111  g=new EGS_PrismY(new EGS_PolygonXZ(points,
112  EGS_YProjector(__prismY),p_open),tmp[0],tmp[1]);
113  else
114  g=new EGS_PrismZ(new EGS_PolygonXY(points,
115  EGS_ZProjector(__prismZ),p_open),tmp[0],tmp[1]);
116  }
117  }
118  else {
119  int np = p.size()/3;
120  if (np < 3) {
121  egsWarning("createGeometry(prism): at least 3 points are required"
122  " to construct a prism\n");
123  return 0;
124  }
125  vector<EGS_Vector> points;
126  for (int j=0; j<np; j++) {
127  points.push_back(EGS_Vector(p[3*j],p[3*j+1],p[3*j+2]));
128  }
129  EGS_Vector aux(points[np-1]-points[0]);
130  if (aux.length2() > epsilon) {
131  points.push_back(points[0]);
132  }
133  np = points.size();
134  EGS_Projector pro(points[0],points[1],points[np-2],__prism);
135  vector<EGS_2DVector> p2;
136  {
137  for (int j=0; j<np; j++) {
138  p2.push_back(pro.getProjection(points[j]));
139  EGS_Float d = pro.distance(points[j]);
140  if (fabs(d) > epsilon) {
141  egsWarning("createGeometry(prism): "
142  "points are not on a plane\n");
143  return 0;
144  }
145  }
146  }
147  if (open) {
148  g = new EGS_Prism(new EGS_Polygon(p2,pro,p_open));
149  }
150  else {
151  g = new EGS_Prism(new EGS_Polygon(p2,pro,p_open),tmp[0],tmp[1]);
152  }
153  }
154  g->setName(input);
155  g->setBoundaryTolerance(input);
156  g->setMedia(input);
157  g->setLabels(input);
158  return g;
159  }
160 
161 }
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:1170
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 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:57
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.