// File: /workspace/github_cpp_makefile_star2_O3/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 $0xb8, %rsp
cmpl $0x5, 0x34(%rdi)
jge 0x15804
xorl %ebx, %ebx
jmp 0x15db5
movq %rdx, %r13
movq %rsi, %rdx
movq %rdi, 0x20(%rsp)
movq 0x8(%rsi), %rbx
movq 0x10(%rsi), %r12
movq 0x18(%rsi), %r15
movq 0x20(%rsi), %rbp
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
xorl %r14d, %r14d
movq %r14, 0x40(%rsp)
movq %r14, 0x38(%rsp)
movl $0x0, %eax
movq %rax, 0x8(%rsp)
movl $0x0, %esi
testq %r15, %r15
movq %rdx, 0x28(%rsp)
movq %rbp, (%rsp)
je 0x158af
movq %rbx, %rsi
movq %r15, %rcx
movq %rdx, %r14
callq 0x13962
movq %rax, 0x8(%rsp)
movq %rax, 0x50(%rsp)
movq %r12, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x13962
movq %rax, %r14
movq %rax, 0x58(%rsp)
movq 0x8(%rax), %rsi
cmpq %r12, %rsi
jne 0x15894
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rax
cmpq %r15, %rax
jne 0x158a1
movq 0x20(%r14), %rax
movq %rax, 0x40(%rsp)
movq 0x28(%rsp), %rdx
movq (%rsp), %rbp
movq %rsi, 0x30(%rsp)
movq %r15, 0x18(%rsp)
testq %rbp, %rbp
movq %r13, 0x48(%rsp)
je 0x1591a
movq %rbx, %rsi
movq %rbp, %rcx
movq %rdx, %r15
callq 0x13962
movq %rax, %r13
movq %rax, 0x60(%rsp)
movq %r12, %rsi
movq %r15, %rdx
movq %rbp, %rcx
callq 0x13962
movq %rax, %r15
movq %rax, 0x68(%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x10(%rsp)
cmpq %r12, %rcx
jne 0x15906
movq 0x10(%r15), %rax
movq %rax, 0x10(%rsp)
movq 0x18(%r15), %rax
cmpq %rbp, %rax
jne 0x15913
movq 0x20(%r15), %rax
movq %rax, 0x38(%rsp)
jmp 0x15929
xorl %r15d, %r15d
xorl %r13d, %r13d
movq $0x0, 0x10(%rsp)
movslq 0x14(%rbx), %rax
testq %rax, %rax
jle 0x159ad
movq 0x8(%rbx), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rdi
cmpq 0x8(%rsp), %rdi
sete %sil
cmpq %r13, %rdi
sete %r8b
orb %sil, %r8b
jne 0x159a5
movq 0x8(%rdi), %rsi
cmpq %rbx, %rsi
jne 0x1595e
movq 0x10(%rdi), %rsi
movslq 0x14(%r12), %rdi
testq %rdi, %rdi
jle 0x159a5
movq 0x8(%r12), %r8
xorl %r9d, %r9d
movq (%r8,%r9,8), %r10
cmpq %r14, %r10
sete %r11b
cmpq %r15, %r10
sete %bpl
orb %r11b, %bpl
jne 0x1599d
movq 0x8(%r10), %r11
cmpq %r12, %r11
jne 0x15994
movq 0x10(%r10), %r11
cmpq %r11, %rsi
je 0x157fd
incq %r9
cmpq %r9, %rdi
jne 0x15970
incq %rdx
cmpq %rax, %rdx
jne 0x15938
leaq 0x8(%rbx), %r13
movq %r13, %rdi
movq 0x28(%rsp), %rbp
movq %rbp, %rsi
callq 0x6f884
subq 0x8(%rbx), %rax
shrq $0x3, %rax
movq %r13, %rdi
movl %eax, %esi
callq 0x6fa2c
movl 0x14(%r12), %eax
testl %eax, %eax
movq (%rsp), %r8
jle 0x15a2a
movq %rbp, %rdi
xorl %ebp, %ebp
movq 0x8(%r12), %rcx
movq (%rcx,%rbp,8), %rsi
cmpq %rdi, %rsi
sete %cl
cmpq %r14, %rsi
sete %dl
orb %cl, %dl
cmpq %r15, %rsi
sete %cl
orb %dl, %cl
jne 0x15a1f
movl 0x14(%rbx), %edx
movq %r13, %rdi
callq 0x6f8a2
movq (%rsp), %r8
movq 0x28(%rsp), %rdi
movl 0x14(%r12), %eax
incq %rbp
movslq %eax, %rcx
cmpq %rcx, %rbp
jl 0x159e5
cmpq $0x0, 0x18(%rsp)
je 0x15a5c
movq 0x30(%rsp), %rbp
leaq 0x8(%rbp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x6f884
subq 0x8(%rbp), %rax
shrq $0x3, %rax
movq %r13, %rdi
movl %eax, %esi
callq 0x6fa2c
movq (%rsp), %r8
testq %r8, %r8
je 0x15a87
movq 0x10(%rsp), %rbp
leaq 0x8(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x6f884
subq 0x8(%rbp), %rax
shrq $0x3, %rax
movq %r13, %rdi
movl %eax, %esi
callq 0x6fa2c
leaq 0x60(%rsp), %rax
movslq 0x14(%r12), %rcx
testq %rcx, %rcx
jle 0x15acc
xorl %edx, %edx
movq 0x8(%r12), %rsi
movq (%rsi,%rdx,8), %rsi
movb $0x1, %dil
xorl %r8d, %r8d
cmpq %r12, 0x8(%rsi,%r8,8)
jne 0x15ab3
movq %rbx, 0x8(%rsi,%r8,8)
movl $0x1, %r8d
testb $0x1, %dil
movl $0x0, %edi
jne 0x15aa7
incq %rdx
cmpq %rcx, %rdx
jne 0x15a98
leaq 0x50(%rsp), %r9
leaq 0x40(%rsp), %rdi
movb $0x1, %sil
leaq 0x38(%rsp), %rdx
movq 0x18(%rsp), %r8
testq %r8, %r8
je 0x15b12
movq (%r9), %r9
movb $0x1, %r10b
xorl %r11d, %r11d
cmpq %r8, 0x18(%r9,%r11,8)
jne 0x15b00
movq (%rdi), %r13
movq %r13, 0x18(%r9,%r11,8)
movl $0x1, %r11d
testb $0x1, %r10b
movl $0x0, %r10d
jne 0x15af1
movq %rdx, %rdi
movq %rax, %r9
movq (%rsp), %r11
movq %r11, %r8
testb $0x1, %sil
movl $0x0, %esi
jne 0x15ae3
testl %ecx, %ecx
jle 0x15b7b
xorl %edx, %edx
movq 0x8(%r12), %rsi
movq (%rsi,%rdx,8), %rsi
movb $0x1, %dil
xorl %r8d, %r8d
movq 0x18(%rsi,%r8,8), %r8
testq %r8, %r8
je 0x15b62
movl $0x1, %r9d
cmpq %r12, (%r8,%r9,8)
jne 0x15b59
movq %rbx, (%r8,%r9,8)
incq %r9
cmpq $0x4, %r9
jne 0x15b4f
movl $0x1, %r8d
testb $0x1, %dil
movl $0x0, %edi
jne 0x15b3f
incq %rdx
cmpq %rcx, %rdx
jne 0x15b30
leaq 0x50(%rsp), %rsi
leaq 0x40(%rsp), %rdi
movb $0x1, %dl
leaq 0x38(%rsp), %rcx
movq 0x18(%rsp), %r8
testq %r8, %r8
je 0x15bbd
movq (%rdi), %rdi
testq %rdi, %rdi
je 0x15bbd
movq 0x8(%rsi), %r8
xorl %r9d, %r9d
cmpq %r8, 0x20(%rdi,%r9,8)
jne 0x15bb4
movq (%rsi), %r10
movq %r10, 0x20(%rdi,%r9,8)
incq %r9
cmpq $0x3, %r9
jne 0x15ba5
movq %rcx, %rdi
movq %rax, %rsi
movq %r11, %r8
testb $0x1, %dl
movl $0x0, %edx
jne 0x15b91
leaq 0x18(%rbx), %rsi
movq 0x48(%rsp), %r13
movq %r13, %rdi
callq 0x49404
movsd %xmm0, 0x8(%rsp)
leaq 0x18(%r12), %rsi
movq %r13, %rdi
callq 0x49404
movsd 0x8(%rsp), %xmm2
movapd %xmm2, %xmm1
addsd %xmm0, %xmm2
leaq 0xcff8f(%rip), %rax     # 0xe5b98
ucomisd (%rax), %xmm2
jbe 0x15d1c
divsd %xmm2, %xmm1
movsd %xmm1, 0x8(%rsp)
divsd %xmm2, %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0x48(%rbx), %r13
leaq 0xa0(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x6834d
leaq 0x48(%r12), %rsi
leaq 0x70(%rsp), %rdi
movsd 0x8(%rsp), %xmm0
callq 0x6834d
leaq 0x88(%rsp), %rdi
movq %rbp, %rsi
leaq 0x70(%rsp), %rdx
callq 0x682bb
movq %r13, %rdi
leaq 0x88(%rsp), %rsi
callq 0x681de
leaq 0x58(%rbx), %rax
movq %rax, 0x10(%rsp)
movsd 0x58(%rbx), %xmm0
movsd 0x30(%rsp), %xmm2
mulsd %xmm2, %xmm0
movsd 0x60(%rbx), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x68(%rbx), %xmm2
leaq 0xa0(%rsp), %rbp
movq %rbp, %rdi
callq 0x6f680
movsd 0x58(%r12), %xmm0
movsd 0x8(%rsp), %xmm2
mulsd %xmm2, %xmm0
movsd 0x60(%r12), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x68(%r12), %xmm2
leaq 0x70(%rsp), %r13
movq %r13, %rdi
callq 0x6f680
movsd (%rbp), %xmm0
movsd 0x8(%rbp), %xmm1
addsd (%r13), %xmm0
addsd 0x8(%r13), %xmm1
movsd 0x10(%rbp), %xmm2
addsd 0x10(%r13), %xmm2
leaq 0x88(%rsp), %r13
movq %r13, %rdi
callq 0x6f680
movq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x6f712
movq 0x18(%rsp), %rsi
testq %rsi, %rsi
je 0x15d34
movq 0x20(%rsp), %rdi
movq (%rdi), %rax
callq *0x2a8(%rax)
movq (%rsp), %rsi
testq %rsi, %rsi
je 0x15d4f
movq 0x20(%rsp), %rdi
movq (%rdi), %rax
callq *0x2a8(%rax)
movq (%rsp), %rsi
cmpq $0x0, 0x18(%rsp)
je 0x15d6c
movq 0x20(%rsp), %rdi
movq (%rdi), %rax
movq %r14, %rsi
callq *0x2a0(%rax)
movq (%rsp), %rsi
testq %rsi, %rsi
movq 0x20(%rsp), %r14
je 0x15d85
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x2a0(%rax)
movq (%r14), %rax
movq %r14, %rdi
movq 0x28(%rsp), %rsi
callq *0x2a0(%rax)
movq (%r14), %rax
movq %r14, %rdi
movq %r12, %rsi
callq *0x298(%rax)
movq %r14, %rdi
movq %rbx, %rsi
movq 0x48(%rsp), %rdx
callq 0x13858
movq %rbx, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq