// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::TracePath(gaps::R3MeshVertex const*, gaps::R3Vector const&, double, gaps::R3Point*, gaps::R3MeshFace**, gaps::R3MeshIntersection*, int*) const
RNLength R3Mesh::
TracePath(const R3MeshVertex *source_vertex, const R3Vector& tangent_direction, RNLength max_distance,
  R3Point *final_position, R3MeshFace **final_face, R3MeshIntersection *intersections, int *nintersections) const
{
  // Increment mesh mark (used to avoid visiting same edge/face twice in recursive function)
  R3Mesh *mesh = (R3Mesh *) this;
  R3mesh_mark++;

  // Initialize result
  if (final_position) *final_position = VertexPosition(source_vertex);
  if (final_face) *final_face = NULL;
  if (nintersections) *nintersections = 0;
  if (max_distance <= 0) return 0;

  // Get convenient variables
  const R3Point& source_position = VertexPosition(source_vertex);
  R3Vector source_normal = VertexNormal(source_vertex);
  R3Vector split_direction = tangent_direction % source_normal;
  RNLength split_direction_length = split_direction.Length();
  if (split_direction_length == 0) return 0;
  split_direction /= split_direction_length;
  R3Plane split_plane(source_position, split_direction);

  // Find first face, edge, and position
  R3MeshFace *cur_face = NULL;
  R3MeshEdge *cur_edge = NULL;
  R3Point cur_position = R3zero_point;
  for (int i = 0; i < VertexValence(source_vertex); i++) {
    R3MeshEdge *cw_edge = EdgeOnVertex(source_vertex, i);
    R3MeshEdge *ccw_edge = EdgeOnVertex(source_vertex, cw_edge, RN_CCW);
    R3MeshFace *face = FaceOnVertex(source_vertex, cw_edge, RN_CCW);
    if (!face) continue;
    R3MeshEdge *edge = EdgeAcrossFace(face, source_vertex);

    // Check position of cw_vertex
    R3MeshVertex *cw_vertex = VertexAcrossEdge(cw_edge, source_vertex);
    const R3Point& cw_position = VertexPosition(cw_vertex);
    RNScalar cw_d = R3SignedDistance(split_plane, cw_position);
    if (cw_d == 0) {
      cur_position = cw_position;
      cur_face = face;
      cur_edge = edge;
      break;
    }

    // Check position of ccw_vertex
    R3MeshVertex *ccw_vertex = VertexAcrossEdge(ccw_edge, source_vertex);
    const R3Point& ccw_position = VertexPosition(ccw_vertex);
    RNScalar ccw_d = R3SignedDistance(split_plane, ccw_position);
    if (ccw_d == 0) {
      cur_position = ccw_position;
      cur_face = face;
      cur_edge = edge;
      break;
    }

    // Check if source direction splits cw and ccw vertices
    if ((cw_d > 0)  && (ccw_d < 0)) {
      RNScalar t = cw_d / (cw_d - ccw_d);
      cur_position = cw_position + t * (ccw_position - cw_position);
      cur_face = face;
      cur_edge = edge;
      break;
    }
  }      

  // Get next distance and direction
  RNLength cur_distance = R3Distance(cur_position, source_position);
  R3Vector cur_direction = cur_position - source_position; cur_direction.Normalize();

  // Check if found next face
  if (!cur_face) return 0;

  // Check if max_distance lies within next face
  if (cur_distance >= max_distance) {
    RNScalar t = max_distance / cur_distance;
    if (final_position) *final_position = (1-t)*source_position + t*cur_position;
    if (final_face) *final_face = cur_face;
    return max_distance;
  }

  // Trace path
  while (TRUE) {
    // Mark cur edge and face
    mesh->SetEdgeMark(cur_edge, R3mesh_mark);
    mesh->SetFaceMark(cur_face, R3mesh_mark);

    // Add intersection
    if (intersections && nintersections) {
      intersections[*nintersections].type = R3_MESH_EDGE_TYPE;
      intersections[*nintersections].vertex = NULL;
      intersections[*nintersections].edge = cur_edge;
      intersections[*nintersections].face = cur_face;
      intersections[*nintersections].point = cur_position;
      intersections[*nintersections].t = cur_distance;
      (*nintersections)++;
    }

    // Get next face
    R3MeshFace *next_face = FaceAcrossEdge(cur_edge, cur_face);
    if (!next_face) {
      if (final_position) *final_position = cur_position;
      if (final_face) *final_face = cur_face;
      return cur_distance;
    }
    
    // Get next direction 
    R3Vector cur_face_normal = FaceNormal(cur_face);
    R3Vector next_face_normal = FaceNormal(next_face);
    R4Matrix matrix = R4identity_matrix;
    matrix.Rotate(cur_face_normal, next_face_normal);
    R3Vector next_direction = matrix * cur_direction;
    next_direction.Normalize();
    
    // Get plane splitting next face along next_direction
    R3Vector split_plane_normal = next_face_normal % next_direction;  
    split_plane_normal.Normalize();
    R3Plane split_plane(cur_position, split_plane_normal);
    
    // Get next edge and position
    R3MeshVertex *vertex = VertexAcrossFace(next_face, cur_edge);
    R3Point next_position = VertexPosition(vertex);
    RNScalar d = R3SignedDistance(split_plane, next_position);
    R3MeshEdge *next_edge = EdgeOnFace(next_face, cur_edge, (d < 0) ? RN_CW : RN_CCW);
    if (d != 0) {
      // Split does not hit vertex
      R3Vector next_edge_vector = EdgeVector(next_edge);
      RNScalar denom = split_plane.Normal().Dot(next_edge_vector);
      if (denom != 0) {
        R3Point next_edge_start = VertexPosition(VertexOnEdge(next_edge, 0));
        RNScalar t = -(R3SignedDistance(split_plane, next_edge_start)) / denom;
        next_position = next_edge_start + next_edge_vector * t;
      }
    }

    // Get next distance
    RNLength next_distance = cur_distance + R3Distance(cur_position, next_position);
    if (next_distance >= max_distance) {
      assert(next_distance > cur_distance);
      RNScalar t = (max_distance - cur_distance) / (next_distance - cur_distance);
      if (final_position) *final_position = (1-t)*cur_position + t*next_position;
      if (final_face) *final_face = next_face;
      return max_distance;
    }

    // Go to next face
    cur_edge = next_edge;
    cur_face = next_face;
    cur_position = next_position;
    cur_direction = next_direction;
    cur_distance = next_distance;
  }

  // Return error
  return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x238, %rsp            # imm = 0x238
movq %r9, 0x48(%rsp)
movq %r8, %rbp
movq %rcx, %r15
movapd %xmm0, %xmm1
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, 0x28(%rsp)
incl 0xc8a79(%rip)           # 0xe54b0
testq %rcx, %rcx
movsd %xmm0, 0x18(%rsp)
je 0x1ca54
leaq 0x18(%r14), %rsi
movq %r15, %rdi
callq 0x3c89a
movsd 0x18(%rsp), %xmm1
movq 0x270(%rsp), %rax
testq %rbp, %rbp
je 0x1ca69
movq $0x0, (%rbp)
testq %rax, %rax
je 0x1ca74
movl $0x0, (%rax)
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jae 0x1d1ba
movq %r15, 0x20(%rsp)
testb $0x2, 0x78(%r14)
jne 0x1ca9f
movq 0x28(%rsp), %rdi
movq (%rdi), %rax
movq %r14, %rsi
callq *0x2b0(%rax)
leaq 0x30(%r14), %rsi
leaq 0x1a0(%rsp), %r15
movq %r15, %rdi
callq 0x3cfd6
leaq 0x140(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x3d8fa
movq %r13, %rdi
callq 0x3d0cc
movapd %xmm0, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x1cae5
jnp 0x1d1ba
leaq 0x18(%r14), %r15
leaq 0x140(%rsp), %rbx
movq %rbx, %rdi
movapd %xmm1, %xmm0
callq 0x3db98
leaq 0xd8(%rsp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x3b0b2
leaq 0x4c9f49(%rip), %rsi    # 0x4e6a60
leaq 0x30(%rsp), %rdi
callq 0x3c542
movl 0x14(%r14), %ecx
testl %ecx, %ecx
movq %rbp, 0x8(%rsp)
jle 0x1cc54
movq %r15, 0x10(%rsp)
xorl %ebp, %ebp
movq 0x8(%r14), %rax
movq (%rax,%rbp,8), %rax
movq 0x8(%rax), %r15
xorl %edx, %edx
cmpq %r14, %r15
setne %dl
movq 0x18(%rax,%rdx,8), %r13
testq %r13, %r13
je 0x1cc3d
cmpq %r14, 0x8(%r13)
je 0x1cb71
cmpq %r14, 0x10(%r13)
je 0x1cb78
cmpq %r14, 0x18(%r13)
je 0x1cb7f
xorl %ebx, %ebx
jmp 0x1cb89
movl $0x30, %ecx
jmp 0x1cb84
movl $0x20, %ecx
jmp 0x1cb84
movl $0x28, %ecx
movq (%r13,%rcx), %rbx
movq 0x20(%r13), %r12
cmpq %r14, 0x8(%r12)
je 0x1cb9b
cmpq %r14, 0x10(%r12)
jne 0x1cbca
movq 0x28(%r13), %r12
cmpq %r14, 0x8(%r12)
je 0x1cbad
cmpq %r14, 0x10(%r12)
jne 0x1cbca
movq 0x30(%r13), %rcx
cmpq %r14, 0x8(%rcx)
je 0x1cbc7
cmpq %r14, 0x10(%rcx)
movl $0x0, %r12d
cmovneq %rcx, %r12
jmp 0x1cbca
xorl %r12d, %r12d
cmpq %r14, %r15
jne 0x1cbd3
movq 0x10(%rax), %r15
addq $0x18, %r15
leaq 0xd8(%rsp), %rdi
movq %r15, %rsi
callq 0x49617
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1cbf3
jnp 0x1cc5c
movsd %xmm0, (%rsp)
movq %rbx, %rax
movq 0x8(%rbx), %rbx
cmpq %r14, %rbx
jne 0x1cc08
movq 0x10(%rax), %rbx
addq $0x18, %rbx
leaq 0xd8(%rsp), %rdi
movq %rbx, %rsi
callq 0x49617
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1cc28
jnp 0x1cc66
movsd (%rsp), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x1cc39
ucomisd %xmm0, %xmm1
ja 0x1cc70
movl 0x14(%r14), %ecx
incq %rbp
movslq %ecx, %rax
cmpq %rax, %rbp
jl 0x1cb39
xorl %r12d, %r12d
xorl %r13d, %r13d
jmp 0x1cccf
xorl %r12d, %r12d
xorl %r13d, %r13d
jmp 0x1ccd9
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
jmp 0x1ccca
leaq 0x30(%rsp), %rdi
movq %rbx, %rsi
jmp 0x1ccca
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
divsd %xmm1, %xmm2
movsd %xmm2, (%rsp)
leaq 0x70(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x3c78e
leaq 0xb8(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movsd (%rsp), %xmm0
callq 0x3d962
leaq 0x1b8(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x3c877
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x3c89a
movq 0x8(%rsp), %rbp
movq 0x10(%rsp), %r15
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x49404
movsd %xmm0, (%rsp)
leaq 0x70(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x3c78e
movq %r14, %rdi
callq 0x3d2dc
testq %r13, %r13
je 0x1d06f
movsd 0x18(%rsp), %xmm2
movsd (%rsp), %xmm1
ucomisd %xmm2, %xmm1
jae 0x1d078
cmpq $0x0, 0x270(%rsp)
setne %al
cmpq $0x0, 0x48(%rsp)
setne %cl
andb %al, %cl
movb %cl, 0x10(%rsp)
leaq 0xb8(%rsp), %r15
movl 0xc8760(%rip), %eax     # 0xe54b0
movl %eax, 0x48(%r12)
movl %eax, 0xb8(%r13)
cmpb $0x0, 0x10(%rsp)
movsd %xmm1, (%rsp)
je 0x1cdbf
movq 0x270(%rsp), %rbp
movslq (%rbp), %rax
shlq $0x6, %rax
movq 0x48(%rsp), %rbx
leaq (%rbx,%rax), %rdi
addq $0x20, %rdi
movl $0x2, -0x20(%rdi)
movq $0x0, -0x18(%rdi)
movq %r12, -0x10(%rdi)
movq %r13, -0x8(%rdi)
leaq 0x30(%rsp), %rsi
callq 0x3c89a
movsd (%rsp), %xmm1
movslq (%rbp), %rax
leal 0x1(%rax), %ecx
shlq $0x6, %rax
movsd %xmm1, 0x38(%rbx,%rax)
movl %ecx, (%rbp)
movq 0x18(%r12), %rbx
cmpq %r13, %rbx
jne 0x1cdce
movq 0x20(%r12), %rbx
testq %rbx, %rbx
je 0x1d0fd
testb $0x4, 0xa8(%r13)
jne 0x1cdf2
movq 0x28(%rsp), %rdi
movq (%rdi), %rax
movq %r13, %rsi
callq *0x2c8(%rax)
movq %r15, %r14
addq $0x38, %r13
leaq 0xa0(%rsp), %rdi
movq %r13, %rsi
callq 0x3cfd6
testb $0x4, 0xa8(%rbx)
jne 0x1ce23
movq 0x28(%rsp), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
callq *0x2c8(%rax)
leaq 0x38(%rbx), %rsi
leaq 0x188(%rsp), %r13
movq %r13, %rdi
callq 0x3cfd6
leaq 0x1b8(%rsp), %rbp
movq %rbp, %rdi
leaq 0x85ff7(%rip), %rsi     # 0xa2e40
callq 0x2c2be
movq %rbp, %rdi
leaq 0xa0(%rsp), %rsi
movq %r13, %rdx
callq 0x2dbe4
leaq 0x128(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
leaq 0x70(%rsp), %rdx
callq 0x2e342
movq %r15, %rdi
callq 0x3d2dc
leaq 0x170(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x3d8fa
movq %rbp, %rdi
callq 0x3d2dc
movq %r14, %r15
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
movq %rbp, %rdx
callq 0x3b0b2
movq 0x8(%rbx), %rsi
movq 0x8(%r12), %rax
cmpq %rsi, %rax
leaq 0x88(%rsp), %r14
je 0x1cecf
cmpq %rsi, 0x10(%r12)
jne 0x1cef5
movq 0x10(%rbx), %rsi
movq 0x10(%r12), %rcx
cmpq %rsi, %rax
je 0x1cee4
cmpq %rsi, %rcx
jne 0x1cef5
jmp 0x1cee7
movq %rcx, %rsi
movq 0x18(%rbx), %rax
cmpq %rax, %rsi
jne 0x1cef2
xorl %eax, %eax
movq %rax, %rsi
addq $0x18, %rsi
movq %r14, %rdi
callq 0x3c542
movq %r15, %rdi
movq %r14, %rsi
callq 0x49617
movq 0x20(%rbx), %r13
cmpq %r12, %r13
je 0x1cf3f
movq 0x28(%rbx), %rax
xorpd %xmm1, %xmm1
cmpq %r12, %rax
je 0x1cf54
ucomisd %xmm0, %xmm1
jbe 0x1cf2b
movq %rax, %r13
cmpq %r12, 0x30(%rbx)
movl $0x0, %eax
cmovneq %rax, %r13
leaq 0x58(%rsp), %r12
jmp 0x1cf63
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
leaq 0x58(%rsp), %r12
ja 0x1cf5f
movq 0x28(%rbx), %r13
jmp 0x1cf63
ucomisd %xmm0, %xmm1
leaq 0x58(%rsp), %r12
ja 0x1cf63
movq 0x30(%rbx), %r13
ucomisd 0x8527d(%rip), %xmm0    # 0xa21e8
jne 0x1cf73
jnp 0x1d015
movq 0x8(%r13), %rdx
movq 0x10(%r13), %rsi
addq $0x18, %rsi
addq $0x18, %rdx
movq %r12, %rdi
callq 0x3c78e
movq %r15, %rdi
movq %r12, %rsi
callq 0x3d252
ucomisd 0x8524a(%rip), %xmm0    # 0xa21e8
jne 0x1cfa2
jnp 0x1d015
movq 0x8(%r13), %rsi
addq $0x18, %rsi
leaq 0x110(%rsp), %rbp
movq %rbp, %rdi
movsd %xmm0, 0x50(%rsp)
callq 0x3c542
movq %r15, %rdi
movq %rbp, %rsi
callq 0x49617
xorpd 0x8504d(%rip), %xmm0    # 0xa2020
divsd 0x50(%rsp), %xmm0
leaq 0x158(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x3d962
leaq 0xf8(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
movq %r14, %rdx
leaq 0x88(%rsp), %r14
callq 0x3c877
movq %r14, %rdi
movq %r12, %rsi
callq 0x3c89a
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x49404
movsd (%rsp), %xmm1
addsd %xmm1, %xmm0
movsd 0x18(%rsp), %xmm2
ucomisd %xmm2, %xmm0
jae 0x1d12c
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
movsd %xmm0, (%rsp)
callq 0x3c89a
leaq 0x70(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x3d752
movsd (%rsp), %xmm1
movq %r13, %r12
movq %rbx, %r13
jmp 0x1cd4a
xorpd %xmm2, %xmm2
jmp 0x1d1b6
movq 0x20(%rsp), %r12
testq %r12, %r12
je 0x1d0eb
divsd %xmm1, %xmm2
movsd %xmm2, (%rsp)
movsd 0x84fa5(%rip), %xmm0    # 0xa2038
subsd %xmm2, %xmm0
leaq 0xb8(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3cf76
leaq 0xa0(%rsp), %r14
leaq 0x30(%rsp), %rsi
movq %r14, %rdi
movsd (%rsp), %xmm0
callq 0x3cf76
leaq 0x1b8(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x3cf30
movq %r12, %rdi
movq %r15, %rsi
callq 0x3c89a
movsd 0x18(%rsp), %xmm2
testq %rbp, %rbp
je 0x1d1b6
movq %r13, (%rbp)
jmp 0x1d1b6
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x1d116
leaq 0x30(%rsp), %rsi
callq 0x3c89a
movsd (%rsp), %xmm1
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x1d123
movq %r13, (%rax)
movapd %xmm1, %xmm2
jmp 0x1d1b6
movq 0x20(%rsp), %r13
testq %r13, %r13
je 0x1d1a9
subsd %xmm1, %xmm2
subsd %xmm1, %xmm0
divsd %xmm0, %xmm2
movsd %xmm2, (%rsp)
movsd 0x84ee9(%rip), %xmm0    # 0xa2038
subsd %xmm2, %xmm0
leaq 0x110(%rsp), %r14
leaq 0x30(%rsp), %rsi
movq %r14, %rdi
callq 0x3cf76
leaq 0xf8(%rsp), %r15
leaq 0x88(%rsp), %rsi
movq %r15, %rdi
movsd (%rsp), %xmm0
callq 0x3cf76
leaq 0x58(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x3cf30
movq %r13, %rdi
movq %r12, %rsi
callq 0x3c89a
movsd 0x18(%rsp), %xmm2
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x1d1b6
movq %rbx, (%rax)
movapd %xmm2, %xmm0
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq