// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, 0x10(%rsp)
movq %r8, 0x8(%rsp)
movq %rcx, %r15
movq %rdi, %r13
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
leaq 0x28(%rsp), %rbp
movq %rax, (%rbp)
movsd 0xc0(%rdi), %xmm2
mulsd %xmm2, %xmm0
addsd 0xd8(%rdi), %xmm0
leaq 0x20(%rsp), %r14
movsd %xmm0, (%r14)
mulsd %xmm2, %xmm1
addsd 0xe0(%rdi), %xmm1
movq %rdx, %r12
leaq 0x18(%rsp), %rbx
movsd %xmm1, (%rbx)
addq $0x8, %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x5ba0
movsd 0x118(%r13), %xmm0
movsd (%rbp), %xmm1
movsd 0xe8(%r13), %xmm7
movsd 0xf0(%r13), %xmm6
subsd %xmm1, %xmm7
movsd 0x120(%r13), %xmm3
movsd (%r14), %xmm5
subsd %xmm5, %xmm6
movsd 0x128(%r13), %xmm10
movsd 0xf8(%r13), %xmm4
movsd (%rbx), %xmm2
subsd %xmm2, %xmm4
movapd %xmm0, %xmm8
mulsd %xmm1, %xmm8
movapd %xmm3, %xmm9
mulsd %xmm5, %xmm9
mulsd %xmm6, %xmm9
mulsd %xmm7, %xmm8
addsd %xmm9, %xmm8
movapd %xmm10, %xmm9
mulsd %xmm2, %xmm9
mulsd %xmm4, %xmm9
addsd %xmm8, %xmm9
addsd %xmm9, %xmm9
unpcklpd %xmm7, %xmm1            # xmm1 = xmm1[0],xmm7[0]
mulpd %xmm1, %xmm1
unpcklpd %xmm6, %xmm5            # xmm5 = xmm5[0],xmm6[0]
mulpd %xmm5, %xmm5
unpcklpd %xmm3, %xmm3            # xmm3 = xmm3[0,0]
mulpd %xmm5, %xmm3
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd %xmm1, %xmm0
addpd %xmm3, %xmm0
unpcklpd %xmm4, %xmm2            # xmm2 = xmm2[0],xmm4[0]
mulpd %xmm2, %xmm2
unpcklpd %xmm10, %xmm10          # xmm10 = xmm10[0,0]
mulpd %xmm2, %xmm10
addpd %xmm0, %xmm10
movsd 0x50fe(%rip), %xmm0     # 0xd460
addsd %xmm10, %xmm0
unpckhpd %xmm10, %xmm10          # xmm10 = xmm10[1,1]
movsd 0x50f4(%rip), %xmm1     # 0xd468
mulsd %xmm10, %xmm1
mulsd %xmm0, %xmm1
movapd %xmm9, %xmm0
mulsd %xmm9, %xmm0
addsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x857b
movapd %xmm0, 0x30(%rsp)
jb 0x83a7
sqrtsd %xmm0, %xmm0
jmp 0x83c8
movapd %xmm10, 0x50(%rsp)
movapd %xmm9, 0x40(%rsp)
callq 0x4180
movapd 0x40(%rsp), %xmm9
movapd 0x50(%rsp), %xmm10
movq 0x8(%rsp), %rbp
subsd %xmm9, %xmm0
addsd %xmm10, %xmm10
divsd %xmm10, %xmm0
movsd 0x4c64(%rip), %xmm1     # 0xd048
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
movsd 0x28(%rsp), %xmm3
mulsd %xmm2, %xmm3
movsd 0xe8(%r13), %xmm4
mulsd %xmm0, %xmm4
addsd %xmm3, %xmm4
movsd %xmm4, (%r12)
movsd 0x20(%rsp), %xmm3
mulsd %xmm2, %xmm3
movsd 0xf0(%r13), %xmm4
mulsd %xmm0, %xmm4
addsd %xmm3, %xmm4
movsd %xmm4, (%r15)
mulsd 0x18(%rsp), %xmm2
movq 0xa0(%rsp), %rbx
mulsd 0xf8(%r13), %xmm0
addsd %xmm2, %xmm0
movsd %xmm0, (%rbp)
movsd 0x100(%r13), %xmm2
ucomisd %xmm1, %xmm2
jne 0x84af
jp 0x84af
movsd 0x108(%r13), %xmm1
ucomisd 0x4bd9(%rip), %xmm1     # 0xd048
jne 0x84af
jp 0x84af
movsd 0x110(%r13), %xmm1
ucomisd 0x4bc4(%rip), %xmm1     # 0xd048
jne 0x84af
jp 0x84af
callq 0x42f0
movq 0x10(%rsp), %rax
movsd %xmm0, (%rax)
movsd (%r15), %xmm0
movsd (%r12), %xmm1
callq 0x4050
movsd %xmm0, (%rbx)
jmp 0x8571
movupd 0x118(%r13), %xmm1
movsd (%r12), %xmm2
movhpd (%r15), %xmm2           # xmm2 = xmm2[0],mem[0]
mulpd %xmm1, %xmm2
mulpd %xmm2, %xmm2
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
addsd %xmm2, %xmm1
sqrtsd %xmm1, %xmm1
mulsd 0x128(%r13), %xmm0
divsd %xmm1, %xmm0
callq 0x41b0
movq 0x10(%rsp), %r14
movsd %xmm0, (%r14)
movsd 0x118(%r13), %xmm1
movsd 0x120(%r13), %xmm0
mulsd (%r15), %xmm0
mulsd (%r12), %xmm1
callq 0x4050
movsd %xmm0, (%rbx)
callq 0x4370
movsd %xmm0, 0x8(%rsp)
movsd (%r14), %xmm0
callq 0x4370
mulsd 0x8(%rsp), %xmm0
movsd %xmm0, (%r12)
movsd (%rbx), %xmm0
callq 0x4070
movsd %xmm0, 0x8(%rsp)
movsd (%r14), %xmm0
callq 0x4370
mulsd 0x8(%rsp), %xmm0
movsd %xmm0, (%r15)
movsd (%r14), %xmm0
callq 0x4070
movsd %xmm0, (%rbp)
movapd 0x30(%rsp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
setae %al
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop