Code covered by the BSD License  

Highlights from
plot3AniAnaglyph

image thumbnail

plot3AniAnaglyph

by

 

20 Apr 2011 (Updated )

Animated anaglyph (red/cyan 3D) plot3.

plot3AniAnaglyph(S,params)
function plot3AniAnaglyph(S,params)

%PLOT3ANIANAGLYPH    Animated anaglyph (red/cyan 3D) plot3.
%   plot3AniAnaglyph(S,params) produces an anaglyph plot3 animation of
%   the of arguments in cartesian coordinates S = [X,Y,Z].
%
%   Demo can be produced with no input arguments, i.e. plot3AniAnaglyph().
%
%   The params argument determines animation and anaglyph parameters:
%
%   Field:                  Description:                    Default value:
%   params.lineWidth        Line width                      1
%   params.viewAngle        Viewing angle                   [40 17.5]
%   params.depthDegree      3D depth degree                 3
%   params.nFrames          # frames to step when evolving data 200
%   params.bgColor          Background color                'w'
%   params.animate          Animation on / off              1
%   params.nRotations       # of rotations about axes       [1 1 1]
%   params.dotsOrLine       plot dots or lines              '-'
%   params.threeD           3D on / off                     1
%
%   Make sure to wear glasses such that your left eye looks through the red
%   glass and your right eye looks through the cyan glass.
%
%   See also plot3

if nargin < 2
    params.lineWidth = 1;
    params.viewAngle = [40 17.5];
    params.depthDegree = 3;
    params.nFrames = 200;
    params.bgColor = 'w';
    params.animate = 1;
    params.nRotations = [1 1 1];
    params.dotsOrLine = '-';
    params.threeD = 1;
end

switch nargin
    case 0 % Demo
        S(:,3) = linspace(0,8*pi,400);
        S(:,1) = sin(S(:,3));
        S(:,2) = cos(S(:,3));
        
    case 1 % params set to default
        
    case 2 % params partially defined
        if ~isfield(params, 'lineWidth')
            params.lineWidth = 1;
        end
        
        if ~isfield(params, 'viewAngle')
            params.viewAngle = [40 17.5];
        end
        
        if ~isfield(params, 'depthDegree')
            params.depthDegree = 3;
        end
        
        if ~isfield(params, 'nFrames')
            params.nFrames = 200;
        end
        
        if ~isfield(params, 'bgColor')
            params.bgColor = 'w';
        end
        
        if ~isfield(params, 'animate')
            params.animate = 1;
        end
        
        if ~isfield(params, 'nRotations')
            params.nRotations = [1 1 1];
        end
        
        if ~isfield(params, 'line')
            params.line = 0;
        end
        
        if ~isfield(params, 'dotsOrLine')
            params.dotsOrLine = '-';
        end
        
        if ~isfield(params, 'threeD')
            params.dotsOrLine = 1;
        end
        
    otherwise
        error('plot3AniAnaglyph is undefined for %d input arguments.', nargin)
end

if strcmp(params.bgColor, 'k')
    leftColor = 'c';
    rightColor = 'r';
    axesColor = 'w';
elseif strcmp(params.bgColor, 'w')
    leftColor = 'r';
    rightColor = 'c';
    axesColor = 'k';
else
    error('Background color must be ''w'' or ''k''.')
end

rotX = @(w) [1 0 0;0 cos(w) -sin(w);0 sin(w) cos(w)];
rotY = @(w) [cos(w) 0 sin(w);0 1 0;-sin(w) 0 cos(w)];
rotZ = @(w) [cos(w) -sin(w) 0;sin(w) cos(w) 0;0 0 1];

%% Normalize XYZ to 1x1x1 Cube centered at (0,0,0)
Xmax = max(S(:,1));
Xmin = min(S(:,1));
Xmid = (max(S(:,1)) + min(S(:,1)))/2;
Ymax = max(S(:,2));
Ymin = min(S(:,2));
Ymid = (max(S(:,2)) + min(S(:,2)))/2;
Zmax = max(S(:,3));
Zmin = min(S(:,3));
Zmid = (max(S(:,3)) + min(S(:,3)))/2;

S(:,1) = 2*(S(:,1) - Xmid)/(Xmax - Xmin);
S(:,2) = 2*(S(:,2) - Ymid)/(Ymax - Ymin);
S(:,3) = 2*(S(:,3) - Zmid)/(Zmax - Zmin);

%% Plot
close all
figure('Position',get(0,'ScreenSize'))
for i = 1:ceil(length(S(:,1))/params.nFrames):length(S(:,1))
    
    if params.animate
        I = i;
    else
        I = length(S(:,1));
    end
    
    w = i/length(S(:,1))*2*pi*params.nRotations;
    Si = S(1:I,:)*rotX(w(1))*rotY(w(2))*rotZ(w(3));
    
    SLeft = Si*rotZ(pi*params.depthDegree/180);
    SRight = Si*rotZ(-pi*params.depthDegree/180);
    
    clf
    hold on
    %     title('3D Animation!','FontSize',14,'Color',axesColor)
    if params.threeD == 1;
        plot3(SLeft(:,1),SLeft(:,2),SLeft(:,3),params.dotsOrLine,...
            'Color',leftColor,'LineWidth',params.lineWidth);
        plot3(SRight(:,1),SRight(:,2),SRight(:,3),params.dotsOrLine,...
            'Color',rightColor,'LineWidth',params.lineWidth);
        plot3(SLeft(end,1),SLeft(end,2),SLeft(end,3),...
            'o','MarkerEdgeColor',leftColor,'MarkerSize',15);
        plot3(SRight(end,1),SRight(end,2),SRight(end,3),...
            'o','MarkerEdgeColor',rightColor,'MarkerSize',15);
    else
        plot3(Si(:,1),Si(:,2),Si(:,3),params.dotsOrLine,...
            'Color','k','LineWidth',params.lineWidth);
        plot3(Si(end,1),Si(end,2),Si(end,3),...
            'o','MarkerEdgeColor','k','MarkerSize',15);
    end
    axis on
    axis equal
    axis([-1 1 -1 1 -1 1])
    grid on
    view(params.viewAngle)
    camproj('perspective');
    
    set(gca,'XTick',-1:0.5:1,'YTick',-1:0.5:1,'ZTick',-1:0.5:1);
    set(gcf,'Color',params.bgColor)
    set(gca,'Color',params.bgColor)
    set(gca,'XColor',axesColor,'YColor',axesColor','ZColor',axesColor)
    
    axis off
    hold off
    drawnow
    
end % end for loop

end % end plot3AniAnaglyph function

Contact us