make Node.links return a tuple, this may you can't do socket.links.append() by mistake.
[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 extern "C" {
33         #include "BKE_movieclip.h"
34         #include "BKE_tracking.h"
35 }
36
37 TrackPositionOperation::TrackPositionOperation() : NodeOperation()
38 {
39         this->addOutputSocket(COM_DT_VALUE);
40         this->m_movieClip = NULL;
41         this->m_framenumber = 0;
42         this->m_trackingObjectName[0] = 0;
43         this->m_trackName[0] = 0;
44         this->m_axis = 0;
45         this->m_position = POSITION_ABSOLUTE;
46         this->m_relativeFrame = 0;
47 }
48
49 void TrackPositionOperation::initExecution()
50 {
51         MovieTracking *tracking = NULL;
52         MovieClipUser user = {0};
53         MovieTrackingObject *object;
54
55         zero_v2(this->m_markerPos);
56         zero_v2(this->m_relativePos);
57
58         if (!this->m_movieClip)
59                 return;
60
61         tracking = &this->m_movieClip->tracking;
62
63         BKE_movieclip_user_set_frame(&user, this->m_framenumber);
64         BKE_movieclip_get_size(this->m_movieClip, &user, &this->m_width, &this->m_height);
65
66         object = BKE_tracking_object_get_named(tracking, this->m_trackingObjectName);
67         if (object) {
68                 MovieTrackingTrack *track;
69
70                 track = BKE_tracking_track_get_named(tracking, object, this->m_trackName);
71
72                 if (track) {
73                         MovieTrackingMarker *marker;
74                         int clip_framenr = BKE_movieclip_remap_scene_to_clip_frame(this->m_movieClip, this->m_framenumber);
75
76                         marker = BKE_tracking_marker_get(track, clip_framenr);
77
78                         copy_v2_v2(this->m_markerPos, marker->pos);
79
80                         if (this->m_position == POSITION_RELATIVE_START) {
81                                 int i;
82
83                                 for (i = 0; i < track->markersnr; i++) {
84                                         marker = &track->markers[i];
85
86                                         if ((marker->flag & MARKER_DISABLED) == 0) {
87                                                 copy_v2_v2(this->m_relativePos, marker->pos);
88
89                                                 break;
90                                         }
91                                 }
92                         }
93                         else if (this->m_position == POSITION_RELATIVE_FRAME) {
94                                 int relative_clip_framenr = BKE_movieclip_remap_scene_to_clip_frame(this->m_movieClip,
95                                                                                                     this->m_relativeFrame);
96
97                                 marker = BKE_tracking_marker_get(track, relative_clip_framenr);
98                                 copy_v2_v2(this->m_relativePos, marker->pos);
99                         }
100                 }
101         }
102 }
103
104 void TrackPositionOperation::executePixel(float output[4], float x, float y, PixelSampler sampler)
105 {
106         output[0] = this->m_markerPos[this->m_axis] - this->m_relativePos[this->m_axis];
107
108         if (this->m_axis == 0)
109                 output[0] *= this->m_width;
110         else
111                 output[0] *= this->m_height;
112 }
113
114 void TrackPositionOperation::determineResolution(unsigned int resolution[2], unsigned int preferredResolution[2])
115 {
116         resolution[0] = preferredResolution[0];
117         resolution[1] = preferredResolution[1];
118 }