// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelPointGraph.cpp
// Function: gaps::R3SurfelPointGraph::UpdateNormals() const
void R3SurfelPointGraph::
UpdateNormals(void) const
{
  // Declare variables (fill it only if needed)
  R3Point *positions = NULL;

  // Compute normals for all points that don't already have them
  for (int i = 0; i < NPoints(); i++) {
    R3SurfelPoint *point = Point(i);
    if (point->HasNormal()) continue;

    // Allocate array of positions
    if (!positions) {
      positions = new R3Point [MaxNeighbors() + 1];
      if (!positions) RNAbort("Unable to allocate positions to update normals");
    }

    // Create array of positions for neighborhood
    int npositions = 0;
    positions[npositions++] = point->Position();
    for (int j = 0; j < NNeighbors(i); j++) {
      R3SurfelPoint *neighbor = Neighbor(i, j);
      positions[npositions++] = neighbor->Position();
    }

    // Compute normal with PCA of neighborhood
    R3Point centroid = R3Centroid(npositions, positions);
    R3Triad triad = R3PrincipleAxes(centroid, npositions, positions);
    R3Vector normal = triad[2];

    // Assign normal
    point->SetNormal(normal);
  }

  // Delete data
  if (positions) delete [] positions;
}

// Assembly:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x130(%rsp)
movq 0x130(%rsp), %rax
movq %rax, 0x48(%rsp)
movq $0x0, 0x128(%rsp)
movl $0x0, 0x124(%rsp)
movq 0x48(%rsp), %rdi
movl 0x124(%rsp), %eax
movl %eax, 0x44(%rsp)
callq 0x43e20
movl %eax, %ecx
movl 0x44(%rsp), %eax
cmpl %ecx, %eax
jge 0x5e0d6
movq 0x48(%rsp), %rdi
movl 0x124(%rsp), %esi
callq 0x43e40
movq %rax, 0x118(%rsp)
movq 0x118(%rsp), %rdi
callq 0x5b2f0
cmpl $0x0, %eax
je 0x5de56
jmp 0x5e0c0
cmpq $0x0, 0x128(%rsp)
jne 0x5df4b
movq 0x48(%rsp), %rdi
callq 0x44210
addl $0x1, %eax
cltq
movq %rax, 0x30(%rsp)
movl $0x18, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, 0x28(%rsp)
callq 0x13030
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x38(%rsp)
cmpq $0x0, %rax
je 0x5def9
movq 0x38(%rsp), %rax
movq 0x30(%rsp), %rcx
imulq $0x18, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0x18(%rsp)
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0xf8340
jmp 0x5dee1
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rax
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, 0x20(%rsp)
jne 0x5ded0
movq 0x38(%rsp), %rax
movq %rax, 0x128(%rsp)
cmpq $0x0, 0x128(%rsp)
jne 0x5df49
leaq 0x1478b4(%rip), %rdi    # 0x1a57cc
movb $0x0, %al
callq 0x1463a0
jmp 0x5df49
movq 0x28(%rsp), %rsi
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
callq 0x13a90
jmp 0x5e106
jmp 0x5df4b
movl $0x0, 0x108(%rsp)
movq 0x118(%rsp), %rsi
leaq 0xf0(%rsp), %rdi
callq 0x18730
movq 0x128(%rsp), %rdi
movl 0x108(%rsp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x108(%rsp)
cltq
imulq $0x18, %rax, %rax
addq %rax, %rdi
leaq 0xf0(%rsp), %rsi
callq 0xf89e0
movl $0x0, 0xec(%rsp)
movq 0x48(%rsp), %rdi
movl 0xec(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0x124(%rsp), %esi
callq 0x43ef0
movl %eax, %ecx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x5e049
movq 0x48(%rsp), %rdi
movl 0x124(%rsp), %esi
movl 0xec(%rsp), %edx
callq 0x43f20
movq %rax, 0xe0(%rsp)
movq 0xe0(%rsp), %rsi
leaq 0xc8(%rsp), %rdi
callq 0x18730
movq 0x128(%rsp), %rdi
movl 0x108(%rsp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x108(%rsp)
cltq
imulq $0x18, %rax, %rax
addq %rax, %rdi
leaq 0xc8(%rsp), %rsi
callq 0xf89e0
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x5dfa7
movl 0x108(%rsp), %esi
movq 0x128(%rsp), %rdx
leaq 0xb0(%rsp), %rdi
xorl %eax, %eax
movl %eax, %ecx
callq 0x97b20
movl 0x108(%rsp), %edx
movq 0x128(%rsp), %rcx
leaq 0x68(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %r8
callq 0x97be0
leaq 0x68(%rsp), %rdi
movl $0x2, %esi
callq 0x44220
movq %rax, %rsi
leaq 0x50(%rsp), %rdi
callq 0xf94f0
movq 0x118(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x559f0
movl 0x124(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x124(%rsp)
jmp 0x5de03
cmpq $0x0, 0x128(%rsp)
je 0x5e0fe
movq 0x128(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x5e0fc
movq (%rsp), %rdi
callq 0x13750
jmp 0x5e0fe
addq $0x138, %rsp            # imm = 0x138
retq
movq 0x110(%rsp), %rdi
callq 0x13970
nopw %cs:(%rax,%rax)