Browse Source

fix unitTransforms reset with complex representation

Alexander Rose 5 years ago
parent
commit
8c2a4b5cae

+ 21 - 1
src/mol-model/structure/structure/util/unit-transforms.ts

@@ -9,6 +9,8 @@ import { Mat4 } from '../../../../mol-math/linear-algebra';
 import { IntMap } from '../../../../mol-data/int';
 import { fillIdentityTransform } from '../../../../mol-geo/geometry/transform-data';
 
+const tmpMat = Mat4()
+
 export class StructureUnitTransforms {
     private unitTransforms: Float32Array
     private groupUnitTransforms: Float32Array[] = []
@@ -17,10 +19,12 @@ export class StructureUnitTransforms {
     private groupIndexMap = IntMap.Mutable<number>();
     private size: number;
 
+    private _isIdentity: boolean | undefined = undefined;
+
     constructor(readonly structure: Structure) {
         this.unitTransforms = new Float32Array(structure.units.length * 16)
         this.size = structure.units.length
-        fillIdentityTransform(this.unitTransforms, structure.units.length)
+        this.reset() // to set to identity
         let groupOffset = 0
         for (let i = 0, il = structure.unitSymmetryGroups.length; i <il; ++i) {
             const g = structure.unitSymmetryGroups[i]
@@ -36,10 +40,26 @@ export class StructureUnitTransforms {
 
     reset() {
         fillIdentityTransform(this.unitTransforms, this.size);
+        this._isIdentity = true
+    }
+
+    get isIdentity() {
+        if (this._isIdentity === undefined) {
+            this._isIdentity = true
+            for (let i = 0, il = this.size * 16; i < il; i += 16) {
+                Mat4.fromArray(tmpMat, this.unitTransforms, i)
+                if (!Mat4.isIdentity(tmpMat)) {
+                    this._isIdentity = false
+                    break
+                }
+            }
+        }
+        return this._isIdentity
     }
 
     setTransform(matrix: Mat4, unit: Unit) {
         Mat4.toArray(matrix, this.unitTransforms, this.unitOffsetMap.get(unit.id))
+        this._isIdentity = undefined
     }
 
     getTransform(out: Mat4, unit: Unit) {

+ 6 - 5
src/mol-repr/structure/complex-representation.ts

@@ -74,8 +74,8 @@ export function ComplexRepresentation<P extends StructureParams>(label: string,
         StructureRepresentationStateBuilder.update(_state, state)
 
         if (state.visible !== undefined && visual) {
-            // hide visual when _unitTransforms is set
-            visual.setVisibility(state.visible && _state.unitTransforms === null)
+            // hide visual when _unitTransforms is set and not the identity
+            visual.setVisibility(state.visible && (_state.unitTransforms === null || _state.unitTransforms.isIdentity))
         }
         if (state.alphaFactor !== undefined && visual) visual.setAlphaFactor(state.alphaFactor)
         if (state.pickable !== undefined && visual) visual.setPickable(state.pickable)
@@ -87,9 +87,10 @@ export function ComplexRepresentation<P extends StructureParams>(label: string,
         if (state.transparency !== undefined && visual) visual.setTransparency(state.transparency)
         if (state.transform !== undefined && visual) visual.setTransform(state.transform)
         if (state.unitTransforms !== undefined && visual) {
-            // Since ComplexVisuals always renders geometries between units the application of `unitTransforms`
-            // does not make sense. When given it is ignored here and sets the visual's visibility to `false`.
-            visual.setVisibility(_state.visible && state.unitTransforms === null)
+            // Since ComplexVisuals always renders geometries between units, the application
+            // of `unitTransforms` does not make sense. When given here and not the identity,
+            // it is ignored and sets the visual's visibility to `false`.
+            visual.setVisibility(_state.visible && (state.unitTransforms === null || state.unitTransforms.isIdentity))
         }
     }