forked from jamesra/VikingPlot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHemispherePatch.m
94 lines (71 loc) · 2.64 KB
/
HemispherePatch.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
function [ verts,normals,Faces ] = HemispherePatch( numPts, radius, axis )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
axis = axis ./ norm(axis);
RhoStep = (pi/2) / ((numPts/2)+1);
ThetaStep = (2*pi) / numPts;
numVerts = numPts * (numPts / 2) + 1
verts = zeros(numVerts,3);
normals = zeros(numVerts,3);
iVert = 1;
for(rho = pi/2:-RhoStep:0+RhoStep)
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];
[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;
%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;
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 NextIndexOnCircle + ZOffset iTheta + ((iZ+1) * numPts)];
iFace = iFace + 1;
Faces(iFace, :) = [NextIndexOnCircle + ZOffset NextIndexOnCircle + ((iZ+1) * numPts) iTheta + ((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 1+ZOffset iCap];
else
Faces(iFace,:) = [i+ZOffset i+1+ZOffset iCap];
end
iFace = iFace + 1;
end
% patch('Faces', Faces, ...
% 'Vertices', verts, ...
% 'FaceVertexCData', [0 0 0], ...
% 'VertexNormals', normals, ...
% 'FaceColor', [1 0 0]);
% set(gca, 'DataAspectRatio', [1 1 1]);
end