EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_shapes.cpp
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ shapes
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:
27 #
28 ###############################################################################
29 */
30 
31 
39 #include "egs_shapes.h"
40 #include "egs_input.h"
41 #include "egs_library.h"
42 
43 #include <vector>
44 using namespace std;
45 
46 static int __shape_count = 0;
47 
49 shape_creator(string("egs++/dso/")+CONFIG_NAME,"EGS_BaseShape");
50 
52  if (!__shape_count) {
53  shape_creator.addKnownObject(new EGS_PointShape);
54  shape_creator.addKnownObject(new EGS_BoxShape);
55  shape_creator.addKnownObject(new EGS_SphereShape);
56  shape_creator.addKnownObject(new EGS_CylinderShape);
57  }
58  __shape_count++;
59  EGS_Object *o = shape_creator.createSingleObject(i,"createShape",true);
60  return dynamic_cast<EGS_BaseShape *>(o);
61 }
62 
63 EGS_BaseShape *EGS_BaseShape::getShape(const string &Name) {
64  EGS_Object *o = shape_creator.getObject(Name);
65  return dynamic_cast<EGS_BaseShape *>(o);
66 }
67 
69  if (T) {
70  delete T;
71  }
73 }
74 
75 /****************************************************************************
76  *
77  * concrete shapes
78  *
79  ****************************************************************************/
80 
81 
82 /********************** Point *******************************/
83 
85  vector<EGS_Float> pos;
86  int err = input->getInput("position",pos);
87  if (err) {
88  egsWarning("EGS_PointShape::createShape: no 'position' input\n");
89  return 0;
90  }
91  if (pos.size() != 3) {
92  egsWarning("EGS_PointShape::createShape: found %d inputs "
93  "instead of 3\n",pos.size());
94  return 0;
95  }
96  EGS_PointShape *res = new EGS_PointShape(EGS_Vector(pos[0],pos[1],pos[2]));
97  res->setName(input);
98  return res;
99 }
100 
101 /********************** Box **********************************/
102 
104  vector<EGS_Float> s;
105  int err = input->getInput("box size",s);
106  if (err) {
107  egsWarning("EGS_BoxShape::createShape: no 'box size' input?\n");
108  return 0;
109  }
111  EGS_BoxShape *result;
112  if (s.size() == 1) {
113  result = new EGS_BoxShape(s[0],t);
114  }
115  else if (s.size() == 3) {
116  result = new EGS_BoxShape(s[0],s[1],s[2],t);
117  }
118  else {
119  egsWarning("EGS_BoxShape::createShape: invalid 'box size' input\n");
120  result = 0;
121  }
122  if (t) {
123  delete t;
124  }
125  result->setName(input);
126  return result;
127 }
128 
129 /********************** Sphere **********************************/
130 
132  EGS_Float r;
133  int err = input->getInput("radius",r);
134  if (err) {
135  egsWarning("EGS_SphereShape::createShape: wrong/missing 'radius'"
136  " input\n");
137  return 0;
138  }
139  EGS_SphereShape *result;
140  vector<EGS_Float> xo;
141  err = input->getInput("midpoint",xo);
142  if (!err && xo.size() == 3) {
143  result = new EGS_SphereShape(r,EGS_Vector(xo[0],xo[1],xo[2]));
144  }
145  else {
146  result = new EGS_SphereShape(r);
147  }
148  result->setName(input);
149  return result;
150 }
151 
152 /********************** Cylinder **********************************/
153 
155  EGS_Float r, H;
156  int err = input->getInput("radius",r);
157  if (err) {
158  egsWarning("EGS_CylinderShape::getShape: wrong/missing 'radius'"
159  " input\n");
160  return 0;
161  }
162  err = input->getInput("height",H);
163  if (err) {
164  egsWarning("EGS_CylinderShape::getShape: wrong/missing 'height'"
165  " input\n");
166  return 0;
167  }
168  vector<EGS_Float> phi_range;
169  bool set_phi = false;
170  if (!input->getInput("phi range",phi_range) && phi_range.size() == 2) {
171  set_phi = true;
172  phi_range[0] *= M_PI/180;
173  phi_range[1] *= M_PI/180;
174  }
176  if (t) {
177  EGS_CylinderShape *result = new EGS_CylinderShape(r,H,t);
178  result->setName(input);
179  if (set_phi) {
180  result->setPhiRange(phi_range[0],phi_range[1]);
181  }
182  delete t;
183  return result;
184  }
185  vector<EGS_Float> Xo, A;
186  int err1 = input->getInput("midpoint",Xo);
187  int err2 = input->getInput("axis",A);
188  bool has_Xo = (err1 == 0 && Xo.size() == 3);
189  bool has_A = (err2 == 0 && A.size() == 3);
190  EGS_CylinderShape *result;
191  if (has_Xo && has_A) result = new EGS_CylinderShape(r,H,
192  EGS_Vector(Xo[0],Xo[1],Xo[2]),EGS_Vector(A[0],A[1],A[2]));
193  else if (has_Xo) result = new EGS_CylinderShape(r,H,
194  EGS_Vector(Xo[0],Xo[1],Xo[2]));
195  else if (has_A) result = new EGS_CylinderShape(r,H,
196  EGS_Vector(0,0,0),EGS_Vector(A[0],A[1],A[2]));
197  else {
198  result = new EGS_CylinderShape(r,H);
199  }
200  result->setName(input);
201  if (set_phi) {
202  result->setPhiRange(phi_range[0],phi_range[1]);
203  }
204  return result;
205 }
206 
Base egspp object.
EGS_Object * createObject(EGS_Input *)
Definition: egs_shapes.cpp:154
EGS_Object * createObject(EGS_Input *inp)
Definition: egs_shapes.cpp:131
void setTransformation(EGS_Input *inp)
Set the transformation attached to this shape.
Definition: egs_shapes.cpp:68
A class providing affine transformations.
static EGS_BaseShape * createShape(EGS_Input *inp)
Create a shape from the information pointed to by inp.
Definition: egs_shapes.cpp:51
EGS_Input class header file.
A typed object factory.
A class representing 3D vectors.
Definition: egs_vector.h:56
static EGS_BaseShape * getShape(const string &Name)
Get a pointer to the shape named Name.
Definition: egs_shapes.cpp:63
EGS_Object * createObject(EGS_Input *)
Create a box shape from the information pointed to by inp and return a pointer to it...
Definition: egs_shapes.cpp:103
Base shape class. All shapes in the EGSnrc C++ class library are derived from EGS_BaseShape.
Definition: egs_shapes.h:112
EGS_Object * createObject(EGS_Input *inp)
Creates a point shape from the input inp and returns a pointer to it.
Definition: egs_shapes.cpp:84
A point shape. This is the simplest shape possible: it simply always returns the same point...
Definition: egs_shapes.h:314
A sphere shape.
Definition: egs_shapes.h:488
EGS_BaseShape and shape classes header file.
EGS_Library class header file.
A cylinder shape.
Definition: egs_shapes.h:596
A box shape.
Definition: egs_shapes.h:356
void setName(EGS_Input *inp)
Set the name of the object from the information provided by inp.
void setPhiRange(EGS_Float Phi_min, EGS_Float Phi_max)
Definition: egs_shapes.h:668
static EGS_AffineTransform * getTransformation(EGS_Input *inp)
Constructs an affine transformation object from the input pointed to by inp and returns a pointer to ...
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
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.