如何计算存储在 STL 文件中的对象的体积? [英] How do I calculate the volume of an object stored in STL files?

查看:22
本文介绍了如何计算存储在 STL 文件中的对象的体积?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 .stl (STL 是由 3D 创建的立体光刻 CAD 软件的原生文件格式Systems) 文件,我必须从中计算体积.我该怎么做呢?我使用下面的计算但是体积不等于其他软件计算的体积

I have .stl (STL is a file format native to the stereolithography CAD software created by 3D Systems) files, from which I must calculate the volume. How do I do this? I'm using the calculation below but the volume is not equal to that calculated by other software

float x1,y1,z1;
float x2,y2,z2;
float x3,y3,z3;

以上是顶点.triangles 只是一个包含每个三角形顶点的对象的数据结构.

The above are the vertices. triangles is just a data structure holding an object with the vertices of each triangle.

totalVolume += currentVolume = 
(triangles[i].x1*triangles[i].y2*triangles[i].z3 - 
triangles[i].x1*triangles[i].y3*triangles[i].z2 -

triangles[i].x2*triangles[i].y1*triangles[i].z3 + 
triangles[i].x2*triangles[i].y3*triangles[i].z1 + 
triangles[i].x3*triangles[i].y1*triangles[i].z2 - 
triangles[i].x3*triangles[i].y2*triangles[i].z1) / 6;

你是否也涉及到法向量的计算?

Do you also involve the calculation with the normal vector?

推荐答案

多面体的体积在 polyhedron 文章.任何有效的 STL 细分都必须是一个多面体,其中每个面都是一个边,因此那里讨论的公式成立.

The volume of polyhedra is discussed in the polyhedron article in Wikipedia. Any valid STL tessellation must be precisely a polyhedron where each facet is a side, so the formula discussed there holds.

假设顶点是逆时针方向的(向下看法线),下面的扩展成立:

Assuming the vertices are oriented counter-clockwise (looking down the outward normals) the following expansion holds:

公式 1 只是多面体的散度公式.p[i,j] 是第 i 个三角形的第 j 个顶点(作为距原点的向量).

Equation 1 is just the divergence formula for polyhedra. The p[i,j] are the jth vertex (as a vector from the origin) of the ith triangle.

公式 2 通过使用以下事实扩展了这一点:两个三角形边(平行四边形)的叉积是三角形的法向量,其大小是三角形面积的两倍.

Equation 2 expands this by using the fact that the the cross product of the two triangle sides (a parallelogram) is a normal vector of the triangle with magnitude twice the area of the triangle.

顺便说一下,这就是为什么三角形法线没有出现在你的表达式中,即使你的直觉认为它应该以某种方式存在.它已经在汤里了!

By the way, this is why the triangle normal doesn't show up in your expression even though your intuition says it should be there somehow. It's already in the soup!

等式 3 只是使用叉积的分布特性扩展了等式 2,而且向量交叉本身就是零向量这一事实.

Equation 3 just expands equation 2 using the distributive properties of the cross product, and that fact that a vector cross itself is the zero vector.

您从等式 3 中得到的体积根据三角形的方向进行标记.我已经调整了我的公式以同意您的代码,但您可能会得到负面结果.如果是,就取绝对值.

The volume you get from equation 3 is signed according to the orientation of the triangles. I've aligned my formulation to agree with your code, but you may have a negative result. If so, just take the absolute value.

现在用你的符号写出被加数(除了我不费心输入重复的三角形[i])我得到:

Now writing out the summand in your notation (except that I don't bother putting in the repeated triangle[i]) I get:

(-x3 y2 z1 + x2 y3 z1 + x3 y1 z2 - x1 y3 z2 - x2 y1 z3 + x1 y2 z3)/6.;

这与您所写的完全匹配(订单除外)!

This exactly matches what you've written (except for order)!

所以有两种可能性:

(1) 也许你的 STL 文件有缺陷,三角形的方向不一致.您可以通过验证每个边在向前方向上被三角形使用一次,在相反方向使用一次来检查这一点.边"是指作为同一个三角形一部分的一对顶点 (p[r], p[s]).我正在更改符号并使用下标来指示文件中唯一索引的顶点,而不是顶点相对于面的索引,就像我在上面使用双索引符号所做的那样).

(1) Perhaps your STL file is flawed and the triangles aren't all oriented consistently. You can check this by verifying that each edge is used by a triangle once in the forward direction, and once in the opposite direction. By and "edge" I mean a pair of vertices (p[r], p[s]) that are part of the same triangle. I'm changing notation and using the subscript to indicate the uniquely indexed vertex in the file, not the index of the vertex relative to the face as I did with the two-index notation above).

如果一个三角形在它的方向边列表中有边 (p[r], p[s]),那么其他三角形必须有 (p[s], p[r])(相同的顶点,相反的顺序)在它的定向边列表中.

If a triangle has edge (p[r], p[s]) in its list of oriented edges, then some other triangle must have (p[s], p[r]) (same vertices, reversed order) in it's list of oriented edges.

(2) 曲面细分未关闭.这是与 1 相同的测试,只是如果只有一个三角形使用边缘,则无论方向如何,它都是开放的.

(2) The tessellation isn't closed. This is the same test as 1, except that it's open if only one triangle uses the edge, regardless of orientation.

(3) 我敢提吗?你确定你初始化了 currentVolume 吗?

(3) Dare I mention it? Are you sure you initialized currentVolume?

(4) 你得到了正确的答案,但音量是负的,你被愚弄了认为方法是错误的.请参阅我上面的评论.

(4) You get the right answer, but the volume is negative and you're fooled into thinking the method is wrong. See my comments above.

这篇关于如何计算存储在 STL 文件中的对象的体积?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