tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

xrRay_constructor.https.html (5523B)


      1 <!DOCTYPE html>
      2 <script src="/resources/testharness.js"></script>
      3 <script src="/resources/testharnessreport.js"></script>
      4 <script src="../resources/webxr_test_constants.js"></script>
      5 <script src="../resources/webxr_test_asserts.js"></script>
      6 <script>
      7 
      8 let constructor_test_name = "XRRay constructors work";
      9 
     10 let constructor_tests = function() {
     11  // Constructor tests for XRRay.
     12  // Spec: https://immersive-web.github.io/webxr/#xrray-interface
     13 
     14  //
     15  // Constructor 1 - from origin and direction
     16  //
     17 
     18  {
     19    // Check defaults - should be 0,0,0,1 for origin and 0,0,-1,0 for direction,
     20    // identity matrix for the transform:
     21    let xrRay1 = new XRRay();
     22    let xrRay2 = new XRRay({});
     23    let xrRay3 = new XRRay({}, {});
     24 
     25    assert_point_approx_equals(
     26      xrRay1.origin, {x : 0.0, y : 0.0, z : 0.0, w : 1.0},
     27      FLOAT_EPSILON, "origin-default:");
     28    assert_point_approx_equals(
     29      xrRay1.direction, {x : 0.0, y : 0.0, z : -1.0, w : 0.0},
     30      FLOAT_EPSILON, "direction-default:");
     31    assert_matrix_approx_equals(
     32      xrRay1.matrix, IDENTITY_MATRIX,
     33      FLOAT_EPSILON, "matrix-default:");
     34 
     35    assert_ray_approx_equals(xrRay1, xrRay2, FLOAT_EPSILON, "ray1-ray2-default:");
     36    assert_ray_approx_equals(xrRay2, xrRay3, FLOAT_EPSILON, "ray2-ray3-default:");
     37  }
     38 
     39  {
     40    // Check custom value for origin, default for direction:
     41    let originDict = {x : 11.0, y : 12.0, z : 13.0, w : 1.0};
     42    let xrRay1 = new XRRay(originDict);
     43    let xrRay2 = new XRRay(DOMPoint.fromPoint(originDict));
     44    let xrRay3 = new XRRay(DOMPointReadOnly.fromPoint(originDict));
     45    let matrix1 = [ 1,  0,  0, 0,
     46                    0,  1,  0, 0,
     47                    0,  0,  1, 0,
     48                   11, 12, 13, 1];
     49 
     50    assert_point_approx_equals(
     51      xrRay1.origin, originDict,
     52      FLOAT_EPSILON, "origin-custom-direction-default:");
     53    assert_point_approx_equals(
     54      xrRay1.direction, {x : 0.0, y : 0.0, z : -1.0, w : 0.0},
     55      FLOAT_EPSILON, "direction-custom-direction-default:");
     56    assert_matrix_approx_equals(
     57      xrRay1.matrix, matrix1,
     58      FLOAT_EPSILON, "matrix-custom-direction-default:");
     59 
     60    assert_ray_approx_equals(xrRay1, xrRay2, FLOAT_EPSILON, "ray1-ray2-direction-default:");
     61    assert_ray_approx_equals(xrRay2, xrRay3, FLOAT_EPSILON, "ray2-ray3-direction-default:");
     62  }
     63 
     64  {
     65    // Check custom values - ray rotated from -Z onto +X,
     66    // not placed at origin:
     67    // - from DOMPoint
     68    // - from DOMPointReadOnly
     69    let originDict = {x : 10.0, y : 10.0, z : 10.0, w : 1.0};
     70    let directionDict = {x : 10.0, y : 0.0, z : 0.0, w : 0.0};
     71    let directionNorm = {x :  1.0, y : 0.0, z : 0.0, w : 0.0};
     72    // column-major
     73    let matrix1 = [ 0,  0,  1,  0,
     74                    0,  1,  0,  0,
     75                   -1,  0,  0,  0,
     76                   10, 10, 10,  1];
     77 
     78    let xrRay1 = new XRRay(
     79      originDict,
     80      directionDict);
     81 
     82    let xrRay2 = new XRRay(
     83      DOMPoint.fromPoint(originDict),
     84      DOMPoint.fromPoint(directionDict));
     85 
     86    let xrRay3 = new XRRay(
     87      DOMPointReadOnly.fromPoint(originDict),
     88      DOMPointReadOnly.fromPoint(directionDict));
     89 
     90    assert_point_approx_equals(
     91      xrRay1.origin, originDict,
     92      FLOAT_EPSILON, "origin-custom:");
     93    assert_point_approx_equals(
     94      xrRay1.direction, directionNorm,
     95      FLOAT_EPSILON, "direction-custom:");
     96    assert_matrix_approx_equals(
     97      xrRay1.matrix, matrix1,
     98      FLOAT_EPSILON, "matrix-custom:");
     99 
    100    assert_ray_approx_equals(xrRay1, xrRay2, FLOAT_EPSILON, "ray1-ray2:");
    101    assert_ray_approx_equals(xrRay2, xrRay3, FLOAT_EPSILON, "ray2-ray3:");
    102  }
    103 
    104  {
    105    // Check that we throw exception on direction too close to 0,0,0:
    106    let originDict = {x : 10.0, y : 10.0, z : 10.0, w : 1.0};
    107    let directionDict = {x : 1.0, y : 0.0, z : 0.0, w : 0.0};
    108 
    109    assert_throws_js(TypeError, () => new XRRay(
    110        DOMPoint.fromPoint(originDict),
    111        DOMPoint.fromPoint({x : 0.0, y : 0.0, z : 0.0, w : 0.0})
    112    ), "Constructor should throw for zero direction");
    113 
    114    assert_throws_js(TypeError, () => new XRRay(
    115        DOMPoint.fromPoint(originDict),
    116        DOMPoint.fromPoint({x : 1.0, y : 0.0, z : 0.0, w : 0.5})
    117    ), "Constructor should throw for nonzero direction w coordinate");
    118 
    119    assert_throws_js(TypeError, () => new XRRay(
    120        DOMPoint.fromPoint({x : 10.0, y : 10.0, z : 10.0, w : 0.5}),
    121        DOMPoint.fromPoint(directionDict)
    122    ), "Constructor should throw for non-1 origin w coordinate");
    123  }
    124 
    125  //
    126  // Constructor 2 - from rigid transform.
    127  //
    128 
    129  {
    130    // Not placed at origin, ray rotated by 135 degrees around Y:
    131    let originDict = {x : 10.0, y : 10.0, z : 10.0, w : 1.0};
    132    let directionQuaternionDict = { x : 0, y : 0.9239, z : 0, w : 0.3827 };
    133    let directionNorm2 = { x : -0.707, y : 0.0, z : 0.707, w : 0.0 };
    134    let matrix2 = [-0.707,  0,  -0.707,   0,
    135                    0.,     1,   0,       0,
    136                    0.707,  0,  -0.707,   0,
    137                   10.,    10,  10.,      1];
    138 
    139    let xrRay = new XRRay(
    140      new XRRigidTransform(
    141        DOMPoint.fromPoint(originDict),
    142        DOMPoint.fromPoint(directionQuaternionDict)));
    143 
    144    assert_point_approx_equals(
    145      xrRay.origin, originDict,
    146      FLOAT_EPSILON, "origin-custom-rigid:");
    147    assert_point_approx_equals(
    148      xrRay.direction, directionNorm2,
    149      FLOAT_EPSILON, "direction-custom-rigid:");
    150 
    151    assert_matrix_approx_equals(
    152      xrRay.matrix, matrix2,
    153      FLOAT_EPSILON, "matrix-custom-rigid:");
    154  }
    155 };
    156 
    157 test(constructor_tests, constructor_test_name);
    158 
    159 </script>