1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 19:48:20 +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

@ -76,6 +76,9 @@ namespace System.IO
Platform.BfpFileCreateKind createKind = .CreateAlways;
Platform.BfpFileCreateFlags createFlags = .Pipe;
if (options.HasFlag(.AllowTimeouts))
createFlags |= .AllowTimeouts;
createKind = .OpenExisting;
createFlags |= .Read;
createFlags |= .Write;

View file

@ -392,8 +392,11 @@ namespace System.Net
service.sin_addr = address;
service.sin_port = (uint16)htons((int16)port);
if (bind(mHandle, &service, sizeof(SockAddr_in)) == SOCKET_ERROR)
int32 size = sizeof(SockAddr_in);
if (bind(mHandle, &service, size) == SOCKET_ERROR)
{
int err = WSAGetLastError();
Close();
return .Err;
}

View file

@ -107,6 +107,11 @@ namespace System
public int32 Height => bottom - top;
}
public struct Point : this(int32 x, int32 y)
{
}
[CRepr]
public struct OpenFileName
{
@ -1703,6 +1708,12 @@ namespace System
[Import("user32.lib"), CLink, CallingConvention(.Stdcall)]
public static extern HWnd SetActiveWindow(HWnd wnd);
[Import("user32.lib"), CLink, CallingConvention(.Stdcall)]
public static extern HWnd SetForegroundWindow(HWnd wnd);
[Import("user32.lib"), CLink, CallingConvention(.Stdcall)]
public static extern HWnd GetForegroundWindow();
[CLink, CallingConvention(.Stdcall)]
public static extern int CallWindowProcA(int wndProc, HWnd hWnd, int32 msg, int wParam, int lParam);
@ -1749,6 +1760,12 @@ namespace System
[Import("user32.lib"), CLink, CallingConvention(.Stdcall)]
public static extern IntBool SetWindowPos(HWnd hWnd, HWnd hWndAfter, int x, int y, int cx, int cy, int flags);
[Import("user32.lib"), CLink, CallingConvention(.Stdcall)]
public static extern IntBool GetClientRect(HWnd hWnd, out Rect rect);
[Import("user32.lib"), CLink, CallingConvention(.Stdcall)]
public static extern IntBool ClientToScreen(HWnd hWnd, ref Point rect);
[Import("user32.lib"), CLink, CallingConvention(.Stdcall)]
public static extern IntBool PostMessageW(HWnd hWnd, int32 msg, int wParam, int lParam);

View file

@ -587,15 +587,15 @@ BF_EXPORT void BF_CALLTYPE Gfx_ApplyEffect(TextureSegment* destTextureSegment, T
ImageData destImageData;
ImageData srcImageData;
Rect srcRect = srcTextureSegment->GetRect();
Rect destRect = destTextureSegment->GetRect();
RectF srcRect = srcTextureSegment->GetRect();
RectF destRect = destTextureSegment->GetRect();
destTextureSegment->GetImageData(destImageData);
srcImageData.CreateNew(destImageData.mWidth, destImageData.mHeight);
srcTextureSegment->GetImageData(srcImageData,
(int)(destRect.mWidth - srcRect.mWidth) / 2,
(int)(destRect.mHeight - srcRect.mHeight) / 2);
(int)(destRect.width - srcRect.width) / 2,
(int)(destRect.height - srcRect.height) / 2);
effect->Apply(NULL, &srcImageData, &destImageData);
if (needsPremultiply)
@ -831,10 +831,10 @@ BF_EXPORT void BF_CALLTYPE RenderState_SetWireframe(RenderState* renderState, bo
BF_EXPORT void BF_CALLTYPE RenderState_SetClip(RenderState* renderState, float x, float y, float width, float height)
{
BF_ASSERT((width >= 0) && (height >= 0));
renderState->mClipRect.mX = x;
renderState->mClipRect.mY = y;
renderState->mClipRect.mWidth = width;
renderState->mClipRect.mHeight = height;
renderState->mClipRect.x = x;
renderState->mClipRect.y = y;
renderState->mClipRect.width = width;
renderState->mClipRect.height = height;
if (!renderState->mClipped)
renderState->SetClipped(true);
}

View file

@ -36,13 +36,13 @@ int CachedDataStream::GetSize()
return mStream->GetSize();
}
void CachedDataStream::Read(void* ptr, int size)
int CachedDataStream::Read(void* ptr, int size)
{
Flush();
mStream->Read(ptr, size);
return mStream->Read(ptr, size);
}
void CachedDataStream::Write(void* ptr, int size)
int CachedDataStream::Write(void* ptr, int size)
{
while (size > 0)
{
@ -59,6 +59,7 @@ void CachedDataStream::Write(void* ptr, int size)
size -= writeBytes;
mDataPtr += writeBytes;
}
return size;
}
int CachedDataStream::GetPos()

View file

@ -24,8 +24,8 @@ public:
virtual bool Eof() override;
virtual int GetSize() override;
virtual void Read(void* ptr, int size) override;
virtual void Write(void* ptr, int size) override;
virtual int Read(void* ptr, int size) override;
virtual int Write(void* ptr, int size) override;
virtual int GetPos() override;
virtual void Seek(int size) override;

View file

@ -21,8 +21,8 @@ public:
virtual bool Eof() = 0;
virtual int GetSize() = 0;
virtual void Read(void* ptr, int size) = 0;
virtual void Write(void* ptr, int size) = 0;
virtual int Read(void* ptr, int size) = 0;
virtual int Write(void* ptr, int size) = 0;
virtual void WriteZeros(int size);
virtual void Align(int size);

View file

@ -62,10 +62,12 @@ int FileHandleStream::GetSize()
return ::GetFileSize(mFileHandle, NULL);
}
void FileHandleStream::Read(void* ptr, int size)
int FileHandleStream::Read(void* ptr, int size)
{
if (mCacheBuffer != NULL)
{
int totalReadSize = 0;
while (true)
{
int buffOffset = mVFilePos - mCacheReadPos;
@ -74,7 +76,7 @@ void FileHandleStream::Read(void* ptr, int size)
// If inside
memcpy(ptr, mCacheBuffer + buffOffset, size);
mVFilePos += size;
return;
return size;
}
else if ((buffOffset >= 0) && (buffOffset < mCacheSize))
{
@ -84,6 +86,7 @@ void FileHandleStream::Read(void* ptr, int size)
ptr = (uint8*)ptr + subSize;
size -= subSize;
totalReadSize += subSize;
}
mCacheReadPos = mVFilePos & ~(4096 - 1);
@ -95,7 +98,9 @@ void FileHandleStream::Read(void* ptr, int size)
// Zero out underflow bytes
memset((uint8*)ptr + aSize, 0, mCacheSize - aSize);
}
totalReadSize += aSize;
}
return totalReadSize;
}
else
{
@ -106,12 +111,13 @@ void FileHandleStream::Read(void* ptr, int size)
// Zero out underflow bytes
memset((uint8*)ptr + aSize, 0, size - aSize);
}
return aSize;
}
}
void FileHandleStream::Write(void* ptr, int size)
int FileHandleStream::Write(void* ptr, int size)
{
::WriteFile(mFileHandle, ptr, size, NULL, NULL);
return (int)::WriteFile(mFileHandle, ptr, size, NULL, NULL);
}
int FileHandleStream::GetPos()

