123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- /**
- * Copyright (C) 2022, Protein Bioinformatics Research Group, RCNS
- *
- * Licensed under CC BY-NC 4.0, see LICENSE file for more info.
- *
- * @author Gabor Tusnady <tusnady.gabor@ttk.hu>
- * @author Csongor Gerdan <gerdan.csongor@ttk.hu>
- */
- import { PluginUIContext } from 'molstar/lib/mol-plugin-ui/context';
- import { Quat, Vec3 } from 'molstar/lib/mol-math/linear-algebra';
- import { PluginCommands } from 'molstar/lib/mol-plugin/commands';
- import { Camera } from 'molstar/lib/mol-canvas3d/camera';
- import { DebugUtil } from './debug-utils';
- let initialSnapshot: Camera.Snapshot;
- export function storeCameraSnapshot(plugin: PluginUIContext): void {
- if (!initialSnapshot) {
- initialSnapshot = plugin.canvas3d!.camera.getSnapshot();
- DebugUtil.log('initialSnapshot stored:', initialSnapshot);
- }
- }
- export function loadInitialSnapshot(plugin: PluginUIContext): void {
- if (!initialSnapshot) {
- DebugUtil.log('initialSnapshot is undefined');
- } else {
- DebugUtil.log('Loading initial snapshot:', initialSnapshot);
- PluginCommands.Camera.Reset(plugin, { snapshot: initialSnapshot });
- }
- }
- export async function rotateCamera(plugin: PluginUIContext) {
- function rot90q(v: Vec3, axis: Vec3 = Vec3.create(1, 0, 0)): Vec3 {
- const q = Quat.setAxisAngle(Quat(), axis, -Math.PI/2);
- return Vec3.transformQuat(Vec3(), v, q);
- }
- function sub(v: Vec3, u: Vec3): Vec3 {
- return Vec3.sub(Vec3(), v, u);
- }
- function add(v: Vec3, u: Vec3): Vec3 {
- return Vec3.add(Vec3(), v, u);
- }
- if (!plugin.canvas3d) {
- return;
- }
- const cam = plugin.canvas3d!.camera;
- const snapshot = cam.getSnapshot();
- const newSnapshot = {
- ...snapshot,
- // target + rotateBy90(postition - target)
- position: add(snapshot.target, rot90q(sub(snapshot.position, snapshot.target))),
- target: snapshot.target,
- up: Vec3.negUnitZ
- };
- const duration = 100;
- PluginCommands.Camera.Reset(plugin, { snapshot: newSnapshot, durationMs: duration }).then(() => {
- setTimeout(()=> {
- requestAnimationFrame(() => plugin.canvas3d?.requestCameraReset());
- }, duration + 50); // The 2nd reset needs some delay
- });
- }
|