// File: /workspace/github_cpp_makefile_star2_O3/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 0x59277
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, %rdi
callq 0x65478
cmpl $0x2, %eax
jl 0x59277
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 0x683d2
leaq 0x58(%rsp), %rdi
movsd 0x48f35(%rip), %xmm0    # 0xa2030
movq %r15, %rsi
callq 0x6870c
movsd 0x18(%r14), %xmm0
movsd 0x20(%r14), %xmm1
subsd 0x8(%r14), %xmm0
subsd 0x10(%r14), %xmm1
leaq 0xc0(%rsp), %r12
movq %r12, %rdi
callq 0x683d2
leaq 0x68(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x65688
movsd (%r13), %xmm0
movsd 0x8(%r13), %xmm1
leaq 0x48(%rsp), %rdi
callq 0x683d2
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 0x8b36a(%rip), %rax     # 0xe44f0
addq $0x10, %rax
movq %rax, (%r13)
leaq 0x70(%rsp), %r14
leaq 0x48edbe(%rip), %r15    # 0x4e7f58
leaq 0x8(%r15), %rsi
movq %r14, %rdi
callq 0x66172
movl 0x50(%r15), %eax
movl %eax, 0xb8(%rsp)
leaq 0x58(%rsp), %rsi
movq %r14, %rdi
callq 0x6687e
movapd 0x10(%rsp), %xmm3
movsd (%rsp), %xmm2
divsd %xmm2, %xmm3
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
cmpltsd %xmm2, %xmm0
andpd %xmm0, %xmm3
movsd 0x48e5a(%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 0x48e14(%rip), %xmm0    # 0xa2038
andpd %xmm2, %xmm0
andnpd %xmm1, %xmm2
orpd %xmm2, %xmm0
ucomisd 0x48e00(%rip), %xmm0    # 0xa2038
jne 0x5923c
jnp 0x59244
movq %r14, %rdi
callq 0x66944
leaq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x6887f
leaq 0x38(%rsp), %rsi
movq %r14, %rdi
callq 0x6687e
leaq 0x68(%rsp), %rsi
movq %rbx, %rdi
callq 0x59362
leaq 0x68(%rsp), %rdi
callq 0x65f5c
addq $0xe0, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
jmp 0x5928c
jmp 0x5928c
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x65f5c
movq %rbx, %rdi
callq 0xe7e0
nop