%% Extrusions
%
% This example demonstrates the use of extrusions - objects that are formed
% by extruding a 'profile' (simple 2D polygon) along a 'spine' (polyline in
% 3D space).

%   Copyright 1998-2021 HUMUSOFT s.r.o.

%% Initialize the 3D Foundation object
FW = F3D.World(mfilename);
initialize(FW, [0 -10 5]);

% Set the viewport properties
VP = FW.Viewports.Main;
VP.Title = mfilename;
Path = fileparts(which(mfilename));
setLogo(VP, fullfile(Path, 'data', 'MatlabLogo.png'));

F3D.Geometry.templateFloor(FW.Root, [10 10 0.1]);


%% Waves

% Custom profile
Alpha = 0:10:360;
Profile = [cosd(Alpha).*(1 + sind(Alpha*8)/10); sind(Alpha).*(1 + sind(Alpha*8)/10)]'/100;

for i = 1:10
    x = 1:10;
    y = [0 rand(1, 8) + 1 0];
    X = 1:0.1:10;
    Y = pchip(x, y, X);
    Spine = [X' X' Y'].*[0.5 0 2];

    W = addChild(FW.Root);
    createShape(W, 'extrusion', Spine, 10, Profile);
    W.Color = [i 10-i 0]/10;
    W.Shininess = 0.7;
    W.Location = [-2.5 i*0.4 0];
end


%% Spring
S = addChild(FW.Root, 'Spring');
Alpha = 0:5:360*8;
Spine = [sind(Alpha)' -cosd(Alpha)' Alpha'/1000];
Scale = 5 + 12*sin(Alpha/Alpha(end)*pi)';   % profile*S
% Scale = [Alpha'*0 + 15 15*sin(Alpha/Alpha(end)*pi)'];   % profile.*[X Y]
createShape(S, 'extrusion', Spine, Scale/100);
S.Color = [1 0.5 0.2];
S.Shininess = 0.8;
S.Location = [-2 -2 0];

%% Revolution
z = linspace(0, 3);
x = z/2 + sind(z*200)*0.3;
R = addChild(FW.Root, 'Revolution');
createShape(R, 'Revolution', [z' x'], 32);
R.TwoSided = true;
R.Color = [0.2 0.6 1];
R.Location = [2 -2 0];

%% Interpolate spring scale
pause(1);
interpolate(S, 'Scale', [1 1 2], 'hill', 4);