UNPKG

molstar

Version:

A comprehensive macromolecular library.

85 lines 3.26 kB
/** * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> */ import { Vec3 } from '../../mol-math/linear-algebra'; import { NullLocation } from '../../mol-model/location'; export function LocationIterator(groupCount, instanceCount, stride, getLocation, nonInstanceable, isSecondary) { if (nonInstanceable === void 0) { nonInstanceable = false; } if (isSecondary === void 0) { isSecondary = function () { return false; }; } if (groupCount % stride !== 0) { throw new Error('incompatible groupCount and stride'); } var value = { location: NullLocation, index: 0, groupIndex: 0, instanceIndex: 0, isSecondary: false }; var hasNext = value.groupIndex < groupCount; var isNextNewInstance = false; var groupIndex = 0; var instanceIndex = 0; var voidInstances = false; return { get hasNext() { return hasNext; }, get isNextNewInstance() { return isNextNewInstance; }, groupCount: groupCount, instanceCount: instanceCount, count: groupCount * instanceCount, stride: stride, nonInstanceable: nonInstanceable, move: function () { if (hasNext) { value.groupIndex = groupIndex; value.instanceIndex = instanceIndex; value.index = instanceIndex * groupCount + groupIndex; value.location = getLocation(groupIndex, voidInstances ? -1 : instanceIndex); value.isSecondary = isSecondary(groupIndex, voidInstances ? -1 : instanceIndex); groupIndex += stride; if (groupIndex === groupCount) { ++instanceIndex; isNextNewInstance = true; if (instanceIndex < instanceCount) groupIndex = 0; } else { isNextNewInstance = false; } hasNext = groupIndex < groupCount; } return value; }, reset: function () { value.location = NullLocation; value.index = 0; value.groupIndex = 0; value.instanceIndex = 0; value.isSecondary = false; hasNext = value.groupIndex < groupCount; isNextNewInstance = false; groupIndex = 0; instanceIndex = 0; voidInstances = false; }, skipInstance: function () { if (hasNext && value.instanceIndex === instanceIndex) { ++instanceIndex; groupIndex = 0; hasNext = instanceIndex < instanceCount; } }, voidInstances: function () { voidInstances = true; } }; } export function PositionLocation(position) { return { kind: 'position-location', position: position ? Vec3.clone(position) : Vec3() }; } export function isPositionLocation(x) { return !!x && x.kind === 'position-location'; } //# sourceMappingURL=location-iterator.js.map