OpenToonz - An open-source full-featured 2D animation creation software
C
Fix Auto Close performance
Anderson Pradobfb4cb038b5f
diff --git a/toonz/sources/include/toonz/autoclose.h b/toonz/sources/include/toonz/autoclose.hindex 231958a86..56b0a2bf8 100644--- a/toonz/sources/include/toonz/autoclose.h+++ b/toonz/sources/include/toonz/autoclose.h@@ -33,7 +33,7 @@ struct AutocloseSettings {int m_closingDistance = 30;double m_spotAngle = 60.0;int m_opacity = 255;- bool m_ignoreAPInks = false;+ bool m_ignoreAPInks = false;AutocloseSettings() = default;@@ -55,14 +55,14 @@ public:std::set<int> autoPaintStyles = std::set<int>());~TAutocloser();- // calcola i segmenti e li disegna sul raster+ // calculates the segments and draws them on the rastervoid exec();void exec(std::string id);- // non modifica il raster. Si limita a calcolare i segmenti+ // does not modify the raster. It only calculates the segmentsvoid compute(std::vector<Segment> &segments);- // disegna sul raster i segmenti+ // draws the segments on the rastervoid draw(const std::vector<Segment> &segments);static bool hasSegmentCache(const std::string &id) {std::lock_guard<std::mutex> lock(m_mutex);@@ -79,12 +79,6 @@ public:return empty;}- static void setSegmentCache(const std::string &id,- const std::vector<Segment> &segments) {- std::lock_guard<std::mutex> lock(m_mutex);- m_cache[id] = segments;- }-static void invalidateSegmentCache(const std::string &id) {std::lock_guard<std::mutex> lock(m_mutex);m_cache.erase(id);diff --git a/toonz/sources/tnztools/rastertapetool.cpp b/toonz/sources/tnztools/rastertapetool.cppindex 4ab6b6e04..1ef644455 100644--- a/toonz/sources/tnztools/rastertapetool.cpp+++ b/toonz/sources/tnztools/rastertapetool.cpp@@ -83,7 +83,7 @@ public:ac.draw(m_segments);ToolUtils::updateSaveBox();- /*-- Viewerを更新させるため --*/+ /*-- Notify to update Viewer --*/TTool::getApplication()->getCurrentXsheet()->notifyXsheetChanged();notifyImageChanged();}@@ -125,7 +125,7 @@ class RasterTapeTool final : public TTool {bool m_isXsheetCell;std::pair<int, int> m_currCell;- // Aggiunte per disegnare il lazzo a la polyline+ // Added to draw freehand and polylineStrokeGenerator m_track;TPointD m_firstPos;TPointD m_mousePosition;@@ -212,103 +212,88 @@ public:}//------------------------------------------------------------- /*-- AutoClose Returns true if executed, false otherwise --*/+ /*-- AutoClose Returns true if executed, false otherwise --*/bool applyAutoclose(const TToonzImageP &ti, const TFrameId fid,const TRectD &selRect = TRectD(), TStroke *stroke = 0) {- if (!ti) return false;- // inizializzo gli AutocloseParameters+ if (!ti || !ti->getRaster()) return false;++ TTool::Application *app = TTool::getApplication();+ TXshLevel *level = app->getCurrentLevel()->getLevel();+ if (!level) return false;++ TXshSimpleLevel *sl = level->getSimpleLevel();+ if (!sl) return false;++ std::string cacheId = sl->getImageId(fid);+ // initialize AutocloseParametersAutocloseParameters params;- params.m_closingDistance = (int)(m_distance.getValue());- params.m_spotAngle = (int)(m_angle.getValue());+ params.m_closingDistance = m_distance.getValue();+ params.m_spotAngle = m_angle.getValue();params.m_opacity = m_opacity.getValue();- std::string inkString = ::to_string(m_inkIndex.getValue());- int inkIndex =- TTool::getApplication()- ->getCurrentLevelStyleIndex(); // TApp::instance()->getCurrentPalette()->getStyleIndex();++ std::string inkString = ::to_string(m_inkIndex.getValue());+ int inkIndex = TTool::getApplication()->getCurrentLevelStyleIndex();if (isInt(inkString)) inkIndex = std::stoi(inkString);params.m_inkIndex = inkIndex;- TPoint delta(0, 0);- TRasterCM32P ras, raux = ti->getRaster();- TRectD selArea;- TRect myRect;-- TXshSimpleLevel *sl =- TTool::getApplication()->getCurrentLevel()->getSimpleLevel();+ std::wstring closeType = m_closeType.getValue();+ TRasterCM32P raux = ti->getRaster();std::vector<TAutocloser::Segment> segments;- bool useCache = TAutocloser::hasSegmentCache(sl->getImageId(fid));- if (useCache) segments = TAutocloser::getSegmentCache(sl->getImageId(fid));- std::wstring closeType = m_closeType.getValue();-- if (useCache) {- ras = raux;+ // Use The Cache+ if (TAutocloser::hasSegmentCache(cacheId)) {+ segments = TAutocloser::getSegmentCache(cacheId);} else {- bool hasRect = (closeType == RECT_CLOSE && raux && !selRect.isEmpty());- bool hasStroke =- ((closeType == FREEHAND_CLOSE || closeType == POLYLINE_CLOSE) &&- stroke);-- if (hasRect || hasStroke) {- if (hasRect) {- selArea = TRectD(std::min(selRect.x0, selRect.x1),- std::min(selRect.y0, selRect.y1),- std::max(selRect.x0, selRect.x1),- std::max(selRect.y0, selRect.y1));- } else {- selArea = stroke->getBBox();- }-- myRect = ToonzImageUtils::convertWorldToRaster(selArea, ti);- TRect enlargedRect = myRect.enlarge(params.m_closingDistance);- ras = raux->extract(enlargedRect);- delta = enlargedRect.getP00();- } else {- ras = raux;- }+ // Calculate only once and store in cache+ TAutocloser ac(raux, params.m_closingDistance, params.m_spotAngle,+ params.m_inkIndex, params.m_opacity);+ ac.exec(cacheId); // <-- This computes + draws + saves in cache!+ segments = TAutocloser::getSegmentCache(cacheId);}- if (!ras) return false;-- TAutocloser ac(ras, params.m_closingDistance, params.m_spotAngle,- params.m_inkIndex, params.m_opacity);+ if (segments.empty()) return false;- if (!useCache) ac.compute(segments);+ // Fiter by region (only if necessary)+ TPoint delta(0, 0);+ std::vector<TAutocloser::Segment> filteredSegments = segments;- if (closeType == RECT_CLOSE && useCache) {- TRectD selAreaOrdered(+ if (closeType == RECT_CLOSE && !selRect.isEmpty()) {+ TRectD orderedRect(std::min(selRect.x0, selRect.x1), std::min(selRect.y0, selRect.y1),std::max(selRect.x0, selRect.x1), std::max(selRect.y0, selRect.y1));- TRect rect = ToonzImageUtils::convertWorldToRaster(selAreaOrdered, ti);- checkSegments(segments, convert(rect), delta);+ TRect rasterRect = ToonzImageUtils::convertWorldToRaster(orderedRect, ti);+ checkSegments(filteredSegments, convert(rasterRect), delta);} else if ((closeType == FREEHAND_CLOSE || closeType == POLYLINE_CLOSE) &&stroke) {- checkSegments(segments, stroke, raux, delta);- }; // Normal-- std::vector<TAutocloser::Segment> segments2(segments);-- /*-- segmentが取得できなければfalseを返す --*/- if (segments2.empty()) return false;-- int i;- if (delta != TPoint(0, 0))- for (i = 0; i < (int)segments2.size(); i++) {- segments2[i].first += delta;- segments2[i].second += delta;+ checkSegments(filteredSegments, stroke, raux, delta);+ } // Normal mode: use all segments+ /*-- Return false if no segment is obtained --*/+ if (filteredSegments.empty()) return false;++ // Adjust coordinates if necessary+ if (delta != TPoint(0, 0)) {+ for (auto &seg : filteredSegments) {+ seg.first += delta;+ seg.second += delta;}+ }+ // UNDOTTileSetCM32 *tileSet = new TTileSetCM32(raux->getSize());- for (i = 0; i < (int)segments2.size(); i++) {- TRect bbox(segments2[i].first, segments2[i].second);- bbox = bbox.enlarge(2);- tileSet->add(raux, bbox);+ for (const auto &seg : filteredSegments) {+ TRect bbox(seg.first, seg.second);+ tileSet->add(raux, bbox.enlarge(2));}TUndoManager::manager()->add(- new RasterAutocloseUndo(tileSet, params, segments2, sl, fid));- ac.draw(segments);+ new RasterAutocloseUndo(tileSet, params, filteredSegments, sl, fid));++ // DRAW+ TAutocloser ac(raux, params.m_closingDistance, params.m_spotAngle,+ params.m_inkIndex, params.m_opacity);+ ac.draw(filteredSegments);+ToolUtils::updateSaveBox();return true;}@@ -418,7 +403,7 @@ public:TToonzImageP ti = TToonzImageP(getImage(true));if (!ti) return;- /*-- Rectの座標の向きを揃える --*/+ /*-- Normalize Rect coordinates --*/if (m_selectingRect.x0 > m_selectingRect.x1)std::swap(m_selectingRect.x1, m_selectingRect.x0);if (m_selectingRect.y0 > m_selectingRect.y1)@@ -427,7 +412,6 @@ public:TTool::Application *app = TTool::getApplication();m_selecting = false;- TRasterCM32P ras;if (m_closeType.getValue() == RECT_CLOSE) {if (m_multi.getValue()) {if (m_firstFrameSelected) {@@ -453,7 +437,7 @@ public:return;}- /*-- AutoCloseが実行されたか判定する --*/+ /*-- Check if AutoClose was executed --*/if (!applyAutoclose(ti, getCurrentFid(), m_selectingRect)) {if (m_stroke) {delete m_stroke;@@ -612,10 +596,9 @@ public:!m_firstStroke))resetMulti();else if (m_firstFrameId == getFrameId())- m_firstFrameSelected = false; // nel caso sono passato allo stato 1 e- // torno all'immagine iniziale, torno allo- // stato iniziale- else { // cambio stato.+ m_firstFrameSelected = false; // when returning to initial image,+ // return to initial state+ else { // change state.m_firstFrameSelected = true;if (m_closeType.getValue() == RECT_CLOSE) {assert(!m_selectingRect.isEmpty());@@ -766,8 +749,8 @@ public://----------------------------------------------------------------------- //! Viene aggiunto \b pos a \b m_track e disegnato il primo pezzetto del- //! lazzo. Viene inizializzato \b m_firstPos+ //! Adds \b pos to \b m_track and draws the first piece of the freehand.+ //! Initializes \b m_firstPosvoid startFreehand(const TPointD &pos) {m_track.clear();m_firstPos = pos;@@ -777,8 +760,7 @@ public://------------------------------------------------------------------- //! Viene aggiunto \b pos a \b m_track e disegnato un altro pezzetto del- //! lazzo.+ //! Adds \b pos to \b m_track and draws another piece of the freehand.void freehandDrag(const TPointD &pos) {double pixelSize2 = getPixelSize() * getPixelSize();m_track.add(TThickPoint(pos, m_thick), pixelSize2);@@ -786,8 +768,8 @@ public://------------------------------------------------------------------- //! Viene chiuso il lazzo (si aggiunge l'ultimo punto ad m_track) e viene- //! creato lo stroke rappresentante il lazzo.+ //! Closes the freehand (adds the last point to m_track) and creates the+ //! stroke representing the freehand.void closeFreehand(const TPointD &pos) {if (m_track.isEmpty()) return;double pixelSize2 = getPixelSize() * getPixelSize();@@ -800,7 +782,7 @@ public://------------------------------------------------------------------- //! Viene aggiunto un punto al vettore m_polyline.+ //! Adds a point to the vector m_polyline.void addPointPolyline(const TPointD &pos) {m_firstPos = pos;m_polyline.push_back(pos);@@ -808,8 +790,8 @@ public://------------------------------------------------------------------- //! Agginge l'ultimo pos a \b m_polyline e chiude la spezzata (aggiunge \b- //! m_polyline.front() alla fine del vettore)+ //! Adds the last pos to \b m_polyline and closes the polyline (adds \b+ //! m_polyline.front() to the end of the vector)void closePolyline(const TPointD &pos) {if (m_polyline.size() <= 1) return;if (m_polyline.back() != pos) m_polyline.push_back(pos);@@ -820,7 +802,7 @@ public://-------------------------------------------------------------------- //! Elimina i segmenti che non sono contenuti all'interno dello stroke!!!+ //! Deletes segments that are not contained within the stroke!!!void checkSegments(std::vector<TAutocloser::Segment> &segments,TStroke *stroke, const TRasterCM32P &ras,const TPoint &delta) {diff --git a/toonz/sources/toonzlib/autoclose.cpp b/toonz/sources/toonzlib/autoclose.cppindex 6e30751cf..6bb0452df 100644--- a/toonz/sources/toonzlib/autoclose.cpp+++ b/toonz/sources/toonzlib/autoclose.cpp@@ -10,6 +10,7 @@#include <set>#include <queue>#include <unordered_set>+#include <algorithm>// #define AUT_SPOT_SAMPLES 40using namespace SkeletonLut;@@ -229,12 +230,12 @@ unsigned char buildPaintnInkNeighborPattern(int x, int y, TRasterCM32P r,.arg(isLine ? 1 : 0).arg(i);*/if (isLine) {- pattern |= (1 << 7 - i);+ pattern |= (1 << (7 - i));}// qDebug() << "Pattern:"// << QString::number(pattern, 2).rightJustified(8, '0');} else- pattern |= (1 << 7 - i);+ pattern |= (1 << (7 - i));}// qDebug() << "Final pattern: decimal =" << (int)pattern << "| binary = 0b"// << QString::number(pattern, 2).rightJustified(8, '0');@@ -375,15 +376,15 @@ void drawSegment(TRasterCM32P &r, const TAutocloser::Segment &s,int wrap = r->getWrap();TPixelCM32 *buf = r->pixels();/*-int i, j;-for (i=0; i<r->getLy();i++)-{- for (j=0; j<r->getLx();j++, buf++)-*buf = (1<<4)|0xf;-buf += wrap-r->getLx();- }-return;-*/+ int i, j;+ for (i=0; i<r->getLy();i++)+ {+ for (j=0; j<r->getLx();j++, buf++)+ *buf = (1<<4)|0xf;+ buf += wrap-r->getLx();+ }+ return;+ */int x, y, dx, dy, d, incr_1, incr_2;@@ -429,7 +430,7 @@ return;void TAutocloser::Imp::compute(std::vector<Segment> &closingSegmentArray) {std::vector<TPoint> endpoints;try {- assert(closingSegmentArray.empty());+ // assert(closingSegmentArray.empty());TRasterCM32P raux;@@ -495,7 +496,7 @@ void TAutocloser::Imp::draw(const std::vector<Segment> &closingSegmentArray) {/*------------------------------------------------------------------------*/void TAutocloser::Imp::copy(const TRasterGR8P &br, TRaster32P &r) {- assert(r->getLx() == br->getLx() && r->getLy() == br->getLy());+ // assert(r->getLx() == br->getLx() && r->getLy() == br->getLy());int i, j;int lx = r->getLx();@@ -541,7 +542,7 @@ inline int distance2(const TPoint p0, const TPoint p1) {int closerPoint(const std::vector<TAutocloser::Segment> &points,std::vector<bool> &marks, int index) {- assert(points.size() == marks.size());+ // assert(points.size() == marks.size());int min, curr;int minval = (std::numeric_limits<int>::max)();@@ -818,51 +819,173 @@ static bool allMarked(const std::vector<bool> &marks, int index) {bool TAutocloser::Imp::spotResearchTwoPoints(std::vector<Segment> &endpoints, std::vector<Segment> &closingSegments) {- int i, distance, current = 0, closerIndex;- int sqrDistance = m_closingDistance * m_closingDistance;- bool found = 0;- std::vector<bool> marks(endpoints.size());-- while (current < (int)endpoints.size() - 1) {- found = 0;- for (i = current + 1; i < (int)marks.size(); i++) marks[i] = false;- distance = 0;-- while (!found && (distance <= sqrDistance) && !allMarked(marks, current)) {- closerIndex = closerPoint(endpoints, marks, current);- if (exploreTwoSpots(endpoints[current], endpoints[closerIndex]) &&- notInsidePath(endpoints[current].first,- endpoints[closerIndex].first) &&- !hasInkBetween(endpoints[current].first,- endpoints[closerIndex].first)) {- drawInByteRaster(endpoints[current].first,- endpoints[closerIndex].first);- closingSegments.push_back(- Segment(endpoints[current].first, endpoints[closerIndex].first));-- if (!EndpointTable[neighboursCode(- getPtr(endpoints[closerIndex].first))]) {- std::vector<Segment>::iterator it = endpoints.begin();- std::advance(it, closerIndex);- endpoints.erase(it);- std::vector<bool>::iterator it1 = marks.begin();- std::advance(it1, closerIndex);- marks.erase(it1);+ if (endpoints.size() < 2) return false;++ bool found = false;++ const int sqrDistance = m_closingDistance * m_closingDistance;+ const int lx = m_raster->getLx();+ const int ly = m_raster->getLy();++ // Lambda for fallback (used in two places)+ auto fallback = [&]() -> bool {+ std::vector<bool> keep(endpoints.size(), true);++ for (int i = 0; i < (int)endpoints.size(); ++i) {+ if (!keep[i]) continue;+ const TPoint &p = endpoints[i].first;++ for (int j = i + 1; j < (int)endpoints.size(); ++j) {+ if (!keep[j]) continue;+ const TPoint &q = endpoints[j].first;++ int dist = distance2(p, q);+ if (dist > sqrDistance) continue;++ if (exploreTwoSpots(endpoints[i], endpoints[j]) &&+ notInsidePath(p, q) && !hasInkBetween(p, q)) {+ drawInByteRaster(p, q);+ closingSegments.push_back(Segment(p, q));+ keep[i] = false;+ if (!EndpointTable[neighboursCode(getPtr(q))]) {+ keep[j] = false;+ }+ found = true; // <-- uses found from main function}- found = true;}}if (found) {- std::vector<Segment>::iterator it = endpoints.begin();- std::advance(it, current);- endpoints.erase(it);- std::vector<bool>::iterator it1 = marks.begin();- std::advance(it1, current);- marks.erase(it1);- } else- current++;+ endpoints.erase(std::remove_if(endpoints.begin(), endpoints.end(),+ [&keep, idx = 0](const Segment &) mutable {+ return !keep[idx++];+ }),+ endpoints.end());+ }+ return found;+ };++ // Quick fallback for few endpoints+ if (endpoints.size() < 40) {+ return fallback();}++ // ========== DYNAMIC CELL_SIZE CALCULATION ==========+ int CELL_SIZE;++ // Calculate endpoint density (endpoints per pixel)+ double area = lx * ly;+ double density = (area > 0) ? (endpoints.size() / area) : 0;++ // Heuristic: ideal CELL_SIZE based on density and distance+ if (density > 0) {+ // Formula: cell_size = sqrt(TARGET_ENDPOINTS_PER_CELL / density)+ // where TARGET_ENDPOINTS_PER_CELL is the desired number of endpoints per+ // cell+ const double TARGET_ENDPOINTS_PER_CELL = 8.0; // Adjustable value++ double ideal_cell_size = std::sqrt(TARGET_ENDPOINTS_PER_CELL / density);++ // Limit by practical constraints+ CELL_SIZE = static_cast<int>(ideal_cell_size);++ // Minimum and maximum limits+ const int MIN_CELL_SIZE = std::max(5, m_closingDistance / 4);+ const int MAX_CELL_SIZE =+ std::min(50, // Absolute limit+ std::max(m_closingDistance * 2, std::max(lx, ly) / 10));++ CELL_SIZE = std::clamp(CELL_SIZE, MIN_CELL_SIZE, MAX_CELL_SIZE);++ // Adjust based on number of endpoints+ // For many endpoints, smaller cells may be better+ if (endpoints.size() > 500) {+ CELL_SIZE = std::max(MIN_CELL_SIZE, CELL_SIZE * 2 / 3);+ }+ } else {+ // Zero density (shouldn't happen, but as fallback)+ CELL_SIZE =+ std::max(10, std::min(m_closingDistance, std::max(lx, ly) / 10));+ }++ // Ensure grid is not too large or too small+ const int GRID_W = (lx + CELL_SIZE - 1) / CELL_SIZE;+ const int GRID_H = (ly + CELL_SIZE - 1) / CELL_SIZE;++ // If grid too small (less than 9 cells), use fallback+ if (GRID_W * GRID_H < 9) {+ return fallback();+ }++ // ========== OPTIMIZED SPATIAL GRID ==========+ std::vector<std::vector<int>> grid(GRID_W * GRID_H);++ // Fill grid+ for (int i = 0; i < (int)endpoints.size(); ++i) {+ const TPoint &p = endpoints[i].first;+ // Use clamp for safety+ int gx = std::clamp(p.x / CELL_SIZE, 0, GRID_W - 1);+ int gy = std::clamp(p.y / CELL_SIZE, 0, GRID_H - 1);+ grid[gy * GRID_W + gx].push_back(i);+ }++ std::vector<bool> keep(endpoints.size(), true);++ const int cellRadius = (m_closingDistance + CELL_SIZE - 1) / CELL_SIZE;++ for (int i = 0; i < (int)endpoints.size(); ++i) {+ if (!keep[i]) continue;+ const TPoint &p = endpoints[i].first;+ // Adjustment: Also in lookup+ int gx = std::clamp(p.x / CELL_SIZE, 0, GRID_W - 1);+ int gy = std::clamp(p.y / CELL_SIZE, 0, GRID_H - 1);++ bool pairFound = false;+ for (int dy = -cellRadius; dy <= cellRadius && !pairFound; ++dy) {+ for (int dx = -cellRadius; dx <= cellRadius && !pairFound; ++dx) {+ int ngx = gx + dx;+ int ngy = gy + dy;+ if (ngx < 0 || ngx >= GRID_W || ngy < 0 || ngy >= GRID_H) continue;++ const auto &cell = grid[ngy * GRID_W + ngx];+ for (int j_idx : cell) {+ if (j_idx <= i || !keep[j_idx]) continue;+ const TPoint &q = endpoints[j_idx].first;++ // Optimization: quick bounding box+ int dx_abs = std::abs(p.x - q.x);+ int dy_abs = std::abs(p.y - q.y);+ if (dx_abs > m_closingDistance || dy_abs > m_closingDistance)+ continue;++ int dist = distance2(p, q);+ if (dist > sqrDistance) continue;++ if (exploreTwoSpots(endpoints[i], endpoints[j_idx]) &&+ notInsidePath(p, q) && !hasInkBetween(p, q)) {+ drawInByteRaster(p, q);+ closingSegments.push_back(Segment(p, q));+ keep[i] = false;+ if (!EndpointTable[neighboursCode(getPtr(q))]) {+ keep[j_idx] = false;+ }+ found = true;+ pairFound = true;+ break; // Exit inner loop+ }+ }+ }+ }+ }++ if (found) {+ endpoints.erase(std::remove_if(endpoints.begin(), endpoints.end(),+ [&keep, idx = 0](const Segment &) mutable {+ return !keep[idx++];+ }),+ endpoints.end());+ }+return found;}@@ -915,7 +1038,7 @@ continue;p1 = p0 - displAverage;/*if ((point->x2<0 && point->y2<0) || (point->x2>Lx && point->y2>Ly))- * printf("che palle!!!!!!\n");*/+ * printf("what a pain!!!!!!\n");*/if (p1.x < 0) {p1.y = tround(p0.y - (float)((p0.y - p1.y) * p0.x) / (p0.x - p1.x));@@ -942,34 +1065,51 @@ continue;bool TAutocloser::Imp::spotResearchOnePoint(std::vector<Segment> &endpoints, std::vector<Segment> &closingSegments) {- int count = 0;- bool ret = false;+ if (endpoints.empty()) return false;++ bool found = false;+ std::vector<bool> keep(endpoints.size(), true);++ for (int i = 0; i < (int)endpoints.size(); ++i) {+ if (!keep[i]) continue;- while (count < (int)endpoints.size()) {TPoint p;+ if (exploreSpot(endpoints[i], p)) {+ Segment segment(endpoints[i].first, p);- if (exploreSpot(endpoints[count], p)) {- Segment segment(endpoints[count].first, p);- std::vector<Segment>::iterator it =+ // Avoid duplicates+ auto it =std::find(closingSegments.begin(), closingSegments.end(), segment);- if (it == closingSegments.end() &&- notInsidePath(endpoints[count].first, p)) {- ret = true;- drawInByteRaster(endpoints[count].first, p);- closingSegments.push_back(Segment(endpoints[count].first, p));- cancelFromArray(endpoints, p, count);- if (!EndpointTable[neighboursCode(getPtr(endpoints[count].first))]) {- std::vector<Segment>::iterator it = endpoints.begin();- std::advance(it, count);- endpoints.erase(it);- continue;+ if (it == closingSegments.end() && notInsidePath(endpoints[i].first, p)) {+ drawInByteRaster(endpoints[i].first, p);+ closingSegments.push_back(segment);+ found = true;++ // Mark current endpoint for possible removal+ if (!EndpointTable[neighboursCode(getPtr(endpoints[i].first))]) {+ keep[i] = false;+ }++ // Mark endpoints that end at 'p' (if they exist)+ for (int j = 0; j < (int)endpoints.size(); ++j) {+ if (keep[j] && endpoints[j].first == p) {+ keep[j] = false;+ }}}}- count++;}- return ret;+ // Final removal compacting the vector+ if (found) {+ endpoints.erase(std::remove_if(endpoints.begin(), endpoints.end(),+ [&keep, index = 0](const Segment &) mutable {+ return !keep[index++];+ }),+ endpoints.end());+ }++ return found;}/*------------------------------------------------------------------------*/@@ -1191,19 +1331,6 @@ void TAutocloser::Imp::cancelMarks(UCHAR *br) {/*=============================================================================*/-/*=============================================================================*/-/*=============================================================================*/-/*=============================================================================*/-/*=============================================================================*/-/*=============================================================================*/-/*=============================================================================*/-/*=============================================================================*/-/*=============================================================================*/-/*=============================================================================*/-/*=============================================================================*/--/*=============================================================================*/-void TAutocloser::Imp::skeletonize(std::vector<TPoint> &endpoints) {std::vector<Seed> seeds;@@ -1226,7 +1353,7 @@ void TAutocloser::Imp::findSeeds(std::vector<Seed> &seeds,if (notMarkedBorderInk(br)) {preseed = FirstPreseedTable[neighboursCode(br)];- if (preseed != 8) /*non e' un pixel isolato*/+ if (preseed != 8) /*not an isolated pixel*/{seeds.push_back(Seed(br, preseed));circuitAndMark(br, preseed);@@ -1255,7 +1382,7 @@ void TAutocloser::Imp::circuitAndMark(UCHAR *seed, UCHAR preseed) {prewalker = displ ^ 0x7;while ((walker != seed) || (preseed != prewalker)) {- *walker |= 0x4; /* metto la marca di passaggio */+ *walker |= 0x4; /* set the pass mark */displ = NextPointTable[(neighboursCode(walker) << 3) | prewalker];// assert(displ>=0 && displ<8);@@ -1294,7 +1421,7 @@ void TAutocloser::Imp::erase(std::vector<Seed> &seeds,// assert(displ>=0 && displ<8);seeds.push_back(Seed(seed + m_displaceVector[displ], displ ^ 0x7));- } else /* il seed e' stato cancellato */+ } else /* the seed has been erased */{code = NextSeedTable[neighboursCode(seed)];seeds.push_back(@@ -1372,7 +1499,7 @@ void TAutocloser::Imp::cancelFromArray(std::vector<Segment> &array, TPoint p,for (; it != array.end(); ++it, i++)if (it->first == p) {if (!EndpointTable[neighboursCode(getPtr(p))]) {- assert(i != count);+ // assert(i != count);if (i < count) count--;array.erase(it);}@@ -1411,7 +1538,7 @@ TAutocloser::TAutocloser(const TRasterP &r, int ink, const AutocloseSettings st,std::set<int> autoPaints): m_imp(new Imp(r, st.m_closingDistance, st.m_spotAngle, ink, st.m_opacity)), m_autoPaintStyles(autoPaints) {}-//...............................+/*------------------------------------------------------------------------*/void TAutocloser::exec() {std::vector<TAutocloser::Segment> segments;@@ -1423,14 +1550,30 @@ void TAutocloser::exec(std::string id) {std::vector<TAutocloser::Segment> segments;compute(segments);draw(segments);- setSegmentCache(id, std::move(segments));++ // === SAVE TO CACHE WITH SIZE LIMIT ===+ {+ std::lock_guard<std::mutex> lock(m_mutex);++ constexpr size_t MAX_CACHE_SIZE = 50; // maximum number of images in cache++ m_cache[id] = std::move(segments); // move avoids copy++ // Remove oldest entries if exceeding limit+ if (m_cache.size() > MAX_CACHE_SIZE) {+ // Remove the first (oldest) ones until MAX_CACHE_SIZE remains+ auto it = m_cache.begin();+ std::advance(it, m_cache.size() - MAX_CACHE_SIZE);+ m_cache.erase(m_cache.begin(), it);+ }+ }}-//...............................+/*------------------------------------------------------------------------*/TAutocloser::~TAutocloser() {}-//-------------------------------------------------+/*------------------------------------------------------------------------*/void TAutocloser::compute(std::vector<Segment> &closingSegmentArray) {m_imp->compute(closingSegmentArray);@@ -1453,8 +1596,8 @@ void TAutocloser::compute(std::vector<Segment> &closingSegmentArray) {}}}-//-------------------------------------------------+/*------------------------------------------------------------------------*/void TAutocloser::draw(const std::vector<Segment> &closingSegmentArray) {m_imp->draw(closingSegmentArray);-}\ No newline at end of file+}