// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x328, %rsp            # imm = 0x328
movsd %xmm0, 0x60(%rsp)
movq %rcx, 0xb0(%rsp)
movq %rdx, %r14
movq %rsi, %r15
movq 0x15674(%rip), %rbx     # 0x1cfc8
leaq 0x10263(%rip), %rsi     # 0x17bbe
movl $0x13, %edx
movq %rbx, %rdi
callq 0x32f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32a0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31e0
xorl %eax, %eax
movq %rax, 0xa0(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x90(%rsp)
movq %rax, 0x10(%rsp)
movapd %xmm0, (%rsp)
movq %rax, 0x80(%rsp)
movapd %xmm0, 0x70(%rsp)
movq 0x18(%r15), %rax
movq 0x20(%r15), %rcx
movq %rax, 0x58(%rsp)
movl %eax, %eax
movq %rax, 0x40(%rsp)
movq %rcx, 0x48(%rsp)
movl %ecx, %eax
movq %rax, 0x50(%rsp)
movq %r15, 0xc0(%rsp)
movl 0x28(%r15), %eax
movq %rax, 0x20(%rsp)
movq (%r14), %rax
cmpq %rax, 0x8(%r14)
je 0x79fc
movq %rax, 0x8(%r14)
movupd %xmm0, 0x18(%r14)
movq $0x0, 0x28(%r14)
xorl %r15d, %r15d
movq %r14, 0xb8(%rsp)
movq %r14, %rdi
movq 0x40(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq 0x20(%rsp), %rcx
xorl %r8d, %r8d
callq 0xcd8e
movq 0x58(%rsp), %rax
movl %eax, %ecx
shrl %ecx
movq 0x48(%rsp), %rax
movl %eax, %edx
shrl %edx
movq %rcx, 0x38(%rsp)
movl %ecx, %eax
imull %ecx, %eax
movl %edx, 0x2c(%rsp)
movl %edx, %ecx
imull %ecx, %ecx
addl %eax, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
sqrtsd %xmm0, %xmm0
divsd 0xf5d6(%rip), %xmm0     # 0x17040
movsd %xmm0, 0x68(%rsp)
leaq 0x90(%rsp), %r12
leaq 0x128(%rsp), %r14
movq %rsp, %r13
leaq 0xc8(%rsp), %rbp
leaq 0x70(%rsp), %rbx
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
mulsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x128(%rsp)
movq 0x98(%rsp), %rsi
cmpq 0xa0(%rsp), %rsi
je 0x7acb
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x98(%rsp)
jmp 0x7ad6
movq %r12, %rdi
movq %r14, %rdx
callq 0x14bac
movq $0x0, 0xc8(%rsp)
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0x7b00
movq $0x0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rsp)
jmp 0x7b0b
movq %r13, %rdi
movq %rbp, %rdx
callq 0xdfe2
movl $0x0, 0xc8(%rsp)
movq 0x78(%rsp), %rsi
cmpq 0x80(%rsp), %rsi
je 0x7b36
movl $0x0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x78(%rsp)
jmp 0x7b41
movq %rbx, %rdi
movq %rbp, %rdx
callq 0xee26
incl %r15d
cmpl $0x28, %r15d
jne 0x7a90
movq 0x15473(%rip), %rdi     # 0x1cfc8
leaq 0x10076(%rip), %rsi     # 0x17bd2
movl $0x1b, %edx
callq 0x32f0
movq 0x1545b(%rip), %rdi     # 0x1cfc8
movl $0x28, %esi
callq 0x31d0
movq %rax, %rbx
leaq 0xfbc0(%rip), %rsi      # 0x17741
movl $0x5, %edx
movq %rax, %rdi
callq 0x32f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32a0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31e0
cmpq $0x0, 0x20(%rsp)
je 0x7c75
movq 0x38(%rsp), %r15
negl %r15d
xorl %ebx, %ebx
cmpl $0x0, 0x48(%rsp)
je 0x7c67
xorl %r14d, %r14d
cmpl $0x0, 0x58(%rsp)
je 0x7c59
movl %r14d, %r12d
subl 0x2c(%rsp), %r12d
imull %r12d, %r12d
xorl %r13d, %r13d
movq 0xc0(%rsp), %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x8fee
movl %eax, %ebp
leal (%r15,%r13), %eax
imull %eax, %eax
addl %r12d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
sqrtsd %xmm0, %xmm0
divsd 0x68(%rsp), %xmm0
callq 0x33f0
cvttsd2si %xmm0, %rax
movzwl %bp, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movl %eax, %eax
movq (%rsp), %rcx
addsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rcx,%rax,8)
movq 0x70(%rsp), %rcx
incl (%rcx,%rax,4)
incq %r13
cmpq %r13, 0x40(%rsp)
jne 0x7bef
incq %r14
cmpq 0x50(%rsp), %r14
jne 0x7bd9
incq %rbx
cmpq 0x20(%rsp), %rbx
jne 0x7bcb
movq 0x1534c(%rip), %rbx     # 0x1cfc8
leaq 0xff6b(%rip), %rsi      # 0x17bee
movl $0x16, %edx
movq %rbx, %rdi
callq 0x32f0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x32a0
movsbl %al, %esi
movq 0x1531a(%rip), %rdi     # 0x1cfc8
callq 0x3040
movq %rax, %rdi
callq 0x31e0
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
je 0x7cf4
sarq $0x3, %rcx
movq 0x70(%rsp), %rdx
xorl %esi, %esi
xorps %xmm0, %xmm0
cvtsi2sdl (%rdx,%rsi,4), %xmm0
movsd (%rax,%rsi,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rax,%rsi,8)
incq %rsi
movl %esi, %edi
cmpq %rdi, %rcx
jne 0x7cd4
movq 0x152cd(%rip), %rbx     # 0x1cfc8
leaq 0xff03(%rip), %rsi      # 0x17c05
movl $0x14, %edx
movq %rbx, %rdi
callq 0x32f0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x32a0
movsbl %al, %esi
movq 0x1529b(%rip), %rdi     # 0x1cfc8
callq 0x3040
movq %rax, %rdi
callq 0x31e0
leaq 0x128(%rsp), %rdi
callq 0x3400
leaq 0xd8(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0xb0(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0xc8(%rsp), %rdi
callq 0x12fde
leaq 0xfea0(%rip), %rsi      # 0x17c1a
leaq 0xc8(%rsp), %rdi
callq 0x3470
leaq 0x128(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
movl $0x10, %edx
callq 0x3130
leaq 0xfe83(%rip), %rsi      # 0x17c2b
leaq 0x128(%rsp), %rbx
movl $0x11, %edx
movq %rbx, %rdi
callq 0x32f0
movq 0x128(%rsp), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x32a0
movsbl %al, %esi
leaq 0x128(%rsp), %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31e0
movq 0x8(%rsp), %rax
cmpq (%rsp), %rax
je 0x7eb6
movl $0x1, %r15d
xorl %ebp, %ebp
leaq 0x128(%rsp), %rbx
leaq 0xfe29(%rip), %r14      # 0x17c3d
movq 0x90(%rsp), %rax
movsd (%rax,%rbp,8), %xmm0
movq %rbx, %rdi
callq 0x33c0
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x32f0
movq (%rsp), %rax
movsd (%rax,%rbp,8), %xmm0
movq %r13, %rdi
callq 0x33c0
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x32f0
movq 0x70(%rsp), %rax
movl (%rax,%rbp,4), %esi
movq %r13, %rdi
callq 0x3420
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x32a0
movsbl %al, %esi
movq %r13, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31e0
movq 0x8(%rsp), %rax
subq (%rsp), %rax
movl %r15d, %ebp
sarq $0x3, %rax
incl %r15d
cmpq %rbp, %rax
jne 0x7e14
movq 0xc8(%rsp), %rdi
cmpq %r12, %rdi
je 0x7ec8
callq 0x3280
movq 0x150e9(%rip), %rax     # 0x1cfb8
movq (%rax), %r12
movq 0x18(%rax), %rcx
leaq 0x130(%rsp), %rdi
movq %r12, -0x8(%rdi)
movq -0x18(%r12), %rax
movq %rcx, 0x100(%rsp)
movq %rcx, 0x128(%rsp,%rax)
callq 0x3250
leaq 0x220(%rsp), %rdi
callq 0x30c0
leaq 0xd0(%rsp), %rax
movl $0x0, (%rax)
xorl %ecx, %ecx
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rcx, 0x20(%rax)
movabsq $0x4040000000000000, %rcx # imm = 0x4040000000000000
movq %rcx, 0x28(%rax)
movq 0x1508a(%rip), %rbx     # 0x1cfc8
leaq 0xfcfa(%rip), %rsi      # 0x17c3f
movl $0x13, %edx
movq %rbx, %rdi
callq 0x32f0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x32a0
movsbl %al, %esi
movq 0x15058(%rip), %rdi     # 0x1cfc8
callq 0x3040
movq %rax, %rdi
callq 0x31e0
cmpq $0x0, 0x20(%rsp)
je 0x811f
cvtsi2sdq 0x40(%rsp), %xmm0
mulsd 0xf0b0(%rip), %xmm0     # 0x17048
movsd 0xf0b0(%rip), %xmm1     # 0x17050
subsd 0x60(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x60(%rsp)
movq 0x38(%rsp), %rax
negl %eax
movq %rax, 0x38(%rsp)
xorl %ebx, %ebx
cmpl $0x0, 0x48(%rsp)
je 0x8111
xorl %r13d, %r13d
cmpl $0x0, 0x58(%rsp)
je 0x8103
movl %r13d, %r15d
subl 0x2c(%rsp), %r15d
imull %r15d, %r15d
xorl %r14d, %r14d
movq 0x38(%rsp), %rax
addl %r14d, %eax
imull %eax, %eax
addl %r15d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
sqrtsd %xmm0, %xmm0
ucomisd 0x60(%rsp), %xmm0
jbe 0x8026
movq 0xb8(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0xd25a
jmp 0x80f5
divsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
callq 0x33f0
cvttsd2si %xmm0, %rax
movl %eax, %eax
movq (%rsp), %rcx
movq 0x8(%rsp), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
addq $-0x2, %rdx
cmpq %rax, %rdx
jbe 0x8088
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd 0x30(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd 0xefdf(%rip), %xmm0     # 0x17050
subsd %xmm1, %xmm0
leal 0x1(%rax), %edx
mulsd (%rcx,%rdx,8), %xmm1
mulsd (%rcx,%rax,8), %xmm0
addsd %xmm1, %xmm0
jmp 0x808d
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0xc0(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x8fee
movzwl %ax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x30(%rsp), %xmm0
mulsd 0xef97(%rip), %xmm0     # 0x17058
cvttsd2si %xmm0, %eax
movzwl %ax, %ebp
movq 0xb8(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
movl %ebp, %r8d
callq 0xd25a
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
leaq 0xc8(%rsp), %rdi
callq 0x14904
incq %r14
cmpq %r14, 0x40(%rsp)
jne 0x7fe6
incq %r13
cmpq 0x50(%rsp), %r13
jne 0x7fcc
incq %rbx
cmpq 0x20(%rsp), %rbx
jne 0x7fbe
leaq 0x128(%rsp), %rdi
callq 0x3400
leaq 0x118(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0xb0(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x108(%rsp), %rdi
callq 0x12fde
leaq 0xfaf5(%rip), %rsi      # 0x17c53
leaq 0x108(%rsp), %rdi
callq 0x3470
leaq 0x128(%rsp), %rdi
leaq 0x108(%rsp), %rsi
movl $0x10, %edx
callq 0x3130
leaq 0x128(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x14a16
movq 0x108(%rsp), %rdi
cmpq %r14, %rdi
je 0x81ac
callq 0x3280
leaq 0x130(%rsp), %rdi
movq %r12, -0x8(%rdi)
movq -0x18(%r12), %rax
movq 0x100(%rsp), %rcx
movq %rcx, 0x128(%rsp,%rax)
callq 0x3250
leaq 0x220(%rsp), %rdi
callq 0x30c0
leaq 0xc8(%rsp), %rbx
movq %rbx, %rdi
callq 0x14dca
movq %rbx, %rdi
callq 0x12f4a
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x8206
callq 0x3280
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x8214
callq 0x3280
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x8226
callq 0x3280
addq $0x328, %rsp            # imm = 0x328
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x824c
movq %rax, %rbx
jmp 0x8261
jmp 0x82a4
jmp 0x827c
movq %rax, %rbx
jmp 0x8291
jmp 0x82c1
jmp 0x82c1
movq %rax, %rbx
movq 0x108(%rsp), %rdi
cmpq %r14, %rdi
je 0x8261
callq 0x3280
leaq 0x128(%rsp), %rdi
callq 0x3380
jmp 0x82a7
jmp 0x82a4
jmp 0x827c
jmp 0x82c1
jmp 0x82c1
jmp 0x82c1
jmp 0x82c1
movq %rax, %rbx
movq 0xc8(%rsp), %rdi
cmpq %r12, %rdi
je 0x8291
callq 0x3280
leaq 0x128(%rsp), %rdi
callq 0x3380
jmp 0x82c4
jmp 0x82a4
jmp 0x82a4
movq %rax, %rbx
leaq 0xc8(%rsp), %r14
movq %r14, %rdi
callq 0x14dca
movq %r14, %rdi
callq 0x12f4a
jmp 0x82c4
movq %rax, %rbx
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x82d3
callq 0x3280
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x82e1
callq 0x3280
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x82f3
callq 0x3280
movq %rbx, %rdi
callq 0x3430
nop