// File: Luthaf[P]zeopp/ray.cc
// Function: rayTraceToSphere(ATOM_NETWORK*, std::vector<Sphere, std::allocator<Sphere>>&, ray, std::vector<Plane, std::allocator<Plane>>&, hitdata&)
void rayTraceToSphere(ATOM_NETWORK *cell,vector<Sphere>& spheres,ray r, vector<Plane>& faces,hitdata& hitobject)
{
  
#if DEBUG
  cout << "Current Ray Length: " << hitobject.dist << endl;
#endif
  
  if (hitobject.dist > MAXRAYDIST){
    return;
  }
  
  

  //Find the closest atom that it will hit
  hitdata hitsphere = findClosestSphere(spheres,r);
  
  //If I hit an Sphere great!
  if (hitsphere.hit == true)
    {
      if (hitsphere.id == hitobject.id)
	{
	  //cerr << "Just hit same atom this is not allowable" << endl;
	}
      hitobject.hit = true;
      hitobject.hitpoint = hitsphere.hitpoint;
      hitobject.dist += hitsphere.dist;
      hitobject.id = hitsphere.id;
      hitobject.object = hitsphere.object;
      return;
    }
  //If I didn't hit a Sphere then I must hit a unitcell face
  hitsphere = findClosestPlane(faces,r);
  
  assert(hitsphere.hit==true); //Thus it hit a plane

  hitobject.hit = true;
  hitobject.hitpoint = hitsphere.hitpoint;
  hitobject.dist += hitsphere.dist;
  hitobject.id = -1;
  hitobject.object = NULL;
  
  //I am moving the point just slightly outside of the unit cell to be shifted back in
  r.base = cell->shiftXYZInUC(hitobject.hitpoint+r.vector.scale(threshold));
  
  //this is a recursive call to get the total length of the ray
  rayTraceToSphere(cell,spheres,r,faces,hitobject);
  return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
leaq 0x10(%rbp), %rax
movq %rax, -0x110(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x20(%rbp), %rax
movsd 0x20(%rax), %xmm0
movsd 0x164009(%rip), %xmm1   # 0x1f2040
ucomisd %xmm1, %xmm0
jbe 0x8e042
jmp 0x8e294
movq -0x110(%rbp), %rdx
movq -0x10(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x8c600
movb -0x58(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x8e0c2
movl -0x30(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jne 0x8e071
jmp 0x8e071
movq -0x20(%rbp), %rax
movb $0x1, (%rax)
movq -0x20(%rbp), %rax
movq -0x50(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x48(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x40(%rbp), %rcx
movq %rcx, 0x18(%rax)
movsd -0x38(%rbp), %xmm0
movq -0x20(%rbp), %rax
addsd 0x20(%rax), %xmm0
movsd %xmm0, 0x20(%rax)
movl -0x30(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x30(%rax)
jmp 0x8e294
movq -0x110(%rbp), %rdx
movq -0x18(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x8c790
leaq -0x58(%rbp), %rdi
leaq -0x90(%rbp), %rsi
movl $0x38, %edx
callq 0x5430
movb -0x58(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x8e0fd
jmp 0x8e11c
leaq 0x16943b(%rip), %rdi    # 0x1f753f
leaq 0x169323(%rip), %rsi    # 0x1f742e
movl $0x195, %edx            # imm = 0x195
leaq 0x16943c(%rip), %rcx    # 0x1f7553
callq 0x5320
movq -0x110(%rbp), %rsi
movq -0x20(%rbp), %rax
movb $0x1, (%rax)
movq -0x20(%rbp), %rax
movq -0x50(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x48(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x40(%rbp), %rcx
movq %rcx, 0x18(%rax)
movsd -0x38(%rbp), %xmm0
movq -0x20(%rbp), %rax
addsd 0x20(%rax), %xmm0
movsd %xmm0, 0x20(%rax)
movq -0x20(%rbp), %rax
movl $0xffffffff, 0x28(%rax) # imm = 0xFFFFFFFF
movq -0x20(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0x128(%rbp)
movq -0x20(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x130(%rbp)
addq $0x18, %rsi
leaq -0xd8(%rbp), %rdi
movsd 0x1691fb(%rip), %xmm0   # 0x1f7398
callq 0xea000
movq -0x130(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
leaq -0xd8(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0xea420
movq -0x128(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
leaq -0xc0(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x97f20
movq -0x110(%rbp), %rsi
movq -0xa8(%rbp), %rax
movq %rax, (%rsi)
movq -0xa0(%rbp), %rax
movq %rax, 0x8(%rsi)
movq -0x98(%rbp), %rax
movq %rax, 0x10(%rsi)
movq -0x8(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x118(%rbp)
leaq -0x108(%rbp), %rdi
movl $0x30, %edx
callq 0x5430
movq -0x120(%rbp), %rdi
movq -0x118(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movups -0x108(%rbp), %xmm0
movups -0xf8(%rbp), %xmm1
movups -0xe8(%rbp), %xmm2
movq %rsp, %rax
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x8e000
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
nopl (%rax)