소스 검색

convert to use ValueCell

Alexander Rose 7 년 전
부모
커밋
9ec290fcfa

+ 13 - 13
src/apps/render-test/state.ts

@@ -4,7 +4,7 @@
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
 
-import { ValueBox } from 'mol-util/value-cell'
+import { ValueCell } from 'mol-util/value-cell'
 
 import { Vec3, Mat4 } from 'mol-math/linear-algebra'
 import { createRenderer, createRenderObject, RenderObject } from 'mol-gl/renderer'
@@ -40,21 +40,21 @@ export default class State {
         const p1 = Vec3.create(0, 4, 0)
         const p2 = Vec3.create(-3, 0, 0)
 
-        const position = ValueBox(new Float32Array([0, -1, 0, -1, 0, 0, 1, 1, 0]))
-        const normal = ValueBox(new Float32Array([0, 0, 0, 0, 0, 0, 0, 0, 0]))
+        const position = ValueCell.create(new Float32Array([0, -1, 0, -1, 0, 0, 1, 1, 0]))
+        const normal = ValueCell.create(new Float32Array([0, 0, 0, 0, 0, 0, 0, 0, 0]))
 
-        const transformArray1 = ValueBox(new Float32Array(16))
-        const transformArray2 = ValueBox(new Float32Array(16 * 3))
+        const transformArray1 = ValueCell.create(new Float32Array(16))
+        const transformArray2 = ValueCell.create(new Float32Array(16 * 3))
         const m4 = Mat4.identity()
-        Mat4.toArray(m4, transformArray1.value, 0)
-        Mat4.toArray(m4, transformArray2.value, 0)
+        Mat4.toArray(m4, transformArray1.ref.value, 0)
+        Mat4.toArray(m4, transformArray2.ref.value, 0)
         Mat4.setTranslation(m4, p1)
-        Mat4.toArray(m4, transformArray2.value, 16)
+        Mat4.toArray(m4, transformArray2.ref.value, 16)
         Mat4.setTranslation(m4, p2)
-        Mat4.toArray(m4, transformArray2.value, 32)
+        Mat4.toArray(m4, transformArray2.ref.value, 32)
 
-        const color = ValueBox(createColorTexture(3))
-        color.value.set([
+        const color = ValueCell.create(createColorTexture(3))
+        color.ref.value.set([
             0, 0, 255,
             0, 255, 0,
             255, 0, 0
@@ -78,7 +78,7 @@ export default class State {
         console.log(box)
 
         const points2 = createRenderObject('point', {
-            position: ValueBox(new Float32Array(box.vertices)),
+            position: ValueCell.create(new Float32Array(box.vertices)),
             transform: transformArray1
         })
 
@@ -94,7 +94,7 @@ export default class State {
             normal: cubes.surface.normalBuffer as any,
             color,
             transform: transformArray1,
-        }, cubes.surface.indexBuffer.value);
+        }, cubes.surface.indexBuffer.ref.value);
         const mesh2 = makeCubesMesh();
         renderer.add(mesh2)
 

+ 17 - 6
src/mol-geo/representation/structure/index.ts

@@ -6,6 +6,8 @@
 
 import { AtomGroup, AtomSet, Structure, Unit } from 'mol-model/structure';
 import { RenderObject } from 'mol-gl/renderer';
+import { EquivalenceClasses } from 'mol-data/util';
+import { OrderedSet } from 'mol-data/int'
 
 export interface RepresentationProps {
 
@@ -22,20 +24,29 @@ export interface UnitRepresentation {
 // }
 
 export class StructureRepresentation {
+    // map: uint.id -> atomGroup.hashCode[]
+
     constructor () {
 
     }
     create (structure: Structure, props: RepresentationProps) {
         const { atoms, units } = structure;
-        const unitIds = AtomSet.unitIds(atoms);
-        for (let i = 0, _i = unitIds.length; i < _i; i++) {
-            const unitId = unitIds[i];
-            const unit = units[unitId];
-            const atomGroup = AtomSet.unitGetByIndex(atoms, i);
+        const uniqueGroups = EquivalenceClasses<number, AtomGroup>(
+            AtomGroup.hashCode,
+            (a, b) => units[a.id].model.id === units[b.id].model.id && OrderedSet.areEqual(a.atoms, b.atoms));
+
+        for (let i = 0, _i = AtomSet.unitCount(atoms); i < _i; i++) {
+            const group = AtomSet.unitGetByIndex(atoms, i);
+            uniqueGroups.add(group.id, group);
 
         }
 
+        //uniqueGroups.groups
+
         return true
     }
-    update: (props: RepresentationProps) => false
+    update (atoms: AtomSet, props: RepresentationProps) {
+        // TODO check model.id, conformation.id, unit.id, atomGroup(.hashCode/.areEqual)
+        return false
+    }
 }

+ 8 - 6
src/mol-geo/representation/structure/spacefill.ts

@@ -4,7 +4,7 @@
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
 
-import { ValueBox } from 'mol-util/value-cell'
+import { ValueCell } from 'mol-util/value-cell'
 
 import { createRenderObject, RenderObject } from 'mol-gl/renderer'
 import { createColorTexture } from 'mol-gl/util';
@@ -21,6 +21,8 @@ export default function Spacefill(): UnitRepresentation {
 
     const renderObjects: RenderObject[] = []
 
+    // unit: Unit, atomGroup: AtomGroup
+
     return {
         create: (unit: Unit, atomGroup: AtomGroup, props: RepresentationProps) => {
             const atomCount = OrderedSet.size(atomGroup.atoms)
@@ -58,16 +60,16 @@ export default function Spacefill(): UnitRepresentation {
             const m4 = Mat4.identity()
             Mat4.toArray(m4, transformArray, 0)
 
-            const color = ValueBox(createColorTexture(1))
-            color.value.set([ 0, 0, 255 ])
+            const color = ValueCell.create(createColorTexture(1))
+            color.ref.value.set([ 0, 0, 255 ])
 
             const spheres = createRenderObject(
                 'mesh',
                 {
-                    position: ValueBox(new Float32Array(vertices)),
-                    normal: ValueBox(new Float32Array(normals)),
+                    position: ValueCell.create(new Float32Array(vertices)),
+                    normal: ValueCell.create(new Float32Array(normals)),
                     color,
-                    transform: ValueBox(transformArray)
+                    transform: ValueCell.create(transformArray)
                 }
             )
 

+ 10 - 10
src/mol-geo/shape/surface.ts

@@ -5,18 +5,18 @@
  */
 
 import { Task } from 'mol-task'
-import { ValueBox } from 'mol-util'
+import { ValueCell } from 'mol-util'
 import { Vec3, Mat4 } from 'mol-math/linear-algebra'
 
 export interface Surface {
     vertexCount: number,
     triangleCount: number,
-    vertexBuffer: ValueBox<Float32Array>,
-    indexBuffer: ValueBox<Uint32Array>,
-    normalBuffer: ValueBox<Float32Array | undefined>,
+    vertexBuffer: ValueCell<Float32Array>,
+    indexBuffer: ValueCell<Uint32Array>,
+    normalBuffer: ValueCell<Float32Array | undefined>,
     normalsComputed: boolean,
 
-    vertexAnnotation?: ValueBox<ArrayLike<number>>
+    vertexAnnotation?: ValueCell<ArrayLike<number>>
     //boundingSphere?: { center: Geometry.LinearAlgebra.Vector3, radius: number };
 }
 
@@ -24,10 +24,10 @@ export namespace Surface {
     export function computeNormalsImmediate(surface: Surface) {
         if (surface.normalsComputed) return;
 
-        const normals = surface.normalBuffer.value && surface.normalBuffer.value!.length >= surface.vertexCount * 3
-            ? surface.normalBuffer.value : new Float32Array(surface.vertexBuffer.value!.length);
+        const normals = surface.normalBuffer.ref.value && surface.normalBuffer.ref.value!.length >= surface.vertexCount * 3
+            ? surface.normalBuffer.ref.value : new Float32Array(surface.vertexBuffer.ref.value!.length);
 
-        const v = surface.vertexBuffer.value, triangles = surface.indexBuffer.value;
+        const v = surface.vertexBuffer.ref.value, triangles = surface.indexBuffer.ref.value;
 
         const x = Vec3.zero(), y = Vec3.zero(), z = Vec3.zero(), d1 = Vec3.zero(), d2 = Vec3.zero(), n = Vec3.zero();
         for (let i = 0, ii = 3 * surface.triangleCount; i < ii; i += 3) {
@@ -54,7 +54,7 @@ export namespace Surface {
 
            // console.log([normals[i], normals[i + 1], normals[i + 2]], [v[i], v[i + 1], v[i + 2]])
         }
-        surface.normalBuffer = ValueBox(surface.normalBuffer, normals);
+        surface.normalBuffer = ValueCell.update(surface.normalBuffer, normals);
         surface.normalsComputed = true;
     }
 
@@ -70,7 +70,7 @@ export namespace Surface {
 
     export function transformImmediate(surface: Surface, t: Mat4) {
         const p = Vec3.zero();
-        const vertices = surface.vertexBuffer.value;
+        const vertices = surface.vertexBuffer.ref.value;
         for (let i = 0, _c = surface.vertexCount * 3; i < _c; i += 3) {
             p[0] = vertices[i];
             p[1] = vertices[i + 1];

+ 8 - 8
src/mol-geo/util/marching-cubes/algorithm.ts

@@ -9,7 +9,7 @@ import { ChunkedArray } from 'mol-data/util'
 import { Tensor } from 'mol-math/linear-algebra'
 import { Surface } from '../../shape/surface'
 import { Index, EdgeIdInfo, CubeEdges, EdgeTable, TriTable } from './tables'
-import { ValueBox } from 'mol-util'
+import { ValueCell } from 'mol-util'
 
 /**
  * The parameters required by the algorithm.
@@ -74,13 +74,13 @@ class MarchingCubesComputation {
         let ret: Surface = {
             vertexCount:  this.state.vertexCount,
             triangleCount: this.state.triangleCount,
-            vertexBuffer: this.parameters.oldSurface ? ValueBox(this.parameters.oldSurface.vertexBuffer, vb) : ValueBox(vb),
-            indexBuffer: this.parameters.oldSurface ? ValueBox(this.parameters.oldSurface.indexBuffer, ib) : ValueBox(ib),
-            normalBuffer: this.parameters.oldSurface ? this.parameters.oldSurface.normalBuffer : ValueBox(void 0),
+            vertexBuffer: this.parameters.oldSurface ? ValueCell.update(this.parameters.oldSurface.vertexBuffer, vb) : ValueCell.create(vb),
+            indexBuffer: this.parameters.oldSurface ? ValueCell.update(this.parameters.oldSurface.indexBuffer, ib) : ValueCell.create(ib),
+            normalBuffer: this.parameters.oldSurface ? this.parameters.oldSurface.normalBuffer : ValueCell.create(void 0),
             vertexAnnotation: this.state.annotate
                 ? this.parameters.oldSurface && this.parameters.oldSurface.vertexAnnotation
-                    ? ValueBox(this.parameters.oldSurface.vertexAnnotation, ChunkedArray.compact(this.state.annotationBuffer))
-                    : ValueBox(ChunkedArray.compact(this.state.annotationBuffer))
+                    ? ValueCell.update(this.parameters.oldSurface.vertexAnnotation, ChunkedArray.compact(this.state.annotationBuffer))
+                    : ValueCell.create(ChunkedArray.compact(this.state.annotationBuffer))
                 : void 0,
             normalsComputed: false
         }
@@ -199,9 +199,9 @@ class MarchingCubesState {
             triangleBufferSize = Math.min(1 << 16, vertexBufferSize * 4);
 
         this.vertexBuffer = ChunkedArray.create<number>(s => new Float32Array(s), 3, vertexBufferSize,
-            params.oldSurface && params.oldSurface.vertexBuffer.value);
+            params.oldSurface && params.oldSurface.vertexBuffer.ref.value);
         this.triangleBuffer = ChunkedArray.create<number>(s => new Uint32Array(s), 3, triangleBufferSize,
-            params.oldSurface && params.oldSurface.indexBuffer.value);
+            params.oldSurface && params.oldSurface.indexBuffer.ref.value);
 
         this.annotate = !!params.annotationField;
         if (this.annotate) this.annotationBuffer = ChunkedArray.create(s => new Int32Array(s), 1, vertexBufferSize);

+ 3 - 3
src/mol-gl/attribute.ts

@@ -5,7 +5,7 @@
  */
 
 import REGL = require('regl');
-import { ValueBox } from 'mol-util/value-cell'
+import { ValueCell } from 'mol-util/value-cell'
 
 // export type AttributeGroupMutator<T extends AttributesData> = (data: T) => (boolean | void)
 // export type AttributeGroupData = { [k: string]: Helpers.TypedArray }
@@ -63,9 +63,9 @@ namespace Attribute {
     export type ArrayCell<T> = { array: ReferenceCell<T> }
     export type ReferenceCell<T> = { readonly version: number, readonly value: T }
 
-    export function create<T extends Helpers.TypedArray>(regl: REGL.Regl, array: ValueBox<T>, props: AttributeProps): Attribute<T> {
+    export function create<T extends Helpers.TypedArray>(regl: REGL.Regl, array: ValueCell<T>, props: AttributeProps): Attribute<T> {
         const itemSize = props.size
-        let _array = array.value
+        let _array = array.ref.value
         let _count = _array.length / itemSize
         if (props.stride) _count = _array.length / (props.stride / _array.BYTES_PER_ELEMENT)
         console.log(_array.length, props.stride)

+ 5 - 5
src/mol-gl/renderable/mesh.ts

@@ -5,7 +5,7 @@
  */
 
 import REGL = require('regl');
-import { ValueBox } from 'mol-util/value-cell'
+import { ValueCell } from 'mol-util/value-cell'
 
 import { Renderable } from '../renderable'
 import { ColorTexture } from '../util'
@@ -25,7 +25,7 @@ namespace Mesh {
         color: { type: ColorTexture, itemSize: 16 }
     }
     export type Data = { [K in keyof DataType]: DataType[K]['type'] }
-    export type BoxedData = { [K in keyof Data]: ValueBox<Data[K]> }
+    export type BoxedData = { [K in keyof Data]: ValueCell<Data[K]> }
 
     export function create(regl: REGL.Regl, data: BoxedData, uniforms: Uniforms, elements?: Helpers.UintArray): Renderable<Data> {
         // console.log('mesh', {
@@ -34,8 +34,8 @@ namespace Mesh {
         //     attributes,
         //     uniforms
         // })
-        const instanceCount = data.transform.value.length / 16
-        const instanceId = ValueBox(fillSerial(new Float32Array(instanceCount)))
+        const instanceCount = data.transform.ref.value.length / 16
+        const instanceId = ValueCell.create(fillSerial(new Float32Array(instanceCount)))
         // console.log(instanceId)
         const command = regl({
             ...MeshShaders,
@@ -58,7 +58,7 @@ namespace Mesh {
                 // count: elements.length / 3,
                 // length: elements.length * 2
             }),
-            count: elements ? elements.length : data.position.value.length / 3,
+            count: elements ? elements.length : data.position.ref.value.length / 3,
             instances: instanceCount,
             primitive: 'triangles'
         })

+ 5 - 5
src/mol-gl/renderable/point.ts

@@ -5,7 +5,7 @@
  */
 
 import REGL = require('regl');
-import { ValueBox } from 'mol-util/value-cell'
+import { ValueCell } from 'mol-util/value-cell'
 
 import { Renderable } from '../renderable'
 import { getBuffers, createTransformAttributes, fillSerial } from './util'
@@ -20,11 +20,11 @@ namespace Point {
         transform: { type: Float32Array, itemSize: 16 }
     }
     export type Data = { [K in keyof DataType]: DataType[K]['type'] }
-    export type BoxedData = { [K in keyof Data]: ValueBox<Data[K]> }
+    export type BoxedData = { [K in keyof Data]: ValueCell<Data[K]> }
 
     export function create(regl: REGL.Regl, data: BoxedData): Renderable<Data> {
-        const instanceCount = data.transform.value.length / 16
-        const instanceId = ValueBox(fillSerial(new Float32Array(instanceCount)))
+        const instanceCount = data.transform.ref.value.length / 16
+        const instanceId = ValueCell.create(fillSerial(new Float32Array(instanceCount)))
         const command = regl({
             ...PointShaders,
             attributes: getBuffers({
@@ -32,7 +32,7 @@ namespace Point {
                 position: Attribute.create(regl, data.position, { size: 3 }),
                 ...createTransformAttributes(regl, data.transform)
             }),
-            count: data.position.value.length / 3,
+            count: data.position.ref.value.length / 3,
             instances: instanceCount,
             primitive: 'points'
         })

+ 8 - 8
src/mol-gl/renderable/util.ts

@@ -5,16 +5,16 @@
  */
 
 import REGL = require('regl');
-import { ValueBox } from 'mol-util/value-cell'
+import { ValueCell } from 'mol-util/value-cell'
 
 import { ColorTexture } from '../util';
 import { Attributes, AttributesData, AttributesBuffers } from '../renderable'
 import Attribute from '../attribute'
 
-export function createTransformAttributes (regl: REGL.Regl, transform: ValueBox<Float32Array>) {
+export function createTransformAttributes (regl: REGL.Regl, transform: ValueCell<Float32Array>) {
     const size = 4
     const divisor = 1
-    const bpe = transform.value.BYTES_PER_ELEMENT
+    const bpe = transform.ref.value.BYTES_PER_ELEMENT
     const stride = 16 * bpe
     return {
         transformColumn0: Attribute.create(regl, transform, { size, divisor, offset: 0, stride }),
@@ -24,19 +24,19 @@ export function createTransformAttributes (regl: REGL.Regl, transform: ValueBox<
     }
 }
 
-export function createColorUniforms (regl: REGL.Regl, color: ValueBox<ColorTexture>) {
+export function createColorUniforms (regl: REGL.Regl, color: ValueCell<ColorTexture>) {
     const colorTex = regl.texture({
-        width: color.value.width,
-        height: color.value.height,
+        width: color.ref.value.width,
+        height: color.ref.value.height,
         format: 'rgb',
         type: 'uint8',
         wrapS: 'clamp',
         wrapT: 'clamp',
-        data: color.value
+        data: color.ref.value
     })
     return {
         colorTex,
-        colorTexSize: [ color.value.width, color.value.height ]
+        colorTexSize: [ color.ref.value.width, color.ref.value.height ]
     }
 }
 

+ 9 - 0
src/mol-gl/renderer.ts

@@ -10,12 +10,21 @@ import { Camera } from './camera'
 import { PointRenderable, MeshRenderable, Renderable } from './renderable'
 
 import { Vec3, Mat4 } from 'mol-math/linear-algebra'
+import { ValueCell } from 'mol-util';
 
 let _renderObjectId = 0;
 function getNextId() {
     return _renderObjectId++ % 0x7FFFFFFF;
 }
 
+
+
+export interface RenderUpdateInfo {
+
+}
+
+export type RenderData = { [k: string]: ValueCell<Helpers.TypedArray> }
+
 export interface RenderObject {
     id: number
     type: 'mesh' | 'point'

+ 2 - 2
src/mol-util/value-cell.ts

@@ -34,7 +34,7 @@ namespace ValueBox {
         return { id: getNextId(), version: 0, value, metadata: metadata! };
     }
 
-    /** If diffInfo is not specified, copy the old value */
+    /** The box.metadata is carried over from the old box */
     export function withValue<T, D>(box: ValueBox<T, D>, value: T): ValueBox<T, D> {
         return { id: box.id, version: box.version + 1, value, metadata: box.metadata };
     }
@@ -48,7 +48,7 @@ namespace ValueCell {
         return ValueRef.create(ValueBox.create(value, metadata));
     }
 
-    /** If diffInfo is not specified, copy the old value */
+    /** The box.metadata is carried over from the old box */
     export function update<T, D>(cell: ValueCell<T, D>, value: T): ValueCell<T, D> {
         return ValueRef.set(cell, ValueBox.withValue(cell.ref, value));
     }