计算 3D 网格的体积

发布于 2024-11-01 14:37:31 字数 2793 浏览 3 评论 0 原文

我厌倦了计算 3D 物体(立方体、圆柱体...)的体积,有人可以帮助

解决这个问题吗?问题是,如何根据三角形的坐标计算物体的体积

。我的班级做得不好,有人帮助我

改善班级吗?

谢谢

public class Algorithm
{
    private  Mesh _mesh { get; set; }

    public Algorithm(Mesh mesh)
    {
        _mesh = mesh;
    }

    private double SignedVolumeOfTriangle(Vector3 p1, Vector3 p2, Vector3 p3)
    {
        var v321 = p3.X * p2.Y * p1.Z;
        var v231 = p2.X * p3.Y * p1.Z;
        var v312 = p3.X * p1.Y * p2.Z;
        var v132 = p1.X * p3.Y * p2.Z;
        var v213 = p2.X * p1.Y * p3.Z;
        var v123 = p1.X * p2.Y * p3.Z;
        return (1.0 / 6.0) * (-v321 + v231 + v312 - v132 - v213 + v123);
    }

    public double VolumeOfMesh()
    {
        double volume = 0.0;

        Vector3[] vertices = _mesh.Vertices;
        int[] triangles = _mesh.Triangles;

        for (int i = 0; i < _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 Math.Abs(volume);
    }
}



public  class Mesh
{
    public Mesh(Vector3[] _vertices,int[] _triangles)
    {
        Vertices = _vertices;
        Triangles = _triangles;
    }

    public Vector3[] Vertices { get; set; }

    public int[] Triangles { get; set; }
}


public class Vector3
{
    public Vector3()
    {

    }

    public Vector3(double x,double y,double z)
    {
        X = x;
        Y = y;
        Z = z;
    }

    public double X { get; set; }

    public double Y { get; set; }

    public double Z { get; set; }
}



private void button1_Click(object sender, EventArgs e)
{
        Vector3[] vers = new Vector3[8] {
                                    new Vector3 {X = 5,Y = 5,Z =5},
                                    new Vector3 {X = 15,Y = 5,Z =5},
                                    new Vector3 {X = 15,Y = 15,Z =5},
                                    new Vector3 {X = 5,Y = 15,Z =5},
                                    new Vector3 {X = 5,Y = 5,Z =15},
                                    new Vector3 {X = 15,Y = 5,Z =15},
                                    new Vector3 {X = 15,Y = 15,Z =15},
                                    new Vector3 {X = 5,Y = 15,Z =15},
        };

        int[] trs = new int[36] { 0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 
                                  1, 6, 2, 1, 5, 6, 0, 4, 7, 0, 7, 3,
                                  0, 1, 5, 0, 5, 4, 3, 2, 6,3, 6, 7 };


        Mesh mesh = new Mesh(vers, trs);
        Algorithm algo = new Algorithm(mesh);
        var vol = algo.VolumeOfMesh();

        MessageBox.Show(vol.ToString());
}

我的测试结果是 vol = 666,666 但它应该是 1000。

i get tired to calculate the volume of 3D object (Cube, Cylinder ...) can any one help

with this problem ? the question is , how to calculate the volume of object from his

coordinates based on triangles. my class don't do well the job , any one help me to

emproove the class ?

thanks

public class Algorithm
{
    private  Mesh _mesh { get; set; }

    public Algorithm(Mesh mesh)
    {
        _mesh = mesh;
    }

    private double SignedVolumeOfTriangle(Vector3 p1, Vector3 p2, Vector3 p3)
    {
        var v321 = p3.X * p2.Y * p1.Z;
        var v231 = p2.X * p3.Y * p1.Z;
        var v312 = p3.X * p1.Y * p2.Z;
        var v132 = p1.X * p3.Y * p2.Z;
        var v213 = p2.X * p1.Y * p3.Z;
        var v123 = p1.X * p2.Y * p3.Z;
        return (1.0 / 6.0) * (-v321 + v231 + v312 - v132 - v213 + v123);
    }

