// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3PlanarGrid.cpp
// Function: gaps::R3PlanarGrid::Reset(gaps::R3Plane const&, gaps::R3Box const&, gaps::R3Point const&, gaps::R3Vector const&, double)
void R3PlanarGrid::
Reset(const R3Plane& plane, const R3Box& world_bbox, 
  const R3Point& origin, const R3Vector& up, 
  RNLength spacing)
{
  // Set plane and bbox
  this->plane = plane;
  this->bbox = world_bbox;

  // Determine transformation from 3D to 2D
  R3Point centroid = origin;
  centroid.Project(plane);
  R3Vector zaxis = plane.Normal();
  R3Vector xaxis = up % zaxis;
  xaxis.Normalize();
  R3Vector yaxis = zaxis % xaxis;
  yaxis.Normalize();
  R3Triad triad(xaxis, yaxis, zaxis);
  R3CoordSystem cs(centroid, triad);
  transformation.Reset(cs.InverseMatrix());

  // Determine 2D bounding box
  R2Box planar_bbox = R2null_box;
  for (int i = 0; i < 8; i++) {
    R3Point corner = world_bbox.Corner(i);
    corner.Transform(transformation);
    planar_bbox.Union(R2Point(corner[0], corner[1]));
  }

  // Determine grid resolution
  int xres = (int) (planar_bbox.XLength() / spacing + 0.5);
  int yres = (int) (planar_bbox.YLength() / spacing + 0.5);
  if (xres < 2) xres = 2;
  if (yres < 2) yres = 2;
  grid.Resample(xres, yres);

  // Determine transformation from 2D to grid
  grid.SetWorldToGridTransformation(planar_bbox);
}

// Assembly:
subq $0x268, %rsp            # imm = 0x268
movq %rdi, 0x260(%rsp)
movq %rsi, 0x258(%rsp)
movq %rdx, 0x250(%rsp)
movq %rcx, 0x248(%rsp)
movq %r8, 0x240(%rsp)
movsd %xmm0, 0x238(%rsp)
movq 0x260(%rsp), %rdi
movq %rdi, 0x30(%rsp)
movq 0x258(%rsp), %rsi
callq 0x44240
movq 0x30(%rsp), %rdi
movq 0x250(%rsp), %rsi
addq $0x200, %rdi            # imm = 0x200
callq 0x1bd40
movq 0x248(%rsp), %rsi
leaq 0x220(%rsp), %rdi
callq 0xf8390
movq 0x258(%rsp), %rsi
leaq 0x220(%rsp), %rdi
callq 0xf8a20
movq 0x258(%rsp), %rdi
callq 0x44500
movq %rax, %rsi
leaq 0x208(%rsp), %rdi
callq 0xf94f0
movq 0x240(%rsp), %rsi
leaq 0x1f0(%rsp), %rdi
leaq 0x208(%rsp), %rdx
callq 0xfa2f0
leaq 0x1f0(%rsp), %rdi
callq 0xf9c10
leaq 0x1d8(%rsp), %rdi
leaq 0x208(%rsp), %rsi
leaq 0x1f0(%rsp), %rdx
callq 0xfa2f0
leaq 0x1d8(%rsp), %rdi
callq 0xf9c10
leaq 0x190(%rsp), %rdi
leaq 0x1f0(%rsp), %rsi
leaq 0x1d8(%rsp), %rdx
leaq 0x208(%rsp), %rcx
callq 0xd5c80
leaq 0x130(%rsp), %rdi
leaq 0x220(%rsp), %rsi
leaq 0x190(%rsp), %rdx
callq 0xd5570
movq 0x30(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x38(%rsp)
leaq 0xb0(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0xd5640
movq 0x38(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
xorl %edx, %edx
callq 0xd52b0
leaq 0x88(%rsp), %rdi
leaq 0x521caa(%rip), %rsi    # 0x5fea68
callq 0x12dc40
movl $0x0, 0x84(%rsp)
cmpl $0x8, 0x84(%rsp)
jge 0xdceab
movq 0x250(%rsp), %rsi
movl 0x84(%rsp), %edx
leaq 0x68(%rsp), %rdi
callq 0xca360
jmp 0xdcdf7
movq 0x30(%rsp), %rsi
addq $0x20, %rsi
leaq 0x68(%rsp), %rdi
callq 0xf8fc0
jmp 0xdce0c
leaq 0x68(%rsp), %rdi
xorl %esi, %esi
callq 0x1bde0
movq %rax, 0x28(%rsp)
jmp 0xdce1f
movq 0x28(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
leaq 0x68(%rsp), %rdi
movl $0x1, %esi
callq 0x1bde0
movq %rax, 0x20(%rsp)
jmp 0xdce44
movsd 0x18(%rsp), %xmm0
movq 0x20(%rsp), %rax
movsd (%rax), %xmm1
leaq 0x48(%rsp), %rdi
callq 0x133960
jmp 0xdce5f
leaq 0x88(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x12ecd0
jmp 0xdce73
jmp 0xdce75
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0xdcdce
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x60(%rsp)
movl %eax, 0x5c(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x54990
jmp 0xdcf8a
leaq 0x88(%rsp), %rdi
callq 0xdebb0
movsd %xmm0, 0x10(%rsp)
jmp 0xdcec0
movsd 0x10(%rsp), %xmm0
movsd 0x238(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd 0xc727d(%rip), %xmm1    # 0x1a4158
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x44(%rsp)
leaq 0x88(%rsp), %rdi
callq 0xdebd0
movsd %xmm0, 0x8(%rsp)
jmp 0xdcefc
movsd 0x8(%rsp), %xmm0
divsd 0x238(%rsp), %xmm0
movsd 0xc7245(%rip), %xmm1    # 0x1a4158
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x40(%rsp)
cmpl $0x2, 0x44(%rsp)
jge 0xdcf2e
movl $0x2, 0x44(%rsp)
cmpl $0x2, 0x40(%rsp)
jge 0xdcf3d
movl $0x2, 0x40(%rsp)
movq 0x30(%rsp), %rdi
addq $0x130, %rdi            # imm = 0x130
movl 0x44(%rsp), %esi
movl 0x40(%rsp), %edx
xorl %ecx, %ecx
callq 0x11b220
jmp 0xdcf5a
movq 0x30(%rsp), %rdi
addq $0x130, %rdi            # imm = 0x130
leaq 0x88(%rsp), %rsi
callq 0x111610
jmp 0xdcf75
leaq 0x88(%rsp), %rdi
callq 0x54990
addq $0x268, %rsp            # imm = 0x268
retq
movq 0x60(%rsp), %rdi
callq 0x13970
nopw %cs:(%rax,%rax)