EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_rectangle.cpp
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ rectangle shape
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 
37 #include "egs_rectangle.h"
38 #include "egs_input.h"
39 #include "egs_functions.h"
40 
41 EGS_RectangularRing::EGS_RectangularRing(EGS_Float xmin, EGS_Float xmax,
42  EGS_Float ymin, EGS_Float ymax, EGS_Float xmin_i, EGS_Float xmax_i,
43  EGS_Float ymin_i, EGS_Float ymax_i, const string &Name,
44  EGS_ObjectFactory *f) : EGS_SurfaceShape(Name,f), valid(true) {
45  EGS_Float tmp;
46  if (xmin > xmax) {
47  tmp = xmax;
48  xmax = xmin;
49  xmin = tmp;
50  }
51  if (ymin > ymax) {
52  tmp = ymax;
53  ymax = ymin;
54  ymin = tmp;
55  }
56  if (xmin_i > xmax_i) {
57  tmp = xmax_i;
58  xmax_i = xmin_i;
59  xmin_i = tmp;
60  }
61  if (ymin_i > ymax_i) {
62  tmp = ymax_i;
63  ymax_i = ymin_i;
64  ymin_i = tmp;
65  }
66  if (xmin_i < xmin || ymin_i < ymin || xmax_i > xmax || ymax_i > ymax) {
67  valid = false;
68  return;
69  }
70  r[0] = new EGS_RectangleShape(xmin,xmin_i,ymin,ymax);
71  r[1] = new EGS_RectangleShape(xmax_i,xmax,ymin,ymax);
72  r[2] = new EGS_RectangleShape(xmin_i,xmax_i,ymin,ymin_i);
73  r[3] = new EGS_RectangleShape(xmin_i,xmax_i,ymax_i,ymax);
74  p[0] = (xmin_i - xmin)*(ymax - ymin);
75  p[1] = (xmax - xmax_i)*(ymax - ymin);
76  p[2] = (xmax_i - xmin_i)*(ymin_i - ymin);
77  p[3] = (xmax_i - xmin_i)*(ymax - ymax_i);
78  A = p[0] + p[1] + p[2] + p[3];
79  p[0] /= A;
80  p[1] = p[1]/A + p[0];
81  p[2] = p[2]/A + p[1];
82  p[3] = 1.1;
83 
84  otype = "rectangular ring";
85 
86 }
87 
88 EGS_RectangularRing::~EGS_RectangularRing() {
89  if (valid) {
90  delete r[0];
91  delete r[1];
92  delete r[2];
93  delete r[3];
94  };
95 }
96 
97 
98 extern "C" {
99 
100  EGS_RECTANGLE_EXPORT EGS_BaseShape *createShape(EGS_Input *input,
101  EGS_ObjectFactory *f) {
102  if (!input) {
103  egsWarning("createShape(rectangle): null input?\n");
104  return 0;
105  }
106  vector<EGS_Float> pos;
107  int err = input->getInput("rectangle",pos);
108  if (err) {
109  egsWarning("createShape(rectangle): no 'rectangle' input\n");
110  return 0;
111  }
112  if (pos.size() != 4) {
113  egsWarning("createShape(rectangle): found only %d inputs instead"
114  " of 4\n");
115  return 0;
116  }
117  EGS_BaseShape *shape=0;
118  vector<EGS_Float> posi;
119  err = input->getInput("inner rectangle",posi);
120  if (!err && posi.size() == 4) {
121  EGS_RectangularRing *s = new EGS_RectangularRing(pos[0],pos[2],pos[1],
122  pos[3],posi[0],posi[2],posi[1],posi[3],"",f);
123  if (!s->isValid()) {
124  egsWarning("createShape(rectangle): your input did not result in"
125  " a valid \"rectangular ring\"\n");
126  delete s;
127  }
128  else {
129  shape = s;
130  }
131  }
132  else {
133  shape = new EGS_RectangleShape(pos[0],pos[2],pos[1],pos[3],"",f);
134  }
135  if (shape) {
136  shape->setName(input);
137  shape->setTransformation(input);
138  }
139  return shape;
140  }
141 
142 }
A surface shape.
Definition: egs_shapes.h:252
void setTransformation(EGS_Input *inp)
Set the transformation attached to this shape.
Definition: egs_shapes.cpp:68
EGS_Input class header file.
Global egspp functions header file.
Base shape class. All shapes in the EGSnrc C++ class library are derived from EGS_BaseShape.
Definition: egs_shapes.h:112
An object factory.
A &quot;rectangular ring&quot;.
void setName(EGS_Input *inp)
Set the name of the object from the information provided by inp.
Rectangular shape.
A class for storing information in a tree-like structure of key-value pairs. This class is used throu...
Definition: egs_input.h:182
A rectangular shape.
Definition: egs_rectangle.h:87
string otype
The object type.
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.