// File: 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
movq %rsp, %rbp
subq $0x230, %rsp            # imm = 0x230
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x150(%rbp)
movq $0x0, -0x20(%rbp)
leaq -0x40(%rbp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0xea8e0
leaq -0x58(%rbp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0xea8e0
leaq -0x70(%rbp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0xea8e0
leaq -0x88(%rbp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0xea8e0
movl $0x3b4ca6f5, %edi       # imm = 0x3B4CA6F5
callq 0x5510
leaq -0xc0(%rbp), %rdi
callq 0x852e0
movl $0x0, -0xc4(%rbp)
movq -0x150(%rbp), %rdi
movl -0xc4(%rbp), %eax
movq %rax, -0x158(%rbp)
addq $0x90, %rdi
callq 0x706e0
movq %rax, %rcx
movq -0x158(%rbp), %rax
cmpq %rcx, %rax
jae 0xd91ae
movq -0x150(%rbp), %rdi
addq $0xa8, %rdi
movl -0xc4(%rbp), %eax
movl %eax, %esi
callq 0xaf5b0
movq %rax, -0x160(%rbp)
jmp 0xd8be4
movq -0x160(%rbp), %rsi
leaq -0xe0(%rbp), %rdi
callq 0xeec0
jmp 0xd8bf9
movq -0x150(%rbp), %rdi
addq $0x90, %rdi
movl -0xc4(%rbp), %eax
movl %eax, %esi
callq 0xcd720
movq %rax, -0x168(%rbp)
jmp 0xd8c1d
movq -0x168(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0xf8(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0xf0(%rbp)
movl $0x0, -0xfc(%rbp)
movl -0xfc(%rbp), %eax
movq %rax, -0x170(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x10090
movq %rax, %rcx
movq -0x170(%rbp), %rax
cmpq %rcx, %rax
jae 0xd917d
movq -0x150(%rbp), %rax
addq $0x60, %rax
movq %rax, -0x180(%rbp)
movl -0xfc(%rbp), %eax
movl %eax, %esi
leaq -0xe0(%rbp), %rdi
callq 0x101d0
movq %rax, -0x178(%rbp)
jmp 0xd8c9c
movq -0x180(%rbp), %rdi
movq -0x178(%rbp), %rax
movslq (%rax), %rsi
callq 0x10130
movq %rax, -0x188(%rbp)
jmp 0xd8cbb
movq -0x188(%rbp), %rsi
leaq -0x148(%rbp), %rdi
callq 0x64400
jmp 0xd8cd0
movq -0x150(%rbp), %rax
cmpl $0x1, 0x108(%rax)
jge 0xd8e26
movq -0x150(%rbp), %rax
movsd -0x140(%rbp), %xmm1
movsd 0xc0(%rax), %xmm0
cvtsi2sdl -0xf8(%rbp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0xd8(%rax), %xmm1
cvtsi2sdl -0xf4(%rbp), %xmm2
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0xf0(%rax), %xmm0
cvtsi2sdl -0xf0(%rbp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0x138(%rbp), %xmm1
movsd 0xc8(%rax), %xmm0
cvtsi2sdl -0xf8(%rbp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0xe0(%rax), %xmm1
cvtsi2sdl -0xf4(%rbp), %xmm2
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0xf8(%rax), %xmm0
cvtsi2sdl -0xf0(%rbp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd -0x130(%rbp), %xmm1
movsd 0xd0(%rax), %xmm0
cvtsi2sdl -0xf8(%rbp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0xe8(%rax), %xmm1
cvtsi2sdl -0xf4(%rbp), %xmm2
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0x100(%rax), %xmm0
cvtsi2sdl -0xf0(%rbp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x98(%rbp)
jmp 0xd8f71
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
jmp 0xd94a3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
jmp 0xd919d
movq -0x150(%rbp), %rax
addq $0x60, %rax
movq %rax, -0x198(%rbp)
movl -0xfc(%rbp), %eax
movl %eax, %esi
leaq -0xe0(%rbp), %rdi
callq 0x101d0
movq %rax, -0x190(%rbp)
jmp 0xd8e55
movq -0x198(%rbp), %rdi
movq -0x190(%rbp), %rax
movslq (%rax), %rsi
callq 0x10130
movq %rax, -0x1a0(%rbp)
jmp 0xd8e74
movq -0x150(%rbp), %rax
movq -0x1a0(%rbp), %rcx
movsd 0x8(%rcx), %xmm0
movsd %xmm0, -0xa8(%rbp)
addq $0x60, %rax
movq %rax, -0x1b0(%rbp)
movl -0xfc(%rbp), %eax
movl %eax, %esi
leaq -0xe0(%rbp), %rdi
callq 0x101d0
movq %rax, -0x1a8(%rbp)
jmp 0xd8eb7
movq -0x1b0(%rbp), %rdi
movq -0x1a8(%rbp), %rax
movslq (%rax), %rsi
callq 0x10130
movq %rax, -0x1b8(%rbp)
jmp 0xd8ed6
movq -0x150(%rbp), %rax
movq -0x1b8(%rbp), %rcx
movsd 0x10(%rcx), %xmm0
movsd %xmm0, -0xa0(%rbp)
addq $0x60, %rax
movq %rax, -0x1c8(%rbp)
movl -0xfc(%rbp), %eax
movl %eax, %esi
leaq -0xe0(%rbp), %rdi
callq 0x101d0
movq %rax, -0x1c0(%rbp)
jmp 0xd8f19
movq -0x1c8(%rbp), %rdi
movq -0x1c0(%rbp), %rax
movslq (%rax), %rsi
callq 0x10130
movq %rax, -0x1d0(%rbp)
jmp 0xd8f38
movq -0x1d0(%rbp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, -0x98(%rbp)
jmp 0xd8f71
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x100e0
jmp 0xd919d
movsd -0x110(%rbp), %xmm0
movsd 0x11e36f(%rip), %xmm1   # 0x1f72f0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x90(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x85360
jmp 0xd8fa2
leaq -0xc0(%rbp), %rdi
callq 0x85300
cmpq $0x1, %rax
jne 0xd903b
movsd -0xa8(%rbp), %xmm0
subsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0xa8(%rbp), %xmm0
addsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0xa0(%rbp), %xmm0
subsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0xa0(%rbp), %xmm0
addsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x98(%rbp), %xmm0
subsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x98(%rbp), %xmm0
addsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
jmp 0xd915d
movsd -0x58(%rbp), %xmm0
movsd -0xa8(%rbp), %xmm1
subsd -0x90(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0xd906b
movsd -0xa8(%rbp), %xmm0
subsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x40(%rbp), %xmm1
movsd -0xa8(%rbp), %xmm0
addsd -0x90(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xd909b
movsd -0xa8(%rbp), %xmm0
addsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0x50(%rbp), %xmm0
movsd -0xa0(%rbp), %xmm1
subsd -0x90(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0xd90cb
movsd -0xa0(%rbp), %xmm0
subsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x38(%rbp), %xmm1
movsd -0xa0(%rbp), %xmm0
addsd -0x90(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xd90fb
movsd -0xa0(%rbp), %xmm0
addsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x48(%rbp), %xmm0
movsd -0x98(%rbp), %xmm1
subsd -0x90(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0xd912b
movsd -0x98(%rbp), %xmm0
subsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x30(%rbp), %xmm1
movsd -0x98(%rbp), %xmm0
addsd -0x90(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xd915b
movsd -0x98(%rbp), %xmm0
addsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
jmp 0xd915d
leaq -0x148(%rbp), %rdi
callq 0x100e0
movl -0xfc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xfc(%rbp)
jmp 0xd8c41
leaq -0xe0(%rbp), %rdi
callq 0xec80
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0xd8b8d
leaq -0xe0(%rbp), %rdi
callq 0xec80
jmp 0xd94a3
movsd -0x40(%rbp), %xmm0
movsd -0x58(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x38(%rbp), %xmm0
movsd -0x50(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movsd -0x30(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
movsd -0x70(%rbp), %xmm0
movsd -0x68(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd -0x60(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x28(%rbp)
movq 0x199db6(%rip), %rdi    # 0x272fc0
leaq 0x122969(%rip), %rsi    # 0x1fbb7a
callq 0x5540
movq %rax, -0x1d8(%rbp)
jmp 0xd921f
movq -0x1d8(%rbp), %rdi
movsd -0x28(%rbp), %xmm0
callq 0x5980
movq %rax, -0x1e0(%rbp)
jmp 0xd9239
movq -0x1e0(%rbp), %rdi
leaq 0x1203de(%rip), %rsi    # 0x1f9625
callq 0x5540
jmp 0xd924e
movq $0x0, -0x10(%rbp)
cmpq $0x186a0, -0x10(%rbp)   # imm = 0x186A0
jge 0xd9451
callq 0x5190
cvtsi2sd %eax, %xmm0
movsd 0x118ddb(%rip), %xmm1   # 0x1f2050
mulsd %xmm1, %xmm0
movsd 0x11d387(%rip), %xmm1   # 0x1f6608
divsd %xmm1, %xmm0
movsd -0x70(%rbp), %xmm2
movsd -0x58(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
callq 0x5190
cvtsi2sd %eax, %xmm0
movsd 0x118da0(%rip), %xmm1   # 0x1f2050
mulsd %xmm1, %xmm0
movsd 0x11d34c(%rip), %xmm1   # 0x1f6608
divsd %xmm1, %xmm0
movsd -0x68(%rbp), %xmm2
movsd -0x50(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
callq 0x5190
cvtsi2sd %eax, %xmm0
movsd 0x118d68(%rip), %xmm1   # 0x1f2050
mulsd %xmm1, %xmm0
movsd 0x11d314(%rip), %xmm1   # 0x1f6608
divsd %xmm1, %xmm0
movsd -0x60(%rbp), %xmm2
movsd -0x48(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x1e8(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x85300
movq %rax, %rcx
movq -0x1e8(%rbp), %rax
movslq %ecx, %rcx
cmpq %rcx, %rax
jge 0xd943e
movsd -0x88(%rbp), %xmm0
movsd %xmm0, -0x218(%rbp)
movsd -0x80(%rbp), %xmm0
movsd %xmm0, -0x210(%rbp)
movsd -0x78(%rbp), %xmm0
movsd %xmm0, -0x208(%rbp)
movq -0x18(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
movq %rdi, -0x220(%rbp)
callq 0x88920
movq -0x220(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x200(%rbp)
movq -0x18(%rbp), %rsi
callq 0x88920
movq -0x220(%rbp), %rdi
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x1f8(%rbp)
movq -0x18(%rbp), %rsi
callq 0x88920
movsd -0x218(%rbp), %xmm0
movsd -0x210(%rbp), %xmm1
movsd -0x208(%rbp), %xmm2
movsd -0x200(%rbp), %xmm3
movsd -0x1f8(%rbp), %xmm4
movsd 0x10(%rax), %xmm5
callq 0xebaf0
movsd %xmm0, -0x1f0(%rbp)
jmp 0xd93fa
movq -0x18(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
callq 0x88920
movsd -0x1f0(%rbp), %xmm1
movsd 0x18(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xd942b
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0xd943e
jmp 0xd942d
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0xd9317
jmp 0xd9440
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x10(%rbp)
jmp 0xd9256
movsd -0x28(%rbp), %xmm0
movl $0x186a0, %eax          # imm = 0x186A0
subq -0x20(%rbp), %rax
cvtsi2sd %rax, %xmm1
mulsd %xmm1, %xmm0
movsd 0x122458(%rip), %xmm1   # 0x1fb8c8
divsd %xmm1, %xmm0
movsd %xmm0, -0x28(%rbp)
movsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x228(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x85400
movsd -0x228(%rbp), %xmm0
addq $0x230, %rsp            # imm = 0x230
popq %rbp
retq
leaq -0xc0(%rbp), %rdi
callq 0x85400
movq -0xe8(%rbp), %rdi
callq 0x58f0
nopl (%rax,%rax)