simplifier.cpp 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333
  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 <math.h>
  6. #include <string.h>
  7. #ifndef TRACE
  8. #define TRACE 0
  9. #endif
  10. #if TRACE
  11. #include <stdio.h>
  12. #endif
  13. #if TRACE
  14. #define TRACESTATS(i) stats[i]++;
  15. #else
  16. #define TRACESTATS(i) (void)0
  17. #endif
  18. // This work is based on:
  19. // Michael Garland and Paul S. Heckbert. Surface simplification using quadric error metrics. 1997
  20. // Michael Garland. Quadric-based polygonal surface simplification. 1999
  21. // Peter Lindstrom. Out-of-Core Simplification of Large Polygonal Models. 2000
  22. // Matthias Teschner, Bruno Heidelberger, Matthias Mueller, Danat Pomeranets, Markus Gross. Optimized Spatial Hashing for Collision Detection of Deformable Objects. 2003
  23. // Peter Van Sandt, Yannis Chronis, Jignesh M. Patel. Efficiently Searching In-Memory Sorted Arrays: Revenge of the Interpolation Search? 2019
  24. // Hugues Hoppe. New Quadric Metric for Simplifying Meshes with Appearance Attributes. 1999
  25. namespace meshopt
  26. {
  27. struct EdgeAdjacency
  28. {
  29. struct Edge
  30. {
  31. unsigned int next;
  32. unsigned int prev;
  33. };
  34. unsigned int* offsets;
  35. Edge* data;
  36. };
  37. static void prepareEdgeAdjacency(EdgeAdjacency& adjacency, size_t index_count, size_t vertex_count, meshopt_Allocator& allocator)
  38. {
  39. adjacency.offsets = allocator.allocate<unsigned int>(vertex_count + 1);
  40. adjacency.data = allocator.allocate<EdgeAdjacency::Edge>(index_count);
  41. }
  42. static void updateEdgeAdjacency(EdgeAdjacency& adjacency, const unsigned int* indices, size_t index_count, size_t vertex_count, const unsigned int* remap)
  43. {
  44. size_t face_count = index_count / 3;
  45. unsigned int* offsets = adjacency.offsets + 1;
  46. EdgeAdjacency::Edge* data = adjacency.data;
  47. // fill edge counts
  48. memset(offsets, 0, vertex_count * sizeof(unsigned int));
  49. for (size_t i = 0; i < index_count; ++i)
  50. {
  51. unsigned int v = remap ? remap[indices[i]] : indices[i];
  52. assert(v < vertex_count);
  53. offsets[v]++;
  54. }
  55. // fill offset table
  56. unsigned int offset = 0;
  57. for (size_t i = 0; i < vertex_count; ++i)
  58. {
  59. unsigned int count = offsets[i];
  60. offsets[i] = offset;
  61. offset += count;
  62. }
  63. assert(offset == index_count);
  64. // fill edge data
  65. for (size_t i = 0; i < face_count; ++i)
  66. {
  67. unsigned int a = indices[i * 3 + 0], b = indices[i * 3 + 1], c = indices[i * 3 + 2];
  68. if (remap)
  69. {
  70. a = remap[a];
  71. b = remap[b];
  72. c = remap[c];
  73. }
  74. data[offsets[a]].next = b;
  75. data[offsets[a]].prev = c;
  76. offsets[a]++;
  77. data[offsets[b]].next = c;
  78. data[offsets[b]].prev = a;
  79. offsets[b]++;
  80. data[offsets[c]].next = a;
  81. data[offsets[c]].prev = b;
  82. offsets[c]++;
  83. }
  84. // finalize offsets
  85. adjacency.offsets[0] = 0;
  86. assert(adjacency.offsets[vertex_count] == index_count);
  87. }
  88. struct PositionHasher
  89. {
  90. const float* vertex_positions;
  91. size_t vertex_stride_float;
  92. const unsigned int* sparse_remap;
  93. size_t hash(unsigned int index) const
  94. {
  95. unsigned int ri = sparse_remap ? sparse_remap[index] : index;
  96. const unsigned int* key = reinterpret_cast<const unsigned int*>(vertex_positions + ri * vertex_stride_float);
  97. // scramble bits to make sure that integer coordinates have entropy in lower bits
  98. unsigned int x = key[0] ^ (key[0] >> 17);
  99. unsigned int y = key[1] ^ (key[1] >> 17);
  100. unsigned int z = key[2] ^ (key[2] >> 17);
  101. // Optimized Spatial Hashing for Collision Detection of Deformable Objects
  102. return (x * 73856093) ^ (y * 19349663) ^ (z * 83492791);
  103. }
  104. bool equal(unsigned int lhs, unsigned int rhs) const
  105. {
  106. unsigned int li = sparse_remap ? sparse_remap[lhs] : lhs;
  107. unsigned int ri = sparse_remap ? sparse_remap[rhs] : rhs;
  108. return memcmp(vertex_positions + li * vertex_stride_float, vertex_positions + ri * vertex_stride_float, sizeof(float) * 3) == 0;
  109. }
  110. };
  111. struct RemapHasher
  112. {
  113. unsigned int* remap;
  114. size_t hash(unsigned int id) const
  115. {
  116. return id * 0x5bd1e995;
  117. }
  118. bool equal(unsigned int lhs, unsigned int rhs) const
  119. {
  120. return remap[lhs] == rhs;
  121. }
  122. };
  123. static size_t hashBuckets2(size_t count)
  124. {
  125. size_t buckets = 1;
  126. while (buckets < count + count / 4)
  127. buckets *= 2;
  128. return buckets;
  129. }
  130. template <typename T, typename Hash>
  131. static T* hashLookup2(T* table, size_t buckets, const Hash& hash, const T& key, const T& empty)
  132. {
  133. assert(buckets > 0);
  134. assert((buckets & (buckets - 1)) == 0);
  135. size_t hashmod = buckets - 1;
  136. size_t bucket = hash.hash(key) & hashmod;
  137. for (size_t probe = 0; probe <= hashmod; ++probe)
  138. {
  139. T& item = table[bucket];
  140. if (item == empty)
  141. return &item;
  142. if (hash.equal(item, key))
  143. return &item;
  144. // hash collision, quadratic probing
  145. bucket = (bucket + probe + 1) & hashmod;
  146. }
  147. assert(false && "Hash table is full"); // unreachable
  148. return NULL;
  149. }
  150. static void buildPositionRemap(unsigned int* remap, unsigned int* wedge, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, const unsigned int* sparse_remap, meshopt_Allocator& allocator)
  151. {
  152. PositionHasher hasher = {vertex_positions_data, vertex_positions_stride / sizeof(float), sparse_remap};
  153. size_t table_size = hashBuckets2(vertex_count);
  154. unsigned int* table = allocator.allocate<unsigned int>(table_size);
  155. memset(table, -1, table_size * sizeof(unsigned int));
  156. // build forward remap: for each vertex, which other (canonical) vertex does it map to?
  157. // we use position equivalence for this, and remap vertices to other existing vertices
  158. for (size_t i = 0; i < vertex_count; ++i)
  159. {
  160. unsigned int index = unsigned(i);
  161. unsigned int* entry = hashLookup2(table, table_size, hasher, index, ~0u);
  162. if (*entry == ~0u)
  163. *entry = index;
  164. remap[index] = *entry;
  165. }
  166. // build wedge table: for each vertex, which other vertex is the next wedge that also maps to the same vertex?
  167. // entries in table form a (cyclic) wedge loop per vertex; for manifold vertices, wedge[i] == remap[i] == i
  168. for (size_t i = 0; i < vertex_count; ++i)
  169. wedge[i] = unsigned(i);
  170. for (size_t i = 0; i < vertex_count; ++i)
  171. if (remap[i] != i)
  172. {
  173. unsigned int r = remap[i];
  174. wedge[i] = wedge[r];
  175. wedge[r] = unsigned(i);
  176. }
  177. allocator.deallocate(table);
  178. }
  179. static unsigned int* buildSparseRemap(unsigned int* indices, size_t index_count, size_t vertex_count, size_t* out_vertex_count, meshopt_Allocator& allocator)
  180. {
  181. // use a bit set to compute the precise number of unique vertices
  182. unsigned char* filter = allocator.allocate<unsigned char>((vertex_count + 7) / 8);
  183. memset(filter, 0, (vertex_count + 7) / 8);
  184. size_t unique = 0;
  185. for (size_t i = 0; i < index_count; ++i)
  186. {
  187. unsigned int index = indices[i];
  188. assert(index < vertex_count);
  189. unique += (filter[index / 8] & (1 << (index % 8))) == 0;
  190. filter[index / 8] |= 1 << (index % 8);
  191. }
  192. unsigned int* remap = allocator.allocate<unsigned int>(unique);
  193. size_t offset = 0;
  194. // temporary map dense => sparse; we allocate it last so that we can deallocate it
  195. size_t revremap_size = hashBuckets2(unique);
  196. unsigned int* revremap = allocator.allocate<unsigned int>(revremap_size);
  197. memset(revremap, -1, revremap_size * sizeof(unsigned int));
  198. // fill remap, using revremap as a helper, and rewrite indices in the same pass
  199. RemapHasher hasher = {remap};
  200. for (size_t i = 0; i < index_count; ++i)
  201. {
  202. unsigned int index = indices[i];
  203. unsigned int* entry = hashLookup2(revremap, revremap_size, hasher, index, ~0u);
  204. if (*entry == ~0u)
  205. {
  206. remap[offset] = index;
  207. *entry = unsigned(offset);
  208. offset++;
  209. }
  210. indices[i] = *entry;
  211. }
  212. allocator.deallocate(revremap);
  213. assert(offset == unique);
  214. *out_vertex_count = unique;
  215. return remap;
  216. }
  217. enum VertexKind
  218. {
  219. Kind_Manifold, // not on an attribute seam, not on any boundary
  220. Kind_Border, // not on an attribute seam, has exactly two open edges
  221. Kind_Seam, // on an attribute seam with exactly two attribute seam edges
  222. Kind_Complex, // none of the above; these vertices can move as long as all wedges move to the target vertex
  223. Kind_Locked, // none of the above; these vertices can't move
  224. Kind_Count
  225. };
  226. // manifold vertices can collapse onto anything
  227. // border/seam vertices can collapse onto border/seam respectively, or locked
  228. // complex vertices can collapse onto complex/locked
  229. // a rule of thumb is that collapsing kind A into kind B preserves the kind B in the target vertex
  230. // for example, while we could collapse Complex into Manifold, this would mean the target vertex isn't Manifold anymore
  231. const unsigned char kCanCollapse[Kind_Count][Kind_Count] = {
  232. {1, 1, 1, 1, 1},
  233. {0, 1, 0, 0, 1},
  234. {0, 0, 1, 0, 1},
  235. {0, 0, 0, 1, 1},
  236. {0, 0, 0, 0, 0},
  237. };
  238. // if a vertex is manifold or seam, adjoining edges are guaranteed to have an opposite edge
  239. // note that for seam edges, the opposite edge isn't present in the attribute-based topology
  240. // but is present if you consider a position-only mesh variant
  241. const unsigned char kHasOpposite[Kind_Count][Kind_Count] = {
  242. {1, 1, 1, 0, 1},
  243. {1, 0, 1, 0, 0},
  244. {1, 1, 1, 0, 1},
  245. {0, 0, 0, 0, 0},
  246. {1, 0, 1, 0, 0},
  247. };
  248. static bool hasEdge(const EdgeAdjacency& adjacency, unsigned int a, unsigned int b)
  249. {
  250. unsigned int count = adjacency.offsets[a + 1] - adjacency.offsets[a];
  251. const EdgeAdjacency::Edge* edges = adjacency.data + adjacency.offsets[a];
  252. for (size_t i = 0; i < count; ++i)
  253. if (edges[i].next == b)
  254. return true;
  255. return false;
  256. }
  257. 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)
  258. {
  259. memset(loop, -1, vertex_count * sizeof(unsigned int));
  260. memset(loopback, -1, vertex_count * sizeof(unsigned int));
  261. // incoming & outgoing open edges: ~0u if no open edges, i if there are more than 1
  262. // note that this is the same data as required in loop[] arrays; loop[] data is only valid for border/seam
  263. // but here it's okay to fill the data out for other types of vertices as well
  264. unsigned int* openinc = loopback;
  265. unsigned int* openout = loop;
  266. for (size_t i = 0; i < vertex_count; ++i)
  267. {
  268. unsigned int vertex = unsigned(i);
  269. unsigned int count = adjacency.offsets[vertex + 1] - adjacency.offsets[vertex];
  270. const EdgeAdjacency::Edge* edges = adjacency.data + adjacency.offsets[vertex];
  271. for (size_t j = 0; j < count; ++j)
  272. {
  273. unsigned int target = edges[j].next;
  274. if (target == vertex)
  275. {
  276. // degenerate triangles have two distinct edges instead of three, and the self edge
  277. // is bi-directional by definition; this can break border/seam classification by "closing"
  278. // the open edge from another triangle and falsely marking the vertex as manifold
  279. // instead we mark the vertex as having >1 open edges which turns it into locked/complex
  280. openinc[vertex] = openout[vertex] = vertex;
  281. }
  282. else if (!hasEdge(adjacency, target, vertex))
  283. {
  284. openinc[target] = (openinc[target] == ~0u) ? vertex : target;
  285. openout[vertex] = (openout[vertex] == ~0u) ? target : vertex;
  286. }
  287. }
  288. }
  289. #if TRACE
  290. size_t stats[4] = {};
  291. #endif
  292. for (size_t i = 0; i < vertex_count; ++i)
  293. {
  294. if (remap[i] == i)
  295. {
  296. if (wedge[i] == i)
  297. {
  298. // no attribute seam, need to check if it's manifold
  299. unsigned int openi = openinc[i], openo = openout[i];
  300. // note: we classify any vertices with no open edges as manifold
  301. // this is technically incorrect - if 4 triangles share an edge, we'll classify vertices as manifold
  302. // it's unclear if this is a problem in practice
  303. if (openi == ~0u && openo == ~0u)
  304. {
  305. result[i] = Kind_Manifold;
  306. }
  307. else if (openi != i && openo != i)
  308. {
  309. result[i] = Kind_Border;
  310. }
  311. else
  312. {
  313. result[i] = Kind_Locked;
  314. TRACESTATS(0);
  315. }
  316. }
  317. else if (wedge[wedge[i]] == i)
  318. {
  319. // attribute seam; need to distinguish between Seam and Locked
  320. unsigned int w = wedge[i];
  321. unsigned int openiv = openinc[i], openov = openout[i];
  322. unsigned int openiw = openinc[w], openow = openout[w];
  323. // seam should have one open half-edge for each vertex, and the edges need to "connect" - point to the same vertex post-remap
  324. if (openiv != ~0u && openiv != i && openov != ~0u && openov != i &&
  325. openiw != ~0u && openiw != w && openow != ~0u && openow != w)
  326. {
  327. if (remap[openiv] == remap[openow] && remap[openov] == remap[openiw] && remap[openiv] != remap[openov])
  328. {
  329. result[i] = Kind_Seam;
  330. }
  331. else
  332. {
  333. result[i] = Kind_Locked;
  334. TRACESTATS(1);
  335. }
  336. }
  337. else
  338. {
  339. result[i] = Kind_Locked;
  340. TRACESTATS(2);
  341. }
  342. }
  343. else
  344. {
  345. // more than one vertex maps to this one; we don't have classification available
  346. result[i] = Kind_Locked;
  347. TRACESTATS(3);
  348. }
  349. }
  350. else
  351. {
  352. assert(remap[i] < i);
  353. result[i] = result[remap[i]];
  354. }
  355. }
  356. if (vertex_lock)
  357. {
  358. // vertex_lock may lock any wedge, not just the primary vertex, so we need to lock the primary vertex and relock any wedges
  359. for (size_t i = 0; i < vertex_count; ++i)
  360. if (vertex_lock[sparse_remap ? sparse_remap[i] : i])
  361. result[remap[i]] = Kind_Locked;
  362. for (size_t i = 0; i < vertex_count; ++i)
  363. if (result[remap[i]] == Kind_Locked)
  364. result[i] = Kind_Locked;
  365. }
  366. if (options & meshopt_SimplifyLockBorder)
  367. for (size_t i = 0; i < vertex_count; ++i)
  368. if (result[i] == Kind_Border)
  369. result[i] = Kind_Locked;
  370. #if TRACE
  371. printf("locked: many open edges %d, disconnected seam %d, many seam edges %d, many wedges %d\n",
  372. int(stats[0]), int(stats[1]), int(stats[2]), int(stats[3]));
  373. #endif
  374. }
  375. struct Vector3
  376. {
  377. float x, y, z;
  378. };
  379. 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)
  380. {
  381. size_t vertex_stride_float = vertex_positions_stride / sizeof(float);
  382. float minv[3] = {FLT_MAX, FLT_MAX, FLT_MAX};
  383. float maxv[3] = {-FLT_MAX, -FLT_MAX, -FLT_MAX};
  384. for (size_t i = 0; i < vertex_count; ++i)
  385. {
  386. unsigned int ri = sparse_remap ? sparse_remap[i] : unsigned(i);
  387. const float* v = vertex_positions_data + ri * vertex_stride_float;
  388. if (result)
  389. {
  390. result[i].x = v[0];
  391. result[i].y = v[1];
  392. result[i].z = v[2];
  393. }
  394. for (int j = 0; j < 3; ++j)
  395. {
  396. float vj = v[j];
  397. minv[j] = minv[j] > vj ? vj : minv[j];
  398. maxv[j] = maxv[j] < vj ? vj : maxv[j];
  399. }
  400. }
  401. float extent = 0.f;
  402. extent = (maxv[0] - minv[0]) < extent ? extent : (maxv[0] - minv[0]);
  403. extent = (maxv[1] - minv[1]) < extent ? extent : (maxv[1] - minv[1]);
  404. extent = (maxv[2] - minv[2]) < extent ? extent : (maxv[2] - minv[2]);
  405. if (result)
  406. {
  407. float scale = extent == 0 ? 0.f : 1.f / extent;
  408. for (size_t i = 0; i < vertex_count; ++i)
  409. {
  410. result[i].x = (result[i].x - minv[0]) * scale;
  411. result[i].y = (result[i].y - minv[1]) * scale;
  412. result[i].z = (result[i].z - minv[2]) * scale;
  413. }
  414. }
  415. return extent;
  416. }
  417. static void rescaleAttributes(float* result, const float* vertex_attributes_data, size_t vertex_count, size_t vertex_attributes_stride, const float* attribute_weights, size_t attribute_count, const unsigned int* attribute_remap, const unsigned int* sparse_remap)
  418. {
  419. size_t vertex_attributes_stride_float = vertex_attributes_stride / sizeof(float);
  420. for (size_t i = 0; i < vertex_count; ++i)
  421. {
  422. unsigned int ri = sparse_remap ? sparse_remap[i] : unsigned(i);
  423. for (size_t k = 0; k < attribute_count; ++k)
  424. {
  425. unsigned int rk = attribute_remap[k];
  426. float a = vertex_attributes_data[ri * vertex_attributes_stride_float + rk];
  427. result[i * attribute_count + k] = a * attribute_weights[rk];
  428. }
  429. }
  430. }
  431. static const size_t kMaxAttributes = 32;
  432. struct Quadric
  433. {
  434. // a00*x^2 + a11*y^2 + a22*z^2 + 2*(a10*xy + a20*xz + a21*yz) + b0*x + b1*y + b2*z + c
  435. float a00, a11, a22;
  436. float a10, a20, a21;
  437. float b0, b1, b2, c;
  438. float w;
  439. };
  440. struct QuadricGrad
  441. {
  442. // gx*x + gy*y + gz*z + gw
  443. float gx, gy, gz, gw;
  444. };
  445. struct Reservoir
  446. {
  447. float x, y, z;
  448. float r, g, b;
  449. float w;
  450. };
  451. struct Collapse
  452. {
  453. unsigned int v0;
  454. unsigned int v1;
  455. union
  456. {
  457. unsigned int bidi;
  458. float error;
  459. unsigned int errorui;
  460. };
  461. };
  462. static float normalize(Vector3& v)
  463. {
  464. float length = sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
  465. if (length > 0)
  466. {
  467. v.x /= length;
  468. v.y /= length;
  469. v.z /= length;
  470. }
  471. return length;
  472. }
  473. static void quadricAdd(Quadric& Q, const Quadric& R)
  474. {
  475. Q.a00 += R.a00;
  476. Q.a11 += R.a11;
  477. Q.a22 += R.a22;
  478. Q.a10 += R.a10;
  479. Q.a20 += R.a20;
  480. Q.a21 += R.a21;
  481. Q.b0 += R.b0;
  482. Q.b1 += R.b1;
  483. Q.b2 += R.b2;
  484. Q.c += R.c;
  485. Q.w += R.w;
  486. }
  487. static void quadricAdd(QuadricGrad* G, const QuadricGrad* R, size_t attribute_count)
  488. {
  489. for (size_t k = 0; k < attribute_count; ++k)
  490. {
  491. G[k].gx += R[k].gx;
  492. G[k].gy += R[k].gy;
  493. G[k].gz += R[k].gz;
  494. G[k].gw += R[k].gw;
  495. }
  496. }
  497. static float quadricEval(const Quadric& Q, const Vector3& v)
  498. {
  499. float rx = Q.b0;
  500. float ry = Q.b1;
  501. float rz = Q.b2;
  502. rx += Q.a10 * v.y;
  503. ry += Q.a21 * v.z;
  504. rz += Q.a20 * v.x;
  505. rx *= 2;
  506. ry *= 2;
  507. rz *= 2;
  508. rx += Q.a00 * v.x;
  509. ry += Q.a11 * v.y;
  510. rz += Q.a22 * v.z;
  511. float r = Q.c;
  512. r += rx * v.x;
  513. r += ry * v.y;
  514. r += rz * v.z;
  515. return r;
  516. }
  517. static float quadricError(const Quadric& Q, const Vector3& v)
  518. {
  519. float r = quadricEval(Q, v);
  520. float s = Q.w == 0.f ? 0.f : 1.f / Q.w;
  521. return fabsf(r) * s;
  522. }
  523. static float quadricError(const Quadric& Q, const QuadricGrad* G, size_t attribute_count, const Vector3& v, const float* va)
  524. {
  525. float r = quadricEval(Q, v);
  526. // see quadricFromAttributes for general derivation; here we need to add the parts of (eval(pos) - attr)^2 that depend on attr
  527. for (size_t k = 0; k < attribute_count; ++k)
  528. {
  529. float a = va[k];
  530. float g = v.x * G[k].gx + v.y * G[k].gy + v.z * G[k].gz + G[k].gw;
  531. r += a * (a * Q.w - 2 * g);
  532. }
  533. // note: unlike position error, we do not normalize by Q.w to retain edge scaling as described in quadricFromAttributes
  534. return fabsf(r);
  535. }
  536. static void quadricFromPlane(Quadric& Q, float a, float b, float c, float d, float w)
  537. {
  538. float aw = a * w;
  539. float bw = b * w;
  540. float cw = c * w;
  541. float dw = d * w;
  542. Q.a00 = a * aw;
  543. Q.a11 = b * bw;
  544. Q.a22 = c * cw;
  545. Q.a10 = a * bw;
  546. Q.a20 = a * cw;
  547. Q.a21 = b * cw;
  548. Q.b0 = a * dw;
  549. Q.b1 = b * dw;
  550. Q.b2 = c * dw;
  551. Q.c = d * dw;
  552. Q.w = w;
  553. }
  554. static void quadricFromTriangle(Quadric& Q, const Vector3& p0, const Vector3& p1, const Vector3& p2, float weight)
  555. {
  556. Vector3 p10 = {p1.x - p0.x, p1.y - p0.y, p1.z - p0.z};
  557. Vector3 p20 = {p2.x - p0.x, p2.y - p0.y, p2.z - p0.z};
  558. // normal = cross(p1 - p0, p2 - p0)
  559. 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};
  560. float area = normalize(normal);
  561. float distance = normal.x * p0.x + normal.y * p0.y + normal.z * p0.z;
  562. // we use sqrtf(area) so that the error is scaled linearly; this tends to improve silhouettes
  563. quadricFromPlane(Q, normal.x, normal.y, normal.z, -distance, sqrtf(area) * weight);
  564. }
  565. static void quadricFromTriangleEdge(Quadric& Q, const Vector3& p0, const Vector3& p1, const Vector3& p2, float weight)
  566. {
  567. Vector3 p10 = {p1.x - p0.x, p1.y - p0.y, p1.z - p0.z};
  568. // edge length; keep squared length around for projection correction
  569. float lengthsq = p10.x * p10.x + p10.y * p10.y + p10.z * p10.z;
  570. float length = sqrtf(lengthsq);
  571. // p20p = length of projection of p2-p0 onto p1-p0; note that p10 is unnormalized so we need to correct it later
  572. Vector3 p20 = {p2.x - p0.x, p2.y - p0.y, p2.z - p0.z};
  573. float p20p = p20.x * p10.x + p20.y * p10.y + p20.z * p10.z;
  574. // perp = perpendicular vector from p2 to line segment p1-p0
  575. // note: since p10 is unnormalized we need to correct the projection; we scale p20 instead to take advantage of normalize below
  576. Vector3 perp = {p20.x * lengthsq - p10.x * p20p, p20.y * lengthsq - p10.y * p20p, p20.z * lengthsq - p10.z * p20p};
  577. normalize(perp);
  578. float distance = perp.x * p0.x + perp.y * p0.y + perp.z * p0.z;
  579. // note: the weight is scaled linearly with edge length; this has to match the triangle weight
  580. quadricFromPlane(Q, perp.x, perp.y, perp.z, -distance, length * weight);
  581. }
  582. static void quadricFromAttributes(Quadric& Q, QuadricGrad* G, const Vector3& p0, const Vector3& p1, const Vector3& p2, const float* va0, const float* va1, const float* va2, size_t attribute_count)
  583. {
  584. // for each attribute we want to encode the following function into the quadric:
  585. // (eval(pos) - attr)^2
  586. // where eval(pos) interpolates attribute across the triangle like so:
  587. // eval(pos) = pos.x * gx + pos.y * gy + pos.z * gz + gw
  588. // where gx/gy/gz/gw are gradients
  589. Vector3 p10 = {p1.x - p0.x, p1.y - p0.y, p1.z - p0.z};
  590. Vector3 p20 = {p2.x - p0.x, p2.y - p0.y, p2.z - p0.z};
  591. // normal = cross(p1 - p0, p2 - p0)
  592. 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};
  593. float area = sqrtf(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z) * 0.5f;
  594. // quadric is weighted with the square of edge length (= area)
  595. // this equalizes the units with the positional error (which, after normalization, is a square of distance)
  596. // as a result, a change in weighted attribute of 1 along distance d is approximately equivalent to a change in position of d
  597. float w = area;
  598. // we compute gradients using barycentric coordinates; barycentric coordinates can be computed as follows:
  599. // v = (d11 * d20 - d01 * d21) / denom
  600. // w = (d00 * d21 - d01 * d20) / denom
  601. // u = 1 - v - w
  602. // here v0, v1 are triangle edge vectors, v2 is a vector from point to triangle corner, and dij = dot(vi, vj)
  603. // note: v2 and d20/d21 can not be evaluated here as v2 is effectively an unknown variable; we need these only as variables for derivation of gradients
  604. const Vector3& v0 = p10;
  605. const Vector3& v1 = p20;
  606. float d00 = v0.x * v0.x + v0.y * v0.y + v0.z * v0.z;
  607. float d01 = v0.x * v1.x + v0.y * v1.y + v0.z * v1.z;
  608. float d11 = v1.x * v1.x + v1.y * v1.y + v1.z * v1.z;
  609. float denom = d00 * d11 - d01 * d01;
  610. float denomr = denom == 0 ? 0.f : 1.f / denom;
  611. // precompute gradient factors
  612. // these are derived by directly computing derivative of eval(pos) = a0 * u + a1 * v + a2 * w and factoring out expressions that are shared between attributes
  613. float gx1 = (d11 * v0.x - d01 * v1.x) * denomr;
  614. float gx2 = (d00 * v1.x - d01 * v0.x) * denomr;
  615. float gy1 = (d11 * v0.y - d01 * v1.y) * denomr;
  616. float gy2 = (d00 * v1.y - d01 * v0.y) * denomr;
  617. float gz1 = (d11 * v0.z - d01 * v1.z) * denomr;
  618. float gz2 = (d00 * v1.z - d01 * v0.z) * denomr;
  619. memset(&Q, 0, sizeof(Quadric));
  620. Q.w = w;
  621. for (size_t k = 0; k < attribute_count; ++k)
  622. {
  623. float a0 = va0[k], a1 = va1[k], a2 = va2[k];
  624. // compute gradient of eval(pos) for x/y/z/w
  625. // the formulas below are obtained by directly computing derivative of eval(pos) = a0 * u + a1 * v + a2 * w
  626. float gx = gx1 * (a1 - a0) + gx2 * (a2 - a0);
  627. float gy = gy1 * (a1 - a0) + gy2 * (a2 - a0);
  628. float gz = gz1 * (a1 - a0) + gz2 * (a2 - a0);
  629. float gw = a0 - p0.x * gx - p0.y * gy - p0.z * gz;
  630. // quadric encodes (eval(pos)-attr)^2; this means that the resulting expansion needs to compute, for example, pos.x * pos.y * K
  631. // since quadrics already encode factors for pos.x * pos.y, we can accumulate almost everything in basic quadric fields
  632. // note: for simplicity we scale all factors by weight here instead of outside the loop
  633. Q.a00 += w * (gx * gx);
  634. Q.a11 += w * (gy * gy);
  635. Q.a22 += w * (gz * gz);
  636. Q.a10 += w * (gy * gx);
  637. Q.a20 += w * (gz * gx);
  638. Q.a21 += w * (gz * gy);
  639. Q.b0 += w * (gx * gw);
  640. Q.b1 += w * (gy * gw);
  641. Q.b2 += w * (gz * gw);
  642. Q.c += w * (gw * gw);
  643. // the only remaining sum components are ones that depend on attr; these will be addded during error evaluation, see quadricError
  644. G[k].gx = w * gx;
  645. G[k].gy = w * gy;
  646. G[k].gz = w * gz;
  647. G[k].gw = w * gw;
  648. }
  649. }
  650. static void fillFaceQuadrics(Quadric* vertex_quadrics, const unsigned int* indices, size_t index_count, const Vector3* vertex_positions, const unsigned int* remap)
  651. {
  652. for (size_t i = 0; i < index_count; i += 3)
  653. {
  654. unsigned int i0 = indices[i + 0];
  655. unsigned int i1 = indices[i + 1];
  656. unsigned int i2 = indices[i + 2];
  657. Quadric Q;
  658. quadricFromTriangle(Q, vertex_positions[i0], vertex_positions[i1], vertex_positions[i2], 1.f);
  659. quadricAdd(vertex_quadrics[remap[i0]], Q);
  660. quadricAdd(vertex_quadrics[remap[i1]], Q);
  661. quadricAdd(vertex_quadrics[remap[i2]], Q);
  662. }
  663. }
  664. static void fillEdgeQuadrics(Quadric* vertex_quadrics, const unsigned int* indices, size_t index_count, const Vector3* vertex_positions, const unsigned int* remap, const unsigned char* vertex_kind, const unsigned int* loop, const unsigned int* loopback)
  665. {
  666. for (size_t i = 0; i < index_count; i += 3)
  667. {
  668. static const int next[4] = {1, 2, 0, 1};
  669. for (int e = 0; e < 3; ++e)
  670. {
  671. unsigned int i0 = indices[i + e];
  672. unsigned int i1 = indices[i + next[e]];
  673. unsigned char k0 = vertex_kind[i0];
  674. unsigned char k1 = vertex_kind[i1];
  675. // check that either i0 or i1 are border/seam and are on the same edge loop
  676. // note that we need to add the error even for edged that connect e.g. border & locked
  677. // if we don't do that, the adjacent border->border edge won't have correct errors for corners
  678. if (k0 != Kind_Border && k0 != Kind_Seam && k1 != Kind_Border && k1 != Kind_Seam)
  679. continue;
  680. if ((k0 == Kind_Border || k0 == Kind_Seam) && loop[i0] != i1)
  681. continue;
  682. if ((k1 == Kind_Border || k1 == Kind_Seam) && loopback[i1] != i0)
  683. continue;
  684. // seam edges should occur twice (i0->i1 and i1->i0) - skip redundant edges
  685. if (kHasOpposite[k0][k1] && remap[i1] > remap[i0])
  686. continue;
  687. unsigned int i2 = indices[i + next[e + 1]];
  688. // we try hard to maintain border edge geometry; seam edges can move more freely
  689. // due to topological restrictions on collapses, seam quadrics slightly improves collapse structure but aren't critical
  690. const float kEdgeWeightSeam = 1.f;
  691. const float kEdgeWeightBorder = 10.f;
  692. float edgeWeight = (k0 == Kind_Border || k1 == Kind_Border) ? kEdgeWeightBorder : kEdgeWeightSeam;
  693. Quadric Q;
  694. quadricFromTriangleEdge(Q, vertex_positions[i0], vertex_positions[i1], vertex_positions[i2], edgeWeight);
  695. quadricAdd(vertex_quadrics[remap[i0]], Q);
  696. quadricAdd(vertex_quadrics[remap[i1]], Q);
  697. }
  698. }
  699. }
  700. static void fillAttributeQuadrics(Quadric* attribute_quadrics, QuadricGrad* attribute_gradients, const unsigned int* indices, size_t index_count, const Vector3* vertex_positions, const float* vertex_attributes, size_t attribute_count)
  701. {
  702. for (size_t i = 0; i < index_count; i += 3)
  703. {
  704. unsigned int i0 = indices[i + 0];
  705. unsigned int i1 = indices[i + 1];
  706. unsigned int i2 = indices[i + 2];
  707. Quadric QA;
  708. QuadricGrad G[kMaxAttributes];
  709. quadricFromAttributes(QA, G, vertex_positions[i0], vertex_positions[i1], vertex_positions[i2], &vertex_attributes[i0 * attribute_count], &vertex_attributes[i1 * attribute_count], &vertex_attributes[i2 * attribute_count], attribute_count);
  710. quadricAdd(attribute_quadrics[i0], QA);
  711. quadricAdd(attribute_quadrics[i1], QA);
  712. quadricAdd(attribute_quadrics[i2], QA);
  713. quadricAdd(&attribute_gradients[i0 * attribute_count], G, attribute_count);
  714. quadricAdd(&attribute_gradients[i1 * attribute_count], G, attribute_count);
  715. quadricAdd(&attribute_gradients[i2 * attribute_count], G, attribute_count);
  716. }
  717. }
  718. // does triangle ABC flip when C is replaced with D?
  719. static bool hasTriangleFlip(const Vector3& a, const Vector3& b, const Vector3& c, const Vector3& d)
  720. {
  721. Vector3 eb = {b.x - a.x, b.y - a.y, b.z - a.z};
  722. Vector3 ec = {c.x - a.x, c.y - a.y, c.z - a.z};
  723. Vector3 ed = {d.x - a.x, d.y - a.y, d.z - a.z};
  724. Vector3 nbc = {eb.y * ec.z - eb.z * ec.y, eb.z * ec.x - eb.x * ec.z, eb.x * ec.y - eb.y * ec.x};
  725. Vector3 nbd = {eb.y * ed.z - eb.z * ed.y, eb.z * ed.x - eb.x * ed.z, eb.x * ed.y - eb.y * ed.x};
  726. float ndp = nbc.x * nbd.x + nbc.y * nbd.y + nbc.z * nbd.z;
  727. float abc = nbc.x * nbc.x + nbc.y * nbc.y + nbc.z * nbc.z;
  728. float abd = nbd.x * nbd.x + nbd.y * nbd.y + nbd.z * nbd.z;
  729. // scale is cos(angle); somewhat arbitrarily set to ~75 degrees
  730. // note that the "pure" check is ndp <= 0 (90 degree cutoff) but that allows flipping through a series of close-to-90 collapses
  731. return ndp <= 0.25f * sqrtf(abc * abd);
  732. }
  733. static bool hasTriangleFlips(const EdgeAdjacency& adjacency, const Vector3* vertex_positions, const unsigned int* collapse_remap, unsigned int i0, unsigned int i1)
  734. {
  735. assert(collapse_remap[i0] == i0);
  736. assert(collapse_remap[i1] == i1);
  737. const Vector3& v0 = vertex_positions[i0];
  738. const Vector3& v1 = vertex_positions[i1];
  739. const EdgeAdjacency::Edge* edges = &adjacency.data[adjacency.offsets[i0]];
  740. size_t count = adjacency.offsets[i0 + 1] - adjacency.offsets[i0];
  741. for (size_t i = 0; i < count; ++i)
  742. {
  743. unsigned int a = collapse_remap[edges[i].next];
  744. unsigned int b = collapse_remap[edges[i].prev];
  745. // skip triangles that will get collapsed by i0->i1 collapse or already got collapsed previously
  746. if (a == i1 || b == i1 || a == b)
  747. continue;
  748. // early-out when at least one triangle flips due to a collapse
  749. if (hasTriangleFlip(vertex_positions[a], vertex_positions[b], v0, v1))
  750. {
  751. #if TRACE >= 2
  752. printf("edge block %d -> %d: flip welded %d %d %d\n", i0, i1, a, i0, b);
  753. #endif
  754. return true;
  755. }
  756. }
  757. return false;
  758. }
  759. static size_t boundEdgeCollapses(const EdgeAdjacency& adjacency, size_t vertex_count, size_t index_count, unsigned char* vertex_kind)
  760. {
  761. size_t dual_count = 0;
  762. for (size_t i = 0; i < vertex_count; ++i)
  763. {
  764. unsigned char k = vertex_kind[i];
  765. unsigned int e = adjacency.offsets[i + 1] - adjacency.offsets[i];
  766. dual_count += (k == Kind_Manifold || k == Kind_Seam) ? e : 0;
  767. }
  768. assert(dual_count <= index_count);
  769. // pad capacity by 3 so that we can check for overflow once per triangle instead of once per edge
  770. return (index_count - dual_count / 2) + 3;
  771. }
  772. static size_t pickEdgeCollapses(Collapse* collapses, size_t collapse_capacity, const unsigned int* indices, size_t index_count, const unsigned int* remap, const unsigned char* vertex_kind, const unsigned int* loop, const unsigned int* loopback)
  773. {
  774. size_t collapse_count = 0;
  775. for (size_t i = 0; i < index_count; i += 3)
  776. {
  777. static const int next[3] = {1, 2, 0};
  778. // this should never happen as boundEdgeCollapses should give an upper bound for the collapse count, but in an unlikely event it does we can just drop extra collapses
  779. if (collapse_count + 3 > collapse_capacity)
  780. break;
  781. for (int e = 0; e < 3; ++e)
  782. {
  783. unsigned int i0 = indices[i + e];
  784. unsigned int i1 = indices[i + next[e]];
  785. // this can happen either when input has a zero-length edge, or when we perform collapses for complex
  786. // topology w/seams and collapse a manifold vertex that connects to both wedges onto one of them
  787. // we leave edges like this alone since they may be important for preserving mesh integrity
  788. if (remap[i0] == remap[i1])
  789. continue;
  790. unsigned char k0 = vertex_kind[i0];
  791. unsigned char k1 = vertex_kind[i1];
  792. // the edge has to be collapsible in at least one direction
  793. if (!(kCanCollapse[k0][k1] | kCanCollapse[k1][k0]))
  794. continue;
  795. // manifold and seam edges should occur twice (i0->i1 and i1->i0) - skip redundant edges
  796. if (kHasOpposite[k0][k1] && remap[i1] > remap[i0])
  797. continue;
  798. // two vertices are on a border or a seam, but there's no direct edge between them
  799. // this indicates that they belong to two different edge loops and we should not collapse this edge
  800. // loop[] tracks half edges so we only need to check i0->i1
  801. if (k0 == k1 && (k0 == Kind_Border || k0 == Kind_Seam) && loop[i0] != i1)
  802. continue;
  803. if (k0 == Kind_Locked || k1 == Kind_Locked)
  804. {
  805. // the same check as above, but for border/seam -> locked collapses
  806. // loop[] and loopback[] track half edges so we only need to check one of them
  807. if ((k0 == Kind_Border || k0 == Kind_Seam) && loop[i0] != i1)
  808. continue;
  809. if ((k1 == Kind_Border || k1 == Kind_Seam) && loopback[i1] != i0)
  810. continue;
  811. }
  812. // edge can be collapsed in either direction - we will pick the one with minimum error
  813. // note: we evaluate error later during collapse ranking, here we just tag the edge as bidirectional
  814. if (kCanCollapse[k0][k1] & kCanCollapse[k1][k0])
  815. {
  816. Collapse c = {i0, i1, {/* bidi= */ 1}};
  817. collapses[collapse_count++] = c;
  818. }
  819. else
  820. {
  821. // edge can only be collapsed in one direction
  822. unsigned int e0 = kCanCollapse[k0][k1] ? i0 : i1;
  823. unsigned int e1 = kCanCollapse[k0][k1] ? i1 : i0;
  824. Collapse c = {e0, e1, {/* bidi= */ 0}};
  825. collapses[collapse_count++] = c;
  826. }
  827. }
  828. }
  829. return collapse_count;
  830. }
  831. static void rankEdgeCollapses(Collapse* collapses, size_t collapse_count, const Vector3* vertex_positions, const float* vertex_attributes, const Quadric* vertex_quadrics, const Quadric* attribute_quadrics, const QuadricGrad* attribute_gradients, size_t attribute_count, const unsigned int* remap)
  832. {
  833. for (size_t i = 0; i < collapse_count; ++i)
  834. {
  835. Collapse& c = collapses[i];
  836. unsigned int i0 = c.v0;
  837. unsigned int i1 = c.v1;
  838. // most edges are bidirectional which means we need to evaluate errors for two collapses
  839. // to keep this code branchless we just use the same edge for unidirectional edges
  840. unsigned int j0 = c.bidi ? i1 : i0;
  841. unsigned int j1 = c.bidi ? i0 : i1;
  842. float ei = quadricError(vertex_quadrics[remap[i0]], vertex_positions[i1]);
  843. float ej = quadricError(vertex_quadrics[remap[j0]], vertex_positions[j1]);
  844. #if TRACE >= 3
  845. float di = ei, dj = ej;
  846. #endif
  847. if (attribute_count)
  848. {
  849. // note: ideally we would evaluate max/avg of attribute errors for seam edges, but it's not clear if it's worth the extra cost
  850. ei += quadricError(attribute_quadrics[i0], &attribute_gradients[i0 * attribute_count], attribute_count, vertex_positions[i1], &vertex_attributes[i1 * attribute_count]);
  851. ej += quadricError(attribute_quadrics[j0], &attribute_gradients[j0 * attribute_count], attribute_count, vertex_positions[j1], &vertex_attributes[j1 * attribute_count]);
  852. }
  853. // pick edge direction with minimal error
  854. c.v0 = ei <= ej ? i0 : j0;
  855. c.v1 = ei <= ej ? i1 : j1;
  856. c.error = ei <= ej ? ei : ej;
  857. #if TRACE >= 3
  858. if (i0 == j0) // c.bidi has been overwritten
  859. printf("edge eval %d -> %d: error %f (pos %f, attr %f)\n", c.v0, c.v1,
  860. sqrtf(c.error), sqrtf(ei <= ej ? di : dj), sqrtf(ei <= ej ? ei - di : ej - dj));
  861. else
  862. printf("edge eval %d -> %d: error %f (pos %f, attr %f); reverse %f (pos %f, attr %f)\n", c.v0, c.v1,
  863. sqrtf(ei <= ej ? ei : ej), sqrtf(ei <= ej ? di : dj), sqrtf(ei <= ej ? ei - di : ej - dj),
  864. sqrtf(ei <= ej ? ej : ei), sqrtf(ei <= ej ? dj : di), sqrtf(ei <= ej ? ej - dj : ei - di));
  865. #endif
  866. }
  867. }
  868. static void sortEdgeCollapses(unsigned int* sort_order, const Collapse* collapses, size_t collapse_count)
  869. {
  870. // we use counting sort to order collapses by error; since the exact sort order is not as critical,
  871. // only top 12 bits of exponent+mantissa (8 bits of exponent and 4 bits of mantissa) are used.
  872. // to avoid excessive stack usage, we clamp the exponent range as collapses with errors much higher than 1 are not useful.
  873. const unsigned int sort_bits = 12;
  874. const unsigned int sort_bins = 2048 + 512; // exponent range [-127, 32)
  875. // fill histogram for counting sort
  876. unsigned int histogram[sort_bins];
  877. memset(histogram, 0, sizeof(histogram));
  878. for (size_t i = 0; i < collapse_count; ++i)
  879. {
  880. // skip sign bit since error is non-negative
  881. unsigned int error = collapses[i].errorui;
  882. unsigned int key = (error << 1) >> (32 - sort_bits);
  883. key = key < sort_bins ? key : sort_bins - 1;
  884. histogram[key]++;
  885. }
  886. // compute offsets based on histogram data
  887. size_t histogram_sum = 0;
  888. for (size_t i = 0; i < sort_bins; ++i)
  889. {
  890. size_t count = histogram[i];
  891. histogram[i] = unsigned(histogram_sum);
  892. histogram_sum += count;
  893. }
  894. assert(histogram_sum == collapse_count);
  895. // compute sort order based on offsets
  896. for (size_t i = 0; i < collapse_count; ++i)
  897. {
  898. // skip sign bit since error is non-negative
  899. unsigned int error = collapses[i].errorui;
  900. unsigned int key = (error << 1) >> (32 - sort_bits);
  901. key = key < sort_bins ? key : sort_bins - 1;
  902. sort_order[histogram[key]++] = unsigned(i);
  903. }
  904. }
  905. static size_t performEdgeCollapses(unsigned int* collapse_remap, unsigned char* collapse_locked, const Collapse* collapses, size_t collapse_count, const unsigned int* collapse_order, const unsigned int* remap, const unsigned int* wedge, const unsigned char* vertex_kind, const unsigned int* loop, const unsigned int* loopback, const Vector3* vertex_positions, const EdgeAdjacency& adjacency, size_t triangle_collapse_goal, float error_limit, float& result_error)
  906. {
  907. size_t edge_collapses = 0;
  908. size_t triangle_collapses = 0;
  909. // most collapses remove 2 triangles; use this to establish a bound on the pass in terms of error limit
  910. // note that edge_collapse_goal is an estimate; triangle_collapse_goal will be used to actually limit collapses
  911. size_t edge_collapse_goal = triangle_collapse_goal / 2;
  912. #if TRACE
  913. size_t stats[7] = {};
  914. #endif
  915. for (size_t i = 0; i < collapse_count; ++i)
  916. {
  917. const Collapse& c = collapses[collapse_order[i]];
  918. TRACESTATS(0);
  919. if (c.error > error_limit)
  920. {
  921. TRACESTATS(4);
  922. break;
  923. }
  924. if (triangle_collapses >= triangle_collapse_goal)
  925. {
  926. TRACESTATS(5);
  927. break;
  928. }
  929. // we limit the error in each pass based on the error of optimal last collapse; since many collapses will be locked
  930. // as they will share vertices with other successfull collapses, we need to increase the acceptable error by some factor
  931. float error_goal = edge_collapse_goal < collapse_count ? 1.5f * collapses[collapse_order[edge_collapse_goal]].error : FLT_MAX;
  932. // on average, each collapse is expected to lock 6 other collapses; to avoid degenerate passes on meshes with odd
  933. // topology, we only abort if we got over 1/6 collapses accordingly.
  934. if (c.error > error_goal && c.error > result_error && triangle_collapses > triangle_collapse_goal / 6)
  935. {
  936. TRACESTATS(6);
  937. break;
  938. }
  939. unsigned int i0 = c.v0;
  940. unsigned int i1 = c.v1;
  941. unsigned int r0 = remap[i0];
  942. unsigned int r1 = remap[i1];
  943. unsigned char kind = vertex_kind[i0];
  944. // we don't collapse vertices that had source or target vertex involved in a collapse
  945. // it's important to not move the vertices twice since it complicates the tracking/remapping logic
  946. // it's important to not move other vertices towards a moved vertex to preserve error since we don't re-rank collapses mid-pass
  947. if (collapse_locked[r0] | collapse_locked[r1])
  948. {
  949. TRACESTATS(1);
  950. continue;
  951. }
  952. if (hasTriangleFlips(adjacency, vertex_positions, collapse_remap, r0, r1))
  953. {
  954. // adjust collapse goal since this collapse is invalid and shouldn't factor into error goal
  955. edge_collapse_goal++;
  956. TRACESTATS(2);
  957. continue;
  958. }
  959. #if TRACE >= 2
  960. printf("edge commit %d -> %d: kind %d->%d, error %f\n", i0, i1, vertex_kind[i0], vertex_kind[i1], sqrtf(c.error));
  961. #endif
  962. assert(collapse_remap[r0] == r0);
  963. assert(collapse_remap[r1] == r1);
  964. if (kind == Kind_Complex)
  965. {
  966. // remap all vertices in the complex to the target vertex
  967. unsigned int v = i0;
  968. do
  969. {
  970. collapse_remap[v] = i1;
  971. v = wedge[v];
  972. } while (v != i0);
  973. }
  974. else if (kind == Kind_Seam)
  975. {
  976. // for seam collapses we need to move the seam pair together; this is a bit tricky to compute since we need to rely on edge loops as target vertex may be locked (and thus have more than two wedges)
  977. unsigned int s0 = wedge[i0];
  978. unsigned int s1 = loop[i0] == i1 ? loopback[s0] : loop[s0];
  979. assert(s0 != i0 && wedge[s0] == i0);
  980. assert(s1 != ~0u && remap[s1] == r1);
  981. // additional asserts to verify that the seam pair is consistent
  982. assert(kind != vertex_kind[i1] || s1 == wedge[i1]);
  983. assert(loop[i0] == i1 || loopback[i0] == i1);
  984. assert(loop[s0] == s1 || loopback[s0] == s1);
  985. // 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
  986. s1 = (s1 != ~0u) ? s1 : wedge[i1];
  987. collapse_remap[i0] = i1;
  988. collapse_remap[s0] = s1;
  989. }
  990. else
  991. {
  992. assert(wedge[i0] == i0);
  993. collapse_remap[i0] = i1;
  994. }
  995. // note: we technically don't need to lock r1 if it's a locked vertex, as it can't move and its quadric won't be used
  996. // however, this results in slightly worse error on some meshes because the locked collapses get an unfair advantage wrt scheduling
  997. collapse_locked[r0] = 1;
  998. collapse_locked[r1] = 1;
  999. // border edges collapse 1 triangle, other edges collapse 2 or more
  1000. triangle_collapses += (kind == Kind_Border) ? 1 : 2;
  1001. edge_collapses++;
  1002. result_error = result_error < c.error ? c.error : result_error;
  1003. }
  1004. #if TRACE
  1005. float error_goal_last = edge_collapse_goal < collapse_count ? 1.5f * collapses[collapse_order[edge_collapse_goal]].error : FLT_MAX;
  1006. float error_goal_limit = error_goal_last < error_limit ? error_goal_last : error_limit;
  1007. printf("removed %d triangles, error %e (goal %e); evaluated %d/%d collapses (done %d, skipped %d, invalid %d); %s\n",
  1008. int(triangle_collapses), sqrtf(result_error), sqrtf(error_goal_limit),
  1009. int(stats[0]), int(collapse_count), int(edge_collapses), int(stats[1]), int(stats[2]),
  1010. stats[4] ? "error limit" : (stats[5] ? "count limit" : (stats[6] ? "error goal" : "out of collapses")));
  1011. #endif
  1012. return edge_collapses;
  1013. }
  1014. 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)
  1015. {
  1016. for (size_t i = 0; i < vertex_count; ++i)
  1017. {
  1018. if (collapse_remap[i] == i)
  1019. continue;
  1020. unsigned int i0 = unsigned(i);
  1021. unsigned int i1 = collapse_remap[i];
  1022. unsigned int r0 = remap[i0];
  1023. unsigned int r1 = remap[i1];
  1024. // ensure we only update vertex_quadrics once: primary vertex must be moved if any wedge is moved
  1025. if (i0 == r0)
  1026. quadricAdd(vertex_quadrics[r1], vertex_quadrics[r0]);
  1027. if (attribute_count)
  1028. {
  1029. quadricAdd(attribute_quadrics[i1], attribute_quadrics[i0]);
  1030. quadricAdd(&attribute_gradients[i1 * attribute_count], &attribute_gradients[i0 * attribute_count], attribute_count);
  1031. if (i0 == r0)
  1032. {
  1033. // when attributes are used, distance error needs to be recomputed as collapses don't track it; it is safe to do this after the quadric adjustment
  1034. float derr = quadricError(vertex_quadrics[r0], vertex_positions[r1]);
  1035. vertex_error = vertex_error < derr ? derr : vertex_error;
  1036. }
  1037. }
  1038. }
  1039. }
  1040. static size_t remapIndexBuffer(unsigned int* indices, size_t index_count, const unsigned int* collapse_remap)
  1041. {
  1042. size_t write = 0;
  1043. for (size_t i = 0; i < index_count; i += 3)
  1044. {
  1045. unsigned int v0 = collapse_remap[indices[i + 0]];
  1046. unsigned int v1 = collapse_remap[indices[i + 1]];
  1047. unsigned int v2 = collapse_remap[indices[i + 2]];
  1048. // we never move the vertex twice during a single pass
  1049. assert(collapse_remap[v0] == v0);
  1050. assert(collapse_remap[v1] == v1);
  1051. assert(collapse_remap[v2] == v2);
  1052. if (v0 != v1 && v0 != v2 && v1 != v2)
  1053. {
  1054. indices[write + 0] = v0;
  1055. indices[write + 1] = v1;
  1056. indices[write + 2] = v2;
  1057. write += 3;
  1058. }
  1059. }
  1060. return write;
  1061. }
  1062. static void remapEdgeLoops(unsigned int* loop, size_t vertex_count, const unsigned int* collapse_remap)
  1063. {
  1064. for (size_t i = 0; i < vertex_count; ++i)
  1065. {
  1066. // note: this is a no-op for vertices that were remapped
  1067. // ideally we would clear the loop entries for those for consistency, even though they aren't going to be used
  1068. // however, the remapping process needs loop information for remapped vertices, so this would require a separate pass
  1069. if (loop[i] != ~0u)
  1070. {
  1071. unsigned int l = loop[i];
  1072. unsigned int r = collapse_remap[l];
  1073. // i == r is a special case when the seam edge is collapsed in a direction opposite to where loop goes
  1074. if (i == r)
  1075. loop[i] = (loop[l] != ~0u) ? collapse_remap[loop[l]] : ~0u;
  1076. else
  1077. loop[i] = r;
  1078. }
  1079. }
  1080. }
  1081. static unsigned int follow(unsigned int* parents, unsigned int index)
  1082. {
  1083. while (index != parents[index])
  1084. {
  1085. unsigned int parent = parents[index];
  1086. parents[index] = parents[parent];
  1087. index = parent;
  1088. }
  1089. return index;
  1090. }
  1091. static size_t buildComponents(unsigned int* components, size_t vertex_count, const unsigned int* indices, size_t index_count, const unsigned int* remap)
  1092. {
  1093. for (size_t i = 0; i < vertex_count; ++i)
  1094. components[i] = unsigned(i);
  1095. // compute a unique (but not sequential!) index for each component via union-find
  1096. for (size_t i = 0; i < index_count; i += 3)
  1097. {
  1098. static const int next[4] = {1, 2, 0, 1};
  1099. for (int e = 0; e < 3; ++e)
  1100. {
  1101. unsigned int i0 = indices[i + e];
  1102. unsigned int i1 = indices[i + next[e]];
  1103. unsigned int r0 = remap[i0];
  1104. unsigned int r1 = remap[i1];
  1105. r0 = follow(components, r0);
  1106. r1 = follow(components, r1);
  1107. // merge components with larger indices into components with smaller indices
  1108. // this guarantees that the root of the component is always the one with the smallest index
  1109. if (r0 != r1)
  1110. components[r0 < r1 ? r1 : r0] = r0 < r1 ? r0 : r1;
  1111. }
  1112. }
  1113. // make sure each element points to the component root *before* we renumber the components
  1114. for (size_t i = 0; i < vertex_count; ++i)
  1115. if (remap[i] == i)
  1116. components[i] = follow(components, unsigned(i));
  1117. unsigned int next_component = 0;
  1118. // renumber components using sequential indices
  1119. // a sequential pass is sufficient because component root always has the smallest index
  1120. // note: it is unsafe to use follow() in this pass because we're replacing component links with sequential indices inplace
  1121. for (size_t i = 0; i < vertex_count; ++i)
  1122. {
  1123. if (remap[i] == i)
  1124. {
  1125. unsigned int root = components[i];
  1126. assert(root <= i); // make sure we already computed the component for non-roots
  1127. components[i] = (root == i) ? next_component++ : components[root];
  1128. }
  1129. else
  1130. {
  1131. assert(remap[i] < i); // make sure we already computed the component
  1132. components[i] = components[remap[i]];
  1133. }
  1134. }
  1135. return next_component;
  1136. }
  1137. static void measureComponents(float* component_errors, size_t component_count, const unsigned int* components, const Vector3* vertex_positions, size_t vertex_count)
  1138. {
  1139. memset(component_errors, 0, component_count * 4 * sizeof(float));
  1140. // compute approximate sphere center for each component as an average
  1141. for (size_t i = 0; i < vertex_count; ++i)
  1142. {
  1143. unsigned int c = components[i];
  1144. assert(components[i] < component_count);
  1145. Vector3 v = vertex_positions[i]; // copy avoids aliasing issues
  1146. component_errors[c * 4 + 0] += v.x;
  1147. component_errors[c * 4 + 1] += v.y;
  1148. component_errors[c * 4 + 2] += v.z;
  1149. component_errors[c * 4 + 3] += 1; // weight
  1150. }
  1151. // complete the center computation, and reinitialize [3] as a radius
  1152. for (size_t i = 0; i < component_count; ++i)
  1153. {
  1154. float w = component_errors[i * 4 + 3];
  1155. float iw = w == 0.f ? 0.f : 1.f / w;
  1156. component_errors[i * 4 + 0] *= iw;
  1157. component_errors[i * 4 + 1] *= iw;
  1158. component_errors[i * 4 + 2] *= iw;
  1159. component_errors[i * 4 + 3] = 0; // radius
  1160. }
  1161. // compute squared radius for each component
  1162. for (size_t i = 0; i < vertex_count; ++i)
  1163. {
  1164. unsigned int c = components[i];
  1165. float dx = vertex_positions[i].x - component_errors[c * 4 + 0];
  1166. float dy = vertex_positions[i].y - component_errors[c * 4 + 1];
  1167. float dz = vertex_positions[i].z - component_errors[c * 4 + 2];
  1168. float r = dx * dx + dy * dy + dz * dz;
  1169. component_errors[c * 4 + 3] = component_errors[c * 4 + 3] < r ? r : component_errors[c * 4 + 3];
  1170. }
  1171. // we've used the output buffer as scratch space, so we need to move the results to proper indices
  1172. for (size_t i = 0; i < component_count; ++i)
  1173. {
  1174. #if TRACE >= 2
  1175. printf("component %d: center %f %f %f, error %e\n", int(i),
  1176. component_errors[i * 4 + 0], component_errors[i * 4 + 1], component_errors[i * 4 + 2], sqrtf(component_errors[i * 4 + 3]));
  1177. #endif
  1178. // note: we keep the squared error to make it match quadric error metric
  1179. component_errors[i] = component_errors[i * 4 + 3];
  1180. }
  1181. }
  1182. 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)
  1183. {
  1184. size_t write = 0;
  1185. for (size_t i = 0; i < index_count; i += 3)
  1186. {
  1187. unsigned int c = components[indices[i]];
  1188. assert(c == components[indices[i + 1]] && c == components[indices[i + 2]]);
  1189. if (component_errors[c] > error_cutoff)
  1190. {
  1191. indices[write + 0] = indices[i + 0];
  1192. indices[write + 1] = indices[i + 1];
  1193. indices[write + 2] = indices[i + 2];
  1194. write += 3;
  1195. }
  1196. }
  1197. #if TRACE
  1198. size_t pruned_components = 0;
  1199. for (size_t i = 0; i < component_count; ++i)
  1200. pruned_components += (component_errors[i] >= nexterror && component_errors[i] <= error_cutoff);
  1201. printf("pruned %d triangles in %d components (goal %e)\n", int((index_count - write) / 3), int(pruned_components), sqrtf(error_cutoff));
  1202. #endif
  1203. // update next error with the smallest error of the remaining components for future pruning
  1204. nexterror = FLT_MAX;
  1205. for (size_t i = 0; i < component_count; ++i)
  1206. if (component_errors[i] > error_cutoff)
  1207. nexterror = nexterror > component_errors[i] ? component_errors[i] : nexterror;
  1208. return write;
  1209. }
  1210. struct CellHasher
  1211. {
  1212. const unsigned int* vertex_ids;
  1213. size_t hash(unsigned int i) const
  1214. {
  1215. unsigned int h = vertex_ids[i];
  1216. // MurmurHash2 finalizer
  1217. h ^= h >> 13;
  1218. h *= 0x5bd1e995;
  1219. h ^= h >> 15;
  1220. return h;
  1221. }
  1222. bool equal(unsigned int lhs, unsigned int rhs) const
  1223. {
  1224. return vertex_ids[lhs] == vertex_ids[rhs];
  1225. }
  1226. };
  1227. struct IdHasher
  1228. {
  1229. size_t hash(unsigned int id) const
  1230. {
  1231. unsigned int h = id;
  1232. // MurmurHash2 finalizer
  1233. h ^= h >> 13;
  1234. h *= 0x5bd1e995;
  1235. h ^= h >> 15;
  1236. return h;
  1237. }
  1238. bool equal(unsigned int lhs, unsigned int rhs) const
  1239. {
  1240. return lhs == rhs;
  1241. }
  1242. };
  1243. struct TriangleHasher
  1244. {
  1245. const unsigned int* indices;
  1246. size_t hash(unsigned int i) const
  1247. {
  1248. const unsigned int* tri = indices + i * 3;
  1249. // Optimized Spatial Hashing for Collision Detection of Deformable Objects
  1250. return (tri[0] * 73856093) ^ (tri[1] * 19349663) ^ (tri[2] * 83492791);
  1251. }
  1252. bool equal(unsigned int lhs, unsigned int rhs) const
  1253. {
  1254. const unsigned int* lt = indices + lhs * 3;
  1255. const unsigned int* rt = indices + rhs * 3;
  1256. return lt[0] == rt[0] && lt[1] == rt[1] && lt[2] == rt[2];
  1257. }
  1258. };
  1259. static void computeVertexIds(unsigned int* vertex_ids, const Vector3* vertex_positions, size_t vertex_count, int grid_size)
  1260. {
  1261. assert(grid_size >= 1 && grid_size <= 1024);
  1262. float cell_scale = float(grid_size - 1);
  1263. for (size_t i = 0; i < vertex_count; ++i)
  1264. {
  1265. const Vector3& v = vertex_positions[i];
  1266. int xi = int(v.x * cell_scale + 0.5f);
  1267. int yi = int(v.y * cell_scale + 0.5f);
  1268. int zi = int(v.z * cell_scale + 0.5f);
  1269. vertex_ids[i] = (xi << 20) | (yi << 10) | zi;
  1270. }
  1271. }
  1272. static size_t countTriangles(const unsigned int* vertex_ids, const unsigned int* indices, size_t index_count)
  1273. {
  1274. size_t result = 0;
  1275. for (size_t i = 0; i < index_count; i += 3)
  1276. {
  1277. unsigned int id0 = vertex_ids[indices[i + 0]];
  1278. unsigned int id1 = vertex_ids[indices[i + 1]];
  1279. unsigned int id2 = vertex_ids[indices[i + 2]];
  1280. result += (id0 != id1) & (id0 != id2) & (id1 != id2);
  1281. }
  1282. return result;
  1283. }
  1284. static size_t fillVertexCells(unsigned int* table, size_t table_size, unsigned int* vertex_cells, const unsigned int* vertex_ids, size_t vertex_count)
  1285. {
  1286. CellHasher hasher = {vertex_ids};
  1287. memset(table, -1, table_size * sizeof(unsigned int));
  1288. size_t result = 0;
  1289. for (size_t i = 0; i < vertex_count; ++i)
  1290. {
  1291. unsigned int* entry = hashLookup2(table, table_size, hasher, unsigned(i), ~0u);
  1292. if (*entry == ~0u)
  1293. {
  1294. *entry = unsigned(i);
  1295. vertex_cells[i] = unsigned(result++);
  1296. }
  1297. else
  1298. {
  1299. vertex_cells[i] = vertex_cells[*entry];
  1300. }
  1301. }
  1302. return result;
  1303. }
  1304. static size_t countVertexCells(unsigned int* table, size_t table_size, const unsigned int* vertex_ids, size_t vertex_count)
  1305. {
  1306. IdHasher hasher;
  1307. memset(table, -1, table_size * sizeof(unsigned int));
  1308. size_t result = 0;
  1309. for (size_t i = 0; i < vertex_count; ++i)
  1310. {
  1311. unsigned int id = vertex_ids[i];
  1312. unsigned int* entry = hashLookup2(table, table_size, hasher, id, ~0u);
  1313. result += (*entry == ~0u);
  1314. *entry = id;
  1315. }
  1316. return result;
  1317. }
  1318. static void fillCellQuadrics(Quadric* cell_quadrics, const unsigned int* indices, size_t index_count, const Vector3* vertex_positions, const unsigned int* vertex_cells)
  1319. {
  1320. for (size_t i = 0; i < index_count; i += 3)
  1321. {
  1322. unsigned int i0 = indices[i + 0];
  1323. unsigned int i1 = indices[i + 1];
  1324. unsigned int i2 = indices[i + 2];
  1325. unsigned int c0 = vertex_cells[i0];
  1326. unsigned int c1 = vertex_cells[i1];
  1327. unsigned int c2 = vertex_cells[i2];
  1328. int single_cell = (c0 == c1) & (c0 == c2);
  1329. Quadric Q;
  1330. quadricFromTriangle(Q, vertex_positions[i0], vertex_positions[i1], vertex_positions[i2], single_cell ? 3.f : 1.f);
  1331. if (single_cell)
  1332. {
  1333. quadricAdd(cell_quadrics[c0], Q);
  1334. }
  1335. else
  1336. {
  1337. quadricAdd(cell_quadrics[c0], Q);
  1338. quadricAdd(cell_quadrics[c1], Q);
  1339. quadricAdd(cell_quadrics[c2], Q);
  1340. }
  1341. }
  1342. }
  1343. static void fillCellReservoirs(Reservoir* cell_reservoirs, size_t cell_count, const Vector3* vertex_positions, const float* vertex_colors, size_t vertex_colors_stride, size_t vertex_count, const unsigned int* vertex_cells)
  1344. {
  1345. static const float dummy_color[] = {0.f, 0.f, 0.f};
  1346. size_t vertex_colors_stride_float = vertex_colors_stride / sizeof(float);
  1347. for (size_t i = 0; i < vertex_count; ++i)
  1348. {
  1349. unsigned int cell = vertex_cells[i];
  1350. const Vector3& v = vertex_positions[i];
  1351. Reservoir& r = cell_reservoirs[cell];
  1352. const float* color = vertex_colors ? &vertex_colors[i * vertex_colors_stride_float] : dummy_color;
  1353. r.x += v.x;
  1354. r.y += v.y;
  1355. r.z += v.z;
  1356. r.r += color[0];
  1357. r.g += color[1];
  1358. r.b += color[2];
  1359. r.w += 1.f;
  1360. }
  1361. for (size_t i = 0; i < cell_count; ++i)
  1362. {
  1363. Reservoir& r = cell_reservoirs[i];
  1364. float iw = r.w == 0.f ? 0.f : 1.f / r.w;
  1365. r.x *= iw;
  1366. r.y *= iw;
  1367. r.z *= iw;
  1368. r.r *= iw;
  1369. r.g *= iw;
  1370. r.b *= iw;
  1371. }
  1372. }
  1373. static void fillCellRemap(unsigned int* cell_remap, float* cell_errors, size_t cell_count, const unsigned int* vertex_cells, const Quadric* cell_quadrics, const Vector3* vertex_positions, size_t vertex_count)
  1374. {
  1375. memset(cell_remap, -1, cell_count * sizeof(unsigned int));
  1376. for (size_t i = 0; i < vertex_count; ++i)
  1377. {
  1378. unsigned int cell = vertex_cells[i];
  1379. float error = quadricError(cell_quadrics[cell], vertex_positions[i]);
  1380. if (cell_remap[cell] == ~0u || cell_errors[cell] > error)
  1381. {
  1382. cell_remap[cell] = unsigned(i);
  1383. cell_errors[cell] = error;
  1384. }
  1385. }
  1386. }
  1387. static void fillCellRemap(unsigned int* cell_remap, float* cell_errors, size_t cell_count, const unsigned int* vertex_cells, const Reservoir* cell_reservoirs, const Vector3* vertex_positions, const float* vertex_colors, size_t vertex_colors_stride, float color_weight, size_t vertex_count)
  1388. {
  1389. static const float dummy_color[] = {0.f, 0.f, 0.f};
  1390. size_t vertex_colors_stride_float = vertex_colors_stride / sizeof(float);
  1391. memset(cell_remap, -1, cell_count * sizeof(unsigned int));
  1392. for (size_t i = 0; i < vertex_count; ++i)
  1393. {
  1394. unsigned int cell = vertex_cells[i];
  1395. const Vector3& v = vertex_positions[i];
  1396. const Reservoir& r = cell_reservoirs[cell];
  1397. const float* color = vertex_colors ? &vertex_colors[i * vertex_colors_stride_float] : dummy_color;
  1398. float pos_error = (v.x - r.x) * (v.x - r.x) + (v.y - r.y) * (v.y - r.y) + (v.z - r.z) * (v.z - r.z);
  1399. float col_error = (color[0] - r.r) * (color[0] - r.r) + (color[1] - r.g) * (color[1] - r.g) + (color[2] - r.b) * (color[2] - r.b);
  1400. float error = pos_error + color_weight * col_error;
  1401. if (cell_remap[cell] == ~0u || cell_errors[cell] > error)
  1402. {
  1403. cell_remap[cell] = unsigned(i);
  1404. cell_errors[cell] = error;
  1405. }
  1406. }
  1407. }
  1408. static size_t filterTriangles(unsigned int* destination, unsigned int* tritable, size_t tritable_size, const unsigned int* indices, size_t index_count, const unsigned int* vertex_cells, const unsigned int* cell_remap)
  1409. {
  1410. TriangleHasher hasher = {destination};
  1411. memset(tritable, -1, tritable_size * sizeof(unsigned int));
  1412. size_t result = 0;
  1413. for (size_t i = 0; i < index_count; i += 3)
  1414. {
  1415. unsigned int c0 = vertex_cells[indices[i + 0]];
  1416. unsigned int c1 = vertex_cells[indices[i + 1]];
  1417. unsigned int c2 = vertex_cells[indices[i + 2]];
  1418. if (c0 != c1 && c0 != c2 && c1 != c2)
  1419. {
  1420. unsigned int a = cell_remap[c0];
  1421. unsigned int b = cell_remap[c1];
  1422. unsigned int c = cell_remap[c2];
  1423. if (b < a && b < c)
  1424. {
  1425. unsigned int t = a;
  1426. a = b, b = c, c = t;
  1427. }
  1428. else if (c < a && c < b)
  1429. {
  1430. unsigned int t = c;
  1431. c = b, b = a, a = t;
  1432. }
  1433. destination[result * 3 + 0] = a;
  1434. destination[result * 3 + 1] = b;
  1435. destination[result * 3 + 2] = c;
  1436. unsigned int* entry = hashLookup2(tritable, tritable_size, hasher, unsigned(result), ~0u);
  1437. if (*entry == ~0u)
  1438. *entry = unsigned(result++);
  1439. }
  1440. }
  1441. return result * 3;
  1442. }
  1443. static float interpolate(float y, float x0, float y0, float x1, float y1, float x2, float y2)
  1444. {
  1445. // three point interpolation from "revenge of interpolation search" paper
  1446. float num = (y1 - y) * (x1 - x2) * (x1 - x0) * (y2 - y0);
  1447. float den = (y2 - y) * (x1 - x2) * (y0 - y1) + (y0 - y) * (x1 - x0) * (y1 - y2);
  1448. return x1 + num / den;
  1449. }
  1450. } // namespace meshopt
  1451. // Note: this is only exposed for debug visualization purposes; do *not* use
  1452. enum
  1453. {
  1454. meshopt_SimplifyInternalDebug = 1 << 30
  1455. };
  1456. size_t meshopt_simplifyEdge(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)
  1457. {
  1458. using namespace meshopt;
  1459. assert(index_count % 3 == 0);
  1460. assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
  1461. assert(vertex_positions_stride % sizeof(float) == 0);
  1462. assert(target_index_count <= index_count);
  1463. assert(target_error >= 0);
  1464. assert((options & ~(meshopt_SimplifyLockBorder | meshopt_SimplifySparse | meshopt_SimplifyErrorAbsolute | meshopt_SimplifyPrune | meshopt_SimplifyInternalDebug)) == 0);
  1465. assert(vertex_attributes_stride >= attribute_count * sizeof(float) && vertex_attributes_stride <= 256);
  1466. assert(vertex_attributes_stride % sizeof(float) == 0);
  1467. assert(attribute_count <= kMaxAttributes);
  1468. for (size_t i = 0; i < attribute_count; ++i)
  1469. assert(attribute_weights[i] >= 0);
  1470. meshopt_Allocator allocator;
  1471. unsigned int* result = destination;
  1472. if (result != indices)
  1473. memcpy(result, indices, index_count * sizeof(unsigned int));
  1474. // build an index remap and update indices/vertex_count to minimize the subsequent work
  1475. // note: as a consequence, errors will be computed relative to the subset extent
  1476. unsigned int* sparse_remap = NULL;
  1477. if (options & meshopt_SimplifySparse)
  1478. sparse_remap = buildSparseRemap(result, index_count, vertex_count, &vertex_count, allocator);
  1479. // build adjacency information
  1480. EdgeAdjacency adjacency = {};
  1481. prepareEdgeAdjacency(adjacency, index_count, vertex_count, allocator);
  1482. updateEdgeAdjacency(adjacency, result, index_count, vertex_count, NULL);
  1483. // build position remap that maps each vertex to the one with identical position
  1484. unsigned int* remap = allocator.allocate<unsigned int>(vertex_count);
  1485. unsigned int* wedge = allocator.allocate<unsigned int>(vertex_count);
  1486. buildPositionRemap(remap, wedge, vertex_positions_data, vertex_count, vertex_positions_stride, sparse_remap, allocator);
  1487. // classify vertices; vertex kind determines collapse rules, see kCanCollapse
  1488. unsigned char* vertex_kind = allocator.allocate<unsigned char>(vertex_count);
  1489. unsigned int* loop = allocator.allocate<unsigned int>(vertex_count);
  1490. unsigned int* loopback = allocator.allocate<unsigned int>(vertex_count);
  1491. classifyVertices(vertex_kind, loop, loopback, vertex_count, adjacency, remap, wedge, vertex_lock, sparse_remap, options);
  1492. #if TRACE
  1493. size_t unique_positions = 0;
  1494. for (size_t i = 0; i < vertex_count; ++i)
  1495. unique_positions += remap[i] == i;
  1496. printf("position remap: %d vertices => %d positions\n", int(vertex_count), int(unique_positions));
  1497. size_t kinds[Kind_Count] = {};
  1498. for (size_t i = 0; i < vertex_count; ++i)
  1499. kinds[vertex_kind[i]] += remap[i] == i;
  1500. printf("kinds: manifold %d, border %d, seam %d, complex %d, locked %d\n",
  1501. int(kinds[Kind_Manifold]), int(kinds[Kind_Border]), int(kinds[Kind_Seam]), int(kinds[Kind_Complex]), int(kinds[Kind_Locked]));
  1502. #endif
  1503. Vector3* vertex_positions = allocator.allocate<Vector3>(vertex_count);
  1504. float vertex_scale = rescalePositions(vertex_positions, vertex_positions_data, vertex_count, vertex_positions_stride, sparse_remap);
  1505. float* vertex_attributes = NULL;
  1506. if (attribute_count)
  1507. {
  1508. unsigned int attribute_remap[kMaxAttributes];
  1509. // remap attributes to only include ones with weight > 0 to minimize memory/compute overhead for quadrics
  1510. size_t attributes_used = 0;
  1511. for (size_t i = 0; i < attribute_count; ++i)
  1512. if (attribute_weights[i] > 0)
  1513. attribute_remap[attributes_used++] = unsigned(i);
  1514. attribute_count = attributes_used;
  1515. vertex_attributes = allocator.allocate<float>(vertex_count * attribute_count);
  1516. rescaleAttributes(vertex_attributes, vertex_attributes_data, vertex_count, vertex_attributes_stride, attribute_weights, attribute_count, attribute_remap, sparse_remap);
  1517. }
  1518. Quadric* vertex_quadrics = allocator.allocate<Quadric>(vertex_count);
  1519. memset(vertex_quadrics, 0, vertex_count * sizeof(Quadric));
  1520. Quadric* attribute_quadrics = NULL;
  1521. QuadricGrad* attribute_gradients = NULL;
  1522. if (attribute_count)
  1523. {
  1524. attribute_quadrics = allocator.allocate<Quadric>(vertex_count);
  1525. memset(attribute_quadrics, 0, vertex_count * sizeof(Quadric));
  1526. attribute_gradients = allocator.allocate<QuadricGrad>(vertex_count * attribute_count);
  1527. memset(attribute_gradients, 0, vertex_count * attribute_count * sizeof(QuadricGrad));
  1528. }
  1529. fillFaceQuadrics(vertex_quadrics, result, index_count, vertex_positions, remap);
  1530. fillEdgeQuadrics(vertex_quadrics, result, index_count, vertex_positions, remap, vertex_kind, loop, loopback);
  1531. if (attribute_count)
  1532. fillAttributeQuadrics(attribute_quadrics, attribute_gradients, result, index_count, vertex_positions, vertex_attributes, attribute_count);
  1533. unsigned int* components = NULL;
  1534. float* component_errors = NULL;
  1535. size_t component_count = 0;
  1536. float component_nexterror = 0;
  1537. if (options & meshopt_SimplifyPrune)
  1538. {
  1539. components = allocator.allocate<unsigned int>(vertex_count);
  1540. component_count = buildComponents(components, vertex_count, result, index_count, remap);
  1541. component_errors = allocator.allocate<float>(component_count * 4); // overallocate for temporary use inside measureComponents
  1542. measureComponents(component_errors, component_count, components, vertex_positions, vertex_count);
  1543. component_nexterror = FLT_MAX;
  1544. for (size_t i = 0; i < component_count; ++i)
  1545. component_nexterror = component_nexterror > component_errors[i] ? component_errors[i] : component_nexterror;
  1546. #if TRACE
  1547. printf("components: %d (min error %e)\n", int(component_count), sqrtf(component_nexterror));
  1548. #endif
  1549. }
  1550. #if TRACE
  1551. size_t pass_count = 0;
  1552. #endif
  1553. size_t collapse_capacity = boundEdgeCollapses(adjacency, vertex_count, index_count, vertex_kind);
  1554. Collapse* edge_collapses = allocator.allocate<Collapse>(collapse_capacity);
  1555. unsigned int* collapse_order = allocator.allocate<unsigned int>(collapse_capacity);
  1556. unsigned int* collapse_remap = allocator.allocate<unsigned int>(vertex_count);
  1557. unsigned char* collapse_locked = allocator.allocate<unsigned char>(vertex_count);
  1558. size_t result_count = index_count;
  1559. float result_error = 0;
  1560. float vertex_error = 0;
  1561. // target_error input is linear; we need to adjust it to match quadricError units
  1562. float error_scale = (options & meshopt_SimplifyErrorAbsolute) ? vertex_scale : 1.f;
  1563. float error_limit = (target_error * target_error) / (error_scale * error_scale);
  1564. while (result_count > target_index_count)
  1565. {
  1566. // note: throughout the simplification process adjacency structure reflects welded topology for result-in-progress
  1567. updateEdgeAdjacency(adjacency, result, result_count, vertex_count, remap);
  1568. size_t edge_collapse_count = pickEdgeCollapses(edge_collapses, collapse_capacity, result, result_count, remap, vertex_kind, loop, loopback);
  1569. assert(edge_collapse_count <= collapse_capacity);
  1570. // no edges can be collapsed any more due to topology restrictions
  1571. if (edge_collapse_count == 0)
  1572. break;
  1573. #if TRACE
  1574. printf("pass %d:%c", int(pass_count++), TRACE >= 2 ? '\n' : ' ');
  1575. #endif
  1576. rankEdgeCollapses(edge_collapses, edge_collapse_count, vertex_positions, vertex_attributes, vertex_quadrics, attribute_quadrics, attribute_gradients, attribute_count, remap);
  1577. sortEdgeCollapses(collapse_order, edge_collapses, edge_collapse_count);
  1578. size_t triangle_collapse_goal = (result_count - target_index_count) / 3;
  1579. for (size_t i = 0; i < vertex_count; ++i)
  1580. collapse_remap[i] = unsigned(i);
  1581. memset(collapse_locked, 0, vertex_count);
  1582. size_t collapses = performEdgeCollapses(collapse_remap, collapse_locked, edge_collapses, edge_collapse_count, collapse_order, remap, wedge, vertex_kind, loop, loopback, vertex_positions, adjacency, triangle_collapse_goal, error_limit, result_error);
  1583. // no edges can be collapsed any more due to hitting the error limit or triangle collapse limit
  1584. if (collapses == 0)
  1585. break;
  1586. updateQuadrics(collapse_remap, vertex_count, vertex_quadrics, attribute_quadrics, attribute_gradients, attribute_count, vertex_positions, remap, vertex_error);
  1587. // updateQuadrics will update vertex error if we use attributes, but if we don't then result_error and vertex_error are equivalent
  1588. vertex_error = attribute_count == 0 ? result_error : vertex_error;
  1589. remapEdgeLoops(loop, vertex_count, collapse_remap);
  1590. remapEdgeLoops(loopback, vertex_count, collapse_remap);
  1591. size_t new_count = remapIndexBuffer(result, result_count, collapse_remap);
  1592. assert(new_count < result_count);
  1593. result_count = new_count;
  1594. if ((options & meshopt_SimplifyPrune) && result_count > target_index_count && component_nexterror <= vertex_error)
  1595. result_count = pruneComponents(result, result_count, components, component_errors, component_count, vertex_error, component_nexterror);
  1596. }
  1597. // we're done with the regular simplification but we're still short of the target; try pruning more aggressively towards error_limit
  1598. while ((options & meshopt_SimplifyPrune) && result_count > target_index_count && component_nexterror <= error_limit)
  1599. {
  1600. #if TRACE
  1601. printf("pass %d: cleanup; ", int(pass_count++));
  1602. #endif
  1603. float component_cutoff = component_nexterror * 1.5f < error_limit ? component_nexterror * 1.5f : error_limit;
  1604. // track maximum error in eligible components as we are increasing resulting error
  1605. float component_maxerror = 0;
  1606. for (size_t i = 0; i < component_count; ++i)
  1607. if (component_errors[i] > component_maxerror && component_errors[i] <= component_cutoff)
  1608. component_maxerror = component_errors[i];
  1609. size_t new_count = pruneComponents(result, result_count, components, component_errors, component_count, component_cutoff, component_nexterror);
  1610. if (new_count == result_count)
  1611. break;
  1612. result_count = new_count;
  1613. result_error = result_error < component_maxerror ? component_maxerror : result_error;
  1614. vertex_error = vertex_error < component_maxerror ? component_maxerror : vertex_error;
  1615. }
  1616. #if TRACE
  1617. printf("result: %d triangles, error: %e; total %d passes\n", int(result_count / 3), sqrtf(result_error), int(pass_count));
  1618. #endif
  1619. // if debug visualization data is requested, fill it instead of index data; for simplicity, this doesn't work with sparsity
  1620. if ((options & meshopt_SimplifyInternalDebug) && !sparse_remap)
  1621. {
  1622. assert(Kind_Count <= 8 && vertex_count < (1 << 28)); // 3 bit kind, 1 bit loop
  1623. for (size_t i = 0; i < result_count; i += 3)
  1624. {
  1625. unsigned int a = result[i + 0], b = result[i + 1], c = result[i + 2];
  1626. result[i + 0] |= (vertex_kind[a] << 28) | (unsigned(loop[a] == b || loopback[b] == a) << 31);
  1627. result[i + 1] |= (vertex_kind[b] << 28) | (unsigned(loop[b] == c || loopback[c] == b) << 31);
  1628. result[i + 2] |= (vertex_kind[c] << 28) | (unsigned(loop[c] == a || loopback[a] == c) << 31);
  1629. }
  1630. }
  1631. // convert resulting indices back into the dense space of the larger mesh
  1632. if (sparse_remap)
  1633. for (size_t i = 0; i < result_count; ++i)
  1634. result[i] = sparse_remap[result[i]];
  1635. // result_error is quadratic; we need to remap it back to linear
  1636. if (out_result_error)
  1637. *out_result_error = sqrtf(vertex_error) * error_scale;
  1638. return result_count;
  1639. }
  1640. 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)
  1641. {
  1642. 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);
  1643. }
  1644. 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)
  1645. {
  1646. 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);
  1647. }
  1648. 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)
  1649. {
  1650. using namespace meshopt;
  1651. assert(index_count % 3 == 0);
  1652. assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
  1653. assert(vertex_positions_stride % sizeof(float) == 0);
  1654. assert(target_index_count <= index_count);
  1655. // we expect to get ~2 triangles/vertex in the output
  1656. size_t target_cell_count = target_index_count / 6;
  1657. meshopt_Allocator allocator;
  1658. Vector3* vertex_positions = allocator.allocate<Vector3>(vertex_count);
  1659. rescalePositions(vertex_positions, vertex_positions_data, vertex_count, vertex_positions_stride);
  1660. // find the optimal grid size using guided binary search
  1661. #if TRACE
  1662. printf("source: %d vertices, %d triangles\n", int(vertex_count), int(index_count / 3));
  1663. printf("target: %d cells, %d triangles\n", int(target_cell_count), int(target_index_count / 3));
  1664. #endif
  1665. unsigned int* vertex_ids = allocator.allocate<unsigned int>(vertex_count);
  1666. const int kInterpolationPasses = 5;
  1667. // invariant: # of triangles in min_grid <= target_count
  1668. int min_grid = int(1.f / (target_error < 1e-3f ? 1e-3f : target_error));
  1669. int max_grid = 1025;
  1670. size_t min_triangles = 0;
  1671. size_t max_triangles = index_count / 3;
  1672. // 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
  1673. if (min_grid > 1)
  1674. {
  1675. computeVertexIds(vertex_ids, vertex_positions, vertex_count, min_grid);
  1676. min_triangles = countTriangles(vertex_ids, indices, index_count);
  1677. }
  1678. // instead of starting in the middle, let's guess as to what the answer might be! triangle count usually grows as a square of grid size...
  1679. int next_grid_size = int(sqrtf(float(target_cell_count)) + 0.5f);
  1680. for (int pass = 0; pass < 10 + kInterpolationPasses; ++pass)
  1681. {
  1682. if (min_triangles >= target_index_count / 3 || max_grid - min_grid <= 1)
  1683. break;
  1684. // we clamp the prediction of the grid size to make sure that the search converges
  1685. int grid_size = next_grid_size;
  1686. grid_size = (grid_size <= min_grid) ? min_grid + 1 : (grid_size >= max_grid ? max_grid - 1 : grid_size);
  1687. computeVertexIds(vertex_ids, vertex_positions, vertex_count, grid_size);
  1688. size_t triangles = countTriangles(vertex_ids, indices, index_count);
  1689. #if TRACE
  1690. printf("pass %d (%s): grid size %d, triangles %d, %s\n",
  1691. pass, (pass == 0) ? "guess" : (pass <= kInterpolationPasses ? "lerp" : "binary"),
  1692. grid_size, int(triangles),
  1693. (triangles <= target_index_count / 3) ? "under" : "over");
  1694. #endif
  1695. float tip = interpolate(float(size_t(target_index_count / 3)), float(min_grid), float(min_triangles), float(grid_size), float(triangles), float(max_grid), float(max_triangles));
  1696. if (triangles <= target_index_count / 3)
  1697. {
  1698. min_grid = grid_size;
  1699. min_triangles = triangles;
  1700. }
  1701. else
  1702. {
  1703. max_grid = grid_size;
  1704. max_triangles = triangles;
  1705. }
  1706. // we start by using interpolation search - it usually converges faster
  1707. // however, interpolation search has a worst case of O(N) so we switch to binary search after a few iterations which converges in O(logN)
  1708. next_grid_size = (pass < kInterpolationPasses) ? int(tip + 0.5f) : (min_grid + max_grid) / 2;
  1709. }
  1710. if (min_triangles == 0)
  1711. {
  1712. if (out_result_error)
  1713. *out_result_error = 1.f;
  1714. return 0;
  1715. }
  1716. // build vertex->cell association by mapping all vertices with the same quantized position to the same cell
  1717. size_t table_size = hashBuckets2(vertex_count);
  1718. unsigned int* table = allocator.allocate<unsigned int>(table_size);
  1719. unsigned int* vertex_cells = allocator.allocate<unsigned int>(vertex_count);
  1720. computeVertexIds(vertex_ids, vertex_positions, vertex_count, min_grid);
  1721. size_t cell_count = fillVertexCells(table, table_size, vertex_cells, vertex_ids, vertex_count);
  1722. // build a quadric for each target cell
  1723. Quadric* cell_quadrics = allocator.allocate<Quadric>(cell_count);
  1724. memset(cell_quadrics, 0, cell_count * sizeof(Quadric));
  1725. fillCellQuadrics(cell_quadrics, indices, index_count, vertex_positions, vertex_cells);
  1726. // for each target cell, find the vertex with the minimal error
  1727. unsigned int* cell_remap = allocator.allocate<unsigned int>(cell_count);
  1728. float* cell_errors = allocator.allocate<float>(cell_count);
  1729. fillCellRemap(cell_remap, cell_errors, cell_count, vertex_cells, cell_quadrics, vertex_positions, vertex_count);
  1730. // compute error
  1731. float result_error = 0.f;
  1732. for (size_t i = 0; i < cell_count; ++i)
  1733. result_error = result_error < cell_errors[i] ? cell_errors[i] : result_error;
  1734. // collapse triangles!
  1735. // note that we need to filter out triangles that we've already output because we very frequently generate redundant triangles between cells :(
  1736. size_t tritable_size = hashBuckets2(min_triangles);
  1737. unsigned int* tritable = allocator.allocate<unsigned int>(tritable_size);
  1738. size_t write = filterTriangles(destination, tritable, tritable_size, indices, index_count, vertex_cells, cell_remap);
  1739. #if TRACE
  1740. printf("result: %d cells, %d triangles (%d unfiltered), error %e\n", int(cell_count), int(write / 3), int(min_triangles), sqrtf(result_error));
  1741. #endif
  1742. if (out_result_error)
  1743. *out_result_error = sqrtf(result_error);
  1744. return write;
  1745. }
  1746. size_t meshopt_simplifyPoints(unsigned int* destination, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, const float* vertex_colors, size_t vertex_colors_stride, float color_weight, size_t target_vertex_count)
  1747. {
  1748. using namespace meshopt;
  1749. assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
  1750. assert(vertex_positions_stride % sizeof(float) == 0);
  1751. assert(vertex_colors_stride == 0 || (vertex_colors_stride >= 12 && vertex_colors_stride <= 256));
  1752. assert(vertex_colors_stride % sizeof(float) == 0);
  1753. assert(vertex_colors == NULL || vertex_colors_stride != 0);
  1754. assert(target_vertex_count <= vertex_count);
  1755. size_t target_cell_count = target_vertex_count;
  1756. if (target_cell_count == 0)
  1757. return 0;
  1758. meshopt_Allocator allocator;
  1759. Vector3* vertex_positions = allocator.allocate<Vector3>(vertex_count);
  1760. rescalePositions(vertex_positions, vertex_positions_data, vertex_count, vertex_positions_stride);
  1761. // find the optimal grid size using guided binary search
  1762. #if TRACE
  1763. printf("source: %d vertices\n", int(vertex_count));
  1764. printf("target: %d cells\n", int(target_cell_count));
  1765. #endif
  1766. unsigned int* vertex_ids = allocator.allocate<unsigned int>(vertex_count);
  1767. size_t table_size = hashBuckets2(vertex_count);
  1768. unsigned int* table = allocator.allocate<unsigned int>(table_size);
  1769. const int kInterpolationPasses = 5;
  1770. // invariant: # of vertices in min_grid <= target_count
  1771. int min_grid = 0;
  1772. int max_grid = 1025;
  1773. size_t min_vertices = 0;
  1774. size_t max_vertices = vertex_count;
  1775. // instead of starting in the middle, let's guess as to what the answer might be! triangle count usually grows as a square of grid size...
  1776. int next_grid_size = int(sqrtf(float(target_cell_count)) + 0.5f);
  1777. for (int pass = 0; pass < 10 + kInterpolationPasses; ++pass)
  1778. {
  1779. assert(min_vertices < target_vertex_count);
  1780. assert(max_grid - min_grid > 1);
  1781. // we clamp the prediction of the grid size to make sure that the search converges
  1782. int grid_size = next_grid_size;
  1783. grid_size = (grid_size <= min_grid) ? min_grid + 1 : (grid_size >= max_grid ? max_grid - 1 : grid_size);
  1784. computeVertexIds(vertex_ids, vertex_positions, vertex_count, grid_size);
  1785. size_t vertices = countVertexCells(table, table_size, vertex_ids, vertex_count);
  1786. #if TRACE
  1787. printf("pass %d (%s): grid size %d, vertices %d, %s\n",
  1788. pass, (pass == 0) ? "guess" : (pass <= kInterpolationPasses ? "lerp" : "binary"),
  1789. grid_size, int(vertices),
  1790. (vertices <= target_vertex_count) ? "under" : "over");
  1791. #endif
  1792. float tip = interpolate(float(target_vertex_count), float(min_grid), float(min_vertices), float(grid_size), float(vertices), float(max_grid), float(max_vertices));
  1793. if (vertices <= target_vertex_count)
  1794. {
  1795. min_grid = grid_size;
  1796. min_vertices = vertices;
  1797. }
  1798. else
  1799. {
  1800. max_grid = grid_size;
  1801. max_vertices = vertices;
  1802. }
  1803. if (vertices == target_vertex_count || max_grid - min_grid <= 1)
  1804. break;
  1805. // we start by using interpolation search - it usually converges faster
  1806. // however, interpolation search has a worst case of O(N) so we switch to binary search after a few iterations which converges in O(logN)
  1807. next_grid_size = (pass < kInterpolationPasses) ? int(tip + 0.5f) : (min_grid + max_grid) / 2;
  1808. }
  1809. if (min_vertices == 0)
  1810. return 0;
  1811. // build vertex->cell association by mapping all vertices with the same quantized position to the same cell
  1812. unsigned int* vertex_cells = allocator.allocate<unsigned int>(vertex_count);
  1813. computeVertexIds(vertex_ids, vertex_positions, vertex_count, min_grid);
  1814. size_t cell_count = fillVertexCells(table, table_size, vertex_cells, vertex_ids, vertex_count);
  1815. // accumulate points into a reservoir for each target cell
  1816. Reservoir* cell_reservoirs = allocator.allocate<Reservoir>(cell_count);
  1817. memset(cell_reservoirs, 0, cell_count * sizeof(Reservoir));
  1818. fillCellReservoirs(cell_reservoirs, cell_count, vertex_positions, vertex_colors, vertex_colors_stride, vertex_count, vertex_cells);
  1819. // for each target cell, find the vertex with the minimal error
  1820. unsigned int* cell_remap = allocator.allocate<unsigned int>(cell_count);
  1821. float* cell_errors = allocator.allocate<float>(cell_count);
  1822. // we scale the color weight to bring it to the same scale as position so that error addition makes sense
  1823. float color_weight_scaled = color_weight * (min_grid == 1 ? 1.f : 1.f / (min_grid - 1));
  1824. fillCellRemap(cell_remap, cell_errors, cell_count, vertex_cells, cell_reservoirs, vertex_positions, vertex_colors, vertex_colors_stride, color_weight_scaled * color_weight_scaled, vertex_count);
  1825. // copy results to the output
  1826. assert(cell_count <= target_vertex_count);
  1827. memcpy(destination, cell_remap, sizeof(unsigned int) * cell_count);
  1828. #if TRACE
  1829. // compute error
  1830. float result_error = 0.f;
  1831. for (size_t i = 0; i < cell_count; ++i)
  1832. result_error = result_error < cell_errors[i] ? cell_errors[i] : result_error;
  1833. printf("result: %d cells, %e error\n", int(cell_count), sqrtf(result_error));
  1834. #endif
  1835. return cell_count;
  1836. }
  1837. float meshopt_simplifyScale(const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride)
  1838. {
  1839. using namespace meshopt;
  1840. assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
  1841. assert(vertex_positions_stride % sizeof(float) == 0);
  1842. float extent = rescalePositions(NULL, vertex_positions, vertex_count, vertex_positions_stride);
  1843. return extent;
  1844. }