计算三角面片的面积、体积

项目需求,需要计算三角面片的面积和体积,本以为很难,查阅资料才知,算法已经非常成熟,有篇国外的论文提到了一种计算方式。有兴趣的童学可以看看,算法非常简单,代码量也少。
项目是在Unity平台做的,我用的是C#代码,你们还可以参考这个unity帖子

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(MeshFilter))]
public class MeshCalculator : MonoBehaviour
{
    private MeshFilter mf = null;
    private Vector3 scale;

    // Use this for initialization
    void Start()
    {
        this.mf = this.GetComponent();
        this.scale = this.transform.lossyScale;

        this.CalculateSumArea();
        this.CalculateSumVolume();
    }

    private void CalculateSumVolume()
    {
        Vector3[] arrVertices = this.mf.mesh.vertices;
        int[] arrTriangles = this.mf.mesh.triangles;
        float sum = 0.0f;

        for (int i = 0; i < this.mf.mesh.subMeshCount; i++)
        {
            int[] arrIndices = this.mf.mesh.GetTriangles(i);
            for (int j = 0; j < arrIndices.Length; j += 3)
                sum += this.CalculateVolume(arrVertices[arrIndices[j]]
                                        , arrVertices[arrIndices[j + 1]]
                                        , arrVertices[arrIndices[j + 2]]);
        }

        Debug.Log("Volume= " + Mathf.Abs(sum));
    }

    private void CalculateSumArea()
    {
        Vector3[] arrVertices = this.mf.mesh.vertices;
        int[] arrTriangles = this.mf.mesh.triangles;
        float sum = 0.0f;

        for (int i = 0; i < this.mf.mesh.subMeshCount; i++)
        {
            int[] arrIndices = this.mf.mesh.GetTriangles(i);
            for (int j = 0; j < arrIndices.Length; j += 3)
                sum += this.CalculateArea(arrVertices[arrIndices[j]]
                                        , arrVertices[arrIndices[j + 1]]
                                        , arrVertices[arrIndices[j + 2]]);
        }

        Debug.Log("Area = " + sum);
    }

    private float CalculateVolume(Vector3 pt0, Vector3 pt1, Vector3 pt2)
    {
        pt0 = new Vector3(pt0.x * this.scale.x, pt0.y * this.scale.y, pt0.z * this.scale.z);
        pt1 = new Vector3(pt1.x * this.scale.x, pt1.y * this.scale.y, pt1.z * this.scale.z);
        pt2 = new Vector3(pt2.x * this.scale.x, pt2.y * this.scale.y, pt2.z * this.scale.z);

        float v321 = pt2.x * pt1.y * pt0.z;
        float v231 = pt1.x * pt2.y * pt0.z;
        float v312 = pt2.x * pt0.y * pt1.z;
        float v132 = pt0.x * pt2.y * pt1.z;
        float v213 = pt1.x * pt0.y * pt2.z;
        float v123 = pt0.x * pt1.y * pt2.z;

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

    private float CalculateArea(Vector3 pt0, Vector3 pt1, Vector3 pt2)
    {
        pt0 = new Vector3(pt0.x * this.scale.x, pt0.y * this.scale.y, pt0.z * this.scale.z);
        pt1 = new Vector3(pt1.x * this.scale.x, pt1.y * this.scale.y, pt1.z * this.scale.z);
        pt2 = new Vector3(pt2.x * this.scale.x, pt2.y * this.scale.y, pt2.z * this.scale.z);

        float a = (pt1 - pt0).magnitude;
        float b = (pt2 - pt1).magnitude;
        float c = (pt0 - pt2).magnitude;
        float p = (a + b + c) * 0.5f;

        return Mathf.Sqrt(p * (p - a) * (p - b) * (p - c));
    }

}

你可能感兴趣的:(unity,C#)