View file

@ -23,9 +23,9 @@ public:
bool Eof() override;
int GetSize() override;
using DataStream::Read;
void Read(void* ptr, int size) override;
int Read(void* ptr, int size) override;
using DataStream::Write;
void Write(void* ptr, int size) override;
int Write(void* ptr, int size) override;
int GetPos() override;
void Seek(int size) override;

View file

@ -99,10 +99,12 @@ int FileStream::GetSize()
return aSize;
}
void FileStream::Read(void* ptr, int size)
int FileStream::Read(void* ptr, int size)
{
if (mCacheBuffer != NULL)
{
int totalReadSize = 0;
while (true)
{
int buffOffset = mVFilePos - mCacheReadPos;
@ -111,7 +113,7 @@ void FileStream::Read(void* ptr, int size)
// If inside
memcpy(ptr, mCacheBuffer + buffOffset, size);
mVFilePos += size;
return;
return size;
}
else if ((buffOffset >= 0) && (buffOffset < mCacheSize))
{
@ -121,6 +123,8 @@ void FileStream::Read(void* ptr, int size)
ptr = (uint8*) ptr + subSize;
size -= subSize;
totalReadSize += subSize;
}
mCacheReadPos = mVFilePos & ~(4096-1);
@ -131,7 +135,11 @@ void FileStream::Read(void* ptr, int size)
// Zero out underflow bytes
memset((uint8*) ptr + aSize, 0, mCacheSize - aSize);
}
totalReadSize += aSize;
}
return totalReadSize;
}
else
{
@ -142,12 +150,13 @@ void FileStream::Read(void* ptr, int size)
memset((uint8*) ptr + aSize, 0, size - aSize);
mReadPastEnd = true;
}
return aSize;
}
}
void FileStream::Write(void* ptr, int size)
int FileStream::Write(void* ptr, int size)
{
fwrite(ptr, 1, size, mFP);
return (int)fwrite(ptr, 1, size, mFP);
}
int FileStream::GetPos()
@ -250,7 +259,7 @@ int SysFileStream::GetSize()
return (int)BfpFile_GetFileSize(mFile);
}
void SysFileStream::Read(void* ptr, int size)
int SysFileStream::Read(void* ptr, int size)
{
int readSize = (int)BfpFile_Read(mFile, ptr, size, -1, NULL);
if (readSize != size)
@ -258,11 +267,12 @@ void SysFileStream::Read(void* ptr, int size)
// Zero out underflow bytes
memset((uint8*)ptr + readSize, 0, size - readSize);
}
return readSize;
}
void SysFileStream::Write(void* ptr, int size)
int SysFileStream::Write(void* ptr, int size)
{
BfpFile_Write(mFile, ptr, size, -1, NULL);
return (int)BfpFile_Write(mFile, ptr, size, -1, NULL);
}
int SysFileStream::GetPos()

