Browse Source

wip, shape object

Alexander Rose 6 years ago
parent
commit
4df0e845e9

+ 8 - 8
src/apps/canvas/index.ts

@@ -115,26 +115,26 @@ async function init() {
 
     // create a mesh
     const meshBuilder = MeshBuilder.create(256, 128)
-    const groupColors: Color[] = []
-    const groupLabels: string[] = []
+    const colors: Color[] = []
+    const labels: string[] = []
     // red sphere
     meshBuilder.setGroup(0)
-    groupColors[0] = Color(0xFF2233)
-    groupLabels[0] = 'red sphere'
+    colors[0] = Color(0xFF2233)
+    labels[0] = 'red sphere'
     meshBuilder.addSphere(Vec3.create(0, 0, 0), 4, 2)
     // green cube
     meshBuilder.setGroup(1)
-    groupColors[1] = Color(0x2233FF)
-    groupLabels[1] = 'blue cube'
+    colors[1] = Color(0x2233FF)
+    labels[1] = 'blue cube'
     const t = Mat4.identity()
-    Mat4.setTranslation(t, Vec3.create(10, 0, 0))
+    Mat4.fromTranslation(t, Vec3.create(10, 0, 0))
     Mat4.scale(t, t, Vec3.create(3, 3, 3))
     meshBuilder.addBox(t)
     const mesh = meshBuilder.getMesh()
     // const mesh = getObjFromUrl('mesh.obj')
 
     // create shape from mesh
-    const shape = Shape.create(mesh, 'myShape', group => groupColors[group], group => groupLabels[group])
+    const shape = Shape.create('myShape', mesh, colors, labels)
 
     // add representation from shape
     const customRepr = ShapeRepresentation()

+ 5 - 5
src/mol-geo/mesh/mesh.ts

@@ -17,13 +17,13 @@ export interface Mesh {
     triangleCount: number,
 
     /** Vertex buffer as array of xyz values wrapped in a value cell */
-    vertexBuffer: ValueCell<Float32Array>,
+    readonly vertexBuffer: ValueCell<Float32Array>,
     /** Index buffer as array of vertex index triplets wrapped in a value cell */
-    indexBuffer: ValueCell<Uint32Array>,
+    readonly indexBuffer: ValueCell<Uint32Array>,
     /** Normal buffer as array of xyz values for each vertex wrapped in a value cell */
-    normalBuffer: ValueCell<Float32Array>,
+    readonly normalBuffer: ValueCell<Float32Array>,
     /** Group buffer as array of group ids for each vertex wrapped in a value cell */
-    groupBuffer: ValueCell<Float32Array>,
+    readonly groupBuffer: ValueCell<Float32Array>,
 
     /** Flag indicating if normals are computed for the current set of vertices */
     normalsComputed: boolean,
@@ -82,7 +82,7 @@ export namespace Mesh {
 
             // console.log([normals[i], normals[i + 1], normals[i + 2]], [v[i], v[i + 1], v[i + 2]])
         }
-        surface.normalBuffer = ValueCell.update(surface.normalBuffer, normals);
+        ValueCell.update(surface.normalBuffer, normals);
         surface.normalsComputed = true;
     }
 

+ 3 - 3
src/mol-geo/representation/shape/index.ts

@@ -17,7 +17,6 @@ import { ValueCell } from 'mol-util';
 import { ColorThemeProps } from 'mol-view/theme/color';
 import { Shape } from 'mol-model/shape';
 import { LocationIterator } from '../../util/location-iterator';
-import { arrayMax } from 'mol-util/array';
 import { createColors } from '../structure/visual/util/common';
 import { OrderedSet, Interval } from 'mol-data/int';
 
@@ -69,7 +68,8 @@ export function ShapeRepresentation<P extends ShapeProps>(): ShapeRepresentation
 
     function update(props: Partial<P>) {
         return Task.create('ShapeRepresentation.update', async ctx => {
-            // TODO
+            // TODO handle general update
+            // TODO check shape.colors.ref.version
         })
     }
 
@@ -116,7 +116,7 @@ export function ShapeRepresentation<P extends ShapeProps>(): ShapeRepresentation
 
 export namespace ShapeGroupIterator {
     export function fromShape(shape: Shape): LocationIterator {
-        const groupCount = arrayMax(shape.mesh.groupBuffer.ref.value) + 1
+        const { groupCount } = shape
         const instanceCount = 1
         const location = Shape.Location(shape)
         const getLocation = (groupIndex: number) => {

+ 22 - 5
src/mol-model/shape/shape.ts

@@ -6,20 +6,37 @@
 
 import { Mesh } from 'mol-geo/mesh/mesh';
 import { Color } from 'mol-util/color';
-import { UUID } from 'mol-util';
+import { UUID, ValueCell } from 'mol-util';
 import { OrderedSet } from 'mol-data/int';
+import { arrayMax } from 'mol-util/array';
 
 export interface Shape {
     readonly id: UUID
     readonly name: string
     readonly mesh: Mesh
-    getColor(group: number): Color
-    getLabel(group: number): string
+    readonly colors: ValueCell<Color[]>
+    readonly labels: ValueCell<string[]>
+    readonly groupCount: number
 }
 
 export namespace Shape {
-    export function create(mesh: Mesh, name: string, getColor: (group: number) => Color, getLabel: (group: number) => string): Shape {
-        return { id: UUID.create(), name, mesh, getColor, getLabel }
+    export function create(name: string, mesh: Mesh, colors: Color[], labels: string[]): Shape {
+        let currentGroupBufferVersion = -1
+        let currentGroupCount = -1
+
+        return {
+            id: UUID.create(),
+            name,
+            mesh,
+            get groupCount() {
+                if (mesh.groupBuffer.ref.version !== currentGroupBufferVersion) {
+                    currentGroupCount = arrayMax(mesh.groupBuffer.ref.value) + 1
+                }
+                return currentGroupCount
+            },
+            colors: ValueCell.create(colors),
+            labels: ValueCell.create(labels),
+        }
     }
 
     export interface Location {

+ 1 - 1
src/mol-view/label.ts

@@ -39,7 +39,7 @@ export function labelFirst(loci: Loci): string {
         case 'group-loci':
             const g = loci.groups[0]
             if (g) {
-                return g.shape.getLabel(OrderedSet.getAt(g.ids, 0))
+                return g.shape.labels.ref.value[OrderedSet.getAt(g.ids, 0)]
             } else {
                 return 'Unknown'
             }

+ 4 - 3
src/mol-view/stage.ts

@@ -91,7 +91,7 @@ export class Stage {
         // this.loadPdbid('3pqr') // inter unit bonds, two polymer chains, ligands, water, carbohydrates linked to protein
         // this.loadPdbid('4v5a') // ribosome
         // this.loadPdbid('3j3q') // ...
-        // this.loadPdbid('2np2') // dna
+        this.loadPdbid('2np2') // dna
         // this.loadPdbid('1d66') // dna
         // this.loadPdbid('9dna') // A form dna
         // this.loadPdbid('1bna') // B form dna
@@ -121,9 +121,10 @@ export class Stage {
         // this.loadMmcifUrl(`../../examples/1cbs_updated.cif`)
         // this.loadMmcifUrl(`../../examples/1crn.cif`)
         // this.loadPdbid('5u0q') // mixed dna/rna in same polymer
-        // this.loadPdbid('5u0q') // temp
+        // this.loadPdbid('1xj9') // peptide nucleic acid
+        // this.loadPdbid('1xj9') // temp
 
-        this.loadMmcifUrl(`../../../test/pdb-dev/PDBDEV_00000001.cif`) // ok
+        // this.loadMmcifUrl(`../../../test/pdb-dev/PDBDEV_00000001.cif`) // ok
         // this.loadMmcifUrl(`../../../test/pdb-dev/PDBDEV_00000002.cif`) // ok
         // this.loadMmcifUrl(`../../../test/pdb-dev/PDBDEV_00000003.cif`) // ok
         // this.loadMmcifUrl(`../../../test/pdb-dev/PDBDEV_00000004.cif`) // TODO issue with cross-link extraction, not shown

+ 1 - 1
src/mol-view/theme/color/shape-group.ts

@@ -16,7 +16,7 @@ export function ShapeGroupColorTheme(props: ColorThemeProps): ColorTheme {
         kind: 'group',
         color: (location: Location): Color => {
             if (Shape.isLocation(location)) {
-                return location.shape.getColor(location.group)
+                return location.shape.colors.ref.value[location.group]
             }
             return DefaultColor
         }