nX=40; % number of grid pts along x, y and z
nY=40;
nZ=20;

mapFid = fopen('map.dat', 'w+'); % file in which material map will be written

x0 = nX/4; % upper left corner of hexagon in rectangular coords
y0 = 3*nY/4;
z0 = nZ/2;

a = 10; % pyramid base length
h = 3; % height 

for x = 1:nX % loop over all three dimensions
   for y = 1:nY
      for z = 1:nZ
   if (
        ((x>= x0 && x < x0+a) && (y<=y0 && y >y0-a))
     || ((x>= x0+a-1) && (x<x0+2*a-1) && (y<=y0-a+1) && (y>y0-2*a+1))
     || ((abs(x-x0)  > abs(y0-y)) && ((x-x0) < (a + (y0-y)) && (x<x0+2*a-1)) && (x > x0) && (y < y0))
     || ((abs(y0-y)  > abs(x-x0)) && ((y0-y) < (a + (x-x0)) && (y>y0-2*a+1)) && (y < y0) && (x > x0))
       ) && (z >= z0) && (z < z0+h) m1 = 1;
      else m1 = 0;
      endif
      m2 = 1 - m1; % ensure that m1 + m2 = 1 for all x,y,z
  
      fprintf(mapFid, '%f   %f\n', m1, m2); % write map file

      endfor
   endfor
endfor

fclose(mapFid);

