1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-02-02 03:20:06 +00:00

EdgeTable: Implement using vector to simplify special member functions

This commit is contained in:
reuk 2024-05-02 14:35:48 +01:00
parent 631b2ea2ba
commit 417d97b36e
No known key found for this signature in database
GPG key ID: FCB43929F012EE5C
2 changed files with 30 additions and 60 deletions

View file

@ -47,7 +47,7 @@ EdgeTable::EdgeTable (Rectangle<int> area, const Path& path, const AffineTransfo
lineStrideElements (maxEdgesPerLine * 2 + 1)
{
allocate();
int* t = table;
int* t = table.data();
for (int i = bounds.getHeight(); --i >= 0;)
{
@ -125,7 +125,7 @@ EdgeTable::EdgeTable (Rectangle<int> rectangleToAdd)
auto x1 = scale * rectangleToAdd.getX();
auto x2 = scale * rectangleToAdd.getRight();
int* t = table;
int* t = table.data();
for (int i = rectangleToAdd.getHeight(); --i >= 0;)
{
@ -227,7 +227,7 @@ EdgeTable::EdgeTable (Rectangle<float> rectangleToAdd)
}
int lineY = 0;
int* t = table;
int* t = table.data();
if ((y1 / scale) == (y2 / scale))
{
@ -278,27 +278,6 @@ EdgeTable::EdgeTable (Rectangle<float> rectangleToAdd)
}
}
EdgeTable::EdgeTable (const EdgeTable& other)
{
operator= (other);
}
EdgeTable& EdgeTable::operator= (const EdgeTable& other)
{
bounds = other.bounds;
maxEdgesPerLine = other.maxEdgesPerLine;
lineStrideElements = other.lineStrideElements;
needToCheckEmptiness = other.needToCheckEmptiness;
allocate();
copyEdgeTableData (table, lineStrideElements, other.table, lineStrideElements, bounds.getHeight());
return *this;
}
EdgeTable::~EdgeTable()
{
}
//==============================================================================
static size_t getEdgeTableAllocationSize (int lineStride, int height) noexcept
{
@ -308,12 +287,12 @@ static size_t getEdgeTableAllocationSize (int lineStride, int height) noexcept
void EdgeTable::allocate()
{
table.malloc (getEdgeTableAllocationSize (lineStrideElements, bounds.getHeight()));
table.resize (getEdgeTableAllocationSize (lineStrideElements, bounds.getHeight()));
}
void EdgeTable::clearLineSizes() noexcept
{
int* t = table;
int* t = table.data();
for (int i = bounds.getHeight(); --i >= 0;)
{
@ -335,7 +314,7 @@ void EdgeTable::copyEdgeTableData (int* dest, int destLineStride, const int* src
void EdgeTable::sanitiseLevels (const bool useNonZeroWinding) noexcept
{
// Convert the table from relative windings to absolute levels..
int* lineStart = table;
int* lineStart = table.data();
for (int y = bounds.getHeight(); --y >= 0;)
{
@ -405,11 +384,11 @@ void EdgeTable::remapTableForNumEdges (const int newNumEdgesPerLine)
jassert (bounds.getHeight() > 0);
auto newLineStrideElements = maxEdgesPerLine * 2 + 1;
HeapBlock<int> newTable (getEdgeTableAllocationSize (newLineStrideElements, bounds.getHeight()));
std::vector<int> newTable (getEdgeTableAllocationSize (newLineStrideElements, bounds.getHeight()));
copyEdgeTableData (newTable, newLineStrideElements, table, lineStrideElements, bounds.getHeight());
copyEdgeTableData (newTable.data(), newLineStrideElements, table.data(), lineStrideElements, bounds.getHeight());
table.swapWith (newTable);
table = std::move (newTable);
lineStrideElements = newLineStrideElements;
}
}
@ -425,7 +404,7 @@ void EdgeTable::optimiseTable()
int maxLineElements = 0;
for (int i = bounds.getHeight(); --i >= 0;)
maxLineElements = jmax (maxLineElements, table[i * lineStrideElements]);
maxLineElements = jmax (maxLineElements, table[(size_t) i * (size_t) lineStrideElements]);
remapTableForNumEdges (maxLineElements);
}
@ -434,13 +413,13 @@ void EdgeTable::addEdgePoint (const int x, const int y, const int winding)
{
jassert (y >= 0 && y < bounds.getHeight());
auto* line = table + lineStrideElements * y;
auto* line = table.data() + lineStrideElements * y;
auto numPoints = line[0];
if (numPoints >= maxEdgesPerLine)
{
remapWithExtraSpace (numPoints);
line = table + lineStrideElements * y;
line = table.data() + lineStrideElements * y;
}
line[0] = numPoints + 1;
@ -453,13 +432,13 @@ void EdgeTable::addEdgePointPair (int x1, int x2, int y, int winding)
{
jassert (y >= 0 && y < bounds.getHeight());
auto* line = table + lineStrideElements * y;
auto* line = table.data() + lineStrideElements * y;
auto numPoints = line[0];
if (numPoints + 1 >= maxEdgesPerLine)
{
remapWithExtraSpace (numPoints + 1);
line = table + lineStrideElements * y;
line = table.data() + lineStrideElements * y;
}
line[0] = numPoints + 2;
@ -474,7 +453,7 @@ void EdgeTable::translate (float dx, int dy) noexcept
{
bounds.translate ((int) std::floor (dx), dy);
int* lineStart = table;
int* lineStart = table.data();
auto intDx = (int) (dx * 256.0f);
for (int i = bounds.getHeight(); --i >= 0;)
@ -493,7 +472,7 @@ void EdgeTable::translate (float dx, int dy) noexcept
void EdgeTable::multiplyLevels (float amount)
{
int* lineStart = table;
int* lineStart = table.data();
auto multiplier = (int) (amount * 256.0f);
for (int y = 0; y < bounds.getHeight(); ++y)
@ -514,7 +493,7 @@ void EdgeTable::intersectWithEdgeTableLine (const int y, const int* const otherL
{
jassert (y >= 0 && y < bounds.getHeight());
auto* srcLine = table + lineStrideElements * y;
auto* srcLine = table.data() + lineStrideElements * y;
auto srcNum1 = *srcLine;
if (srcNum1 == 0)
@ -599,16 +578,16 @@ void EdgeTable::intersectWithEdgeTableLine (const int y, const int* const otherL
memcpy (oldTemp, src1, tempSize);
remapTableForNumEdges (jmax (256, destTotal * 2));
srcLine = table + lineStrideElements * y;
srcLine = table.data() + lineStrideElements * y;
auto* newTemp = table + lineStrideElements * bounds.getHeight();
auto* newTemp = table.data() + lineStrideElements * bounds.getHeight();
memcpy (newTemp, oldTemp, tempSize);
src1 = newTemp;
}
else
{
remapTableForNumEdges (jmax (256, destTotal * 2));
srcLine = table + lineStrideElements * y;
srcLine = table.data() + lineStrideElements * y;
}
}
@ -618,7 +597,7 @@ void EdgeTable::intersectWithEdgeTableLine (const int y, const int* const otherL
if (! isUsingTempSpace)
{
isUsingTempSpace = true;
auto* temp = table + lineStrideElements * bounds.getHeight();
auto* temp = table.data() + lineStrideElements * bounds.getHeight();
memcpy (temp, src1, (size_t) srcNum1 * 2 * sizeof (int));
src1 = temp;
}
@ -635,7 +614,7 @@ void EdgeTable::intersectWithEdgeTableLine (const int y, const int* const otherL
{
srcLine[0] = destTotal;
remapTableForNumEdges (jmax (256, destTotal * 2));
srcLine = table + lineStrideElements * y;
srcLine = table.data() + lineStrideElements * y;
}
++destTotal;
@ -705,13 +684,13 @@ void EdgeTable::clipToRectangle (Rectangle<int> r)
bounds.setHeight (bottom);
for (int i = 0; i < top; ++i)
table[lineStrideElements * i] = 0;
table[(size_t) lineStrideElements * (size_t) i] = 0;
if (clipped.getX() > bounds.getX() || clipped.getRight() < bounds.getRight())
{
auto x1 = scale * clipped.getX();
auto x2 = scale * jmin (bounds.getRight(), clipped.getRight());
int* line = table + lineStrideElements * top;
int* line = table.data() + lineStrideElements * top;
for (int i = bottom - top; --i >= 0;)
{
@ -768,9 +747,9 @@ void EdgeTable::clipToEdgeTable (const EdgeTable& other)
bounds.setRight (clipped.getRight());
for (int i = 0; i < top; ++i)
table[lineStrideElements * i] = 0;
table[(size_t) lineStrideElements * (size_t) i] = 0;
auto* otherLine = other.table + other.lineStrideElements * (clipped.getY() - other.bounds.getY());
auto* otherLine = other.table.data() + other.lineStrideElements * (clipped.getY() - other.bounds.getY());
for (int i = top; i < bottom; ++i)
{
@ -793,7 +772,7 @@ void EdgeTable::clipLineToMask (int x, int y, const uint8* mask, int maskStride,
if (numPixels <= 0)
{
table[lineStrideElements * y] = 0;
table[(size_t) lineStrideElements * (size_t) y] = 0;
return;
}
@ -831,7 +810,7 @@ bool EdgeTable::isEmpty() noexcept
if (needToCheckEmptiness)
{
needToCheckEmptiness = false;
int* t = table;
int* t = table.data();
for (int i = bounds.getHeight(); --i >= 0;)
{

View file

@ -72,15 +72,6 @@ public:
/** Creates an edge table containing a rectangle list. */
explicit EdgeTable (const RectangleList<float>& rectanglesToAdd);
/** Creates a copy of another edge table. */
EdgeTable (const EdgeTable&);
/** Copies from another edge table. */
EdgeTable& operator= (const EdgeTable&);
/** Destructor. */
~EdgeTable();
//==============================================================================
void clipToRectangle (Rectangle<int> r);
void excludeRectangle (Rectangle<int> r);
@ -120,7 +111,7 @@ public:
template <class EdgeTableIterationCallback>
void iterate (EdgeTableIterationCallback& iterationCallback) const noexcept
{
const int* lineStart = table;
const int* lineStart = table.data();
for (int y = 0; y < bounds.getHeight(); ++y)
{
@ -213,7 +204,7 @@ private:
bool operator< (const LineItem& other) const noexcept { return x < other.x; }
};
HeapBlock<int> table;
std::vector<int> table;
Rectangle<int> bounds;
int maxEdgesPerLine, lineStrideElements;
bool needToCheckEmptiness = true;