// File: /workspace/github_cpp_makefile_star2_O2/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 $0x188, %rsp            # imm = 0x188
movq %rdi, %rbx
leaq 0x30(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x6402e
movq %rsp, %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x6402e
leaq 0xc0(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x6402e
leaq 0x98(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x6402e
movl $0x3b4ca6f5, %edi       # imm = 0x3B4CA6F5
callq 0x4460
leaq 0x70(%rsp), %rax
andq $0x0, 0x10(%rax)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rax)
leaq 0x90(%rbx), %rax
movq %rax, 0xd8(%rsp)
leaq 0xa8(%rbx), %rax
movq %rax, 0xe0(%rsp)
leaq 0x60(%rbx), %rbp
xorl %esi, %esi
leaq 0xe8(%rsp), %r13
leaq 0x140(%rsp), %r12
leaq 0x50(%rsp), %r14
movl %esi, %esi
movq 0x98(%rbx), %rax
subq 0x90(%rbx), %rax
cqto
pushq $0xc
popq %rcx
idivq %rcx
cmpq %rsi, %rax
jbe 0x5a9c2
movq 0xe0(%rsp), %rdi
movq %rsi, 0x90(%rsp)
callq 0x4bbec
movq %r13, %rdi
movq %rax, %rsi
callq 0xa6ce
movq 0xd8(%rsp), %rdi
movq 0x90(%rsp), %rsi
callq 0x576b6
cvtsi2sdl (%rax), %xmm0
cvtsi2sdl 0x4(%rax), %xmm1
cvtsi2sdl 0x8(%rax), %xmm2
movapd %xmm2, 0x130(%rsp)
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
movapd %xmm2, 0x120(%rsp)
movapd %xmm1, 0xb0(%rsp)
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
movapd %xmm1, 0x110(%rsp)
movapd %xmm0, 0x20(%rsp)
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movapd %xmm0, 0x100(%rsp)
xorl %r15d, %r15d
movl %r15d, %r15d
movq 0xf0(%rsp), %rax
subq 0xe8(%rsp), %rax
sarq $0x2, %rax
cmpq %r15, %rax
jbe 0x5a9ab
movq %r13, %rdi
movq %r15, %rsi
callq 0xac00
movslq (%rax), %rsi
movq %rbp, %rdi
callq 0xabe2
movq %r12, %rdi
movq %rax, %rsi
callq 0x29ebe
cmpl $0x0, 0x108(%rbx)
jle 0x5a7eb
movq %r13, %rdi
movq %r15, %rsi
callq 0xac00
movslq (%rax), %rsi
movq %rbp, %rdi
callq 0xabe2
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x50(%rsp)
movq %r13, %rdi
movq %r15, %rsi
callq 0xac00
movslq (%rax), %rsi
movq %rbp, %rdi
callq 0xabe2
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x58(%rsp)
movq %r13, %rdi
movq %r15, %rsi
callq 0xac00
movslq (%rax), %rsi
movq %rbp, %rdi
callq 0xabe2
movsd 0x18(%rax), %xmm0
jmp 0x5a87a
movupd 0x148(%rsp), %xmm0
movupd 0xc0(%rbx), %xmm1
mulpd 0x100(%rsp), %xmm1
addpd %xmm0, %xmm1
movupd 0xd8(%rbx), %xmm0
mulpd 0x110(%rsp), %xmm0
addpd %xmm1, %xmm0
movupd 0xf0(%rbx), %xmm1
mulpd 0x120(%rsp), %xmm1
addpd %xmm0, %xmm1
movapd %xmm1, 0x50(%rsp)
movsd 0xd0(%rbx), %xmm0
mulsd 0x20(%rsp), %xmm0
addsd 0x158(%rsp), %xmm0
movsd 0xe8(%rbx), %xmm1
mulsd 0xb0(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd 0x100(%rbx), %xmm0
mulsd 0x130(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x178(%rsp), %xmm0
mulsd 0xa68af(%rip), %xmm0    # 0x101140
movsd %xmm0, 0x68(%rsp)
leaq 0x70(%rsp), %rdi
movq %r14, %rsi
callq 0x37192
movq 0x78(%rsp), %rax
subq 0x70(%rsp), %rax
cmpq $0x20, %rax
jne 0x5a8fc
movsd 0x68(%rsp), %xmm0
movapd 0x50(%rsp), %xmm1
movapd %xmm0, %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
movapd %xmm1, %xmm3
subpd %xmm2, %xmm3
movapd %xmm3, (%rsp)
addpd %xmm1, %xmm2
movapd %xmm2, 0x30(%rsp)
movsd 0x60(%rsp), %xmm1
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
movsd %xmm2, 0x10(%rsp)
addsd %xmm1, %xmm0
jmp 0x5a990
movsd (%rsp), %xmm3
movsd 0x50(%rsp), %xmm1
movsd 0x68(%rsp), %xmm0
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
ucomisd %xmm2, %xmm3
jbe 0x5a920
movsd %xmm2, (%rsp)
addsd %xmm0, %xmm1
ucomisd 0x30(%rsp), %xmm1
jbe 0x5a932
movsd %xmm1, 0x30(%rsp)
movsd 0x8(%rsp), %xmm3
movsd 0x58(%rsp), %xmm1
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
ucomisd %xmm2, %xmm3
jbe 0x5a952
movsd %xmm2, 0x8(%rsp)
addsd %xmm0, %xmm1
ucomisd 0x38(%rsp), %xmm1
jbe 0x5a964
movsd %xmm1, 0x38(%rsp)
movsd 0x10(%rsp), %xmm3
movsd 0x60(%rsp), %xmm1
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
ucomisd %xmm2, %xmm3
jbe 0x5a984
movsd %xmm2, 0x10(%rsp)
addsd %xmm1, %xmm0
ucomisd 0x40(%rsp), %xmm0
jbe 0x5a996
movsd %xmm0, 0x40(%rsp)
leaq 0x160(%rsp), %rdi
callq 0xaf14
incl %r15d
jmp 0x5a73f
movq %r13, %rdi
callq 0xb290
movq 0x90(%rsp), %rsi
incl %esi
jmp 0x5a699
movapd 0x30(%rsp), %xmm0
subpd (%rsp), %xmm0
movapd %xmm0, 0x20(%rsp)
movapd %xmm0, 0xc0(%rsp)
movsd 0x40(%rsp), %xmm0
subsd 0x10(%rsp), %xmm0
movsd %xmm0, 0xb0(%rsp)
movsd %xmm0, 0xd0(%rsp)
movq 0xf55bf(%rip), %rdi     # 0x14ffc0
leaq 0xa947a(%rip), %rsi     # 0x103e82
callq 0x4480
movapd 0x20(%rsp), %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
mulsd %xmm1, %xmm0
mulsd 0xb0(%rsp), %xmm0
movq %rax, %rdi
movapd %xmm0, 0x20(%rsp)
callq 0x46c0
leaq 0xa7b41(%rip), %rsi     # 0x10257e
movq %rax, %rdi
callq 0x4480
xorl %ebx, %ebx
pushq $-0x1
popq %r14
xorl %r15d, %r15d
cmpq $0x186a0, %r15          # imm = 0x186A0
je 0x5ab50
callq 0x4170
cvtsi2sd %eax, %xmm0
movsd 0xa5b14(%rip), %xmm1    # 0x100580
divsd %xmm1, %xmm0
mulsd 0xc0(%rsp), %xmm0
addsd (%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
callq 0x4170
cvtsi2sd %eax, %xmm0
divsd 0xa5ae8(%rip), %xmm0    # 0x100580
mulsd 0xc8(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
callq 0x4170
cvtsi2sd %eax, %xmm0
divsd 0xa5abf(%rip), %xmm0    # 0x100580
mulsd 0xd0(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
movsd %xmm0, 0xa8(%rsp)
movq 0x70(%rsp), %rax
movq %r14, %r12
xorl %r13d, %r13d
movq 0x78(%rsp), %rcx
subq %rax, %rcx
shrq $0x5, %rcx
movslq %ecx, %rcx
incq %r12
cmpq %rcx, %r12
jge 0x5ab48
movsd 0x98(%rsp), %xmm0
movsd 0xa0(%rsp), %xmm1
movsd 0xa8(%rsp), %xmm2
movsd (%rax,%r13), %xmm3
movsd 0x8(%rax,%r13), %xmm4
movsd 0x10(%rax,%r13), %xmm5
callq 0x64934
movq 0x70(%rsp), %rax
movsd 0x18(%rax,%r13), %xmm1
addq $0x20, %r13
ucomisd %xmm0, %xmm1
jbe 0x5aae4
incq %rbx
incq %r15
jmp 0x5aa4e
movl $0x186a0, %eax          # imm = 0x186A0
subq %rbx, %rax
cvtsi2sd %rax, %xmm0
movapd 0x20(%rsp), %xmm1
mulsd %xmm0, %xmm1
divsd 0xa9061(%rip), %xmm1    # 0x103bd0
movapd %xmm1, 0x20(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x3796e
movaps 0x20(%rsp), %xmm0
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5ab9c
jmp 0x5aba1
jmp 0x5ab9c
movq %rax, %rbx
jmp 0x5abc3
movq %rax, %rbx
jmp 0x5abb6
movq %rax, %rbx
leaq 0x160(%rsp), %rdi
callq 0xaf14
leaq 0xe8(%rsp), %rdi
callq 0xb290
leaq 0x70(%rsp), %rdi
callq 0x3796e
movq %rbx, %rdi
callq 0x4780
nop