From 240bfa8ed5022627de0d4b256c67296fd2211f1a Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 20 Nov 2022 02:35:02 +0100 Subject: [PATCH] Rewrote render_paint_node now it works without leaving pixels out, but it is an extermely inefficient implementation. It will be reworked soon. --- modules/paint/nodes/paint_node.cpp | 63 ++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 13 deletions(-) diff --git a/modules/paint/nodes/paint_node.cpp b/modules/paint/nodes/paint_node.cpp index 07ad1e60a..928832ac6 100644 --- a/modules/paint/nodes/paint_node.cpp +++ b/modules/paint/nodes/paint_node.cpp @@ -1,6 +1,7 @@ #include "paint_node.h" #include "../paint_utilities.h" +#include "core/math/geometry.h" #include "paint_project.h" Vector2i PaintNode::get_size() { @@ -106,24 +107,60 @@ void PaintNode::render_paint_node(PaintNode *node, Transform2D transform, Refget_size(); Vector2i image_size = image->get_size(); + // NOTE: this is EXTREMELY inefficient, and slow, however it does work for now. + // It will be rewritten soon. + + Vector2 p0 = transform.xform(Vector2(0, 0)); + Vector2 p1 = transform.xform(Vector2(0, save_image_size.y)); + Vector2 p2 = transform.xform(Vector2(save_image_size.x, 0)); + Vector2 p3 = transform.xform(Vector2(save_image_size.x, save_image_size.y)); + + Vector queued_points = Geometry::brenzenham_line(p0.x, p1.x, p0.y, p1.y); + queued_points.append_array(Geometry::brenzenham_line(p1.x, p3.x, p1.y, p3.y)); + queued_points.append_array(Geometry::brenzenham_line(p2.x, p3.x, p2.y, p3.y)); + queued_points.append_array(Geometry::brenzenham_line(p0.x, p2.x, p0.y, p2.y)); + + Vector points_to_check; + + for (int i = 0; i < queued_points.size(); ++i) { + Point2i p = queued_points[i]; + + Vector points = Geometry::brenzenham_line(p0.x, p.x, p0.y, p.y); + points.append_array(Geometry::brenzenham_line(p3.x, p.x, p3.y, p.y)); + points.append_array(Geometry::brenzenham_line(p2.x, p.x, p2.y, p.y)); + points.append_array(Geometry::brenzenham_line(p1.x, p.x, p1.y, p.y)); + + for (int j = 0; j < points.size(); ++j) { + Point2i pc = points[j]; + + if (points_to_check.find(pc) == -1) { + points_to_check.push_back(pc); + } + } + } + + Transform2D inverse = transform.affine_inverse(); + save_image->lock(); image->lock(); - //should return early eventually via a simple overlap check here + for (int i = 0; i < points_to_check.size(); ++i) { + Point2i p = points_to_check[i]; - for (int x = 0; x < save_image_size.x; ++x) { - for (int y = 0; y < save_image_size.y; ++y) { - Vector2i npos = transform.xform(Vector2(x, y)); - - if (npos.x < 0 || npos.y < 0 || npos.x >= image_size.x || npos.y >= image_size.y) { - continue; - } - - Color sic = save_image->get_pixel(x, y); - Color oic = image->get_pixel(npos.x, npos.y); - - image->set_pixel(npos.x, npos.y, oic.blend(sic)); + if (p.x < 0 || p.y < 0 || p.x >= image_size.x || p.y >= image_size.y) { + continue; } + + Vector2 npos = inverse.xform(Vector2(p.x, p.y)); + + if (npos.x < 0 || npos.y < 0 || npos.x >= save_image_size.x || npos.y >= save_image_size.y) { + continue; + } + + Color sic = save_image->get_pixel(npos.x, npos.y); + Color oic = image->get_pixel(p.x, p.y); + + image->set_pixel(p.x, p.y, oic.blend(sic)); } image->unlock();