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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#[cfg(feature = "bevy")]
use bevy::transform::components::Transform;
use bincode::{Decode, Encode};
#[cfg(feature = "bevy")]
use glam::Quat;
use glam::{Vec2, Vec3, Vec4};
use nalgebra::{Point2, Point3, Point4, Vector2, Vector3, Vector4};

/// Network representation of translation and rotation. Note that scale is
/// assumed to be always 1.0 along all axes.
#[derive(Debug, Encode, Decode)]
pub struct TransformNet {
    translation: Vec3Net,
    rotation: Vec4Net,
}

#[cfg(feature = "bevy")]
impl From<&Transform> for TransformNet {
    fn from(transform: &Transform) -> Self {
        Self {
            translation: transform.translation.into(),
            rotation: Vec4Net {
                x: transform.rotation.x,
                y: transform.rotation.y,
                z: transform.rotation.z,
                w: transform.rotation.w,
            },
        }
    }
}

#[cfg(feature = "bevy")]
impl From<Transform> for TransformNet {
    fn from(transform: Transform) -> Self {
        Self::from(&transform)
    }
}

#[cfg(feature = "bevy")]
impl From<&TransformNet> for Transform {
    fn from(transform: &TransformNet) -> Self {
        Self {
            translation: transform.translation.into(),
            rotation: Quat::from_vec4(transform.rotation.into()),
            scale: Vec3::ONE,
        }
    }
}

#[derive(Clone, Copy, Debug, Encode, Decode)]
pub struct Vec2Net {
    x: f32,
    y: f32,
}

impl From<Vec2> for Vec2Net {
    fn from(vec: Vec2) -> Self {
        Self { x: vec.x, y: vec.y }
    }
}

impl From<Point2<f32>> for Vec2Net {
    fn from(point: Point2<f32>) -> Self {
        Self {
            x: point.x,
            y: point.y,
        }
    }
}

impl From<Vector2<f32>> for Vec2Net {
    fn from(vector: Vector2<f32>) -> Self {
        Self {
            x: vector.x,
            y: vector.y,
        }
    }
}

impl From<Vec2Net> for Vec2 {
    fn from(vec: Vec2Net) -> Self {
        Self::new(vec.x, vec.y)
    }
}

impl From<Vec2Net> for Point2<f32> {
    fn from(vec: Vec2Net) -> Self {
        Self::new(vec.x, vec.y)
    }
}

impl From<Vec2Net> for Vector2<f32> {
    fn from(vec: Vec2Net) -> Self {
        Self::new(vec.x, vec.y)
    }
}

#[derive(Clone, Copy, Debug, Encode, Decode)]
pub struct Vec3Net {
    x: f32,
    y: f32,
    z: f32,
}

impl From<Vec3> for Vec3Net {
    fn from(vec: Vec3) -> Self {
        Self {
            x: vec.x,
            y: vec.y,
            z: vec.z,
        }
    }
}

impl From<Point3<f32>> for Vec3Net {
    fn from(point: Point3<f32>) -> Self {
        Self {
            x: point.x,
            y: point.y,
            z: point.z,
        }
    }
}

impl From<Vector3<f32>> for Vec3Net {
    fn from(vector: Vector3<f32>) -> Self {
        Self {
            x: vector.x,
            y: vector.y,
            z: vector.z,
        }
    }
}

impl From<Vec3Net> for Vec3 {
    fn from(vec: Vec3Net) -> Self {
        Self::new(vec.x, vec.y, vec.z)
    }
}

impl From<Vec3Net> for Point3<f32> {
    fn from(vec: Vec3Net) -> Self {
        Self::new(vec.x, vec.y, vec.z)
    }
}

impl From<Vec3Net> for Vector3<f32> {
    fn from(vec: Vec3Net) -> Self {
        Self::new(vec.x, vec.y, vec.z)
    }
}

#[derive(Clone, Copy, Debug, Encode, Decode)]
pub struct Vec4Net {
    x: f32,
    y: f32,
    z: f32,
    w: f32,
}

impl From<Vec4> for Vec4Net {
    fn from(vec: Vec4) -> Self {
        Self {
            x: vec.x,
            y: vec.y,
            z: vec.z,
            w: vec.w,
        }
    }
}

impl From<Point4<f32>> for Vec4Net {
    fn from(point: Point4<f32>) -> Self {
        Self {
            x: point.x,
            y: point.y,
            z: point.z,
            w: point.w,
        }
    }
}

impl From<Vector4<f32>> for Vec4Net {
    fn from(vector: Vector4<f32>) -> Self {
        Self {
            x: vector.x,
            y: vector.y,
            z: vector.z,
            w: vector.w,
        }
    }
}

impl From<Vec4Net> for Vec4 {
    fn from(vec: Vec4Net) -> Self {
        Self::new(vec.x, vec.y, vec.z, vec.w)
    }
}

impl From<Vec4Net> for Point4<f32> {
    fn from(vec: Vec4Net) -> Self {
        Self::new(vec.x, vec.y, vec.z, vec.w)
    }
}

impl From<Vec4Net> for Vector4<f32> {
    fn from(vec: Vec4Net) -> Self {
        Self::new(vec.x, vec.y, vec.z, vec.w)
    }
}