Skip to content

Commit

Permalink
Merge pull request #591 from ferdnyc/frac-python-types
Browse files Browse the repository at this point in the history
Add class constructors taking STL container types, primarily for Python use
  • Loading branch information
jonoomph committed Jan 27, 2021
2 parents b5c3d0b + 6bd3110 commit 3b73425
Show file tree
Hide file tree
Showing 18 changed files with 388 additions and 206 deletions.
62 changes: 61 additions & 1 deletion bindings/python/openshot.i
Expand Up @@ -127,30 +127,90 @@

/* Instantiate the required template specializations */
%template() std::map<std::string, int>;
%template() std::pair<int, int>;
%template() std::vector<int>;
%template() std::pair<double, double>;
%template() std::pair<float, float>;

/* Make openshot.Fraction more Pythonic */
%extend openshot::Fraction {
%{
#include <sstream>
#include <map>

static std::vector<std::string> _keys{"num", "den"};
static int fracError = 0;
%}
double __float__() {
return $self->ToDouble();
}
int __int__() {
return $self->ToInt();
}
/* Dictionary-type methods */
int __len__() {
return _keys.size();
}
%exception __getitem__ {
$action
if (fracError == 1) {
fracError = 0; // Clear flag for reuse
PyErr_SetString(PyExc_KeyError, "Key not found");
SWIG_fail;
}
}
const std::string __getitem__(int index) {
if (index < _keys.size()) {
return _keys[index];
}
/* Otherwise, raise an exception */
fracError = 1;
return "";
}
int __getitem__(const std::string& key) {
if (key == "num") {
return $self->num;
} else if (key == "den") {
return $self->den;
}
/* Otherwise, raise an exception */
fracError = 1;
return 0;
}
bool __contains__(const std::string& key) {
return bool(std::find(_keys.begin(), _keys.end(), key) != _keys.end());
}
std::map<std::string, int> GetMap() {
std::map<std::string, int> map1;
map1.insert({"num", $self->num});
map1.insert({"den", $self->den});
return map1;
}
std::string __repr__() {
/* Display methods */
const std::string __string__() {
std::ostringstream result;
result << $self->num << ":" << $self->den;
return result.str();
}
const std::string __repr__() {
std::ostringstream result;
result << "Fraction(" << $self->num << ", " << $self->den << ")";
return result.str();
}
/* Implement dict methods in Python */
%pythoncode %{
def __iter__(self):
return iter(self.GetMap())
def keys(self):
_items = self.GetMap()
return _items.keys()
def items(self):
_items = self.GetMap()
return _items.items()
def values(self):
_items = self.GetMap()
return _items.values()
%}
}

%extend openshot::OpenShotVersion {
Expand Down
7 changes: 7 additions & 0 deletions bindings/ruby/openshot.i
Expand Up @@ -57,6 +57,13 @@
%shared_ptr(juce::AudioSampleBuffer)
%shared_ptr(openshot::Frame)

/* Template specializations */
%template() std::map<std::string, int>;
%template() std::pair<int, int>;
%template() std::vector<int>;
%template() std::pair<double, double>;
%template() std::pair<float, float>;

%{
/* Ruby and FFmpeg define competing RSHIFT macros,
* so we move Ruby's out of the way for now. We'll
Expand Down
1 change: 0 additions & 1 deletion src/Clip.h
Expand Up @@ -41,7 +41,6 @@
#include "EffectBase.h"
#include "Effects.h"
#include "EffectInfo.h"
#include "Fraction.h"
#include "Frame.h"
#include "KeyFrame.h"
#include "ReaderBase.h"
Expand Down
14 changes: 6 additions & 8 deletions src/Coordinate.cpp
Expand Up @@ -31,19 +31,17 @@
#include "Coordinate.h"
#include "Exceptions.h"

using namespace std;
using namespace openshot;

// Default constructor for a coordinate, which defaults the X and Y to zero (0,0)
Coordinate::Coordinate() :
X(0), Y(0) {
}
// Default constructor for a coordinate, delegating to the full signature
Coordinate::Coordinate() : Coordinate::Coordinate(0, 0) {};

// Constructor which also allows the user to set the X and Y
Coordinate::Coordinate(double x, double y) :
X(x), Y(y) {
}
Coordinate::Coordinate(double x, double y) : X(x), Y(y) {};

// Constructor which accepts a std::pair for (X, Y)
Coordinate::Coordinate(const std::pair<double, double>& co)
: X(co.first), Y(co.second) {};

// Generate JSON string of this object
std::string Coordinate::Json() const {
Expand Down
4 changes: 4 additions & 0 deletions src/Coordinate.h
Expand Up @@ -64,6 +64,10 @@ namespace openshot {
/// @param y The Y coordinate (usually representing the value of the property being animated)
Coordinate(double x, double y);

/// @brief Constructor which accepts a std::pair tuple for {X, Y}
/// @param co A std::pair<double, double> tuple containing (X, Y)
Coordinate(const std::pair<double, double>& co);

/// Get and Set JSON methods
std::string Json() const; ///< Generate JSON string of this object
Json::Value JsonValue() const; ///< Generate Json::Value for this object
Expand Down
18 changes: 14 additions & 4 deletions src/Fraction.cpp
Expand Up @@ -29,13 +29,23 @@
*/

#include "Fraction.h"
#include <cmath>

using namespace openshot;

// Constructor
Fraction::Fraction() :
num(1), den(1) {
}
// Delegating constructors
Fraction::Fraction() : Fraction::Fraction(1, 1) {};

Fraction::Fraction(std::pair<int, int> pair)
: Fraction::Fraction(pair.first, pair.second) {};

Fraction::Fraction(std::map<std::string, int> mapping)
: Fraction::Fraction(mapping["num"], mapping["den"]) {};

Fraction::Fraction(std::vector<int> vector)
: Fraction::Fraction(vector[0], vector[1]) {};

// Full constructor
Fraction::Fraction(int num, int den) :
num(num), den(den) {
}
Expand Down
15 changes: 14 additions & 1 deletion src/Fraction.h
Expand Up @@ -31,7 +31,10 @@
#ifndef OPENSHOT_FRACTION_H
#define OPENSHOT_FRACTION_H

#include <cmath>
#include <string> // for std::string
#include <utility> // for std::pair
#include <map> // for std::map
#include <vector> // for std::vector

namespace openshot {

Expand All @@ -49,9 +52,19 @@ namespace openshot {

/// Default Constructor
Fraction();

/// Constructor with numerator and denominator
Fraction(int num, int den);

/// Constructor that accepts a (num, den) pair
Fraction(std::pair<int, int> pair);

/// Constructor that takes a vector of length 2 (containing {num, den})
Fraction(std::vector<int> vector);

/// Constructor that takes a key-value mapping (keys: 'num'. 'den')
Fraction(std::map<std::string, int> mapping);

/// Calculate the greatest common denominator
int GreatestCommonDenominator();

Expand Down
9 changes: 4 additions & 5 deletions src/ImageWriter.h
Expand Up @@ -43,12 +43,11 @@
#include "ReaderBase.h"
#include "WriterBase.h"

#include <cmath>
#include <ctime>
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <vector>
#include "CacheMemory.h"
#include "Exceptions.h"
#include "Fraction.h"

#include "OpenMPUtilities.h"
#include "MagickUtilities.h"
Expand Down
26 changes: 15 additions & 11 deletions src/KeyFrame.cpp
Expand Up @@ -30,6 +30,10 @@

#include "KeyFrame.h"
#include "Exceptions.h"

#include <cassert> // For assert()
#include <iostream> // For std::cout
#include <iomanip> // For std::setprecision
#include <algorithm>
#include <functional>
#include <utility>
Expand Down Expand Up @@ -121,6 +125,16 @@ Keyframe::Keyframe(double value) {
AddPoint(Point(value));
}

// Constructor which takes a vector of Points
Keyframe::Keyframe(const std::vector<Point>& points) : Points(points) {};

// Constructor which takes a vector of std::pair tuples (and adds them all)
Keyframe::Keyframe(const std::vector<Coordinate>& coordinates) {
for (const auto& co : coordinates) {
AddPoint(Point(co));
}
}

// Add a new point on the key-frame. Each point has a primary coordinate,
// a left handle, and a right handle.
void Keyframe::AddPoint(Point p) {
Expand All @@ -147,17 +161,7 @@ void Keyframe::AddPoint(Point p) {
}
}

// Add a new point on the key-frame, with some defaults set (BEZIER)
void Keyframe::AddPoint(double x, double y)
{
// Create a point
Point new_point(x, y, BEZIER);

// Add the point
AddPoint(new_point);
}

// Add a new point on the key-frame, with a specific interpolation type
// Add a new point on the key-frame, interpolate is optional (default: BEZIER)
void Keyframe::AddPoint(double x, double y, InterpolationType interpolate)
{
// Create a point
Expand Down
19 changes: 10 additions & 9 deletions src/KeyFrame.h
Expand Up @@ -31,14 +31,10 @@
#ifndef OPENSHOT_KEYFRAME_H
#define OPENSHOT_KEYFRAME_H

#include <iostream>
#include <iomanip>
#include <cmath>
#include <assert.h>
#include <vector>

#include "Fraction.h"
#include "Coordinate.h"
#include "Point.h"
#include "Json.h"

Expand Down Expand Up @@ -66,21 +62,26 @@ namespace openshot {
std::vector<Point> Points; ///< Vector of all Points

public:
using CoordinateVector = std::vector<Coordinate>;
using PointsVector = std::vector<Point>;

/// Default constructor for the Keyframe class
Keyframe() = default;

/// Constructor which sets the default point & coordinate at X=1
Keyframe(double value);

/// Constructor which adds a supplied vector of Points
Keyframe(const PointsVector& points);

/// Constructor which takes a vector of std::pair tuples
Keyframe(const CoordinateVector& coordinates);

/// Add a new point on the key-frame. Each point has a primary coordinate, a left handle, and a right handle.
void AddPoint(Point p);

/// Add a new point on the key-frame, with some defaults set (BEZIER)
void AddPoint(double x, double y);

/// Add a new point on the key-frame, with a specific interpolation type
void AddPoint(double x, double y, InterpolationType interpolate);
/// Add a new point on the key-frame, with optional interpolation type
void AddPoint(double x, double y, InterpolationType interpolate=BEZIER);

/// Does this keyframe contain a specific point
bool Contains(Point p) const;
Expand Down
57 changes: 13 additions & 44 deletions src/Point.cpp
Expand Up @@ -34,58 +34,27 @@
using namespace std;
using namespace openshot;

// Default constructor (defaults to 1,0)
Point::Point() : interpolation(BEZIER), handle_type(AUTO)
{
// set new coorinate
co = Coordinate(1, 0);

// set handles
Initialize_Handles();
}
// Default constructor
Point::Point() : Point::Point(Coordinate(1, 0), BEZIER, AUTO) {};

// Constructor which creates a single coordinate at X=1
Point::Point(float y) :
interpolation(CONSTANT), handle_type(AUTO) {
// set new coorinate
co = Coordinate(1, y);
Point::Point(float y) : Point::Point(Coordinate(1, y), CONSTANT, AUTO) {};

// set handles
Initialize_Handles();
}
// Constructor which creates a Bezier curve with point at (x, y)
Point::Point(float x, float y) : Point::Point(Coordinate(x, y), BEZIER, AUTO) {};

Point::Point(float x, float y) :
interpolation(BEZIER), handle_type(AUTO) {
// set new coorinate
co = Coordinate(x, y);
// Constructor which also creates a Point, setting X,Y, and interpolation.
Point::Point(float x, float y, InterpolationType interpolation)
: Point::Point(Coordinate(x, y), interpolation, AUTO) {};

// set handles
Initialize_Handles();
}

// Constructor which also creates a Point and sets the X,Y, and interpolation of the Point.
Point::Point(float x, float y, InterpolationType interpolation) :
handle_type(AUTO), interpolation(interpolation) {
// set new coorinate
co = Coordinate(x, y);
// Direct Coordinate-accepting constructors
Point::Point(const Coordinate& co) : Point::Point(co, BEZIER, AUTO) {};

// set handles
Initialize_Handles();
}

Point::Point(Coordinate co) :
co(co), interpolation(BEZIER), handle_type(AUTO) {
// set handles
Initialize_Handles();
}

Point::Point(Coordinate co, InterpolationType interpolation) :
co(co), interpolation(interpolation), handle_type(AUTO) {
// set handles
Initialize_Handles();
}
Point::Point(const Coordinate& co, InterpolationType interpolation)
: Point::Point(co, interpolation, AUTO) {};

Point::Point(Coordinate co, InterpolationType interpolation, HandleType handle_type) :
Point::Point(const Coordinate& co, InterpolationType interpolation, HandleType handle_type) :
co(co), interpolation(interpolation), handle_type(handle_type) {
// set handles
Initialize_Handles();
Expand Down

0 comments on commit 3b73425

Please sign in to comment.