// File: /workspace/github_cpp_makefile_star2_O2/matthewarcus[P]mmps/transform.cpp
// Function: Perspective::ProjectInv(TransformParams const&, double, double, double&, double&) const
bool Perspective::ProjectInv(const TransformParams& params, 
			     double phi, double lambda, double& x, double& y) const
{
  (void(params));
  bool result = false;
  // Find where phi, lambda project to on the ellipsoid
  double x0,y0,z0;
  double nx,ny,nz;
  if (a == 1.0 && b == 1.0 && c == 1.0) {
    // Just a sphere
    x0 = cos(lambda) * cos(phi);
    y0 = sin(lambda) * cos(phi);
    z0 = sin(phi);
    minv.Apply(x0,y0,z0);
    nx = x0; ny = y0; nz = z0;
  } else {
    // The normal vector
    nx = cos(lambda);
    ny = sin(lambda);
    nz = tan(phi);

    // The actual vector
    double r = sqrt(1/(ia2 * sqr(nx) + ib2 * sqr(ny) + ic2 * sqr(nz)));
    x0 = nx * r * ia2;
    y0 = ny * r * ib2;
    z0 = nz * r * ic2;
    // And apply rotations
    minv.Apply(x0,y0,z0);
    minv.Apply(nx,ny,nz);
  }

  // Test for visibility - dot product of nx,ny,nz and the line
  // towards to viewpoint must be positive
  double p = (vx-x0) * nx + (vy-y0) * ny + (vz-z0) * nz;
  if (p >= 0) {
    // Project from (vx, vy, vz) through (x0,y0,z0) to (-1, y, z)
    double t = (1 + x0)/(x0 - vx);
    x = t * vy + (1-t) * y0; // Note change of axes
    y = t * vz + (1-t) * z0;
    x = (x - vy) * iscalefactor;
    y = (y - vz) * iscalefactor;
    result = true;
  }
  return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, %rbx
movq %rdx, %r15
movsd %xmm0, 0x8(%rsp)
movq %rdi, %r14
movsd 0x100(%rdi), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x108(%rdi), %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x110(%rdi), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd %xmm1, 0x10(%rsp)
movaps %xmm1, %xmm0
callq 0x43a0
movsd 0x50(%rsp), %xmm1
ucomisd 0x43ab(%rip), %xmm1     # 0xc048
movsd %xmm0, 0x18(%rsp)
jne 0x7d79
jp 0x7d79
movsd 0x48(%rsp), %xmm1
ucomisd 0x438b(%rip), %xmm1     # 0xc048
jne 0x7d79
jp 0x7d79
movsd 0x40(%rsp), %xmm1
ucomisd 0x4371(%rip), %xmm1     # 0xc048
jne 0x7d79
jp 0x7d79
movsd 0x8(%rsp), %xmm0
callq 0x43a0
movsd 0x18(%rsp), %xmm1
mulsd %xmm0, %xmm1
leaq 0x28(%rsp), %r12
movsd %xmm1, (%r12)
movsd 0x10(%rsp), %xmm0
callq 0x4070
movsd %xmm0, 0x18(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x43a0
mulsd 0x18(%rsp), %xmm0
leaq 0x20(%rsp), %r13
movsd %xmm0, (%r13)
movsd 0x8(%rsp), %xmm0
callq 0x4070
leaq 0x70(%rsp), %rbp
movsd %xmm0, (%rbp)
leaq 0x58(%r14), %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x55a2
movsd (%r12), %xmm6
movsd (%r13), %xmm3
movsd (%rbp), %xmm4
movapd %xmm4, %xmm0
movapd %xmm3, %xmm1
movapd %xmm6, %xmm2
jmp 0x7ed7
movsd %xmm0, 0x68(%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x4070
movsd %xmm0, 0x10(%rsp)
movsd %xmm0, 0x60(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x4110
movapd %xmm0, %xmm2
movsd %xmm0, 0x58(%rsp)
movsd 0x18(%rsp), %xmm0
mulsd %xmm0, %xmm0
movsd 0x10(%rsp), %xmm1
mulsd %xmm1, %xmm1
mulsd 0x138(%r14), %xmm1
mulsd 0x130(%r14), %xmm0
addsd %xmm1, %xmm0
movapd %xmm2, %xmm1
mulsd %xmm2, %xmm1
mulsd 0x140(%r14), %xmm1
addsd %xmm0, %xmm1
movsd 0x4256(%rip), %xmm0     # 0xc048
divsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movq %rbx, 0x38(%rsp)
movq %r15, 0x30(%rsp)
jb 0x7e10
sqrtsd %xmm0, %xmm0
jmp 0x7e21
movsd %xmm2, 0x8(%rsp)
callq 0x4190
movsd 0x8(%rsp), %xmm2
movsd 0x18(%rsp), %xmm1
mulsd %xmm0, %xmm1
mulsd 0x130(%r14), %xmm1
leaq 0x28(%rsp), %rax
movsd %xmm1, (%rax)
movq %rax, %rsi
movsd 0x10(%rsp), %xmm1
mulsd %xmm0, %xmm1
mulsd 0x138(%r14), %xmm1
leaq 0x20(%rsp), %rax
movsd %xmm1, (%rax)
movq %rax, %rdx
mulsd %xmm0, %xmm2
mulsd 0x140(%r14), %xmm2
leaq 0x70(%rsp), %rbp
movsd %xmm2, (%rbp)
leaq 0x58(%r14), %rbx
movq %rbx, %rdi
movq %rbp, %rcx
callq 0x55a2
leaq 0x68(%rsp), %r15
leaq 0x60(%rsp), %r12
leaq 0x58(%rsp), %r13
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x55a2
leaq 0x28(%rsp), %rax
movsd (%rax), %xmm2
movsd (%r15), %xmm6
leaq 0x20(%rsp), %rax
movsd (%rax), %xmm1
movsd (%r12), %xmm3
movsd (%rbp), %xmm0
movsd (%r13), %xmm4
movq 0x38(%rsp), %rbx
movq 0x30(%rsp), %r15
movsd 0xd0(%r14), %xmm7
movsd 0xd8(%r14), %xmm5
movapd %xmm7, %xmm8
subsd %xmm2, %xmm8
mulsd %xmm6, %xmm8
movapd %xmm5, %xmm6
subsd %xmm1, %xmm6
mulsd %xmm3, %xmm6
addsd %xmm8, %xmm6
movsd 0xe0(%r14), %xmm3
subsd %xmm0, %xmm3
mulsd %xmm4, %xmm3
addsd %xmm6, %xmm3
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm3
jb 0x7fa7
movsd 0x4118(%rip), %xmm6     # 0xc048
movapd %xmm2, %xmm8
addsd %xmm6, %xmm8
subsd %xmm7, %xmm2
divsd %xmm2, %xmm8
subsd %xmm8, %xmm6
mulsd %xmm6, %xmm1
mulsd %xmm8, %xmm5
addsd %xmm1, %xmm5
movsd %xmm5, (%r15)
mulsd 0xe0(%r14), %xmm8
mulsd %xmm0, %xmm6
addsd %xmm6, %xmm8
movsd %xmm8, (%rbx)
movsd (%r15), %xmm0
subsd 0xd8(%r14), %xmm0
mulsd 0xc8(%r14), %xmm0
movsd %xmm0, (%r15)
movsd (%rbx), %xmm0
subsd 0xe0(%r14), %xmm0
mulsd 0xc8(%r14), %xmm0
movsd %xmm0, (%rbx)
ucomisd %xmm4, %xmm3
setae %al
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop