// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::GenerateIsoSurface(double, gaps::R3Point*, int) const
int R3Grid::
GenerateIsoSurface(RNScalar isolevel, R3Point *points, int max_points) const
{
  // Initialize pointer into array of points
  R3Point *pointsp = points;

  // Fill array of points with triplets forming triangles of isosurface
  int npoints = 0;
  R3Point corner_points[8];
  RNScalar corner_levels[8];
  for (int k = 0; k < ZResolution()-1; k++) {
    corner_points[0][2] = corner_points[1][2] = corner_points[4][2] = corner_points[5][2] = k;
    corner_points[2][2] = corner_points[3][2] = corner_points[6][2] = corner_points[7][2] = k+1;
    for (int j = 0; j < YResolution()-1; j++) {
      corner_points[0][1] = corner_points[1][1] = corner_points[2][1] = corner_points[3][1] = j;
      corner_points[4][1] = corner_points[5][1] = corner_points[6][1] = corner_points[7][1] = j+1;
      for (int i = 0; i < XResolution()-1; i++) {
        corner_points[0][0] = corner_points[3][0] = corner_points[4][0] = corner_points[7][0] = i;
        corner_points[1][0] = corner_points[2][0] = corner_points[5][0] = corner_points[6][0] = i+1;

        // Check if points array is full (five tris is max for any cell)
        if (npoints >= max_points - 3*5) return npoints;

        // Fill in grid values
        corner_levels[0] = GridValue(i,j,k);
        corner_levels[1] = GridValue(i+1,j,k);
        corner_levels[2] = GridValue(i+1,j,k+1);
        corner_levels[3] = GridValue(i,j,k+1);
        corner_levels[4] = GridValue(i,j+1,k);
        corner_levels[5] = GridValue(i+1,j+1,k);
        corner_levels[6] = GridValue(i+1,j+1,k+1);
        corner_levels[7] = GridValue(i,j+1,k+1);

        // Create triangles
        int n = MarchingCubes(corner_points, corner_levels, isolevel, pointsp);
        pointsp += n;
        npoints += n;
      }
    }
  }

  // Just checking
  assert((npoints % 3) == 0);
  assert(npoints < max_points);

  // Return number of points
  return npoints;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movl %edx, 0xc(%rsp)
movq %rsi, %r14
movsd %xmm0, 0x18(%rsp)
movq %rdi, %r15
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x60, %rdi
callq 0x3c530
addq $0x18, %rbx
cmpq $0xc0, %rbx
jne 0x3a6d5
xorl %ebp, %ebp
cmpl $0x2, 0x240(%r15)
jl 0x3a996
addl $-0xf, 0xc(%rsp)
xorl %ebx, %ebx
movl $0x1, 0x4(%rsp)
xorl %ebp, %ebp
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
movsd %xmm0, 0xe8(%rsp)
movsd %xmm0, 0xd0(%rsp)
movsd %xmm0, 0x88(%rsp)
movsd %xmm0, 0x70(%rsp)
leal 0x1(%rbx), %esi
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
movsd %xmm0, 0x118(%rsp)
movsd %xmm0, 0x100(%rsp)
movsd %xmm0, 0xb8(%rsp)
movsd %xmm0, 0xa0(%rsp)
movl 0x23c(%r15), %eax
cmpl $0x2, %eax
jl 0x3a97f
movl 0x238(%r15), %ecx
xorl %r8d, %r8d
movl $0x1, 0x8(%rsp)
movl %esi, 0x10(%rsp)
movl %r8d, %r12d
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
movsd %xmm0, 0xb0(%rsp)
movsd %xmm0, 0x98(%rsp)
movsd %xmm0, 0x80(%rsp)
movsd %xmm0, 0x68(%rsp)
leal 0x1(%r12), %r8d
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
movsd %xmm0, 0x110(%rsp)
movsd %xmm0, 0xf8(%rsp)
movsd %xmm0, 0xe0(%rsp)
movsd %xmm0, 0xc8(%rsp)
cmpl $0x2, %ecx
jl 0x3a96f
movl %r8d, 0x14(%rsp)
xorl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x108(%rsp)
movsd %xmm0, 0xc0(%rsp)
movsd %xmm0, 0xa8(%rsp)
movsd %xmm0, 0x60(%rsp)
leal 0x1(%rax), %r13d
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
movsd %xmm0, 0xf0(%rsp)
movsd %xmm0, 0xd8(%rsp)
movsd %xmm0, 0x90(%rsp)
movsd %xmm0, 0x78(%rsp)
cmpl 0xc(%rsp), %ebp
jge 0x3a996
movq 0x230(%r15), %rcx
movl 0x248(%r15), %esi
movl 0x244(%r15), %edx
movl %ebx, %edi
imull %esi, %edi
movl %r12d, %r8d
imull %edx, %r8d
leal (%rdi,%r8), %r9d
leal (%rax,%r9), %r10d
movslq %r10d, %r10
movsd (%rcx,%r10,8), %xmm0
movsd %xmm0, 0x20(%rsp)
leal 0x1(%rax,%r9), %r9d
movslq %r9d, %r9
movsd (%rcx,%r9,8), %xmm0
movsd %xmm0, 0x28(%rsp)
imull 0x4(%rsp), %esi
addl %esi, %r8d
leal (%rax,%r8), %r9d
addl %eax, %r8d
incl %r8d
movslq %r8d, %r8
movsd (%rcx,%r8,8), %xmm0
movsd %xmm0, 0x30(%rsp)
movslq %r9d, %r8
movsd (%rcx,%r8,8), %xmm0
movsd %xmm0, 0x38(%rsp)
imull 0x8(%rsp), %edx
addl %edx, %edi
leal (%rax,%rdi), %r8d
movslq %r8d, %r8
movsd (%rcx,%r8,8), %xmm0
movsd %xmm0, 0x40(%rsp)
leal 0x1(%rax,%rdi), %edi
movslq %edi, %rdi
movsd (%rcx,%rdi,8), %xmm0
movsd %xmm0, 0x48(%rsp)
addl %esi, %edx
movl %eax, %esi
addl %edx, %esi
addl %edx, %eax
incl %eax
cltq
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, 0x50(%rsp)
movslq %esi, %rax
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, 0x58(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movsd 0x18(%rsp), %xmm0
movq %r14, %rdx
callq 0x352d3
cltq
leaq (%rax,%rax,2), %rcx
leaq (%r14,%rcx,8), %r14
addl %eax, %ebp
movl 0x238(%r15), %ecx
leal -0x1(%rcx), %edx
movl %r13d, %eax
cmpl %edx, %r13d
jl 0x3a7f9
movl 0x23c(%r15), %eax
movl 0x10(%rsp), %esi
movl 0x14(%rsp), %r8d
leal -0x1(%rax), %edx
incl 0x8(%rsp)
cmpl %edx, %r8d
jl 0x3a78c
movl 0x240(%r15), %eax
decl %eax
incl 0x4(%rsp)
movl %esi, %ebx
cmpl %eax, %esi
jl 0x3a710
movl %ebp, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq