-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpherePatch.m
196 lines (154 loc) · 5.49 KB
/
SpherePatch.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
function [ outStruct ] = SpherePatch( numPts, radius, varargin)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
FlipZ = false;
optargin = size(varargin,2);
if(optargin == 0)
axis = [0 0 1];
hemisphere = False;
elseif(optargin == 1)
axis = varargin{1};
hemisphere = false;
elseif(optargin == 2)
axis = varargin{1};
hemisphere = varargin{2};
elseif(optargin == 3)
FlipZ = varargin{3};
axis = varargin{1};
hemisphere = varargin{2};
end
numVerts = (numPts * numPts) + 2;
RhoStep = (pi) / (numPts+2);
RhoRange = pi;
if(hemisphere)
RhoStep = (pi/2) / ((numPts/2)+1);
RhoRange = pi/2 + RhoStep;
numVerts = numPts * (numPts / 2) + 1;
end
axis = axis ./ norm(axis);
ThetaStep = (2*pi) / numPts;
verts = zeros(numVerts,3);
normals = zeros(numVerts,3);
iVert = 1;
for(rho = RhoRange-RhoStep:-RhoStep:0+RhoStep) %Leave the tips of the sphere as single points
for(theta = 0:ThetaStep:(2*pi) - ThetaStep)
x = cos(theta) * sin(rho) * radius;
y = sin(theta) * sin(rho) * radius;
z = cos(rho) * radius;
verts(iVert, :) = [x y z];
normals(iVert, :) = [x y z];
iVert = iVert+1;
end
end
verts(iVert,:) = [0 0 radius];
normals(iVert,:) = [0 0 radius];
iVert = iVert+1;
if(~hemisphere)
verts(iVert,:) = [0 0 -radius];
normals(iVert,:) = [0 0 -radius];
iVert = iVert+1;
end
if(FlipZ)
verts(:, 3) = -verts(:, 3);
normals(:, 3) = -normals(:, 3);
end
%We never bother rotating spheres
if(hemisphere)
%CreateRotationMatrix(world_up=[0 0 1], out=axis, up=[1? 0 0])
[angle, newaxis] = AngleAndAxis([0 0 1], axis);
RotMat = RotationMatrix(angle, newaxis);
TestVector = verts(1,:);
TestVectorRot = TestVector * RotMat;
[testangle,testaxis] = AngleAndAxis(axis, TestVectorRot);
% disp(['Test Angle: ' num2str(testangle)]);
if(testangle > .0001 || testangle < .0001)
angle = pi-angle;
end
RotMat = RotationMatrix(angle, newaxis);
verts = verts * RotMat;
normals = normals * RotMat;
end
%Build faces for the sphere
%One triangle from each pair to the cap...
%
NumFaces = numPts + (numPts * (numPts-1)/2);
Faces = zeros(NumFaces,3);
%Add the cap
iFace = 1;
if(hemisphere)
for(iZ = 0:(numPts/2)-1)
ZOffset = iZ * numPts;
for(iTheta = 1:numPts)
NextIndexOnCircle = iTheta + 1;
if(iTheta + 1 > numPts)
NextIndexOnCircle = 1;
end
Faces(iFace, :) = [iTheta + ZOffset iTheta + ((iZ+1)) * numPts NextIndexOnCircle + ZOffset ];
iFace = iFace + 1;
Faces(iFace, :) = [NextIndexOnCircle + ZOffset iTheta + ((iZ+1) * numPts) NextIndexOnCircle + ((iZ+1) * numPts) ];
iFace = iFace + 1;
end
end
iCap = length(verts);
ZOffset = (numPts/2) * numPts;
for(i = 1:numPts)
if(i+1 > numPts)
Faces(iFace,:) = [i+ZOffset iCap 1+ZOffset ];
else
Faces(iFace,:) = [i+ZOffset iCap i+1+ZOffset ];
end
iFace = iFace + 1;
end
if(FlipZ)
temp = Faces(:,1);
Faces(:,1) = Faces(:,3);
Faces(:,3) = temp;
end
else
for(iZ = 0:(numPts)-1)
ZOffset = iZ * numPts;
for(iTheta = 1:numPts)
NextIndexOnCircle = iTheta + 1;
if(iTheta + 1 > numPts)
NextIndexOnCircle = 1;
end
Faces(iFace, :) = [iTheta + ZOffset iTheta + ((iZ+1) * numPts) NextIndexOnCircle + ZOffset ];
iFace = iFace + 1;
Faces(iFace, :) = [NextIndexOnCircle + ZOffset iTheta + ((iZ+1) * numPts) NextIndexOnCircle + ((iZ+1) * numPts) ];
iFace = iFace + 1;
end
end
iCap = length(verts)-1;
ZOffset = (numPts) * numPts;
for(i = 1:numPts)
if(i+1 > numPts)
Faces(iFace,:) = [ i+ZOffset iCap 1+ZOffset ];
else
Faces(iFace,:) = [ i+ZOffset iCap i+1+ZOffset ];
end
iFace = iFace + 1;
end
iCap = length(verts);
ZOffset = 0;
for(i = 1:numPts)
if(i+1 > numPts)
Faces(iFace,:) = [i+ZOffset 1+ZOffset iCap];
else
Faces(iFace,:) = [i+ZOffset i+1+ZOffset iCap];
end
iFace = iFace + 1;
end
% temp = Faces(:,1);
% Faces(:,1) = Faces(:,3);
% Faces(:,3) = temp;
end
outStruct = struct('Verts', verts, ...
'Normals', normals, ...
'Faces', Faces);
% patch('Faces', Faces, ...
% 'Vertices', verts, ...
% 'FaceVertexCData', [0 0 0], ...
% 'VertexNormals', normals, ...
% 'FaceColor', [1 0 0]);
% set(gca, 'DataAspectRatio', [1 1 1]);
end