// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3TriangleArray.cpp
// Function: gaps::R3TriangleArray::CreateVertexNormals(double)
void R3TriangleArray::
CreateVertexNormals(RNAngle max_angle)
{
  // Create data for computing vertex normals
  R3Vector *normals = new R3Vector [ vertices.NEntries() ];
  RNBoolean *smooth = new RNBoolean [ vertices.NEntries() ];
  for (int i = 0; i < vertices.NEntries(); i++) {
    R3TriangleVertex *vertex = vertices.Kth(i);
    normals[i] = R3zero_vector;
    smooth[i] = TRUE;
    vertex->SetMark(i);
  }

  // Aggregate info for vertex normals
  for (int i = 0; i < triangles.NEntries(); i++) {
    R3Triangle *triangle = triangles.Kth(i);
    RNArea weight = triangle->Area();
    const R3Vector& normal = triangle->Normal();
    for (int j = 0; j < 3; j++) {
      R3TriangleVertex *vertex = triangle->Vertex(j);
      int index = vertex->Mark();
      normals[index] += weight * normal;
    }
  }

  // Normalize vertex normals
  for (int i = 0; i < vertices.NEntries(); i++) {
    normals[i].Normalize();
  }

  // Mark smooth vertices
  RNScalar min_dot = (max_angle < RN_PI) ? cos(max_angle) : -1;
  for (int i = 0; i < triangles.NEntries(); i++) {
    R3Triangle *triangle = triangles.Kth(i);
    const R3Vector& normal = triangle->Normal();
    for (int j = 0; j < 3; j++) {
      R3TriangleVertex *vertex = triangle->Vertex(j);
      int index = vertex->Mark();
      if (normal.Dot(normals[index]) > min_dot) continue;
      smooth[index] = FALSE;
    }
  }
        
  // Update vertex normals
  for (int i = 0; i < vertices.NEntries(); i++) {
    R3TriangleVertex *vertex = vertices.Kth(i);
    if (vertex->HasNormal()) continue;
    if (normals[i].IsZero()) continue;
    if (!smooth[i]) continue;
    vertex->SetNormal(normals[i]);
  }

  // Update triangle flags
  for (int i = 0; i < triangles.NEntries(); i++) {
    R3Triangle *triangle = triangles.Kth(i);
    triangle->UpdateFlags();
  }

  // Delete temporary data
  delete [] normals;
  delete [] smooth;

  // Update
  Update();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movsd %xmm0, 0x20(%rsp)
movq %rdi, 0x8(%rsp)
movslq 0x14(%rdi), %r12
movl $0x18, %ecx
movq %r12, %rax
mulq %rcx
movq $-0x1, %r15
cmovnoq %rax, %r15
movq %r15, %rdi
callq 0xe030
movq %rax, %r14
testq %r12, %r12
je 0x528d7
shlq $0x3, %r12
leaq (%r12,%r12,2), %rbx
xorl %r12d, %r12d
leaq (%r14,%r12), %rdi
callq 0x3cfc4
addq $0x18, %r12
cmpq %r12, %rbx
jne 0x52856
movq 0x8(%rsp), %r15
movl 0x14(%r15), %ebp
movslq %ebp, %rax
shlq $0x2, %rax
testl %ebp, %ebp
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, 0x10(%rsp)
testl %ebp, %ebp
jle 0x528ea
xorl %ebx, %ebx
movq %r14, %r13
movq 0x10(%rsp), %r12
movq 0x8(%r15), %rax
movq (%rax,%rbx,8), %r15
movq %r13, %rdi
leaq 0x55e31(%rip), %rsi     # 0xa86e0
callq 0x3d752
movl $0x1, (%r12,%rbx,4)
movl %ebx, 0x60(%r15)
movq 0x8(%rsp), %r15
incq %rbx
movslq 0x14(%r15), %rbp
addq $0x18, %r13
cmpq %rbp, %rbx
jl 0x5289d
jmp 0x528ea
xorl %ebp, %ebp
xorl %edi, %edi
callq 0xe030
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %r15
cmpl $0x0, 0x24(%r15)
jle 0x5296c
xorl %ecx, %ecx
leaq 0x30(%rsp), %r12
movq 0x18(%r15), %rax
movq %rcx, 0x28(%rsp)
movq (%rax,%rcx,8), %r13
movq (%r13), %rax
movq %r13, %rdi
callq *0x58(%rax)
movsd %xmm0, 0x18(%rsp)
leaq 0x20(%r13), %rbp
movl $0x1, %r15d
movq (%r13,%r15,8), %rax
movslq 0x60(%rax), %rbx
movq %r12, %rdi
movq %rbp, %rsi
movsd 0x18(%rsp), %xmm0
callq 0x3d962
leaq (%rbx,%rbx,2), %rax
leaq (%r14,%rax,8), %rdi
movq %r12, %rsi
callq 0x3d984
incq %r15
cmpq $0x4, %r15
jne 0x5291f
movq 0x28(%rsp), %rcx
incq %rcx
movq 0x8(%rsp), %r15
movslq 0x24(%r15), %rax
cmpq %rax, %rcx
jl 0x528f8
movl 0x14(%r15), %ebp
testl %ebp, %ebp
jle 0x5298d
xorl %ebx, %ebx
movq %r14, %r12
movq %r12, %rdi
callq 0x3d2dc
incq %rbx
movslq 0x14(%r15), %rax
addq $0x18, %r12
cmpq %rax, %rbx
jl 0x52975
leaq 0x57634(%rip), %rax     # 0xa9fc8
movsd (%rax), %xmm0
movsd 0x20(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x529af
movapd %xmm1, %xmm0
callq 0xe860
jmp 0x529b7
movsd 0x4f691(%rip), %xmm0    # 0xa2048
movsd %xmm0, 0x18(%rsp)
movl 0x24(%r15), %eax
testl %eax, %eax
jle 0x52a20
xorl %ebx, %ebx
movq 0x18(%r15), %rax
movq (%rax,%rbx,8), %r13
leaq 0x20(%r13), %r12
movl $0x1, %ebp
movq (%r13,%rbp,8), %rax
movslq 0x60(%rax), %r15
leaq (%r15,%r15,2), %rax
leaq (%r14,%rax,8), %rsi
movq %r12, %rdi
callq 0x3d252
ucomisd 0x18(%rsp), %xmm0
ja 0x52a06
movq 0x10(%rsp), %rax
movl $0x0, (%rax,%r15,4)
incq %rbp
cmpq $0x4, %rbp
jne 0x529d8
incq %rbx
movq 0x8(%rsp), %r15
movslq 0x24(%r15), %rax
cmpq %rax, %rbx
jl 0x529c7
cmpl $0x0, 0x14(%r15)
jle 0x52ace
xorl %r13d, %r13d
xorpd %xmm1, %xmm1
movq %r14, %r12
movq 0x10(%rsp), %rbx
movq 0x8(%r15), %rax
movq (%rax,%r13,8), %rbp
testb $0x1, 0x59(%rbp)
jne 0x52ab6
movsd (%r12), %xmm0
ucomisd %xmm1, %xmm0
jne 0x52a74
jp 0x52a74
movsd 0x8(%r12), %xmm0
ucomisd %xmm1, %xmm0
jne 0x52a74
jp 0x52a74
movsd 0x10(%r12), %xmm0
ucomisd %xmm1, %xmm0
jne 0x52a74
jnp 0x52ab6
cmpl $0x0, (%rbx,%r13,4)
je 0x52ab6
leaq 0x18(%rbp), %rdi
movq %r12, %rsi
callq 0x3d752
xorpd %xmm1, %xmm1
movsd 0x18(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jne 0x52ab2
jp 0x52ab2
movsd 0x20(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jne 0x52ab2
jp 0x52ab2
movsd 0x28(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jne 0x52ab2
jnp 0x52ab6
orb $0x1, 0x59(%rbp)
incq %r13
movslq 0x14(%r15), %rax
addq $0x18, %r12
cmpq %rax, %r13
jl 0x52a3a
movl 0x24(%r15), %eax
testl %eax, %eax
jle 0x52af1
xorl %ebx, %ebx
movq 0x18(%r15), %rax
movq (%rax,%rbx,8), %rdi
movq (%rdi), %rax
callq *0x248(%rax)
incq %rbx
movslq 0x24(%r15), %rax
cmpq %rax, %rbx
jl 0x52ad4
movq %r14, %rdi
callq 0xe5b0
movq 0x10(%rsp), %rdi
callq 0xe5b0
movq (%r15), %rax
movq %r15, %rdi
callq *0x248(%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r14, %rdi
movq %r15, %rsi
callq 0xe8c0
movq %rbx, %rdi
callq 0xe7e0