mesh.ts 8.2 KB

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