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::{
    convex2contact_manifold_wide_to_convex2contact_manifold, get_input_wide,
    Convex2ContactManifold, TestInput,
};
use crate::{
    Capsule, CapsuleWide, Convex4ContactManifoldWide, PairWideTest, ShapeWideTester, Triangle,
    TriangleWide,
};

type CapsuleTriangleInput = TestInput<Capsule, Triangle>;

wasm_bindgen_test_configure!(run_in_browser);

#[test]
#[wasm_bindgen_test]
fn test_collision_capsule_triangle_0() {
    let _ = env_logger::builder().is_test(true).try_init();

    let file_input = include_bytes!("resource/capsule_triangle/input0.json");
    let file_output = include_bytes!("resource/capsule_triangle/output0.json");

    let input0: CapsuleTriangleInput =
        serde_json::from_slice(file_input).expect("file should be proper JSON");
    let output0: Convex2ContactManifold =
        serde_json::from_slice(file_output).expect("file should be proper JSON");
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();

    TestWide!(
        CapsuleWide,
        TriangleWide,
        array,
        pair_count,
        outputs,
        convex2contact_manifold_wide_to_convex2contact_manifold
    );
}

#[test]
#[wasm_bindgen_test]
fn test_collision_capsule_triangle_1() {
    let _ = env_logger::builder().is_test(true).try_init();

    let file_input = include_bytes!("resource/capsule_triangle/input1.json");
    let file_output = include_bytes!("resource/capsule_triangle/output1.json");

    let input0: CapsuleTriangleInput =
        serde_json::from_slice(file_input).expect("file should be proper JSON");
    let output0: Convex2ContactManifold =
        serde_json::from_slice(file_output).expect("file should be proper JSON");
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();

    TestWide!(
        CapsuleWide,
        TriangleWide,
        array,
        pair_count,
        outputs,
        convex2contact_manifold_wide_to_convex2contact_manifold
    );
}

/// Dummy data should early exit, otherwise it will affect performance
#[test]
#[wasm_bindgen_test]
fn test_collision_capsule_triangle_early_exit() {
    let _ = env_logger::builder().is_test(true).try_init();

    let file_input = include_bytes!("resource/capsule_triangle/input_early_exit.json");

    let input0: CapsuleTriangleInput =
        serde_json::from_slice(file_input).expect("file should be proper JSON");
    let array = [input0];
    let pair_count = array.len();

    let input_wide = get_input_wide::<CapsuleWide, TriangleWide>(&array[..]);
    let mut res = Convex4ContactManifoldWide::default();

    let contact_context = crate::traits::ContactContext {
        orientation_a: &input_wide.orientation_a,
        orientation_b: &input_wide.orientation_b,
        offset_b: &input_wide.offset_b,
        speculative_margin: input_wide.speculative_margin,
        pair_count,
        complex_shape_container: None,
    };
    ShapeWideTester::test(&input_wide.a, &input_wide.b, &contact_context, &mut res);
    assert_eq!(res, Convex4ContactManifoldWide::default());
}

#[test]
#[wasm_bindgen_test]
fn test_collision_capsule_triangle_solid_face() {
    let _ = env_logger::builder().is_test(true).try_init();

    let file_input = include_bytes!("resource/capsule_triangle/input_solid_face.json");

    let input0: CapsuleTriangleInput =
        serde_json::from_slice(file_input).expect("file should be proper JSON");
    let array = [input0];
    let pair_count = array.len();

    let input_wide = get_input_wide::<CapsuleWide, TriangleWide>(&array[..]);
    let mut res = Convex4ContactManifoldWide::default();
    let contact_context = crate::traits::ContactContext {
        orientation_a: &input_wide.orientation_a,
        orientation_b: &input_wide.orientation_b,
        offset_b: &input_wide.offset_b,
        speculative_margin: input_wide.speculative_margin,
        pair_count,
        complex_shape_container: None,
    };

    ShapeWideTester::test(&input_wide.a, &input_wide.b, &contact_context, &mut res);
    assert_eq!(res, Convex4ContactManifoldWide::default());
}