NDEVR
API Documentation
MapPointProjection.h
1#pragma once
2#include "OrbSLAM/Headers/MapPoint.h"
3#include "OrbSLAM/Headers/Frame.h"
4#include "GraphOptimization/Headers/GeometricCamera.h"
5namespace NDEVR
6{
13 {
21
22 uint04 track_scale_level, track_scale_levelR;
23 float mTrackViewCos, mTrackViewCosR;
24 bool mbTrackInView = false;
25 bool mbTrackInViewR = false;
26 bool is_in_frustum = false;
32 MapPointProjection(const Frame& frame, MapPoint* pMP, fltp04 view_cos_limit)
33 : reference(pMP)
34 {
35 is_in_frustum = checkInFrustum(frame, pMP, view_cos_limit);
36 }
37
44 bool checkInFrustum(const Frame& frame, MapPoint* pMP, fltp04 view_cos_limit)
45 {
46 if (frame.m_view[0]->keypoint_count == 0 || IsInvalid(frame.m_view[0]->keypoint_count))
47 {
48 mbTrackInView = false;
49 mTrackProjX = -1.0f;
50 mTrackProjY = -1.0f;
51
52 // 3D in absolute coordinates
53 Eigen::Matrix<fltp04, 3, 1> P = pMP->worldPos();
54
55 // 3D in camera coordinates
56 const Eigen::Vector3f pc = frame.inRefCoordinates(P);
57 mTrackDepth = pc.norm();
58
59 // Check positive depth
60 const fltp04 pcz = pc(2);
61 const fltp04 invz = 1.0f / pcz;
62 if (pcz < 0.0f)
63 return false;
64
65 const Eigen::Vector2f uv = frame.m_view[0]->camera->project(pc);
66
67 if (uv(X) < frame.info().image_bounds[MIN][X] || uv(X) > frame.info().image_bounds[MAX][X])
68 return false;
69 if (uv(Y) < frame.info().image_bounds[MIN][Y] || uv(Y) > frame.info().image_bounds[MAX][Y])
70 return false;
71
72 mTrackProjX = uv(0);
73 mTrackProjY = uv(1);
74
75 // Check distance is in the scale invariance region of the MapPoint
76 const fltp04 max_distance = pMP->GetMaxDistanceInvariance();
77 const fltp04 min_distance = pMP->GetMinDistanceInvariance();
78 const Eigen::Vector3<g_type> PO = P - frame.cameraCenter();
79 const g_type dist = PO.norm();
80
81 if (dist < min_distance || dist > max_distance)
82 return false;
83
84 // Check viewing angle
85 Eigen::Vector3<g_type> Pn = pMP->GetNormal();
86
87 mTrackViewCos = PO.dot(Pn) / dist;
88
89 if (mTrackViewCos < view_cos_limit)
90 return false;
91
92 // Predict scale in the image
93 track_scale_level = pMP->predictScale(dist, frame);
94
95 // Data used by the tracking
96 mbTrackInView = true;
97 mTrackProjX = uv(0);
98 mTrackProjXR = uv(0) - frame.info().mbf * invz;
99 mTrackProjY = uv(1);
100 return true;
101 }
102 else
103 {
104 track_scale_level = Constant<uint04>::Invalid;
105 track_scale_levelR = Constant<uint04>::Invalid;
106 mbTrackInView = isInFrustumChecks(frame, pMP, view_cos_limit, false);
107 mbTrackInViewR = isInFrustumChecks(frame, pMP, view_cos_limit, true);
109 }
110 }
111
119 bool isInFrustumChecks(const Frame& frame, MapPoint* pMP, float viewingCosLimit, bool bRight = false)
120 {
121 // 3D in absolute coordinates
122 Eigen::Vector3f P = pMP->worldPos();
123 Eigen::Matrix3f mR;
124 Eigen::Vector3f mt, twc;
125 if (bRight)
126 {
127
128 auto mTrl = frame.GetRelativePoseTrl();
129 //Eigen::Matrix3f Rrl = mTrl.rotationMatrix();
130 Eigen::Matrix3f Rrl = mTrl.rotationMatrix();
131 //Eigen::Vector3f trl = mTrl.translation();
132 Eigen::Vector3f trl = mTrl.translation();
133 mR = Rrl * frame.GetRotation();
134 mt = Rrl * frame.tcw() + trl;
135 twc = frame.GetRotation() * frame.GetRelativePoseTlr_translation() + frame.cameraCenter();
136 }
137 else
138 {
139 mR = frame.GetRotation();
140 mt = frame.tcw();
141 twc = frame.cameraCenter();
142 }
143
144 // 3D in camera coordinates
145 Eigen::Vector3f Pc = mR * P + mt;
146 const float Pc_dist = Pc.norm();
147 const float& PcZ = Pc(2);
148
149 // Check positive depth
150 if (PcZ < 0.0f)
151 return false;
152
153 // Project in image and check it is not outside
154 Eigen::Vector2f uv = frame.m_view[bRight ? 1 : 0]->camera->project(Pc);
155
156 if (uv(0) < frame.info().image_bounds[MIN][X] || uv(0) > frame.info().image_bounds[MAX][X])
157 return false;
158 if (uv(1) < frame.info().image_bounds[MIN][Y] || uv(1) > frame.info().image_bounds[MAX][Y])
159 return false;
160
161 // Check distance is in the scale invariance region of the MapPoint
162 const Eigen::Vector3f PO = P - twc;
163 const float dist = PO.norm();
164
165 if (dist < pMP->GetMinDistanceInvariance())
166 return false;
167 if (dist > pMP->GetMaxDistanceInvariance())
168 return false;
169
170 // Check viewing angle
171 Eigen::Vector3f Pn = pMP->GetNormal();
172
173 const float viewCos = PO.dot(Pn) / dist;
174
175 if (viewCos < viewingCosLimit)
176 return false;
177
178 // Predict scale in the image
179 const uint04 nPredictedLevel = pMP->predictScale(dist, frame);
180
181 if (bRight)
182 {
183 mTrackProjXR = uv(0);
184 mTrackProjYR = uv(1);
185 track_scale_levelR = nPredictedLevel;
186 mTrackViewCosR = viewCos;
187 mTrackDepthR = Pc_dist;
188 }
189 else
190 {
191 mTrackProjX = uv(0);
192 mTrackProjY = uv(1);
193 track_scale_level = nPredictedLevel;
194 mTrackViewCos = viewCos;
195 mTrackDepth = Pc_dist;
196 }
197
198 return true;
199 }
200 };
201}
Represents a single image frame with extracted features and pose information.
Definition Frame.h:111
const Eigen::Matrix< g_type, 3, 1 > & tcw() const
Returns the camera-to-world translation.
Definition Frame.h:385
Eigen::Vector3< g_type > inRefCoordinates(Eigen::Vector3< g_type > pCw) const
Transforms a world-coordinate point into this frame's camera coordinates.
Sophus::SE3< g_type > GetRelativePoseTrl() const
Returns the relative pose from right to left camera.
FrameView * m_view[2]
Left [0] and right [1] view data.
Definition Frame.h:580
virtual Eigen::Matrix3< g_type > GetRotation() const
Returns the camera rotation matrix (camera-to-world).
const FrameInfo & info() const
Returns the shared frame info.
Definition Frame.h:527
const Eigen::Vector3< g_type > & cameraCenter() const
Returns the camera center in world coordinates.
Definition Frame.h:379
Eigen::Vector3< g_type > GetRelativePoseTlr_translation() const
Returns the translation component of the left-to-right relative pose.
virtual cv::Point2f project(const cv::Point3f &p3D)=0
Projects a 3D point to 2D image coordinates (cv::Point3f overload).
A 3D point in the SLAM map observed by multiple keyframes.
Definition MapPoint.h:24
The primary namespace for the NDEVR SDK.
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
static constexpr bool IsInvalid(const Angle< t_type > &value)
Checks whether the given Angle holds an invalid value.
Definition Angle.h:388
float mbf
Stereo baseline multiplied by fx.
Definition Frame.h:79
Bounds< 2, fltp04 > image_bounds
Undistorted image bounds.
Definition Frame.h:95
uint04 keypoint_count
Number of overlapping keypoints.
Definition Frame.h:61
GeometricCamera * camera
Camera model for this view.
Definition Frame.h:60
float mTrackDepth
Depth in the left camera.
float mTrackProjX
Projected x coordinate in the left image.
MapPoint * reference
The map point being projected.
bool isInFrustumChecks(const Frame &frame, MapPoint *pMP, float viewingCosLimit, bool bRight=false)
Checks frustum visibility for a single view (left or right).
bool mbTrackInViewR
Whether the point is visible in the right view.
bool checkInFrustum(const Frame &frame, MapPoint *pMP, fltp04 view_cos_limit)
Checks whether a map point is in the camera frustum and fills tracking data.
float mTrackViewCosR
Viewing cosine angles (left and right).
float mTrackDepthR
Depth in the right camera.
float mTrackProjXR
Projected x coordinate in the right image.
uint04 track_scale_levelR
Predicted scale levels (left and right).
float mTrackProjYR
Projected y coordinate in the right image.
bool mbTrackInView
Whether the point is visible in the left view.
bool is_in_frustum
Whether the point is inside the camera frustum.
float mTrackProjY
Projected y coordinate in the left image.
MapPointProjection(const Frame &frame, MapPoint *pMP, fltp04 view_cos_limit)
Constructs a projection and checks frustum visibility.