// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movq %r9, 0x18(%rsp)
movq %r8, 0x8(%rsp)
movq %rcx, %r15
movq %rdi, %r13
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
leaq 0x40(%rsp), %rbx
movq %rax, (%rbx)
movsd 0xc0(%rdi), %xmm2
mulsd %xmm2, %xmm0
addsd 0xd8(%rdi), %xmm0
leaq 0x38(%rsp), %r14
movsd %xmm0, (%r14)
mulsd %xmm2, %xmm1
addsd 0xe0(%rdi), %xmm1
movq %rdx, %r12
leaq 0x30(%rsp), %rbp
movsd %xmm1, (%rbp)
addq $0x8, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %rbp, %rcx
callq 0x5894
movsd 0x118(%r13), %xmm1
movsd (%rbx), %xmm0
movsd 0xe8(%r13), %xmm3
movsd 0xf0(%r13), %xmm2
subsd %xmm0, %xmm3
movapd %xmm1, %xmm4
mulsd %xmm0, %xmm4
mulsd %xmm3, %xmm4
movapd %xmm3, %xmm6
mulsd %xmm3, %xmm6
movsd 0x120(%r13), %xmm5
movsd (%r14), %xmm3
subsd %xmm3, %xmm2
movapd %xmm5, %xmm7
mulsd %xmm3, %xmm7
mulsd %xmm2, %xmm7
mulsd %xmm2, %xmm2
mulsd %xmm5, %xmm2
mulsd %xmm1, %xmm6
addsd %xmm2, %xmm6
movsd 0x128(%r13), %xmm8
movsd 0xf8(%r13), %xmm9
movsd (%rbp), %xmm2
subsd %xmm2, %xmm9
movapd %xmm8, %xmm10
mulsd %xmm2, %xmm10
mulsd %xmm9, %xmm10
mulsd %xmm9, %xmm9
mulsd %xmm8, %xmm9
addsd %xmm6, %xmm9
addsd %xmm7, %xmm4
addsd %xmm4, %xmm10
addsd %xmm10, %xmm10
mulsd %xmm0, %xmm0
mulsd %xmm3, %xmm3
mulsd %xmm5, %xmm3
mulsd %xmm1, %xmm0
addsd %xmm3, %xmm0
mulsd %xmm2, %xmm2
mulsd %xmm8, %xmm2
addsd %xmm0, %xmm2
addsd 0x53dd(%rip), %xmm2     # 0xd470
movsd 0x53dd(%rip), %xmm1     # 0xd478
mulsd %xmm9, %xmm1
mulsd %xmm2, %xmm1
movapd %xmm10, %xmm0
mulsd %xmm10, %xmm0
addsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x82bd
movsd %xmm0, 0x10(%rsp)
jb 0x80ce
sqrtsd %xmm0, %xmm0
jmp 0x80ef
movsd %xmm10, 0x28(%rsp)
movsd %xmm9, 0x20(%rsp)
callq 0x4190
movsd 0x20(%rsp), %xmm9
movsd 0x28(%rsp), %xmm10
movq 0x8(%rsp), %rbp
subsd %xmm10, %xmm0
addsd %xmm9, %xmm9
divsd %xmm9, %xmm0
movsd 0x4f3d(%rip), %xmm1     # 0xd048
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
movsd 0x40(%rsp), %xmm3
mulsd %xmm2, %xmm3
movsd 0xe8(%r13), %xmm4
mulsd %xmm0, %xmm4
addsd %xmm3, %xmm4
movsd %xmm4, (%r12)
movsd 0x38(%rsp), %xmm3
mulsd %xmm2, %xmm3
movsd 0xf0(%r13), %xmm4
mulsd %xmm0, %xmm4
addsd %xmm3, %xmm4
movsd %xmm4, (%r15)
mulsd 0x30(%rsp), %xmm2
movq 0x80(%rsp), %rbx
mulsd 0xf8(%r13), %xmm0
addsd %xmm2, %xmm0
movsd %xmm0, (%rbp)
movsd 0x100(%r13), %xmm2
ucomisd %xmm1, %xmm2
jne 0x81d6
jp 0x81d6
movsd 0x108(%r13), %xmm1
ucomisd 0x4eb2(%rip), %xmm1     # 0xd048
jne 0x81d6
jp 0x81d6
movsd 0x110(%r13), %xmm1
ucomisd 0x4e9d(%rip), %xmm1     # 0xd048
jne 0x81d6
jp 0x81d6
callq 0x4300
movq 0x18(%rsp), %rax
movsd %xmm0, (%rax)
movsd (%r15), %xmm0
movsd (%r12), %xmm1
callq 0x4050
movsd %xmm0, (%rbx)
jmp 0x82b3
movsd 0x118(%r13), %xmm1
movsd 0x120(%r13), %xmm0
mulsd (%r12), %xmm1
mulsd %xmm1, %xmm1
mulsd (%r15), %xmm0
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x820f
sqrtsd %xmm0, %xmm0
jmp 0x8214
callq 0x4190
movsd 0x128(%r13), %xmm1
mulsd (%rbp), %xmm1
divsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x41c0
movq 0x18(%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 0x4380
movsd %xmm0, 0x8(%rsp)
movsd (%r14), %xmm0
callq 0x4380
mulsd 0x8(%rsp), %xmm0
movsd %xmm0, (%r12)
movsd (%rbx), %xmm0
callq 0x4070
movsd %xmm0, 0x8(%rsp)
movsd (%r14), %xmm0
callq 0x4380
mulsd 0x8(%rsp), %xmm0
movsd %xmm0, (%r15)
movsd (%r14), %xmm0
callq 0x4070
movsd %xmm0, (%rbp)
movsd 0x10(%rsp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
setae %al
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop