-
Notifications
You must be signed in to change notification settings - Fork 28
/
platonicSolid.m
156 lines (142 loc) · 4.65 KB
/
platonicSolid.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
function [u, dirs, mesh] = platonicSolid(shape)
%PLATONICSOLID Generates platonic solid vertices and faces
% PLATONICSOLID Generates the five platonic solids, centered at the origin,
% with their vertices being at unit radius. The cartesian coordinates, the
% directions (in [azi elev] convention), or the mesh structure with
% vertices and faces can be returned. The solid to be generated is
% specified by the string 'shape'.
%
% Inputs:
% shape: 'tetra' or 'tetrahedron'
% 'hexa' or 'hexahedron' or 'cube'
% 'octa' or 'octahedron'
% 'dodeca' or 'dodecahedron'
% 'icosa' or 'icosahedron'
% string to get the respective shape, capital or lowercase
%
% Outputs:
% u: matrix of vertex coordinates
% dirs: matrix of spherical coordinates in [azi elev] style
% mesh: structure combining vertices and faces, for plotting 3D plots,
% or generating dense meshes by subdividing faces and
% re-triangulating
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Archontis Politis, 15/11/2015
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
switch lower(shape)
case {'tetra','tetrahedron'}
u = [1 1 1
1 -1 -1
-1 1 -1
-1 -1 1 ]/sqrt(3);
f = [1 2 3
1 3 4
1 4 2
2 4 3 ];
case {'cube','hexa','hexahedron'}
u = [1 1 1
1 1 -1
1 -1 1
1 -1 -1
-1 1 1
-1 1 -1
-1 -1 1
-1 -1 -1 ]/sqrt(3);
f = [2 4 3 1
8 6 5 7
6 2 1 5
4 8 7 3
3 7 5 1
6 8 4 2];
case {'octa','octahedron'}
u = [1 0 0
-1 0 0
0 1 0
0 -1 0
0 0 1
0 0 -1 ];
f = [1 3 5
1 4 6
1 5 4
1 6 3
2 3 6
2 4 5
2 5 3
2 6 4];
case {'dodeca','dodecahedron'}
gr = (1 + sqrt(5))/2; % golden ratio
u = [1 1 -1
-1 1 -1
-1 1 1
-1 -1 1
1 -1 1
1 -1 -1
-1 -1 -1
1 1 1
0 1/gr -gr
0 -1/gr gr
0 -1/gr -gr
0 1/gr gr
1/gr gr 0
-1/gr gr 0
1/gr -gr 0
-1/gr -gr 0
-gr 0 -1/gr
-gr 0 1/gr
gr 0 1/gr
gr 0 -1/gr ] / sqrt(3);
f = [2 3 5 4 1
17 14 15 2 1
18 16 11 14 17
1 4 6 18 17
15 13 19 3 2
14 11 12 13 15
13 12 10 20 19
3 19 20 7 5
4 5 7 8 6
10 9 8 7 20
18 6 8 9 16
16 9 10 12 11 ];
case {'icosa','icosahedron'}
gr = (1 + sqrt(5)) / 2; % golden ratio
u = [0 gr 1
0 -gr 1
0 gr -1
0 -gr -1
1 0 gr
1 0 -gr
-1 0 gr
-1 0 -gr
gr 1 0
gr -1 0
-gr 1 0
-gr -1 0 ] / sqrt(1+gr^2);
f = [1 3 11
1 5 9
1 7 5
1 9 3
1 11 7
2 4 10
2 5 7
2 7 12
2 10 5
2 12 4
3 6 8
3 8 11
3 9 6
4 6 10
4 8 6
4 12 8
5 10 9
6 9 10
7 11 12
8 12 11 ];
end
% return directions and mesh with vertices/faces
[dirs(:,1), dirs(:,2)] = cart2sph(u(:,1), u(:,2), u(:,3));
mesh.vertices = u;
mesh.faces = f;