    public double VolumeOfMesh()
    {
        double volume = 0.0;

        Vector3[] vertices = _mesh.Vertices;
        int[] triangles = _mesh.Triangles;

        for (int i = 0; i < _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 Math.Abs(volume);
    }
}



public  class Mesh
{
    public Mesh(Vector3[] _vertices,int[] _triangles)
    {
        Vertices = _vertices;
        Triangles = _triangles;
    }

    public Vector3[] Vertices { get; set; }

    public int[] Triangles { get; set; }
}


public class Vector3
{
    public Vector3()
    {

    }

    public Vector3(double x,double y,double z)
    {
        X = x;
        Y = y;
        Z = z;
    }

    public double X { get; set; }

    public double Y { get; set; }

    public double Z { get; set; }
}



private void button1_Click(object sender, EventArgs e)
{
        Vector3[] vers = new Vector3[8] {
                                    new Vector3 {X = 5,Y = 5,Z =5},
                                    new Vector3 {X = 15,Y = 5,Z =5},
                                    new Vector3 {X = 15,Y = 15,Z =5},
                                    new Vector3 {X = 5,Y = 15,Z =5},
                                    new Vector3 {X = 5,Y = 5,Z =15},
                                    new Vector3 {X = 15,Y = 5,Z =15},
                                    new Vector3 {X = 15,Y = 15,Z =15},
                                    new Vector3 {X = 5,Y = 15,Z =15},
        };

        int[] trs = new int[36] { 0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 
                                  1, 6, 2, 1, 5, 6, 0, 4, 7, 0, 7, 3,
                                  0, 1, 5, 0, 5, 4, 3, 2, 6,3, 6, 7 };


        Mesh mesh = new Mesh(vers, trs);
        Algorithm algo = new Algorithm(mesh);
        var vol = algo.VolumeOfMesh();

        MessageBox.Show(vol.ToString());
}

The result of my test is vol = 666,666 but it should be 1000.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话墙 2024-11-08 14:37:32

正确

根据 double v132 = (p3.X - basePoint.X) * (p3.Y - basePoint.Y) * (p2.Z - basePoint.Z); 行 href="https://stackoverflow.com/questions/1406029/how-to-calculate-the-volume-of-a-3d-mesh-object-the-surface-of-which-is-made-up-t ">如何计算表面由三角形组成的 3D 网格对象的体积(注意 p1.X 而不是 p3.X):

var v132 = p1.X*p3.Y*p2.Z;

编辑

虽然您将此答案标记为正确,但我测试了代码并发现了更多错误。

通过像这样调整三角形索引,三角形并不全部朝外(或朝内):

0, 1, 2,
0, 2, 3,
4, 6, 5,
4, 7, 6,// adjusted
1, 6, 2,
1, 5, 6,
0, 7, 4,// adjusted
0, 3, 7,// adjusted
0, 5, 1,// adjusted
0, 4, 5,// adjusted
3, 2, 6,
3, 6, 7

所有法线朝外。然后计算返回 -1000 负值,具体取决于基本偏移量。

The line

double v132 = (p3.X - basePoint.X) * (p3.Y - basePoint.Y) * (p2.Z - basePoint.Z);

Is not correct according to How to calculate the volume of a 3D mesh object the surface of which is made up triangles it should be (notice the p1.X instead of p3.X):

var v132 = p1.X*p3.Y*p2.Z;

EDIT

Although you marked this answer as correct I tested the code and found more errors.

The triangles are not all facing outward (or inward) by adjusting the triangle indices like this:

0, 1, 2,
0, 2, 3,
4, 6, 5,
4, 7, 6,// adjusted
1, 6, 2,
1, 5, 6,
0, 7, 4,// adjusted
0, 3, 7,// adjusted
0, 5, 1,// adjusted
0, 4, 5,// adjusted
3, 2, 6,
3, 6, 7

all normals face outward. the calculation then returns -1000 the minus depending on the base offset.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文