Triangle centroid

17 views (last 30 days)
tomas
tomas on 20 Mar 2012
Edited: DGM on 3 Oct 2025 at 11:57
Hello, do you somebody know any simlpe method how to find the triangle centroid (or geometric barycenter) in 3D?
Thanks a lot,
Tom
  1 Comment
Zhenren  Yang
Zhenren Yang on 9 May 2016
Moved: DGM on 30 Jun 2025
hi, have you get the code that can find the barycenter of 3d (stl,ply)?

Sign in to comment.

Accepted Answer

Jonathan Sullivan
Jonathan Sullivan on 20 Mar 2012
Just average all the coordinates. For example, if you have a vector containing x coordinates and a vector containing y coordinates, you can find it in the following manner.
x = rand(3,1); % x-coordinate
y = rand(3,1); % y-coordinate
x_centroid = mean(x);
y_centroid = mean(y);
  1 Comment
tomas
tomas on 20 Mar 2012
Hmm, that's very simple :-)
Thanks

Sign in to comment.

More Answers (1)

DGM
DGM on 30 Jun 2025
Edited: DGM ongeveer 22 uur ago
Another example for emphasis:
unzip stepholecube.stl.zip % for the forum
% so you have some triangles in 3D
T = stlread('stepholecube.stl');
[F V] = t2fv(T); % just for cleanliness
% then get the centroids. you're done
C = mean(permute(reshape(V(F,:),[size(F,1) 3 3]),[1 3 2]),3);
% not sure if that's right?
% well, the barycenter is at [1 1 1]/3 in barycentric coordinates, so ...
idx = (1:size(T,1)).';
Cref = barycentricToCartesian(T,idx,ones(numel(idx),3)/3);
immse(C,Cref) % they're the same.
ans = 3.3543e-34
Now, would this example have worked in 2012? The calculation of the centroid would work fine, though some of the other tools are anachronistic. That said, you don't actually need them to take the mean. If we were living in 2012, the same demo could still be written:
% so you have some triangles in 3D
[F V] = stlread('stepholecube.stl'); % FEX #22409 (NOT the same function!)
% then get the centroids. you're done
C = mean(permute(reshape(V(F,:),[size(F,1) 3 3]),[1 3 2]),3);
% not sure if that's right?
% well, the barycenter is at [1 1 1]/3 in barycentric coordinates, so ...
T = TriRep(F,V);
idx = (1:size(T,1)).';
Cref = baryToCart(T,idx,ones(numel(idx),3)/3);
mean((C(:) - Cref(:)).^2) % they're the same.
ans = 3.3543e-34
For what it's worth, getfacecenters() from FEX #182013 can be used to get other triangle centers, not limited to the centroid. It covers the centroid, incenter, circumcenter, and 8 other centers. It wouldn't have been available in 2012, but it would certainly work in a MATLAB version of the era.

Categories

Find more on Surfaces, Volumes, and Polygons in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!