# Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT
# except for the third-party components listed below.
# Hunyuan 3D does not impose any additional limitations beyond what is outlined
# in the repsective licenses of these third-party components.
# Users must comply with all terms and conditions of original licenses of these third-party
# components and must ensure that the usage of the third party components adheres to
# all relevant laws and regulations.

# For avoidance of doubts, Hunyuan 3D means the large language models and
# their software and algorithms, including trained model weights, parameters (including
# optimizer states), machine-learning model code, inference-enabling code, training-enabling code,
# fine-tuning enabling code and other elements of the foregoing made publicly available
# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.

import os
import trimesh
import pymeshlab


def remesh_mesh(mesh_path, remesh_path):
    # MODELBEAST: HY3D_REMESH_FACES env overrides the decimation target so the
    # bake-mesh budget is tunable per node (Studio GPUs handle far more than the
    # laptop-tuned 40k default). Additive — unset keeps upstream behavior.
    target = int(os.environ.get("HY3D_REMESH_FACES", "40000"))
    mesh = mesh_simplify_trimesh(mesh_path, remesh_path, target_count=target)


def mesh_simplify_trimesh(inputpath, outputpath, target_count=40000):
    # 先去除离散面
    ms = pymeshlab.MeshSet()
    if inputpath.endswith(".glb"):
        ms.load_new_mesh(inputpath, load_in_a_single_layer=True)
    else:
        ms.load_new_mesh(inputpath)
    ms.save_current_mesh(outputpath.replace(".glb", ".obj"), save_textures=False)
    # 调用减面函数
    courent = trimesh.load(outputpath.replace(".glb", ".obj"), force="mesh")
    face_num = courent.faces.shape[0]

    if face_num > target_count:
        # trimesh's simplify_quadric_decimation changed its signature:
        # older versions took an integer face target as the first positional
        # arg, newer ones expect `face_count=...` (first positional is
        # `percent`, 0-1). Pass explicitly so either API works.
        courent = courent.simplify_quadric_decimation(face_count=target_count)
    courent.export(outputpath)
