laplacian-smoothing.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. // TODO
  7. // function addVertex(src: Float32Array, i: number, dst: Float32Array, j: number) {
  8. // dst[3 * j] += src[3 * i];
  9. // dst[3 * j + 1] += src[3 * i + 1];
  10. // dst[3 * j + 2] += src[3 * i + 2];
  11. // }
  12. // function laplacianSmoothIter(surface: Surface, vertexCounts: Int32Array, vs: Float32Array, vertexWeight: number) {
  13. // const triCount = surface.triangleIndices.length,
  14. // src = surface.vertices;
  15. // const triangleIndices = surface.triangleIndices;
  16. // for (let i = 0; i < triCount; i += 3) {
  17. // const a = triangleIndices[i],
  18. // b = triangleIndices[i + 1],
  19. // c = triangleIndices[i + 2];
  20. // addVertex(src, b, vs, a);
  21. // addVertex(src, c, vs, a);
  22. // addVertex(src, a, vs, b);
  23. // addVertex(src, c, vs, b);
  24. // addVertex(src, a, vs, c);
  25. // addVertex(src, b, vs, c);
  26. // }
  27. // const vw = 2 * vertexWeight;
  28. // for (let i = 0, _b = surface.vertexCount; i < _b; i++) {
  29. // const n = vertexCounts[i] + vw;
  30. // vs[3 * i] = (vs[3 * i] + vw * src[3 * i]) / n;
  31. // vs[3 * i + 1] = (vs[3 * i + 1] + vw * src[3 * i + 1]) / n;
  32. // vs[3 * i + 2] = (vs[3 * i + 2] + vw * src[3 * i + 2]) / n;
  33. // }
  34. // }
  35. // async function laplacianSmoothComputation(ctx: Computation.Context, surface: Surface, iterCount: number, vertexWeight: number) {
  36. // await ctx.updateProgress('Smoothing surface...', true);
  37. // const vertexCounts = new Int32Array(surface.vertexCount),
  38. // triCount = surface.triangleIndices.length;
  39. // const tris = surface.triangleIndices;
  40. // for (let i = 0; i < triCount; i++) {
  41. // // in a triangle 2 edges touch each vertex, hence the constant.
  42. // vertexCounts[tris[i]] += 2;
  43. // }
  44. // let vs = new Float32Array(surface.vertices.length);
  45. // let started = Utils.PerformanceMonitor.currentTime();
  46. // await ctx.updateProgress('Smoothing surface...', true);
  47. // for (let i = 0; i < iterCount; i++) {
  48. // if (i > 0) {
  49. // for (let j = 0, _b = vs.length; j < _b; j++) vs[j] = 0;
  50. // }
  51. // surface.normals = void 0;
  52. // laplacianSmoothIter(surface, vertexCounts, vs, vertexWeight);
  53. // const t = surface.vertices;
  54. // surface.vertices = <any>vs;
  55. // vs = <any>t;
  56. // const time = Utils.PerformanceMonitor.currentTime();
  57. // if (time - started > Computation.UpdateProgressDelta) {
  58. // started = time;
  59. // await ctx.updateProgress('Smoothing surface...', true, i + 1, iterCount);
  60. // }
  61. // }
  62. // return surface;
  63. // }
  64. // /*
  65. // * Smooths the vertices by averaging the neighborhood.
  66. // *
  67. // * Resets normals. Might replace vertex array.
  68. // */
  69. // export function laplacianSmooth(surface: Surface, iterCount: number = 1, vertexWeight: number = 1): Computation<Surface> {
  70. // if (iterCount < 1) iterCount = 0;
  71. // if (iterCount === 0) return Computation.resolve(surface);
  72. // return computation(async ctx => await laplacianSmoothComputation(ctx, surface, iterCount, (1.1 * vertexWeight) / 1.1));
  73. // }