OSVR-Core
BeaconBasedPoseEstimator.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_BeaconBasedPoseEstimator_h_GUID_7B983CED_F2C5_4B86_109A_948863B665B1
26 #define INCLUDED_BeaconBasedPoseEstimator_h_GUID_7B983CED_F2C5_4B86_109A_948863B665B1
27 
28 // Internal Includes
29 #include "Types.h"
30 #include "LED.h"
31 #include "CameraParameters.h"
32 
33 // Library/third-party includes
34 #include <osvr/Util/TimeValue.h>
37 #include <osvr/Kalman/PoseState.h>
39 
40 // Standard includes
41 #include <vector>
42 #include <list>
43 #include <memory>
44 #include <iosfwd>
45 
46 namespace osvr {
47 namespace vbtracker {
48 
49  struct BeaconData {
50  bool seen = false;
51  double size = 0;
52  cv::Point2d measurement = {0, 0};
53  cv::Point2d residual = {0, 0};
54  double variance = 0;
55  void reset() { *this = BeaconData{}; }
56  };
57 
64  public:
65  static BeaconIDPredicate getDefaultBeaconFixedPredicate() {
66  return [](int id) { return id <= 4; };
67  }
68 
81  size_t requiredInliers = 4,
82  size_t permittedOutliers = 2,
83  ConfigParams const &params = ConfigParams{});
84  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
93  bool EstimatePoseFromLeds(LedGroup &leds, OSVR_TimeValue const &tv,
94  OSVR_PoseState &out);
95 
96  std::size_t getNumBeacons() const { return m_beacons.size(); }
97 
101  bool ProjectBeaconsToImage(std::vector<cv::Point2f> &outPose);
102 
105  void permitKalmanMode(bool permitKalman);
106 
111  OSVR_PoseState GetState() const;
112  OSVR_PoseState GetPredictedState(double dt) const;
113  Eigen::Vector3d GetLinearVelocity() const;
114  Eigen::Vector3d GetAngularVelocity() const;
116 
120  bool SetBeacons(const Point3Vector &beacons,
121  Vec3Vector const &emissionDirection,
122  std::vector<double> const &variance,
123  BeaconIDPredicate const &autocalibrationFixedPredicate,
124  double beaconAutocalibErrorScale = 1);
125  bool SetCameraParameters(CameraParameters const &camParams);
127 
128  void dumpBeaconLocationsToStream(std::ostream &os) const;
129 
130  std::vector<BeaconData> const &getBeaconDebugData() const {
131  return m_beaconDebugData;
132  }
133 
134  Eigen::Vector3d getBeaconAutocalibPosition(std::size_t i) const;
135 
136  Eigen::Vector3d getBeaconAutocalibVariance(std::size_t i) const;
137 
138  private:
139  void m_updateBeaconCentroid(const Point3Vector &beacons);
140  void m_updateBeaconDebugInfoArray();
143  Eigen::Vector3d m_convertInternalPositionRepToExternal(
144  Eigen::Vector3d const &pos) const;
145 
147  bool m_estimatePoseFromLeds(LedGroup &leds, OSVR_TimeValue const &tv,
148  OSVR_PoseState &out);
149 
152  bool m_pnpransacEstimator(LedGroup &leds);
153 
156  bool m_kalmanAutocalibEstimator(LedGroup &leds, double dt);
157 
164  bool m_forceRansacIfKalmanNeedsReset(LedGroup const &leds);
165 
168  void m_resetState(Eigen::Vector3d const &xlate,
169  Eigen::Quaterniond const &quat);
171  using BeaconStateVec = std::vector<std::unique_ptr<BeaconState>>;
172  BeaconStateVec m_beacons;
173  std::vector<double> m_beaconMeasurementVariance;
175  std::vector<bool> m_beaconFixed;
176  Vec3Vector m_beaconEmissionDirection;
177 
178  std::vector<BeaconData> m_beaconDebugData;
179 
180  CameraParameters m_camParams;
181  size_t m_requiredInliers; //< How many inliers do we require?
182  size_t m_permittedOutliers; //< How many outliers do we allow?
183 
184  ConfigParams const m_params;
185 
189  Eigen::Vector3d m_centroid;
190 
192  OSVR_TimeValue m_prev;
194  bool m_gotPrev = false;
195 
196  bool m_permitKalman = true;
197 
203  bool m_gotPose = false;
205  bool m_gotMeasurement = false;
207  cv::Mat m_rvec;
209  cv::Mat m_tvec;
211  State m_state;
212  using ProcessModel =
214  ProcessModel m_model;
216 
220  std::size_t m_framesInProbation = 0;
223  std::size_t m_framesWithoutUtilizedMeasurements = 0;
224 
225  std::size_t m_framesWithoutIdentifiedBlobs = 0;
227  };
228 
229 } // namespace vbtracker
230 } // namespace osvr
231 
232 #endif // INCLUDED_BeaconBasedPoseEstimator_h_GUID_7B983CED_F2C5_4B86_109A_948863B665B1
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Definition: CameraParameters.h:41
Header file for class that tracks and identifies LEDs.
Header.
std::function< bool(int)> BeaconIDPredicate
Takes in a 1-based index, returns true or false (true if the beacon should be considered fixed - not ...
Definition: Types.h:81
General configuration parameters.
Definition: ConfigParams.h:82
Class to track an object that has identified LED beacons on it as seen in a camera, where the absolute location of the LEDs with respect to a common frame of reference is known.
Definition: BeaconBasedPoseEstimator.h:63
Header.
Header providing a C++ wrapper around TimeValueC.h.
Definition: Quaternion.h:47
A structure defining a 3D (6DOF) rigid body pose: translation and rotation.
Definition: Pose3C.h:54
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
A very simple (3D by default) vector state with no velocity, ideal for use as a position, with ConstantProcess for beacon autocalibration.
Definition: PureVectorState.h:40
A basically-constant-velocity model, with the addition of some damping of the velocities inspired by ...
Definition: PoseSeparatelyDampedConstantVelocity.h:43