// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Isect.cpp
// Function: gaps::R3Intersects(gaps::R3Ray const&, gaps::R3Cone const&, gaps::R3Point*, gaps::R3Vector*, double*)
RNClassID R3Intersects(const R3Ray& ray, const R3Cone& cone, 
    R3Point *hit_point1, R3Vector *hit_normal1, RNScalar *hit_t1)
{
    // Check if sphere contains ray start point
    RNBoolean start_inside = R3Contains(cone, ray.Start());
    if (start_inside) {
      if (hit_point1) *hit_point1 = ray.Start();
      if (hit_normal1) *hit_normal1 = -ray.Vector();
      if (hit_t1) *hit_t1 = 0;
      return R3_SPAN_CLASS_ID;
    }

    // Transform ray into canonical coordinate system with
    // axis end point at origin and axis going along negative z axis
    R3Ray ray1(ray);
    R3Affine xform(R4identity_matrix);
    xform.Translate(- (cone.Axis().End().Vector()) );
    xform.Rotate(cone.Axis().Vector(), R3negz_vector);
    ray1.Transform(xform);

    // Compute coefficients for quadratic formula
    RNScalar r = cone.Radius();
    const R3Point& p = ray1.Start();
    const R3Vector& v = ray1.Vector();
    RNScalar A = v.X()*v.X() + v.Y()*v.Y() - r*r*v.Z()*v.Z();
    RNScalar B = 2.0*p.X()*v.X() + 2.0*p.Y()*v.Y() - 2.0*r*r*p.Z()*v.Z();
    RNScalar C = p.X()*p.X() + p.Y()*p.Y() - r*r*p.Z()*p.Z();

    // Solve for t1 and t2 on sides
    RNScalar ray_t;
    if (RNIsZero(A)) {
        if (RNIsZero(B)) {
	    // No intersection
	    return RN_NULL_CLASS_ID;
	}
	else {
	    // ???
	    ray_t = -C/B;
	}
    }
    else {
        RNScalar sqr = B*B - 4.0*A*C;
	if (RNIsNegative(sqr)) {
	    // No intersection
	    return RN_NULL_CLASS_ID;
	}
	else if (RNIsPositive(sqr)) {
	    // Intersection at two points
	    RNScalar s = sqrt(sqr);
	    RNScalar ray_t1 = 0.5 * (-B - s) / A;
	    RNScalar ray_t2 = 0.5 * (-B + s) / A;
	    ray_t = (ray_t1 < ray_t2) ? ray_t1 : ray_t2;
	}
	else {
	    // Intersection at one point
	    ray_t = -0.5 * B / A;
	}
    }

    // Check whether intersection point is on right side of ray
    if (RNIsNegative(ray_t)) return RN_NULL_CLASS_ID;

    // Check whether intersection point is on right side of double cone or beyond cap
    R3Point p1 = ray1.Point(ray_t);
    if (RNIsNegative(p1.Z())) return RN_NULL_CLASS_ID;
    if (RNIsGreater(p1.Z(), cone.Axis().Length())) return RN_NULL_CLASS_ID;

    // Check for intersection with base
    if (RNIsGreater(ray1.Start().Z(), cone.Axis().Length())) {
        // ???
    }

    // This is not complete ???

    // Transform intersection back to world coordinates
    R3Point intersection = ray.Point(ray_t);
    intersection.InverseTransform(xform);

    // Ray hit cone
    if (hit_t1) *hit_t1 = ray_t;
    if (hit_point1) { *hit_point1 = intersection; }
    if (hit_normal1) { *hit_normal1 = R3zero_vector; }
    return R3_POINT_CLASS_ID;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movq %r8, %r14
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %r12
movq %rsi, %rdi
movq %r12, %rsi
callq 0x49960
testl %eax, %eax
je 0x44b9a
testq %r15, %r15
je 0x44b59
movq %r15, %rdi
movq %r12, %rsi
callq 0x3cd94
testq %rbx, %rbx
je 0x44b80
addq $0x18, %r12
leaq 0xa8(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x3e154
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3dc6a
movl $0x4, %ebp
testq %r14, %r14
je 0x44ece
movq $0x0, (%r14)
jmp 0x44ece
leaq 0x28(%rsp), %rdi
movq %r12, %rsi
callq 0x3c60e
leaq 0x5e292(%rip), %rsi     # 0xa2e40
leaq 0xa8(%rsp), %rdi
xorl %edx, %edx
callq 0x2a22e
movsd 0x38(%r13), %xmm0
movsd 0x40(%r13), %xmm1
movsd 0x48(%r13), %xmm2
leaq 0x10(%rsp), %rdi
callq 0x3d4be
leaq 0x58(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x3e154
leaq 0xb0(%rsp), %rbp
leaq 0x58(%rsp), %rsi
movq %rbp, %rdi
callq 0x2d6f4
andb $-0x3, 0x1b0(%rsp)
leaq 0x20(%r13), %rsi
leaq 0x63b78(%rip), %rdx     # 0xa8788
movq %rbp, %rdi
callq 0x2dee0
leaq 0xa8(%rsp), %rsi
andb $-0x3, 0x108(%rsi)
leaq 0x28(%rsp), %rdi
callq 0x3c6e8
movsd 0x78(%r13), %xmm0
movsd 0x40(%rsp), %xmm3
movsd 0x48(%rsp), %xmm2
movsd 0x30(%rsp), %xmm1
movapd %xmm1, %xmm4
addsd %xmm1, %xmm4
mulsd %xmm2, %xmm4
movapd %xmm2, %xmm5
mulsd %xmm2, %xmm5
movsd 0x28(%rsp), %xmm2
movapd %xmm2, %xmm6
addsd %xmm2, %xmm6
mulsd %xmm3, %xmm6
mulsd %xmm3, %xmm3
addsd %xmm5, %xmm3
movsd 0x50(%rsp), %xmm5
movapd 0x5d39a(%rip), %xmm8    # 0xa2020
movapd %xmm0, %xmm7
xorpd %xmm8, %xmm7
mulsd %xmm0, %xmm7
movapd %xmm7, %xmm9
mulsd %xmm5, %xmm9
mulsd %xmm5, %xmm9
addsd %xmm3, %xmm9
addsd %xmm4, %xmm6
movsd 0x38(%rsp), %xmm3
movsd 0x5d547(%rip), %xmm4    # 0xa2200
mulsd %xmm0, %xmm4
mulsd %xmm0, %xmm4
mulsd %xmm3, %xmm4
mulsd %xmm5, %xmm4
addsd %xmm6, %xmm4
mulsd %xmm1, %xmm1
mulsd %xmm2, %xmm2
addsd %xmm1, %xmm2
mulsd %xmm3, %xmm7
mulsd %xmm3, %xmm7
addsd %xmm2, %xmm7
leaq 0xa0eac(%rip), %rbp     # 0xe5b98
movsd (%rbp), %xmm0
xorpd %xmm0, %xmm8
ucomisd %xmm8, %xmm9
jb 0x44d26
ucomisd %xmm9, %xmm0
jb 0x44d26
ucomisd %xmm8, %xmm4
jb 0x44d15
ucomisd %xmm4, %xmm0
jae 0x44ebf
xorpd 0x5d303(%rip), %xmm7    # 0xa2020
divsd %xmm4, %xmm7
jmp 0x44e05
movsd 0x64222(%rip), %xmm1    # 0xa8f50
mulsd %xmm9, %xmm1
mulsd %xmm7, %xmm1
movapd %xmm4, %xmm2
mulsd %xmm4, %xmm2
addsd %xmm1, %xmm2
ucomisd %xmm2, %xmm8
ja 0x44dfa
ucomisd %xmm0, %xmm2
ja 0x44d6a
mulsd 0x5d504(%rip), %xmm4    # 0xa2260
divsd %xmm9, %xmm4
movapd %xmm4, %xmm7
jmp 0x44dfa
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jb 0x44d7d
xorps %xmm0, %xmm0
sqrtsd %xmm2, %xmm0
jmp 0x44dc4
movapd %xmm2, %xmm0
movapd %xmm8, (%rsp)
movapd %xmm9, 0x90(%rsp)
movapd %xmm4, 0x80(%rsp)
movapd %xmm2, 0x70(%rsp)
callq 0xe340
movapd 0x70(%rsp), %xmm2
movapd 0x80(%rsp), %xmm4
movapd 0x90(%rsp), %xmm9
movapd (%rsp), %xmm8
movapd 0x5d254(%rip), %xmm7    # 0xa2020
xorpd %xmm4, %xmm7
unpcklpd %xmm0, %xmm7            # xmm7 = xmm7[0],xmm0[0]
unpcklpd %xmm4, %xmm0            # xmm0 = xmm0[0],xmm4[0]
subpd %xmm0, %xmm7
mulpd 0x5d22c(%rip), %xmm7    # 0xa2010
unpcklpd %xmm9, %xmm9            # xmm9 = xmm9[0,0]
divpd %xmm9, %xmm7
movapd %xmm7, %xmm0
unpckhpd %xmm7, %xmm0            # xmm0 = xmm0[1],xmm7[1]
minsd %xmm0, %xmm7
ucomisd %xmm2, %xmm8
ja 0x44ebf
movsd (%rbp), %xmm0
xorpd 0x5d20e(%rip), %xmm0    # 0xa2020
ucomisd %xmm7, %xmm0
ja 0x44ebf
leaq 0x58(%rsp), %rdi
leaq 0x28(%rsp), %rsi
movapd %xmm7, (%rsp)
movapd %xmm7, %xmm0
callq 0x3c626
movsd 0x68(%rsp), %xmm0
movsd (%rbp), %xmm1
movapd 0x5d1d9(%rip), %xmm2    # 0xa2020
xorpd %xmm1, %xmm2
xorl %ebp, %ebp
ucomisd %xmm0, %xmm2
ja 0x44ec1
subsd 0x50(%r13), %xmm0
ucomisd %xmm1, %xmm0
ja 0x44ec1
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
movapd (%rsp), %xmm0
callq 0x3c626
leaq 0x10(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x3d354
testq %r14, %r14
je 0x44e92
movapd (%rsp), %xmm0
movsd %xmm0, (%r14)
testq %r15, %r15
je 0x44ea4
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x3cd94
testq %rbx, %rbx
je 0x44eb8
leaq 0x63830(%rip), %rsi     # 0xa86e0
movq %rbx, %rdi
callq 0x3dc6a
movl $0x1, %ebp
jmp 0x44ec1
xorl %ebp, %ebp
leaq 0xa8(%rsp), %rdi
callq 0x2a27c
movl %ebp, %eax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x44ee8
jmp 0x44ee8
jmp 0x44ee8
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x2a27c
movq %rbx, %rdi
callq 0xe7e0