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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
use cgmath::{
    BaseFloat, Basis2, EuclideanSpace, Euler, Quaternion, Rad, Rotation, Rotation2, Vector3,
    VectorSpace, Zero,
};

use Pose;

/// Velocity
///
/// ### Type parameters:
///
/// - `L`: Linear velocity, usually `Vector2` or `Vector3`
/// - `A`: Angular velocity, usually `Scalar` or `Vector3`
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Velocity<L, A> {
    linear: L,
    angular: A,
}

impl<L, A> Default for Velocity<L, A>
where
    L: Zero,
    A: Clone + Zero,
{
    fn default() -> Self {
        Self::new(L::zero(), A::zero())
    }
}

impl<L, A> Velocity<L, A>
where
    L: Zero,
    A: Clone + Zero,
{
    /// Create new velocity object, with both linear and angular velocity
    pub fn new(linear: L, angular: A) -> Self {
        Self { linear, angular }
    }

    /// Create new velocity object with only linear velocity
    pub fn from_linear(linear: L) -> Self {
        Self::new(linear, A::zero())
    }

    /// Set linear velocity
    pub fn set_linear(&mut self, linear: L) {
        self.linear = linear;
    }

    /// Get linear velocity
    pub fn linear(&self) -> &L {
        &self.linear
    }

    /// Set angular velocity
    pub fn set_angular(&mut self, angular: A) {
        self.angular = angular;
    }

    /// Get angular velocity
    pub fn angular(&self) -> &A {
        &self.angular
    }

    /// Apply velocity to pose.
    ///
    /// ### Parameters:
    ///
    /// - `pose`: Pose to apply the velocity to
    /// - `dt`: Time step
    ///
    /// ### Type parameters:
    ///
    /// - `B`: Transform type (`BodyPose3` or similar)
    /// - `P`: Positional quantity, usually `Point2` or `Point3`
    /// - `R`: Rotational quantity, usually `Basis2` or `Quaternion`
    pub fn apply<B, P, R>(&self, pose: &B, dt: L::Scalar) -> B
    where
        P: EuclideanSpace<Scalar = L::Scalar, Diff = L>,
        L: VectorSpace,
        L::Scalar: BaseFloat,
        R: ApplyAngular<L::Scalar, A> + Rotation<P>,
        B: Pose<P, R>,
    {
        B::new(
            self.apply_linear(pose.position(), dt),
            self.apply_angular(pose.rotation(), dt),
        )
    }

    /// Apply linear velocity to a positional quantity
    ///
    /// ### Parameters:
    ///
    /// - `linear`: Positional value
    /// - `dt`: Time step
    ///
    /// ### Type parameters:
    ///
    /// - `P`: Positional quantity, usually `Point2` or `Point3`
    pub fn apply_linear<P>(&self, linear: P, dt: L::Scalar) -> P
    where
        P: EuclideanSpace<Scalar = L::Scalar, Diff = L>,
        L::Scalar: BaseFloat,
        L: VectorSpace,
    {
        linear + self.linear * dt
    }

    /// Apply angular velocity to a rotational quantity
    ///
    /// ### Parameters:
    ///
    /// - `rotation`: Rotational value
    /// - `dt`: Time step
    ///
    /// ### Type parameters:
    ///
    /// - `R`: Rotational quantity, usually `Basis2` or `Quaternion`
    pub fn apply_angular<R>(&self, rotation: R, dt: L::Scalar) -> R
    where
        R: ApplyAngular<L::Scalar, A>,
        L: VectorSpace,
        L::Scalar: BaseFloat,
    {
        rotation.apply(&self.angular, dt)
    }
}

/// Apply an angular velocity to a rotational quantity
///
/// ### Type parameters:
///
/// - `A`: Angular velocity, usually `Scalar` or `Vector3`
pub trait ApplyAngular<S, A> {
    /// Apply given velocity
    fn apply(&self, velocity: &A, dt: S) -> Self;
}

impl<S> ApplyAngular<S, S> for S
where
    S: BaseFloat,
{
    fn apply(&self, velocity: &S, dt: S) -> Self {
        *self + *velocity * dt
    }
}

