| 1 | // This file is part of meshoptimizer library; see meshoptimizer.h for version/license details |
| 2 | #include "meshoptimizer.h" |
| 3 | |
| 4 | #include <assert.h> |
| 5 | #include <float.h> |
| 6 | #include <string.h> |
| 7 | |
| 8 | // This work is based on: |
| 9 | // Fabian Giesen. Decoding Morton codes. 2009 |
| 10 | namespace meshopt |
| 11 | { |
| 12 | |
| 13 | // "Insert" two 0 bits after each of the 20 low bits of x |
| 14 | inline unsigned long long part1By2(unsigned long long x) |
| 15 | { |
| 16 | x &= 0x000fffffull; // x = ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- jihg fedc ba98 7654 3210 |
| 17 | x = (x ^ (x << 32)) & 0x000f00000000ffffull; // x = ---- ---- ---- jihg ---- ---- ---- ---- ---- ---- ---- ---- fedc ba98 7654 3210 |
| 18 | x = (x ^ (x << 16)) & 0x000f0000ff0000ffull; // x = ---- ---- ---- jihg ---- ---- ---- ---- fedc ba98 ---- ---- ---- ---- 7654 3210 |
| 19 | x = (x ^ (x << 8)) & 0x000f00f00f00f00full; // x = ---- ---- ---- jihg ---- ---- fedc ---- ---- ba98 ---- ---- 7654 ---- ---- 3210 |
| 20 | x = (x ^ (x << 4)) & 0x00c30c30c30c30c3ull; // x = ---- ---- ji-- --hg ---- fe-- --dc ---- ba-- --98 ---- 76-- --54 ---- 32-- --10 |
| 21 | x = (x ^ (x << 2)) & 0x0249249249249249ull; // x = ---- --j- -i-- h--g --f- -e-- d--c --b- -a-- 9--8 --7- -6-- 5--4 --3- -2-- 1--0 |
| 22 | return x; |
| 23 | } |
| 24 | |
| 25 | static void computeOrder(unsigned long long* result, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, bool morton) |
| 26 | { |
| 27 | size_t vertex_stride_float = vertex_positions_stride / sizeof(float); |
| 28 | |
| 29 | float minv[3] = {FLT_MAX, FLT_MAX, FLT_MAX}; |
| 30 | float maxv[3] = {-FLT_MAX, -FLT_MAX, -FLT_MAX}; |
| 31 | |
| 32 | for (size_t i = 0; i < vertex_count; ++i) |
| 33 | { |
| 34 | const float* v = vertex_positions_data + i * vertex_stride_float; |
| 35 | |
| 36 | for (int j = 0; j < 3; ++j) |
| 37 | { |
| 38 | float vj = v[j]; |
| 39 | |
| 40 | minv[j] = minv[j] > vj ? vj : minv[j]; |
| 41 | maxv[j] = maxv[j] < vj ? vj : maxv[j]; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | float extent = 0.f; |
| 46 | |
| 47 | extent = (maxv[0] - minv[0]) < extent ? extent : (maxv[0] - minv[0]); |
| 48 | extent = (maxv[1] - minv[1]) < extent ? extent : (maxv[1] - minv[1]); |
| 49 | extent = (maxv[2] - minv[2]) < extent ? extent : (maxv[2] - minv[2]); |
| 50 | |
| 51 | // rescale each axis to 16 bits to get 48-bit Morton codes |
| 52 | float scale = extent == 0 ? 0.f : 65535.f / extent; |
| 53 | |
| 54 | // generate Morton order based on the position inside a unit cube |
| 55 | for (size_t i = 0; i < vertex_count; ++i) |
| 56 | { |
| 57 | const float* v = vertex_positions_data + i * vertex_stride_float; |
| 58 | |
| 59 | int x = int((v[0] - minv[0]) * scale + 0.5f); |
| 60 | int y = int((v[1] - minv[1]) * scale + 0.5f); |
| 61 | int z = int((v[2] - minv[2]) * scale + 0.5f); |
| 62 | |
| 63 | if (morton) |
| 64 | result[i] = part1By2(x) | (part1By2(y) << 1) | (part1By2(z) << 2); |
| 65 | else |
| 66 | result[i] = ((unsigned long long)x << 0) | ((unsigned long long)y << 20) | ((unsigned long long)z << 40); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | static void radixSort10(unsigned int* destination, const unsigned int* source, const unsigned short* keys, size_t count) |
| 71 | { |
| 72 | unsigned int hist[1024]; |
| 73 | memset(hist, 0, sizeof(hist)); |
| 74 | |
| 75 | // compute histogram (assume keys are 10-bit) |
| 76 | for (size_t i = 0; i < count; ++i) |
| 77 | hist[keys[i]]++; |
| 78 | |
| 79 | unsigned int sum = 0; |
| 80 | |
| 81 | // replace histogram data with prefix histogram sums in-place |
| 82 | for (int i = 0; i < 1024; ++i) |
| 83 | { |
| 84 | unsigned int h = hist[i]; |
| 85 | hist[i] = sum; |
| 86 | sum += h; |
| 87 | } |
| 88 | |
| 89 | assert(sum == count); |
| 90 | |
| 91 | // reorder values |
| 92 | for (size_t i = 0; i < count; ++i) |
| 93 | { |
| 94 | unsigned int id = keys[source[i]]; |
| 95 | |
| 96 | destination[hist[id]++] = source[i]; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | static void computeHistogram(unsigned int (&hist)[256][2], const unsigned short* data, size_t count) |
| 101 | { |
| 102 | memset(hist, 0, sizeof(hist)); |
| 103 | |
| 104 | // compute 2 8-bit histograms in parallel |
| 105 | for (size_t i = 0; i < count; ++i) |
| 106 | { |
| 107 | unsigned long long id = data[i]; |
| 108 | |
| 109 | hist[(id >> 0) & 255][0]++; |
| 110 | hist[(id >> 8) & 255][1]++; |
| 111 | } |
| 112 | |
| 113 | unsigned int sum0 = 0, sum1 = 0; |
| 114 | |
| 115 | // replace histogram data with prefix histogram sums in-place |
| 116 | for (int i = 0; i < 256; ++i) |
| 117 | { |
| 118 | unsigned int h0 = hist[i][0], h1 = hist[i][1]; |
| 119 | |
| 120 | hist[i][0] = sum0; |
| 121 | hist[i][1] = sum1; |
| 122 | |
| 123 | sum0 += h0; |
| 124 | sum1 += h1; |
| 125 | } |
| 126 | |
| 127 | assert(sum0 == count && sum1 == count); |
| 128 | } |
| 129 | |
| 130 | static void radixPass(unsigned int* destination, const unsigned int* source, const unsigned short* keys, size_t count, unsigned int (&hist)[256][2], int pass) |
| 131 | { |
| 132 | int bitoff = pass * 8; |
| 133 | |
| 134 | for (size_t i = 0; i < count; ++i) |
| 135 | { |
| 136 | unsigned int id = unsigned(keys[source[i]] >> bitoff) & 255; |
| 137 | |
| 138 | destination[hist[id][pass]++] = source[i]; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | static void partitionPoints(unsigned int* target, const unsigned int* order, const unsigned char* sides, size_t split, size_t count) |
| 143 | { |
| 144 | size_t l = 0, r = split; |
| 145 | |
| 146 | for (size_t i = 0; i < count; ++i) |
| 147 | { |
| 148 | unsigned char side = sides[order[i]]; |
| 149 | target[side ? r : l] = order[i]; |
| 150 | l += 1; |
| 151 | l -= side; |
| 152 | r += side; |
| 153 | } |
| 154 | |
| 155 | assert(l == split && r == count); |
| 156 | } |
| 157 | |
| 158 | static void splitPoints(unsigned int* destination, unsigned int* orderx, unsigned int* ordery, unsigned int* orderz, const unsigned long long* keys, size_t count, void* scratch, size_t cluster_size) |
| 159 | { |
| 160 | if (count <= cluster_size) |
| 161 | { |
| 162 | memcpy(destination, orderx, count * sizeof(unsigned int)); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | unsigned int* axes[3] = {orderx, ordery, orderz}; |
| 167 | |
| 168 | int bestk = -1; |
| 169 | unsigned int bestdim = 0; |
| 170 | |
| 171 | for (int k = 0; k < 3; ++k) |
| 172 | { |
| 173 | const unsigned int mask = (1 << 20) - 1; |
| 174 | unsigned int dim = (unsigned(keys[axes[k][count - 1]] >> (k * 20)) & mask) - (unsigned(keys[axes[k][0]] >> (k * 20)) & mask); |
| 175 | |
| 176 | if (dim >= bestdim) |
| 177 | { |
| 178 | bestk = k; |
| 179 | bestdim = dim; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | assert(bestk >= 0); |
| 184 | |
| 185 | // split roughly in half, with the left split always being aligned to cluster size |
| 186 | size_t split = ((count / 2) + cluster_size - 1) / cluster_size * cluster_size; |
| 187 | assert(split > 0 && split < count); |
| 188 | |
| 189 | // mark sides of split for partitioning |
| 190 | unsigned char* sides = static_cast<unsigned char*>(scratch) + count * sizeof(unsigned int); |
| 191 | |
| 192 | for (size_t i = 0; i < split; ++i) |
| 193 | sides[axes[bestk][i]] = 0; |
| 194 | |
| 195 | for (size_t i = split; i < count; ++i) |
| 196 | sides[axes[bestk][i]] = 1; |
| 197 | |
| 198 | // partition all axes into two sides, maintaining order |
| 199 | unsigned int* temp = static_cast<unsigned int*>(scratch); |
| 200 | |
| 201 | for (int k = 0; k < 3; ++k) |
| 202 | { |
| 203 | if (k == bestk) |
| 204 | continue; |
| 205 | |
| 206 | unsigned int* axis = axes[k]; |
| 207 | memcpy(temp, axis, sizeof(unsigned int) * count); |
| 208 | partitionPoints(axis, temp, sides, split, count); |
| 209 | } |
| 210 | |
| 211 | // recursion depth is logarithmic and bounded as we always split in approximately half |
| 212 | splitPoints(destination, orderx, ordery, orderz, keys, split, scratch, cluster_size); |
| 213 | splitPoints(destination + split, orderx + split, ordery + split, orderz + split, keys, count - split, scratch, cluster_size); |
| 214 | } |
| 215 | |
| 216 | } // namespace meshopt |
| 217 | |
| 218 | void meshopt_spatialSortRemap(unsigned int* destination, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride) |
| 219 | { |
| 220 | using namespace meshopt; |
| 221 | |
| 222 | assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256); |
| 223 | assert(vertex_positions_stride % sizeof(float) == 0); |
| 224 | |
| 225 | meshopt_Allocator allocator; |
| 226 | |
| 227 | unsigned long long* keys = allocator.allocate<unsigned long long>(vertex_count); |
| 228 | computeOrder(keys, vertex_positions, vertex_count, vertex_positions_stride, /* morton= */ true); |
| 229 | |
| 230 | unsigned int* scratch = allocator.allocate<unsigned int>(vertex_count * 2); // 4b for order + 2b for keys |
| 231 | unsigned short* keyk = (unsigned short*)(scratch + vertex_count); |
| 232 | |
| 233 | for (size_t i = 0; i < vertex_count; ++i) |
| 234 | destination[i] = unsigned(i); |
| 235 | |
| 236 | unsigned int* order[] = {scratch, destination}; |
| 237 | |
| 238 | // 5-pass radix sort computes the resulting order into scratch |
| 239 | for (int k = 0; k < 5; ++k) |
| 240 | { |
| 241 | // copy 10-bit key segments into keyk to reduce cache pressure during radix pass |
| 242 | for (size_t i = 0; i < vertex_count; ++i) |
| 243 | keyk[i] = (unsigned short)((keys[i] >> (k * 10)) & 1023); |
| 244 | |
| 245 | radixSort10(order[k % 2], order[(k + 1) % 2], keyk, vertex_count); |
| 246 | } |
| 247 | |
| 248 | // since our remap table is mapping old=>new, we need to reverse it |
| 249 | for (size_t i = 0; i < vertex_count; ++i) |
| 250 | destination[scratch[i]] = unsigned(i); |
| 251 | } |
| 252 | |
| 253 | 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) |
| 254 | { |
| 255 | using namespace meshopt; |
| 256 | |
| 257 | assert(index_count % 3 == 0); |
| 258 | assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256); |
| 259 | assert(vertex_positions_stride % sizeof(float) == 0); |
| 260 | |
| 261 | (void)vertex_count; |
| 262 | |
| 263 | size_t face_count = index_count / 3; |
| 264 | size_t vertex_stride_float = vertex_positions_stride / sizeof(float); |
| 265 | |
| 266 | meshopt_Allocator allocator; |
| 267 | |
| 268 | float* centroids = allocator.allocate<float>(face_count * 3); |
| 269 | |
| 270 | for (size_t i = 0; i < face_count; ++i) |
| 271 | { |
| 272 | unsigned int a = indices[i * 3 + 0], b = indices[i * 3 + 1], c = indices[i * 3 + 2]; |
| 273 | assert(a < vertex_count && b < vertex_count && c < vertex_count); |
| 274 | |
| 275 | const float* va = vertex_positions + a * vertex_stride_float; |
| 276 | const float* vb = vertex_positions + b * vertex_stride_float; |
| 277 | const float* vc = vertex_positions + c * vertex_stride_float; |
| 278 | |
| 279 | centroids[i * 3 + 0] = (va[0] + vb[0] + vc[0]) / 3.f; |
| 280 | centroids[i * 3 + 1] = (va[1] + vb[1] + vc[1]) / 3.f; |
| 281 | centroids[i * 3 + 2] = (va[2] + vb[2] + vc[2]) / 3.f; |
| 282 | } |
| 283 | |
| 284 | unsigned int* remap = allocator.allocate<unsigned int>(face_count); |
| 285 | |
| 286 | meshopt_spatialSortRemap(remap, centroids, face_count, sizeof(float) * 3); |
| 287 | |
| 288 | // support in-order remap |
| 289 | if (destination == indices) |
| 290 | { |
| 291 | unsigned int* indices_copy = allocator.allocate<unsigned int>(index_count); |
| 292 | memcpy(indices_copy, indices, index_count * sizeof(unsigned int)); |
| 293 | indices = indices_copy; |
| 294 | } |
| 295 | |
| 296 | for (size_t i = 0; i < face_count; ++i) |
| 297 | { |
| 298 | unsigned int a = indices[i * 3 + 0], b = indices[i * 3 + 1], c = indices[i * 3 + 2]; |
| 299 | unsigned int r = remap[i]; |
| 300 | |
| 301 | destination[r * 3 + 0] = a; |
| 302 | destination[r * 3 + 1] = b; |
| 303 | destination[r * 3 + 2] = c; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | void meshopt_spatialClusterPoints(unsigned int* destination, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t cluster_size) |
| 308 | { |
| 309 | using namespace meshopt; |
| 310 | |
| 311 | assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256); |
| 312 | assert(vertex_positions_stride % sizeof(float) == 0); |
| 313 | assert(cluster_size > 0); |
| 314 | |
| 315 | meshopt_Allocator allocator; |
| 316 | |
| 317 | unsigned long long* keys = allocator.allocate<unsigned long long>(vertex_count); |
| 318 | computeOrder(keys, vertex_positions, vertex_count, vertex_positions_stride, /* morton= */ false); |
| 319 | |
| 320 | unsigned int* order = allocator.allocate<unsigned int>(vertex_count * 3); |
| 321 | unsigned int* scratch = allocator.allocate<unsigned int>(vertex_count * 2); // 4b for order + 1b for side or 2b for keys |
| 322 | unsigned short* keyk = reinterpret_cast<unsigned short*>(scratch + vertex_count); |
| 323 | |
| 324 | for (int k = 0; k < 3; ++k) |
| 325 | { |
| 326 | // copy 16-bit key segments into keyk to reduce cache pressure during radix pass |
| 327 | for (size_t i = 0; i < vertex_count; ++i) |
| 328 | keyk[i] = (unsigned short)(keys[i] >> (k * 20)); |
| 329 | |
| 330 | unsigned int hist[256][2]; |
| 331 | computeHistogram(hist, keyk, vertex_count); |
| 332 | |
| 333 | for (size_t i = 0; i < vertex_count; ++i) |
| 334 | order[k * vertex_count + i] = unsigned(i); |
| 335 | |
| 336 | radixPass(scratch, order + k * vertex_count, keyk, vertex_count, hist, 0); |
| 337 | radixPass(order + k * vertex_count, scratch, keyk, vertex_count, hist, 1); |
| 338 | } |
| 339 | |
| 340 | splitPoints(destination, order, order + vertex_count, order + 2 * vertex_count, keys, vertex_count, scratch, cluster_size); |
| 341 | } |