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
#![allow(missing_docs)]

//! Type conversions between bevy and nalgebra (used by rapier's API)
//!
//! Provides the [`IntoBevy`](IntoBevy) and [`IntoRapier`](IntoRapier)
//! with implementations for bevy and rapier types

use bevy::math::prelude::*;

use heron_core::{AxisAngle, CollisionLayers};

use crate::nalgebra::{
    self, Point2, Point3, Quaternion, UnitComplex, UnitQuaternion, Vector2, Vector3,
};
use crate::rapier::geometry::InteractionGroups;
use crate::rapier::math::{Isometry, Translation, Vector};

pub trait IntoBevy<T> {
    #[must_use]
    fn into_bevy(self) -> T;
}

pub trait IntoRapier<T> {
    #[must_use]
    fn into_rapier(self) -> T;
}

impl IntoBevy<Vec3> for Vector2<f32> {
    fn into_bevy(self) -> Vec3 {
        Vec3::new(self.x, self.y, 0.0)
    }
}

impl IntoBevy<Vec3> for Vector3<f32> {
    fn into_bevy(self) -> Vec3 {
        Vec3::new(self.x, self.y, self.z)
    }
}

impl IntoBevy<Vec3> for Translation<f32> {
    fn into_bevy(self) -> Vec3 {
        self.vector.into_bevy()
    }
}

impl IntoBevy<Quat> for UnitComplex<f32> {
    fn into_bevy(self) -> Quat {
        Quat::from_axis_angle(Vec3::Z, self.angle())
    }
}

impl IntoBevy<Quat> for UnitQuaternion<f32> {
    fn into_bevy(self) -> Quat {
        Quat::from_xyzw(self.i, self.j, self.k, self.w)
    }
}

impl IntoBevy<(Vec3, Quat)> for Isometry<f32> {
    fn into_bevy(self) -> (Vec3, Quat) {
        (self.translation.into_bevy(), self.rotation.into_bevy())
    }
}

impl IntoRapier<Vector2<f32>> for Vec2 {
    fn into_rapier(self) -> Vector2<f32> {
        Vector2::new(self.x, self.y)
    }
}

impl IntoRapier<Vector2<f32>> for Vec3 {
    fn into_rapier(self) -> Vector2<f32> {
        self.truncate().into_rapier()
    }
}

impl IntoRapier<Vector3<f32>> for Vec3 {
    fn into_rapier(self) -> Vector3<f32> {
        Vector3::new(self.x, self.y, self.z)
    }
}

impl IntoRapier<Point2<f32>> for Vec2 {
    fn into_rapier(self) -> Point2<f32> {
        Point2 {
            coords: self.into_rapier(),
        }
    }
}

impl IntoRapier<Point2<f32>> for Vec3 {
    fn into_rapier(self) -> Point2<f32> {
        Point2 {
            coords: self.into_rapier(),
        }
    }
}

impl IntoRapier<Point3<f32>> for Vec3 {
    fn into_rapier(self) -> Point3<f32> {
        Point3 {
            coords: self.into_rapier(),
        }
    }
}

impl IntoRapier<Vec<Point2<f32>>> for &[Vec3] {
    fn into_rapier(self) -> Vec<Point2<f32>> {
        self.iter().copied().map(IntoRapier::into_rapier).collect()
    }
}

impl IntoRapier<Vec<Point3<f32>>> for &[Vec3] {
    fn into_rapier(self) -> Vec<Point3<f32>> {
        self.iter().copied().map(IntoRapier::into_rapier).collect()
    }
}

impl IntoBevy<Vec2> for Point2<f32> {
    fn into_bevy(self) -> Vec2 {
        Vec2::new(self.x, self.y)
    }
}

impl IntoBevy<Vec3> for Point3<f32> {
    fn into_bevy(self) -> Vec3 {
        Vec3::new(self.x, self.y, self.z)
    }
}

impl IntoBevy<Vec<Vec2>> for &[Point2<f32>] {
    fn into_bevy(self) -> Vec<Vec2> {
        self.iter().copied().map(IntoBevy::into_bevy).collect()
    }
}

impl IntoRapier<Translation<f32>> for Vec3 {
    fn into_rapier(self) -> Translation<f32> {
        <Vec3 as IntoRapier<Vector<f32>>>::into_rapier(self).into()
    }
}

impl IntoRapier<UnitComplex<f32>> for Quat {
    fn into_rapier(self) -> UnitComplex<f32> {
        let (axis, angle) = self.to_axis_angle();
        nalgebra::UnitComplex::new(if axis.z > 0.0 { angle } else { -angle })
    }
}

impl IntoRapier<UnitQuaternion<f32>> for Quat {
    fn into_rapier(self) -> UnitQuaternion<f32> {
        UnitQuaternion::new_normalize(Quaternion::new(self.w, self.x, self.y, self.z))
    }
}

