surface.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. export interface Surface {
  7. vertexCount: number,
  8. triangleCount: number,
  9. vertexBuffer: Float32Array,
  10. indexBuffer: Uint32Array,
  11. normalBuffer?: Float32Array,
  12. normalsComputed: boolean,
  13. vertexAnnotation?: ArrayLike<number>[]
  14. //boundingSphere?: { center: Geometry.LinearAlgebra.Vector3, radius: number };
  15. }
  16. // export namespace Surface {
  17. // export function computeNormalsImmediate(surface: Surface) {
  18. // if (surface.normals) return;
  19. // const normals = new Float32Array(surface.vertices.length),
  20. // v = surface.vertices, triangles = surface.triangleIndices;
  21. // for (let i = 0; i < triangles.length; i += 3) {
  22. // const a = 3 * triangles[i],
  23. // b = 3 * triangles[i + 1],
  24. // c = 3 * triangles[i + 2];
  25. // const nx = v[a + 2] * (v[b + 1] - v[c + 1]) + v[b + 2] * v[c + 1] - v[b + 1] * v[c + 2] + v[a + 1] * (-v[b + 2] + v[c + 2]),
  26. // ny = -(v[b + 2] * v[c]) + v[a + 2] * (-v[b] + v[c]) + v[a] * (v[b + 2] - v[c + 2]) + v[b] * v[c + 2],
  27. // nz = v[a + 1] * (v[b] - v[c]) + v[b + 1] * v[c] - v[b] * v[c + 1] + v[a] * (-v[b + 1] + v[b + 1]);
  28. // normals[a] += nx; normals[a + 1] += ny; normals[a + 2] += nz;
  29. // normals[b] += nx; normals[b + 1] += ny; normals[b + 2] += nz;
  30. // normals[c] += nx; normals[c + 1] += ny; normals[c + 2] += nz;
  31. // }
  32. // for (let i = 0; i < normals.length; i += 3) {
  33. // const nx = normals[i];
  34. // const ny = normals[i + 1];
  35. // const nz = normals[i + 2];
  36. // const f = 1.0 / Math.sqrt(nx * nx + ny * ny + nz * nz);
  37. // normals[i] *= f; normals[i + 1] *= f; normals[i + 2] *= f;
  38. // }
  39. // surface.normals = normals;
  40. // }
  41. // export function computeNormals(surface: Surface): Computation<Surface> {
  42. // return computation<Surface>(async ctx => {
  43. // if (surface.normals) {
  44. // return surface;
  45. // };
  46. // await ctx.updateProgress('Computing normals...');
  47. // computeNormalsImmediate(surface);
  48. // return surface;
  49. // });
  50. // }
  51. // function addVertex(src: Float32Array, i: number, dst: Float32Array, j: number) {
  52. // dst[3 * j] += src[3 * i];
  53. // dst[3 * j + 1] += src[3 * i + 1];
  54. // dst[3 * j + 2] += src[3 * i + 2];
  55. // }
  56. // function laplacianSmoothIter(surface: Surface, vertexCounts: Int32Array, vs: Float32Array, vertexWeight: number) {
  57. // const triCount = surface.triangleIndices.length,
  58. // src = surface.vertices;
  59. // const triangleIndices = surface.triangleIndices;
  60. // for (let i = 0; i < triCount; i += 3) {
  61. // const a = triangleIndices[i],
  62. // b = triangleIndices[i + 1],
  63. // c = triangleIndices[i + 2];
  64. // addVertex(src, b, vs, a);
  65. // addVertex(src, c, vs, a);
  66. // addVertex(src, a, vs, b);
  67. // addVertex(src, c, vs, b);
  68. // addVertex(src, a, vs, c);
  69. // addVertex(src, b, vs, c);
  70. // }
  71. // const vw = 2 * vertexWeight;
  72. // for (let i = 0, _b = surface.vertexCount; i < _b; i++) {
  73. // const n = vertexCounts[i] + vw;
  74. // vs[3 * i] = (vs[3 * i] + vw * src[3 * i]) / n;
  75. // vs[3 * i + 1] = (vs[3 * i + 1] + vw * src[3 * i + 1]) / n;
  76. // vs[3 * i + 2] = (vs[3 * i + 2] + vw * src[3 * i + 2]) / n;
  77. // }
  78. // }
  79. // async function laplacianSmoothComputation(ctx: Computation.Context, surface: Surface, iterCount: number, vertexWeight: number) {
  80. // await ctx.updateProgress('Smoothing surface...', true);
  81. // const vertexCounts = new Int32Array(surface.vertexCount),
  82. // triCount = surface.triangleIndices.length;
  83. // const tris = surface.triangleIndices;
  84. // for (let i = 0; i < triCount; i++) {
  85. // // in a triangle 2 edges touch each vertex, hence the constant.
  86. // vertexCounts[tris[i]] += 2;
  87. // }
  88. // let vs = new Float32Array(surface.vertices.length);
  89. // let started = Utils.PerformanceMonitor.currentTime();
  90. // await ctx.updateProgress('Smoothing surface...', true);
  91. // for (let i = 0; i < iterCount; i++) {
  92. // if (i > 0) {
  93. // for (let j = 0, _b = vs.length; j < _b; j++) vs[j] = 0;
  94. // }
  95. // surface.normals = void 0;
  96. // laplacianSmoothIter(surface, vertexCounts, vs, vertexWeight);
  97. // const t = surface.vertices;
  98. // surface.vertices = <any>vs;
  99. // vs = <any>t;
  100. // const time = Utils.PerformanceMonitor.currentTime();
  101. // if (time - started > Computation.UpdateProgressDelta) {
  102. // started = time;
  103. // await ctx.updateProgress('Smoothing surface...', true, i + 1, iterCount);
  104. // }
  105. // }
  106. // return surface;
  107. // }
  108. // /*
  109. // * Smooths the vertices by averaging the neighborhood.
  110. // *
  111. // * Resets normals. Might replace vertex array.
  112. // */
  113. // export function laplacianSmooth(surface: Surface, iterCount: number = 1, vertexWeight: number = 1): Computation<Surface> {
  114. // if (iterCount < 1) iterCount = 0;
  115. // if (iterCount === 0) return Computation.resolve(surface);
  116. // return computation(async ctx => await laplacianSmoothComputation(ctx, surface, iterCount, (1.1 * vertexWeight) / 1.1));
  117. // }
  118. // export function computeBoundingSphere(surface: Surface): Computation<Surface> {
  119. // return computation<Surface>(async ctx => {
  120. // if (surface.boundingSphere) {
  121. // return surface;
  122. // }
  123. // await ctx.updateProgress('Computing bounding sphere...');
  124. // const vertices = surface.vertices;
  125. // let x = 0, y = 0, z = 0;
  126. // for (let i = 0, _c = surface.vertices.length; i < _c; i += 3) {
  127. // x += vertices[i];
  128. // y += vertices[i + 1];
  129. // z += vertices[i + 2];
  130. // }
  131. // x /= surface.vertexCount;
  132. // y /= surface.vertexCount;
  133. // z /= surface.vertexCount;
  134. // let r = 0;
  135. // for (let i = 0, _c = vertices.length; i < _c; i += 3) {
  136. // const dx = x - vertices[i];
  137. // const dy = y - vertices[i + 1];
  138. // const dz = z - vertices[i + 2];
  139. // r = Math.max(r, dx * dx + dy * dy + dz * dz);
  140. // }
  141. // surface.boundingSphere = {
  142. // center: LinearAlgebra.Vector3.fromValues(x, y, z),
  143. // radius: Math.sqrt(r)
  144. // }
  145. // return surface;
  146. // });
  147. // }
  148. // export function transformImmediate(surface: Surface, t: number[]) {
  149. // const p = LinearAlgebra.Vector3.zero();
  150. // const m = LinearAlgebra.Vector3.transformMat4;
  151. // const vertices = surface.vertices;
  152. // for (let i = 0, _c = surface.vertices.length; i < _c; i += 3) {
  153. // p[0] = vertices[i];
  154. // p[1] = vertices[i + 1];
  155. // p[2] = vertices[i + 2];
  156. // m(p, p, t);
  157. // vertices[i] = p[0];
  158. // vertices[i + 1] = p[1];
  159. // vertices[i + 2] = p[2];
  160. // }
  161. // surface.normals = void 0;
  162. // surface.boundingSphere = void 0;
  163. // }
  164. // export function transform(surface: Surface, t: number[]): Computation<Surface> {
  165. // return computation<Surface>(async ctx => {
  166. // ctx.updateProgress('Updating surface...');
  167. // transformImmediate(surface, t);
  168. // return surface;
  169. // });
  170. // }
  171. // }