Main MRPT website > C++ reference for MRPT 1.9.9
ba_internals.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #ifndef ba_internals_H
11 #define ba_internals_H
12 
15 #include <mrpt/poses/CPose3D.h>
17 #include <mrpt/vision/types.h>
18 
19 #include <array>
20 
21 // Declarations shared between ba_*.cpp files, but which are private to MRPT
22 // not to be seen by an MRPT API user.
23 
24 namespace mrpt
25 {
26 namespace vision
27 {
28 using mrpt::math::CArrayDouble; // Allow these "using"s since these headers are
29 // internal to mrpt
31 using std::vector;
32 
33 #define VERBOSE_COUT \
34  if (verbose) cout
35 
36 // Auxiliary struct for keeping the list of all Jacobians in a sparse, efficient
37 // way.
38 template <int FrameDof, int PointDof, int ObsDim>
39 struct JacData
40 {
41  inline JacData()
44  J_frame_valid(false),
45  J_point_valid(false)
46  {
47  }
48 
51 
52  // Jacobians of the observation wrt the camera pose & the point:
56 
57  MRPT_MAKE_ALIGNED_OPERATOR_NEW // Needed by any struct having
58  // Eigen::Matrix<> fields
59 };
60 
61 /** The projective camera 2x6 Jacobian \f$ \frac{\partial h(f,p)}{\partial p}
62  * \f$ (wrt the 6D camera pose)
63  * \note Jacobians as described in
64  * http://www.mrpt.org/6D_poses:equivalences_compositions_and_uncertainty
65  * (Appendix A)
66  */
67 template <bool POSES_ARE_INVERSE>
68 void frameJac(
69  const mrpt::utils::TCamera& camera_params,
70  const mrpt::poses::CPose3D& cam_pose,
71  const mrpt::math::TPoint3D& landmark_global,
73 {
74  using mrpt::math::square;
75 
76  double x, y, z; // wrt cam (local coords)
77  if (POSES_ARE_INVERSE)
78  cam_pose.composePoint(
79  landmark_global.x, landmark_global.y, landmark_global.z, x, y, z);
80  else
81  cam_pose.inverseComposePoint(
82  landmark_global.x, landmark_global.y, landmark_global.z, x, y, z);
83 
84  ASSERT_(z != 0)
85 
86  const double _z = 1.0 / z;
87  const double fx_z = camera_params.fx() * _z;
88  const double fy_z = camera_params.fy() * _z;
89  const double _z2 = square(_z);
90  const double fx_z2 = camera_params.fx() * _z2;
91  const double fy_z2 = camera_params.fy() * _z2;
92 
93  if (POSES_ARE_INVERSE)
94  {
95  const double xy = x * y;
96 
97  const double J_vals[] = {fx_z,
98  0,
99  -fx_z2 * x,
100  -fx_z2 * xy,
101  camera_params.fx() * (1 + square(x * _z)),
102  -fx_z * y,
103  0,
104  fy_z,
105  -fy_z2 * y,
106  -camera_params.fy() * (1 + square(y * _z)),
107  fy_z2 * xy,
108  fy_z * x};
109  out_J.loadFromArray(J_vals);
110  }
111  else
112  {
113  const mrpt::math::CMatrixDouble33& R = cam_pose.getRotationMatrix();
114 
115  const double jac_proj_vals[] = {fx_z, 0, -fx_z2 * x,
116  0, fy_z, -fy_z2 * y};
118  jac_proj_vals);
119 
120  const double p_x = cam_pose.x();
121  const double p_y = cam_pose.y();
122  const double p_z = cam_pose.z();
123  const double tx = landmark_global.x - p_x;
124  const double ty = landmark_global.y - p_y;
125  const double tz = landmark_global.z - p_z;
126 
127  const double aux_vals[] = {
128  -R(0, 0),
129  -R(1, 0),
130  -R(2, 0),
131  tz * R(1, 0) - ty * R(2, 0) + R(1, 0) * p_z - R(2, 0) * p_y,
132  tx * R(2, 0) - tz * R(0, 0) - R(0, 0) * p_z + R(2, 0) * p_x,
133  ty * R(0, 0) - tx * R(1, 0) + R(0, 0) * p_y - R(1, 0) * p_x,
134  -R(0, 1),
135  -R(1, 1),
136  -R(2, 1),
137  tz * R(1, 1) - ty * R(2, 1) + R(1, 1) * p_z - R(2, 1) * p_y,
138  tx * R(2, 1) - tz * R(0, 1) - R(0, 1) * p_z + R(2, 1) * p_x,
139  ty * R(0, 1) - tx * R(1, 1) + R(0, 1) * p_y - R(1, 1) * p_x,
140  -R(0, 2),
141  -R(1, 2),
142  -R(2, 2),
143  tz * R(1, 2) - ty * R(2, 2) + R(1, 2) * p_z - R(2, 2) * p_y,
144  tx * R(2, 2) - tz * R(0, 2) - R(0, 2) * p_z + R(2, 2) * p_x,
145  ty * R(0, 2) - tx * R(1, 2) + R(0, 2) * p_y - R(1, 2) * p_x};
147  out_J.multiply_AB(jac_proj, vals);
148  }
149 }
150 
151 /** Jacobians wrt the point
152  * \note Jacobians as described in
153  * http://www.mrpt.org/6D_poses:equivalences_compositions_and_uncertainty
154  * (Appendix A)
155 */
156 template <bool POSES_ARE_INVERSE>
157 void pointJac(
158  const mrpt::utils::TCamera& camera_params,
159  const mrpt::poses::CPose3D& cam_pose,
160  const mrpt::math::TPoint3D& landmark_global,
162 {
163  using namespace mrpt::math;
164 
165  mrpt::math::TPoint3D l; // Local point, wrt camera
166 
168 
169  if (POSES_ARE_INVERSE)
170  cam_pose.composePoint(
171  landmark_global.x, landmark_global.y, landmark_global.z, l.x, l.y,
172  l.z, &dp_point);
173  else
174  cam_pose.inverseComposePoint(
175  landmark_global.x, landmark_global.y, landmark_global.z, l.x, l.y,
176  l.z, &dp_point);
177 
178  ASSERT_(l.z != 0)
179 
180  const double _z = 1.0 / l.z;
181  const double _z2 = square(_z);
182 
183  const double tmp_vals[] = {
184  camera_params.fx() * _z, 0,
185  -camera_params.fx() * l.x * _z2, 0,
186  camera_params.fy() * _z, -camera_params.fy() * l.y * _z2};
188 
189  out_J.multiply_AB(tmp, dp_point);
190 }
191 
192 // === Compute sparse Jacobians ====
193 // Case: 6D poses + 3D points + 2D (x,y) observations
194 // For the case of *inverse* or *normal* frame poses being estimated.
195 // Made inline so immediate values in "poses_are_inverses" are propragated by
196 // the compiler
197 template <bool POSES_ARE_INVERSE>
199  const TFramePosesVec& frame_poses,
200  const TLandmarkLocationsVec& landmark_points,
201  const mrpt::utils::TCamera& camera_params,
202  mrpt::aligned_containers<JacData<6, 3, 2>>::vector_t& jac_data_vec,
203  const size_t num_fix_frames, const size_t num_fix_points)
204 {
205  MRPT_START
206 
207  // num_fix_frames & num_fix_points: Are relative to the order in frame_poses
208  // & landmark_points
209  ASSERT_(!frame_poses.empty() && !landmark_points.empty())
210 
211  const size_t N = jac_data_vec.size();
212 
213  for (size_t i = 0; i < N; i++)
214  {
215  JacData<6, 3, 2>& D = jac_data_vec[i];
216 
217  const TCameraPoseID i_f = D.frame_id;
218  const TLandmarkID i_p = D.point_id;
219 
220  ASSERTDEB_(i_f < frame_poses.size())
221  ASSERTDEB_(i_p < landmark_points.size())
222 
223  if (i_f >= num_fix_frames)
224  {
225  frameJac<POSES_ARE_INVERSE>(
226  camera_params, frame_poses[i_f], landmark_points[i_p],
227  D.J_frame);
228  D.J_frame_valid = true;
229  }
230 
231  if (i_p >= num_fix_points)
232  {
233  pointJac<POSES_ARE_INVERSE>(
234  camera_params, frame_poses[i_f], landmark_points[i_p],
235  D.J_point);
236  D.J_point_valid = true;
237  }
238  }
239  MRPT_END
240 }
241 
242 /** Construct the BA linear system.
243  * Set kernel_1st_deriv!=nullptr if using robust kernel.
244  */
246  const TSequenceFeatureObservations& observations,
247  const std::vector<std::array<double, 2>>& residual_vec,
248  const mrpt::aligned_containers<JacData<6, 3, 2>>::vector_t& jac_data_vec,
251  mrpt::aligned_containers<CArrayDouble<6>>::vector_t& eps_frame,
254  mrpt::aligned_containers<CArrayDouble<3>>::vector_t& eps_point,
255  const size_t num_fix_frames, const size_t num_fix_points,
256  const vector<double>* kernel_1st_deriv);
257 }
258 }
259 
260 #endif
void pointJac(const mrpt::utils::TCamera &camera_params, const mrpt::poses::CPose3D &cam_pose, const mrpt::math::TPoint3D &landmark_global, mrpt::math::CMatrixFixedNumeric< double, 2, 3 > &out_J)
Jacobians wrt the point.
Definition: ba_internals.h:157
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:135
mrpt::math::CMatrixFixedNumeric< double, ObsDim, PointDof > J_point
Definition: ba_internals.h:54
GLdouble GLdouble z
Definition: glext.h:3872
#define MRPT_MAKE_ALIGNED_OPERATOR_NEW
Definition: memory.h:134
TCameraPoseID frame_id
Definition: ba_internals.h:49
Helper types for STL containers with Eigen memory allocators.
void frameJac(const mrpt::utils::TCamera &camera_params, const mrpt::poses::CPose3D &cam_pose, const mrpt::math::TPoint3D &landmark_global, mrpt::math::CMatrixFixedNumeric< double, 2, 6 > &out_J)
The projective camera 2x6 Jacobian (wrt the 6D camera pose)
Definition: ba_internals.h:68
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:55
double fx() const
Get the value of the focal length x-value (in pixels).
Definition: TCamera.h:178
mrpt::aligned_containers< mrpt::poses::CPose3D >::vector_t TFramePosesVec
A list of camera frames (6D poses), which assumes indexes are unique, consecutive IDs...
double fy() const
Get the value of the focal length y-value (in pixels).
Definition: TCamera.h:180
A numeric matrix of compile-time fixed size.
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
#define MRPT_END
GLbyte ty
Definition: glext.h:6092
double x
X,Y,Z coordinates.
#define MRPT_START
uint64_t TCameraPoseID
Unique IDs for camera frames (poses)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
uint64_t TLandmarkID
Unique IDs for landmarks.
#define ASSERTDEB_(f)
Defines an assertion mechanism - only when compiled in debug.
const float R
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
void ba_build_gradient_Hessians(const TSequenceFeatureObservations &observations, const std::vector< std::array< double, 2 >> &residual_vec, const mrpt::aligned_containers< JacData< 6, 3, 2 >>::vector_t &jac_data_vec, mrpt::aligned_containers< mrpt::math::CMatrixFixedNumeric< double, 6, 6 >>::vector_t &U, mrpt::aligned_containers< CArrayDouble< 6 >>::vector_t &eps_frame, mrpt::aligned_containers< mrpt::math::CMatrixFixedNumeric< double, 3, 3 >>::vector_t &V, mrpt::aligned_containers< CArrayDouble< 3 >>::vector_t &eps_point, const size_t num_fix_frames, const size_t num_fix_points, const vector< double > *kernel_1st_deriv)
Construct the BA linear system.
Definition: ba_common.cpp:238
void composePoint(double lx, double ly, double lz, double &gx, double &gy, double &gz, mrpt::math::CMatrixFixedNumeric< double, 3, 3 > *out_jacobian_df_dpoint=nullptr, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dpose=nullptr, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dse3=nullptr, bool use_small_rot_approx=false) const
An alternative, slightly more efficient way of doing with G and L being 3D points and P this 6D pose...
Definition: CPose3D.cpp:453
T square(const T x)
Inline function for the square of a number.
#define ASSERT_(f)
std::vector< mrpt::math::TPoint3D > TLandmarkLocationsVec
A list of landmarks (3D points), which assumes indexes are unique, consecutive IDs.
A partial specialization of CArrayNumeric for double numbers.
Definition: CArrayNumeric.h:87
mrpt::math::CMatrixFixedNumeric< double, ObsDim, FrameDof > J_frame
Definition: ba_internals.h:53
GLbyte GLbyte tz
Definition: glext.h:6092
GLenum GLint GLint y
Definition: glext.h:3538
GLenum GLint x
Definition: glext.h:3538
void getRotationMatrix(mrpt::math::CMatrixDouble33 &ROT) const
Get the 3x3 rotation matrix.
Definition: CPose3D.h:237
Lightweight 3D point.
TLandmarkID point_id
Definition: ba_internals.h:50
void ba_compute_Jacobians(const TFramePosesVec &frame_poses, const TLandmarkLocationsVec &landmark_points, const mrpt::utils::TCamera &camera_params, mrpt::aligned_containers< JacData< 6, 3, 2 >>::vector_t &jac_data_vec, const size_t num_fix_frames, const size_t num_fix_points)
Definition: ba_internals.h:198
Structure to hold the parameters of a pinhole camera model.
Definition: TCamera.h:32
void inverseComposePoint(const double gx, const double gy, const double gz, double &lx, double &ly, double &lz, mrpt::math::CMatrixFixedNumeric< double, 3, 3 > *out_jacobian_df_dpoint=nullptr, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dpose=nullptr, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dse3=nullptr) const
Computes the 3D point L such as .
Definition: CPose3D.cpp:723



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019