// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::SetWorldToGridTransformation(gaps::R2Point const&, gaps::R2Vector const&, double)
void R2Grid::
SetWorldToGridTransformation(const R2Point& world_origin, const R2Vector& world_xaxis, RNLength world_radius)
{
  // Just checking
  if (grid_size == 0) return;

  // Compute grid origin
  R2Vector grid_diagonal(XResolution()-1, YResolution()-1);
  R2Vector grid_origin = 0.5 * grid_diagonal;
  RNScalar grid_radius = grid_origin[0];
  if (grid_origin[1] < grid_radius) grid_radius = grid_origin[1];

  // Compute scale
  if (RNIsZero(world_radius)) return;
  if (RNIsZero(grid_radius)) return;
  RNScalar scale = grid_radius / world_radius;

  // Compute rotation
  RNAngle rotation = R2InteriorAngle(world_xaxis, R2posx_vector);
  if (world_xaxis.Y() < 0.0) rotation = RN_TWO_PI - rotation;

  // Compute world-to-grid transformation
  R2Affine affine(R2identity_affine);
  affine.Translate(grid_origin);
  affine.Rotate(-rotation);
  affine.Scale(scale);
  affine.Translate(-(world_origin.Vector()));

  // Set transformations
  SetWorldToGridTransformation(affine);
}

// Assembly:
subq $0xf8, %rsp
movq %rdi, 0xf0(%rsp)
movq %rsi, 0xe8(%rsp)
movq %rdx, 0xe0(%rsp)
movsd %xmm0, 0xd8(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpl $0x0, 0xcc(%rax)
jne 0xb3a93
jmp 0xb3cad
movq 0x10(%rsp), %rdi
callq 0xb9260
movq 0x10(%rsp), %rdi
subl $0x1, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0xb9280
movsd 0x8(%rsp), %xmm0
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
leaq 0xc8(%rsp), %rdi
callq 0xc3c30
leaq 0xb8(%rsp), %rdi
movsd 0x7f532(%rip), %xmm0    # 0x133010
leaq 0xc8(%rsp), %rsi
callq 0xa5c30
leaq 0xb8(%rsp), %rdi
xorl %esi, %esi
callq 0xa5c10
movsd (%rax), %xmm0
movsd %xmm0, 0xb0(%rsp)
leaq 0xb8(%rsp), %rdi
movl $0x1, %esi
callq 0xa5c10
movsd (%rax), %xmm1
movsd 0xb0(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xb3b4b
leaq 0xb8(%rsp), %rdi
movl $0x1, %esi
callq 0xa5c10
movsd (%rax), %xmm0
movsd %xmm0, 0xb0(%rsp)
movsd 0xd8(%rsp), %xmm0
leaq 0xc4985(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36b80
cmpl $0x0, %eax
je 0xb3b6e
jmp 0xb3cad
movsd 0xb0(%rsp), %xmm0
leaq 0xc4962(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36b80
cmpl $0x0, %eax
je 0xb3b91
jmp 0xb3cad
movsd 0xb0(%rsp), %xmm0
divsd 0xd8(%rsp), %xmm0
movsd %xmm0, 0xa8(%rsp)
movq 0xe0(%rsp), %rdi
leaq 0x4c72fd(%rip), %rsi    # 0x57aeb8
callq 0xcd240
movsd %xmm0, 0xa0(%rsp)
movq 0xe0(%rsp), %rdi
callq 0x78ab0
movaps %xmm0, %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0xb3bff
leaq 0x82d0f(%rip), %rax     # 0x1368f8
movsd (%rax), %xmm0
subsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
leaq 0x4c6e62(%rip), %rsi    # 0x57aa68
leaq 0x48(%rsp), %rdi
movq %rdi, (%rsp)
callq 0xb9740
movq (%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0xb92e0
jmp 0xb3c27
movaps 0x7f402(%rip), %xmm1    # 0x133030
movsd 0xa0(%rsp), %xmm0
pxor %xmm1, %xmm0
leaq 0x48(%rsp), %rdi
callq 0xb97f0
jmp 0xb3c47
movsd 0xa8(%rsp), %xmm0
leaq 0x48(%rsp), %rdi
callq 0xb97c0
jmp 0xb3c5c
movq 0xe8(%rsp), %rsi
leaq 0x18(%rsp), %rdi
callq 0xa5b80
jmp 0xb3c70
leaq 0x28(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0xc4700
jmp 0xb3c81
leaq 0x48(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0xb92e0
jmp 0xb3c92
movq 0x10(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0xa69b0
jmp 0xb3ca3
leaq 0x48(%rsp), %rdi
callq 0xb9250
addq $0xf8, %rsp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x40(%rsp)
movl %eax, 0x3c(%rsp)
leaq 0x48(%rsp), %rdi
callq 0xb9250
movq 0x40(%rsp), %rdi
callq 0xf890
nopw (%rax,%rax)