Merge branch 'working' into test
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sean Barrett · committed by Sean Barrett · parents 1d6e55a, e585dad · Browse files · 5 files·+86 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+86 added−12 removed
- tests:+5 added−6 removed
- other:+81 added−6 removed
Provenance
- Commit
a719308- Tree
38adb96- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent 1
1d6e55a- Parent 2
e585dad
5 files changed+86 added−12 removed
modified:data/map_01.pngbinary
A binary file, modified.
modified:data/map_03.pngbinary
A binary file, modified.
modified:tests/stb.dsp+5 added-1 removed
40 unmodified lines41 # PROP Intermediate_Dir "Release"42 # PROP Ignore_Export_Lib 043 # PROP Target_Dir ""44 # ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c45-# ADD CPP /nologo /G6 /MT /W3 /GX /Z7 /O2 /Ob2 /I ".." /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "TT_TEST" /FD /c45+# ADD CPP /nologo /G6 /MT /W3 /GX /Z7 /O2 /Ob2 /I ".." /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "GRID_TEST" /FD /c46 # ADD BASE RSC /l 0x409 /d "NDEBUG"47 # ADD RSC /l 0x409 /d "NDEBUG"48 BSC32=bscmake.exe49 # ADD BASE BSC32 /nologo55 unmodified lines105 SOURCE=..\stb_c_lexer.h106 # End Source File107 # Begin Source File108 109+SOURCE=..\stb_connected_components.h110+# End Source File111+# Begin Source File112+113 SOURCE=..\stb_divide.h114 # End Source File115 # Begin Source File116 84 unmodified linesmodified:tests/test_c_compilation.c-5 removed
7 unmodified lines8 #define STB_IMAGE_RESIZE_IMPLEMENTATION9 #define STB_RECT_PACK_IMPLEMENTATION10 #define STB_VOXEL_RENDER_IMPLEMENTATION11 #define STB_EASY_FONT_IMPLEMENTATION12-#define STB_CONNECTED_COMPONENTS_IMPLEMENTATION12 13 #include "stb_easy_font.h"14 #include "stb_herringbone_wang_tile.h"15 #include "stb_image.h"4 unmodified lines20 #include "stb_divide.h"21 #include "stb_image_resize.h"22 #include "stb_rect_pack.h"23 25-#define STBCC_GRID_COUNT_X_LOG2 1026-#define STBCC_GRID_COUNT_Y_LOG2 1027-#include "stb_connected_components.h"28-24 #define STBVOX_CONFIG_MODE 125 #include "stb_voxel_render.h"26 27 #define STBTE_DRAW_RECT(x0,y0,x1,y1,color) 03 unmodified linesmodified:stb_connected_components.h+81 added-6 removed
22 unmodified lines23 // license: you are granted a perpetual, irrevocable license to copy, modify,24 // publish, and distribute this file as you see fit.25 //26 //27+// CHANGELOG28+//29+// 0.92 (2016-04-16) Compute sqrt(N) cluster size by default30+// 0.91 (2016-04-15) Initial release31+//32 // TODO:28-// - test C++ compile33 // - better API documentation30-// - internals documentation (including algorithm)34+// - more comments35 // - try re-integrating naive algorithm & compare performance32-// - batching (keep data structure w/ dirty clusters)36+// - more optimized batching (current approach still recomputes local clumps many times)37 // - function for setting a grid of squares at once (just use batching)38 // - shrink data by storing only, say, 2X max exits39 // (instead of max exits per clump), and repack cluster40 // if it runs out (possibly by just rebuilding from scratch,41 // could even use dirty-cluster data structure)42 // should reduce 1Kx1K from ~66MB to ~8MB43+//44+// ALGORITHM45+//46+// The NxN grid map is split into sqrt(N) x sqrt(N) blocks called47+// "clusters". Each cluster independently computes a set of connected48+// components within that cluster (ignoring all connectivity out of49+// that cluster) using a union-find disjoint set forest. This produces a bunch50+// of locally connected components called "clumps". Each clump is (a) connected51+// within its cluster, (b) does not directly connect to any other clumps in the52+// cluster (though it may connect to them by paths that lead outside the cluster,53+// but those are ignored at this step), and (c) maintains an adjacency list of54+// all clumps in adjacent clusters that it _is_ connected to. Then a second55+// union-find disjoint set forest is used to compute connected clumps56+// globally, across the whole map. Reachability is then computed by57+// finding which clump each input point belongs to, and checking whether58+// those clumps are in the same "global" connected component.59+//60+// The above data structure can be updated efficiently; on a change61+// of a single grid square on the map, only one cluster changes its62+// purely-local state, so only one cluster needs its clumps fully63+// recomputed. Clumps in adjacent clusters need their adjacency lists64+// updated: first to remove all references to the old clumps in the65+// rebuilt cluster, then to add new references to the new clumps. Both66+// of these operations can use the existing "find which clump each input67+// point belongs to" query to compute that adjacency information rapidly.68+// In one 1024x1024 test on a specific machine, a one-tile update was69+// about 250 times faster than a full disjoint-set-forest on the full map.70 71 #ifndef INCLUDE_STB_CONNECTED_COMPONENTS_H72 #define INCLUDE_STB_CONNECTED_COMPONENTS_H73 34 unmodified lines108 //109 // bonus functions110 //111 112+// wrap multiple stbcc_update_grid calls in these function to compute113+// multiple updates more efficiently; cannot make queries inside batch114+extern void stbcc_update_batch_begin(stbcc_grid *g);115+extern void stbcc_update_batch_end(stbcc_grid *g);116+117 // query the grid data structure for whether a given square is open or not118 extern int stbcc_query_grid_open(stbcc_grid *g, int x, int y);119 120 // get a unique id for the connected component this is in; it's not necessarily121 // small, you'll need a hash table or something to remap it (or just use122 extern unsigned int stbcc_get_unique_id(stbcc_grid *g, int x, int y);123+#define STBCC_NULL_UNIQUE_ID 0xffffffff // returned for closed map squares124 125 #ifdef __cplusplus126 }127 #endif14 unmodified lines142 143 #define STBCC__MAP_STRIDE (1 << (STBCC_GRID_COUNT_X_LOG2-3))144 145 #ifndef STBCC_CLUSTER_SIZE_X_LOG2146+ #define STBCC_CLUSTER_SIZE_X_LOG2 (STBCC_GRID_COUNT_X_LOG2/2) // log2(sqrt(2^N)) = 1/2 * log2(2^N)) = 1/2 * N147+ #if STBCC_CLUSTER_SIZE_X_LOG2 > 6148+ #undef STBCC_CLUSTER_SIZE_X_LOG2109-#define STBCC_CLUSTER_SIZE_X_LOG2 5149+ #define STBCC_CLUSTER_SIZE_X_LOG2 6150+ #endif151 #endif152 153 #ifndef STBCC_CLUSTER_SIZE_Y_LOG2113-#define STBCC_CLUSTER_SIZE_Y_LOG2 5154+ #define STBCC_CLUSTER_SIZE_Y_LOG2 (STBCC_GRID_COUNT_Y_LOG2/2)155+ #if STBCC_CLUSTER_SIZE_Y_LOG2 > 6156+ #undef STBCC_CLUSTER_SIZE_Y_LOG2157+ #define STBCC_CLUSTER_SIZE_Y_LOG2 6158+ #endif159 #endif160 161 #define STBCC__CLUSTER_SIZE_X (1 << STBCC_CLUSTER_SIZE_X_LOG2)162 #define STBCC__CLUSTER_SIZE_Y (1 << STBCC_CLUSTER_SIZE_Y_LOG2)83 unmodified lines246 247 struct st_stbcc_grid248 {249 int w,h,cw,ch;250+ int in_batched_update;251+ //unsigned char cluster_dirty[STBCC__CLUSTER_COUNT_Y][STBCC__CLUSTER_COUNT_X]; // could bitpack, but: 1K x 1K => 1KB252 unsigned char map[STBCC__GRID_COUNT_Y][STBCC__MAP_STRIDE]; // 1K x 1K => 1K x 128 => 128KB253 stbcc__clumpid clump_for_node[STBCC__GRID_COUNT_Y][STBCC__GRID_COUNT_X]; // 1K x 1K x 2 = 2MB254 stbcc__cluster cluster[STBCC__CLUSTER_COUNT_Y][STBCC__CLUSTER_COUNT_X]; // 1K x 4.5KB = 9MB255 };6 unmodified lines262 int cx1 = STBCC__CLUSTER_X_FOR_COORD_X(x1);263 int cy1 = STBCC__CLUSTER_Y_FOR_COORD_Y(y1);264 int cx2 = STBCC__CLUSTER_X_FOR_COORD_X(x2);265 int cy2 = STBCC__CLUSTER_Y_FOR_COORD_Y(y2);266+ assert(!g->in_batched_update);267 if (c1 == STBCC__NULL_CLUMPID || c2 == STBCC__NULL_CLUMPID)268 return 0;269 label1 = g->cluster[cy1][cx1].clump[c1].global_label;270 label2 = g->cluster[cy2][cx2].clump[c2].global_label;11 unmodified lines282 {283 stbcc__clumpid c = g->clump_for_node[y][x];284 int cx = STBCC__CLUSTER_X_FOR_COORD_X(x);285 int cy = STBCC__CLUSTER_Y_FOR_COORD_Y(y);286+ assert(!g->in_batched_update);287+ if (c == STBCC__NULL_CLUMPID) return STBCC_NULL_UNIQUE_ID;288 return g->cluster[cy][cx].clump[c].global_label.c;289 }290 291 typedef struct230 unmodified lines522 stbcc__add_connections_to_adjacent_cluster_with_rebuild(g, cx+1, cy, -1, 0);523 stbcc__add_connections_to_adjacent_cluster_with_rebuild(g, cx, cy-1, 0, 1);524 stbcc__add_connections_to_adjacent_cluster_with_rebuild(g, cx, cy+1, 0,-1);525 526+ if (!g->in_batched_update)476- stbcc__build_connected_components_for_clumps(g);527+ stbcc__build_connected_components_for_clumps(g);528+ #if 0529+ else530+ g->cluster_dirty[cy][cx] = 1;531+ #endif532 }533 534+void stbcc_update_batch_begin(stbcc_grid *g)535+{536+ assert(!g->in_batched_update);537+ g->in_batched_update = 1;538+}539+540+void stbcc_update_batch_end(stbcc_grid *g)541+{542+ assert(g->in_batched_update);543+ g->in_batched_update = 0;544+ stbcc__build_connected_components_for_clumps(g); // @OPTIMIZE: only do this if update was non-empty545+}546+547 size_t stbcc_grid_sizeof(void)548 {549 return sizeof(stbcc_grid);550 }8 unmodified lines559 g->w = w;560 g->h = h;561 g->cw = w >> STBCC_CLUSTER_SIZE_X_LOG2;562 g->ch = h >> STBCC_CLUSTER_SIZE_Y_LOG2;563+ g->in_batched_update = 0;564 565+ #if 0566+ for (j=0; j < STBCC__CLUSTER_COUNT_Y; ++j)567+ for (i=0; i < STBCC__CLUSTER_COUNT_X; ++i) 568+ g->cluster_dirty[j][i] = 0;569+ #endif570+571 for (j=0; j < h; ++j) {572 for (i=0; i < w; i += 8) {573 unsigned char c = 0;574 for (k=0; k < 8; ++k)306 unmodified lines