View file

@ -28,9 +28,9 @@ public:
bool Eof() override;
int GetSize() override;
using DataStream::Read;
void Read(void* ptr, int size) override;
int Read(void* ptr, int size) override;
using DataStream::Write;
void Write(void* ptr, int size) override;
int Write(void* ptr, int size) override;
int GetPos() override;
void Seek(int size) override;
@ -67,9 +67,9 @@ public:
bool Eof() override;
int GetSize() override;
using DataStream::Read;
void Read(void* ptr, int size) override;
int Read(void* ptr, int size) override;
using DataStream::Write;
void Write(void* ptr, int size) override;
int Write(void* ptr, int size) override;
int GetPos() override;
void Seek(int size) override;

View file

@ -44,17 +44,19 @@ int MemStream::GetSize()
return mSize;
}
void MemStream::Read(void* ptr, int size)
int MemStream::Read(void* ptr, int size)
{
memcpy(ptr, mData + mPos, size);
mPos += size;
return size;
}
void Beefy::MemStream::Write(void* ptr, int size)
int Beefy::MemStream::Write(void* ptr, int size)
{
memcpy(mData + mPos, ptr, size);
mPos += size;
return size;
}
int MemStream::GetPos()
@ -79,7 +81,7 @@ SafeMemStream::SafeMemStream(void* data, int size, bool freeMemory) : MemStream(
mFailed = false;
}
void SafeMemStream::Read(void* ptr, int size)
int SafeMemStream::Read(void* ptr, int size)
{
if (mPos + size > mSize)
{
@ -91,6 +93,7 @@ void SafeMemStream::Read(void* ptr, int size)
memcpy(ptr, mData + mPos, size);
}
mPos += size;
return size;
}
//////////////////////////////////////////////////////////////////////////
@ -110,16 +113,18 @@ int DynMemStream::GetSize()
return (int)mData.size();
}
void DynMemStream::Read(void* ptr, int size)
int DynMemStream::Read(void* ptr, int size)
{
memcpy(ptr, (uint8*)&mData.front() + mPos, size);
mPos += size;
return size;
}
void DynMemStream::Write(void* ptr, int size)
int DynMemStream::Write(void* ptr, int size)
{
mData.Insert(mPos, (uint8*)ptr, size);
mPos += size;
return size;
}
void DynMemStream::Write(uint8 val)

