%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Name: wholeSpace.m
% Author: J. Kristoffer Hultgren
%
% Description:
%
% Returns coordinates for the whole plane p=0,
% where p is one of the characters x y z defining which
% axis is zero, and where the axes goes from -lim to lim.
%
% Input:  p     - Non existing plane
%         lim   - Plane limits
%
% Output: x,y,z - Coordinates of resulting plane
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [x, y, z] = wholeSpace(p,lim)

flagg = 0;

if lim >= 100
    lim = lim/10;
    flagg = 1;
end

if lim >= 100
    lim = lim/10;
    flagg = 2;
end

res = lim*2 - 1;


if p == 'x'

    whole = zeros((res)^2, 3);
    n = -(lim-1);
    q = 1;
    while n < (res+1)/2
        m = -(lim-1);
        while m < (res+1)/2
            whole(q,3) = m;
            whole(q,2) = n;
            m = m+1;
            q = q+1;
        end
        n = n+1;
    end
    
    if flagg == 1
        whole = whole.*10;
    end
    
    if flagg == 2
        whole = whole.*100;
    end

    whole = whole.*1000;
    x = whole(:,1);
    y = whole(:,2);
    z = whole(:,3);

    M = [x y z];
    
    dlmwrite('space.dat',M,' ');
    
    
end


if p == 'y'

    whole = zeros((res)^2, 3);
    n = -(lim-1);
    q = 1;
    while n < (res+1)/2
        m = -(lim-1);
        while m < (res+1)/2
            whole(q,3) = m;
            whole(q,1) = n;
            m = m+1;
            q = q+1;
        end
        n = n+1;
    end
    
    if flagg == 1
        whole = whole.*10;
    end
    
    if flagg == 2
        whole = whole.*100;
    end

    whole = whole.*1000;
    x = whole(:,1);
    y = whole(:,2);
    z = whole(:,3);

    M=[x y z];

    dlmwrite('space.dat',M,' ');
    
end


if p == 'z'

    whole = zeros((res)^2, 3);
    n = -(lim-1);
    q = 1;
    while n < (res+1)/2
        m = -(lim-1);
        while m < (res+1)/2
            whole(q,2) = m;
            whole(q,1) = n;
            m = m+1;
            q = q+1;
        end
        n = n+1;
    end
    
    if flagg == 1
        whole = whole.*10;
    end
    
    if flagg == 2
        whole = whole.*100;
    end

    whole = whole.*1000;
    x = whole(:,1);
    y = whole(:,2);
    z = whole(:,3);

    M=[x y z];

    dlmwrite('space.dat',M,' ');
    
end

% Adds initiating command at top of file
!sed -i 1i"#START" space.dat