Blender V5.0
tracking_detect.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
12#include "DNA_movieclip_types.h"
13#include "DNA_object_types.h" /* SELECT */
14
15#include "BKE_tracking.h"
16
17#include "IMB_imbuf_types.hh"
18
19#include "libmv-capi.h"
20
21/* Check whether point is inside grease pencil stroke. */
22static bool check_point_in_stroke(bGPDstroke *stroke, float x, float y)
23{
24 int count = 0;
25 bGPDspoint *points = stroke->points;
26
27 /* Count intersections of horizontal ray coming from the point.
28 * Point will be inside layer if and only if number of intersection
29 * is uneven.
30 *
31 * Well, if layer has got self-intersections, this logic wouldn't
32 * work, but such situation is crappy anyway.
33 */
34
35 for (int i = 0, prev = stroke->totpoints - 1; i < stroke->totpoints; prev = i, i++) {
36 if ((points[i].y < y && points[prev].y >= y) || (points[prev].y < y && points[i].y >= y)) {
37 float fac = (y - points[i].y) / (points[prev].y - points[i].y);
38
39 if (points[i].x + fac * (points[prev].x - points[i].x) < x) {
40 count++;
41 }
42 }
43 }
44
45 return (count % 2) ? true : false;
46}
47
48/* Check whether point is inside any stroke of grease pencil layer. */
49static bool check_point_in_layer(bGPDlayer *layer, float x, float y)
50{
51 bGPDframe *frame = static_cast<bGPDframe *>(layer->frames.first);
52
53 while (frame) {
54 bGPDstroke *stroke = static_cast<bGPDstroke *>(frame->strokes.first);
55
56 while (stroke) {
57 if (check_point_in_stroke(stroke, x, y)) {
58 return true;
59 }
60
61 stroke = stroke->next;
62 }
63 frame = frame->next;
64 }
65
66 return false;
67}
68
69/* Get features detected by libmv and create tracks on the clip for them. */
71 ListBase *tracksbase,
72 libmv_Features *features,
73 int framenr,
74 int width,
75 int height,
76 bGPDlayer *layer,
77 bool place_outside_layer)
78{
79 int a;
80
81 a = libmv_countFeatures(features);
82 while (a--) {
83 double x, y, size, score;
84 bool ok = true;
85 float xu, yu;
86
87 libmv_getFeature(features, a, &x, &y, &score, &size);
88
89 /* In Libmv integer coordinate points to pixel center, in blender
90 * it's not. Need to add 0.5px offset to center.
91 */
92 xu = (x + 0.5) / width;
93 yu = (y + 0.5) / height;
94
95 if (layer) {
96 ok = check_point_in_layer(layer, xu, yu) != place_outside_layer;
97 }
98
99 if (ok) {
101 tracking, tracksbase, xu, yu, framenr, width, height);
102 track->flag |= SELECT;
103 track->pat_flag |= SELECT;
104 track->search_flag |= SELECT;
105 }
106 }
107}
108
110 ListBase *tracksbase,
111 ImBuf *ibuf,
112 int framenr,
113 bGPDlayer *layer,
114 bool place_outside_layer,
116{
117 libmv_Features *features = nullptr;
118
119 if (ibuf->float_buffer.data) {
120 features = libmv_detectFeaturesFloat(ibuf->float_buffer.data, ibuf->x, ibuf->y, 4, options);
121 }
122 else if (ibuf->byte_buffer.data) {
123 features = libmv_detectFeaturesByte(ibuf->byte_buffer.data, ibuf->x, ibuf->y, 4, options);
124 }
125
126 if (features != nullptr) {
128 tracking, tracksbase, features, framenr, ibuf->x, ibuf->y, layer, place_outside_layer);
129
130 libmv_featuresDestroy(features);
131 }
132}
133
135 ListBase *tracksbase,
136 ImBuf *ibuf,
137 int framenr,
138 int margin,
139 int min_trackness,
140 int min_distance,
141 bGPDlayer *layer,
142 bool place_outside_layer)
143{
145
146 options.detector = LIBMV_DETECTOR_FAST;
147 options.margin = margin;
148 options.min_distance = min_distance;
149 options.fast_min_trackness = min_trackness;
150
152 tracking, tracksbase, ibuf, framenr, layer, place_outside_layer, &options);
153}
154
156 ListBase *tracksbase,
157 ImBuf *ibuf,
158 int framenr,
159 int margin,
160 float threshold,
161 int min_distance,
162 bGPDlayer *layer,
163 bool place_outside_layer)
164{
166
168 options.margin = margin;
169 options.min_distance = min_distance;
170 options.harris_threshold = threshold;
171
173 tracking, tracksbase, ibuf, framenr, layer, place_outside_layer, &options);
174}
struct MovieTrackingTrack * BKE_tracking_track_add(struct MovieTracking *tracking, struct ListBase *tracksbase, float x, float y, int framenr, int width, int height)
Definition tracking.cc:526
Object is a sort of wrapper for general info.
return true
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
#define SELECT
CCL_NAMESPACE_BEGIN struct Options options
void libmv_getFeature(const libmv_Features *libmv_features, int number, double *x, double *y, double *score, double *size)
Definition detector.cc:115
libmv_Features * libmv_detectFeaturesByte(const unsigned char *image_buffer, int width, int height, int channels, libmv_DetectOptions *options)
Definition detector.cc:60
void libmv_featuresDestroy(libmv_Features *libmv_features)
Definition detector.cc:104
libmv_Features * libmv_detectFeaturesFloat(const float *image_buffer, int width, int height, int channels, libmv_DetectOptions *options)
Definition detector.cc:82
int libmv_countFeatures(const libmv_Features *libmv_features)
Definition detector.cc:111
@ LIBMV_DETECTOR_FAST
Definition detector.h:15
@ LIBMV_DETECTOR_HARRIS
Definition detector.h:17
int count
ImBufFloatBuffer float_buffer
ImBufByteBuffer byte_buffer
void * first
struct bGPDframe * next
struct bGPDstroke * next
i
Definition text_draw.cc:230
static bool check_point_in_stroke(bGPDstroke *stroke, float x, float y)
static void run_configured_detector(MovieTracking *tracking, ListBase *tracksbase, ImBuf *ibuf, int framenr, bGPDlayer *layer, bool place_outside_layer, libmv_DetectOptions *options)
void BKE_tracking_detect_fast(MovieTracking *tracking, ListBase *tracksbase, ImBuf *ibuf, int framenr, int margin, int min_trackness, int min_distance, bGPDlayer *layer, bool place_outside_layer)
static bool check_point_in_layer(bGPDlayer *layer, float x, float y)
void BKE_tracking_detect_harris(MovieTracking *tracking, ListBase *tracksbase, ImBuf *ibuf, int framenr, int margin, float threshold, int min_distance, bGPDlayer *layer, bool place_outside_layer)
static void detect_retrieve_libmv_features(MovieTracking *tracking, ListBase *tracksbase, libmv_Features *features, int framenr, int width, int height, bGPDlayer *layer, bool place_outside_layer)