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

  // Determine transformation from 3D to 2D
  R3Point centroid = world_bbox.Centroid();
  centroid.Project(plane);
  RNDimension dim = plane.Normal().MinDimension();
  R3Vector zaxis = plane.Normal();
  R3Vector xaxis = R3xyz_triad[dim] % 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 $0x258, %rsp            # imm = 0x258
movq %rdi, 0x250(%rsp)
movq %rsi, 0x248(%rsp)
movq %rdx, 0x240(%rsp)
movsd %xmm0, 0x238(%rsp)
movq 0x250(%rsp), %rdi
movq %rdi, 0x28(%rsp)
movq 0x248(%rsp), %rsi
callq 0x44240
movq 0x28(%rsp), %rdi
movq 0x240(%rsp), %rsi
addq $0x200, %rdi            # imm = 0x200
callq 0x1bd40
movq 0x240(%rsp), %rsi
movq (%rsi), %rax
leaq 0x220(%rsp), %rdi
callq *0x68(%rax)
movq 0x248(%rsp), %rsi
leaq 0x220(%rsp), %rdi
callq 0xf8a20
movq 0x248(%rsp), %rdi
callq 0x44500
movq %rax, %rdi
callq 0xf9710
movl %eax, 0x21c(%rsp)
movq 0x248(%rsp), %rdi
callq 0x44500
movq %rax, %rsi
leaq 0x200(%rsp), %rdi
callq 0xf94f0
movl 0x21c(%rsp), %esi
leaq 0x120dab(%rip), %rdi    # 0x1fd628
callq 0x44220
movq %rax, %rsi
leaq 0x1e8(%rsp), %rdi
leaq 0x200(%rsp), %rdx
callq 0xfa2f0
leaq 0x1e8(%rsp), %rdi
callq 0xf9c10
leaq 0x1d0(%rsp), %rdi
leaq 0x200(%rsp), %rsi
leaq 0x1e8(%rsp), %rdx
callq 0xfa2f0
leaq 0x1d0(%rsp), %rdi
callq 0xf9c10
leaq 0x188(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
leaq 0x1d0(%rsp), %rdx
leaq 0x200(%rsp), %rcx
callq 0xd5c80
leaq 0x128(%rsp), %rdi
leaq 0x220(%rsp), %rsi
leaq 0x188(%rsp), %rdx
callq 0xd5570
movq 0x28(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x30(%rsp)
leaq 0xa8(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0xd5640
movq 0x30(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
xorl %edx, %edx
callq 0xd52b0
leaq 0x80(%rsp), %rdi
leaq 0x52210f(%rip), %rsi    # 0x5fea68
callq 0x12dc40
movl $0x0, 0x7c(%rsp)
cmpl $0x8, 0x7c(%rsp)
jge 0xdca37
movq 0x240(%rsp), %rsi
movl 0x7c(%rsp), %edx
leaq 0x60(%rsp), %rdi
callq 0xca360
jmp 0xdc989
movq 0x28(%rsp), %rsi
addq $0x20, %rsi
leaq 0x60(%rsp), %rdi
callq 0xf8fc0
jmp 0xdc99e
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x1bde0
movq %rax, 0x20(%rsp)
jmp 0xdc9b1
movq 0x20(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x60(%rsp), %rdi
movl $0x1, %esi
callq 0x1bde0
movq %rax, 0x18(%rsp)
jmp 0xdc9d6
movsd 0x10(%rsp), %xmm0
movq 0x18(%rsp), %rax
movsd (%rax), %xmm1
leaq 0x40(%rsp), %rdi
callq 0x133960
jmp 0xdc9f1
leaq 0x80(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x12ecd0
jmp 0xdca05
jmp 0xdca07
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0xdc966
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x54990
jmp 0xdcb14
leaq 0x80(%rsp), %rdi
callq 0xdebb0
movsd %xmm0, 0x8(%rsp)
jmp 0xdca4c
movsd 0x8(%rsp), %xmm0
movsd 0x238(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd 0xc76f1(%rip), %xmm1    # 0x1a4158
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x3c(%rsp)
leaq 0x80(%rsp), %rdi
callq 0xdebd0
movsd %xmm0, (%rsp)
jmp 0xdca87
movsd (%rsp), %xmm0
divsd 0x238(%rsp), %xmm0
movsd 0xc76bb(%rip), %xmm1    # 0x1a4158
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x38(%rsp)
cmpl $0x2, 0x3c(%rsp)
jge 0xdcab8
movl $0x2, 0x3c(%rsp)
cmpl $0x2, 0x38(%rsp)
jge 0xdcac7
movl $0x2, 0x38(%rsp)
movq 0x28(%rsp), %rdi
addq $0x130, %rdi            # imm = 0x130
movl 0x3c(%rsp), %esi
movl 0x38(%rsp), %edx
xorl %ecx, %ecx
callq 0x11b220
jmp 0xdcae4
movq 0x28(%rsp), %rdi
addq $0x130, %rdi            # imm = 0x130
leaq 0x80(%rsp), %rsi
callq 0x111610
jmp 0xdcaff
leaq 0x80(%rsp), %rdi
callq 0x54990
addq $0x258, %rsp            # imm = 0x258
retq
movq 0x58(%rsp), %rdi
callq 0x13970
nop