HerdSoftware  0.1.1
RefFrame.h
Go to the documentation of this file.
1 /*
2  * RefFrame.h
3  *
4  * Created on: 19 Oct 2018
5  * Author: Nicola Mori
6  */
7 
10 #ifndef HERD_REFFRAME_H_
11 #define HERD_REFFRAME_H_
12 
13 #include <array>
14 #include <stdexcept>
15 #include <string>
16 
17 namespace Herd {
18 
19 namespace RefFrame {
20 
24 enum class Coo : int { NONE = -1, X = 0, Y = 1, Z = 2 };
25 constexpr std::array<Coo, 3> Coos{Coo::X, Coo::Y, Coo::Z};
26 
30 // REMEMBER: modify the ToCoo function if/when new axes are defined.
31 enum class Axis : int { NONE = -1, X = 0, Y = 1, Z = 2 };
33 constexpr int NAxes = 3;
41 constexpr std::array<Axis, NAxes> Axes{Axis::X, Axis::Y, Axis::Z};
42 const std::array<std::string, NAxes> AxisName{"X", "Y", "Z"};
43 inline std::ostream &operator<<(std::ostream &out, const Axis &axis) { return out << AxisName[static_cast<int>(axis)]; }
44 inline std::string operator+(const std::string &str, const Axis &axis) {
45  return str + AxisName[static_cast<int>(axis)];
46 }
47 
52 enum class Direction : int {
53  NONE = -1,
54  Xpos = 0,
55  Xneg = 1,
56  Ypos = 2,
57  Yneg = 3,
58  Zpos = 4,
59  Zneg = 5,
60  XnegYneg = 6,
61  XposYneg = 7,
62  XnegYpos = 8,
63  XposYpos = 9
64 };
65 
67 constexpr int NDirections = 10;
68 
69 constexpr std::array<RefFrame::Direction, RefFrame::NDirections> Directions{
74 
75 const std::array<std::string, RefFrame::NDirections> DirectionName{
76  "Xpos", "Xneg", "Ypos", "Yneg", "Zpos", "Zneg", "XnegYneg", "XposYneg", "XnegYpos", "XposYpos"};
77 
78 inline std::ostream &operator<<(std::ostream &out, const Direction &direction) {
79  return out << DirectionName[static_cast<int>(direction)];
80 }
81 inline std::string operator+(const std::string &str, const Direction &direction) {
82  return str + DirectionName[static_cast<int>(direction)];
83 }
84 
88 enum class View : int { NONE = -1, XY = 0, XZ = 1, YZ = 2 };
90 constexpr int NViews = 3;
98 constexpr std::array<View, NViews> Views{View::XY, View::XZ, View::YZ};
99 const std::array<std::string, NViews> ViewName{"XY", "XZ", "YZ"};
100 
104 enum class Side : int { NONE = -1, Xpos = 0, Xneg = 1, Ypos = 2, Yneg = 3, TOP = 4 };
106 constexpr int NSides = 5;
114 constexpr std::array<Side, NSides> Sides{Side::Xpos, Side::Xneg, Side::Ypos, Side::Yneg, Side::TOP};
115 const std::array<std::string, NSides> SideName{"Xpos", "Xneg", "Ypos", "Yneg", "TOP"};
116 inline std::ostream &operator<<(std::ostream &out, const Side &side) { return out << SideName[static_cast<int>(side)]; }
117 inline std::string operator+(const std::string &str, const Side &side) {
118  return str + SideName[static_cast<int>(side)];
119 }
120 
122 template <RefFrame::Axis T> struct Ortog {};
123 
124 template <> struct Ortog<RefFrame::Axis::X> {
127 };
128 
129 template <> struct Ortog<RefFrame::Axis::Y> {
132 };
133 
134 template <> struct Ortog<RefFrame::Axis::Z> {
137 };
138 
140 template <RefFrame::View T> struct ViewAxes {};
141 
142 template <> struct ViewAxes<RefFrame::View::YZ> {
145 };
146 
147 template <> struct ViewAxes<RefFrame::View::XZ> {
150 };
151 
152 template <> struct ViewAxes<RefFrame::View::XY> {
155 };
156 
157 /* ******** Conversion functions ******** */
158 
168 inline Coo ToCoo(Axis axis) { return static_cast<Coo>(static_cast<int>(axis)); }
169 
177 inline Axis ToAxis(Coo coo) { return static_cast<Axis>(static_cast<int>(coo)); }
178 
185 inline Axis ToAxis(Direction dir) {
186  switch (dir) {
187  case Direction::Xneg:
188  case Direction::Xpos:
189  return Axis::X;
190  case Direction::Yneg:
191  case Direction::Ypos:
192  return Axis::Y;
193  case Direction::Zneg:
194  case Direction::Zpos:
195  return Axis::Z;
196  case Direction::NONE:
197  return Axis::NONE;
198  default:
199  throw std::runtime_error("RefFrame::ToAxis: unknown direction");
200  }
201  throw std::runtime_error("RefFrame::ToAxis: unknown direction");
202 }
203 
214 inline Coo ToCoo(Direction direction) { return static_cast<Coo>(static_cast<int>(RefFrame::ToAxis(direction))); }
215 
216 /* ******** Utility functions ******** */
217 
224 inline Axis NormalAxis(Side side) {
225  switch (side) {
226  case Side::TOP:
227  return Axis::Z;
228  case Side::Xpos:
229  case Side::Xneg:
230  return Axis::X;
231  case Side::Ypos:
232  case Side::Yneg:
233  return Axis::Y;
234  case Side::NONE:
235  return Axis::NONE;
236  }
237  throw std::runtime_error("RefFrame::NormalAxis: unknown side");
238 }
239 
246 inline Axis NormalAxis(View view) {
247  switch (view) {
248  case View::XZ:
249  return Axis::Y;
250  case View::YZ:
251  return Axis::X;
252  case View::XY:
253  return Axis::Z;
254  case View::NONE:
255  return Axis::NONE;
256  }
257  throw std::runtime_error("RefFrame::NormalAxis: unknown side");
258 }
259 
267  switch (side) {
268  case Side::TOP:
269  return Direction::Zpos;
270  case Side::Xpos:
271  return Direction::Xpos;
272  case Side::Xneg:
273  return Direction::Xneg;
274  case Side::Ypos:
275  return Direction::Ypos;
276  case Side::Yneg:
277  return Direction::Yneg;
278  case Side::NONE:
279  return Direction::NONE;
280  }
281  throw std::runtime_error("RefFrame::NormalAxis: unknown side");
282 }
283 
292 inline Axis NormalAxis(Axis ax1, Axis ax2) {
293  if (ax1 == ax2) {
294  throw std::runtime_error("RefFrame::NormalAxis: called with ax1 = ax2");
295  }
296 
297  if ((ax1 == Axis::X && ax2 == Axis::Y) || (ax1 == Axis::Y && ax2 == Axis::X)) {
298  return Axis::Z;
299  }
300  if ((ax1 == Axis::X && ax2 == Axis::Z) || (ax1 == Axis::Z && ax2 == Axis::X)) {
301  return Axis::Y;
302  }
303  if ((ax1 == Axis::Z && ax2 == Axis::Y) || (ax1 == Axis::Y && ax2 == Axis::Z)) {
304  return Axis::X;
305  }
306 
307  return Axis::NONE;
308 }
309 
316 inline bool IsPositive(Direction dir) {
317  if (dir == Direction::XposYpos || dir == Direction::XnegYpos || dir == Direction::XnegYneg ||
318  dir == Direction::XposYneg)
319  throw std::runtime_error(
320  "RefFrame:::IsPositive called with direction = to XposYpos or XnegYpos or XnegYneg or XposYneg");
321  return (dir == Direction::Xpos || dir == Direction::Ypos || dir == Direction::Zpos);
322 }
323 
324 } // namespace RefFrame
325 
326 } // namespace Herd
327 
328 #endif /* HERD_REFFRAME_H_ */
constexpr std::array< Coo, 3 > Coos
Definition: RefFrame.h:25
Coo
Aliases for the three space coordinates. RefFrame.h common/RefFrame.h.
Definition: RefFrame.h:24
CssGeoParams.h CssGeoParams class declaration.
Definition: CaloPDCalibrationAlgo.h:24
bool IsPositive(Direction dir)
Tells if a direction is positive or not.
Definition: RefFrame.h:316
constexpr int NSides
The number of sides (i.e. (number of elements of Side) - 1).
Definition: RefFrame.h:106
Base struct to get the two axes orthogonal to a direction.
Definition: RefFrame.h:122
Base struct to get the two axes of a given view.
Definition: RefFrame.h:140
const std::array< std::string, NViews > ViewName
Definition: RefFrame.h:99
Axis ToAxis(Coo coo)
Associates an axis to a coordinate .
Definition: RefFrame.h:177
constexpr int NViews
The number of views (i.e. (number of elements of View) - 1).
Definition: RefFrame.h:90
const std::array< std::string, NAxes > AxisName
Definition: RefFrame.h:42
Side
Aliases for the five sides. RefFrame.h common/RefFrame.h.
Definition: RefFrame.h:104
constexpr int NAxes
The number of axes (i.e. (number of elements of Axis) - 1).
Definition: RefFrame.h:33
Axis
Aliases for the axes. RefFrame.h common/RefFrame.h.
Definition: RefFrame.h:31
Coo ToCoo(Axis axis)
Associates a coordinate to an axis.
Definition: RefFrame.h:168
constexpr std::array< View, NViews > Views
Array of all the views.
Definition: RefFrame.h:98
std::string operator+(const std::string &str, const Axis &axis)
Definition: RefFrame.h:44
const std::array< std::string, RefFrame::NDirections > DirectionName
Definition: RefFrame.h:75
Direction NormalDirection(Side side)
Definition: RefFrame.h:266
constexpr std::array< RefFrame::Direction, RefFrame::NDirections > Directions
Definition: RefFrame.h:69
Axis NormalAxis(Side side)
Definition: RefFrame.h:224
const std::array< std::string, NSides > SideName
Definition: RefFrame.h:115
constexpr int NDirections
The number of directions (i.e. (number of elements of Direction) - 1).
Definition: RefFrame.h:67
constexpr std::array< Side, NSides > Sides
Array of all the sides.
Definition: RefFrame.h:114
std::ostream & operator<<(std::ostream &out, const Axis &axis)
Definition: RefFrame.h:43
View
Aliases for the three 2D projection planes. RefFrame.h common/RefFrame.h.
Definition: RefFrame.h:88
constexpr std::array< Axis, NAxes > Axes
Array of all the axes.
Definition: RefFrame.h:41
Direction
Aliases for the six axis directions. RefFrame.h common/RefFrame.h.
Definition: RefFrame.h:52