// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::SplitFace(gaps::R3MeshFace*, gaps::R3Point const&, gaps::R3MeshFace**, gaps::R3MeshFace**, gaps::R3MeshFace**)
R3MeshVertex *R3Mesh::
SplitFace(R3MeshFace *f, const R3Point& point, R3MeshFace **f0, R3MeshFace **f1, R3MeshFace **f2)
{
  // Invalidate GL buffer objects
  InvalidateGLBufferObjects();

  // Find vertices/edges bounding face
  R3MeshVertex *v0 = VertexOnFace(f, 0);
  R3MeshEdge *e0 = EdgeOnFace(f, v0, RN_CCW);
  R3MeshVertex *v1 = VertexAcrossEdge(e0, v0);
  R3MeshEdge *e1 = EdgeOnFace(f, v1, RN_CCW);
  R3MeshVertex *v2 = VertexAcrossEdge(e1, v1);
  R3MeshEdge *e2 = EdgeOnFace(f, v2, RN_CCW);
  int m = FaceMaterial(f);
  int s = FaceSegment(f);
  int c = FaceCategory(f);

  // Delete face
  DeleteFace(f);

  // Create new vertex at point
  R3MeshVertex *vertex = CreateVertex(point);

  // Create three new edges
  R3MeshEdge *s0 = CreateEdge(v0, vertex);
  R3MeshEdge *s1 = CreateEdge(v1, vertex);
  R3MeshEdge *s2 = CreateEdge(v2, vertex);

  // Create three new faces
  R3MeshFace *t0 = CreateFace(vertex, v0, v1, s0, e0, s1);
  R3MeshFace *t1 = CreateFace(vertex, v1, v2, s1, e1, s2);
  R3MeshFace *t2 = CreateFace(vertex, v2, v0, s2, e2, s0);
  
  // Set face materials
  SetFaceMaterial(t0, m);
  SetFaceMaterial(t1, m);
  SetFaceMaterial(t2, m);

  // Set face segments
  SetFaceSegment(t0, s);
  SetFaceSegment(t1, s);
  SetFaceSegment(t2, s);

  // Set face categories
  SetFaceCategory(t0, c);
  SetFaceCategory(t1, c);
  SetFaceCategory(t2, c);

  // Interpolate vertex properties
  RNScalar d0 = R3Distance(point, VertexPosition(v0));
  RNScalar d1 = R3Distance(point, VertexPosition(v1));
  RNScalar d2 = R3Distance(point, VertexPosition(v2));
  RNScalar dsum = d0 + d1 + d2;
  if (RNIsPositive(dsum)) {
    RNScalar t0 = (d1+d2)/dsum;   RNScalar t1 = (d0+d2)/dsum;  RNScalar t2 = (d0+d1)/dsum;
    SetVertexTextureCoords(vertex, t0*VertexTextureCoords(v0) + t1*VertexTextureCoords(v1) + t2*VertexTextureCoords(v2)); 
    SetVertexColor(vertex, t0*VertexColor(v0) + t1*VertexColor(v1) + t2*VertexColor(v2)); 
  }

  // Return created faces
  if (f0) *f0 = t0;
  if (f1) *f1 = t1;
  if (f2) *f2 = t2;

  // Return new vertex
  return vertex;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %r9, 0x78(%rsp)
movq %r8, 0x70(%rsp)
movq %rcx, 0x68(%rsp)
movq %rdx, %r12
movq %rsi, %rbp
movq %rdi, %r15
movq (%rdi), %rax
callq *0x290(%rax)
movq 0x8(%rbp), %rbx
movq 0x20(%rbp), %rdx
movq 0x8(%rdx), %r14
cmpq %rbx, %r14
jne 0x1657b
movq 0x10(%rdx), %r14
cmpq %r14, %rbx
je 0x16590
cmpq %r14, 0x10(%rbp)
je 0x16597
cmpq %r14, 0x18(%rbp)
je 0x1659e
xorl %eax, %eax
jmp 0x165a8
movl $0x20, %eax
jmp 0x165a3
movl $0x28, %eax
jmp 0x165a3
movl $0x30, %eax
movq (%rbp,%rax), %rax
movq 0x8(%rax), %rcx
cmpq %r14, %rcx
jne 0x165b5
movq 0x10(%rax), %rcx
cmpq %rcx, %rbx
movq %rcx, 0x30(%rsp)
movq %rdx, (%rsp)
movq %rax, 0x8(%rsp)
je 0x165df
cmpq %rcx, 0x10(%rbp)
je 0x165e6
cmpq %rcx, 0x18(%rbp)
je 0x165ed
movq $0x0, 0x10(%rsp)
jmp 0x165fc
movl $0x20, %eax
jmp 0x165f2
movl $0x28, %eax
jmp 0x165f2
movl $0x30, %eax
movq (%rbp,%rax), %rax
movq %rax, 0x10(%rsp)
movl 0x98(%rbp), %eax
movl %eax, 0x20(%rsp)
movl 0x9c(%rbp), %eax
movl %eax, 0x28(%rsp)
movl 0xa0(%rbp), %eax
movl %eax, 0x18(%rsp)
movq (%r15), %rax
movq %r15, %rdi
movq %rbp, %rsi
callq *0x70(%rax)
movq (%r15), %rax
movq %r15, %rdi
movq %r12, %rbp
movq %r12, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movq %rax, %r13
movq (%r15), %rax
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq *0x40(%rax)
movq %rax, %r12
movq %rax, 0x58(%rsp)
movq (%r15), %rax
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq *0x40(%rax)
movq %rbp, 0x60(%rsp)
movq %rbx, 0x40(%rsp)
movq %rax, %rbx
movq (%r15), %rax
movq %r15, %rdi
movq 0x30(%rsp), %rbp
movq %rbp, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq *0x40(%rax)
movq %rax, 0x50(%rsp)
movq (%r15), %rax
movq %r15, %rdi
movq %r13, %rsi
movq 0x40(%rsp), %rdx
movq %r14, %rcx
movq %r12, %r8
movq (%rsp), %r9
xorl %r10d, %r10d
pushq %r10
pushq %rbx
callq *0x58(%rax)
addq $0x10, %rsp
movq %r14, 0x48(%rsp)
movq %rax, %r12
movq (%r15), %rax
movq %r15, %rdi
movq %r13, %rsi
movq 0x48(%rsp), %rdx
movq %rbp, %rcx
movq %rbx, %r8
movq 0x40(%rsp), %r14
movq 0x8(%rsp), %r9
xorl %r10d, %r10d
pushq %r10
movq 0x58(%rsp), %rbx
pushq %rbx
callq *0x58(%rax)
addq $0x10, %rsp
movq %rax, %rbp
movq (%r15), %rax
movq %r15, %rdi
movq %r13, 0x38(%rsp)
movq %r13, %rsi
movq 0x30(%rsp), %r15
movq %r15, %rdx
movq %r14, %rcx
movq %rbx, %r8
movq %r14, %rbx
movq 0x10(%rsp), %r9
xorl %r10d, %r10d
pushq %r10
pushq 0x60(%rsp)
callq *0x58(%rax)
addq $0x10, %rsp
movl 0x20(%rsp), %ecx
movl %ecx, 0x98(%r12)
movl %ecx, 0x98(%rbp)
movl %ecx, 0x98(%rax)
movl 0x28(%rsp), %ecx
movl %ecx, 0x9c(%r12)
movl %ecx, 0x9c(%rbp)
movl %ecx, 0x9c(%rax)
movq %r12, 0x28(%rsp)
movl 0x18(%rsp), %ecx
movl %ecx, 0xa0(%r12)
movq 0x48(%rsp), %r12
movq %rbp, %r14
movl %ecx, 0xa0(%rbp)
movq %rax, 0x10(%rsp)
movl %ecx, 0xa0(%rax)
leaq 0x18(%rbx), %rsi
movq 0x60(%rsp), %r13
movq %r13, %rdi
callq 0x49404
movsd %xmm0, (%rsp)
leaq 0x18(%r12), %rsi
movq %r13, %rdi
callq 0x49404
movsd %xmm0, 0x8(%rsp)
leaq 0x18(%r15), %rsi
movq %r13, %rdi
callq 0x49404
movsd (%rsp), %xmm2
movapd %xmm2, %xmm3
movsd 0x8(%rsp), %xmm1
movapd %xmm1, %xmm4
addsd %xmm1, %xmm2
movapd %xmm2, %xmm1
addsd %xmm0, %xmm1
leaq 0xcf3c3(%rip), %rax     # 0xe5b98
ucomisd (%rax), %xmm1
jbe 0x16992
addsd %xmm0, %xmm4
divsd %xmm1, %xmm4
movsd %xmm4, 0x8(%rsp)
addsd %xmm0, %xmm3
divsd %xmm1, %xmm3
movsd %xmm3, (%rsp)
divsd %xmm1, %xmm2
movsd %xmm2, 0x18(%rsp)
leaq 0x48(%rbx), %rsi
leaq 0xb0(%rsp), %rbp
movq %rbp, %rdi
movapd %xmm4, %xmm0
callq 0x6834d
leaq 0x48(%r12), %rsi
movq %r14, 0x20(%rsp)
leaq 0x98(%rsp), %r14
movq %r14, %rdi
movsd (%rsp), %xmm0
callq 0x6834d
leaq 0xc8(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x682bb
movq 0x30(%rsp), %rbp
leaq 0x48(%rbp), %rsi
leaq 0x80(%rsp), %r13
movq %r13, %rdi
movsd 0x18(%rsp), %xmm0
callq 0x6834d
leaq 0xe0(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x682bb
movq 0x38(%rsp), %r13
leaq 0x48(%r13), %rdi
movq %r14, %rsi
callq 0x681de
movsd 0x58(%rbx), %xmm0
movsd 0x8(%rsp), %xmm2
mulsd %xmm2, %xmm0
movsd 0x60(%rbx), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x68(%rbx), %xmm2
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
callq 0x6f680
movsd 0x58(%r12), %xmm0
movsd (%rsp), %xmm2
mulsd %xmm2, %xmm0
movsd 0x60(%r12), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x68(%r12), %xmm2
leaq 0x98(%rsp), %r14
movq %r14, %rdi
callq 0x6f680
movsd (%rbx), %xmm0
movsd 0x8(%rbx), %xmm1
addsd (%r14), %xmm0
addsd 0x8(%r14), %xmm1
movsd 0x10(%rbx), %xmm2
addsd 0x10(%r14), %xmm2
leaq 0xc8(%rsp), %rbx
movq %rbx, %rdi
callq 0x6f680
movsd 0x58(%rbp), %xmm0
movsd 0x18(%rsp), %xmm2
mulsd %xmm2, %xmm0
movsd 0x60(%rbp), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x68(%rbp), %xmm2
leaq 0x80(%rsp), %r14
movq %r14, %rdi
callq 0x6f680
movsd (%rbx), %xmm0
movsd 0x8(%rbx), %xmm1
addsd (%r14), %xmm0
addsd 0x8(%r14), %xmm1
movsd 0x10(%rbx), %xmm2
addsd 0x10(%r14), %xmm2
movq 0x20(%rsp), %r14
leaq 0xe0(%rsp), %rbx
movq %rbx, %rdi
callq 0x6f680
leaq 0x58(%r13), %rdi
movq %rbx, %rsi
callq 0x6f712
movq 0x68(%rsp), %rax
testq %rax, %rax
je 0x169a4
movq 0x28(%rsp), %rcx
movq %rcx, (%rax)
movq 0x70(%rsp), %rcx
testq %rcx, %rcx
movq 0x78(%rsp), %rax
je 0x169b6
movq %r14, (%rcx)
testq %rax, %rax
je 0x169c3
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
movq 0x38(%rsp), %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq