HerdSoftware  0.3.2
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 const std::array<std::string, 3> CooName{"X", "Y", "Z"};
29 inline std::ostream &operator<<(std::ostream &out, const Coo &coo) { return out << CooName[static_cast<int>(coo)]; }
30 inline std::string operator+(const std::string &str, const Coo &coo) { return str + CooName[static_cast<int>(coo)]; }
31 
35 // REMEMBER: modify the ToCoo function if/when new axes are defined.
36 enum class Axis : int { NONE = -1, X = 0, Y = 1, Z = 2 };
38 constexpr int NAxes = 3;
46 constexpr std::array<Axis, NAxes> Axes{Axis::X, Axis::Y, Axis::Z};
47 const std::array<std::string, NAxes> AxisName{"X", "Y", "Z"};
48 inline std::ostream &operator<<(std::ostream &out, const Axis &axis) { return out << AxisName[static_cast<int>(axis)]; }
49 inline std::string operator+(const std::string &str, const Axis &axis) {
50  return str + AxisName[static_cast<int>(axis)];
51 }
52 
57 enum class Direction : int {
58  NONE = -1,
59  Xpos = 0,
60  Xneg = 1,
61  Ypos = 2,
62  Yneg = 3,
63  Zpos = 4,
64  Zneg = 5,
65  XnegYneg = 6,
66  XposYneg = 7,
67  XnegYpos = 8,
68  XposYpos = 9
69 };
70 
72 constexpr int NDirections = 10;
73 
74 constexpr std::array<RefFrame::Direction, RefFrame::NDirections> Directions{
79 
80 const std::array<std::string, RefFrame::NDirections> DirectionName{
81  "Xpos", "Xneg", "Ypos", "Yneg", "Zpos", "Zneg", "XnegYneg", "XposYneg", "XnegYpos", "XposYpos"};
82 
83 inline std::ostream &operator<<(std::ostream &out, const Direction &direction) {
84  return out << DirectionName[static_cast<int>(direction)];
85 }
86 inline std::string operator+(const std::string &str, const Direction &direction) {
87  return str + DirectionName[static_cast<int>(direction)];
88 }
89 
93 enum class View : int { NONE = -1, XY = 0, XZ = 1, YZ = 2 };
95 constexpr int NViews = 3;
103 constexpr std::array<View, NViews> Views{View::XY, View::XZ, View::YZ};
104 const std::array<std::string, NViews> ViewName{"XY", "XZ", "YZ"};
105 inline std::ostream &operator<<(std::ostream &out, const View &view) { return out << ViewName[static_cast<int>(view)]; }
106 inline std::string operator+(const std::string &str, const View &view) {
107  return str + ViewName[static_cast<int>(view)];
108 }
109 
113 enum class Side : int { NONE = -1, Xpos = 0, Xneg = 1, Ypos = 2, Yneg = 3, TOP = 4 };
115 constexpr int NSides = 5;
123 constexpr std::array<Side, NSides> Sides{Side::Xpos, Side::Xneg, Side::Ypos, Side::Yneg, Side::TOP};
124 const std::array<std::string, NSides> SideName{"Xpos", "Xneg", "Ypos", "Yneg", "TOP"};
125 inline std::ostream &operator<<(std::ostream &out, const Side &side) { return out << SideName[static_cast<int>(side)]; }
126 inline std::string operator+(const std::string &str, const Side &side) {
127  return str + SideName[static_cast<int>(side)];
128 }
129 
131 template <RefFrame::Axis T> struct Ortog {};
132 
133 template <> struct Ortog<RefFrame::Axis::X> {
136 };
137 
138 template <> struct Ortog<RefFrame::Axis::Y> {
141 };
142 
143 template <> struct Ortog<RefFrame::Axis::Z> {
146 };
147 
149 struct ViewAxes {
152 };
153 
155  switch (view) {
156  case View::XZ:
157  return {Axis::X, Axis::Z};
158  break;
159  case View::YZ:
160  return {Axis::Y, Axis::Z};
161  break;
162  case View::XY:
163  return {Axis::X, Axis::Y};
164  break;
165  default:
166  throw Exception("RefFrame::AxesOf: Invalid view");
167  break;
168  }
169 }
170 
171 /* ******** Conversion functions ******** */
172 
182 inline Coo ToCoo(Axis axis) { return static_cast<Coo>(static_cast<int>(axis)); }
183 
191 inline Axis ToAxis(Coo coo) { return static_cast<Axis>(static_cast<int>(coo)); }
192 
199 inline Axis ToAxis(Direction dir) {
200  switch (dir) {
201  case Direction::Xneg:
202  case Direction::Xpos:
203  return Axis::X;
204  case Direction::Yneg:
205  case Direction::Ypos:
206  return Axis::Y;
207  case Direction::Zneg:
208  case Direction::Zpos:
209  return Axis::Z;
210  case Direction::NONE:
211  return Axis::NONE;
212  default:
213  throw std::runtime_error("RefFrame::ToAxis: unknown direction");
214  }
215  throw std::runtime_error("RefFrame::ToAxis: unknown direction");
216 }
217 
228 inline Coo ToCoo(Direction direction) { return static_cast<Coo>(static_cast<int>(RefFrame::ToAxis(direction))); }
229 
230 /* ******** Utility functions ******** */
231 
238 inline Axis NormalAxis(Side side) {
239  switch (side) {
240  case Side::TOP:
241  return Axis::Z;
242  case Side::Xpos:
243  case Side::Xneg:
244  return Axis::X;
245  case Side::Ypos:
246  case Side::Yneg:
247  return Axis::Y;
248  case Side::NONE:
249  return Axis::NONE;
250  }
251  throw std::runtime_error("RefFrame::NormalAxis: unknown side");
252 }
253 
260 inline Axis NormalAxis(View view) {
261  switch (view) {
262  case View::XZ:
263  return Axis::Y;
264  case View::YZ:
265  return Axis::X;
266  case View::XY:
267  return Axis::Z;
268  case View::NONE:
269  return Axis::NONE;
270  }
271  throw std::runtime_error("RefFrame::NormalAxis: unknown side");
272 }
273 
281  switch (side) {
282  case Side::TOP:
283  return Direction::Zpos;
284  case Side::Xpos:
285  return Direction::Xpos;
286  case Side::Xneg:
287  return Direction::Xneg;
288  case Side::Ypos:
289  return Direction::Ypos;
290  case Side::Yneg:
291  return Direction::Yneg;
292  case Side::NONE:
293  return Direction::NONE;
294  }
295  throw std::runtime_error("RefFrame::NormalAxis: unknown side");
296 }
297 
306 inline Axis NormalAxis(Axis ax1, Axis ax2) {
307  if (ax1 == ax2) {
308  throw std::runtime_error("RefFrame::NormalAxis: called with ax1 = ax2");
309  }
310 
311  if ((ax1 == Axis::X && ax2 == Axis::Y) || (ax1 == Axis::Y && ax2 == Axis::X)) {
312  return Axis::Z;
313  }
314  if ((ax1 == Axis::X && ax2 == Axis::Z) || (ax1 == Axis::Z && ax2 == Axis::X)) {
315  return Axis::Y;
316  }
317  if ((ax1 == Axis::Z && ax2 == Axis::Y) || (ax1 == Axis::Y && ax2 == Axis::Z)) {
318  return Axis::X;
319  }
320 
321  return Axis::NONE;
322 }
323 
331 inline std::pair<Axis, Axis> NormalAxes(Axis ax) {
332  std::pair<Axis, Axis> normalAxes;
333  if (ax == Axis::X) {
334  normalAxes.first = Axis::Y;
335  normalAxes.second = Axis::Z;
336  } else if (ax == Axis::Y) {
337  normalAxes.first = Axis::Z;
338  normalAxes.second = Axis::X;
339  } else if (ax == Axis::Z) {
340  normalAxes.first = Axis::X;
341  normalAxes.second = Axis::Y;
342  } else {
343  throw Herd::Exception("RefFrame::NormalAxes: called with invalid axis");
344  }
345  return normalAxes;
346 }
347 
354 inline bool IsPositive(Direction dir) {
355  if (dir == Direction::XposYpos || dir == Direction::XnegYpos || dir == Direction::XnegYneg ||
356  dir == Direction::XposYneg)
357  throw std::runtime_error(
358  "RefFrame:::IsPositive called with direction = to XposYpos or XnegYpos or XnegYneg or XposYneg");
359  return (dir == Direction::Xpos || dir == Direction::Ypos || dir == Direction::Zpos);
360 }
361 
362 } // namespace RefFrame
363 
364 } // namespace Herd
365 
366 #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
std::string operator+(const std::string &str, const Coo &coo)
Definition: RefFrame.h:30
bool IsPositive(Direction dir)
Tells if a direction is positive or not.
Definition: RefFrame.h:354
constexpr int NSides
The number of sides (i.e. (number of elements of Side) - 1).
Definition: RefFrame.h:115
RefFrame::Axis ordinate
Definition: RefFrame.h:151
Base struct to get the two axes orthogonal to a direction.
Definition: RefFrame.h:131
Base struct to get the two axes of a given view.
Definition: RefFrame.h:149
const std::array< std::string, NViews > ViewName
Definition: RefFrame.h:104
Axis ToAxis(Coo coo)
Associates an axis to a coordinate .
Definition: RefFrame.h:191
constexpr int NViews
The number of views (i.e. (number of elements of View) - 1).
Definition: RefFrame.h:95
const std::array< std::string, 3 > CooName
Definition: RefFrame.h:28
const std::array< std::string, NAxes > AxisName
Definition: RefFrame.h:47
Side
Aliases for the five sides. RefFrame.h common/RefFrame.h.
Definition: RefFrame.h:113
constexpr int NAxes
The number of axes (i.e. (number of elements of Axis) - 1).
Definition: RefFrame.h:38
Axis
Aliases for the axes. RefFrame.h common/RefFrame.h.
Definition: RefFrame.h:36
Coo ToCoo(Axis axis)
Associates a coordinate to an axis.
Definition: RefFrame.h:182
constexpr std::array< View, NViews > Views
Array of all the views.
Definition: RefFrame.h:103
RefFrame::ViewAxes AxesOf(RefFrame::View view)
Definition: RefFrame.h:154
const std::array< std::string, RefFrame::NDirections > DirectionName
Definition: RefFrame.h:80
Direction NormalDirection(Side side)
Definition: RefFrame.h:280
std::pair< Axis, Axis > NormalAxes(Axis ax)
Given one axes get the other two ;)
Definition: RefFrame.h:331
constexpr std::array< RefFrame::Direction, RefFrame::NDirections > Directions
Definition: RefFrame.h:74
Axis NormalAxis(Side side)
Definition: RefFrame.h:238
const std::array< std::string, NSides > SideName
Definition: RefFrame.h:124
constexpr int NDirections
The number of directions (i.e. (number of elements of Direction) - 1).
Definition: RefFrame.h:72
constexpr std::array< Side, NSides > Sides
Array of all the sides.
Definition: RefFrame.h:123
Definition: Exception.h:24
RefFrame::Axis abscissa
Definition: RefFrame.h:150
std::ostream & operator<<(std::ostream &out, const Coo &coo)
Definition: RefFrame.h:29
View
Aliases for the three 2D projection planes. RefFrame.h common/RefFrame.h.
Definition: RefFrame.h:93
constexpr std::array< Axis, NAxes > Axes
Array of all the axes.
Definition: RefFrame.h:46
Direction
Aliases for the six axis directions. RefFrame.h common/RefFrame.h.
Definition: RefFrame.h:57