// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::CreateNormals(gaps::R3SurfelPointGraph*, int)
R3Vector *
CreateNormals(R3SurfelPointGraph *graph, RNBoolean fast_and_approximate)
{
#if 0
  // Sorry
  RNAbort("CreateNormals has been deprecated.  Use UpdateNormals instead.");
  return NULL;
#else
  // Check graph
  if (graph->NPoints() == 0) return NULL;
  if (graph->MaxNeighbors() < 2) return NULL;

  // Allocate normals
  R3Vector *normals = new R3Vector [ graph->NPoints() ];
  if (!normals) {
    RNFail("Unable to allocate normals\n");
    return NULL;
  }

  // Allocate temporary memory
  R3Point *positions = new R3Point [graph->MaxNeighbors() + 1];

  // Compute normals
  for (int i = 0; i < graph->NPoints(); i++) {
    const R3SurfelPoint *point = graph->Point(i);
    if (graph->NNeighbors(i) < 2) { 
      // Punt
      normals[i] = R3zero_vector; 
    }
    else {
      if (fast_and_approximate) {
        // Compute normal with vector cross product
        positions[0] = point->Position();
        int index1 = (int) (RNRandomScalar() * graph->NNeighbors(i));
        int index2 = (index1 + graph->NNeighbors(i)/2) % graph->NNeighbors(i);
        const R3SurfelPoint *neighbor1 = graph->Neighbor(i, index1);
        const R3SurfelPoint *neighbor2 = graph->Neighbor(i, index2);
        positions[1] = neighbor1->Position();
        positions[2] = neighbor2->Position();
        R3Vector v1 = positions[1] - positions[0];
        R3Vector v2 = positions[2] - positions[0];
        R3Vector n = v1 % v2;
        n.Normalize();
        normals[i] = n;
      }
      else {
        // Compute normal with PCA of neighborhood
        positions[0] = point->Position();
        for (int j = 0; j < graph->NNeighbors(i); j++) {
          const R3SurfelPoint *neighbor = graph->Neighbor(i, j);
          positions[j+1] = neighbor->Position();
        }
        int npositions = graph->NNeighbors(i) + 1;
        R3Point centroid = R3Centroid(npositions, positions);
        R3Triad triad = R3PrincipleAxes(centroid, npositions, positions);
        normals[i] = triad[2];
      }

      // Flip normal so that positive in max dimension
      int dim = normals[i].MaxDimension();
      if (normals[i][dim] < 0) normals[i].Flip();
    }
  }

  // Delete positions
  delete [] positions;

  // Return normals
  return normals;
#endif
}

