// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::SetWorldToGridTransformation(gaps::R3Box const&)
void R3Grid::
SetWorldToGridTransformation(const R3Box& world_box)
{
  // Just checking
  if (grid_size == 0) return;
  if (world_box.NDimensions() < 3) return;

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

  // Compute world origin
  R3Vector world_diagonal(world_box.XLength(), world_box.YLength(), world_box.ZLength());
  R3Vector 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;
  RNScalar zscale = (world_diagonal[2] > 0) ? grid_diagonal[2] / world_diagonal[2] : FLT_MAX;
  if (zscale < scale) scale = zscale;
  if (scale == FLT_MAX) scale = 1;

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

  // Set transformations
  SetWorldToGridTransformation(affine);
}

// Assembly:
subq $0x248, %rsp            # imm = 0x248
movq %rdi, 0x240(%rsp)
movq %rsi, 0x238(%rsp)
movq 0x240(%rsp), %rax
movq %rax, 0x60(%rsp)
cmpl $0x0, 0x24c(%rax)
jne 0x55a12
jmp 0x55e10
movq 0x238(%rsp), %rdi
callq 0x42b70
cmpl $0x3, %eax
jge 0x55a29
jmp 0x55e10
movq 0x60(%rsp), %rdi
callq 0x138c0
movq 0x60(%rsp), %rdi
subl $0x1, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x40(%rsp)
callq 0x138e0
movq 0x60(%rsp), %rdi
subl $0x1, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x48(%rsp)
callq 0x13900
movsd 0x40(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
subl $0x1, %eax
cvtsi2sd %eax, %xmm2
leaq 0x220(%rsp), %rdi
callq 0x6fcb0
leaq 0x208(%rsp), %rdi
movsd 0xdd57f(%rip), %xmm0    # 0x133010
leaq 0x220(%rsp), %rsi
callq 0x13800
movq 0x238(%rsp), %rdi
callq 0x37850
movsd %xmm0, 0x58(%rsp)
movq 0x238(%rsp), %rdi
callq 0x378a0
movsd %xmm0, 0x50(%rsp)
movq 0x238(%rsp), %rdi
callq 0x378f0
movsd 0x50(%rsp), %xmm1
movaps %xmm0, %xmm2
movsd 0x58(%rsp), %xmm0
leaq 0x1f0(%rsp), %rdi
callq 0x6fcb0
movq 0x238(%rsp), %rsi
movq (%rsi), %rax
leaq 0x1c0(%rsp), %rdi
callq *0x68(%rax)
leaq 0x1d8(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
callq 0x36d10
movsd 0xdd500(%rip), %xmm0    # 0x133020
movsd %xmm0, 0x1b8(%rsp)
leaq 0x1f0(%rsp), %rdi
xorl %esi, %esi
callq 0x36c00
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x55b7f
leaq 0x220(%rsp), %rdi
xorl %esi, %esi
callq 0x36c00
movsd (%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0x1f0(%rsp), %rdi
xorl %esi, %esi
callq 0x36c00
movsd 0x30(%rsp), %xmm0
divsd (%rax), %xmm0
movsd %xmm0, 0x38(%rsp)
jmp 0x55b8f
movsd 0xdd499(%rip), %xmm0    # 0x133020
movsd %xmm0, 0x38(%rsp)
jmp 0x55b8f
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x1b0(%rsp)
movsd 0x1b0(%rsp), %xmm1
movsd 0x1b8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x55bc8
movsd 0x1b0(%rsp), %xmm0
movsd %xmm0, 0x1b8(%rsp)
leaq 0x1f0(%rsp), %rdi
movl $0x1, %esi
callq 0x36c00
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x55c27
leaq 0x220(%rsp), %rdi
movl $0x1, %esi
callq 0x36c00
movsd (%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0x1f0(%rsp), %rdi
movl $0x1, %esi
callq 0x36c00
movsd 0x20(%rsp), %xmm0
divsd (%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
jmp 0x55c37
movsd 0xdd3f1(%rip), %xmm0    # 0x133020
movsd %xmm0, 0x28(%rsp)
jmp 0x55c37
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x1a8(%rsp)
movsd 0x1a8(%rsp), %xmm1
movsd 0x1b8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x55c70
movsd 0x1a8(%rsp), %xmm0
movsd %xmm0, 0x1b8(%rsp)
leaq 0x1f0(%rsp), %rdi
movl $0x2, %esi
callq 0x36c00
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x55ccf
leaq 0x220(%rsp), %rdi
movl $0x2, %esi
callq 0x36c00
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x1f0(%rsp), %rdi
movl $0x2, %esi
callq 0x36c00
movsd 0x10(%rsp), %xmm0
divsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
jmp 0x55cdf
movsd 0xdd349(%rip), %xmm0    # 0x133020
movsd %xmm0, 0x18(%rsp)
jmp 0x55cdf
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x1a0(%rsp)
movsd 0x1a0(%rsp), %xmm1
movsd 0x1b8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x55d18
movsd 0x1a0(%rsp), %xmm0
movsd %xmm0, 0x1b8(%rsp)
movsd 0x1b8(%rsp), %xmm0
movsd 0xdd2f7(%rip), %xmm1    # 0x133020
ucomisd %xmm1, %xmm0
jne 0x55d42
jp 0x55d42
movsd 0xdd2cf(%rip), %xmm0    # 0x133008
movsd %xmm0, 0x1b8(%rsp)
leaq 0x122c07(%rip), %rsi    # 0x178950
leaq 0x90(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x36c20
movq 0x8(%rsp), %rdi
leaq 0x208(%rsp), %rsi
callq 0x36cd0
jmp 0x55d6f
movsd 0x1b8(%rsp), %xmm0
movsd 0xdd288(%rip), %xmm1    # 0x133008
ucomisd %xmm1, %xmm0
jne 0x55d8a
jp 0x55d8a
jmp 0x55dc7
movsd 0x1b8(%rsp), %xmm0
leaq 0x90(%rsp), %rdi
callq 0x36d80
jmp 0x55da2
jmp 0x55dc7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x4d430
jmp 0x55e18
leaq 0x68(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
callq 0x70fa0
jmp 0x55ddb
leaq 0x90(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x36cd0
jmp 0x55def
movq 0x60(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x5e430
jmp 0x55e03
leaq 0x90(%rsp), %rdi
callq 0x4d430
addq $0x248, %rsp            # imm = 0x248
retq
movq 0x88(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)