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::{Cylinder, CylinderWide, PairWideTest, ShapeWideTester};
wasm_bindgen_test_configure!(run_in_browser);
type InputType = TestInput<Triangle, Cylinder>;

#[test]
#[wasm_bindgen_test]
#[cfg(feature = "serde")]
fn test_collide_on_side_edge() {
    let _ = env_logger::builder().is_test(true).try_init();
    let file_input = include_bytes!("resource/cylinder_triangle/collide_on_side_egde.json");
    let input0: InputType = serde_json::from_slice(file_input).expect("file should be proper JSON");
    let output0 = Convex4ContactManifold {
        Normal: Mvec3 {
            x: -0.77739346,
            y: 3.545_208_6E-8,
            z: 0.6290146,
        },
        OffsetA0: Mvec3 {
            x: -0.37,
            y: -0.5,
            z: 0.3,
        },
        OffsetA1: Mvec3 {
            x: -0.37,
            y: 0.5,
            z: 0.3,
        },
        OffsetA2: Mvec3 {
            x: 0.0,
            y: 0.0,
            z: 0.0,
        },
        OffsetA3: Mvec3 {
            x: 0.0,
            y: 0.0,
            z: 0.0,
        },
        Depth0: 0.023659606,
        Depth1: 0.023659606,
        Depth2: 0.0,
        Depth3: 0.0,
        Contact0Exists: true,
        Contact1Exists: true,
        Contact2Exists: false,
        Contact3Exists: false,
        FeatureId0: 0,
        FeatureId1: 1,
        FeatureId2: 0,
        FeatureId3: 0,
    };
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();

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

#[test]
#[wasm_bindgen_test]
#[cfg(feature = "serde")]
fn test_collide_side_face_with_tootbird() {
    let _ = env_logger::builder().is_test(true).try_init();
    let file_input =
        include_bytes!("resource/cylinder_triangle/collide_side_face_with_tootbird.json");
    let input0: InputType = serde_json::from_slice(file_input).expect("file should be proper JSON");
    let output0 = Convex4ContactManifold {
        Normal: Mvec3 {
            x: -1.0,
            y: -0.0,
            z: -0.0,
        },
        OffsetA0: Mvec3 {
            x: 3.0,
            y: 0.0,
            z: 1.5,
        },
        OffsetA1: Mvec3 {
            x: 1.4999495,
            y: 0.42501435,
            z: 1.4999493,
        },
        OffsetA2: Mvec3 {
            x: 3.446,
            y: 0.1,
            z: 1.5,
        },
        OffsetA3: Mvec3 {
            x: 3.446,
            y: 0.1,
            z: 1.5,
        },
        Depth0: 0.0539999,
        Depth1: -1.4460506,
        Depth2: 0.0,
        Depth3: 0.0,
        Contact0Exists: true,
        Contact1Exists: false,
        Contact2Exists: false,
        Contact3Exists: false,
        FeatureId0: 0,
        FeatureId1: 1,
        FeatureId2: 0,
        FeatureId3: 0,
    };
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();

    TestWide!(
        TriangleWide,
        CylinderWide,
        array,
        pair_count,
        outputs,
        convex4manifold_wide2convex4contact_manifold
    );
}
#[test]
#[wasm_bindgen_test]
#[cfg(feature = "serde")]
fn test_collide_side_face() {
    let _ = env_logger::builder().is_test(true).try_init();
    let file_input = include_bytes!("resource/cylinder_triangle/collide_on_side_face.json");
    let input0: InputType = serde_json::from_slice(file_input).expect("file should be proper JSON");
    let output0 = Convex4ContactManifold {
        Normal: Mvec3 {
            x: -0.76822126,
            y: -0.6401844,
            z: -0.0,
        },
        OffsetA0: Mvec3 {
            x: 2.8442624,
            y: 0.18688527,
            z: 1.5,
        },
        OffsetA1: Mvec3 {
            x: 2.352459,
            y: 0.7770492,
            z: 1.5,
        },
        OffsetA2: Mvec3 {
            x: 3.3,
            y: 0.65,
            z: 1.5,
        },
        OffsetA3: Mvec3 {
            x: 3.3,
            y: 0.65,
            z: 1.5,
        },
        Depth0: 0.057616666,
        Depth1: -0.58256775,
        Depth2: 0.0,
        Depth3: 0.0,
        Contact0Exists: true,
        Contact1Exists: false,
        Contact2Exists: false,
        Contact3Exists: false,
        FeatureId0: 0,
        FeatureId1: 1,
        FeatureId2: 0,
        FeatureId3: 0,
    };
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();

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

#[test]
#[wasm_bindgen_test]
#[cfg(feature = "serde")]
fn test_collide_on_cap_face() {
    let _ = env_logger::builder().is_test(true).try_init();
    let file_input = include_bytes!("resource/cylinder_triangle/collide_on_cap_face.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: 2.3169951,
            y: 0.0,
            z: 1.0,
        },
        OffsetA1: Mvec3 {
            x: 2.0,
            y: 0.0,
            z: 1.0,
        },
        OffsetA2: Mvec3 {
            x: 2.0,
            y: 0.0,
            z: 1.0,
        },
        OffsetA3: Mvec3 {
            x: 2.0,
            y: 0.0,
            z: 1.0,
        },
        Depth0: 0.18301296,
        Depth1: 0.0,
        Depth2: 0.0,
        Depth3: 0.0,
        Contact0Exists: true,
        Contact1Exists: false,
        Contact2Exists: false,
        Contact3Exists: false,
        FeatureId0: 0,
        FeatureId1: 0,
        FeatureId2: 0,
        FeatureId3: 0,
    };
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();

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

#[test]
#[wasm_bindgen_test]
#[cfg(feature = "serde")]
fn test_collide_on_cap_edge() {
    let _ = env_logger::builder().is_test(true).try_init();
    let file_input = include_bytes!("resource/cylinder_triangle/collide_on_cap_edge.json");
    let input0: InputType = serde_json::from_slice(file_input).expect("file should be proper JSON");
    let output0 = Convex4ContactManifold {
        Normal: Mvec3 {
            x: -0.98168474,
            y: -0.1904736,
            z: -0.0,
        },
        OffsetA0: Mvec3 {
            x: 2.9999557,
            y: -0.0,
            z: 1.2970796,
        },
        OffsetA1: Mvec3 {
            x: 2.9999557,
            y: 0.0,
            z: 0.30292064,
        },
        OffsetA2: Mvec3 {
            x: 1.9999707,
            y: -0.0,
            z: 1.0,
        },
        OffsetA3: Mvec3 {
            x: 1.9999707,
            y: 0.0,
            z: 1.0,
        },
        Depth0: 0.027633965,
        Depth1: 0.027633965,
        Depth2: 0.0,
        Depth3: 0.0,
        Contact0Exists: true,
        Contact1Exists: true,
        Contact2Exists: false,
        Contact3Exists: false,
        FeatureId0: 5,
        FeatureId1: 1,
        FeatureId2: 0,
        FeatureId3: 9,
    };
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();

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

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

    let file_input = include_bytes!("resource/cylinder_triangle/no_collide_back_face_cull.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();

    let reset = <ShapeWideTester as PairWideTest<TriangleWide, CylinderWide>>::should_reset_manifold_before_test();
    assert!(reset);

    TestWide!(
        TriangleWide,
        CylinderWide,
        array,
        pair_count,
        outputs,
        convex4manifold_wide2convex4contact_manifold
    );
}
#[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/cylinder_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();

    let reset = <ShapeWideTester as PairWideTest<TriangleWide, CylinderWide>>::should_reset_manifold_before_test();
    assert!(reset);

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