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: Hannah Gallop
27 #
28 ###############################################################################
29 */
30 
31 
37 #include "egs_rectangle.h"
38 #include "egs_input.h"
39 #include "egs_functions.h"
40 
41 static bool EGS_RECTANGLE_LOCAL inputSet = false;
42 static shared_ptr<EGS_BlockInput> EGS_RECTANGLE_LOCAL shapeBlockInput = make_shared<EGS_BlockInput>("shape");
43 
44 EGS_RectangularRing::EGS_RectangularRing(EGS_Float xmin, EGS_Float xmax,
45  EGS_Float ymin, EGS_Float ymax, EGS_Float xmin_i, EGS_Float xmax_i,
46  EGS_Float ymin_i, EGS_Float ymax_i, const string &Name,
47  EGS_ObjectFactory *f) : EGS_SurfaceShape(Name,f), valid(true) {
48  EGS_Float tmp;
49  if (xmin > xmax) {
50  tmp = xmax;
51  xmax = xmin;
52  xmin = tmp;
53  }
54  if (ymin > ymax) {
55  tmp = ymax;
56  ymax = ymin;
57  ymin = tmp;
58  }
59  if (xmin_i > xmax_i) {
60  tmp = xmax_i;
61  xmax_i = xmin_i;
62  xmin_i = tmp;
63  }
64  if (ymin_i > ymax_i) {
65  tmp = ymax_i;
66  ymax_i = ymin_i;
67  ymin_i = tmp;
68  }
69  if (xmin_i < xmin || ymin_i < ymin || xmax_i > xmax || ymax_i > ymax) {
70  valid = false;
71  return;
72  }
73  r[0] = new EGS_RectangleShape(xmin,xmin_i,ymin,ymax);
74  r[1] = new EGS_RectangleShape(xmax_i,xmax,ymin,ymax);
75  r[2] = new EGS_RectangleShape(xmin_i,xmax_i,ymin,ymin_i);
76  r[3] = new EGS_RectangleShape(xmin_i,xmax_i,ymax_i,ymax);
77  p[0] = (xmin_i - xmin)*(ymax - ymin);
78  p[1] = (xmax - xmax_i)*(ymax - ymin);
79  p[2] = (xmax_i - xmin_i)*(ymin_i - ymin);
80  p[3] = (xmax_i - xmin_i)*(ymax - ymax_i);
81  A = p[0] + p[1] + p[2] + p[3];
82  p[0] /= A;
83  p[1] = p[1]/A + p[0];
84  p[2] = p[2]/A + p[1];
85  p[3] = 1.1;
86 
87  otype = "rectangular ring";
88 
89 }
90 
91 EGS_RectangularRing::~EGS_RectangularRing() {
92  if (valid) {
93  delete r[0];
94  delete r[1];
95  delete r[2];
96  delete r[3];
97  };
98 }
99 
100 
101 extern "C" {
102 
103  static void setInputs() {
104  inputSet = true;
105 
106  setShapeInputs(shapeBlockInput);
107  shapeBlockInput->getSingleInput("library")->setValues({"egs_rectangle"});
108 
109  shapeBlockInput->addSingleInput("rectangle", true, "Two 2D coordinates to define a rectangle: x1 y1 x2 y2. By default these are in the x-y plane at z=0; use a transformation to adjust.");
110  shapeBlockInput->addSingleInput("inner rectangle", false, "Two 2D coordinates to define an inner rectangle, and create a 'rectangular ring': xp1 yp1 xp2 yp2");
111  }
112 
113  EGS_RECTANGLE_EXPORT string getExample() {
114  string example;
115  example = {
116  R"(
117  # Example of egs_rectangle
118  #:start shape:
119  library = egs_rectangle
120  rectangle = x1 y1 x2 y2
121  inner rectangle = xp1 yp1 xp2 yp2 (optional)
122  :stop shape:
123 )"};
124  return example;
125  }
126 
127  EGS_RECTANGLE_EXPORT shared_ptr<EGS_BlockInput> getInputs() {
128  if(!inputSet) {
129  setInputs();
130  }
131  return shapeBlockInput;
132  }
133 
134  EGS_RECTANGLE_EXPORT EGS_BaseShape *createShape(EGS_Input *input,
135  EGS_ObjectFactory *f) {
136  if (!input) {
137  egsWarning("createShape(rectangle): null input?\n");
138  return 0;
139  }
140  vector<EGS_Float> pos;
141  int err = input->getInput("rectangle",pos);
142  if (err) {
143  egsWarning("createShape(rectangle): no 'rectangle' input\n");
144  return 0;
145  }
146  if (pos.size() != 4) {
147  egsWarning("createShape(rectangle): found only %d inputs instead"
148  " of 4\n");
149  return 0;
150  }
151  EGS_BaseShape *shape=0;
152  vector<EGS_Float> posi;
153  err = input->getInput("inner rectangle",posi);
154  if (!err && posi.size() == 4) {
155  EGS_RectangularRing *s = new EGS_RectangularRing(pos[0],pos[2],pos[1],
156  pos[3],posi[0],posi[2],posi[1],posi[3],"",f);
157  if (!s->isValid()) {
158  egsWarning("createShape(rectangle): your input did not result in"
159  " a valid \"rectangular ring\"\n");
160  delete s;
161  }
162  else {
163  shape = s;
164  }
165  }
166  else {
167  shape = new EGS_RectangleShape(pos[0],pos[2],pos[1],pos[3],"",f);
168  }
169  if (shape) {
170  shape->setName(input);
171  shape->setTransformation(input);
172  }
173  return shape;
174  }
175 
176 }
Base shape class. All shapes in the EGSnrc C++ class library are derived from EGS_BaseShape.
Definition: egs_shapes.h:145
void setTransformation(EGS_Input *inp)
Set the transformation attached to this shape.
Definition: egs_shapes.cpp:69
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:341
An object factory.
void setName(EGS_Input *inp)
Set the name of the object from the information provided by inp.
A rectangular shape.
Definition: egs_rectangle.h:87
A "rectangular ring".
A surface shape.
Definition: egs_shapes.h:306
Global egspp functions header file.
EGS_Input class header file.
Rectangular shape.
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.