-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscatter3sph.m
95 lines (86 loc) · 2.65 KB
/
scatter3sph.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
function scatter3sph(X,Y,Z,varargin)
%SCATTER3SPH (X,Y,Z) Plots a 3d scatter plot with 3D spheres
% SCATTER3SPH is like scatter3 only drawing spheres with volume, instead
% of flat circles, at coordinates specified by vectors X, Y, Z. All three
% vectors have to be of the same length.
% SCATTER3SPH(X,Y,Z) draws the spheres with the default size and color.
% SCATTER3SPH(X,Y,Z,'size',S) draws the spheres with sizes S. If length(S)= 1
% the same size is used for all spheres.
% SCATTER3SPH(X,Y,Z,'color',C) draws the spheres with colors speciffied in a
% N-by-3 matrix C as RGB values.
% Parameter names can be abreviated to 3 letters. For example: 'siz' or
% 'col'. Case is irrelevant.
%
% Example
% %Coordinates
% X= 100*rand(9,1); Y= 100*rand(9,1); Z= 100*rand(9,1);
%
% %Colors: 3 blue, 3 red and 3 green
% C= ones(3,1)*[0 0 1];
% C= [C;ones(3,1)*[1 0 0]];
% C= [C;ones(3,1)*[0 1 0]];
%
% %Spheres sizes
% S= 5+10*rand(9,1);
%
% figure(1);
% scatter3sph(X,Y,Z,'size',S,'color',C);
% axis equal
% axis tight
% view(125,20);
% grid ON
%-- Some checking...
if nargin < 3, error 'Need at least three arguments', end
X = X(:);
Y = Y(:);
Z = Z(:);
if mean([length(X),length(Y),length(Z)]) ~= length(X), error 'Imput vectors X, Y, Z are of different lengths', end
%-- Defaults
C = ones(length(X),1)*[0 0 1];
S= 0.01*range([X;Y;Z])*ones(length(X),1);
% S = 0.01*ones(length(X),1);
%BW 2015
% SScaling = [1/range(X) 1/range(Y) 1/range(Z)];
% SScaling(SScaling>1) = 1;
SScaling = [1 1 1];
%-- Extract optional arguments
for j= 1:2:length(varargin)
string= lower(varargin{j});
switch string(1:min(3,length(string)))
case 'siz'
S= varargin{j+1};
SScaling = [1 1 1];
if length(S) == 1
S= ones(length(X),1)*S;
elseif length(S) < length(X)
error 'The vector of sizes must be of the same length as coordinate vectors (or 1)'
end
case 'col'
C= varargin{j+1};
if size(C,2) < 3 error('Colors matrix must have 3 columns'); return; end
if size(C,1) == 1
C= ones(length(X),1)*C(1:3);
elseif size(C,1) < length(X)
error 'Colors matrix must have the same number of rows as length of coordinate vectors (or 1)'
end
otherwise
error 'Unknown parameter name. Allowed names: ''size'', ''color'' '
end
end
%-- Sphere facets
[sx,sy,sz]= sphere(20);
%-- Plot spheres
hold on
for j= 1:length(X)
% surf(sx*S(j)+X(j), sy*S(j)+Y(j), sz*S(j)+Z(j),...
%note scaling - BW2015
surf(SScaling(1)*sx*S(j)+X(j), SScaling(2)*sy*S(j)+Y(j), SScaling(3)*sz*S(j)+Z(j),...
'LineStyle','none',...
'AmbientStrength',0.7,...
'FaceColor',C(j,:),...
'SpecularStrength',0.2,...
'DiffuseStrength',0.3,...
'FaceAlpha',1.0,...
'SpecularExponent',1);
end
view(30,15)