python实现STL模型文件体积表面积计算

没有什么特殊的算法,直接用包,开箱即用
from stl import mesh
import numpy as np

# 读取stl文件
filename = './text.stl'
mesh_data = mesh.Mesh.from_file(filename)

xyz = (mesh_data.max_ - mesh_data.min_)
sizel = round(xyz[0] / 10, 2)
sizew = round(xyz[1] / 10, 2)
sizeh = round(xyz[2] / 10, 2)

# 计算体积
volume, _, _ = mesh_data.get_mass_properties()

# 计算表面积
surface_area = 0.0
for triangle in mesh_data.vectors:
    # 获取三角形的三个顶点
    a = triangle[0]
    b = triangle[1]
    c = triangle[2]
    # 计算三角形的面积并累加
    surface_area += 0.5 * np.linalg.norm(np.cross(b - a, c - a))

print("Surface Area:", surface_area)

print("Volume:", volume)

 

下面是PHP计算体积和表面积 应该是顶点问题 计算有bug 等有空再来修复 仅做参考

<?php
// 从STL文件中解析顶点信息
// 从二进制STL文件中解析顶点信息
function parseBinarySTLFile($filePath) {
    $vertices = array();

    // 读取STL文件内容
    $stlContent = file_get_contents($filePath);
//var_dump($stlContent);
    // 假设STL文件头部80字节为文件头信息,跳过这部分
    $stlContent = substr($stlContent, 80);

    // 之后的4字节为三角形数量,按little-endian解析
    $numTriangles = unpack('V', substr($stlContent, 0, 4))[1];
    $stlContent = substr($stlContent, 4);

    // 依次解析每个三角形的顶点信息
    for ($i = 0; $i < $numTriangles; $i++) {
        // 每个三角形占据50个字节,包括面法向量和三个顶点坐标
        $data = unpack('f*', substr($stlContent, 0, 50));
        $vertices[] = array(
            [$data[3], $data[4], $data[5]],
            [$data[6], $data[7], $data[8]],
            [$data[9], $data[10], $data[11]]
        );
        $stlContent = substr($stlContent, 50);
    }
    return $vertices;
}

// 计算模型体积
function calculateModelVolume($vertices) {
    $volume = 0;

    // 对于每个三角形,计算有向体积并累加
    foreach ($vertices as $triangle) {
        // 以第一个顶点为起点,计算有向体积
        $v321 = $triangle[2][0] * $triangle[1][1] * $triangle[0][2];
        $v231 = $triangle[1][0] * $triangle[2][1] * $triangle[0][2];
        $v312 = $triangle[2][0] * $triangle[0][1] * $triangle[1][2];
        $v132 = $triangle[0][0] * $triangle[2][1] * $triangle[1][2];
        $v213 = $triangle[1][0] * $triangle[0][1] * $triangle[2][2];
        $v123 = $triangle[0][0] * $triangle[1][1] * $triangle[2][2];

        $volume += (1.0 / 6.0) * (-$v321 + $v231 + $v312 - $v132 - $v213 + $v123);
        //return (-v321 + v231 + v312 - v132 - v213 + v123)/6.0;
    }

    return abs($volume);
}


// 计算模型表面积
function calculateModelSurfaceArea($vertices) {
    $surfaceArea = 0;

    // 对于每个三角形,计算面积并累加
    foreach ($vertices as $triangle) {
        // 计算三角形的两条边的向量
        $v1 = array(
            $triangle[1][0] - $triangle[0][0],
            $triangle[1][1] - $triangle[0][1],
            $triangle[1][2] - $triangle[0][2]
        );
        $v2 = array(
            $triangle[2][0] - $triangle[0][0],
            $triangle[2][1] - $triangle[0][1],
            $triangle[2][2] - $triangle[0][2]
        );

        // 计算三角形的面积(使用向量叉乘)
        $crossProduct = array(
            $v1[1] * $v2[2] - $v1[2] * $v2[1],
            $v1[2] * $v2[0] - $v1[0] * $v2[2],
            $v1[0] * $v2[1] - $v1[1] * $v2[0]
        );
        $triangleArea = 0.5 * sqrt(pow($crossProduct[0], 2) + pow($crossProduct[1], 2) + pow($crossProduct[2], 2));

        // 累加三角形的面积
        $surfaceArea += $triangleArea;
    }

    return $surfaceArea;
}


function calculateModelDimensions($vertices) {
    $minX = $maxX = $vertices[0][0][0];
    $minY = $maxY = $vertices[0][0][1];
    $minZ = $maxZ = $vertices[0][0][2];
    foreach ($vertices as $triangle) {
        foreach ($triangle as $vertex) {
            $minX = min($minX, $vertex[0]);
            $maxX = max($maxX, $vertex[0]);
            $minY = min($minY, $vertex[1]);
            $maxY = max($maxY, $vertex[1]);
            $minZ = min($minZ, $vertex[2]);
            $maxZ = max($maxZ, $vertex[2]);
        }
    }

    $width = $maxX - $minX;
    $height = $maxY - $minY;
    $depth = $maxZ - $minZ;

    return array('width' => $width, 'height' => $height, 'depth' => $depth);
}



// STL文件路径
$stlFilePath = '111.stl';

// 解析STL文件并获取顶点信息
$vertices = parseBinarySTLFile($stlFilePath);
//var_dump($vertices);
// 计算模型体积
$modelVolume = calculateModelVolume($vertices);

// 表面积
//$area = calculateModelSurfaceArea($vertices);

// 计算模型的长宽高尺寸
//$modelDimensions = calculateModelDimensions($vertices);

// 输出模型的长宽高尺寸
//echo "Model Dimensions: Width = " . $modelDimensions['width'] . ", Height = " . $modelDimensions['height'] . ", Depth = " . $modelDimensions['depth'];

//echo $area;
// 输出模型体积
echo "The volume of the STL model is: " . $modelVolume;
?>

 

posted @ 2023-11-08 16:03  Solo李  阅读(668)  评论(0编辑  收藏  举报