moved various classes from root to common folder

This commit is contained in:
itsmattkc
2019-07-15 03:58:39 -04:00
parent dc2101938e
commit 837d43fe07
21 changed files with 41 additions and 20 deletions
+25
View File
@@ -0,0 +1,25 @@
# Olive - Non-Linear Video Editor
# Copyright (C) 2019 Olive Team
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set(OLIVE_SOURCES
${OLIVE_SOURCES}
common/clamp.h
common/lerp.h
common/rational.h
common/rational.cpp
common/qobjectlistcast.h
PARENT_SCOPE
)
+17
View File
@@ -0,0 +1,17 @@
#ifndef CLAMP_H
#define CLAMP_H
template<typename T>
T clamp(T value, T minimum, T maximum) {
if (value < minimum) {
return minimum;
}
if (value > maximum) {
return maximum;
}
return value;
}
#endif // CLAMP_H
+9
View File
@@ -0,0 +1,9 @@
#ifndef LERP_H
#define LERP_H
template<typename T>
T lerp(T a, T b, double t) {
return (a * (1.0 - t)) + (b * t);
}
#endif // LERP_H
+56
View File
@@ -0,0 +1,56 @@
#ifndef QOBJECTLISTCAST_H
#define QOBJECTLISTCAST_H
#include <QObject>
template<class T>
/**
* @brief Statically cast a QObjectList (aka QList<QObject*>) to a QList of any type (must be a QObject derivative)
*
* Best used to convert the list from a QObject's children() list to a list of another type, assuming it's known that
* all children are of a certain type.
*
* As a static cast, the objects in the QObjectList must all be of the SAME type or the behavior
* is undefined.
*/
QList<T*> static_qobjectlist_cast(const QObjectList& list) {
QList<T*> new_list;
new_list.reserve(list.size());
for (int i=0;i<list.size();i++) {
new_list.append(static_cast<T*>(list.at(i)));
}
return new_list;
}
template<class T>
/**
* @brief Dynamically cast a QObjectList (aka QList<QObject*>) to a QList of any type (must be a QObject derivative)
*
* Best used to convert the list from a QObject's children() list to a list of another type, assuming it's known that
* all children are of a certain type.
*
* Unlike static_qobjectlist_cast(), not all of the objects must be of the same type. Objects that are of a different
* type are not added to the list. Therefore it is guarantee the list will contain valid objects of the type you
* specify (or an empty list if there are none).
*/
QList<T*> dynamic_qobjectlist_cast(const QObjectList& list) {
QList<T*> new_list;
new_list.reserve(list.size());
for (int i=0;i<list.size();i++) {
T* casted_obj = dynamic_cast<T*>(list.at(i));
// Test cast (casted_obj will be nullptr if the dynamic_cast fails)
if (casted_obj != nullptr) {
new_list.append(casted_obj);
}
}
return new_list;
}
#endif // QOBJECTLISTCAST_H
+407
View File
@@ -0,0 +1,407 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "rational.h"
rational::rational(const int64_t &numerator) :
numerator_(numerator),
denominator_(1)
{
if (numerator_ == 0) {
denominator_ = 0;
}
}
rational::rational(const int64_t &numerator, const int64_t &denominator) :
numerator_(numerator),
denominator_(denominator)
{
if (denominator_ != 0) {
if (numerator_ != 0) {
FixSigns();
Reduce();
} else {
denominator_ = 0;
}
} else {
numerator_ = 0;
}
}
rational::rational(const AVRational &r) :
numerator_(r.num),
denominator_(r.den)
{
}
rational::rational(const rational &r) :
numerator_(r.numerator_),
denominator_(r.denominator_)
{
}
const rational &rational::operator=(const rational &r)
{
if (this != &r) {
numerator_ = r.numerator_;
denominator_ = r.denominator_;
}
return *this;
}
const rational &rational::operator+=(const rational &r)
{
if (numerator_ * denominator_ == 0 && r.numerator_ * r.denominator_ == 0) {
numerator_ = 0;
denominator_ = 0;
} else {
if(numerator_ * denominator_ != 0 && r.numerator_ * r.denominator_ == 0) {
// The other rational == 0, no addition needs to be done
} else {
if(numerator_ * denominator_ == 0 && r.numerator_ * r.denominator_ != 0)
{
numerator_ = r.numerator_;
denominator_ = r.denominator_;
}
else
{
numerator_ = (numerator_ * r.denominator_) + (r.numerator_ * denominator_);
denominator_ = denominator_ * r.denominator_;
FixSigns();
Reduce();
}
}
}
return *this;
}
const rational &rational::operator-=(const rational &r)
{
if(numerator_ * denominator_ == 0 && r.numerator_ * r.numerator_ == 0) {
numerator_ = 0;
denominator_ = 0;
} else {
if(numerator_ * denominator_ != 0 && r.numerator_ * r.denominator_ == 0) {
} else {
if(numerator_ * denominator_ == 0 && r.numerator_ * r.denominator_ != 0) {
numerator_ = -(r.numerator_);
denominator_ = r.denominator_;
} else {
numerator_ = (numerator_ * r.denominator_) - (r.numerator_ * denominator_);
numerator_ = denominator_ * r.denominator_;
FixSigns();
Reduce();
}
}
}
return *this;
}
const rational &rational::operator/=(const rational &r)
{
numerator_ = numerator_ * r.denominator_;
denominator_ = denominator_ * r.numerator_;
FixSigns();
Reduce();
return *this;
}
const rational &rational::operator*=(const rational &r)
{
numerator_ = numerator_ * r.numerator_;
denominator_ = denominator_ * r.denominator_;
FixSigns();
Reduce();
return *this;
}
rational rational::operator+(const rational &r) const
{
rational result(*this);
result += r;
return result;
}
rational rational::operator-(const rational &r) const
{
rational result(*this);
result -= r;
return result;
}
rational rational::operator/(const rational &r) const
{
rational result(*this);
result /= r;
return result;
}
rational rational::operator*(const rational &r) const
{
rational result(*this);
result *= r;
return result;
}
bool rational::operator<(const rational &r) const
{
if (numerator_ * denominator_ == 0 && r.numerator_ * r.denominator_ == 0) {
return false;
} else {
if (numerator_ * denominator_ != 0 && r.numerator_ * r.denominator_ == 0) {
if(numerator_ * denominator_ < 0)
return true;
else
return false;
}
else {
if (numerator_ * denominator_ == 0 && r.numerator_ * r.denominator_ != 0) {
if (r.numerator_ * r.denominator_ < 0) {
return false;
} else {
return true;
}
} else {
return ((numerator_ * r.denominator_) < (denominator_ * r.numerator_));
}
}
}
}
bool rational::operator<=(const rational &r) const
{
if(numerator_ * denominator_ == 0 && r.numerator_ * r.denominator_ == 0) {
return true;
} else {
if(numerator_ * denominator_ != 0 && r.numerator_ * r.denominator_ == 0) {
if(numerator_ * denominator_ < 0) {
return true;
} else {
return false;
}
} else {
if(numerator_ * denominator_ == 0 && r.numerator_ * r.denominator_ != 0) {
if(r.numerator_ * r.denominator_ < 0) {
return false;
} else {
return true;
}
} else {
return ((numerator_ * r.denominator_) <= (denominator_ * r.numerator_));
}
}
}
}
bool rational::operator>(const rational &r) const
{
if(numerator_ * denominator_ == 0 && r.numerator_ * r.denominator_ == 0) {
return false;
} else {
if(numerator_ * denominator_ != 0 && r.numerator_ * r.denominator_ == 0) {
if(numerator_ * denominator_ > 0) {
return true;
} else {
return false;
}
} else {
if(numerator_ * denominator_ == 0 && r.numerator_ * r.denominator_ != 0) {
if(r.numerator_ * r.denominator_ > 0) {
return false;
} else {
return true;
}
} else {
return ((numerator_ * r.denominator_) > (denominator_ * r.numerator_));
}
}
}
}
bool rational::operator>=(const rational &r) const
{
if(numerator_ * denominator_ == 0 && r.numerator_ * r.denominator_ == 0) {
return true;
} else {
if(numerator_ * denominator_ != 0 && r.numerator_ * r.denominator_ == 0) {
if(numerator_ * denominator_ > 0) {
return true;
} else {
return false;
}
} else {
if(numerator_ * denominator_ == 0 && r.numerator_ * r.denominator_ != 0) {
if(r.numerator_ * r.denominator_ > 0) {
return false;
} else {
return true;
}
} else {
return ((numerator_ * r.denominator_) >= (denominator_ * r.numerator_));
}
}
}
}
bool rational::operator==(const rational &r) const
{
return (numerator_ == r.numerator_ && denominator_ == r.denominator_);
}
bool rational::operator!=(const rational &r) const
{
return (numerator_ != r.numerator_) || (denominator_ != r.denominator_);
}
const rational &rational::operator++()
{
numerator_ += denominator_;
return *this;
}
rational rational::operator++(int)
{
rational tmp = *this;
numerator_ += denominator_;
return tmp;
}
const rational &rational::operator--()
{
numerator_ -= denominator_;
return *this;
}
rational rational::operator--(int)
{
rational tmp;
numerator_ -= denominator_;
return tmp;
}
const rational &rational::operator+() const
{
return *this;
}
rational rational::operator-() const
{
return rational(numerator_, -denominator_);
}
bool rational::operator!() const
{
return !numerator_;
}
double rational::ToDouble() const
{
if (denominator_ == 0) {
return 0;
} else {
return static_cast<double>(numerator_)/static_cast<double>(denominator_);
}
}
const int64_t &rational::numerator()
{
return numerator_;
}
const int64_t &rational::denominator()
{
return denominator_;
}
void rational::FixSigns()
{
// Ensures denominator is always positive (while numerator can be positive or negative)
if(denominator_ < 0) {
denominator_ = -denominator_;
numerator_ = -numerator_;
}
// Ensures if either are zero, they're both zero
if(numerator_ == 0 || denominator_ == 0) {
numerator_ = 0;
denominator_ = 0;
}
}
void rational::Reduce()
{
int64_t d = 1;
if(denominator_ != 0 && numerator_ != 0) {
d = GreatestCommonDenominator(numerator_, denominator_);
}
if(d > 1) {
numerator_ /= d;
denominator_ /= d;
}
}
int64_t rational::GreatestCommonDenominator(const int64_t& x, const int64_t& y)
{
if (y == 0) {
return x;
} else {
int64_t tmp = x % y;
return GreatestCommonDenominator(y, tmp);
}
}
std::ostream &operator<<(std::ostream &out, const rational &value)
{
out << value.numerator_;
if(value.denominator_ != 1)
{
out << '/' << value.denominator_;
return out;
}
return out;
}
std::istream &operator>>(std::istream &in, rational &value)
{
in >> value.numerator_;
value.denominator_ = 1;
char ch;
in.get(ch);
if(!in.eof())
{
if(ch == '/')
{
in >> value.denominator_;
value.FixSigns();
value.Reduce();
}
else
in.putback(ch);
}
return in;
}
+98
View File
@@ -0,0 +1,98 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#ifndef RATIONAL_H
#define RATIONAL_H
// Adapted from https://github.com/angularadam/Qt-Class-rational used in compliance with the GNU General Public License
#include <iostream>
/**
* Including AVFormat to support converting from AVRational to Olive's rational class
*/
extern "C" {
#include <libavformat/avformat.h>
}
/**
* @brief A rational (numerator/denominator) class with C++ operations built in for ease of use.
*
* Rationals in Olive most frequently represent timing information to easily handle timing in various different
* frame/sample rates without the inaccuracy/rounding errors of a floating point type.
*/
class rational {
public:
// Constructors
rational(const int64_t& numerator = 0);
rational(const int64_t& numerator, const int64_t& denominator);
rational(const AVRational& r); // Auto-convert from an FFmpeg AVRational
rational(const rational& r);
// Assignment Operators
const rational& operator=(const rational& r);
const rational& operator+=(const rational& r);
const rational& operator-=(const rational& r);
const rational& operator/=(const rational& r);
const rational& operator*=(const rational& r);
// Math Operators
rational operator+(const rational& r) const;
rational operator-(const rational& r) const;
rational operator/(const rational& r) const;
rational operator*(const rational& r) const;
// Relational and Equality Operators
bool operator<(const rational &r) const;
bool operator<=(const rational &r) const;
bool operator>(const rational &r) const;
bool operator>=(const rational &r) const;
bool operator==(const rational &r) const;
bool operator!=(const rational &r) const;
//Unary operators
const rational& operator++(); //prefix
rational operator++(int); //postfix
const rational& operator--(); //prefix
rational operator--(int); //postfix
const rational& operator+() const;
rational operator-() const;
bool operator!() const;
// IO
friend std::ostream& operator<<(std::ostream &out, const rational& value);
friend std::istream& operator>>(std::istream &in, rational& value);
// Convert to double
double ToDouble() const;
// Specific values
const int64_t& numerator();
const int64_t& denominator();
private:
int64_t numerator_;
int64_t denominator_;
void FixSigns();
void Reduce();
int64_t GreatestCommonDenominator(const int64_t &x, const int64_t &y);
};
#endif // RATIONAL_H