ply.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Schäfer, Marco <marco.schaefer@uni-tuebingen.de>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import { RuntimeContext, Task } from 'mol-task';
  8. import { addTriangle } from 'mol-geo/geometry/mesh/builder/triangle';
  9. import { ShapeProvider } from 'mol-model/shape/provider';
  10. import { Color } from 'mol-util/color';
  11. import { PlyData, PlyFile } from 'mol-io/reader/ply/schema';
  12. import { MeshBuilder } from 'mol-geo/geometry/mesh/mesh-builder';
  13. import { Mesh } from 'mol-geo/geometry/mesh/mesh';
  14. import { Shape } from 'mol-model/shape';
  15. async function getPlyMesh(ctx: RuntimeContext, centers: number[], normals: number[], faces: number[], mesh?: Mesh) {
  16. const builderState = MeshBuilder.createState(faces.length, faces.length, mesh)
  17. builderState.currentGroup = 0
  18. for (let i = 0, il = faces.length/4; i < il; ++i) {
  19. if (i % 10000 === 0 && ctx.shouldUpdate) await ctx.update({ current: i, max: il, message: `adding triangle ${i}` })
  20. builderState.currentGroup = i
  21. let triangle_vertices: number[];
  22. let triangle_normals: number[];
  23. let triangle_indices: number[];
  24. triangle_vertices = [centers[faces[4*i+1]*3], centers[faces[4*i+1]*3+1], centers[faces[4*i+1]*3+2],
  25. centers[faces[4*i+2]*3], centers[faces[4*i+2]*3+1], centers[faces[4*i+2]*3+2],
  26. centers[faces[4*i+3]*3], centers[faces[4*i+3]*3+1], centers[faces[4*i+3]*3+2]];
  27. triangle_normals = [ normals[faces[4*i+1]*3], normals[faces[4*i+1]*3+1], normals[faces[4*i+1]*3+2],
  28. normals[faces[4*i+2]*3], normals[faces[4*i+2]*3+1], normals[faces[4*i+2]*3+2],
  29. normals[faces[4*i+3]*3], normals[faces[4*i+3]*3+1], normals[faces[4*i+3]*3+2]];
  30. triangle_indices = [0, 1, 2];
  31. // console.log(triangle_vertices)
  32. addTriangle(builderState, triangle_vertices, triangle_normals, triangle_indices)
  33. }
  34. return MeshBuilder.getMesh(builderState);
  35. }
  36. async function getShape(ctx: RuntimeContext, parsedData: PlyData, props: {}, shape?: Shape<Mesh>) {
  37. await ctx.update('async creation of shape from myData')
  38. const { vertices, normals, faces, colors, properties } = parsedData
  39. const mesh = await getPlyMesh(ctx, vertices, normals, faces, shape && shape.geometry)
  40. return shape || Shape.create(
  41. 'test', mesh,
  42. (groupId: number) => {
  43. return Color.fromRgb(
  44. colors[faces[4 * groupId + 1] * 3 + 0],
  45. colors[faces[4 * groupId + 1] * 3 + 1],
  46. colors[faces[4 * groupId + 1] * 3 + 2]
  47. )
  48. },
  49. () => 1, // size: constant
  50. (groupId: number) => {
  51. return properties[parsedData.propertyCount * faces[4 * groupId + 1] + 10].toString()
  52. }
  53. )
  54. }
  55. export const PlyShapeParams = {
  56. ...Mesh.Params
  57. }
  58. export type PlyShapeParams = typeof PlyShapeParams
  59. export function shapeFromPly(source: PlyFile, params?: {}) {
  60. return Task.create<ShapeProvider<PlyData, Mesh, PlyShapeParams>>('Parse Shape Data', async ctx => {
  61. console.log('source', source)
  62. return {
  63. label: 'Mesh',
  64. data: source.PLY_File,
  65. getShape,
  66. geometryUtils: Mesh.Utils
  67. }
  68. })
  69. }