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.

use approx_det::assert_relative_eq;
use wasm_bindgen_test::*;

use super::common::{
    convex4manifold_wide2convex4contact_manifold, get_input_wide, Convex4ContactManifold, Mvec3,
    TestInput,
};
use crate::shapes::{Triangle, TriangleWide};
use crate::{Cuboid, CuboidWide, PairWideTest, ShapeWideTester};

pub(crate) type InputType = TestInput<Cuboid, Triangle>;

wasm_bindgen_test_configure!(run_in_browser);

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

    let file_input = include_bytes!("resource/cuboid_triangle/no_collide.json");
    let input0: InputType = serde_json::from_slice(file_input).expect("file should be proper JSON");

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

    TestWide!(
        CuboidWide,
        TriangleWide,
        array,
        pair_count,
        outputs,
        convex4manifold_wide2convex4contact_manifold
    );
}

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

    let file_input = include_bytes!("resource/cuboid_triangle/collide_1_contact.json");
    let input0: InputType = serde_json::from_slice(file_input).expect("file should be proper JSON");

    let output0 = Convex4ContactManifold {
        Normal: Mvec3 {
            x: -0.0664,
            y: 0.9972,
            z: -0.0332,
        },
        OffsetA0: Mvec3 {
            x: 0.0237,
            y: -0.6213,
            z: 0.2723,
        },
        OffsetA1: Mvec3 {
            x: 0.1790,
            y: -0.6173,
            z: 0.0830,
        },
        OffsetA2: Mvec3 {
            x: 0.1790,
            y: -0.6173,
            z: 0.0830,
        },
        OffsetA3: Mvec3 {
            x: 0.1790,
            y: -0.6173,
            z: 0.0830,
        },
        Depth0: 0.0303,
        Depth1: 0.0,
        Depth2: 0.0,
        Depth3: 0.0,
        Contact0Exists: true,
        Contact1Exists: false,
        Contact2Exists: false,
        Contact3Exists: false,
        FeatureId0: 2,
        FeatureId1: 0,
        FeatureId2: 0,
        FeatureId3: 0,
    };

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

    TestWide!(
        CuboidWide,
        TriangleWide,
        array,
        pair_count,
        outputs,
        convex4manifold_wide2convex4contact_manifold
    );
}

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

    let file_input = include_bytes!("resource/cuboid_triangle/collide_2_contact.json");
    let input0: InputType = serde_json::from_slice(file_input).expect("file should be proper JSON");

    let output0 = Convex4ContactManifold {
        Normal: Mvec3 {
            x: 0.31456244,
            y: 0.94870996,
            z: -0.03162364,
        },
        OffsetA0: Mvec3 {
            x: -0.1699,
            y: -0.2727,
            z: 0.4972,
        },
        OffsetA1: Mvec3 {
            x: -0.1699,
            y: -0.3060,
            z: -0.5017,
        },
        OffsetA2: Mvec3 {
            x: -1.1699969,
            y: -0.38133225,
            z: -0.759,
        },
        OffsetA3: Mvec3 {
            x: -1.1699969,
            y: -0.38133225,
            z: -0.759,
        },
        Depth0: 0.087611616,
        Depth1: 0.055987984,
        Depth2: 0.0,
        Depth3: 0.0,
        Contact0Exists: true,
        Contact1Exists: true,
        Contact2Exists: false,
        Contact3Exists: false,
        FeatureId0: 13,
        FeatureId1: 5,
        FeatureId2: 0,
        FeatureId3: 0,
    };

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

    TestWide!(
        CuboidWide,
        TriangleWide,
        array,
        pair_count,
        outputs,
        convex4manifold_wide2convex4contact_manifold
    );
}

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

    let file_input = include_bytes!("resource/cuboid_triangle/collide_3_contact.json");
    let input0: InputType = serde_json::from_slice(file_input).expect("file should be proper JSON");

    let output0 = Convex4ContactManifold {
        Normal: Mvec3 {
            x: 0.0,
            y: 1.0,
            z: -0.0,
        },
        OffsetA0: Mvec3 {
            x: -0.3,
            y: -0.324,
            z: 0.17599976,
        },
        OffsetA1: Mvec3 {
            x: -0.0746668,
            y: -0.324,
            z: -0.49999988,
        },
        OffsetA2: Mvec3 {
            x: -0.98333347,
            y: -0.324,
            z: -0.77400005,
        },
        OffsetA3: Mvec3 {
            x: -0.3,
            y: -0.324,
            z: -0.5,
        },
        Depth0: 0.076000005,
        Depth1: 0.076000005,
        Depth2: 0.0,
        Depth3: 0.076000005,
        Contact0Exists: true,
        Contact1Exists: true,
        Contact2Exists: false,
        Contact3Exists: true,
        FeatureId0: 13,
        FeatureId1: 5,
        FeatureId2: 0,
        FeatureId3: 0,
    };

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

    TestWide!(
        CuboidWide,
        TriangleWide,
        array,
        pair_count,
        outputs,
        convex4manifold_wide2convex4contact_manifold
    );
}

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

    let file_input = include_bytes!("resource/cuboid_triangle/collide_4_contact.json");
    let input0: InputType = serde_json::from_slice(file_input).expect("file should be proper JSON");

    let output0 = Convex4ContactManifold {
        Normal: Mvec3 {
            x: -0.1699,
            y: 0.9706,
            z: 0.1699,
        },
        OffsetA0: Mvec3 {
            x: 0.8690,
            y: -0.4220,
            z: 1.0140,
        },
        OffsetA1: Mvec3 {
            x: -0.8197,
            y: -0.4219,
            z: -1.0859,
        },
        OffsetA2: Mvec3 {
            x: 1.2080,
            y: -0.4219,
            z: -0.9989,
        },
        OffsetA3: Mvec3 {
            x: -0.8651,
            y: -0.4219,
            z: -0.7201,
        },
        Depth0: 0.62472,
        Depth1: 0.53631,
        Depth2: 0.20538,
        Depth3: 0.60976,
        FeatureId0: 6,
        FeatureId1: 4,
        FeatureId2: 0,
        FeatureId3: 14,
        Contact0Exists: true,
        Contact1Exists: true,
        Contact2Exists: true,
        Contact3Exists: true,
    };

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

    TestWide!(
        CuboidWide,
        TriangleWide,
        array,
        pair_count,
        outputs,
        convex4manifold_wide2convex4contact_manifold
    );
}