// File: spatialfruitsalad[P]volume2position/./homogenator.hpp
// Function: void homogenator::Process<unsigned short>(tomogram3d<unsigned short>&, tomogram3d<unsigned short>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, double)
void Process (tomogram3d<VALUE>& matrix, tomogram3d<VALUE>& out, std::string foldername, double outside = 0.05)
    {
        std::cout << "radial homogenator " << std::endl;
        std::vector<double> vec_r;  // vector for radial bins
        std::vector<double> vec_mean;   // vector for mean values (same length as vec_r)
        std::vector<int> vec_counts;    // vector for voxel counts (same length as vec_r)

        unsigned nx = matrix.get_sx();  
        unsigned ny = matrix.get_sy();
        unsigned nz = matrix.get_sz();

        // create empty tomogram
        out.clear();
        out.initialise(nx,ny,nz,0);

        // get center for radial calculations
        unsigned xh = nx/2;
        unsigned yh = ny/2;

        // some geometry
        double rwidth = sqrt(xh*xh + yh*yh);    // half diagonal
        double rdelta = rwidth/(NumberOfBins+1);    // number of bins


        /// initialize vectors with empty values
        for (unsigned int i = 0 ; i != NumberOfBins; ++i)
        {
            double rmid = rdelta * static_cast<double>(i);  // radial bin position
            vec_r.push_back(rmid);                          
            vec_mean.push_back(0);
            vec_counts.push_back(0);
        }


        std::cout << "calculating mean values in " << NumberOfBins << " bins" << std::endl;
        // fill vectors
        for (unsigned int k = 0; k != nz; ++k)
        for (unsigned int j = 0; j != ny; ++j)
        for (unsigned int i = 0; i != nx; ++i)
        {
            // get bin 
            double r = sqrt( (i-nx/2)*(i-nx/2) + (j- ny/2)*(j-ny/2));
            unsigned idx = floor(r/rdelta);   

            // get value from tomogram
            VALUE v = matrix.get(i,j,k);
            
            // increase counters
            vec_mean[idx]+=v;
            vec_counts[idx]+=1;
        }

        std::cout << "normalize mean values " << std::endl;
        // correct for different voxel count numbers
        for (unsigned i = 0; i != vec_mean.size(); ++i)
        {
            vec_mean[i] /=static_cast<double>(vec_counts[i]);   
        }

        std::cout << "save homogenator.dat" << std::endl;
        // save output file
        {
        std::ofstream meanFile;
        std::string meanFileName = foldername + "/homogenator.dat";
        meanFile.open (meanFileName); 
        meanFile << "#rmid\tmean\tcounts" << std::endl;
        for (unsigned i = 0; i != vec_mean.size(); ++i)
        {
            meanFile << vec_r[i] << "\t" << vec_mean[i] << "\t" << vec_counts[i] << std::endl;
        }
        }

        // histogram class
        cSummerLinear summer(32);
        // maximum value: we want to be between 0 and vMAX/2 (not using the last bit to prevent intersample clipping in further calculations) 
        VALUE vMAX = std::numeric_limits<VALUE>::max();
        // adapt homogenatorization
        // and calculate histogram
        std::cout << "homogenize Tomogram" << std::endl;
        for (unsigned int k = 0; k != nz; ++k)
        for (unsigned int j = 0; j != ny; ++j)
        for (unsigned int i = 0; i != nx; ++i)
        {
            // omit outside voxels radially
            double r = sqrt( (i-nx/2)*(i-nx/2) + (j- ny/2)*(j-ny/2));
            if (r > 0.5*nx*(1.0 - outside))
            {
                out.set(i,j,k, 0);
                continue;
            }


            // homogenize tomogram
            unsigned idx = floor(r/rdelta);   
            double factor = 0;
            // no interpolation in the last bins (not interesting anyway)
            if( idx >= vec_mean.size() -2)
            {
                factor = vec_mean[idx];
            }
            else
            {
                // linear interpolation
                double inter = r/rdelta - idx;  // between 0 and 1
                factor = vec_mean[idx] * (1.0 - inter) + vec_mean[idx+1] * inter; 
            }

            // calculate new value
            VALUE v = matrix.get(i,j,k);
            VALUE newValue = static_cast<VALUE>(static_cast<double>(v) / factor *static_cast<double>(vMAX/2) );

            out.set(i,j,k,newValue);
            summer.Add(newValue);
        }
        {
        std::ofstream sumFile;
        std::string sumFileName = foldername + "/histogram_hom.dat";
        sumFile.open (sumFileName); 

        sumFile << summer;
        }

    }

