// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %esi, 0x14(%rsp)
movslq 0x18(%rdi), %r12
testq %r12, %r12
je 0x26e62
movq %rdi, %r15
cmpl $0x2, 0x70(%rdi)
jge 0x26e69
xorl %eax, %eax
jmp 0x27249
movl $0x18, %ecx
movq %r12, %rax
mulq %rcx
movq $-0x1, %rbx
cmovnoq %rax, %rbx
movq %rbx, %rdi
callq 0x13030
movq %rax, %r13
shlq $0x3, %r12
leaq (%r12,%r12,2), %r14
xorl %r12d, %r12d
leaq (%r12,%r13), %rdi
callq 0x84854
addq $0x18, %r12
cmpq %r12, %r14
jne 0x26e95
movq %r13, 0x8(%rsp)
movl 0x70(%r15), %r14d
leal 0x1(%r14), %eax
movslq %eax, %r13
movl $0x18, %ecx
movq %r13, %rax
mulq %rcx
movq $-0x1, %rbx
cmovnoq %rax, %rbx
movq %rbx, %rdi
callq 0x13030
movq %rax, (%rsp)
incl %r14d
je 0x26f06
shlq $0x3, %r13
leaq (,%r13,2), %r14
addq %r13, %r14
xorl %r12d, %r12d
movq (%rsp), %rax
leaq (%rax,%r12), %rdi
callq 0x83dc0
addq $0x18, %r12
cmpq %r12, %r14
jne 0x26ef0
cmpl $0x0, 0x18(%r15)
jle 0x2723b
movq (%rsp), %rax
leaq 0x18(%rax), %rcx
movq %rcx, 0x18(%rsp)
addq $0x30, %rax
movq %rax, 0x28(%rsp)
xorl %r14d, %r14d
leaq 0x30(%rsp), %rbx
movq %r14, %r12
shlq $0x4, %r12
movq 0x68(%r15), %rax
cmpl $0x1, 0xc(%rax,%r12)
movq 0x8(%rsp), %r13
jg 0x26f67
leaq (%r14,%r14,2), %rax
leaq (,%rax,8), %rdi
addq %r13, %rdi
leaq 0xbe25b(%rip), %rsi     # 0xe51b8
callq 0x84fe2
jmp 0x2722b
movq 0x10(%r15), %rax
addq %r12, %rax
cmpl $0x0, 0x14(%rsp)
je 0x270e9
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 %rbx, %rdi
callq 0x83dc2
movq (%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x8412a
callq 0xaba84
movq 0x68(%r15), %rcx
movl 0xc(%rcx,%r12), %esi
xorps %xmm1, %xmm1
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,%r12), %rax
movslq %edi, %rcx
movq (%rax,%rcx,8), %rcx
movslq %edx, %rdx
movq (%rax,%rdx,8), %rbx
movq (%rcx), %rax
movq 0x8(%rcx), %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 0x30(%rsp), %rdi
callq 0x83dc2
movq 0x18(%rsp), %r12
movq %r12, %rdi
leaq 0x30(%rsp), %rsi
callq 0x8412a
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 0x30(%rsp), %rdi
callq 0x83dc2
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
leaq 0x30(%rsp), %rsi
callq 0x8412a
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x8401e
leaq 0x78(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
leaq 0x30(%rsp), %rbx
movq %rbp, %rdx
callq 0x8401e
leaq 0x90(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x8518a
movq %rbp, %rdi
callq 0x84b6c
leaq (%r14,%r14,2), %rax
leaq (,%rax,8), %rdi
addq %r13, %rdi
movq %rbp, %rsi
jmp 0x271fe
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 %rbx, %rdi
callq 0x83dc2
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x8412a
movq 0x68(%r15), %rax
movl 0xc(%rax,%r12), %ebp
testl %ebp, %ebp
jle 0x271b5
movq %r14, 0x20(%rsp)
movq 0x18(%rsp), %r13
xorl %ebx, %ebx
movq %r15, %r14
leaq 0x30(%rsp), %r15
movq (%rax,%r12), %rax
movq (%rax,%rbx,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 %r15, %rdi
callq 0x83dc2
incq %rbx
movq %r13, %rdi
movq %r15, %rsi
callq 0x8412a
movq 0x68(%r14), %rax
movslq 0xc(%rax,%r12), %rbp
addq $0x18, %r13
cmpq %rbp, %rbx
jl 0x2714b
movq 0x8(%rsp), %r13
movq %r14, %r15
movq 0x20(%rsp), %r14
incl %ebp
leaq 0x78(%rsp), %r12
movq %r12, %rdi
movl %ebp, %esi
movq (%rsp), %rbx
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x545c1
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
movl %ebp, %edx
movq %rbx, %rcx
movq %rdi, %rbx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x5464b
leaq (%r14,%r14,2), %rax
leaq (,%rax,8), %rdi
addq %r13, %rdi
leaq 0x60(%rsp), %rsi
callq 0x84fe2
leaq (%r14,%r14,2), %rax
leaq (%r13,%rax,8), %r13
movq %r13, %rdi
callq 0x849de
cltq
xorpd %xmm0, %xmm0
ucomisd (%r13,%rax,8), %xmm0
jbe 0x2722b
movq %r13, %rdi
callq 0x84bc4
incq %r14
movslq 0x18(%r15), %rax
cmpq %rax, %r14
jl 0x26f2f
movq (%rsp), %rdi
callq 0x13680
movq 0x8(%rsp), %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
movq (%rsp), %rdi
jmp 0x2726a
movq %rax, %r15
movq %r13, %rdi
movq %rbx, %rsi
callq 0x139b0
movq %r15, %rdi
callq 0x138b0