geometry.py

# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (C) 2025 SWGY, Inc
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
import os
import numpy as np
import trimesh

# Geometry loading and transformation
def load_helmet(path: str) -> trimesh.base.Trimesh:
    """
    Load and transform the helmet mesh.

    Args:
        path: Path to the helmet mesh file

    Returns:
        Transformed helmet mesh
    """
    if not os.path.exists(path):
        raise FileNotFoundError(f"Helmet geometry file not found: {path}")

    try:
        helmet = trimesh.load_mesh(path)
    except Exception as e:
        raise ValueError(f"Failed to load helmet geometry from {path}: {str(e)}")

    # Apply rotation and scaling transformations
    rotation = trimesh.transformations.rotation_matrix(np.pi/2, [1, 0, 0])
    scale = trimesh.transformations.scale_matrix(0.5)
    transform = trimesh.transformations.concatenate_matrices(rotation, scale)

    helmet.apply_transform(transform)
    helmet.fix_normals()

    return helmet


def load_vest(path: str) -> trimesh.base.Trimesh:
    """
    Load and transform the vest mesh.

    Args:
        path: Path to the vest mesh file

    Returns:
        Transformed vest mesh
    """
    if not os.path.exists(path):
        raise FileNotFoundError(f"SAPI vest plate geometry file not found: {path}")

    try:
        vest = trimesh.load_mesh(path)
    except Exception as e:
        raise ValueError(f"Failed to load SAPI vest geometry from {path}: {str(e)}")

    # Apply rotation and translation transformations
    rotation = trimesh.transformations.rotation_matrix(np.pi, [1, 0, 0])
    translation = trimesh.transformations.translation_matrix([0, -0.12, -0.5])
    transform = trimesh.transformations.concatenate_matrices(translation, rotation)

    vest.apply_transform(transform)
    vest.fix_normals()

    return vest