View file

@ -24,9 +24,9 @@ public:
bool Eof() override;
int GetSize() override;
using DataStream::Read;
void Read(void* ptr, int size) override;
int Read(void* ptr, int size) override;
using DataStream::Write;
void Write(void* ptr, int size) override;
int Write(void* ptr, int size) override;
int GetPos() override;
void Seek(int size) override;
@ -41,7 +41,7 @@ public:
SafeMemStream(void* data, int size, bool freeMemory);
using DataStream::Read;
void Read(void* ptr, int size) override;
int Read(void* ptr, int size) override;
};
class DynMemStream : public DataStream
@ -56,9 +56,9 @@ public:
bool Eof() override;
int GetSize() override;
using DataStream::Read;
void Read(void* ptr, int size) override;
int Read(void* ptr, int size) override;
using DataStream::Write;
void Write(void* ptr, int size) override;
int Write(void* ptr, int size) override;
void Write(uint8 val) override;
int GetPos() override;

View file

@ -201,7 +201,7 @@ public:
bool mClipped;
bool mTexWrap;
bool mWireframe;
Rect mClipRect;
RectF mClipRect;
CullMode mCullMode;
Topology3D mTopology;
@ -213,7 +213,7 @@ public:
virtual void SetTexWrap(bool wrap) { mTexWrap = wrap; }
virtual void SetWireframe(bool wireframe) { mWireframe = wireframe; }
virtual void SetClipped(bool clipped) { mClipped = clipped; }
virtual void SetClipRect(const Rect& rect) { mClipRect = rect; }
virtual void SetClipRect(const RectF& rect) { mClipRect = rect; }
virtual void SetWriteDepthBuffer(bool writeDepthBuffer) { mWriteDepthBuffer = writeDepthBuffer; }
virtual void SetDepthFunc(DepthFunc depthFunc) { mDepthFunc = depthFunc; }
virtual void SetTopology(Topology3D topology) { mTopology = topology; }

View file

@ -71,11 +71,11 @@ void TextureSegment::SetImageData(ImageData& imageData)
SetBits(0, 0, imageData.mWidth, imageData.mHeight, imageData.mStride, imageData.mBits);
}
Rect TextureSegment::GetRect()
RectF TextureSegment::GetRect()
{
float x1 = mU1 * mTexture->mWidth;
float x2 = mU2 * mTexture->mWidth;
float y1 = mV1 * mTexture->mHeight;
float y2 = mV2 * mTexture->mHeight;
return Rect(x1, y1, x2 - x1, y2 - y1);
return RectF(x1, y1, x2 - x1, y2 - y1);
}

View file

@ -47,7 +47,7 @@ public:
void GetImageData(ImageData& imageData, int destX, int destY);
void SetImageData(ImageData& imageData);
Rect GetRect();
RectF GetRect();
};
NS_BF_END;

View file

