// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::DrawIsoSurface(double) const
void R3Grid::
DrawIsoSurface(RNScalar isolevel) const
{
  // Allocate storage for isosurface
  static const R3Grid *isosurface_grid = NULL;
  static RNScalar isosurface_level = -12345679;
  static const int isosurface_max_points = 8*1024*1024;
  static R3Point *isosurface_points = NULL;
  static int isosurface_npoints = 0;

  // Allocate points for isosurface
  if (!isosurface_points) { 
    isosurface_points = new R3Point [isosurface_max_points];
    assert(isosurface_points);
  }

  // Generate isosurface
  if ((this != isosurface_grid) || (isosurface_level != isolevel)) {
    isosurface_npoints = GenerateIsoSurface(isolevel, isosurface_points, isosurface_max_points);
    isosurface_grid = this;
    isosurface_level = isolevel;
  }

  // Draw isosurface
  R3Point *pointsp = isosurface_points;
  RNGrfxBegin(RN_GRFX_TRIANGLES);
  for (int i = 0; i < isosurface_npoints; i += 3) {
    R3Point p0 = *(pointsp++);
    R3Point p1 = *(pointsp++);
    R3Point p2 = *(pointsp++);
    R3Plane plane(p0, p1, p2);
    R3LoadNormal(plane.Normal());
    R3LoadPoint(p0);
    R3LoadPoint(p1);
    R3LoadPoint(p2);
  }
  RNGrfxEnd();
}

// Assembly:
subq $0xc8, %rsp
movq %rdi, 0xc0(%rsp)
movsd %xmm0, 0xb8(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x10e6c3(%rip)    # 0x1790a0
jne 0x6aa65
movl $0xc000000, %edi        # imm = 0xC000000
callq 0xf030
movq %rax, 0x10(%rsp)
movq %rax, %rcx
addq $0xc000000, %rcx        # imm = 0xC000000
movq %rcx, 0x18(%rsp)
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x6eb40
jmp 0x6aa17
movq 0x18(%rsp), %rcx
movq 0x8(%rsp), %rax
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, 0x20(%rsp)
jne 0x6aa06
movq 0x10(%rsp), %rax
movq %rax, 0x10e665(%rip)    # 0x1790a0
jmp 0x6aa65
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
movl $0xc000000, %esi        # imm = 0xC000000
callq 0xf990
jmp 0x6abdd
movq 0x28(%rsp), %rax
cmpq 0x10e627(%rip), %rax    # 0x179098
jne 0x6aa8a
movsd 0x1093f5(%rip), %xmm0   # 0x173e70
ucomisd 0xb8(%rsp), %xmm0
jne 0x6aa8a
jp 0x6aa8a
jmp 0x6aace
movq 0x28(%rsp), %rdi
movsd 0xb8(%rsp), %xmm0
movq 0x10e601(%rip), %rsi    # 0x1790a0
movl $0x800000, %edx         # imm = 0x800000
callq 0x6a360
movl %eax, %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0x10e5f2(%rip)    # 0x1790a8
movq %rax, 0x10e5db(%rip)    # 0x179098
movsd 0xb8(%rsp), %xmm0
movsd %xmm0, 0x1093a2(%rip)   # 0x173e70
movq 0x10e5cb(%rip), %rax    # 0x1790a0
movq %rax, 0xa0(%rsp)
movl $0x4, %edi
callq 0x38df0
movl $0x0, 0x9c(%rsp)
movl 0x9c(%rsp), %eax
cmpl 0x10e5a9(%rip), %eax    # 0x1790a8
jge 0x6abd0
movq 0xa0(%rsp), %rsi
movq %rsi, %rax
addq $0x18, %rax
movq %rax, 0xa0(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x6eb90
movq 0xa0(%rsp), %rsi
movq %rsi, %rax
addq $0x18, %rax
movq %rax, 0xa0(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x6eb90
movq 0xa0(%rsp), %rsi
movq %rsi, %rax
addq $0x18, %rax
movq %rax, 0xa0(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x6eb90
leaq 0x30(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x68(%rsp), %rdx
leaq 0x50(%rsp), %rcx
callq 0x6c5f0
leaq 0x30(%rsp), %rdi
callq 0x13e60
movq %rax, %rdi
callq 0x38ea0
leaq 0x80(%rsp), %rdi
callq 0x38e40
leaq 0x68(%rsp), %rdi
callq 0x38e40
leaq 0x50(%rsp), %rdi
callq 0x38e40
movl 0x9c(%rsp), %eax
addl $0x3, %eax
movl %eax, 0x9c(%rsp)
jmp 0x6aaf2
callq 0x3c060
addq $0xc8, %rsp
retq
movq 0xb0(%rsp), %rdi
callq 0xf890
nopw (%rax,%rax)