molstar
Version:
A comprehensive macromolecular library.
72 lines • 3.85 kB
JavaScript
/**
* Copyright (c) 2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBoxMesh = exports.BoxShape3D = void 0;
var tslib_1 = require("tslib");
var mesh_1 = require("../../mol-geo/geometry/mesh/mesh");
var mesh_builder_1 = require("../../mol-geo/geometry/mesh/mesh-builder");
var box_1 = require("../../mol-geo/primitive/box");
var geometry_1 = require("../../mol-math/geometry");
var linear_algebra_1 = require("../../mol-math/linear-algebra");
var shape_1 = require("../../mol-model/shape");
var mol_task_1 = require("../../mol-task");
var names_1 = require("../../mol-util/color/names");
var param_definition_1 = require("../../mol-util/param-definition");
var objects_1 = require("../objects");
var BoxShape3D = objects_1.PluginStateTransform.BuiltIn({
name: 'box-shape-3d',
display: 'Box Shape',
from: objects_1.PluginStateObject.Root,
to: objects_1.PluginStateObject.Shape.Provider,
params: {
bottomLeft: param_definition_1.ParamDefinition.Vec3((0, linear_algebra_1.Vec3)()),
topRight: param_definition_1.ParamDefinition.Vec3(linear_algebra_1.Vec3.create(1, 1, 1)),
radius: param_definition_1.ParamDefinition.Numeric(0.15, { min: 0.01, max: 4, step: 0.01 }),
color: param_definition_1.ParamDefinition.Color(names_1.ColorNames.red)
}
})({
canAutoUpdate: function () {
return true;
},
apply: function (_a) {
var _this = this;
var params = _a.params;
return mol_task_1.Task.create('Shape Representation', function (ctx) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
return (0, tslib_1.__generator)(this, function (_a) {
return [2 /*return*/, new objects_1.PluginStateObject.Shape.Provider({
label: 'Box',
data: params,
params: mesh_1.Mesh.Params,
getShape: function (_, data) {
var mesh = getBoxMesh(geometry_1.Box3D.create(params.bottomLeft, params.topRight), params.radius);
return shape_1.Shape.create('Box', data, mesh, function () { return data.color; }, function () { return 1; }, function () { return 'Box'; });
},
geometryUtils: mesh_1.Mesh.Utils
}, { label: 'Box' })];
});
}); });
}
});
exports.BoxShape3D = BoxShape3D;
function getBoxMesh(box, radius, oldMesh) {
var diag = linear_algebra_1.Vec3.sub((0, linear_algebra_1.Vec3)(), box.max, box.min);
var translateUnit = linear_algebra_1.Mat4.fromTranslation((0, linear_algebra_1.Mat4)(), linear_algebra_1.Vec3.create(0.5, 0.5, 0.5));
var scale = linear_algebra_1.Mat4.fromScaling((0, linear_algebra_1.Mat4)(), diag);
var translate = linear_algebra_1.Mat4.fromTranslation((0, linear_algebra_1.Mat4)(), box.min);
var transform = linear_algebra_1.Mat4.mul3((0, linear_algebra_1.Mat4)(), translate, scale, translateUnit);
// TODO: optimize?
var state = mesh_builder_1.MeshBuilder.createState(256, 128, oldMesh);
state.currentGroup = 1;
mesh_builder_1.MeshBuilder.addCage(state, transform, (0, box_1.BoxCage)(), radius, 2, 20);
var mesh = mesh_builder_1.MeshBuilder.getMesh(state);
var center = linear_algebra_1.Vec3.scaleAndAdd((0, linear_algebra_1.Vec3)(), box.min, diag, 0.5);
var sphereRadius = linear_algebra_1.Vec3.distance(box.min, center);
mesh.setBoundingSphere(geometry_1.Sphere3D.create(center, sphereRadius));
return mesh;
}
exports.getBoxMesh = getBoxMesh;
//# sourceMappingURL=shape.js.map
;