function [ heightMap ] = FindHeightMap( fileName, normals )
%FINDHEIGHTMAP Summary of this function goes here
%   Detailed explanation goes here

    mask = imread(strcat(fileName,'.mask.bmp'));

    height = size(normals,1);
    width = size(normals,2);

    % Declare matrices for SVD
    A = sparse([],[],[],2 * height * width, height * width, 4 * height * width);
    b = zeros(2 * width * height,1);
    
    bRow = 1;
    for i=1:height-1
        for j=1:width-1
            %if normals(i,j,1) > 0 && normals(i,j,2) > 0 && normals(i,j,3) > 0
                A(bRow,(i - 1) * width + j) = -normals(i,j,3);
                A(bRow,(i - 1) * width + j + 1) = normals(i,j,3);
                A(bRow+1,(i - 1) * width + j) = -normals(i,j,3);
                A(bRow+1,(i - 1) * width + j + width) = normals(i,j,3);

                b(bRow) = -normals(i,j,1);
                b(bRow+1) = -normals(i,j,2);

                bRow = bRow + 2;
            %end
        end
    end
    
    
    

    %SVD Fml
    x = A\b;
    
    heightMap = zeros(height,width);
    
    minValue = min(x);
    minValue = min(minValue);
    
    count = 1;
    for i=1:height
        for j=1:width
            if mask(i,j,1) > 0
                heightMap(i,j) = x(count) - minValue;
            end
            count = count + 1;
        end
    end
    
end

