using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EdgeDetector
{
    class KernelGaussian1deriv
    {
        public static double[] getKernel(int scale, int width , int deriv)
            //deriv urcuje stupen derivacie gauss. filtra
        {
            int sigma = scale;
            //naplnenie pola range hodnotami 1 az dlzka jadra
            double[] range = new double[2 * width * sigma + 1];
            for (int i = 0; i < range.Length; i++)
            {
                range[i] = i+1;
                Console.WriteLine(range[i] + " " );
            }
            Console.WriteLine("count:   " + range.Length);
            
            double center = range[((range.Length - 1) / 2)];

            //vypocet hodnot Gauss. jadra
            // matlab kod: kernel = (1 / (sigma * sqrt(2 * pi))) 
            //* exp(-((range - center).^ 2) / (2 * sigma ^ 2))
            //.* (-(range - center) / (sigma ^ 2));

            //vypocet kernel je dost zlozity, preto som rozdelil na kernel1,2 a 3
            double[] rangecentered = new double[range.Length];
            for (int i = 0; i < range.Length; i++)
            {
                rangecentered[i] = range[i] - center;
            }

            //kernel1
            double kernel1 = (1 / (sigma * Math.Sqrt(2 * Math.PI)));

            //kernel2
            double[] kernel2 = new double[range.Length];
            for (int i = 0; i < kernel2.Length; i++)
            {
                kernel2[i] = Math.Exp(   - (rangecentered[i] * rangecentered[i])  /  (2 * sigma * sigma)  );
            }

            //kernel3
            double[] kernel3 = new double[range.Length];
            for (int i = 0; i < kernel3.Length; i++)
            {
                kernel3[i] = ( - rangecentered[i] / (sigma * sigma) );
            }


            //kernel
            
            double[] kernel = new double[range.Length];
            if (deriv == 1)//prva derivacia gauss. filtra
            {
                for (int i = 0; i < kernel.Length; i++)
                {
                    kernel[i] = kernel2[i] * kernel3[i] * kernel1;
                }
            }
            else //nulta derivacia gauss. filtra
            {
                for (int i = 0; i < kernel.Length; i++)
                {
                    kernel[i] = kernel2[i] * kernel1;
                }

            }
            return kernel;
        }

  

    }
}