@ -737,9 +737,9 @@ void GLRenderDevice::PhysSetRenderState(RenderState* renderState)
if (renderState->mClipped)
{
glEnable(GL_SCISSOR_TEST);
glScissor((GLsizei)renderState->mClipRect.mX,
mPhysRenderWindow->mHeight - (GLsizei)renderState->mClipRect.mY - (GLsizei)renderState->mClipRect.mHeight,
(GLsizei)renderState->mClipRect.mWidth, (GLsizei)renderState->mClipRect.mHeight);
glScissor((GLsizei)renderState->mClipRect.x,
mPhysRenderWindow->mHeight - (GLsizei)renderState->mClipRect.y - (GLsizei)renderState->mClipRect.height,
(GLsizei)renderState->mClipRect.width, (GLsizei)renderState->mClipRect.height);
}
else
{

View file

@ -762,11 +762,11 @@ void DXDrawBatch::Render(RenderDevice* renderDevice, RenderWindow* renderWindow)
return;
if ((mRenderState->mClipped) &&
((mRenderState->mClipRect.mWidth == 0) || (mRenderState->mClipRect.mHeight == 0)))
((mRenderState->mClipRect.width == 0) || (mRenderState->mClipRect.height == 0)))
return;
if (mRenderState->mClipped)
BF_ASSERT((mRenderState->mClipRect.mWidth > 0) && (mRenderState->mClipRect.mHeight > 0));
BF_ASSERT((mRenderState->mClipRect.width > 0) && (mRenderState->mClipRect.height > 0));
DXRenderDevice* aRenderDevice = (DXRenderDevice*)renderDevice;
/*if ((mDrawLayer->mRenderWindow != NULL) && (aRenderDevice->mPhysRenderWindow != mDrawLayer->mRenderWindow))
@ -931,10 +931,10 @@ void DXRenderDevice::PhysSetRenderState(RenderState* renderState)
if (renderState->mClipped)
{
D3D11_RECT rects[1];
rects[0].left = (int)renderState->mClipRect.mX;
rects[0].right = (int) (renderState->mClipRect.mX + renderState->mClipRect.mWidth);
rects[0].top = (int) renderState->mClipRect.mY;
rects[0].bottom = (int) (renderState->mClipRect.mY + renderState->mClipRect.mHeight);
rects[0].left = (int)renderState->mClipRect.x;
rects[0].right = (int) (renderState->mClipRect.x + renderState->mClipRect.width);
rects[0].top = (int) renderState->mClipRect.y;
rects[0].bottom = (int) (renderState->mClipRect.y + renderState->mClipRect.height);
mD3DDeviceContext->RSSetScissorRects(1, rects);
}
setRasterizerState = true;
@ -1383,9 +1383,9 @@ void DXRenderState::SetTexWrap(bool wrap)
InvalidateRasterizerState();
}
void DXRenderState::SetClipRect(const Rect& rect)
void DXRenderState::SetClipRect(const RectF& rect)
{
BF_ASSERT((rect.mWidth >= 0) && (rect.mHeight >= 0));
BF_ASSERT((rect.width >= 0) && (rect.height >= 0));
mClipRect = rect;
InvalidateRasterizerState();
}

View file

@ -219,7 +219,7 @@ public:
virtual void SetClipped(bool clipped);
virtual void SetTexWrap(bool clipped);
virtual void SetClipRect(const Rect& rect);
virtual void SetClipRect(const RectF& rect);
virtual void SetWriteDepthBuffer(bool writeDepthBuffer);
virtual void SetDepthFunc(DepthFunc depthFunc);
};

View file

@ -3016,6 +3016,9 @@ BFP_EXPORT BfpFile* BFP_CALLTYPE BfpFile_Create(const char* path, BfpFileCreateK
if ((createFlags & BfpFileCreateFlag_NoBuffering) != 0)
desiredAccess |= FILE_FLAG_NO_BUFFERING;
if ((createFlags & BfpFileCreateFlag_AllowTimeouts) != 0)
attributes |= FILE_FLAG_OVERLAPPED;
HANDLE handle = ::CreateFileW(wPath.c_str(), desiredAccess, shareMode, NULL, creationDisposition, attributes, NULL);
if (handle == INVALID_HANDLE_VALUE)
{
@ -3043,6 +3046,13 @@ BFP_EXPORT BfpFile* BFP_CALLTYPE BfpFile_Create(const char* path, BfpFileCreateK
BfpFile* bfpFile = new BfpFile();
bfpFile->mHandle = handle;
if ((createFlags & BfpFileCreateFlag_AllowTimeouts) != 0)
bfpFile->mAsyncData = new BfpAsyncData();
if ((createFlags & BfpFileCreateFlag_Pipe) != 0)
bfpFile->mIsPipe = true;
return bfpFile;
}

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,8 +44,7 @@ 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;
}
};