stripifier.cpp 7.4 KB

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