Welcome to mirror list, hosted at ThFree Co, Russian Federation.

tracking_ops_correspondence.c « space_clip « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4c48632634a5c55180ade948791953a7280260b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2016 Blender Foundation.
 * All rights reserved.
 *
 *
 * Contributor(s): Blender Foundation,
 *                 Tianwei Shen
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/editors/space_clip/tracking_ops_correspondence.c
 *  \ingroup spclip
 */

#include "MEM_guardedalloc.h"

#include "DNA_screen_types.h"
#include "DNA_space_types.h"
#include "DNA_camera_types.h"
#include "DNA_object_types.h"

#include "BLI_utildefines.h"
#include "BLI_ghash.h"
#include "BLI_math.h"
#include "BLI_blenlib.h"
#include "BLI_string.h"

#include "BKE_main.h"
#include "BKE_context.h"
#include "BKE_movieclip.h"
#include "BKE_tracking.h"
#include "BKE_depsgraph.h"
#include "BKE_report.h"
#include "BKE_global.h"
#include "BKE_library.h"

#include "WM_api.h"
#include "WM_types.h"

#include "ED_screen.h"
#include "ED_clip.h"

#include "RNA_access.h"
#include "RNA_define.h"

#include "BLT_translation.h"

#include "clip_intern.h"
#include "tracking_ops_intern.h"

/********************** add correspondence operator *********************/

/* return the pointer to a single selected track, if more than one is selected, return NULL */
static MovieTrackingTrack *get_single_track(SpaceClip *sc, ListBase *tracksbase)
{
	int num_selected_tracks = 0;
	MovieTrackingTrack *selected_track;
	for (MovieTrackingTrack *track = tracksbase->first; track; track = track->next) {
		if (TRACK_VIEW_SELECTED(sc, track)) {
			selected_track = track;
			num_selected_tracks++;
		}
	}
	if (num_selected_tracks == 1) {
		return selected_track;
	}
	return NULL;
}

static int add_correspondence_exec(bContext *C, wmOperator *op)
{
	SpaceClip *sc = CTX_wm_space_clip(C);
	/* get primary clip */
	MovieClip *clip = ED_space_clip_get_clip(sc);
	MovieTracking *tracking = &clip->tracking;
	ListBase *tracksbase = BKE_tracking_get_active_tracks(tracking);

	/* get one track from each clip and link them */
	MovieTrackingTrack *primary_track = NULL, *witness_track = NULL;

	/* get a single selected tracks in the primary camera */
	primary_track = get_single_track(sc, tracksbase);

	/* get a single selected tracks in the witness camera, only one witness camera is allowed */
	MovieClip *second_clip = ED_space_clip_get_secondary_clip(sc);
	MovieTracking *second_tracking = &second_clip->tracking;
	ListBase *second_tracksbase = BKE_tracking_get_active_tracks(second_tracking);
	witness_track = get_single_track(sc, second_tracksbase);

	if (!primary_track || !witness_track) {
		BKE_report(op->reports, RPT_ERROR, "Select exactly one track in each clip");
		return OPERATOR_CANCELLED;
	}

	// add these correspondence
	char error_msg[256] = "\0";
	if (!BKE_tracking_correspondence_add(&(tracking->correspondences), primary_track, witness_track,
	                                     clip, second_clip, error_msg, sizeof(error_msg))) {
		if (error_msg[0])
			BKE_report(op->reports, RPT_ERROR, error_msg);
		return OPERATOR_CANCELLED;
	}

	return OPERATOR_FINISHED;
}

void CLIP_OT_add_correspondence(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Add Correspondence";
	ot->idname = "CLIP_OT_add_correspondence";
	ot->description = "Add correspondence between primary camera and witness camera";

	/* api callbacks */
	ot->exec = add_correspondence_exec;
	ot->poll = ED_space_clip_correspondence_poll;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}

/********************** delete correspondence operator *********************/

static int delete_correspondence_exec(bContext *C, wmOperator *UNUSED(op))
{
	SpaceClip *sc = CTX_wm_space_clip(C);
	MovieClip *clip = ED_space_clip_get_clip(sc);
	MovieTracking *tracking = &clip->tracking;
	bool changed = false;

	/* Remove track correspondences from correspondence base */
	MovieTrackingObject *object = BKE_tracking_object_get_active(tracking);
	ListBase *correspondence_base = &tracking->correspondences;
	for (MovieTrackingCorrespondence *corr = correspondence_base->first;
	     corr != NULL;
	     corr = corr->next)
	{
		MovieTrackingTrack *track;
		track = BKE_tracking_track_get_named(tracking, object, corr->self_track_name);
		if (TRACK_VIEW_SELECTED(sc, track)) {
			BLI_freelinkN(correspondence_base, corr);
			changed = true;
		}
	}

	/* Nothing selected now, unlock view so it can be scrolled nice again. */
	sc->flag &= ~SC_LOCK_SELECTION;

	if (changed) {
		WM_event_add_notifier(C, NC_MOVIECLIP | NA_EDITED, clip);
	}

	return OPERATOR_FINISHED;
}

