计算非典型物体的宽度、高度和体积

Calculating width, height and volume of atypical object

我有非典型物体,我想计算它的 宽度高度体积 .为了 volume 我有 this script

public class MeshVolume : MonoBehaviour
{
    void Start()
    {
        Mesh mesh = GetComponent<MeshFilter>().sharedMesh;
        float volume = VolumeOfMesh(mesh);
        string msg = "The volume of the mesh is " + volume + " cube units.";
        Debug.Log(msg);
    }


 public float SignedVolumeOfTriangle(Vector3 p1, Vector3 p2, Vector3 p3)
 {
     float v321 = p3.x * p2.y * p1.z;
     float v231 = p2.x * p3.y * p1.z;
     float v312 = p3.x * p1.y * p2.z;
     float v132 = p1.x * p3.y * p2.z;
     float v213 = p2.x * p1.y * p3.z;
     float v123 = p1.x * p2.y * p3.z;
     return (1.0f / 6.0f) * (-v321 + v231 + v312 - v132 - v213 + v123);
 }
 public float VolumeOfMesh(Mesh mesh)
 {
     float volume = 0;
     Vector3[] vertices = mesh.vertices;
     int[] triangles = mesh.triangles;
     for (int i = 0; i &lt; mesh.triangles.Length; i += 3)
     {
         Vector3 p1 = vertices[triangles[i + 0]];
         Vector3 p2 = vertices[triangles[i + 1]];
         Vector3 p3 = vertices[triangles[i + 2]];
         volume += SignedVolumeOfTriangle(p1, p2, p3);
     }
     return Mathf.Abs(volume);
 }

}

因为我不是数学家,所以我无法验证它是否工作正常,但有一点很清楚 如果我放大或缩小对象,它不会更新体积计算。那么这是计算物体体积的正确方法吗? 其次,我使用 meshRenderer bounds

得到对象的 widthheight
Bounds meshBounds = GetComponent<MeshRenderer>().bounds;
float width = meshBounds.size.x;
float height = meshBounds.size.y;

这是计算宽度和高度的正确方法吗?我怎样才能交叉检查它。

我已经通过revit工具划线检查了宽高和体积。

对于宽度、高度和呼吸::

Bounds meshBounds = GetComponent<MeshRenderer>().bounds;
float width   = meshBounds.size.x;
float height  = meshBounds.size.y;
float breath  =  meshBounds.size.z

在我的例子中,我采用了 MeshRendere 边界,您也可以通过 Collider 计算它。

音量:

public float SignedVolumeOfTriangle(Vector3 p1, Vector3 p2, Vector3 p3)
 {
     float v321 = p3.x * p2.y * p1.z;
     float v231 = p2.x * p3.y * p1.z;
     float v312 = p3.x * p1.y * p2.z;
     float v132 = p1.x * p3.y * p2.z;
     float v213 = p2.x * p1.y * p3.z;
     float v123 = p1.x * p2.y * p3.z;
     return (1.0f / 6.0f) * (-v321 + v231 + v312 - v132 - v213 + v123);
 }
 public float VolumeOfMesh(Mesh mesh)
 {
     float volume = 0;
     Vector3[] vertices = mesh.vertices;
     int[] triangles = mesh.triangles;
     for (int i = 0; i &lt; mesh.triangles.Length; i += 3)
     {
         Vector3 p1 = vertices[triangles[i + 0]];
         Vector3 p2 = vertices[triangles[i + 1]];
         Vector3 p3 = vertices[triangles[i + 2]];
         volume += SignedVolumeOfTriangle(p1, p2, p3);
     }
     return Mathf.Abs(volume);
 }