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 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::{CapsuleWide, InfinitePlaneWide};
use crate::traits::PairWideTest;
use crate::{Capsule, InfinitePlane};
wasm_bindgen_test_configure!(run_in_browser);
type InputType = TestInput<Capsule, InfinitePlane>;

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

    let file_input = include_bytes!("resource/capsule_infinite_plane/000_nocollide.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: 0.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: -5.0,
        Depth1: -4.0,
        FeatureId0: 0,
        FeatureId1: 1,
        Contact0Exists: false,
        Contact1Exists: false,
    };
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();
    TestWide!(
        CapsuleWide,
        InfinitePlaneWide,
        array,
        pair_count,
        outputs,
        convex2contact_manifold_wide_to_convex2contact_manifold
    );
}

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

    let file_input =
        include_bytes!("resource/capsule_infinite_plane/001_capsule_one_hemisphere_tangent.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: -0.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: -1.0,
        Depth1: -0.0,
        FeatureId0: 0,
        FeatureId1: 1,
        Contact0Exists: false,
        Contact1Exists: true,
    };
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();
    TestWide!(
        CapsuleWide,
        InfinitePlaneWide,
        array,
        pair_count,
        outputs,
        convex2contact_manifold_wide_to_convex2contact_manifold
    );
}

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

    let file_input =
        include_bytes!("resource/capsule_infinite_plane/002_capsule_one_hemisphere_penetrate.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: -0.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.8,
        Depth1: 0.2,
        FeatureId0: 0,
        FeatureId1: 1,
        Contact0Exists: false,
        Contact1Exists: true,
    };
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();
    TestWide!(
        CapsuleWide,
        InfinitePlaneWide,
        array,
        pair_count,
        outputs,
        convex2contact_manifold_wide_to_convex2contact_manifold
    );
}

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

    let file_input =
        include_bytes!("resource/capsule_infinite_plane/003_capsule_both_hemisphere_tangent.json");

    let input0: InputType = serde_json::from_slice(file_input).expect("file should be proper JSON");
    let output0 = Convex2ContactManifold {
        OffsetA0: Mvec3 {
            x: -0.5,
            y: -0.5,
            z: 0.0,
        },
        OffsetA1: Mvec3 {
            x: 0.5,
            y: -0.5,
            z: 0.0,
        },
        Normal: Mvec3 {
            x: 0.0,
            y: 1.0,
            z: 0.0,
        },

        Depth0: 0.0,
        Depth1: 0.0,
        FeatureId0: 0,
        FeatureId1: 1,
        Contact0Exists: true,
        Contact1Exists: true,
    };
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();
    TestWide!(
        CapsuleWide,
        InfinitePlaneWide,
        array,
        pair_count,
        outputs,
        convex2contact_manifold_wide_to_convex2contact_manifold
    );
}

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

    let file_input = include_bytes!(
        "resource/capsule_infinite_plane/004_capsule_both_hemisphere_penetrate.json"
    );

    let input0: InputType = serde_json::from_slice(file_input).expect("file should be proper JSON");
    let output0 = Convex2ContactManifold {
        OffsetA0: Mvec3 {
            x: -0.5,
            y: -0.5,
            z: 0.0,
        },
        OffsetA1: Mvec3 {
            x: 0.5,
            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!(
        CapsuleWide,
        InfinitePlaneWide,
        array,
        pair_count,
        outputs,
        convex2contact_manifold_wide_to_convex2contact_manifold
    );
}