tcuMatrixUtil.js (2292B)
1 /*------------------------------------------------------------------------- 2 * drawElements Quality Program OpenGL ES Utilities 3 * ------------------------------------------------ 4 * 5 * Copyright 2014 The Android Open Source Project 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * 19 */ 20 'use strict'; 21 goog.provide('framework.common.tcuMatrixUtil'); 22 goog.require('framework.common.tcuMatrix'); 23 24 goog.scope(function() { 25 26 var tcuMatrixUtil = framework.common.tcuMatrixUtil; 27 var tcuMatrix = framework.common.tcuMatrix; 28 29 /** 30 * @param {Array<number>} translation 31 * @return {tcuMatrix.Matrix} 32 */ 33 tcuMatrixUtil.translationMatrix = function(translation) { 34 var len = translation.length; 35 var res = new tcuMatrix.Matrix(len + 1, len + 1); 36 for (var row = 0; row < len; row++) 37 res.set(row, len, translation[row]); 38 return res; 39 }; 40 41 /** 42 * Flatten an array of arrays or matrices 43 * @param {(Array<Array<number>> | Array<tcuMatrix.Matrix>)} a 44 * @return {Array<number>} 45 */ 46 tcuMatrixUtil.flatten = function(a) { 47 if (a[0] instanceof Array) { 48 var merged = []; 49 return merged.concat.apply(merged, a); 50 } 51 52 if (a[0] instanceof tcuMatrix.Matrix) { 53 /** @type {tcuMatrix.Matrix} */ var m = a[0]; 54 var rows = m.rows; 55 var cols = m.cols; 56 var size = a.length; 57 var result = []; 58 for (var col = 0; col < cols; col++) 59 for (var i = 0; i < size; i++) 60 result.push(a[i].getColumn(col)); 61 return [].concat.apply([], result); 62 } 63 64 if (typeof(a[0]) === 'number') 65 return a; 66 67 throw new Error('Invalid input'); 68 }; 69 70 });