// File: matthewarcus[P]mmps/transform.cpp
// Function: Perspective::Project(TransformParams const&, double, double, double&, double&, double&, double&, double&) const
bool Perspective::Project(const TransformParams& params,
			  double x0, double y0, 
			  double& x, double& y, double& z,
			  double& phi, double& lambda) const
{
  (void(params));
  bool result = false;
  // Apply our rotation to the point we are projecting to
  double x1 = -1;
  double y1 = scalefactor * x0 + vy;
  double z1 = scalefactor * y0 + vz; 
  m.Apply(x1,y1,z1);

  // Projecting from (rx, ry, rz) to point (x1, y1, z1)
  // Solve a quadratic obtained from equating line equation with r = 1
  double qa = a2 * sqr(rx-x1) + b2 * sqr(ry-y1) + c2 * sqr(rz-z1);
  double qb = 2 * (a2 * x1 * (rx-x1) + b2 * y1 * (ry-y1) + c2 * z1 * (rz-z1));
  double qc = a2 * sqr(x1) + b2 * sqr(y1) + c2 * sqr(z1) - 1;
  double qm = qb * qb - 4 * qa * qc;
  if (qm >=  0) {
    // Since qa is always positive, the + solution is nearest to the point
    // of view, so we always use that one.
    double k = (-qb + sqrt(qm)) / (2*qa);
    x = k * rx + (1-k) * x1;
    y = k * ry + (1-k) * y1;
    z = k * rz + (1-k) * z1;
    if (a == 1.0 && b == 1.0 && c == 1.0) {
      phi = asin(z);
      lambda = atan2(y, x);
    } else {
      // This is a point on the ellipsoid, so convert to lat long
      double r = sqrt(sqr(a2 * x) + sqr(b2 * y));
      phi = atan(c2 * z / r);
      lambda = atan2(b2*y, a2*x);
      // Now return the spherical x, y, z corresponding to phi, lambda
      x = cos(lambda) * cos(phi);
      y = sin(lambda) * cos(phi);
      z = sin(phi);
    }
    result = true;
  }
  return result;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
movsd %xmm1, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movq %r9, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xb0(%rbp)
movb $0x0, -0x41(%rbp)
movsd 0x9c70(%rip), %xmm0     # 0x15428
movsd %xmm0, -0x50(%rbp)
movsd 0xc0(%rdi), %xmm0
movsd -0x18(%rbp), %xmm2
movsd 0xd8(%rdi), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movsd 0xc0(%rdi), %xmm0
movsd -0x20(%rbp), %xmm2
movsd 0xe0(%rdi), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
addq $0x8, %rdi
leaq -0x50(%rbp), %rsi
leaq -0x58(%rbp), %rdx
leaq -0x60(%rbp), %rcx
callq 0x7da0
movq -0xb0(%rbp), %rax
movsd 0x118(%rax), %xmm0
movsd %xmm0, -0xd8(%rbp)
movsd 0xe8(%rax), %xmm0
subsd -0x50(%rbp), %xmm0
callq 0xddd0
movq -0xb0(%rbp), %rax
movsd %xmm0, -0xe0(%rbp)
movsd 0x120(%rax), %xmm0
movsd %xmm0, -0xe8(%rbp)
movsd 0xf0(%rax), %xmm0
subsd -0x58(%rbp), %xmm0
callq 0xddd0
movsd -0xe8(%rbp), %xmm1
movsd -0xe0(%rbp), %xmm2
movq -0xb0(%rbp), %rax
movaps %xmm0, %xmm3
movsd -0xd8(%rbp), %xmm0
mulsd %xmm3, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xd0(%rbp)
movsd 0x128(%rax), %xmm0
movsd %xmm0, -0xc8(%rbp)
movsd 0xf8(%rax), %xmm0
subsd -0x60(%rbp), %xmm0
callq 0xddd0
movsd -0xd0(%rbp), %xmm1
movq -0xb0(%rbp), %rax
movaps %xmm0, %xmm2
movsd -0xc8(%rbp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movsd 0x118(%rax), %xmm0
mulsd -0x50(%rbp), %xmm0
movsd 0xe8(%rax), %xmm2
subsd -0x50(%rbp), %xmm2
movsd 0x120(%rax), %xmm1
mulsd -0x58(%rbp), %xmm1
movsd 0xf0(%rax), %xmm3
subsd -0x58(%rbp), %xmm3
mulsd %xmm3, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x128(%rax), %xmm1
mulsd -0x60(%rbp), %xmm1
movsd 0xf8(%rax), %xmm2
subsd -0x60(%rbp), %xmm2
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0x9ac7(%rip), %xmm0     # 0x15420
mulsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
movsd 0x118(%rax), %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0x50(%rbp), %xmm0
callq 0xddd0
movq -0xb0(%rbp), %rax
movsd %xmm0, -0xb8(%rbp)
movsd 0x120(%rax), %xmm0
movsd %xmm0, -0xc0(%rbp)
movsd -0x58(%rbp), %xmm0
callq 0xddd0
movsd -0xc0(%rbp), %xmm1
movsd -0xb8(%rbp), %xmm2
movq -0xb0(%rbp), %rax
movaps %xmm0, %xmm3
movsd -0xa8(%rbp), %xmm0
mulsd %xmm3, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd 0x128(%rax), %xmm0
movsd %xmm0, -0x98(%rbp)
movsd -0x60(%rbp), %xmm0
callq 0xddd0
movsd -0xa0(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0x98(%rbp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x9630(%rip), %xmm1     # 0x15048
subsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x70(%rbp), %xmm0
movsd -0x70(%rbp), %xmm2
movsd 0x99fd(%rip), %xmm1     # 0x15430
mulsd -0x68(%rbp), %xmm1
mulsd -0x78(%rbp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
movsd -0x80(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0xbd43
movsd -0x70(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0xf0(%rbp)
movsd -0x80(%rbp), %xmm0
callq 0x5200
movq -0xb0(%rbp), %rax
movaps %xmm0, %xmm1
movsd -0xf0(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd 0x9961(%rip), %xmm1     # 0x15420
mulsd -0x68(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x88(%rbp), %xmm0
movsd 0xe8(%rax), %xmm2
movsd 0x9560(%rip), %xmm1     # 0x15048
subsd -0x88(%rbp), %xmm1
mulsd -0x50(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0x28(%rbp), %rcx
movsd %xmm0, (%rcx)
movsd -0x88(%rbp), %xmm0
movsd 0xf0(%rax), %xmm2
movsd 0x952b(%rip), %xmm1     # 0x15048
subsd -0x88(%rbp), %xmm1
mulsd -0x58(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0x30(%rbp), %rcx
movsd %xmm0, (%rcx)
movsd -0x88(%rbp), %xmm0
movsd 0xf8(%rax), %xmm2
movsd 0x94f6(%rip), %xmm1     # 0x15048
subsd -0x88(%rbp), %xmm1
mulsd -0x60(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0x38(%rbp), %rcx
movsd %xmm0, (%rcx)
movsd 0x100(%rax), %xmm0
movsd 0x94c9(%rip), %xmm1     # 0x15048
ucomisd %xmm1, %xmm0
jne 0xbbfc
jp 0xbbfc
movq -0xb0(%rbp), %rax
movsd 0x108(%rax), %xmm0
movsd 0x94aa(%rip), %xmm1     # 0x15048
ucomisd %xmm1, %xmm0
jne 0xbbfc
jp 0xbbfc
movq -0xb0(%rbp), %rax
movsd 0x110(%rax), %xmm0
movsd 0x948b(%rip), %xmm1     # 0x15048
ucomisd %xmm1, %xmm0
jne 0xbbfc
jp 0xbbfc
movq -0x38(%rbp), %rax
movsd (%rax), %xmm0
callq 0x53e0
movq -0x40(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x30(%rbp), %rax
movsd (%rax), %xmm0
movq -0x28(%rbp), %rax
movsd (%rax), %xmm1
callq 0x5050
movq 0x10(%rbp), %rax
movsd %xmm0, (%rax)
jmp 0xbd3f
movq -0xb0(%rbp), %rax
movsd 0x118(%rax), %xmm0
movq -0x28(%rbp), %rax
mulsd (%rax), %xmm0
callq 0xddd0
movq -0xb0(%rbp), %rax
movsd %xmm0, -0x108(%rbp)
movsd 0x120(%rax), %xmm0
movq -0x30(%rbp), %rax
mulsd (%rax), %xmm0
callq 0xddd0
movaps %xmm0, %xmm1
movsd -0x108(%rbp), %xmm0
addsd %xmm1, %xmm0
callq 0x5200
movq -0xb0(%rbp), %rax
movsd %xmm0, -0x90(%rbp)
movsd 0x128(%rax), %xmm0
movq -0x38(%rbp), %rax
mulsd (%rax), %xmm0
divsd -0x90(%rbp), %xmm0
callq 0x5240
movq -0xb0(%rbp), %rax
movq -0x40(%rbp), %rcx
movsd %xmm0, (%rcx)
movsd 0x120(%rax), %xmm0
movq -0x30(%rbp), %rcx
mulsd (%rcx), %xmm0
movsd 0x118(%rax), %xmm1
movq -0x28(%rbp), %rax
mulsd (%rax), %xmm1
callq 0x5050
movq 0x10(%rbp), %rax
movsd %xmm0, (%rax)
movq 0x10(%rbp), %rax
movsd (%rax), %xmm0
callq 0x5470
movsd %xmm0, -0x100(%rbp)
movq -0x40(%rbp), %rax
movsd (%rax), %xmm0
callq 0x5470
movaps %xmm0, %xmm1
movsd -0x100(%rbp), %xmm0
mulsd %xmm1, %xmm0
movq -0x28(%rbp), %rax
movsd %xmm0, (%rax)
movq 0x10(%rbp), %rax
movsd (%rax), %xmm0
callq 0x5080
movsd %xmm0, -0xf8(%rbp)
movq -0x40(%rbp), %rax
movsd (%rax), %xmm0
callq 0x5470
movaps %xmm0, %xmm1
movsd -0xf8(%rbp), %xmm0
mulsd %xmm1, %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x40(%rbp), %rax
movsd (%rax), %xmm0
callq 0x5080
movq -0x38(%rbp), %rax
movsd %xmm0, (%rax)
movb $0x1, -0x41(%rbp)
movb -0x41(%rbp), %al
andb $0x1, %al
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw %cs:(%rax,%rax)