// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movq %rcx, %rbx
movq %rdx, %r15
movsd %xmm0, 0x10(%rsp)
movq %rdi, %r14
movsd 0x100(%rdi), %xmm0
movsd %xmm0, (%rsp)
movsd 0x108(%rdi), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x110(%rdi), %xmm0
movsd %xmm0, 0x48(%rsp)
movsd %xmm1, 0x8(%rsp)
movaps %xmm1, %xmm0
callq 0x4380
movsd (%rsp), %xmm1
ucomisd 0x4d13(%rip), %xmm1     # 0xd048
jne 0x8415
jp 0x8415
movsd 0x50(%rsp), %xmm1
ucomisd 0x4cf9(%rip), %xmm1     # 0xd048
jne 0x8415
jp 0x8415
movsd 0x48(%rsp), %xmm1
ucomisd 0x4cdf(%rip), %xmm1     # 0xd048
jne 0x8415
jp 0x8415
movsd %xmm0, (%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x4380
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm1
leaq 0x40(%rsp), %r12
movsd %xmm1, (%r12)
movsd 0x8(%rsp), %xmm0
callq 0x4070
movsd %xmm0, 0x8(%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x4380
mulsd 0x8(%rsp), %xmm0
leaq 0x38(%rsp), %r13
movsd %xmm0, (%r13)
movsd 0x10(%rsp), %xmm0
callq 0x4070
leaq 0x30(%rsp), %rbp
movsd %xmm0, (%rbp)
leaq 0x58(%r14), %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x5894
movsd (%r12), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd (%r13), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd (%rbp), %xmm0
movsd %xmm0, 0x28(%rsp)
jmp 0x8526
movq %r15, (%rsp)
movsd %xmm0, 0x20(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x4070
movsd %xmm0, 0x18(%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x4110
movapd %xmm0, %xmm1
movsd %xmm0, 0x28(%rsp)
movsd 0x20(%rsp), %xmm0
mulsd %xmm0, %xmm0
movsd 0x18(%rsp), %xmm2
mulsd %xmm2, %xmm2
mulsd 0x138(%r14), %xmm2
mulsd 0x130(%r14), %xmm0
addsd %xmm2, %xmm0
mulsd %xmm1, %xmm1
mulsd 0x140(%r14), %xmm1
addsd %xmm0, %xmm1
movsd 0x4bc0(%rip), %xmm0     # 0xd048
divsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
movq %rbx, %r15
ucomisd %xmm1, %xmm0
jb 0x849f
sqrtsd %xmm0, %xmm0
jmp 0x84a4
callq 0x4190
leaq 0x20(%rsp), %r12
movsd (%r12), %xmm1
mulsd %xmm0, %xmm1
mulsd 0x130(%r14), %xmm1
leaq 0x40(%rsp), %rsi
movsd %xmm1, (%rsi)
leaq 0x18(%rsp), %r13
movsd (%r13), %xmm1
mulsd %xmm0, %xmm1
mulsd 0x138(%r14), %xmm1
leaq 0x38(%rsp), %rdx
movsd %xmm1, (%rdx)
leaq 0x28(%rsp), %rbp
mulsd (%rbp), %xmm0
mulsd 0x140(%r14), %xmm0
leaq 0x30(%rsp), %rcx
movsd %xmm0, (%rcx)
leaq 0x58(%r14), %rbx
movq %rbx, %rdi
callq 0x5894
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x5894
movq %r15, %rbx
movq (%rsp), %r15
movsd 0xd0(%r14), %xmm5
movsd 0xd8(%r14), %xmm2
movsd 0x40(%rsp), %xmm6
movapd %xmm5, %xmm1
subsd %xmm6, %xmm1
movsd 0x38(%rsp), %xmm3
movapd %xmm2, %xmm0
subsd %xmm3, %xmm0
mulsd 0x18(%rsp), %xmm0
mulsd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd 0xe0(%r14), %xmm0
movsd 0x30(%rsp), %xmm4
subsd %xmm4, %xmm0
mulsd 0x28(%rsp), %xmm0
addsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x860a
movsd 0x4ab5(%rip), %xmm7     # 0xd048
movapd %xmm6, %xmm8
addsd %xmm7, %xmm8
subsd %xmm5, %xmm6
divsd %xmm6, %xmm8
subsd %xmm8, %xmm7
mulsd %xmm7, %xmm3
mulsd %xmm8, %xmm2
addsd %xmm3, %xmm2
movsd %xmm2, (%r15)
mulsd 0xe0(%r14), %xmm8
mulsd %xmm4, %xmm7
addsd %xmm7, %xmm8
movsd %xmm8, (%rbx)
movsd (%r15), %xmm2
subsd 0xd8(%r14), %xmm2
mulsd 0xc8(%r14), %xmm2
movsd %xmm2, (%r15)
movsd (%rbx), %xmm2
subsd 0xe0(%r14), %xmm2
mulsd 0xc8(%r14), %xmm2
movsd %xmm2, (%rbx)
ucomisd %xmm1, %xmm0
setae %al
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq