Skip to content

OpenToonz - An open-source full-featured 2D animation creation software

C

Fix(autoclose): Add line detect when drawing gap close lines with define region with paint on

Abel You10adb4558e73
diff --git a/toonz/sources/toonzlib/autoclose.cpp b/toonz/sources/toonzlib/autoclose.cpp
index 9e5834ca9..d1d261abe 100644
--- a/toonz/sources/toonzlib/autoclose.cpp
+++ b/toonz/sources/toonzlib/autoclose.cpp
@@ -250,6 +250,28 @@ const bool isSmallEnclosedRegion(const TRasterCM32P &ras, int x, int y,
return visited.size() <= maxSize;
}
+
+bool doSegmentsIntersect(int x1, int y1, int x2, int y2, int x3, int y3, int x4,
+ int y4) {
+ auto cross = [](int x1, int y1, int x2, int y2) -> int {
+ return x1 * y2 - y1 * x2;
+ };
+
+ int dx1 = x2 - x1;
+ int dy1 = y2 - y1;
+ int dx2 = x4 - x3;
+ int dy2 = y4 - y3;
+
+ int d1 = cross(dx2, dy2, x1 - x3, y1 - y3);
+ int d2 = cross(dx2, dy2, x2 - x3, y2 - y3);
+ int d3 = cross(dx1, dy1, x3 - x1, y3 - y1);
+ int d4 = cross(dx1, dy1, x4 - x1, y4 - y1);
+
+ if (d1 == 0 || d2 == 0 || d3 == 0 || d4 == 0) return false;
+
+ return (d1 * d2 < 0) && (d3 * d4 < 0);
+}
+
// For Paint defined Region
void closeSegment(const TRasterCM32P &r, const TAutocloser::Segment &s,
const USHORT ink, const USHORT opacity) {
@@ -294,7 +316,7 @@ void closeSegment(const TRasterCM32P &r, const TAutocloser::Segment &s,
int x = x1;
int y = y1;
std::vector<TPoint> points;
-
+ int lastX = x, lastY = y;
while (true) {
TPixelCM32 *pix = r->pixels(y) + x;
// Only check side pixels if current line pixel is purePaint
@@ -302,24 +324,95 @@ void closeSegment(const TRasterCM32P &r, const TAutocloser::Segment &s,
pix->setInk(ink);
pix->setTone(255 - opacity);
bool shouldKeep = true;
- int linePaint = pix->getPaint();
+ int linePaint = pix->getPaint();
int sx1 = x + nx;
int sy1 = y + ny;
int side1Paint = -1;
+ int side1Tone = -1;
if (sx1 >= 0 && sx1 < r->getLx() && sy1 >= 0 && sy1 < r->getLy()) {
side1Paint = r->pixels(sy1)[sx1].getPaint();
+ side1Tone = r->pixels(sy1)[sx1].getTone();
}
int sx2 = x - nx;
int sy2 = y - ny;
int side2Paint = -1;
+ int side2Tone = -1;
if (sx2 >= 0 && sx2 < r->getLx() && sy2 >= 0 && sy2 < r->getLy()) {
+ side2Tone = r->pixels(sy2)[sx2].getTone();
side2Paint = r->pixels(sy2)[sx2].getPaint();
}
+ bool connected = false;
+ if (side1Tone == side2Tone && side1Tone == TPixelCM32::getMaxTone() &&
+ side1Paint == side2Paint && linePaint == side1Paint) {
+ // Check if Direct Connected
+ connected = true;
+ } else if (side1Tone != side2Tone &&
+ (side1Tone == TPixelCM32::getMaxTone() ||
+ side2Tone == TPixelCM32::getMaxTone())) {
+ // Check if Cornor Connected
+ int side3Tone = -1;
+ int side4Tone = -1;
+ int side3Paint = -1;
+ int side4Paint = -1;
+
+ int sx3 = x + ny;
+ int sy3 = y + nx;
+ if (sx3 >= 0 && sx3 < r->getLx() && sy3 >= 0 && sy3 < r->getLy()) {
+ side3Tone = r->pixels(sy3)[sx3].getTone();
+ side3Paint = r->pixels(sy3)[sx3].getPaint();
+ }
+
+ int sx4 = x - ny;
+ int sy4 = y - nx;
+ if (sx4 >= 0 && sx4 < r->getLx() && sy4 >= 0 && sy4 < r->getLy()) {
+ side4Tone = r->pixels(sy4)[sx4].getTone();
+ side4Paint = r->pixels(sy4)[sx4].getPaint();
+ }
- bool notEdge = (side1Paint == side2Paint && linePaint == side1Paint);
- if (notEdge) {
+ const int MAX_TONE = TPixelCM32::getMaxTone();
+
+ int nextX = x, nextY = y;
+ int nextErr = err;
+ int next_e2 = 2 * nextErr;
+
+ bool moveX = (next_e2 > -abs_dy);
+ bool moveY = (next_e2 < abs_dx);
+
+ if (moveX) {
+ nextErr -= abs_dy;
+ nextX += sx;
+ }
+ if (moveY) {
+ nextErr += abs_dx;
+ nextY += sy;
+ }
+
+ if (side1Tone > side2Tone) {
+ bool intersect3 = doSegmentsIntersect(lastX, lastY, nextX, nextY, sx1,
+ sy1, sx3, sy3);
+
+ connected |= (side1Paint == linePaint && side3Tone == MAX_TONE &&
+ side3Paint == linePaint && intersect3);
+
+ bool intersect4 = doSegmentsIntersect(lastX, lastY, nextX, nextY, sx1,
+ sy1, sx4, sy4);
+ connected |= (side1Paint == linePaint && side4Tone == MAX_TONE &&
+ side4Paint == linePaint && intersect4);
+ } else {
+ bool intersect3 = doSegmentsIntersect(lastX, lastY, nextX, nextY, sx2,
+ sy2, sx3, sy3);
+ connected |= (side2Paint == linePaint && side3Tone == MAX_TONE &&
+ side3Paint == linePaint && intersect3);
+
+ bool intersect4 = doSegmentsIntersect(lastX, lastY, nextX, nextY, sx2,
+ sy2, sx4, sy4);
+ connected |= (side2Paint == linePaint && side4Tone == MAX_TONE &&
+ side4Paint == linePaint && intersect4);
+ }
+ }
+ if (connected) {
bool side1Small = isSmallEnclosedRegion(r, sx1, sy1, 4);
bool side2Small = isSmallEnclosedRegion(r, sx2, sy2, 4);
if (side1Small || side2Small) {
@@ -336,7 +429,8 @@ void closeSegment(const TRasterCM32P &r, const TAutocloser::Segment &s,
}
if (x == x2 && y == y2) break;
-
+ lastX = x;
+ lastY = y;
int e2 = 2 * err;
if (e2 > -abs_dy) {
err -= abs_dy;