all files / src/util/ transferable.js

92.68% Statements 38/41
74.19% Branches 23/31
100% Functions 4/4
92.68% Lines 38/41
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                                                                              27× 27×   27×                                                                                               12×            
import util from '../core/util';
import Geometry from '../Geometry';
import BoundingBox from '../math/BoundingBox';
import Vector3 from '../math/Vector3';
 
var META = {
    version: 1.0,
    type: 'Geometry',
    generator: 'util.transferable.toObject'
};
 
/**
 * @alias clay.util.transferable
 */
var transferableUtil = {
    /**
     * Convert geometry to a object containing transferable data
     * @param {Geometry} geometry geometry
     * @param {Boolean} shallow whether shallow copy
     * @returns {Object} { data : data, buffers : buffers }, buffers is the transferable list
     */
    toObject : function (geometry, shallow) {
        Iif (!geometry) {
            return null;
        }
        var data = {
            metadata : util.extend({}, META)
        };
        //transferable buffers
        var buffers = [];
 
        //dynamic
        data.dynamic = geometry.dynamic;
 
        //bounding box
        Eif (geometry.boundingBox) {
            data.boundingBox = {
                min : geometry.boundingBox.min.toArray(),
                max : geometry.boundingBox.max.toArray()
            };
        }
 
        //indices
        Eif (geometry.indices && geometry.indices.length > 0) {
            data.indices = copyIfNecessary(geometry.indices, shallow);
            buffers.push(data.indices.buffer);
        }
 
        //attributes
        data.attributes = {};
        for (var p in geometry.attributes) {
            Eif (geometry.attributes.hasOwnProperty(p)) {
                var attr = geometry.attributes[p];
                //ignore empty attributes
                if (attr && attr.value && attr.value.length > 0) {
                    attr = data.attributes[p] = copyAttribute(attr, shallow);
                    buffers.push(attr.value.buffer);
                }
            }
        }
 
        return {
            data : data,
            buffers : buffers
        };
    },
 
    /**
     * Reproduce a geometry from object generated by toObject
     * @param {Object} object object generated by toObject
     * @returns {Geometry} geometry
     */
    toGeometry : function (object) {
        Iif (!object) {
            return null;
        }
        if (object.data && object.buffers) {
            return transferableUtil.toGeometry(object.data);
        }
        Iif (!object.metadata || object.metadata.generator !== META.generator) {
            throw new Error('[util.transferable.toGeometry] the object is not generated by util.transferable.');
        }
 
        //basic options
        var options = {
            dynamic : object.dynamic,
            indices : object.indices
        };
 
        Eif (object.boundingBox) {
            var min = new Vector3().setArray(object.boundingBox.min);
            var max = new Vector3().setArray(object.boundingBox.max);
            options.boundingBox = new BoundingBox(min, max);
        }
 
        var geometry = new Geometry(options);
 
        //attributes
        for (var p in object.attributes) {
            Eif (object.attributes.hasOwnProperty(p)) {
                var attr = object.attributes[p];
                geometry.attributes[p] = new Geometry.Attribute(attr.name, attr.type, attr.size, attr.semantic);
                geometry.attributes[p].value = attr.value;
            }
        }
 
        return geometry;
    }
 
}
 
function copyAttribute(attr, shallow) {
    return {
        name : attr.name,
        type : attr.type,
        size : attr.size,
        semantic : attr.semantic,
        value : copyIfNecessary(attr.value, shallow)
    };
}
 
function copyIfNecessary(arr, shallow) {
    if (!shallow) {
        return new arr.constructor(arr);
    } else {
        return arr;
    }
}
 
export default transferableUtil;