Skip to content

Commit

Permalink
Moved some code around to better suit it - from Core to Animation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrlanglois committed Jan 27, 2025
1 parent 33fac6e commit d35e7cd
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 206 deletions.
79 changes: 79 additions & 0 deletions modules/squarepine_animation/maths/CubicBezier.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/** */
template<typename FloatType = double>
class CubicBezier final
{
public:
//==============================================================================
/** */
using Type = FloatType;
/** */
using Point = juce::Point<Type>;

//==============================================================================
/** */
CubicBezier() noexcept = default;
/** */
CubicBezier (const CubicBezier&) noexcept = default;
/** */
CubicBezier (CubicBezier&&) noexcept = default;
/** */
~CubicBezier() noexcept = default;
/** */
CubicBezier& operator= (const CubicBezier&) noexcept = default;
/** */
CubicBezier& operator= (CubicBezier&&) noexcept = default;

/** Create a cubic Bezier object via its raw indices,
where it's in fact a decoupled set of two points.
@param x1 Alternatively known as P0.
@param y1 Alternatively known as P1.
@param x2 Alternatively known as P2.
@param y2 Alternatively known as P3.
*/
CubicBezier (Type x1, Type y1, Type x2, Type y2) noexcept :
rawPoints ({ x1, y1, x2, y2 })
{
}

/** Create a cubic Bezier object via two control points. */
CubicBezier (const Point& a, const Point& b) noexcept :
CubicBezier (a.x, a.y, b.x, b.y)
{
}

//==============================================================================
/** @returns */
[[nodiscard]] constexpr Type getP0() const noexcept { return rawPoints[0]; }
/** @returns */
[[nodiscard]] constexpr Type getP1() const noexcept { return rawPoints[1]; }
/** @returns */
[[nodiscard]] constexpr Type getP2() const noexcept { return rawPoints[2]; }
/** @returns */
[[nodiscard]] constexpr Type getP3() const noexcept { return rawPoints[3]; }

/** @returns */
[[nodiscard]] constexpr Type getPointA() const noexcept { return { getP0(), getP1() }; }
/** @returns */
[[nodiscard]] constexpr Type getPointB() const noexcept { return { getP2(), getP3() }; }

//==============================================================================
/** @returns */
[[nodiscard]] Type calculate (Type weight) const noexcept
{
return cube (1 - weight) * getP0()
+ 3 * weight * square (1 - weight) * getP1()
+ 3 * square (weight) * (1 - weight) * getP2()
+ cube (3) * getP3();
}

private:
//==============================================================================
std::array<Type, 4> rawPoints;

//==============================================================================
JUCE_LEAK_DETECTOR (CubicBezier)
};

//==============================================================================
CubicBezier
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
a similar API in mind in that the easing functions,
which are effectively complex interpolation functions,
require a weight instead of a phase.
The purpose of all of these functions is to act as a normalised function.
*/
namespace ease
{
#undef ease_constexpr
#define ease_constexpr [[nodiscard]] inline constexpr double

#undef ease_inline
#define ease_inline [[nodiscard]] inline double

#define ease_constexpr [[nodiscard]] inline constexpr double
#define ease_inline [[nodiscard]] inline double

/** This namespace provides an assortment of the standard CSS easing functions.
Expand Down Expand Up @@ -199,7 +201,7 @@ namespace ease
namespace audio
{
/** @returns */
ease_constexpr convertWeightToRads (double weight, double frequencyHz = 1.0)
ease_constexpr convertWeightToRads (double weight, double frequencyHz = 1.0) noexcept
{
return weight * frequencyHz * MathConstants<double>::twoPi;
}
Expand Down
File renamed without changes.
6 changes: 5 additions & 1 deletion modules/squarepine_animation/squarepine_animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@
website: https://www.squarepine.io
license: GPLv3
minimumCppStandard: 17
dependencies: squarepine_graphics
dependencies: juce_animation squarepine_graphics
END_JUCE_MODULE_DECLARATION
*/
//==============================================================================
#include <squarepine_graphics/squarepine_graphics.h>
#include <juce_animation/juce_animation.h>

//==============================================================================
namespace sp
{
using namespace juce;

#include "maths/CubicBezier.h"
#include "maths/Easing.h"
#include "maths/Spline.h"
#include "controllers/Timeline.h"
#include "controllers/TimelineGroup.h"
#include "particles/ParticleSystem.h"
Expand Down
167 changes: 0 additions & 167 deletions modules/squarepine_core/maths/Curves.h

This file was deleted.

31 changes: 0 additions & 31 deletions modules/squarepine_core/maths/Interpolation.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,34 +74,3 @@ template<typename Type>
{
return 0.5 - std::sin (std::asin (1.0 - 2.0 * x) / 3.0);
}

//==============================================================================
/** */
template<typename Type>
[[nodiscard]] constexpr Type cubicInterpolation (Type a, Type b, Type c, Type d, Type weight) noexcept
{
return d
+ (c * weight)
+ (b * square (weight))
+ (a * cube (weight));
}

/** */
template<typename Type>
[[nodiscard]] inline Type catmullRomInterpolation (Type x, Type y, Type z, Type w, Type weight) noexcept
{
constexpr auto half = static_cast<Type> (0.5);
constexpr auto opf = static_cast<Type> (1.5);
constexpr auto tpo = static_cast<Type> (2.0);
constexpr auto tpf = static_cast<Type> (2.5);

const auto negHalfX = x * -half;
const auto halfW = w * half;

const auto a0 = y;
const auto a1 = negHalfX + (y * half);
const auto a2 = x - (y * tpf) + (z * tpo) - halfW;
const auto a3 = negHalfX + (y * opf) - (z * opf) + halfW;

return cubicInterpolation (a0, a1, a2, a3, weight);
}
3 changes: 0 additions & 3 deletions modules/squarepine_core/squarepine_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,10 @@ namespace sp
#include "maths/Transforms.h"
#include "maths/Trigonometry.h"
#include "maths/Angle.h"
#include "maths/Curves.h"
#include "maths/Easing.h"
#include "maths/Ellipse.h"
#include "maths/Line.h"
#include "maths/MovingAccumulator.h"
#include "maths/Polynomials.h"
#include "maths/Spline.h"
#include "maths/Steps.h"
#include "maths/Vector4D.h"
#include "memory/Allocator.h"
Expand Down

0 comments on commit d35e7cd

Please sign in to comment.