# All files / src VolumetricRender.ts

0% Statements 0/158
0% Branches 0/30
0% Functions 0/11
0% Lines 0/139

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282

     import { Sphere } from "./WebGL/shapes";
    import { Vector3, Matrix4 } from "./WebGL/math";
    import { VolumetricMaterial, Mesh, Texture, Object3D } from "./WebGL";
    import { CC } from "./WebGL";
    import { GLShape } from "./GLShape";

    /**
     * A GLVolumetricRender is a "shape" for representing volumetric data as a density distribution.
     *
     * @class
     *
     * @param {VolumeData} data - volumetric data
     * @param {VolumetricRenderSpec} spec - specification of volumetric render
     * @returns {$3Dmol.GLShape}
     */
    export class GLVolumetricRender {

        static interpolateArray(data, fitCount) {
            function linearInterpolate(before, after, atPoint) {
                return before + (after - before) * atPoint;
            }
            var newData = [];
            var springFactor = (data.length - 1) / (fitCount - 1);
            newData[0] = data[0]; // for new allocation
            for (var i = 1; i < fitCount - 1; i++) {
                var tmp = i * springFactor;
                var before = Math.floor(tmp);
                var after = Math.ceil(tmp);
                var atPoint = tmp - before;
                newData[i] = linearInterpolate(data[before], data[after], atPoint);
            }
            newData[fitCount - 1] = data[data.length - 1]; // for new allocation
            return newData;
        }

        hidden = false;
        boundingSphere = new Sphere();
        shapePosition: any;
        renderedShapeObj: any = null;
        shapeObj: any = null;
        geo: any;
        subsamples = 5.0;
        data: any = null;
        transferfunctionbuffer: any = [];
        min:number = 0;
        max:number = 0;
        extent: any;
        maxdepth: number;
        texmatrix: any;
        minunit: any;

        constructor(data, spec) {
            spec = spec || {};
            var transferfn = Object.assign([], spec.transferfn);
            this.subsamples = spec.subsamples || 5.0;

            let TRANSFER_BUFFER_SIZE = 256;

            // arrange points based on position property
            transferfn.forEach(function (a) { a.value = parseFloat(a.value); });
            transferfn.sort(function (a, b) { return a.value - b.value; });
            this.min = transferfn[0].value;
            Iif (transferfn.length == 0) transferfn.push(transferfn[0]); //need at least two
            this.max = transferfn[transferfn.length - 1].value;

            // create and fill an array of interpolated values per 2 colors
            var pos1, pos2, color1, color2, R, G, B, A, alpha1, alpha2;
            for (let i = 0; i < transferfn.length - 1; i++) {
                color1 = CC.color(transferfn[i].color);
                color2 = CC.color(transferfn[i + 1].color);
                alpha1 = transferfn[i].opacity;
                alpha2 = transferfn[i + 1].opacity;
                pos1 = Math.floor((transferfn[i].value - this.min) * TRANSFER_BUFFER_SIZE / (this.max - this.min));
                pos2 = Math.floor((transferfn[i + 1].value - this.min) * TRANSFER_BUFFER_SIZE / (this.max - this.min));
                Iif (pos1 == pos2)
                    continue;
                R = GLVolumetricRender.interpolateArray([color1.r * 255, color2.r * 255], pos2 - pos1);
                G = GLVolumetricRender.interpolateArray([color1.g * 255, color2.g * 255], pos2 - pos1);
                B = GLVolumetricRender.interpolateArray([color1.b * 255, color2.b * 255], pos2 - pos1);
                A = GLVolumetricRender.interpolateArray([alpha1 * 255, alpha2 * 255], pos2 - pos1);

                for (let j = 0; j < R.length; j++) {
                    this.transferfunctionbuffer.push(R[j]);
                    this.transferfunctionbuffer.push(G[j]);
                    this.transferfunctionbuffer.push(B[j]);
                    this.transferfunctionbuffer.push(A[j]); // opacity will be added later
                }
            }

            this.transferfunctionbuffer = new Uint8ClampedArray(this.transferfunctionbuffer);

            //need to create transformation matrix that maps model points into
            //texture space
            // need extent (bounding box dimensions), maxdepth (box diagonal),
            // texmatrix (conversion from model to texture coords), minunit,
            // possibly non-orthnormal basis if matrix
            if (data.matrix) {
                //figure out bounding box of transformed grid
                let start = new Vector3(0, 0, 0);
                let end = new Vector3(data.size.x, data.size.y, data.size.z);
                let unit = new Vector3(1, 1, 1);

                start.applyMatrix4(data.matrix);
                end.applyMatrix4(data.matrix);
                unit.applyMatrix4(data.matrix).sub(start);

                this.extent = [[start.x, start.y, start.z], [end.x, end.y, end.z]];

                //check all corners, these may not be the farthest apart
                for (let i = 1; i < 7; i++) {
                    end.x = (i & 1) ? data.size.x : 0;
                    end.y = (i & 2) ? data.size.y : 0;
                    end.z = (i & 4) ? data.size.z : 0;
                    end.applyMatrix4(data.matrix);
                    this.extent[0][0] = Math.min(this.extent[0][0], end.x);
                    this.extent[0][1] = Math.min(this.extent[0][1], end.y);
                    this.extent[0][2] = Math.min(this.extent[0][2], end.z);
                    this.extent[1][0] = Math.max(this.extent[1][0], end.x);
                    this.extent[1][1] = Math.max(this.extent[1][1], end.y);
                    this.extent[1][2] = Math.max(this.extent[1][2], end.z);
                }

                let xoff = end.x - start.x;
                let yoff = end.y - start.y;
                let zoff = end.z - start.z;
                this.maxdepth = Math.sqrt(xoff * xoff + yoff * yoff + zoff * zoff);

                this.minunit = Math.min(Math.min(unit.x, unit.y), unit.z);

                //invert onto grid, then scale by grid dimensions to get
                //normalized texture coordinates
                this.texmatrix = new Matrix4().identity().scale({ x: data.size.x, y: data.size.y, z: data.size.z });
                this.texmatrix = this.texmatrix.multiplyMatrices(data.matrix, this.texmatrix);

                this.texmatrix = this.texmatrix.getInverse(this.texmatrix);

            } else {
                this.texmatrix = new Matrix4().identity();
                let xoff = data.unit.x * data.size.x;
                let yoff = data.unit.y * data.size.y;
                let zoff = data.unit.z * data.size.z;
                //scale doesn't apply to the translation vector, so preapply it
                this.texmatrix.makeTranslation(-data.origin.x / xoff, -data.origin.y / yoff, -data.origin.z / zoff);
                this.texmatrix.scale({ x: 1.0 / xoff, y: 1.0 / yoff, z: 1.0 / zoff });
                this.minunit = Math.min(Math.min(data.unit.x, data.unit.y), data.unit.z);

                //need the bounding box so we can intersect with rays
                this.extent = [[data.origin.x, data.origin.y, data.origin.z],
                [data.origin.x + xoff, data.origin.y + yoff, data.origin.z + zoff]];

                this.maxdepth = Math.sqrt(xoff * xoff + yoff * yoff + zoff * zoff);
            }

            //use GLShape to construct box
            var shape = new GLShape({});
            shape.addBox({
                corner: { x: this.extent[0][0], y: this.extent[0][1], z: this.extent[0][2] },
                dimensions: {
                    w: this.extent[1][0] - this.extent[0][0],
                    h: this.extent[1][1] - this.extent[0][1],
                    d: this.extent[1][2] - this.extent[0][2]
                }
            });

            this.geo = shape.finalize();
            this.boundingSphere.center = new Vector3(
                (this.extent[0][0] + this.extent[1][0]) / 2.0,
                (this.extent[0][1] + this.extent[1][1]) / 2.0,
                (this.extent[0][2] + this.extent[1][2]) / 2.0
            );
            this.boundingSphere.radius = this.maxdepth / 2;

            // volume selectivity based on given coords and distance
            Iif (spec.coords !== undefined && spec.seldist !== undefined) {
                let mask = new Uint8Array(data.data.length);
                //for each coordinate
                let d = spec.seldist;
                let d2 = d * d;
                for (let i = 0, n = spec.coords.length; i < n; i++) {
                    let c = spec.coords[i];
                    let minx = c.x - d, miny = c.y - d, minz = c.z - d;
                    let maxx = c.x + d, maxy = c.y + d, maxz = c.z + d;
                    Iif (data.getIndex(minx, miny, minz) >= 0 || data.getIndex(maxx, maxy, maxz) >= 0) {
                        //bounding box overlaps grid
                        //iterate over the grid points in the seldist bounding box
                        //minunit may be inefficient if axes have very different units. oh well.
                        for (let x = minx; x < maxx; x += this.minunit) {
                            for (let y = miny; y < maxy; y += this.minunit) {
                                for (let z = minz; z < maxz; z += this.minunit) {
                                    let idx = data.getIndex(x, y, z);
                                    Iif (idx >= 0 && !mask[idx]) {
                                        //if not already masked, check distance
                                        let distsq = (x - c.x) * (x - c.x) + (y - c.y) * (y - c.y) + (z - c.z) * (z - c.z);
                                        Iif (distsq < d2) {
                                            mask[idx] = 1;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //any place mask is zero, make infinite in data
                for (let i = 0, n = data.data.length; i < n; i++) {
                    Iif (mask[i] == 0) data.data[i] = Infinity;
                }
            }
            this.data = data;
        }

        /**
         * Initialize webgl objects for rendering
         * @param {Object3D} group
         *
         */
        globj(group) {

            Iif (this.renderedShapeObj) {
                group.remove(this.renderedShapeObj);
                this.renderedShapeObj = null;
            }

            Iif (this.hidden)
                return;

            this.shapeObj = new Object3D();
            var material = null;

            var texture = new Texture(this.data, true);
            var transfertexture = new Texture(this.transferfunctionbuffer, false);
            texture.needsUpdate = true;
            transfertexture.needsUpdate = true;
            transfertexture.flipY = false;

            material = new VolumetricMaterial({
                transferfn: transfertexture,
                transfermin: this.min,
                transfermax: this.max,
                map: texture,
                extent: this.extent,
                maxdepth: this.maxdepth,
                texmatrix: this.texmatrix,
                unit: this.minunit,
                subsamples: this.subsamples,
            });

            var mesh = new Mesh(this.geo, material);
            this.shapeObj.add(mesh);

            this.renderedShapeObj = this.shapeObj.clone();
            group.add(this.renderedShapeObj);
        };

        removegl(group) {
            Iif (this.renderedShapeObj) {
                // dispose of geos and materials
                Iif (this.renderedShapeObj.geometry !== undefined)
                    this.renderedShapeObj.geometry.dispose();
                Iif (this.renderedShapeObj.material !== undefined)
                    this.renderedShapeObj.material.dispose();
                group.remove(this.renderedShapeObj);
                this.renderedShapeObj = null;
            }
            this.shapeObj = null;
        };

        get position() {
            return this.boundingSphere.center;
        }

        get x() {
            return this.boundingSphere.center.x;
        }
        get y() {
            return this.boundingSphere.center.y;
        }
        get z() {
            return this.boundingSphere.center.z;
        }
    }