HerdSoftware  0.4.0
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 
18 
19 namespace Herd::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 const std::array<std::string, 3> CooName{"X", "Y", "Z"};
27 inline std::ostream &operator<<(std::ostream &out, const Coo &coo) { return out << CooName[static_cast<int>(coo)]; }
28 inline std::string operator+(const std::string &str, const Coo &coo) { return str + CooName[static_cast<int>(coo)]; }
29 
33 // REMEMBER: modify the ToCoo function if/when new axes are defined.
34 enum class Axis : int { NONE = -1, X = 0, Y = 1, Z = 2 };
36 constexpr int NAxes = 3;
44 constexpr std::array<Axis, NAxes> Axes{Axis::X, Axis::Y, Axis::Z};
45 const std::array<std::string, NAxes> AxisName{"X", "Y", "Z"};
46 inline std::ostream &operator<<(std::ostream &out, const Axis &axis) { return out << AxisName[static_cast<int>(axis)]; }
47 inline std::string operator+(const std::string &str, const Axis &axis) {
48  return str + AxisName[static_cast<int>(axis)];
49 }
50 
55 enum class Direction : int {
56  NONE = -1,
57  Xpos = 0,
58  Xneg = 1,
59  Ypos = 2,
60  Yneg = 3,
61  Zpos = 4,
62  Zneg = 5,
63  XnegYneg = 6,
64  XposYneg = 7,
65  XnegYpos = 8,
66  XposYpos = 9
67 };
68 
70 constexpr int NDirections = 10;
71 
72 constexpr std::array<RefFrame::Direction, RefFrame::NDirections> Directions{
77 
78 const std::array<std::string, RefFrame::NDirections> DirectionName{
79  "Xpos", "Xneg", "Ypos", "Yneg", "Zpos", "Zneg", "XnegYneg", "XposYneg", "XnegYpos", "XposYpos"};
80 
81 inline std::ostream &operator<<(std::ostream &out, const Direction &direction) {
82  return out << DirectionName[static_cast<int>(direction)];
83 }
84 inline std::string operator+(const std::string &str, const Direction &direction) {
85  return str + DirectionName[static_cast<int>(direction)];
86 }
87 
91 enum class View : int { NONE = -1, XY = 0, XZ = 1, YZ = 2 };
93 constexpr int NViews = 3;
101 constexpr std::array<View, NViews> Views{View::XY, View::XZ, View::YZ};
102 const std::array<std::string, NViews> ViewName{"XY", "XZ", "YZ"};
103 inline std::ostream &operator<<(std::ostream &out, const View &view) { return out << ViewName[static_cast<int>(view)]; }
104 inline std::string operator+(const std::string &str, const View &view) {
105  return str + ViewName[static_cast<int>(view)];
106 }
107 
111 enum class Side : int { NONE = -1, Xpos = 0, Xneg = 1, Ypos = 2, Yneg = 3, TOP = 4 };
113 constexpr int NSides = 5;
121 constexpr std::array<Side, NSides> Sides{Side::Xpos, Side::Xneg, Side::Ypos, Side::Yneg, Side::TOP};
122 const std::array<std::string, NSides> SideName{"Xpos", "Xneg", "Ypos", "Yneg", "TOP"};
123 inline std::ostream &operator<<(std::ostream &out, const Side &side) { return out << SideName[static_cast<int>(side)]; }
124 inline std::string operator+(const std::string &str, const Side &side) {
125  return str + SideName[static_cast<int>(side)];
126 }
127 
129 template <RefFrame::Axis T> struct Ortog {};
130 
131 template <> struct Ortog<RefFrame::Axis::X> {
134 };
135 
136 template <> struct Ortog<RefFrame::Axis::Y> {
139 };
140 
141 template <> struct Ortog<RefFrame::Axis::Z> {
144 };
145 
147 struct ViewAxes {
150 };
151 
152 inline constexpr RefFrame::ViewAxes AxesOf(RefFrame::View view) {
153  switch (view) {
154  case View::XZ:
155  return {Axis::X, Axis::Z};
156  break;
157  case View::YZ:
158  return {Axis::Y, Axis::Z};
159  break;
160  case View::XY:
161  return {Axis::X, Axis::Y};
162  break;
163  default:
164  throw Exception("RefFrame::AxesOf: Invalid view");
165  break;
166  }
167 }
168 
169 /* ******** Conversion functions ******** */
170 
180 constexpr Coo ToCoo(Axis axis) { return static_cast<Coo>(static_cast<int>(axis)); }
181 
189 constexpr Axis ToAxis(Coo coo) { return static_cast<Axis>(static_cast<int>(coo)); }
190 
197 constexpr Axis ToAxis(Direction dir) {
198  switch (dir) {
199  case Direction::Xneg:
200  case Direction::Xpos:
201  return Axis::X;
202  case Direction::Yneg:
203  case Direction::Ypos:
204  return Axis::Y;
205  case Direction::Zneg:
206  case Direction::Zpos:
207  return Axis::Z;
208  case Direction::NONE:
209  return Axis::NONE;
210  default:
211  throw std::runtime_error("RefFrame::ToAxis: unknown direction");
212  }
213 }
214 
225 constexpr Coo ToCoo(Direction direction) { return static_cast<Coo>(static_cast<int>(RefFrame::ToAxis(direction))); }
226 
227 /* ******** Utility functions ******** */
228 
235 constexpr Axis NormalAxis(Side side) {
236  switch (side) {
237  case Side::TOP:
238  return Axis::Z;
239  case Side::Xpos:
240  case Side::Xneg:
241  return Axis::X;
242  case Side::Ypos:
243  case Side::Yneg:
244  return Axis::Y;
245  case Side::NONE:
246  return Axis::NONE;
247  default:
248  throw std::runtime_error("RefFrame::NormalAxis: unknown side");
249  }
250 }
251 
258 constexpr Axis NormalAxis(View view) {
259  switch (view) {
260  case View::XZ:
261  return Axis::Y;
262  case View::YZ:
263  return Axis::X;
264  case View::XY:
265  return Axis::Z;
266  case View::NONE:
267  return Axis::NONE;
268  default:
269  throw std::runtime_error("RefFrame::NormalAxis: unknown side");
270  }
271 }
272 
279 constexpr Direction NormalDirection(Side side) {
280  switch (side) {
281  case Side::TOP:
282  return Direction::Zpos;
283  case Side::Xpos:
284  return Direction::Xpos;
285  case Side::Xneg:
286  return Direction::Xneg;
287  case Side::Ypos:
288  return Direction::Ypos;
289  case Side::Yneg:
290  return Direction::Yneg;
291  case Side::NONE:
292  return Direction::NONE;
293  default:
294  throw std::runtime_error("RefFrame::NormalAxis: unknown side");
295  }
296 }
297 
306 constexpr 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 constexpr 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 constexpr 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 Herd::RefFrame
363 
364 #endif /* HERD_REFFRAME_H_ */
Herd::RefFrame::View::XY
@ XY
Herd::RefFrame::Coo::Y
@ Y
Herd::RefFrame::Direction::Yneg
@ Yneg
Herd::RefFrame::IsPositive
constexpr bool IsPositive(Direction dir)
Tells if a direction is positive or not.
Definition: RefFrame.h:354
Herd::RefFrame::SideName
const std::array< std::string, NSides > SideName
Definition: RefFrame.h:122
Herd::RefFrame::Coo::X
@ X
Herd::RefFrame::DirectionName
const std::array< std::string, RefFrame::NDirections > DirectionName
Definition: RefFrame.h:78
Herd::RefFrame::ViewAxes::ordinate
RefFrame::Axis ordinate
Definition: RefFrame.h:149
Herd::RefFrame::Direction::Zneg
@ Zneg
Herd::RefFrame::Direction::XposYpos
@ XposYpos
Herd::RefFrame::Directions
constexpr std::array< RefFrame::Direction, RefFrame::NDirections > Directions
Definition: RefFrame.h:72
Herd::RefFrame::Direction::Xneg
@ Xneg
Herd::RefFrame::Coo::NONE
@ NONE
Herd::RefFrame::CooName
const std::array< std::string, 3 > CooName
Definition: RefFrame.h:26
Herd::RefFrame::AxisName
const std::array< std::string, NAxes > AxisName
Definition: RefFrame.h:45
Herd::RefFrame::Sides
constexpr std::array< Side, NSides > Sides
Array of all the sides.
Definition: RefFrame.h:121
Herd::RefFrame::Axis::X
@ X
Herd::RefFrame::NormalDirection
constexpr Direction NormalDirection(Side side)
Definition: RefFrame.h:279
Herd::RefFrame::Axes
constexpr std::array< Axis, NAxes > Axes
Array of all the axes.
Definition: RefFrame.h:44
Herd::RefFrame::Axis::NONE
@ NONE
Herd::RefFrame::Direction::XnegYpos
@ XnegYpos
Herd::RefFrame::Side::Ypos
@ Ypos
Herd::RefFrame::Direction
Direction
Aliases for the six axis directions.
Definition: RefFrame.h:55
Herd::RefFrame::View::XZ
@ XZ
Herd::RefFrame::Side
Side
Aliases for the five sides.
Definition: RefFrame.h:111
Herd::RefFrame::NAxes
constexpr int NAxes
The number of axes (i.e. (number of elements of Axis) - 1).
Definition: RefFrame.h:36
Herd::RefFrame::NSides
constexpr int NSides
The number of sides (i.e. (number of elements of Side) - 1).
Definition: RefFrame.h:113
Herd::RefFrame::View::NONE
@ NONE
Herd::RefFrame::Direction::Zpos
@ Zpos
Herd::RefFrame::View
View
Aliases for the three 2D projection planes.
Definition: RefFrame.h:91
Exception.h
Herd::RefFrame::Direction::Xpos
@ Xpos
Herd::RefFrame::Axis::Y
@ Y
Herd::RefFrame::operator<<
std::ostream & operator<<(std::ostream &out, const Coo &coo)
Definition: RefFrame.h:27
Herd::RefFrame::Side::Yneg
@ Yneg
Herd::RefFrame::View::YZ
@ YZ
Herd::RefFrame::ToCoo
constexpr Coo ToCoo(Axis axis)
Associates a coordinate to an axis.
Definition: RefFrame.h:180
Herd::RefFrame
Definition: RefFrame.h:19
Herd::RefFrame::Direction::Ypos
@ Ypos
Herd::RefFrame::ViewName
const std::array< std::string, NViews > ViewName
Definition: RefFrame.h:102
Herd::RefFrame::operator+
std::string operator+(const std::string &str, const Coo &coo)
Definition: RefFrame.h:28
Herd::RefFrame::Axis::Z
@ Z
Herd::RefFrame::Direction::XnegYneg
@ XnegYneg
Herd::RefFrame::Coo::Z
@ Z
Herd::RefFrame::Views
constexpr std::array< View, NViews > Views
Array of all the views.
Definition: RefFrame.h:101
Herd::RefFrame::Side::TOP
@ TOP
Herd::RefFrame::NormalAxis
constexpr Axis NormalAxis(Side side)
Definition: RefFrame.h:235
Herd::RefFrame::ViewAxes
Base struct to get the two axes of a given view.
Definition: RefFrame.h:147
Herd::RefFrame::Side::Xpos
@ Xpos
Herd::Exception
Definition: Exception.h:24
Herd::RefFrame::Ortog
Base struct to get the two axes orthogonal to a direction.
Definition: RefFrame.h:129
Herd::RefFrame::Axis
Axis
Aliases for the axes.
Definition: RefFrame.h:34
Herd::RefFrame::ToAxis
constexpr Axis ToAxis(Coo coo)
Associates an axis to a coordinate .
Definition: RefFrame.h:189
Herd::RefFrame::Direction::NONE
@ NONE
Herd::RefFrame::Direction::XposYneg
@ XposYneg
Herd::RefFrame::Coo
Coo
Aliases for the three space coordinates.
Definition: RefFrame.h:24
Herd::RefFrame::ViewAxes::abscissa
RefFrame::Axis abscissa
Definition: RefFrame.h:148
Herd::RefFrame::NDirections
constexpr int NDirections
The number of directions (i.e. (number of elements of Direction) - 1).
Definition: RefFrame.h:70
Herd::RefFrame::Side::Xneg
@ Xneg
Herd::RefFrame::NormalAxes
constexpr std::pair< Axis, Axis > NormalAxes(Axis ax)
Given one axes get the other two ;)
Definition: RefFrame.h:331
Herd::RefFrame::Coos
constexpr std::array< Coo, 3 > Coos
Definition: RefFrame.h:25
Herd::RefFrame::Side::NONE
@ NONE
Herd::RefFrame::AxesOf
constexpr RefFrame::ViewAxes AxesOf(RefFrame::View view)
Definition: RefFrame.h:152
Herd::RefFrame::NViews
constexpr int NViews
The number of views (i.e. (number of elements of View) - 1).
Definition: RefFrame.h:93