UNPKG

molstar

Version:

A comprehensive macromolecular library.

294 lines (293 loc) 14.2 kB
/** * Copyright (c) 2019-2022 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Schäfer, Marco <marco.schaefer@uni-tuebingen.de> * @author Alexander Rose <alexander.rose@weirdbyte.de> */ import { __assign, __awaiter, __generator } from "tslib"; import { Task } from '../../mol-task'; import { Color } from '../../mol-util/color'; import { MeshBuilder } from '../../mol-geo/geometry/mesh/mesh-builder'; import { Mesh } from '../../mol-geo/geometry/mesh/mesh'; import { Shape } from '../../mol-model/shape'; import { ChunkedArray } from '../../mol-data/util'; import { arrayMax, fillSerial } from '../../mol-util/array'; import { Column } from '../../mol-data/db'; import { ParamDefinition as PD } from '../../mol-util/param-definition'; import { ColorNames } from '../../mol-util/color/names'; import { deepClone } from '../../mol-util/object'; import { stringToWords } from '../../mol-util/string'; import { ValueCell } from '../../mol-util/value-cell'; // TODO support 'edge' element, see https://www.mathworks.com/help/vision/ug/the-ply-format.html // TODO support missing face element function createPlyShapeParams(plyFile) { var vertex = plyFile && plyFile.getElement('vertex'); var material = plyFile && plyFile.getElement('material'); var defaultValues = { group: '', vRed: '', vGreen: '', vBlue: '', mRed: '', mGreen: '', mBlue: '' }; var groupOptions = [['', '']]; var colorOptions = [['', '']]; if (vertex) { for (var i = 0, il = vertex.propertyNames.length; i < il; ++i) { var name_1 = vertex.propertyNames[i]; var type = vertex.propertyTypes[i]; if (type === 'uchar' || type === 'uint8' || type === 'ushort' || type === 'uint16' || type === 'uint' || type === 'uint32' || type === 'int') groupOptions.push([name_1, name_1]); if (type === 'uchar' || type === 'uint8') colorOptions.push([name_1, name_1]); } // TODO hardcoded as convenience for data provided by MegaMol if (vertex.propertyNames.includes('atomid')) defaultValues.group = 'atomid'; else if (vertex.propertyNames.includes('material_index')) defaultValues.group = 'material_index'; if (vertex.propertyNames.includes('red')) defaultValues.vRed = 'red'; if (vertex.propertyNames.includes('green')) defaultValues.vGreen = 'green'; if (vertex.propertyNames.includes('blue')) defaultValues.vBlue = 'blue'; } var materialOptions = [['', '']]; if (material) { for (var i = 0, il = material.propertyNames.length; i < il; ++i) { var name_2 = material.propertyNames[i]; var type = material.propertyTypes[i]; if (type === 'uchar' || type === 'uint8') materialOptions.push([name_2, name_2]); } if (material.propertyNames.includes('red')) defaultValues.mRed = 'red'; if (material.propertyNames.includes('green')) defaultValues.mGreen = 'green'; if (material.propertyNames.includes('blue')) defaultValues.mBlue = 'blue'; } var defaultColoring = defaultValues.vRed && defaultValues.vGreen && defaultValues.vBlue ? 'vertex' : defaultValues.mRed && defaultValues.mGreen && defaultValues.mBlue ? 'material' : 'uniform'; return __assign(__assign({}, Mesh.Params), { coloring: PD.MappedStatic(defaultColoring, { vertex: PD.Group({ red: PD.Select(defaultValues.vRed, colorOptions, { label: 'Red Property' }), green: PD.Select(defaultValues.vGreen, colorOptions, { label: 'Green Property' }), blue: PD.Select(defaultValues.vBlue, colorOptions, { label: 'Blue Property' }), }, { isFlat: true }), material: PD.Group({ red: PD.Select(defaultValues.mRed, materialOptions, { label: 'Red Property' }), green: PD.Select(defaultValues.mGreen, materialOptions, { label: 'Green Property' }), blue: PD.Select(defaultValues.mBlue, materialOptions, { label: 'Blue Property' }), }, { isFlat: true }), uniform: PD.Group({ color: PD.Color(ColorNames.grey) }, { isFlat: true }) }), grouping: PD.MappedStatic(defaultValues.group ? 'vertex' : 'none', { vertex: PD.Group({ group: PD.Select(defaultValues.group, groupOptions, { label: 'Group Property' }), }, { isFlat: true }), none: PD.Group({}) }) }); } export var PlyShapeParams = createPlyShapeParams(); function addVerticesRange(begI, endI, state, vertex, groupIds) { var vertices = state.vertices, normals = state.normals, groups = state.groups; var x = vertex.getProperty('x'); var y = vertex.getProperty('y'); var z = vertex.getProperty('z'); if (!x || !y || !z) throw new Error('missing coordinate properties'); var nx = vertex.getProperty('nx'); var ny = vertex.getProperty('ny'); var nz = vertex.getProperty('nz'); var hasNormals = !!nx && !!ny && !!nz; for (var i = begI; i < endI; ++i) { ChunkedArray.add3(vertices, x.value(i), y.value(i), z.value(i)); if (hasNormals) ChunkedArray.add3(normals, nx.value(i), ny.value(i), nz.value(i)); ChunkedArray.add(groups, groupIds[i]); } } function addFacesRange(begI, endI, state, face) { var indices = state.indices; for (var i = begI; i < endI; ++i) { var _a = face.value(i), entries = _a.entries, count = _a.count; if (count === 3) { // triangle ChunkedArray.add3(indices, entries[0], entries[1], entries[2]); } else if (count === 4) { // quadrilateral ChunkedArray.add3(indices, entries[2], entries[1], entries[0]); ChunkedArray.add3(indices, entries[2], entries[0], entries[3]); } } } function getMesh(ctx, vertex, face, groupIds, mesh) { return __awaiter(this, void 0, void 0, function () { var builderState, x, y, z, nx, ny, nz, hasNormals, updateChunk, i, il, i, il, m; return __generator(this, function (_a) { switch (_a.label) { case 0: builderState = MeshBuilder.createState(vertex.rowCount, vertex.rowCount / 4, mesh); x = vertex.getProperty('x'); y = vertex.getProperty('y'); z = vertex.getProperty('z'); if (!x || !y || !z) throw new Error('missing coordinate properties'); nx = vertex.getProperty('nx'); ny = vertex.getProperty('ny'); nz = vertex.getProperty('nz'); hasNormals = !!nx && !!ny && !!nz; updateChunk = 100000; i = 0, il = vertex.rowCount; _a.label = 1; case 1: if (!(i < il)) return [3 /*break*/, 4]; addVerticesRange(i, Math.min(i + updateChunk, il), builderState, vertex, groupIds); if (!ctx.shouldUpdate) return [3 /*break*/, 3]; return [4 /*yield*/, ctx.update({ message: 'adding ply mesh vertices', current: i, max: il })]; case 2: _a.sent(); _a.label = 3; case 3: i += updateChunk; return [3 /*break*/, 1]; case 4: i = 0, il = face.rowCount; _a.label = 5; case 5: if (!(i < il)) return [3 /*break*/, 8]; addFacesRange(i, Math.min(i + updateChunk, il), builderState, face); if (!ctx.shouldUpdate) return [3 /*break*/, 7]; return [4 /*yield*/, ctx.update({ message: 'adding ply mesh faces', current: i, max: il })]; case 6: _a.sent(); _a.label = 7; case 7: i += updateChunk; return [3 /*break*/, 5]; case 8: m = MeshBuilder.getMesh(builderState); if (!hasNormals) Mesh.computeNormals(m); // TODO: check if needed ValueCell.updateIfChanged(m.varyingGroup, true); return [2 /*return*/, m]; } }); }); } var int = Column.Schema.int; function getGrouping(vertex, props) { var grouping = props.grouping; var rowCount = vertex.rowCount; var column = grouping.name === 'vertex' ? vertex.getProperty(grouping.params.group) : undefined; var label = grouping.name === 'vertex' ? stringToWords(grouping.params.group) : 'Vertex'; var ids = column ? column.toArray({ array: Uint32Array }) : fillSerial(new Uint32Array(rowCount)); var maxId = column ? arrayMax(ids) : rowCount - 1; // assumes uint ids var map = new Uint32Array(maxId + 1); for (var i = 0, il = ids.length; i < il; ++i) map[ids[i]] = i; return { ids: ids, map: map, label: label }; } function getColoring(vertex, material, props) { var coloring = props.coloring; var rowCount = vertex.rowCount; var red, green, blue; if (coloring.name === 'vertex') { red = vertex.getProperty(coloring.params.red) || Column.ofConst(127, rowCount, int); green = vertex.getProperty(coloring.params.green) || Column.ofConst(127, rowCount, int); blue = vertex.getProperty(coloring.params.blue) || Column.ofConst(127, rowCount, int); } else if (coloring.name === 'material') { red = (material && material.getProperty(coloring.params.red)) || Column.ofConst(127, rowCount, int); green = (material && material.getProperty(coloring.params.green)) || Column.ofConst(127, rowCount, int); blue = (material && material.getProperty(coloring.params.blue)) || Column.ofConst(127, rowCount, int); } else { var _a = Color.toRgb(coloring.params.color), r = _a[0], g = _a[1], b = _a[2]; red = Column.ofConst(r, rowCount, int); green = Column.ofConst(g, rowCount, int); blue = Column.ofConst(b, rowCount, int); } return { kind: coloring.name, red: red, green: green, blue: blue }; } function createShape(plyFile, mesh, coloring, grouping) { var kind = coloring.kind, red = coloring.red, green = coloring.green, blue = coloring.blue; var ids = grouping.ids, map = grouping.map, label = grouping.label; return Shape.create('ply-mesh', plyFile, mesh, function (groupId) { var idx = kind === 'material' ? groupId : map[groupId]; return Color.fromRgb(red.value(idx), green.value(idx), blue.value(idx)); }, function () { return 1; }, // size: constant function (groupId) { return "".concat(label, " ").concat(ids[groupId]); }); } function makeShapeGetter() { var _this = this; var _plyFile; var _props; var _shape; var _mesh; var _coloring; var _grouping; var getShape = function (ctx, plyFile, props, shape) { return __awaiter(_this, void 0, void 0, function () { var vertex, face, material, newMesh, newColor; return __generator(this, function (_a) { switch (_a.label) { case 0: vertex = plyFile.getElement('vertex'); if (!vertex) throw new Error('missing vertex element'); face = plyFile.getElement('face'); if (!face) throw new Error('missing face element'); material = plyFile.getElement('material'); newMesh = false; newColor = false; if (!_plyFile || _plyFile !== plyFile) { newMesh = true; } if (!_props || !PD.isParamEqual(PlyShapeParams.grouping, _props.grouping, props.grouping)) { newMesh = true; } if (!_props || !PD.isParamEqual(PlyShapeParams.coloring, _props.coloring, props.coloring)) { newColor = true; } if (!newMesh) return [3 /*break*/, 2]; _coloring = getColoring(vertex, material, props); _grouping = getGrouping(vertex, props); return [4 /*yield*/, getMesh(ctx, vertex, face, _grouping.ids, shape && shape.geometry)]; case 1: _mesh = _a.sent(); _shape = createShape(plyFile, _mesh, _coloring, _grouping); return [3 /*break*/, 3]; case 2: if (newColor) { _coloring = getColoring(vertex, material, props); _shape = createShape(plyFile, _mesh, _coloring, _grouping); } _a.label = 3; case 3: _plyFile = plyFile; _props = deepClone(props); return [2 /*return*/, _shape]; } }); }); }; return getShape; } export function shapeFromPly(source, params) { var _this = this; return Task.create('Shape Provider', function (ctx) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, { label: 'Mesh', data: source, params: createPlyShapeParams(source), getShape: makeShapeGetter(), geometryUtils: Mesh.Utils }]; }); }); }); }