Skip to content

Commit 68c8eec

Browse files
committed
Merge remote-tracking branch 'origin/feature/packetmine-rewrite' into feature/packetmine-rewrite
# Conflicts: # common/src/test/kotlin/PlaceDirectionTest.kt
2 parents 1443cb5 + 8221847 commit 68c8eec

File tree

4 files changed

+318
-21
lines changed

4 files changed

+318
-21
lines changed

common/src/main/kotlin/com/lambda/interaction/request/rotation/Rotation.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,14 @@ data class Rotation(val yaw: Double, val pitch: Double) {
104104
fun wrap(deg: Double) = wrapDegrees(deg)
105105

106106
fun Rotation.lerp(other: Rotation, delta: Double): Rotation {
107-
val yaw = wrap(this.yaw + delta * (other.yaw - this.yaw))
108-
val pitch = wrap(this.pitch + delta * (other.pitch - this.pitch))
107+
// Calculate the wrapped difference to ensure we take the shortest path
108+
val yawDiff = wrap(other.yaw - this.yaw)
109+
val pitchDiff = wrap(other.pitch - this.pitch)
110+
111+
// Apply the delta to the wrapped difference
112+
val yaw = wrap(this.yaw + delta * yawDiff)
113+
val pitch = wrap(this.pitch + delta * pitchDiff)
114+
109115
return Rotation(yaw, pitch)
110116
}
111117

common/src/main/kotlin/com/lambda/interaction/request/rotation/visibilty/PlaceDirection.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,17 @@ enum class PlaceDirection(
3434
val vector: Vec3i,
3535
private val yawRanges: List<ClosedRange<Double>>
3636
) {
37-
Up ( 0.0, -90.0, 0, 1, 0, listOf(Double.MIN_VALUE..Double.MAX_VALUE)),
38-
Down ( 0.0, 90.0, 0, -1, 0, listOf(Double.MIN_VALUE..Double.MAX_VALUE)),
39-
40-
UpNorth ( -180.0, -90.0, 0, 1, -1, northYawRanges),
37+
UpNorth ( -180.0, -90.0, 0, 1, -1, northYawRanges),
4138
UpSouth ( 0.0, -90.0, 0, 1, 1, listOf(southYawRange)),
4239
UpWest ( 90.0, -90.0, 1, 1, 0, listOf(westYawRange)),
4340
UpEast ( -90.0, -90.0, -1, 1, 0, listOf(eastYawRange)),
4441

45-
DownNorth( -180.0, 90.0, 0, -1, -1, northYawRanges),
42+
DownNorth( -180.0, 90.0, 0, -1, -1, northYawRanges),
4643
DownSouth( 0.0, 90.0, 0, -1, 1, listOf(southYawRange)),
4744
DownWest ( 90.0, 90.0, 1, -1, 0, listOf(westYawRange)),
4845
DownEast ( -90.0, 90.0, -1, -1, 0, listOf(eastYawRange)),
4946

50-
North ( -180.0, 0.0, 0, 0, -1, northYawRanges),
47+
North ( -180.0, 0.0, 0, 0, -1, northYawRanges),
5148
South ( 0.0, 0.0, 0, 0, 1, listOf(southYawRange)),
5249
West ( 90.0, 0.0, 1, 0, 0, listOf(westYawRange)),
5350
East ( -90.0, 0.0, -1, 0, 0, listOf(eastYawRange));
@@ -90,7 +87,7 @@ enum class PlaceDirection(
9087
else -> calculateHorizontalPitch(rot.pitch, pitchBoundaryNS)
9188
}
9289
}
93-
// Handle purely UP/DOWN directions
90+
// impossible to look just up or just down as you are always facing a horizontal direction
9491
else -> rotation.pitch
9592
}
9693