void CLIP_OT_delete_correspondence(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Delete Correspondence";
	ot->idname = "CLIP_OT_delete_correspondence";
	ot->description = "Delete selected tracker correspondene between primary and witness camera";

	/* api callbacks */
	ot->invoke = WM_operator_confirm;
	ot->exec = delete_correspondence_exec;
	ot->poll = ED_space_clip_correspondence_poll;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}

/********************** solve multiview operator *********************/

typedef struct {
	Scene *scene;
	int clip_num;					/* the number of active clips for multi-view reconstruction*/
	MovieClip **clips;				/* a clip pointer array that records all the clip pointers */
	MovieClipUser user;
	ReportList *reports;
	char stats_message[256];
	struct MovieMultiviewReconstructContext *context;
} SolveMultiviewJob;

/* initialize multiview reconstruction solve
 * which is assumed to be triggered only in the primary clip
 */
static bool solve_multiview_initjob(bContext *C,
                                    SolveMultiviewJob *smj,
                                    wmOperator *op,
                                    char *error_msg,
                                    int max_error)
{
	SpaceClip *sc = CTX_wm_space_clip(C);
	MovieClip *clip = ED_space_clip_get_clip(sc);
	Scene *scene = CTX_data_scene(C);
	MovieTracking *tracking = &clip->tracking;
	MovieTrackingObject *object = BKE_tracking_object_get_active(tracking);
	int width, height;

	/* count all clips number, primary clip will always be the first
	 * iterate over bContext->data.main.movieclip to get open clips.
	 */
	Main *main = CTX_data_main(C);
	ListBase *mc_base = &(main->movieclip);
	smj->clip_num = BLI_listbase_count(mc_base);
	printf("%d open clips for reconstruction\n", smj->clip_num);
	smj->clips = MEM_callocN(smj->clip_num * sizeof(MovieClip*), "multiview clip pointers");
	smj->clips[0] = clip;

	/* do multi-view reconstruction, fill in witness clips from Main.movieclip */
	int mc_counter = 1;
	for (Link *link = main->movieclip.first;
	     link != NULL;
	     link = link->next)
	{
		MovieClip *mc_link = (MovieClip*) link;
		if (mc_link != smj->clips[0]) {
			smj->clips[mc_counter++] = mc_link;
		}
	}
	BLI_assert(mc_counter == smj->clip_num);

	if (!BKE_tracking_multiview_reconstruction_check(smj->clips,
	                                                 object,
	                                                 error_msg,
	                                                 max_error))
	{
		return false;
	}

	/* Could fail if footage uses images with different sizes. */
	BKE_movieclip_get_size(clip, &sc->user, &width, &height);

	smj->scene = scene;
	smj->reports = op->reports;
	smj->user = sc->user;

	// create multiview reconstruction context and pass the tracks and markers to libmv
	smj->context = BKE_tracking_multiview_reconstruction_context_new(smj->clips,
	                                                                 smj->clip_num,
	                                                                 object,
	                                                                 object->keyframe1,
	                                                                 object->keyframe2,
	                                                                 width,
	                                                                 height);
	printf("new multiview reconstruction context\n");

	tracking->stats = MEM_callocN(sizeof(MovieTrackingStats), "solve multiview stats");

	return true;
}

static void solve_multiview_updatejob(void *scv)
{
	SolveMultiviewJob *smj = (SolveMultiviewJob *)scv;
	MovieClip *primary_clip = smj->clips[0];
	MovieTracking *tracking = &primary_clip->tracking;

	BLI_strncpy(tracking->stats->message,
	            smj->stats_message,
	            sizeof(tracking->stats->message));
}

static void solve_multiview_startjob(void *scv, short *stop, short *do_update, float *progress)
{
	SolveMultiviewJob *smj = (SolveMultiviewJob *)scv;
	BKE_tracking_multiview_reconstruction_solve(smj->context,
	                                            stop,
	                                            do_update,
	                                            progress,
	                                            smj->stats_message,
	                                            sizeof(smj->stats_message));
}

// TODO(tianwei): not sure about the scene for witness cameras, check with Sergey
static void solve_multiview_freejob(void *scv)
{
	SolveMultiviewJob *smj = (SolveMultiviewJob *)scv;
	MovieClip *clip = smj->clips[0];	// primary camera
	MovieTracking *tracking = &clip->tracking;
	Scene *scene = smj->scene;
	int solved;

	if (!smj->context) {
		/* job weren't fully initialized due to some error */
		MEM_freeN(smj);
		return;
	}

	solved = BKE_tracking_multiview_reconstruction_finish(smj->context, smj->clips);
	if (!solved) {
		BKE_report(smj->reports,
		           RPT_WARNING,
		           "Some data failed to reconstruct (see console for details)");
	}
	else {
		BKE_reportf(smj->reports,
		            RPT_INFO,
		            "Average re-projection error: %.3f",
		            tracking->reconstruction.error);
	}

	/* Set the currently solved primary clip as active for scene. */
	if (scene->clip != NULL) {
		id_us_min(&clip->id);
	}
	scene->clip = clip;
	id_us_plus(&clip->id);

	/* Set blender camera focal length so result would look fine there. */
	if (scene->camera != NULL &&
	    scene->camera->data &&
	    GS(((ID *) scene->camera->data)->name) == ID_CA) {
		Camera *camera = (Camera *)scene->camera->data;
		int width, height;
		BKE_movieclip_get_size(clip, &smj->user, &width, &height);
		BKE_tracking_camera_to_blender(tracking, scene, camera, width, height);
		WM_main_add_notifier(NC_OBJECT, camera);
	}

	MEM_freeN(tracking->stats);
	tracking->stats = NULL;

	DAG_id_tag_update(&clip->id, 0);

	WM_main_add_notifier(NC_MOVIECLIP | NA_EVALUATED, clip);
	WM_main_add_notifier(NC_OBJECT | ND_TRANSFORM, NULL);

	/* Update active clip displayed in scene buttons. */
	WM_main_add_notifier(NC_SCENE, scene);

	BKE_tracking_multiview_reconstruction_context_free(smj->context);
	MEM_freeN(smj);
}

