% % replace_block_matrix( mtxLS0, mtxRT0, mtxMR0, arrayRules, arrayRotes, arrayMirrs ) % % Applys a block replacement transform to a matrix, including n*pi/2 rotations and mirroring. % % Written as a utility for L_system_tiling.m. This function does no % sanity checks on dimensions, bounds, or number of arguments. % See L_system tiling for examples of bounds checking. % % USAGE: % arrayRules( 1:2, 1:2, 1 ) = [1 0; 0 1]; % arrayRules( 1:2, 1:2, 2 ) = [1 1; 0 0]; % arrayRotes = arrayRules; % arrayMirrs = arrayRules; % [arrayNext arrayNextRotes arrayNextMirrs] = % replace_block_matrix( [1; 0], [0; 1], [0; 0], arrayRules, arrayRotes, arrayMirrs ); % % ARGUMENTS: % % mtxLS0: L-system initial 2-D matrix, which follows the same % coding conventions as the replacement rules. % mtxRT0: Rotation codes. % mtxRT0: Mirror codes. % % % arrayRules: A set of L-system block replacement rules, n x m x nRules. . % arrayRotes: Rotation codes. % arrayMirrs: Mirror codes. % % RETURN VALUES: % % mtxOut: Matrix of integers of the output pattern. % mtxRT: Matrix of integers of the output rotations. % mtxMR: Matrix of integers of the output mirrors. % % HARDCODED: (see below 'function' statement) % % bShow: Show details for debugging, and show output matrix in a % figure window. % % % Mark Dow, August 25, 2009 % function [mtxOut mtxRT mtxMR] = replace_block_matrix( mtxLS0, mtxRT0, mtxMR0, arrayRules, arrayRotes, arrayMirrs ) %%%%%%%%%%%%%%%%%%%%%%%% % Hardcoded information: bShow = false; % %%%%%%%%%%%%%%%%%%%%%%%% % To Do: 2-argument version, no mirrors or rotations. % mtxLS0 % mtxRT0 % mtxMR0 % % arrayRules % arrayRotes % arrayMirrs szRules = size( arrayRules ); % To Do: If non-square system and rotations (and mirrors?) are non-zero, it % will fail. Insert reality check and abort with message. % Initialize matrices. szLS0 = size(mtxLS0); mtxOut( 1:szLS0(1)*szRules(1), 1:szLS0(2)*szRules(2) ) = 0; mtxRT( 1:szLS0(1)*szRules(1), 1:szLS0(2)*szRules(2) ) = 0; mtxMR( 1:szLS0(1)*szRules(1), 1:szLS0(2)*szRules(2) ) = 0; % Construct the next generation of the pattern. for j = 1 : szLS0(1) for k = 1 : szLS0(2) y1 = (j-1)*szRules(1) + 1 : j*szRules(1); x1 = (k-1)*szRules(2) + 1 : k*szRules(2); % Insert rotated replacement pattern. mtxOut( y1, x1 ) = rot90( arrayRules( 1:szRules(1), 1:szRules(2), mtxLS0( j, k ) + 1 ), ... -mtxRT0( j, k ) ); % Mirror replaced pattern. if mod( mtxMR0( j, k ), 2 ) == 1 mtxOut( y1, x1 ) = fliplr( mtxOut( y1, x1 ) ); end % Copy rotate and mirror rules. mtxRT( y1, x1 ) = mtxRT0( j, k ); mtxMR( y1, x1 ) = mtxMR0( j, k ); % Rotate rotate and mirror rules. mtxRT( y1, x1 ) = mtxRT( y1, x1 ) ... + rot90( arrayRotes( 1:szRules(1), 1:szRules(2), mtxLS0( j, k ) + 1 ), ... -mtxRT0( j, k ) ); % Mirror rotate and mirror rules. if mod( mtxMR0( j, k ), 2 ) == 1 mtxMR( y1, x1 ) = mtxMR( y1, x1 ) ... + fliplr( arrayMirrs( 1:szRules(1), 1:szRules(2), mtxLS0( j, k ) + 1 ) ); else mtxMR( y1, x1 ) = mtxMR( y1, x1 ) ... + arrayMirrs( 1:szRules(1), 1:szRules(2), mtxLS0( j, k ) + 1 ); end end end mtxRT = mod( mtxRT, 4 ); mtxMR = mod( mtxMR, 2 ); if bShow figure; imshow( mtxOut ) end