1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-07-04 23:36:00 +02:00

BeefySysLib point/rect updates, async pipe fixes

This commit is contained in:
Brian Fiete 2025-03-28 08:08:33 -04:00
parent 81a9478e77
commit fe1aa3c26e
32 changed files with 282 additions and 120 deletions

View file

@ -34,9 +34,9 @@ void BSpline2D::AddPt(float x, float y)
delete mUVals;
mUVals = NULL;
Point2D pt;
pt.mX = x;
pt.mY = y;
PointF pt;
pt.x = x;
pt.y = y;
mInputPoints.push_back(pt);
}
@ -44,9 +44,8 @@ void BSpline2D::Calculate()
{
int n = (int) mInputPoints.size();
int t = 1;
Point2D* control = &mInputPoints[0];
PointF* control = &mInputPoints[0];
mUVals=new int[n+t+1];
compute_intervals(mUVals, n, t);
@ -112,7 +111,7 @@ void BSpline2D::Evaluate(float pct, float* x, float* y)
int t = (int)mInputPoints.size() - 3; // ????
t = 1;
Point2D* control = &mInputPoints[0];
PointF* control = &mInputPoints[0];
// initialize the variables that will hold our outputted point
@ -122,8 +121,8 @@ void BSpline2D::Evaluate(float pct, float* x, float* y)
for (k=0; k<=n; k++)
{
temp = blend(t,t,mUVals,pct); // same blend is used for each dimension coordinate
oX = oX + (control[k]).mX * temp;
oY = oY + (control[k]).mY * temp;
oX = oX + (control[k]).x * temp;
oY = oY + (control[k]).y * temp;
}
*x = oX;

View file

@ -8,7 +8,7 @@ NS_BF_BEGIN;
class BSpline2D
{
public:
Array<Point2D> mInputPoints;
Array<PointF> mInputPoints;
int* mUVals;
public:

View file

@ -2,7 +2,7 @@
USING_NS_BF;
Point2D Beefy::CatmullRomEvaluate(Point2D &p0, Point2D &p1, Point2D &p2, Point2D &p3, float tension, float t)
PointF Beefy::CatmullRomEvaluate(PointF &p0, PointF &p1, PointF &p2, PointF &p3, float tension, float t)
{
float t2 = t * t;
float t3 = t2 * t;
@ -17,8 +17,8 @@ Point2D Beefy::CatmullRomEvaluate(Point2D &p0, Point2D &p1, Point2D &p2, Point2D
float b3 = s * (t3 - 2 * t2 + t) + (-2 * t3 + 3 * t2); // s(t3 - 2 t2 + t)P3 + (-2 t3 + 3 t2)P3
float b4 = s * (t3 - t2); // s(t3 - t2)P4
float x = (p0.mX*b1 + p1.mX*b2 + p2.mX*b3 + p3.mX*b4);
float y = (p0.mY*b1 + p1.mY*b2 + p2.mY*b3 + p3.mY*b4);
float x = (p0.x*b1 + p1.x*b2 + p2.x*b3 + p3.x*b4);
float y = (p0.y*b1 + p1.y*b2 + p2.y*b3 + p3.y*b4);
return Point2D(x,y);
return PointF(x,y);
}

View file

@ -5,6 +5,6 @@
NS_BF_BEGIN;
Point2D CatmullRomEvaluate(Point2D &p0, Point2D &p1, Point2D &p2, Point2D &p3, float tension, float t);
PointF CatmullRomEvaluate(PointF &p0, PointF &p1, PointF &p2, PointF &p3, float tension, float t);
NS_BF_END;

View file

@ -25,7 +25,7 @@ void CubicFuncSpline::AddPt(float x, float y)
intpoly = NULL;
slopes = NULL;
mInputPoints.push_back(Point2D(x, y));
mInputPoints.push_back(PointF(x, y));
}
int CubicFuncSpline::GetLength()
@ -42,7 +42,7 @@ void CubicFuncSpline::Lagrange()
for( i = 0; i < nPts; i++ )
{
lagpoly[i+0*nPts] = 1.0;
float fac = mInputPoints[i].mY;
float fac = mInputPoints[i].y;
j = 0;
for( k = 0; k < nPts; k++ )
{
@ -50,10 +50,10 @@ void CubicFuncSpline::Lagrange()
continue;
lagpoly[i+(j+1)*nPts] = lagpoly[i+j*nPts];
for( jj = j; jj > 0; jj-- )
lagpoly[i+jj*nPts] = lagpoly[i+(jj-1)*nPts] - lagpoly[i+jj*nPts]*mInputPoints[k].mX;
lagpoly[i+0*nPts] *= -mInputPoints[k].mX;
lagpoly[i+jj*nPts] = lagpoly[i+(jj-1)*nPts] - lagpoly[i+jj*nPts]*mInputPoints[k].x;
lagpoly[i+0*nPts] *= -mInputPoints[k].x;
j++;
fac /= ( mInputPoints[i].mX - mInputPoints[k].mX );
fac /= ( mInputPoints[i].x - mInputPoints[k].x );
}
for( j = 0; j < nPts; j++ )
lagpoly[i+j*nPts] *= fac;
@ -75,9 +75,9 @@ void CubicFuncSpline::ComputeSplineSlopes()
for( i = 0; i < n; i++ )
{
h[i] = mInputPoints[i+1].mX - mInputPoints[i].mX;
h[i] = mInputPoints[i+1].x - mInputPoints[i].x;
hinv[i] = 1.0f / h[i];
g[i] = 3 * ( mInputPoints[i+1].mY-mInputPoints[i].mY ) * hinv[i] * hinv[i];
g[i] = 3 * ( mInputPoints[i+1].y-mInputPoints[i].y ) * hinv[i] * hinv[i];
}
a[0] = 2 * hinv[0];
b[0] = g[0];
@ -119,7 +119,7 @@ float CubicFuncSpline::Evaluate(float x)
Calculate();
int idx = (int) 0;
while ((idx < (int) mInputPoints.size()) && (x > mInputPoints[idx].mX))
while ((idx < (int) mInputPoints.size()) && (x > mInputPoints[idx].x))
idx++;
if ((idx == mInputPoints.size()) || (idx == 0))
@ -128,13 +128,13 @@ float CubicFuncSpline::Evaluate(float x)
if (idx == mInputPoints.size())
idx--;
float s1 = slopes[idx];
return mInputPoints[idx].mY + (x - mInputPoints[idx].mX) * s1;
return mInputPoints[idx].y + (x - mInputPoints[idx].x) * s1;
}
float x0 = mInputPoints[idx-1].mX;
float x1 = mInputPoints[idx].mX;
float y0 = mInputPoints[idx-1].mY;
float y1 = mInputPoints[idx].mY;
float x0 = mInputPoints[idx-1].x;
float x1 = mInputPoints[idx].x;
float y0 = mInputPoints[idx-1].y;
float y1 = mInputPoints[idx].y;
float s0 = slopes[idx-1];
float s1 = slopes[idx];

View file

@ -9,7 +9,7 @@ NS_BF_BEGIN;
class CubicFuncSpline
{
public:
std::vector<Point2D> mInputPoints;
std::vector<PointF> mInputPoints;
float* lagpoly;
float* intpoly;
float* slopes;

View file

@ -72,7 +72,7 @@ void CubicSpline2D::AddPt(float x, float y)
delete mYCubicArray;
mYCubicArray = NULL;
mInputPoints.push_back(Point2D(x, y));
mInputPoints.push_back(PointF(x, y));
}
int CubicSpline2D::GetLength()
@ -86,15 +86,15 @@ void CubicSpline2D::Calculate()
std::vector<float> yVals;
for (int i = 0; i < (int) mInputPoints.size(); i++)
{
xVals.push_back(mInputPoints[i].mX);
yVals.push_back(mInputPoints[i].mY);
xVals.push_back(mInputPoints[i].x);
yVals.push_back(mInputPoints[i].y);
}
mXCubicArray = SolveCubic(xVals);
mYCubicArray = SolveCubic(yVals);
}
Point2D CubicSpline2D::Evaluate(float t)
PointF CubicSpline2D::Evaluate(float t)
{
if (mXCubicArray == NULL)
Calculate();
@ -105,5 +105,5 @@ Point2D CubicSpline2D::Evaluate(float t)
if (idx >= (int) mInputPoints.size() - 1)
return mInputPoints[mInputPoints.size() - 1];
return Point2D(mXCubicArray[idx].Evaluate(frac), mYCubicArray[idx].Evaluate(frac));
return PointF(mXCubicArray[idx].Evaluate(frac), mYCubicArray[idx].Evaluate(frac));
}

View file

@ -38,7 +38,7 @@ public:
class CubicSpline2D
{
public:
std::vector<Point2D> mInputPoints;
std::vector<PointF> mInputPoints;
CubicVal* mXCubicArray;
CubicVal* mYCubicArray;
@ -53,7 +53,7 @@ public:
int GetLength();
void Calculate();
Point2D Evaluate(float t);
PointF Evaluate(float t);
};
NS_BF_END;

View file

@ -4,18 +4,33 @@
NS_BF_BEGIN;
class Point2D
template <typename T>
class Point
{
public:
float mX;
float mY;
T x;
T y;
public:
Point2D(float x = 0, float y = 0)
Point(T x = 0, T y = 0)
{
mX = x;
mY = y;
this->x = x;
this->y = y;
}
Point operator+(Point rhs)
{
return Point(x + rhs.x, y + rhs.y);
}
Point operator-(Point rhs)
{
return Point(x - rhs.x, y - rhs.y);
}
};
typedef Point<double> PointD;
typedef Point<float> PointF;
typedef Point<int32> PointI32;
NS_BF_END;

View file

@ -19,7 +19,7 @@ void PolySpline2D::AddPt(float x, float y)
mInputPoints.push_back(Point2D(x, y));
mInputPoints.push_back(PointF(x, y));
}
int PolySpline2D::GetLength()
@ -34,11 +34,11 @@ void PolySpline2D::Calculate()
mCoefs = new float[n];
for (int j=0; j<n; j++)
mat[j*n] = mInputPoints[j].mY;
mat[j*n] = mInputPoints[j].y;
for (int i=1; i<n; i++)
{
for (int j=0; j<n-i; j++)
mat[i+j*n]=(mat[(i-1)+j*n]-mat[(i-1)+(j+1)*n])/(mInputPoints[j].mX-mInputPoints[j+i].mX);
mat[i+j*n]=(mat[(i-1)+j*n]-mat[(i-1)+(j+1)*n])/(mInputPoints[j].x-mInputPoints[j+i].x);
}
for (int i=0; i<n; i++)
@ -58,7 +58,7 @@ float PolySpline2D::Evaluate(float x)
{
float add = mCoefs[i];
for (int j = 0; j < i; j++)
add *= (x - mInputPoints[j].mX);
add *= (x - mInputPoints[j].x);
result += add;
}

