// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
cmpl $0x0, 0x24c(%rdi)
je 0x2b6e6
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, %rdi
callq 0x24cb0
cmpl $0x3, %eax
jl 0x2b6e6
movl 0x238(%rbx), %eax
decl %eax
cvtsi2sd %eax, %xmm0
movl 0x23c(%rbx), %eax
decl %eax
cvtsi2sd %eax, %xmm1
movl 0x240(%rbx), %eax
decl %eax
cvtsi2sd %eax, %xmm2
leaq 0xb0(%rsp), %r12
movq %r12, %rdi
callq 0x3830c
leaq 0x98(%rsp), %r15
movsd 0x67ae7(%rip), %xmm0    # 0x93010
movq %r15, %rdi
movq %r12, %rsi
callq 0x38a02
movsd 0x20(%r14), %xmm0
movsd 0x28(%r14), %xmm1
subsd 0x8(%r14), %xmm0
subsd 0x10(%r14), %xmm1
movsd 0x30(%r14), %xmm2
subsd 0x18(%r14), %xmm2
leaq 0x80(%rsp), %r13
movq %r13, %rdi
callq 0x3830c
movq (%r14), %rax
leaq 0xc8(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq *0x68(%rax)
leaq 0x68(%rsp), %rdi
movq %rbp, %rsi
callq 0x212f8
movsd (%r13), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x8(%r13), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd (%r12), %xmm0
movaps %xmm0, 0x20(%rsp)
movsd 0x8(%r12), %xmm0
movaps %xmm0, 0x30(%rsp)
movsd 0x10(%r13), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x10(%r12), %xmm0
movapd %xmm0, 0x40(%rsp)
leaq 0xafa38(%rip), %rsi     # 0xdb010
leaq 0xc8(%rsp), %r14
movq %r14, %rdi
callq 0x21282
movq %r14, %rdi
movq %r15, %rsi
callq 0x212e2
movapd 0x20(%rsp), %xmm3
movsd 0x8(%rsp), %xmm2
divsd %xmm2, %xmm3
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
cmpltsd %xmm2, %xmm0
andpd %xmm0, %xmm3
movsd 0x679fc(%rip), %xmm2    # 0x93018
andnpd %xmm2, %xmm0
orpd %xmm3, %xmm0
minsd %xmm2, %xmm0
movapd 0x30(%rsp), %xmm4
movsd 0x10(%rsp), %xmm5
divsd %xmm5, %xmm4
xorpd %xmm3, %xmm3
cmpltsd %xmm5, %xmm3
andpd %xmm3, %xmm4
andnpd %xmm2, %xmm3
orpd %xmm4, %xmm3
minsd %xmm0, %xmm3
movapd 0x40(%rsp), %xmm0
movsd 0x18(%rsp), %xmm4
divsd %xmm4, %xmm0
cmpltsd %xmm4, %xmm1
andpd %xmm1, %xmm0
andnpd %xmm2, %xmm1
orpd %xmm0, %xmm1
minsd %xmm3, %xmm1
cmpeqsd %xmm1, %xmm2
movsd 0x67985(%rip), %xmm0    # 0x93008
andpd %xmm2, %xmm0
andnpd %xmm1, %xmm2
orpd %xmm2, %xmm0
ucomisd 0x67971(%rip), %xmm0    # 0x93008
jne 0x2b69b
jnp 0x2b6a8
leaq 0xc8(%rsp), %rdi
callq 0x21314
leaq 0x50(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x38c72
leaq 0xc8(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x212e2
leaq 0xc8(%rsp), %rsi
movq %rbx, %rdi
callq 0x2f570
leaq 0xc8(%rsp), %rdi
callq 0x276ee
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2b6fa
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x276ee
movq %rbx, %rdi
callq 0xe810