PZFlex Table File Writer

by

 

Creates .FLXTBL files from TIFF images - imports into PZFlex as a model

write_flex_material_table.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%   Title:          Write Flex Material Table
%
%   Description:    Writes a table of coordinates and material values to be
%                   read into Flex.
%   Author:         WAI
%   Date:           01/03/2012
%   Version:        1.1                                                   
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

close all;
clear;
clc;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Inputs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% General
JobName = 'AB'; % Image filename
% 0 = load predefined clourmap, 1 = select your own colour map
ColourFlag = 1;         

% Image
ImageFileName = [JobName '.tif'];

% Grid - The size of elements to break image into for PZFlex
XMax = 100e-3;
YMax = 90e-3;
BoxSize = 200e-6;

% Materials
NumMaterials = 7;
MaterialNames = cell(NumMaterials,1);

% Paste material names - Material names must be same as in PZFlex file
MaterialNames{1} = 'air';
MaterialNames{2} = 'pzt5a';
MaterialNames{3} = 'pzt5h';
MaterialNames{4} = 'watr';
MaterialNames{5} = 'pmt3';
MaterialNames{6} = 'stst';
MaterialNames{7} = 'alum';
%MaterialNames{8} = 'purp';
%MaterialNames{9} = 'gray';
%MaterialNames{10} = 'gree';
%MaterialNames{11} = 'whit';
%MaterialNames{12} = 'stst';
%MaterialNames{13} = 'void';

% Output
TableFileName = [JobName '.flxtbl'];
%TableFileName = 'rand.flxtbl';

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Create grid
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Get vectors
XVector = (0 : BoxSize : XMax)';
YVector = (0 : BoxSize : YMax)';

% Get lengths
NumXNodes = length(XVector);
NumYNodes = length(YVector);



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Read image and scale
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Read file, ensure expressed as double
RawImage = double(imread(ImageFileName));

% Get image size
[ImSizeX ImSizeY Dum] = size(RawImage);

% Create raw image scales
ImVectX = linspace(0,XMax,ImSizeX);
ImVectY = linspace(0,XMax,ImSizeY);
[ImGridX ImGridY] = meshgrid(ImVectX,ImVectY);

% Create element scales (not nodal!)
NodeVectX = linspace(BoxSize / 2,XMax - BoxSize / 2,NumXNodes - 1);
NodeVectY = linspace(BoxSize / 2,XMax - BoxSize / 2,NumYNodes - 1);
[NodeGridX NodeGridY] = meshgrid(NodeVectX,NodeVectY);

