// File: /workspace/github_cpp_makefile_star2_O2/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 $0xd8, %rsp
cmpl $0x5, 0x34(%rdi)
jge 0x14828
xorl %ebx, %ebx
movq %rbx, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %rbp
movq %rdi, %r13
movq %rdx, 0x38(%rsp)
movq 0x8(%rsi), %rbx
movq 0x10(%rsi), %r12
movq 0x18(%rsi), %r14
movq %r14, 0x60(%rsp)
movq 0x20(%rsi), %rcx
movq %rcx, 0x68(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movapd %xmm0, 0x80(%rsp)
movapd %xmm0, 0x70(%rsp)
testq %r14, %r14
movq %rcx, 0x10(%rsp)
je 0x148d2
movq %rbx, %rsi
movq %rbp, %rdx
movq %r14, %rcx
callq 0x12dd6
movq %rax, (%rsp)
movq %rax, 0x40(%rsp)
movq %r12, %rsi
movq %rbp, %rdx
movq %r14, %rcx
callq 0x12dd6
movq %rax, %r15
movq %rax, 0x48(%rsp)
movq 0x8(%rax), %rax
cmpq %r12, %rax
jne 0x148b0
movq 0x10(%r15), %rax
movq %rax, 0x8(%rsp)
movq 0x18(%r15), %rax
cmpq %r14, %rax
jne 0x148c2
movq 0x20(%r15), %rax
movq %rax, 0x78(%rsp)
movq 0x10(%rsp), %rcx
movq (%rsp), %r11
jmp 0x148e1
xorl %r15d, %r15d
xorl %r11d, %r11d
movq $0x0, 0x8(%rsp)
movq %r13, 0x28(%rsp)
testq %rcx, %rcx
movq %rbp, 0x18(%rsp)
movq %r14, 0x30(%rsp)
je 0x14952
movq %r11, (%rsp)
movq %rbx, %rsi
movq %rbp, %rdx
movq %rcx, %r14
callq 0x12dd6
movq %rax, %r13
movq %rax, 0x50(%rsp)
movq %r12, %rsi
movq %rbp, %rdx
movq %r14, %rcx
callq 0x12dd6
movq %rax, %rbp
movq %rax, 0x58(%rsp)
movq 0x8(%rax), %rax
cmpq %r12, %rax
jne 0x14932
movq 0x10(%rbp), %rax
movq %rax, 0x20(%rsp)
movq 0x18(%rbp), %rax
cmpq %r14, %rax
jne 0x14944
movq 0x20(%rbp), %rax
movq %rax, 0x88(%rsp)
movq (%rsp), %r11
jmp 0x14960
xorl %ebp, %ebp
xorl %r13d, %r13d
movq $0x0, 0x20(%rsp)
movl 0x14(%rbx), %eax
leaq 0x8(%rbx), %r14
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
xorl %ecx, %ecx
cmpq %rax, %rcx
je 0x149f0
movq (%r14), %rdx
movq (%rdx,%rcx,8), %rsi
cmpq %r11, %rsi
sete %dl
cmpq %r13, %rsi
sete %dil
orb %dl, %dil
je 0x14996
incq %rcx
jmp 0x14973
movq 0x8(%rsi), %rdx
cmpq %rbx, %rdx
jne 0x149a3
movq 0x10(%rsi), %rdx
movl 0x14(%r12), %esi
testl %esi, %esi
movl $0x0, %edi
cmovlel %edi, %esi
xorl %edi, %edi
cmpq %rdi, %rsi
je 0x14991
movq 0x8(%r12), %r8
movq (%r8,%rdi,8), %r8
cmpq %r15, %r8
sete %r9b
cmpq %rbp, %r8
sete %r10b
orb %r9b, %r10b
jne 0x149eb
movq 0x8(%r8), %r9
cmpq %r12, %r9
jne 0x149e2
movq 0x10(%r8), %r9
cmpq %r9, %rdx
je 0x14811
incq %rdi
jmp 0x149b4
movq %r14, %rdi
movq 0x18(%rsp), %rsi
callq 0x22c04
xorl %r13d, %r13d
movslq 0x14(%r12), %rax
cmpq %rax, %r13
jge 0x14a3a
movq 0x8(%r12), %rax
movq (%rax,%r13,8), %rsi
cmpq 0x18(%rsp), %rsi
sete %al
cmpq %r15, %rsi
sete %cl
orb %al, %cl
cmpq %rbp, %rsi
sete %al
orb %cl, %al
jne 0x14a35
movq %r14, %rdi
callq 0x22bfc
incq %r13
jmp 0x14a00
cmpq $0x0, 0x30(%rsp)
je 0x14a53
movq 0x8(%rsp), %rdi
addq $0x8, %rdi
movq %r15, %rsi
callq 0x22c04
cmpq $0x0, 0x10(%rsp)
movq 0x38(%rsp), %r14
movq 0x28(%rsp), %r13
je 0x14a76
movq 0x20(%rsp), %rdi
addq $0x8, %rdi
movq %rbp, %rsi
callq 0x22c04
movl 0x14(%r12), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
cmpq %rax, %rcx
je 0x14aae
movq 0x8(%r12), %rdx
movq (%rdx,%rcx,8), %rdx
xorl %esi, %esi
cmpq $0x2, %rsi
je 0x14aa9
cmpq %r12, 0x8(%rdx,%rsi,8)
jne 0x14aa4
movq %rbx, 0x8(%rdx,%rsi,8)
incq %rsi
jmp 0x14a92
incq %rcx
jmp 0x14a82
xorl %ecx, %ecx
cmpq $0x2, %rcx
je 0x14af5
movq 0x60(%rsp,%rcx,8), %rdx
testq %rdx, %rdx
je 0x14af0
movq %rcx, %rdi
shlq $0x4, %rdi
movq 0x40(%rsp,%rdi), %rsi
addq %rsp, %rdi
addq $0x78, %rdi
xorl %r8d, %r8d
cmpq $0x2, %r8
je 0x14af0
cmpq %rdx, 0x18(%rsi,%r8,8)
jne 0x14aeb
movq (%rdi), %r9
movq %r9, 0x18(%rsi,%r8,8)
incq %r8
jmp 0x14ad6
incq %rcx
jmp 0x14ab0
xorl %ecx, %ecx
pushq $0x1
popq %rdx
cmpq %rax, %rcx
je 0x14b3c
movq 0x8(%r12), %rsi
movq (%rsi,%rcx,8), %rsi
xorl %edi, %edi
cmpq $0x2, %rdi
je 0x14b37
movq 0x18(%rsi,%rdi,8), %r8
testq %r8, %r8
je 0x14b32
movq %rdx, %r9
cmpq $0x4, %r9
je 0x14b32
cmpq %r12, (%r8,%r9,8)
jne 0x14b2d
movq %rbx, (%r8,%r9,8)
incq %r9
jmp 0x14b1d
incq %rdi
jmp 0x14b0a
incq %rcx
jmp 0x14afa
xorl %eax, %eax
cmpq $0x2, %rax
je 0x14b89
cmpq $0x0, 0x60(%rsp,%rax,8)
je 0x14b84
movq %rax, %rdx
shlq $0x4, %rdx
movq 0x78(%rsp,%rdx), %rcx
testq %rcx, %rcx
je 0x14b84
addq %rsp, %rdx
addq $0x40, %rdx
movq 0x8(%rdx), %rsi
xorl %edi, %edi
cmpq $0x3, %rdi
je 0x14b84
cmpq %rsi, 0x20(%rcx,%rdi,8)
jne 0x14b7f
movq (%rdx), %r8
movq %r8, 0x20(%rcx,%rdi,8)
incq %rdi
jmp 0x14b6a
incq %rax
jmp 0x14b3e
leaq 0x18(%rbx), %rsi
movq %r14, %rdi
callq 0x4321a
movsd %xmm0, (%rsp)
leaq 0x18(%r12), %rsi
movq %r14, %rdi
callq 0x4321a
movsd (%rsp), %xmm2
movapd %xmm2, %xmm1
addsd %xmm0, %xmm2
leaq 0xc5fed(%rip), %rax     # 0xdaba8
ucomisd (%rax), %xmm2
jbe 0x14c8f
divsd %xmm2, %xmm1
movsd %xmm1, (%rsp)
divsd %xmm2, %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x48(%rbx), %r14
leaq 0xa8(%rsp), %rdi
movq %r14, %rsi
callq 0x5f833
leaq 0x48(%r12), %rsi
leaq 0x90(%rsp), %rdi
movsd (%rsp), %xmm0
callq 0x5f833
leaq 0xc0(%rsp), %r13
movq %r13, %rdi
leaq 0xa8(%rsp), %rsi
leaq 0x90(%rsp), %rdx
callq 0x5f7a1
movq %r14, %rdi
movq %r13, %rsi
callq 0x5f6c4
leaq 0x58(%rbx), %r14
leaq 0xa8(%rsp), %rdi
movq %r14, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x223f6
leaq 0x58(%r12), %rsi
leaq 0x90(%rsp), %rdi
movsd (%rsp), %xmm0
callq 0x223f6
leaq 0xc0(%rsp), %r13
movq %r13, %rdi
leaq 0xa8(%rsp), %rsi
leaq 0x90(%rsp), %rdx
callq 0x213f3
movq %r14, %rdi
movq %r13, %rsi
movq 0x28(%rsp), %r13
callq 0x66212
movq 0x30(%rsp), %r14
testq %r14, %r14
je 0x14ca9
movq (%r13), %rax
movq %r13, %rdi
movq %r14, %rsi
callq *0x2a8(%rax)
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x14cc0
movq (%r13), %rax
movq %r13, %rdi
callq *0x2a8(%rax)
testq %r14, %r14
je 0x14cd5
movq (%r13), %rax
movq %r13, %rdi
movq %r15, %rsi
callq *0x2a0(%rax)
cmpq $0x0, 0x10(%rsp)
movq 0x38(%rsp), %r14
je 0x14cf2
movq (%r13), %rax
movq %r13, %rdi
movq %rbp, %rsi
callq *0x2a0(%rax)
movq (%r13), %rax
movq %r13, %rdi
movq 0x18(%rsp), %rsi
callq *0x2a0(%rax)
movq (%r13), %rax
movq %r13, %rdi
movq %r12, %rsi
callq *0x298(%rax)
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x12ccc
jmp 0x14813
nop