// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::CollapseEdge(gaps::R3MeshEdge*, gaps::R3Point const&)
R3MeshVertex *R3Mesh::
CollapseEdge(R3MeshEdge *edge, const R3Point& point)
{
  // Just to be safe
  if (NFaces() <= 4) return NULL;

  // Get vertices on edge
  R3MeshVertex *v[2];
  v[0] = edge->vertex[0];
  v[1] = edge->vertex[1];

  // Get faces on edge
  R3MeshFace *f[2];
  f[0] = edge->face[0];
  f[1] = edge->face[1];

#if 1
  // Get other edges and vertex on faces
  R3MeshEdge *ef[2][2] = { { NULL, NULL }, { NULL, NULL } };
  R3MeshFace *ff[2][2] = { { NULL, NULL }, { NULL, NULL } };
  R3MeshVertex *vf[2] = { NULL, NULL };
  if (f[0]) {
    ef[0][0] = EdgeAcrossVertex(v[0], edge, f[0]);
    ef[0][1] = EdgeAcrossVertex(v[1], edge, f[0]);
    vf[0] = VertexAcrossEdge(ef[0][1], v[1]);
    assert(vf[0] == VertexAcrossEdge(ef[0][0], v[0]));
    ff[0][0] = FaceAcrossEdge(ef[0][0], f[0]);
    ff[0][1] = FaceAcrossEdge(ef[0][1], f[0]);
  }
  if (f[1]) {
    ef[1][0] = EdgeAcrossVertex(v[0], edge, f[1]);
    ef[1][1] = EdgeAcrossVertex(v[1], edge, f[1]);
    vf[1] = VertexAcrossEdge(ef[1][1], v[1]);
    assert(vf[1] == VertexAcrossEdge(ef[1][0], v[0]));
    ff[1][0] = FaceAcrossEdge(ef[1][0], f[1]);
    ff[1][1] = FaceAcrossEdge(ef[1][1], f[1]);
  }

  // Check if will create a fin
  for (int i = 0; i < VertexValence(v[0]); i++) {
    R3MeshEdge *e0 = EdgeOnVertex(v[0], i);
    if ((e0 == ef[0][0]) || (e0 == ef[1][0])) continue;
    R3MeshVertex *ve0 = VertexAcrossEdge(e0, v[0]);
    for (int j = 0; j < VertexValence(v[1]); j++) {
      R3MeshEdge *e1 = EdgeOnVertex(v[1], j);
      if ((e1 == ef[0][1]) || (e1 == ef[1][1])) continue;
      R3MeshVertex *ve1 = VertexAcrossEdge(e1, v[1]);
      if (ve0 == ve1) return NULL;
    }
  }

  // Update vertex-edge relations
  v[0]->edges.Remove(edge);
  for (int i = 0; i < v[1]->edges.NEntries(); i++) {
    R3MeshEdge *ev1 = v[1]->edges[i];
    if (ev1 == edge) continue;
    if (ev1 == ef[0][1]) continue;
    if (ev1 == ef[1][1]) continue;
    v[0]->edges.Insert(ev1);
  }
  if (f[0]) {
    assert(vf[0] && ef[0][1]);
    vf[0]->edges.Remove(ef[0][1]);
  }
  if (f[1]) {
    assert(vf[1] && ef[1][1]);
    vf[1]->edges.Remove(ef[1][1]);
  }

  // Update edge-vertex relations (v[1]->v[0]) 
  for (int i = 0; i < v[1]->edges.NEntries(); i++) {
    R3MeshEdge *ev = v[1]->edges[i];
    for (int j = 0; j < 2; j++) {
      if (ev->vertex[j] == v[1]) ev->vertex[j] = v[0];
    }
  }
  
  // Update edge-face relations (ef[i][1]->ef[i][0])
  for (int i = 0; i < 2; i++) {
    if (!f[i]) continue;
    assert(ef[i][0]);
    for (int j = 0; j < 2; j++) {
      if (ef[i][0]->face[j] == f[i]) ef[i][0]->face[j] = ff[i][1];
    }
  }

  // Update face-vertex relations (v[1]->v[0])
  for (int i = 0; i < v[1]->edges.NEntries(); i++) {
    R3MeshEdge *ev = v[1]->edges[i];
    for (int j = 0; j < 2; j++) {
      R3MeshFace *fv = ev->face[j];
      if (fv) {
        for (int k = 0; k < 3; k++) {
          if (fv->vertex[k] == v[1]) fv->vertex[k] = v[0];
        }
      }
    }
  }
  
  // Update face-edge relations (ef[i][1]->ef[i][0])
  for (int i = 0; i < 2; i++) {
    if (!f[i]) continue;
    if (!ff[i][1]) continue;
    for (int j = 0; j < 3; j++) {
      if (ff[i][1]->edge[j] == ef[i][1]) {
        ff[i][1]->edge[j] = ef[i][0];
      }
    }
  }

  // Update vertex properties
  RNScalar d0 = R3Distance(point, VertexPosition(v[0]));
  RNScalar d1 = R3Distance(point, VertexPosition(v[1]));
  RNScalar dsum = d0 + d1;
  if (RNIsPositive(dsum)) {
    d0 /= dsum;  d1 /= dsum;
    SetVertexTextureCoords(v[0], d1*VertexTextureCoords(v[0]) + d0*VertexTextureCoords(v[1])); 
    SetVertexColor(v[0], d1*VertexColor(v[0]) + d0*VertexColor(v[1])); 
  }

  // Deallocate faces 
  if (f[0]) DeallocateFace(f[0]);
  if (f[1]) DeallocateFace(f[1]);

  // Deallocate edges
  if (f[0]) DeallocateEdge(ef[0][1]);
  if (f[1]) DeallocateEdge(ef[1][1]);
  DeallocateEdge(edge);

  // Deallocate vertex
  DeallocateVertex(v[1]);

  // Set position of remaining vertex
  SetVertexPosition(v[0], point);

  // Return remaining vertex
  return v[0];
#else
  // Set vertex position 
  SetVertexPosition(v[0], point);

  // Make arrays of everything attached to v[1]
  R3mesh_mark++;
  RNArray<R3MeshFace *> faces_to_remove;
  RNArray<R3MeshEdge *> edges_to_remove;
  RNArray<R3MeshVertex *> cw_vertices;
  RNArray<R3MeshVertex *> ccw_vertices;
  for (int i = 0; i < v[1]->edges.NEntries(); i++) {
    R3MeshEdge *v1_e = v[1]->edges[i];
    edges_to_remove.Insert(v1_e);
    R3MeshFace *v1_f = FaceOnEdge(v1_e, v[1], RN_CCW);
    if (v1_f) {
      faces_to_remove.Insert(v1_f);
      if ((v1_f != f[0]) && (v1_f != f[1])) {
        cw_vertices.Insert(VertexOnFace(v1_f, v[1], RN_CW));
        ccw_vertices.Insert(VertexOnFace(v1_f, v[1], RN_CCW));
      }
    }
  }

  // Remove everything attached to v1
  for (int i = 0; i < faces_to_remove.NEntries(); i++) 
    DeleteFace(faces_to_remove[i]);
  for (int i = 0; i < edges_to_remove.NEntries(); i++) 
    DeleteEdge(edges_to_remove[i]);
  DeleteVertex(v[1]);

  // Recreate everything by replacing v1 with v0
  for (int i = 0; i < cw_vertices.NEntries(); i++) {
    CreateFace(v[0], cw_vertices[i], ccw_vertices[i]);
  }

  // Return remaining vertex
  return v[0];
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
cmpl $0x5, 0x34(%rdi)
jl 0x15e17
movq %rsi, %r13
movq %rdi, 0x48(%rsp)
movq %rdx, 0x68(%rsp)
movq 0x8(%rsi), %rbx
movq 0x10(%rsi), %r12
movq 0x18(%rsi), %rbp
movq %rbp, 0x50(%rsp)
movq 0x20(%rsi), %r14
movq %r14, 0x58(%rsp)
pxor %xmm0, %xmm0
movdqa %xmm0, 0x20(%rsp)
movdqa %xmm0, 0x10(%rsp)
movdqa %xmm0, 0x80(%rsp)
movdqa %xmm0, 0x70(%rsp)
testq %rbp, %rbp
movq %rsi, 0x8(%rsp)
je 0x158d0
movq %rbx, %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x13922
movq %rax, %r15
movq %rax, 0x10(%rsp)
movq %r12, %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x13922
movq %rax, 0x18(%rsp)
movq 0x8(%rax), %rdx
cmpq %r12, %rdx
jne 0x158a5
movq 0x10(%rax), %rdx
movq 0x18(%r15), %rcx
cmpq %rbp, %rcx
jne 0x158b2
movq 0x20(%r15), %rcx
movq %rcx, 0x70(%rsp)
movq 0x18(%rax), %rcx
cmpq %rbp, %rcx
jne 0x158c4
movq 0x20(%rax), %rcx
movq %rcx, 0x78(%rsp)
movq 0x8(%rsp), %r13
jmp 0x158d2
xorl %edx, %edx
movq %rdx, 0x98(%rsp)
testq %r14, %r14
movq %rbp, 0x40(%rsp)
je 0x15955
movq %rbx, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x13922
movq %rax, %r15
movq %rax, 0x20(%rsp)
movq %r12, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x13922
movq %rax, 0x28(%rsp)
movq 0x8(%rax), %rdx
movq %rdx, 0x38(%rsp)
cmpq %r12, %rdx
jne 0x15924
movq 0x10(%rax), %rcx
movq %rcx, 0x38(%rsp)
movq 0x18(%r15), %rcx
cmpq %r14, %rcx
jne 0x15931
movq 0x20(%r15), %rcx
movq %rcx, 0x80(%rsp)
movq 0x18(%rax), %rcx
cmpq %r14, %rcx
jne 0x15946
movq 0x20(%rax), %rcx
movq %rcx, 0x88(%rsp)
movq 0x8(%rsp), %r13
jmp 0x1595e
movq $0x0, 0x38(%rsp)
movslq 0x14(%rbx), %rax
testq %rax, %rax
jle 0x15a37
movq 0x8(%rbx), %rcx
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdi
movq %rdi, (%rsp)
movq 0x28(%rsp), %r8
xorl %r10d, %r10d
movq (%rcx,%r10,8), %r9
movl $0x4, %r14d
cmpq %rdx, %r9
je 0x15a10
cmpq (%rsp), %r9
je 0x15a10
movq 0x8(%r9), %r11
cmpq %rbx, %r11
jne 0x159ac
movq 0x10(%r9), %r11
movslq 0x14(%r12), %r15
testq %r15, %r15
jle 0x159fd
movq 0x8(%r12), %rbp
xorl %r9d, %r9d
movq (%rbp,%r9,8), %r13
movl $0x7, %r14d
cmpq %rsi, %r13
je 0x159ea
cmpq %r8, %r13
je 0x159ea
movq 0x8(%r13), %rdi
cmpq %r12, %rdi
jne 0x159e0
movq 0x10(%r13), %rdi
xorl %r14d, %r14d
cmpq %rdi, %r11
sete %r14b
cmpl $0x7, %r14d
je 0x159f5
testl %r14d, %r14d
jne 0x15a03
incq %r9
cmpq %r9, %r15
jne 0x159be
movl $0x5, %r14d
cmpl $0x5, %r14d
movl $0x0, %edi
cmovel %edi, %r14d
testb $0x3, %r14b
jne 0x15a28
incq %r10
cmpq %rax, %r10
jne 0x1598a
movl $0x2, %r14d
cmpl $0x2, %r14d
movq 0x8(%rsp), %r13
jne 0x15e17
leaq 0x8(%rbx), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x70664
subq 0x8(%rbx), %rax
shrq $0x3, %rax
movq %r14, %rdi
movl %eax, %esi
callq 0x707f6
movq 0x8(%rsp), %rcx
cmpl $0x0, 0x14(%r12)
jle 0x15aa7
movq 0x18(%rsp), %r15
movq 0x28(%rsp), %rbp
xorl %r13d, %r13d
movq 0x8(%r12), %rax
movq (%rax,%r13,8), %rsi
cmpq %rcx, %rsi
je 0x15a9a
cmpq %r15, %rsi
je 0x15a9a
cmpq %rbp, %rsi
je 0x15a9a
movl 0x14(%rbx), %edx
movq %r14, %rdi
callq 0x70682
movq 0x8(%rsp), %rcx
incq %r13
movslq 0x14(%r12), %rax
cmpq %rax, %r13
jl 0x15a72
cmpq $0x0, 0x40(%rsp)
je 0x15ada
movq 0x98(%rsp), %r15
leaq 0x8(%r15), %r14
movq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0x70664
subq 0x8(%r15), %rax
shrq $0x3, %rax
movq %r14, %rdi
movl %eax, %esi
callq 0x707f6
cmpq $0x0, 0x58(%rsp)
je 0x15b0a
movq 0x38(%rsp), %r15
leaq 0x8(%r15), %r14
movq 0x28(%rsp), %rsi
movq %r14, %rdi
callq 0x70664
subq 0x8(%r15), %rax
shrq $0x3, %rax
movq %r14, %rdi
movl %eax, %esi
callq 0x707f6
movslq 0x14(%r12), %rax
testq %rax, %rax
jle 0x15b60
movq %r12, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
xorl %ecx, %ecx
movq 0x8(%r12), %rdx
movq (%rdx,%rcx,8), %rdx
movdqu 0x8(%rdx), %xmm2
pcmpeqd %xmm0, %xmm2
pshufd $0xb1, %xmm2, %xmm1     # xmm1 = xmm2[1,0,3,2]
pand %xmm2, %xmm1
movd %xmm1, %esi
testb $0x1, %sil
je 0x15b49
movq %rbx, 0x8(%rdx)
pextrw $0x4, %xmm1, %esi
testb $0x1, %sil
je 0x15b58
movq %rbx, 0x10(%rdx)
incq %rcx
cmpq %rcx, %rax
jne 0x15b20
xorl %ecx, %ecx
movq 0x50(%rsp,%rcx,8), %rdx
testq %rdx, %rdx
je 0x15b9a
movq %rcx, %rdi
shlq $0x4, %rdi
movq 0x10(%rsp,%rdi), %rsi
addq %rsp, %rdi
addq $0x78, %rdi
xorl %r8d, %r8d
cmpq %rdx, 0x18(%rsi,%r8,8)
jne 0x15b91
movq (%rdi), %r9
movq %r9, 0x18(%rsi,%r8,8)
incq %r8
cmpq $0x1, %r8
je 0x15b82
leaq 0x1(%rcx), %rdx
testq %rcx, %rcx
movq %rdx, %rcx
je 0x15b62
testl %eax, %eax
jle 0x15bee
xorl %ecx, %ecx
movq 0x8(%r12), %rdx
movq (%rdx,%rcx,8), %rdx
xorl %esi, %esi
movq 0x18(%rdx,%rsi,8), %rdi
testq %rdi, %rdi
je 0x15bda
movl $0x1, %r8d
cmpq %r12, (%rdi,%r8,8)
jne 0x15bd1
movq %rbx, (%rdi,%r8,8)
incq %r8
cmpq $0x4, %r8
jne 0x15bc7
leaq 0x1(%rsi), %rdi
testq %rsi, %rsi
movq %rdi, %rsi
je 0x15bb7
incq %rcx
cmpq %rax, %rcx
jne 0x15bac
xorl %eax, %eax
cmpq $0x0, 0x50(%rsp,%rax,8)
je 0x15c2e
movq %rax, %rdx
shlq $0x4, %rdx
movq 0x78(%rsp,%rdx), %rcx
testq %rcx, %rcx
je 0x15c2e
addq %rsp, %rdx
addq $0x10, %rdx
movq 0x8(%rdx), %rsi
xorl %edi, %edi
cmpq %rsi, 0x20(%rcx,%rdi,8)
jne 0x15c25
movq (%rdx), %r8
movq %r8, 0x20(%rcx,%rdi,8)
incq %rdi
cmpq $0x3, %rdi
jne 0x15c16
leaq 0x1(%rax), %rcx
testq %rax, %rax
movq %rcx, %rax
je 0x15bf0
leaq 0x18(%rbx), %rsi
movq 0x68(%rsp), %r14
movq %r14, %rdi
callq 0x49efc
movq %xmm0, (%rsp)
leaq 0x18(%r12), %rsi
movq %r14, %rdi
callq 0x49efc
movsd (%rsp), %xmm2
movapd %xmm2, %xmm1
addsd %xmm0, %xmm2
leaq 0xcff27(%rip), %rax     # 0xe5b98
ucomisd (%rax), %xmm2
jbe 0x15d7e
divsd %xmm2, %xmm1
movsd %xmm1, (%rsp)
divsd %xmm2, %xmm0
movsd %xmm0, 0x40(%rsp)
leaq 0x48(%rbx), %r14
leaq 0xb8(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x690d5
leaq 0x48(%r12), %rsi
leaq 0xa0(%rsp), %rbp
movq %rbp, %rdi
movsd (%rsp), %xmm0
callq 0x690d5
leaq 0xd0(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x69043
movq %r14, %rdi
movq %r15, %rsi
callq 0x68f66
leaq 0x58(%rbx), %r14
movsd 0x58(%rbx), %xmm0
movsd 0x40(%rsp), %xmm2
mulsd %xmm2, %xmm0
movsd 0x60(%rbx), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x68(%rbx), %xmm2
leaq 0xb8(%rsp), %r15
movq %r15, %rdi
callq 0x70460
movsd 0x58(%r12), %xmm0
movsd (%rsp), %xmm2
mulsd %xmm2, %xmm0
movsd 0x60(%r12), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x68(%r12), %xmm2
leaq 0xa0(%rsp), %rbp
movq %rbp, %rdi
callq 0x70460
movsd (%r15), %xmm0
movsd 0x8(%r15), %xmm1
addsd (%rbp), %xmm0
addsd 0x8(%rbp), %xmm1
movsd 0x10(%r15), %xmm2
addsd 0x10(%rbp), %xmm2
leaq 0xd0(%rsp), %r15
movq %r15, %rdi
callq 0x70460
movq %r14, %rdi
movq %r15, %rsi
callq 0x704f2
movq 0x50(%rsp), %r14
testq %r14, %r14
je 0x15d99
movq 0x48(%rsp), %rdi
movq (%rdi), %rax
movq %r14, %rsi
callq *0x2a8(%rax)
movq 0x58(%rsp), %r15
testq %r15, %r15
je 0x15db4
movq 0x48(%rsp), %rdi
movq (%rdi), %rax
movq %r15, %rsi
callq *0x2a8(%rax)
testq %r14, %r14
movq 0x48(%rsp), %r14
je 0x15dcf
movq 0x18(%rsp), %rsi
movq (%r14), %rax
movq %r14, %rdi
callq *0x2a0(%rax)
testq %r15, %r15
je 0x15de5
movq 0x28(%rsp), %rsi
movq (%r14), %rax
movq %r14, %rdi
callq *0x2a0(%rax)
movq (%r14), %rax
movq %r14, %rdi
movq 0x8(%rsp), %rsi
callq *0x2a0(%rax)
movq (%r14), %rax
movq %r14, %rdi
movq %r12, %rsi
callq *0x298(%rax)
movq %r14, %rdi
movq %rbx, %rsi
movq 0x68(%rsp), %rdx
callq 0x13808
jmp 0x15e19
xorl %ebx, %ebx
movq %rbx, %rax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq