// File: /workspace/github_cpp_makefile_star2_O2/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 $0x308, %rsp            # imm = 0x308
movsd %xmm0, 0x40(%rsp)
movq %rcx, 0x50(%rsp)
movq %rdx, %rbx
movq %rsi, %r14
movq 0x11e57(%rip), %rdi     # 0x17fc8
leaq 0xb9e1(%rip), %rsi      # 0x11b59
callq 0x3260
movq %rax, %rdi
callq 0x3110
xorpd %xmm0, %xmm0
movapd %xmm0, 0x90(%rsp)
andq $0x0, 0xa0(%rsp)
movapd %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movapd %xmm0, 0x70(%rsp)
andq $0x0, 0x80(%rsp)
movq 0x18(%r14), %rax
movq 0x20(%r14), %rcx
movq %rax, %r15
movl %eax, %eax
movq %rax, 0x8(%rsp)
movq %rcx, %r12
movl %ecx, %eax
movq %rax, 0x38(%rsp)
movq %r14, 0x68(%rsp)
movl 0x28(%r14), %eax
movq %rax, 0x30(%rsp)
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0x61ed
movq %rax, 0x8(%rbx)
movupd %xmm0, 0x18(%rbx)
andq $0x0, 0x28(%rbx)
xorl %r14d, %r14d
movq %rbx, 0x60(%rsp)
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0x30(%rsp), %rcx
xorl %r8d, %r8d
callq 0x9666
shrl %r15d
movq %r12, %rcx
shrl %ecx
movq %r15, %r12
movl %r15d, %eax
imull %r15d, %eax
movq %rcx, 0x58(%rsp)
imull %ecx, %ecx
addl %eax, %ecx
cvtsi2sd %rcx, %xmm0
sqrtsd %xmm0, %xmm0
divsd 0xadf2(%rip), %xmm0     # 0x11038
movsd %xmm0, 0x48(%rsp)
leaq 0x90(%rsp), %r15
leaq 0x10(%rsp), %rbp
leaq 0xb0(%rsp), %rbx
leaq 0x70(%rsp), %r13
cmpl $0x28, %r14d
je 0x62bc
cvtsi2sd %r14d, %xmm0
mulsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x108(%rsp)
movq %r15, %rdi
leaq 0x108(%rsp), %rsi
callq 0xf3e0
andq $0x0, 0xb0(%rsp)
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xa430
andl $0x0, 0xb0(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
callq 0xdfb2
incl %r14d
jmp 0x6266
movq 0x11d05(%rip), %rdi     # 0x17fc8
leaq 0xb8a3(%rip), %rsi      # 0x11b6d
callq 0x3260
pushq $0x28
popq %rsi
movq %rax, %rdi
callq 0x31a0
leaq 0xb425(%rip), %rsi      # 0x11706
movq %rax, %rdi
callq 0x3260
movq %rax, %rdi
callq 0x3110
negl %r12d
xorl %ebx, %ebx
cmpq 0x30(%rsp), %rbx
je 0x638c
xorl %r13d, %r13d
cmpq 0x38(%rsp), %r13
je 0x6384
movl %r13d, %r15d
subl 0x58(%rsp), %r15d
imull %r15d, %r15d
xorl %ebp, %ebp
cmpq %rbp, 0x8(%rsp)
je 0x637f
movq 0x68(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x70ba
movl %eax, %r14d
leal (%r12,%rbp), %eax
imull %eax, %eax
addl %r15d, %eax
cvtsi2sd %rax, %xmm0
sqrtsd %xmm0, %xmm0
divsd 0x48(%rsp), %xmm0
callq 0x33e0
cvttsd2si %xmm0, %rax
movzwl %r14w, %ecx
cvtsi2sd %ecx, %xmm0
movl %eax, %eax
movq 0x10(%rsp), %rcx
addsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rcx,%rax,8)
movq 0x70(%rsp), %rcx
incl (%rcx,%rax,4)
incq %rbp
jmp 0x6319
incq %r13
jmp 0x6304
incq %rbx
jmp 0x62f6
movq 0x11c35(%rip), %rdi     # 0x17fc8
leaq 0xb7ef(%rip), %rsi      # 0x11b89
callq 0x3260
movq %rax, %rdi
callq 0x3110
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
xorl %esi, %esi
movq 0x70(%rsp), %rdx
movl %esi, %esi
cmpq %rsi, %rcx
je 0x63dd
cvtsi2sdl (%rdx,%rsi,4), %xmm0
movsd (%rax,%rsi,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rax,%rsi,8)
incl %esi
jmp 0x63bf
movq 0x11be4(%rip), %rdi     # 0x17fc8
leaq 0xb7b5(%rip), %rsi      # 0x11ba0
callq 0x3260
movq %rax, %rdi
callq 0x3110
leaq 0x108(%rsp), %rdi
callq 0x33f0
leaq 0xb7a9(%rip), %rdx      # 0x11bb5
leaq 0xb0(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x4f34
leaq 0x108(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x3100
leaq 0xb789(%rip), %rsi      # 0x11bc6
leaq 0x108(%rsp), %rdi
callq 0x3260
movq %rax, %rdi
callq 0x3110
xorl %r14d, %r14d
leaq 0x108(%rsp), %rbx
leaq 0xb774(%rip), %r13      # 0x11bd8
movl %r14d, %r14d
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x3, %rax
cmpq %r14, %rax
je 0x64d7
movq 0x90(%rsp), %rax
movsd (%rax,%r14,8), %xmm0
movq %rbx, %rdi
callq 0x33a0
movq %rax, %rdi
movq %r13, %rsi
callq 0x3260
movq 0x10(%rsp), %rcx
movsd (%rcx,%r14,8), %xmm0
movq %rax, %rdi
callq 0x33a0
movq %rax, %rdi
movq %r13, %rsi
callq 0x3260
movq 0x70(%rsp), %rcx
movl (%rcx,%r14,4), %esi
movq %rax, %rdi
callq 0x3410
movq %rax, %rdi
callq 0x3110
incl %r14d
jmp 0x6464
leaq 0xb0(%rsp), %rdi
callq 0x3488
leaq 0x108(%rsp), %rdi
callq 0x3350
leaq 0xb8(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movabsq $0x4040000000000000, %rcx # imm = 0x4040000000000000
movq %rcx, 0x28(%rax)
movq 0x11aa5(%rip), %rdi     # 0x17fc8
leaq 0xb6b0(%rip), %rsi      # 0x11bda
callq 0x3260
movq %rax, %rdi
callq 0x3110
cvtsi2sdq 0x8(%rsp), %xmm0
mulsd 0xaafa(%rip), %xmm0     # 0x11040
movsd 0xaafa(%rip), %xmm1     # 0x11048
subsd 0x40(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x40(%rsp)
xorl %ebx, %ebx
cmpq 0x30(%rsp), %rbx
je 0x669f
xorl %r13d, %r13d
cmpq 0x38(%rsp), %r13
je 0x6697
movl %r13d, %r15d
subl 0x58(%rsp), %r15d
imull %r15d, %r15d
xorl %ebp, %ebp
cmpq %rbp, 0x8(%rsp)
je 0x668f
leal (%r12,%rbp), %eax
imull %eax, %eax
addl %r15d, %eax
cvtsi2sd %rax, %xmm0
sqrtsd %xmm0, %xmm0
ucomisd 0x40(%rsp), %xmm0
jbe 0x65c8
movq 0x60(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x9988
jmp 0x6687
divsd 0x48(%rsp), %xmm0
movsd %xmm0, (%rsp)
callq 0x33e0
cvttsd2si %xmm0, %rax
movl %eax, %eax
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
addq $-0x2, %rdx
cmpq %rax, %rdx
jbe 0x6626
cvtsi2sd %rax, %xmm0
movsd (%rsp), %xmm1
subsd %xmm0, %xmm1
movsd 0xaa39(%rip), %xmm0     # 0x11048
subsd %xmm1, %xmm0
leal 0x1(%rax), %edx
mulsd (%rcx,%rdx,8), %xmm1
mulsd (%rcx,%rax,8), %xmm0
addsd %xmm1, %xmm0
jmp 0x662b
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rsp)
movq 0x68(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x70ba
movzwl %ax, %eax
cvtsi2sd %eax, %xmm0
divsd (%rsp), %xmm0
mulsd 0xa9f9(%rip), %xmm0     # 0x11050
cvttsd2si %xmm0, %eax
movzwl %ax, %r14d
movq 0x60(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq %rbx, %rcx
movl %r14d, %r8d
callq 0x9988
cvtsi2sd %r14d, %xmm0
leaq 0xb0(%rsp), %rdi
callq 0xf402
incq %rbp
jmp 0x6587
incq %r13
jmp 0x656e
incq %rbx
jmp 0x6560
leaq 0x108(%rsp), %rdi
callq 0x33f0
leaq 0xb53b(%rip), %rdx      # 0x11bee
leaq 0xe8(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x4f34
leaq 0x108(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x3100
leaq 0x108(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0xf4b4
leaq 0xe8(%rsp), %rdi
callq 0x3488
leaq 0x108(%rsp), %rdi
callq 0x3350
leaq 0xb0(%rsp), %rdi
callq 0xf5bc
leaq 0x70(%rsp), %rdi
callq 0xb1c8
leaq 0x10(%rsp), %rdi
callq 0xe210
leaq 0x90(%rsp), %rdi
callq 0xe210
addq $0x308, %rsp            # imm = 0x308
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x676c
jmp 0x67aa
movq %rax, %rbx
jmp 0x6797
jmp 0x67bc
jmp 0x67bc
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x3488
leaq 0x108(%rsp), %rdi
callq 0x3350
jmp 0x67ad
jmp 0x67aa
jmp 0x6787
jmp 0x67bc
jmp 0x67bc
jmp 0x67bc
jmp 0x67bc
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x3488
leaq 0x108(%rsp), %rdi
callq 0x3350
jmp 0x67bf
jmp 0x67aa
jmp 0x67aa
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0xf5bc
jmp 0x67bf
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0xb1c8
leaq 0x10(%rsp), %rdi
callq 0xe210
leaq 0x90(%rsp), %rdi
callq 0xe210
movq %rbx, %rdi
callq 0x3420