stripifier.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // This file is part of meshoptimizer library; see meshoptimizer.h for version/license details
  2. #include "meshoptimizer.h"
  3. #include <assert.h>
  4. #include <limits.h>
  5. #include <string.h>
  6. // This work is based on:
  7. // Francine Evans, Steven Skiena and Amitabh Varshney. Optimizing Triangle Strips for Fast Rendering. 1996
  8. namespace meshopt
  9. {
  10. static unsigned int findStripFirst(const unsigned int buffer[][3], unsigned int buffer_size, const unsigned char* valence)
  11. {
  12. unsigned int index = 0;
  13. unsigned int iv = ~0u;
  14. for (size_t i = 0; i < buffer_size; ++i)
  15. {
  16. unsigned char va = valence[buffer[i][0]], vb = valence[buffer[i][1]], vc = valence[buffer[i][2]];
  17. unsigned int v = (va < vb && va < vc) ? va : (vb < vc ? vb : vc);
  18. if (v < iv)
  19. {
  20. index = unsigned(i);
  21. iv = v;
  22. }
  23. }
  24. return index;
  25. }
  26. static int findStripNext(const unsigned int buffer[][3], unsigned int buffer_size, unsigned int e0, unsigned int e1)
  27. {
  28. for (size_t i = 0; i < buffer_size; ++i)
  29. {
  30. unsigned int a = buffer[i][0], b = buffer[i][1], c = buffer[i][2];
  31. if (e0 == a && e1 == b)
  32. return (int(i) << 2) | 2;
  33. else if (e0 == b && e1 == c)
  34. return (int(i) << 2) | 0;
  35. else if (e0 == c && e1 == a)
  36. return (int(i) << 2) | 1;
  37. }
  38. return -1;
  39. }
  40. } // namespace meshopt
  41. size_t meshopt_stripify(unsigned int* destination, const unsigned int* indices, size_t index_count, size_t vertex_count, unsigned int restart_index)
  42. {
  43. assert(destination != indices);
  44. assert(index_count % 3 == 0);
  45. using namespace meshopt;
  46. meshopt_Allocator allocator;
  47. const size_t buffer_capacity = 8;
  48. unsigned int buffer[buffer_capacity][3] = {};
  49. unsigned int buffer_size = 0;
  50. size_t index_offset = 0;
  51. unsigned int strip[2] = {};
  52. unsigned int parity = 0;
  53. size_t strip_size = 0;
  54. // compute vertex valence; this is used to prioritize starting triangle for strips
  55. // note: we use 8-bit counters for performance; for outlier vertices the valence is incorrect but that just affects the heuristic
  56. unsigned char* valence = allocator.allocate<unsigned char>(vertex_count);
  57. memset(valence, 0, vertex_count);
  58. for (size_t i = 0; i < index_count; ++i)
  59. {
  60. unsigned int index = indices[i];
  61. assert(index < vertex_count);
  62. valence[index]++;
  63. }
  64. int next = -1;
  65. while (buffer_size > 0 || index_offset < index_count)
  66. {
  67. assert(next < 0 || (size_t(next >> 2) < buffer_size && (next & 3) < 3));
  68. // fill triangle buffer
  69. while (buffer_size < buffer_capacity && index_offset < index_count)
  70. {
  71. buffer[buffer_size][0] = indices[index_offset + 0];
  72. buffer[buffer_size][1] = indices[index_offset + 1];
  73. buffer[buffer_size][2] = indices[index_offset + 2];
  74. buffer_size++;
  75. index_offset += 3;
  76. }
  77. assert(buffer_size > 0);
  78. if (next >= 0)
  79. {
  80. unsigned int i = next >> 2;
  81. unsigned int a = buffer[i][0], b = buffer[i][1], c = buffer[i][2];
  82. unsigned int v = buffer[i][next & 3];
  83. // ordered removal from the buffer
  84. memmove(buffer[i], buffer[i + 1], (buffer_size - i - 1) * sizeof(buffer[0]));
  85. buffer_size--;
  86. // update vertex valences for strip start heuristic
  87. valence[a]--;
  88. valence[b]--;
  89. valence[c]--;
  90. // find next triangle (note that edge order flips on every iteration)
  91. // in some cases we need to perform a swap to pick a different outgoing triangle edge
  92. // for [a b c], the default strip edge is [b c], but we might want to use [a c]
  93. int cont = findStripNext(buffer, buffer_size, parity ? strip[1] : v, parity ? v : strip[1]);
  94. int swap = cont < 0 ? findStripNext(buffer, buffer_size, parity ? v : strip[0], parity ? strip[0] : v) : -1;
  95. if (cont < 0 && swap >= 0)
  96. {
  97. // [a b c] => [a b a c]
  98. destination[strip_size++] = strip[0];
  99. destination[strip_size++] = v;
  100. // next strip has same winding
  101. // ? a b => b a v
  102. strip[1] = v;
  103. next = swap;
  104. }
  105. else
  106. {
  107. // emit the next vertex in the strip
  108. destination[strip_size++] = v;
  109. // next strip has flipped winding
  110. strip[0] = strip[1];
  111. strip[1] = v;
  112. parity ^= 1;
  113. next = cont;
  114. }
  115. }
  116. else
  117. {
  118. // if we didn't find anything, we need to find the next new triangle
  119. // we use a heuristic to maximize the strip length
  120. unsigned int i = findStripFirst(buffer, buffer_size, valence);
  121. unsigned int a = buffer[i][0], b = buffer[i][1], c = buffer[i][2];
  122. // ordered removal from the buffer
  123. memmove(buffer[i], buffer[i + 1], (buffer_size - i - 1) * sizeof(buffer[0]));
  124. buffer_size--;
  125. // update vertex valences for strip start heuristic
  126. valence[a]--;
  127. valence[b]--;
  128. valence[c]--;
  129. // we need to pre-rotate the triangle so that we will find a match in the existing buffer on the next iteration
  130. int ea = findStripNext(buffer, buffer_size, c, b);
  131. int eb = findStripNext(buffer, buffer_size, a, c);
  132. int ec = findStripNext(buffer, buffer_size, b, a);
  133. // in some cases we can have several matching edges; since we can pick any edge, we pick the one with the smallest
  134. // triangle index in the buffer. this reduces the effect of stripification on ACMR and additionally - for unclear
  135. // reasons - slightly improves the stripification efficiency
  136. int mine = INT_MAX;
  137. mine = (ea >= 0 && mine > ea) ? ea : mine;
  138. mine = (eb >= 0 && mine > eb) ? eb : mine;
  139. mine = (ec >= 0 && mine > ec) ? ec : mine;
  140. if (ea == mine)
  141. {
  142. // keep abc
  143. next = ea;
  144. }
  145. else if (eb == mine)
  146. {
  147. // abc -> bca
  148. unsigned int t = a;
  149. a = b, b = c, c = t;
  150. next = eb;
  151. }
  152. else if (ec == mine)
  153. {
  154. // abc -> cab
  155. unsigned int t = c;
  156. c = b, b = a, a = t;
  157. next = ec;
  158. }
  159. if (restart_index)
  160. {
  161. if (strip_size)
  162. destination[strip_size++] = restart_index;
  163. destination[strip_size++] = a;
  164. destination[strip_size++] = b;
  165. destination[strip_size++] = c;
  166. // new strip always starts with the same edge winding
  167. strip[0] = b;
  168. strip[1] = c;
  169. parity = 1;
  170. }
  171. else
  172. {
  173. if (strip_size)
  174. {
  175. // connect last strip using degenerate triangles
  176. destination[strip_size++] = strip[1];
  177. destination[strip_size++] = a;
  178. }
  179. // note that we may need to flip the emitted triangle based on parity
  180. // we always end up with outgoing edge "cb" in the end
  181. unsigned int e0 = parity ? c : b;
  182. unsigned int e1 = parity ? b : c;
  183. destination[strip_size++] = a;
  184. destination[strip_size++] = e0;
  185. destination[strip_size++] = e1;
  186. strip[0] = e0;
  187. strip[1] = e1;
  188. parity ^= 1;
  189. }
  190. }
  191. }
  192. return strip_size;
  193. }
  194. size_t meshopt_stripifyBound(size_t index_count)
  195. {
  196. assert(index_count % 3 == 0);
  197. // worst case without restarts is 2 degenerate indices and 3 indices per triangle
  198. // worst case with restarts is 1 restart index and 3 indices per triangle
  199. return (index_count / 3) * 5;
  200. }
  201. size_t meshopt_unstripify(unsigned int* destination, const unsigned int* indices, size_t index_count, unsigned int restart_index)
  202. {
  203. assert(destination != indices);
  204. size_t offset = 0;
  205. size_t start = 0;
  206. for (size_t i = 0; i < index_count; ++i)
  207. {
  208. if (restart_index && indices[i] == restart_index)
  209. {
  210. start = i + 1;
  211. }
  212. else if (i - start >= 2)
  213. {
  214. unsigned int a = indices[i - 2], b = indices[i - 1], c = indices[i];
  215. // flip winding for odd triangles
  216. if ((i - start) & 1)
  217. {
  218. unsigned int t = a;
  219. a = b, b = t;
  220. }
  221. // although we use restart indices, strip swaps still produce degenerate triangles, so skip them
  222. if (a != b && a != c && b != c)
  223. {
  224. destination[offset + 0] = a;
  225. destination[offset + 1] = b;
  226. destination[offset + 2] = c;
  227. offset += 3;
  228. }
  229. }
  230. }
  231. return offset;
  232. }
  233. size_t meshopt_unstripifyBound(size_t index_count)
  234. {
  235. assert(index_count == 0 || index_count >= 3);
  236. return (index_count == 0) ? 0 : (index_count - 2) * 3;
  237. }