EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_transformations.cpp
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ transformations
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: Reid Townson
27 # Alexandre Demelo
28 #
29 ###############################################################################
30 */
31 
32 
38 #include "egs_transformations.h"
39 #include "egs_input.h"
40 
41 #include <vector>
42 using std::vector;
43 
45  if (!i) {
46  return 0;
47  }
48  EGS_Input *input;
49  bool delete_it = false;
50  if (i->isA("transformation")) {
51  input = i;
52  }
53  else {
54  input = i->takeInputItem("transformation");
55  if (!input) {
56  return 0;
57  }
58  delete_it = true;
59  }
60  EGS_Vector t;
61  vector<EGS_Float> tmp;
62  int err = input->getInput("translation",tmp);
63  if (!err && tmp.size() == 3) {
64  t = EGS_Vector(tmp[0],tmp[1],tmp[2]);
65  }
66  EGS_AffineTransform *result;
67  err = input->getInput("rotation vector",tmp);
68  if (!err && tmp.size() == 3)
69  result = new EGS_AffineTransform(
70  EGS_RotationMatrix(EGS_Vector(tmp[0],tmp[1],tmp[2])),t);
71  else {
72  err = input->getInput("rotation",tmp);
73  if (!err) {
74  if (tmp.size() == 2) result = new EGS_AffineTransform(
75  EGS_RotationMatrix(tmp[0],tmp[1]),t);
76  else if (tmp.size() == 3) result = new EGS_AffineTransform(
77  EGS_RotationMatrix(tmp[0],tmp[1],tmp[2]),t);
78  else if (tmp.size() == 4) {
79  EGS_Vector tmp1(tmp[0],tmp[1],tmp[2]);
80  EGS_RotationMatrix R1(tmp1);
82  EGS_RotationMatrix Rtot = R1.inverse()*R2*R1;
83  result = new EGS_AffineTransform(Rtot,t);
84  }
85  else if (tmp.size() == 9) {
86  EGS_RotationMatrix R(tmp[0],tmp[1],tmp[2],
87  tmp[3],tmp[4],tmp[5],
88  tmp[6],tmp[7],tmp[8]);
89  if (!R.isRotation())
90  egsWarning("getTransformation: the rotation specified by\n"
91  " %g %g %g\n %g %g %g\n %g %g %g\n"
92  " is not a rotation\n",tmp[0],tmp[1],tmp[2],
93  tmp[3],tmp[4],tmp[5],tmp[6],tmp[7],tmp[8]);
94  result = new EGS_AffineTransform(R,t);
95  }
96  else {
97  result = new EGS_AffineTransform(EGS_RotationMatrix(),t);
98  }
99  }
100  else {
101  result = new EGS_AffineTransform(EGS_RotationMatrix(),t);
102  }
103  }
104  if (delete_it) {
105  delete input;
106  }
107  return result;
108 }
109 
110 // Overload for dynamic geometry
111 EGS_AffineTransform *EGS_AffineTransform::getTransformation(vector<EGS_Float> translation, vector<EGS_Float> rotation) {
112 
113  /* The following method is used by the dynamic geometry class to create a
114  * transformation corresponding to a certain translation and rotation
115  * vector. These vectors are determined through the control points and the
116  * sampled time value. The sampled transformation coordinates are passed
117  * here to build a transformation */
118 
119  // First check that appropriate values have been provided. May work with
120  // either 2 or 3 rotation parameters, and exactly 3 translation parameters
121  if (translation.size()!=3 || (rotation.size()!=2 && rotation.size()!=3)) {
122  egsWarning("getTransformation: invalid transformation parameters\n");
123  return 0;
124  }
125 
126  EGS_AffineTransform *result; //the returned transformation
127 
128  // EGS_vector holding translation parameters is defined to pass to the
129  // EGS_AffineTransform constructor
130  EGS_Vector t = EGS_Vector(translation[0],translation[1],translation[2]);
131 
132  // Check the size of the rotation vector provided and call the appropriate
133  // EGS_AffineTransform constructor (converting rotation vector to matrix)
134  if (rotation.size() == 2) {
135  result = new EGS_AffineTransform(EGS_RotationMatrix(rotation[0],rotation[1]),t);
136  }
137  else if (rotation.size() == 3) {
138  result = new EGS_AffineTransform(EGS_RotationMatrix(rotation[0],rotation[1],rotation[2]),t);
139  }
140  else {
141  result = new EGS_AffineTransform(EGS_RotationMatrix(),t);
142  }
143  return result;
144 }
A class providing affine transformations.
static EGS_AffineTransform * getTransformation(EGS_Input *inp)
Constructs an affine transformation object from the input pointed to by inp and returns a pointer to ...
EGS_AffineTransform()
Constructs a unit affine transformation.
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
bool isA(const string &key) const
Definition: egs_input.cpp:278
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
A class for vector rotations.
bool isRotation() const
Is this object a real rotation matrix?
static EGS_RotationMatrix rotZ(EGS_Float cphi, EGS_Float sphi)
Returns a rotation around the z-axis by the angle with cphi, sphi = .
EGS_RotationMatrix inverse() const
Returns the inverse matrix.
A class representing 3D vectors.
Definition: egs_vector.h:57
EGS_Input class header file.
EGS_AffineTransform and EGS_RotationMatrix class header file.
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.