surface.ts 8.2 KB

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