// File: /workspace/github_cpp_makefile_star2_O2/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, 0x28(%rsp)
movq %rdi, %rbp
movslq 0x14(%rdi), %r12
pushq $0x18
popq %rcx
movq %r12, %rax
mulq %rcx
movq %rax, %r15
pushq $-0x1
popq %rbx
cmovoq %rbx, %r15
movq %r15, %rdi
callq 0xe030
movq %rax, %r14
testq %r12, %r12
movq %rbp, 0x10(%rsp)
je 0x4ba0c
imulq $0x18, %r12, %r12
xorl %r13d, %r13d
leaq (%r14,%r13), %rdi
callq 0x3830a
addq $0x18, %r13
cmpq %r13, %r12
jne 0x4b9e4
movl 0x14(%rbp), %ebp
movslq %ebp, %rax
leaq (,%rax,4), %rdi
testl %eax, %eax
cmovsq %rbx, %rdi
jmp 0x4ba10
xorl %ebp, %ebp
xorl %edi, %edi
callq 0xe030
movq %rax, 0x8(%rsp)
xorl %ebx, %ebx
leaq 0x4dc6d(%rip), %r12     # 0x99690
movq %r14, %r13
movq 0x10(%rsp), %r15
movslq %ebp, %rax
cmpq %rax, %rbx
jge 0x4ba62
movq 0x8(%r15), %rax
movq (%rax,%rbx,8), %rbp
movq %r13, %rdi
movq %r12, %rsi
callq 0x3887c
movq 0x8(%rsp), %rax
movl $0x1, (%rax,%rbx,4)
movl %ebx, 0x60(%rbp)
incq %rbx
movl 0x14(%r15), %ebp
addq $0x18, %r13
jmp 0x4ba2b
xorl %ecx, %ecx
leaq 0x30(%rsp), %r12
movq 0x10(%rsp), %r15
movslq 0x24(%r15), %rax
cmpq %rax, %rcx
jge 0x4bad9
movq 0x18(%r15), %rax
movq %rcx, 0x20(%rsp)
movq (%rax,%rcx,8), %r13
movq (%r13), %rax
movq %r13, %rdi
callq *0x58(%rax)
movsd %xmm0, 0x18(%rsp)
leaq 0x20(%r13), %rbp
pushq $0x1
popq %rbx
cmpq $0x4, %rbx
je 0x4bacf
movq (%r13,%rbx,8), %rax
movslq 0x60(%rax), %r15
movq %r12, %rdi
movq %rbp, %rsi
movsd 0x18(%rsp), %xmm0
callq 0x38a02
imulq $0x18, %r15, %rdi
addq %r14, %rdi
movq %r12, %rsi
callq 0x38a24
incq %rbx
jmp 0x4ba9b
movq 0x20(%rsp), %rcx
incq %rcx
jmp 0x4ba69
xorl %ebx, %ebx
movq %r14, %r12
movslq 0x14(%r15), %rax
cmpq %rax, %rbx
jge 0x4baf8
movq %r12, %rdi
callq 0x3860e
incq %rbx
addq $0x18, %r12
jmp 0x4bade
leaq 0x4f449(%rip), %rax     # 0x9af48
movsd (%rax), %xmm0
movsd 0x28(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x4bb1a
movapd %xmm1, %xmm0
callq 0xe890
jmp 0x4bb22
movsd 0x474fe(%rip), %xmm0    # 0x93020
movsd %xmm0, 0x18(%rsp)
xorl %ecx, %ecx
movslq 0x24(%r15), %rax
cmpq %rax, %rcx
jge 0x4bb8c
movq 0x18(%r15), %rax
movq %rcx, 0x20(%rsp)
movq (%rax,%rcx,8), %rbp
leaq 0x20(%rbp), %r12
pushq $0x1
popq %r13
movq 0x8(%rsp), %r15
cmpq $0x4, %r13
je 0x4bb7d
movq (%rbp,%r13,8), %rax
movslq 0x60(%rax), %rbx
imulq $0x18, %rbx, %rsi
addq %r14, %rsi
movq %r12, %rdi
callq 0x38586
ucomisd 0x18(%rsp), %xmm0
ja 0x4bb78
andl $0x0, (%r15,%rbx,4)
incq %r13
jmp 0x4bb4d
movq 0x20(%rsp), %rcx
incq %rcx
movq 0x10(%rsp), %r15
jmp 0x4bb2a
xorl %ebx, %ebx
movq %r14, %r12
movslq 0x14(%r15), %rax
cmpq %rax, %rbx
jge 0x4bbd4
movq 0x8(%r15), %rax
movq (%rax,%rbx,8), %r13
testb $0x1, 0x59(%r13)
jne 0x4bbcb
movq %r12, %rdi
callq 0x21450
testl %eax, %eax
jne 0x4bbcb
movq 0x8(%rsp), %rax
cmpl $0x0, (%rax,%rbx,4)
je 0x4bbcb
movq %r13, %rdi
movq %r12, %rsi
callq 0x4c3ac
incq %rbx
addq $0x18, %r12
jmp 0x4bb91
xorl %ebx, %ebx
movslq 0x24(%r15), %rax
cmpq %rax, %rbx
jge 0x4bbf5
movq 0x18(%r15), %rax
movq (%rax,%rbx,8), %rdi
movq (%rdi), %rax
callq *0x248(%rax)
incq %rbx
jmp 0x4bbd6
movq %r14, %rdi
callq 0xe5d0
movq 0x8(%rsp), %rdi
callq 0xe5d0
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 0xe8f0
movq %rbx, %rdi
callq 0xe810