From a5feadce1e13eddfe23aa1238bdbb1ab4eb2f081 Mon Sep 17 00:00:00 2001 From: Brenno Date: Mon, 18 Jan 2021 14:52:01 -0300 Subject: [PATCH] Changed KeyframeBBox to TrackedObjectBBox and added TrackedObjectBase Added new base class TrackedObjectBase, changed class KeyframeBBox to TrackedObjectBBox and changes it's inheritance from KeyframeBase to TrackedObjectBase. --- src/CMakeLists.txt | 3 +- src/Clip.cpp | 17 ++-- src/Clip.h | 8 +- src/KeyFrameBase.cpp | 6 +- src/KeyFrameBase.h | 14 +-- src/OpenShot.h | 2 +- src/Timeline.cpp | 19 ++-- src/Timeline.h | 16 +-- src/TimelineBase.cpp | 1 - ...KeyFrameBBox.cpp => TrackedObjectBBox.cpp} | 76 ++++++++++----- src/{KeyFrameBBox.h => TrackedObjectBBox.h} | 18 ++-- src/TrackedObjectBase.cpp | 46 +++++++++ src/TrackedObjectBase.h | 97 +++++++++++++++++++ src/effects/Tracker.cpp | 16 +-- src/effects/Tracker.h | 4 +- tests/KeyFrame_Tests.cpp | 40 ++++---- 16 files changed, 278 insertions(+), 105 deletions(-) rename src/{KeyFrameBBox.cpp => TrackedObjectBBox.cpp} (82%) rename src/{KeyFrameBBox.h => TrackedObjectBBox.h} (94%) create mode 100644 src/TrackedObjectBase.cpp create mode 100644 src/TrackedObjectBase.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0fc36752..adb7351a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -87,7 +87,8 @@ set(OPENSHOT_SOURCES Json.cpp KeyFrame.cpp KeyFrameBase.cpp - KeyFrameBBox.cpp + TrackedObjectBase.cpp + TrackedObjectBBox.cpp OpenShotVersion.cpp ZmqLogger.cpp PlayerBase.cpp diff --git a/src/Clip.cpp b/src/Clip.cpp index a5e3702d..341e111c 100644 --- a/src/Clip.cpp +++ b/src/Clip.cpp @@ -251,7 +251,7 @@ void Clip::AttachToTracker(std::string tracked_id) Timeline *parentTimeline = (Timeline *) ParentTimeline(); // Create a smart pointer to the tracked object from the timeline - std::shared_ptr trackedObject = parentTimeline->GetTrackedObject(tracked_id); + std::shared_ptr trackedObject = parentTimeline->GetTrackedObject(tracked_id); // Check for valid tracked object if (trackedObject){ @@ -264,7 +264,7 @@ void Clip::AttachToTracker(std::string tracked_id) } // Set the pointer to the trackedObject this clip is attached to -void Clip::SetAttachedObject(std::shared_ptr trackedObject){ +void Clip::SetAttachedObject(std::shared_ptr trackedObject){ attachedObject = trackedObject; return; } @@ -1142,7 +1142,7 @@ void Clip::AddEffect(EffectBase* effect) Tracker* tracker = (Tracker *) effect; // Get tracked data from the Tracker effect - std::shared_ptr trackedData = tracker->trackedData; + std::shared_ptr trackedData = tracker->trackedData; // Add tracked data to the timeline parentTimeline->AddTrackedObject(trackedData); @@ -1279,13 +1279,18 @@ void Clip::apply_keyframes(std::shared_ptr frame, int width, int height) long clip_start_frame = (Start() * info.fps.ToDouble()) + 1; double timeline_frame_number = frame->number + clip_start_position - clip_start_frame; - // Access the KeyframeBBox properties - std::map boxValues = attachedObject->GetBoxValues(timeline_frame_number); + // Get attachedObject's parent clip frame number + Timeline* parentTimeline = (Timeline *) ParentTimeline(); + double attachedObjectParentClip_frame_number = timeline_frame_number - clip_start_position + clip_start_frame; + + // Access the TrackedObjectBBox properties + std::map boxValues = attachedObject->GetBoxValues(attachedObjectParentClip_frame_number); // Get the bounding-box values and correct them by the clip's reference system box_cx = boxValues["cx"] - 0.5; box_cy = boxValues["cy"] - 0.5; - box_sx = boxValues["w"]*boxValues["sx"]*2.0; + // box_sx = boxValues["w"]*boxValues["sx"]*2.0; + box_sx = boxValues["w"]*boxValues["sx"]; box_sy = boxValues["h"]*boxValues["sy"]; box_r = boxValues["r"]; } diff --git a/src/Clip.h b/src/Clip.h index 0886f789..8b31405a 100644 --- a/src/Clip.h +++ b/src/Clip.h @@ -54,7 +54,7 @@ #include "Fraction.h" #include "Frame.h" #include "KeyFrame.h" -#include "KeyFrameBase.h" +#include "TrackedObjectBase.h" #include "ReaderBase.h" #include "JuceHeader.h" @@ -125,7 +125,7 @@ namespace openshot { std::list effects; /// Is Reader opened std::string attached_id; ///< Id of the bounding box that this clip is attached to - std::shared_ptr attachedObject; + std::shared_ptr attachedObject; // Audio resampler (if time mapping) openshot::AudioResampler *resampler; @@ -206,9 +206,9 @@ namespace openshot { void AttachToTracker(std::string tracked_id); /// Set the pointer to the trackedObject this clip is attached to - void SetAttachedObject(std::shared_ptr trackedObject); + void SetAttachedObject(std::shared_ptr trackedObject); /// Return a pointer to the trackedObject this clip is attached to - std::shared_ptr GetAttachedObject() const { return attachedObject; }; + std::shared_ptr GetAttachedObject() const { return attachedObject; }; /// Return the type name of the class std::string Name() override { return "Clip"; }; diff --git a/src/KeyFrameBase.cpp b/src/KeyFrameBase.cpp index acdf389f..79f4f528 100644 --- a/src/KeyFrameBase.cpp +++ b/src/KeyFrameBase.cpp @@ -98,10 +98,6 @@ namespace openshot{ } KeyframeBase::KeyframeBase(){ - id = "TESTBASEID"; + } - - KeyframeBase::KeyframeBase(std::string _id){ - SetId(_id); - } } \ No newline at end of file diff --git a/src/KeyFrameBase.h b/src/KeyFrameBase.h index 316ed83a..6a55ea5e 100644 --- a/src/KeyFrameBase.h +++ b/src/KeyFrameBase.h @@ -43,6 +43,7 @@ #include "Point.h" #include "Json.h" + namespace openshot { /** * @brief This abstract class is the base class of all Keyframes. @@ -71,27 +72,16 @@ namespace openshot { // int64_t SearchBetweenPoints(Point const & left, Point const & right, int64_t const current, Check check); class KeyframeBase{ - private: - std::string id; public: - + /// Blank constructor KeyframeBase(); - /// Default constructor - KeyframeBase(std::string _id); - - std::string Id() const { return id; } - void SetId(std::string _id) { id = _id; } - /// Scale all points by a percentage (good for evenly lengthening or shortening an openshot::Keyframe) /// 1.0 = same size, 1.05 = 5% increase, etc... virtual void ScalePoints(double scale) { return; }; - /// Return the main properties of a KeyframeBBox instance using a pointer to this base class - virtual std::map GetBoxValues(int64_t frame_number) { std::map ret; return ret; }; - }; } // Namespace openshot diff --git a/src/OpenShot.h b/src/OpenShot.h index 1cb8700a..561c1bed 100644 --- a/src/OpenShot.h +++ b/src/OpenShot.h @@ -132,7 +132,7 @@ #include "TextReader.h" #endif #include "KeyFrame.h" -#include "KeyFrameBBox.h" +#include "TrackedObjectBBox.h" #include "PlayerBase.h" #include "Point.h" #include "Profiles.h" diff --git a/src/Timeline.cpp b/src/Timeline.cpp index 3956dab2..4c4be0c0 100644 --- a/src/Timeline.cpp +++ b/src/Timeline.cpp @@ -240,8 +240,8 @@ Timeline::~Timeline() { } } -// Add to the tracked_objects map a pointer to a tracked object (KeyframeBBox) -void Timeline::AddTrackedObject(std::shared_ptr trackedObject){ +// Add to the tracked_objects map a pointer to a tracked object (TrackedObjectBBox) +void Timeline::AddTrackedObject(std::shared_ptr trackedObject){ // Search for the tracked object on the map auto iterator = tracked_objects.find(trackedObject->Id()); @@ -254,18 +254,19 @@ void Timeline::AddTrackedObject(std::shared_ptr trackedO // Tracked object's id not present -> insert it on the map tracked_objects[trackedObject->Id()] = trackedObject; } + return; } // Return tracked object pointer by it's id -std::shared_ptr Timeline::GetTrackedObject(std::string id) const{ +std::shared_ptr Timeline::GetTrackedObject(std::string id) const{ // Search for the tracked object on the map auto iterator = tracked_objects.find(id); if (iterator != tracked_objects.end()){ // Id found, return the pointer to the tracked object - std::shared_ptr trackedObject = iterator->second; + std::shared_ptr trackedObject = iterator->second; return trackedObject; } else { @@ -274,15 +275,15 @@ std::shared_ptr Timeline::GetTrackedObject(std::string i } } -// Return the ID's of the tracked objects as a vector of strings -std::vector Timeline::GetTrackedObjectsIds() const{ +// Return the ID's of the tracked objects as a list of strings +std::list Timeline::GetTrackedObjectsIds() const{ - // Create a vector of strings - std::vector trackedObjects_ids; + // Create a list of strings + std::list trackedObjects_ids; // Iterate through the tracked_objects map for (auto const& it: tracked_objects){ - // Add the IDs to the vector + // Add the IDs to the list trackedObjects_ids.push_back(it.first); } diff --git a/src/Timeline.h b/src/Timeline.h index d5d1bda9..69cebc8a 100644 --- a/src/Timeline.h +++ b/src/Timeline.h @@ -52,8 +52,8 @@ #include "Frame.h" #include "FrameMapper.h" #include "KeyFrame.h" -#include "KeyFrameBBox.h" -#include "KeyFrameBase.h" +#include "TrackedObjectBBox.h" +#include "TrackedObjectBase.h" #include "OpenMPUtilities.h" #include "ReaderBase.h" #include "Settings.h" @@ -179,7 +179,7 @@ namespace openshot { bool managed_cache; ///< Does this timeline instance manage the cache object std::string path; ///< Optional path of loaded UTF-8 OpenShot JSON project file std::mutex get_frame_mutex; ///< Mutex to protect GetFrame method from different threads calling it - std::map> tracked_objects; ///< map of KeyframeBBoxes and their IDs + std::map> tracked_objects; ///< map of TrackedObjectBBoxes and their IDs /// Process a new layer of video or audio void add_layer(std::shared_ptr new_frame, openshot::Clip* source_clip, int64_t clip_frame_number, int64_t timeline_frame_number, bool is_top_clip, float max_volume); @@ -244,12 +244,12 @@ namespace openshot { virtual ~Timeline(); - /// Add to the tracked_objects map a pointer to a tracked object (KeyframeBBox) - void AddTrackedObject(std::shared_ptr trackedObject); + /// Add to the tracked_objects map a pointer to a tracked object (TrackedObjectBBox) + void AddTrackedObject(std::shared_ptr trackedObject); /// Return tracked object pointer by it's id - std::shared_ptr GetTrackedObject(std::string id) const; - /// Return the ID's of the tracked objects as a vector of strings - std::vector GetTrackedObjectsIds() const; + std::shared_ptr GetTrackedObject(std::string id) const; + /// Return the ID's of the tracked objects as a list of strings + std::list GetTrackedObjectsIds() const; /// @brief Add an openshot::Clip to the timeline /// @param clip Add an openshot::Clip to the timeline. A clip can contain any type of Reader. diff --git a/src/TimelineBase.cpp b/src/TimelineBase.cpp index 6170bff2..120de01b 100644 --- a/src/TimelineBase.cpp +++ b/src/TimelineBase.cpp @@ -45,6 +45,5 @@ TimelineBase::TimelineBase() * the TimelineBase class */ void TimelineBase::Clips(int test){ - std::cout << test << std::endl; return; } \ No newline at end of file diff --git a/src/KeyFrameBBox.cpp b/src/TrackedObjectBBox.cpp similarity index 82% rename from src/KeyFrameBBox.cpp rename to src/TrackedObjectBBox.cpp index 2a1155a8..119ec083 100644 --- a/src/KeyFrameBBox.cpp +++ b/src/TrackedObjectBBox.cpp @@ -28,7 +28,8 @@ * along with OpenShot Library. If not, see . */ -#include "KeyFrameBBox.h" +#include "TrackedObjectBBox.h" +#include "Clip.h" #include #include #include @@ -39,14 +40,14 @@ using namespace openshot; // Default Constructor that sets the bounding-box displacement as 0 and the scales as 1 for the first frame -KeyFrameBBox::KeyFrameBBox() : delta_x(0.0), delta_y(0.0), scale_x(1.0), scale_y(1.0), rotation(0.0) +TrackedObjectBBox::TrackedObjectBBox() : delta_x(0.0), delta_y(0.0), scale_x(1.0), scale_y(1.0), rotation(0.0) { this->TimeScale = 1.0; return; } // Add a BBox to the BoxVec map -void KeyFrameBBox::AddBox(int64_t _frame_num, float _cx, float _cy, float _width, float _height, float _angle) +void TrackedObjectBBox::AddBox(int64_t _frame_num, float _cx, float _cy, float _width, float _height, float _angle) { // Check if the given frame number is valid if (_frame_num < 0) @@ -73,7 +74,7 @@ void KeyFrameBBox::AddBox(int64_t _frame_num, float _cx, float _cy, float _width } // Get the size of BoxVec map -int64_t KeyFrameBBox::GetLength() const +int64_t TrackedObjectBBox::GetLength() const { if (BoxVec.empty()) return 0; @@ -83,7 +84,7 @@ int64_t KeyFrameBBox::GetLength() const } // Check if there is a bounding-box in the given frame -bool KeyFrameBBox::Contains(int64_t frame_num) +bool TrackedObjectBBox::Contains(int64_t frame_num) { // Get the time of given frame double time = this->FrameNToTime(frame_num, 1.0); @@ -96,7 +97,7 @@ bool KeyFrameBBox::Contains(int64_t frame_num) } // Remove a bounding-box from the BoxVec map -void KeyFrameBBox::RemoveBox(int64_t frame_number) +void TrackedObjectBBox::RemoveBox(int64_t frame_number) { // Get the time of given frame double time = this->FrameNToTime(frame_number, 1.0); @@ -111,7 +112,7 @@ void KeyFrameBBox::RemoveBox(int64_t frame_number) } // Return a bounding-box from BoxVec with it's properties adjusted by the Keyframes -BBox KeyFrameBBox::GetBox(int64_t frame_number) +BBox TrackedObjectBBox::GetBox(int64_t frame_number) { // Get the time position of the given frame. double time = this->FrameNToTime(frame_number, this->TimeScale); @@ -164,7 +165,7 @@ BBox KeyFrameBBox::GetBox(int64_t frame_number) } // Interpolate the bouding-boxes properties -BBox KeyFrameBBox::InterpolateBoxes(double t1, double t2, BBox left, BBox right, double target) +BBox TrackedObjectBBox::InterpolateBoxes(double t1, double t2, BBox left, BBox right, double target) { // Interpolate the x-coordinate of the center point Point cx_left(t1, left.cx, openshot::InterpolationType::LINEAR); @@ -198,30 +199,30 @@ BBox KeyFrameBBox::InterpolateBoxes(double t1, double t2, BBox left, BBox right, } // Update object's BaseFps -void KeyFrameBBox::SetBaseFPS(Fraction fps){ +void TrackedObjectBBox::SetBaseFPS(Fraction fps){ this->BaseFps = fps; return; } // Return the object's BaseFps -Fraction KeyFrameBBox::GetBaseFPS(){ +Fraction TrackedObjectBBox::GetBaseFPS(){ return BaseFps; } // Get the time of the given frame -double KeyFrameBBox::FrameNToTime(int64_t frame_number, double time_scale){ +double TrackedObjectBBox::FrameNToTime(int64_t frame_number, double time_scale){ double time = ((double)frame_number) * this->BaseFps.Reciprocal().ToDouble() * (1.0 / time_scale); return time; } // Update the TimeScale member variable -void KeyFrameBBox::ScalePoints(double time_scale){ +void TrackedObjectBBox::ScalePoints(double time_scale){ this->TimeScale = time_scale; } // Load the bounding-boxes information from the protobuf file -bool KeyFrameBBox::LoadBoxData(std::string inputFilePath) +bool TrackedObjectBBox::LoadBoxData(std::string inputFilePath) { // Variable to hold the loaded data pb_tracker::Tracker bboxMessage; @@ -277,20 +278,20 @@ bool KeyFrameBBox::LoadBoxData(std::string inputFilePath) } // Clear the BoxVec map -void KeyFrameBBox::clear() +void TrackedObjectBBox::clear() { BoxVec.clear(); } // Generate JSON string of this object -std::string KeyFrameBBox::Json() const +std::string TrackedObjectBBox::Json() const { // Return formatted string return JsonValue().toStyledString(); } // Generate Json::Value for this object -Json::Value KeyFrameBBox::JsonValue() const +Json::Value TrackedObjectBBox::JsonValue() const { // Create root json object Json::Value root; @@ -313,7 +314,7 @@ Json::Value KeyFrameBBox::JsonValue() const } // Load JSON string into this object -void KeyFrameBBox::SetJson(const std::string value) +void TrackedObjectBBox::SetJson(const std::string value) { // Parse JSON string into JSON objects try @@ -331,12 +332,12 @@ void KeyFrameBBox::SetJson(const std::string value) } // Load Json::Value into this object -void KeyFrameBBox::SetJsonValue(const Json::Value root) +void TrackedObjectBBox::SetJsonValue(const Json::Value root) { // Set the Id if (!root["box_id"].isNull()) - SetId(root["box_id"].asString()); + Id(root["box_id"].asString()); // Set the BaseFps by the given JSON object if (!root["BaseFPS"].isNull() && root["BaseFPS"].isObject()) @@ -374,7 +375,7 @@ void KeyFrameBBox::SetJsonValue(const Json::Value root) // Get all properties for a specific frame (perfect for a UI to display the current state // of all properties at any time) -Json::Value KeyFrameBBox::PropertiesJSON(int64_t requested_frame) const +Json::Value TrackedObjectBBox::PropertiesJSON(int64_t requested_frame) const { Json::Value root; @@ -402,7 +403,7 @@ Json::Value KeyFrameBBox::PropertiesJSON(int64_t requested_frame) const // Generate JSON for a property -Json::Value KeyFrameBBox::add_property_json(std::string name, float value, std::string type, std::string memo, const Keyframe* keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame) const { +Json::Value TrackedObjectBBox::add_property_json(std::string name, float value, std::string type, std::string memo, const Keyframe* keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame) const { // Requested Point const Point requested_point(requested_frame, requested_frame); @@ -439,7 +440,7 @@ Json::Value KeyFrameBBox::add_property_json(std::string name, float value, std:: } // Return the bounding box properties and it's keyframes indexed by their names -std::map KeyFrameBBox::GetBoxValues(int64_t frame_number){ +std::map TrackedObjectBBox::GetBoxValues(int64_t frame_number){ // Create the map std::map boxValues; @@ -463,4 +464,35 @@ std::map KeyFrameBBox::GetBoxValues(int64_t frame_number){ return boxValues; +} + +// Return properties of this object's parent clip +std::map TrackedObjectBBox::GetParentClipProperties(int64_t frame_number){ + + // Get the parent clip of this object as a Clip pointer + Clip* parentClip = (Clip *) ParentClip(); + + // Calculate parentClip's frame number + long parentClip_start_position = round( parentClip->Position() * parentClip->info.fps.ToDouble() ) + 1; + long parentClip_start_frame = ( parentClip->Start() * parentClip->info.fps.ToDouble() ) + 1; + float parentClip_frame_number = frame_number - parentClip_start_position + parentClip_start_frame; + + // Get parentClip's Keyframes + float parentClip_location_x = parentClip->location_x.GetValue(parentClip_frame_number); + float parentClip_location_y = parentClip->location_y.GetValue(parentClip_frame_number); + float parentClip_scale_x = parentClip->scale_x.GetValue(parentClip_frame_number); + float parentClip_scale_y = parentClip->scale_y.GetValue(parentClip_frame_number); + float parentClip_rotation = parentClip->rotation.GetValue(parentClip_frame_number); + + std::map parentClipProperties; + + parentClipProperties["frame_number"] = parentClip_frame_number; + parentClipProperties["timeline_frame_number"] = frame_number; + parentClipProperties["location_x"] = parentClip_location_x; + parentClipProperties["location_y"] = parentClip_location_y; + parentClipProperties["scale_x"] = parentClip_scale_x; + parentClipProperties["scale_y"] = parentClip_scale_y; + parentClipProperties["rotation"] = parentClip_rotation; + + return parentClipProperties; } \ No newline at end of file diff --git a/src/KeyFrameBBox.h b/src/TrackedObjectBBox.h similarity index 94% rename from src/KeyFrameBBox.h rename to src/TrackedObjectBBox.h index b3f44606..3e05529d 100644 --- a/src/KeyFrameBBox.h +++ b/src/TrackedObjectBBox.h @@ -1,6 +1,6 @@ /** * @file - * @brief Header file for the KeyFrameBBox class + * @brief Header file for the TrackedObjectBBox class * @author Jonathan Thomas * * @ref License @@ -28,8 +28,8 @@ * along with OpenShot Library. If not, see . */ -#ifndef OPENSHOT_BBOXKEYFRAME_H -#define OPENSHOT_BBOXKEYFRAME_H +#ifndef OPENSHOT_TRACKEDOBJECTBBOX_H +#define OPENSHOT_TRACKEDOBJECTBBOX_H #include #include @@ -42,7 +42,7 @@ #include "Point.h" #include "Json.h" #include "KeyFrame.h" -#include "KeyFrameBase.h" +#include "TrackedObjectBase.h" #include "protobuf_messages/trackerdata.pb.h" #include @@ -154,7 +154,7 @@ namespace openshot * object of this class. */ - class KeyFrameBBox : public KeyframeBase + class TrackedObjectBBox : public TrackedObjectBase { private: bool visible; @@ -171,7 +171,7 @@ namespace openshot std::string protobufDataPath; ///< Path to the protobuf file that holds the bbox points across the frames /// Default Constructor - KeyFrameBBox(); + TrackedObjectBBox(); /// Add a BBox to the BoxVec map void AddBox(int64_t _frame_num, float _cx, float _cy, float _width, float _height, float _angle); @@ -197,7 +197,7 @@ namespace openshot /// Return a bounding-box from BoxVec with it's properties adjusted by the Keyframes BBox GetBox(int64_t frame_number) const { - return const_cast(this)->GetBox(frame_number); + return const_cast(this)->GetBox(frame_number); } BBox GetBox(int64_t frame_number); @@ -227,7 +227,9 @@ namespace openshot Json::Value add_property_json(std::string name, float value, std::string type, std::string memo, const Keyframe* keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame) const; /// Return the bounding box properties and it's keyframes indexed by their names - std::map GetBoxValues(int64_t frame_number) override; + std::map GetBoxValues(int64_t frame_number) override; + /// Return properties of this object's parent clip + std::map GetParentClipProperties(int64_t frame_number); }; } // namespace openshot diff --git a/src/TrackedObjectBase.cpp b/src/TrackedObjectBase.cpp new file mode 100644 index 00000000..5d43d7fa --- /dev/null +++ b/src/TrackedObjectBase.cpp @@ -0,0 +1,46 @@ +/** + * @file + * @brief Source file for the TrackedObjectBase class + * @author Jonathan Thomas + * + * @ref License + */ + +/* LICENSE + * + * Copyright (c) 2008-2019 OpenShot Studios, LLC + * . This file is part of + * OpenShot Library (libopenshot), an open-source project dedicated to + * delivering high quality video editing and animation solutions to the + * world. For more information visit . + * + * OpenShot Library (libopenshot) is free software: you can redistribute it + * and/or modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * OpenShot Library (libopenshot) is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with OpenShot Library. If not, see . + */ + +#include "TrackedObjectBase.h" +#include +#include +#include + + +namespace openshot{ + + TrackedObjectBase::TrackedObjectBase(){ + id = "None"; + } + + TrackedObjectBase::TrackedObjectBase(std::string _id){ + Id(_id); + } +} \ No newline at end of file diff --git a/src/TrackedObjectBase.h b/src/TrackedObjectBase.h new file mode 100644 index 00000000..acfd3ce8 --- /dev/null +++ b/src/TrackedObjectBase.h @@ -0,0 +1,97 @@ +/** + * @file + * @brief Header file for the TrackedObjectBase class + * @author Jonathan Thomas + * + * @ref License + */ + +/* LICENSE + * + * Copyright (c) 2008-2019 OpenShot Studios, LLC + * . This file is part of + * OpenShot Library (libopenshot), an open-source project dedicated to + * delivering high quality video editing and animation solutions to the + * world. For more information visit . + * + * OpenShot Library (libopenshot) is free software: you can redistribute it + * and/or modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * OpenShot Library (libopenshot) is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with OpenShot Library. If not, see . + */ + +#ifndef OPENSHOT_TRACKEDOBJECTBASE_H +#define OPENSHOT_TRACKEDOBJECTBASE_H + +#include +#include +#include +#include +#include +#include +#include "Exceptions.h" +#include "Fraction.h" +#include "Coordinate.h" +#include "Point.h" +#include "Json.h" +#include "ClipBase.h" + + +namespace openshot { + /** + * @brief This abstract class is the base class of all Keyframes. + * + * A Keyframe is a collection of Point instances, which is used to vary a number or property over time. + * + * Keyframes are used to animate and interpolate values of properties over time. For example, a single property + * can use a Keyframe instead of a constant value. Assume you want to slide an image (from left to right) over + * a video. You can create a Keyframe which will adjust the X value of the image over 100 frames (or however many + * frames the animation needs to last) from the value of 0 to 640. + */ + + class TrackedObjectBase { + private: + std::string id; + ClipBase* parentClip; + + public: + + /// Blank constructor + TrackedObjectBase(); + + /// Default constructor + TrackedObjectBase(std::string _id); + + /// Get and set the id of this object + std::string Id() const { return id; } + void Id(std::string _id) { id = _id; } + /// Get and set the parentClip of this object + ClipBase* ParentClip() { return parentClip; } + void ParentClip(ClipBase* clip) { parentClip = clip; } + + /// Scale a property + virtual void ScalePoints(double scale) { return; }; + /// Return the main properties of a TrackedObjectBBox instance using a pointer to this base class + virtual std::map GetBoxValues(int64_t frame_number) { std::map ret; return ret; }; + /// Return the main properties of the tracked object's parent clip + virtual std::map GetParentClipProperties(int64_t frame_number) { std::map ret; return ret; } + + /// Get and Set JSON methods + virtual std::string Json() const = 0; ///< Generate JSON string of this object + virtual Json::Value JsonValue() const = 0; ///< Generate Json::Value for this object + virtual void SetJson(const std::string value) = 0; ///< Load JSON string into this object + virtual void SetJsonValue(const Json::Value root) = 0; ///< Load Json::Value into this object + + + }; +} // Namespace openshot + +#endif diff --git a/src/effects/Tracker.cpp b/src/effects/Tracker.cpp index 4c0df453..651d9fca 100644 --- a/src/effects/Tracker.cpp +++ b/src/effects/Tracker.cpp @@ -41,11 +41,13 @@ Tracker::Tracker(std::string clipTrackerDataPath) { // Init effect properties init_effect_details(); - // Instantiate a keyframebbox object and point to it - KeyFrameBBox trackedDataObject; - trackedData = std::make_shared(trackedDataObject); + // Instantiate a TrackedObjectBBox object and point to it + TrackedObjectBBox trackedDataObject; + trackedData = std::make_shared(trackedDataObject); // Tries to load the tracked object's data from protobuf file trackedData->LoadBoxData(clipTrackerDataPath); + ClipBase* parentClip = this->ParentClip(); + trackedData->ParentClip(parentClip); } // Default constructor @@ -53,9 +55,11 @@ Tracker::Tracker() { // Init effect properties init_effect_details(); - // Instantiate a keyframebbox object and point to it - KeyFrameBBox trackedDataObject; - trackedData = std::make_shared(trackedDataObject); + // Instantiate a TrackedObjectBBox object and point to it + TrackedObjectBBox trackedDataObject; + trackedData = std::make_shared(trackedDataObject); + ClipBase* parentClip = this->ParentClip(); + trackedData->ParentClip(parentClip); } diff --git a/src/effects/Tracker.h b/src/effects/Tracker.h index 41d0baed..2cb1faf5 100644 --- a/src/effects/Tracker.h +++ b/src/effects/Tracker.h @@ -41,7 +41,7 @@ #include "../Json.h" #include "../KeyFrame.h" #include "protobuf_messages/trackerdata.pb.h" -#include "../KeyFrameBBox.h" +#include "../TrackedObjectBBox.h" #include "../Clip.h" using namespace std; @@ -63,7 +63,7 @@ namespace openshot public: std::string protobuf_data_path; ///< Path to the protobuf file that holds the bounding-box data - std::shared_ptr trackedData; ///< Pointer to an object that holds the bounding-box data and it's Keyframes + std::shared_ptr trackedData; ///< Pointer to an object that holds the bounding-box data and it's Keyframes /// Blank constructor, useful when using Json to load the effect properties Tracker(std::string clipTrackerDataPath); diff --git a/tests/KeyFrame_Tests.cpp b/tests/KeyFrame_Tests.cpp index 9e67f9ee..31d93c3f 100644 --- a/tests/KeyFrame_Tests.cpp +++ b/tests/KeyFrame_Tests.cpp @@ -35,7 +35,7 @@ // Prevent name clashes with juce::UnitTest #define DONT_SET_USING_JUCE_NAMESPACE 1 #include "KeyFrame.h" -#include "KeyFrameBBox.h" +#include "TrackedObjectBBox.h" #include "Coordinate.h" #include "Fraction.h" #include "Clip.h" @@ -506,14 +506,14 @@ TEST(Keyframe_Handle_Large_Segment) } -TEST(KeyFrameBBox_init_test) { +TEST(TrackedObjectBBox_init_test) { - KeyFrameBBox kfb; + TrackedObjectBBox kfb; } -TEST(KeyFrameBBox_addBox_test) { - KeyFrameBBox kfb; +TEST(TrackedObjectBBox_addBox_test) { + TrackedObjectBBox kfb; kfb.AddBox(1, 10.0, 10.0, 100.0, 100.0, 0.0); @@ -527,8 +527,8 @@ TEST(KeyFrameBBox_addBox_test) { } -TEST(KeyFrameBBox_GetVal_test) { - KeyFrameBBox kfb; +TEST(TrackedObjectBBox_GetVal_test) { + TrackedObjectBBox kfb; kfb.AddBox(1, 10.0, 10.0, 100.0, 100.0, 0.0); @@ -542,8 +542,8 @@ TEST(KeyFrameBBox_GetVal_test) { } -TEST(KeyFrameBBox_GetVal_Interpolation) { - KeyFrameBBox kfb; +TEST(TrackedObjectBBox_GetVal_Interpolation) { + TrackedObjectBBox kfb; kfb.AddBox(1, 10.0, 10.0, 100.0, 100.0, 0.0); kfb.AddBox(11, 20.0, 20.0, 100.0, 100.0, 0.0); @@ -574,8 +574,8 @@ TEST(KeyFrameBBox_GetVal_Interpolation) { } -TEST(KeyFrameBBox_Json_set) { - KeyFrameBBox kfb; +TEST(TrackedObjectBBox_Json_set) { + TrackedObjectBBox kfb; kfb.AddBox(1, 10.0, 10.0, 100.0, 100.0, 0.0); kfb.AddBox(10, 20.0, 20.0, 100.0, 100.0, 0.0); @@ -588,7 +588,7 @@ TEST(KeyFrameBBox_Json_set) { kfb.SetBaseFPS(Fraction(24.0, 1.0)); auto dataJSON = kfb.Json(); - KeyFrameBBox fromJSON_kfb; + TrackedObjectBBox fromJSON_kfb; fromJSON_kfb.SetJson(dataJSON); CHECK_EQUAL(kfb.GetBaseFPS().num, fromJSON_kfb.GetBaseFPS().num); @@ -607,8 +607,8 @@ TEST(KeyFrameBBox_Json_set) { CHECK_EQUAL(kfb_bbox.angle, fromJSON_bbox.angle); } -TEST(KeyFrameBBox_Scale_test){ - KeyFrameBBox kfb; +TEST(TrackedObjectBBox_Scale_test){ + TrackedObjectBBox kfb; kfb.AddBox(1, 10.0, 10.0, 10.0, 10.0, 0.0); kfb.scale_x.AddPoint(1.0, 2.0); @@ -646,7 +646,7 @@ TEST(Attach_test){ clip.AddEffect(&tracker); // Save a pointer to trackedData - std::shared_ptr trackedData = tracker.trackedData; + std::shared_ptr trackedData = tracker.trackedData; // Change trackedData scale trackedData->scale_x.AddPoint(1, 2.0); @@ -656,8 +656,8 @@ TEST(Attach_test){ auto trackedDataJson = trackedData->JsonValue(); // Get and cast the trakcedObject - auto trackedObject_base = t.GetTrackedObject("TESTBASEID"); - std::shared_ptr trackedObject = std::static_pointer_cast(trackedObject_base); + auto trackedObject_base = t.GetTrackedObject("None"); + std::shared_ptr trackedObject = std::static_pointer_cast(trackedObject_base); CHECK_EQUAL(trackedData, trackedObject); // Set trackedObject Json Value @@ -668,7 +668,7 @@ TEST(Attach_test){ childClip.Open(); childClip.AttachToTracker(tracked_id); - std::shared_ptr trackedTest = std::static_pointer_cast(childClip.GetAttachedObject()); + std::shared_ptr trackedTest = std::static_pointer_cast(childClip.GetAttachedObject()); CHECK_EQUAL(trackedData->scale_x.GetValue(1), trackedTest->scale_x.GetValue(1)); @@ -678,10 +678,10 @@ TEST(Attach_test){ TEST(GetBoxValues_test){ - KeyFrameBBox trackedDataObject; + TrackedObjectBBox trackedDataObject; trackedDataObject.AddBox(1, 10.0, 10.0, 20.0, 20.0, 30.0); - std::shared_ptr trackedData = std::make_shared(trackedDataObject); + std::shared_ptr trackedData = std::make_shared(trackedDataObject); auto boxValues = trackedData->GetBoxValues(1);