// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
testl %edx, %edx
js 0x369fc
movq %rsi, %r14
movq 0x68(%rsi), %rax
movl %edx, %ebx
shlq $0x4, %rbx
cmpl $0x1, 0xc(%rax,%rbx)
jg 0x36a13
leaq 0xaf7cd(%rip), %rsi     # 0xe61d0
movq %r15, %rdi
callq 0x85826
movq %r15, %rax
jmp 0x36d2f
movl %ecx, %ebp
movq 0x10(%r14), %r12
addq %rbx, %r12
movq %r15, 0x8(%rsp)
movq %r15, %rdi
callq 0x85814
testl %ebp, %ebp
je 0x36bbb
xorl %r15d, %r15d
leaq (%rsp,%r15), %rdi
addq $0x30, %rdi
callq 0x84d7c
addq $0x18, %r15
cmpq $0x48, %r15
jne 0x36a34
callq 0xacf78
movq 0x68(%r14), %rcx
movl 0xc(%rcx,%rbx), %esi
cvtsi2sd %esi, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %edi
movl %esi, %eax
shrl $0x1f, %eax
addl %esi, %eax
sarl %eax
addl %edi, %eax
cltd
idivl %esi
movq (%rcx,%rbx), %rax
movslq %edi, %rcx
movq (%rax,%rcx,8), %r13
movslq %edx, %rcx
movq (%rax,%rcx,8), %rbx
movq (%r12), %rax
movq 0x8(%r12), %rcx
xorps %xmm0, %xmm0
cvtss2sd (%rcx), %xmm0
addsd 0x10(%rax), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rcx), %xmm1
addsd 0x18(%rax), %xmm1
cvtss2sd 0x8(%rcx), %xmm2
addsd 0x20(%rax), %xmm2
leaq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x84d7e
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x850ec
movq (%r13), %rax
movq 0x8(%r13), %rcx
xorps %xmm0, %xmm0
cvtss2sd (%rcx), %xmm0
addsd 0x10(%rax), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rcx), %xmm1
addsd 0x18(%rax), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rcx), %xmm2
addsd 0x20(%rax), %xmm2
leaq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0x84d7e
leaq 0x48(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x850ec
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
xorps %xmm0, %xmm0
cvtss2sd (%rcx), %xmm0
addsd 0x10(%rax), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rcx), %xmm1
addsd 0x18(%rax), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rcx), %xmm2
addsd 0x20(%rax), %xmm2
leaq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0x84d7e
leaq 0x60(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x850ec
leaq 0x18(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x84fe0
leaq 0x90(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x84fe0
leaq 0x78(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x8616a
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x85fc2
movq %rbx, %rdi
callq 0x85b38
jmp 0x36d0f
movq 0x68(%r14), %rax
movl 0xc(%rax,%rbx), %ebp
leal 0x1(%rbp), %eax
movl %eax, 0x4(%rsp)
movslq %eax, %r13
movl $0x18, %ecx
movq %r13, %rax
mulq %rcx
movq $-0x1, %rdi
cmovnoq %rax, %rdi
movq %rdi, 0x10(%rsp)
callq 0x13030
movq %rax, %r15
incl %ebp
je 0x36c11
shlq $0x3, %r13
leaq (%r13,%r13,2), %r13
xorl %ebp, %ebp
leaq (%r15,%rbp), %rdi
callq 0x84d7c
addq $0x18, %rbp
cmpq %rbp, %r13
jne 0x36bff
movq (%r12), %rax
movq 0x8(%r12), %rcx
cvtss2sd (%rcx), %xmm0
addsd 0x10(%rax), %xmm0
cvtss2sd 0x4(%rcx), %xmm1
addsd 0x18(%rax), %xmm1
cvtss2sd 0x8(%rcx), %xmm2
addsd 0x20(%rax), %xmm2
leaq 0x30(%rsp), %r12
movq %r12, %rdi
callq 0x84d7e
movq %r15, %rdi
movq %r12, %rsi
callq 0x850ec
movq 0x68(%r14), %rax
cmpl $0x0, 0xc(%rax,%rbx)
jle 0x36cc5
movq %r15, %r12
addq $0x18, %r12
xorl %ebp, %ebp
leaq 0x30(%rsp), %r13
movq (%rax,%rbx), %rax
movq (%rax,%rbp,8), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
addsd 0x10(%rcx), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax), %xmm1
addsd 0x18(%rcx), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rax), %xmm2
addsd 0x20(%rcx), %xmm2
movq %r13, %rdi
callq 0x84d7e
incq %rbp
movq %r12, %rdi
movq %r13, %rsi
callq 0x850ec
movq 0x68(%r14), %rax
movslq 0xc(%rax,%rbx), %rcx
addq $0x18, %r12
cmpq %rcx, %rbp
jl 0x36c68
leaq 0x18(%rsp), %r14
movq %r14, %rdi
movl 0x4(%rsp), %ebx
movl %ebx, %esi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x5501d
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
movl %ebx, %edx
movq %r15, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x550a7
movq %r15, %rdi
callq 0x13680
leaq 0x60(%rsp), %rsi
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x85fc2
movq %rbx, %rdi
callq 0x859aa
cltq
xorpd %xmm0, %xmm0
ucomisd (%rbx,%rax,8), %xmm0
jbe 0x36d2c
movq %rbx, %rdi
callq 0x85ba4
movq %rbx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r15, %rdi
movq 0x10(%rsp), %rsi
callq 0x139b0
movq %rbx, %rdi
callq 0x138b0
nop