12#include <unordered_map>
15# include <tbb/parallel_for.h>
29 std::array<uint, 3> neighbor;
31 std::array<uint, 3> group;
33 std::array<uint, 3> vertices;
45 std::array<uint8_t, 3> faceVertex;
48 bool markDegenerate : 1;
49 bool quadOneDegenTri : 1;
50 bool groupWithAny : 1;
51 bool orientPreserving : 1;
53 Triangle(
uint faceIdx_,
uint tSpaceIdx_)
56 tSpaceIdx{tSpaceIdx_},
57 markDegenerate{
false},
58 quadOneDegenTri{
false},
60 orientPreserving{
false}
66 void setVertices(uint8_t i0, uint8_t i1, uint8_t i2)
79 uint vertexRepresentative;
80 bool orientPreserving;
82 Group(
uint vertexRepresentative_,
bool orientPreserving_)
84 vertexRepresentative{vertexRepresentative_},
85 orientPreserving{orientPreserving_}
89 void normalizeTSpace()
94 void accumulateTSpaceAtomic(
float3 v_tangent)
101 void accumulateTSpace(
float3 v_tangent)
103 tangent += v_tangent;
110 bool orientPreserving =
false;
112 void accumulateGroup(
const Group &group)
117 tangent = group.tangent;
119 else if (tangent == group.tangent) {
125 tangent = (tangent + group.tangent).
normalize();
129 orientPreserving = group.orientPreserving;
135 std::vector<Triangle> triangles;
136 std::vector<TSpace> tSpaces;
137 std::vector<Group> groups;
139 uint nrTSpaces, nrFaces, nrTriangles, totalTriangles;
149 nrFaces =
uint(mesh.GetNumFaces());
152 nrThreads = tbb::this_task_arena::max_concurrency();
153 isParallel = (nrThreads > 1) && (nrFaces > 10000);
162 if (nrTriangles == 0) {
176 if (nrTriangles == 0) {
178 tSpaces.resize(nrTSpaces);
202 for (
uint f = 0; f < nrFaces; f++) {
203 const uint verts = mesh.GetNumVerticesOfFace(f);
210 const TSpace &tSpace = tSpaces[index++];
211 mesh.SetTangentSpace(f,
i, tSpace.tangent, tSpace.orientPreserving);
221 tbb::parallel_for(start, end, func);
226 for (
uint i = start;
i < end;
i++) {
239 return mesh.GetPosition(f,
v);
246 return mesh.GetNormal(f,
v);
253 return mesh.GetTexCoord(f,
v);
258 return mesh.has_uv();
267 for (
uint f = 0; f < nrFaces; f++) {
268 const uint verts = mesh.GetNumVerticesOfFace(f);
272 else if (
verts == 4) {
277 triangles.reserve(nrTriangles);
280 for (
uint f = 0; f < nrFaces; f++) {
281 const uint verts = mesh.GetNumVerticesOfFace(f);
287 triangles.emplace_back(f, nrTSpaces);
288 Triangle &triA = triangles[tA];
291 triA.setVertices(0, 1, 2);
295 triangles.emplace_back(f, nrTSpaces);
296 Triangle &triB = triangles[tB];
301 float distSQ_02 = (mesh.GetTexCoord(f, 2) - mesh.GetTexCoord(f, 0)).length_squared();
302 float distSQ_13 = (mesh.GetTexCoord(f, 3) - mesh.GetTexCoord(f, 1)).length_squared();
304 if (distSQ_02 != distSQ_13) {
305 quadDiagIs_02 = (distSQ_02 < distSQ_13);
308 distSQ_02 = (mesh.GetPosition(f, 2) - mesh.GetPosition(f, 0)).length_squared();
309 distSQ_13 = (mesh.GetPosition(f, 3) - mesh.GetPosition(f, 1)).length_squared();
310 quadDiagIs_02 = !(distSQ_13 < distSQ_02);
314 triA.setVertices(0, 1, 2);
315 triB.setVertices(0, 2, 3);
318 triA.setVertices(0, 1, 3);
319 triB.setVertices(1, 2, 3);
339 return mikk->getTexCoord(kA) ==
mikk->getTexCoord(kB) &&
340 mikk->getNormal(kA) ==
mikk->getNormal(kB) &&
341 mikk->getPosition(kA) ==
mikk->getPosition(kB);
357 auto res = set.emplace(triangles[t].vertices[
i]);
359 triangles[t].vertices[
i] = res.first;
380 totalTriangles = nrTriangles;
381 std::atomic<uint> degenTriangles(0);
386 if (p0 == p1 || p0 == p2 || p1 == p2)
388 triangles[t].markDegenerate =
true;
389 degenTriangles.fetch_add(1);
392 nrTriangles -= degenTriangles.load();
394 if (totalTriangles == nrTriangles) {
400 Triangle &triangleA = triangles[t], &triangleB = triangles[t + 1];
401 if (triangleA.faceIdx != triangleB.faceIdx) {
405 if (triangleA.markDegenerate != triangleB.markDegenerate) {
406 triangleA.quadOneDegenTri =
true;
407 triangleB.quadOneDegenTri =
true;
411 std::stable_partition(triangles.begin(), triangles.end(), [](
const Triangle &tri) {
412 return !tri.markDegenerate;
418 if (nrTriangles == totalTriangles) {
422 std::unordered_map<uint, uint> goodTriangleMap;
423 for (
uint t = 0; t < nrTriangles; t++) {
425 goodTriangleMap.emplace(triangles[t].vertices[
i],
pack_index(t,
i));
431 for (
uint t = nrTriangles; t < totalTriangles; t++) {
434 if (triangles[t].quadOneDegenTri) {
439 const auto entry = goodTriangleMap.find(triangles[t].vertices[
i]);
440 if (entry == goodTriangleMap.end()) {
447 const uint iSrcVert = triangles[tSrc].faceVertex[iSrc];
448 const uint iSrcOffs = triangles[tSrc].tSpaceIdx;
449 const uint iDstVert = triangles[t].faceVertex[
i];
450 const uint iDstOffs = triangles[t].tSpaceIdx;
452 tSpaces[iDstOffs + iDstVert] = tSpaces[iSrcOffs + iSrcVert];
457 for (
uint t = 0; t < nrTriangles; t++) {
460 if (!triangles[t].quadOneDegenTri) {
463 uint vertFlag = (1u << triangles[t].faceVertex[0]) | (1u << triangles[t].faceVertex[1]) |
464 (1u << triangles[t].faceVertex[2]);
465 uint missingFaceVertex = 0;
466 if ((vertFlag & 2) == 0) {
467 missingFaceVertex = 1;
469 else if ((vertFlag & 4) == 0) {
470 missingFaceVertex = 2;
472 else if ((vertFlag & 8) == 0) {
473 missingFaceVertex = 3;
476 uint faceIdx = triangles[t].faceIdx;
477 float3 dstP = mesh.GetPosition(faceIdx, missingFaceVertex);
480 const uint faceVertex = triangles[t].faceVertex[
i];
481 const float3 srcP = mesh.GetPosition(faceIdx, faceVertex);
483 const uint offset = triangles[t].tSpaceIdx;
484 tSpaces[offset + missingFaceVertex] = tSpaces[offset + faceVertex];
504 const float t21x = t2.
x - t1.
x;
505 const float t21y = t2.
y - t1.
y;
506 const float t31x = t3.
x - t1.
x;
507 const float t31y = t3.
y - t1.
y;
509 const float signedAreaSTx2 = t21x * t31y - t21y * t31x;
510 return fabsf(signedAreaSTx2);
520 Triangle &triangle = triangles[t];
530 float t21x = t2.
x - t1.
x;
531 float t31x = t3.
x - t1.
x;
535 t21x -=
floorf(t21x + 0.5f);
536 t31x -=
floorf(t31x + 0.5f);
539 const float t21y = t2.
y - t1.
y;
540 const float t31y = t3.
y - t1.
y;
541 const float3 d1 =
v2 - v1, d2 = v3 - v1;
543 const float signedAreaSTx2 = t21x * t31y - t21y * t31x;
544 const float3 vOs = (t31y * d1) - (t21y * d2);
545 const float3 vOt = (-t31x * d1) + (t21x * d2);
547 triangle.orientPreserving = (signedAreaSTx2 > 0);
552 const float fS = triangle.orientPreserving ? 1.0f : (-1.0f);
554 triangle.tangent = vOs * (fS /
sqrtf(lenOs2));
559 triangle.groupWithAny =
false;
566 Triangle &triangleA = triangles[t], &triangleB = triangles[t + 1];
567 if (triangleA.faceIdx != triangleB.faceIdx) {
574 if (!(triangleA.markDegenerate || triangleB.markDegenerate)) {
576 if (triangleA.orientPreserving != triangleB.orientPreserving) {
577 bool chooseOrientFirstTri =
false;
578 if (triangleB.groupWithAny) {
579 chooseOrientFirstTri =
true;
582 chooseOrientFirstTri =
true;
586 const uint t0 = chooseOrientFirstTri ? t : (t + 1);
587 const uint t1 = chooseOrientFirstTri ? (t + 1) : t;
588 triangles[t1].orientPreserving = triangles[t0].orientPreserving;
614 std::vector<Entry> tempEntries(
entries.size(), {0, 0});
622 Mikktspace<Mesh>::Triangle &triA =
mikk->triangles[tA];
628 uint i0A = triA.vertices[iA], i1A = triA.vertices[(iA != 2) ? (iA + 1) : 0];
633 Mikktspace<Mesh>::Triangle &triB =
mikk->triangles[tB];
635 if (
b.key != a.
key) {
643 uint i1B = triB.vertices[iB], i0B = triB.vertices[(iB != 2) ? (iB + 1) : 0];
644 if (i0A == i0B && i1A == i1B) {
645 triA.neighbor[iA] = tB;
646 triB.neighbor[iB] = tA;
663 uint targetNrShards = isParallel ?
uint(4 * nrThreads) : 1;
664 uint nrShards = 1, hashShift = 32;
665 while (nrShards < targetNrShards) {
671 size_t reserveSize = size_t(
double(3 * nrTriangles) * 1.25 / nrShards);
672 std::vector<NeighborShard> shards(nrShards, {reserveSize});
674 for (
uint t = 0; t < nrTriangles; t++) {
675 Triangle &triangle = triangles[t];
677 const uint i0 = triangle.vertices[
i];
678 const uint i1 = triangle.vertices[(
i != 2) ? (
i + 1) : 0];
679 const uint high = std::max(i0, i1), low = std::min(i0, i1);
683 const uint shard = isParallel ? (
hash >> hashShift) : 0;
688 runParallel(0u, nrShards, [&](
uint s) { shards[s].buildNeighbors(
this); });
700 Triangle &triangle = triangles[t];
701 Group &group = groups[groupId];
704 const uint vertRep = group.vertexRepresentative;
706 if (triangle.vertices[0] == vertRep) {
709 else if (triangle.vertices[1] == vertRep) {
712 else if (triangle.vertices[2] == vertRep) {
722 if (triangle.groupWithAny) {
729 triangle.orientPreserving = group.orientPreserving;
733 if (triangle.orientPreserving != group.orientPreserving) {
737 triangle.group[
i] = groupId;
739 const uint t_L = triangle.neighbor[
i];
740 const uint t_R = triangle.neighbor[
i > 0 ? (
i - 1) : 2];
752 for (
uint t = 0; t < nrTriangles; t++) {
753 Triangle &triangle = triangles[t];
756 if (triangle.groupWithAny || triangle.group[
i] !=
UNSET_ENTRY) {
760 const uint newGroupId =
uint(groups.size());
761 triangle.group[
i] = newGroupId;
763 groups.emplace_back(triangle.vertices[
i],
bool(triangle.orientPreserving));
765 const uint t_L = triangle.neighbor[
i];
766 const uint t_R = triangle.neighbor[
i > 0 ? (
i - 1) : 2];
778 const Triangle &triangle = triangles[t];
780 if (triangle.groupWithAny) {
787 std::array<float3, 3> n, p;
793 std::array<float, 3> fCos = {
dot(
project(n[0], p[1] - p[0]),
project(n[0], p[2] - p[0])),
798 uint groupId = triangle.group[
i];
802 if constexpr (atomic) {
803 groups[groupId].accumulateTSpaceAtomic(tangent);
806 groups[groupId].accumulateTSpace(tangent);
818 for (
uint t = 0; t < nrTriangles; t++) {
824 for (Group &group : groups) {
825 group.normalizeTSpace();
828 tSpaces.resize(nrTSpaces);
830 for (
uint t = 0; t < nrTriangles; t++) {
831 Triangle &triangle = triangles[t];
833 uint groupId = triangle.group[
i];
837 const Group group = groups[groupId];
838 assert(triangle.orientPreserving == group.orientPreserving);
841 const uint offset = triangle.tSpaceIdx;
842 const uint faceVertex = triangle.faceVertex[
i];
843 tSpaces[offset + faceVertex].accumulateGroup(group);
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ATTR_WARN_UNUSED_RESULT const BMVert * v
void assignRecur(const uint t, uint groupId)
void generateSharedVerticesIndexList_impl()
float3 getPosition(uint vertexID)
void generateSharedVerticesIndexList()
void runParallel(uint start, uint end, F func)
void generateInitialVerticesIndexList()
float3 getNormal(uint vertexID)
float calcTexArea(uint tri)
float3 getTexCoord(uint vertexID)
void accumulateTSpaces(uint t)
#define assert(assertion)
VecBase< float, D > normalize(VecOp< float, D >) RET
VecBase< float, 3 > float3
static uint hash_uint3(uint kx, uint ky, uint kz)
float dot(const float3 &a, const float3 &b)
static uint pack_index(const uint face, const uint vert)
bool not_zero(const float fX)
float3 project(const float3 &n, const float3 &v)
static void unpack_index(uint &face, uint &vert, const uint indexIn)
static uint hash_float3x3(const float3 &x, const float3 &y, const float3 &z)
static void float_add_atomic(float *val, float add)
void radixsort(std::vector< T > &data, std::vector< T > &data2, KeyGetter getKey)
float fast_acosf(float x)
static constexpr uint UNSET_ENTRY
Entry(uint32_t key_, uint data_)
std::vector< Entry > entries
NeighborShard(size_t capacity)
void buildNeighbors(Mikktspace< Mesh > *mikk)
bool operator()(const uint &kA, const uint &kB) const
uint operator()(const uint &k) const
float length_squared() const