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 common::get_input_wide;
use wasm_bindgen_test::*;

use crate::collision_tasks::tests::common;
use crate::collision_tasks::tests::common::{
    convex4manifold_wide2convex4contact_manifold, Convex4ContactManifold, TestInput,
};
use crate::collision_tasks::ShapeWideTester;
use crate::shapes::CuboidWide;
use crate::traits::PairWideTest;
use crate::Cuboid;

type Input = TestInput<Cuboid, Cuboid>;

wasm_bindgen_test_configure!(run_in_browser);

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

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

    let input0: Input = serde_json::from_slice(file_input).expect("file should be proper JSON");
    let mut output0: Convex4ContactManifold =
        serde_json::from_slice(file_output).expect("file should be proper JSON");
    output0.OffsetA0 -= output0.Normal * output0.Depth0;
    output0.OffsetA1 -= output0.Normal * output0.Depth1;
    output0.OffsetA2 -= output0.Normal * output0.Depth2;
    output0.OffsetA3 -= output0.Normal * output0.Depth3;
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();
    TestWide!(
        CuboidWide,
        CuboidWide,
        array,
        pair_count,
        outputs,
        convex4manifold_wide2convex4contact_manifold
    );
}

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

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

    let input0: Input = serde_json::from_slice(file_input).expect("file should be proper JSON");
    let mut output0: Convex4ContactManifold =
        serde_json::from_slice(file_output).expect("file should be proper JSON");
    output0.OffsetA0 -= output0.Normal * output0.Depth0;
    output0.OffsetA1 -= output0.Normal * output0.Depth1;
    output0.OffsetA2 -= output0.Normal * output0.Depth2;
    output0.OffsetA3 -= output0.Normal * output0.Depth3;
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();
    TestWide!(
        CuboidWide,
        CuboidWide,
        array,
        pair_count,
        outputs,
        convex4manifold_wide2convex4contact_manifold
    );
}

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

    let file_input = include_bytes!("resource/cuboid_cuboid/input2.json");
    let file_output = include_bytes!("resource/cuboid_cuboid/output2.json");

    let input0: Input = serde_json::from_slice(file_input).expect("file should be proper JSON");
    let mut output0: Convex4ContactManifold =
        serde_json::from_slice(file_output).expect("file should be proper JSON");
    output0.OffsetA0 -= output0.Normal * output0.Depth0;
    output0.OffsetA1 -= output0.Normal * output0.Depth1;
    output0.OffsetA2 -= output0.Normal * output0.Depth2;
    output0.OffsetA3 -= output0.Normal * output0.Depth3;
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();
    TestWide!(
        CuboidWide,
        CuboidWide,
        array,
        pair_count,
        outputs,
        convex4manifold_wide2convex4contact_manifold
    );
}

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

    let file_input = include_bytes!("resource/cuboid_cuboid/input3_face_on_face.json");
    let file_output = include_bytes!("resource/cuboid_cuboid/output3.json");

    let input0: Input = serde_json::from_slice(file_input).expect("file should be proper JSON");
    let mut output0: Convex4ContactManifold =
        serde_json::from_slice(file_output).expect("file should be proper JSON");
    output0.OffsetA0 -= output0.Normal * output0.Depth0;
    output0.OffsetA1 -= output0.Normal * output0.Depth1;
    output0.OffsetA2 -= output0.Normal * output0.Depth2;
    output0.OffsetA3 -= output0.Normal * output0.Depth3;
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();
    TestWide!(
        CuboidWide,
        CuboidWide,
        array,
        pair_count,
        outputs,
        convex4manifold_wide2convex4contact_manifold
    );
}

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

    let file_input = include_bytes!("resource/cuboid_cuboid/input4_face_on_face.json");
    let file_output = include_bytes!("resource/cuboid_cuboid/output4.json");
    let input0: Input = serde_json::from_slice(file_input).expect("file should be proper JSON");
    let mut output0: Convex4ContactManifold =
        serde_json::from_slice(file_output).expect("file should be proper JSON");
    output0.OffsetA0 -= output0.Normal * output0.Depth0;
    output0.OffsetA1 -= output0.Normal * output0.Depth1;
    output0.OffsetA2 -= output0.Normal * output0.Depth2;
    output0.OffsetA3 -= output0.Normal * output0.Depth3;
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();
    TestWide!(
        CuboidWide,
        CuboidWide,
        array,
        pair_count,
        outputs,
        convex4manifold_wide2convex4contact_manifold
    );
}

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

    let file_input = include_bytes!("resource/cuboid_cuboid/input8_face_on_face.json");
    let file_output = include_bytes!("resource/cuboid_cuboid/output8.json");

    let input0: Input = serde_json::from_slice(file_input).expect("file should be proper JSON");
    let mut output0: Convex4ContactManifold =
        serde_json::from_slice(file_output).expect("file should be proper JSON");
    output0.OffsetA0 -= output0.Normal * output0.Depth0;
    output0.OffsetA1 -= output0.Normal * output0.Depth1;
    output0.OffsetA2 -= output0.Normal * output0.Depth2;
    output0.OffsetA3 -= output0.Normal * output0.Depth3;
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();
    TestWide!(
        CuboidWide,
        CuboidWide,
        array,
        pair_count,
        outputs,
        convex4manifold_wide2convex4contact_manifold
    );
}

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

    let file_input = include_bytes!("resource/cuboid_cuboid/input9_face_on_face.json");
    let file_output = include_bytes!("resource/cuboid_cuboid/output9.json");
    let input0: Input = serde_json::from_slice(file_input).expect("file should be proper JSON");
    let mut output0: Convex4ContactManifold =
        serde_json::from_slice(file_output).expect("file should be proper JSON");
    output0.OffsetA0 -= output0.Normal * output0.Depth0;
    output0.OffsetA1 -= output0.Normal * output0.Depth1;
    output0.OffsetA2 -= output0.Normal * output0.Depth2;
    output0.OffsetA3 -= output0.Normal * output0.Depth3;
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();
    TestWide!(
        CuboidWide,
        CuboidWide,
        array,
        pair_count,
        outputs,
        convex4manifold_wide2convex4contact_manifold
    );
}

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

    let file_input = include_bytes!("resource/cuboid_cuboid/input8_face_on_face.json");
    let file_output = include_bytes!("resource/cuboid_cuboid/output8.json");
    let file_input2 = include_bytes!("resource/cuboid_cuboid/input9_face_on_face.json");
    let file_output2 = include_bytes!("resource/cuboid_cuboid/output9.json");

    let input0: Input = serde_json::from_slice(file_input).expect("file should be proper JSON");
    let input1: Input = serde_json::from_slice(file_input2).expect("file should be proper JSON");
    let mut output0: Convex4ContactManifold =
        serde_json::from_slice(file_output).expect("file should be proper JSON");
    output0.OffsetA0 -= output0.Normal * output0.Depth0;
    output0.OffsetA1 -= output0.Normal * output0.Depth1;
    output0.OffsetA2 -= output0.Normal * output0.Depth2;
    output0.OffsetA3 -= output0.Normal * output0.Depth3;
    let mut output1: Convex4ContactManifold =
        serde_json::from_slice(file_output2).expect("file should be proper JSON");
    output1.OffsetA0 -= output1.Normal * output1.Depth0;
    output1.OffsetA1 -= output1.Normal * output1.Depth1;
    output1.OffsetA2 -= output1.Normal * output1.Depth2;
    output1.OffsetA3 -= output1.Normal * output1.Depth3;
    let array = [input0, input1];
    let outputs = [output0, output1];
    let pair_count = array.len();
    TestWide!(
        CuboidWide,
        CuboidWide,
        array,
        pair_count,
        outputs,
        convex4manifold_wide2convex4contact_manifold
    );
}

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

    let file_input = include_bytes!("resource/cuboid_cuboid/input5_face_on_vertice.json");
    let file_output = include_bytes!("resource/cuboid_cuboid/output5.json");
    let input0: Input = serde_json::from_slice(file_input).expect("file should be proper JSON");
    let mut output0: Convex4ContactManifold =
        serde_json::from_slice(file_output).expect("file should be proper JSON");
    output0.OffsetA0 -= output0.Normal * output0.Depth0;
    output0.OffsetA1 -= output0.Normal * output0.Depth1;
    output0.OffsetA2 -= output0.Normal * output0.Depth2;
    output0.OffsetA3 -= output0.Normal * output0.Depth3;
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();
    TestWide!(
        CuboidWide,
        CuboidWide,
        array,
        pair_count,
        outputs,
        convex4manifold_wide2convex4contact_manifold
    );
}

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

    let file_input = include_bytes!("resource/cuboid_cuboid/input6_vertice_on_vertice.json");
    let file_output = include_bytes!("resource/cuboid_cuboid/output6.json");
    let input0: Input = serde_json::from_slice(file_input).expect("file should be proper JSON");
    let mut output0: Convex4ContactManifold =
        serde_json::from_slice(file_output).expect("file should be proper JSON");
    output0.OffsetA0 -= output0.Normal * output0.Depth0;
    output0.OffsetA1 -= output0.Normal * output0.Depth1;
    output0.OffsetA2 -= output0.Normal * output0.Depth2;
    output0.OffsetA3 -= output0.Normal * output0.Depth3;
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();
    TestWide!(
        CuboidWide,
        CuboidWide,
        array,
        pair_count,
        outputs,
        convex4manifold_wide2convex4contact_manifold
    );
}

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

    let file_input = include_bytes!("resource/cuboid_cuboid/input7_line_on_line.json");
    let file_output = include_bytes!("resource/cuboid_cuboid/output7.json");

    let input0: Input = serde_json::from_slice(file_input).expect("file should be proper JSON");
    let mut output0: Convex4ContactManifold =
        serde_json::from_slice(file_output).expect("file should be proper JSON");
    output0.OffsetA0 -= output0.Normal * output0.Depth0;
    output0.OffsetA1 -= output0.Normal * output0.Depth1;
    output0.OffsetA2 -= output0.Normal * output0.Depth2;
    output0.OffsetA3 -= output0.Normal * output0.Depth3;
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();
    TestWide!(
        CuboidWide,
        CuboidWide,
        array,
        pair_count,
        outputs,
        convex4manifold_wide2convex4contact_manifold
    );
}