// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::SetWorldToGridTransformation(gaps::R2Box const&)
void R2Grid::
SetWorldToGridTransformation(const R2Box& world_box)
{
  // Just checking
  if (grid_size == 0) return;
  if (world_box.NDimensions() < 2) return;

  // Compute grid origin
  R2Vector grid_diagonal(XResolution()-1, YResolution()-1);
  R2Vector grid_origin = 0.5 * grid_diagonal;

  // Compute world origin
  R2Vector world_diagonal(world_box.XLength(), world_box.YLength());
  R2Vector world_origin = world_box.Centroid().Vector();

  // Compute scale
  RNScalar scale = FLT_MAX;
  RNScalar xscale = (world_diagonal[0] > 0) ? grid_diagonal[0] / world_diagonal[0] : FLT_MAX;
  if (xscale < scale) scale = xscale;
  RNScalar yscale = (world_diagonal[1] > 0) ? grid_diagonal[1] / world_diagonal[1] : FLT_MAX;
  if (yscale < scale) scale = yscale;
  if (scale == FLT_MAX) scale = 1;

  // Compute world-to-grid transformation
  R2Affine affine(R2identity_affine);
  affine.Translate(grid_origin);
  if (scale != 1) affine.Scale(scale);
  affine.Translate(-world_origin);

  // Set transformations
  SetWorldToGridTransformation(affine);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe0, %rsp
cmpl $0x0, 0xcc(%rdi)
je 0x59f7f
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, %rdi
callq 0x661f8
cmpl $0x2, %eax
jl 0x59f7f
movl 0xc0(%rbx), %eax
decl %eax
cvtsi2sd %eax, %xmm0
movl 0xc4(%rbx), %eax
decl %eax
cvtsi2sd %eax, %xmm1
leaq 0xd0(%rsp), %r15
movq %r15, %rdi
callq 0x6915a
leaq 0x58(%rsp), %rdi
movsd 0x4822d(%rip), %xmm0    # 0xa2030
movq %r15, %rsi
callq 0x694f6
movsd 0x18(%r14), %xmm0
movsd 0x20(%r14), %xmm1
subsd 0x8(%r14), %xmm0
subsd 0x10(%r14), %xmm1
leaq 0xc0(%rsp), %r12
movq %r12, %rdi
callq 0x6915a
leaq 0x68(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x66416
movsd (%r13), %xmm0
movsd 0x8(%r13), %xmm1
leaq 0x48(%rsp), %rdi
callq 0x6915a
movsd (%r12), %xmm0
movsd %xmm0, (%rsp)
movsd 0x8(%r12), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd (%r15), %xmm0
movaps %xmm0, 0x10(%rsp)
movsd 0x8(%r15), %xmm0
movapd %xmm0, 0x20(%rsp)
leaq 0x8a662(%rip), %rax     # 0xe44f0
addq $0x10, %rax
movq %rax, (%r13)
leaq 0x70(%rsp), %r14
leaq 0x48e0b6(%rip), %r15    # 0x4e7f58
leaq 0x8(%r15), %rsi
movq %r14, %rdi
callq 0x66efa
movl 0x50(%r15), %eax
movl %eax, 0xb8(%rsp)
leaq 0x58(%rsp), %rsi
movq %r14, %rdi
callq 0x67606
movapd 0x10(%rsp), %xmm3
movsd (%rsp), %xmm2
divsd %xmm2, %xmm3
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
cmpltsd %xmm2, %xmm0
andpd %xmm0, %xmm3
movsd 0x48152(%rip), %xmm2    # 0xa2040
andnpd %xmm2, %xmm0
orpd %xmm3, %xmm0
minsd %xmm2, %xmm0
movapd 0x20(%rsp), %xmm3
movsd 0x8(%rsp), %xmm4
divsd %xmm4, %xmm3
cmpltsd %xmm4, %xmm1
andpd %xmm1, %xmm3
andnpd %xmm2, %xmm1
orpd %xmm3, %xmm1
minsd %xmm0, %xmm1
cmpeqsd %xmm1, %xmm2
movsd 0x4810c(%rip), %xmm0    # 0xa2038
andpd %xmm2, %xmm0
andnpd %xmm1, %xmm2
orpd %xmm2, %xmm0
ucomisd 0x480f8(%rip), %xmm0    # 0xa2038
jne 0x59f44
jnp 0x59f4c
movq %r14, %rdi
callq 0x676cc
leaq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x69669
leaq 0x38(%rsp), %rsi
movq %r14, %rdi
callq 0x67606
leaq 0x68(%rsp), %rsi
movq %rbx, %rdi
callq 0x5a060
leaq 0x68(%rsp), %rdi
callq 0x66ce4
addq $0xe0, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
jmp 0x59f94
jmp 0x59f94
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x66ce4
movq %rbx, %rdi
callq 0xe7e0
nop