Fix unworkable track position node
[blender.git] / source / blender / compositor / operations / COM_TrackPositionOperation.cpp
1 /*
2  * Copyright 2012, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor:
19  *              Jeroen Bakker
20  *              Monique Dewanchand
21  *              Sergey Sharybin
22  */
23
24 #include "COM_TrackPositionOperation.h"
25
26 #include "MEM_guardedalloc.h"
27
28 #include "BLI_listbase.h"
29 #include "BLI_math.h"
30 #include "BLI_math_color.h"
31
32 #include "DNA_scene_types.h"
33
34 extern "C" {
35         #include "BKE_movieclip.h"
36         #include "BKE_tracking.h"
37 }
38
39 TrackPositionOperation::TrackPositionOperation() : NodeOperation()
40 {
41         this->addOutputSocket(COM_DT_VALUE);
42         this->movieClip = NULL;
43         this->framenumber = 0;
44         this->trackingObject[0] = 0;
45         this->trackName[0] = 0;
46         this->axis = 0;
47         this->relative = false;
48 }
49
50 void TrackPositionOperation::executePixel(float *outputValue, float x, float y, PixelSampler sampler)
51 {
52         MovieClipUser user = {0};
53         MovieTracking *tracking = &movieClip->tracking;
54         MovieTrackingObject *object = BKE_tracking_object_get_named(tracking, this->trackingObject);
55         MovieTrackingTrack *track;
56         MovieTrackingMarker *marker;
57         int width, height;
58
59         outputValue[0] = 0.0f;
60
61         if (!object)
62                 return;
63
64         track = BKE_tracking_track_get_named(tracking, object, this->trackName);
65
66         if (!track)
67                 return;
68
69         BKE_movieclip_user_set_frame(&user, this->framenumber);
70         BKE_movieclip_get_size(this->movieClip, &user, &width, &height);
71
72         marker = BKE_tracking_marker_get(track, this->framenumber);
73
74         outputValue[0] = marker->pos[this->axis];
75
76         if (this->relative) {
77                 int i;
78
79                 for (i = 0; i < track->markersnr; i++) {
80                         marker = &track->markers[i];
81
82                         if ((marker->flag & MARKER_DISABLED) == 0) {
83                                 outputValue[0] -= marker->pos[this->axis];
84
85                                 break;
86                         }
87                 }
88         }
89
90         if (this->axis == 0)
91                 outputValue[0] *= width;
92         else
93                 outputValue[0] *= height;
94 }
95
96 void TrackPositionOperation::determineResolution(unsigned int resolution[], unsigned int preferredResolution[])
97 {
98         resolution[0] = preferredResolution[0];
99         resolution[1] = preferredResolution[1];
100 }