Skip to content

Commit 261ff15

Browse files
Steffen SchulerSteffen Schuler
Steffen Schuler
authored and
Steffen Schuler
committed
Added vtkCenterOfArea and vtkCenterOfVolume
1 parent f4cdf92 commit 261ff15

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
build/
33
*.mex*
44
*.m~
5+
*.asv

MATLAB/remeshTriangleMesh.m

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
instantmeshes = '/Applications/Instant Meshes.app/Contents/MacOS/Instant Meshes';
2929
end
3030
if ~exist(instantmeshes, 'file')
31-
instantmeshes = '/Users/ss029/Applications/Instant Meshes.app/Contents/MacOS/Instant Meshes';
32-
% instantmeshes = '/Volumes/ServerApps/Instant Meshes.app/Contents/MacOS/Instant Meshes';
31+
instantmeshes = '/Volumes/ServerApps/Instant Meshes.app/Contents/MacOS/Instant Meshes';
3332
end
3433
if ~exist(instantmeshes, 'file')
3534
error('Instant Meshes could not be found.');

MATLAB/vtkCenterOfArea.m

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
% Computes the center of area (center of mass of a surface, assuming
2+
% uniform density) of a triangle mesh.
3+
% Relies on gptoolbox by Alec Jacobson.
4+
%
5+
% Syntax:
6+
% [centerOfArea,area,a] = vtkCenterOfArea(inStruct)
7+
%
8+
% Written in 2020 by Steffen Schuler
9+
% Institute of Biomedical Engineering, KIT
10+
% www.ibt.kit.edu
11+
12+
function [centerOfArea,area,a] = vtkCenterOfArea(inStruct)
13+
14+
P = double(inStruct.points);
15+
C = inStruct.cells;
16+
17+
if size(C,2) ~=3
18+
error('This function can only be applied to triangle meshes.');
19+
end
20+
21+
centroids = squeeze(mean(reshape(P(C,:),[],size(C,2),size(P,2)),2));
22+
a = doublearea(P,C)/2;
23+
area = sum(a);
24+
25+
centerOfArea = sum(repmat(a,1,3) .* centroids, 1) ./ area;
26+
27+
end

MATLAB/vtkCenterOfVolume.m

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
% Computes the center of volume (center of mass assuming uniform density)
2+
% of a tetrahedral mesh or a closed triangle mesh.
3+
% Relies on gptoolbox by Alec Jacobson.
4+
%
5+
% Syntax:
6+
% [centerOfVolume,volume,v] = vtkCenterOfVolume(inStruct)
7+
%
8+
% Written in 2020 by Steffen Schuler
9+
% Institute of Biomedical Engineering, KIT
10+
% www.ibt.kit.edu
11+
12+
function [centerOfVolume,volume,v] = vtkCenterOfVolume(inStruct)
13+
14+
P = double(inStruct.points);
15+
C = inStruct.cells;
16+
17+
dim = size(C,2);
18+
if dim == 3
19+
P = [P; mean(P,1)];
20+
C = [C repmat(size(P,1), size(C,1), 1)];
21+
end
22+
23+
a = P(C(:,1),:);
24+
b = P(C(:,2),:);
25+
c = P(C(:,3),:);
26+
d = P(C(:,4),:);
27+
v = dot(a-d, cross(b-d, c-d, 2), 2)./6;
28+
volume = sum(v);
29+
30+
if dim == 3
31+
d0 = repmat([0 0 0], size(C,1), 1);
32+
v0 = dot(a-d0, cross(b-d0, c-d0, 2), 2)./6;
33+
volume0 = sum(v0);
34+
if abs(volume-volume0) > 1e-5
35+
warning('The surface is not closed.');
36+
end
37+
end
38+
39+
centroids = squeeze(mean(reshape(P(C,:),[],size(C,2),size(P,2)),2));
40+
41+
centerOfVolume = sum(repmat(v,1,3) .* centroids, 1) ./ volume;
42+
43+
end

0 commit comments

Comments
 (0)