Blender  V3.3
tracking_detect.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2011 Blender Foundation. All rights reserved. */
3 
10 #include "DNA_gpencil_types.h"
11 #include "DNA_movieclip_types.h"
12 #include "DNA_object_types.h" /* SELECT */
13 
14 #include "BLI_utildefines.h"
15 
16 #include "BKE_tracking.h"
17 
18 #include "IMB_imbuf_types.h"
19 
20 #include "libmv-capi.h"
21 
22 /* Check whether point is inside grease pencil stroke. */
23 static bool check_point_in_stroke(bGPDstroke *stroke, float x, float y)
24 {
25  int count = 0;
26  bGPDspoint *points = stroke->points;
27 
28  /* Count intersections of horizontal ray coming from the point.
29  * Point will be inside layer if and only if number of intersection
30  * is uneven.
31  *
32  * Well, if layer has got self-intersections, this logic wouldn't
33  * work, but such situation is crappy anyway.
34  */
35 
36  for (int i = 0, prev = stroke->totpoints - 1; i < stroke->totpoints; prev = i, i++) {
37  if ((points[i].y < y && points[prev].y >= y) || (points[prev].y < y && points[i].y >= y)) {
38  float fac = (y - points[i].y) / (points[prev].y - points[i].y);
39 
40  if (points[i].x + fac * (points[prev].x - points[i].x) < x) {
41  count++;
42  }
43  }
44  }
45 
46  return (count % 2) ? true : false;
47 }
48 
49 /* Check whether point is inside any stroke of grease pencil layer. */
50 static bool check_point_in_layer(bGPDlayer *layer, float x, float y)
51 {
52  bGPDframe *frame = layer->frames.first;
53 
54  while (frame) {
55  bGPDstroke *stroke = frame->strokes.first;
56 
57  while (stroke) {
58  if (check_point_in_stroke(stroke, x, y)) {
59  return true;
60  }
61 
62  stroke = stroke->next;
63  }
64  frame = frame->next;
65  }
66 
67  return false;
68 }
69 
70 /* Get features detected by libmv and create tracks on the clip for them. */
72  ListBase *tracksbase,
73  struct libmv_Features *features,
74  int framenr,
75  int width,
76  int height,
77  bGPDlayer *layer,
78  bool place_outside_layer)
79 {
80  int a;
81 
82  a = libmv_countFeatures(features);
83  while (a--) {
84  MovieTrackingTrack *track;
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) {
102  track = BKE_tracking_track_add(tracking, tracksbase, xu, yu, framenr, width, height);
103  track->flag |= SELECT;
104  track->pat_flag |= SELECT;
105  track->search_flag |= SELECT;
106  }
107  }
108 }
109 
111  ListBase *tracksbase,
112  ImBuf *ibuf,
113  int framenr,
114  bGPDlayer *layer,
115  bool place_outside_layer,
117 {
118  struct libmv_Features *features = NULL;
119 
120  if (ibuf->rect_float) {
121  features = libmv_detectFeaturesFloat(ibuf->rect_float, ibuf->x, ibuf->y, 4, options);
122  }
123  else if (ibuf->rect) {
124  features = libmv_detectFeaturesByte((unsigned char *)ibuf->rect, ibuf->x, ibuf->y, 4, options);
125  }
126 
127  if (features != NULL) {
129  tracking, tracksbase, features, framenr, ibuf->x, ibuf->y, layer, place_outside_layer);
130 
132  }
133 }
134 
136  ListBase *tracksbase,
137  ImBuf *ibuf,
138  int framenr,
139  int margin,
140  int min_trackness,
141  int min_distance,
142  bGPDlayer *layer,
143  bool place_outside_layer)
144 {
146 
147  options.detector = LIBMV_DETECTOR_FAST;
148  options.margin = margin;
149  options.min_distance = min_distance;
150  options.fast_min_trackness = min_trackness;
151 
153  tracking, tracksbase, ibuf, framenr, layer, place_outside_layer, &options);
154 }
155 
157  ListBase *tracksbase,
158  ImBuf *ibuf,
159  int framenr,
160  int margin,
161  float threshold,
162  int min_distance,
163  bGPDlayer *layer,
164  bool place_outside_layer)
165 {
167 
168  options.detector = LIBMV_DETECTOR_HARRIS;
169  options.margin = margin;
170  options.min_distance = min_distance;
171  options.harris_threshold = threshold;
172 
174  tracking, tracksbase, ibuf, framenr, layer, place_outside_layer, &options);
175 }
struct MovieTrackingTrack * BKE_tracking_track_add(struct MovieTracking *tracking, struct ListBase *tracksbase, float x, float y, int framenr, int width, int height)
Definition: tracking.c:535
Object is a sort of wrapper for general info.
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei height
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei width
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
libmv_Features * libmv_detectFeaturesByte(const unsigned char *image_buffer, int width, int height, int channels, libmv_DetectOptions *options)
Definition: detector.cc:59
void libmv_getFeature(const libmv_Features *libmv_features, int number, double *x, double *y, double *score, double *size)
Definition: detector.cc:114
libmv_Features * libmv_detectFeaturesFloat(const float *image_buffer, int width, int height, int channels, libmv_DetectOptions *options)
Definition: detector.cc:81
void libmv_featuresDestroy(libmv_Features *libmv_features)
Definition: detector.cc:103
int libmv_countFeatures(const libmv_Features *libmv_features)
Definition: detector.cc:110
@ LIBMV_DETECTOR_FAST
Definition: detector.h:14
@ LIBMV_DETECTOR_HARRIS
Definition: detector.h:16
int count
ccl_gpu_kernel_postfix ccl_global float int int int int float threshold
static unsigned a[3]
Definition: RandGen.cpp:78
SymEdge< T > * prev(const SymEdge< T > *se)
Definition: delaunay_2d.cc:105
unsigned int * rect
float * rect_float
void * first
Definition: DNA_listBase.h:31
struct bGPDframe * next
ListBase strokes
ListBase frames
bGPDspoint * points
struct bGPDstroke * next
Feature * features
Definition: detector.cc:16
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 void detect_retrieve_libmv_features(MovieTracking *tracking, ListBase *tracksbase, struct libmv_Features *features, int framenr, int width, int height, 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)