Skip to content

Use CPU texture in contains methods #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/cputexturemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ const std::vector<QPoint> &CpuTextureManager::getTextureConvexHullPoints(const T
return it->second;
}

bool CpuTextureManager::textureContainsPoint(const Texture &texture, const QPointF &localPoint)
{
// https://github.com/scratchfoundation/scratch-render/blob/7b823985bc6fe92f572cc3276a8915e550f7c5e6/src/Silhouette.js#L219-L226
return getPointAlpha(texture, localPoint.x(), localPoint.y()) > 0;
}

void CpuTextureManager::removeTexture(const Texture &texture)
{
if (!texture.isValid())
Expand Down Expand Up @@ -131,3 +137,12 @@ bool CpuTextureManager::addTexture(const Texture &texture)

return true;
}

int CpuTextureManager::getPointAlpha(const Texture &texture, int x, int y)
{
if ((x < 0 || x >= texture.width()) || (y < 0 || y >= texture.height()))
return 0;

GLubyte *pixels = getTextureData(texture);
return pixels[(y * texture.width() + x) * 4 + 3];
}
3 changes: 3 additions & 0 deletions src/cputexturemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ class CpuTextureManager
GLubyte *getTextureData(const Texture &texture);
const std::vector<QPoint> &getTextureConvexHullPoints(const Texture &texture);

bool textureContainsPoint(const Texture &texture, const QPointF &localPoint);

void removeTexture(const Texture &texture);

private:
bool addTexture(const Texture &texture);
int getPointAlpha(const Texture &texture, int x, int y);

std::unordered_map<GLuint, GLubyte *> m_textureData;
std::unordered_map<GLuint, std::vector<QPoint>> m_convexHullPoints;
Expand Down
15 changes: 1 addition & 14 deletions src/renderedtarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -814,20 +814,7 @@ void RenderedTarget::updateHullPoints()

bool RenderedTarget::containsLocalPoint(const QPointF &point) const
{
if (!boundingRect().contains(point))
return false;

const std::vector<QPoint> &points = hullPoints();
QPoint intPoint = point.toPoint();
auto it = std::lower_bound(points.begin(), points.end(), intPoint, [](const QPointF &lhs, const QPointF &rhs) { return (lhs.y() < rhs.y()) || (lhs.y() == rhs.y() && lhs.x() < rhs.x()); });

if (it == points.end()) {
// The point is beyond the last point in the convex hull
return false;
}

// Check if the point is equal to the one found
return *it == intPoint;
return textureManager()->textureContainsPoint(m_cpuTexture, point);
}

QPointF RenderedTarget::transformPoint(double scratchX, double scratchY, double originX, double originY, double rot) const
Expand Down
24 changes: 12 additions & 12 deletions test/renderedtarget/renderedtarget_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,12 @@ TEST_F(RenderedTargetTest, CpuRendering)
ASSERT_TRUE(target.contains({ 1, 2 }));
ASSERT_FALSE(target.contains({ 2, 2 }));
ASSERT_TRUE(target.contains({ 3, 2 }));
ASSERT_FALSE(target.contains({ 3.5, 2.1 }));
ASSERT_TRUE(target.contains({ 3.5, 2.1 }));

ASSERT_TRUE(target.contains({ 1, 3 }));
ASSERT_TRUE(target.contains({ 2, 3 }));
ASSERT_TRUE(target.contains({ 3, 3 }));
ASSERT_FALSE(target.contains({ 3.3, 3.5 }));
ASSERT_TRUE(target.contains({ 3.3, 3.5 }));

// Test contains() with horizontal mirroring
target.updateRotationStyle(Sprite::RotationStyle::LeftRight);
Expand All @@ -408,9 +408,9 @@ TEST_F(RenderedTargetTest, CpuRendering)
ASSERT_TRUE(target.contains({ -1, 1 }));
ASSERT_FALSE(target.contains({ -2, 2 }));
ASSERT_TRUE(target.contains({ -3, 2 }));
ASSERT_FALSE(target.contains({ -3.5, 2.1 }));
ASSERT_TRUE(target.contains({ -3.5, 2.1 }));
ASSERT_TRUE(target.contains({ -2, 3 }));
ASSERT_FALSE(target.contains({ -3.3, 3.5 }));
ASSERT_TRUE(target.contains({ -3.3, 3.5 }));

