indexgenerator.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 <string.h>
  5. // This work is based on:
  6. // John McDonald, Mark Kilgard. Crack-Free Point-Normal Triangles using Adjacent Edge Normals. 2010
  7. namespace meshopt
  8. {
  9. static unsigned int hashUpdate4(unsigned int h, const unsigned char* key, size_t len)
  10. {
  11. // MurmurHash2
  12. const unsigned int m = 0x5bd1e995;
  13. const int r = 24;
  14. while (len >= 4)
  15. {
  16. unsigned int k = *reinterpret_cast<const unsigned int*>(key);
  17. k *= m;
  18. k ^= k >> r;
  19. k *= m;
  20. h *= m;
  21. h ^= k;
  22. key += 4;
  23. len -= 4;
  24. }
  25. return h;
  26. }
  27. struct VertexHasher
  28. {
  29. const unsigned char* vertices;
  30. size_t vertex_size;
  31. size_t vertex_stride;
  32. size_t hash(unsigned int index) const
  33. {
  34. return hashUpdate4(0, vertices + index * vertex_stride, vertex_size);
  35. }
  36. bool equal(unsigned int lhs, unsigned int rhs) const
  37. {
  38. return memcmp(vertices + lhs * vertex_stride, vertices + rhs * vertex_stride, vertex_size) == 0;
  39. }
  40. };
  41. struct VertexStreamHasher
  42. {
  43. const meshopt_Stream* streams;
  44. size_t stream_count;
  45. size_t hash(unsigned int index) const
  46. {
  47. unsigned int h = 0;
  48. for (size_t i = 0; i < stream_count; ++i)
  49. {
  50. const meshopt_Stream& s = streams[i];
  51. const unsigned char* data = static_cast<const unsigned char*>(s.data);
  52. h = hashUpdate4(h, data + index * s.stride, s.size);
  53. }
  54. return h;
  55. }
  56. bool equal(unsigned int lhs, unsigned int rhs) const
  57. {
  58. for (size_t i = 0; i < stream_count; ++i)
  59. {
  60. const meshopt_Stream& s = streams[i];
  61. const unsigned char* data = static_cast<const unsigned char*>(s.data);
  62. if (memcmp(data + lhs * s.stride, data + rhs * s.stride, s.size) != 0)
  63. return false;
  64. }
  65. return true;
  66. }
  67. };
  68. struct EdgeHasher
  69. {
  70. const unsigned int* remap;
  71. size_t hash(unsigned long long edge) const
  72. {
  73. unsigned int e0 = unsigned(edge >> 32);
  74. unsigned int e1 = unsigned(edge);
  75. unsigned int h1 = remap[e0];
  76. unsigned int h2 = remap[e1];
  77. const unsigned int m = 0x5bd1e995;
  78. // MurmurHash64B finalizer
  79. h1 ^= h2 >> 18;
  80. h1 *= m;
  81. h2 ^= h1 >> 22;
  82. h2 *= m;
  83. h1 ^= h2 >> 17;
  84. h1 *= m;
  85. h2 ^= h1 >> 19;
  86. h2 *= m;
  87. return h2;
  88. }
  89. bool equal(unsigned long long lhs, unsigned long long rhs) const
  90. {
  91. unsigned int l0 = unsigned(lhs >> 32);
  92. unsigned int l1 = unsigned(lhs);
  93. unsigned int r0 = unsigned(rhs >> 32);
  94. unsigned int r1 = unsigned(rhs);
  95. return remap[l0] == remap[r0] && remap[l1] == remap[r1];
  96. }
  97. };
  98. static size_t hashBuckets(size_t count)
  99. {
  100. size_t buckets = 1;
  101. while (buckets < count + count / 4)
  102. buckets *= 2;
  103. return buckets;
  104. }
  105. template <typename T, typename Hash>
  106. static T* hashLookup(T* table, size_t buckets, const Hash& hash, const T& key, const T& empty)
  107. {
  108. assert(buckets > 0);
  109. assert((buckets & (buckets - 1)) == 0);
  110. size_t hashmod = buckets - 1;
  111. size_t bucket = hash.hash(key) & hashmod;
  112. for (size_t probe = 0; probe <= hashmod; ++probe)
  113. {
  114. T& item = table[bucket];
  115. if (item == empty)
  116. return &item;
  117. if (hash.equal(item, key))
  118. return &item;
  119. // hash collision, quadratic probing
  120. bucket = (bucket + probe + 1) & hashmod;
  121. }
  122. assert(false && "Hash table is full"); // unreachable
  123. return 0;
  124. }
  125. static void buildPositionRemap(unsigned int* remap, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, meshopt_Allocator& allocator)
  126. {
  127. VertexHasher vertex_hasher = {reinterpret_cast<const unsigned char*>(vertex_positions), 3 * sizeof(float), vertex_positions_stride};
  128. size_t vertex_table_size = hashBuckets(vertex_count);
  129. unsigned int* vertex_table = allocator.allocate<unsigned int>(vertex_table_size);
  130. memset(vertex_table, -1, vertex_table_size * sizeof(unsigned int));
  131. for (size_t i = 0; i < vertex_count; ++i)
  132. {
  133. unsigned int index = unsigned(i);
  134. unsigned int* entry = hashLookup(vertex_table, vertex_table_size, vertex_hasher, index, ~0u);
  135. if (*entry == ~0u)
  136. *entry = index;
  137. remap[index] = *entry;
  138. }
  139. }
  140. } // namespace meshopt
  141. size_t meshopt_generateVertexRemap(unsigned int* destination, const unsigned int* indices, size_t index_count, const void* vertices, size_t vertex_count, size_t vertex_size)
  142. {
  143. using namespace meshopt;
  144. assert(indices || index_count == vertex_count);
  145. assert(!indices || index_count % 3 == 0);
  146. assert(vertex_size > 0 && vertex_size <= 256);
  147. meshopt_Allocator allocator;
  148. memset(destination, -1, vertex_count * sizeof(unsigned int));
  149. VertexHasher hasher = {static_cast<const unsigned char*>(vertices), vertex_size, vertex_size};
  150. size_t table_size = hashBuckets(vertex_count);
  151. unsigned int* table = allocator.allocate<unsigned int>(table_size);
  152. memset(table, -1, table_size * sizeof(unsigned int));
  153. unsigned int next_vertex = 0;
  154. for (size_t i = 0; i < index_count; ++i)
  155. {
  156. unsigned int index = indices ? indices[i] : unsigned(i);
  157. assert(index < vertex_count);
  158. if (destination[index] == ~0u)
  159. {
  160. unsigned int* entry = hashLookup(table, table_size, hasher, index, ~0u);
  161. if (*entry == ~0u)
  162. {
  163. *entry = index;
  164. destination[index] = next_vertex++;
  165. }
  166. else
  167. {
  168. assert(destination[*entry] != ~0u);
  169. destination[index] = destination[*entry];
  170. }
  171. }
  172. }
  173. assert(next_vertex <= vertex_count);
  174. return next_vertex;
  175. }
  176. size_t meshopt_generateVertexRemapMulti(unsigned int* destination, const unsigned int* indices, size_t index_count, size_t vertex_count, const struct meshopt_Stream* streams, size_t stream_count)
  177. {
  178. using namespace meshopt;
  179. assert(indices || index_count == vertex_count);
  180. assert(index_count % 3 == 0);
  181. assert(stream_count > 0 && stream_count <= 16);
  182. for (size_t i = 0; i < stream_count; ++i)
  183. {
  184. assert(streams[i].size > 0 && streams[i].size <= 256);
  185. assert(streams[i].size <= streams[i].stride);
  186. }
  187. meshopt_Allocator allocator;
  188. memset(destination, -1, vertex_count * sizeof(unsigned int));
  189. VertexStreamHasher hasher = {streams, stream_count};
  190. size_t table_size = hashBuckets(vertex_count);
  191. unsigned int* table = allocator.allocate<unsigned int>(table_size);
  192. memset(table, -1, table_size * sizeof(unsigned int));
  193. unsigned int next_vertex = 0;
  194. for (size_t i = 0; i < index_count; ++i)
  195. {
  196. unsigned int index = indices ? indices[i] : unsigned(i);
  197. assert(index < vertex_count);
  198. if (destination[index] == ~0u)
  199. {
  200. unsigned int* entry = hashLookup(table, table_size, hasher, index, ~0u);
  201. if (*entry == ~0u)
  202. {
  203. *entry = index;
  204. destination[index] = next_vertex++;
  205. }
  206. else
  207. {
  208. assert(destination[*entry] != ~0u);
  209. destination[index] = destination[*entry];
  210. }
  211. }
  212. }
  213. assert(next_vertex <= vertex_count);
  214. return next_vertex;
  215. }
  216. void meshopt_remapVertexBuffer(void* destination, const void* vertices, size_t vertex_count, size_t vertex_size, const unsigned int* remap)
  217. {
  218. assert(vertex_size > 0 && vertex_size <= 256);
  219. meshopt_Allocator allocator;
  220. // support in-place remap
  221. if (destination == vertices)
  222. {
  223. unsigned char* vertices_copy = allocator.allocate<unsigned char>(vertex_count * vertex_size);
  224. memcpy(vertices_copy, vertices, vertex_count * vertex_size);
  225. vertices = vertices_copy;
  226. }
  227. for (size_t i = 0; i < vertex_count; ++i)
  228. {
  229. if (remap[i] != ~0u)
  230. {
  231. assert(remap[i] < vertex_count);
  232. memcpy(static_cast<unsigned char*>(destination) + remap[i] * vertex_size, static_cast<const unsigned char*>(vertices) + i * vertex_size, vertex_size);
  233. }
  234. }
  235. }
  236. void meshopt_remapIndexBuffer(unsigned int* destination, const unsigned int* indices, size_t index_count, const unsigned int* remap)
  237. {
  238. assert(index_count % 3 == 0);
  239. for (size_t i = 0; i < index_count; ++i)
  240. {
  241. unsigned int index = indices ? indices[i] : unsigned(i);
  242. assert(remap[index] != ~0u);
  243. destination[i] = remap[index];
  244. }
  245. }
  246. void meshopt_generateShadowIndexBuffer(unsigned int* destination, const unsigned int* indices, size_t index_count, const void* vertices, size_t vertex_count, size_t vertex_size, size_t vertex_stride)
  247. {
  248. using namespace meshopt;
  249. assert(indices);
  250. assert(index_count % 3 == 0);
  251. assert(vertex_size > 0 && vertex_size <= 256);
  252. assert(vertex_size <= vertex_stride);
  253. meshopt_Allocator allocator;
  254. unsigned int* remap = allocator.allocate<unsigned int>(vertex_count);
  255. memset(remap, -1, vertex_count * sizeof(unsigned int));
  256. VertexHasher hasher = {static_cast<const unsigned char*>(vertices), vertex_size, vertex_stride};
  257. size_t table_size = hashBuckets(vertex_count);
  258. unsigned int* table = allocator.allocate<unsigned int>(table_size);
  259. memset(table, -1, table_size * sizeof(unsigned int));
  260. for (size_t i = 0; i < index_count; ++i)
  261. {
  262. unsigned int index = indices[i];
  263. assert(index < vertex_count);
  264. if (remap[index] == ~0u)
  265. {
  266. unsigned int* entry = hashLookup(table, table_size, hasher, index, ~0u);
  267. if (*entry == ~0u)
  268. *entry = index;
  269. remap[index] = *entry;
  270. }
  271. destination[i] = remap[index];
  272. }
  273. }
  274. void meshopt_generateShadowIndexBufferMulti(unsigned int* destination, const unsigned int* indices, size_t index_count, size_t vertex_count, const struct meshopt_Stream* streams, size_t stream_count)
  275. {
  276. using namespace meshopt;
  277. assert(indices);
  278. assert(index_count % 3 == 0);
  279. assert(stream_count > 0 && stream_count <= 16);
  280. for (size_t i = 0; i < stream_count; ++i)
  281. {
  282. assert(streams[i].size > 0 && streams[i].size <= 256);
  283. assert(streams[i].size <= streams[i].stride);
  284. }
  285. meshopt_Allocator allocator;
  286. unsigned int* remap = allocator.allocate<unsigned int>(vertex_count);
  287. memset(remap, -1, vertex_count * sizeof(unsigned int));
  288. VertexStreamHasher hasher = {streams, stream_count};
  289. size_t table_size = hashBuckets(vertex_count);
  290. unsigned int* table = allocator.allocate<unsigned int>(table_size);
  291. memset(table, -1, table_size * sizeof(unsigned int));
  292. for (size_t i = 0; i < index_count; ++i)
  293. {
  294. unsigned int index = indices[i];
  295. assert(index < vertex_count);
  296. if (remap[index] == ~0u)
  297. {
  298. unsigned int* entry = hashLookup(table, table_size, hasher, index, ~0u);
  299. if (*entry == ~0u)
  300. *entry = index;
  301. remap[index] = *entry;
  302. }
  303. destination[i] = remap[index];
  304. }
  305. }
  306. void meshopt_generateAdjacencyIndexBuffer(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride)
  307. {
  308. using namespace meshopt;
  309. assert(index_count % 3 == 0);
  310. assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
  311. assert(vertex_positions_stride % sizeof(float) == 0);
  312. meshopt_Allocator allocator;
  313. static const int next[4] = {1, 2, 0, 1};
  314. // build position remap: for each vertex, which other (canonical) vertex does it map to?
  315. unsigned int* remap = allocator.allocate<unsigned int>(vertex_count);
  316. buildPositionRemap(remap, vertex_positions, vertex_count, vertex_positions_stride, allocator);
  317. // build edge set; this stores all triangle edges but we can look these up by any other wedge
  318. EdgeHasher edge_hasher = {remap};
  319. size_t edge_table_size = hashBuckets(index_count);
  320. unsigned long long* edge_table = allocator.allocate<unsigned long long>(edge_table_size);
  321. unsigned int* edge_vertex_table = allocator.allocate<unsigned int>(edge_table_size);
  322. memset(edge_table, -1, edge_table_size * sizeof(unsigned long long));
  323. memset(edge_vertex_table, -1, edge_table_size * sizeof(unsigned int));
  324. for (size_t i = 0; i < index_count; i += 3)
  325. {
  326. for (int e = 0; e < 3; ++e)
  327. {
  328. unsigned int i0 = indices[i + e];
  329. unsigned int i1 = indices[i + next[e]];
  330. unsigned int i2 = indices[i + next[e + 1]];
  331. assert(i0 < vertex_count && i1 < vertex_count && i2 < vertex_count);
  332. unsigned long long edge = ((unsigned long long)i0 << 32) | i1;
  333. unsigned long long* entry = hashLookup(edge_table, edge_table_size, edge_hasher, edge, ~0ull);
  334. if (*entry == ~0ull)
  335. {
  336. *entry = edge;
  337. // store vertex opposite to the edge
  338. edge_vertex_table[entry - edge_table] = i2;
  339. }
  340. }
  341. }
  342. // build resulting index buffer: 6 indices for each input triangle
  343. for (size_t i = 0; i < index_count; i += 3)
  344. {
  345. unsigned int patch[6];
  346. for (int e = 0; e < 3; ++e)
  347. {
  348. unsigned int i0 = indices[i + e];
  349. unsigned int i1 = indices[i + next[e]];
  350. assert(i0 < vertex_count && i1 < vertex_count);
  351. // note: this refers to the opposite edge!
  352. unsigned long long edge = ((unsigned long long)i1 << 32) | i0;
  353. unsigned long long* oppe = hashLookup(edge_table, edge_table_size, edge_hasher, edge, ~0ull);
  354. patch[e * 2 + 0] = i0;
  355. patch[e * 2 + 1] = (*oppe == ~0ull) ? i0 : edge_vertex_table[oppe - edge_table];
  356. }
  357. memcpy(destination + i * 2, patch, sizeof(patch));
  358. }
  359. }
  360. void meshopt_generateTessellationIndexBuffer(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride)
  361. {
  362. using namespace meshopt;
  363. assert(index_count % 3 == 0);
  364. assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
  365. assert(vertex_positions_stride % sizeof(float) == 0);
  366. meshopt_Allocator allocator;
  367. static const int next[3] = {1, 2, 0};
  368. // build position remap: for each vertex, which other (canonical) vertex does it map to?
  369. unsigned int* remap = allocator.allocate<unsigned int>(vertex_count);
  370. buildPositionRemap(remap, vertex_positions, vertex_count, vertex_positions_stride, allocator);
  371. // build edge set; this stores all triangle edges but we can look these up by any other wedge
  372. EdgeHasher edge_hasher = {remap};
  373. size_t edge_table_size = hashBuckets(index_count);
  374. unsigned long long* edge_table = allocator.allocate<unsigned long long>(edge_table_size);
  375. memset(edge_table, -1, edge_table_size * sizeof(unsigned long long));
  376. for (size_t i = 0; i < index_count; i += 3)
  377. {
  378. for (int e = 0; e < 3; ++e)
  379. {
  380. unsigned int i0 = indices[i + e];
  381. unsigned int i1 = indices[i + next[e]];
  382. assert(i0 < vertex_count && i1 < vertex_count);
  383. unsigned long long edge = ((unsigned long long)i0 << 32) | i1;
  384. unsigned long long* entry = hashLookup(edge_table, edge_table_size, edge_hasher, edge, ~0ull);
  385. if (*entry == ~0ull)
  386. *entry = edge;
  387. }
  388. }
  389. // build resulting index buffer: 12 indices for each input triangle
  390. for (size_t i = 0; i < index_count; i += 3)
  391. {
  392. unsigned int patch[12];
  393. for (int e = 0; e < 3; ++e)
  394. {
  395. unsigned int i0 = indices[i + e];
  396. unsigned int i1 = indices[i + next[e]];
  397. assert(i0 < vertex_count && i1 < vertex_count);
  398. // note: this refers to the opposite edge!
  399. unsigned long long edge = ((unsigned long long)i1 << 32) | i0;
  400. unsigned long long oppe = *hashLookup(edge_table, edge_table_size, edge_hasher, edge, ~0ull);
  401. // use the same edge if opposite edge doesn't exist (border)
  402. oppe = (oppe == ~0ull) ? edge : oppe;
  403. // triangle index (0, 1, 2)
  404. patch[e] = i0;
  405. // opposite edge (3, 4; 5, 6; 7, 8)
  406. patch[3 + e * 2 + 0] = unsigned(oppe);
  407. patch[3 + e * 2 + 1] = unsigned(oppe >> 32);
  408. // dominant vertex (9, 10, 11)
  409. patch[9 + e] = remap[i0];
  410. }
  411. memcpy(destination + i * 4, patch, sizeof(patch));
  412. }
  413. }