// File: /workspace/github_cpp_makefile_star2_O1/Luthaf[P]zeopp/feature.cc
// Function: FEATURE::calculateVolume()
double FEATURE::calculateVolume(){
 
  long int i,j,counter=0;  
  double volume;

  XYZ max,min;
  XYZ box;
  XYZ pt;
  srand(randSeed2);
  SPHERE sph;
  vector <SPHERE> object; // a vector of spheres that define object (segemnt/feature/channel)

    // Draw the components located in each unit cell
    for(unsigned int i = 0; i < unitCells.size(); i++){
      vector<int> nodeIDs = ucNodes.at(i);
      DELTA_POS disp = unitCells.at(i);
      
      // Iterate over all nodes in the unit cell
      for(unsigned int j = 0; j < nodeIDs.size(); j++){
	DIJKSTRA_NODE curNode = nodes.at(nodeIDs.at(j));
	
	// Find node coordinates

        if(dimensionality<1) 
           { // if feature is not a channel, use reconstrcted feature
	   sph.x = curNode.x + v_a.x*disp.x + v_b.x*disp.y + v_c.x*disp.z;
	   sph.y = curNode.y + v_a.y*disp.x + v_b.y*disp.y + v_c.y*disp.z;
	   sph.z = curNode.z + v_a.z*disp.x + v_b.z*disp.y + v_c.z*disp.z;
           } else {
           sph.x = nodes.at(nodeIDs.at(j)).x;sph.y = nodes.at(nodeIDs.at(j)).y;sph.z = nodes.at(nodeIDs.at(j)).z;
//           cout << "draw sphere { " << sph.x << "  " << sph.y << "  " << sph.z << " } radius 0.1 " << "\n"; 
           };

	sph.r = SPHERE_SCALE*curNode.max_radius;

        object.push_back(sph);

        if(object.size()==1)
          { // first sphere added
          min.x=sph.x-sph.r;max.x=sph.x+sph.r;
          min.y=sph.y-sph.r;max.y=sph.y+sph.r;
          min.z=sph.z-sph.r;max.z=sph.z+sph.r;
          }else
          {
          if(min.x>(sph.x-sph.r)) min.x=sph.x-sph.r;
          if(max.x<(sph.x+sph.r)) max.x=sph.x+sph.r;
          if(min.y>(sph.y-sph.r)) min.y=sph.y-sph.r;
          if(max.y<(sph.y+sph.r)) max.y=sph.y+sph.r;
          if(min.z>(sph.z-sph.r)) min.z=sph.z-sph.r;
          if(max.z<(sph.z+sph.r)) max.z=sph.z+sph.r;
          };
		
      }
    } // end of analysis of object




  box.x=max.x-min.x;
  box.y=max.y-min.y;
  box.z=max.z-min.z;
 
  volume=box.x*box.y*box.z;

  cout << "Segment Box volume= " << volume << "\n"; 

// Now MC integration of volume

// example of random generation
// //     double theta = (rand()*1.0/RAND_MAX)*2*pi;

  for(i=0;i<N_MC_SAMPLE;i++)
     {

     pt.x = (rand()*1.0/RAND_MAX)*box.x+min.x;
     pt.y = (rand()*1.0/RAND_MAX)*box.y+min.y;
     pt.z = (rand()*1.0/RAND_MAX)*box.z+min.z;


     for(j=0;j<(int)object.size();j++)
        {
        if(calcEuclideanDistance(pt.x,pt.y,pt.z,object[j].x,object[j].y,object[j].z)<object[j].r)
           {
           counter++;
           break;
           };
        };

     };

volume=volume*((double)(N_MC_SAMPLE-counter))/(double)N_MC_SAMPLE;

return volume;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rdi, %rbx
leaq 0x68(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x85982
leaq 0x30(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x85982
leaq 0xc0(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x85982
leaq 0xa8(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x85982
movl $0x3b4ca6f5, %edi       # imm = 0x3B4CA6F5
callq 0x4410
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x98(%rbx), %rax
cmpq 0x90(%rbx), %rax
je 0x7a040
leaq 0x100(%rsp), %r15
xorl %r14d, %r14d
movabsq $-0x5555555555555555, %r13 # imm = 0xAAAAAAAAAAAAAAAB
movq 0xa8(%rbx), %rax
movq 0xb0(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
imulq %r13, %rdx
cmpq %r14, %rdx
jbe 0x7a28c
leaq (%r14,%r14,2), %r12
leaq (%rax,%r12,8), %rsi
leaq 0x90(%rsp), %rdi
callq 0xe886
movq 0x90(%rbx), %rax
movq 0x98(%rbx), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
imulq %r13, %rdx
cmpq %r14, %rdx
jbe 0x7a27b
movq 0x90(%rsp), %rdi
cmpq %rdi, 0x98(%rsp)
je 0x7a009
movq %r14, 0xd8(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl (%rax,%r12,4), %xmm0
movsd %xmm0, 0x8(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x4(%rax,%r12,4), %xmm0
movsd %xmm0, 0x88(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x8(%rax,%r12,4), %xmm0
movsd %xmm0, 0x80(%rsp)
movl $0x1, %ebp
xorl %r12d, %r12d
movslq (%rdi,%r12,4), %rsi
movq 0x60(%rbx), %r14
movq 0x68(%rbx), %rdx
subq %r14, %rdx
sarq $0x3, %rdx
movabsq $-0x71c71c71c71c71c7, %rax # imm = 0x8E38E38E38E38E39
imulq %rax, %rdx
cmpq %rsi, %rdx
jbe 0x7a26d
leaq (%rsi,%rsi,8), %r13
leaq (%r14,%r13,8), %rsi
addq $0x20, %rsi
movupd -0x20(%rsi), %xmm0
movupd -0x10(%rsi), %xmm1
movapd %xmm1, 0xf0(%rsp)
movapd %xmm0, 0xe0(%rsp)
movq %r15, %rdi
callq 0x38318
leaq (%r14,%r13,8), %rax
movb 0x40(%rax), %cl
movb %cl, 0x20(%r15)
movq 0x38(%rax), %rax
movq %rax, 0x18(%r15)
cmpl $0x0, 0x108(%rbx)
jle 0x79d8f
movq 0x90(%rsp), %rax
movq 0x98(%rsp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %r12, %rdx
jbe 0x7a25c
movslq (%rax,%r12,4), %r12
movq 0x60(%rbx), %rax
movq 0x68(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x71c71c71c71c71c7, %rcx # imm = 0x8E38E38E38E38E39
imulq %rcx, %rdx
cmpq %r12, %rdx
jbe 0x7a25c
leaq (%r12,%r12,8), %rcx
movsd 0x8(%rax,%rcx,8), %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x10(%rax,%rcx,8), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x18(%rax,%rcx,8), %xmm0
jmp 0x79e52
movsd 0xc0(%rbx), %xmm0
movsd 0x8(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd 0xe8(%rsp), %xmm0
movsd 0xd8(%rbx), %xmm1
movsd 0x88(%rsp), %xmm3
mulsd %xmm3, %xmm1
addsd %xmm0, %xmm1
movsd 0xf0(%rbx), %xmm0
movsd 0x80(%rsp), %xmm4
mulsd %xmm4, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0xc8(%rbx), %xmm0
mulsd %xmm2, %xmm0
addsd 0xf0(%rsp), %xmm0
movsd 0xe0(%rbx), %xmm1
mulsd %xmm3, %xmm1
addsd %xmm0, %xmm1
movsd 0xf8(%rbx), %xmm0
mulsd %xmm4, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0xd0(%rbx), %xmm0
mulsd %xmm2, %xmm0
addsd 0xf8(%rsp), %xmm0
movsd 0xe8(%rbx), %xmm1
mulsd %xmm3, %xmm1
addsd %xmm0, %xmm1
movsd 0x100(%rbx), %xmm0
mulsd %xmm4, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0x118(%rsp), %xmm0
mulsd 0xda2b7(%rip), %xmm0    # 0x154120
movsd %xmm0, 0x60(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x79e9b
movupd 0x48(%rsp), %xmm0
movupd 0x58(%rsp), %xmm1
movupd %xmm1, 0x10(%rsi)
movupd %xmm0, (%rsi)
addq $0x20, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x79eaa
leaq 0x10(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x49c88
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
cmpq $0x20, %rax
jne 0x79f19
movsd 0x60(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
movsd 0x50(%rsp), %xmm2
movapd %xmm1, %xmm3
subsd %xmm0, %xmm3
movsd %xmm3, 0x30(%rsp)
addsd %xmm0, %xmm1
movsd %xmm1, 0x68(%rsp)
movapd %xmm0, %xmm1
addsd %xmm2, %xmm1
subsd %xmm0, %xmm2
movsd %xmm2, 0x38(%rsp)
movsd %xmm1, 0x70(%rsp)
movsd 0x58(%rsp), %xmm1
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
movsd %xmm2, 0x40(%rsp)
addsd %xmm1, %xmm0
jmp 0x79faf
movsd 0x30(%rsp), %xmm3
movsd 0x48(%rsp), %xmm1
movsd 0x60(%rsp), %xmm0
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
ucomisd %xmm2, %xmm3
jbe 0x79f3f
movsd %xmm2, 0x30(%rsp)
addsd %xmm0, %xmm1
ucomisd 0x68(%rsp), %xmm1
jbe 0x79f51
movsd %xmm1, 0x68(%rsp)
movsd 0x38(%rsp), %xmm3
movsd 0x50(%rsp), %xmm1
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
ucomisd %xmm2, %xmm3
jbe 0x79f71
movsd %xmm2, 0x38(%rsp)
addsd %xmm0, %xmm1
ucomisd 0x70(%rsp), %xmm1
jbe 0x79f83
movsd %xmm1, 0x70(%rsp)
movsd 0x40(%rsp), %xmm3
movsd 0x58(%rsp), %xmm1
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
ucomisd %xmm2, %xmm3
jbe 0x79fa3
movsd %xmm2, 0x40(%rsp)
addsd %xmm1, %xmm0
ucomisd 0x78(%rsp), %xmm0
jbe 0x79fb5
movsd %xmm0, 0x78(%rsp)
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x79fd2
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movl %ebp, %r12d
movq 0x90(%rsp), %rdi
movq 0x98(%rsp), %rax
subq %rdi, %rax
sarq $0x2, %rax
incl %ebp
cmpq %r12, %rax
ja 0x79ca8
movq 0xd8(%rsp), %r14
movabsq $-0x5555555555555555, %r13 # imm = 0xAAAAAAAAAAAAAAAB
testq %rdi, %rdi
je 0x7a01e
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x98(%rbx), %rax
subq 0x90(%rbx), %rax
incl %r14d
sarq $0x2, %rax
imulq %r13, %rax
cmpq %r14, %rax
ja 0x79bf4
movsd 0x68(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
subsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd %xmm0, 0xc0(%rsp)
subsd 0x38(%rsp), %xmm1
movsd %xmm1, 0x88(%rsp)
movsd %xmm1, 0xc8(%rsp)
movsd 0x78(%rsp), %xmm0
subsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movsd %xmm0, 0xd0(%rsp)
movq 0x11cf22(%rip), %rdi    # 0x196fc0
leaq 0xdcd6d(%rip), %rsi     # 0x156e12
movl $0x14, %edx
callq 0x44e0
movsd 0x8(%rsp), %xmm0
mulsd 0x88(%rsp), %xmm0
mulsd 0x80(%rsp), %xmm0
movq 0x11cef2(%rip), %rdi    # 0x196fc0
movsd %xmm0, 0x8(%rsp)
callq 0x46b0
leaq 0xdb44e(%rip), %rsi     # 0x15552e
movl $0x1, %edx
movq %rax, %rdi
callq 0x44e0
xorl %r14d, %r14d
xorl %ebx, %ebx
callq 0x4150
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0xd945a(%rip), %xmm1    # 0x153560
divsd %xmm1, %xmm0
mulsd 0xc0(%rsp), %xmm0
addsd 0x30(%rsp), %xmm0
movsd %xmm0, 0xa8(%rsp)
callq 0x4150
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0xd942a(%rip), %xmm0    # 0x153560
mulsd 0xc8(%rsp), %xmm0
addsd 0x38(%rsp), %xmm0
movsd %xmm0, 0xb0(%rsp)
callq 0x4150
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0xd93fe(%rip), %xmm0    # 0x153560
mulsd 0xd0(%rsp), %xmm0
addsd 0x40(%rsp), %xmm0
movsd %xmm0, 0xb8(%rsp)
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
shlq $0x1b, %rcx
testq %rcx, %rcx
jle 0x7a201
xorl %r15d, %r15d
xorl %r12d, %r12d
movsd 0xa8(%rsp), %xmm0
movsd 0xb0(%rsp), %xmm1
movsd 0xb8(%rsp), %xmm2
movsd (%rax,%r15), %xmm3
movsd 0x8(%rax,%r15), %xmm4
movsd 0x10(%rax,%r15), %xmm5
callq 0x868fa
movq 0x10(%rsp), %rax
movsd 0x18(%rax,%r15), %xmm1
ucomisd %xmm0, %xmm1
ja 0x7a1fe
incq %r12
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
shrq $0x5, %rcx
movslq %ecx, %rcx
addq $0x20, %r15
cmpq %rcx, %r12
jl 0x7a196
jmp 0x7a201
incq %rbx
incq %r14
cmpq $0x186a0, %r14          # imm = 0x186A0
jne 0x7a0f2
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x7a228
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movl $0x186a0, %eax          # imm = 0x186A0
subq %rbx, %rax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
movsd 0x8(%rsp), %xmm0
mulsd %xmm1, %xmm0
divsd 0xdc916(%rip), %xmm0    # 0x156b60
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd5442(%rip), %rdi     # 0x14f6a5
movq %r12, %rsi
xorl %eax, %eax
callq 0x4630
leaq 0xd5431(%rip), %rdi     # 0x14f6a5
xorl %eax, %eax
callq 0x4630
leaq 0xd5423(%rip), %rdi     # 0x14f6a5
movq %r14, %rsi
xorl %eax, %eax
callq 0x4630
leaq 0xd5412(%rip), %rdi     # 0x14f6a5
movq %r14, %rsi
xorl %eax, %eax
callq 0x4630
jmp 0x7a2a7
jmp 0x7a2a7
jmp 0x7a2a7
jmp 0x7a2ae
jmp 0x7a2b3
movq %rax, %rbx
jmp 0x7a2f0
jmp 0x7a2ae
movq %rax, %rbx
jmp 0x7a2d3
movq %rax, %rbx
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x7a2d3
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x7a2f0
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x7a307
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq %rbx, %rdi
callq 0x4780
nop