4#include "glm/gtx/matrix_decompose.hpp"
5#include "glm/gtx/quaternion.hpp"
9#ifdef GA_OPENFRAMEWORKS
10#include "ofRectangle.h"
17 const double pi = 4. * std::atan( 1. );
66 Rect(
float xPos,
float yPos,
float width,
float height )
73 Rect(
const vec2& ptA,
const vec2& ptB )
81#ifdef GA_OPENFRAMEWORKS
82 Rect(
const ofRectangle& rect )
94 vec2
size()
const {
return vec2 {
w,
h }; }
95 float area()
const {
return std::abs(
w *
h ); }
96 vec2
min()
const {
return vec2( std::min(
x,
x +
w ), std::min(
y,
y +
h ) ); }
97 vec2
max()
const {
return vec2( std::max(
x,
x +
w ), std::max(
y,
y +
h ) ); }
102 return pt.x >= a.x && pt.y >= a.y && pt.x <= b.x && pt.y <= b.y;
139inline const T&
clamp(
const T& val,
const T& min,
const T& max )
141 return ( val < min ) ? min
142 : ( val > max ) ? max
154inline T
map(
const T& val,
const T& inMin,
const T& inMax,
const T& outMin,
const T& outMax,
bool bClamp =
false )
158 r = ( val - inMin ) / ( inMax - inMin ) * ( outMax - outMin ) + outMin;
161 return bClamp ?
clamp( r, outMin, outMax ) : r;
164inline float map(
const float& val,
const float& inMin,
const float& inMax,
const float& outMin,
const float& outMax,
bool bClamp =
false)
168 r = (val - inMin) / (inMax - inMin) * (outMax - outMin) + outMin;
172 return bClamp ?
clamp(r, outMin, outMax) : r;
180inline T
lerp(
const T& a,
const T& b,
const T& pct )
182 return ( b - a ) * pct + a;
186inline T
lerp(
const T& a,
const T& b,
float pct )
188 return ( b - a ) * pct + a;
191inline float lerp(
float a,
float b,
float pct )
193 return ( b - a ) * pct + a;
196inline quat
lerp(
const quat& a,
const quat& b,
float pct )
198 return slerp( a, b, pct );
202inline quat
slerp(
const quat& a,
const quat& b,
float pct )
204 return glm::slerp( a, b, pct );
208inline float cubicBezier(
float x,
float x1,
float y1,
float x2,
float y2 )
226 float A = x3a - 3.f * x2a + 3.f * x1a - x0a;
227 float B = 3.f * x2a - 6.f * x1a + 3.f * x0a;
228 float C = 3.f * x1a - 3.f * x0a;
231 float E = y3a - 3.f * y2a + 3.f * y1a - y0a;
232 float F = 3.f * y2a - 6.f * y1a + 3.f * y0a;
233 float G = 3.f * y1a - 3.f * y0a;
236 auto slopeFromT = [](
float t,
float A,
float B,
float C ) ->
float {
237 return 1.f / ( 3.f * A * t * t + 2.f * B * t + C );
239 auto xFromT = [](
float t,
float A,
float B,
float C,
float D ) ->
float {
240 return A * ( t * t * t ) + B * ( t * t ) + C * t + D;
242 auto yFromT = [](
float t,
float E,
float F,
float G,
float H ) ->
float {
243 return E * ( t * t * t ) + F * ( t * t ) + G * t + H;
249 int nRefinementIterations = 5;
250 for (
int i = 0; i < nRefinementIterations; i++ ) {
251 float currentx = xFromT( currentt, A, B, C, D );
252 float currentslope = slopeFromT( currentt, A, B, C );
253 currentt -= ( currentx - x ) * ( currentslope );
254 currentt =
clamp( currentt, 0.f, 1.f );
257 return yFromT( currentt, E, F, G, H );
263inline T
interpolate(
const T& a,
const T& b,
float pct, std::function<
float(
float )>
easeFn,
bool bClamp =
false )
const double pi
Definition: math.h:17
const double degToRadFactor
Definition: math.h:21
const double halfPi
Definition: math.h:18
const double radToDegFactor
Definition: math.h:20
std::function< float(float)> easeFn(const EaseType &type)
Definition: easing.h:102
T map(const T &val, const T &inMin, const T &inMax, const T &outMin, const T &outMax, bool bClamp=false)
Map a value from one range to another.
Definition: math.h:154
float cubicBezier(float x, float x1, float y1, float x2, float y2)
Definition: math.h:208
T interpolate(const T &a, const T &b, float pct, std::function< float(float)> easeFn, bool bClamp=false)
Definition: math.h:263
T toRadians(const T °rees)
Definition: math.h:47
quat slerp(const quat &a, const quat &b, float pct)
Definition: math.h:202
T toDegrees(const T &radians)
Definition: math.h:36
T lerp(const T &a, const T &b, const T &pct)
Definition: math.h:180
const T & clamp(const T &val, const T &min, const T &max)
Clamp a value between a minimum and a maximum.
Definition: math.h:139
A 3D axis-aligned bounding box.
Definition: math.h:124
vec3 max
Definition: math.h:125
vec3 size() const
Definition: math.h:127
float height() const
Definition: math.h:129
vec3 center() const
Definition: math.h:126
vec3 min
Definition: math.h:125
float width() const
Definition: math.h:128
float depth() const
Definition: math.h:130
A 2D axis-aligned rectangle, with x,y (float) position and w,h (float) size components.
Definition: math.h:64
vec2 position() const
Definition: math.h:93
float y
Definition: math.h:91
vec2 max() const
Definition: math.h:97
Rect(float xPos, float yPos, float width, float height)
Definition: math.h:66
float x
Definition: math.h:91
vec2 size() const
Definition: math.h:94
Rect()
Definition: math.h:65
Rect(const vec2 &ptA, const vec2 &ptB)
Definition: math.h:73
float h
Definition: math.h:91
float area() const
Definition: math.h:95
vec2 min() const
Definition: math.h:96
float w
Definition: math.h:91
bool contains(const vec2 &pt) const
Definition: math.h:98