25 #ifndef INCLUDED_ImagePointMeasurement_h_GUID_BE292A08_8C31_4987_E179_CD2F0CE63183 26 #define INCLUDED_ImagePointMeasurement_h_GUID_BE292A08_8C31_4987_E179_CD2F0CE63183 43 using AugmentedStateWithBeacon =
44 kalman::AugmentedState<kalman::pose_externalized_rotation::State,
45 kalman::PureVectorState<3>>;
47 Eigen::Vector2d principalPoint;
49 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
62 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
64 Eigen::Vector3d
const &targetFromBody)
65 : m_variance(SquareMatrix::Identity()), m_cam(cam),
66 m_targetFromBody(targetFromBody) {}
71 m_beacon = m_targetFromBody + state.
b().stateVector();
72 m_rot = state.a().getCombinedQuaternion().toRotationMatrix();
73 m_objExtRot = state.a().getQuaternion() * m_beacon;
74 m_incRot = state.a().incrementalOrientation();
75 m_rotatedObjPoint = m_rot * m_beacon;
76 m_rotatedTranslatedPoint = m_rotatedObjPoint + state.a().position();
77 m_xlate = state.a().position();
80 Eigen::Vector3d
const &getBeaconInCameraSpace()
const {
81 return m_rotatedTranslatedPoint;
86 Eigen::Vector2d predicted =
88 m_rotatedTranslatedPoint);
89 return m_measurement - predicted;
92 void setMeasurement(
Vector const &m) { m_measurement = m; }
94 auto v1 = m_rot(0, 2) * m_beacon[2] + m_rot(0, 1) * m_beacon[1] +
95 m_beacon[0] * m_rot(0, 0) + m_xlate[0];
96 auto v2 = m_beacon[2] * m_rot(2, 2) + m_beacon[1] * m_rot(2, 1) +
97 m_beacon[0] * m_rot(2, 0) + m_xlate[2];
98 auto v3 = 1 / (v2 * v2);
100 auto v5 = m_rot(1, 2) * m_beacon[2] + m_beacon[1] * m_rot(1, 1) +
101 m_beacon[0] * m_rot(1, 0) + m_xlate[1];
103 ret << m_rot(0, 0) * v4 * m_cam.focalLength -
104 v1 * m_rot(2, 0) * v3 * m_cam.focalLength,
105 m_rot(0, 1) * v4 * m_cam.focalLength -
106 v1 * m_rot(2, 1) * v3 * m_cam.focalLength,
107 m_rot(0, 2) * v4 * m_cam.focalLength -
108 v1 * m_rot(2, 2) * v3 * m_cam.focalLength,
109 m_rot(1, 0) * v4 * m_cam.focalLength -
110 v5 * m_rot(2, 0) * v3 * m_cam.focalLength,
111 m_rot(1, 1) * v4 * m_cam.focalLength -
112 v5 * m_rot(2, 1) * v3 * m_cam.focalLength,
113 m_rot(1, 2) * v4 * m_cam.focalLength -
114 v5 * m_rot(2, 2) * v3 * m_cam.focalLength;
120 auto fl = m_cam.focalLength;
121 auto tmp0 = fl / (m_rotatedTranslatedPoint.z() *
122 m_rotatedTranslatedPoint.z());
123 auto tmp1 = 1.0 / m_rotatedTranslatedPoint.z();
124 auto tmp2 = fl * tmp1;
126 ret << -m_rotatedObjPoint.y() * tmp0 * m_rotatedTranslatedPoint.x(),
127 tmp2 * (m_rotatedObjPoint.x() * tmp1 *
128 m_rotatedTranslatedPoint.x() +
129 m_rotatedObjPoint.z()),
130 -m_rotatedObjPoint.y() * tmp2,
131 -tmp2 * (m_rotatedObjPoint.y() * tmp1 *
132 m_rotatedTranslatedPoint.y() +
133 m_rotatedObjPoint.z()),
134 m_rotatedObjPoint.x() * tmp0 * m_rotatedTranslatedPoint.y(),
135 m_rotatedObjPoint.x() * tmp2;
144 Eigen::Array2d rotXlated =
145 m_rotatedTranslatedPoint.head<2>().array();
146 Eigen::Array2d rotObj = m_rotatedObjPoint.head<2>().array();
150 Eigen::Array2d negativePositive(-1, 1);
151 Eigen::Array2d positiveNegative(1, -1);
153 double zRecip = 1. / m_rotatedTranslatedPoint.z();
154 double rotObjZ = m_rotatedObjPoint.z();
155 Eigen::Array2d mainDiagonal =
156 rotXlated * rotObj.reverse() * negativePositive * zRecip;
157 Eigen::Array2d otherDiagonal =
158 (rotObj * rotXlated * zRecip + rotObjZ) * positiveNegative;
159 Eigen::Array2d lastCol = rotObj.reverse() * negativePositive;
161 prelim.leftCols<2>() << mainDiagonal[0], otherDiagonal[0],
162 otherDiagonal[1], mainDiagonal[1];
163 prelim.rightCols<1>() << lastCol.matrix();
164 return prelim * zRecip * m_cam.focalLength;
173 Eigen::Matrix3d incrotOP;
175 m_incRot * m_incRot.transpose();
177 auto v1 = 0.5 - incrotOP(1, 2) / 24;
178 auto v2 = 2 * m_incRot[0] * v1;
179 auto v3 = -incrotOP(1, 2) / 6;
180 auto v4 = incrotOP(1, 2) / 6;
181 auto v5 = -(incrotOP(1, 1) * m_incRot[2]) / 24;
183 m_objExtRot[0] * (v5 + v4) + m_objExtRot[1] * (v3 + v2 + 1);
184 auto v7 = -incrotOP(0, 2) / 6;
186 auto v9 = 0.5 - incrotOP(0, 2) / 24;
187 auto v10 = incrotOP(1, 1) * v9;
189 auto v12 = incrotOP(0, 0) * v1;
190 auto v13 = m_objExtRot[0] * (v10 - m_incRot[1] * v8) +
191 m_objExtRot[1] * (v12 + m_incRot[0] * v11) +
192 m_objExtRot[2] + m_xlate[2];
193 auto v14 = 1 / (v13 * v13);
194 auto v15 = -incrotOP(0, 1) / 6;
196 auto v17 = 0.5 - incrotOP(0, 1) / 24;
197 auto v18 = incrotOP(2, 2) * v17;
198 auto v19 = m_objExtRot[2] * (m_incRot[1] * v8 + v10) +
199 m_objExtRot[1] * (v18 - m_incRot[2] * v16) +
200 m_objExtRot[0] + m_xlate[0];
202 auto v21 = -(m_incRot[1] * incrotOP(2, 2)) / 24;
203 auto v22 = 2 * m_incRot[1] * v9;
204 auto v23 = incrotOP(0, 2) / 6;
205 auto v24 = -(m_incRot[0] * incrotOP(2, 2)) / 24;
206 auto v25 = -(incrotOP(0, 0) * m_incRot[2]) / 24;
208 m_objExtRot[1] * (v7 + v25) + m_objExtRot[0] * (v23 + v22 - 1);
209 auto v27 = incrotOP(0, 1) / 6;
210 auto v28 = 2 * m_incRot[2] * v17;
211 auto v29 = -(m_incRot[0] * incrotOP(1, 1)) / 24;
212 auto v30 = -(incrotOP(0, 0) * m_incRot[1]) / 24;
214 m_objExtRot[1] * (v30 + v15) + m_objExtRot[0] * (v29 + v27);
215 auto v32 = m_objExtRot[0] * (v18 + m_incRot[2] * v16) +
216 m_objExtRot[2] * (v12 - m_incRot[0] * v11) +
217 m_objExtRot[1] + m_xlate[1];
219 ret << v20 * (m_objExtRot[1] * (v21 + v4) +
220 (v5 + v3) * m_objExtRot[2]) *
222 v6 * v14 * v19 * m_cam.focalLength,
223 v20 * (m_objExtRot[1] * (v24 + v23) +
224 (v22 + v7 + 1) * m_objExtRot[2]) *
226 v26 * v14 * v19 * m_cam.focalLength,
227 v20 * ((v29 + v15) * m_objExtRot[2] +
228 m_objExtRot[1] * (v28 + v27 - 1)) *
230 v31 * v14 * v19 * m_cam.focalLength,
231 v20 * (m_objExtRot[0] * (v21 + v3) +
232 (v4 + v2 - 1) * m_objExtRot[2]) *
234 v6 * v14 * v32 * m_cam.focalLength,
235 v20 * (m_objExtRot[0] * (v24 + v7) +
236 (v25 + v23) * m_objExtRot[2]) *
238 v26 * v14 * v32 * m_cam.focalLength,
239 v20 * ((v30 + v27) * m_objExtRot[2] +
240 m_objExtRot[0] * (v28 + v15 + 1)) *
242 v31 * v14 * v32 * m_cam.focalLength;
249 return getRotationJacobianNoIncrot();
256 Eigen::Matrix2d::Identity() *
258 std::abs(m_rotatedTranslatedPoint.z())),
260 -m_rotatedTranslatedPoint.head<2>() * m_cam.focalLength /
261 (m_rotatedTranslatedPoint.z() *
262 m_rotatedTranslatedPoint.z()),
264 getRotationJacobian(),
272 void setVariance(
double s) {
275 static const auto VARIANCE_Y_FACTOR = 3.;
276 m_variance << s, 0, 0, (s / VARIANCE_Y_FACTOR);
278 m_variance = SquareMatrix::Identity() * s;
291 Eigen::Vector3d m_targetFromBody;
292 Eigen::Vector3d m_beacon;
293 Eigen::Vector3d m_objExtRot;
294 Eigen::Vector3d m_incRot;
295 Eigen::Vector3d m_rotatedObjPoint;
296 Eigen::Vector3d m_rotatedTranslatedPoint;
297 Eigen::Vector3d m_xlate;
298 Eigen::Matrix3d m_rot;
303 #endif // INCLUDED_ImagePointMeasurement_h_GUID_BE292A08_8C31_4987_E179_CD2F0CE63183
void updateFromState(State const &state)
Updates some internal cached partial solutions.
Definition: ImagePointMeasurement.h:69
typename detail::Dimension_impl< T >::type Dimension
Given a state or measurement, get the dimension as a std::integral_constant.
Definition: FlexibleKalmanBase.h:87
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Eigen::Matrix< double, 2, 3 > getRotationJacobianNoIncrot() const
This version assumes incrot == 0.
Definition: ImagePointMeasurement.h:119
Eigen::Vector2d projectPoint(double focalLength, Eigen::Vector2d const &principalPoint, Eigen::Vector3d const &camPoint)
Project point for a simple pinhole camera model, with focal lengths equal on both axes and no distort...
Definition: ProjectPoint.h:44
View matrix as an upper triangular matrix.
Definition: Constants.h:169
Measurement class for auto-calibrating Kalman filter in video-based tracker.
Definition: ImagePointMeasurement.h:53
Eigen::Matrix< double, 2, 3 > getRotationJacobianNoIncrotElegant() const
This version also assumes incrot == 0 but does the computation in a more elegant (manually factored) ...
Definition: ImagePointMeasurement.h:141
Definition: ImagePointMeasurement.h:46
State type that consists entirely of references to two independent sub-states.
Definition: AugmentedState.h:41
StateTypeB & b()
Access the second part of the state.
Definition: AugmentedState.h:116
SquareMatrix const & getCovariance(State &state) const
Definition: ImagePointMeasurement.h:282
std::size_t DimensionType
Type for dimensions.
Definition: FlexibleKalmanBase.h:51
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127