如何计算具有三角形面的网格的质心? [英] How to compute the centroid of a mesh with triangular faces?
问题描述
鉴于以下说明,我想为我的网格计算一个新的质心.但是我不想使用Blender的内置函数来计算质心,如此处所述 因为它们似乎没有给我我期望得到的质心.首先,我要计算网格的网格质心的面(三角形)的中心.然后我需要计算人脸面积.新质心是网格面中心的平均值,并由其面积加权.如何在Python中执行此操作(但不一定使用Blender的Python API)?
I want to compute a new centroid for my meshes given the following description. But I do not want to use Blender's built-in functions for computing centroids as explained here as it seems that they do not give me the kind of centroid I expect to get. First, I want to compute the centers of faces (triangle) of a mesh centroid of a mesh. Then I need to compute the faces area. The new centroid is the average of the mesh faces' centers, weighted by their area. How can I do this in Python (but not necessarily using Blender's Python API)?
推荐答案
使用3个顶点定义每个三角形 p0,p1,p2
中心很容易
let define each triangle with 3 vertexes p0,p1,p2
the center is easy
center = (p0+p1+p2) /3
它只是构成它的所有顶点的平均值.面积可以通过叉积计算为:
it is just the average of all vertices which forms it. The area can be computed by cross product as:
area = 0.5 * | (p1-p0) x (p2-p0) |
area = 0.5 * length(cross( p1-p0, p2-p0 ))
两者都只是符号不同而已......因此,您所描述的质心应按以下方式计算(在 C ++ 中):
Both are the same just in different notation... So the centroid you are describing should be computed like this (in C++):
float area_sum=0.0;
vec3 centroid=vec3(0.0,0.0,0.0);
for (int i=0;i<triangles;i++)
{
t = triangle[i];
vec3 center = (t.p0+t.p1+t.p2) /3;
float area = 0.5 * length(cross(t.p1-t.p0, t.p2-t.p0));
centroid += area*center;
area_sum += area;
}
centroid /= area_sum;
其中 triangle [trianges]
是您的面孔数组,其中每个面孔都有 p0,p1,p2
作为 3D 向量.抱歉,我没有使用 Python ,因此您需要根据自己的风格/环境调整矢量数学.如果您不知道如何计算叉积,请点击此处:
where triangle[trianges]
is array of your faces where each face has p0,p1,p2
as 3D vectors. Sorry I do not use Python so you need to adapt the vector math to your style/environment. If you do not know how to compute cross product look here:
向量数学在底部...
vector math is at the bottom ...
这篇关于如何计算具有三角形面的网格的质心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!