// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2025 lacklustr@protonmail.com https://github.com/eadf

use remesh::prelude::*;
#[test]
fn test_non_manifold_1() -> Result<(), RemeshError> {
    let vertices = vec![
        [0.0, 0.0, 0.0],
        [1.0, 0.0, 0.0],
        [0.5, 0.0, 1.0],
        [0.0, 1.0, 1.0],
        [0.5, 1.0, 0.0],
        [0.0, 0.0, 1.0],
        [0.5, 0.0, 0.0],
    ];
    let indices = vec![
        0, 5, 3, 5, 0, 2, 3, 2, 4, 0, 6, 2, 0, 4, 6, 2, 1, 4, 2, 6, 1, 4, 1, 6, 3, 5, 2, 4, 0, 3,
    ];

    let (result_vertices, result_indices) = IsotropicRemesh::<f64, _>::new(&vertices, &indices)?
        .with_target_edge_length(2.0)?
        .with_split_edges(SplitStrategy::Disabled)?
        .with_default_collapse_multiplier()?
        .without_smooth_weight()?
        .with_flip_edges(FlipStrategy::Disabled)?
        .with_fix_non_manifold()?
        .run(1)?;

    assert_eq!(result_vertices.len(), 6, "result_vertices.len() mismatch");
    assert_eq!(result_indices.len(), 24, "result_indices.len() mismatch");
    Ok(())
}