// Assembly:
subq $0x228, %rsp            # imm = 0x228
movq %rdi, 0x218(%rsp)
movl %esi, 0x214(%rsp)
movq 0x218(%rsp), %rdi
callq 0x43e20
cmpl $0x0, %eax
jne 0x3c039
movq $0x0, 0x220(%rsp)
jmp 0x3c6d2
movq 0x218(%rsp), %rdi
callq 0x44210
cmpl $0x2, %eax
jge 0x3c05c
movq $0x0, 0x220(%rsp)
jmp 0x3c6d2
movq 0x218(%rsp), %rdi
callq 0x43e20
cltq
movq %rax, 0x78(%rsp)
movl $0x18, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, 0x70(%rsp)
callq 0x13030
movq %rax, %rcx
movq 0x78(%rsp), %rax
movq %rcx, 0x80(%rsp)
cmpq $0x0, %rax
je 0x3c0f6
movq 0x80(%rsp), %rax
movq 0x78(%rsp), %rcx
imulq $0x18, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0x60(%rsp)
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0xf94a0
jmp 0x3c0de
movq 0x60(%rsp), %rcx
movq 0x58(%rsp), %rax
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, 0x68(%rsp)
jne 0x3c0cd
movq 0x80(%rsp), %rax
movq %rax, 0x208(%rsp)
cmpq $0x0, 0x208(%rsp)
jne 0x3c15b
leaq 0x168b86(%rip), %rdi    # 0x1a4c9e
movb $0x0, %al
callq 0x1464b0
movq $0x0, 0x220(%rsp)
jmp 0x3c6d2
movq 0x70(%rsp), %rsi
movq 0x80(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x200(%rsp)
movl %eax, 0x1fc(%rsp)
callq 0x13a90
jmp 0x3c6e2
movq 0x218(%rsp), %rdi
callq 0x44210
addl $0x1, %eax
cltq
movq %rax, 0x48(%rsp)
movl $0x18, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, 0x40(%rsp)
callq 0x13030
movq %rax, %rcx
movq 0x48(%rsp), %rax
movq %rcx, 0x50(%rsp)
cmpq $0x0, %rax
je 0x3c1f2
movq 0x50(%rsp), %rax
movq 0x48(%rsp), %rcx
imulq $0x18, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0xf8340
jmp 0x3c1da
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %rax
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, 0x38(%rsp)
jne 0x3c1c9
movq 0x50(%rsp), %rax
movq %rax, 0x1f0(%rsp)
movl $0x0, 0x1ec(%rsp)
movl 0x1ec(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0x218(%rsp), %rdi
callq 0x43e20
movl %eax, %ecx
movl 0x24(%rsp), %eax
cmpl %ecx, %eax
jge 0x3c6a7
movq 0x218(%rsp), %rdi
movl 0x1ec(%rsp), %esi
callq 0x43e40
movq %rax, 0x1e0(%rsp)
movq 0x218(%rsp), %rdi
movl 0x1ec(%rsp), %esi
callq 0x43ef0
cmpl $0x2, %eax
jge 0x3c2b5
movq 0x208(%rsp), %rdi
movslq 0x1ec(%rsp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rdi
leaq 0x5c1f1d(%rip), %rsi    # 0x5fe1a0
callq 0xfa170
jmp 0x3c68f
movq 0x40(%rsp), %rsi
movq 0x50(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x200(%rsp)
movl %eax, 0x1fc(%rsp)
callq 0x13a90
jmp 0x3c6e2
cmpl $0x0, 0x214(%rsp)
je 0x3c4b3
movq 0x1e0(%rsp), %rsi
leaq 0x1c8(%rsp), %rdi
callq 0x18730
movq 0x1f0(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
callq 0xf89e0
callq 0x144f30
movsd %xmm0, 0x10(%rsp)
movq 0x218(%rsp), %rdi
movl 0x1ec(%rsp), %esi
callq 0x43ef0
movsd 0x10(%rsp), %xmm0
cvtsi2sd %eax, %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x1c4(%rsp)
movl 0x1c4(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x218(%rsp), %rdi
movl 0x1ec(%rsp), %esi
callq 0x43ef0
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl 0x1c(%rsp), %eax
addl %ecx, %eax
movl %eax, 0x20(%rsp)
movq 0x218(%rsp), %rdi
movl 0x1ec(%rsp), %esi
callq 0x43ef0
movl %eax, %ecx
movl 0x20(%rsp), %eax
cltd
idivl %ecx
movl %edx, 0x1c0(%rsp)
movq 0x218(%rsp), %rdi
movl 0x1ec(%rsp), %esi
movl 0x1c4(%rsp), %edx
callq 0x43f20
movq %rax, 0x1b8(%rsp)
movq 0x218(%rsp), %rdi
movl 0x1ec(%rsp), %esi
movl 0x1c0(%rsp), %edx
callq 0x43f20
movq %rax, 0x1b0(%rsp)
movq 0x1b8(%rsp), %rsi
leaq 0x198(%rsp), %rdi
callq 0x18730
movq 0x1f0(%rsp), %rdi
addq $0x18, %rdi
leaq 0x198(%rsp), %rsi
callq 0xf89e0
movq 0x1b0(%rsp), %rsi
leaq 0x180(%rsp), %rdi
callq 0x18730
movq 0x1f0(%rsp), %rdi
addq $0x30, %rdi
leaq 0x180(%rsp), %rsi
callq 0xf89e0
movq 0x1f0(%rsp), %rsi
addq $0x18, %rsi
movq 0x1f0(%rsp), %rdx
leaq 0x168(%rsp), %rdi
callq 0xf86b0
movq 0x1f0(%rsp), %rsi
addq $0x30, %rsi
movq 0x1f0(%rsp), %rdx
leaq 0x150(%rsp), %rdi
callq 0xf86b0
leaq 0x138(%rsp), %rdi
leaq 0x168(%rsp), %rsi
leaq 0x150(%rsp), %rdx
callq 0xfa2f0
leaq 0x138(%rsp), %rdi
callq 0xf9c10
movq 0x208(%rsp), %rdi
movslq 0x1ec(%rsp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rdi
leaq 0x138(%rsp), %rsi
callq 0xfa170
jmp 0x3c622
movq 0x1e0(%rsp), %rsi
leaq 0x120(%rsp), %rdi
callq 0x18730
movq 0x1f0(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0xf89e0
movl $0x0, 0x11c(%rsp)
movl 0x11c(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x218(%rsp), %rdi
movl 0x1ec(%rsp), %esi
callq 0x43ef0
movl %eax, %ecx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x3c587
movq 0x218(%rsp), %rdi
movl 0x1ec(%rsp), %esi
movl 0x11c(%rsp), %edx
callq 0x43f20
movq %rax, 0x110(%rsp)
movq 0x110(%rsp), %rsi
leaq 0xf8(%rsp), %rdi
callq 0x18730
movq 0x1f0(%rsp), %rdi
movl 0x11c(%rsp), %eax
addl $0x1, %eax
cltq
imulq $0x18, %rax, %rax
addq %rax, %rdi
leaq 0xf8(%rsp), %rsi
callq 0xf89e0
movl 0x11c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x11c(%rsp)
jmp 0x3c4e8
movq 0x218(%rsp), %rdi
movl 0x1ec(%rsp), %esi
callq 0x43ef0
addl $0x1, %eax
movl %eax, 0xf4(%rsp)
movl 0xf4(%rsp), %esi
movq 0x1f0(%rsp), %rdx
leaq 0xd8(%rsp), %rdi
xorl %eax, %eax
movl %eax, %ecx
callq 0x97b20
movl 0xf4(%rsp), %edx
movq 0x1f0(%rsp), %rcx
leaq 0x90(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %r8
callq 0x97be0
leaq 0x90(%rsp), %rdi
movl $0x2, %esi
callq 0x44220
movq %rax, %rsi
movq 0x208(%rsp), %rdi
movslq 0x1ec(%rsp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0xfa170
movq 0x208(%rsp), %rdi
movslq 0x1ec(%rsp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0xf97d0
movl %eax, 0x8c(%rsp)
movq 0x208(%rsp), %rdi
movslq 0x1ec(%rsp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rdi
movl 0x8c(%rsp), %esi
callq 0x31d80
xorps %xmm0, %xmm0
ucomisd (%rax), %xmm0
jbe 0x3c68d
movq 0x208(%rsp), %rdi
movslq 0x1ec(%rsp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0xf9c90
jmp 0x3c68f
jmp 0x3c691
movl 0x1ec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1ec(%rsp)
jmp 0x3c20a
movq 0x1f0(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x3c6c2
movq (%rsp), %rdi
callq 0x13750
movq 0x208(%rsp), %rax
movq %rax, 0x220(%rsp)
movq 0x220(%rsp), %rax
addq $0x228, %rsp            # imm = 0x228
retq
movq 0x200(%rsp), %rdi
callq 0x13970
nop