34# define snprintf _snprintf
44struct EuclideanPipelineRoutines {
45 typedef EuclideanReconstruction Reconstruction;
46 typedef EuclideanCamera
Camera;
47 typedef EuclideanPoint Point;
49 static void Bundle(
const Tracks& tracks,
65 static Marker ProjectMarker(
const EuclideanPoint& point,
66 const EuclideanCamera& camera,
67 const CameraIntrinsics& intrinsics) {
68 Vec3 projected = camera.R * point.X + camera.t;
69 projected /= projected(2);
71 Marker reprojected_marker;
72 intrinsics.ApplyIntrinsics(projected(0),
74 &reprojected_marker.x,
75 &reprojected_marker.y);
77 reprojected_marker.image = camera.image;
78 reprojected_marker.track = point.track;
79 return reprojected_marker;
83struct ProjectivePipelineRoutines {
84 typedef ProjectiveReconstruction Reconstruction;
85 typedef ProjectiveCamera
Camera;
86 typedef ProjectivePoint Point;
88 static void Bundle(
const Tracks& tracks,
106 static Marker ProjectMarker(
const ProjectivePoint& point,
107 const ProjectiveCamera& camera,
108 const CameraIntrinsics& intrinsics) {
109 Vec3 projected = camera.P * point.X;
110 projected /= projected(2);
112 Marker reprojected_marker;
113 intrinsics.ApplyIntrinsics(projected(0),
115 &reprojected_marker.x,
116 &reprojected_marker.y);
118 reprojected_marker.image = camera.image;
119 reprojected_marker.track = point.track;
120 return reprojected_marker;
129 const char* step =
NULL) {
130 if (update_callback) {
136 "Completing solution %d%% | %s",
137 (
int)(progress * 100),
142 "Completing solution %d%%",
143 (
int)(progress * 100));
146 update_callback->
invoke(progress, message);
150template <
typename PipelineRoutines>
155 int max_track = tracks.MaxTrack();
156 int max_image = tracks.MaxImage();
157 int num_resects = -1;
158 int num_intersects = -1;
160 LG <<
"Max track: " << max_track;
161 LG <<
"Max image: " << max_image;
162 LG <<
"Number of markers: " << tracks.NumMarkers();
163 while (num_resects != 0 || num_intersects != 0) {
166 for (
int track = 0; track <= max_track; ++track) {
168 LG <<
"Skipping point: " << track;
172 LG <<
"Got " << all_markers.size() <<
" markers for track " << track;
175 for (
int i = 0; i < all_markers.size(); ++i) {
177 reconstructed_markers.push_back(all_markers[i]);
180 LG <<
"Got " << reconstructed_markers.size()
181 <<
" reconstructed markers for track " << track;
182 if (reconstructed_markers.size() >= 2) {
184 double(tot_resects) / (max_image));
185 if (PipelineRoutines::Intersect(reconstructed_markers,
188 LG <<
"Ran Intersect() for track " << track;
190 LG <<
"Failed Intersect() for track " << track;
194 if (num_intersects) {
196 update_callback,
double(tot_resects) / (max_image),
"Bundling...");
198 LG <<
"Ran Bundle() after intersections.";
200 LG <<
"Did " << num_intersects <<
" intersects.";
204 for (
int image = 0; image <= max_image; ++
image) {
206 LG <<
"Skipping frame: " <<
image;
210 LG <<
"Got " << all_markers.size() <<
" markers for image " <<
image;
213 for (
int i = 0; i < all_markers.size(); ++i) {
215 reconstructed_markers.push_back(all_markers[i]);
218 LG <<
"Got " << reconstructed_markers.size()
219 <<
" reconstructed markers for image " <<
image;
220 if (reconstructed_markers.size() >= 5) {
222 double(tot_resects) / (max_image));
223 if (PipelineRoutines::Resect(
227 LG <<
"Ran Resect() for image " <<
image;
229 LG <<
"Failed Resect() for image " <<
image;
235 update_callback,
double(tot_resects) / (max_image),
"Bundling...");
238 LG <<
"Did " << num_resects <<
" resects.";
243 for (
int image = 0; image <= max_image; ++
image) {
245 LG <<
"Skipping frame: " <<
image;
251 for (
int i = 0; i < all_markers.size(); ++i) {
253 reconstructed_markers.push_back(all_markers[i]);
256 if (reconstructed_markers.size() >= 5) {
258 double(tot_resects) / (max_image));
259 if (PipelineRoutines::Resect(
262 LG <<
"Ran final Resect() for image " <<
image;
264 LG <<
"Failed final Resect() for image " <<
image;
270 update_callback,
double(tot_resects) / (max_image),
"Bundling...");
275template <
typename PipelineRoutines>
277 const Tracks& image_tracks,
281 int num_reprojected = 0;
282 double total_error = 0.0;
284 for (
int i = 0; i <
markers.size(); ++i) {
285 double weight =
markers[i].weight;
286 const typename PipelineRoutines::Camera* camera =
288 const typename PipelineRoutines::Point* point =
290 if (!camera || !point || weight == 0.0) {
296 Marker reprojected_marker =
297 PipelineRoutines::ProjectMarker(*point, *camera, intrinsics);
298 double ex = (reprojected_marker.
x -
markers[i].x) * weight;
299 double ey = (reprojected_marker.
y -
markers[i].y) * weight;
305 "image %-3d track %-3d "
314 reprojected_marker.
x,
315 reprojected_marker.
y,
318 sqrt(ex * ex + ey * ey));
321 total_error +=
sqrt(ex * ex + ey * ey);
323 LG <<
"Skipped " << num_skipped <<
" markers.";
324 LG <<
"Reprojected " << num_reprojected <<
" markers.";
325 LG <<
"Total error: " << total_error <<
" px";
326 LG <<
"Average error: " << (total_error / num_reprojected) <<
" px";
327 return total_error / num_reprojected;
338 const Tracks& image_tracks,
360 Tracks* calibrated_tracks) {
362 for (
int i = 0; i <
markers.size(); ++i) {
DBVT_INLINE bool Intersect(const btDbvtAabbMm &a, const btDbvtAabbMm &b)
virtual void InvertIntrinsics(double image_x, double image_y, double *normalized_x, double *normalized_y) const =0
virtual void invoke(double progress, const char *message)=0
ProjectivePoint * PointForTrack(int track)
Returns a pointer to the point corresponding to track.
ProjectiveCamera * CameraForImage(int image)
Returns a pointer to the camera corresponding to image.
vector< Marker > AllMarkers() const
Returns all the markers.
input_tx image(0, GPU_RGBA16F, Qualifier::WRITE, ImageType::FLOAT_2D, "preview_img") .compute_source("compositor_compute_preview.glsl") .do_static_compilation(true)
const vector< Marker > & markers
const ProjectiveReconstruction & reconstruction
double ProjectiveReprojectionError(const Tracks &image_tracks, const ProjectiveReconstruction &reconstruction, const CameraIntrinsics &intrinsics)
void ProjectiveBundle(const Tracks &, ProjectiveReconstruction *)
bool ProjectiveResect(const vector< Marker > &markers, ProjectiveReconstruction *reconstruction)
bool EuclideanResect(const vector< Marker > &markers, EuclideanReconstruction *reconstruction, bool final_pass)
static void CompleteReconstructionLogProgress(ProgressUpdateCallback *update_callback, double progress, const char *step=NULL)
void EuclideanBundle(const Tracks &tracks, EuclideanReconstruction *reconstruction)
void EuclideanCompleteReconstruction(const Tracks &tracks, EuclideanReconstruction *reconstruction, ProgressUpdateCallback *update_callback)
bool EuclideanIntersect(const vector< Marker > &markers, EuclideanReconstruction *reconstruction)
double InternalReprojectionError(const Tracks &image_tracks, const typename PipelineRoutines::Reconstruction &reconstruction, const CameraIntrinsics &intrinsics)
double EuclideanReprojectionError(const Tracks &image_tracks, const EuclideanReconstruction &reconstruction, const CameraIntrinsics &intrinsics)
void InvertIntrinsicsForTracks(const Tracks &raw_tracks, const CameraIntrinsics &camera_intrinsics, Tracks *calibrated_tracks)
bool ProjectiveIntersect(const vector< Marker > &markers, ProjectiveReconstruction *reconstruction)
void InternalCompleteReconstruction(const Tracks &tracks, typename PipelineRoutines::Reconstruction *reconstruction, ProgressUpdateCallback *update_callback=NULL)
void ProjectiveCompleteReconstruction(const Tracks &tracks, ProjectiveReconstruction *reconstruction)