EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_line_shape.cpp
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ line 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_line_shape.h"
38 #include "egs_input.h"
39 #include "egs_functions.h"
40 
41 static bool EGS_LINE_SHAPE_LOCAL inputSet = false;
42 static shared_ptr<EGS_BlockInput> EGS_LINE_SHAPE_LOCAL shapeBlockInput = make_shared<EGS_BlockInput>("shape");
43 
44 EGS_LineShape::EGS_LineShape(const vector<EGS_Float> &points,
45  const string &Name, EGS_ObjectFactory *f) : EGS_BaseShape(Name,f) {
46  int np = points.size();
47  n = np/2;
48  if (n <= 1) {
49  n = 0;
50  return;
51  }
52  x = new EGS_Float[n];
53  y = new EGS_Float[n];
54  EGS_Float *w = new EGS_Float [n-1];
55  for (int j=0; j<n; j++) {
56  x[j] = points[2*j];
57  y[j] = points[2*j+1];
58  if (j > 0) {
59  EGS_Float ax = x[j]-x[j-1], ay = y[j]-y[j-1];
60  w[j-1] = sqrt(ax*ax+ay*ay);
61  }
62  }
63  table = new EGS_AliasTable(n-1,x,w,0);
64  otype = "line";
65 }
66 
67 extern "C" {
68 
69  static void setInputs() {
70  inputSet = true;
71 
72  setShapeInputs(shapeBlockInput);
73  shapeBlockInput->getSingleInput("library")->setValues({"egs_line_shape"});
74 
75  shapeBlockInput->addSingleInput("points", true, "A list of 2D positions, at least 2 required. By default these are in the x-y plane at z=0; use a transformation to adjust.");
76  }
77 
78  EGS_LINE_SHAPE_EXPORT string getExample() {
79  string example;
80  example = {
81  R"(
82  # Example of egs_line_shape
83  :start shape:
84  library = egs_line_shape
85  points = list of 2D positions
86  :stop shape:
87 )"};
88  return example;
89  }
90 
91  EGS_LINE_SHAPE_EXPORT shared_ptr<EGS_BlockInput> getInputs() {
92  if(!inputSet) {
93  setInputs();
94  }
95  return shapeBlockInput;
96  }
97 
98  EGS_LINE_SHAPE_EXPORT EGS_BaseShape *createShape(EGS_Input *input,
99  EGS_ObjectFactory *f) {
100  if (!input) {
101  egsWarning("createShape(line): null input?\n");
102  return 0;
103  }
104  vector<EGS_Float> points;
105  int err = input->getInput("points",points);
106  if (err) {
107  egsWarning("createShape(line): no 'points' input\n");
108  return 0;
109  }
110  int np = points.size();
111  if ((np%2) != 0) {
112  egsWarning("createShape(line): you must input an even number of"
113  " floating numbers\n");
114  return 0;
115  }
116  if (np < 4) {
117  egsWarning("createShape(line): you must input at least 2 2D points"
118  " to form a line\n");
119  return 0;
120  }
121  EGS_LineShape *shape = new EGS_LineShape(points,"",f);
122  shape->setName(input);
123  shape->setTransformation(input);
124  return shape;
125  }
126 
127 }
A class for sampling random values from a given probability distribution using the alias table techni...
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
A line shape.
An object factory.
void setName(EGS_Input *inp)
Set the name of the object from the information provided by inp.
Global egspp functions header file.
EGS_Input class header file.
A line shape.
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.