// File: 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:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x130(%rsp)
movq %rsi, 0x128(%rsp)
movq 0x130(%rsp), %rax
movq %rax, 0x40(%rsp)
cmpl $0x0, 0xcc(%rax)
jne 0xa6522
jmp 0xa6829
movq 0x128(%rsp), %rdi
callq 0xbd760
cmpl $0x2, %eax
jge 0xa6539
jmp 0xa6829
movq 0x40(%rsp), %rdi
callq 0xb9260
movq 0x40(%rsp), %rdi
subl $0x1, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x30(%rsp)
callq 0xb9280
movsd 0x30(%rsp), %xmm0
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
leaq 0x118(%rsp), %rdi
callq 0xc3c30
leaq 0x108(%rsp), %rdi
movsd 0x8ca8c(%rip), %xmm0    # 0x133010
leaq 0x118(%rsp), %rsi
callq 0xa5c30
movq 0x128(%rsp), %rdi
callq 0xb9370
movsd %xmm0, 0x38(%rsp)
movq 0x128(%rsp), %rdi
callq 0xb9390
movaps %xmm0, %xmm1
movsd 0x38(%rsp), %xmm0
leaq 0xf8(%rsp), %rdi
callq 0xc3c30
movq 0x128(%rsp), %rsi
leaq 0xd8(%rsp), %rdi
callq 0xbdf00
leaq 0xe8(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0xa5b80
movsd 0x8ca27(%rip), %xmm0    # 0x133020
movsd %xmm0, 0xd0(%rsp)
leaq 0xf8(%rsp), %rdi
xorl %esi, %esi
callq 0xa5c10
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xa6658
leaq 0x118(%rsp), %rdi
xorl %esi, %esi
callq 0xa5c10
movsd (%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0xf8(%rsp), %rdi
xorl %esi, %esi
callq 0xa5c10
movsd 0x20(%rsp), %xmm0
divsd (%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
jmp 0xa6668
movsd 0x8c9c0(%rip), %xmm0    # 0x133020
movsd %xmm0, 0x28(%rsp)
jmp 0xa6668
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
movsd 0xc8(%rsp), %xmm1
movsd 0xd0(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xa66a1
movsd 0xc8(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
leaq 0xf8(%rsp), %rdi
movl $0x1, %esi
callq 0xa5c10
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xa6700
leaq 0x118(%rsp), %rdi
movl $0x1, %esi
callq 0xa5c10
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0xf8(%rsp), %rdi
movl $0x1, %esi
callq 0xa5c10
movsd 0x10(%rsp), %xmm0
divsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
jmp 0xa6710
movsd 0x8c918(%rip), %xmm0    # 0x133020
movsd %xmm0, 0x18(%rsp)
jmp 0xa6710
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0xc0(%rsp)
movsd 0xc0(%rsp), %xmm1
movsd 0xd0(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xa6749
movsd 0xc0(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
movsd 0xd0(%rsp), %xmm0
movsd 0x8c8c6(%rip), %xmm1    # 0x133020
ucomisd %xmm1, %xmm0
jne 0xa6773
jp 0xa6773
movsd 0x8c89e(%rip), %xmm0    # 0x133008
movsd %xmm0, 0xd0(%rsp)
leaq 0x4d42ee(%rip), %rsi    # 0x57aa68
leaq 0x68(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0xb9740
movq 0x8(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0xb92e0
jmp 0xa679d
movsd 0xd0(%rsp), %xmm0
movsd 0x8c85a(%rip), %xmm1    # 0x133008
ucomisd %xmm1, %xmm0
jne 0xa67b8
jp 0xa67b8
jmp 0xa67e9
movsd 0xd0(%rsp), %xmm0
leaq 0x68(%rsp), %rdi
callq 0xb97c0
jmp 0xa67cd
jmp 0xa67e9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x60(%rsp)
movl %eax, 0x5c(%rsp)
leaq 0x68(%rsp), %rdi
callq 0xb9250
jmp 0xa6831
leaq 0x48(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0xc4700
jmp 0xa67fd
leaq 0x68(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0xb92e0
jmp 0xa680e
movq 0x40(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0xa69b0
jmp 0xa681f
leaq 0x68(%rsp), %rdi
callq 0xb9250
addq $0x138, %rsp            # imm = 0x138
retq
movq 0x60(%rsp), %rdi
callq 0xf890
nopl (%rax,%rax)