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

use remesh::prelude::*;

#[test]
fn test_f32_split_1() -> Result<(), RemeshError> {
    let vertices = vec![
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        [0.0, 0.5, 0.0],
        [1.0, 0.0, 0.0],
    ];

    let indices = vec![0, 1, 2, 1, 3, 2, 2, 3, 0, 1, 0, 3];

    let (result_vertices, result_indices) = IsotropicRemesh::<f32, _>::new(&vertices, &indices)?
        .with_target_edge_length(0.7)?
        .with_split_edges(SplitStrategy::DihedralAngle)?
        .with_collapse_edges(CollapseStrategy::Disabled)?
        .without_smooth_weight()?
        .with_flip_edges(FlipStrategy::Disabled)?
        .run(1)?;

    println!(
        "remesh result: {} vertices, {} triangles",
        result_vertices.len(),
        result_indices.len() / 3
    );
    assert_eq!(result_vertices.len(), 8, "result_vertices.len() mismatch");
    assert_eq!(result_indices.len(), 36, "result_indices.len() mismatch");
    Ok(())
}

#[test]
fn test_f64_split_1() -> Result<(), RemeshError> {
    let vertices = vec![
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        [0.0, 0.5, 0.0],
        [1.0, 0.0, 0.0],
    ];

    let indices = vec![0, 1, 2, 1, 3, 2, 2, 3, 0, 1, 0, 3];

    let (result_vertices, result_indices) = IsotropicRemesh::<f64, _>::new(&vertices, &indices)?
        .with_target_edge_length(1.1)?
        .with_split_multiplier(1.0)?
        .with_collapse_edges(CollapseStrategy::Disabled)?
        .without_smooth_weight()?
        .with_flip_edges(FlipStrategy::Disabled)?
        .run(1)?;

    println!(
        "remesh result: {} vertices, {} indices",
        result_vertices.len(),
        result_indices.len()
    );
    assert_eq!(result_vertices.len(), 6, "result_vertices.len() mismatch");
    assert_eq!(result_indices.len(), 24, "result_indices.len() mismatch");
    Ok(())
}

#[test]
fn test_f64_split_3() -> Result<(), RemeshError> {
    let vertices = vec![
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        [0.0, 0.5, 0.0],
        [1.0, 0.0, 0.0],
    ];

    let indices = vec![0, 1, 2, 2, 1, 3, 3, 1, 0, 2, 3, 0];

    let (result_vertices, result_indices) = IsotropicRemesh::<f64, _>::new(&vertices, &indices)?
        .with_target_edge_length(1.1)?
        .with_default_split_multiplier()?
        .with_collapse_edges(CollapseStrategy::Disabled)?
        .without_smooth_weight()?
        .with_flip_edges(FlipStrategy::Valence)?
        .run(2)?;

    println!(
        "f64 remesh result: {} vertices, {} triangles",
        result_vertices.len(),
        result_indices.len() / 3
    );
    println!(
        "result vertices:{result_vertices:?}:{}",
        result_vertices.len()
    );
    println!("result indices:{result_indices:?}:{}", result_indices.len());
    assert_eq!(result_vertices.len(), 4, "result_vertices.len() mismatch");
    assert_eq!(result_indices.len(), 12, "result_indices.len() mismatch");
    Ok(())
}

#[test]
fn test_split_4() -> Result<(), RemeshError> {
    let vertices = vec![
        [-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.99999994, -0.2928932, -0.2928932],
        [-0.2928932, 0.99999994, -0.2928932],
        [0.99999994, 0.2928932, -0.2928932],
        [0.2928932, -0.99999994, -0.2928932],
        [-0.2928932, 0.2928932, -0.99999994],
        [-0.99999994, 0.2928932, 0.2928932],
        [0.2928932, 0.99999994, 0.2928932],
        [0.99999994, -0.2928932, 0.2928932],
        [-0.2928932, -0.99999994, 0.2928932],
        [0.2928932, -0.2928932, -0.99999994],
        [-0.2928932, -0.2928932, 0.99999994],
        [0.2928932, 0.2928932, 0.99999994],
    ];
    let indices = vec![
        3, 6, 9, 7, 4, 10, 5, 0, 11, 6, 0, 12, 1, 3, 13, 3, 7, 14, 7, 5, 15, 5, 1, 16, 6, 4, 17, 3,
        1, 18, 7, 3, 19, 1, 2, 8, 2, 0, 8, 0, 1, 8, 6, 2, 9, 2, 3, 9, 4, 6, 10, 6, 7, 10, 0, 4, 11,
        4, 5, 11, 0, 2, 12, 2, 6, 12, 3, 2, 13, 2, 1, 13, 7, 6, 14, 6, 3, 14, 5, 4, 15, 4, 7, 15,
        1, 0, 16, 0, 5, 16, 4, 0, 17, 0, 6, 17, 1, 5, 18, 5, 3, 18, 3, 5, 19, 5, 7, 19,
    ];

    let (result_vertices, result_indices) = IsotropicRemesh::<f64, _>::new(&vertices, &indices)?
        .with_target_edge_length(3.0)?
        .with_default_split_multiplier()?
        .with_default_collapse_multiplier()?
        .without_smooth_weight()?
        .with_flip_edges(FlipStrategy::Valence)?
        .run(10)?;

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