impl<S> ApplyAngular<S, S> for Basis2<S>
where
    S: BaseFloat,
{
    fn apply(&self, velocity: &S, dt: S) -> Self {
        *self * Basis2::from_angle(Rad(*velocity * dt))
    }
}

impl<S> ApplyAngular<S, Vector3<S>> for Quaternion<S>
where
    S: BaseFloat,
{
    fn apply(&self, velocity: &Vector3<S>, dt: S) -> Self {
        self * Quaternion::from(Euler {
            x: Rad(velocity.x * dt),
            y: Rad(velocity.y * dt),
            z: Rad(velocity.z * dt),
        })
    }
}

#[cfg(test)]
mod tests_f32 {
    use cgmath::{Basis2, Point2, Point3, Rad, Rotation2, Rotation3, Transform, Vector2};

    use super::*;
    use physics2d::BodyPose2;
    use physics3d::BodyPose3;

    #[test]
    fn test_velocity_linear() {
        let velocity = Velocity::new(Vector2::new(1., 1.), 0.);
        let pose = Point2::<f32>::new(0., 0.);
        let pose = velocity.apply_linear(pose, 0.1);
        assert_eq!(Point2::new(0.1, 0.1), pose);
    }

    #[test]
    fn test_velocity_2d_angular() {
        let velocity = Velocity::new(Vector2::new(1., 1.), 1.);
        let orientation = Basis2::<f32>::from_angle(Rad(0.));
        let orientation = velocity.apply_angular(orientation, 0.1);
        assert_eq!(Basis2::from_angle(Rad(0.1)), orientation);
    }

    #[test]
    fn test_velocity_3d_angular() {
        let velocity = Velocity::new(Vector3::new(1., 1., 1.), Vector3::new(0., 1., 0.));

        let orientation = Quaternion::<f32>::from_angle_y(Rad(0.));
        let orientation = velocity.apply_angular(orientation, 0.1);
        assert_eq!(Quaternion::from_angle_y(Rad(0.1)), orientation);
    }

    #[test]
    fn test_velocity_full_2d() {
        let velocity = Velocity::new(Vector2::new(1., 1.), 1.);
        let pose = BodyPose2::<f32>::one();
        let pose = velocity.apply(&pose, 0.1);
        assert_eq!(Point2::new(0.1, 0.1), pose.position());
        assert_eq!(Basis2::from_angle(Rad(0.1)), pose.rotation());
    }

    #[test]
    fn test_velocity_full_3d() {
        let velocity = Velocity::new(Vector3::new(1., 1., 1.), Vector3::new(0., 1., 0.));
        let pose = BodyPose3::<f32>::one();
        let pose = velocity.apply(&pose, 0.1);
        assert_eq!(Point3::new(0.1, 0.1, 0.1), pose.position());
        assert_eq!(Quaternion::from_angle_y(Rad(0.1)), pose.rotation());
    }

    #[test]
    fn test_apply_angular_basis2() {
        let orientation = Basis2::<f32>::from_angle(Rad(0.));
        let velocity = 0.5;
        let orientation = orientation.apply(&velocity, 0.1);
        let orientation = orientation.apply(&velocity, 0.1);
        let orientation = orientation.apply(&velocity, 0.1);
        let orientation = orientation.apply(&velocity, 0.1);

        assert_ulps_eq!(Basis2::from_angle(Rad(0.2)), orientation);
    }

    #[test]
    fn test_apply_angular_real() {
        let orientation: f32 = 0.;
        let velocity = 0.5;
        let orientation = orientation.apply(&velocity, 0.1);
        let orientation = orientation.apply(&velocity, 0.1);
        let orientation = orientation.apply(&velocity, 0.1);
        let orientation = orientation.apply(&velocity, 0.1);

        assert_eq!(0.2, orientation);
    }