@@ -141,8 +138,8 @@ enum class PlaceDirection(
141138
private fun isWest(): Boolean = this == West || this == UpWest || this == DownWest
142139
private fun isNorth(): Boolean = this == North || this == UpNorth || this == DownNorth
143140
private fun isSouth(): Boolean = this == South || this == UpSouth || this == DownSouth
144-
private fun isUp(): Boolean = this == UpEast || this == UpWest || this == UpNorth || this == UpSouth || this == Up
145-
private fun isDown(): Boolean = this == DownEast || this == DownWest || this == DownNorth || this == DownSouth || this == Down
141+
private fun isUp(): Boolean = this == UpEast || this == UpWest || this == UpNorth || this == UpSouth
142+
private fun isDown(): Boolean = this == DownEast || this == DownWest || this == DownNorth || this == DownSouth
146143

147144
fun isInArea(rot: Rotation) = fromRotation(rot) == this
148145

common/src/test/kotlin/PlaceDirectionTest.kt

Lines changed: 189 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,19 @@ import com.lambda.interaction.request.rotation.visibilty.PlaceDirection
2020
import kotlin.math.abs
2121
import kotlin.math.atan
2222
import kotlin.math.cos
23+
import kotlin.math.sin
2324
import kotlin.test.Test
2425
import kotlin.test.assertEquals
26+
import kotlin.test.assertTrue
27+
import kotlin.test.assertFalse
2528

29+
/**
30+
* Tests for the PlaceDirection class
31+
*/
2632
class PlaceDirectionTest {
2733

34+
// Tests for snapToArea method - Horizontal directions
35+
2836
@Test
2937
fun `test pitch snapping for East direction`() {
3038
val direction = PlaceDirection.East
@@ -35,6 +43,16 @@ class PlaceDirectionTest {
3543
assertEquals(direction, PlaceDirection.fromRotation(snapped))
3644
}
3745

46+
@Test
47+
fun `test pitch snapping for West direction`() {
48+
val direction = PlaceDirection.West
49+
val rot = Rotation(90.0, 90.0)
50+
val snapped = direction.snapToArea(rot)
51+
52+
// Verify that the pitch is snapped to the boundary
53+
assertEquals(direction, PlaceDirection.fromRotation(snapped))
54+
}
55+
3856
@Test
3957
fun `test pitch snapping for North direction`() {
4058
val direction = PlaceDirection.North
@@ -45,6 +63,18 @@ class PlaceDirectionTest {
4563
assertEquals(direction, PlaceDirection.fromRotation(snapped))
4664
}
4765

66+
@Test
67+
fun `test pitch snapping for South direction`() {
68+
val direction = PlaceDirection.South
69+
val rot = Rotation(0.0, 90.0)
70+
val snapped = direction.snapToArea(rot)
71+
72+
// Verify that the pitch is snapped to the boundary
73+
assertEquals(direction, PlaceDirection.fromRotation(snapped))
74+
}
75+
76+
// Tests for snapToArea method - Up directions
77+
4878
@Test
4979
fun `test pitch snapping for UpEast direction`() {
5080
val direction = PlaceDirection.UpEast
@@ -55,19 +85,168 @@ class PlaceDirectionTest {
5585
assertEquals(direction, PlaceDirection.fromRotation(snapped))
5686
}
5787

88+
@Test
89+
fun `test pitch snapping for UpWest direction`() {
90+
val direction = PlaceDirection.UpWest
91+
val rot = Rotation(90.0, 0.0)
92+
val snapped = direction.snapToArea(rot)
93+
94+
// Verify that the pitch is snapped to the boundary
95+
assertEquals(direction, PlaceDirection.fromRotation(snapped))
96+
}
97+
98+
@Test
99+
fun `test pitch snapping for UpNorth direction`() {
100+
val direction = PlaceDirection.UpNorth
101+
val rot = Rotation(-180.0, 0.0)
102+
val snapped = direction.snapToArea(rot)
103+
104+
// Verify that the pitch is snapped to the boundary
105+
assertEquals(direction, PlaceDirection.fromRotation(snapped))
106+
}
107+
108+
@Test
109+
fun `test pitch snapping for UpSouth direction`() {
110+
val direction = PlaceDirection.UpSouth
111+
val rot = Rotation(0.0, 0.0)
112+
val snapped = direction.snapToArea(rot)
113+
114+
// Verify that the pitch is snapped to the boundary
115+
assertEquals(direction, PlaceDirection.fromRotation(snapped))
116+
}
117+
118+
// Tests for snapToArea method - Down directions
119+
120+
@Test
121+
fun `test pitch snapping for DownEast direction`() {
122+
val direction = PlaceDirection.DownEast
123+
val rot = Rotation(-90.0, 0.0)
124+
val snapped = direction.snapToArea(rot)
125+
126+
// Verify that the pitch is snapped to the boundary
127+
assertEquals(direction, PlaceDirection.fromRotation(snapped))
128+
}
129+
130+
@Test
131+
fun `test pitch snapping for DownWest direction`() {
132+
val direction = PlaceDirection.DownWest
133+
val rot = Rotation(90.0, 0.0)
134+
val snapped = direction.snapToArea(rot)
135+
136+
// Verify that the pitch is snapped to the boundary
137+
assertEquals(direction, PlaceDirection.fromRotation(snapped))
138+
}
139+
58140
@Test
59141
fun `test pitch snapping for DownNorth direction`() {
60142
val direction = PlaceDirection.DownNorth
61-
62-
// Calculate expected pitch boundary for DownNorth at yaw -180.0
63-
val yawRad = Math.toRadians(-180.0)
64-
val expectedBoundary = Math.toDegrees(atan(abs(cos(yawRad))))
65-
66-
// Test rotation outside the area (pitch too high)
67-
val rotationOutside = Rotation(-180.0, -expectedBoundary + 10.0)
68-
val snapped = direction.snapToArea(rotationOutside)
69-
143+
val rot = Rotation(-180.0, 0.0)
144+
val snapped = direction.snapToArea(rot)
145+
146+
// Verify that the pitch is snapped to the boundary
147+
assertEquals(direction, PlaceDirection.fromRotation(snapped))
148+
}
149+
150+
@Test
151+
fun `test pitch snapping for DownSouth direction`() {
152+
val direction = PlaceDirection.DownSouth
153+
val rot = Rotation(0.0, 0.0)
154+
val snapped = direction.snapToArea(rot)
155+
156+
// Verify that the pitch is snapped to the boundary
157+
assertEquals(direction, PlaceDirection.fromRotation(snapped))
158+
}
159+
160+
// Tests for snapToArea method - Pure Up/Down directions
161+
162+
@Test
163+
fun `test pitch snapping for UpSouth direction with extreme pitch`() {
164+
val direction = PlaceDirection.UpSouth
165+
val rot = Rotation(0.0, -45.0)
166+
val snapped = direction.snapToArea(rot)
167+
168+
// Verify that the pitch is snapped to the boundary
169+
assertEquals(direction, PlaceDirection.fromRotation(snapped))
170+
}
171+
172+
@Test
173+
fun `test pitch snapping for DownSouth direction with extreme pitch`() {
174+
val direction = PlaceDirection.DownSouth
175+
val rot = Rotation(0.0, 45.0)
176+
val snapped = direction.snapToArea(rot)
177+
70178
// Verify that the pitch is snapped to the boundary
71179
assertEquals(direction, PlaceDirection.fromRotation(snapped))
72180
}
73-
}
181+
182+
// Tests for when rotation is already in the area
183+
184+
@Test
185+
fun `test no snapping when rotation is already in area`() {
186+
val direction = PlaceDirection.East
187+
// Create a rotation that's already in the East area
188+
val rot = Rotation(-90.0, 0.0)
189+
190+
// Verify it's already in the area
191+
assertEquals(direction, PlaceDirection.fromRotation(rot))
192+
193+
val snapped = direction.snapToArea(rot)
194+
195+
// Verify that the rotation is unchanged
196+
assertEquals(rot, snapped)
197+
}
198+
199+
// Tests for isInArea method
200+
201+
@Test
202+
fun `test isInArea method`() {
203+
val eastRot = Rotation(-90.0, 0.0)
204+
val northRot = Rotation(-180.0, 0.0)
205+
206+
assertTrue(PlaceDirection.East.isInArea(eastRot))
207+
assertFalse(PlaceDirection.East.isInArea(northRot))
208+
209+
assertTrue(PlaceDirection.North.isInArea(northRot))
210+
assertFalse(PlaceDirection.North.isInArea(eastRot))
211+
}
212+
213+
// Tests for fromRotation method
214+
215+
@Test
216+
fun `test fromRotation for horizontal directions`() {
217+
assertEquals(PlaceDirection.East, PlaceDirection.fromRotation(Rotation(-90.0, 0.0)))
218+
assertEquals(PlaceDirection.West, PlaceDirection.fromRotation(Rotation(90.0, 0.0)))
219+
assertEquals(PlaceDirection.North, PlaceDirection.fromRotation(Rotation(-180.0, 0.0)))
220+
assertEquals(PlaceDirection.South, PlaceDirection.fromRotation(Rotation(0.0, 0.0)))
221+
}
222+
223+
@Test
224+
fun `test fromRotation for up directions`() {
225+
assertEquals(PlaceDirection.UpEast, PlaceDirection.fromRotation(Rotation(-90.0, -60.0)))
226+
assertEquals(PlaceDirection.UpWest, PlaceDirection.fromRotation(Rotation(90.0, -60.0)))
227+
assertEquals(PlaceDirection.UpNorth, PlaceDirection.fromRotation(Rotation(-180.0, -60.0)))
228+
assertEquals(PlaceDirection.UpSouth, PlaceDirection.fromRotation(Rotation(0.0, -60.0)))
229+
}
230+
231+
@Test
232+
fun `test fromRotation for down directions`() {
233+
assertEquals(PlaceDirection.DownEast, PlaceDirection.fromRotation(Rotation(-90.0, 60.0)))
234+
assertEquals(PlaceDirection.DownWest, PlaceDirection.fromRotation(Rotation(90.0, 60.0)))
235+
assertEquals(PlaceDirection.DownNorth, PlaceDirection.fromRotation(Rotation(-180.0, 60.0)))
236+
assertEquals(PlaceDirection.DownSouth, PlaceDirection.fromRotation(Rotation(0.0, 60.0)))
237+
}
238+
239+
// Edge case tests
240+
241+
@Test
242+
fun `test edge case with yaw at boundaries`() {
243+
// Test with yaw at the boundaries between directions
244+
val boundaryYaw = -135.0 // Boundary between North and East
245+
246+
// Slightly to the East side
247+
assertEquals(PlaceDirection.East, PlaceDirection.fromRotation(Rotation(boundaryYaw + 0.1, 0.0)))
248+
249+
// Slightly to the North side
250+
assertEquals(PlaceDirection.North, PlaceDirection.fromRotation(Rotation(boundaryYaw - 0.1, 0.0)))
251+
}
252+
}

0 commit comments

Comments
 (0)