// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::SwapEdge(gaps::R3MeshEdge*)
int R3Mesh::
SwapEdge(R3MeshEdge *edge)
{
  // 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 (!f[0] || !f[1]) return 0;

  // Get edges and vertices across faces
  R3MeshEdge *ef[2][2] = { { NULL, NULL }, { NULL, NULL } };
  R3MeshVertex *vf[2] = { NULL, NULL };
  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]));
  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]));
  if (EdgeBetweenVertices(vf[0], vf[1])) return 0;

  // Update vertices
  v[0]->edges.Remove(edge);
  v[1]->edges.Remove(edge);
  vf[0]->edges.Insert(edge);
  vf[1]->edges.Insert(edge);

  // Update edge
  edge->vertex[0] = vf[0];
  edge->vertex[1] = vf[1];
  edge->length = 0;
  edge->flags = 0;

  // Update faces
  UpdateFaceRefs(f[0], vf[0], vf[1], v[1], edge, ef[1][1], ef[0][1]);
  UpdateFacePlane(f[0]);
  UpdateFaceBBox(f[0]);
  UpdateFaceRefs(f[1], vf[1], vf[0], v[0], edge, ef[0][0], ef[1][0]);
  UpdateFacePlane(f[1]);
  UpdateFaceBBox(f[1]);

  // Invalidate GL buffer objects
  InvalidateGLBufferObjects();

  // Return success
  return 1;
}

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x78(%rsp)
movq %rsi, 0x70(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x60(%rsp)
movq 0x70(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x68(%rsp)
movq 0x70(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x50(%rsp)
movq 0x70(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x50(%rsp)
je 0x1e2c3
cmpq $0x0, 0x58(%rsp)
jne 0x1e2d3
movl $0x0, 0x84(%rsp)
jmp 0x1e533
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0xf280
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0xf280
movq 0x10(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x70(%rsp), %rdx
movq 0x50(%rsp), %rcx
callq 0x18410
movq 0x10(%rsp), %rdi
movq %rax, 0x30(%rsp)
movq 0x68(%rsp), %rsi
movq 0x70(%rsp), %rdx
movq 0x50(%rsp), %rcx
callq 0x18410
movq 0x10(%rsp), %rdi
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rsi
movq 0x68(%rsp), %rdx
callq 0x36f50
movq 0x10(%rsp), %rdi
movq %rax, 0x20(%rsp)
movq 0x60(%rsp), %rsi
movq 0x70(%rsp), %rdx
movq 0x58(%rsp), %rcx
callq 0x18410
movq 0x10(%rsp), %rdi
movq %rax, 0x40(%rsp)
movq 0x68(%rsp), %rsi
movq 0x70(%rsp), %rdx
movq 0x58(%rsp), %rcx
callq 0x18410
movq 0x10(%rsp), %rdi
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rsi
movq 0x68(%rsp), %rdx
callq 0x36f50
movq 0x10(%rsp), %rdi
movq %rax, 0x28(%rsp)
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x18280
cmpq $0x0, %rax
je 0x1e3c9
movl $0x0, 0x84(%rsp)
jmp 0x1e533
movq 0x60(%rsp), %rdi
addq $0x8, %rdi
movq 0x70(%rsp), %rsi
callq 0x377b0
movq 0x68(%rsp), %rdi
addq $0x8, %rdi
movq 0x70(%rsp), %rsi
callq 0x377b0
movq 0x20(%rsp), %rdi
addq $0x8, %rdi
movq 0x70(%rsp), %rsi
callq 0x376d0
movq 0x28(%rsp), %rdi
addq $0x8, %rdi
movq 0x70(%rsp), %rsi
callq 0x376d0
movq 0x20(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x28(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x70(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x28(%rax)
leaq 0x18(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x13d80
movq 0x10(%rsp), %rdi
movq 0x70(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0x38(%rax)
movq 0x50(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0x68(%rsp), %r8
movq 0x70(%rsp), %r9
movq 0x48(%rsp), %r11
movq 0x38(%rsp), %r10
movq (%rdi), %rax
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
callq *0x2d8(%rax)
movq 0x10(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq (%rdi), %rax
callq *0x2c8(%rax)
movq 0x10(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq (%rdi), %rax
callq *0x2d0(%rax)
movq 0x10(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq 0x60(%rsp), %r8
movq 0x70(%rsp), %r9
movq 0x30(%rsp), %r11
movq 0x40(%rsp), %r10
movq (%rdi), %rax
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
callq *0x2d8(%rax)
movq 0x10(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq (%rdi), %rax
callq *0x2c8(%rax)
movq 0x10(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq (%rdi), %rax
callq *0x2d0(%rax)
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
callq *0x290(%rax)
movl $0x1, 0x84(%rsp)
movl 0x84(%rsp), %eax
addq $0x88, %rsp
retq
nopw %cs:(%rax,%rax)