#[test]
fn test_non_manifold_vertex_1() -> Result<(), RemeshError> {
    let vertices = [
        [-1.0, 0.0, -1.0],
        [1.0, 0.0, -1.0],
        [0.0, 1.0, -1.0],
        [0.0, 0.0, 0.0],
        [-1.0, 0.0, 1.0],
        [1.0, 0.0, 1.0],
        [0.0, 1.0, 1.0],
    ];
    let indices = [
        0, 3, 2, 3, 0, 1, 0, 2, 1, 2, 3, 1, 4, 6, 3, 3, 5, 4, 6, 4, 5, 3, 6, 5,
    ];

    let (result_vertices, result_indices) = IsotropicRemesh::<f64, _>::new(&vertices, &indices)?
        .with_target_edge_length(2.0)?
        .with_split_edges(SplitStrategy::Disabled)?
        .with_default_collapse_multiplier()?
        .without_smooth_weight()?
        .with_flip_edges(FlipStrategy::Disabled)?
        .with_fix_non_manifold()?
        .run(1)?;

    assert_eq!(result_vertices.len(), 8, "result_vertices.len() mismatch");
    assert_eq!(result_indices.len(), 24, "result_indices.len() mismatch");
    Ok(())
}
#[test]
fn test_non_manifold_edge_1() -> Result<(), RemeshError> {
    //******** vertices:6 indices:24
    let vertices = [
        [1.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        [0.0, 1.0, 0.0],
        [0.0, 0.0, -1.0],
        [-1.0, 0.0, 0.0],
        [0.0, -1.0, 0.0],
    ];
    let indices = [
        0, 3, 2, 3, 0, 1, 2, 1, 0, 2, 3, 1, 4, 1, 3, 3, 5, 4, 1, 4, 5, 3, 1, 5,
    ];

    let (result_vertices, result_indices) =
        IsotropicRemesh::<f32, _, true>::new(&vertices, &indices)?
            .with_target_edge_length(0.5)?
            .with_split_edges(SplitStrategy::Disabled)?
            .with_default_collapse_multiplier()?
            .without_smooth_weight()?
            .with_flip_edges(FlipStrategy::Disabled)?
            .with_fix_non_manifold()?
            .with_print_stats(26)?
            .run(1)?;

    assert_eq!(result_vertices.len(), 8, "result_vertices.len() mismatch");
    assert_eq!(result_indices.len(), 24, "result_indices.len() mismatch");
    Ok(())
}

#[test]
fn test_non_manifold_edge_vertex_1() -> Result<(), RemeshError> {
    let vertices = [
        [1.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        [0.0, 1.0, 0.0],
        [0.0, 0.0, -1.0],
        [-1.0, 0.0, 0.0],
        [0.0, -1.0, 0.0],
        [-2.0, -1.0, 0.0],
        [-2.0, 0.06273615, 1.0],
        [-2.0, 1.0, 0.0],
    ];
    let indices = [
        0, 3, 2, 3, 0, 1, 2, 1, 0, 2, 3, 1, 4, 1, 3, 3, 5, 4, 1, 4, 5, 3, 1, 5, 6, 7, 8, 8, 7, 4,
        4, 7, 6, 8, 4, 6,
    ];

    let (result_vertices, result_indices) =
        IsotropicRemesh::<f32, _, true>::new(&vertices, &indices)?
            .with_target_edge_length(0.5)?
            .with_split_edges(SplitStrategy::Disabled)?
            .with_default_collapse_multiplier()?
            .without_smooth_weight()?
            .with_flip_edges(FlipStrategy::Disabled)?
            .with_fix_non_manifold()?
            .with_print_stats(26)?
            .run(1)?;

    assert_eq!(result_vertices.len(), 12, "result_vertices.len() mismatch");
    assert_eq!(result_indices.len(), 36, "result_indices.len() mismatch");
    Ok(())
}

#[test]
fn test_non_manifold_edge_vertex_2() -> Result<(), RemeshError> {
    //******** vertices:12 indices:66
    let vertices = [
        [-0.09564209, 0.36882865, 0.59282345],
        [0.51751983, -0.16481292, 0.4320112],
        [-0.27665937, -0.067007184, 0.6739998],
        [0.32408184, 0.37566555, 0.37200242],
        [0.32408184, 0.09331238, 0.0812469],
        [0.32408184, -0.19744253, -0.21791103],
        [-0.44006443, -0.047445446, 0.4207506],
        [-0.26885033, 0.34844816, 0.20589744],
        [-0.26885033, 0.14883888, 0.20589744],
        [-0.79925287, -0.067007184, 0.3243292],
        [-0.44205856, 0.3280729, -0.16191342],
        [-0.24822032, -0.033623815, -0.38173035],
    ];
    let indices = [
        9, 7, 10, 1, 4, 3, 1, 3, 0, 5, 4, 1, 1, 0, 2, 0, 3, 7, 8, 0, 7, 8, 2, 0, 8, 3, 4, 8, 7, 3,
        6, 2, 8, 6, 8, 9, 9, 8, 7, 10, 7, 8, 10, 8, 11, 11, 8, 4, 11, 4, 5, 5, 1, 6, 6, 11, 5, 6,
        9, 11, 1, 2, 6, 10, 11, 9,
    ];

    let (result_vertices, result_indices) =
        IsotropicRemesh::<f32, _, true>::new(&vertices, &indices)?
            .with_target_edge_length(0.5)?
            .with_split_edges(SplitStrategy::Disabled)?
            .with_default_collapse_multiplier()?
            .without_smooth_weight()?
            .with_flip_edges(FlipStrategy::Disabled)?
            .with_fix_non_manifold()?
            .with_print_stats(26)?
            .run(1)?;

    assert_eq!(result_vertices.len(), 13, "result_vertices.len() mismatch");
    assert_eq!(result_indices.len(), 66, "result_indices.len() mismatch");
    Ok(())
}

#[test]
fn test_non_manifold_edge_2() -> Result<(), RemeshError> {
    //******** vertices:10 indices:54
    let vertices = [
        [-1.0, -1.0, -1.0],
        [-1.0, -1.0, 1.0],
        [-1.0, 1.0, -1.0],
        [-1.0, 1.0, 1.0],
        [1.0, -1.0, -1.0],
        [1.0, -1.0, 1.0],
        [1.0, 1.0, -1.0],
        [1.0, 1.0, 1.0],
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
    ];
    let indices = [
        8, 3, 2, 3, 6, 2, 8, 5, 4, 5, 0, 4, 6, 0, 2, 7, 3, 9, 3, 8, 9, 2, 0, 8, 0, 1, 8, 4, 6, 8,
        6, 7, 8, 8, 9, 5, 8, 7, 9, 8, 1, 9, 5, 9, 1, 3, 7, 6, 5, 1, 0, 6, 4, 0,
    ];

    let (result_vertices, result_indices) =
        IsotropicRemesh::<f32, _, true>::new(&vertices, &indices)?
            .with_target_edge_length(0.5)?
            .with_split_edges(SplitStrategy::Disabled)?
            .with_default_collapse_multiplier()?
            .without_smooth_weight()?
            .with_flip_edges(FlipStrategy::Disabled)?
            .with_fix_non_manifold()?
            .with_print_stats(26)?
            .run(1)?;

    assert_eq!(result_vertices.len(), 11, "result_vertices.len() mismatch");
    assert_eq!(result_indices.len(), 54, "result_indices.len() mismatch");
    Ok(())
}
