EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_shape_collection.cpp
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ shape collection
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: Marc Chamberland
27 #
28 ###############################################################################
29 */
30 
31 
37 #include "egs_shape_collection.h"
38 #include "egs_input.h"
39 #include "egs_functions.h"
40 
41 EGS_ShapeCollection::EGS_ShapeCollection(const vector<EGS_BaseShape *> &Shapes,
42  const vector<EGS_Float> &Probs, const string &Name, EGS_ObjectFactory *f) :
43  EGS_BaseShape(Name,f), nshape(0) {
44  int n1 = Shapes.size(), n2 = Probs.size();
45  if (n1 < 2 || n2 < 2 || n1 != n2) {
46  egsWarning("EGS_ShapeCollection::EGS_ShapeCollection: invalid input\n");
47  return;
48  }
49  nshape = n1;
50  shapes = new EGS_BaseShape* [nshape];
51  EGS_Float *p = new EGS_Float [nshape];
52  for (int j=0; j<nshape; j++) {
53  shapes[j] = Shapes[j];
54  shapes[j]->ref();
55  p[j] = Probs[j];
56  }
57  table = new EGS_SimpleAliasTable(nshape,p);
58  delete [] p;
59 }
60 
61 extern "C" {
62 
63  EGS_SHAPE_COLLECTION_EXPORT EGS_BaseShape *createShape(EGS_Input *input,
64  EGS_ObjectFactory *f) {
65  if (!input) {
66  egsWarning("createShape(shape collection): null input?\n");
67  return 0;
68  }
69  vector<EGS_BaseShape *> shapes;
70  vector<EGS_Float> probs;
71  EGS_Input *ishape;
72  while ((ishape = input->takeInputItem("shape",false))) {
74  if (!shape) {
75  egsWarning("createShape(shape collection): got null shape\n");
76  }
77  else {
78  shapes.push_back(shape);
79  }
80  delete ishape;
81  }
82  vector<string> snames;
83  int err = input->getInput("shape names",snames);
84  if (!err && snames.size() > 0) {
85  for (unsigned int j=0; j<snames.size(); j++) {
86  EGS_BaseShape *shape = EGS_BaseShape::getShape(snames[j]);
87  if (!shape) egsWarning("createShape(shape collection): no shape "
88  "named %s exists\n",snames[j].c_str());
89  else {
90  shapes.push_back(shape);
91  }
92  }
93  }
94  err = input->getInput("probabilities",probs);
95  bool ok = true;
96  if (err) {
97  egsWarning("createShape(shape collection): no 'probabilities' input\n");
98  ok = false;
99  }
100  if (shapes.size() < 2) {
101  egsWarning("createShape(shape collection): at least 2 shapes are "
102  "needed for a shape collection, you defined %d\n",shapes.size());
103  ok = false;
104  }
105  if (shapes.size() != probs.size()) {
106  egsWarning("createShape(shape collection): the number of shapes (%d)"
107  " is not the same as the number of input probabilities (%d)\n");
108  ok = false;
109  }
110  for (unsigned int i=0; i<probs.size(); i++) {
111  if (probs[i] < 0) {
112  egsWarning("createShape(shape collection): probabilities must not"
113  " be negative, but your input probability %d is %g\n",
114  i,probs[i]);
115  ok = false;
116  }
117  }
118  if (!ok) {
119  for (unsigned int j=0; j<shapes.size(); j++) {
120  EGS_Object::deleteObject(shapes[j]);
121  }
122  return 0;
123  }
124  EGS_ShapeCollection *s = new EGS_ShapeCollection(shapes,probs,"",f);
125  s->setName(input);
126  s->setTransformation(input);
127  return s;
128  }
129 
130 }
void setTransformation(EGS_Input *inp)
Set the transformation attached to this shape.
Definition: egs_shapes.cpp:68
static EGS_BaseShape * createShape(EGS_Input *inp)
Create a shape from the information pointed to by inp.
Definition: egs_shapes.cpp:51
A shape collection.
EGS_Input class header file.
static EGS_BaseShape * getShape(const string &Name)
Get a pointer to the shape named Name.
Definition: egs_shapes.cpp:63
Global egspp functions header file.
static void deleteObject(EGS_Object *o)
Delete an object.
Base shape class. All shapes in the EGSnrc C++ class library are derived from EGS_BaseShape.
Definition: egs_shapes.h:112
A class for sampling random bins from a given probability distribution using the alias table techniqu...
An object factory.
void setName(EGS_Input *inp)
Set the name of the object from the information provided by 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
EGS_Input * takeInputItem(const string &key, bool self=true)
Get the property named key.
Definition: egs_input.cpp:226
A shape collection.
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.