OSVR-Core
UtilityFunctions.h
Go to the documentation of this file.
1 
11 // Copyright 2016 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_UtilityFunctions_h_GUID_18928B12_FA9D_4285_102C_3106E5EEE14C
26 #define INCLUDED_UtilityFunctions_h_GUID_18928B12_FA9D_4285_102C_3106E5EEE14C
27 
28 // Internal Includes
29 #include <osvr/Util/TimeValue.h>
30 
31 #include <LedMeasurement.h>
32 
33 // Library/third-party includes
36 
37 // Standard includes
38 #include <array>
39 #include <cstddef>
40 #include <iostream>
41 #include <memory>
42 #include <vector>
43 
44 namespace osvr {
45 namespace vbtracker {
46 
49  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
50  TimeValue tv;
51  Eigen::Vector3d xlate;
53  LedMeasurementVec measurements;
54  bool ok = false;
55  };
56  using TimestampedMeasurementsPtr = std::unique_ptr<TimestampedMeasurements>;
57  using MeasurementsRows = std::vector<TimestampedMeasurementsPtr>;
58 
59  template <std::size_t N> using Vec = Eigen::Matrix<double, N, 1>;
61  inline Eigen::IOFormat const &getFullFormat() {
62  static const Eigen::IOFormat FullFormat =
63  Eigen::IOFormat(Eigen::FullPrecision, 0, ",", ",\n");
64  return FullFormat;
65  }
66 
67  inline double getReallyBigCost() { return 1000000.; }
68 
69  template <typename VecType>
70  inline Eigen::Quaterniond rot_exp(VecType const &v) {
71  Eigen::Vector3d vec = v;
72  return util::quat_exp_map(vec).exp();
73  }
74 
75  inline Eigen::Isometry3d makeIsometry(Eigen::Vector3d const &xlate,
76  Eigen::Quaterniond const &quat) {
78  Eigen::Isometry3d(quat);
79  }
80 
81  inline Eigen::Isometry3d makeIsometry(Eigen::Translation3d const &xlate,
82  Eigen::Quaterniond const &quat) {
83  return Eigen::Isometry3d(xlate) * Eigen::Isometry3d(quat);
84  }
85 
86  inline Eigen::Isometry3d
87  makeIsometry(Eigen::Ref<Eigen::Vector3d const> const &xlate) {
89  }
90 
91  namespace reftracker {
92  using TransformParams = Vec<12>;
97  static const TransformParams data =
98  (TransformParams() << -1.328683782476265, 1.405860857175348,
99  2.382413079481474, -0.08838304984947577, -0.3418371322988537,
100  0.08973247977775475, 0.04221099149748584, -0.289480834047175,
101  -0.1169425186552738, -0.03623604095935174, -0.0460904237026321,
102  0.001219163540875095)
103  .finished();
104  return data;
105  }
107  enum {
108  BaseXlate = 0,
109  BaseRot = BaseXlate + 3,
110  InnerXlate = BaseRot + 3,
111  InnerRot = InnerXlate + 3
112  };
113 
114  inline Eigen::Translation3d
115  getBaseTranslation(TransformParams const &vec) {
116  return Eigen::Translation3d(vec.head<3>());
117  }
118  inline Eigen::Quaterniond getBaseRotation(TransformParams const &vec) {
119  return rot_exp(vec.segment<3>(BaseRot));
120  }
121  inline Eigen::Isometry3d getBaseTransform(TransformParams const &vec) {
122  return makeIsometry(getBaseTranslation(vec), getBaseRotation(vec));
123  }
124  inline Eigen::Translation3d
125  getInnerTranslation(TransformParams const &vec) {
126  return Eigen::Translation3d(vec.segment<3>(InnerXlate));
127  }
128  inline Eigen::Quaterniond getInnerRotation(TransformParams const &vec) {
129  return rot_exp(vec.segment<3>(InnerRot));
130  }
131  inline Eigen::Isometry3d getInnerTransform(TransformParams const &vec) {
132  return makeIsometry(getInnerTranslation(vec),
133  getInnerRotation(vec));
134  }
135  } // namespace reftracker
136 
137  double costMeasurement(Eigen::Isometry3d const &refPose,
138  Eigen::Isometry3d const &expPose) {
139  auto distanceAway = -1.;
142  using Point = std::array<double, 3>;
143  auto corners = {Point{.2, -.2, distanceAway},
144  Point{-.2, -.2, distanceAway},
145  Point{0, .4, distanceAway}};
146  double accum = 0;
147  for (auto &corner : corners) {
148  Eigen::Vector3d pt = Eigen::Vector3d::Map(corner.data());
149  accum += ((refPose * pt) - (expPose * pt)).norm();
150  }
151  return accum / 3.;
152  }
153 
154  struct TrackedData {
155  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
156  Eigen::Isometry3d videoPose;
157  Eigen::Isometry3d refPose;
158  };
159  using TrackedDataPtr = std::unique_ptr<TrackedData>;
160  using TrackedSamples = std::vector<TrackedDataPtr>;
161 
162  inline void outputTransformedSample(Eigen::Isometry3d const &baseXform,
163  Eigen::Isometry3d const &innerXform,
164  TrackedData const &sample) {
165  std::cout << "HDK to VideoBase: "
166  << (sample.videoPose).translation().transpose() << std::endl;
167  std::cout << "Ref to VideoBase: "
168  << (baseXform * sample.refPose * innerXform)
169  .translation()
170  .transpose()
171  << std::endl;
172  std::cout << "Vive to VideoBase: "
173  << (baseXform * sample.refPose).translation().transpose()
174  << std::endl;
175  std::cout << "---------" << std::endl;
176  }
177 
178 } // namespace vbtracker
179 } // namespace osvr
180 #endif // INCLUDED_UtilityFunctions_h_GUID_18928B12_FA9D_4285_102C_3106E5EEE14C
Definition: UtilityFunctions.h:48
Eigen::IOFormat const & getFullFormat()
Use for output of parameters.
Definition: UtilityFunctions.h:61
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Header wrapping include of <Eigen/Core> and <Eigen/Geometry> for warning quieting.
TransformParams const & getRefTrackerTransformParams()
where we can store results from a run of computeRefTrackerTransform() for use later/elsewhere.
Definition: UtilityFunctions.h:96
::OSVR_TimeValue TimeValue
C++-friendly typedef for the OSVR_TimeValue structure.
Definition: TimeValue.h:48
Definition: ForwardDeclarations.h:236
Definition: UtilityFunctions.h:154
A matrix or vector expression mapping an existing expressions.
Definition: Ref.h:17
Header providing a C++ wrapper around TimeValueC.h.
Definition: Quaternion.h:47
double costMeasurement(Eigen::Isometry3d const &refPose, Eigen::Isometry3d const &expPose)
Definition: UtilityFunctions.h:137
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
Stores a set of parameters controlling the way matrices are printed.
Definition: IO.h:50
Definition: Transform.h:43