智慧水务管理系统 - 精河县供水工程综合管理平台

meshopt_simplifier.test.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import assert from 'assert/strict';
  2. import { MeshoptSimplifier as simplifier } from './meshopt_simplifier.js';
  3. process.on('unhandledRejection', (error) => {
  4. console.log('unhandledRejection', error);
  5. process.exit(1);
  6. });
  7. var tests = {
  8. compactMesh: function () {
  9. var indices = new Uint32Array([0, 1, 3, 3, 1, 5]);
  10. var expected = new Uint32Array([0, 1, 2, 2, 1, 3]);
  11. var missing = 2 ** 32 - 1;
  12. var remap = new Uint32Array([0, 1, missing, 2, missing, 3]);
  13. var res = simplifier.compactMesh(indices);
  14. assert.deepEqual(indices, expected);
  15. assert.deepEqual(res[0], remap);
  16. assert.equal(res[1], 4); // unique
  17. },
  18. simplify: function () {
  19. // 0
  20. // 1 2
  21. // 3 4 5
  22. var indices = new Uint32Array([0, 2, 1, 1, 2, 3, 3, 2, 4, 2, 5, 4]);
  23. var positions = new Float32Array([0, 4, 0, 0, 1, 0, 2, 2, 0, 0, 0, 0, 1, 0, 0, 4, 0, 0]);
  24. var res = simplifier.simplify(indices, positions, 3, /* target indices */ 3, /* target error */ 0.01);
  25. var expected = new Uint32Array([0, 5, 3]);
  26. assert.deepEqual(res[0], expected);
  27. assert(res[1] < 1e-4); // error
  28. },
  29. simplify16: function () {
  30. // 0
  31. // 1 2
  32. // 3 4 5
  33. var indices = new Uint16Array([0, 2, 1, 1, 2, 3, 3, 2, 4, 2, 5, 4]);
  34. var positions = new Float32Array([0, 4, 0, 0, 1, 0, 2, 2, 0, 0, 0, 0, 1, 0, 0, 4, 0, 0]);
  35. var res = simplifier.simplify(indices, positions, 3, /* target indices */ 3, /* target error */ 0.01);
  36. var expected = new Uint16Array([0, 5, 3]);
  37. assert.deepEqual(res[0], expected);
  38. assert(res[1] < 1e-4); // error
  39. },
  40. simplifyLockBorder: function () {
  41. // 0
  42. // 1 2
  43. // 3 4 5
  44. var indices = new Uint32Array([0, 2, 1, 1, 2, 3, 3, 2, 4, 2, 5, 4]);
  45. var positions = new Float32Array([0, 2, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0]);
  46. var res = simplifier.simplify(indices, positions, 3, /* target indices */ 3, /* target error */ 0.01, ['LockBorder']);
  47. var expected = new Uint32Array([0, 2, 1, 1, 2, 3, 3, 2, 4, 2, 5, 4]);
  48. assert.deepEqual(res[0], expected);
  49. assert(res[1] < 1e-4); // error
  50. },
  51. simplifyAttr: function () {
  52. var vb_pos = new Float32Array(8 * 3 * 3);
  53. var vb_att = new Float32Array(8 * 3 * 3);
  54. for (var y = 0; y < 8; ++y) {
  55. // first four rows are a blue gradient, next four rows are a yellow gradient
  56. var r = y < 4 ? 0.8 + y * 0.05 : 0;
  57. var g = y < 4 ? 0.8 + y * 0.05 : 0;
  58. var b = y < 4 ? 0 : 0.8 + (7 - y) * 0.05;
  59. for (var x = 0; x < 3; ++x) {
  60. vb_pos[(y * 3 + x) * 3 + 0] = x;
  61. vb_pos[(y * 3 + x) * 3 + 1] = y;
  62. vb_pos[(y * 3 + x) * 3 + 2] = 0.03 * x + 0.028 * (y % 2) + (x == 2 && y == 7 ? 1 : 0) * 0.03;
  63. vb_att[(y * 3 + x) * 3 + 0] = r;
  64. vb_att[(y * 3 + x) * 3 + 1] = g;
  65. vb_att[(y * 3 + x) * 3 + 2] = b;
  66. }
  67. }
  68. var ib = new Uint32Array(7 * 2 * 6);
  69. for (var y = 0; y < 7; ++y) {
  70. for (var x = 0; x < 2; ++x) {
  71. ib[(y * 2 + x) * 6 + 0] = (y + 0) * 3 + (x + 0);
  72. ib[(y * 2 + x) * 6 + 1] = (y + 0) * 3 + (x + 1);
  73. ib[(y * 2 + x) * 6 + 2] = (y + 1) * 3 + (x + 0);
  74. ib[(y * 2 + x) * 6 + 3] = (y + 1) * 3 + (x + 0);
  75. ib[(y * 2 + x) * 6 + 4] = (y + 0) * 3 + (x + 1);
  76. ib[(y * 2 + x) * 6 + 5] = (y + 1) * 3 + (x + 1);
  77. }
  78. }
  79. var attr_weights = [0.5, 0.5, 0.5];
  80. var res = simplifier.simplifyWithAttributes(ib, vb_pos, 3, vb_att, 3, attr_weights, null, 6 * 3, 1e-2);
  81. var expected = new Uint32Array([0, 2, 11, 0, 11, 9, 9, 11, 12, 12, 11, 14, 12, 14, 23, 12, 23, 21]);
  82. assert.deepEqual(res[0], expected);
  83. },
  84. simplifyUpdate: function () {
  85. var indices = new Uint32Array([0, 1, 3, 3, 1, 4, 4, 1, 2, 0, 3, 2, 3, 4, 2]);
  86. var positions = new Float32Array([0, 0, 0, 1, 1, 0, 2, 0, 0, 0.9, 0.2, 0.1, 1.1, 0.2, 0.1]);
  87. var attributes = new Float32Array([0, 0, 0, 0.2, 0.1]);
  88. var res = simplifier.simplifyWithUpdate(indices, positions, 3, attributes, 1, [1], null, 9, 1);
  89. var expected = new Uint32Array([0, 1, 3, 3, 1, 2, 0, 3, 2]);
  90. assert.equal(res[0], expected.length);
  91. assert.deepEqual(indices.subarray(0, expected.length), expected);
  92. // border vertices haven't moved but may have small floating point drift
  93. for (var i = 0; i < 3; ++i) {
  94. assert(Math.abs(attributes[i]) < 1e-6);
  95. }
  96. // center vertex got updated
  97. assert(Math.abs(positions[3 * 3 + 0] - 0.88) < 1e-2);
  98. assert(Math.abs(positions[3 * 3 + 1] - 0.19) < 1e-2);
  99. assert(Math.abs(positions[3 * 3 + 2] - 0.11) < 1e-2);
  100. assert(Math.abs(attributes[3] - 0.18) < 1e-2);
  101. },
  102. simplifyLockFlags: function () {
  103. // 0
  104. // 1 2
  105. // 3 4 5
  106. var indices = new Uint32Array([0, 2, 1, 1, 2, 3, 3, 2, 4, 2, 5, 4]);
  107. var positions = new Float32Array([0, 2, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0]);
  108. var locks = new Uint8Array([1, 1, 1, 1, 0, 1]); // only vertex 4 can move
  109. var res = simplifier.simplifyWithAttributes(indices, positions, 3, new Float32Array(), 0, [], locks, 3, 0.01);
  110. var expected = new Uint32Array([0, 2, 1, 1, 2, 3, 2, 5, 3]);
  111. assert.deepEqual(res[0], expected);
  112. assert(res[1] < 1e-4); // error
  113. },
  114. getScale: function () {
  115. var positions = new Float32Array([0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3]);
  116. assert(simplifier.getScale(positions, 3) == 3.0);
  117. },
  118. simplifyPoints: function () {
  119. var positions = new Float32Array([0, 0, 0, 100, 0, 0, 100, 1, 1, 110, 0, 0]);
  120. var colors = new Float32Array([1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]);
  121. var expected = new Uint32Array([0, 1]);
  122. var expectedC = new Uint32Array([0, 2]);
  123. var res = simplifier.simplifyPoints(positions, 3, 2);
  124. assert.deepEqual(res, expected);
  125. // note: recommended value for color_weight is 1e-2 but here we push color weight to be very high to bias candidate selection for testing
  126. var resC1 = simplifier.simplifyPoints(positions, 3, 2, colors, 3, 1e-1);
  127. assert.deepEqual(resC1, expectedC);
  128. var resC2 = simplifier.simplifyPoints(positions, 3, 2, colors, 3, 1e-2);
  129. assert.deepEqual(resC2, expected);
  130. },
  131. simplifyPrune: function () {
  132. var indices = new Uint32Array([0, 1, 2, 3, 4, 5, 6, 7, 8]);
  133. var positions = new Float32Array([0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 2, 1, 2, 0, 1, 0, 0, 2, 0, 4, 2, 4, 0, 2]);
  134. var expected = new Uint32Array([6, 7, 8]);
  135. var res = simplifier.simplifyPrune(indices, positions, 3, 0.5);
  136. assert.deepEqual(res, expected);
  137. },
  138. };
  139. Promise.all([simplifier.ready]).then(() => {
  140. var count = 0;
  141. for (var key in tests) {
  142. tests[key]();
  143. count++;
  144. }
  145. console.log(count, 'tests passed');
  146. });