// Test containsScratchPoint()
target.updateDirection(0);
Expand All @@ -425,15 +425,15 @@ TEST_F(RenderedTargetTest, CpuRendering)
ASSERT_TRUE(target.containsScratchPoint(-226, 164)); // [2, 1]
ASSERT_TRUE(target.containsScratchPoint(-225, 164)); // [3, 1]

ASSERT_TRUE(target.containsScratchPoint(-227, 163)); // [1, 2]
ASSERT_FALSE(target.containsScratchPoint(-226, 163)); // [2, 2]
ASSERT_TRUE(target.containsScratchPoint(-225, 163)); // [3, 2]
ASSERT_FALSE(target.containsScratchPoint(-224.5, 162.9)); // [3.5, 2.1]
ASSERT_TRUE(target.containsScratchPoint(-227, 163)); // [1, 2]
ASSERT_FALSE(target.containsScratchPoint(-226, 163)); // [2, 2]
ASSERT_TRUE(target.containsScratchPoint(-225, 163)); // [3, 2]
ASSERT_TRUE(target.containsScratchPoint(-224.5, 162.9)); // [3.5, 2.1]

ASSERT_TRUE(target.containsScratchPoint(-227, 162)); // [1, 3]
ASSERT_TRUE(target.containsScratchPoint(-226, 162)); // [2, 3]
ASSERT_TRUE(target.containsScratchPoint(-225, 162)); // [3, 3]
ASSERT_FALSE(target.containsScratchPoint(-224.7, 161.5)); // [3.3, 3.5]
ASSERT_TRUE(target.containsScratchPoint(-227, 162)); // [1, 3]
ASSERT_TRUE(target.containsScratchPoint(-226, 162)); // [2, 3]
ASSERT_TRUE(target.containsScratchPoint(-225, 162)); // [3, 3]
ASSERT_TRUE(target.containsScratchPoint(-224.7, 161.5)); // [3.3, 3.5]

// Test colorAtScratchPoint()
ASSERT_EQ(target.colorAtScratchPoint(-228, 165), 0); // [0, 0]
Expand Down
41 changes: 41 additions & 0 deletions test/texture/cputexturemanager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,44 @@ TEST_F(CpuTextureManagerTest, TextureDataAndHullPoints)
// Cleanup
context.doneCurrent();
}

TEST_F(CpuTextureManagerTest, TextureContainsPoint)
{
// Create OpenGL context
QOpenGLContext context;
QOffscreenSurface surface;
createContextAndSurface(&context, &surface);

// Paint
QNanoPainter painter;
ImagePainter imgPainter(&painter, "image.png");

// Read texture data
Texture texture(imgPainter.fbo()->texture(), imgPainter.fbo()->size());

// Test
CpuTextureManager manager;
ASSERT_FALSE(manager.textureContainsPoint(texture, { 0, 0 }));
ASSERT_FALSE(manager.textureContainsPoint(texture, { 1, 0 }));
ASSERT_FALSE(manager.textureContainsPoint(texture, { 2, 0 }));
ASSERT_FALSE(manager.textureContainsPoint(texture, { 3, 0 }));

ASSERT_FALSE(manager.textureContainsPoint(texture, { 0, 1 }));
ASSERT_TRUE(manager.textureContainsPoint(texture, { 1, 1 }));
ASSERT_TRUE(manager.textureContainsPoint(texture, { 1.4, 1.25 }));
ASSERT_TRUE(manager.textureContainsPoint(texture, { 2, 1 }));
ASSERT_TRUE(manager.textureContainsPoint(texture, { 3, 1 }));

ASSERT_TRUE(manager.textureContainsPoint(texture, { 1, 2 }));
ASSERT_FALSE(manager.textureContainsPoint(texture, { 2, 2 }));
ASSERT_TRUE(manager.textureContainsPoint(texture, { 3, 2 }));
ASSERT_TRUE(manager.textureContainsPoint(texture, { 3.5, 2.1 }));

ASSERT_TRUE(manager.textureContainsPoint(texture, { 1, 3 }));
ASSERT_TRUE(manager.textureContainsPoint(texture, { 2, 3 }));
ASSERT_TRUE(manager.textureContainsPoint(texture, { 3, 3 }));
ASSERT_TRUE(manager.textureContainsPoint(texture, { 3.3, 3.5 }));

// Cleanup
context.doneCurrent();
}
Loading