static int solve_multiview_exec(bContext *C, wmOperator *op)
{
	SolveMultiviewJob *scj;
	char error_msg[256] = "\0";
	scj = MEM_callocN(sizeof(SolveMultiviewJob), "SolveMultiviewJob data");
	if (!solve_multiview_initjob(C, scj, op, error_msg, sizeof(error_msg))) {
		if (error_msg[0]) {
			BKE_report(op->reports, RPT_ERROR, error_msg);
		}
		solve_multiview_freejob(scj);
		return OPERATOR_CANCELLED;
	}
	solve_multiview_startjob(scj, NULL, NULL, NULL);
	solve_multiview_freejob(scj);
	return OPERATOR_FINISHED;
}

static int solve_multiview_invoke(bContext *C,
                                  wmOperator *op,
                                  const wmEvent *UNUSED(event))
{
	SolveMultiviewJob *scj;
	ScrArea *sa = CTX_wm_area(C);
	SpaceClip *sc = CTX_wm_space_clip(C);
	MovieClip *clip = ED_space_clip_get_clip(sc);
	MovieTracking *tracking = &clip->tracking;
	MovieTrackingReconstruction *reconstruction =
	        BKE_tracking_get_active_reconstruction(tracking);
	wmJob *wm_job;
	char error_msg[256] = "\0";

	if (WM_jobs_test(CTX_wm_manager(C), sa, WM_JOB_TYPE_ANY)) {
		/* only one solve is allowed at a time */
		return OPERATOR_CANCELLED;
	}

	scj = MEM_callocN(sizeof(SolveMultiviewJob), "SolveCameraJob data");
	if (!solve_multiview_initjob(C, scj, op, error_msg, sizeof(error_msg))) {
		if (error_msg[0]) {
			BKE_report(op->reports, RPT_ERROR, error_msg);
		}
		solve_multiview_freejob(scj);
		return OPERATOR_CANCELLED;
	}

	BLI_strncpy(tracking->stats->message,
	            "Solving multiview | Preparing solve",
	            sizeof(tracking->stats->message));

	/* Hide reconstruction statistics from previous solve. */
	reconstruction->flag &= ~TRACKING_RECONSTRUCTED;
	WM_event_add_notifier(C, NC_MOVIECLIP | NA_EVALUATED, clip);

	/* Setup job. */
	wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), sa, "Solve Camera",
	                     WM_JOB_PROGRESS, WM_JOB_TYPE_CLIP_SOLVE_CAMERA);
	WM_jobs_customdata_set(wm_job, scj, solve_multiview_freejob);
	WM_jobs_timer(wm_job, 0.1, NC_MOVIECLIP | NA_EVALUATED, 0);
	WM_jobs_callbacks(wm_job,
	                  solve_multiview_startjob,
	                  NULL,
	                  solve_multiview_updatejob,
	                  NULL);

	G.is_break = false;

	WM_jobs_start(CTX_wm_manager(C), wm_job);
	WM_cursor_wait(0);

	/* add modal handler for ESC */
	WM_event_add_modal_handler(C, op);

	return OPERATOR_RUNNING_MODAL;
}

static int solve_multiview_modal(bContext *C,
                                 wmOperator *UNUSED(op),
                                 const wmEvent *event)
{
	/* No running solver, remove handler and pass through. */
	if (0 == WM_jobs_test(CTX_wm_manager(C), CTX_wm_area(C), WM_JOB_TYPE_ANY))
		return OPERATOR_FINISHED | OPERATOR_PASS_THROUGH;

	/* Running solver. */
	switch (event->type) {
		case ESCKEY:
			return OPERATOR_RUNNING_MODAL;
	}

	return OPERATOR_PASS_THROUGH;
}

void CLIP_OT_solve_multiview(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Solve multi-view reconstruction";
	ot->idname = "CLIP_OT_solve_multiview";
	ot->description = "Solve multiview reconstruction";

	/* api callbacks */
	ot->exec = solve_multiview_exec;
	ot->invoke = solve_multiview_invoke;
	ot->modal = solve_multiview_modal;
	ot->poll = ED_space_clip_correspondence_poll;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}