gusucode.com > 三维模仿源码程序 > 三维模仿源码程序/MathRubik2/string2hist.m
function list=string2hist(cmd) %string2hist - convert a string of commands to a numerical list % list=string2hist(cmd) % F(ront),R(ight),U(p) are axes 1,2,3 on positive sides % B(ack),L(eft),D(own) are these axes on negative sides % a list is something like % FR2(UR)3, which means Front (clockwise (direction 1)), double right % three times the combination of Up and Right % ("'" can also be used for 3 rotates clockwise - or reverse) % the list may be ended with ".", after which all characters are not used list=zeros(100,3); iL=0; S=cell(0,2); cmd(end+1)='.'; % sentinel iCmd=1; while cmd(iCmd)~='.' axe=0; switch lower(cmd(iCmd)) case 'f' axe=1; side=1; case 'r' axe=2; side=1; case 'u' axe=3; side=1; case 'b' axe=1; side=-1; case 'l' axe=2; side=-1; case 'd' axe=3; side=-1; case '''' if isempty(S) list(iL,3)=-list(iL,3); elseif S{end,2} % still open S{end,1}(end,3)=-S{end,1}(end,3); else S{end,1}=S{end,1}(end:-1:1,:); S{end,1}(:,3)=-S{end,1}(:,3); [S,list,iL]=TestEnd(S,list,iL); end case {'1','2','3','4','5','6','7','8','9'} i=iCmd+1; while any(cmd(i)=='0123456789') i=i+1; end n=str2num(cmd(iCmd:i-1)); if n>1 if isempty(S) list(iL+1:iL+n-1,:)=list(iL+zeros(1,n-1),:); iL=iL+n-1; elseif S{end,2} % still open m=size(S{end,1},1); S{end,1}(m+1:m+n-1,:)=S{end,1}(m+zeros(1,n-1),:); else S{end,1}=repmat(S{end,1},n,1); [S,list,iL]=TestEnd(S,list,iL); end end iCmd=i-1; case '(' [S,list,iL]=TestEnd(S,list,iL); S{end+1,1}=zeros(0,3); S{end,2}=1; case ')' [S,list,iL]=TestEnd(S,list,iL); S{end,2}=0; case {' ',','} % doe niets otherwise error(sprintf('Unknown character in command list (''%c'')',cmd(iCmd))) end if axe [S,list,iL]=TestEnd(S,list,iL); if isempty(S) iL=iL+1; list(iL,1)=axe; list(iL,2)=side; list(iL,3)=1; else S{end,1}(end+1,:)=[axe side 1]; end end iCmd=iCmd+1; end [S,list,iL]=TestEnd(S,list,iL); if ~isempty(S) error('Not a correct command list') end list=list(1:iL,:); function [S,list,iL]=TestEnd(S,list,iL) if ~isempty(S) if S{end,2}==0 if size(S,1)==1 n1=size(S{end,1},1); list(iL+1:iL+n1,:)=S{end,1}; iL=iL+n1; else S{end-1,1}=[S{end-1,1};S{end,1}]; end S(end,:)=[]; end end