// File: /workspace/github_cpp_makefile_star2_O3/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe0, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x8(%rdi), %r15
movq %r15, %rdi
callq 0x552e
movsd 0x8(%r14), %xmm0
leaq 0x40(%rsp), %r12
movq %r12, %rdi
movl $0x2, %esi
callq 0x55cc
leaq 0x90(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x59a0
movl $0x9, %ecx
movq %r15, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movb 0x48(%r13), %al
movb %al, 0x50(%rbx)
movsd (%r14), %xmm0
leaq 0x40(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x55cc
leaq 0x90(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x59a0
movl $0x9, %ecx
movq %r15, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movb 0x48(%r13), %al
movb %al, 0x50(%rbx)
movsd 0x18(%r14), %xmm0
leaq 0x40(%rsp), %r12
movq %r12, %rdi
movl $0x1, %esi
callq 0x55cc
leaq 0x90(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x59a0
movl $0x9, %ecx
movq %r15, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movb 0x48(%r13), %al
movb %al, 0x50(%rbx)
movsd 0x20(%r14), %xmm0
leaq 0x40(%rsp), %r12
movq %r12, %rdi
movl $0x2, %esi
callq 0x55cc
leaq 0x90(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x59a0
movl $0x9, %ecx
movq %r15, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movb 0x48(%r13), %al
movb %al, 0x50(%rbx)
leaq 0x58(%rbx), %r15
movq %r15, %rdi
callq 0x552e
movsd 0x20(%r14), %xmm0
xorps 0x6fbf(%rip), %xmm0     # 0xd070
leaq 0x40(%rsp), %r12
movq %r12, %rdi
movl $0x2, %esi
callq 0x55cc
leaq 0x90(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x59a0
movl $0x9, %ecx
movq %r15, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movb 0x48(%r13), %al
movb %al, 0xa0(%rbx)
movsd 0x18(%r14), %xmm0
xorps 0x6f72(%rip), %xmm0     # 0xd070
leaq 0x40(%rsp), %r12
movq %r12, %rdi
movl $0x1, %esi
callq 0x55cc
leaq 0x90(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x59a0
movl $0x9, %ecx
movq %r15, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movb 0x48(%r13), %al
movb %al, 0xa0(%rbx)
movsd (%r14), %xmm0
xorps 0x6f26(%rip), %xmm0     # 0xd070
leaq 0x40(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x55cc
leaq 0x90(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x59a0
movl $0x9, %ecx
movq %r15, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movb 0x48(%r13), %al
movb %al, 0xa0(%rbx)
movsd 0x8(%r14), %xmm0
xorps 0x6edc(%rip), %xmm0     # 0xd070
leaq 0x40(%rsp), %r12
movq %r12, %rdi
movl $0x2, %esi
callq 0x55cc
leaq 0x90(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x59a0
movl $0x9, %ecx
movq %r15, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movb 0x48(%r13), %al
movb %al, 0xa0(%rbx)
cmpb $0x0, 0x78(%r14)
je 0x6285
movsd 0x90(%r14), %xmm0
addsd 0x7220(%rip), %xmm0     # 0xd410
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x620c
movsd 0x7216(%rip), %xmm2     # 0xd418
addsd %xmm2, %xmm0
ucomisd %xmm0, %xmm1
ja 0x6202
ucomisd 0x7204(%rip), %xmm0     # 0xd418
jb 0x6230
movsd 0x7202(%rip), %xmm1     # 0xd420
movsd 0x71f2(%rip), %xmm2     # 0xd418
addsd %xmm1, %xmm0
ucomisd %xmm2, %xmm0
jae 0x6226
movsd %xmm0, 0x38(%rsp)
fldl 0x38(%rsp)
fmull 0x6df8(%rip)            # 0xd038
fdivs 0x72ea(%rip)            # 0xd530
fstpl 0x30(%rsp)
movsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0xa79a
movsd %xmm0, 0x18(%rsp)
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd 0x6dd7(%rip), %xmm0     # 0xd048
subsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x629f
sqrtsd %xmm0, %xmm0
jmp 0x62a4
xorps %xmm0, %xmm0
movups %xmm0, 0xa8(%rbx)
movq $0x0, 0xb8(%rbx)
jmp 0x632a
callq 0x4180
movsd %xmm0, 0x10(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0xa536
addsd %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
fldl 0x28(%rsp)
fmull 0x6d3f(%rip)            # 0xd008
fdivs 0x7265(%rip)            # 0xd534
fstpl 0x20(%rsp)
movsd 0x88(%r14), %xmm0
addsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0x4370
xorpd 0x6d7b(%rip), %xmm0     # 0xd070
mulsd 0x10(%rsp), %xmm0
movsd %xmm0, 0xa8(%rbx)
movsd 0x8(%rsp), %xmm0
callq 0x4070
mulsd 0x10(%rsp), %xmm0
movsd %xmm0, 0xb0(%rbx)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0xb8(%rbx)
addq $0xe0, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
nop