1+ /*
2+ * Copyright 2025 Lambda
3+ *
4+ * This program is free software: you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation, either version 3 of the License, or
7+ * (at your option) any later version.
8+ *
9+ * This program is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ * GNU General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU General Public License
15+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16+ */
17+
18+ import com.lambda.interaction.request.rotation.Rotation
19+ import com.lambda.interaction.request.rotation.visibilty.PlaceDirection
20+ import kotlin.math.abs
21+ import kotlin.math.atan
22+ import kotlin.math.cos
23+ import kotlin.math.sin
24+ import kotlin.test.Test
25+ import kotlin.test.assertEquals
26+ import kotlin.test.assertTrue
27+
28+ class PlaceDirectionTest {
29+
30+ @Test
31+ fun `test pitch snapping for East direction` () {
32+ val direction = PlaceDirection .East
33+
34+ // Calculate expected pitch boundary for East at yaw -90.0
35+ val yawRad = Math .toRadians(- 90.0 )
36+ val expectedBoundary = Math .toDegrees(atan(abs(sin(yawRad))))
37+
38+ // Test rotation outside the area (pitch too high)
39+ val rotationOutsideHigh = Rotation (- 90.0 , expectedBoundary + 10.0 )
40+ val snappedHigh = direction.snapToArea(rotationOutsideHigh)
41+
42+ // Test rotation outside the area (pitch too low)
43+ val rotationOutsideLow = Rotation (- 90.0 , - expectedBoundary - 10.0 )
44+ val snappedLow = direction.snapToArea(rotationOutsideLow)
45+
46+ // Verify that the pitch is snapped to the boundary
47+ assertEquals(expectedBoundary, snappedHigh.pitch, 0.001 , " Pitch should be snapped to the upper boundary" )
48+ assertEquals(- expectedBoundary, snappedLow.pitch, 0.001 , " Pitch should be snapped to the lower boundary" )
49+ }
50+
51+ @Test
52+ fun `test pitch snapping for North direction` () {
53+ val direction = PlaceDirection .North
54+
55+ // Calculate expected pitch boundary for North at yaw -180.0
56+ val yawRad = Math .toRadians(- 180.0 )
57+ val expectedBoundary = Math .toDegrees(atan(abs(cos(yawRad))))
58+
59+ // Test rotation outside the area (pitch too high)
60+ val rotationOutsideHigh = Rotation (- 180.0 , expectedBoundary + 10.0 )
61+ val snappedHigh = direction.snapToArea(rotationOutsideHigh)
62+
63+ // Test rotation outside the area (pitch too low)
64+ val rotationOutsideLow = Rotation (- 180.0 , - expectedBoundary - 10.0 )
65+ val snappedLow = direction.snapToArea(rotationOutsideLow)
66+
67+ // Verify that the pitch is snapped to the boundary
68+ assertEquals(expectedBoundary, snappedHigh.pitch, 0.001 , " Pitch should be snapped to the upper boundary" )
69+ assertEquals(- expectedBoundary, snappedLow.pitch, 0.001 , " Pitch should be snapped to the lower boundary" )
70+ }
71+
72+ @Test
73+ fun `test pitch snapping for UpEast direction` () {
74+ val direction = PlaceDirection .UpEast
75+
76+ // Calculate expected pitch boundary for UpEast at yaw -90.0
77+ val yawRad = Math .toRadians(- 90.0 )
78+ val expectedBoundary = Math .toDegrees(atan(abs(sin(yawRad))))
79+
80+ // Test rotation outside the area (pitch too low)
81+ val rotationOutside = Rotation (- 90.0 , expectedBoundary - 10.0 )
82+ val snapped = direction.snapToArea(rotationOutside)
83+
84+ // Verify that the pitch is snapped to the boundary
85+ assertEquals(expectedBoundary, snapped.pitch, 0.001 , " Pitch should be snapped to the boundary" )
86+ }
87+
88+ @Test
89+ fun `test pitch snapping for DownNorth direction` () {
90+ val direction = PlaceDirection .DownNorth
91+
92+ // Calculate expected pitch boundary for DownNorth at yaw -180.0
93+ val yawRad = Math .toRadians(- 180.0 )
94+ val expectedBoundary = Math .toDegrees(atan(abs(cos(yawRad))))
95+
96+ // Test rotation outside the area (pitch too high)
97+ val rotationOutside = Rotation (- 180.0 , - expectedBoundary + 10.0 )
98+ val snapped = direction.snapToArea(rotationOutside)
99+
100+ // Verify that the pitch is snapped to the boundary
101+ assertEquals(- expectedBoundary, snapped.pitch, 0.001 , " Pitch should be snapped to the boundary" )
102+ }
103+
104+ @Test
105+ fun `test no snapping when rotation is already in area` () {
106+ val direction = PlaceDirection .East
107+
108+ // Create a rotation that should be in the East area
109+ val rotation = Rotation (- 90.0 , 0.0 )
110+
111+ // Verify that the rotation is in the area
112+ assertTrue(direction.isInArea(rotation), " Rotation should be in the East area" )
113+
114+ // Verify that snapToArea returns the same rotation
115+ val snapped = direction.snapToArea(rotation)
116+ assertEquals(rotation.yaw, snapped.yaw, 0.001 , " Yaw should not change" )
117+ assertEquals(rotation.pitch, snapped.pitch, 0.001 , " Pitch should not change" )
118+ }
119+ }
0 commit comments