如何计算具有三角形面的网格的质心? [英] How to compute the centroid of a mesh with triangular faces?

查看:24
本文介绍了如何计算具有三角形面的网格的质心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据以下描述,我想为我的网格计算一个新的质心.但我不想使用 Blender 的内置函数来计算质心,如 here 因为它们似乎没有给我我期望得到的质心.首先,我想计算网格的网格质心的面(三角形)的中心.然后我需要计算人脸面积.新质心是网格面中心的平均值,按其面积加权.如何在 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屋!

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