phys-collision 2.0.1-beta.0

Provides collision detection ability
// Copyright (C) 2020-2025 phys-collision authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#[cfg(test)]
#[cfg(feature = "serde")]
mod tests {
    use approx_det::assert_relative_eq;
    use wasm_bindgen_test::*;

    use crate::collision_tasks::tests::common::{
        convex2contact_manifold_wide_to_convex2contact_manifold, get_input_wide,
        Convex2ContactManifold, Mvec3, TestInput,
    };
    use crate::collision_tasks::ShapeWideTester;
    use crate::shapes::{CylinderWide, InfinitePlaneWide};
    use crate::traits::PairWideTest;
    use crate::{Cylinder, InfinitePlane};

    type InputType = TestInput<Cylinder, InfinitePlane>;

    wasm_bindgen_test_configure!(run_in_browser);

    #[test]
    #[cfg(feature = "serde")]
    #[wasm_bindgen_test]
    fn test_cylinder_infinite_plane_perpendicular_contact() {
        let _ = env_logger::builder().is_test(true).try_init();

        let file_input =
            include_bytes!("resource/cylinder_infinite_plane/000_perpendicular_contact.json");

        let input0: InputType =
            serde_json::from_slice(file_input).expect("file should be proper JSON");

        let output0 = Convex2ContactManifold {
            OffsetA0: Mvec3 {
                x: 0.0,
                y: -1.0,
                z: 0.0,
            },
            OffsetA1: Mvec3 {
                x: 0.0,
                y: -1.0,
                z: 0.0,
            },
            Normal: Mvec3 {
                x: 0.0,
                y: 1.0,
                z: 0.0,
            },

            Depth0: f32::MIN,
            Depth1: 0.0,
            FeatureId0: 0,
            FeatureId1: 1,
            Contact0Exists: false,
            Contact1Exists: true,
        };

        let array = [input0];
        let outputs = [output0];
        let pair_count = array.len();

        TestWide!(
            CylinderWide,
            InfinitePlaneWide,
            array,
            pair_count,
            outputs,
            convex2contact_manifold_wide_to_convex2contact_manifold
        );
    }

    #[test]
    #[cfg(feature = "serde")]
    #[wasm_bindgen_test]
    fn test_cylinder_infinite_plane_perpendicular_contact_rotate_z_180() {
        let _ = env_logger::builder().is_test(true).try_init();

        let file_input = include_bytes!(
            "resource/cylinder_infinite_plane/001_perpendicular_contact_rotate_z_180.json"
        );

        let input0: InputType =
            serde_json::from_slice(file_input).expect("file should be proper JSON");

        let output0 = Convex2ContactManifold {
            OffsetA0: Mvec3 {
                x: 0.0,
                y: -1.0,
                z: 0.0,
            },
            OffsetA1: Mvec3 {
                x: 0.0,
                y: -1.0,
                z: 0.0,
            },
            Normal: Mvec3 {
                x: 0.0,
                y: 1.0,
                z: 0.0,
            },

            Depth0: 0.0,
            Depth1: f32::MIN,
            FeatureId0: 0,
            FeatureId1: 1,
            Contact0Exists: true,
            Contact1Exists: false,
        };

        let array = [input0];
        let outputs = [output0];
        let pair_count = array.len();

        TestWide!(
            CylinderWide,
            InfinitePlaneWide,
            array,
            pair_count,
            outputs,
            convex2contact_manifold_wide_to_convex2contact_manifold
        );
    }

    #[test]
    #[cfg(feature = "serde")]
    #[wasm_bindgen_test]
    fn test_cylinder_infinite_plane_2contacts_rotate_z_90() {
        let _ = env_logger::builder().is_test(true).try_init();

        let file_input =
            include_bytes!("resource/cylinder_infinite_plane/002_2contacts_rotate_z_90.json");

        let input0: InputType =
            serde_json::from_slice(file_input).expect("file should be proper JSON");

        let output0 = Convex2ContactManifold {
            OffsetA0: Mvec3 {
                x: -1.0,
                y: -0.5,
                z: 0.0,
            },
            OffsetA1: Mvec3 {
                x: 1.0,
                y: -0.5,
                z: 0.0,
            },
            Normal: Mvec3 {
                x: 0.0,
                y: 1.0,
                z: 0.0,
            },

            Depth0: 0.5,
            Depth1: 0.5,
            FeatureId0: 0,
            FeatureId1: 1,
            Contact0Exists: true,
            Contact1Exists: true,
        };

        let array = [input0];
        let outputs = [output0];
        let pair_count = array.len();

        TestWide!(
            CylinderWide,
            InfinitePlaneWide,
            array,
            pair_count,
            outputs,
            convex2contact_manifold_wide_to_convex2contact_manifold
        );
    }

    #[test]
    #[cfg(feature = "serde")]
    #[wasm_bindgen_test]
    fn test_cylinder_infinite_plane_1contact_rotate_z_45() {
        let _ = env_logger::builder().is_test(true).try_init();

        let file_input =
            include_bytes!("resource/cylinder_infinite_plane/003_1contact_rotate_z_45.json");

        let input0: InputType =
            serde_json::from_slice(file_input).expect("file should be proper JSON");

        let output0 = Convex2ContactManifold {
            OffsetA0: Mvec3 {
                x: 0.0,
                y: -std::f32::consts::FRAC_1_SQRT_2,
                z: 0.0,
            },
            OffsetA1: Mvec3 {
                x: 0.353553,
                y: -1.0606601,
                z: 0.0,
            },
            Normal: Mvec3 {
                x: 0.0,
                y: 1.0,
                z: 0.0,
            },

            Depth0: f32::MIN,
            Depth1: 0.353553,
            FeatureId0: 0,
            FeatureId1: 1,
            Contact0Exists: false,
            Contact1Exists: true,
        };

        let array = [input0];
        let outputs = [output0];
        let pair_count = array.len();

        TestWide!(
            CylinderWide,
            InfinitePlaneWide,
            array,
            pair_count,
            outputs,
            convex2contact_manifold_wide_to_convex2contact_manifold
        );
    }
}