@@ -142,14 +142,14 @@ KeyFrameBBox::KeyFrameBBox() : delta_x(0.0), delta_y(0.0), scale_x(1.0), scale_y
142142}
143143
144144// Add a BBox to the BoxVec map
145- void KeyFrameBBox::AddBox (int64_t _frame_num, float _x1 , float _y1 , float _width, float _height)
145+ void KeyFrameBBox::AddBox (int64_t _frame_num, float _cx , float _cy , float _width, float _height, float _angle )
146146{
147147 // Check if the given frame number is valid
148148 if (_frame_num < 0 )
149149 return ;
150150
151151 // Instantiate a new bounding-box
152- BBox newBBox = BBox (_x1, _y1 , _width, _height);
152+ BBox newBBox = BBox (_cx, _cy , _width, _height, _angle );
153153
154154 // Get the time of given frame
155155 double time = this ->FrameNToTime (_frame_num, 1.0 );
@@ -231,25 +231,12 @@ BBox KeyFrameBBox::GetValue(int64_t frame_number)
231231 BBox currentBBox = currentBBoxIterator->second ;
232232
233233 // Adjust the BBox properties by the Keyframes values
234- currentBBox.x1 += this ->delta_x .GetValue (frame_number);
235- currentBBox.y1 += this ->delta_y .GetValue (frame_number);
234+ currentBBox.cx += this ->delta_x .GetValue (frame_number);
235+ currentBBox.cy += this ->delta_y .GetValue (frame_number);
236236 currentBBox.width *= this ->scale_x .GetValue (frame_number);
237237 currentBBox.height *= this ->scale_y .GetValue (frame_number);
238+ currentBBox.angle += this ->rotation .GetValue (frame_number);
238239
239- /* TODO - Add rotation
240- (x1,y1) -> current point
241- (x1',y1') -> transformed point
242- (xc, yc) -> center of the BBox
243- (xc, yc) = (x1 + w/2, y1 + h/2)
244- rot -> rotation angle [radians]
245- x1' = xc + (x1 - xc)*cos(rot) - (y1-yc)*sin(rot)
246- y1' = yc + (x1 - xc)*sin(rot) + (y1-yc)*cos(rot)
247- ***
248- x1' = (x1 + w/2) + (-w/2)*cos(rot) - (-h/2)*sin(rot)
249- y1' = (y1 + h/2) + (-w/2)*sin(rot) + (-h/2)*cos(rot)
250- currentBBox.x1 += currentBBox.width/2 - (currentBBox.width/2)*cos(rot*PI/180) + (currentBBox.height)*sin(rot*PI/180);
251- currentBBox.y1 += currentBBox.height/2 - (currentBBox.width/2)*sin(rot*PI/180) - (currentBBox.height)*cos(rot*PI/180);
252- */
253240 return currentBBox;
254241 }
255242
@@ -263,37 +250,47 @@ BBox KeyFrameBBox::GetValue(int64_t frame_number)
263250 previousBBox, currentBBox, time);
264251
265252 // Adjust the BBox properties by the Keyframes values
266- interpolatedBBox.x1 += this ->delta_x .GetValue (frame_number);
267- interpolatedBBox.y1 += this ->delta_y .GetValue (frame_number);
253+ interpolatedBBox.cx += this ->delta_x .GetValue (frame_number);
254+ interpolatedBBox.cy += this ->delta_y .GetValue (frame_number);
268255 interpolatedBBox.width *= this ->scale_x .GetValue (frame_number);
269256 interpolatedBBox.height *= this ->scale_y .GetValue (frame_number);
257+ interpolatedBBox.angle += this ->rotation .GetValue (frame_number);
270258
271259 return interpolatedBBox;
272260}
273261
274262// Interpolate the bouding-boxes properties
275263BBox KeyFrameBBox::InterpolateBoxes (double t1, double t2, BBox left, BBox right, double target)
276264{
277-
278- Point p1_left (t1, left.x1 , openshot::InterpolationType::LINEAR);
279- Point p1_right (t2, right.x1 , openshot::InterpolationType::LINEAR);
280- Point p1 = InterpolateBetween (p1_left, p1_right, target, 0.01 );
281-
282- Point p2_left (t1, left.y1 , openshot::InterpolationType::LINEAR);
283- Point p2_right (t2, right.y1 , openshot::InterpolationType::LINEAR);
284- Point p2 = InterpolateBetween (p2_left, p2_right, target, 0.01 );
285-
286- Point p3_left (t1, left.height , openshot::InterpolationType::LINEAR);
287- Point p3_right (t2, right.height , openshot::InterpolationType::LINEAR);
288- Point p3 = InterpolateBetween (p3_left, p3_right, target, 0.01 );
289-
290- Point p4_left (t1, left.width , openshot::InterpolationType::LINEAR);
291- Point p4_right (t2, right.width , openshot::InterpolationType::LINEAR);
292- Point p4 = InterpolateBetween (p4_left, p4_right, target, 0.01 );
293-
294- BBox ans (p1.co .Y , p2.co .Y , p4.co .Y , p3.co .Y );
295-
296- return ans;
265+ // Interpolate the x-coordinate of the center point
266+ Point cx_left (t1, left.cx , openshot::InterpolationType::LINEAR);
267+ Point cx_right (t2, right.cx , openshot::InterpolationType::LINEAR);
268+ Point cx = InterpolateBetween (cx_left, cx_right, target, 0.01 );
269+
270+ // Interpolate de y-coordinate of the center point
271+ Point cy_left (t1, left.cy , openshot::InterpolationType::LINEAR);
272+ Point cy_right (t2, right.cy , openshot::InterpolationType::LINEAR);
273+ Point cy = InterpolateBetween (cy_left, cy_right, target, 0.01 );
274+
275+ // Interpolate the width
276+ Point width_left (t1, left.width , openshot::InterpolationType::LINEAR);
277+ Point width_right (t2, right.width , openshot::InterpolationType::LINEAR);
278+ Point width = InterpolateBetween (width_left, width_right, target, 0.01 );
279+
280+ // Interpolate the height
281+ Point height_left (t1, left.height , openshot::InterpolationType::LINEAR);
282+ Point height_right (t2, right.height , openshot::InterpolationType::LINEAR);
283+ Point height = InterpolateBetween (height_left, height_right, target, 0.01 );
284+
285+ // Interpolate the rotation angle
286+ Point angle_left (t1, left.angle , openshot::InterpolationType::LINEAR);
287+ Point angle_right (t1, right.angle , openshot::InterpolationType::LINEAR);
288+ Point angle = InterpolateBetween (angle_left, angle_right, target, 0.01 );
289+
290+ // Create a bounding box with the interpolated points
291+ BBox interpolatedBox (cx.co .Y , cy.co .Y , width.co .Y , height.co .Y , angle.co .Y );
292+
293+ return interpolatedBox;
297294}
298295
299296// Update object's BaseFps
@@ -348,16 +345,18 @@ bool KeyFrameBBox::LoadBoxData(std::string inputFilePath)
348345
349346 // Get bounding box data from current frame
350347 const libopenshottracker::Frame::Box &box = pbFrameData.bounding_box ();
351-
352- float x1 = box.x1 ();
353- float y1 = box.y1 ();
354- float x2 = box.x2 ();
355- float y2 = box.y2 ();
356348
357- if ( (x1 >= 0.0 ) && (y1 >= 0.0 ) && (x2 >= 0.0 ) && (y2 >= 0.0 ) )
349+ float width = box.x2 () - box.x1 ();
350+ float height = box.y2 () - box.y1 ();
351+ float cx = box.x1 () + width/2 ;
352+ float cy = box.y1 () + height/2 ;
353+ float angle = 0.0 ;
354+
355+
356+ if ( (cx >= 0.0 ) && (cy >= 0.0 ) && (width >= 0.0 ) && (height >= 0.0 ) )
358357 {
359358 // The bounding-box properties are valid, so add it to the BoxVec map
360- this ->AddBox (frame_number, x1, y1, (x2 - x1), (y2 - y1) );
359+ this ->AddBox (frame_number, cx, cy, width, height, angle );
361360 }
362361 }
363362
@@ -464,7 +463,6 @@ void KeyFrameBBox::SetJsonValue(const Json::Value root)
464463 // Insert BBox into the BoxVec map
465464 BBox box;
466465 box.SetJsonValue (existing_point[" data" ]);
467- // BoxVec.insert({existing_point["time"].asDouble(), box});
468466 BoxVec[existing_point[" time" ].asDouble ()] = box;
469467 }
470468 }
@@ -482,4 +480,4 @@ void KeyFrameBBox::SetJsonValue(const Json::Value root)
482480 rotation.SetJsonValue (root[" rotation" ]);
483481
484482 return ;
485- }
483+ }
0 commit comments