1 /*
2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 @file:JvmName("MathAssertions")
18 
19 package androidx.xr.runtime.testing.math
20 
21 import androidx.xr.runtime.math.Pose
22 import androidx.xr.runtime.math.Quaternion
23 import androidx.xr.runtime.math.Vector3
24 import com.google.common.truth.Truth.assertThat
25 
26 /**
27  * Asserts that two [Vector3]s are equal.
28  *
29  * @param actual the actual [Vector3] to compare
30  * @param expected the expected [Vector3] to compare
31  * @param epsilon the maximum difference allowed between the two [Vector3] objects
32  */
33 @JvmOverloads
assertVector3null34 public fun assertVector3(actual: Vector3, expected: Vector3, epsilon: Float = 1e-5f) {
35     assertThat(actual.x).isWithin(epsilon).of(expected.x)
36     assertThat(actual.y).isWithin(epsilon).of(expected.y)
37     assertThat(actual.z).isWithin(epsilon).of(expected.z)
38 }
39 
40 /**
41  * Asserts that two [Quaternion]s are equal.
42  *
43  * @param actual the actual [Quaternion] to compare
44  * @param expected the expected [Quaternion] to compare
45  * @param epsilon the maximum difference allowed between the two [Quaternion] objects
46  */
47 @JvmOverloads
assertRotationnull48 public fun assertRotation(actual: Quaternion, expected: Quaternion, epsilon: Float = 1e-5f) {
49     val dot = Math.abs(actual.toNormalized().dot(expected.toNormalized()))
50     assertThat(dot).isWithin(epsilon).of(1.0f)
51 }
52 
53 /**
54  * Asserts that two [Pose]s are equal.
55  *
56  * @param actual the actual [Pose] to compare
57  * @param expected the expected [Pose] to compare
58  * @param epsilon the maximum difference allowed between the two [Pose] objects
59  */
60 @JvmOverloads
assertPosenull61 public fun assertPose(actual: Pose, expected: Pose, epsilon: Float = 1e-5f) {
62     assertVector3(actual.translation, expected.translation, epsilon)
63     assertRotation(actual.rotation, expected.rotation, epsilon)
64 }
65