Skip to content

Commit 620e894

Browse files
committed
Fixing parentClipObject and parentTrackedObject transform, so the parent clip can be scaled and moved without breaking the tracking. Also refactoring out unneeded complex code left over.
1 parent 3351b52 commit 620e894

File tree

7 files changed

+41
-159
lines changed

7 files changed

+41
-159
lines changed

src/Clip.cpp

Lines changed: 35 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -789,38 +789,8 @@ std::string Clip::PropertiesJSON(int64_t requested_frame) const {
789789
root["waveform"]["choices"].append(add_property_choice_json("Yes", true, waveform));
790790
root["waveform"]["choices"].append(add_property_choice_json("No", false, waveform));
791791

792-
// Add the parentTrackedObject's properties
793-
if (parentTrackedObject && parentClipObject)
794-
{
795-
// Convert Clip's frame position to Timeline's frame position
796-
long clip_start_position = round(Position() * info.fps.ToDouble()) + 1;
797-
long clip_start_frame = (Start() * info.fps.ToDouble()) + 1;
798-
double timeline_frame_number = requested_frame + clip_start_position - clip_start_frame;
799-
800-
// Get attached object's parent clip properties
801-
std::map< std::string, float > trackedObjectParentClipProperties = parentTrackedObject->GetParentClipProperties(timeline_frame_number);
802-
double parentObject_frame_number = trackedObjectParentClipProperties["frame_number"];
803-
// Get attached object properties
804-
std::map< std::string, float > trackedObjectProperties = parentTrackedObject->GetBoxValues(parentObject_frame_number);
805-
806-
// Correct the parent Tracked Object properties by the clip's reference system
807-
float parentObject_location_x = trackedObjectProperties["cx"] - 0.5 + trackedObjectParentClipProperties["cx"];
808-
float parentObject_location_y = trackedObjectProperties["cy"] - 0.5 + trackedObjectParentClipProperties["cy"];
809-
float parentObject_scale_x = trackedObjectProperties["w"]*trackedObjectProperties["sx"];
810-
float parentObject_scale_y = trackedObjectProperties["h"]*trackedObjectProperties["sy"];
811-
float parentObject_rotation = trackedObjectProperties["r"] + trackedObjectParentClipProperties["r"];
812-
813-
// Add the parent Tracked Object properties to JSON
814-
root["location_x"] = add_property_json("Location X", parentObject_location_x, "float", "", &location_x, -1.0, 1.0, false, requested_frame);
815-
root["location_y"] = add_property_json("Location Y", parentObject_location_y, "float", "", &location_y, -1.0, 1.0, false, requested_frame);
816-
root["scale_x"] = add_property_json("Scale X", parentObject_scale_x, "float", "", &scale_x, 0.0, 1.0, false, requested_frame);
817-
root["scale_y"] = add_property_json("Scale Y", parentObject_scale_y, "float", "", &scale_y, 0.0, 1.0, false, requested_frame);
818-
root["rotation"] = add_property_json("Rotation", parentObject_rotation, "float", "", &rotation, -360, 360, false, requested_frame);
819-
root["shear_x"] = add_property_json("Shear X", shear_x.GetValue(requested_frame), "float", "", &shear_x, -1.0, 1.0, false, requested_frame);
820-
root["shear_y"] = add_property_json("Shear Y", shear_y.GetValue(requested_frame), "float", "", &shear_y, -1.0, 1.0, false, requested_frame);
821-
}
822792
// Add the parentClipObject's properties
823-
else if (parentClipObject)
793+
if (parentClipObject)
824794
{
825795
// Convert Clip's frame position to Timeline's frame position
826796
long clip_start_position = round(Position() * info.fps.ToDouble()) + 1;
@@ -1345,7 +1315,7 @@ void Clip::apply_waveform(std::shared_ptr<Frame> frame, QSize timeline_size) {
13451315
frame->AddImage(source_image);
13461316
}
13471317

1348-
// Apply keyframes to the source frame (if any)
1318+
// Get QTransform from keyframes
13491319
QTransform Clip::get_transform(std::shared_ptr<Frame> frame, int width, int height)
13501320
{
13511321
// Get image from clip
@@ -1440,62 +1410,41 @@ QTransform Clip::get_transform(std::shared_ptr<Frame> frame, int width, int heig
14401410

14411411
// Get the parentClipObject properties
14421412
if (parentClipObject){
1443-
1444-
// Convert Clip's frame position to Timeline's frame position
1445-
long clip_start_position = round(Position() * info.fps.ToDouble()) + 1;
1446-
long clip_start_frame = (Start() * info.fps.ToDouble()) + 1;
1447-
double timeline_frame_number = frame->number + clip_start_position - clip_start_frame;
1413+
// Get the start trim position of the parent clip
1414+
long parent_start_offset = parentClipObject->Start() * info.fps.ToDouble();
1415+
long parent_frame_number = frame->number + parent_start_offset;
14481416

14491417
// Get parent object's properties (Clip)
1450-
parentObject_location_x = parentClipObject->location_x.GetValue(timeline_frame_number);
1451-
parentObject_location_y = parentClipObject->location_y.GetValue(timeline_frame_number);
1452-
parentObject_scale_x = parentClipObject->scale_x.GetValue(timeline_frame_number);
1453-
parentObject_scale_y = parentClipObject->scale_y.GetValue(timeline_frame_number);
1454-
parentObject_shear_x = parentClipObject->shear_x.GetValue(timeline_frame_number);
1455-
parentObject_shear_y = parentClipObject->shear_y.GetValue(timeline_frame_number);
1456-
parentObject_rotation = parentClipObject->rotation.GetValue(timeline_frame_number);
1418+
parentObject_location_x = parentClipObject->location_x.GetValue(parent_frame_number);
1419+
parentObject_location_y = parentClipObject->location_y.GetValue(parent_frame_number);
1420+
parentObject_scale_x = parentClipObject->scale_x.GetValue(parent_frame_number);
1421+
parentObject_scale_y = parentClipObject->scale_y.GetValue(parent_frame_number);
1422+
parentObject_shear_x = parentClipObject->shear_x.GetValue(parent_frame_number);
1423+
parentObject_shear_y = parentClipObject->shear_y.GetValue(parent_frame_number);
1424+
parentObject_rotation = parentClipObject->rotation.GetValue(parent_frame_number);
14571425
}
14581426

1459-
// Get the parentTrackedObject properties
1460-
if (parentTrackedObject){
1461-
// Convert Clip's frame position to Timeline's frame position
1462-
long clip_start_position = round(Position() * info.fps.ToDouble()) + 1;
1463-
long clip_start_frame = (Start() * info.fps.ToDouble()) + 1;
1464-
double timeline_frame_number = frame->number + clip_start_position - clip_start_frame;
1465-
1466-
// Get parentTrackedObject's parent clip's properties
1467-
std::map<std::string, float> trackedObjectParentClipProperties =
1468-
parentTrackedObject->GetParentClipProperties(timeline_frame_number);
1469-
1470-
// Get the attached object's parent clip's properties
1471-
if (!trackedObjectParentClipProperties.empty())
1472-
{
1473-
// Get parent object's properties (Tracked Object)
1474-
float parentObject_frame_number = trackedObjectParentClipProperties["frame_number"];
1475-
1476-
// Access the parentTrackedObject's properties
1477-
std::map<std::string, float> trackedObjectProperties = parentTrackedObject->GetBoxValues(parentObject_frame_number);
1478-
1479-
// Get the Tracked Object's properties and correct them by the clip's reference system
1480-
parentObject_location_x = trackedObjectProperties["cx"] - 0.5 + trackedObjectParentClipProperties["location_x"];
1481-
parentObject_location_y = trackedObjectProperties["cy"] - 0.5 + trackedObjectParentClipProperties["location_y"];
1482-
parentObject_scale_x = trackedObjectProperties["w"]*trackedObjectProperties["sx"];
1483-
parentObject_scale_y = trackedObjectProperties["h"]*trackedObjectProperties["sy"];
1484-
parentObject_rotation = trackedObjectProperties["r"] + trackedObjectParentClipProperties["rotation"];
1485-
}
1486-
else
1487-
{
1488-
// Access the parentTrackedObject's properties
1489-
std::map<std::string, float> trackedObjectProperties = parentTrackedObject->GetBoxValues(timeline_frame_number);
1490-
1491-
// Get the Tracked Object's properties and correct them by the clip's reference system
1492-
parentObject_location_x = trackedObjectProperties["cx"] - 0.5;
1493-
parentObject_location_y = trackedObjectProperties["cy"] - 0.5;
1494-
parentObject_scale_x = trackedObjectProperties["w"]*trackedObjectProperties["sx"];
1495-
parentObject_scale_y = trackedObjectProperties["h"]*trackedObjectProperties["sy"];
1496-
parentObject_rotation = trackedObjectProperties["r"];
1497-
}
1498-
}
1427+
// Get the parentTrackedObject properties
1428+
if (parentTrackedObject){
1429+
// Get the attached object's parent clip's properties
1430+
Clip* parentClip = (Clip*) parentTrackedObject->ParentClip();
1431+
if (parentClip)
1432+
{
1433+
// Get the start trim position of the parent clip
1434+
long parent_start_offset = parentClip->Start() * info.fps.ToDouble();
1435+
long parent_frame_number = frame->number + parent_start_offset;
1436+
1437+
// Access the parentTrackedObject's properties
1438+
std::map<std::string, float> trackedObjectProperties = parentTrackedObject->GetBoxValues(parent_frame_number);
1439+
1440+
// Get the Tracked Object's properties and correct them by the clip's reference system
1441+
parentObject_location_x = parentClip->location_x.GetValue(parent_frame_number) + ((trackedObjectProperties["cx"] - 0.5) * parentClip->scale_x.GetValue(parent_frame_number));
1442+
parentObject_location_y = parentClip->location_y.GetValue(parent_frame_number) + ((trackedObjectProperties["cy"] - 0.5) * parentClip->scale_y.GetValue(parent_frame_number));
1443+
parentObject_scale_x = trackedObjectProperties["w"] * trackedObjectProperties["sx"] * parentClip->scale_x.GetValue(parent_frame_number);
1444+
parentObject_scale_y = trackedObjectProperties["h"] * trackedObjectProperties["sy"] * parentClip->scale_y.GetValue(parent_frame_number);
1445+
parentObject_rotation = trackedObjectProperties["r"] + parentClip->rotation.GetValue(parent_frame_number);
1446+
}
1447+
}
14991448

15001449
/* GRAVITY LOCATION - Initialize X & Y to the correct values (before applying location curves) */
15011450
float x = 0.0; // left
@@ -1561,8 +1510,8 @@ QTransform Clip::get_transform(std::shared_ptr<Frame> frame, int width, int heig
15611510

15621511
/* LOCATION, ROTATION, AND SCALE */
15631512
float r = rotation.GetValue(frame->number) + parentObject_rotation; // rotate in degrees
1564-
x += (width * (location_x.GetValue(frame->number) + parentObject_location_x )); // move in percentage of final width
1565-
y += (height * (location_y.GetValue(frame->number) + parentObject_location_y )); // move in percentage of final height
1513+
x += width * (location_x.GetValue(frame->number) + parentObject_location_x); // move in percentage of final width
1514+
y += height * (location_y.GetValue(frame->number) + parentObject_location_y); // move in percentage of final height
15661515
float shear_x_value = shear_x.GetValue(frame->number) + parentObject_shear_x;
15671516
float shear_y_value = shear_y.GetValue(frame->number) + parentObject_shear_y;
15681517
float origin_x_value = origin_x.GetValue(frame->number);

src/TrackedObjectBBox.cpp

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -522,36 +522,3 @@ std::map<std::string, float> TrackedObjectBBox::GetBoxValues(int64_t frame_numbe
522522

523523
return boxValues;
524524
}
525-
526-
// Return a map that contains the properties of this object's parent clip
527-
std::map<std::string, float> TrackedObjectBBox::GetParentClipProperties(int64_t frame_number) const {
528-
529-
// Get the parent clip of this object as a Clip pointer
530-
Clip* parentClip = (Clip *) ParentClip();
531-
532-
// Calculate parentClip's frame number
533-
long parentClip_start_position = round( parentClip->Position() * parentClip->info.fps.ToDouble() ) + 1;
534-
long parentClip_start_frame = ( parentClip->Start() * parentClip->info.fps.ToDouble() ) + 1;
535-
float parentClip_frame_number = round(frame_number - parentClip_start_position) + parentClip_start_frame;
536-
537-
// Get parentClip's Keyframes
538-
float parentClip_location_x = parentClip->location_x.GetValue(parentClip_frame_number);
539-
float parentClip_location_y = parentClip->location_y.GetValue(parentClip_frame_number);
540-
float parentClip_scale_x = parentClip->scale_x.GetValue(parentClip_frame_number);
541-
float parentClip_scale_y = parentClip->scale_y.GetValue(parentClip_frame_number);
542-
float parentClip_rotation = parentClip->rotation.GetValue(parentClip_frame_number);
543-
544-
// Initialize the parent clip properties map
545-
std::map<std::string, float> parentClipProperties;
546-
547-
// Set the map properties
548-
parentClipProperties["frame_number"] = parentClip_frame_number;
549-
parentClipProperties["timeline_frame_number"] = frame_number;
550-
parentClipProperties["location_x"] = parentClip_location_x;
551-
parentClipProperties["location_y"] = parentClip_location_y;
552-
parentClipProperties["scale_x"] = parentClip_scale_x;
553-
parentClipProperties["scale_y"] = parentClip_scale_y;
554-
parentClipProperties["rotation"] = parentClip_rotation;
555-
556-
return parentClipProperties;
557-
}

src/TrackedObjectBBox.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,6 @@ namespace openshot
211211

212212
/// Return a map that contains the bounding box properties and it's keyframes indexed by their names
213213
std::map<std::string, float> GetBoxValues(int64_t frame_number) const override;
214-
/// Return a map that contains the properties of this object's parent clip
215-
std::map<std::string, float> GetParentClipProperties(int64_t frame_number) const override;
216-
217214
};
218215
} // namespace openshot
219216

src/TrackedObjectBase.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ namespace openshot {
6666
virtual void ScalePoints(double scale) { return; };
6767
/// Return the main properties of a TrackedObjectBBox instance - such as position, size and rotation
6868
virtual std::map<std::string, float> GetBoxValues(int64_t frame_number) const { std::map<std::string, float> ret; return ret; };
69-
/// Return the main properties of the tracked object's parent clip - such as position, size and rotation
70-
virtual std::map<std::string, float> GetParentClipProperties(int64_t frame_number) const { std::map<std::string, float> ret; return ret; }
7169
/// Add a bounding box to the tracked object's BoxVec map
7270
virtual void AddBox(int64_t _frame_num, float _cx, float _cy, float _width, float _height, float _angle) { return; };
7371

src/effects/ObjectDetection.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ std::shared_ptr<Frame> ObjectDetection::GetFrame(std::shared_ptr<Frame> frame, i
8282
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
8383

8484
if (detectionsData.find(frame_number) != detectionsData.end()) {
85-
float fw = frame_image->width();
86-
float fh = frame_image->height();
87-
8885
DetectionData detections = detectionsData[frame_number];
8986
for (int i = 0; i < detections.boxes.size(); i++) {
9087
if (detections.confidences.at(i) < confidence_threshold ||
@@ -99,13 +96,13 @@ std::shared_ptr<Frame> ObjectDetection::GetFrame(std::shared_ptr<Frame> frame, i
9996
if (trackedObject_it != trackedObjects.end()) {
10097
std::shared_ptr<TrackedObjectBBox> trackedObject = std::static_pointer_cast<TrackedObjectBBox>(trackedObject_it->second);
10198

102-
if (trackedObject->Contains(frame_number) && trackedObject->visible.GetValue(frame_number) == 1) {
99+
Clip* parentClip = (Clip*) trackedObject->ParentClip();
100+
if (parentClip && trackedObject->Contains(frame_number) && trackedObject->visible.GetValue(frame_number) == 1) {
103101
BBox trackedBox = trackedObject->GetBox(frame_number);
104-
105-
QRectF boxRect((trackedBox.cx - trackedBox.width / 2) * fw,
106-
(trackedBox.cy - trackedBox.height / 2) * fh,
107-
trackedBox.width * fw,
108-
trackedBox.height * fh);
102+
QRectF boxRect((trackedBox.cx - trackedBox.width / 2) * frame_image->width(),
103+
(trackedBox.cy - trackedBox.height / 2) * frame_image->height(),
104+
trackedBox.width * frame_image->width(),
105+
trackedBox.height * frame_image->height());
109106

110107
if (trackedObject->draw_box.GetValue(frame_number) == 1) {
111108
// Draw bounding box

src/effects/Tracker.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -135,29 +135,6 @@ std::shared_ptr<Frame> Tracker::GetFrame(std::shared_ptr<Frame> frame, int64_t f
135135
return frame;
136136
}
137137

138-
QRectF Tracker::scaleAndCenterRect(const QRectF& sourceRect, const QRectF& targetRect) {
139-
float sourceAspectRatio = sourceRect.width() / sourceRect.height();
140-
float targetWidth = targetRect.width();
141-
float targetHeight = targetRect.height();
142-
float newWidth, newHeight;
143-
144-
if (sourceAspectRatio > targetRect.width() / targetRect.height()) {
145-
// Source is wider relative to target, so it's constrained by target's width
146-
newWidth = targetWidth;
147-
newHeight = newWidth / sourceAspectRatio;
148-
} else {
149-
// Source is taller relative to target, so it's constrained by target's height
150-
newHeight = targetHeight;
151-
newWidth = newHeight * sourceAspectRatio;
152-
}
153-
154-
// Center the new rectangle within the target rectangle
155-
float newX = targetRect.left() + (targetWidth - newWidth) / 2.0;
156-
float newY = targetRect.top() + (targetHeight - newHeight) / 2.0;
157-
158-
return QRectF(newX, newY, newWidth, newHeight);
159-
}
160-
161138
// Get the indexes and IDs of all visible objects in the given frame
162139
std::string Tracker::GetVisibleObjects(int64_t frame_number) const{
163140

src/effects/Tracker.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ namespace openshot
7171
/// Get the indexes and IDs of all visible objects in the given frame
7272
std::string GetVisibleObjects(int64_t frame_number) const override;
7373

74-
/// Find a rectangle inside another (centered)
75-
static QRectF scaleAndCenterRect(const QRectF& sourceRect, const QRectF& targetRect);
76-
7774
// Get and Set JSON methods
7875

7976
/// Generate JSON string of this object

0 commit comments

Comments
 (0)