How to use dst method in wpt

Best JavaScript code snippet using wpt

m4.js

Source:m4.js Github

copy

Full Screen

1/*2 * Copyright 2014, Gregg Tavares.3 * All rights reserved.4 *5 * Redistribution and use in source and binary forms, with or without6 * modification, are permitted provided that the following conditions are7 * met:8 *9 * * Redistributions of source code must retain the above copyright10 * notice, this list of conditions and the following disclaimer.11 * * Redistributions in binary form must reproduce the above12 * copyright notice, this list of conditions and the following disclaimer13 * in the documentation and/or other materials provided with the14 * distribution.15 * * Neither the name of Gregg Tavares. nor the names of his16 * contributors may be used to endorse or promote products derived from17 * this software without specific prior written permission.18 *19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30 */31/**32 * Various 3d math functions.33 *34 * @module webgl-3d-math35 */36(function(root, factory) { // eslint-disable-line37 if (typeof define === 'function' && define.amd) {38 // AMD. Register as an anonymous module.39 define([], factory);40 } else {41 // Browser globals42 root.m4 = factory();43 }44}(this, function() {45 "use strict";46 /**47 * An array or typed array with 3 values.48 * @typedef {number[]|TypedArray} Vector349 * @memberOf module:webgl-3d-math50 */51 /**52 * An array or typed array with 4 values.53 * @typedef {number[]|TypedArray} Vector454 * @memberOf module:webgl-3d-math55 */56 /**57 * An array or typed array with 16 values.58 * @typedef {number[]|TypedArray} Matrix459 * @memberOf module:webgl-3d-math60 */61 /**62 * Takes two 4-by-4 matrices, a and b, and computes the product in the order63 * that pre-composes b with a. In other words, the matrix returned will64 * transform by b first and then a. Note this is subtly different from just65 * multiplying the matrices together. For given a and b, this function returns66 * the same object in both row-major and column-major mode.67 * @param {Matrix4} a A matrix.68 * @param {Matrix4} b A matrix.69 * @param {Matrix4} [dst] optional matrix to store result70 * @return {Matrix4} dst or a new matrix if none provided71 */72 function multiply(a, b, dst) {73 dst = dst || new Float32Array(16);74 var b00 = b[0 * 4 + 0];75 var b01 = b[0 * 4 + 1];76 var b02 = b[0 * 4 + 2];77 var b03 = b[0 * 4 + 3];78 var b10 = b[1 * 4 + 0];79 var b11 = b[1 * 4 + 1];80 var b12 = b[1 * 4 + 2];81 var b13 = b[1 * 4 + 3];82 var b20 = b[2 * 4 + 0];83 var b21 = b[2 * 4 + 1];84 var b22 = b[2 * 4 + 2];85 var b23 = b[2 * 4 + 3];86 var b30 = b[3 * 4 + 0];87 var b31 = b[3 * 4 + 1];88 var b32 = b[3 * 4 + 2];89 var b33 = b[3 * 4 + 3];90 var a00 = a[0 * 4 + 0];91 var a01 = a[0 * 4 + 1];92 var a02 = a[0 * 4 + 2];93 var a03 = a[0 * 4 + 3];94 var a10 = a[1 * 4 + 0];95 var a11 = a[1 * 4 + 1];96 var a12 = a[1 * 4 + 2];97 var a13 = a[1 * 4 + 3];98 var a20 = a[2 * 4 + 0];99 var a21 = a[2 * 4 + 1];100 var a22 = a[2 * 4 + 2];101 var a23 = a[2 * 4 + 3];102 var a30 = a[3 * 4 + 0];103 var a31 = a[3 * 4 + 1];104 var a32 = a[3 * 4 + 2];105 var a33 = a[3 * 4 + 3];106 dst[ 0] = b00 * a00 + b01 * a10 + b02 * a20 + b03 * a30;107 dst[ 1] = b00 * a01 + b01 * a11 + b02 * a21 + b03 * a31;108 dst[ 2] = b00 * a02 + b01 * a12 + b02 * a22 + b03 * a32;109 dst[ 3] = b00 * a03 + b01 * a13 + b02 * a23 + b03 * a33;110 dst[ 4] = b10 * a00 + b11 * a10 + b12 * a20 + b13 * a30;111 dst[ 5] = b10 * a01 + b11 * a11 + b12 * a21 + b13 * a31;112 dst[ 6] = b10 * a02 + b11 * a12 + b12 * a22 + b13 * a32;113 dst[ 7] = b10 * a03 + b11 * a13 + b12 * a23 + b13 * a33;114 dst[ 8] = b20 * a00 + b21 * a10 + b22 * a20 + b23 * a30;115 dst[ 9] = b20 * a01 + b21 * a11 + b22 * a21 + b23 * a31;116 dst[10] = b20 * a02 + b21 * a12 + b22 * a22 + b23 * a32;117 dst[11] = b20 * a03 + b21 * a13 + b22 * a23 + b23 * a33;118 dst[12] = b30 * a00 + b31 * a10 + b32 * a20 + b33 * a30;119 dst[13] = b30 * a01 + b31 * a11 + b32 * a21 + b33 * a31;120 dst[14] = b30 * a02 + b31 * a12 + b32 * a22 + b33 * a32;121 dst[15] = b30 * a03 + b31 * a13 + b32 * a23 + b33 * a33;122 return dst;123 }124 /**125 * adds 2 vectors3s126 * @param {Vector3} a a127 * @param {Vector3} b b128 * @param {Vector3} dst optional vector3 to store result129 * @return {Vector3} dst or new Vector3 if not provided130 * @memberOf module:webgl-3d-math131 */132 function addVectors(a, b, dst) {133 dst = dst || new Float32Array(3);134 dst[0] = a[0] + b[0];135 dst[1] = a[1] + b[1];136 dst[2] = a[2] + b[2];137 return dst;138 }139 /**140 * subtracts 2 vectors3s141 * @param {Vector3} a a142 * @param {Vector3} b b143 * @param {Vector3} dst optional vector3 to store result144 * @return {Vector3} dst or new Vector3 if not provided145 * @memberOf module:webgl-3d-math146 */147 function subtractVectors(a, b, dst) {148 dst = dst || new Float32Array(3);149 dst[0] = a[0] - b[0];150 dst[1] = a[1] - b[1];151 dst[2] = a[2] - b[2];152 return dst;153 }154 /**155 * normalizes a vector.156 * @param {Vector3} v vector to normalzie157 * @param {Vector3} dst optional vector3 to store result158 * @return {Vector3} dst or new Vector3 if not provided159 * @memberOf module:webgl-3d-math160 */161 function normalize(v, dst) {162 dst = dst || new Float32Array(3);163 var length = Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);164 // make sure we don't divide by 0.165 if (length > 0.00001) {166 dst[0] = v[0] / length;167 dst[1] = v[1] / length;168 dst[2] = v[2] / length;169 }170 return dst;171 }172 /**173 * Computes the cross product of 2 vectors3s174 * @param {Vector3} a a175 * @param {Vector3} b b176 * @param {Vector3} dst optional vector3 to store result177 * @return {Vector3} dst or new Vector3 if not provided178 * @memberOf module:webgl-3d-math179 */180 function cross(a, b, dst) {181 dst = dst || new Float32Array(3);182 dst[0] = a[1] * b[2] - a[2] * b[1];183 dst[1] = a[2] * b[0] - a[0] * b[2];184 dst[2] = a[0] * b[1] - a[1] * b[0];185 return dst;186 }187 /**188 * Computes the dot product of two vectors; assumes both vectors have189 * three entries.190 * @param {Vector3} a Operand vector.191 * @param {Vector3} b Operand vector.192 * @return {number} dot product193 * @memberOf module:webgl-3d-math194 */195 function dot(a, b) {196 return (a[0] * b[0]) + (a[1] * b[1]) + (a[2] * b[2]);197 }198 /**199 * Computes the distance squared between 2 points200 * @param {Vector3} a201 * @param {Vector3} b202 * @return {nubmer} distance squared between a and b203 */204 function distanceSq(a, b) {205 const dx = a[0] - b[0];206 const dy = a[1] - b[1];207 const dz = a[2] - b[2];208 return dx * dx + dy * dy + dz * dz;209 }210 /**211 * Computes the distance between 2 points212 * @param {Vector3} a213 * @param {Vector3} b214 * @return {nubmer} distance between a and b215 */216 function distance(a, b) {217 return Math.sqrt(distanceSq(a, b));218 }219 /**220 * Makes an identity matrix.221 * @param {Matrix4} [dst] optional matrix to store result222 * @return {Matrix4} dst or a new matrix if none provided223 * @memberOf module:webgl-3d-math224 */225 function identity(dst) {226 dst = dst || new Float32Array(16);227 dst[ 0] = 1;228 dst[ 1] = 0;229 dst[ 2] = 0;230 dst[ 3] = 0;231 dst[ 4] = 0;232 dst[ 5] = 1;233 dst[ 6] = 0;234 dst[ 7] = 0;235 dst[ 8] = 0;236 dst[ 9] = 0;237 dst[10] = 1;238 dst[11] = 0;239 dst[12] = 0;240 dst[13] = 0;241 dst[14] = 0;242 dst[15] = 1;243 return dst;244 }245 /**246 * Transposes a matrix.247 * @param {Matrix4} m matrix to transpose.248 * @param {Matrix4} [dst] optional matrix to store result249 * @return {Matrix4} dst or a new matrix if none provided250 * @memberOf module:webgl-3d-math251 */252 function transpose(m, dst) {253 dst = dst || new Float32Array(16);254 dst[ 0] = m[0];255 dst[ 1] = m[4];256 dst[ 2] = m[8];257 dst[ 3] = m[12];258 dst[ 4] = m[1];259 dst[ 5] = m[5];260 dst[ 6] = m[9];261 dst[ 7] = m[13];262 dst[ 8] = m[2];263 dst[ 9] = m[6];264 dst[10] = m[10];265 dst[11] = m[14];266 dst[12] = m[3];267 dst[13] = m[7];268 dst[14] = m[11];269 dst[15] = m[15];270 return dst;271 }272 /**273 * Creates a lookAt matrix.274 * This is a world matrix for a camera. In other words it will transform275 * from the origin to a place and orientation in the world. For a view276 * matrix take the inverse of this.277 * @param {Vector3} cameraPosition position of the camera278 * @param {Vector3} target position of the target279 * @param {Vector3} up direction280 * @param {Matrix4} [dst] optional matrix to store result281 * @return {Matrix4} dst or a new matrix if none provided282 * @memberOf module:webgl-3d-math283 */284 function lookAt(cameraPosition, target, up, dst) {285 dst = dst || new Float32Array(16);286 var zAxis = normalize(287 subtractVectors(cameraPosition, target));288 var xAxis = normalize(cross(up, zAxis));289 var yAxis = normalize(cross(zAxis, xAxis));290 dst[ 0] = xAxis[0];291 dst[ 1] = xAxis[1];292 dst[ 2] = xAxis[2];293 dst[ 3] = 0;294 dst[ 4] = yAxis[0];295 dst[ 5] = yAxis[1];296 dst[ 6] = yAxis[2];297 dst[ 7] = 0;298 dst[ 8] = zAxis[0];299 dst[ 9] = zAxis[1];300 dst[10] = zAxis[2];301 dst[11] = 0;302 dst[12] = cameraPosition[0];303 dst[13] = cameraPosition[1];304 dst[14] = cameraPosition[2];305 dst[15] = 1;306 return dst;307 }308 /**309 * Computes a 4-by-4 perspective transformation matrix given the angular height310 * of the frustum, the aspect ratio, and the near and far clipping planes. The311 * arguments define a frustum extending in the negative z direction. The given312 * angle is the vertical angle of the frustum, and the horizontal angle is313 * determined to produce the given aspect ratio. The arguments near and far are314 * the distances to the near and far clipping planes. Note that near and far315 * are not z coordinates, but rather they are distances along the negative316 * z-axis. The matrix generated sends the viewing frustum to the unit box.317 * We assume a unit box extending from -1 to 1 in the x and y dimensions and318 * from -1 to 1 in the z dimension.319 * @param {number} fieldOfViewInRadians field of view in y axis.320 * @param {number} aspect aspect of viewport (width / height)321 * @param {number} near near Z clipping plane322 * @param {number} far far Z clipping plane323 * @param {Matrix4} [dst] optional matrix to store result324 * @return {Matrix4} dst or a new matrix if none provided325 * @memberOf module:webgl-3d-math326 */327 function perspective(fieldOfViewInRadians, aspect, near, far, dst) {328 dst = dst || new Float32Array(16);329 var f = Math.tan(Math.PI * 0.5 - 0.5 * fieldOfViewInRadians);330 var rangeInv = 1.0 / (near - far);331 dst[ 0] = f / aspect;332 dst[ 1] = 0;333 dst[ 2] = 0;334 dst[ 3] = 0;335 dst[ 4] = 0;336 dst[ 5] = f;337 dst[ 6] = 0;338 dst[ 7] = 0;339 dst[ 8] = 0;340 dst[ 9] = 0;341 dst[10] = (near + far) * rangeInv;342 dst[11] = -1;343 dst[12] = 0;344 dst[13] = 0;345 dst[14] = near * far * rangeInv * 2;346 dst[15] = 0;347 return dst;348 }349 /**350 * Computes a 4-by-4 orthographic projection matrix given the coordinates of the351 * planes defining the axis-aligned, box-shaped viewing volume. The matrix352 * generated sends that box to the unit box. Note that although left and right353 * are x coordinates and bottom and top are y coordinates, near and far354 * are not z coordinates, but rather they are distances along the negative355 * z-axis. We assume a unit box extending from -1 to 1 in the x and y356 * dimensions and from -1 to 1 in the z dimension.357 * @param {number} left The x coordinate of the left plane of the box.358 * @param {number} right The x coordinate of the right plane of the box.359 * @param {number} bottom The y coordinate of the bottom plane of the box.360 * @param {number} top The y coordinate of the right plane of the box.361 * @param {number} near The negative z coordinate of the near plane of the box.362 * @param {number} far The negative z coordinate of the far plane of the box.363 * @param {Matrix4} [dst] optional matrix to store result364 * @return {Matrix4} dst or a new matrix if none provided365 * @memberOf module:webgl-3d-math366 */367 function orthographic(left, right, bottom, top, near, far, dst) {368 dst = dst || new Float32Array(16);369 dst[ 0] = 2 / (right - left);370 dst[ 1] = 0;371 dst[ 2] = 0;372 dst[ 3] = 0;373 dst[ 4] = 0;374 dst[ 5] = 2 / (top - bottom);375 dst[ 6] = 0;376 dst[ 7] = 0;377 dst[ 8] = 0;378 dst[ 9] = 0;379 dst[10] = 2 / (far - near);380 dst[11] = 0;381 dst[12] = (left + right) / (left - right);382 dst[13] = (bottom + top) / (bottom - top);383 dst[14] = (near + far) / (near - far);384 dst[15] = 1;385 return dst;386 }387 /**388 * Computes a 4-by-4 perspective transformation matrix given the left, right,389 * top, bottom, near and far clipping planes. The arguments define a frustum390 * extending in the negative z direction. The arguments near and far are the391 * distances to the near and far clipping planes. Note that near and far are not392 * z coordinates, but rather they are distances along the negative z-axis. The393 * matrix generated sends the viewing frustum to the unit box. We assume a unit394 * box extending from -1 to 1 in the x and y dimensions and from -1 to 1 in the z395 * dimension.396 * @param {number} left The x coordinate of the left plane of the box.397 * @param {number} right The x coordinate of the right plane of the box.398 * @param {number} bottom The y coordinate of the bottom plane of the box.399 * @param {number} top The y coordinate of the right plane of the box.400 * @param {number} near The negative z coordinate of the near plane of the box.401 * @param {number} far The negative z coordinate of the far plane of the box.402 * @param {Matrix4} [dst] optional matrix to store result403 * @return {Matrix4} dst or a new matrix if none provided404 * @memberOf module:webgl-3d-math405 */406 function frustum(left, right, bottom, top, near, far) {407 var dx = right - left;408 var dy = top - bottom;409 var dz = far - near;410 dst[ 0] = 2 * near / dx;411 dst[ 1] = 0;412 dst[ 2] = 0;413 dst[ 3] = 0;414 dst[ 4] = 0;415 dst[ 5] = 2 * near / dy;416 dst[ 6] = 0;417 dst[ 7] = 0;418 dst[ 8] = (left + right) / dx;419 dst[ 9] = (top + bottom) / dy;420 dst[10] = -(far + near) / dz;421 dst[11] = -1;422 dst[12] = 0;423 dst[13] = 0;424 dst[14] = -2 * near * far / dz;425 dst[15] = 0;426 return dst;427 }428 /**429 * Makes a translation matrix430 * @param {number} tx x translation.431 * @param {number} ty y translation.432 * @param {number} tz z translation.433 * @param {Matrix4} [dst] optional matrix to store result434 * @return {Matrix4} dst or a new matrix if none provided435 * @memberOf module:webgl-3d-math436 */437 function translation(tx, ty, tz, dst) {438 dst = dst || new Float32Array(16);439 dst[ 0] = 1;440 dst[ 1] = 0;441 dst[ 2] = 0;442 dst[ 3] = 0;443 dst[ 4] = 0;444 dst[ 5] = 1;445 dst[ 6] = 0;446 dst[ 7] = 0;447 dst[ 8] = 0;448 dst[ 9] = 0;449 dst[10] = 1;450 dst[11] = 0;451 dst[12] = tx;452 dst[13] = ty;453 dst[14] = tz;454 dst[15] = 1;455 return dst;456 }457 /**458 * Mutliply by translation matrix.459 * @param {Matrix4} m matrix to multiply460 * @param {number} tx x translation.461 * @param {number} ty y translation.462 * @param {number} tz z translation.463 * @param {Matrix4} [dst] optional matrix to store result464 * @return {Matrix4} dst or a new matrix if none provided465 * @memberOf module:webgl-3d-math466 */467 function translate(m, tx, ty, tz, dst) {468 // This is the optimized version of469 // return multiply(m, translation(tx, ty, tz), dst);470 dst = dst || new Float32Array(16);471 var m00 = m[0];472 var m01 = m[1];473 var m02 = m[2];474 var m03 = m[3];475 var m10 = m[1 * 4 + 0];476 var m11 = m[1 * 4 + 1];477 var m12 = m[1 * 4 + 2];478 var m13 = m[1 * 4 + 3];479 var m20 = m[2 * 4 + 0];480 var m21 = m[2 * 4 + 1];481 var m22 = m[2 * 4 + 2];482 var m23 = m[2 * 4 + 3];483 var m30 = m[3 * 4 + 0];484 var m31 = m[3 * 4 + 1];485 var m32 = m[3 * 4 + 2];486 var m33 = m[3 * 4 + 3];487 if (m !== dst) {488 dst[ 0] = m00;489 dst[ 1] = m01;490 dst[ 2] = m02;491 dst[ 3] = m03;492 dst[ 4] = m10;493 dst[ 5] = m11;494 dst[ 6] = m12;495 dst[ 7] = m13;496 dst[ 8] = m20;497 dst[ 9] = m21;498 dst[10] = m22;499 dst[11] = m23;500 }501 dst[12] = m00 * tx + m10 * ty + m20 * tz + m30;502 dst[13] = m01 * tx + m11 * ty + m21 * tz + m31;503 dst[14] = m02 * tx + m12 * ty + m22 * tz + m32;504 dst[15] = m03 * tx + m13 * ty + m23 * tz + m33;505 return dst;506 }507 /**508 * Makes an x rotation matrix509 * @param {number} angleInRadians amount to rotate510 * @param {Matrix4} [dst] optional matrix to store result511 * @return {Matrix4} dst or a new matrix if none provided512 * @memberOf module:webgl-3d-math513 */514 function xRotation(angleInRadians, dst) {515 dst = dst || new Float32Array(16);516 var c = Math.cos(angleInRadians);517 var s = Math.sin(angleInRadians);518 dst[ 0] = 1;519 dst[ 1] = 0;520 dst[ 2] = 0;521 dst[ 3] = 0;522 dst[ 4] = 0;523 dst[ 5] = c;524 dst[ 6] = s;525 dst[ 7] = 0;526 dst[ 8] = 0;527 dst[ 9] = -s;528 dst[10] = c;529 dst[11] = 0;530 dst[12] = 0;531 dst[13] = 0;532 dst[14] = 0;533 dst[15] = 1;534 return dst;535 }536 /**537 * Multiply by an x rotation matrix538 * @param {Matrix4} m matrix to multiply539 * @param {number} angleInRadians amount to rotate540 * @param {Matrix4} [dst] optional matrix to store result541 * @return {Matrix4} dst or a new matrix if none provided542 * @memberOf module:webgl-3d-math543 */544 function xRotate(m, angleInRadians, dst) {545 // this is the optimized version of546 // return multiply(m, xRotation(angleInRadians), dst);547 dst = dst || new Float32Array(16);548 var m10 = m[4];549 var m11 = m[5];550 var m12 = m[6];551 var m13 = m[7];552 var m20 = m[8];553 var m21 = m[9];554 var m22 = m[10];555 var m23 = m[11];556 var c = Math.cos(angleInRadians);557 var s = Math.sin(angleInRadians);558 dst[4] = c * m10 + s * m20;559 dst[5] = c * m11 + s * m21;560 dst[6] = c * m12 + s * m22;561 dst[7] = c * m13 + s * m23;562 dst[8] = c * m20 - s * m10;563 dst[9] = c * m21 - s * m11;564 dst[10] = c * m22 - s * m12;565 dst[11] = c * m23 - s * m13;566 if (m !== dst) {567 dst[ 0] = m[ 0];568 dst[ 1] = m[ 1];569 dst[ 2] = m[ 2];570 dst[ 3] = m[ 3];571 dst[12] = m[12];572 dst[13] = m[13];573 dst[14] = m[14];574 dst[15] = m[15];575 }576 return dst;577 }578 /**579 * Makes an y rotation matrix580 * @param {number} angleInRadians amount to rotate581 * @param {Matrix4} [dst] optional matrix to store result582 * @return {Matrix4} dst or a new matrix if none provided583 * @memberOf module:webgl-3d-math584 */585 function yRotation(angleInRadians, dst) {586 dst = dst || new Float32Array(16);587 var c = Math.cos(angleInRadians);588 var s = Math.sin(angleInRadians);589 dst[ 0] = c;590 dst[ 1] = 0;591 dst[ 2] = -s;592 dst[ 3] = 0;593 dst[ 4] = 0;594 dst[ 5] = 1;595 dst[ 6] = 0;596 dst[ 7] = 0;597 dst[ 8] = s;598 dst[ 9] = 0;599 dst[10] = c;600 dst[11] = 0;601 dst[12] = 0;602 dst[13] = 0;603 dst[14] = 0;604 dst[15] = 1;605 return dst;606 }607 /**608 * Multiply by an y rotation matrix609 * @param {Matrix4} m matrix to multiply610 * @param {number} angleInRadians amount to rotate611 * @param {Matrix4} [dst] optional matrix to store result612 * @return {Matrix4} dst or a new matrix if none provided613 * @memberOf module:webgl-3d-math614 */615 function yRotate(m, angleInRadians, dst) {616 // this is the optimized verison of617 // return multiply(m, yRotation(angleInRadians), dst);618 dst = dst || new Float32Array(16);619 var m00 = m[0 * 4 + 0];620 var m01 = m[0 * 4 + 1];621 var m02 = m[0 * 4 + 2];622 var m03 = m[0 * 4 + 3];623 var m20 = m[2 * 4 + 0];624 var m21 = m[2 * 4 + 1];625 var m22 = m[2 * 4 + 2];626 var m23 = m[2 * 4 + 3];627 var c = Math.cos(angleInRadians);628 var s = Math.sin(angleInRadians);629 dst[ 0] = c * m00 - s * m20;630 dst[ 1] = c * m01 - s * m21;631 dst[ 2] = c * m02 - s * m22;632 dst[ 3] = c * m03 - s * m23;633 dst[ 8] = c * m20 + s * m00;634 dst[ 9] = c * m21 + s * m01;635 dst[10] = c * m22 + s * m02;636 dst[11] = c * m23 + s * m03;637 if (m !== dst) {638 dst[ 4] = m[ 4];639 dst[ 5] = m[ 5];640 dst[ 6] = m[ 6];641 dst[ 7] = m[ 7];642 dst[12] = m[12];643 dst[13] = m[13];644 dst[14] = m[14];645 dst[15] = m[15];646 }647 return dst;648 }649 /**650 * Makes an z rotation matrix651 * @param {number} angleInRadians amount to rotate652 * @param {Matrix4} [dst] optional matrix to store result653 * @return {Matrix4} dst or a new matrix if none provided654 * @memberOf module:webgl-3d-math655 */656 function zRotation(angleInRadians, dst) {657 dst = dst || new Float32Array(16);658 var c = Math.cos(angleInRadians);659 var s = Math.sin(angleInRadians);660 dst[ 0] = c;661 dst[ 1] = s;662 dst[ 2] = 0;663 dst[ 3] = 0;664 dst[ 4] = -s;665 dst[ 5] = c;666 dst[ 6] = 0;667 dst[ 7] = 0;668 dst[ 8] = 0;669 dst[ 9] = 0;670 dst[10] = 1;671 dst[11] = 0;672 dst[12] = 0;673 dst[13] = 0;674 dst[14] = 0;675 dst[15] = 1;676 return dst;677 }678 /**679 * Multiply by an z rotation matrix680 * @param {Matrix4} m matrix to multiply681 * @param {number} angleInRadians amount to rotate682 * @param {Matrix4} [dst] optional matrix to store result683 * @return {Matrix4} dst or a new matrix if none provided684 * @memberOf module:webgl-3d-math685 */686 function zRotate(m, angleInRadians, dst) {687 // This is the optimized verison of688 // return multiply(m, zRotation(angleInRadians), dst);689 dst = dst || new Float32Array(16);690 var m00 = m[0 * 4 + 0];691 var m01 = m[0 * 4 + 1];692 var m02 = m[0 * 4 + 2];693 var m03 = m[0 * 4 + 3];694 var m10 = m[1 * 4 + 0];695 var m11 = m[1 * 4 + 1];696 var m12 = m[1 * 4 + 2];697 var m13 = m[1 * 4 + 3];698 var c = Math.cos(angleInRadians);699 var s = Math.sin(angleInRadians);700 dst[ 0] = c * m00 + s * m10;701 dst[ 1] = c * m01 + s * m11;702 dst[ 2] = c * m02 + s * m12;703 dst[ 3] = c * m03 + s * m13;704 dst[ 4] = c * m10 - s * m00;705 dst[ 5] = c * m11 - s * m01;706 dst[ 6] = c * m12 - s * m02;707 dst[ 7] = c * m13 - s * m03;708 if (m !== dst) {709 dst[ 8] = m[ 8];710 dst[ 9] = m[ 9];711 dst[10] = m[10];712 dst[11] = m[11];713 dst[12] = m[12];714 dst[13] = m[13];715 dst[14] = m[14];716 dst[15] = m[15];717 }718 return dst;719 }720 /**721 * Makes an rotation matrix around an arbitrary axis722 * @param {Vector3} axis axis to rotate around723 * @param {number} angleInRadians amount to rotate724 * @param {Matrix4} [dst] optional matrix to store result725 * @return {Matrix4} dst or a new matrix if none provided726 * @memberOf module:webgl-3d-math727 */728 function axisRotation(axis, angleInRadians, dst) {729 dst = dst || new Float32Array(16);730 var x = axis[0];731 var y = axis[1];732 var z = axis[2];733 var n = Math.sqrt(x * x + y * y + z * z);734 x /= n;735 y /= n;736 z /= n;737 var xx = x * x;738 var yy = y * y;739 var zz = z * z;740 var c = Math.cos(angleInRadians);741 var s = Math.sin(angleInRadians);742 var oneMinusCosine = 1 - c;743 dst[ 0] = xx + (1 - xx) * c;744 dst[ 1] = x * y * oneMinusCosine + z * s;745 dst[ 2] = x * z * oneMinusCosine - y * s;746 dst[ 3] = 0;747 dst[ 4] = x * y * oneMinusCosine - z * s;748 dst[ 5] = yy + (1 - yy) * c;749 dst[ 6] = y * z * oneMinusCosine + x * s;750 dst[ 7] = 0;751 dst[ 8] = x * z * oneMinusCosine + y * s;752 dst[ 9] = y * z * oneMinusCosine - x * s;753 dst[10] = zz + (1 - zz) * c;754 dst[11] = 0;755 dst[12] = 0;756 dst[13] = 0;757 dst[14] = 0;758 dst[15] = 1;759 return dst;760 }761 /**762 * Multiply by an axis rotation matrix763 * @param {Matrix4} m matrix to multiply764 * @param {Vector3} axis axis to rotate around765 * @param {number} angleInRadians amount to rotate766 * @param {Matrix4} [dst] optional matrix to store result767 * @return {Matrix4} dst or a new matrix if none provided768 * @memberOf module:webgl-3d-math769 */770 function axisRotate(m, axis, angleInRadians, dst) {771 // This is the optimized verison of772 // return multiply(m, axisRotation(axis, angleInRadians), dst);773 dst = dst || new Float32Array(16);774 var x = axis[0];775 var y = axis[1];776 var z = axis[2];777 var n = Math.sqrt(x * x + y * y + z * z);778 x /= n;779 y /= n;780 z /= n;781 var xx = x * x;782 var yy = y * y;783 var zz = z * z;784 var c = Math.cos(angleInRadians);785 var s = Math.sin(angleInRadians);786 var oneMinusCosine = 1 - c;787 var r00 = xx + (1 - xx) * c;788 var r01 = x * y * oneMinusCosine + z * s;789 var r02 = x * z * oneMinusCosine - y * s;790 var r10 = x * y * oneMinusCosine - z * s;791 var r11 = yy + (1 - yy) * c;792 var r12 = y * z * oneMinusCosine + x * s;793 var r20 = x * z * oneMinusCosine + y * s;794 var r21 = y * z * oneMinusCosine - x * s;795 var r22 = zz + (1 - zz) * c;796 var m00 = m[0];797 var m01 = m[1];798 var m02 = m[2];799 var m03 = m[3];800 var m10 = m[4];801 var m11 = m[5];802 var m12 = m[6];803 var m13 = m[7];804 var m20 = m[8];805 var m21 = m[9];806 var m22 = m[10];807 var m23 = m[11];808 dst[ 0] = r00 * m00 + r01 * m10 + r02 * m20;809 dst[ 1] = r00 * m01 + r01 * m11 + r02 * m21;810 dst[ 2] = r00 * m02 + r01 * m12 + r02 * m22;811 dst[ 3] = r00 * m03 + r01 * m13 + r02 * m23;812 dst[ 4] = r10 * m00 + r11 * m10 + r12 * m20;813 dst[ 5] = r10 * m01 + r11 * m11 + r12 * m21;814 dst[ 6] = r10 * m02 + r11 * m12 + r12 * m22;815 dst[ 7] = r10 * m03 + r11 * m13 + r12 * m23;816 dst[ 8] = r20 * m00 + r21 * m10 + r22 * m20;817 dst[ 9] = r20 * m01 + r21 * m11 + r22 * m21;818 dst[10] = r20 * m02 + r21 * m12 + r22 * m22;819 dst[11] = r20 * m03 + r21 * m13 + r22 * m23;820 if (m !== dst) {821 dst[12] = m[12];822 dst[13] = m[13];823 dst[14] = m[14];824 dst[15] = m[15];825 }826 return dst;827 }828 /**829 * Makes a scale matrix830 * @param {number} sx x scale.831 * @param {number} sy y scale.832 * @param {number} sz z scale.833 * @param {Matrix4} [dst] optional matrix to store result834 * @return {Matrix4} dst or a new matrix if none provided835 * @memberOf module:webgl-3d-math836 */837 function scaling(sx, sy, sz, dst) {838 dst = dst || new Float32Array(16);839 dst[ 0] = sx;840 dst[ 1] = 0;841 dst[ 2] = 0;842 dst[ 3] = 0;843 dst[ 4] = 0;844 dst[ 5] = sy;845 dst[ 6] = 0;846 dst[ 7] = 0;847 dst[ 8] = 0;848 dst[ 9] = 0;849 dst[10] = sz;850 dst[11] = 0;851 dst[12] = 0;852 dst[13] = 0;853 dst[14] = 0;854 dst[15] = 1;855 return dst;856 }857 /**858 * Multiply by a scaling matrix859 * @param {Matrix4} m matrix to multiply860 * @param {number} sx x scale.861 * @param {number} sy y scale.862 * @param {number} sz z scale.863 * @param {Matrix4} [dst] optional matrix to store result864 * @return {Matrix4} dst or a new matrix if none provided865 * @memberOf module:webgl-3d-math866 */867 function scale(m, sx, sy, sz, dst) {868 // This is the optimized verison of869 // return multiply(m, scaling(sx, sy, sz), dst);870 dst = dst || new Float32Array(16);871 dst[ 0] = sx * m[0 * 4 + 0];872 dst[ 1] = sx * m[0 * 4 + 1];873 dst[ 2] = sx * m[0 * 4 + 2];874 dst[ 3] = sx * m[0 * 4 + 3];875 dst[ 4] = sy * m[1 * 4 + 0];876 dst[ 5] = sy * m[1 * 4 + 1];877 dst[ 6] = sy * m[1 * 4 + 2];878 dst[ 7] = sy * m[1 * 4 + 3];879 dst[ 8] = sz * m[2 * 4 + 0];880 dst[ 9] = sz * m[2 * 4 + 1];881 dst[10] = sz * m[2 * 4 + 2];882 dst[11] = sz * m[2 * 4 + 3];883 if (m !== dst) {884 dst[12] = m[12];885 dst[13] = m[13];886 dst[14] = m[14];887 dst[15] = m[15];888 }889 return dst;890 }891 /**892 * Computes the inverse of a matrix.893 * @param {Matrix4} m matrix to compute inverse of894 * @param {Matrix4} [dst] optional matrix to store result895 * @return {Matrix4} dst or a new matrix if none provided896 * @memberOf module:webgl-3d-math897 */898 function inverse(m, dst) {899 dst = dst || new Float32Array(16);900 var m00 = m[0 * 4 + 0];901 var m01 = m[0 * 4 + 1];902 var m02 = m[0 * 4 + 2];903 var m03 = m[0 * 4 + 3];904 var m10 = m[1 * 4 + 0];905 var m11 = m[1 * 4 + 1];906 var m12 = m[1 * 4 + 2];907 var m13 = m[1 * 4 + 3];908 var m20 = m[2 * 4 + 0];909 var m21 = m[2 * 4 + 1];910 var m22 = m[2 * 4 + 2];911 var m23 = m[2 * 4 + 3];912 var m30 = m[3 * 4 + 0];913 var m31 = m[3 * 4 + 1];914 var m32 = m[3 * 4 + 2];915 var m33 = m[3 * 4 + 3];916 var tmp_0 = m22 * m33;917 var tmp_1 = m32 * m23;918 var tmp_2 = m12 * m33;919 var tmp_3 = m32 * m13;920 var tmp_4 = m12 * m23;921 var tmp_5 = m22 * m13;922 var tmp_6 = m02 * m33;923 var tmp_7 = m32 * m03;924 var tmp_8 = m02 * m23;925 var tmp_9 = m22 * m03;926 var tmp_10 = m02 * m13;927 var tmp_11 = m12 * m03;928 var tmp_12 = m20 * m31;929 var tmp_13 = m30 * m21;930 var tmp_14 = m10 * m31;931 var tmp_15 = m30 * m11;932 var tmp_16 = m10 * m21;933 var tmp_17 = m20 * m11;934 var tmp_18 = m00 * m31;935 var tmp_19 = m30 * m01;936 var tmp_20 = m00 * m21;937 var tmp_21 = m20 * m01;938 var tmp_22 = m00 * m11;939 var tmp_23 = m10 * m01;940 var t0 = (tmp_0 * m11 + tmp_3 * m21 + tmp_4 * m31) -941 (tmp_1 * m11 + tmp_2 * m21 + tmp_5 * m31);942 var t1 = (tmp_1 * m01 + tmp_6 * m21 + tmp_9 * m31) -943 (tmp_0 * m01 + tmp_7 * m21 + tmp_8 * m31);944 var t2 = (tmp_2 * m01 + tmp_7 * m11 + tmp_10 * m31) -945 (tmp_3 * m01 + tmp_6 * m11 + tmp_11 * m31);946 var t3 = (tmp_5 * m01 + tmp_8 * m11 + tmp_11 * m21) -947 (tmp_4 * m01 + tmp_9 * m11 + tmp_10 * m21);948 var d = 1.0 / (m00 * t0 + m10 * t1 + m20 * t2 + m30 * t3);949 dst[0] = d * t0;950 dst[1] = d * t1;951 dst[2] = d * t2;952 dst[3] = d * t3;953 dst[4] = d * ((tmp_1 * m10 + tmp_2 * m20 + tmp_5 * m30) -954 (tmp_0 * m10 + tmp_3 * m20 + tmp_4 * m30));955 dst[5] = d * ((tmp_0 * m00 + tmp_7 * m20 + tmp_8 * m30) -956 (tmp_1 * m00 + tmp_6 * m20 + tmp_9 * m30));957 dst[6] = d * ((tmp_3 * m00 + tmp_6 * m10 + tmp_11 * m30) -958 (tmp_2 * m00 + tmp_7 * m10 + tmp_10 * m30));959 dst[7] = d * ((tmp_4 * m00 + tmp_9 * m10 + tmp_10 * m20) -960 (tmp_5 * m00 + tmp_8 * m10 + tmp_11 * m20));961 dst[8] = d * ((tmp_12 * m13 + tmp_15 * m23 + tmp_16 * m33) -962 (tmp_13 * m13 + tmp_14 * m23 + tmp_17 * m33));963 dst[9] = d * ((tmp_13 * m03 + tmp_18 * m23 + tmp_21 * m33) -964 (tmp_12 * m03 + tmp_19 * m23 + tmp_20 * m33));965 dst[10] = d * ((tmp_14 * m03 + tmp_19 * m13 + tmp_22 * m33) -966 (tmp_15 * m03 + tmp_18 * m13 + tmp_23 * m33));967 dst[11] = d * ((tmp_17 * m03 + tmp_20 * m13 + tmp_23 * m23) -968 (tmp_16 * m03 + tmp_21 * m13 + tmp_22 * m23));969 dst[12] = d * ((tmp_14 * m22 + tmp_17 * m32 + tmp_13 * m12) -970 (tmp_16 * m32 + tmp_12 * m12 + tmp_15 * m22));971 dst[13] = d * ((tmp_20 * m32 + tmp_12 * m02 + tmp_19 * m22) -972 (tmp_18 * m22 + tmp_21 * m32 + tmp_13 * m02));973 dst[14] = d * ((tmp_18 * m12 + tmp_23 * m32 + tmp_15 * m02) -974 (tmp_22 * m32 + tmp_14 * m02 + tmp_19 * m12));975 dst[15] = d * ((tmp_22 * m22 + tmp_16 * m02 + tmp_21 * m12) -976 (tmp_20 * m12 + tmp_23 * m22 + tmp_17 * m02));977 return dst;978 }979 /**980 * Takes a matrix and a vector with 4 entries, transforms that vector by981 * the matrix, and returns the result as a vector with 4 entries.982 * @param {Matrix4} m The matrix.983 * @param {Vector4} v The point in homogenous coordinates.984 * @param {Vector4} dst optional vector4 to store result985 * @return {Vector4} dst or new Vector4 if not provided986 * @memberOf module:webgl-3d-math987 */988 function transformVector(m, v, dst) {989 dst = dst || new Float32Array(4);990 for (var i = 0; i < 4; ++i) {991 dst[i] = 0.0;992 for (var j = 0; j < 4; ++j) {993 dst[i] += v[j] * m[j * 4 + i];994 }995 }996 return dst;997 }998 /**999 * Takes a 4-by-4 matrix and a vector with 3 entries,1000 * interprets the vector as a point, transforms that point by the matrix, and1001 * returns the result as a vector with 3 entries.1002 * @param {Matrix4} m The matrix.1003 * @param {Vector3} v The point.1004 * @param {Vector4} dst optional vector4 to store result1005 * @return {Vector4} dst or new Vector4 if not provided1006 * @memberOf module:webgl-3d-math1007 */1008 function transformPoint(m, v, dst) {1009 dst = dst || new Float32Array(3);1010 var v0 = v[0];1011 var v1 = v[1];1012 var v2 = v[2];1013 var d = v0 * m[0 * 4 + 3] + v1 * m[1 * 4 + 3] + v2 * m[2 * 4 + 3] + m[3 * 4 + 3];1014 dst[0] = (v0 * m[0 * 4 + 0] + v1 * m[1 * 4 + 0] + v2 * m[2 * 4 + 0] + m[3 * 4 + 0]) / d;1015 dst[1] = (v0 * m[0 * 4 + 1] + v1 * m[1 * 4 + 1] + v2 * m[2 * 4 + 1] + m[3 * 4 + 1]) / d;1016 dst[2] = (v0 * m[0 * 4 + 2] + v1 * m[1 * 4 + 2] + v2 * m[2 * 4 + 2] + m[3 * 4 + 2]) / d;1017 return dst;1018 }1019 /**1020 * Takes a 4-by-4 matrix and a vector with 3 entries, interprets the vector as a1021 * direction, transforms that direction by the matrix, and returns the result;1022 * assumes the transformation of 3-dimensional space represented by the matrix1023 * is parallel-preserving, i.e. any combination of rotation, scaling and1024 * translation, but not a perspective distortion. Returns a vector with 31025 * entries.1026 * @param {Matrix4} m The matrix.1027 * @param {Vector3} v The direction.1028 * @param {Vector4} dst optional vector4 to store result1029 * @return {Vector4} dst or new Vector4 if not provided1030 * @memberOf module:webgl-3d-math1031 */1032 function transformDirection(m, v, dst) {1033 dst = dst || new Float32Array(3);1034 var v0 = v[0];1035 var v1 = v[1];1036 var v2 = v[2];1037 dst[0] = v0 * m[0 * 4 + 0] + v1 * m[1 * 4 + 0] + v2 * m[2 * 4 + 0];1038 dst[1] = v0 * m[0 * 4 + 1] + v1 * m[1 * 4 + 1] + v2 * m[2 * 4 + 1];1039 dst[2] = v0 * m[0 * 4 + 2] + v1 * m[1 * 4 + 2] + v2 * m[2 * 4 + 2];1040 return dst;1041 }1042 /**1043 * Takes a 4-by-4 matrix m and a vector v with 3 entries, interprets the vector1044 * as a normal to a surface, and computes a vector which is normal upon1045 * transforming that surface by the matrix. The effect of this function is the1046 * same as transforming v (as a direction) by the inverse-transpose of m. This1047 * function assumes the transformation of 3-dimensional space represented by the1048 * matrix is parallel-preserving, i.e. any combination of rotation, scaling and1049 * translation, but not a perspective distortion. Returns a vector with 31050 * entries.1051 * @param {Matrix4} m The matrix.1052 * @param {Vector3} v The normal.1053 * @param {Vector3} [dst] The direction.1054 * @return {Vector3} The transformed direction.1055 * @memberOf module:webgl-3d-math1056 */1057 function transformNormal(m, v, dst) {1058 dst = dst || new Float32Array(3);1059 var mi = inverse(m);1060 var v0 = v[0];1061 var v1 = v[1];1062 var v2 = v[2];1063 dst[0] = v0 * mi[0 * 4 + 0] + v1 * mi[0 * 4 + 1] + v2 * mi[0 * 4 + 2];1064 dst[1] = v0 * mi[1 * 4 + 0] + v1 * mi[1 * 4 + 1] + v2 * mi[1 * 4 + 2];1065 dst[2] = v0 * mi[2 * 4 + 0] + v1 * mi[2 * 4 + 1] + v2 * mi[2 * 4 + 2];1066 return dst;1067 }1068 function copy(src, dst) {1069 dst = dst || new Float32Array(16);1070 dst[ 0] = src[ 0];1071 dst[ 1] = src[ 1];1072 dst[ 2] = src[ 2];1073 dst[ 3] = src[ 3];1074 dst[ 4] = src[ 4];1075 dst[ 5] = src[ 5];1076 dst[ 6] = src[ 6];1077 dst[ 7] = src[ 7];1078 dst[ 8] = src[ 8];1079 dst[ 9] = src[ 9];1080 dst[10] = src[10];1081 dst[11] = src[11];1082 dst[12] = src[12];1083 dst[13] = src[13];1084 dst[14] = src[14];1085 dst[15] = src[15];1086 return dst;1087 }1088 return {1089 copy: copy,1090 lookAt: lookAt,1091 addVectors: addVectors,1092 subtractVectors: subtractVectors,1093 distance: distance,1094 distanceSq: distanceSq,1095 normalize: normalize,1096 cross: cross,1097 dot: dot,1098 identity: identity,1099 transpose: transpose,1100 orthographic: orthographic,1101 frustum: frustum,1102 perspective: perspective,1103 translation: translation,1104 translate: translate,1105 xRotation: xRotation,1106 yRotation: yRotation,1107 zRotation: zRotation,1108 xRotate: xRotate,1109 yRotate: yRotate,1110 zRotate: zRotate,1111 axisRotation: axisRotation,1112 axisRotate: axisRotate,1113 scaling: scaling,1114 scale: scale,1115 multiply: multiply,1116 inverse: inverse,1117 transformVector: transformVector,1118 transformPoint: transformPoint,1119 transformDirection: transformDirection,1120 transformNormal: transformNormal,1121 };...

Full Screen

Full Screen

ummath.js

Source:ummath.js Github

copy

Full Screen

1/*jslint devel:true*/2(function () {3 "use strict";4 var UMVec3d,5 UMVec4d,6 UMMat44d,7 UMBox,8 EPSILON = 0.00001;9 UMVec3d = function (x, y, z) {10 this.xyz = [0.0, 0.0, 0.0];11 if (x && x instanceof Array) {12 this.xyz = x;13 } else {14 if (x) { this.xyz[0] = x; }15 if (y) { this.xyz[1] = y; }16 if (z) { this.xyz[2] = z; }17 }18 };19 UMVec3d.prototype.x = function () {20 return this.xyz[0];21 };22 UMVec3d.prototype.y = function () {23 return this.xyz[1];24 };25 UMVec3d.prototype.z = function () {26 return this.xyz[2];27 };28 UMVec3d.prototype.at = function (index) {29 return this.xyz[index];30 };31 UMVec3d.prototype.dot = function (v) {32 return this.xyz[0] * v.xyz[0] + this.xyz[1] * v.xyz[1] + this.xyz[2] * v.xyz[2];33 };34 UMVec3d.prototype.scale = function (s) {35 this.xyz[0] = this.xyz[0] * s;36 this.xyz[1] = this.xyz[1] * s;37 this.xyz[2] = this.xyz[2] * s;38 return this;39 };40 UMVec3d.prototype.cross = function (v) {41 return new UMVec3d(42 this.y() * v.z() - this.z() * v.y(),43 this.z() * v.x() - this.x() * v.z(),44 this.x() * v.y() - this.y() * v.x()45 );46 };47 UMVec3d.prototype.add = function (v) {48 return new UMVec3d(this.x() + v.x(), this.y() + v.y(), this.z() + v.z());49 };50 UMVec3d.prototype.sub = function (v) {51 return new UMVec3d(this.x() - v.x(), this.y() - v.y(), this.z() - v.z());52 };53 UMVec3d.prototype.multiply = function (v) {54 return new UMVec3d(this.x() * v.x(), this.y() * v.y(), this.z() * v.z());55 };56 UMVec3d.prototype.normalized = function () {57 var dst = new UMVec3d(this.x(), this.y(), this.z()),58 a = this.x() * this.x() + this.y() * this.y() + this.z() * this.z(),59 b;60 if (a > EPSILON) {61 b = 1.0 / Math.sqrt(a);62 dst.xyz[0] = this.x() * b;63 dst.xyz[1] = this.y() * b;64 dst.xyz[2] = this.z() * b;65 } else {66 dst.xyz[0] = dst.xyz[1] = dst.xyz[2] = 0;67 }68 return dst;69 };70 UMVec3d.prototype.length_sq = function () {71 return this.dot(this);72 };73 UMVec3d.prototype.length = function () {74 return Math.sqrt(this.length_sq());75 };76 //------------------------------------------------------------------------77 UMVec4d = function (x, y, z, w) {78 this.xyzw = [0.0, 0.0, 0.0, 0.0];79 if (x && x instanceof Array) {80 this.xyzw = x;81 } else {82 if (x) { this.xyzw[0] = x; }83 if (y) { this.xyzw[1] = y; }84 if (z) { this.xyzw[2] = z; }85 if (w) { this.xyzw[3] = w; }86 }87 };88 UMVec4d.prototype.x = function () {89 return this.xyzw[0];90 };91 UMVec4d.prototype.y = function () {92 return this.xyzw[1];93 };94 UMVec4d.prototype.z = function () {95 return this.xyzw[2];96 };97 UMVec4d.prototype.w = function () {98 return this.xyzw[3];99 };100 UMVec4d.prototype.at = function (index) {101 return this.xyzw[index];102 };103 UMVec4d.prototype.dot = function (v) {104 return this.xyzw[0] * v.xyzw[0] + this.xyzw[1] * v.xyzw[1] + this.xyzw[2] * v.xyzw[2] + this.xyzw[3] * v.xyzw[3];105 };106 UMVec4d.prototype.scale = function (s) {107 this.xyzw[0] = this.xyzw[0] * s;108 this.xyzw[1] = this.xyzw[1] * s;109 this.xyzw[2] = this.xyzw[2] * s;110 this.xyzw[3] = this.xyzw[3] * s;111 };112 UMVec4d.prototype.cross = function (v1, v2) {113 return new UMVec4d((this.y() * (v1.z() * v2.w() - v2.z() * v1.w())114 - this.z() * (v1.y() * v2.w() - v2.y() * v1.w())115 + this.w() * (v1.y() * v2.z() - v1.z() * v2.y())),116 -(this.x() * (v1.z() * v2.w() - v2.z() * v1.w())117 - this.z() * (v1.x() * v2.w() - v2.x() * v1.w())118 + this.w() * (v1.x() * v2.z() - v1.z() * v2.x())),119 (this.x() * (v1.y() * v2.w() - v2.y() * v1.w())120 - this.y() * (v1.x() * v2.w() - v2.x()) * v1.w()121 + this.w() * (v1.x() * v2.y() - v2.x() * v1.y())),122 -(this.x() * (v1.y() * v2.z() - v2.y() * v1.z())123 - this.y() * (v1.x() * v2.z() - v2.x() * v1.z())124 + this.z() * (v1.x() * v2.y() - v2.x() * v1.y()))125 );126 };127 UMVec4d.prototype.add = function (v) {128 if (v instanceof UMVec4d) {129 return new UMVec4d(this.x() + v.x(), this.y() + v.y(), this.z() + v.z(), this.w() + v.w());130 } else {131 return new UMVec3d(this.x() + v.x(), this.y() + v.y(), this.z() + v.z());132 }133 };134 UMVec4d.prototype.sub = function (v) {135 return new UMVec4d(this.x() - v.x(), this.y() - v.y(), this.z() - v.z(), this.w() - v.w());136 };137 UMVec4d.prototype.multiply = function (v) {138 return new UMVec4d(this.x() * v.x(), this.y() * v.y(), this.z() * v.z(), this.w() * v.w());139 };140 UMVec4d.prototype.normalized = function () {141 var dst = new UMVec4d(this.x(), this.y(), this.z(), this.w()),142 a = this.x() * this.x() + this.y() * this.y() + this.z() * this.z() + this.w() * this.w(),143 b;144 if (a > EPSILON) {145 b = 1.0 / Math.sqrt(a);146 dst.xyzw[0] = this.x() * b;147 dst.xyzw[1] = this.y() * b;148 dst.xyzw[2] = this.z() * b;149 dst.xyzw[3] = this.w() * b;150 } else {151 dst.xyzw[0] = dst.xyzw[1] = dst.xyzw[2] = dst.xyzw[3] = 0;152 }153 return dst;154 };155 UMVec4d.prototype.length_sq = function () {156 return this.dot(this);157 };158 UMVec4d.prototype.length = function () {159 return Math.sqrt(this.length_sq());160 };161 //------------------------------------------------------------------------162 UMMat44d = function (mat) {163 var i,164 k;165 this.m = [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]];166 if (mat) {167 for (i = 0; i < 4; i = i + 1) {168 for (k = 0; k < 4; k = k + 1) {169 this.m[i][k] = mat.m[i][k];170 }171 }172 } else {173 for (i = 0; i < 4; i = i + 1) {174 for (k = 0; k < 4; k = k + 1) {175 this.m[i][k] = (i === k) ? 1 : 0;176 }177 }178 }179 };180 UMMat44d.prototype.value = function () {181 return [this.m[0][0], this.m[0][1], this.m[0][2], this.m[0][3],182 this.m[1][0], this.m[1][1], this.m[1][2], this.m[1][3],183 this.m[2][0], this.m[2][1], this.m[2][2], this.m[2][3],184 this.m[3][0], this.m[3][1], this.m[3][2], this.m[3][3]185 ];186 };187 UMMat44d.prototype.multiply = function (vecOrMat) {188 var i,189 j,190 k,191 tmp,192 dst;193 if (vecOrMat instanceof UMMat44d) {194 dst = new UMMat44d();195 for (i = 0; i < 4; i = i + 1) {196 for (j = 0; j < 4; j = j + 1) {197 dst.m[i][j] = 0.0;198 for (k = 0; k < 4; k = k + 1) {199 dst.m[i][j] += this.m[i][k] * vecOrMat.m[k][j];200 }201 }202 }203 } else if (vecOrMat instanceof UMVec4d) {204 tmp = [205 vecOrMat.at(0) * this.m[0][0] + vecOrMat.at(1) * this.m[1][0] + vecOrMat.at(2) * this.m[2][0] + vecOrMat.at(3) * this.m[3][0],206 vecOrMat.at(0) * this.m[0][1] + vecOrMat.at(1) * this.m[1][1] + vecOrMat.at(2) * this.m[2][1] + vecOrMat.at(3) * this.m[3][1],207 vecOrMat.at(0) * this.m[0][2] + vecOrMat.at(1) * this.m[1][2] + vecOrMat.at(2) * this.m[2][2] + vecOrMat.at(3) * this.m[3][2],208 vecOrMat.at(0) * this.m[0][3] + vecOrMat.at(1) * this.m[1][3] + vecOrMat.at(2) * this.m[2][3] + vecOrMat.at(3) * this.m[3][3]209 ];210 dst = new UMVec4d(tmp[0], tmp[1], tmp[2], tmp[3]);211 } else if (vecOrMat instanceof UMVec3d) {212 tmp = [213 vecOrMat.at(0) * this.m[0][0] + vecOrMat.at(1) * this.m[1][0] + vecOrMat.at(2) * this.m[2][0],214 vecOrMat.at(0) * this.m[0][1] + vecOrMat.at(1) * this.m[1][1] + vecOrMat.at(2) * this.m[2][1],215 vecOrMat.at(0) * this.m[0][2] + vecOrMat.at(1) * this.m[1][2] + vecOrMat.at(2) * this.m[2][2]216 ];217 dst = new UMVec3d(tmp[0], tmp[1], tmp[2]);218 }219 return dst;220 };221 UMMat44d.prototype.identity = function () {222 var i,223 k;224 for (i = 0; i < 4; i = i + 1) {225 for (k = 0; k < 4; k = k + 1) {226 this.m[i][k] = (i === k) ? 1 : 0;227 }228 }229 };230 UMMat44d.prototype.transposed = function () {231 var i,232 k,233 dst = new UMMat44d();234 for (i = 0; i < 4; i = i + 1) {235 for (k = 0; k < 4; k = k + 1) {236 dst.m[k][i] = this.m[i][k];237 }238 }239 return dst;240 };241 UMMat44d.prototype.determinant = function () {242 var temp_a = (this.m[2][2] * this.m[3][3]) - (this.m[2][3] * this.m[3][2]),243 temp_b = (this.m[2][1] * this.m[3][3]) - (this.m[2][3] * this.m[3][1]),244 temp_c = (this.m[2][1] * this.m[3][2]) - (this.m[2][2] * this.m[3][1]),245 temp_d = (this.m[2][0] * this.m[3][3]) - (this.m[2][3] * this.m[3][0]),246 temp_e = (this.m[2][0] * this.m[3][2]) - (this.m[2][2] * this.m[3][0]),247 temp_f = (this.m[2][0] * this.m[3][1]) - (this.m[2][1] * this.m[3][0]);248 return (this.m[0][0] * this.m[1][1]) * (temp_a)249 - (this.m[0][0] * this.m[1][2]) * (temp_b)250 + (this.m[0][0] * this.m[1][3]) * (temp_c)251 - (this.m[0][1] * this.m[1][0]) * (temp_a)252 + (this.m[0][1] * this.m[1][2]) * (temp_d)253 - (this.m[0][1] * this.m[1][3]) * (temp_e)254 + (this.m[0][2] * this.m[1][0]) * (temp_b)255 - (this.m[0][2] * this.m[1][1]) * (temp_d)256 + (this.m[0][2] * this.m[1][3]) * (temp_f)257 - (this.m[0][3] * this.m[1][0]) * (temp_c)258 + (this.m[0][3] * this.m[1][1]) * (temp_e)259 - (this.m[0][3] * this.m[1][2]) * (temp_f);260 };261 UMMat44d.prototype.inverted = function () {262 var dst = new UMMat44d(),263 vec = [new UMVec4d(), new UMVec4d(), new UMVec4d()],264 det = this.determinant(),265 i,266 j,267 k,268 a,269 v,270 rev_det;271 if (!det) { return dst; }272 for (i = 0; i < 4; i = i + 1) {273 for (j = 0; j < 4; j = j + 1) {274 if (j !== i) {275 a = j;276 if (j > i) { a = a - 1; }277 vec[a].xyzw[0] = this.m[j][0];278 vec[a].xyzw[1] = this.m[j][1];279 vec[a].xyzw[2] = this.m[j][2];280 vec[a].xyzw[3] = this.m[j][3];281 }282 }283 v = new UMVec4d(vec[0].xyzw[0], vec[0].xyzw[1], vec[0].xyzw[2], vec[0].xyzw[3]);284 v = v.cross(vec[1], vec[2]);285 rev_det = 1.0 / det;286 dst.m[0][i] = Math.pow(-1.0, i) * v.x() * rev_det;287 dst.m[1][i] = Math.pow(-1.0, i) * v.y() * rev_det;288 dst.m[2][i] = Math.pow(-1.0, i) * v.z() * rev_det;289 dst.m[3][i] = Math.pow(-1.0, i) * v.w() * rev_det;290 }291 return dst;292 };293 UMMat44d.prototype.translation = function () {294 return new UMVec3d(this.m[3][0], this.m[3][1], this.m[3][2]);295 };296 //------------------------------------------------------------------------297 /**298 * @param min UMVec3d299 * @param max UMVec3d300 */301 UMBox = function (min, max) {302 this.min_ = min;303 this.max_ = max;304 };305 UMBox.prototype.init = function () {306 this.set_min(new UMVec3d(Infinity, Infinity, Infinity));307 this.set_max(new UMVec3d(-Infinity, -Infinity, -Infinity));308 };309 UMBox.prototype.min = function () {310 return this.min_;311 };312 UMBox.prototype.max = function () {313 return this.max_;314 };315 UMBox.prototype.set_min = function (min) {316 this.min_ = min;317 };318 UMBox.prototype.set_max = function (max) {319 this.max_ = max;320 };321 UMBox.prototype.extend = function (val) {322 if (val instanceof Array) {323 this.min_.xyz[0] = Math.min(this.min_.xyz[0], val[0]);324 this.min_.xyz[1] = Math.min(this.min_.xyz[1], val[1]);325 this.min_.xyz[2] = Math.min(this.min_.xyz[2], val[2]);326 this.max_.xyz[0] = Math.max(this.max_.xyz[0], val[0]);327 this.max_.xyz[1] = Math.max(this.max_.xyz[1], val[1]);328 this.max_.xyz[2] = Math.max(this.max_.xyz[2], val[2]);329 } else if (val instanceof UMVec3d) {330 this.min_.xyz[0] = Math.min(this.min_.xyz[0], val.xyz[0]);331 this.min_.xyz[1] = Math.min(this.min_.xyz[1], val.xyz[1]);332 this.min_.xyz[2] = Math.min(this.min_.xyz[2], val.xyz[2]);333 this.max_.xyz[0] = Math.max(this.max_.xyz[0], val.xyz[0]);334 this.max_.xyz[1] = Math.max(this.max_.xyz[1], val.xyz[1]);335 this.max_.xyz[2] = Math.max(this.max_.xyz[2], val.xyz[2]);336 } else if (val instanceof UMBox){337 this.min_.xyz[0] = Math.min(this.min_.xyz[0], box.min_.xyz[0]);338 this.min_.xyz[1] = Math.min(this.min_.xyz[1], box.min_.xyz[1]);339 this.min_.xyz[2] = Math.min(this.min_.xyz[2], box.min_.xyz[2]);340 this.max_.xyz[0] = Math.max(this.max_.xyz[0], box.max_.xyz[0]);341 this.max_.xyz[1] = Math.max(this.max_.xyz[1], box.max_.xyz[1]);342 this.max_.xyz[2] = Math.max(this.max_.xyz[2], box.max_.xyz[2]);343 }344 };345 UMBox.prototype.center = function () {346 return new UMVec3d(347 (min_.xyz[0] + max_.xyz[0]) * 0.5,348 (min_.xyz[1] + max_.xyz[1]) * 0.5,349 (min_.xyz[2] + max_.xyz[2]) * 0.5350 )351 };352 UMBox.prototype.is_empty = function () {353 return min_.x >= max_.x || min_.y >= max_.y || min_.z >= max_.z;354 }355 /**356 * @param src UMMat44d357 */358 function um_matrix_to_euler_xyz(src) {359 var euler = new UMVec3d(),360 r00 = src.m[0][0],361 r01 = src.m[0][1],362 r02 = src.m[0][2],363 r10 = src.m[1][0],364 r11 = src.m[1][1],365 r12 = src.m[1][2],366 r20 = src.m[2][0],367 r21 = src.m[2][1],368 r22 = src.m[2][2];369 if (r02 < 1) {370 if (r02 > -1) {371 euler.xyz[1] = Math.asin(r02); // y372 euler.xyz[0] = Math.atan2(-r12, r22); // x373 euler.xyz[2] = Math.atan2(-r01, r00); // z374 } else {375 euler.xyz[1] = -Math.PI / 2.0; // y376 euler.xyz[0] = -Math.atan2(r10, r11); // x377 euler.xyz[2] = 0; // z378 }379 } else {380 euler.xyz[1] = Math.PI / 2.0; // y381 euler.xyz[0] = Math.atan2(r10, r11); // x382 euler.xyz[2] = 0; // z383 }384 return euler;385 }386 /**387 * @param src UMVec3d388 */389 function um_euler_to_matrix_xyz(src) {390 var dst = new UMMat44d(),391 cx = Math.cos(src.x()),392 cy = Math.cos(src.y()),393 cz = Math.cos(src.z()),394 sx = Math.sin(src.x()),395 sy = Math.sin(src.y()),396 sz = Math.sin(src.z());397 dst.m[0][0] = cy * cz;398 dst.m[0][1] = -cy * sz;399 dst.m[0][2] = sy;400 dst.m[1][0] = cz * sx * sy + cx * sz;401 dst.m[1][1] = cx * cz - sx * sy * sz;402 dst.m[1][2] = -cy * sx;403 dst.m[2][0] = -cx * cz * sy + sx * sz;404 dst.m[2][1] = cz * sx + cx * sy * sz;405 dst.m[2][2] = cx * cy;406 return dst;407 }408 /**409 * @param dst UMMat44d410 * @param w float411 * @param h float412 * @param zn float413 * @param zf float414 */415 function um_matrix_ortho_rh(dst, w, h, zn, zf) {416 dst.m[0][0] = 2 / w;417 dst.m[1][0] = 0.0;418 dst.m[2][0] = 0.0;419 dst.m[3][0] = 0.0;420 dst.m[0][1] = 0.0;421 dst.m[1][1] = 2 / h;422 dst.m[2][1] = 0.0;423 dst.m[3][1] = 0.0;424 dst.m[0][2] = 0.0;425 dst.m[1][2] = 0.0;426 dst.m[2][2] = 1 / (zn - zf);427 dst.m[3][2] = zn / (zn - zf);428 dst.m[0][3] = 0.0;429 dst.m[1][3] = 0.0;430 dst.m[2][3] = 0.0;431 dst.m[3][3] = 1.0;432 }433 /**434 * @param dst UMMat44d435 * @param w float436 * @param h float437 * @param zn float438 * @param zf float439 */440 function um_matrix_perspective_rh(dst, w, h, zn, zf) {441 dst.m[0][0] = 2 * zn / w;442 dst.m[1][0] = 0.0;443 dst.m[2][0] = 0.0;444 dst.m[3][0] = 0.0;445 dst.m[0][1] = 0.0;446 dst.m[1][1] = 2 * zn / h;447 dst.m[2][1] = 0.0;448 dst.m[3][1] = 0.0;449 dst.m[0][2] = 0.0;450 dst.m[1][2] = 0.0;451 dst.m[2][2] = zf / (zn - zf);452 dst.m[3][2] = zn * zf / (zn - zf);453 dst.m[0][3] = 0.0;454 dst.m[1][3] = 0.0;455 dst.m[2][3] = -1.0;456 dst.m[3][3] = 0.0;457 }458 /**459 * @param dst UMMat44d460 * @param fov_y float461 * @param aspect float462 * @param zn float463 * @param zf float464 */465 function um_matrix_perspective_fov_rh(dst, fov_y, aspect, zn, zf) {466 var h = 1.0 / Math.tan(fov_y / 2.0),467 w,468 zn_zf;469 if (EPSILON < Math.abs(aspect)) {470 w = h / aspect;471 } else {472 w = 0;473 }474 zn_zf = zn - zf;475 dst.m[0][0] = w;476 dst.m[1][0] = 0.0;477 dst.m[2][0] = 0.0;478 dst.m[3][0] = 0.0;479 dst.m[0][1] = 0.0;480 dst.m[1][1] = h;481 dst.m[2][1] = 0.0;482 dst.m[3][1] = 0.0;483 dst.m[0][2] = 0.0;484 dst.m[1][2] = 0.0;485 if ((zn_zf < -EPSILON) || (EPSILON < zn_zf)) {486 dst.m[2][2] = zf / zn_zf;487 dst.m[3][2] = zn * zf / zn_zf;488 } else {489 dst.m[2][2] = 0.0;490 dst.m[3][2] = 0.0;491 }492 dst.m[0][3] = 0.0;493 dst.m[1][3] = 0.0;494 dst.m[2][3] = -1.0;495 dst.m[3][3] = 0.0;496 }497 /**498 * @param dst UMMat44d499 * @param eye UMVec3d500 * @param at UMVec3d501 * @param up UMVec3d502 */503 function um_matrix_look_at_rh(dst, eye, at, up) {504 var zaxis = ((eye.sub(at)).normalized()),505 xaxis = ((up.cross(zaxis)).normalized()),506 yaxis = (zaxis.cross(xaxis));507 dst.m[0][0] = xaxis.x();508 dst.m[1][0] = xaxis.y();509 dst.m[2][0] = xaxis.z();510 dst.m[3][0] = -xaxis.dot(eye);511 dst.m[0][1] = yaxis.x();512 dst.m[1][1] = yaxis.y();513 dst.m[2][1] = yaxis.z();514 dst.m[3][1] = -yaxis.dot(eye);515 dst.m[0][2] = zaxis.x();516 dst.m[1][2] = zaxis.y();517 dst.m[2][2] = zaxis.z();518 dst.m[3][2] = -zaxis.dot(eye);519 dst.m[0][3] = 0.0;520 dst.m[1][3] = 0.0;521 dst.m[2][3] = 0.0;522 dst.m[3][3] = 1.0;523 }524 /**525 * convert degree to radian526 */527 function um_to_radian(degree) { return ((degree) * (Math.PI / 180.0)); }528 /**529 * convert radian to degree530 */531 function um_to_degree(radian) { return ((radian) * (180.0 / Math.PI)); }532 /**533 * signum534 */535 function um_sign(val) {536 return (0 < val) - (val < 0);537 }538 window.ummath = {};539 window.ummath.UMMat44d = UMMat44d;540 window.ummath.UMVec3d = UMVec3d;541 window.ummath.UMVec4d = UMVec4d;542 window.ummath.UMBox = UMBox;543 window.ummath.um_matrix_to_euler_xyz = um_matrix_to_euler_xyz;544 window.ummath.um_euler_to_matrix_xyz = um_euler_to_matrix_xyz;545 window.ummath.um_matrix_ortho_rh = um_matrix_ortho_rh;546 window.ummath.um_matrix_perspective_rh = um_matrix_perspective_rh;547 window.ummath.um_matrix_perspective_fov_rh = um_matrix_perspective_fov_rh;548 window.ummath.um_matrix_look_at_rh = um_matrix_look_at_rh;549 window.ummath.um_to_radian = um_to_radian;550 window.ummath.um_to_degree = um_to_degree;551 window.ummath.um_sign = um_sign;552 window.ummath.EPSILON = EPSILON;...

Full Screen

Full Screen

matrix4.js

Source:matrix4.js Github

copy

Full Screen

1// more from 'tdl'2var $builtinmodule = function(name)3{4 var mod = {};5 var temp0v3_ = new Float32Array(3);6 var temp1v3_ = new Float32Array(3);7 var temp2v3_ = new Float32Array(3);8 var temp0v4_ = new Float32Array(4);9 var temp1v4_ = new Float32Array(4);10 var temp2v4_ = new Float32Array(4);11 var temp0m4_ = new Float32Array(16);12 var temp1m4_ = new Float32Array(16);13 var temp2m4_ = new Float32Array(16);14 var normalize = function(dst, a) {15 var n = 0.0;16 var aLength = a.length;17 for (var i = 0; i < aLength; ++i)18 n += a[i] * a[i];19 n = Math.sqrt(n);20 if (n > 0.00001) {21 for (var i = 0; i < aLength; ++i)22 dst[i] = a[i] / n;23 } else {24 for (var i = 0; i < aLength; ++i)25 dst[i] = 0;26 }27 return dst;28 };29 var cross = function(dst, a, b) {30 dst[0] = a[1] * b[2] - a[2] * b[1];31 dst[1] = a[2] * b[0] - a[0] * b[2];32 dst[2] = a[0] * b[1] - a[1] * b[0];33 return dst;34 };35 var subVector = function(dst, a, b) {36 var aLength = a.length;37 for (var i = 0; i < aLength; ++i)38 dst[i] = a[i] - b[i];39 return dst;40 };41 var dot = function(a, b) {42 return (a[0] * b[0]) + (a[1] * b[1]) + (a[2] * b[2]);43 };44 mod.lookAt = new Sk.builtin.func(function(view, eye, target, up)45 {46 var t0 = temp0v3_;47 var t1 = temp1v3_;48 var t2 = temp2v3_;49 var vz = normalize(t0, subVector(t0, eye.v, target.v));50 var vx = normalize(t1, cross(t1, up.v, vz));51 var vy = cross(t2, vz, vx);52 var dst = view.v;53 dst[ 0] = vx[0];54 dst[ 1] = vy[0];55 dst[ 2] = vz[0];56 dst[ 3] = 0;57 dst[ 4] = vx[1];58 dst[ 5] = vy[1];59 dst[ 6] = vz[1];60 dst[ 7] = 0;61 dst[ 8] = vx[2];62 dst[ 9] = vy[2];63 dst[10] = vz[2];64 dst[11] = 0;65 dst[12] = -dot(vx, eye.v);66 dst[13] = -dot(vy, eye.v);67 dst[14] = -dot(vz, eye.v);68 dst[15] = 1;69 return view;70 });71 mod.perspective = new Sk.builtin.func(function(proj, angle, aspect, near, far)72 {73 var f = Math.tan(Math.PI * 0.5 - 0.5 * (angle * Math.PI / 180));74 var rangeInv = 1.0 / (near - far);75 var dst = proj.v;76 dst[0] = f / aspect;77 dst[1] = 0;78 dst[2] = 0;79 dst[3] = 0;80 dst[4] = 0;81 dst[5] = f;82 dst[6] = 0;83 dst[7] = 0;84 dst[8] = 0;85 dst[9] = 0;86 dst[10] = (near + far) * rangeInv;87 dst[11] = -1;88 dst[12] = 0;89 dst[13] = 0;90 dst[14] = near * far * rangeInv * 2;91 dst[15] = 0;92 return proj;93 });94 // builds, not appending95 mod.rotationY = new Sk.builtin.func(function(target, angle)96 {97 var dst = target.v;98 var c = Math.cos(angle * Math.PI / 180);99 var s = Math.sin(angle * Math.PI / 180);100 dst[ 0] = c;101 dst[ 1] = 0;102 dst[ 2] = -s;103 dst[ 3] = 0;104 dst[ 4] = 0;105 dst[ 5] = 1;106 dst[ 6] = 0;107 dst[ 7] = 0;108 dst[ 8] = s;109 dst[ 9] = 0;110 dst[10] = c;111 dst[11] = 0;112 dst[12] = 0;113 dst[13] = 0;114 dst[14] = 0;115 dst[15] = 1;116 return target;117 });118 mod.identity = new Sk.builtin.func(function(target)119 {120 var dst = target.v;121 dst[ 0] = 1;122 dst[ 1] = 0;123 dst[ 2] = 0;124 dst[ 3] = 0;125 dst[ 4] = 0;126 dst[ 5] = 1;127 dst[ 6] = 0;128 dst[ 7] = 0;129 dst[ 8] = 0;130 dst[ 9] = 0;131 dst[10] = 1;132 dst[11] = 0;133 dst[12] = 0;134 dst[13] = 0;135 dst[14] = 0;136 dst[15] = 1;137 return target;138 });139 // row major140 mod.mul = new Sk.builtin.func(function(target, x, y)141 {142 var dst = target.v;143 var a = x.v;144 var b = y.v;145 var a00 = a[0];146 var a01 = a[1];147 var a02 = a[2];148 var a03 = a[3];149 var a10 = a[ 4 + 0];150 var a11 = a[ 4 + 1];151 var a12 = a[ 4 + 2];152 var a13 = a[ 4 + 3];153 var a20 = a[ 8 + 0];154 var a21 = a[ 8 + 1];155 var a22 = a[ 8 + 2];156 var a23 = a[ 8 + 3];157 var a30 = a[12 + 0];158 var a31 = a[12 + 1];159 var a32 = a[12 + 2];160 var a33 = a[12 + 3];161 var b00 = b[0];162 var b01 = b[1];163 var b02 = b[2];164 var b03 = b[3];165 var b10 = b[ 4 + 0];166 var b11 = b[ 4 + 1];167 var b12 = b[ 4 + 2];168 var b13 = b[ 4 + 3];169 var b20 = b[ 8 + 0];170 var b21 = b[ 8 + 1];171 var b22 = b[ 8 + 2];172 var b23 = b[ 8 + 3];173 var b30 = b[12 + 0];174 var b31 = b[12 + 1];175 var b32 = b[12 + 2];176 var b33 = b[12 + 3];177 dst[ 0] = a00 * b00 + a01 * b10 + a02 * b20 + a03 * b30;178 dst[ 1] = a00 * b01 + a01 * b11 + a02 * b21 + a03 * b31;179 dst[ 2] = a00 * b02 + a01 * b12 + a02 * b22 + a03 * b32;180 dst[ 3] = a00 * b03 + a01 * b13 + a02 * b23 + a03 * b33;181 dst[ 4] = a10 * b00 + a11 * b10 + a12 * b20 + a13 * b30;182 dst[ 5] = a10 * b01 + a11 * b11 + a12 * b21 + a13 * b31;183 dst[ 6] = a10 * b02 + a11 * b12 + a12 * b22 + a13 * b32;184 dst[ 7] = a10 * b03 + a11 * b13 + a12 * b23 + a13 * b33;185 dst[ 8] = a20 * b00 + a21 * b10 + a22 * b20 + a23 * b30;186 dst[ 9] = a20 * b01 + a21 * b11 + a22 * b21 + a23 * b31;187 dst[10] = a20 * b02 + a21 * b12 + a22 * b22 + a23 * b32;188 dst[11] = a20 * b03 + a21 * b13 + a22 * b23 + a23 * b33;189 dst[12] = a30 * b00 + a31 * b10 + a32 * b20 + a33 * b30;190 dst[13] = a30 * b01 + a31 * b11 + a32 * b21 + a33 * b31;191 dst[14] = a30 * b02 + a31 * b12 + a32 * b22 + a33 * b32;192 dst[15] = a30 * b03 + a31 * b13 + a32 * b23 + a33 * b33;193 return target;194 });195 mod.invert = new Sk.builtin.func(function(target, mat)196 {197 var dst = target.v;198 var m = mat.v;199 var m00 = m[0 * 4 + 0];200 var m01 = m[0 * 4 + 1];201 var m02 = m[0 * 4 + 2];202 var m03 = m[0 * 4 + 3];203 var m10 = m[1 * 4 + 0];204 var m11 = m[1 * 4 + 1];205 var m12 = m[1 * 4 + 2];206 var m13 = m[1 * 4 + 3];207 var m20 = m[2 * 4 + 0];208 var m21 = m[2 * 4 + 1];209 var m22 = m[2 * 4 + 2];210 var m23 = m[2 * 4 + 3];211 var m30 = m[3 * 4 + 0];212 var m31 = m[3 * 4 + 1];213 var m32 = m[3 * 4 + 2];214 var m33 = m[3 * 4 + 3];215 var tmp_0 = m22 * m33;216 var tmp_1 = m32 * m23;217 var tmp_2 = m12 * m33;218 var tmp_3 = m32 * m13;219 var tmp_4 = m12 * m23;220 var tmp_5 = m22 * m13;221 var tmp_6 = m02 * m33;222 var tmp_7 = m32 * m03;223 var tmp_8 = m02 * m23;224 var tmp_9 = m22 * m03;225 var tmp_10 = m02 * m13;226 var tmp_11 = m12 * m03;227 var tmp_12 = m20 * m31;228 var tmp_13 = m30 * m21;229 var tmp_14 = m10 * m31;230 var tmp_15 = m30 * m11;231 var tmp_16 = m10 * m21;232 var tmp_17 = m20 * m11;233 var tmp_18 = m00 * m31;234 var tmp_19 = m30 * m01;235 var tmp_20 = m00 * m21;236 var tmp_21 = m20 * m01;237 var tmp_22 = m00 * m11;238 var tmp_23 = m10 * m01;239 var t0 = (tmp_0 * m11 + tmp_3 * m21 + tmp_4 * m31) -240 (tmp_1 * m11 + tmp_2 * m21 + tmp_5 * m31);241 var t1 = (tmp_1 * m01 + tmp_6 * m21 + tmp_9 * m31) -242 (tmp_0 * m01 + tmp_7 * m21 + tmp_8 * m31);243 var t2 = (tmp_2 * m01 + tmp_7 * m11 + tmp_10 * m31) -244 (tmp_3 * m01 + tmp_6 * m11 + tmp_11 * m31);245 var t3 = (tmp_5 * m01 + tmp_8 * m11 + tmp_11 * m21) -246 (tmp_4 * m01 + tmp_9 * m11 + tmp_10 * m21);247 var d = 1.0 / (m00 * t0 + m10 * t1 + m20 * t2 + m30 * t3);248 dst[ 0] = d * t0;249 dst[ 1] = d * t1;250 dst[ 2] = d * t2;251 dst[ 3] = d * t3;252 dst[ 4] = d * ((tmp_1 * m10 + tmp_2 * m20 + tmp_5 * m30) -253 (tmp_0 * m10 + tmp_3 * m20 + tmp_4 * m30));254 dst[ 5] = d * ((tmp_0 * m00 + tmp_7 * m20 + tmp_8 * m30) -255 (tmp_1 * m00 + tmp_6 * m20 + tmp_9 * m30));256 dst[ 6] = d * ((tmp_3 * m00 + tmp_6 * m10 + tmp_11 * m30) -257 (tmp_2 * m00 + tmp_7 * m10 + tmp_10 * m30));258 dst[ 7] = d * ((tmp_4 * m00 + tmp_9 * m10 + tmp_10 * m20) -259 (tmp_5 * m00 + tmp_8 * m10 + tmp_11 * m20));260 dst[ 8] = d * ((tmp_12 * m13 + tmp_15 * m23 + tmp_16 * m33) -261 (tmp_13 * m13 + tmp_14 * m23 + tmp_17 * m33));262 dst[ 9] = d * ((tmp_13 * m03 + tmp_18 * m23 + tmp_21 * m33) -263 (tmp_12 * m03 + tmp_19 * m23 + tmp_20 * m33));264 dst[10] = d * ((tmp_14 * m03 + tmp_19 * m13 + tmp_22 * m33) -265 (tmp_15 * m03 + tmp_18 * m13 + tmp_23 * m33));266 dst[11] = d * ((tmp_17 * m03 + tmp_20 * m13 + tmp_23 * m23) -267 (tmp_16 * m03 + tmp_21 * m13 + tmp_22 * m23));268 dst[12] = d * ((tmp_14 * m22 + tmp_17 * m32 + tmp_13 * m12) -269 (tmp_16 * m32 + tmp_12 * m12 + tmp_15 * m22));270 dst[13] = d * ((tmp_20 * m32 + tmp_12 * m02 + tmp_19 * m22) -271 (tmp_18 * m22 + tmp_21 * m32 + tmp_13 * m02));272 dst[14] = d * ((tmp_18 * m12 + tmp_23 * m32 + tmp_15 * m02) -273 (tmp_22 * m32 + tmp_14 * m02 + tmp_19 * m12));274 dst[15] = d * ((tmp_22 * m22 + tmp_16 * m02 + tmp_21 * m12) -275 (tmp_20 * m12 + tmp_23 * m22 + tmp_17 * m02));276 return target;277 });278 mod.transpose = new Sk.builtin.func(function(target, mat)279 {280 var dst = target.v;281 var m = mat.v;282 for (var j = 0; j < 4; ++j) {283 for (var i = 0; i < 4; ++i)284 dst[j * 4 + i] = m[i * 4 + j];285 }286 return dst;287 });288 return mod;...

Full Screen

Full Screen

solve.js

Source:solve.js Github

copy

Full Screen

1const fs = require('fs');2const axios = require('axios');3const QrCode = require('qrcode-reader');4const Jimp = require("jimp");5const Cube = require('cubejs');6require('cubejs/lib/solve.js');7Cube.initSolver();8const size = 82;9const host = 'http://qubicrube.pwn.seccon.jp:33654/';10let hash = '';11let n = 0;12function decodeQR(buf) {13 return new Promise((resolve, reject) => {14 Jimp.read(buf, (err, img) => {15 if (err != null) {16 return reject(err);17 }18 let qr = new QrCode();19 qr.callback = (err, value) => {20 if (err != null) {21 return reject(err);22 }23 return resolve(value);24 };25 qr.decode(img.bitmap);26 });27 });28}29function splitImage(buf) {30 return new Promise((resolve, reject) => {31 Jimp.read(buf, (err, img) => {32 if (err != null) {33 return reject(err);34 }35 resolve(img);36 });37 }).then(src => {38 tasks = [];39 for (let y = 0; y < 3; y++) {40 for (let x = 0; x < 3; x++) {41 tasks.push(new Promise((resolve, reject) => {42 let img = src.clone().crop(x * size, y * size, size, size);43 img.getBuffer(Jimp.MIME_PNG, (err, buf) => {44 if (err != null) {45 return reject(err);46 }47 resolve([buf, img]);48 });49 }));50 }51 }52 return Promise.all(tasks);53 });54}55function rotateRight(src) {56 return [57 src[6].rotate(90), src[3].rotate(90), src[0].rotate(90),58 src[7].rotate(90), src[4].rotate(90), src[1].rotate(90),59 src[8].rotate(90), src[5].rotate(90), src[2].rotate(90),60 ];61}62function rotate(src, move) {63 let dst = {64 U: [...src.U],65 R: [...src.R],66 F: [...src.F],67 D: [...src.D],68 L: [...src.L],69 B: [...src.B],70 };71 switch (move) {72 case 'R':73 dst['R'] = rotateRight(src['R']);74 dst['F'][2] = src['D'][2];75 dst['F'][5] = src['D'][5];76 dst['F'][8] = src['D'][8];77 dst['U'][2] = src['F'][2];78 dst['U'][5] = src['F'][5];79 dst['U'][8] = src['F'][8];80 dst['B'][6] = src['U'][2];81 dst['B'][3] = src['U'][5];82 dst['B'][0] = src['U'][8];83 dst['D'][2] = src['B'][6];84 dst['D'][5] = src['B'][3];85 dst['D'][8] = src['B'][0];86 dst['B'][6].rotate(180);87 dst['B'][3].rotate(180);88 dst['B'][0].rotate(180);89 dst['D'][2].rotate(180);90 dst['D'][5].rotate(180);91 dst['D'][8].rotate(180);92 break;93 case 'L':94 dst['L'] = rotateRight(src['L']);95 dst['B'][2] = src['D'][6];96 dst['B'][5] = src['D'][3];97 dst['B'][8] = src['D'][0];98 dst['U'][6] = src['B'][2];99 dst['U'][3] = src['B'][5];100 dst['U'][0] = src['B'][8];101 dst['F'][6] = src['U'][6];102 dst['F'][3] = src['U'][3];103 dst['F'][0] = src['U'][0];104 dst['D'][6] = src['F'][6];105 dst['D'][3] = src['F'][3];106 dst['D'][0] = src['F'][0];107 dst['B'][2].rotate(180);108 dst['B'][5].rotate(180);109 dst['B'][8].rotate(180);110 dst['U'][6].rotate(180);111 dst['U'][3].rotate(180);112 dst['U'][0].rotate(180);113 break;114 case 'U':115 dst['U'] = rotateRight(src['U']);116 dst['B'][0] = src['L'][0];117 dst['B'][1] = src['L'][1];118 dst['B'][2] = src['L'][2];119 dst['R'][0] = src['B'][0];120 dst['R'][1] = src['B'][1];121 dst['R'][2] = src['B'][2];122 dst['F'][0] = src['R'][0];123 dst['F'][1] = src['R'][1];124 dst['F'][2] = src['R'][2];125 dst['L'][0] = src['F'][0];126 dst['L'][1] = src['F'][1];127 dst['L'][2] = src['F'][2];128 break;129 case 'D':130 dst['D'] = rotateRight(src['D']);131 dst['L'][6] = src['B'][6];132 dst['L'][7] = src['B'][7];133 dst['L'][8] = src['B'][8];134 dst['F'][6] = src['L'][6];135 dst['F'][7] = src['L'][7];136 dst['F'][8] = src['L'][8];137 dst['R'][6] = src['F'][6];138 dst['R'][7] = src['F'][7];139 dst['R'][8] = src['F'][8];140 dst['B'][6] = src['R'][6];141 dst['B'][7] = src['R'][7];142 dst['B'][8] = src['R'][8];143 break;144 case 'F':145 dst['F'] = rotateRight(src['F']);146 dst['L'][2] = src['D'][0];147 dst['L'][5] = src['D'][1];148 dst['L'][8] = src['D'][2];149 dst['U'][8] = src['L'][2];150 dst['U'][7] = src['L'][5];151 dst['U'][6] = src['L'][8];152 dst['R'][6] = src['U'][8];153 dst['R'][3] = src['U'][7];154 dst['R'][0] = src['U'][6];155 dst['D'][0] = src['R'][6];156 dst['D'][1] = src['R'][3];157 dst['D'][2] = src['R'][0];158 dst['D'][0].rotate(90);159 dst['D'][1].rotate(90);160 dst['D'][2].rotate(90);161 dst['L'][2].rotate(90);162 dst['L'][5].rotate(90);163 dst['L'][8].rotate(90);164 dst['R'][6].rotate(90);165 dst['R'][3].rotate(90);166 dst['R'][0].rotate(90);167 dst['U'][8].rotate(90);168 dst['U'][7].rotate(90);169 dst['U'][6].rotate(90);170 break;171 case 'B':172 dst['B'] = rotateRight(src['B']);173 dst['R'][2] = src['D'][8];174 dst['R'][5] = src['D'][7];175 dst['R'][8] = src['D'][6];176 dst['U'][0] = src['R'][2];177 dst['U'][1] = src['R'][5];178 dst['U'][2] = src['R'][8];179 dst['L'][6] = src['U'][0];180 dst['L'][3] = src['U'][1];181 dst['L'][0] = src['U'][2];182 dst['D'][8] = src['L'][6];183 dst['D'][7] = src['L'][3];184 dst['D'][6] = src['L'][0];185 dst['R'][2].rotate(270);186 dst['R'][5].rotate(270);187 dst['R'][8].rotate(270);188 dst['U'][0].rotate(270);189 dst['U'][1].rotate(270);190 dst['U'][2].rotate(270);191 dst['D'][8].rotate(270);192 dst['D'][7].rotate(270);193 dst['D'][6].rotate(270);194 dst['L'][6].rotate(270);195 dst['L'][3].rotate(270);196 dst['L'][0].rotate(270);197 break;198 }199 return dst;200}201function solve(hash) {202 return axios.get(`${host}${hash}`)203 .then(resp => resp.data.match(/images\/\w+_[A-Z].png/g))204 .then(imgs => imgs.sort((a, b) => {205 let ac = a.slice(-5, -4);206 let bc = b.slice(-5, -4);207 const s = 'URFDLB';208 return s.indexOf(ac) - s.indexOf(bc);209 }))210 .then(imgs => imgs.map(img => axios.get(`${host}${img}`, {211 responseType: 'arraybuffer',212 }).then(resp => {213 fs.writeFileSync(`./${img}`, resp.data);214 return resp;215 })))216 .then(imgs => Promise.all(imgs))217 .then(imgs => {218 let cubeStr = [];219 let cubeMap = {220 U: [],221 R: [],222 F: [],223 D: [],224 L: [],225 B: [],226 };227 let colors = {};228 return Promise.all(imgs.map((resp, k) => splitImage(resp.data).then(imgs => {229 imgs.forEach(([buf, img], i) => {230 let dir = resp.request.path.slice(-5, -4);231 for (let j = 0; j < img.bitmap.data.length; j += 4) {232 let r = img.bitmap.data[j];233 let g = img.bitmap.data[j + 1];234 let b = img.bitmap.data[j + 2];235 let rgb = (r << 16) | (g << 8) | b;236 if (rgb == 0) {237 continue;238 }239 let color = rgb.toString(16);240 if (i == 4) {241 colors[color] = dir;242 }243 cubeStr[k * 9 + i] = color;244 cubeMap[dir][i] = img;245 break;246 }247 });248 }))).then(() => {249 cubeStr = cubeStr.map(c => colors[c]);250 let cube = Cube.fromString(cubeStr.join(''));251 let moves = process.argv[3];252 moves = moves || cube.solve();253 console.log(moves);254 moves.split(' ').forEach(move => {255 let n = 1;256 if (move[1] == '2') {257 n = 2;258 } else if (move[1] == "'") {259 n = 3;260 }261 for (let i = 0; i < n; i++) {262 cubeMap = rotate(cubeMap, move[0]);263 }264 });265 return Promise.all(Object.entries(cubeMap).map(p => {266 return new Promise((resolve, reject) => {267 new Jimp(size * 3, size * 3, (err, img) => {268 if (err != null) {269 return reject(err);270 }271 p[1].forEach((t, i) => img.composite(t, size * (i % 3), size * (i / 3 | 0)));272 let result = null;273 const fn = i => new Promise((resolve, reject) => {274 img.composite(p[1][4].rotate(i * 90), size, size);275 const d = img.bitmap.data;276 for (let i = 0; i < d.length; i += 4) {277 let c = d[i] | d[i + 1] | d[i + 2];278 d[i] = c ? 0xff : 0;279 d[i + 1] = c ? 0xff : 0;280 d[i + 2] = c ? 0xff : 0;281 }282 img.getBuffer(Jimp.MIME_PNG, (err, buf) => {283 if (err != null) {284 return reject(err);285 }286 fs.writeFileSync(`./images/${hash}-${p[0]}-${i}.png`, buf);287 decodeQR(buf).then(v => {288 fs.writeFileSync(`./images/${hash}-${p[0]}.png`, buf);289 result = v;290 return resolve();291 }, resolve);292 });293 });294 const ps = [];295 for (let i = 0; i < 4; i++) {296 ps.push(fn(i));297 }298 Promise.all(ps).then(() => resolve(result), reject);299 });300 });301 }));302 });303 return imgs;304 })305 .then(imgs => imgs.filter(img => img))306 .then(imgs => {307 let next = null;308 imgs.forEach(img => {309 if (img.result.indexOf(host) == 0) {310 next = img.result.slice(host.length)311 }312 if (img.result.indexOf('No.') == 0) {313 console.log(img.result);314 }315 });316 if (next == null) {317 console.log(imgs);318 throw new Error('faild to detect next URL');319 };320 console.log(`next URL: ${host}${next}`);321 return next;322 });323}324solve(process.argv[2].replace(/\s/g, ''))325 .then(solve)326 .then(solve)327 .then(solve)328 .then(solve)329 .then(solve)330 .then(solve)331 .then(solve)332 .then(solve)333 .then(solve)...

Full Screen

Full Screen

gen.py

Source:gen.py Github

copy

Full Screen

1# Copyright (c) 2018 Viktor Vorobjov2import sys, os3path = "../../upy_main/tools/gen_tool"4gen_tool_path = os.path.abspath("{}/".format(path))5sys.path.append(gen_tool_path)6print(gen_tool_path)7from gen_tool import *8print("Gen libs")9path_ulib = "../../upy_main/micropython-lib"10path_emu = "../../upy_main/emu_lib"11path_core = "../../upy_main/ucore"12path_app_mod = "../../upy_main/umod"13work_path = "."14work_app_path = "./app"15work_core_path = "./core"16work_lib_path = "./lib"17work_mod_path = "./mod"18print("-- Path")19print(path_ulib)20print(work_path)21print(work_lib_path)22print("-- Start Gen Lib")23libs = (24# LIB PC25 # logging26 {27 "src_lib": path_ulib,28 "src_path": "logging/simple",29 "src": "logging.py",30 "dst_lib": work_lib_path,31 "dst_path": ""32 },33 # machine34 {35 "src_lib": path_emu,36 "src_path": "esp32/machine",37 "src": "__init__.py",38 "dst_lib": work_lib_path,39 "dst_path": "machine"40 },41 {42 "src_lib": path_emu,43 "src_path": "esp32/machine",44 "src": "emu_pin.py",45 "dst_lib": work_lib_path,46 "dst_path": "machine"47 },48 {49 "src_lib": path_emu,50 "src_path": "esp32/machine",51 "src": "emu_state.py",52 "dst_lib": work_lib_path,53 "dst_path": "machine"54 },55 # network56 {57 "src_lib": path_emu,58 "src_path": "esp32/network",59 "src": "__init__.py",60 "dst_lib": work_lib_path,61 "dst_path": "network"62 },63 {64 "src_lib": path_emu,65 "src_path": "esp32/network",66 "src": "emu_wlan.py",67 "dst_lib": work_lib_path,68 "dst_path": "network"69 },70#CORE71 # board72 {73 "src_lib": path_core,74 "src_path": "board/pc",75 "src": "platform.py",76 "dst_lib": work_core_path,77 "dst_path": ""78 },79 # loader80 {81 "src_lib": path_core,82 "src_path": "",83 "src": "loader.py",84 "dst_lib": work_core_path,85 "dst_path": ""86 },87 # asyn88 {89 "src_lib": path_core,90 "src_path": "asyn",91 "src": "asyn.py",92 "dst_lib": work_core_path,93 "dst_path": "asyn"94 },95 # db96 {97 "src_lib": path_core,98 "src_path": "db",99 "src": "filedb.py",100 "dst_lib": work_core_path,101 "dst_path": "db"102 },103 # mbus104 {105 "src_lib": path_core,106 "src_path": "mbus",107 "src": "mbus.py",108 "dst_lib": work_core_path,109 "dst_path": "mbus"110 },111 # umod112 {113 "src_lib": path_core,114 "src_path": "umod",115 "src": "board.py",116 "dst_lib": work_core_path,117 "dst_path": "umod"118 },119 {120 "src_lib": path_core,121 "src_path": "umod",122 "src": "table.py",123 "dst_lib": work_core_path,124 "dst_path": "umod"125 },126 {127 "src_lib": path_core,128 "src_path": "umod",129 "src": "umod.py",130 "dst_lib": work_core_path,131 "dst_path": "umod"132 },133#APP134 # FTP app135 {136 "src_lib": path_app_mod,137 "src_path": "ftp/app",138 "src": "ftp_mod.py",139 "dst_lib": work_app_path,140 "dst_path": ""141 },142 # FTP mod143 {144 "src_lib": path_app_mod,145 "src_path": "ftp/mod",146 "src": "ftp.py",147 "dst_lib": work_mod_path,148 "dst_path": "ftp"149 },150 {151 "src_lib": path_app_mod,152 "src_path": "ftp/mod",153 "src": "runner.py",154 "dst_lib": work_mod_path,155 "dst_path": "ftp"156 },157 # TELNET app158 {159 "src_lib": path_app_mod,160 "src_path": "telnet/app",161 "src": "telnet_mod.py",162 "dst_lib": work_app_path,163 "dst_path": ""164 },165 # TELNET mod166 {167 "src_lib": path_app_mod,168 "src_path": "telnet/mod",169 "src": "telnet.py",170 "dst_lib": work_mod_path,171 "dst_path": "telnet"172 },173 {174 "src_lib": path_app_mod,175 "src_path": "telnet/mod",176 "src": "runner.py",177 "dst_lib": work_mod_path,178 "dst_path": "telnet"179 },180 {181 "src_lib": path_app_mod,182 "src_path": "telnet/mod",183 "src": "telnetio.py",184 "dst_lib": work_mod_path,185 "dst_path": "telnet"186 },187 # HTTP app188 {189 "src_lib": path_app_mod,190 "src_path": "http/app",191 "src": "http_mod.py",192 "dst_lib": work_app_path,193 "dst_path": ""194 },195 # HTTP mod196 {197 "src_lib": path_app_mod,198 "src_path": "http/mod",199 "src": "runner.py",200 "dst_lib": work_mod_path,201 "dst_path": "http"202 },203 {204 "src_lib": path_app_mod,205 "src_path": "http/mod",206 "src": "route_rpc.py",207 "dst_lib": work_mod_path,208 "dst_path": "http"209 },210 {211 "src_lib": path_app_mod,212 "src_path": "http/mod",213 "src": "http.py",214 "dst_lib": work_mod_path,215 "dst_path": "http"216 },217 # NET app218 {219 "src_lib": path_app_mod,220 "src_path": "net/app",221 "src": "net_mod.py",222 "dst_lib": work_app_path,223 "dst_path": ""224 },225 # NET mod226 {227 "src_lib": path_app_mod,228 "src_path": "net/mod",229 "src": "actions.py",230 "dst_lib": work_mod_path,231 "dst_path": "net"232 },233 {234 "src_lib": path_app_mod,235 "src_path": "net/mod/esp32",236 "src": "wifi.py",237 "dst_lib": work_mod_path,238 "dst_path": "net"239 },240 # MQTT app241 {242 "src_lib": path_app_mod,243 "src_path": "mqtt/app",244 "src": "mqtt_mod.py",245 "dst_lib": work_app_path,246 "dst_path": ""247 },248 # MQTT mod249 {250 "src_lib": path_app_mod,251 "src_path": "mqtt/mod",252 "src": "mqtt.py",253 "dst_lib": work_mod_path,254 "dst_path": "mqtt"255 },256 {257 "src_lib": path_app_mod,258 "src_path": "mqtt/mod",259 "src": "runner.py",260 "dst_lib": work_mod_path,261 "dst_path": "mqtt"262 },263 {264 "src_lib": path_app_mod,265 "src_path": "mqtt/mod",266 "src": "message.py",267 "dst_lib": work_mod_path,268 "dst_path": "mqtt"269 },270 # PIN271 # PIN app272 {273 "src_lib": path_app_mod,274 "src_path": "pin/app",275 "src": "pin_mod.py",276 "dst_lib": work_app_path,277 "dst_path": ""278 },279 # PIN mod280 {281 "src_lib": path_app_mod,282 "src_path": "pin/mod",283 "src": "runner.py",284 "dst_lib": work_mod_path,285 "dst_path": "pin"286 },287 # PUSH288 # PUSH app289 {290 "src_lib": path_app_mod,291 "src_path": "push/app",292 "src": "push_mod.py",293 "dst_lib": work_app_path,294 "dst_path": ""295 },296 # PUSH mod297 {298 "src_lib": path_app_mod,299 "src_path": "push/mod",300 "src": "push.py",301 "dst_lib": work_mod_path,302 "dst_path": "push"303 },304 {305 "src_lib": path_app_mod,306 "src_path": "push/mod",307 "src": "runner.py",308 "dst_lib": work_mod_path,309 "dst_path": "push"310 },311 # RELAY312 # RELAY app313 {314 "src_lib": path_app_mod,315 "src_path": "relay/app",316 "src": "relay_mod.py",317 "dst_lib": work_app_path,318 "dst_path": ""319 },320 # RELAY mod321 {322 "src_lib": path_app_mod,323 "src_path": "relay/mod",324 "src": "relay.py",325 "dst_lib": work_mod_path,326 "dst_path": "relay"327 },328 {329 "src_lib": path_app_mod,330 "src_path": "relay/mod",331 "src": "runner.py",332 "dst_lib": work_mod_path,333 "dst_path": "relay"334 },335 # Control336 # Control app337 {338 "src_lib": path_app_mod,339 "src_path": "control/app",340 "src": "control_mod.py",341 "dst_lib": work_app_path,342 "dst_path": ""343 },344 # Control mod345 {346 "src_lib": path_app_mod,347 "src_path": "control/mod",348 "src": "runner.py",349 "dst_lib": work_mod_path,350 "dst_path": "control"351 },352# PWM353 # PWM app354 {355 "src_lib": path_app_mod,356 "src_path": "pwm/app",357 "src": "pwm_mod.py",358 "dst_lib": work_app_path,359 "dst_path": ""360 },361 # PWM mod362 {363 "src_lib": path_app_mod,364 "src_path": "pwm/mod",365 "src": "pwm.py",366 "dst_lib": work_mod_path,367 "dst_path": "pwm"368 },369 {370 "src_lib": path_app_mod,371 "src_path": "pwm/mod",372 "src": "runner.py",373 "dst_lib": work_mod_path,374 "dst_path": "pwm"375 },376)...

Full Screen

Full Screen

fake_filesystem_shutil.py

Source:fake_filesystem_shutil.py Github

copy

Full Screen

1#!/usr/bin/env python2#3# Copyright 2009 Google Inc. All Rights Reserved.4#5# Licensed under the Apache License, Version 2.0 (the "License");6# you may not use this file except in compliance with the License.7# You may obtain a copy of the License at8#9# http://www.apache.org/licenses/LICENSE-2.010#11# Unless required by applicable law or agreed to in writing, software12# distributed under the License is distributed on an "AS IS" BASIS,13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14# See the License for the specific language governing permissions and15# limitations under the License.16#17# pylint: disable-msg=W0612,W0613,C640918"""A fake shutil module implementation that uses fake_filesystem for unit tests.19Includes:20 FakeShutil: Uses a FakeFilesystem to provide a fake replacement for the21 shutil module.22Usage:23>>> from pyfakefs import fake_filesystem24>>> from pyfakefs import fake_filesystem_shutil25>>> filesystem = fake_filesystem.FakeFilesystem()26>>> shutil_module = fake_filesystem_shutil.FakeShutilModule(filesystem)27Copy a fake_filesystem directory tree:28>>> new_file = filesystem.CreateFile('/src/new-file')29>>> shutil_module.copytree('/src', '/dst')30>>> filesystem.Exists('/dst/new-file')31True32Remove a fake_filesystem directory tree:33>>> shutil_module.rmtree('/src')34>>> filesystem.Exists('/src/new-file')35False36"""37import errno38import os39import shutil40import stat41__pychecker__ = 'no-reimportself'42_PERM_WRITE = 0o200 # Write permission bit.43_PERM_READ = 0o400 # Read permission bit.44_PERM_ALL = 0o7777 # All permission bits.45class FakeShutilModule(object):46 """Uses a FakeFilesystem to provide a fake replacement for shutil module."""47 def __init__(self, filesystem):48 """Construct fake shutil module using the fake filesystem.49 Args:50 filesystem: FakeFilesystem used to provide file system information51 """52 self.filesystem = filesystem53 self._shutil_module = shutil54 def rmtree(self, path, ignore_errors=False, onerror=None):55 """Remove a directory and all its contents.56 Args:57 path: (str) Directory tree to remove.58 ignore_errors: (bool) unimplemented59 onerror: (func) unimplemented60 """61 self.filesystem.RemoveObject(path)62 def copy(self, src, dst):63 """Copy data and mode bits ("cp src dst").64 Args:65 src: (str) source file66 dst: (str) destination, may be a directory67 """68 if self.filesystem.Exists(dst):69 if stat.S_ISDIR(self.filesystem.GetObject(dst).st_mode):70 dst = self.filesystem.JoinPaths(dst, os.path.basename(src))71 self.copyfile(src, dst)72 src_object = self.filesystem.GetObject(src)73 dst_object = self.filesystem.GetObject(dst)74 dst_object.st_mode = ((dst_object.st_mode & ~_PERM_ALL) |75 (src_object.st_mode & _PERM_ALL))76 def copyfile(self, src, dst):77 """Copy data from src to dst.78 Args:79 src: (str) source file80 dst: (dst) destination file81 Raises:82 IOError: if the file can't be copied83 shutil.Error: if the src and dst files are the same84 """85 src_file_object = self.filesystem.GetObject(src)86 if not src_file_object.st_mode & _PERM_READ:87 raise IOError(errno.EACCES, 'Permission denied', src)88 if stat.S_ISDIR(src_file_object.st_mode):89 raise IOError(errno.EISDIR, 'Is a directory', src)90 dst_dir = os.path.dirname(dst)91 if dst_dir:92 if not self.filesystem.Exists(dst_dir):93 raise IOError(errno.ENOTDIR, 'Not a directory', dst)94 dst_dir_object = self.filesystem.GetObject(dst_dir)95 if not dst_dir_object.st_mode & _PERM_WRITE:96 raise IOError(errno.EACCES, 'Permission denied', dst_dir)97 abspath_src = self.filesystem.NormalizePath(98 self.filesystem.ResolvePath(src))99 abspath_dst = self.filesystem.NormalizePath(100 self.filesystem.ResolvePath(dst))101 if abspath_src == abspath_dst:102 raise shutil.Error('`%s` and `%s` are the same file' % (src, dst))103 if self.filesystem.Exists(dst):104 dst_file_object = self.filesystem.GetObject(dst)105 if stat.S_ISDIR(dst_file_object.st_mode):106 raise IOError(errno.EISDIR, 'Is a directory', dst)107 if not dst_file_object.st_mode & _PERM_WRITE:108 raise IOError(errno.EACCES, 'Permission denied', dst)109 dst_file_object.SetContents(src_file_object.contents)110 else:111 self.filesystem.CreateFile(dst, contents=src_file_object.contents)112 def copystat(self, src, dst):113 """Copy all stat info (mode bits, atime, and mtime) from src to dst.114 Args:115 src: (str) source file116 dst: (str) destination file117 """118 src_object = self.filesystem.GetObject(src)119 dst_object = self.filesystem.GetObject(dst)120 dst_object.st_mode = ((dst_object.st_mode & ~_PERM_ALL) |121 (src_object.st_mode & _PERM_ALL))122 dst_object.st_uid = src_object.st_uid123 dst_object.st_gid = src_object.st_gid124 dst_object.st_atime = src_object.st_atime125 dst_object.st_mtime = src_object.st_mtime126 def copy2(self, src, dst):127 """Copy data and all stat info ("cp -p src dst").128 Args:129 src: (str) source file130 dst: (str) destination, may be a directory131 """132 if self.filesystem.Exists(dst):133 if stat.S_ISDIR(self.filesystem.GetObject(dst).st_mode):134 dst = self.filesystem.JoinPaths(dst, os.path.basename(src))135 self.copyfile(src, dst)136 self.copystat(src, dst)137 def copytree(self, src, dst, symlinks=False):138 """Recursively copy a directory tree.139 Args:140 src: (str) source directory141 dst: (str) destination directory, must not already exist142 symlinks: (bool) copy symlinks as symlinks instead of copying the143 contents of the linked files. Currently unused.144 Raises:145 OSError: if src is missing or isn't a directory146 """147 self.filesystem.CreateDirectory(dst)148 try:149 directory = self.filesystem.GetObject(src)150 except IOError as e:151 raise OSError(e.errno, e.message)152 if not stat.S_ISDIR(directory.st_mode):153 raise OSError(errno.ENOTDIR,154 'Fake os module: %r not a directory' % src)155 for name in directory.contents:156 srcname = self.filesystem.JoinPaths(src, name)157 dstname = self.filesystem.JoinPaths(dst, name)158 src_mode = self.filesystem.GetObject(srcname).st_mode159 if stat.S_ISDIR(src_mode):160 self.copytree(srcname, dstname, symlinks)161 else:162 self.copy2(srcname, dstname)163 def move(self, src, dst):164 """Rename a file or directory.165 Args:166 src: (str) source file or directory167 dst: (str) if the src is a directory, the dst must not already exist168 """169 if stat.S_ISDIR(self.filesystem.GetObject(src).st_mode):170 self.copytree(src, dst, symlinks=True)171 else:172 self.copy2(src, dst)173 self.filesystem.RemoveObject(src)174 def __getattr__(self, name):175 """Forwards any non-faked calls to the standard shutil module."""176 return getattr(self._shutil_module, name)177def _RunDoctest():178 # pylint: disable-msg=C6111,C6204,W0406179 import doctest180 from pyfakefs import fake_filesystem_shutil181 return doctest.testmod(fake_filesystem_shutil)182if __name__ == '__main__':...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var test = wpt('www.webpagetest.org');3}, function(err, data) {4 if (err) {5 console.log('Got an error: ' + err);6 } else {7 console.log('Got data: ' + JSON.stringify(data));8 }9});10var wpt = require('webpagetest');11var test = wpt('www.webpagetest.org');12}, function(err, data) {13 if (err) {14 console.log('Got an error: ' + err);15 } else {16 console.log('Got data: ' + JSON.stringify(data));17 }18});19var wpt = require('webpagetest');20var test = wpt('www.webpagetest.org');21}, function(err, data) {22 if (err) {23 console.log('Got an error: ' + err);24 } else {25 console.log('Got data: ' + JSON.stringify(data));26 }27});28var wpt = require('webpagetest');29var test = wpt('www.webpagetest.org');30}, function(err, data) {31 if (err) {32 console.log('Got an error: ' + err);33 } else {34 console.log('Got data

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3var options = {4};5 if (err) return console.error(err);6 console.log('Test Status: ' + data.statusText);7 if (data.statusText == 'Test Complete') {8 console.log('First View (ms): ' + data.data.average.firstView.loadTime);9 console.log('Repeat View (ms): ' + data.data.average.repeatView.loadTime);10 }11});12var wpt = require('webpagetest');13var wpt = new WebPageTest('www.webpagetest.org');14var options = {15};16 if (err) return console.error(err);17 console.log('Test Status: ' + data.statusText);18 if (data.statusText == 'Test Complete') {19 console.log('First View (ms): ' + data.data.average.firstView.loadTime);20 console.log('Repeat View (ms): ' + data.data.average.repeatView.loadTime);21 }22});23var wpt = require('webpagetest');24var wpt = new WebPageTest('www.webpagetest.org');25var options = {26};27 if (err) return console.error(err);28 console.log('Test Status: ' + data.statusText);29 if (data.statusText == 'Test Complete') {30 console.log('First View (ms): ' + data.data.average.firstView.loadTime);31 console.log('Repeat View (ms): ' + data.data.average.repeatView.loadTime);32 }33});34var wpt = require('webpagetest');35var wpt = new WebPageTest('www.webpagetest.org');36var options = {37};38wpt.runTest('

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var api = new wpt('www.webpagetest.org');3var options = {4};5api.runTest(options, function(err, data) {6 if (err) return console.error(err);7 console.log('Test status: ' + data.statusText);8 console.log('Test ID: ' + data.data.testId);9 console.log('Test URL: ' + data.data.summary);10 console.log('Test results: ' + data.data.userUrl);11 console.log('Test results: ' + data.data.jsonUrl);12});13api.getTestResults(data.data.testId, function(err, data) {14 if (err) return console.error(err);15 console.log('Test status: ' + data.statusText);16 console.log('Test ID: ' + data.data.testId);17 console.log('Test URL: ' + data.data.summary);18 console.log('Test results: ' + data.data.userUrl);19 console.log('Test results: ' + data.data.jsonUrl);20});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('wptools');2var fs = require('fs');3page.dst(function(err, resp){4 if(err){5 console.log(err);6 }else{7 console.log(resp);8 fs.writeFile('test.json', JSON.stringify(resp), function(err){9 if(err){10 console.log(err);11 }else{12 console.log('file saved');13 }14 });15 }16});17{ pageid: 17113,18 { name: 'Cristiano Ronaldo dos Santos Aveiro',19 birth_date: { text: '5 February 1985 (age 32)', date: '1985-02-05' },20 height: '1.87 m (6 ft 2 in)',

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2var dst = wpt.dst(40.7143528,-74.0059731,40.7484405,-73.9856644);3var dst = wpt.dst(40.7143528,-74.0059731,40.7484405,-73.9856644, 'm');4var dst = wpt.dst(40.7143528,-74.0059731,40.7484405,-73.9856644, 'n');5var dst = wpt.dst(40.7143528,-74.0059731,40.7484405,-73.9856644, 'f');6var dst = wpt.dst(40.7143528,-74.0059731,40.7484405,-73.9856644, 'k');7var dst = wpt.dst(40.7143528,-74.0059731,40.7484405,-73.9856644, 'x');8var dst = wpt.dst(40.7143528,-74.0059731,40.7484405,-73.9856644, 'y');9var dst = wpt.dst(40.7143528,-74.0059731,40.7484405,-73.9856644, 'a');10var dst = wpt.dst(40.7143528,-

Full Screen

Using AI Code Generation

copy

Full Screen

1var WebPageTest = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.2b5d9a5c5a5f5f8c8b5e5b5e5f5f5e5f5');3var options = {4};5wpt.runTest(url, options, function(err, data) {6 if (err) return console.error(err);7 console.log(data);8 wpt.getTestResults(data.data.testId, function(err, data) {9 if (err) return console.error(err);10 console.log(data);11 });12});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var options = {3};4var wpt = new WebPageTest('www.webpagetest.org', options.key);5wpt.runTest(testURL, options, function(err, data) {6 if (err) return console.error(err);7 console.log(data);8});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful