// File: /workspace/github_cpp_makefile_star2_O1/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 $0x338, %rsp            # imm = 0x338
movsd %xmm0, 0xf8(%rsp)
movq %rcx, %r12
movq %rdx, %r14
movq %rsi, %r15
movq 0x1558c(%rip), %rbx     # 0x1cfc8
leaq 0x1018b(%rip), %rsi     # 0x17bce
movl $0x13, %edx
movq %rbx, %rdi
callq 0x3300
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32b0
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, 0x20(%rsp)
movapd %xmm0, 0x10(%rsp)
movq %rax, 0x80(%rsp)
movapd %xmm0, 0x70(%rsp)
movq 0x18(%r15), %rax
movq 0x20(%r15), %rcx
movq %rax, 0x60(%rsp)
movl %eax, %eax
movq %rax, 0x38(%rsp)
movq %rcx, 0x50(%rsp)
movl %ecx, %eax
movq %rax, 0x58(%rsp)
movq %r15, 0xb8(%rsp)
movq 0x28(%r15), %rax
movq %rax, 0x8(%rsp)
movl %eax, %eax
movq %rax, 0x48(%rsp)
movq (%r14), %rax
cmpq %rax, 0x8(%r14)
je 0x7aec
movq %rax, 0x8(%r14)
movupd %xmm0, 0x18(%r14)
movq $0x0, 0x28(%r14)
movq %r14, %rdi
movq 0x38(%rsp), %rsi
movq 0x58(%rsp), %rdx
movq 0x48(%rsp), %rcx
xorl %r8d, %r8d
callq 0xceda
movq 0x60(%rsp), %rax
movl %eax, %ecx
shrl %ecx
movq 0x50(%rsp), %rax
movl %eax, %edx
shrl %edx
movq %rcx, 0x30(%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
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movq %r14, 0xb0(%rsp)
jb 0x7b5b
sqrtsd %xmm0, %xmm0
jmp 0x7b60
callq 0x3240
divsd 0xf4d8(%rip), %xmm0     # 0x17040
movsd %xmm0, 0x68(%rsp)
xorl %r15d, %r15d
leaq 0x138(%rsp), %r14
leaq 0x10(%rsp), %r13
leaq 0xc0(%rsp), %rbp
leaq 0x70(%rsp), %rbx
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
mulsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x138(%rsp)
movq 0x98(%rsp), %rsi
cmpq 0xa0(%rsp), %rsi
je 0x7bc6
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x98(%rsp)
jmp 0x7bd6
leaq 0x90(%rsp), %rdi
movq %r14, %rdx
callq 0x14d1c
movq $0x0, 0xc0(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x7c00
movq $0x0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x7c0b
movq %r13, %rdi
movq %rbp, %rdx
callq 0xe0fa
movl $0x0, 0xc0(%rsp)
movq 0x78(%rsp), %rsi
cmpq 0x80(%rsp), %rsi
je 0x7c36
movl $0x0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x78(%rsp)
jmp 0x7c41
movq %rbx, %rdi
movq %rbp, %rdx
callq 0xef54
incl %r15d
cmpl $0x28, %r15d
jne 0x7b8b
movq 0x15373(%rip), %rdi     # 0x1cfc8
leaq 0xff86(%rip), %rsi      # 0x17be2
movl $0x1b, %edx
callq 0x3300
movq 0x1535b(%rip), %rdi     # 0x1cfc8
movl $0x28, %esi
callq 0x31d0
movq %rax, %rbx
leaq 0xfad0(%rip), %rsi      # 0x17751
movl $0x5, %edx
movq %rax, %rdi
callq 0x3300
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31e0
movq %r12, 0x110(%rsp)
cmpl $0x0, 0x8(%rsp)
je 0x7da2
movq 0x30(%rsp), %r15
negl %r15d
xorl %ebx, %ebx
cmpl $0x0, 0x50(%rsp)
je 0x7d94
xorl %r14d, %r14d
cmpl $0x0, 0x60(%rsp)
je 0x7d86
movl %r14d, %r12d
subl 0x2c(%rsp), %r12d
imull %r12d, %r12d
xorl %r13d, %r13d
leal (%r15,%r13), %eax
imull %eax, %eax
addl %r12d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
ucomisd 0xf34c(%rip), %xmm0     # 0x17060
jb 0x7d1c
sqrtsd %xmm0, %xmm0
jmp 0x7d21
callq 0x3240
movsd %xmm0, 0x40(%rsp)
movq 0xb8(%rsp), %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x9144
movl %eax, %ebp
movsd 0x40(%rsp), %xmm0
divsd 0x68(%rsp), %xmm0
callq 0x3400
cvttsd2si %xmm0, %rax
movzwl %bp, %ecx
xorps %xmm0, %xmm0
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 %r13
cmpq %r13, 0x38(%rsp)
jne 0x7cfa
incq %r14
cmpq 0x58(%rsp), %r14
jne 0x7ce0
incq %rbx
cmpq 0x48(%rsp), %rbx
jne 0x7cd2
movq 0x1521f(%rip), %rbx     # 0x1cfc8
leaq 0xfe4e(%rip), %rsi      # 0x17bfe
movl $0x16, %edx
movq %rbx, %rdi
callq 0x3300
movq 0x110(%rsp), %r12
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x32b0
movsbl %al, %esi
movq 0x151e5(%rip), %rdi     # 0x1cfc8
callq 0x3040
movq %rax, %rdi
callq 0x31e0
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
je 0x7e2a
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 0x7e0a
movq 0x15197(%rip), %rbx     # 0x1cfc8
leaq 0xfddd(%rip), %rsi      # 0x17c15
movl $0x14, %edx
movq %rbx, %rdi
callq 0x3300
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x32b0
movsbl %al, %esi
movq 0x15165(%rip), %rdi     # 0x1cfc8
callq 0x3040
movq %rax, %rdi
callq 0x31e0
leaq 0x138(%rsp), %rdi
callq 0x3410
leaq 0xd0(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0xc0(%rsp), %rdi
callq 0x1312e
leaq 0xfd81(%rip), %rsi      # 0x17c2a
leaq 0xc0(%rsp), %rdi
callq 0x3480
leaq 0x138(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
movl $0x10, %edx
callq 0x3130
leaq 0xfd64(%rip), %rsi      # 0x17c3b
leaq 0x138(%rsp), %rbx
movl $0x11, %edx
movq %rbx, %rdi
callq 0x3300
movq 0x138(%rsp), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x32b0
movsbl %al, %esi
leaq 0x138(%rsp), %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31e0
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
je 0x7fe8
movl $0x1, %r15d
xorl %ebp, %ebp
leaq 0x138(%rsp), %rbx
leaq 0xfd09(%rip), %r14      # 0x17c4d
movq 0x90(%rsp), %rax
movsd (%rax,%rbp,8), %xmm0
movq %rbx, %rdi
callq 0x33d0
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x3300
movq 0x10(%rsp), %rax
movsd (%rax,%rbp,8), %xmm0
movq %r13, %rdi
callq 0x33d0
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x3300
movq 0x70(%rsp), %rax
movl (%rax,%rbp,4), %esi
movq %r13, %rdi
callq 0x3430
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x32b0
movsbl %al, %esi
movq %r13, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31e0
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
movl %r15d, %ebp
sarq $0x3, %rax
incl %r15d
cmpq %rbp, %rax
jne 0x7f44
movq 0xc0(%rsp), %rdi
leaq 0xd0(%rsp), %rax
cmpq %rax, %rdi
je 0x8002
callq 0x3290
movq 0x14faf(%rip), %rax     # 0x1cfb8
movq (%rax), %rcx
movq 0x18(%rax), %rdx
leaq 0x140(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq %rcx, 0x108(%rsp)
movq -0x18(%rcx), %rax
movq %rdx, 0x100(%rsp)
movq %rdx, 0x138(%rsp,%rax)
callq 0x3260
leaq 0x230(%rsp), %rdi
callq 0x30c0
leaq 0xc8(%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 0x14f49(%rip), %rbx     # 0x1cfc8
leaq 0xfbc9(%rip), %rsi      # 0x17c4f
movl $0x13, %edx
movq %rbx, %rdi
callq 0x3300
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x32b0
movsbl %al, %esi
movq 0x14f17(%rip), %rdi     # 0x1cfc8
callq 0x3040
movq %rax, %rdi
callq 0x31e0
cmpl $0x0, 0x8(%rsp)
je 0x8274
cvtsi2sdq 0x38(%rsp), %xmm0
mulsd 0xef70(%rip), %xmm0     # 0x17048
movsd 0xef70(%rip), %xmm1     # 0x17050
subsd 0xf8(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x40(%rsp)
movq 0x30(%rsp), %rax
negl %eax
movq %rax, 0x30(%rsp)
xorl %ebx, %ebx
cmpl $0x0, 0x50(%rsp)
je 0x8266
xorl %r13d, %r13d
cmpl $0x0, 0x60(%rsp)
je 0x8258
movl %r13d, %r15d
subl 0x2c(%rsp), %r15d
imull %r15d, %r15d
xorl %r14d, %r14d
movq 0x30(%rsp), %rax
addl %r14d, %eax
imull %eax, %eax
addl %r15d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
ucomisd 0xef19(%rip), %xmm0     # 0x17060
jb 0x814f
sqrtsd %xmm0, %xmm0
jmp 0x8154
callq 0x3240
ucomisd 0x40(%rsp), %xmm0
jbe 0x817a
movq 0xb0(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0xd394
jmp 0x824a
divsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0x3400
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 0x81dd
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd 0x8(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd 0xee8a(%rip), %xmm0     # 0x17050
subsd %xmm1, %xmm0
leal 0x1(%rax), %edx
mulsd (%rcx,%rdx,8), %xmm1
mulsd (%rcx,%rax,8), %xmm0
addsd %xmm1, %xmm0
jmp 0x81e2
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0xb8(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x9144
movzwl %ax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x8(%rsp), %xmm0
mulsd 0xee42(%rip), %xmm0     # 0x17058
cvttsd2si %xmm0, %eax
movzwl %ax, %ebp
movq 0xb0(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
movl %ebp, %r8d
callq 0xd394
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
leaq 0xc0(%rsp), %rdi
callq 0x14a40
incq %r14
cmpq %r14, 0x38(%rsp)
jne 0x8129
incq %r13
cmpq 0x58(%rsp), %r13
jne 0x810f
incq %rbx
cmpq 0x48(%rsp), %rbx
jne 0x8101
leaq 0x138(%rsp), %rdi
callq 0x3410
leaq 0x128(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0x118(%rsp), %rdi
callq 0x1312e
leaq 0xf9b6(%rip), %rsi      # 0x17c63
leaq 0x118(%rsp), %rdi
callq 0x3480
leaq 0x138(%rsp), %rdi
leaq 0x118(%rsp), %rsi
movl $0x10, %edx
callq 0x3130
leaq 0x138(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x14b70
movq 0x118(%rsp), %rdi
cmpq %r14, %rdi
je 0x82fb
callq 0x3290
leaq 0x140(%rsp), %rdi
movq 0x108(%rsp), %rax
movq %rax, -0x8(%rdi)
movq -0x18(%rax), %rax
movq 0x100(%rsp), %rcx
movq %rcx, 0x138(%rsp,%rax)
callq 0x3260
leaq 0x230(%rsp), %rdi
callq 0x30c0
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
callq 0x14f3a
movq %rbx, %rdi
callq 0x1309a
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x835c
callq 0x3290
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x836b
callq 0x3290
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x837d
callq 0x3290
addq $0x338, %rsp            # imm = 0x338
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x83b3
movq %rax, %rbx
jmp 0x83c8
jmp 0x8413
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
cmpq %r14, %rdi
jne 0x83fb
jmp 0x8400
movq %rax, %rbx
jmp 0x8400
jmp 0x8430
jmp 0x8430
movq %rax, %rbx
movq 0x118(%rsp), %rdi
cmpq %r14, %rdi
je 0x83c8
callq 0x3290
leaq 0x138(%rsp), %rdi
callq 0x3390
jmp 0x8416
jmp 0x8413
jmp 0x83e3
jmp 0x8430
jmp 0x8430
jmp 0x8430
jmp 0x8430
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
leaq 0xd0(%rsp), %rax
cmpq %rax, %rdi
je 0x8400
callq 0x3290
leaq 0x138(%rsp), %rdi
callq 0x3390
jmp 0x8433
jmp 0x8413
jmp 0x8413
movq %rax, %rbx
leaq 0xc0(%rsp), %r14
movq %r14, %rdi
callq 0x14f3a
movq %r14, %rdi
callq 0x1309a
jmp 0x8433
movq %rax, %rbx
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x8442
callq 0x3290
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x8451
callq 0x3290
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x8463
callq 0x3290
movq %rbx, %rdi
callq 0x3440
nop