// Assembly:
subq $0x658, %rsp            # imm = 0x658
movq %rcx, 0xb0(%rsp)
movq %rdi, 0x650(%rsp)
movq %rsi, 0x648(%rsp)
movq %rdx, 0x640(%rsp)
movq %rcx, 0x638(%rsp)
movsd %xmm0, 0x630(%rsp)
movq 0x2ba11(%rip), %rdi     # 0x33fc0
leaq 0x2056b(%rip), %rsi     # 0x28b21
callq 0x3290
movq %rax, %rdi
movq 0x2b9db(%rip), %rsi     # 0x33fa0
callq 0x32c0
leaq 0x618(%rsp), %rdi
callq 0x9270
leaq 0x600(%rsp), %rdi
callq 0x9270
leaq 0x5e8(%rsp), %rdi
callq 0x125d0
movq 0x648(%rsp), %rdi
callq 0x208b0
movl %eax, 0x5e4(%rsp)
movq 0x648(%rsp), %rdi
callq 0x208c0
movl %eax, 0x5e0(%rsp)
movq 0x648(%rsp), %rdi
callq 0x208d0
movl %eax, 0x5dc(%rsp)
movq 0x640(%rsp), %rdi
callq 0x7d50
movq 0x640(%rsp), %rdi
movl 0x5e4(%rsp), %eax
movl %eax, %esi
movl 0x5e0(%rsp), %eax
movl %eax, %edx
movl 0x5dc(%rsp), %eax
movl %eax, %ecx
xorl %r8d, %r8d
callq 0xee70
jmp 0x8667
movl 0x5e4(%rsp), %eax
shrl %eax
movl %eax, 0x5c8(%rsp)
movl 0x5e0(%rsp), %eax
shrl %eax
movl %eax, 0x5c4(%rsp)
movl 0x5c8(%rsp), %eax
imull %eax, %eax
movl 0x5c4(%rsp), %ecx
imull %ecx, %ecx
addl %ecx, %eax
cvtsi2sd %rax, %xmm0
callq 0x3250
movsd %xmm0, 0x5b8(%rsp)
movsd 0x5b8(%rsp), %xmm0
movsd 0x1f96f(%rip), %xmm1    # 0x28030
divsd %xmm1, %xmm0
movsd %xmm0, 0x5b0(%rsp)
movl $0x0, 0x5ac(%rsp)
cmpl $0x28, 0x5ac(%rsp)
je 0x8796
movsd 0x5b0(%rsp), %xmm0
movl 0x5ac(%rsp), %eax
cvtsi2sd %rax, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x5a0(%rsp)
leaq 0x618(%rsp), %rdi
leaq 0x5a0(%rsp), %rsi
callq 0x212b0
jmp 0x8720
movq $0x0, 0x598(%rsp)
leaq 0x600(%rsp), %rdi
leaq 0x598(%rsp), %rsi
callq 0x103b0
jmp 0x8743
movl $0x0, 0x594(%rsp)
leaq 0x5e8(%rsp), %rdi
leaq 0x594(%rsp), %rsi
callq 0x18310
jmp 0x8765
jmp 0x8767
movl 0x5ac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5ac(%rsp)
jmp 0x86d9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5d0(%rsp)
movl %eax, 0x5cc(%rsp)
jmp 0x913e
movq 0x2b823(%rip), %rdi     # 0x33fc0
leaq 0x20391(%rip), %rsi     # 0x28b35
callq 0x3290
movq %rax, 0xa8(%rsp)
jmp 0x87b3
movq 0xa8(%rsp), %rdi
movl $0x28, %esi
callq 0x3350
movq %rax, 0xa0(%rsp)
jmp 0x87cf
movq 0xa0(%rsp), %rdi
leaq 0x1fef0(%rip), %rsi     # 0x286ce
callq 0x3290
movq %rax, 0x98(%rsp)
jmp 0x87ed
movq 0x98(%rsp), %rdi
movq 0x2b7a4(%rip), %rsi     # 0x33fa0
callq 0x32c0
jmp 0x8803
movl $0x0, 0x590(%rsp)
movl 0x590(%rsp), %eax
cmpl 0x5dc(%rsp), %eax
je 0x89a7
movl $0x0, 0x58c(%rsp)
movl 0x58c(%rsp), %eax
cmpl 0x5e0(%rsp), %eax
je 0x898f
movl $0x0, 0x588(%rsp)
movl 0x588(%rsp), %eax
cmpl 0x5e4(%rsp), %eax
je 0x8977
movl 0x588(%rsp), %eax
movl 0x5e4(%rsp), %ecx
shrl %ecx
subl %ecx, %eax
imull %eax, %eax
movl 0x58c(%rsp), %ecx
movl 0x5e0(%rsp), %edx
shrl %edx
subl %edx, %ecx
imull %ecx, %ecx
addl %ecx, %eax
cvtsi2sd %rax, %xmm0
callq 0x3250
movsd %xmm0, 0x580(%rsp)
movsd 0x580(%rsp), %xmm0
movsd 0x5b0(%rsp), %xmm1
divsd %xmm1, %xmm0
callq 0x3430
cvttsd2si %xmm0, %rax
movl %eax, 0x57c(%rsp)
movq 0x648(%rsp), %rdi
movl 0x588(%rsp), %eax
movl %eax, %esi
movl 0x58c(%rsp), %eax
movl %eax, %edx
movl 0x590(%rsp), %eax
movl %eax, %ecx
callq 0xa350
movw %ax, 0x96(%rsp)
jmp 0x88f8
movw 0x96(%rsp), %ax
movw %ax, 0x57a(%rsp)
movzwl 0x57a(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x88(%rsp)
movl 0x57c(%rsp), %eax
movl %eax, %esi
leaq 0x600(%rsp), %rdi
callq 0x9d40
movsd 0x88(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movl 0x57c(%rsp), %eax
movl %eax, %esi
leaq 0x5e8(%rsp), %rdi
callq 0x12d40
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movl 0x588(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x588(%rsp)
jmp 0x884c
jmp 0x8979
movl 0x58c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58c(%rsp)
jmp 0x882d
jmp 0x8991
movl 0x590(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x590(%rsp)
jmp 0x880e
movq 0x2b612(%rip), %rdi     # 0x33fc0
leaq 0x2019c(%rip), %rsi     # 0x28b51
callq 0x3290
movq %rax, 0x80(%rsp)
jmp 0x89c4
movq 0x80(%rsp), %rdi
movq 0x2b5cd(%rip), %rsi     # 0x33fa0
callq 0x32c0
jmp 0x89da
movl $0x0, 0x574(%rsp)
movl 0x574(%rsp), %eax
movq %rax, 0x78(%rsp)
leaq 0x600(%rsp), %rdi
callq 0x10520
movq %rax, %rcx
movq 0x78(%rsp), %rax
cmpq %rcx, %rax
je 0x8a69
movl 0x574(%rsp), %eax
movl %eax, %esi
leaq 0x5e8(%rsp), %rdi
callq 0x12d40
cvtsi2sdl (%rax), %xmm0
movsd %xmm0, 0x70(%rsp)
movl 0x574(%rsp), %eax
movl %eax, %esi
leaq 0x600(%rsp), %rdi
callq 0x9d40
movsd 0x70(%rsp), %xmm1
movsd (%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movl 0x574(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x574(%rsp)
jmp 0x89e5
movq 0x2b550(%rip), %rdi     # 0x33fc0
leaq 0x200f1(%rip), %rsi     # 0x28b68
callq 0x3290
movq %rax, 0x68(%rsp)
jmp 0x8a83
movq 0x68(%rsp), %rdi
movq 0x2b511(%rip), %rsi     # 0x33fa0
callq 0x32c0
jmp 0x8a96
leaq 0x370(%rsp), %rdi
callq 0x3450
jmp 0x8aa5
movq 0xb0(%rsp), %rsi
leaq 0x200c9(%rip), %rdx     # 0x28b7d
leaq 0x350(%rsp), %rdi
callq 0x6100
jmp 0x8ac3
leaq 0x370(%rsp), %rdi
leaq 0x350(%rsp), %rsi
movl $0x10, %edx
callq 0x3140
jmp 0x8adf
leaq 0x200a8(%rip), %rsi     # 0x28b8e
leaq 0x370(%rsp), %rdi
callq 0x3290
movq %rax, 0x60(%rsp)
jmp 0x8afa
movq 0x60(%rsp), %rdi
movq 0x2b49a(%rip), %rsi     # 0x33fa0
callq 0x32c0
jmp 0x8b0d
movl $0x0, 0x34c(%rsp)
movl 0x34c(%rsp), %eax
movq %rax, 0x58(%rsp)
leaq 0x600(%rsp), %rdi
callq 0x10520
movq %rax, %rcx
movq 0x58(%rsp), %rax
cmpq %rcx, %rax
je 0x8c5e
movl 0x34c(%rsp), %eax
movl %eax, %esi
leaq 0x618(%rsp), %rdi
callq 0x9d40
movsd (%rax), %xmm0
leaq 0x370(%rsp), %rdi
callq 0x34e0
movq %rax, 0x50(%rsp)
jmp 0x8b70
movq 0x50(%rsp), %rdi
leaq 0x20024(%rip), %rsi     # 0x28ba0
callq 0x3290
movq %rax, 0x48(%rsp)
jmp 0x8b88
movl 0x34c(%rsp), %eax
movl %eax, %esi
leaq 0x600(%rsp), %rdi
callq 0x9d40
movq 0x48(%rsp), %rdi
movsd (%rax), %xmm0
callq 0x34e0
movq %rax, 0x40(%rsp)
jmp 0x8bb3
movq 0x40(%rsp), %rdi
leaq 0x1ffe1(%rip), %rsi     # 0x28ba0
callq 0x3290
movq %rax, 0x38(%rsp)
jmp 0x8bcb
movl 0x34c(%rsp), %eax
movl %eax, %esi
leaq 0x5e8(%rsp), %rdi
callq 0x12d40
movq 0x38(%rsp), %rdi
movl (%rax), %esi
callq 0x3470
movq %rax, 0x30(%rsp)
jmp 0x8bf4
movq 0x30(%rsp), %rdi
movq 0x2b3a0(%rip), %rsi     # 0x33fa0
callq 0x32c0
jmp 0x8c07
jmp 0x8c09
movl 0x34c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34c(%rsp)
jmp 0x8b18
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5d0(%rsp)
movl %eax, 0x5cc(%rsp)
jmp 0x8dd3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5d0(%rsp)
movl %eax, 0x5cc(%rsp)
leaq 0x350(%rsp), %rdi
callq 0x3538
jmp 0x8dd3
leaq 0x350(%rsp), %rdi
callq 0x3538
leaq 0x370(%rsp), %rdi
callq 0x3390
leaq 0x310(%rsp), %rdi
movsd 0x1f3b0(%rip), %xmm0    # 0x28038
callq 0x21320
jmp 0x8c8f
callq 0x1f060
movw %ax, 0x30e(%rsp)
movq 0x2b31d(%rip), %rdi     # 0x33fc0
leaq 0x1fef8(%rip), %rsi     # 0x28ba2
callq 0x3290
movq %rax, 0x28(%rsp)
jmp 0x8cb6
movq 0x28(%rsp), %rdi
movq 0x2b2de(%rip), %rsi     # 0x33fa0
callq 0x32c0
jmp 0x8cc9
movl $0x0, 0x308(%rsp)
movl 0x308(%rsp), %eax
cmpl 0x5dc(%rsp), %eax
je 0x9037
movl $0x0, 0x304(%rsp)
movl 0x304(%rsp), %eax
cmpl 0x5e0(%rsp), %eax
je 0x901f
movl $0x0, 0x300(%rsp)
movl 0x300(%rsp), %eax
cmpl 0x5e4(%rsp), %eax
je 0x9007
movl 0x300(%rsp), %eax
movl 0x5e4(%rsp), %ecx
shrl %ecx
subl %ecx, %eax
imull %eax, %eax
movl 0x304(%rsp), %ecx
movl 0x5e0(%rsp), %edx
shrl %edx
subl %edx, %ecx
imull %ecx, %ecx
addl %ecx, %eax
cvtsi2sd %rax, %xmm0
callq 0x3250
movsd %xmm0, 0x2f8(%rsp)
movsd 0x2f8(%rsp), %xmm0
movl 0x5e4(%rsp), %eax
cvtsi2sd %rax, %xmm2
movsd 0x1f2c6(%rip), %xmm1    # 0x28048
mulsd %xmm2, %xmm1
movsd 0x1f2b2(%rip), %xmm2    # 0x28040
subsd 0x630(%rsp), %xmm2
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x8dfe
movq 0x640(%rsp), %rdi
movl 0x300(%rsp), %eax
movl %eax, %esi
movl 0x304(%rsp), %eax
movl %eax, %edx
movl 0x308(%rsp), %eax
movl %eax, %ecx
xorl %r8d, %r8d
callq 0xf360
jmp 0x8dce
jmp 0x8ff1
leaq 0x370(%rsp), %rdi
callq 0x3390
jmp 0x913e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5d0(%rsp)
movl %eax, 0x5cc(%rsp)
jmp 0x9131
movsd 0x2f8(%rsp), %xmm0
movsd 0x5b0(%rsp), %xmm1
divsd %xmm1, %xmm0
callq 0x3430
cvttsd2si %xmm0, %rax
movl %eax, 0x2f4(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x2e8(%rsp)
movl 0x2f4(%rsp), %eax
movq %rax, 0x20(%rsp)
leaq 0x600(%rsp), %rdi
callq 0x10520
movq %rax, %rcx
movq 0x20(%rsp), %rax
subq $0x2, %rcx
cmpq %rcx, %rax
jb 0x8e83
movl 0x2f4(%rsp), %eax
movl %eax, %esi
leaq 0x600(%rsp), %rdi
callq 0x9d40
movsd (%rax), %xmm0
movsd %xmm0, 0x2e8(%rsp)
jmp 0x8f2e
movsd 0x2f8(%rsp), %xmm0
movsd 0x5b0(%rsp), %xmm1
divsd %xmm1, %xmm0
movl 0x2f4(%rsp), %eax
cvtsi2sd %rax, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x2e0(%rsp)
movl 0x2f4(%rsp), %eax
movl %eax, %esi
leaq 0x600(%rsp), %rdi
callq 0x9d40
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x1f166(%rip), %xmm0    # 0x28040
subsd 0x2e0(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movl 0x2f4(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
leaq 0x600(%rsp), %rdi
callq 0x9d40
movsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm2
movsd (%rax), %xmm1
mulsd 0x2e0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2e8(%rsp)
movq 0x648(%rsp), %rdi
movl 0x300(%rsp), %eax
movl %eax, %esi
movl 0x304(%rsp), %eax
movl %eax, %edx
movl 0x308(%rsp), %eax
movl %eax, %ecx
callq 0xa350
movw %ax, 0xe(%rsp)
jmp 0x8f5d
movw 0xe(%rsp), %ax
movw %ax, 0x2de(%rsp)
movzwl 0x2de(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd 0x2e8(%rsp), %xmm1
divsd %xmm1, %xmm0
movzwl 0x30e(%rsp), %eax
shrl %eax
cvtsi2sd %eax, %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movw %ax, 0x2dc(%rsp)
movq 0x640(%rsp), %rdi
movl 0x300(%rsp), %eax
movl %eax, %esi
movl 0x304(%rsp), %eax
movl %eax, %edx
movl 0x308(%rsp), %eax
movl %eax, %ecx
movzwl 0x2dc(%rsp), %r8d
callq 0xf360
jmp 0x8fd4
movzwl 0x2dc(%rsp), %eax
cvtsi2sd %eax, %xmm0
leaq 0x310(%rsp), %rdi
callq 0x21400
jmp 0x8fef
jmp 0x8ff1
movl 0x300(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x300(%rsp)
jmp 0x8d12
jmp 0x9009
movl 0x304(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x304(%rsp)
jmp 0x8cf3
jmp 0x9021
movl 0x308(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x308(%rsp)
jmp 0x8cd4
leaq 0xd8(%rsp), %rdi
callq 0x3450
jmp 0x9046
movq 0xb0(%rsp), %rsi
leaq 0x1fb61(%rip), %rdx     # 0x28bb6
leaq 0xb8(%rsp), %rdi
callq 0x6100
jmp 0x9064
leaq 0xd8(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
movl $0x10, %edx
callq 0x3140
jmp 0x9080
leaq 0xd8(%rsp), %rdi
leaq 0x310(%rsp), %rsi
callq 0x21590
jmp 0x9097
leaq 0xb8(%rsp), %rdi
callq 0x3538
leaq 0xd8(%rsp), %rdi
callq 0x3390
leaq 0x310(%rsp), %rdi
callq 0x21730
leaq 0x5e8(%rsp), %rdi
callq 0x12fb0
leaq 0x600(%rsp), %rdi
callq 0xfdb0
leaq 0x618(%rsp), %rdi
callq 0xfdb0
addq $0x658, %rsp            # imm = 0x658
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5d0(%rsp)
movl %eax, 0x5cc(%rsp)
jmp 0x9124
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5d0(%rsp)
movl %eax, 0x5cc(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x3538
leaq 0xd8(%rsp), %rdi
callq 0x3390
leaq 0x310(%rsp), %rdi
callq 0x21730
leaq 0x5e8(%rsp), %rdi
callq 0x12fb0
leaq 0x600(%rsp), %rdi
callq 0xfdb0
leaq 0x618(%rsp), %rdi
callq 0xfdb0
movq 0x5d0(%rsp), %rdi
callq 0x34a0
nopw %cs:(%rax,%rax)
nopl (%rax)