// File: /workspace/github_cpp_makefile_star2_O1/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 $0x138, %rsp            # imm = 0x138
movl %edx, 0xc(%rsp)
movq %rsi, %r14
movsd %xmm0, 0x28(%rsp)
movq %rdi, %r15
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x30, %rdi
callq 0x3ca24
addq $0x18, %rbx
cmpq $0xc0, %rbx
jne 0x3ab2d
addl $-0xf, 0xc(%rsp)
movl $0x1, 0x10(%rsp)
xorl %ecx, %ecx
xorl %esi, %esi
movl 0x240(%r15), %eax
decl %eax
cmpl %eax, %esi
jge 0x3ae63
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
movsd %xmm0, 0xb8(%rsp)
movsd %xmm0, 0xa0(%rsp)
movsd %xmm0, 0x58(%rsp)
movsd %xmm0, 0x40(%rsp)
leal 0x1(%rsi), %eax
movl %eax, 0x18(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0xe8(%rsp)
movsd %xmm0, 0xd0(%rsp)
movsd %xmm0, 0x88(%rsp)
movsd %xmm0, 0x70(%rsp)
xorl %edx, %edx
movl $0x1, 0x14(%rsp)
movl %edx, %r8d
movl 0x23c(%r15), %eax
decl %eax
cmpl %eax, %edx
movl %eax, 0x20(%rsp)
jge 0x3ae48
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
movsd %xmm0, 0x80(%rsp)
movsd %xmm0, 0x68(%rsp)
movsd %xmm0, 0x50(%rsp)
leal 0x1(%r8), %r9d
xorps %xmm1, %xmm1
cvtsi2sd %r9d, %xmm1
movsd %xmm0, 0x38(%rsp)
movsd %xmm1, 0xe0(%rsp)
movsd %xmm1, 0xc8(%rsp)
movsd %xmm1, 0xb0(%rsp)
movsd %xmm1, 0x98(%rsp)
cmpl $0x2, 0x238(%r15)
setge %dl
movl %r9d, 0x24(%rsp)
jl 0x3ae22
xorl %eax, %eax
movq %rax, 0xd8(%rsp)
movq %rax, 0x90(%rsp)
movq %rax, 0x78(%rsp)
movq %rax, 0x30(%rsp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0xc0(%rsp)
movq %rax, 0xa8(%rsp)
movq %rax, 0x60(%rsp)
movq %rax, 0x48(%rsp)
cmpl 0xc(%rsp), %ecx
jge 0x3ae26
movl %edi, 0x1c(%rsp)
movl $0x1, %r12d
movl %ecx, %ebx
movq 0x230(%r15), %rax
movl 0x248(%r15), %edx
movl 0x244(%r15), %ecx
movq %rsi, %r13
imull %edx, %esi
movq %r8, %rbp
movl %r8d, %edi
imull %ecx, %edi
leal (%rsi,%rdi), %r8d
leal (%r12,%r8), %r9d
addl %r12d, %r8d
decl %r8d
movslq %r8d, %r8
movsd (%rax,%r8,8), %xmm0
movsd %xmm0, 0xf0(%rsp)
movslq %r9d, %r8
movsd (%rax,%r8,8), %xmm0
movsd %xmm0, 0xf8(%rsp)
imull 0x10(%rsp), %edx
addl %edx, %edi
leal (%r12,%rdi), %r8d
movslq %r8d, %r8
movsd (%rax,%r8,8), %xmm0
movsd %xmm0, 0x100(%rsp)
leal -0x1(%r12,%rdi), %edi
movslq %edi, %rdi
movsd (%rax,%rdi,8), %xmm0
movsd %xmm0, 0x108(%rsp)
imull 0x14(%rsp), %ecx
addl %ecx, %esi
leal (%r12,%rsi), %edi
addl %r12d, %esi
decl %esi
movslq %esi, %rsi
movsd (%rax,%rsi,8), %xmm0
movsd %xmm0, 0x110(%rsp)
movslq %edi, %rsi
movsd (%rax,%rsi,8), %xmm0
movsd %xmm0, 0x118(%rsp)
addl %edx, %ecx
leal (%r12,%rcx), %edx
movslq %edx, %rdx
movsd (%rax,%rdx,8), %xmm0
movsd %xmm0, 0x120(%rsp)
leal -0x1(%r12,%rcx), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x128(%rsp)
leaq 0x30(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
movsd 0x28(%rsp), %xmm0
movq %r14, %rdx
callq 0x354c2
movslq %eax, %rcx
leaq (%rcx,%rcx,2), %rax
leaq (%r14,%rax,8), %r14
addl %ebx, %ecx
movl 0x238(%r15), %eax
decl %eax
cmpl %eax, %r12d
setl %dl
jge 0x3ae2a
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0xd8(%rsp)
movsd %xmm0, 0x90(%rsp)
movsd %xmm0, 0x78(%rsp)
movsd %xmm0, 0x30(%rsp)
incl %r12d
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0xc0(%rsp)
movsd %xmm0, 0xa8(%rsp)
movsd %xmm0, 0x60(%rsp)
movsd %xmm0, 0x48(%rsp)
movl %ecx, %eax
cmpl 0xc(%rsp), %ecx
movq %r13, %rsi
movq %rbp, %r8
jl 0x3ac9c
jmp 0x3ae34
movl %edi, %eax
jmp 0x3ae34
movl %ecx, %eax
jmp 0x3ae34
movl 0x1c(%rsp), %eax
movq %r13, %rsi
movq %rbp, %r8
incl 0x14(%rsp)
movl %eax, %edi
testb %dl, %dl
movl 0x24(%rsp), %edx
je 0x3abc7
jmp 0x3ae4a
movl %edi, %eax
incl 0x10(%rsp)
movl 0x18(%rsp), %edx
movl %edx, %esi
movl %eax, %edi
cmpl 0x20(%rsp), %r8d
jge 0x3ab58
jmp 0x3ae65
movl %ecx, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop