36 #include <boost/assert.hpp>
38 #include <util/Stride.h>
47 static const auto ROOM_CALIBRATION_SKIP_BRIGHTS_CUTOFF = 4;
48 static const auto CALIBRATION_RANSAC_ITERATIONS = 8;
53 TrackingSystem::TrackingSystem(ConfigParams
const ¶ms)
54 : m_params(params), m_impl(new Impl(params)) {}
56 TrackingSystem::~TrackingSystem() {}
58 TrackedBody *TrackingSystem::createTrackedBody() {
59 auto newId = BodyId(static_cast<BodyId::wrapped_type>(m_bodies.size()));
60 BodyPtr newBody(
new TrackedBody(*
this, newId));
61 m_bodies.emplace_back(std::move(newBody));
62 return m_bodies.back().get();
65 TrackedBodyTarget *TrackingSystem::getTarget(BodyTargetId target) {
66 return getBody(target.first).getTarget(target.second);
69 TrackedBodyTarget
const *
70 TrackingSystem::getTarget(BodyTargetId target)
const {
71 return getBody(target.first).getTarget(target.second);
74 ImageOutputDataPtr TrackingSystem::performInitialImageProcessing(
76 cv::Mat
const &frameGray, CameraParameters
const &camParams) {
78 ImageOutputDataPtr ret(
new ImageProcessingOutput);
81 ret->frameGray = frameGray;
82 ret->camParams = camParams.createUndistortedVariant();
83 auto rawMeasurements =
84 m_impl->blobExtractor->extractBlobs(ret->frameGray);
85 ret->ledMeasurements = undistortLeds(rawMeasurements, camParams);
89 LedUpdateCount
const &
93 auto &updateCount = m_impl->updateCount;
98 m_impl->frame = imageData->frame;
99 m_impl->frameGray = imageData->frameGray;
100 m_impl->camParams = imageData->camParams;
101 m_impl->lastFrame = imageData->tv;
105 auto usedMeasurements =
107 if (usedMeasurements != 0) {
120 updatePoseEstimates();
123 m_impl->triggerDebugDisplay(*
this);
130 BOOST_ASSERT_MSG(targetPtr !=
nullptr,
"We should never be "
131 "retrieving a nullptr for a "
132 "target with measurements!");
134 throw std::logic_error(
"Logical impossibility: Couldn't "
135 "retrieve a valid pointer for a target "
136 "that we were just told updated its "
137 "LEDs from data this frame.");
140 void TrackingSystem::updatePoseEstimates() {
143 calibrationVideoPhaseThree();
147 auto const &updateCount = m_impl->updateCount;
148 for (
auto &bodyTargetWithMeasurements : updateCount) {
149 auto targetPtr = getTarget(bodyTargetWithMeasurements.first);
150 validateTargetPointerFromUpdateList(targetPtr);
151 auto &target = *targetPtr;
154 auto &body = target.getBody();
157 auto newTime = m_impl->lastFrame;
159 body.getStateAtOrBefore(newTime, stateTime, state);
160 auto initialTime = stateTime;
162 auto gotPose = target.updatePoseEstimateFromLeds(
163 m_impl->camParams, newTime, state, stateTime, validState);
165 body.replaceStateSnapshot(initialTime, newTime, state);
167 static auto s = ::util::Stride{101};
169 std::cout << body.getState().position().transpose() <<
"\n";
172 m_updated.push_back(body.getId());
177 for (
auto &body : m_bodies) {
181 body->pruneHistory(m_impl->lastFrame);
185 void TrackingSystem::calibrationVideoPhaseThree() {
186 auto const &updateCount = m_impl->updateCount;
187 for (
auto &bodyTargetWithMeasurements : updateCount) {
188 auto &bodyTargetId = bodyTargetWithMeasurements.first;
189 if (!m_impl->calib.wantVideoData(*
this, bodyTargetId)) {
194 auto targetPtr = getTarget(bodyTargetId);
195 validateTargetPointerFromUpdateList(targetPtr);
196 auto &target = *targetPtr;
197 Eigen::Vector3d xlate;
198 Eigen::Quaterniond quat;
200 m_impl->camParams, xlate, quat,
201 ROOM_CALIBRATION_SKIP_BRIGHTS_CUTOFF, CALIBRATION_RANSAC_ITERATIONS);
203 m_impl->calib.processVideoData(*
this, bodyTargetId,
204 m_impl->lastFrame, xlate, quat);
208 m_impl->calib.postCalibrationUpdate(*
this);
214 Eigen::Quaterniond
const &quat) {
215 m_impl->calib.processIMUData(*
this,
id, tv, quat);
216 m_impl->calib.postCalibrationUpdate(*
this);
219 void TrackingSystem::setCameraPose(Eigen::Isometry3d
const &camPose) {
220 m_impl->haveCameraPose =
true;
221 m_impl->cameraPose = camPose;
222 m_impl->cameraPoseInv = camPose.inverse();
225 bool TrackingSystem::haveCameraPose()
const {
226 return m_impl->haveCameraPose;
230 return m_impl->cameraPose;
234 return m_impl->cameraPoseInv;
242 if (m_impl->roomCalibCompleteCached) {
246 m_impl->roomCalibCompleteCached =
247 vbtracker::isRoomCalibrationComplete(*
this);
248 return m_impl->roomCalibCompleteCached;
bool uncalibratedRANSACPoseEstimateFromLeds(CameraParameters const &camParams, Eigen::Vector3d &xlate, Eigen::Quaterniond &quat, int skipBrightsCutoff=-1, std::size_t iterations=5)
std::size_t processLedMeasurements(LedMeasurementVec const &undistortedLeds)
::OSVR_TimeValue TimeValue
C++-friendly typedef for the OSVR_TimeValue structure.
bool isRoomCalibrationComplete()
LedUpdateCount const & updateLedsFromVideoData(ImageOutputDataPtr &&imageData)
BodyIndices const & updateBodiesFromVideoData(ImageOutputDataPtr &&imageData)
BodyTargetId getQualifiedId() const
Get a fully-qualified (within a tracking system) id for this target.
Eigen::Isometry3d const & getRoomToCamera() const
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
void calibrationHandleIMUData(BodyId id, util::time::TimeValue const &tv, Eigen::Quaterniond const &quat)
Eigen::Isometry3d const & getCameraPose() const
This gets rTc - the pose of the camera in the room.