From 9d76d089480545c9376d63069a4e1c6b3731e64e Mon Sep 17 00:00:00 2001 From: tfarley Date: Sun, 24 Jul 2016 22:21:14 -0700 Subject: [PATCH] Add Vector3 class * Add Vector3 class * Add Vector3 test --- .../net/psforever/newcodecs/package.scala | 4 ++++ .../scala/net/psforever/types/Vector3.scala | 24 +++++++++++++++++++ common/src/test/scala/CodecTest.scala | 22 +++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 common/src/main/scala/net/psforever/types/Vector3.scala diff --git a/common/src/main/scala/net/psforever/newcodecs/package.scala b/common/src/main/scala/net/psforever/newcodecs/package.scala index 8b090ab1..8dc532ce 100644 --- a/common/src/main/scala/net/psforever/newcodecs/package.scala +++ b/common/src/main/scala/net/psforever/newcodecs/package.scala @@ -1,10 +1,14 @@ // Copyright (c) 2016 PSForever.net to present package net.psforever.newcodecs +import scodec.Attempt +import scodec.Attempt.{Failure, Successful} import scodec.Codec package object newcodecs { def q_double(min: Double, max: Double, bits: Int): Codec[Double] = new QuantizedDoubleCodec(min, max, bits) + def q_float(min : Double, max : Double, bits : Int): Codec[Float] = q_double(min, max, bits).narrow(v => Attempt.successful(v.toFloat), _.toDouble) + } diff --git a/common/src/main/scala/net/psforever/types/Vector3.scala b/common/src/main/scala/net/psforever/types/Vector3.scala new file mode 100644 index 00000000..3fdae394 --- /dev/null +++ b/common/src/main/scala/net/psforever/types/Vector3.scala @@ -0,0 +1,24 @@ +// Copyright (c) 2016 PSForever.net to present +package net.psforever.types + +import net.psforever.newcodecs._ +import scodec.Codec +import scodec.codecs._ + +final case class Vector3(x : Float, + y : Float, + z : Float) + +object Vector3 { + implicit val codec_pos : Codec[Vector3] = ( + ("x" | newcodecs.q_float(0.0, 8192.0, 20)) :: + ("y" | newcodecs.q_float(0.0, 8192.0, 20)) :: + ("z" | newcodecs.q_float(0.0, 1024.0, 16)) + ).as[Vector3] + + implicit val codec_vel : Codec[Vector3] = ( + ("x" | newcodecs.q_float(-256.0, 256.0, 14)) :: + ("y" | newcodecs.q_float(-256.0, 256.0, 14)) :: + ("z" | newcodecs.q_float(-256.0, 256.0, 14)) + ).as[Vector3] +} diff --git a/common/src/test/scala/CodecTest.scala b/common/src/test/scala/CodecTest.scala index 7d05ed21..f3654410 100644 --- a/common/src/test/scala/CodecTest.scala +++ b/common/src/test/scala/CodecTest.scala @@ -1,6 +1,7 @@ // Copyright (c) 2016 PSForever.net to present import org.specs2.mutable._ import net.psforever.newcodecs._ +import net.psforever.types._ import scodec.bits._ class CodecTest extends Specification { @@ -31,4 +32,25 @@ class CodecTest extends Specification { newcodecs.q_double(-256.0, 256.0, 14).encode(257.0).require.bytes mustEqual string_6 } } + + "Vector3" should { + val string_pos = hex"6E2D762222B616" + val string_vel = hex"857D4E0FFFC0" + + "decode position" in { + Vector3.codec_pos.decode(string_pos.bits).require.value mustEqual Vector3(3674.859375f, 1092.7656f, 90.84375f) + } + + "encode position" in { + Vector3.codec_pos.encode(Vector3(3674.859375f, 1092.7656f, 90.84375f)).require.bytes mustEqual string_pos + } + + "decode velocity" in { + Vector3.codec_vel.decode(string_vel.bits).require.value mustEqual Vector3(-3.84375f, 2.59375f, 255.96875f) + } + + "encode velocity" in { + Vector3.codec_vel.encode(Vector3(-3.84375f, 2.59375f, 255.96875f)).require.bytes mustEqual string_vel + } + } }