Blender V4.3
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
12#include "DNA_movieclip_types.h"
13#include "DNA_object_types.h" /* SELECT */
14
15#include "BLI_utildefines.h"
16
17#include "BKE_tracking.h"
18
19#include "IMB_imbuf_types.hh"
20
21#include "libmv-capi.h"
22
23/* Check whether point is inside grease pencil stroke. */
24static bool check_point_in_stroke(bGPDstroke *stroke, float x, float y)
25{
26 int count = 0;
27 bGPDspoint *points = stroke->points;
28
29 /* Count intersections of horizontal ray coming from the point.
30 * Point will be inside layer if and only if number of intersection
31 * is uneven.
32 *
33 * Well, if layer has got self-intersections, this logic wouldn't
34 * work, but such situation is crappy anyway.
35 */
36
37 for (int i = 0, prev = stroke->totpoints - 1; i < stroke->totpoints; prev = i, i++) {
38 if ((points[i].y < y && points[prev].y >= y) || (points[prev].y < y && points[i].y >= y)) {
39 float fac = (y - points[i].y) / (points[prev].y - points[i].y);
40
41 if (points[i].x + fac * (points[prev].x - points[i].x) < x) {
42 count++;
43 }
44 }
45 }
46
47 return (count % 2) ? true : false;
48}
49
50/* Check whether point is inside any stroke of grease pencil layer. */
51static bool check_point_in_layer(bGPDlayer *layer, float x, float y)
52{
53 bGPDframe *frame = static_cast<bGPDframe *>(layer->frames.first);
54
55 while (frame) {
56 bGPDstroke *stroke = static_cast<bGPDstroke *>(frame->strokes.first);
57
58 while (stroke) {
59 if (check_point_in_stroke(stroke, x, y)) {
60 return true;
61 }
62
63 stroke = stroke->next;
64 }
65 frame = frame->next;
66 }
67
68 return false;
69}
70
71/* Get features detected by libmv and create tracks on the clip for them. */
73 ListBase *tracksbase,
74 libmv_Features *features,
75 int framenr,
76 int width,
77 int height,
78 bGPDlayer *layer,
79 bool place_outside_layer)
80{
81 int a;
82
83 a = libmv_countFeatures(features);
84 while (a--) {
85 double x, y, size, score;
86 bool ok = true;
87 float xu, yu;
88
89 libmv_getFeature(features, a, &x, &y, &score, &size);
90
91 /* In Libmv integer coordinate points to pixel center, in blender
92 * it's not. Need to add 0.5px offset to center.
93 */
94 xu = (x + 0.5) / width;
95 yu = (y + 0.5) / height;
96
97 if (layer) {
98 ok = check_point_in_layer(layer, xu, yu) != place_outside_layer;
99 }
100
101 if (ok) {
103 tracking, tracksbase, xu, yu, framenr, width, height);
104 track->flag |= SELECT;
105 track->pat_flag |= SELECT;
106 track->search_flag |= SELECT;
107 }
108 }
109}
110
112 ListBase *tracksbase,
113 ImBuf *ibuf,
114 int framenr,
115 bGPDlayer *layer,
116 bool place_outside_layer,
118{
119 libmv_Features *features = nullptr;
120
121 if (ibuf->float_buffer.data) {
122 features = libmv_detectFeaturesFloat(ibuf->float_buffer.data, ibuf->x, ibuf->y, 4, options);
123 }
124 else if (ibuf->byte_buffer.data) {
125 features = libmv_detectFeaturesByte(ibuf->byte_buffer.data, ibuf->x, ibuf->y, 4, options);
126 }
127
128 if (features != nullptr) {
130 tracking, tracksbase, features, framenr, ibuf->x, ibuf->y, layer, place_outside_layer);
131
132 libmv_featuresDestroy(features);
133 }
134}
135
137 ListBase *tracksbase,
138 ImBuf *ibuf,
139 int framenr,
140 int margin,
141 int min_trackness,
142 int min_distance,
143 bGPDlayer *layer,
144 bool place_outside_layer)
145{
147
148 options.detector = LIBMV_DETECTOR_FAST;
149 options.margin = margin;
150 options.min_distance = min_distance;
151 options.fast_min_trackness = min_trackness;
152
154 tracking, tracksbase, ibuf, framenr, layer, place_outside_layer, &options);
155}
156
158 ListBase *tracksbase,
159 ImBuf *ibuf,
160 int framenr,
161 int margin,
162 float threshold,
163 int min_distance,
164 bGPDlayer *layer,
165 bool place_outside_layer)
166{
168
170 options.margin = margin;
171 options.min_distance = min_distance;
172 options.harris_threshold = threshold;
173
175 tracking, tracksbase, ibuf, framenr, layer, place_outside_layer, &options);
176}
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:525
Object is a sort of wrapper for general info.
Contains defines and structs used throughout the imbuf module.
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
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)