OSVR-Core
MatrixConventions.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_MatrixConventions_h_GUID_82404AE2_0D13_46A4_4618_05AD5A20D7EA
26 #define INCLUDED_MatrixConventions_h_GUID_82404AE2_0D13_46A4_4618_05AD5A20D7EA
27 
28 // Internal Includes
30 
31 // Library/third-party includes
32 // - none
33 
34 // Standard includes
35 #include <bitset>
36 #include <type_traits>
37 
38 namespace osvr {
39 namespace util {
40  namespace detail {
41 
42  enum class CompactMatrixFlags {
45  NeedsTranspose = 0,
46  LeftHandInput = 1,
47  UnsignedZ = 2,
48 
49  SIZEPLUSONE
50  };
51 
52  inline bool matrixNeedsTranspose(OSVR_MatrixConventions flags) {
53  const bool rowmaj(0 != (flags & OSVR_MATRIX_MASK_ROWMAJOR));
54  const bool rowvec(0 != (flags & OSVR_MATRIX_MASK_ROWVECTORS));
55  // xor since one alone implies transpose, but both together
56  // imply no transpose.
57  return (rowmaj ^ rowvec);
58  }
60 
61  public:
63  : m_data() {
64  if (matrixNeedsTranspose(flags)) {
65  set(CompactMatrixFlags::NeedsTranspose);
66  }
67 
68  if (0 != (flags & OSVR_MATRIX_MASK_LHINPUT)) {
69  set(CompactMatrixFlags::LeftHandInput);
70  }
71 
72  if (0 != (flags & OSVR_MATRIX_MASK_UNSIGNEDZ)) {
73  set(CompactMatrixFlags::UnsignedZ);
74  }
75  }
76  template <typename... Args>
77  CompactMatrixConventions(Args... args)
78  : m_data() {
79  set(args...);
80  }
81 
82  bool operator[](CompactMatrixFlags flag) const {
83  return m_data[static_cast<std::size_t>(flag)];
84  }
85 
86  unsigned long getValue() const { return m_data.to_ulong(); }
87 
88  void set(CompactMatrixFlags flag) {
89  m_data.set(static_cast<std::size_t>(flag));
90  }
91 
92  template <typename... Args>
93  void set(CompactMatrixFlags flag, Args... args) {
94  set(flag);
95  set(args...);
96  }
97 
98  private:
99  void set() {}
100  static const std::size_t SIZE =
101  static_cast<std::size_t>(CompactMatrixFlags::SIZEPLUSONE) - 1;
102  std::bitset<SIZE> m_data;
103 
104  template <CompactMatrixFlags Flag>
105  using FlagToMask =
106  std::integral_constant<std::size_t,
107  (0x1 << static_cast<std::size_t>(Flag))>;
108 
109  // General template declaration
110  template <std::size_t State, CompactMatrixFlags... MyFlags>
111  struct ComputeBitsImpl;
112  // Base case
113  template <std::size_t State>
114  struct ComputeBitsImpl<State>
115  : std::integral_constant<std::size_t, State> {};
116  // Recursive case
117  template <std::size_t State, CompactMatrixFlags Flag,
118  CompactMatrixFlags... MyFlags>
119  struct ComputeBitsImpl<State, Flag, MyFlags...>
120  : ComputeBitsImpl<(State | FlagToMask<Flag>::value),
121  MyFlags...> {};
122 
123  public:
124  template <CompactMatrixFlags... MyFlags>
125  using ComputeBits = ComputeBitsImpl<0, MyFlags...>;
126  };
127 
128  } // namespace detail
129 } // namespace util
130 } // namespace osvr
131 #endif // INCLUDED_MatrixConventions_h_GUID_82404AE2_0D13_46A4_4618_05AD5A20D7EA
Definition: RunLoopManager.h:42
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
uint16_t OSVR_MatrixConventions
Type for passing matrix convention flags.
Definition: MatrixConventionsC.h:54
Definition: newuoa.h:1888
Definition: MatrixConventions.h:59
A class that lightly wraps a bool, in order to provide easier maintenance of a "dirty" flag...
Definition: Flag.h:43