OSVR-Core
OptimizationBase.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_OptimizationBase_h_GUID_C3489E14_63D4_4290_45C0_225AC84A8BF5
26 #define INCLUDED_OptimizationBase_h_GUID_C3489E14_63D4_4290_45C0_225AC84A8BF5
27 
28 // Internal Includes
29 #include "UtilityFunctions.h"
30 
31 #include <ConfigParams.h>
32 #include <MakeHDKTrackingSystem.h>
33 #include <TrackedBodyTarget.h>
34 
35 // Library/third-party includes
37 #include <osvr/Util/EigenFilters.h>
38 
39 // Standard includes
40 #include <memory>
41 #include <utility>
42 
43 namespace osvr {
44 namespace vbtracker {
45 
47  struct OptimCommonData {
48  CameraParameters const &camParams;
49  ConfigParams const &initialParams;
50  };
57  class OptimData {
58  public:
59  OptimData(OptimData const &) = delete;
60  OptimData(OptimData &&) = default;
61  OptimData &operator=(OptimData const &) = delete;
62 
63  static OptimData make(ConfigParams const &params,
64  OptimCommonData const &commonData) {
65 
66  auto system = makeHDKTrackingSystem(params);
67  auto &body = system->getBody(BodyId(0));
68  auto &target = *(body.getTarget(TargetId(0)));
69  return OptimData(std::move(system), body, target,
70  commonData.camParams);
71  }
72  static OptimData make(OptimCommonData const &commonData) {
73  return OptimData::make(commonData.initialParams, commonData);
74  }
75 
76  TrackingSystem &getSystem() { return *system_; }
77  TrackedBody &getBody() { return *body_; }
78  TrackedBodyTarget &getTarget() { return *target_; }
79  TrackedBodyTarget const &getTarget() const { return *target_; }
80  CameraParameters const &getCamParams() { return camParams_; }
81 
82  private:
83  OptimData(std::unique_ptr<TrackingSystem> &&system, TrackedBody &body,
84  TrackedBodyTarget &target, CameraParameters const &camParams)
85  : camParams_(camParams), system_(std::move(system)), body_(&body),
86  target_(&target) {}
87 
88  CameraParameters const &camParams_;
89  std::unique_ptr<TrackingSystem> system_;
90  TrackedBody *body_ = nullptr;
91  TrackedBodyTarget *target_ = nullptr;
92  };
93 
100  public:
101  void operator()(OptimData &optim, TimestampedMeasurements const &row) {
102  auto inputData =
103  makeImageOutputDataFromRow(row, optim.getCamParams());
104  optim.getSystem().updateLedsFromVideoData(std::move(inputData));
105  }
106  };
107 
116  public:
117  void operator()(OptimData &optim, TimestampedMeasurements const &row) {
118  auto inputData =
119  makeImageOutputDataFromRow(row, optim.getCamParams());
120  auto indices = optim.getSystem().updateBodiesFromVideoData(
121  std::move(inputData));
122  gotPose = optim.getBody().hasPoseEstimate();
123  if (gotPose) {
124  pose = optim.getBody().getState().getIsometry();
125  }
126  }
127  bool havePose() const { return gotPose; }
128  Eigen::Isometry3d const &getPose() const { return pose; }
129  std::size_t getNumResets(OptimData const &optim) {
130  return optim.getTarget().numTrackingResets();
131  }
132 
133  private:
134  bool gotPose = false;
135  Eigen::Isometry3d pose;
136  };
137 
143  public:
144  void operator()(OptimData &optim, TimestampedMeasurements const &row) {
145  gotPose = false;
146  flippedPose_ = false;
147  Eigen::Vector3d pos;
148  Eigen::Quaterniond quat;
149  auto gotRansac =
150  optim.getTarget().uncalibratedRANSACPoseEstimateFromLeds(
151  optim.getCamParams(), pos, quat);
152  if (gotRansac) {
155  double yAxisYComponent = (quat * Eigen::Vector3d::UnitY()).y();
156  if (yAxisYComponent < 0) {
157  // std::cout << "RANSAC picked upside-down!" << std::endl;
158  flippedPose_ = true;
159  numFlips_++;
160  return;
161  }
162  double dt = 1;
163  if (isFirst) {
164  isFirst = false;
165  } else {
166  dt = osvrTimeValueDurationSeconds(&row.tv, &last);
167  }
168  ransacPoseFilter.filter(dt, pos, quat);
169  last = row.tv;
170  gotPose = true;
171  pose = ransacPoseFilter.getIsometry();
172  }
173  }
174  bool flippedPose() const { return flippedPose_; }
175  std::size_t getNumFlips() const { return numFlips_; }
176  bool havePose() const { return gotPose; }
177  Eigen::Isometry3d const &getPose() const { return pose; }
178 
179  private:
180  util::filters::PoseOneEuroFilterd ransacPoseFilter;
181  TimeValue last;
182  bool isFirst = true;
183  bool gotPose = false;
184  bool flippedPose_ = false;
185  std::size_t numFlips_ = 0;
186  Eigen::Isometry3d pose;
187  };
188 
198  public:
200  : base_(reftracker::getBaseTransform(
201  reftracker::getRefTrackerTransformParams())),
202  inner_(reftracker::getInnerTransform(
203  reftracker::getRefTrackerTransformParams())) {}
204 
205  void operator()(OptimData &optim, TimestampedMeasurements const &row) {
206  // Update our pose by the reference pose transformed.
207  pose_ = base_ * makeIsometry(row.xlate, row.rot) * inner_;
208  }
209  bool havePose() const { return true; }
210  Eigen::Isometry3d const &getPose() const { return pose_; }
211 
212  private:
213  Eigen::Isometry3d base_;
214  Eigen::Isometry3d inner_;
215  Eigen::Isometry3d pose_;
216  };
217 } // namespace vbtracker
218 } // namespace osvr
219 #endif // INCLUDED_OptimizationBase_h_GUID_C3489E14_63D4_4290_45C0_225AC84A8BF5
LedUpdateCount const & updateLedsFromVideoData(ImageOutputDataPtr &&imageData)
This is the second phase of the video-based tracking algorithm - the part that actually changes LED s...
Definition: TrackingSystem.cpp:90
Definition: UtilityFunctions.h:48
Algorithm functor to be called in a loop that processes measurements rows: feeds in LEDs (stage 2) an...
Definition: OptimizationBase.h:115
bool uncalibratedRANSACPoseEstimateFromLeds(CameraParameters const &camParams, Eigen::Vector3d &xlate, Eigen::Quaterniond &quat, int skipBrightsCutoff=-1, std::size_t iterations=5)
Perform a simple RANSAC pose estimation from updated LEDs (third phase of tracking) without storing t...
Definition: TrackedBodyTarget.cpp:644
bool hasPoseEstimate() const
Do we have a pose estimate for this body in general?
Definition: TrackedBody.cpp:241
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
OSVR_UTIL_EXPORT void OSVR_UTIL_EXPORT void OSVR_UTIL_EXPORT int OSVR_EXTERN_C_END OSVR_INLINE double osvrTimeValueDurationSeconds(OSVR_IN_PTR const OSVR_TimeValue *tvA, OSVR_IN_PTR const OSVR_TimeValue *tvB)
Compute the difference between the two time values, returning the duration as a double-precision floa...
Definition: TimeValueC.h:185
Definition: CameraParameters.h:41
Header wrapping include of <Eigen/Core> and <Eigen/Geometry> for warning quieting.
Creates and owns the tracking system created for each optimization run.
Definition: OptimizationBase.h:57
std::unique_ptr< TrackingSystem > makeHDKTrackingSystem(ConfigParams const &params)
Definition: MakeHDKTrackingSystem.h:216
Algorithm functor, call after either stage-2 functor: obtains a RANSAC pose and feeds it into a 1-eur...
Definition: OptimizationBase.h:142
Eigen::Isometry3d getIsometry() const
Get the position and quaternion combined into a single isometry (transformation)
Definition: PoseState.h:246
Algorithm functor, to use a transformed recorded tracker pose, typically instead of an alternate meth...
Definition: OptimizationBase.h:197
::OSVR_TimeValue TimeValue
C++-friendly typedef for the OSVR_TimeValue structure.
Definition: TimeValue.h:48
void operator()(OptimData &optim, TimestampedMeasurements const &row)
Definition: OptimizationBase.h:144
Header defining some filters for Eigen datatypes.
Definition: TrackingSystem.h:54
BodyIndices const & updateBodiesFromVideoData(ImageOutputDataPtr &&imageData)
The combined second and third phases of the video-based tracking algorithm.
Definition: TrackingSystem.cpp:115
Corresponds to a rigid arrangements of discrete beacons detected by video-based tracking - typically ...
Definition: TrackedBodyTarget.h:77
General configuration parameters.
Definition: ConfigParams.h:82
Algorithm functor to be called in a loop that processes measurements rows: all it does is feed in LED...
Definition: OptimizationBase.h:99
This is the class representing a tracked rigid body in the system.
Definition: TrackedBody.h:56
Definition: Quaternion.h:47
Isometry3< typename Derived1::Scalar > makeIsometry(Eigen::MatrixBase< Derived1 > const &translation, Eigen::RotationBase< Derived2, 3 > const &rotation)
A simpler, functional-style alternative to .fromPositionOrientationScale when no scaling is performed...
Definition: EigenExtras.h:93
Input from main to the optimization routine (wrapper)
Definition: OptimizationBase.h:47
Combines a one-euro filter for position and a one-euro filter for orientation for the common use case...
Definition: EigenFilters.h:226
Definition: Transform.h:43
std::size_t numTrackingResets() const
Get the number of times tracking has reset - for debugging/optimization.
Definition: TrackedBodyTarget.cpp:761