View file

@ -9,7 +9,7 @@ NS_BF_BEGIN;
class PolySpline2D
{
public:
std::vector<Point2D> mInputPoints;
std::vector<PointF> mInputPoints;
public:

View file

@ -1,37 +1,130 @@
#pragma once
#include "Common.h"
#include "Point.h"
NS_BF_BEGIN;
template <typename T>
class Rect
{
public:
float mX;
float mY;
float mWidth;
float mHeight;
T x;
T y;
T width;
T height;
public:
Rect()
{
mX = 0;
mY = 0;
mWidth = 0;
mHeight = 0;
x = 0;
y = 0;
width = 0;
height = 0;
}
Rect(float x, float y, float width, float height)
Rect(T x, T y, T width, T height)
{
mX = x;
mY = y;
mWidth = width;
mHeight = height;
this->x = x;
this->y = y;
this->width = width;
this->height = height;
}
bool operator==(const Rect& r2)
{
return (x == r2.x) && (y == r2.y) && (width == r2.width) && (height == r2.height);
}
bool operator!=(const Rect& r2)
{
return (mX != r2.mX) || (mY != r2.mY) || (mWidth != r2.mWidth) || (mHeight != r2.mHeight);
return (x != r2.x) || (y != r2.y) || (width != r2.width) || (height != r2.height);
}
bool Contains(T x, T y)
{
return (x >= this->x) && (y >= this->y) && (x < this->x + width) && (y < this->y + height);
}
bool Contains(Point<T> pt)
{
return (pt.x >= this->x) && (y >= this->y) && (x < this->x + width) && (y < this->y + height);
}
T GetRight()
{
return x + width;
}
T GetBottom()
{
return y + height;
}
Rect Intersection(Rect rect)
{
T x1 = Max(x, rect.x);
T x2 = Min(x + width, rect.x + rect.width);
T y1 = Max(y, rect.y);
T y2 = Min(y + height, rect.y + rect.height);
if (((x2 - x1) < 0) || ((y2 - y1) < 0))
return Rect();
else
return Rect(x1, y1, x2 - x1, y2 - y1);
}
bool Intersects(Rect rect)
{
T x1 = BF_MAX(x, rect.x);
T x2 = BF_MIN(x + width, rect.x + rect.width);
T y1 = BF_MAX(y, rect.y);
T y2 = BF_MIN(y + height, rect.y + rect.height);
if (((x2 - x1) <= 0) || ((y2 - y1) <= 0))
return false;
else
return true;
}
Rect Union(Rect rect)
{
T x1 = Min(x, rect.x);
T x2 = Max(x + width, rect.x + rect.width);
T y1 = Min(y, rect.y);
T y2 = Max(y + height, rect.y + rect.height);
return Rect(x1, y1, x2 - x1, y2 - y1);
}
void Include(Point<T> pt)
{
T left = x;
T top = y;
T right = x + width;
T bottom = y + height;
x = BF_MIN(pt.x, left);
y = BF_MIN(pt.y, top);
width = BF_MAX(pt.x, right) - x;
height = BF_MAX(pt.y, bottom) - y;
}
void Inflate(T x, T y)
{
this->x -= x;
this->width += x + x;
this->y -= y;
this->height += y + y;
}
};
typedef Rect<double> RectD;
typedef Rect<float> RectF;
typedef Rect<int32> RectI32;
template <>
struct BeefHash<RectI32>
{
size_t operator()(RectI32 val)
{
return (size_t)val.x * 4790557 + (size_t)val.y * 6578863 + (size_t)val.width * 6273881 + (size_t)val.height * 9501077;
}
};