|
@@ -4,6 +4,7 @@
|
|
|
* @author Alexander Rose <alexander.rose@weirdbyte.de>
|
|
|
*/
|
|
|
|
|
|
+import { SortedArray } from '../../../../mol-data/int/sorted-array';
|
|
|
import { sortedCantorPairing } from '../../../../mol-data/util';
|
|
|
import { BondType } from '../../model/types';
|
|
|
import { StructureElement } from '../element';
|
|
@@ -15,13 +16,16 @@ export type UnitResonance = {
|
|
|
*
|
|
|
* Does not include triplets that are part of aromatic rings.
|
|
|
*/
|
|
|
- delocalizedTriplets: {
|
|
|
+ readonly delocalizedTriplets: {
|
|
|
/** Return 3rd element in triplet or undefined if `a` and `b` are not part of a triplet */
|
|
|
- getThirdElement: (a: StructureElement.UnitIndex, b: StructureElement.UnitIndex) => StructureElement.UnitIndex | undefined
|
|
|
+ readonly getThirdElement: (a: StructureElement.UnitIndex, b: StructureElement.UnitIndex) => StructureElement.UnitIndex | undefined
|
|
|
+ /** Return index into `triplets` or undefined if `a` is not part of any triplet */
|
|
|
+ readonly getTripletIndices: (a: StructureElement.UnitIndex) => number[] | undefined
|
|
|
+ readonly triplets: SortedArray<StructureElement.UnitIndex>[]
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export function getResonance(unit: Unit.Atomic) {
|
|
|
+export function getResonance(unit: Unit.Atomic): UnitResonance {
|
|
|
return {
|
|
|
delocalizedTriplets: getDelocalizedTriplets(unit)
|
|
|
};
|
|
@@ -33,7 +37,17 @@ function getDelocalizedTriplets(unit: Unit.Atomic) {
|
|
|
const { order: _order, flags: _flags } = edgeProps;
|
|
|
const { elementAromaticRingIndices } = unit.rings;
|
|
|
|
|
|
- const map = new Map<number, StructureElement.UnitIndex>();
|
|
|
+ const triplets: SortedArray<StructureElement.UnitIndex>[] = [];
|
|
|
+ const thirdElementMap = new Map<number, StructureElement.UnitIndex>();
|
|
|
+ const indicesMap = new Map<number, number[]>();
|
|
|
+
|
|
|
+ const add = (a: StructureElement.UnitIndex, b: StructureElement.UnitIndex, c: StructureElement.UnitIndex) => {
|
|
|
+ const index = triplets.length;
|
|
|
+ triplets.push(SortedArray.ofUnsortedArray([a, b, c]));
|
|
|
+ thirdElementMap.set(sortedCantorPairing(a, b), c);
|
|
|
+ if (indicesMap.has(a)) indicesMap.get(a)!.push(index);
|
|
|
+ else indicesMap.set(a, [index]);
|
|
|
+ };
|
|
|
|
|
|
for (let i = 0 as StructureElement.UnitIndex; i < unit.elements.length; i++) {
|
|
|
if (elementAromaticRingIndices.has(i)) continue;
|
|
@@ -50,16 +64,20 @@ function getDelocalizedTriplets(unit: Unit.Atomic) {
|
|
|
}
|
|
|
|
|
|
if (deloBonds.length >= 2) {
|
|
|
- map.set(sortedCantorPairing(i, deloBonds[0]), deloBonds[1]);
|
|
|
+ add(i, deloBonds[0], deloBonds[1]);
|
|
|
for (let j = 1, jl = deloBonds.length; j < jl; j++) {
|
|
|
- map.set(sortedCantorPairing(i, deloBonds[j]), deloBonds[0]);
|
|
|
+ add(i, deloBonds[j], deloBonds[0]);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
getThirdElement: (a: StructureElement.UnitIndex, b: StructureElement.UnitIndex) => {
|
|
|
- return map.get(sortedCantorPairing(a, b));
|
|
|
- }
|
|
|
+ return thirdElementMap.get(sortedCantorPairing(a, b));
|
|
|
+ },
|
|
|
+ getTripletIndices: (a: StructureElement.UnitIndex) => {
|
|
|
+ return indicesMap.get(a);
|
|
|
+ },
|
|
|
+ triplets,
|
|
|
};
|
|
|
}
|