simplifier.cpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908
  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. size_t hash(unsigned int index) const
  93. {
  94. const unsigned int* key = reinterpret_cast<const unsigned int*>(vertex_positions + index * vertex_stride_float);
  95. // scramble bits to make sure that integer coordinates have entropy in lower bits
  96. unsigned int x = key[0] ^ (key[0] >> 17);
  97. unsigned int y = key[1] ^ (key[1] >> 17);
  98. unsigned int z = key[2] ^ (key[2] >> 17);
  99. // Optimized Spatial Hashing for Collision Detection of Deformable Objects
  100. return (x * 73856093) ^ (y * 19349663) ^ (z * 83492791);
  101. }
  102. bool equal(unsigned int lhs, unsigned int rhs) const
  103. {
  104. return memcmp(vertex_positions + lhs * vertex_stride_float, vertex_positions + rhs * vertex_stride_float, sizeof(float) * 3) == 0;
  105. }
  106. };
  107. static size_t hashBuckets2(size_t count)
  108. {
  109. size_t buckets = 1;
  110. while (buckets < count + count / 4)
  111. buckets *= 2;
  112. return buckets;
  113. }
  114. template <typename T, typename Hash>
  115. static T* hashLookup2(T* table, size_t buckets, const Hash& hash, const T& key, const T& empty)
  116. {
  117. assert(buckets > 0);
  118. assert((buckets & (buckets - 1)) == 0);
  119. size_t hashmod = buckets - 1;
  120. size_t bucket = hash.hash(key) & hashmod;
  121. for (size_t probe = 0; probe <= hashmod; ++probe)
  122. {
  123. T& item = table[bucket];
  124. if (item == empty)
  125. return &item;
  126. if (hash.equal(item, key))
  127. return &item;
  128. // hash collision, quadratic probing
  129. bucket = (bucket + probe + 1) & hashmod;
  130. }
  131. assert(false && "Hash table is full"); // unreachable
  132. return NULL;
  133. }
  134. static void buildPositionRemap(unsigned int* remap, unsigned int* wedge, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, meshopt_Allocator& allocator)
  135. {
  136. PositionHasher hasher = {vertex_positions_data, vertex_positions_stride / sizeof(float)};
  137. size_t table_size = hashBuckets2(vertex_count);
  138. unsigned int* table = allocator.allocate<unsigned int>(table_size);
  139. memset(table, -1, table_size * sizeof(unsigned int));
  140. // build forward remap: for each vertex, which other (canonical) vertex does it map to?
  141. // we use position equivalence for this, and remap vertices to other existing vertices
  142. for (size_t i = 0; i < vertex_count; ++i)
  143. {
  144. unsigned int index = unsigned(i);
  145. unsigned int* entry = hashLookup2(table, table_size, hasher, index, ~0u);
  146. if (*entry == ~0u)
  147. *entry = index;
  148. remap[index] = *entry;
  149. }
  150. // build wedge table: for each vertex, which other vertex is the next wedge that also maps to the same vertex?
  151. // entries in table form a (cyclic) wedge loop per vertex; for manifold vertices, wedge[i] == remap[i] == i
  152. for (size_t i = 0; i < vertex_count; ++i)
  153. wedge[i] = unsigned(i);
  154. for (size_t i = 0; i < vertex_count; ++i)
  155. if (remap[i] != i)
  156. {
  157. unsigned int r = remap[i];
  158. wedge[i] = wedge[r];
  159. wedge[r] = unsigned(i);
  160. }
  161. allocator.deallocate(table);
  162. }
  163. enum VertexKind
  164. {
  165. Kind_Manifold, // not on an attribute seam, not on any boundary
  166. Kind_Border, // not on an attribute seam, has exactly two open edges
  167. Kind_Seam, // on an attribute seam with exactly two attribute seam edges
  168. Kind_Complex, // none of the above; these vertices can move as long as all wedges move to the target vertex
  169. Kind_Locked, // none of the above; these vertices can't move
  170. Kind_Count
  171. };
  172. // manifold vertices can collapse onto anything
  173. // border/seam vertices can only be collapsed onto border/seam respectively
  174. // complex vertices can collapse onto complex/locked
  175. // a rule of thumb is that collapsing kind A into kind B preserves the kind B in the target vertex
  176. // for example, while we could collapse Complex into Manifold, this would mean the target vertex isn't Manifold anymore
  177. const unsigned char kCanCollapse[Kind_Count][Kind_Count] = {
  178. {1, 1, 1, 1, 1},
  179. {0, 1, 0, 0, 0},
  180. {0, 0, 1, 0, 0},
  181. {0, 0, 0, 1, 1},
  182. {0, 0, 0, 0, 0},
  183. };
  184. // if a vertex is manifold or seam, adjoining edges are guaranteed to have an opposite edge
  185. // note that for seam edges, the opposite edge isn't present in the attribute-based topology
  186. // but is present if you consider a position-only mesh variant
  187. const unsigned char kHasOpposite[Kind_Count][Kind_Count] = {
  188. {1, 1, 1, 0, 1},
  189. {1, 0, 1, 0, 0},
  190. {1, 1, 1, 0, 1},
  191. {0, 0, 0, 0, 0},
  192. {1, 0, 1, 0, 0},
  193. };
  194. static bool hasEdge(const EdgeAdjacency& adjacency, unsigned int a, unsigned int b)
  195. {
  196. unsigned int count = adjacency.offsets[a + 1] - adjacency.offsets[a];
  197. const EdgeAdjacency::Edge* edges = adjacency.data + adjacency.offsets[a];
  198. for (size_t i = 0; i < count; ++i)
  199. if (edges[i].next == b)
  200. return true;
  201. return false;
  202. }
  203. 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, unsigned int options)
  204. {
  205. memset(loop, -1, vertex_count * sizeof(unsigned int));
  206. memset(loopback, -1, vertex_count * sizeof(unsigned int));
  207. // incoming & outgoing open edges: ~0u if no open edges, i if there are more than 1
  208. // note that this is the same data as required in loop[] arrays; loop[] data is only valid for border/seam
  209. // but here it's okay to fill the data out for other types of vertices as well
  210. unsigned int* openinc = loopback;
  211. unsigned int* openout = loop;
  212. for (size_t i = 0; i < vertex_count; ++i)
  213. {
  214. unsigned int vertex = unsigned(i);
  215. unsigned int count = adjacency.offsets[vertex + 1] - adjacency.offsets[vertex];
  216. const EdgeAdjacency::Edge* edges = adjacency.data + adjacency.offsets[vertex];
  217. for (size_t j = 0; j < count; ++j)
  218. {
  219. unsigned int target = edges[j].next;
  220. if (target == vertex)
  221. {
  222. // degenerate triangles have two distinct edges instead of three, and the self edge
  223. // is bi-directional by definition; this can break border/seam classification by "closing"
  224. // the open edge from another triangle and falsely marking the vertex as manifold
  225. // instead we mark the vertex as having >1 open edges which turns it into locked/complex
  226. openinc[vertex] = openout[vertex] = vertex;
  227. }
  228. else if (!hasEdge(adjacency, target, vertex))
  229. {
  230. openinc[target] = (openinc[target] == ~0u) ? vertex : target;
  231. openout[vertex] = (openout[vertex] == ~0u) ? target : vertex;
  232. }
  233. }
  234. }
  235. #if TRACE
  236. size_t stats[4] = {};
  237. #endif
  238. for (size_t i = 0; i < vertex_count; ++i)
  239. {
  240. if (remap[i] == i)
  241. {
  242. if (wedge[i] == i)
  243. {
  244. // no attribute seam, need to check if it's manifold
  245. unsigned int openi = openinc[i], openo = openout[i];
  246. // note: we classify any vertices with no open edges as manifold
  247. // this is technically incorrect - if 4 triangles share an edge, we'll classify vertices as manifold
  248. // it's unclear if this is a problem in practice
  249. if (openi == ~0u && openo == ~0u)
  250. {
  251. result[i] = Kind_Manifold;
  252. }
  253. else if (openi != i && openo != i)
  254. {
  255. result[i] = Kind_Border;
  256. }
  257. else
  258. {
  259. result[i] = Kind_Locked;
  260. TRACESTATS(0);
  261. }
  262. }
  263. else if (wedge[wedge[i]] == i)
  264. {
  265. // attribute seam; need to distinguish between Seam and Locked
  266. unsigned int w = wedge[i];
  267. unsigned int openiv = openinc[i], openov = openout[i];
  268. unsigned int openiw = openinc[w], openow = openout[w];
  269. // seam should have one open half-edge for each vertex, and the edges need to "connect" - point to the same vertex post-remap
  270. if (openiv != ~0u && openiv != i && openov != ~0u && openov != i &&
  271. openiw != ~0u && openiw != w && openow != ~0u && openow != w)
  272. {
  273. if (remap[openiv] == remap[openow] && remap[openov] == remap[openiw])
  274. {
  275. result[i] = Kind_Seam;
  276. }
  277. else
  278. {
  279. result[i] = Kind_Locked;
  280. TRACESTATS(1);
  281. }
  282. }
  283. else
  284. {
  285. result[i] = Kind_Locked;
  286. TRACESTATS(2);
  287. }
  288. }
  289. else
  290. {
  291. // more than one vertex maps to this one; we don't have classification available
  292. result[i] = Kind_Locked;
  293. TRACESTATS(3);
  294. }
  295. }
  296. else
  297. {
  298. assert(remap[i] < i);
  299. result[i] = result[remap[i]];
  300. }
  301. }
  302. if (options & meshopt_SimplifyLockBorder)
  303. for (size_t i = 0; i < vertex_count; ++i)
  304. if (result[i] == Kind_Border)
  305. result[i] = Kind_Locked;
  306. #if TRACE
  307. printf("locked: many open edges %d, disconnected seam %d, many seam edges %d, many wedges %d\n",
  308. int(stats[0]), int(stats[1]), int(stats[2]), int(stats[3]));
  309. #endif
  310. }
  311. struct Vector3
  312. {
  313. float x, y, z;
  314. };
  315. static float rescalePositions(Vector3* result, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride)
  316. {
  317. size_t vertex_stride_float = vertex_positions_stride / sizeof(float);
  318. float minv[3] = {FLT_MAX, FLT_MAX, FLT_MAX};
  319. float maxv[3] = {-FLT_MAX, -FLT_MAX, -FLT_MAX};
  320. for (size_t i = 0; i < vertex_count; ++i)
  321. {
  322. const float* v = vertex_positions_data + i * vertex_stride_float;
  323. if (result)
  324. {
  325. result[i].x = v[0];
  326. result[i].y = v[1];
  327. result[i].z = v[2];
  328. }
  329. for (int j = 0; j < 3; ++j)
  330. {
  331. float vj = v[j];
  332. minv[j] = minv[j] > vj ? vj : minv[j];
  333. maxv[j] = maxv[j] < vj ? vj : maxv[j];
  334. }
  335. }
  336. float extent = 0.f;
  337. extent = (maxv[0] - minv[0]) < extent ? extent : (maxv[0] - minv[0]);
  338. extent = (maxv[1] - minv[1]) < extent ? extent : (maxv[1] - minv[1]);
  339. extent = (maxv[2] - minv[2]) < extent ? extent : (maxv[2] - minv[2]);
  340. if (result)
  341. {
  342. float scale = extent == 0 ? 0.f : 1.f / extent;
  343. for (size_t i = 0; i < vertex_count; ++i)
  344. {
  345. result[i].x = (result[i].x - minv[0]) * scale;
  346. result[i].y = (result[i].y - minv[1]) * scale;
  347. result[i].z = (result[i].z - minv[2]) * scale;
  348. }
  349. }
  350. return extent;
  351. }
  352. 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)
  353. {
  354. size_t vertex_attributes_stride_float = vertex_attributes_stride / sizeof(float);
  355. for (size_t i = 0; i < vertex_count; ++i)
  356. {
  357. for (size_t k = 0; k < attribute_count; ++k)
  358. {
  359. float a = vertex_attributes_data[i * vertex_attributes_stride_float + k];
  360. result[i * attribute_count + k] = a * attribute_weights[k];
  361. }
  362. }
  363. }
  364. static const size_t kMaxAttributes = 16;
  365. struct Quadric
  366. {
  367. // a00*x^2 + a11*y^2 + a22*z^2 + 2*(a10*xy + a20*xz + a21*yz) + b0*x + b1*y + b2*z + c
  368. float a00, a11, a22;
  369. float a10, a20, a21;
  370. float b0, b1, b2, c;
  371. float w;
  372. };
  373. struct QuadricGrad
  374. {
  375. // gx*x + gy*y + gz*z + gw
  376. float gx, gy, gz, gw;
  377. };
  378. struct Reservoir
  379. {
  380. float x, y, z;
  381. float r, g, b;
  382. float w;
  383. };
  384. struct Collapse
  385. {
  386. unsigned int v0;
  387. unsigned int v1;
  388. union
  389. {
  390. unsigned int bidi;
  391. float error;
  392. unsigned int errorui;
  393. };
  394. };
  395. static float normalize(Vector3& v)
  396. {
  397. float length = sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
  398. if (length > 0)
  399. {
  400. v.x /= length;
  401. v.y /= length;
  402. v.z /= length;
  403. }
  404. return length;
  405. }
  406. static void quadricAdd(Quadric& Q, const Quadric& R)
  407. {
  408. Q.a00 += R.a00;
  409. Q.a11 += R.a11;
  410. Q.a22 += R.a22;
  411. Q.a10 += R.a10;
  412. Q.a20 += R.a20;
  413. Q.a21 += R.a21;
  414. Q.b0 += R.b0;
  415. Q.b1 += R.b1;
  416. Q.b2 += R.b2;
  417. Q.c += R.c;
  418. Q.w += R.w;
  419. }
  420. static void quadricAdd(QuadricGrad* G, const QuadricGrad* R, size_t attribute_count)
  421. {
  422. for (size_t k = 0; k < attribute_count; ++k)
  423. {
  424. G[k].gx += R[k].gx;
  425. G[k].gy += R[k].gy;
  426. G[k].gz += R[k].gz;
  427. G[k].gw += R[k].gw;
  428. }
  429. }
  430. static float quadricError(const Quadric& Q, const Vector3& v)
  431. {
  432. float rx = Q.b0;
  433. float ry = Q.b1;
  434. float rz = Q.b2;
  435. rx += Q.a10 * v.y;
  436. ry += Q.a21 * v.z;
  437. rz += Q.a20 * v.x;
  438. rx *= 2;
  439. ry *= 2;
  440. rz *= 2;
  441. rx += Q.a00 * v.x;
  442. ry += Q.a11 * v.y;
  443. rz += Q.a22 * v.z;
  444. float r = Q.c;
  445. r += rx * v.x;
  446. r += ry * v.y;
  447. r += rz * v.z;
  448. float s = Q.w == 0.f ? 0.f : 1.f / Q.w;
  449. return fabsf(r) * s;
  450. }
  451. static float quadricError(const Quadric& Q, const QuadricGrad* G, size_t attribute_count, const Vector3& v, const float* va)
  452. {
  453. float rx = Q.b0;
  454. float ry = Q.b1;
  455. float rz = Q.b2;
  456. rx += Q.a10 * v.y;
  457. ry += Q.a21 * v.z;
  458. rz += Q.a20 * v.x;
  459. rx *= 2;
  460. ry *= 2;
  461. rz *= 2;
  462. rx += Q.a00 * v.x;
  463. ry += Q.a11 * v.y;
  464. rz += Q.a22 * v.z;
  465. float r = Q.c;
  466. r += rx * v.x;
  467. r += ry * v.y;
  468. r += rz * v.z;
  469. // see quadricFromAttributes for general derivation; here we need to add the parts of (eval(pos) - attr)^2 that depend on attr
  470. for (size_t k = 0; k < attribute_count; ++k)
  471. {
  472. float a = va[k];
  473. float g = v.x * G[k].gx + v.y * G[k].gy + v.z * G[k].gz + G[k].gw;
  474. r += a * a * Q.w;
  475. r -= 2 * a * g;
  476. }
  477. // TODO: weight normalization is breaking attribute error somehow
  478. float s = 1;// Q.w == 0.f ? 0.f : 1.f / Q.w;
  479. return fabsf(r) * s;
  480. }
  481. static void quadricFromPlane(Quadric& Q, float a, float b, float c, float d, float w)
  482. {
  483. float aw = a * w;
  484. float bw = b * w;
  485. float cw = c * w;
  486. float dw = d * w;
  487. Q.a00 = a * aw;
  488. Q.a11 = b * bw;
  489. Q.a22 = c * cw;
  490. Q.a10 = a * bw;
  491. Q.a20 = a * cw;
  492. Q.a21 = b * cw;
  493. Q.b0 = a * dw;
  494. Q.b1 = b * dw;
  495. Q.b2 = c * dw;
  496. Q.c = d * dw;
  497. Q.w = w;
  498. }
  499. static void quadricFromTriangle(Quadric& Q, const Vector3& p0, const Vector3& p1, const Vector3& p2, float weight)
  500. {
  501. Vector3 p10 = {p1.x - p0.x, p1.y - p0.y, p1.z - p0.z};
  502. Vector3 p20 = {p2.x - p0.x, p2.y - p0.y, p2.z - p0.z};
  503. // normal = cross(p1 - p0, p2 - p0)
  504. 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};
  505. float area = normalize(normal);
  506. float distance = normal.x * p0.x + normal.y * p0.y + normal.z * p0.z;
  507. // we use sqrtf(area) so that the error is scaled linearly; this tends to improve silhouettes
  508. quadricFromPlane(Q, normal.x, normal.y, normal.z, -distance, sqrtf(area) * weight);
  509. }
  510. static void quadricFromTriangleEdge(Quadric& Q, const Vector3& p0, const Vector3& p1, const Vector3& p2, float weight)
  511. {
  512. Vector3 p10 = {p1.x - p0.x, p1.y - p0.y, p1.z - p0.z};
  513. float length = normalize(p10);
  514. // p20p = length of projection of p2-p0 onto normalize(p1 - p0)
  515. Vector3 p20 = {p2.x - p0.x, p2.y - p0.y, p2.z - p0.z};
  516. float p20p = p20.x * p10.x + p20.y * p10.y + p20.z * p10.z;
  517. // normal = altitude of triangle from point p2 onto edge p1-p0
  518. Vector3 normal = {p20.x - p10.x * p20p, p20.y - p10.y * p20p, p20.z - p10.z * p20p};
  519. normalize(normal);
  520. float distance = normal.x * p0.x + normal.y * p0.y + normal.z * p0.z;
  521. // note: the weight is scaled linearly with edge length; this has to match the triangle weight
  522. quadricFromPlane(Q, normal.x, normal.y, normal.z, -distance, length * weight);
  523. }
  524. 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)
  525. {
  526. // for each attribute we want to encode the following function into the quadric:
  527. // (eval(pos) - attr)^2
  528. // where eval(pos) interpolates attribute across the triangle like so:
  529. // eval(pos) = pos.x * gx + pos.y * gy + pos.z * gz + gw
  530. // where gx/gy/gz/gw are gradients
  531. Vector3 p10 = {p1.x - p0.x, p1.y - p0.y, p1.z - p0.z};
  532. Vector3 p20 = {p2.x - p0.x, p2.y - p0.y, p2.z - p0.z};
  533. // weight is scaled linearly with edge length
  534. 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};
  535. float area = sqrtf(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z);
  536. float w = sqrtf(area); // TODO this needs more experimentation
  537. // we compute gradients using barycentric coordinates; barycentric coordinates can be computed as follows:
  538. // v = (d11 * d20 - d01 * d21) / denom
  539. // w = (d00 * d21 - d01 * d20) / denom
  540. // u = 1 - v - w
  541. // here v0, v1 are triangle edge vectors, v2 is a vector from point to triangle corner, and dij = dot(vi, vj)
  542. const Vector3& v0 = p10;
  543. const Vector3& v1 = p20;
  544. float d00 = v0.x * v0.x + v0.y * v0.y + v0.z * v0.z;
  545. float d01 = v0.x * v1.x + v0.y * v1.y + v0.z * v1.z;
  546. float d11 = v1.x * v1.x + v1.y * v1.y + v1.z * v1.z;
  547. float denom = d00 * d11 - d01 * d01;
  548. float denomr = denom == 0 ? 0.f : 1.f / denom;
  549. // precompute gradient factors
  550. // these are derived by directly computing derivative of eval(pos) = a0 * u + a1 * v + a2 * w and factoring out common factors that are shared between attributes
  551. float gx1 = (d11 * v0.x - d01 * v1.x) * denomr;
  552. float gx2 = (d00 * v1.x - d01 * v0.x) * denomr;
  553. float gy1 = (d11 * v0.y - d01 * v1.y) * denomr;
  554. float gy2 = (d00 * v1.y - d01 * v0.y) * denomr;
  555. float gz1 = (d11 * v0.z - d01 * v1.z) * denomr;
  556. float gz2 = (d00 * v1.z - d01 * v0.z) * denomr;
  557. memset(&Q, 0, sizeof(Quadric));
  558. Q.w = w;
  559. for (size_t k = 0; k < attribute_count; ++k)
  560. {
  561. float a0 = va0[k], a1 = va1[k], a2 = va2[k];
  562. // compute gradient of eval(pos) for x/y/z/w
  563. // the formulas below are obtained by directly computing derivative of eval(pos) = a0 * u + a1 * v + a2 * w
  564. float gx = gx1 * (a1 - a0) + gx2 * (a2 - a0);
  565. float gy = gy1 * (a1 - a0) + gy2 * (a2 - a0);
  566. float gz = gz1 * (a1 - a0) + gz2 * (a2 - a0);
  567. float gw = a0 - p0.x * gx - p0.y * gy - p0.z * gz;
  568. // quadric encodes (eval(pos)-attr)^2; this means that the resulting expansion needs to compute, for example, pos.x * pos.y * K
  569. // since quadrics already encode factors for pos.x * pos.y, we can accumulate almost everything in basic quadric fields
  570. Q.a00 += w * (gx * gx);
  571. Q.a11 += w * (gy * gy);
  572. Q.a22 += w * (gz * gz);
  573. Q.a10 += w * (gy * gx);
  574. Q.a20 += w * (gz * gx);
  575. Q.a21 += w * (gz * gy);
  576. Q.b0 += w * (gx * gw);
  577. Q.b1 += w * (gy * gw);
  578. Q.b2 += w * (gz * gw);
  579. Q.c += w * (gw * gw);
  580. // the only remaining sum components are ones that depend on attr; these will be addded during error evaluation, see quadricError
  581. G[k].gx = w * gx;
  582. G[k].gy = w * gy;
  583. G[k].gz = w * gz;
  584. G[k].gw = w * gw;
  585. }
  586. }
  587. static void fillFaceQuadrics(Quadric* vertex_quadrics, const unsigned int* indices, size_t index_count, const Vector3* vertex_positions, const unsigned int* remap)
  588. {
  589. for (size_t i = 0; i < index_count; i += 3)
  590. {
  591. unsigned int i0 = indices[i + 0];
  592. unsigned int i1 = indices[i + 1];
  593. unsigned int i2 = indices[i + 2];
  594. Quadric Q;
  595. quadricFromTriangle(Q, vertex_positions[i0], vertex_positions[i1], vertex_positions[i2], 1.f);
  596. quadricAdd(vertex_quadrics[remap[i0]], Q);
  597. quadricAdd(vertex_quadrics[remap[i1]], Q);
  598. quadricAdd(vertex_quadrics[remap[i2]], Q);
  599. }
  600. }
  601. 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)
  602. {
  603. for (size_t i = 0; i < index_count; i += 3)
  604. {
  605. static const int next[4] = {1, 2, 0, 1};
  606. for (int e = 0; e < 3; ++e)
  607. {
  608. unsigned int i0 = indices[i + e];
  609. unsigned int i1 = indices[i + next[e]];
  610. unsigned char k0 = vertex_kind[i0];
  611. unsigned char k1 = vertex_kind[i1];
  612. // check that either i0 or i1 are border/seam and are on the same edge loop
  613. // note that we need to add the error even for edged that connect e.g. border & locked
  614. // if we don't do that, the adjacent border->border edge won't have correct errors for corners
  615. if (k0 != Kind_Border && k0 != Kind_Seam && k1 != Kind_Border && k1 != Kind_Seam)
  616. continue;
  617. if ((k0 == Kind_Border || k0 == Kind_Seam) && loop[i0] != i1)
  618. continue;
  619. if ((k1 == Kind_Border || k1 == Kind_Seam) && loopback[i1] != i0)
  620. continue;
  621. // seam edges should occur twice (i0->i1 and i1->i0) - skip redundant edges
  622. if (kHasOpposite[k0][k1] && remap[i1] > remap[i0])
  623. continue;
  624. unsigned int i2 = indices[i + next[e + 1]];
  625. // we try hard to maintain border edge geometry; seam edges can move more freely
  626. // due to topological restrictions on collapses, seam quadrics slightly improves collapse structure but aren't critical
  627. const float kEdgeWeightSeam = 1.f;
  628. const float kEdgeWeightBorder = 10.f;
  629. float edgeWeight = (k0 == Kind_Border || k1 == Kind_Border) ? kEdgeWeightBorder : kEdgeWeightSeam;
  630. Quadric Q;
  631. quadricFromTriangleEdge(Q, vertex_positions[i0], vertex_positions[i1], vertex_positions[i2], edgeWeight);
  632. quadricAdd(vertex_quadrics[remap[i0]], Q);
  633. quadricAdd(vertex_quadrics[remap[i1]], Q);
  634. }
  635. }
  636. }
  637. 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, const unsigned int* remap)
  638. {
  639. for (size_t i = 0; i < index_count; i += 3)
  640. {
  641. unsigned int i0 = indices[i + 0];
  642. unsigned int i1 = indices[i + 1];
  643. unsigned int i2 = indices[i + 2];
  644. Quadric QA;
  645. QuadricGrad G[kMaxAttributes];
  646. 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);
  647. // TODO: This blends together attribute weights across attribute discontinuities, which is probably not a great idea
  648. quadricAdd(attribute_quadrics[remap[i0]], QA);
  649. quadricAdd(attribute_quadrics[remap[i1]], QA);
  650. quadricAdd(attribute_quadrics[remap[i2]], QA);
  651. quadricAdd(&attribute_gradients[remap[i0] * attribute_count], G, attribute_count);
  652. quadricAdd(&attribute_gradients[remap[i1] * attribute_count], G, attribute_count);
  653. quadricAdd(&attribute_gradients[remap[i2] * attribute_count], G, attribute_count);
  654. }
  655. }
  656. // does triangle ABC flip when C is replaced with D?
  657. static bool hasTriangleFlip(const Vector3& a, const Vector3& b, const Vector3& c, const Vector3& d)
  658. {
  659. Vector3 eb = {b.x - a.x, b.y - a.y, b.z - a.z};
  660. Vector3 ec = {c.x - a.x, c.y - a.y, c.z - a.z};
  661. Vector3 ed = {d.x - a.x, d.y - a.y, d.z - a.z};
  662. 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};
  663. 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};
  664. return nbc.x * nbd.x + nbc.y * nbd.y + nbc.z * nbd.z <= 0;
  665. }
  666. static bool hasTriangleFlips(const EdgeAdjacency& adjacency, const Vector3* vertex_positions, const unsigned int* collapse_remap, unsigned int i0, unsigned int i1)
  667. {
  668. assert(collapse_remap[i0] == i0);
  669. assert(collapse_remap[i1] == i1);
  670. const Vector3& v0 = vertex_positions[i0];
  671. const Vector3& v1 = vertex_positions[i1];
  672. const EdgeAdjacency::Edge* edges = &adjacency.data[adjacency.offsets[i0]];
  673. size_t count = adjacency.offsets[i0 + 1] - adjacency.offsets[i0];
  674. for (size_t i = 0; i < count; ++i)
  675. {
  676. unsigned int a = collapse_remap[edges[i].next];
  677. unsigned int b = collapse_remap[edges[i].prev];
  678. // skip triangles that will get collapsed by i0->i1 collapse or already got collapsed previously
  679. if (a == i1 || b == i1 || a == b)
  680. continue;
  681. // early-out when at least one triangle flips due to a collapse
  682. if (hasTriangleFlip(vertex_positions[a], vertex_positions[b], v0, v1))
  683. return true;
  684. }
  685. return false;
  686. }
  687. static size_t boundEdgeCollapses(const EdgeAdjacency& adjacency, size_t vertex_count, size_t index_count, unsigned char* vertex_kind)
  688. {
  689. size_t dual_count = 0;
  690. for (size_t i = 0; i < vertex_count; ++i)
  691. {
  692. unsigned char k = vertex_kind[i];
  693. unsigned int e = adjacency.offsets[i + 1] - adjacency.offsets[i];
  694. dual_count += (k == Kind_Manifold || k == Kind_Seam) ? e : 0;
  695. }
  696. assert(dual_count <= index_count);
  697. // pad capacity by 3 so that we can check for overflow once per triangle instead of once per edge
  698. return (index_count - dual_count / 2) + 3;
  699. }
  700. 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)
  701. {
  702. size_t collapse_count = 0;
  703. for (size_t i = 0; i < index_count; i += 3)
  704. {
  705. static const int next[3] = {1, 2, 0};
  706. // 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
  707. if (collapse_count + 3 > collapse_capacity)
  708. break;
  709. for (int e = 0; e < 3; ++e)
  710. {
  711. unsigned int i0 = indices[i + e];
  712. unsigned int i1 = indices[i + next[e]];
  713. // this can happen either when input has a zero-length edge, or when we perform collapses for complex
  714. // topology w/seams and collapse a manifold vertex that connects to both wedges onto one of them
  715. // we leave edges like this alone since they may be important for preserving mesh integrity
  716. if (remap[i0] == remap[i1])
  717. continue;
  718. unsigned char k0 = vertex_kind[i0];
  719. unsigned char k1 = vertex_kind[i1];
  720. // the edge has to be collapsible in at least one direction
  721. if (!(kCanCollapse[k0][k1] | kCanCollapse[k1][k0]))
  722. continue;
  723. // manifold and seam edges should occur twice (i0->i1 and i1->i0) - skip redundant edges
  724. if (kHasOpposite[k0][k1] && remap[i1] > remap[i0])
  725. continue;
  726. // two vertices are on a border or a seam, but there's no direct edge between them
  727. // this indicates that they belong to two different edge loops and we should not collapse this edge
  728. // loop[] tracks half edges so we only need to check i0->i1
  729. if (k0 == k1 && (k0 == Kind_Border || k0 == Kind_Seam) && loop[i0] != i1)
  730. continue;
  731. // edge can be collapsed in either direction - we will pick the one with minimum error
  732. // note: we evaluate error later during collapse ranking, here we just tag the edge as bidirectional
  733. if (kCanCollapse[k0][k1] & kCanCollapse[k1][k0])
  734. {
  735. Collapse c = {i0, i1, {/* bidi= */ 1}};
  736. collapses[collapse_count++] = c;
  737. }
  738. else
  739. {
  740. // edge can only be collapsed in one direction
  741. unsigned int e0 = kCanCollapse[k0][k1] ? i0 : i1;
  742. unsigned int e1 = kCanCollapse[k0][k1] ? i1 : i0;
  743. Collapse c = {e0, e1, {/* bidi= */ 0}};
  744. collapses[collapse_count++] = c;
  745. }
  746. }
  747. }
  748. return collapse_count;
  749. }
  750. 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)
  751. {
  752. for (size_t i = 0; i < collapse_count; ++i)
  753. {
  754. Collapse& c = collapses[i];
  755. unsigned int i0 = c.v0;
  756. unsigned int i1 = c.v1;
  757. // most edges are bidirectional which means we need to evaluate errors for two collapses
  758. // to keep this code branchless we just use the same edge for unidirectional edges
  759. unsigned int j0 = c.bidi ? i1 : i0;
  760. unsigned int j1 = c.bidi ? i0 : i1;
  761. float ei = quadricError(vertex_quadrics[remap[i0]], vertex_positions[i1]);
  762. float ej = quadricError(vertex_quadrics[remap[j0]], vertex_positions[j1]);
  763. if (attribute_count)
  764. {
  765. ei += quadricError(attribute_quadrics[remap[i0]], &attribute_gradients[remap[i0] * attribute_count], attribute_count, vertex_positions[i1], &vertex_attributes[i1 * attribute_count]);
  766. ej += quadricError(attribute_quadrics[remap[j0]], &attribute_gradients[remap[j0] * attribute_count], attribute_count, vertex_positions[j1], &vertex_attributes[j1 * attribute_count]);
  767. }
  768. // pick edge direction with minimal error
  769. c.v0 = ei <= ej ? i0 : j0;
  770. c.v1 = ei <= ej ? i1 : j1;
  771. c.error = ei <= ej ? ei : ej;
  772. }
  773. }
  774. static void sortEdgeCollapses(unsigned int* sort_order, const Collapse* collapses, size_t collapse_count)
  775. {
  776. const int sort_bits = 11;
  777. // fill histogram for counting sort
  778. unsigned int histogram[1 << sort_bits];
  779. memset(histogram, 0, sizeof(histogram));
  780. for (size_t i = 0; i < collapse_count; ++i)
  781. {
  782. // skip sign bit since error is non-negative
  783. unsigned int key = (collapses[i].errorui << 1) >> (32 - sort_bits);
  784. histogram[key]++;
  785. }
  786. // compute offsets based on histogram data
  787. size_t histogram_sum = 0;
  788. for (size_t i = 0; i < 1 << sort_bits; ++i)
  789. {
  790. size_t count = histogram[i];
  791. histogram[i] = unsigned(histogram_sum);
  792. histogram_sum += count;
  793. }
  794. assert(histogram_sum == collapse_count);
  795. // compute sort order based on offsets
  796. for (size_t i = 0; i < collapse_count; ++i)
  797. {
  798. // skip sign bit since error is non-negative
  799. unsigned int key = (collapses[i].errorui << 1) >> (32 - sort_bits);
  800. sort_order[histogram[key]++] = unsigned(i);
  801. }
  802. }
  803. static size_t performEdgeCollapses(unsigned int* collapse_remap, unsigned char* collapse_locked, Quadric* vertex_quadrics, Quadric* attribute_quadrics, QuadricGrad* attribute_gradients, size_t attribute_count, 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 Vector3* vertex_positions, const EdgeAdjacency& adjacency, size_t triangle_collapse_goal, float error_limit, float& result_error)
  804. {
  805. size_t edge_collapses = 0;
  806. size_t triangle_collapses = 0;
  807. // most collapses remove 2 triangles; use this to establish a bound on the pass in terms of error limit
  808. // note that edge_collapse_goal is an estimate; triangle_collapse_goal will be used to actually limit collapses
  809. size_t edge_collapse_goal = triangle_collapse_goal / 2;
  810. #if TRACE
  811. size_t stats[4] = {};
  812. #endif
  813. for (size_t i = 0; i < collapse_count; ++i)
  814. {
  815. const Collapse& c = collapses[collapse_order[i]];
  816. TRACESTATS(0);
  817. if (c.error > error_limit)
  818. break;
  819. if (triangle_collapses >= triangle_collapse_goal)
  820. break;
  821. // we limit the error in each pass based on the error of optimal last collapse; since many collapses will be locked
  822. // as they will share vertices with other successfull collapses, we need to increase the acceptable error by some factor
  823. float error_goal = edge_collapse_goal < collapse_count ? 1.5f * collapses[collapse_order[edge_collapse_goal]].error : FLT_MAX;
  824. // on average, each collapse is expected to lock 6 other collapses; to avoid degenerate passes on meshes with odd
  825. // topology, we only abort if we got over 1/6 collapses accordingly.
  826. if (c.error > error_goal && triangle_collapses > triangle_collapse_goal / 6)
  827. break;
  828. unsigned int i0 = c.v0;
  829. unsigned int i1 = c.v1;
  830. unsigned int r0 = remap[i0];
  831. unsigned int r1 = remap[i1];
  832. // we don't collapse vertices that had source or target vertex involved in a collapse
  833. // it's important to not move the vertices twice since it complicates the tracking/remapping logic
  834. // it's important to not move other vertices towards a moved vertex to preserve error since we don't re-rank collapses mid-pass
  835. if (collapse_locked[r0] | collapse_locked[r1])
  836. {
  837. TRACESTATS(1);
  838. continue;
  839. }
  840. if (hasTriangleFlips(adjacency, vertex_positions, collapse_remap, r0, r1))
  841. {
  842. // adjust collapse goal since this collapse is invalid and shouldn't factor into error goal
  843. edge_collapse_goal++;
  844. TRACESTATS(2);
  845. continue;
  846. }
  847. assert(collapse_remap[r0] == r0);
  848. assert(collapse_remap[r1] == r1);
  849. quadricAdd(vertex_quadrics[r1], vertex_quadrics[r0]);
  850. if (attribute_count)
  851. {
  852. quadricAdd(attribute_quadrics[r1], attribute_quadrics[r0]);
  853. quadricAdd(&attribute_gradients[r1 * attribute_count], &attribute_gradients[r0 * attribute_count], attribute_count);
  854. }
  855. if (vertex_kind[i0] == Kind_Complex)
  856. {
  857. unsigned int v = i0;
  858. do
  859. {
  860. collapse_remap[v] = r1;
  861. v = wedge[v];
  862. } while (v != i0);
  863. }
  864. else if (vertex_kind[i0] == Kind_Seam)
  865. {
  866. // remap v0 to v1 and seam pair of v0 to seam pair of v1
  867. unsigned int s0 = wedge[i0];
  868. unsigned int s1 = wedge[i1];
  869. assert(s0 != i0 && s1 != i1);
  870. assert(wedge[s0] == i0 && wedge[s1] == i1);
  871. collapse_remap[i0] = i1;
  872. collapse_remap[s0] = s1;
  873. }
  874. else
  875. {
  876. assert(wedge[i0] == i0);
  877. collapse_remap[i0] = i1;
  878. }
  879. collapse_locked[r0] = 1;
  880. collapse_locked[r1] = 1;
  881. // border edges collapse 1 triangle, other edges collapse 2 or more
  882. triangle_collapses += (vertex_kind[i0] == Kind_Border) ? 1 : 2;
  883. edge_collapses++;
  884. result_error = result_error < c.error ? c.error : result_error;
  885. }
  886. #if TRACE
  887. float error_goal_perfect = edge_collapse_goal < collapse_count ? collapses[collapse_order[edge_collapse_goal]].error : 0.f;
  888. printf("removed %d triangles, error %e (goal %e); evaluated %d/%d collapses (done %d, skipped %d, invalid %d)\n",
  889. int(triangle_collapses), sqrtf(result_error), sqrtf(error_goal_perfect),
  890. int(stats[0]), int(collapse_count), int(edge_collapses), int(stats[1]), int(stats[2]));
  891. #endif
  892. return edge_collapses;
  893. }
  894. static size_t remapIndexBuffer(unsigned int* indices, size_t index_count, const unsigned int* collapse_remap)
  895. {
  896. size_t write = 0;
  897. for (size_t i = 0; i < index_count; i += 3)
  898. {
  899. unsigned int v0 = collapse_remap[indices[i + 0]];
  900. unsigned int v1 = collapse_remap[indices[i + 1]];
  901. unsigned int v2 = collapse_remap[indices[i + 2]];
  902. // we never move the vertex twice during a single pass
  903. assert(collapse_remap[v0] == v0);
  904. assert(collapse_remap[v1] == v1);
  905. assert(collapse_remap[v2] == v2);
  906. if (v0 != v1 && v0 != v2 && v1 != v2)
  907. {
  908. indices[write + 0] = v0;
  909. indices[write + 1] = v1;
  910. indices[write + 2] = v2;
  911. write += 3;
  912. }
  913. }
  914. return write;
  915. }
  916. static void remapEdgeLoops(unsigned int* loop, size_t vertex_count, const unsigned int* collapse_remap)
  917. {
  918. for (size_t i = 0; i < vertex_count; ++i)
  919. {
  920. if (loop[i] != ~0u)
  921. {
  922. unsigned int l = loop[i];
  923. unsigned int r = collapse_remap[l];
  924. // i == r is a special case when the seam edge is collapsed in a direction opposite to where loop goes
  925. loop[i] = (i == r) ? loop[l] : r;
  926. }
  927. }
  928. }
  929. struct CellHasher
  930. {
  931. const unsigned int* vertex_ids;
  932. size_t hash(unsigned int i) const
  933. {
  934. unsigned int h = vertex_ids[i];
  935. // MurmurHash2 finalizer
  936. h ^= h >> 13;
  937. h *= 0x5bd1e995;
  938. h ^= h >> 15;
  939. return h;
  940. }
  941. bool equal(unsigned int lhs, unsigned int rhs) const
  942. {
  943. return vertex_ids[lhs] == vertex_ids[rhs];
  944. }
  945. };
  946. struct IdHasher
  947. {
  948. size_t hash(unsigned int id) const
  949. {
  950. unsigned int h = id;
  951. // MurmurHash2 finalizer
  952. h ^= h >> 13;
  953. h *= 0x5bd1e995;
  954. h ^= h >> 15;
  955. return h;
  956. }
  957. bool equal(unsigned int lhs, unsigned int rhs) const
  958. {
  959. return lhs == rhs;
  960. }
  961. };
  962. struct TriangleHasher
  963. {
  964. const unsigned int* indices;
  965. size_t hash(unsigned int i) const
  966. {
  967. const unsigned int* tri = indices + i * 3;
  968. // Optimized Spatial Hashing for Collision Detection of Deformable Objects
  969. return (tri[0] * 73856093) ^ (tri[1] * 19349663) ^ (tri[2] * 83492791);
  970. }
  971. bool equal(unsigned int lhs, unsigned int rhs) const
  972. {
  973. const unsigned int* lt = indices + lhs * 3;
  974. const unsigned int* rt = indices + rhs * 3;
  975. return lt[0] == rt[0] && lt[1] == rt[1] && lt[2] == rt[2];
  976. }
  977. };
  978. static void computeVertexIds(unsigned int* vertex_ids, const Vector3* vertex_positions, size_t vertex_count, int grid_size)
  979. {
  980. assert(grid_size >= 1 && grid_size <= 1024);
  981. float cell_scale = float(grid_size - 1);
  982. for (size_t i = 0; i < vertex_count; ++i)
  983. {
  984. const Vector3& v = vertex_positions[i];
  985. int xi = int(v.x * cell_scale + 0.5f);
  986. int yi = int(v.y * cell_scale + 0.5f);
  987. int zi = int(v.z * cell_scale + 0.5f);
  988. vertex_ids[i] = (xi << 20) | (yi << 10) | zi;
  989. }
  990. }
  991. static size_t countTriangles(const unsigned int* vertex_ids, const unsigned int* indices, size_t index_count)
  992. {
  993. size_t result = 0;
  994. for (size_t i = 0; i < index_count; i += 3)
  995. {
  996. unsigned int id0 = vertex_ids[indices[i + 0]];
  997. unsigned int id1 = vertex_ids[indices[i + 1]];
  998. unsigned int id2 = vertex_ids[indices[i + 2]];
  999. result += (id0 != id1) & (id0 != id2) & (id1 != id2);
  1000. }
  1001. return result;
  1002. }
  1003. static size_t fillVertexCells(unsigned int* table, size_t table_size, unsigned int* vertex_cells, const unsigned int* vertex_ids, size_t vertex_count)
  1004. {
  1005. CellHasher hasher = {vertex_ids};
  1006. memset(table, -1, table_size * sizeof(unsigned int));
  1007. size_t result = 0;
  1008. for (size_t i = 0; i < vertex_count; ++i)
  1009. {
  1010. unsigned int* entry = hashLookup2(table, table_size, hasher, unsigned(i), ~0u);
  1011. if (*entry == ~0u)
  1012. {
  1013. *entry = unsigned(i);
  1014. vertex_cells[i] = unsigned(result++);
  1015. }
  1016. else
  1017. {
  1018. vertex_cells[i] = vertex_cells[*entry];
  1019. }
  1020. }
  1021. return result;
  1022. }
  1023. static size_t countVertexCells(unsigned int* table, size_t table_size, const unsigned int* vertex_ids, size_t vertex_count)
  1024. {
  1025. IdHasher hasher;
  1026. memset(table, -1, table_size * sizeof(unsigned int));
  1027. size_t result = 0;
  1028. for (size_t i = 0; i < vertex_count; ++i)
  1029. {
  1030. unsigned int id = vertex_ids[i];
  1031. unsigned int* entry = hashLookup2(table, table_size, hasher, id, ~0u);
  1032. result += (*entry == ~0u);
  1033. *entry = id;
  1034. }
  1035. return result;
  1036. }
  1037. static void fillCellQuadrics(Quadric* cell_quadrics, const unsigned int* indices, size_t index_count, const Vector3* vertex_positions, const unsigned int* vertex_cells)
  1038. {
  1039. for (size_t i = 0; i < index_count; i += 3)
  1040. {
  1041. unsigned int i0 = indices[i + 0];
  1042. unsigned int i1 = indices[i + 1];
  1043. unsigned int i2 = indices[i + 2];
  1044. unsigned int c0 = vertex_cells[i0];
  1045. unsigned int c1 = vertex_cells[i1];
  1046. unsigned int c2 = vertex_cells[i2];
  1047. bool single_cell = (c0 == c1) & (c0 == c2);
  1048. Quadric Q;
  1049. quadricFromTriangle(Q, vertex_positions[i0], vertex_positions[i1], vertex_positions[i2], single_cell ? 3.f : 1.f);
  1050. if (single_cell)
  1051. {
  1052. quadricAdd(cell_quadrics[c0], Q);
  1053. }
  1054. else
  1055. {
  1056. quadricAdd(cell_quadrics[c0], Q);
  1057. quadricAdd(cell_quadrics[c1], Q);
  1058. quadricAdd(cell_quadrics[c2], Q);
  1059. }
  1060. }
  1061. }
  1062. 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)
  1063. {
  1064. static const float dummy_color[] = { 0.f, 0.f, 0.f };
  1065. size_t vertex_colors_stride_float = vertex_colors_stride / sizeof(float);
  1066. for (size_t i = 0; i < vertex_count; ++i)
  1067. {
  1068. unsigned int cell = vertex_cells[i];
  1069. const Vector3& v = vertex_positions[i];
  1070. Reservoir& r = cell_reservoirs[cell];
  1071. const float* color = vertex_colors ? &vertex_colors[i * vertex_colors_stride_float] : dummy_color;
  1072. r.x += v.x;
  1073. r.y += v.y;
  1074. r.z += v.z;
  1075. r.r += color[0];
  1076. r.g += color[1];
  1077. r.b += color[2];
  1078. r.w += 1.f;
  1079. }
  1080. for (size_t i = 0; i < cell_count; ++i)
  1081. {
  1082. Reservoir& r = cell_reservoirs[i];
  1083. float iw = r.w == 0.f ? 0.f : 1.f / r.w;
  1084. r.x *= iw;
  1085. r.y *= iw;
  1086. r.z *= iw;
  1087. r.r *= iw;
  1088. r.g *= iw;
  1089. r.b *= iw;
  1090. }
  1091. }
  1092. 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)
  1093. {
  1094. memset(cell_remap, -1, cell_count * sizeof(unsigned int));
  1095. for (size_t i = 0; i < vertex_count; ++i)
  1096. {
  1097. unsigned int cell = vertex_cells[i];
  1098. float error = quadricError(cell_quadrics[cell], vertex_positions[i]);
  1099. if (cell_remap[cell] == ~0u || cell_errors[cell] > error)
  1100. {
  1101. cell_remap[cell] = unsigned(i);
  1102. cell_errors[cell] = error;
  1103. }
  1104. }
  1105. }
  1106. 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)
  1107. {
  1108. static const float dummy_color[] = { 0.f, 0.f, 0.f };
  1109. size_t vertex_colors_stride_float = vertex_colors_stride / sizeof(float);
  1110. memset(cell_remap, -1, cell_count * sizeof(unsigned int));
  1111. for (size_t i = 0; i < vertex_count; ++i)
  1112. {
  1113. unsigned int cell = vertex_cells[i];
  1114. const Vector3& v = vertex_positions[i];
  1115. const Reservoir& r = cell_reservoirs[cell];
  1116. const float* color = vertex_colors ? &vertex_colors[i * vertex_colors_stride_float] : dummy_color;
  1117. 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);
  1118. 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);
  1119. float error = pos_error + color_weight * col_error;
  1120. if (cell_remap[cell] == ~0u || cell_errors[cell] > error)
  1121. {
  1122. cell_remap[cell] = unsigned(i);
  1123. cell_errors[cell] = error;
  1124. }
  1125. }
  1126. }
  1127. 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)
  1128. {
  1129. TriangleHasher hasher = {destination};
  1130. memset(tritable, -1, tritable_size * sizeof(unsigned int));
  1131. size_t result = 0;
  1132. for (size_t i = 0; i < index_count; i += 3)
  1133. {
  1134. unsigned int c0 = vertex_cells[indices[i + 0]];
  1135. unsigned int c1 = vertex_cells[indices[i + 1]];
  1136. unsigned int c2 = vertex_cells[indices[i + 2]];
  1137. if (c0 != c1 && c0 != c2 && c1 != c2)
  1138. {
  1139. unsigned int a = cell_remap[c0];
  1140. unsigned int b = cell_remap[c1];
  1141. unsigned int c = cell_remap[c2];
  1142. if (b < a && b < c)
  1143. {
  1144. unsigned int t = a;
  1145. a = b, b = c, c = t;
  1146. }
  1147. else if (c < a && c < b)
  1148. {
  1149. unsigned int t = c;
  1150. c = b, b = a, a = t;
  1151. }
  1152. destination[result * 3 + 0] = a;
  1153. destination[result * 3 + 1] = b;
  1154. destination[result * 3 + 2] = c;
  1155. unsigned int* entry = hashLookup2(tritable, tritable_size, hasher, unsigned(result), ~0u);
  1156. if (*entry == ~0u)
  1157. *entry = unsigned(result++);
  1158. }
  1159. }
  1160. return result * 3;
  1161. }
  1162. static float interpolate(float y, float x0, float y0, float x1, float y1, float x2, float y2)
  1163. {
  1164. // three point interpolation from "revenge of interpolation search" paper
  1165. float num = (y1 - y) * (x1 - x2) * (x1 - x0) * (y2 - y0);
  1166. float den = (y2 - y) * (x1 - x2) * (y0 - y1) + (y0 - y) * (x1 - x0) * (y1 - y2);
  1167. return x1 + num / den;
  1168. }
  1169. } // namespace meshopt
  1170. #ifndef NDEBUG
  1171. // Note: this is only exposed for debug visualization purposes; do *not* use these in debug builds
  1172. MESHOPTIMIZER_API unsigned char* meshopt_simplifyDebugKind = NULL;
  1173. MESHOPTIMIZER_API unsigned int* meshopt_simplifyDebugLoop = NULL;
  1174. MESHOPTIMIZER_API unsigned int* meshopt_simplifyDebugLoopBack = NULL;
  1175. #endif
  1176. 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, size_t target_index_count, float target_error, unsigned int options, float* out_result_error)
  1177. {
  1178. using namespace meshopt;
  1179. assert(index_count % 3 == 0);
  1180. assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
  1181. assert(vertex_positions_stride % sizeof(float) == 0);
  1182. assert(target_index_count <= index_count);
  1183. assert((options & ~(meshopt_SimplifyLockBorder)) == 0);
  1184. assert(vertex_attributes_stride >= attribute_count * sizeof(float) && vertex_attributes_stride <= 256);
  1185. assert(vertex_attributes_stride % sizeof(float) == 0);
  1186. assert(attribute_count <= kMaxAttributes);
  1187. meshopt_Allocator allocator;
  1188. unsigned int* result = destination;
  1189. // build adjacency information
  1190. EdgeAdjacency adjacency = {};
  1191. prepareEdgeAdjacency(adjacency, index_count, vertex_count, allocator);
  1192. updateEdgeAdjacency(adjacency, indices, index_count, vertex_count, NULL);
  1193. // build position remap that maps each vertex to the one with identical position
  1194. unsigned int* remap = allocator.allocate<unsigned int>(vertex_count);
  1195. unsigned int* wedge = allocator.allocate<unsigned int>(vertex_count);
  1196. buildPositionRemap(remap, wedge, vertex_positions_data, vertex_count, vertex_positions_stride, allocator);
  1197. // classify vertices; vertex kind determines collapse rules, see kCanCollapse
  1198. unsigned char* vertex_kind = allocator.allocate<unsigned char>(vertex_count);
  1199. unsigned int* loop = allocator.allocate<unsigned int>(vertex_count);
  1200. unsigned int* loopback = allocator.allocate<unsigned int>(vertex_count);
  1201. classifyVertices(vertex_kind, loop, loopback, vertex_count, adjacency, remap, wedge, options);
  1202. #if TRACE
  1203. size_t unique_positions = 0;
  1204. for (size_t i = 0; i < vertex_count; ++i)
  1205. unique_positions += remap[i] == i;
  1206. printf("position remap: %d vertices => %d positions\n", int(vertex_count), int(unique_positions));
  1207. size_t kinds[Kind_Count] = {};
  1208. for (size_t i = 0; i < vertex_count; ++i)
  1209. kinds[vertex_kind[i]] += remap[i] == i;
  1210. printf("kinds: manifold %d, border %d, seam %d, complex %d, locked %d\n",
  1211. int(kinds[Kind_Manifold]), int(kinds[Kind_Border]), int(kinds[Kind_Seam]), int(kinds[Kind_Complex]), int(kinds[Kind_Locked]));
  1212. #endif
  1213. Vector3* vertex_positions = allocator.allocate<Vector3>(vertex_count);
  1214. rescalePositions(vertex_positions, vertex_positions_data, vertex_count, vertex_positions_stride);
  1215. float* vertex_attributes = NULL;
  1216. if (attribute_count)
  1217. {
  1218. vertex_attributes = allocator.allocate<float>(vertex_count * attribute_count);
  1219. rescaleAttributes(vertex_attributes, vertex_attributes_data, vertex_count, vertex_attributes_stride, attribute_weights, attribute_count);
  1220. }
  1221. Quadric* vertex_quadrics = allocator.allocate<Quadric>(vertex_count);
  1222. memset(vertex_quadrics, 0, vertex_count * sizeof(Quadric));
  1223. Quadric* attribute_quadrics = NULL;
  1224. QuadricGrad* attribute_gradients = NULL;
  1225. if (attribute_count)
  1226. {
  1227. attribute_quadrics = allocator.allocate<Quadric>(vertex_count);
  1228. memset(attribute_quadrics, 0, vertex_count * sizeof(Quadric));
  1229. attribute_gradients = allocator.allocate<QuadricGrad>(vertex_count * attribute_count);
  1230. memset(attribute_gradients, 0, vertex_count * attribute_count * sizeof(QuadricGrad));
  1231. }
  1232. fillFaceQuadrics(vertex_quadrics, indices, index_count, vertex_positions, remap);
  1233. fillEdgeQuadrics(vertex_quadrics, indices, index_count, vertex_positions, remap, vertex_kind, loop, loopback);
  1234. if (attribute_count)
  1235. fillAttributeQuadrics(attribute_quadrics, attribute_gradients, indices, index_count, vertex_positions, vertex_attributes, attribute_count, remap);
  1236. if (result != indices)
  1237. memcpy(result, indices, index_count * sizeof(unsigned int));
  1238. #if TRACE
  1239. size_t pass_count = 0;
  1240. #endif
  1241. size_t collapse_capacity = boundEdgeCollapses(adjacency, vertex_count, index_count, vertex_kind);
  1242. Collapse* edge_collapses = allocator.allocate<Collapse>(collapse_capacity);
  1243. unsigned int* collapse_order = allocator.allocate<unsigned int>(collapse_capacity);
  1244. unsigned int* collapse_remap = allocator.allocate<unsigned int>(vertex_count);
  1245. unsigned char* collapse_locked = allocator.allocate<unsigned char>(vertex_count);
  1246. size_t result_count = index_count;
  1247. float result_error = 0;
  1248. // target_error input is linear; we need to adjust it to match quadricError units
  1249. float error_limit = target_error * target_error;
  1250. while (result_count > target_index_count)
  1251. {
  1252. // note: throughout the simplification process adjacency structure reflects welded topology for result-in-progress
  1253. updateEdgeAdjacency(adjacency, result, result_count, vertex_count, remap);
  1254. size_t edge_collapse_count = pickEdgeCollapses(edge_collapses, collapse_capacity, result, result_count, remap, vertex_kind, loop);
  1255. assert(edge_collapse_count <= collapse_capacity);
  1256. // no edges can be collapsed any more due to topology restrictions
  1257. if (edge_collapse_count == 0)
  1258. break;
  1259. rankEdgeCollapses(edge_collapses, edge_collapse_count, vertex_positions, vertex_attributes, vertex_quadrics, attribute_quadrics, attribute_gradients, attribute_count, remap);
  1260. sortEdgeCollapses(collapse_order, edge_collapses, edge_collapse_count);
  1261. size_t triangle_collapse_goal = (result_count - target_index_count) / 3;
  1262. for (size_t i = 0; i < vertex_count; ++i)
  1263. collapse_remap[i] = unsigned(i);
  1264. memset(collapse_locked, 0, vertex_count);
  1265. #if TRACE
  1266. printf("pass %d: ", int(pass_count++));
  1267. #endif
  1268. size_t collapses = performEdgeCollapses(collapse_remap, collapse_locked, vertex_quadrics, attribute_quadrics, attribute_gradients, attribute_count, edge_collapses, edge_collapse_count, collapse_order, remap, wedge, vertex_kind, vertex_positions, adjacency, triangle_collapse_goal, error_limit, result_error);
  1269. // no edges can be collapsed any more due to hitting the error limit or triangle collapse limit
  1270. if (collapses == 0)
  1271. break;
  1272. remapEdgeLoops(loop, vertex_count, collapse_remap);
  1273. remapEdgeLoops(loopback, vertex_count, collapse_remap);
  1274. size_t new_count = remapIndexBuffer(result, result_count, collapse_remap);
  1275. assert(new_count < result_count);
  1276. result_count = new_count;
  1277. }
  1278. #if TRACE
  1279. printf("result: %d triangles, error: %e; total %d passes\n", int(result_count), sqrtf(result_error), int(pass_count));
  1280. #endif
  1281. #ifndef NDEBUG
  1282. if (meshopt_simplifyDebugKind)
  1283. memcpy(meshopt_simplifyDebugKind, vertex_kind, vertex_count);
  1284. if (meshopt_simplifyDebugLoop)
  1285. memcpy(meshopt_simplifyDebugLoop, loop, vertex_count * sizeof(unsigned int));
  1286. if (meshopt_simplifyDebugLoopBack)
  1287. memcpy(meshopt_simplifyDebugLoopBack, loopback, vertex_count * sizeof(unsigned int));
  1288. #endif
  1289. // result_error is quadratic; we need to remap it back to linear
  1290. if (out_result_error)
  1291. *out_result_error = sqrtf(result_error);
  1292. return result_count;
  1293. }
  1294. 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)
  1295. {
  1296. return meshopt_simplifyEdge(destination, indices, index_count, vertex_positions_data, vertex_count, vertex_positions_stride, NULL, 0, NULL, 0, target_index_count, target_error, options, out_result_error);
  1297. }
  1298. 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, size_t target_index_count, float target_error, unsigned int options, float* out_result_error)
  1299. {
  1300. 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, target_index_count, target_error, options, out_result_error);
  1301. }
  1302. 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)
  1303. {
  1304. using namespace meshopt;
  1305. assert(index_count % 3 == 0);
  1306. assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
  1307. assert(vertex_positions_stride % sizeof(float) == 0);
  1308. assert(target_index_count <= index_count);
  1309. // we expect to get ~2 triangles/vertex in the output
  1310. size_t target_cell_count = target_index_count / 6;
  1311. meshopt_Allocator allocator;
  1312. Vector3* vertex_positions = allocator.allocate<Vector3>(vertex_count);
  1313. rescalePositions(vertex_positions, vertex_positions_data, vertex_count, vertex_positions_stride);
  1314. // find the optimal grid size using guided binary search
  1315. #if TRACE
  1316. printf("source: %d vertices, %d triangles\n", int(vertex_count), int(index_count / 3));
  1317. printf("target: %d cells, %d triangles\n", int(target_cell_count), int(target_index_count / 3));
  1318. #endif
  1319. unsigned int* vertex_ids = allocator.allocate<unsigned int>(vertex_count);
  1320. const int kInterpolationPasses = 5;
  1321. // invariant: # of triangles in min_grid <= target_count
  1322. int min_grid = int(1.f / (target_error < 1e-3f ? 1e-3f : target_error));
  1323. int max_grid = 1025;
  1324. size_t min_triangles = 0;
  1325. size_t max_triangles = index_count / 3;
  1326. // 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
  1327. if (min_grid > 1)
  1328. {
  1329. computeVertexIds(vertex_ids, vertex_positions, vertex_count, min_grid);
  1330. min_triangles = countTriangles(vertex_ids, indices, index_count);
  1331. }
  1332. // 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...
  1333. int next_grid_size = int(sqrtf(float(target_cell_count)) + 0.5f);
  1334. for (int pass = 0; pass < 10 + kInterpolationPasses; ++pass)
  1335. {
  1336. if (min_triangles >= target_index_count / 3 || max_grid - min_grid <= 1)
  1337. break;
  1338. // we clamp the prediction of the grid size to make sure that the search converges
  1339. int grid_size = next_grid_size;
  1340. grid_size = (grid_size <= min_grid) ? min_grid + 1 : (grid_size >= max_grid) ? max_grid - 1 : grid_size;
  1341. computeVertexIds(vertex_ids, vertex_positions, vertex_count, grid_size);
  1342. size_t triangles = countTriangles(vertex_ids, indices, index_count);
  1343. #if TRACE
  1344. printf("pass %d (%s): grid size %d, triangles %d, %s\n",
  1345. pass, (pass == 0) ? "guess" : (pass <= kInterpolationPasses) ? "lerp" : "binary",
  1346. grid_size, int(triangles),
  1347. (triangles <= target_index_count / 3) ? "under" : "over");
  1348. #endif
  1349. float tip = interpolate(float(target_index_count / 3), float(min_grid), float(min_triangles), float(grid_size), float(triangles), float(max_grid), float(max_triangles));
  1350. if (triangles <= target_index_count / 3)
  1351. {
  1352. min_grid = grid_size;
  1353. min_triangles = triangles;
  1354. }
  1355. else
  1356. {
  1357. max_grid = grid_size;
  1358. max_triangles = triangles;
  1359. }
  1360. // we start by using interpolation search - it usually converges faster
  1361. // 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)
  1362. next_grid_size = (pass < kInterpolationPasses) ? int(tip + 0.5f) : (min_grid + max_grid) / 2;
  1363. }
  1364. if (min_triangles == 0)
  1365. {
  1366. if (out_result_error)
  1367. *out_result_error = 1.f;
  1368. return 0;
  1369. }
  1370. // build vertex->cell association by mapping all vertices with the same quantized position to the same cell
  1371. size_t table_size = hashBuckets2(vertex_count);
  1372. unsigned int* table = allocator.allocate<unsigned int>(table_size);
  1373. unsigned int* vertex_cells = allocator.allocate<unsigned int>(vertex_count);
  1374. computeVertexIds(vertex_ids, vertex_positions, vertex_count, min_grid);
  1375. size_t cell_count = fillVertexCells(table, table_size, vertex_cells, vertex_ids, vertex_count);
  1376. // build a quadric for each target cell
  1377. Quadric* cell_quadrics = allocator.allocate<Quadric>(cell_count);
  1378. memset(cell_quadrics, 0, cell_count * sizeof(Quadric));
  1379. fillCellQuadrics(cell_quadrics, indices, index_count, vertex_positions, vertex_cells);
  1380. // for each target cell, find the vertex with the minimal error
  1381. unsigned int* cell_remap = allocator.allocate<unsigned int>(cell_count);
  1382. float* cell_errors = allocator.allocate<float>(cell_count);
  1383. fillCellRemap(cell_remap, cell_errors, cell_count, vertex_cells, cell_quadrics, vertex_positions, vertex_count);
  1384. // compute error
  1385. float result_error = 0.f;
  1386. for (size_t i = 0; i < cell_count; ++i)
  1387. result_error = result_error < cell_errors[i] ? cell_errors[i] : result_error;
  1388. // collapse triangles!
  1389. // note that we need to filter out triangles that we've already output because we very frequently generate redundant triangles between cells :(
  1390. size_t tritable_size = hashBuckets2(min_triangles);
  1391. unsigned int* tritable = allocator.allocate<unsigned int>(tritable_size);
  1392. size_t write = filterTriangles(destination, tritable, tritable_size, indices, index_count, vertex_cells, cell_remap);
  1393. #if TRACE
  1394. printf("result: %d cells, %d triangles (%d unfiltered), error %e\n", int(cell_count), int(write / 3), int(min_triangles), sqrtf(result_error));
  1395. #endif
  1396. if (out_result_error)
  1397. *out_result_error = sqrtf(result_error);
  1398. return write;
  1399. }
  1400. 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)
  1401. {
  1402. using namespace meshopt;
  1403. assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
  1404. assert(vertex_positions_stride % sizeof(float) == 0);
  1405. assert(vertex_colors_stride == 0 || (vertex_colors_stride >= 12 && vertex_colors_stride <= 256));
  1406. assert(vertex_colors_stride % sizeof(float) == 0);
  1407. assert(vertex_colors == NULL || vertex_colors_stride != 0);
  1408. assert(target_vertex_count <= vertex_count);
  1409. size_t target_cell_count = target_vertex_count;
  1410. if (target_cell_count == 0)
  1411. return 0;
  1412. meshopt_Allocator allocator;
  1413. Vector3* vertex_positions = allocator.allocate<Vector3>(vertex_count);
  1414. rescalePositions(vertex_positions, vertex_positions_data, vertex_count, vertex_positions_stride);
  1415. // find the optimal grid size using guided binary search
  1416. #if TRACE
  1417. printf("source: %d vertices\n", int(vertex_count));
  1418. printf("target: %d cells\n", int(target_cell_count));
  1419. #endif
  1420. unsigned int* vertex_ids = allocator.allocate<unsigned int>(vertex_count);
  1421. size_t table_size = hashBuckets2(vertex_count);
  1422. unsigned int* table = allocator.allocate<unsigned int>(table_size);
  1423. const int kInterpolationPasses = 5;
  1424. // invariant: # of vertices in min_grid <= target_count
  1425. int min_grid = 0;
  1426. int max_grid = 1025;
  1427. size_t min_vertices = 0;
  1428. size_t max_vertices = vertex_count;
  1429. // 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...
  1430. int next_grid_size = int(sqrtf(float(target_cell_count)) + 0.5f);
  1431. for (int pass = 0; pass < 10 + kInterpolationPasses; ++pass)
  1432. {
  1433. assert(min_vertices < target_vertex_count);
  1434. assert(max_grid - min_grid > 1);
  1435. // we clamp the prediction of the grid size to make sure that the search converges
  1436. int grid_size = next_grid_size;
  1437. grid_size = (grid_size <= min_grid) ? min_grid + 1 : (grid_size >= max_grid) ? max_grid - 1 : grid_size;
  1438. computeVertexIds(vertex_ids, vertex_positions, vertex_count, grid_size);
  1439. size_t vertices = countVertexCells(table, table_size, vertex_ids, vertex_count);
  1440. #if TRACE
  1441. printf("pass %d (%s): grid size %d, vertices %d, %s\n",
  1442. pass, (pass == 0) ? "guess" : (pass <= kInterpolationPasses) ? "lerp" : "binary",
  1443. grid_size, int(vertices),
  1444. (vertices <= target_vertex_count) ? "under" : "over");
  1445. #endif
  1446. float tip = interpolate(float(target_vertex_count), float(min_grid), float(min_vertices), float(grid_size), float(vertices), float(max_grid), float(max_vertices));
  1447. if (vertices <= target_vertex_count)
  1448. {
  1449. min_grid = grid_size;
  1450. min_vertices = vertices;
  1451. }
  1452. else
  1453. {
  1454. max_grid = grid_size;
  1455. max_vertices = vertices;
  1456. }
  1457. if (vertices == target_vertex_count || max_grid - min_grid <= 1)
  1458. break;
  1459. // we start by using interpolation search - it usually converges faster
  1460. // 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)
  1461. next_grid_size = (pass < kInterpolationPasses) ? int(tip + 0.5f) : (min_grid + max_grid) / 2;
  1462. }
  1463. if (min_vertices == 0)
  1464. return 0;
  1465. // build vertex->cell association by mapping all vertices with the same quantized position to the same cell
  1466. unsigned int* vertex_cells = allocator.allocate<unsigned int>(vertex_count);
  1467. computeVertexIds(vertex_ids, vertex_positions, vertex_count, min_grid);
  1468. size_t cell_count = fillVertexCells(table, table_size, vertex_cells, vertex_ids, vertex_count);
  1469. // accumulate points into a reservoir for each target cell
  1470. Reservoir* cell_reservoirs = allocator.allocate<Reservoir>(cell_count);
  1471. memset(cell_reservoirs, 0, cell_count * sizeof(Reservoir));
  1472. fillCellReservoirs(cell_reservoirs, cell_count, vertex_positions, vertex_colors, vertex_colors_stride, vertex_count, vertex_cells);
  1473. // for each target cell, find the vertex with the minimal error
  1474. unsigned int* cell_remap = allocator.allocate<unsigned int>(cell_count);
  1475. float* cell_errors = allocator.allocate<float>(cell_count);
  1476. fillCellRemap(cell_remap, cell_errors, cell_count, vertex_cells, cell_reservoirs, vertex_positions, vertex_colors, vertex_colors_stride, color_weight * color_weight, vertex_count);
  1477. // copy results to the output
  1478. assert(cell_count <= target_vertex_count);
  1479. memcpy(destination, cell_remap, sizeof(unsigned int) * cell_count);
  1480. #if TRACE
  1481. // compute error
  1482. float result_error = 0.f;
  1483. for (size_t i = 0; i < cell_count; ++i)
  1484. result_error = result_error < cell_errors[i] ? cell_errors[i] : result_error;
  1485. printf("result: %d cells, %e error\n", int(cell_count), sqrtf(result_error));
  1486. #endif
  1487. return cell_count;
  1488. }
  1489. float meshopt_simplifyScale(const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride)
  1490. {
  1491. using namespace meshopt;
  1492. assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
  1493. assert(vertex_positions_stride % sizeof(float) == 0);
  1494. float extent = rescalePositions(NULL, vertex_positions, vertex_count, vertex_positions_stride);
  1495. return extent;
  1496. }