gusucode.com > 三维模仿源码程序 > 三维模仿源码程序/MathRubik2/RotateLayer.m
function CubeOut = RotateLayer(varargin) % RotateLayer - Rotates a layer, with or without animation % [Cube = ]RotateLayer(hAxes,Cube,Axe,Side,Direction[,Anime]) % or % ... RotateLayer(<rot-list>[,Anim[,hAxes[,Cube]]]) % with <rot-list> : % a string, a string like - R2(FD')2 % a matrix with two columns : [Color-number rotations] % a matrix with two columns : [Axe Side Direction] cmd=0; Anime=1; hAxes=[]; Cube=[]; if ischar(varargin{1}) if nargin>1 Anime=varargin{2}; if nargin>2 hAxes=varargin{3}; if nargin>3 Cube=varargin{4}; end end end switch lower(varargin{1}) case 'back' cmd=10; case 'backall' cmd=11; otherwise list=string2hist(varargin{1}); cmd=1; Axs=list(:,1); Sids=list(:,2); Dirs=list(:,3); end elseif length(varargin{1})>1 if nargin>2 hAxes=varargin{3}; if nargin>3 Cube=varargin{4}; end end if size(varargin{1},2)==2 % color space if isempty(Cube) Cube=FindRubikCube; end Axe=Color2FRU(varargin{1},Cube); elseif size(varargin{1},2)~=3 error('Wrong use of this function') else Axe=varargin{1}; end if nargin>1 Anime=varargin{2}; end cmd=2; Axs=Axe(:,1); Sids=Axe(:,2); Dirs=Axe(:,3); else hAxes=varargin{1}; Cube=varargin{2}; Axs=varargin{3}; Sids=varargin{4}; Dirs=varargin{5}; if nargin>5 Anime=varargin{6}; end end if isempty(hAxes)&Anime hAxes=FindRubikAxes; end if isempty(Cube) if isempty(hAxes) hAxes=FindRubikAxes; end Cube=get(hAxes,'UserData'); end %!!!!!!! if ~isempty(Cube.texture)&Anime==1 Anime=-1; end %!!!!!!! D=Cube.history; if cmd==10 if ~isempty(Cube.sol) showsol back return end if isempty(D) error('No back!!!') end Axs=D(end,1); Sids=D(end,2); Dirs=-D(end,3); D(end,:)=[]; elseif cmd==11 if ~isempty(Cube.sol) showsol backall return end if isempty(D) error('No back!!!') end Axs=D(end:-1:1,1); Sids=D(end:-1:1,2); Dirs=-D(end:-1:1,3); D=zeros(0,3); end Axs(Dirs==0)=[]; Sids(Dirs==0)=[]; Dirs(Dirs==0)=[]; if cmd<10 if isempty(D) D=[Axs Sids Dirs]; else D(end+1:end+length(Axs),:)=[Axs Sids Dirs]; end end Cube.history=D; for iAxe=1:length(Axs) Axe=Axs(iAxe); Side=Sids(iAxe); Direction=Dirs(iAxe); % to rotate layer ind1 = 2*Axe-1+(Side+1)/2; if Anime>0 % to show animation MyCube = Cube; % copy object a1 = Axe; % 1st axe a2 = Axe+1; if a2>3, a2 = 1; end % 2nd axe a3 = 6-a1-a2; % 3rd axe cosa = cos(-Direction*Side*pi/36); % cos(+-1 degree) sina = sin(-Direction*Side*pi/36); % sin(+-1 degree) T = zeros(3); T(a1,a1) = 1; T(a2,a2) = cosa; T(a2,a3) = sina; T(a3,a2) = -sina; T(a3,a3) = cosa; for k1=1:18, % algle loop for k2=1:9, % small cube loop MyCube.Nodes(:,:,Cube.RotLayerCube(ind1,k2)) = ... MyCube.Nodes(:,:,Cube.RotLayerCube(ind1,k2))*T; end PlotCube(hAxes,MyCube,1,0); drawnow; end end ind = 2*Axe-1+(1+Side*sign(Direction))/2; % index number for i=1:abs(Direction) MyColor = Cube.Color; for k1=1:9, % small cube loop for k2=1:6, % flat loop MyColor(Cube.RotCubeFlat(ind,k2),Cube.RotCubeCube(ind,Cube.RotLayerCube(ind1,k1))) = ... Cube.Color(k2,Cube.RotLayerCube(ind1,k1)); end end Cube.Color = MyColor; % change colors end if Anime % to show animation PlotCube(hAxes,Cube,Anime>0,1); end end if nargout CubeOut=Cube; elseif ~isempty(hAxes) set(hAxes,'UserData',Cube) end