HerdSoftware  0.2.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 #include "common/Exception.h"
18 
19 namespace Herd {
20 
21 namespace RefFrame {
22 
26 enum class Coo : int { NONE = -1, X = 0, Y = 1, Z = 2 };
27 constexpr std::array<Coo, 3> Coos{Coo::X, Coo::Y, Coo::Z};
28 
32 // REMEMBER: modify the ToCoo function if/when new axes are defined.
33 enum class Axis : int { NONE = -1, X = 0, Y = 1, Z = 2 };
35 constexpr int NAxes = 3;
43 constexpr std::array<Axis, NAxes> Axes{Axis::X, Axis::Y, Axis::Z};
44 const std::array<std::string, NAxes> AxisName{"X", "Y", "Z"};
45 inline std::ostream &operator<<(std::ostream &out, const Axis &axis) { return out << AxisName[static_cast<int>(axis)]; }
46 inline std::string operator+(const std::string &str, const Axis &axis) {
47  return str + AxisName[static_cast<int>(axis)];
48 }
49 
54 enum class Direction : int {
55  NONE = -1,
56  Xpos = 0,
57  Xneg = 1,
58  Ypos = 2,
59  Yneg = 3,
60  Zpos = 4,
61  Zneg = 5,
62  XnegYneg = 6,
63  XposYneg = 7,
64  XnegYpos = 8,
65  XposYpos = 9
66 };
67 
69 constexpr int NDirections = 10;
70 
71 constexpr std::array<RefFrame::Direction, RefFrame::NDirections> Directions{
76 
77 const std::array<std::string, RefFrame::NDirections> DirectionName{
78  "Xpos", "Xneg", "Ypos", "Yneg", "Zpos", "Zneg", "XnegYneg", "XposYneg", "XnegYpos", "XposYpos"};
79 
80 inline std::ostream &operator<<(std::ostream &out, const Direction &direction) {
81  return out << DirectionName[static_cast<int>(direction)];
82 }
83 inline std::string operator+(const std::string &str, const Direction &direction) {
84  return str + DirectionName[static_cast<int>(direction)];
85 }
86 
90 enum class View : int { NONE = -1, XY = 0, XZ = 1, YZ = 2 };
92 constexpr int NViews = 3;
100 constexpr std::array<View, NViews> Views{View::XY, View::XZ, View::YZ};
101 const std::array<std::string, NViews> ViewName{"XY", "XZ", "YZ"};
102 
106 enum class Side : int { NONE = -1, Xpos = 0, Xneg = 1, Ypos = 2, Yneg = 3, TOP = 4 };
108 constexpr int NSides = 5;
116 constexpr std::array<Side, NSides> Sides{Side::Xpos, Side::Xneg, Side::Ypos, Side::Yneg, Side::TOP};
117 const std::array<std::string, NSides> SideName{"Xpos", "Xneg", "Ypos", "Yneg", "TOP"};
118 inline std::ostream &operator<<(std::ostream &out, const Side &side) { return out << SideName[static_cast<int>(side)]; }
119 inline std::string operator+(const std::string &str, const Side &side) {
120  return str + SideName[static_cast<int>(side)];
121 }
122 
124 template <RefFrame::Axis T> struct Ortog {};
125 
126 template <> struct Ortog<RefFrame::Axis::X> {
129 };
130 
131 template <> struct Ortog<RefFrame::Axis::Y> {
134 };
135 
136 template <> struct Ortog<RefFrame::Axis::Z> {
139 };
140 
142 struct ViewAxes {
145 };
146 
148  switch (view) {
149  case View::XZ:
150  return {Axis::X, Axis::Z};
151  break;
152  case View::YZ:
153  return {Axis::Y, Axis::Z};
154  break;
155  case View::XY:
156  return {Axis::X, Axis::Y};
157  break;
158  default:
159  throw Exception("RefFrame::AxesOf: Invalid view");
160  break;
161  }
162 }
163 
164 /* ******** Conversion functions ******** */
165 
175 inline Coo ToCoo(Axis axis) { return static_cast<Coo>(static_cast<int>(axis)); }
176 
184 inline Axis ToAxis(Coo coo) { return static_cast<Axis>(static_cast<int>(coo)); }
185 
192 inline Axis ToAxis(Direction dir) {
193  switch (dir) {
194  case Direction::Xneg:
195  case Direction::Xpos:
196  return Axis::X;
197  case Direction::Yneg:
198  case Direction::Ypos:
199  return Axis::Y;
200  case Direction::Zneg:
201  case Direction::Zpos:
202  return Axis::Z;
203  case Direction::NONE:
204  return Axis::NONE;
205  default:
206  throw std::runtime_error("RefFrame::ToAxis: unknown direction");
207  }
208  throw std::runtime_error("RefFrame::ToAxis: unknown direction");
209 }
210 
221 inline Coo ToCoo(Direction direction) { return static_cast<Coo>(static_cast<int>(RefFrame::ToAxis(direction))); }
222 
223 /* ******** Utility functions ******** */
224 
231 inline Axis NormalAxis(Side side) {
232  switch (side) {
233  case Side::TOP:
234  return Axis::Z;
235  case Side::Xpos:
236  case Side::Xneg:
237  return Axis::X;
238  case Side::Ypos:
239  case Side::Yneg:
240  return Axis::Y;
241  case Side::NONE:
242  return Axis::NONE;
243  }
244  throw std::runtime_error("RefFrame::NormalAxis: unknown side");
245 }
246 
253 inline Axis NormalAxis(View view) {
254  switch (view) {
255  case View::XZ:
256  return Axis::Y;
257  case View::YZ:
258  return Axis::X;
259  case View::XY:
260  return Axis::Z;
261  case View::NONE:
262  return Axis::NONE;
263  }
264  throw std::runtime_error("RefFrame::NormalAxis: unknown side");
265 }
266 
274  switch (side) {
275  case Side::TOP:
276  return Direction::Zpos;
277  case Side::Xpos:
278  return Direction::Xpos;
279  case Side::Xneg:
280  return Direction::Xneg;
281  case Side::Ypos:
282  return Direction::Ypos;
283  case Side::Yneg:
284  return Direction::Yneg;
285  case Side::NONE:
286  return Direction::NONE;
287  }
288  throw std::runtime_error("RefFrame::NormalAxis: unknown side");
289 }
290 
299 inline Axis NormalAxis(Axis ax1, Axis ax2) {
300  if (ax1 == ax2) {
301  throw std::runtime_error("RefFrame::NormalAxis: called with ax1 = ax2");
302  }
303 
304  if ((ax1 == Axis::X && ax2 == Axis::Y) || (ax1 == Axis::Y && ax2 == Axis::X)) {
305  return Axis::Z;
306  }
307  if ((ax1 == Axis::X && ax2 == Axis::Z) || (ax1 == Axis::Z && ax2 == Axis::X)) {
308  return Axis::Y;
309  }
310  if ((ax1 == Axis::Z && ax2 == Axis::Y) || (ax1 == Axis::Y && ax2 == Axis::Z)) {
311  return Axis::X;
312  }
313 
314  return Axis::NONE;
315 }
316 
323 inline bool IsPositive(Direction dir) {
324  if (dir == Direction::XposYpos || dir == Direction::XnegYpos || dir == Direction::XnegYneg ||
325  dir == Direction::XposYneg)
326  throw std::runtime_error(
327  "RefFrame:::IsPositive called with direction = to XposYpos or XnegYpos or XnegYneg or XposYneg");
328  return (dir == Direction::Xpos || dir == Direction::Ypos || dir == Direction::Zpos);
329 }
330 
331 } // namespace RefFrame
332 
333 } // namespace Herd
334 
335 #endif /* HERD_REFFRAME_H_ */
constexpr std::array< Coo, 3 > Coos
Definition: RefFrame.h:27
Coo
Aliases for the three space coordinates. RefFrame.h common/RefFrame.h.
Definition: RefFrame.h:26
CssGeoParams.h CssGeoParams class declaration.
Definition: CaloPDCalibrationAlgo.h:24
bool IsPositive(Direction dir)
Tells if a direction is positive or not.
Definition: RefFrame.h:323
constexpr int NSides
The number of sides (i.e. (number of elements of Side) - 1).
Definition: RefFrame.h:108
RefFrame::Axis ordinate
Definition: RefFrame.h:144
Base struct to get the two axes orthogonal to a direction.
Definition: RefFrame.h:124
Base struct to get the two axes of a given view.
Definition: RefFrame.h:142
const std::array< std::string, NViews > ViewName
Definition: RefFrame.h:101
Axis ToAxis(Coo coo)
Associates an axis to a coordinate .
Definition: RefFrame.h:184
constexpr int NViews
The number of views (i.e. (number of elements of View) - 1).
Definition: RefFrame.h:92
const std::array< std::string, NAxes > AxisName
Definition: RefFrame.h:44
Side
Aliases for the five sides. RefFrame.h common/RefFrame.h.
Definition: RefFrame.h:106
constexpr int NAxes
The number of axes (i.e. (number of elements of Axis) - 1).
Definition: RefFrame.h:35
Axis
Aliases for the axes. RefFrame.h common/RefFrame.h.
Definition: RefFrame.h:33
Coo ToCoo(Axis axis)
Associates a coordinate to an axis.
Definition: RefFrame.h:175
constexpr std::array< View, NViews > Views
Array of all the views.
Definition: RefFrame.h:100
std::string operator+(const std::string &str, const Axis &axis)
Definition: RefFrame.h:46
RefFrame::ViewAxes AxesOf(RefFrame::View view)
Definition: RefFrame.h:147
const std::array< std::string, RefFrame::NDirections > DirectionName
Definition: RefFrame.h:77
Direction NormalDirection(Side side)
Definition: RefFrame.h:273
constexpr std::array< RefFrame::Direction, RefFrame::NDirections > Directions
Definition: RefFrame.h:71
Axis NormalAxis(Side side)
Definition: RefFrame.h:231
const std::array< std::string, NSides > SideName
Definition: RefFrame.h:117
constexpr int NDirections
The number of directions (i.e. (number of elements of Direction) - 1).
Definition: RefFrame.h:69
constexpr std::array< Side, NSides > Sides
Array of all the sides.
Definition: RefFrame.h:116
std::ostream & operator<<(std::ostream &out, const Axis &axis)
Definition: RefFrame.h:45
Definition: Exception.h:24
RefFrame::Axis abscissa
Definition: RefFrame.h:143
View
Aliases for the three 2D projection planes. RefFrame.h common/RefFrame.h.
Definition: RefFrame.h:90
constexpr std::array< Axis, NAxes > Axes
Array of all the axes.
Definition: RefFrame.h:43
Direction
Aliases for the six axis directions. RefFrame.h common/RefFrame.h.
Definition: RefFrame.h:54