spatialorder.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 <float.h>
  5. #include <string.h>
  6. // This work is based on:
  7. // Fabian Giesen. Decoding Morton codes. 2009
  8. namespace meshopt
  9. {
  10. // "Insert" two 0 bits after each of the 10 low bits of x
  11. inline unsigned int part1By2(unsigned int x)
  12. {
  13. x &= 0x000003ff; // x = ---- ---- ---- ---- ---- --98 7654 3210
  14. x = (x ^ (x << 16)) & 0xff0000ff; // x = ---- --98 ---- ---- ---- ---- 7654 3210
  15. x = (x ^ (x << 8)) & 0x0300f00f; // x = ---- --98 ---- ---- 7654 ---- ---- 3210
  16. x = (x ^ (x << 4)) & 0x030c30c3; // x = ---- --98 ---- 76-- --54 ---- 32-- --10
  17. x = (x ^ (x << 2)) & 0x09249249; // x = ---- 9--8 --7- -6-- 5--4 --3- -2-- 1--0
  18. return x;
  19. }
  20. static void computeOrder(unsigned int* result, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride)
  21. {
  22. size_t vertex_stride_float = vertex_positions_stride / sizeof(float);
  23. float minv[3] = {FLT_MAX, FLT_MAX, FLT_MAX};
  24. float maxv[3] = {-FLT_MAX, -FLT_MAX, -FLT_MAX};
  25. for (size_t i = 0; i < vertex_count; ++i)
  26. {
  27. const float* v = vertex_positions_data + i * vertex_stride_float;
  28. for (int j = 0; j < 3; ++j)
  29. {
  30. float vj = v[j];
  31. minv[j] = minv[j] > vj ? vj : minv[j];
  32. maxv[j] = maxv[j] < vj ? vj : maxv[j];
  33. }
  34. }
  35. float extent = 0.f;
  36. extent = (maxv[0] - minv[0]) < extent ? extent : (maxv[0] - minv[0]);
  37. extent = (maxv[1] - minv[1]) < extent ? extent : (maxv[1] - minv[1]);
  38. extent = (maxv[2] - minv[2]) < extent ? extent : (maxv[2] - minv[2]);
  39. float scale = extent == 0 ? 0.f : 1.f / extent;
  40. // generate Morton order based on the position inside a unit cube
  41. for (size_t i = 0; i < vertex_count; ++i)
  42. {
  43. const float* v = vertex_positions_data + i * vertex_stride_float;
  44. int x = int((v[0] - minv[0]) * scale * 1023.f + 0.5f);
  45. int y = int((v[1] - minv[1]) * scale * 1023.f + 0.5f);
  46. int z = int((v[2] - minv[2]) * scale * 1023.f + 0.5f);
  47. result[i] = part1By2(x) | (part1By2(y) << 1) | (part1By2(z) << 2);
  48. }
  49. }
  50. static void computeHistogram(unsigned int (&hist)[1024][3], const unsigned int* data, size_t count)
  51. {
  52. memset(hist, 0, sizeof(hist));
  53. // compute 3 10-bit histograms in parallel
  54. for (size_t i = 0; i < count; ++i)
  55. {
  56. unsigned int id = data[i];
  57. hist[(id >> 0) & 1023][0]++;
  58. hist[(id >> 10) & 1023][1]++;
  59. hist[(id >> 20) & 1023][2]++;
  60. }
  61. unsigned int sumx = 0, sumy = 0, sumz = 0;
  62. // replace histogram data with prefix histogram sums in-place
  63. for (int i = 0; i < 1024; ++i)
  64. {
  65. unsigned int hx = hist[i][0], hy = hist[i][1], hz = hist[i][2];
  66. hist[i][0] = sumx;
  67. hist[i][1] = sumy;
  68. hist[i][2] = sumz;
  69. sumx += hx;
  70. sumy += hy;
  71. sumz += hz;
  72. }
  73. assert(sumx == count && sumy == count && sumz == count);
  74. }
  75. static void radixPass(unsigned int* destination, const unsigned int* source, const unsigned int* keys, size_t count, unsigned int (&hist)[1024][3], int pass)
  76. {
  77. int bitoff = pass * 10;
  78. for (size_t i = 0; i < count; ++i)
  79. {
  80. unsigned int id = (keys[source[i]] >> bitoff) & 1023;
  81. destination[hist[id][pass]++] = source[i];
  82. }
  83. }
  84. } // namespace meshopt
  85. void meshopt_spatialSortRemap(unsigned int* destination, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride)
  86. {
  87. using namespace meshopt;
  88. assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
  89. assert(vertex_positions_stride % sizeof(float) == 0);
  90. meshopt_Allocator allocator;
  91. unsigned int* keys = allocator.allocate<unsigned int>(vertex_count);
  92. computeOrder(keys, vertex_positions, vertex_count, vertex_positions_stride);
  93. unsigned int hist[1024][3];
  94. computeHistogram(hist, keys, vertex_count);
  95. unsigned int* scratch = allocator.allocate<unsigned int>(vertex_count);
  96. for (size_t i = 0; i < vertex_count; ++i)
  97. destination[i] = unsigned(i);
  98. // 3-pass radix sort computes the resulting order into scratch
  99. radixPass(scratch, destination, keys, vertex_count, hist, 0);
  100. radixPass(destination, scratch, keys, vertex_count, hist, 1);
  101. radixPass(scratch, destination, keys, vertex_count, hist, 2);
  102. // since our remap table is mapping old=>new, we need to reverse it
  103. for (size_t i = 0; i < vertex_count; ++i)
  104. destination[scratch[i]] = unsigned(i);
  105. }
  106. void meshopt_spatialSortTriangles(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride)
  107. {
  108. using namespace meshopt;
  109. assert(index_count % 3 == 0);
  110. assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
  111. assert(vertex_positions_stride % sizeof(float) == 0);
  112. (void)vertex_count;
  113. size_t face_count = index_count / 3;
  114. size_t vertex_stride_float = vertex_positions_stride / sizeof(float);
  115. meshopt_Allocator allocator;
  116. float* centroids = allocator.allocate<float>(face_count * 3);
  117. for (size_t i = 0; i < face_count; ++i)
  118. {
  119. unsigned int a = indices[i * 3 + 0], b = indices[i * 3 + 1], c = indices[i * 3 + 2];
  120. assert(a < vertex_count && b < vertex_count && c < vertex_count);
  121. const float* va = vertex_positions + a * vertex_stride_float;
  122. const float* vb = vertex_positions + b * vertex_stride_float;
  123. const float* vc = vertex_positions + c * vertex_stride_float;
  124. centroids[i * 3 + 0] = (va[0] + vb[0] + vc[0]) / 3.f;
  125. centroids[i * 3 + 1] = (va[1] + vb[1] + vc[1]) / 3.f;
  126. centroids[i * 3 + 2] = (va[2] + vb[2] + vc[2]) / 3.f;
  127. }
  128. unsigned int* remap = allocator.allocate<unsigned int>(face_count);
  129. meshopt_spatialSortRemap(remap, centroids, face_count, sizeof(float) * 3);
  130. // support in-order remap
  131. if (destination == indices)
  132. {
  133. unsigned int* indices_copy = allocator.allocate<unsigned int>(index_count);
  134. memcpy(indices_copy, indices, index_count * sizeof(unsigned int));
  135. indices = indices_copy;
  136. }
  137. for (size_t i = 0; i < face_count; ++i)
  138. {
  139. unsigned int a = indices[i * 3 + 0], b = indices[i * 3 + 1], c = indices[i * 3 + 2];
  140. unsigned int r = remap[i];
  141. destination[r * 3 + 0] = a;
  142. destination[r * 3 + 1] = b;
  143. destination[r * 3 + 2] = c;
  144. }
  145. }