// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, %r14
movq %rdi, 0x18(%rsp)
leaq 0x8(%rdi), %r12
movq %r12, %rdi
callq 0x52c6
movsd 0x8(%r14), %xmm0
leaq 0x48(%rsp), %r15
pushq $0x2
popq %rbx
movq %r15, %rdi
movl %ebx, %esi
callq 0x52fa
leaq 0x98(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x54ea
pushq $0x49
popq %r15
movq %r15, %rcx
movq %r12, %rdi
movq %r13, %rsi
rep  movsb (%rsi), %es:(%rdi)
movsd (%r14), %xmm0
leaq 0x48(%rsp), %r13
movq %r13, %rdi
xorl %esi, %esi
callq 0x52fa
leaq 0x98(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x54ea
movq %r15, %rcx
movq %r12, %rdi
movq %rbp, %rsi
rep  movsb (%rsi), %es:(%rdi)
movsd 0x18(%r14), %xmm0
leaq 0x48(%rsp), %rbp
pushq $0x1
popq %rsi
movq %rbp, %rdi
callq 0x52fa
leaq 0x98(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x54ea
movq %r15, %rcx
movq %r12, %rdi
movq %r13, %rsi
rep  movsb (%rsi), %es:(%rdi)
movsd 0x20(%r14), %xmm0
leaq 0x48(%rsp), %r13
movq %r13, %rdi
movl %ebx, %esi
callq 0x52fa
leaq 0x98(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x54ea
movq %r15, %rcx
movq %r12, %rdi
movq %rbp, %rsi
rep  movsb (%rsi), %es:(%rdi)
movq 0x18(%rsp), %rax
leaq 0x58(%rax), %r12
movq %r12, %rdi
callq 0x52c6
movsd 0x20(%r14), %xmm0
xorps 0x65e0(%rip), %xmm0     # 0xc070
leaq 0x48(%rsp), %r13
movq %r13, %rdi
movl %ebx, %esi
callq 0x52fa
leaq 0x98(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x54ea
movq %r15, %rcx
movq %r12, %rdi
movq %rbp, %rsi
rep  movsb (%rsi), %es:(%rdi)
movsd 0x18(%r14), %xmm0
xorps 0x65a3(%rip), %xmm0     # 0xc070
leaq 0x48(%rsp), %r13
movq %r13, %rdi
pushq $0x1
popq %rsi
callq 0x52fa
leaq 0x98(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x54ea
movq %r15, %rcx
movq %r12, %rdi
movq %rbp, %rsi
rep  movsb (%rsi), %es:(%rdi)
movsd (%r14), %xmm0
xorps 0x6566(%rip), %xmm0     # 0xc070
leaq 0x48(%rsp), %r13
movq %r13, %rdi
xorl %esi, %esi
callq 0x52fa
leaq 0x98(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x54ea
movq %r15, %rcx
movq %r12, %rdi
movq %rbp, %rsi
rep  movsb (%rsi), %es:(%rdi)
movsd 0x8(%r14), %xmm0
xorpd 0x6528(%rip), %xmm0     # 0xc070
leaq 0x48(%rsp), %r13
movq %r13, %rdi
movl %ebx, %esi
callq 0x52fa
leaq 0x98(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x54ea
movq %r15, %rcx
movq %r12, %rdi
movq %rbp, %rsi
rep  movsb (%rsi), %es:(%rdi)
cmpb $0x0, 0x78(%r14)
je 0x5c1a
movsd 0x90(%r14), %xmm0
addsd 0x6844(%rip), %xmm0     # 0xc3d8
xorpd %xmm2, %xmm2
movsd 0x6840(%rip), %xmm1     # 0xc3e0
movq 0x18(%rsp), %rbx
ucomisd %xmm0, %xmm2
jbe 0x5bb1
addsd %xmm1, %xmm0
jmp 0x5ba5
movsd 0x682f(%rip), %xmm2     # 0xc3e8
ucomisd %xmm1, %xmm0
jb 0x5bc5
addsd %xmm2, %xmm0
jmp 0x5bb9
movsd %xmm0, 0x40(%rsp)
fldl 0x40(%rsp)
fmull 0x6463(%rip)            # 0xc038
fdivs 0x6915(%rip)            # 0xc4f0
fstpl 0x38(%rsp)
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0x9c74
movsd %xmm0, 0x20(%rsp)
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd 0x6442(%rip), %xmm0     # 0xc048
subsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x5c38
sqrtsd %xmm0, %xmm0
jmp 0x5c3d
xorpd %xmm0, %xmm0
movq 0x18(%rsp), %rax
movupd %xmm0, 0xa8(%rax)
andq $0x0, 0xb8(%rax)
jmp 0x5cc3
callq 0x4190
movsd %xmm0, 0x10(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x9aa9
addsd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
fldl 0x30(%rsp)
fmull 0x63a6(%rip)            # 0xc008
fdivs 0x688c(%rip)            # 0xc4f4
fstpl 0x28(%rsp)
movsd 0x88(%r14), %xmm0
addsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0x43a0
xorpd 0x63e2(%rip), %xmm0     # 0xc070
mulsd 0x10(%rsp), %xmm0
movsd %xmm0, 0xa8(%rbx)
movsd 0x8(%rsp), %xmm0
callq 0x4070
mulsd 0x10(%rsp), %xmm0
movsd %xmm0, 0xb0(%rbx)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0xb8(%rbx)
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop