xrRay_matrix.https.html (3806B)
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 src="../resources/webxr_math_utils.js"></script> 7 <script> 8 9 let matrix_tests_name = "XRRay matrix works"; 10 11 let matrix_tests = function() { 12 // Matrix tests for XRRay. 13 // Spec: https://immersive-web.github.io/webxr/#xrray-interface 14 15 const initialOrigin = {x : 0, y : 0, z : 0, w : 1}; 16 const initialDirection = {x : 0, y : 0, z : -1, w : 0}; 17 18 // Test 1. Simple translation and rotation. 19 { 20 let originDict = {x : 10.0, y : 10.0, z : 10.0, w : 1.0}; 21 let directionDict = {x : 10.0, y : 0.0, z : 0.0, w : 0.0}; 22 let directionNorm = {x : 1.0, y : 0.0, z : 0.0, w : 0.0}; 23 let xrRay = new XRRay( 24 DOMPoint.fromPoint(originDict), 25 DOMPoint.fromPoint(directionDict)); 26 27 let transformedOrigin = normalize_perspective(transform_point_by_matrix(xrRay.matrix, initialOrigin)); 28 let transformedDirection = normalize_perspective(transform_point_by_matrix(xrRay.matrix, initialDirection)); 29 30 assert_point_approx_equals( 31 originDict, transformedOrigin, 32 FLOAT_EPSILON, "origin-test1:"); 33 assert_point_approx_equals( 34 directionNorm, transformedDirection, 35 FLOAT_EPSILON, "direction-test1:"); 36 } 37 38 // Test 2. Co-linear direction - rotation by 180 deg. 39 { 40 let originDict = {x : 10.0, y : 10.0, z : 10.0, w : 1.0}; 41 let directionDict = {x : 0.0, y : 0.0, z : 1.0, w : 0.0}; 42 let directionNorm = {x : 0.0, y : 0.0, z : 1.0, w : 0.0}; 43 let xrRay = new XRRay( 44 DOMPoint.fromPoint(originDict), 45 DOMPoint.fromPoint(directionDict)); 46 47 let transformedOrigin = normalize_perspective(transform_point_by_matrix(xrRay.matrix, initialOrigin)); 48 let transformedDirection = normalize_perspective(transform_point_by_matrix(xrRay.matrix, initialDirection)); 49 50 assert_point_approx_equals( 51 originDict, transformedOrigin, 52 FLOAT_EPSILON, "origin-test2:"); 53 assert_point_approx_equals( 54 directionNorm, transformedDirection, 55 FLOAT_EPSILON, "direction-test2:"); 56 } 57 58 // Test 3. No translation. 59 { 60 let originDict = {x : 0.0, y : 0.0, z : 0.0, w : 1.0}; 61 let directionDict = {x : 10.0, y : 0.0, z : 0.0, w : 0.0}; 62 let directionNorm = {x : 1.0, y : 0.0, z : 0.0, w : 0.0}; 63 let xrRay = new XRRay( 64 DOMPoint.fromPoint(originDict), 65 DOMPoint.fromPoint(directionDict)); 66 67 let transformedOrigin = normalize_perspective(transform_point_by_matrix(xrRay.matrix, initialOrigin)); 68 let transformedDirection = normalize_perspective(transform_point_by_matrix(xrRay.matrix, initialDirection)); 69 70 assert_point_approx_equals( 71 originDict, transformedOrigin, 72 FLOAT_EPSILON, "origin-test3:"); 73 assert_point_approx_equals( 74 directionNorm, transformedDirection, 75 FLOAT_EPSILON, "direction-test3:"); 76 } 77 78 // Test 4. No rotation. 79 { 80 let originDict = {x : 10.0, y : 10.0, z : 10.0, w : 1.0}; 81 let directionDict = {x : 0.0, y : 0.0, z : -1.0, w : 0.0}; 82 let directionNorm = {x : 0.0, y : 0.0, z : -1.0, w : 0.0}; 83 let xrRay = new XRRay( 84 DOMPoint.fromPoint(originDict), 85 DOMPoint.fromPoint(directionDict)); 86 87 let transformedOrigin = normalize_perspective(transform_point_by_matrix(xrRay.matrix, initialOrigin)); 88 let transformedDirection = normalize_perspective(transform_point_by_matrix(xrRay.matrix, initialDirection)); 89 90 assert_point_approx_equals( 91 originDict, transformedOrigin, 92 FLOAT_EPSILON, "origin-test4:"); 93 assert_point_approx_equals( 94 directionNorm, transformedDirection, 95 FLOAT_EPSILON, "direction-test4:"); 96 } 97 }; 98 99 test(matrix_tests, matrix_tests_name); 100 101 </script>