Path: blob/master/thirdparty/meshoptimizer/spatialorder.cpp
9903 views
// This file is part of meshoptimizer library; see meshoptimizer.h for version/license details1#include "meshoptimizer.h"23#include <assert.h>4#include <float.h>5#include <string.h>67// This work is based on:8// Fabian Giesen. Decoding Morton codes. 20099namespace meshopt10{1112// "Insert" two 0 bits after each of the 20 low bits of x13inline unsigned long long part1By2(unsigned long long x)14{15x &= 0x000fffffull; // x = ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- jihg fedc ba98 7654 321016x = (x ^ (x << 32)) & 0x000f00000000ffffull; // x = ---- ---- ---- jihg ---- ---- ---- ---- ---- ---- ---- ---- fedc ba98 7654 321017x = (x ^ (x << 16)) & 0x000f0000ff0000ffull; // x = ---- ---- ---- jihg ---- ---- ---- ---- fedc ba98 ---- ---- ---- ---- 7654 321018x = (x ^ (x << 8)) & 0x000f00f00f00f00full; // x = ---- ---- ---- jihg ---- ---- fedc ---- ---- ba98 ---- ---- 7654 ---- ---- 321019x = (x ^ (x << 4)) & 0x00c30c30c30c30c3ull; // x = ---- ---- ji-- --hg ---- fe-- --dc ---- ba-- --98 ---- 76-- --54 ---- 32-- --1020x = (x ^ (x << 2)) & 0x0249249249249249ull; // x = ---- --j- -i-- h--g --f- -e-- d--c --b- -a-- 9--8 --7- -6-- 5--4 --3- -2-- 1--021return x;22}2324static void computeOrder(unsigned long long* result, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, bool morton)25{26size_t vertex_stride_float = vertex_positions_stride / sizeof(float);2728float minv[3] = {FLT_MAX, FLT_MAX, FLT_MAX};29float maxv[3] = {-FLT_MAX, -FLT_MAX, -FLT_MAX};3031for (size_t i = 0; i < vertex_count; ++i)32{33const float* v = vertex_positions_data + i * vertex_stride_float;3435for (int j = 0; j < 3; ++j)36{37float vj = v[j];3839minv[j] = minv[j] > vj ? vj : minv[j];40maxv[j] = maxv[j] < vj ? vj : maxv[j];41}42}4344float extent = 0.f;4546extent = (maxv[0] - minv[0]) < extent ? extent : (maxv[0] - minv[0]);47extent = (maxv[1] - minv[1]) < extent ? extent : (maxv[1] - minv[1]);48extent = (maxv[2] - minv[2]) < extent ? extent : (maxv[2] - minv[2]);4950// rescale each axis to 16 bits to get 48-bit Morton codes51float scale = extent == 0 ? 0.f : 65535.f / extent;5253// generate Morton order based on the position inside a unit cube54for (size_t i = 0; i < vertex_count; ++i)55{56const float* v = vertex_positions_data + i * vertex_stride_float;5758int x = int((v[0] - minv[0]) * scale + 0.5f);59int y = int((v[1] - minv[1]) * scale + 0.5f);60int z = int((v[2] - minv[2]) * scale + 0.5f);6162if (morton)63result[i] = part1By2(x) | (part1By2(y) << 1) | (part1By2(z) << 2);64else65result[i] = ((unsigned long long)x << 0) | ((unsigned long long)y << 20) | ((unsigned long long)z << 40);66}67}6869static void radixSort10(unsigned int* destination, const unsigned int* source, const unsigned short* keys, size_t count)70{71unsigned int hist[1024];72memset(hist, 0, sizeof(hist));7374// compute histogram (assume keys are 10-bit)75for (size_t i = 0; i < count; ++i)76hist[keys[i]]++;7778unsigned int sum = 0;7980// replace histogram data with prefix histogram sums in-place81for (int i = 0; i < 1024; ++i)82{83unsigned int h = hist[i];84hist[i] = sum;85sum += h;86}8788assert(sum == count);8990// reorder values91for (size_t i = 0; i < count; ++i)92{93unsigned int id = keys[source[i]];9495destination[hist[id]++] = source[i];96}97}9899static void computeHistogram(unsigned int (&hist)[256][2], const unsigned short* data, size_t count)100{101memset(hist, 0, sizeof(hist));102103// compute 2 8-bit histograms in parallel104for (size_t i = 0; i < count; ++i)105{106unsigned long long id = data[i];107108hist[(id >> 0) & 255][0]++;109hist[(id >> 8) & 255][1]++;110}111112unsigned int sum0 = 0, sum1 = 0;113114// replace histogram data with prefix histogram sums in-place115for (int i = 0; i < 256; ++i)116{117unsigned int h0 = hist[i][0], h1 = hist[i][1];118119hist[i][0] = sum0;120hist[i][1] = sum1;121122sum0 += h0;123sum1 += h1;124}125126assert(sum0 == count && sum1 == count);127}128129static void radixPass(unsigned int* destination, const unsigned int* source, const unsigned short* keys, size_t count, unsigned int (&hist)[256][2], int pass)130{131int bitoff = pass * 8;132133for (size_t i = 0; i < count; ++i)134{135unsigned int id = unsigned(keys[source[i]] >> bitoff) & 255;136137destination[hist[id][pass]++] = source[i];138}139}140141static void partitionPoints(unsigned int* target, const unsigned int* order, const unsigned char* sides, size_t split, size_t count)142{143size_t l = 0, r = split;144145for (size_t i = 0; i < count; ++i)146{147unsigned char side = sides[order[i]];148target[side ? r : l] = order[i];149l += 1;150l -= side;151r += side;152}153154assert(l == split && r == count);155}156157static 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)158{159if (count <= cluster_size)160{161memcpy(destination, orderx, count * sizeof(unsigned int));162return;163}164165unsigned int* axes[3] = {orderx, ordery, orderz};166167int bestk = -1;168unsigned int bestdim = 0;169170for (int k = 0; k < 3; ++k)171{172const unsigned int mask = (1 << 20) - 1;173unsigned int dim = (unsigned(keys[axes[k][count - 1]] >> (k * 20)) & mask) - (unsigned(keys[axes[k][0]] >> (k * 20)) & mask);174175if (dim >= bestdim)176{177bestk = k;178bestdim = dim;179}180}181182assert(bestk >= 0);183184// split roughly in half, with the left split always being aligned to cluster size185size_t split = ((count / 2) + cluster_size - 1) / cluster_size * cluster_size;186assert(split > 0 && split < count);187188// mark sides of split for partitioning189unsigned char* sides = static_cast<unsigned char*>(scratch) + count * sizeof(unsigned int);190191for (size_t i = 0; i < split; ++i)192sides[axes[bestk][i]] = 0;193194for (size_t i = split; i < count; ++i)195sides[axes[bestk][i]] = 1;196197// partition all axes into two sides, maintaining order198unsigned int* temp = static_cast<unsigned int*>(scratch);199200for (int k = 0; k < 3; ++k)201{202if (k == bestk)203continue;204205unsigned int* axis = axes[k];206memcpy(temp, axis, sizeof(unsigned int) * count);207partitionPoints(axis, temp, sides, split, count);208}209210splitPoints(destination, orderx, ordery, orderz, keys, split, scratch, cluster_size);211splitPoints(destination + split, orderx + split, ordery + split, orderz + split, keys, count - split, scratch, cluster_size);212}213214} // namespace meshopt215216void meshopt_spatialSortRemap(unsigned int* destination, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride)217{218using namespace meshopt;219220assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);221assert(vertex_positions_stride % sizeof(float) == 0);222223meshopt_Allocator allocator;224225unsigned long long* keys = allocator.allocate<unsigned long long>(vertex_count);226computeOrder(keys, vertex_positions, vertex_count, vertex_positions_stride, /* morton= */ true);227228unsigned int* scratch = allocator.allocate<unsigned int>(vertex_count * 2); // 4b for order + 2b for keys229unsigned short* keyk = (unsigned short*)(scratch + vertex_count);230231for (size_t i = 0; i < vertex_count; ++i)232destination[i] = unsigned(i);233234unsigned int* order[] = {scratch, destination};235236// 5-pass radix sort computes the resulting order into scratch237for (int k = 0; k < 5; ++k)238{239// copy 10-bit key segments into keyk to reduce cache pressure during radix pass240for (size_t i = 0; i < vertex_count; ++i)241keyk[i] = (unsigned short)((keys[i] >> (k * 10)) & 1023);242243radixSort10(order[k % 2], order[(k + 1) % 2], keyk, vertex_count);244}245246// since our remap table is mapping old=>new, we need to reverse it247for (size_t i = 0; i < vertex_count; ++i)248destination[scratch[i]] = unsigned(i);249}250251void 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)252{253using namespace meshopt;254255assert(index_count % 3 == 0);256assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);257assert(vertex_positions_stride % sizeof(float) == 0);258259(void)vertex_count;260261size_t face_count = index_count / 3;262size_t vertex_stride_float = vertex_positions_stride / sizeof(float);263264meshopt_Allocator allocator;265266float* centroids = allocator.allocate<float>(face_count * 3);267268for (size_t i = 0; i < face_count; ++i)269{270unsigned int a = indices[i * 3 + 0], b = indices[i * 3 + 1], c = indices[i * 3 + 2];271assert(a < vertex_count && b < vertex_count && c < vertex_count);272273const float* va = vertex_positions + a * vertex_stride_float;274const float* vb = vertex_positions + b * vertex_stride_float;275const float* vc = vertex_positions + c * vertex_stride_float;276277centroids[i * 3 + 0] = (va[0] + vb[0] + vc[0]) / 3.f;278centroids[i * 3 + 1] = (va[1] + vb[1] + vc[1]) / 3.f;279centroids[i * 3 + 2] = (va[2] + vb[2] + vc[2]) / 3.f;280}281282unsigned int* remap = allocator.allocate<unsigned int>(face_count);283284meshopt_spatialSortRemap(remap, centroids, face_count, sizeof(float) * 3);285286// support in-order remap287if (destination == indices)288{289unsigned int* indices_copy = allocator.allocate<unsigned int>(index_count);290memcpy(indices_copy, indices, index_count * sizeof(unsigned int));291indices = indices_copy;292}293294for (size_t i = 0; i < face_count; ++i)295{296unsigned int a = indices[i * 3 + 0], b = indices[i * 3 + 1], c = indices[i * 3 + 2];297unsigned int r = remap[i];298299destination[r * 3 + 0] = a;300destination[r * 3 + 1] = b;301destination[r * 3 + 2] = c;302}303}304305void meshopt_spatialClusterPoints(unsigned int* destination, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t cluster_size)306{307using namespace meshopt;308309assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);310assert(vertex_positions_stride % sizeof(float) == 0);311assert(cluster_size > 0);312313meshopt_Allocator allocator;314315unsigned long long* keys = allocator.allocate<unsigned long long>(vertex_count);316computeOrder(keys, vertex_positions, vertex_count, vertex_positions_stride, /* morton= */ false);317318unsigned int* order = allocator.allocate<unsigned int>(vertex_count * 3);319unsigned int* scratch = allocator.allocate<unsigned int>(vertex_count * 2); // 4b for order + 1b for side or 2b for keys320unsigned short* keyk = reinterpret_cast<unsigned short*>(scratch + vertex_count);321322for (int k = 0; k < 3; ++k)323{324// copy 16-bit key segments into keyk to reduce cache pressure during radix pass325for (size_t i = 0; i < vertex_count; ++i)326keyk[i] = (unsigned short)(keys[i] >> (k * 20));327328unsigned int hist[256][2];329computeHistogram(hist, keyk, vertex_count);330331for (size_t i = 0; i < vertex_count; ++i)332order[k * vertex_count + i] = unsigned(i);333334radixPass(scratch, order + k * vertex_count, keyk, vertex_count, hist, 0);335radixPass(order + k * vertex_count, scratch, keyk, vertex_count, hist, 1);336}337338splitPoints(destination, order, order + vertex_count, order + 2 * vertex_count, keys, vertex_count, scratch, cluster_size);339}340341342