1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//     dis-rust - A rust implementation of the DIS simulation protocol.
//     Copyright (C) 2022 Thomas Mann
// 
//     This software is dual-licensed. It is available under the conditions of
//     the GNU Affero General Public License (see the LICENSE file included) 
//     or under a commercial license (email contact@coffeebreakdevs.com for
//     details).

use bytes::{BytesMut, BufMut, Buf};

#[derive(Copy, Clone, Debug, Default)]
/// Linear Acceleration Record as defined in IEEE 1278.1 standard. Used to communicate the acceleration of an entity for dead-reckoning during the simulation.
/// Typically uses metres per second per second as units
 pub struct LinearAccelerationRecord {
     pub first_vector_component_field: f32,
     pub second_vector_component_field: f32,
     pub third_vector_component_field: f32
 }

 impl LinearAccelerationRecord {
    /// Provides a function to create a new LinearAccelerationRecord.
    /// 
    /// # Examples
    /// 
    /// Creating a blank LinearAccelerationRecord:
    /// 
    /// ```
    /// let linear_acceleration_record = LinearAccelerationRecord::new{
    ///     x: 0.0,
    ///     y: 0.0,
    ///     z: 0.0
    /// };
    /// ```
    /// 
    pub fn new(x: f32, y: f32, z: f32) -> Self {
        LinearAccelerationRecord {
            first_vector_component_field: x,
            second_vector_component_field: y,
            third_vector_component_field: z
        }
     }

    /// Fills a BytesMut struct with a LinearAccelerationRecord serialised into binary. This buffer is then ready to be sent via
    /// UDP to the simluation network.
    pub fn serialize(&self, buf: &mut BytesMut) {
        buf.put_f32(self.first_vector_component_field);
        buf.put_f32(self.second_vector_component_field);
        buf.put_f32(self.third_vector_component_field);
    }

    pub fn decode(buf: &mut BytesMut) -> LinearAccelerationRecord {
        LinearAccelerationRecord { 
            first_vector_component_field: buf.get_f32(), 
            second_vector_component_field: buf.get_f32(), 
            third_vector_component_field: buf.get_f32()
        }
    }
 }