    #[test]
    fn test_apply_angular_quat() {
        let orientation = Quaternion::<f32>::from_angle_x(Rad(0.));
        let velocity = Vector3::new(0.5, 0., 0.);
        let orientation = orientation.apply(&velocity, 0.1);
        let orientation = orientation.apply(&velocity, 0.1);
        let orientation = orientation.apply(&velocity, 0.1);
        let orientation = orientation.apply(&velocity, 0.1);

        assert_ulps_eq!(Quaternion::from_angle_x(Rad(0.2)), orientation);
    }
}

#[cfg(test)]
mod tests_f64 {
    use cgmath::{Basis2, Point2, Point3, Rad, Rotation2, Rotation3, Transform, Vector2};

    use super::*;
    use physics2d::BodyPose2;
    use physics3d::BodyPose3;

    #[test]
    fn test_velocity_linear() {
        let velocity = Velocity::new(Vector2::new(1., 1.), 0.);
        let pose = Point2::<f64>::new(0., 0.);
        let pose = velocity.apply_linear(pose, 0.1);
        assert_eq!(Point2::new(0.1, 0.1), pose);
    }

    #[test]
    fn test_velocity_2d_angular() {
        let velocity = Velocity::new(Vector2::new(1., 1.), 1.);
        let orientation = Basis2::<f64>::from_angle(Rad(0.));
        let orientation = velocity.apply_angular(orientation, 0.1);
        assert_eq!(Basis2::from_angle(Rad(0.1)), orientation);
    }

    #[test]
    fn test_velocity_3d_angular() {
        let velocity = Velocity::new(Vector3::new(1., 1., 1.), Vector3::new(0., 1., 0.));

        let orientation = Quaternion::<f64>::from_angle_y(Rad(0.));
        let orientation = velocity.apply_angular(orientation, 0.1);
        assert_eq!(Quaternion::from_angle_y(Rad(0.1)), orientation);
    }

    #[test]
    fn test_velocity_full_2d() {
        let velocity = Velocity::new(Vector2::new(1., 1.), 1.);
        let pose = BodyPose2::<f64>::one();
        let pose = velocity.apply(&pose, 0.1);
        assert_eq!(Point2::new(0.1, 0.1), pose.position());
        assert_eq!(Basis2::from_angle(Rad(0.1)), pose.rotation());
    }

    #[test]
    fn test_velocity_full_3d() {
        let velocity = Velocity::new(Vector3::new(1., 1., 1.), Vector3::new(0., 1., 0.));
        let pose = BodyPose3::<f64>::one();
        let pose = velocity.apply(&pose, 0.1);
        assert_eq!(Point3::new(0.1, 0.1, 0.1), pose.position());
        assert_eq!(Quaternion::from_angle_y(Rad(0.1)), pose.rotation());
    }

    #[test]
    fn test_apply_angular_basis2() {
        let orientation = Basis2::<f64>::from_angle(Rad(0.));
        let velocity = 0.5;
        let orientation = orientation.apply(&velocity, 0.1);
        let orientation = orientation.apply(&velocity, 0.1);
        let orientation = orientation.apply(&velocity, 0.1);
        let orientation = orientation.apply(&velocity, 0.1);

        assert_ulps_eq!(Basis2::from_angle(Rad(0.2)), orientation);
    }

    #[test]
    fn test_apply_angular_real() {
        let orientation: f64 = 0.;
        let velocity = 0.5;
        let orientation = orientation.apply(&velocity, 0.1);
        let orientation = orientation.apply(&velocity, 0.1);
        let orientation = orientation.apply(&velocity, 0.1);
        let orientation = orientation.apply(&velocity, 0.1);

        assert_eq!(0.2, orientation);
    }

    #[test]
    fn test_apply_angular_quat() {
        let orientation = Quaternion::<f64>::from_angle_x(Rad(0.));
        let velocity = Vector3::new(0.5, 0., 0.);
        let orientation = orientation.apply(&velocity, 0.1);
        let orientation = orientation.apply(&velocity, 0.1);
        let orientation = orientation.apply(&velocity, 0.1);
        let orientation = orientation.apply(&velocity, 0.1);

        assert_ulps_eq!(Quaternion::from_angle_x(Rad(0.2)), orientation);
    }
}