// File: /workspace/github_cpp_makefile_star2_O1/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, %rbp
movslq 0x14(%rdi), %r12
movl $0x18, %ecx
movq %r12, %rax
mulq %rcx
movq %rax, %r15
movq $-0x1, %rbx
cmovoq %rbx, %r15
movq %r15, %rdi
callq 0xe030
movq %rax, %r14
testq %r12, %r12
je 0x534f7
shlq $0x3, %r12
leaq (%r12,%r12,2), %r12
xorl %r13d, %r13d
leaq (%r14,%r13), %rdi
callq 0x3d4bc
addq $0x18, %r13
cmpq %r13, %r12
jne 0x534e5
movslq 0x14(%rbp), %r15
leaq (,%r15,4), %rdi
testq %r15, %r15
cmovsq %rbx, %rdi
callq 0xe030
movq %rax, %r12
testq %r15, %r15
jle 0x5354f
xorl %ebx, %ebx
movq %r14, %r13
movq 0x8(%rbp), %rax
movq (%rax,%rbx,8), %r15
movq %r13, %rdi
leaq 0x551b2(%rip), %rsi     # 0xa86e0
callq 0x3dc6a
movl $0x1, (%r12,%rbx,4)
movl %ebx, 0x60(%r15)
incq %rbx
movslq 0x14(%rbp), %rax
addq $0x18, %r13
cmpq %rax, %rbx
jl 0x5351c
movq %r12, 0x18(%rsp)
cmpl $0x0, 0x24(%rbp)
movq %rbp, 0x10(%rsp)
jle 0x535d6
xorl %ecx, %ecx
leaq 0x30(%rsp), %r12
movq 0x18(%rbp), %rax
movq %rcx, 0x28(%rsp)
movq (%rax,%rcx,8), %r13
movq (%r13), %rax
movq %r13, %rdi
callq *0x58(%rax)
movsd %xmm0, 0x8(%rsp)
leaq 0x20(%r13), %rbp
movl $0x1, %r15d
movq (%r13,%r15,8), %rax
movslq 0x60(%rax), %rbx
movq %r12, %rdi
movq %rbp, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x3de8e
leaq (%rbx,%rbx,2), %rax
leaq (%r14,%rax,8), %rdi
movq %r12, %rsi
callq 0x3deb0
incq %r15
cmpq $0x4, %r15
jne 0x5358d
movq 0x28(%rsp), %rcx
incq %rcx
movq 0x10(%rsp), %rbp
movslq 0x24(%rbp), %rax
cmpq %rax, %rcx
jl 0x53566
cmpl $0x0, 0x14(%rbp)
jle 0x535f9
xorl %ebx, %ebx
movq %r14, %r12
movq %r12, %rdi
callq 0x3d7e0
incq %rbx
movslq 0x14(%rbp), %rax
addq $0x18, %r12
cmpq %rax, %rbx
jl 0x535e1
leaq 0x569a8(%rip), %rax     # 0xa9fa8
movsd (%rax), %xmm0
movsd 0x20(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x5361b
movapd %xmm1, %xmm0
callq 0xe860
jmp 0x53623
movsd 0x4ea25(%rip), %xmm0    # 0xa2048
movsd %xmm0, 0x8(%rsp)
cmpl $0x0, 0x24(%rbp)
jle 0x5368a
xorl %ebx, %ebx
movq 0x18(%rbp), %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 0x3d756
ucomisd 0x8(%rsp), %xmm0
ja 0x53670
movq 0x18(%rsp), %rax
movl $0x0, (%rax,%r15,4)
incq %rbp
cmpq $0x4, %rbp
jne 0x53642
incq %rbx
movq 0x10(%rsp), %rbp
movslq 0x24(%rbp), %rax
cmpq %rax, %rbx
jl 0x53631
cmpl $0x0, 0x14(%rbp)
movq 0x18(%rsp), %r15
jle 0x53738
xorl %r13d, %r13d
xorpd %xmm1, %xmm1
movq %r14, %r12
movq 0x8(%rbp), %rax
movq (%rax,%r13,8), %rbp
testb $0x1, 0x59(%rbp)
jne 0x5371f
movsd (%r12), %xmm0
ucomisd %xmm1, %xmm0
jne 0x536dd
jp 0x536dd
movsd 0x8(%r12), %xmm0
ucomisd %xmm1, %xmm0
jne 0x536dd
jp 0x536dd
movsd 0x10(%r12), %xmm0
ucomisd %xmm1, %xmm0
jne 0x536dd
jnp 0x5371f
cmpl $0x0, (%r15,%r13,4)
je 0x5371f
leaq 0x18(%rbp), %rdi
movq %r12, %rsi
callq 0x3dc6a
xorpd %xmm1, %xmm1
movsd 0x18(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jne 0x5371b
jp 0x5371b
movsd 0x20(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jne 0x5371b
jp 0x5371b
movsd 0x28(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jne 0x5371b
jnp 0x5371f
orb $0x1, 0x59(%rbp)
incq %r13
movq 0x10(%rsp), %rbp
movslq 0x14(%rbp), %rax
addq $0x18, %r12
cmpq %rax, %r13
jl 0x536a3
cmpl $0x0, 0x24(%rbp)
jle 0x5375d
xorl %ebx, %ebx
movq 0x18(%rbp), %rax
movq (%rax,%rbx,8), %rdi
movq (%rdi), %rax
callq *0x248(%rax)
incq %rbx
movslq 0x24(%rbp), %rax
cmpq %rax, %rbx
jl 0x53740
movq %r14, %rdi
callq 0xe5b0
movq %r15, %rdi
callq 0xe5b0
movq (%rbp), %rax
movq %rbp, %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
nop