// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelPointGraph.cpp
// Function: gaps::R3SurfelPointGraph::PointNormal(int, int) const
R3Vector R3SurfelPointGraph::
PointNormal(int point_index, RNBoolean fast_and_approximate) const
{
  // Get point index
  if (point_index < 0) return R3zero_vector;
  if (NNeighbors(point_index) < 2) return R3zero_vector;
  R3SurfelPoint *point = Point(point_index);

  // Check algorithm
  R3Vector normal;
  if (fast_and_approximate) {
    // Compute normal with vector cross product
    R3Point positions[3];
    int index1 = (int) (RNRandomScalar() * NNeighbors(point_index));
    int index2 = (index1 + NNeighbors(point_index)/2) % NNeighbors(point_index);
    const R3SurfelPoint *neighbor1 = Neighbor(point_index, index1);
    const R3SurfelPoint *neighbor2 = Neighbor(point_index, index2);
    positions[0] = point->Position();
    positions[1] = neighbor1->Position();
    positions[2] = neighbor2->Position();
    R3Vector v1 = positions[1] - positions[0];
    R3Vector v2 = positions[2] - positions[0];
    normal = v1 % v2;
    normal.Normalize();
  }
  else {
    // Create neighborhood points
    int npositions = NNeighbors(point_index) + 1;
    R3Point *positions = new R3Point [npositions];
    positions[0] = point->Position();
    for (int j = 0; j < NNeighbors(point_index); j++) {
      const R3SurfelPoint *neighbor = Neighbor(point_index, j);
      positions[j+1] = neighbor->Position();
    }

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

    // Delete neighborhood points
    delete [] positions;

    // Return normal
    normal = triad[2];
  }

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

  // Return normal
  return normal;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %r12
testl %edx, %edx
js 0x32c80
movq %rsi, %r14
movq 0x68(%rsi), %rax
movl %edx, %ebx
shlq $0x4, %rbx
cmpl $0x1, 0xc(%rax,%rbx)
jg 0x32c97
leaq 0x9d4d1(%rip), %rsi     # 0xd0158
movq %r12, %rdi
callq 0x7898c
movq %r12, %rax
jmp 0x32ed1
movl %ecx, %ebp
movq 0x10(%r14), %r15
addq %rbx, %r15
movq %r12, 0x8(%rsp)
movq %r12, %rdi
callq 0x7897a
testl %ebp, %ebp
je 0x32dc2
xorl %r12d, %r12d
leaq (%rsp,%r12), %rdi
addq $0x30, %rdi
callq 0x781ba
addq $0x18, %r12
cmpq $0x48, %r12
jne 0x32cb8
callq 0x9c2ba
movq 0x68(%r14), %rsi
movl 0xc(%rsi,%rbx), %ecx
cvtsi2sd %ecx, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %edi
pushq $0x2
popq %r8
movl %ecx, %eax
cltd
idivl %r8d
addl %edi, %eax
cltd
idivl %ecx
movq (%rsi,%rbx), %rax
movslq %edi, %rcx
movq (%rax,%rcx,8), %r12
movslq %edx, %rcx
movq (%rax,%rcx,8), %r13
leaq 0x18(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x16310
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x784a6
leaq 0x18(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x16310
leaq 0x48(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x784a6
leaq 0x18(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x16310
leaq 0x60(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x784a6
leaq 0x18(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x78399
leaq 0x90(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x78399
leaq 0x78(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x7902e
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x78eec
movq %rbx, %rdi
callq 0x78c7e
jmp 0x32eb1
movq 0x68(%r14), %rax
movl 0xc(%rax,%rbx), %r13d
leal 0x1(%r13), %eax
movl %eax, 0x4(%rsp)
movslq %eax, %rbp
pushq $0x18
popq %rcx
movq %rbp, %rax
mulq %rcx
pushq $-0x1
popq %rdi
cmovnoq %rax, %rdi
movq %rdi, 0x10(%rsp)
callq 0x13030
movq %rax, %r12
incl %r13d
je 0x32e11
imulq $0x18, %rbp, %rbp
xorl %r13d, %r13d
leaq (%r12,%r13), %rdi
callq 0x781ba
addq $0x18, %r13
cmpq %r13, %rbp
jne 0x32dff
leaq 0x30(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x16310
movq %r12, %rdi
movq %r13, %rsi
callq 0x784a6
movq %r12, %r15
addq $0x18, %r15
xorl %ebp, %ebp
movq 0x68(%r14), %rax
movslq 0xc(%rax,%rbx), %rcx
cmpq %rcx, %rbp
jge 0x32e67
movq (%rax,%rbx), %rax
movq (%rax,%rbp,8), %rsi
movq %r13, %rdi
callq 0x16310
incq %rbp
movq %r15, %rdi
movq %r13, %rsi
callq 0x784a6
addq $0x18, %r15
jmp 0x32e35
leaq 0x18(%rsp), %r14
movq %r14, %rdi
movl 0x4(%rsp), %ebx
movl %ebx, %esi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x4d640
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
movl %ebx, %edx
movq %r12, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x4d6c9
movq %r12, %rdi
callq 0x136a0
leaq 0x60(%rsp), %rsi
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x78eec
movq %rbx, %rdi
callq 0x78b00
cltq
xorpd %xmm0, %xmm0
ucomisd (%rbx,%rax,8), %xmm0
jbe 0x32ece
movq %rbx, %rdi
callq 0x78cd2
movq %rbx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r12, %rdi
movq 0x10(%rsp), %rsi
callq 0x139f0
movq %rbx, %rdi
callq 0x138f0
nop