Skip to main content

LpVec3

Struct LpVec3 

Source
pub struct LpVec3 {
    pub x: f64,
    pub y: f64,
    pub z: f64,
}
Expand description

Three doubles compressed into a shared-scale, low-precision vector.

LpVec3 normally occupies six bytes. The coordinates are divided by their rounded-up maximum absolute value, quantized into three unsigned 15-bit values, and packed with a shared scale factor:

X: 15 bits | Y: 15 bits | Z: 15 bits | continuation: 1 bit | scale: 2 bits

The first two packed bytes are written in little-endian order, while the remaining four bytes are written in big-endian order. If the scale factor is greater than three, its upper bits follow as a VarInt. A vector whose greatest absolute coordinate is below 1 / 32766, or which contains NaN, is encoded as the single byte 0x00 and decodes as zero.

This format is used by the Java Edition Spawn Entity and Set Entity Velocity packets.

§Examples

use mcproto_types::{TypeCodec, basic::LpVec3};

let value = LpVec3::new(10.0, 0.2, -5.0);
let mut encoded = Vec::new();
value.encode(&mut encoded)?;
assert_eq!(encoded, [0xf6, 0xff, 0x40, 0x01, 0x05, 0x1f, 0x02]);

let mut input = encoded.as_slice();
let decoded = LpVec3::decode(&mut input)?;
assert!((decoded.x - value.x).abs() < 0.001);
assert!((decoded.y - value.y).abs() < 0.001);
assert!((decoded.z - value.z).abs() < 0.001);
assert!(input.is_empty());

Fields§

§x: f64

The x coordinate.

§y: f64

The y coordinate.

§z: f64

The z coordinate.

Implementations§

Source§

impl LpVec3

Source

pub const MAX_QUANTIZED_VALUE: f64 = 32766.0

Greatest quantized coordinate value stored in a 15-bit field.

Source

pub const ZERO_THRESHOLD: f64

Coordinates below this absolute maximum use the one-byte zero form.

Source

pub const MAX_SCALE_FACTOR: u64

Greatest shared scale factor representable by two low bits and a 32-bit VarInt continuation.

Source

pub const fn new(x: f64, y: f64, z: f64) -> Self

Creates a low-precision vector from its coordinates.

Trait Implementations§

Source§

impl Clone for LpVec3

Source§

fn clone(&self) -> LpVec3

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for LpVec3

Source§

impl Debug for LpVec3

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for LpVec3

Source§

fn default() -> LpVec3

Returns the “default value” for a type. Read more
Source§

impl PartialEq for LpVec3

Source§

fn eq(&self, other: &LpVec3) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for LpVec3

Source§

impl TypeCodec for LpVec3

Source§

fn encode(&self, writer: &mut impl Write) -> Result<(), CodecError>

Encodes this value to the writer.
Source§

fn decode(reader: &mut impl Read) -> Result<Self, CodecError>

Decodes this value from the reader.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ContextualCodec for T
where T: TypeCodec,

Source§

fn encode_with_context( &self, writer: &mut impl Write, _context: &Context, ) -> Result<(), CodecError>

Encodes this value using context supplied by its enclosing structure.
Source§

fn decode_with_context( reader: &mut impl Read, _context: &Context, ) -> Result<T, CodecError>
where T: Sized,

Decodes this value using context supplied by its enclosing structure.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.