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

clip_graph_draw.c « space_clip « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cc8541d9fd738532b192b79a8ff7612fb02140b8 (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
/*
 * ***** 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) 2011 Blender Foundation.
 * All rights reserved.
 *
 *
 * Contributor(s): Blender Foundation,
 *                 Sergey Sharybin
 *
 * ***** END GPL LICENSE BLOCK *****
 */

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

#include "DNA_movieclip_types.h"
#include "DNA_scene_types.h"

#include "BLI_utildefines.h"
#include "BLI_math.h"

#include "BKE_context.h"
#include "BKE_movieclip.h"
#include "BKE_tracking.h"

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

#include "GPU_immediate.h"
#include "GPU_immediate_util.h"
#include "GPU_matrix.h"
#include "GPU_state.h"

#include "WM_types.h"

#include "UI_interface.h"
#include "UI_resources.h"
#include "UI_view2d.h"


#include "clip_intern.h"    // own include

typedef struct TrackMotionCurveUserData {
	MovieTrackingTrack *act_track;
	bool sel;
	float xscale, yscale, hsize;
	unsigned int pos;
} TrackMotionCurveUserData;

static void tracking_segment_point_cb(void *userdata, MovieTrackingTrack *UNUSED(track),
                                      MovieTrackingMarker *UNUSED(marker), int UNUSED(coord),
                                      int scene_framenr, float val)
{
	TrackMotionCurveUserData *data = (TrackMotionCurveUserData *) userdata;

	immVertex2f(data->pos, scene_framenr, val);
}

static void tracking_segment_start_cb(void *userdata, MovieTrackingTrack *track, int coord, bool is_point)
{
	TrackMotionCurveUserData *data = (TrackMotionCurveUserData *) userdata;
	float col[4] = {0.0f, 0.0f, 0.0f, 0.0f};

	col[coord] = 1.0f;

	if (track == data->act_track) {
		col[3] = 1.0f;
		GPU_line_width(2.0f);
	}
	else {
		col[3] = 0.5f;
		GPU_line_width(1.0f);
	}

	immUniformColor4fv(col);

	if (is_point) {
		immBeginAtMost(GPU_PRIM_POINTS, 1);
	}
	else {
		/* Graph can be composed of smaller segments, if any marker is disabled */
		immBeginAtMost(GPU_PRIM_LINE_STRIP, track->markersnr);
	}
}

static void tracking_segment_end_cb(void *UNUSED(userdata), int UNUSED(coord))
{
	immEnd();
}

static void tracking_segment_knot_cb(void *userdata, MovieTrackingTrack *track,
                                     MovieTrackingMarker *marker, int coord, int scene_framenr, float val)
{
	TrackMotionCurveUserData *data = (TrackMotionCurveUserData *) userdata;
	int sel = 0, sel_flag;

	if (track != data->act_track)
		return;

	sel_flag = coord == 0 ? MARKER_GRAPH_SEL_X : MARKER_GRAPH_SEL_Y;
	sel = (marker->flag & sel_flag) ? 1 : 0;

	if (sel == data->sel) {
		immUniformThemeColor(sel ? TH_HANDLE_VERTEX_SELECT : TH_HANDLE_VERTEX);

		GPU_matrix_push();
		GPU_matrix_translate_2f(scene_framenr, val);
		GPU_matrix_scale_2f(1.0f / data->xscale * data->hsize, 1.0f / data->yscale * data->hsize);

		imm_draw_circle_wire_2d(data->pos, 0, 0, 0.7, 8);

		GPU_matrix_pop();
	}
}

static void draw_tracks_motion_curves(View2D *v2d, SpaceClip *sc, unsigned int pos)
{
	MovieClip *clip = ED_space_clip_get_clip(sc);
	MovieTracking *tracking = &clip->tracking;
	MovieTrackingTrack *act_track = BKE_tracking_track_get_active(tracking);
	int width, height;
	TrackMotionCurveUserData userdata;

	BKE_movieclip_get_size(clip, &sc->user, &width, &height);

	if (!width || !height)
		return;

	/* non-selected knot handles */
	userdata.hsize = UI_GetThemeValuef(TH_HANDLE_VERTEX_SIZE);
	userdata.sel = false;
	userdata.act_track = act_track;
	userdata.pos = pos;
	UI_view2d_scale_get(v2d, &userdata.xscale, &userdata.yscale);
	clip_graph_tracking_values_iterate(sc,
	                                   (sc->flag & SC_SHOW_GRAPH_SEL_ONLY) != 0,
	                                   (sc->flag & SC_SHOW_GRAPH_HIDDEN) != 0,
	                                   &userdata, tracking_segment_knot_cb, NULL, NULL);
	/* draw graph lines */
	GPU_blend(true);
	clip_graph_tracking_values_iterate(sc,
	                                   (sc->flag & SC_SHOW_GRAPH_SEL_ONLY) != 0,
	                                   (sc->flag & SC_SHOW_GRAPH_HIDDEN) != 0,
	                                   &userdata, tracking_segment_point_cb, tracking_segment_start_cb,
	                                   tracking_segment_end_cb);
	GPU_blend(false);

	/* selected knot handles on top of curves */
	userdata.sel = true;
	clip_graph_tracking_values_iterate(sc,
	                                   (sc->flag & SC_SHOW_GRAPH_SEL_ONLY) != 0,
	                                   (sc->flag & SC_SHOW_GRAPH_HIDDEN) != 0,
	                                   &userdata, tracking_segment_knot_cb, NULL, NULL);
}

typedef struct TrackErrorCurveUserData {
	MovieClip *clip;
	MovieTracking *tracking;
	MovieTrackingObject *tracking_object;
	MovieTrackingTrack *active_track;
	bool matrix_initialized;
	int matrix_frame;
	float projection_matrix[4][4];
	int width, height;
	float aspy;
	unsigned int pos;
} TrackErrorCurveUserData;

static void tracking_error_segment_point_cb(void *userdata,
                                            MovieTrackingTrack *track, MovieTrackingMarker *marker,
                                            int coord, int scene_framenr, float UNUSED(value))
{
	if (coord == 1) {
		TrackErrorCurveUserData *data = (TrackErrorCurveUserData *) userdata;
		float reprojected_position[4], bundle_position[4], marker_position[2], delta[2];
		float reprojection_error;
		float weight = BKE_tracking_track_get_weight_for_marker(data->clip, track, marker);

		if (!data->matrix_initialized || data->matrix_frame != scene_framenr) {
			BKE_tracking_get_projection_matrix(data->tracking, data->tracking_object,
			                                   scene_framenr, data->width, data->height,
			                                   data->projection_matrix);
		}

		copy_v3_v3(bundle_position, track->bundle_pos);
		bundle_position[3] = 1;

		mul_v4_m4v4(reprojected_position, data->projection_matrix, bundle_position);
		reprojected_position[0] = (reprojected_position[0] /
		                          (reprojected_position[3] * 2.0f) + 0.5f) * data->width;
		reprojected_position[1] = (reprojected_position[1] /
		                          (reprojected_position[3] * 2.0f) + 0.5f) * data->height * data->aspy;

		BKE_tracking_distort_v2(data->tracking, reprojected_position, reprojected_position);

		marker_position[0] = (marker->pos[0] + track->offset[0]) * data->width;
		marker_position[1] = (marker->pos[1] + track->offset[1]) * data->height * data->aspy;

		sub_v2_v2v2(delta, reprojected_position, marker_position);
		reprojection_error = len_v2(delta) * weight;

		immVertex2f(data->pos, scene_framenr, reprojection_error);
	}
}

static void tracking_error_segment_start_cb(void *userdata, MovieTrackingTrack *track, int coord, bool is_point)
{
	if (coord == 1) {
		TrackErrorCurveUserData *data = (TrackErrorCurveUserData *) userdata;
		float col[4] = {0.0f, 0.0f, 1.0f, 1.0f};

		if (track == data->active_track) {
			col[3] = 1.0f;
			GPU_line_width(2.0f);
		}
		else {
			col[3] = 0.5f;
			GPU_line_width(1.0f);
		}

		immUniformColor4fv(col);

		if (is_point) { /* This probably never happens here, but just in case... */
			immBeginAtMost(GPU_PRIM_POINTS, 1);
		}
		else {
			/* Graph can be composed of smaller segments, if any marker is disabled */
			immBeginAtMost(GPU_PRIM_LINE_STRIP, track->markersnr);
		}
	}
}

static void tracking_error_segment_end_cb(void *UNUSED(userdata), int coord)
{
	if (coord == 1) {
		immEnd();
	}
}

static void draw_tracks_error_curves(SpaceClip *sc, unsigned int pos)
{
	MovieClip *clip = ED_space_clip_get_clip(sc);
	MovieTracking *tracking = &clip->tracking;
	TrackErrorCurveUserData data;

	data.clip = clip;
	data.tracking = tracking;
	data.tracking_object = BKE_tracking_object_get_active(tracking);
	data.active_track = BKE_tracking_track_get_active(tracking);
	data.matrix_initialized = false;
	data.pos = pos;
	BKE_movieclip_get_size(clip, &sc->user, &data.width, &data.height);
	data.aspy = 1.0f / tracking->camera.pixel_aspect;

	if (!data.width || !data.height) {
		return;
	}

	clip_graph_tracking_values_iterate(sc,
	                                   (sc->flag & SC_SHOW_GRAPH_SEL_ONLY) != 0,
	                                   (sc->flag & SC_SHOW_GRAPH_HIDDEN) != 0,
	                                   &data,
	                                   tracking_error_segment_point_cb,
	                                   tracking_error_segment_start_cb,
	                                   tracking_error_segment_end_cb);
}

static void draw_frame_curves(SpaceClip *sc, unsigned int pos)
{
	MovieClip *clip = ED_space_clip_get_clip(sc);
	MovieTracking *tracking = &clip->tracking;
	MovieTrackingReconstruction *reconstruction = BKE_tracking_get_active_reconstruction(tracking);
	int i, lines = 0, prevfra = 0;

	immUniformColor3f(0.0f, 0.0f, 1.0f);

	for (i = 0; i < reconstruction->camnr; i++) {
		MovieReconstructedCamera *camera = &reconstruction->cameras[i];
		int framenr;

		if (lines && camera->framenr != prevfra + 1) {
			immEnd();
			lines = 0;
		}

		if (!lines) {
			immBeginAtMost(GPU_PRIM_LINE_STRIP, reconstruction->camnr);
			lines = 1;
		}

		framenr = BKE_movieclip_remap_clip_to_scene_frame(clip, camera->framenr);
		immVertex2f(pos, framenr, camera->error);

		prevfra = camera->framenr;
	}

	if (lines) {
		immEnd();
	}
}

void clip_draw_graph(SpaceClip *sc, ARegion *ar, Scene *scene)
{
	MovieClip *clip = ED_space_clip_get_clip(sc);
	View2D *v2d = &ar->v2d;
	View2DGrid *grid;
	short unitx = V2D_UNIT_FRAMESCALE, unity = V2D_UNIT_VALUES;

	/* grid */
	grid = UI_view2d_grid_calc(scene, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP, ar->winx, ar->winy);
	UI_view2d_grid_draw(v2d, grid, V2D_GRIDLINES_ALL);
	UI_view2d_grid_free(grid);

	if (clip) {
		uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
		immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);

		GPU_point_size(3.0f);

		if (sc->flag & SC_SHOW_GRAPH_TRACKS_MOTION) {
			draw_tracks_motion_curves(v2d, sc, pos);
		}

		if (sc->flag & SC_SHOW_GRAPH_TRACKS_ERROR) {
			draw_tracks_error_curves(sc, pos);
		}

		if (sc->flag & SC_SHOW_GRAPH_FRAMES) {
			draw_frame_curves(sc, pos);
		}

		immUnbindProgram();
	}

	/* frame range */
	clip_draw_sfra_efra(v2d, scene);
}