// File: /workspace/github_cpp_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
cmpl $0x0, 0xcc(%rdi)
je 0x51982
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, %rdi
callq 0x5cc2c
cmpl $0x2, %eax
jl 0x51982
movl 0xc0(%rbx), %eax
decl %eax
cvtsi2sd %eax, %xmm0
movl 0xc4(%rbx), %eax
decl %eax
cvtsi2sd %eax, %xmm1
leaq 0xc8(%rsp), %r12
movq %r12, %rdi
callq 0x5f8b4
leaq 0xb8(%rsp), %r15
movsd 0x417e7(%rip), %xmm0    # 0x93010
movq %r15, %rdi
movq %r12, %rsi
callq 0x5fbe5
movsd 0x18(%r14), %xmm0
movsd 0x20(%r14), %xmm1
subsd 0x8(%r14), %xmm0
subsd 0x10(%r14), %xmm1
leaq 0xa8(%rsp), %r13
movq %r13, %rdi
callq 0x5f8b4
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x5ce16
leaq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x51538
movsd (%r13), %xmm0
movsd %xmm0, (%rsp)
movsd 0x8(%r13), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd (%r12), %xmm0
movaps %xmm0, 0x10(%rsp)
movsd 0x8(%r12), %xmm0
movapd %xmm0, 0x20(%rsp)
leaq 0x48b6b9(%rip), %rsi    # 0x4dcf68
leaq 0x50(%rsp), %rdi
callq 0x5ad70
leaq 0x58(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x5de7a
movapd 0x10(%rsp), %xmm3
movsd (%rsp), %xmm2
divsd %xmm2, %xmm3
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
cmpltsd %xmm2, %xmm0
andpd %xmm0, %xmm3
movsd 0x41727(%rip), %xmm2    # 0x93018
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 0x416d9(%rip), %xmm0    # 0x93008
andpd %xmm2, %xmm0
andnpd %xmm1, %xmm2
orpd %xmm2, %xmm0
ucomisd 0x416c5(%rip), %xmm0    # 0x93008
jne 0x51947
jnp 0x5194f
movq %r14, %rdi
callq 0x5df40
leaq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x5fd59
leaq 0x30(%rsp), %rsi
movq %r14, %rdi
callq 0x5de7a
leaq 0x50(%rsp), %rsi
movq %rbx, %rdi
callq 0x51a6a
leaq 0x50(%rsp), %rdi
callq 0x5d61e
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x51996
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x5d61e
movq %rbx, %rdi
callq 0xe810
nop