Update meshoptimizer to v0.25

Also expose new flags as SurfaceTool enums for future use
This commit is contained in:
Arseny Kapoulkine 2025-08-22 20:25:02 -07:00
parent 21fbf033f7
commit 90ff46c292
8 changed files with 1162 additions and 143 deletions

View file

@ -27,6 +27,7 @@
// Matthias Teschner, Bruno Heidelberger, Matthias Mueller, Danat Pomeranets, Markus Gross. Optimized Spatial Hashing for Collision Detection of Deformable Objects. 2003
// Peter Van Sandt, Yannis Chronis, Jignesh M. Patel. Efficiently Searching In-Memory Sorted Arrays: Revenge of the Interpolation Search? 2019
// Hugues Hoppe. New Quadric Metric for Simplifying Meshes with Appearance Attributes. 1999
// Hugues Hoppe, Steve Marschner. Efficient Minimization of New Quadric Metric for Simplifying Meshes with Appearance Attributes. 2000
namespace meshopt
{
@ -316,11 +317,13 @@ const unsigned char kCanCollapse[Kind_Count][Kind_Count] = {
// if a vertex is manifold or seam, adjoining edges are guaranteed to have an opposite edge
// note that for seam edges, the opposite edge isn't present in the attribute-based topology
// but is present if you consider a position-only mesh variant
// while many complex collapses have the opposite edge, since complex vertices collapse to the
// same wedge, keeping opposite edges separate improves the quality by considering both targets
const unsigned char kHasOpposite[Kind_Count][Kind_Count] = {
{1, 1, 1, 0, 1},
{1, 1, 1, 1, 1},
{1, 0, 1, 0, 0},
{1, 1, 1, 0, 1},
{0, 0, 0, 0, 0},
{1, 0, 0, 0, 0},
{1, 0, 1, 0, 0},
};
@ -336,6 +339,25 @@ static bool hasEdge(const EdgeAdjacency& adjacency, unsigned int a, unsigned int
return false;
}
static bool hasEdge(const EdgeAdjacency& adjacency, unsigned int a, unsigned int b, const unsigned int* remap, const unsigned int* wedge)
{
unsigned int v = a;
do
{
unsigned int count = adjacency.offsets[v + 1] - adjacency.offsets[v];
const EdgeAdjacency::Edge* edges = adjacency.data + adjacency.offsets[v];
for (size_t i = 0; i < count; ++i)
if (remap[edges[i].next] == remap[b])
return true;
v = wedge[v];
} while (v != a);
return false;
}
static void classifyVertices(unsigned char* result, unsigned int* loop, unsigned int* loopback, size_t vertex_count, const EdgeAdjacency& adjacency, const unsigned int* remap, const unsigned int* wedge, const unsigned char* vertex_lock, const unsigned int* sparse_remap, unsigned int options)
{
memset(loop, -1, vertex_count * sizeof(unsigned int));
@ -394,6 +416,13 @@ static void classifyVertices(unsigned char* result, unsigned int* loop, unsigned
{
result[i] = Kind_Manifold;
}
else if (openi != ~0u && openo != ~0u && remap[openi] == remap[openo] && openi != i)
{
// classify half-seams as seams (the branch below would mis-classify them as borders)
// half-seam is a single vertex that connects to both vertices of a potential seam
// treating these as seams allows collapsing the "full" seam vertex onto them
result[i] = Kind_Seam;
}
else if (openi != i && openo != i)
{
result[i] = Kind_Border;
@ -446,15 +475,50 @@ static void classifyVertices(unsigned char* result, unsigned int* loop, unsigned
}
}
if (options & meshopt_SimplifyPermissive)
for (size_t i = 0; i < vertex_count; ++i)
if (result[i] == Kind_Seam || result[i] == Kind_Locked)
{
if (remap[i] != i)
{
// only process primary vertices; wedges will be updated to match the primary vertex
result[i] = result[remap[i]];
continue;
}
bool protect = false;
// vertex_lock may protect any wedge, not just the primary vertex, so we switch to complex only if no wedges are protected
unsigned int v = unsigned(i);
do
{
unsigned int rv = sparse_remap ? sparse_remap[v] : v;
protect |= vertex_lock && (vertex_lock[rv] & meshopt_SimplifyVertex_Protect) != 0;
v = wedge[v];
} while (v != i);
// protect if any adjoining edge doesn't have an opposite edge (indicating vertex is on the border)
do
{
const EdgeAdjacency::Edge* edges = &adjacency.data[adjacency.offsets[v]];
size_t count = adjacency.offsets[v + 1] - adjacency.offsets[v];
for (size_t j = 0; j < count; ++j)
protect |= !hasEdge(adjacency, edges[j].next, v, remap, wedge);
v = wedge[v];
} while (v != i);
result[i] = protect ? result[i] : int(Kind_Complex);
}
if (vertex_lock)
{
// vertex_lock may lock any wedge, not just the primary vertex, so we need to lock the primary vertex and relock any wedges
for (size_t i = 0; i < vertex_count; ++i)
{
unsigned int ri = sparse_remap ? sparse_remap[i] : unsigned(i);
assert(vertex_lock[ri] <= 1); // values other than 0/1 are reserved for future use
if (vertex_lock[ri])
if (vertex_lock[ri] & meshopt_SimplifyVertex_Lock)
result[remap[i]] = Kind_Locked;
}
@ -479,7 +543,7 @@ struct Vector3
float x, y, z;
};
static float rescalePositions(Vector3* result, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, const unsigned int* sparse_remap = NULL)
static float rescalePositions(Vector3* result, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, const unsigned int* sparse_remap = NULL, float* out_offset = NULL)
{
size_t vertex_stride_float = vertex_positions_stride / sizeof(float);
@ -525,6 +589,13 @@ static float rescalePositions(Vector3* result, const float* vertex_positions_dat
}
}
if (out_offset)
{
out_offset[0] = minv[0];
out_offset[1] = minv[1];
out_offset[2] = minv[2];
}
return extent;
}
@ -546,11 +617,45 @@ static void rescaleAttributes(float* result, const float* vertex_attributes_data
}
}
static void finalizeVertices(float* vertex_positions_data, size_t vertex_positions_stride, float* vertex_attributes_data, size_t vertex_attributes_stride, const float* attribute_weights, size_t attribute_count, size_t vertex_count, const Vector3* vertex_positions, const float* vertex_attributes, const unsigned int* sparse_remap, const unsigned int* attribute_remap, float vertex_scale, const float* vertex_offset, const unsigned char* vertex_update)
{
size_t vertex_positions_stride_float = vertex_positions_stride / sizeof(float);
size_t vertex_attributes_stride_float = vertex_attributes_stride / sizeof(float);
for (size_t i = 0; i < vertex_count; ++i)
{
if (!vertex_update[i])
continue;
unsigned int ri = sparse_remap ? sparse_remap[i] : unsigned(i);
const Vector3& p = vertex_positions[i];
float* v = vertex_positions_data + ri * vertex_positions_stride_float;
v[0] = p.x * vertex_scale + vertex_offset[0];
v[1] = p.y * vertex_scale + vertex_offset[1];
v[2] = p.z * vertex_scale + vertex_offset[2];
if (attribute_count)
{
const float* sa = vertex_attributes + i * attribute_count;
float* va = vertex_attributes_data + ri * vertex_attributes_stride_float;
for (size_t k = 0; k < attribute_count; ++k)
{
unsigned int rk = attribute_remap[k];
va[rk] = sa[k] / attribute_weights[rk];
}
}
}
}
static const size_t kMaxAttributes = 32;
struct Quadric
{
// a00*x^2 + a11*y^2 + a22*z^2 + 2*(a10*xy + a20*xz + a21*yz) + b0*x + b1*y + b2*z + c
// a00*x^2 + a11*y^2 + a22*z^2 + 2*a10*xy + 2*a20*xz + 2*a21*yz + 2*b0*x + 2*b1*y + 2*b2*z + c
float a00, a11, a22;
float a10, a20, a21;
float b0, b1, b2, c;
@ -612,6 +717,14 @@ static void quadricAdd(Quadric& Q, const Quadric& R)
Q.w += R.w;
}
static void quadricAdd(QuadricGrad& G, const QuadricGrad& R)
{
G.gx += R.gx;
G.gy += R.gy;
G.gz += R.gz;
G.gw += R.gw;
}
static void quadricAdd(QuadricGrad* G, const QuadricGrad* R, size_t attribute_count)
{
for (size_t k = 0; k < attribute_count; ++k)
@ -694,6 +807,17 @@ static void quadricFromPlane(Quadric& Q, float a, float b, float c, float d, flo
Q.w = w;
}
static void quadricFromPoint(Quadric& Q, float x, float y, float z, float w)
{
Q.a00 = Q.a11 = Q.a22 = w;
Q.a10 = Q.a20 = Q.a21 = 0;
Q.b0 = -x * w;
Q.b1 = -y * w;
Q.b2 = -z * w;
Q.c = (x * x + y * y + z * z) * w;
Q.w = w;
}
static void quadricFromTriangle(Quadric& Q, const Vector3& p0, const Vector3& p1, const Vector3& p2, float weight)
{
Vector3 p10 = {p1.x - p0.x, p1.y - p0.y, p1.z - p0.z};
@ -814,7 +938,112 @@ static void quadricFromAttributes(Quadric& Q, QuadricGrad* G, const Vector3& p0,
}
}
static void fillFaceQuadrics(Quadric* vertex_quadrics, const unsigned int* indices, size_t index_count, const Vector3* vertex_positions, const unsigned int* remap)
static void quadricVolumeGradient(QuadricGrad& G, const Vector3& p0, const Vector3& p1, const Vector3& p2)
{
Vector3 p10 = {p1.x - p0.x, p1.y - p0.y, p1.z - p0.z};
Vector3 p20 = {p2.x - p0.x, p2.y - p0.y, p2.z - p0.z};
// normal = cross(p1 - p0, p2 - p0)
Vector3 normal = {p10.y * p20.z - p10.z * p20.y, p10.z * p20.x - p10.x * p20.z, p10.x * p20.y - p10.y * p20.x};
float area = normalize(normal) * 0.5f;
G.gx = normal.x * area;
G.gy = normal.y * area;
G.gz = normal.z * area;
G.gw = (-p0.x * normal.x - p0.y * normal.y - p0.z * normal.z) * area;
}
static bool quadricSolve(Vector3& p, const Quadric& Q, const QuadricGrad& GV)
{
// solve A*p = -b where A is the quadric matrix and b is the linear term
float a00 = Q.a00, a11 = Q.a11, a22 = Q.a22;
float a10 = Q.a10, a20 = Q.a20, a21 = Q.a21;
float x0 = -Q.b0, x1 = -Q.b1, x2 = -Q.b2;
float eps = 1e-6f * Q.w;
// LDL decomposition: A = LDL^T
float d0 = a00;
float l10 = a10 / d0;
float l20 = a20 / d0;
float d1 = a11 - a10 * l10;
float dl21 = a21 - a20 * l10;
float l21 = dl21 / d1;
float d2 = a22 - a20 * l20 - dl21 * l21;
// solve L*y = x
float y0 = x0;
float y1 = x1 - l10 * y0;
float y2 = x2 - l20 * y0 - l21 * y1;
// solve D*z = y
float z0 = y0 / d0;
float z1 = y1 / d1;
float z2 = y2 / d2;
// augment system with linear constraint GV using Lagrange multiplier
float a30 = GV.gx, a31 = GV.gy, a32 = GV.gz;
float x3 = -GV.gw;
float l30 = a30 / d0;
float dl31 = a31 - a30 * l10;
float l31 = dl31 / d1;
float dl32 = a32 - a30 * l20 - dl31 * l21;
float l32 = dl32 / d2;
float d3 = 0.f - a30 * l30 - dl31 * l31 - dl32 * l32;
float y3 = x3 - l30 * y0 - l31 * y1 - l32 * y2;
float z3 = fabsf(d3) > eps ? y3 / d3 : 0.f; // if d3 is zero, we can ignore the constraint
// substitute L^T*p = z
float lambda = z3;
float pz = z2 - l32 * lambda;
float py = z1 - l21 * pz - l31 * lambda;
float px = z0 - l10 * py - l20 * pz - l30 * lambda;
p.x = px;
p.y = py;
p.z = pz;
return fabsf(d0) > eps && fabsf(d1) > eps && fabsf(d2) > eps;
}
static void quadricReduceAttributes(Quadric& Q, const Quadric& A, const QuadricGrad* G, size_t attribute_count)
{
// update vertex quadric with attribute quadric; multiply by vertex weight to minimize normalized error
Q.a00 += A.a00 * Q.w;
Q.a11 += A.a11 * Q.w;
Q.a22 += A.a22 * Q.w;
Q.a10 += A.a10 * Q.w;
Q.a20 += A.a20 * Q.w;
Q.a21 += A.a21 * Q.w;
Q.b0 += A.b0 * Q.w;
Q.b1 += A.b1 * Q.w;
Q.b2 += A.b2 * Q.w;
float iaw = A.w == 0 ? 0.f : Q.w / A.w;
// update linear system based on attribute gradients (BB^T/a)
for (size_t k = 0; k < attribute_count; ++k)
{
const QuadricGrad& g = G[k];
Q.a00 -= (g.gx * g.gx) * iaw;
Q.a11 -= (g.gy * g.gy) * iaw;
Q.a22 -= (g.gz * g.gz) * iaw;
Q.a10 -= (g.gx * g.gy) * iaw;
Q.a20 -= (g.gx * g.gz) * iaw;
Q.a21 -= (g.gy * g.gz) * iaw;
Q.b0 -= (g.gx * g.gw) * iaw;
Q.b1 -= (g.gy * g.gw) * iaw;
Q.b2 -= (g.gz * g.gw) * iaw;
}
}
static void fillFaceQuadrics(Quadric* vertex_quadrics, QuadricGrad* volume_gradients, const unsigned int* indices, size_t index_count, const Vector3* vertex_positions, const unsigned int* remap)
{
for (size_t i = 0; i < index_count; i += 3)
{
@ -828,6 +1057,36 @@ static void fillFaceQuadrics(Quadric* vertex_quadrics, const unsigned int* indic
quadricAdd(vertex_quadrics[remap[i0]], Q);
quadricAdd(vertex_quadrics[remap[i1]], Q);
quadricAdd(vertex_quadrics[remap[i2]], Q);
if (volume_gradients)
{
QuadricGrad GV;
quadricVolumeGradient(GV, vertex_positions[i0], vertex_positions[i1], vertex_positions[i2]);
quadricAdd(volume_gradients[remap[i0]], GV);
quadricAdd(volume_gradients[remap[i1]], GV);
quadricAdd(volume_gradients[remap[i2]], GV);
}
}
}
static void fillVertexQuadrics(Quadric* vertex_quadrics, const Vector3* vertex_positions, size_t vertex_count, const unsigned int* remap, unsigned int options)
{
// by default, we use a very small weight to improve triangulation and numerical stability without affecting the shape or error
float factor = (options & meshopt_SimplifyRegularize) ? 1e-1f : 1e-7f;
for (size_t i = 0; i < vertex_count; ++i)
{
if (remap[i] != i)
continue;
const Vector3& p = vertex_positions[i];
float w = vertex_quadrics[i].w * factor;
Quadric Q;
quadricFromPoint(Q, p.x, p.y, p.z, w);
quadricAdd(vertex_quadrics[i], Q);
}
}
@ -857,15 +1116,11 @@ static void fillEdgeQuadrics(Quadric* vertex_quadrics, const unsigned int* indic
if ((k1 == Kind_Border || k1 == Kind_Seam) && loopback[i1] != i0)
continue;
// seam edges should occur twice (i0->i1 and i1->i0) - skip redundant edges
if (kHasOpposite[k0][k1] && remap[i1] > remap[i0])
continue;
unsigned int i2 = indices[i + next[e + 1]];
// we try hard to maintain border edge geometry; seam edges can move more freely
// due to topological restrictions on collapses, seam quadrics slightly improves collapse structure but aren't critical
const float kEdgeWeightSeam = 1.f;
const float kEdgeWeightSeam = 0.5f; // applied twice due to opposite edges
const float kEdgeWeightBorder = 10.f;
float edgeWeight = (k0 == Kind_Border || k1 == Kind_Border) ? kEdgeWeightBorder : kEdgeWeightSeam;
@ -873,6 +1128,13 @@ static void fillEdgeQuadrics(Quadric* vertex_quadrics, const unsigned int* indic
Quadric Q;
quadricFromTriangleEdge(Q, vertex_positions[i0], vertex_positions[i1], vertex_positions[i2], edgeWeight);
Quadric QT;
quadricFromTriangle(QT, vertex_positions[i0], vertex_positions[i1], vertex_positions[i2], edgeWeight);
// mix edge quadric with triangle quadric to stabilize collapses in both directions; both quadrics inherit edge weight so that their error is added
QT.w = 0;
quadricAdd(Q, QT);
quadricAdd(vertex_quadrics[remap[i0]], Q);
quadricAdd(vertex_quadrics[remap[i1]], Q);
}
@ -954,6 +1216,50 @@ static bool hasTriangleFlips(const EdgeAdjacency& adjacency, const Vector3* vert
return false;
}
static bool hasTriangleFlips(const EdgeAdjacency& adjacency, const Vector3* vertex_positions, unsigned int i0, const Vector3& v1)
{
const Vector3& v0 = vertex_positions[i0];
const EdgeAdjacency::Edge* edges = &adjacency.data[adjacency.offsets[i0]];
size_t count = adjacency.offsets[i0 + 1] - adjacency.offsets[i0];
for (size_t i = 0; i < count; ++i)
{
unsigned int a = edges[i].next, b = edges[i].prev;
if (hasTriangleFlip(vertex_positions[a], vertex_positions[b], v0, v1))
return true;
}
return false;
}
static float getNeighborhoodRadius(const EdgeAdjacency& adjacency, const Vector3* vertex_positions, unsigned int i0)
{
const Vector3& v0 = vertex_positions[i0];
const EdgeAdjacency::Edge* edges = &adjacency.data[adjacency.offsets[i0]];
size_t count = adjacency.offsets[i0 + 1] - adjacency.offsets[i0];
float result = 0.f;
for (size_t i = 0; i < count; ++i)
{
unsigned int a = edges[i].next, b = edges[i].prev;
const Vector3& va = vertex_positions[a];
const Vector3& vb = vertex_positions[b];
float da = (va.x - v0.x) * (va.x - v0.x) + (va.y - v0.y) * (va.y - v0.y) + (va.z - v0.z) * (va.z - v0.z);
float db = (vb.x - v0.x) * (vb.x - v0.x) + (vb.y - v0.y) * (vb.y - v0.y) + (vb.z - v0.z) * (vb.z - v0.z);
result = result < da ? da : result;
result = result < db ? db : result;
}
return sqrtf(result);
}
static size_t boundEdgeCollapses(const EdgeAdjacency& adjacency, size_t vertex_count, size_t index_count, unsigned char* vertex_kind)
{
size_t dual_count = 0;
@ -1008,19 +1314,11 @@ static size_t pickEdgeCollapses(Collapse* collapses, size_t collapse_capacity, c
// two vertices are on a border or a seam, but there's no direct edge between them
// this indicates that they belong to two different edge loops and we should not collapse this edge
// loop[] tracks half edges so we only need to check i0->i1
if (k0 == k1 && (k0 == Kind_Border || k0 == Kind_Seam) && loop[i0] != i1)
// loop[] and loopback[] track half edges so we only need to check one of them
if ((k0 == Kind_Border || k0 == Kind_Seam) && k1 != Kind_Manifold && loop[i0] != i1)
continue;
if ((k1 == Kind_Border || k1 == Kind_Seam) && k0 != Kind_Manifold && loopback[i1] != i0)
continue;
if (k0 == Kind_Locked || k1 == Kind_Locked)
{
// the same check as above, but for border/seam -> locked collapses
// loop[] and loopback[] track half edges so we only need to check one of them
if ((k0 == Kind_Border || k0 == Kind_Seam) && loop[i0] != i1)
continue;
if ((k1 == Kind_Border || k1 == Kind_Seam) && loopback[i1] != i0)
continue;
}
// edge can be collapsed in either direction - we will pick the one with minimum error
// note: we evaluate error later during collapse ranking, here we just tag the edge as bidirectional
@ -1052,14 +1350,10 @@ static void rankEdgeCollapses(Collapse* collapses, size_t collapse_count, const
unsigned int i0 = c.v0;
unsigned int i1 = c.v1;
// most edges are bidirectional which means we need to evaluate errors for two collapses
// to keep this code branchless we just use the same edge for unidirectional edges
unsigned int j0 = c.bidi ? i1 : i0;
unsigned int j1 = c.bidi ? i0 : i1;
bool bidi = c.bidi;
float ei = quadricError(vertex_quadrics[remap[i0]], vertex_positions[i1]);
float ej = c.bidi ? quadricError(vertex_quadrics[remap[j0]], vertex_positions[j1]) : FLT_MAX;
float ej = bidi ? quadricError(vertex_quadrics[remap[i1]], vertex_positions[i0]) : FLT_MAX;
#if TRACE >= 3
float di = ei, dj = ej;
@ -1068,39 +1362,53 @@ static void rankEdgeCollapses(Collapse* collapses, size_t collapse_count, const
if (attribute_count)
{
ei += quadricError(attribute_quadrics[i0], &attribute_gradients[i0 * attribute_count], attribute_count, vertex_positions[i1], &vertex_attributes[i1 * attribute_count]);
ej += c.bidi ? quadricError(attribute_quadrics[j0], &attribute_gradients[j0 * attribute_count], attribute_count, vertex_positions[j1], &vertex_attributes[j1 * attribute_count]) : 0;
ej += bidi ? quadricError(attribute_quadrics[i1], &attribute_gradients[i1 * attribute_count], attribute_count, vertex_positions[i0], &vertex_attributes[i0 * attribute_count]) : 0;
// note: seam edges need to aggregate attribute errors between primary and secondary edges, as attribute quadrics are separate
// seam edges need to aggregate attribute errors between primary and secondary edges, as attribute quadrics are separate
if (vertex_kind[i0] == Kind_Seam)
{
// for seam collapses we need to find the seam pair; this is a bit tricky since we need to rely on edge loops as target vertex may be locked (and thus have more than two wedges)
unsigned int s0 = wedge[i0];
unsigned int s1 = loop[i0] == i1 ? loopback[s0] : loop[s0];
assert(s0 != i0 && wedge[s0] == i0);
assert(wedge[s0] == i0); // s0 may be equal to i0 for half-seams
assert(s1 != ~0u && remap[s1] == remap[i1]);
// note: this should never happen due to the assertion above, but when disabled if we ever hit this case we'll get a memory safety issue; for now play it safe
s1 = (s1 != ~0u) ? s1 : wedge[i1];
ei += quadricError(attribute_quadrics[s0], &attribute_gradients[s0 * attribute_count], attribute_count, vertex_positions[s1], &vertex_attributes[s1 * attribute_count]);
ej += c.bidi ? quadricError(attribute_quadrics[s1], &attribute_gradients[s1 * attribute_count], attribute_count, vertex_positions[s0], &vertex_attributes[s0 * attribute_count]) : 0;
ej += bidi ? quadricError(attribute_quadrics[s1], &attribute_gradients[s1 * attribute_count], attribute_count, vertex_positions[s0], &vertex_attributes[s0 * attribute_count]) : 0;
}
else
{
// complex edges can have multiple wedges, so we need to aggregate errors for all wedges
// this is different from seams (where we aggregate pairwise) because all wedges collapse onto the same target
if (vertex_kind[i0] == Kind_Complex)
for (unsigned int v = wedge[i0]; v != i0; v = wedge[v])
ei += quadricError(attribute_quadrics[v], &attribute_gradients[v * attribute_count], attribute_count, vertex_positions[i1], &vertex_attributes[i1 * attribute_count]);
if (vertex_kind[i1] == Kind_Complex && bidi)
for (unsigned int v = wedge[i1]; v != i1; v = wedge[v])
ej += quadricError(attribute_quadrics[v], &attribute_gradients[v * attribute_count], attribute_count, vertex_positions[i0], &vertex_attributes[i0 * attribute_count]);
}
}
// pick edge direction with minimal error
c.v0 = ei <= ej ? i0 : j0;
c.v1 = ei <= ej ? i1 : j1;
c.error = ei <= ej ? ei : ej;
// pick edge direction with minimal error (branchless)
bool rev = bidi & (ej < ei);
c.v0 = rev ? i1 : i0;
c.v1 = rev ? i0 : i1;
c.error = ej < ei ? ej : ei;
#if TRACE >= 3
if (i0 == j0) // c.bidi has been overwritten
printf("edge eval %d -> %d: error %f (pos %f, attr %f)\n", c.v0, c.v1,
sqrtf(c.error), sqrtf(ei <= ej ? di : dj), sqrtf(ei <= ej ? ei - di : ej - dj));
if (bidi)
printf("edge eval %d -> %d: error %f (pos %f, attr %f); reverse %f (pos %f, attr %f)\n",
rev ? i1 : i0, rev ? i0 : i1,
sqrtf(rev ? ej : ei), sqrtf(rev ? dj : di), sqrtf(rev ? ej - dj : ei - di),
sqrtf(rev ? ei : ej), sqrtf(rev ? di : dj), sqrtf(rev ? ei - di : ej - dj));
else
printf("edge eval %d -> %d: error %f (pos %f, attr %f); reverse %f (pos %f, attr %f)\n", c.v0, c.v1,
sqrtf(ei <= ej ? ei : ej), sqrtf(ei <= ej ? di : dj), sqrtf(ei <= ej ? ei - di : ej - dj),
sqrtf(ei <= ej ? ej : ei), sqrtf(ei <= ej ? dj : di), sqrtf(ei <= ej ? ej - dj : ei - di));
printf("edge eval %d -> %d: error %f (pos %f, attr %f)\n", i0, i1, sqrtf(c.error), sqrtf(di), sqrtf(ei - di));
#endif
}
}
@ -1243,7 +1551,7 @@ static size_t performEdgeCollapses(unsigned int* collapse_remap, unsigned char*
// for seam collapses we need to move the seam pair together; this is a bit tricky since we need to rely on edge loops as target vertex may be locked (and thus have more than two wedges)
unsigned int s0 = wedge[i0];
unsigned int s1 = loop[i0] == i1 ? loopback[s0] : loop[s0];
assert(s0 != i0 && wedge[s0] == i0);
assert(wedge[s0] == i0); // s0 may be equal to i0 for half-seams
assert(s1 != ~0u && remap[s1] == r1);
// additional asserts to verify that the seam pair is consistent
@ -1289,7 +1597,7 @@ static size_t performEdgeCollapses(unsigned int* collapse_remap, unsigned char*
return edge_collapses;
}
static void updateQuadrics(const unsigned int* collapse_remap, size_t vertex_count, Quadric* vertex_quadrics, Quadric* attribute_quadrics, QuadricGrad* attribute_gradients, size_t attribute_count, const Vector3* vertex_positions, const unsigned int* remap, float& vertex_error)
static void updateQuadrics(const unsigned int* collapse_remap, size_t vertex_count, Quadric* vertex_quadrics, QuadricGrad* volume_gradients, Quadric* attribute_quadrics, QuadricGrad* attribute_gradients, size_t attribute_count, const Vector3* vertex_positions, const unsigned int* remap, float& vertex_error)
{
for (size_t i = 0; i < vertex_count; ++i)
{
@ -1304,8 +1612,13 @@ static void updateQuadrics(const unsigned int* collapse_remap, size_t vertex_cou
// ensure we only update vertex_quadrics once: primary vertex must be moved if any wedge is moved
if (i0 == r0)
{
quadricAdd(vertex_quadrics[r1], vertex_quadrics[r0]);
if (volume_gradients)
quadricAdd(volume_gradients[r1], volume_gradients[r0]);
}
if (attribute_count)
{
quadricAdd(attribute_quadrics[i1], attribute_quadrics[i0]);
@ -1321,7 +1634,116 @@ static void updateQuadrics(const unsigned int* collapse_remap, size_t vertex_cou
}
}
static size_t remapIndexBuffer(unsigned int* indices, size_t index_count, const unsigned int* collapse_remap)
static void solveQuadrics(Vector3* vertex_positions, float* vertex_attributes, size_t vertex_count, const Quadric* vertex_quadrics, const QuadricGrad* volume_gradients, const Quadric* attribute_quadrics, const QuadricGrad* attribute_gradients, size_t attribute_count, const unsigned int* remap, const unsigned int* wedge, const EdgeAdjacency& adjacency, const unsigned char* vertex_kind, const unsigned char* vertex_update)
{
#if TRACE
size_t stats[5] = {};
#endif
for (size_t i = 0; i < vertex_count; ++i)
{
if (!vertex_update[i])
continue;
// moving externally locked vertices is prohibited
// moving vertices on an attribute discontinuity may result in extrapolating UV outside of the chart bounds
// moving vertices on a border requires a stronger edge quadric to preserve the border geometry
if (vertex_kind[i] == Kind_Locked || vertex_kind[i] == Kind_Seam || vertex_kind[i] == Kind_Border)
continue;
if (remap[i] != i)
{
vertex_positions[i] = vertex_positions[remap[i]];
continue;
}
TRACESTATS(0);
const Vector3& vp = vertex_positions[i];
Quadric Q = vertex_quadrics[i];
QuadricGrad GV = {};
// add a point quadric for regularization to stabilize the solution
Quadric R;
quadricFromPoint(R, vp.x, vp.y, vp.z, Q.w * 1e-4f);
quadricAdd(Q, R);
if (attribute_count)
{
// optimal point simultaneously minimizes attribute quadrics for all wedges
unsigned int v = unsigned(i);
do
{
quadricReduceAttributes(Q, attribute_quadrics[v], &attribute_gradients[v * attribute_count], attribute_count);
v = wedge[v];
} while (v != i);
// minimizing attribute quadrics results in volume loss so we incorporate volume gradient as a constraint
if (volume_gradients)
GV = volume_gradients[i];
}
Vector3 p;
if (!quadricSolve(p, Q, GV))
{
TRACESTATS(2);
continue;
}
// reject updates that move the vertex too far from its neighborhood
// this detects and fixes most cases when the quadric is not well-defined
float nr = getNeighborhoodRadius(adjacency, vertex_positions, unsigned(i));
float dp = (p.x - vp.x) * (p.x - vp.x) + (p.y - vp.y) * (p.y - vp.y) + (p.z - vp.z) * (p.z - vp.z);
if (dp > nr * nr)
{
TRACESTATS(3);
continue;
}
// reject updates that would flip a neighboring triangle, as we do for edge collapse
if (hasTriangleFlips(adjacency, vertex_positions, unsigned(i), p))
{
TRACESTATS(4);
continue;
}
TRACESTATS(1);
vertex_positions[i] = p;
}
#if TRACE
printf("updated %d/%d positions; failed solve %d bounds %d flip %d\n", int(stats[1]), int(stats[0]), int(stats[2]), int(stats[3]), int(stats[4]));
#endif
if (attribute_count == 0)
return;
for (size_t i = 0; i < vertex_count; ++i)
{
if (!vertex_update[i])
continue;
// updating externally locked vertices is prohibited
if (vertex_kind[i] == Kind_Locked)
continue;
const Vector3& p = vertex_positions[remap[i]];
const Quadric& A = attribute_quadrics[i];
float iw = A.w == 0 ? 0.f : 1.f / A.w;
for (size_t k = 0; k < attribute_count; ++k)
{
const QuadricGrad& G = attribute_gradients[i * attribute_count + k];
vertex_attributes[i * attribute_count + k] = (G.gx * p.x + G.gy * p.y + G.gz * p.z + G.gw) * iw;
}
}
}
static size_t remapIndexBuffer(unsigned int* indices, size_t index_count, const unsigned int* collapse_remap, const unsigned int* remap)
{
size_t write = 0;
@ -1336,7 +1758,14 @@ static size_t remapIndexBuffer(unsigned int* indices, size_t index_count, const
assert(collapse_remap[v1] == v1);
assert(collapse_remap[v2] == v2);
if (v0 != v1 && v0 != v2 && v1 != v2)
// collapse zero area triangles even if they are not topologically degenerate
// this is required to cleanup manifold->seam collapses when a vertex is collapsed onto a seam pair
// as well as complex collapses and some other cases where cross wedge collapses are performed
unsigned int r0 = remap[v0];
unsigned int r1 = remap[v1];
unsigned int r2 = remap[v2];
if (r0 != r1 && r0 != r2 && r1 != r2)
{
indices[write + 0] = v0;
indices[write + 1] = v1;
@ -1494,18 +1923,24 @@ static void measureComponents(float* component_errors, size_t component_count, c
static size_t pruneComponents(unsigned int* indices, size_t index_count, const unsigned int* components, const float* component_errors, size_t component_count, float error_cutoff, float& nexterror)
{
(void)component_count;
size_t write = 0;
float min_error = FLT_MAX;
for (size_t i = 0; i < index_count; i += 3)
{
unsigned int c = components[indices[i]];
assert(c == components[indices[i + 1]] && c == components[indices[i + 2]]);
unsigned int v0 = indices[i + 0], v1 = indices[i + 1], v2 = indices[i + 2];
unsigned int c = components[v0];
assert(c == components[v1] && c == components[v2]);
if (component_errors[c] > error_cutoff)
{
indices[write + 0] = indices[i + 0];
indices[write + 1] = indices[i + 1];
indices[write + 2] = indices[i + 2];
min_error = min_error > component_errors[c] ? component_errors[c] : min_error;
indices[write + 0] = v0;
indices[write + 1] = v1;
indices[write + 2] = v2;
write += 3;
}
}
@ -1515,15 +1950,11 @@ static size_t pruneComponents(unsigned int* indices, size_t index_count, const u
for (size_t i = 0; i < component_count; ++i)
pruned_components += (component_errors[i] >= nexterror && component_errors[i] <= error_cutoff);
printf("pruned %d triangles in %d components (goal %e)\n", int((index_count - write) / 3), int(pruned_components), sqrtf(error_cutoff));
printf("pruned %d triangles in %d components (goal %e); next %e\n", int((index_count - write) / 3), int(pruned_components), sqrtf(error_cutoff), min_error < FLT_MAX ? sqrtf(min_error) : min_error * 2);
#endif
// update next error with the smallest error of the remaining components for future pruning
nexterror = FLT_MAX;
for (size_t i = 0; i < component_count; ++i)
if (component_errors[i] > error_cutoff)
nexterror = nexterror > component_errors[i] ? component_errors[i] : nexterror;
// update next error with the smallest error of the remaining components
nexterror = min_error;
return write;
}
@ -1588,7 +2019,7 @@ struct TriangleHasher
}
};
static void computeVertexIds(unsigned int* vertex_ids, const Vector3* vertex_positions, size_t vertex_count, int grid_size)
static void computeVertexIds(unsigned int* vertex_ids, const Vector3* vertex_positions, const unsigned char* vertex_lock, size_t vertex_count, int grid_size)
{
assert(grid_size >= 1 && grid_size <= 1024);
float cell_scale = float(grid_size - 1);
@ -1601,7 +2032,10 @@ static void computeVertexIds(unsigned int* vertex_ids, const Vector3* vertex_pos
int yi = int(v.y * cell_scale + 0.5f);
int zi = int(v.z * cell_scale + 0.5f);
vertex_ids[i] = (xi << 20) | (yi << 10) | zi;
if (vertex_lock && (vertex_lock[i] & meshopt_SimplifyVertex_Lock))
vertex_ids[i] = (1 << 30) | unsigned(i);
else
vertex_ids[i] = (xi << 20) | (yi << 10) | zi;
}
}
@ -1835,9 +2269,10 @@ static float interpolate(float y, float x0, float y0, float x1, float y1, float
} // namespace meshopt
// Note: this is only exposed for debug visualization purposes; do *not* use
// Note: this is only exposed for development purposes; do *not* use
enum
{
meshopt_SimplifyInternalSolve = 1 << 29,
meshopt_SimplifyInternalDebug = 1 << 30
};
@ -1850,7 +2285,7 @@ size_t meshopt_simplifyEdge(unsigned int* destination, const unsigned int* indic
assert(vertex_positions_stride % sizeof(float) == 0);
assert(target_index_count <= index_count);
assert(target_error >= 0);
assert((options & ~(meshopt_SimplifyLockBorder | meshopt_SimplifySparse | meshopt_SimplifyErrorAbsolute | meshopt_SimplifyPrune | meshopt_SimplifyInternalDebug)) == 0);
assert((options & ~(meshopt_SimplifyLockBorder | meshopt_SimplifySparse | meshopt_SimplifyErrorAbsolute | meshopt_SimplifyPrune | meshopt_SimplifyRegularize | meshopt_SimplifyPermissive | meshopt_SimplifyInternalSolve | meshopt_SimplifyInternalDebug)) == 0);
assert(vertex_attributes_stride >= attribute_count * sizeof(float) && vertex_attributes_stride <= 256);
assert(vertex_attributes_stride % sizeof(float) == 0);
assert(attribute_count <= kMaxAttributes);
@ -1902,14 +2337,14 @@ size_t meshopt_simplifyEdge(unsigned int* destination, const unsigned int* indic
#endif
Vector3* vertex_positions = allocator.allocate<Vector3>(vertex_count);
float vertex_scale = rescalePositions(vertex_positions, vertex_positions_data, vertex_count, vertex_positions_stride, sparse_remap);
float vertex_offset[3] = {};
float vertex_scale = rescalePositions(vertex_positions, vertex_positions_data, vertex_count, vertex_positions_stride, sparse_remap, vertex_offset);
float* vertex_attributes = NULL;
unsigned int attribute_remap[kMaxAttributes];
if (attribute_count)
{
unsigned int attribute_remap[kMaxAttributes];
// remap attributes to only include ones with weight > 0 to minimize memory/compute overhead for quadrics
size_t attributes_used = 0;
for (size_t i = 0; i < attribute_count; ++i)
@ -1926,6 +2361,7 @@ size_t meshopt_simplifyEdge(unsigned int* destination, const unsigned int* indic
Quadric* attribute_quadrics = NULL;
QuadricGrad* attribute_gradients = NULL;
QuadricGrad* volume_gradients = NULL;
if (attribute_count)
{
@ -1934,9 +2370,16 @@ size_t meshopt_simplifyEdge(unsigned int* destination, const unsigned int* indic
attribute_gradients = allocator.allocate<QuadricGrad>(vertex_count * attribute_count);
memset(attribute_gradients, 0, vertex_count * attribute_count * sizeof(QuadricGrad));
if (options & meshopt_SimplifyInternalSolve)
{
volume_gradients = allocator.allocate<QuadricGrad>(vertex_count);
memset(volume_gradients, 0, vertex_count * sizeof(QuadricGrad));
}
}
fillFaceQuadrics(vertex_quadrics, result, index_count, vertex_positions, remap);
fillFaceQuadrics(vertex_quadrics, volume_gradients, result, index_count, vertex_positions, remap);
fillVertexQuadrics(vertex_quadrics, vertex_positions, vertex_count, remap, options);
fillEdgeQuadrics(vertex_quadrics, result, index_count, vertex_positions, remap, vertex_kind, loop, loopback);
if (attribute_count)
@ -2016,23 +2459,26 @@ size_t meshopt_simplifyEdge(unsigned int* destination, const unsigned int* indic
if (collapses == 0)
break;
updateQuadrics(collapse_remap, vertex_count, vertex_quadrics, attribute_quadrics, attribute_gradients, attribute_count, vertex_positions, remap, vertex_error);
updateQuadrics(collapse_remap, vertex_count, vertex_quadrics, volume_gradients, attribute_quadrics, attribute_gradients, attribute_count, vertex_positions, remap, vertex_error);
// updateQuadrics will update vertex error if we use attributes, but if we don't then result_error and vertex_error are equivalent
vertex_error = attribute_count == 0 ? result_error : vertex_error;
// note: we update loops following edge collapses, but after this we might still have stale loop data
// this can happen when a triangle with a loop edge gets collapsed along a non-loop edge
// that works since a loop that points to a vertex that is no longer connected is not affecting collapse logic
remapEdgeLoops(loop, vertex_count, collapse_remap);
remapEdgeLoops(loopback, vertex_count, collapse_remap);
size_t new_count = remapIndexBuffer(result, result_count, collapse_remap);
assert(new_count < result_count);
result_count = new_count;
result_count = remapIndexBuffer(result, result_count, collapse_remap, remap);
if ((options & meshopt_SimplifyPrune) && result_count > target_index_count && component_nexterror <= vertex_error)
result_count = pruneComponents(result, result_count, components, component_errors, component_count, vertex_error, component_nexterror);
}
// at this point, component_nexterror might be stale: component it references may have been removed through a series of edge collapses
bool component_nextstale = true;
// we're done with the regular simplification but we're still short of the target; try pruning more aggressively towards error_limit
while ((options & meshopt_SimplifyPrune) && result_count > target_index_count && component_nexterror <= error_limit)
{
@ -2049,18 +2495,42 @@ size_t meshopt_simplifyEdge(unsigned int* destination, const unsigned int* indic
component_maxerror = component_errors[i];
size_t new_count = pruneComponents(result, result_count, components, component_errors, component_count, component_cutoff, component_nexterror);
if (new_count == result_count)
if (new_count == result_count && !component_nextstale)
break;
component_nextstale = false; // pruneComponents guarantees next error is up to date
result_count = new_count;
result_error = result_error < component_maxerror ? component_maxerror : result_error;
vertex_error = vertex_error < component_maxerror ? component_maxerror : vertex_error;
}
#if TRACE
printf("result: %d triangles, error: %e; total %d passes\n", int(result_count / 3), sqrtf(result_error), int(pass_count));
printf("result: %d triangles, error: %e (pos %.3e); total %d passes\n", int(result_count / 3), sqrtf(result_error), sqrtf(vertex_error), int(pass_count));
#endif
// if solve is requested, update input buffers destructively from internal data
if (options & meshopt_SimplifyInternalSolve)
{
unsigned char* vertex_update = collapse_locked; // reuse as scratch space
memset(vertex_update, 0, vertex_count);
// limit quadric solve to vertices that are still used in the result
for (size_t i = 0; i < result_count; ++i)
{
unsigned int v = result[i];
// recomputing externally locked vertices may result in floating point drift
vertex_update[v] = vertex_kind[v] != Kind_Locked;
}
// edge adjacency may be stale as we haven't updated it after last series of edge collapses
updateEdgeAdjacency(adjacency, result, result_count, vertex_count, remap);
solveQuadrics(vertex_positions, vertex_attributes, vertex_count, vertex_quadrics, volume_gradients, attribute_quadrics, attribute_gradients, attribute_count, remap, wedge, adjacency, vertex_kind, vertex_update);
finalizeVertices(const_cast<float*>(vertex_positions_data), vertex_positions_stride, const_cast<float*>(vertex_attributes_data), vertex_attributes_stride, attribute_weights, attribute_count, vertex_count, vertex_positions, vertex_attributes, sparse_remap, attribute_remap, vertex_scale, vertex_offset, vertex_update);
}
// if debug visualization data is requested, fill it instead of index data; for simplicity, this doesn't work with sparsity
if ((options & meshopt_SimplifyInternalDebug) && !sparse_remap)
{
@ -2090,15 +2560,24 @@ size_t meshopt_simplifyEdge(unsigned int* destination, const unsigned int* indic
size_t meshopt_simplify(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, size_t target_index_count, float target_error, unsigned int options, float* out_result_error)
{
assert((options & meshopt_SimplifyInternalSolve) == 0); // use meshopt_simplifyWithUpdate instead
return meshopt_simplifyEdge(destination, indices, index_count, vertex_positions_data, vertex_count, vertex_positions_stride, NULL, 0, NULL, 0, NULL, target_index_count, target_error, options, out_result_error);
}
size_t meshopt_simplifyWithAttributes(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, const float* vertex_attributes_data, size_t vertex_attributes_stride, const float* attribute_weights, size_t attribute_count, const unsigned char* vertex_lock, size_t target_index_count, float target_error, unsigned int options, float* out_result_error)
{
assert((options & meshopt_SimplifyInternalSolve) == 0); // use meshopt_simplifyWithUpdate instead
return meshopt_simplifyEdge(destination, indices, index_count, vertex_positions_data, vertex_count, vertex_positions_stride, vertex_attributes_data, vertex_attributes_stride, attribute_weights, attribute_count, vertex_lock, target_index_count, target_error, options, out_result_error);
}
size_t meshopt_simplifySloppy(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, size_t target_index_count, float target_error, float* out_result_error)
size_t meshopt_simplifyWithUpdate(unsigned int* indices, size_t index_count, float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, float* vertex_attributes_data, size_t vertex_attributes_stride, const float* attribute_weights, size_t attribute_count, const unsigned char* vertex_lock, size_t target_index_count, float target_error, unsigned int options, float* out_result_error)
{
return meshopt_simplifyEdge(indices, indices, index_count, vertex_positions_data, vertex_count, vertex_positions_stride, vertex_attributes_data, vertex_attributes_stride, attribute_weights, attribute_count, vertex_lock, target_index_count, target_error, options | meshopt_SimplifyInternalSolve, out_result_error);
}
size_t meshopt_simplifySloppy(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, const unsigned char* vertex_lock, size_t target_index_count, float target_error, float* out_result_error)
{
using namespace meshopt;
@ -2126,15 +2605,15 @@ size_t meshopt_simplifySloppy(unsigned int* destination, const unsigned int* ind
const int kInterpolationPasses = 5;
// invariant: # of triangles in min_grid <= target_count
int min_grid = int(1.f / (target_error < 1e-3f ? 1e-3f : target_error));
int min_grid = int(1.f / (target_error < 1e-3f ? 1e-3f : (target_error < 1.f ? target_error : 1.f)));
int max_grid = 1025;
size_t min_triangles = 0;
size_t max_triangles = index_count / 3;
// when we're error-limited, we compute the triangle count for the min. size; this accelerates convergence and provides the correct answer when we can't use a larger grid
if (min_grid > 1)
if (min_grid > 1 || vertex_lock)
{
computeVertexIds(vertex_ids, vertex_positions, vertex_count, min_grid);
computeVertexIds(vertex_ids, vertex_positions, vertex_lock, vertex_count, min_grid);
min_triangles = countTriangles(vertex_ids, indices, index_count);
}
@ -2150,7 +2629,7 @@ size_t meshopt_simplifySloppy(unsigned int* destination, const unsigned int* ind
int grid_size = next_grid_size;
grid_size = (grid_size <= min_grid) ? min_grid + 1 : (grid_size >= max_grid ? max_grid - 1 : grid_size);
computeVertexIds(vertex_ids, vertex_positions, vertex_count, grid_size);
computeVertexIds(vertex_ids, vertex_positions, vertex_lock, vertex_count, grid_size);
size_t triangles = countTriangles(vertex_ids, indices, index_count);
#if TRACE
@ -2192,7 +2671,7 @@ size_t meshopt_simplifySloppy(unsigned int* destination, const unsigned int* ind
unsigned int* vertex_cells = allocator.allocate<unsigned int>(vertex_count);
computeVertexIds(vertex_ids, vertex_positions, vertex_count, min_grid);
computeVertexIds(vertex_ids, vertex_positions, vertex_lock, vertex_count, min_grid);
size_t cell_count = fillVertexCells(table, table_size, vertex_cells, vertex_ids, vertex_count);
// build a quadric for each target cell
@ -2213,15 +2692,15 @@ size_t meshopt_simplifySloppy(unsigned int* destination, const unsigned int* ind
for (size_t i = 0; i < cell_count; ++i)
result_error = result_error < cell_errors[i] ? cell_errors[i] : result_error;
// collapse triangles!
// note that we need to filter out triangles that we've already output because we very frequently generate redundant triangles between cells :(
// vertex collapses often result in duplicate triangles; we need a table to filter them out
size_t tritable_size = hashBuckets2(min_triangles);
unsigned int* tritable = allocator.allocate<unsigned int>(tritable_size);
// note: this is the first and last write to destination, which allows aliasing destination with indices
size_t write = filterTriangles(destination, tritable, tritable_size, indices, index_count, vertex_cells, cell_remap);
#if TRACE
printf("result: %d cells, %d triangles (%d unfiltered), error %e\n", int(cell_count), int(write / 3), int(min_triangles), sqrtf(result_error));
printf("result: grid size %d, %d cells, %d triangles (%d unfiltered), error %e\n", min_grid, int(cell_count), int(write / 3), int(min_triangles), sqrtf(result_error));
#endif
if (out_result_error)
@ -2316,7 +2795,7 @@ size_t meshopt_simplifyPoints(unsigned int* destination, const float* vertex_pos
int grid_size = next_grid_size;
grid_size = (grid_size <= min_grid) ? min_grid + 1 : (grid_size >= max_grid ? max_grid - 1 : grid_size);
computeVertexIds(vertex_ids, vertex_positions, vertex_count, grid_size);
computeVertexIds(vertex_ids, vertex_positions, NULL, vertex_count, grid_size);
size_t vertices = countVertexCells(table, table_size, vertex_ids, vertex_count);
#if TRACE
@ -2353,7 +2832,7 @@ size_t meshopt_simplifyPoints(unsigned int* destination, const float* vertex_pos
// build vertex->cell association by mapping all vertices with the same quantized position to the same cell
unsigned int* vertex_cells = allocator.allocate<unsigned int>(vertex_count);
computeVertexIds(vertex_ids, vertex_positions, vertex_count, min_grid);
computeVertexIds(vertex_ids, vertex_positions, NULL, vertex_count, min_grid);
size_t cell_count = fillVertexCells(table, table_size, vertex_cells, vertex_ids, vertex_count);
// accumulate points into a reservoir for each target cell