% Interpolate image
InterpImage(:,:,1) = interp2(ImGridX,ImGridY,RawImage(:,:,1)',NodeGridY,NodeGridX);
InterpImage(:,:,2) = interp2(ImGridX,ImGridY,RawImage(:,:,2)',NodeGridY,NodeGridX);
InterpImage(:,:,3) = interp2(ImGridX,ImGridY,RawImage(:,:,3)',NodeGridY,NodeGridX);




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Material names and colour values
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Select or load colourmap
if ColourFlag == 1

    % Plot image
    figure;
    image(uint16(RawImage)); 
    title('Original');
    axis image;
    
    % Ask user for colour locations
    [XClk YClk] = ginput(NumMaterials);
    
    % Preallocate
    colourMap = zeros(NumMaterials,3);
    
    % Get colour values
    for Index = 1 : NumMaterials
        
        % Get colour value
        colourMap(Index,:) = squeeze(RawImage(round(YClk(Index)),round(XClk(Index)),:))';
        
    end;
    
    % Save map
    save('colour_map.mat','colourMap');

else
    
    % Load file
    load('colour_map.mat');
    
end;



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Generate material map
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Quantise colour values to closest material
MatGrid = quantise_image(InterpImage,colourMap);
NumMatGridPoints = length(MatGrid(:));

% Flip mat grid to read in correctly
MatGrid = flipud(MatGrid)';

% % TEST
% MatGrid = round((NumMaterials - 1) * rand(NumYNodes-1,NumXNodes-1)) + 1;
%MatGrid = round((11 - 1) * rand(NumYNodes-1,NumXNodes-1)) + 1;
%MatGrid = 12*ones(NumXNodes-1,NumYNodes-1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Write file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Open file
Fid = fopen(TableFileName,'wt');
NumCoordLine = 12;
NumMatLine = 20;

% Create header
Header{1} = 'hedr 0\n';
Header{2} = 'info 1\n';
        
% Write header
for Index = 1 : length(Header)
    
    fprintf(Fid,Header{Index});
    
end;

% Create conversion strings
CStringF = ['%0.4f' repmat('% 0.4f',1,NumCoordLine - 2) '% 0.4f\n'];
CStringD = ['%d' repmat('% d',1,NumMatLine - 2) '% d\n'];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Create and write X
fprintf(Fid,['xcrd ' num2str(NumXNodes) '\n']);
NumLines = floor(NumXNodes / NumCoordLine);

% Preallocate end
End = 0;

% Loop through full lines
for Index = 1 : NumLines
    
    % Get start / end indices
    End = Index * NumCoordLine;
    Start = End - NumCoordLine + 1;
    
    % Write
    fprintf(Fid,CStringF,XVector(Start:End));
    
end;

% Calculate number left
NumLeft = NumXNodes - End;

% Do partial line if any left
if NumLeft > 1
    
    % Format
    CStringEndX = ['%0.4f' repmat('% 0.4f',1,NumLeft -  2) '% 0.4f\n'];
    
    % Write
    fprintf(Fid,CStringEndX,XVector(End + 1:NumXNodes));
    
elseif NumLeft == 1
    
    % Format
    CStringEndX = '%0.4f\n';
    
    % Write
    fprintf(Fid,CStringEndX,XVector(NumXNodes));
    
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Create and write Y
fprintf(Fid,['ycrd ' num2str(NumYNodes) '\n']);
NumLines = floor(NumYNodes / NumCoordLine);

% Loop through full lines
for Index = 1 : NumLines
    
    % Get start / end indices
    End = Index * NumCoordLine;
    Start = End - NumCoordLine + 1;
    
    % Write
    fprintf(Fid,CStringF,YVector(Start:End));
    
end;

% Calculate number left
NumLeft = NumYNodes - End;

% Do partial line if any left
if NumLeft > 1
    
    % Format
    CStringEndY = ['%0.4f' repmat('% 0.4f',1,NumLeft - 2) '% 0.4f\n'];
    
    % Write
    fprintf(Fid,CStringEndY,YVector(End + 1:NumYNodes));
    
elseif NumLeft == 1
    
    % Format
    CStringEndY = '%0.4f\n';
    
    % Write
    fprintf(Fid,CStringEndY,YVector(NumYNodes));
    
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Create and write materials
fprintf(Fid,['name ' num2str(NumMaterials) '\n']);

% Get material name string
NameString = [];
for Index = 1 : NumMaterials
    
    NameString = [NameString ' ' MaterialNames{Index}];    

end;

% Write material names
NameString(1) = [];
fprintf(Fid,[NameString '\n']);
fprintf(Fid,['matr ' num2str(NumMatGridPoints) '\n']);

% Calculate amterial lines
NumLines = floor(NumMatGridPoints / NumMatLine);

% Loop through full lines
for Index = 1 : NumLines
    
    % Get start / end indices
    End = Index * NumMatLine;
    Start = End - NumMatLine + 1;
    
    % Write
    fprintf(Fid,CStringD,MatGrid(Start:End));
    
end;

% Calculate number left
NumLeft = NumMatGridPoints - End;

% Do partial line if any left
if NumLeft > 1
    
    % Format
    CStringEndG = ['%d' repmat('% d',1,NumMatLine - 2) '% d\n'];
    
    % Write
    fprintf(Fid,CStringEndG,MatGrid(End + 1 : NumMatGridPoints));
    
elseif NumLeft == 1
    
    % Format
    CStringEndG = '%d\n';
    
    % Write
    fprintf(Fid,CStringEndG,MatGrid(NumMatGridPoints));
    
end;
    
% Close file
fclose(Fid);



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Plot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Plot original image
% figure;
% image(uint8(RawImage));
% title('Original');
% axis image;

% Plot meshed image
figure;
image(uint8(InterpImage));
title('Meshed');
axis image;

Contact us