// File: matthewarcus[P]mmps/transform.cpp
// Function: Transform::Init(TransformParams const&)
void Transform::Init(const TransformParams& params)
{
  m.Identity();
  m = m * Matrix3(ZAxis, params.turn);
  m = m * Matrix3(XAxis, params.tilt);
  m = m * Matrix3(YAxis, params.lat);
  m = m * Matrix3(ZAxis, params.lon);

  // Construct the inverse matrix
  minv.Identity();
  minv = minv * Matrix3(ZAxis,-params.lon);
  minv = minv * Matrix3(YAxis,-params.lat);
  minv = minv * Matrix3(XAxis,-params.tilt);
  minv = minv * Matrix3(ZAxis,-params.turn);

  // Construct the sun parameters
  if (!params.sun) {
    sunx = 0.0;
    suny = 0.0;
    sunz = 0.0;
  } else {
    // We will use the astronomical day, starting at midnight (at Greenwich)
    // And start the year at the spring equinox
    // Need the vector to the sun.
    double date = params.date-80;
    while (date < 0) date += 365;
    while (date >= 365) date -= 365;
    date = 2 * pi *date / 365;
    double sheight = sunheight(date);
    double q = sqrt(1-sqr(sheight));
    // params.time is mean time, convert to apparent time
    double eot = equationoftime(date) * 2.0 * pi/(60.0 * 60.0 * 24.0);
    // AT = MT + EOT
    double atime = params.time + eot;
    sunx = -cos(atime) * q;
    suny =  sin(atime) * q;
    sunz =  sheight;
    // fprintf (stderr, "Sun: %f %f\n", degrees(asin(sunz)), hours(atan2(suny,-sunx)));
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x590, %rsp            # imm = 0x590
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x550(%rbp)
addq $0x8, %rdi
callq 0x76c0
movq -0x550(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x590(%rbp)
movq -0x10(%rbp), %rax
movsd 0x8(%rax), %xmm0
leaq -0xb0(%rbp), %rdi
movl $0x2, %esi
callq 0x7740
movq -0x590(%rbp), %rsi
leaq -0x60(%rbp), %rdi
leaq -0xb0(%rbp), %rdx
callq 0x7bb0
movq -0x550(%rbp), %rdi
addq $0x8, %rdi
leaq -0x60(%rbp), %rsi
movl $0x49, %edx
callq 0x5230
movq -0x550(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x588(%rbp)
movq -0x10(%rbp), %rax
movsd (%rax), %xmm0
leaq -0x150(%rbp), %rdi
xorl %esi, %esi
callq 0x7740
movq -0x588(%rbp), %rsi
leaq -0x100(%rbp), %rdi
leaq -0x150(%rbp), %rdx
callq 0x7bb0
movq -0x550(%rbp), %rdi
addq $0x8, %rdi
leaq -0x100(%rbp), %rsi
movl $0x49, %edx
callq 0x5230
movq -0x550(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x580(%rbp)
movq -0x10(%rbp), %rax
movsd 0x18(%rax), %xmm0
leaq -0x1f0(%rbp), %rdi
movl $0x1, %esi
callq 0x7740
movq -0x580(%rbp), %rsi
leaq -0x1a0(%rbp), %rdi
leaq -0x1f0(%rbp), %rdx
callq 0x7bb0
movq -0x550(%rbp), %rdi
addq $0x8, %rdi
leaq -0x1a0(%rbp), %rsi
movl $0x49, %edx
callq 0x5230
movq -0x550(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x578(%rbp)
movq -0x10(%rbp), %rax
movsd 0x20(%rax), %xmm0
leaq -0x290(%rbp), %rdi
movl $0x2, %esi
callq 0x7740
movq -0x578(%rbp), %rsi
leaq -0x240(%rbp), %rdi
leaq -0x290(%rbp), %rdx
callq 0x7bb0
movq -0x550(%rbp), %rdi
addq $0x8, %rdi
leaq -0x240(%rbp), %rsi
movl $0x49, %edx
callq 0x5230
movq -0x550(%rbp), %rdi
addq $0x58, %rdi
callq 0x76c0
movq -0x550(%rbp), %rax
addq $0x58, %rax
movq %rax, -0x570(%rbp)
movq -0x10(%rbp), %rax
movsd 0x20(%rax), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
leaq -0x330(%rbp), %rdi
movl $0x2, %esi
callq 0x7740
movq -0x570(%rbp), %rsi
leaq -0x2e0(%rbp), %rdi
leaq -0x330(%rbp), %rdx
callq 0x7bb0
movq -0x550(%rbp), %rdi
addq $0x58, %rdi
leaq -0x2e0(%rbp), %rsi
movl $0x49, %edx
callq 0x5230
movq -0x550(%rbp), %rax
addq $0x58, %rax
movq %rax, -0x568(%rbp)
movq -0x10(%rbp), %rax
movsd 0x18(%rax), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
leaq -0x3d0(%rbp), %rdi
movl $0x1, %esi
callq 0x7740
movq -0x568(%rbp), %rsi
leaq -0x380(%rbp), %rdi
leaq -0x3d0(%rbp), %rdx
callq 0x7bb0
movq -0x550(%rbp), %rdi
addq $0x58, %rdi
leaq -0x380(%rbp), %rsi
movl $0x49, %edx
callq 0x5230
movq -0x550(%rbp), %rax
addq $0x58, %rax
movq %rax, -0x560(%rbp)
movq -0x10(%rbp), %rax
movsd (%rax), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
leaq -0x470(%rbp), %rdi
xorl %esi, %esi
callq 0x7740
movq -0x560(%rbp), %rsi
leaq -0x420(%rbp), %rdi
leaq -0x470(%rbp), %rdx
callq 0x7bb0
movq -0x550(%rbp), %rdi
addq $0x58, %rdi
leaq -0x420(%rbp), %rsi
movl $0x49, %edx
callq 0x5230
movq -0x550(%rbp), %rax
addq $0x58, %rax
movq %rax, -0x558(%rbp)
movq -0x10(%rbp), %rax
movsd 0x8(%rax), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
leaq -0x510(%rbp), %rdi
movl $0x2, %esi
callq 0x7740
movq -0x558(%rbp), %rsi
leaq -0x4c0(%rbp), %rdi
leaq -0x510(%rbp), %rdx
callq 0x7bb0
movq -0x550(%rbp), %rdi
addq $0x58, %rdi
leaq -0x4c0(%rbp), %rsi
movl $0x49, %edx
callq 0x5230
movq -0x10(%rbp), %rax
testb $0x1, 0x78(%rax)
jne 0x88cc
movq -0x550(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0xa8(%rax)
xorps %xmm0, %xmm0
movsd %xmm0, 0xb0(%rax)
xorps %xmm0, %xmm0
movsd %xmm0, 0xb8(%rax)
jmp 0x8a73
movq -0x10(%rbp), %rax
movsd 0x90(%rax), %xmm0
movsd 0xcb18(%rip), %xmm1     # 0x153f8
subsd %xmm1, %xmm0
movsd %xmm0, -0x518(%rbp)
xorps %xmm0, %xmm0
ucomisd -0x518(%rbp), %xmm0
jbe 0x8913
movsd 0xcaff(%rip), %xmm0     # 0x15400
addsd -0x518(%rbp), %xmm0
movsd %xmm0, -0x518(%rbp)
jmp 0x88ec
jmp 0x8915
movsd -0x518(%rbp), %xmm0
movsd 0xcadb(%rip), %xmm1     # 0x15400
ucomisd %xmm1, %xmm0
jb 0x8949
movsd -0x518(%rbp), %xmm0
movsd 0xcac5(%rip), %xmm1     # 0x15400
subsd %xmm1, %xmm0
movsd %xmm0, -0x518(%rbp)
jmp 0x8915
fldl -0x518(%rbp)
fldl 0xc6e3(%rip)            # 0x15038
fmulp %st, %st(1)
flds 0xcb13(%rip)            # 0x15470
fdivrp %st, %st(1)
fstpl -0x518(%rbp)
movsd -0x518(%rbp), %xmm0
callq 0xfc10
movsd %xmm0, -0x520(%rbp)
movsd -0x520(%rbp), %xmm0
callq 0xddd0
movaps %xmm0, %xmm1
movsd 0xc6b6(%rip), %xmm0     # 0x15048
subsd %xmm1, %xmm0
callq 0x5200
movsd %xmm0, -0x528(%rbp)
movsd -0x518(%rbp), %xmm0
callq 0xf950
addsd %xmm0, %xmm0
movsd %xmm0, -0x540(%rbp)
fldl -0x540(%rbp)
fldl 0xc640(%rip)            # 0x15008
fmulp %st, %st(1)
flds 0xcaa4(%rip)            # 0x15474
fdivrp %st, %st(1)
fstpl -0x548(%rbp)
movsd -0x548(%rbp), %xmm0
movsd %xmm0, -0x530(%rbp)
movq -0x10(%rbp), %rax
movsd 0x88(%rax), %xmm0
addsd -0x530(%rbp), %xmm0
movsd %xmm0, -0x538(%rbp)
movsd -0x538(%rbp), %xmm0
callq 0x5470
movq -0x550(%rbp), %rax
movq %xmm0, %rcx
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
xorq %rdx, %rcx
movq %rcx, %xmm0
mulsd -0x528(%rbp), %xmm0
movsd %xmm0, 0xa8(%rax)
movsd -0x538(%rbp), %xmm0
callq 0x5080
movq -0x550(%rbp), %rax
mulsd -0x528(%rbp), %xmm0
movsd %xmm0, 0xb0(%rax)
movsd -0x520(%rbp), %xmm0
movsd %xmm0, 0xb8(%rax)
addq $0x590, %rsp            # imm = 0x590
popq %rbp
retq
nopl (%rax)