最近项目需要对不规则物体的体积与面积进行计算,查阅了很多资料都没有进展,有的说用微积分的也有用VTK的感觉这些都很麻烦而且没有具体明确的思路,今天看到一篇相关资料感觉很简单易懂而切也很实用。
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
public class MeshCalculator : MonoBehaviour
{
private MeshFilter mf = null;
private Vector3 scale;
private float sum;
private string sum1;
public class MeshCalculator : MonoBehaviour
{
private MeshFilter mf = null;
private Vector3 scale;
private float sum;
private string sum1;
// Use this for initialization
void Start()
{
this.mf = this.GetComponent<MeshFilter>();
//有损缩放
this.scale = this.transform.lossyScale;
this.CalculateSumArea();
this.CalculateSumVolume();
}
this.CalculateSumVolume();
}
private void CalculateSumVolume()
{
Vector3[] arrVertices = this.mf.mesh.vertices;
int[] arrTriangles = this.mf.mesh.triangles;
float sum = 0.0f;
{
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]]);
}
{
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 sum1 = 0.0f;
{
Vector3[] arrVertices = this.mf.mesh.vertices;
int[] arrTriangles = this.mf.mesh.triangles;
float sum1 = 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)
sum1 += this.CalculateArea(arrVertices[arrIndices[j]]
, arrVertices[arrIndices[j + 1]]
, arrVertices[arrIndices[j + 2]]);
}
{
int[] arrIndices = this.mf.mesh.GetTriangles(i);
for (int j = 0; j < arrIndices.Length; j += 3)
sum1 += this.CalculateArea(arrVertices[arrIndices[j]]
, arrVertices[arrIndices[j + 1]]
, arrVertices[arrIndices[j + 2]]);
}
Debug.Log("Area = " + sum1);
}
}
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);
{
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;
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);
{
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;
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));
}
}
/* private void OnGUI()
{
GUILayout.Label("体积:" + Mathf.Abs(sum));
GUILayout.Label("面积:" +sum1);
}*/
{
GUILayout.Label("体积:" + Mathf.Abs(sum));
GUILayout.Label("面积:" +sum1);
}*/
}
经测试符合对不规则物体的体积表面积的求解
有篇国外的论文提到了一种计算方式。有兴趣的童学可以看看,算法非常简单,代码量也少。
项目是在Unity平台做的,我用的是C#代码,你们还可以参考这个unity帖子。
他通过对每个三角面片求其面积然后进行总的相加就可以了,但是对于那篇论文上讲解的对三角面片的买诺记求解那块不是很理解,希望有懂得的人指导一下。
转自:https://blog.csdn.net/zhang_hui_cs/article/details/77618329