impl IntoRapier<Isometry<f32>> for (Vec3, Quat) {
    fn into_rapier(self) -> Isometry<f32> {
        Isometry::from_parts(self.0.into_rapier(), self.1.into_rapier())
    }
}

impl IntoRapier<f32> for AxisAngle {
    fn into_rapier(self) -> f32 {
        if self.axis().z > 0.0 {
            self.angle()
        } else {
            -self.angle()
        }
    }
}

impl IntoRapier<Vector3<f32>> for AxisAngle {
    fn into_rapier(self) -> Vector3<f32> {
        Vec3::from(self).into_rapier()
    }
}

impl IntoRapier<InteractionGroups> for CollisionLayers {
    fn into_rapier(self) -> InteractionGroups {
        InteractionGroups::new(self.groups_bits(), self.masks_bits())
    }
}

impl IntoBevy<CollisionLayers> for InteractionGroups {
    fn into_bevy(self) -> CollisionLayers {
        #[allow(clippy::cast_possible_truncation)]
        CollisionLayers::from_bits(self.memberships, self.filter)
    }
}

#[cfg(feature = "2d")]
impl IntoRapier<rapier2d::dynamics::RigidBodyHandle> for crate::RigidBodyHandle {
    #[cfg(not(feature = "3d"))]
    fn into_rapier(self) -> rapier2d::dynamics::RigidBodyHandle {
        self.0
    }
    #[cfg(feature = "3d")]
    fn into_rapier(self) -> rapier2d::dynamics::RigidBodyHandle {
        rapier2d::dynamics::RigidBodyHandle::invalid()
    }
}

#[cfg(feature = "3d")]
impl IntoRapier<rapier3d::dynamics::RigidBodyHandle> for crate::RigidBodyHandle {
    fn into_rapier(self) -> rapier3d::dynamics::RigidBodyHandle {
        self.0
    }
}

#[cfg(feature = "2d")]
impl IntoRapier<rapier2d::geometry::ColliderHandle> for crate::ColliderHandle {
    #[cfg(not(feature = "3d"))]
    fn into_rapier(self) -> rapier2d::geometry::ColliderHandle {
        self.0
    }
    #[cfg(feature = "3d")]
    fn into_rapier(self) -> rapier2d::geometry::ColliderHandle {
        rapier2d::geometry::ColliderHandle::invalid()
    }
}

#[cfg(feature = "3d")]
impl IntoRapier<rapier3d::geometry::ColliderHandle> for crate::ColliderHandle {
    fn into_rapier(self) -> rapier3d::geometry::ColliderHandle {
        self.0
    }
}

#[cfg(test)]
mod tests {
    #[cfg(dim3)]
    use std::f32::consts::PI;

    use bevy::math::{Quat, Vec3};

    use super::*;
    use approx::assert_ulps_eq;

    #[cfg(dim2)]
    mod angle2d {
        use rstest::rstest;

        use super::*;

        #[test]
        fn negative_axis_angle_to_rapier() {
            let result: f32 = AxisAngle::new(Vec3::Z, -1.0).into_rapier();
            assert_eq!(result, -1.0);
        }

        #[rstest(quat,
            case(Quat::from_axis_angle(Vec3::Z, 2.0)),
            case(Quat::from_axis_angle(-Vec3::Z, 2.0)),
            case(Quat::from_axis_angle(Vec3::Z, 0.0)),
        )]
        fn into_rapier_into_bevy_is_identity(quat: Quat) {
            let rapier: UnitComplex<f32> = quat.into_rapier();
            let actual: Quat = rapier.into_bevy();

            assert!((quat.x - actual.x).abs() < 0.0001);
            assert!((quat.y - actual.y).abs() < 0.0001);
            assert!((quat.z - actual.z).abs() < 0.0001);
            assert!((quat.w - actual.w).abs() < 0.0001);
        }
    }

    mod into_isometry {
        use super::*;

        #[test]
        fn set_translation() {
            let translation = Vec3::new(1.0, 2.0, 3.0);
            let result = (translation, Quat::IDENTITY).into_rapier();
            assert_ulps_eq!(translation.x, result.translation.x);
            assert_ulps_eq!(translation.y, result.translation.y);

            #[cfg(dim3)]
            assert_ulps_eq!(translation.z, result.translation.z);
        }

        #[test]
        #[cfg(dim3)]
        fn set_rotation() {
            let angle = PI / 2.0;
            let axis = Vec3::new(1.0, 2.0, 3.0);

            let quat = Quat::from_axis_angle(axis, angle).normalize();
            let result = (Vec3::default(), quat).into_rapier();

            assert_ulps_eq!(result.rotation.i, quat.x);
            assert_ulps_eq!(result.rotation.j, quat.y);
            assert_ulps_eq!(result.rotation.k, quat.z);
            assert_ulps_eq!(result.rotation.w, quat.w);
        }
    }
}