// File: /workspace/github_cpp_makefile_star2_O3/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 $0x198, %rsp            # imm = 0x198
movq %r8, %r14
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %r12
movq %rsi, %rdi
movq %r12, %rsi
callq 0x49012
testl %eax, %eax
je 0x4455d
testq %r15, %r15
je 0x4451c
movq %r15, %rdi
movq %r12, %rsi
callq 0x3c89a
testq %rbx, %rbx
je 0x44543
addq $0x18, %r12
leaq 0x88(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x3dc00
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3d752
movl $0x4, %ebp
testq %r14, %r14
je 0x4485b
movq $0x0, (%r14)
jmp 0x4485b
leaq 0x28(%rsp), %rdi
movq %r12, %rsi
callq 0x3c11a
leaq 0x5e8cf(%rip), %rsi     # 0xa2e40
leaq 0x88(%rsp), %rdi
xorl %edx, %edx
callq 0x29fda
movsd 0x38(%r13), %xmm0
movsd 0x40(%r13), %xmm1
movsd 0x48(%r13), %xmm2
leaq 0x10(%rsp), %rdi
callq 0x3cfc6
leaq 0x58(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x3dc00
leaq 0x90(%rsp), %rbp
leaq 0x58(%rsp), %rsi
movq %rbp, %rdi
callq 0x2d3f8
andb $-0x3, 0x190(%rsp)
leaq 0x20(%r13), %rsi
leaq 0x641b5(%rip), %rdx     # 0xa8788
movq %rbp, %rdi
callq 0x2dbe4
leaq 0x88(%rsp), %rsi
andb $-0x3, 0x108(%rsi)
leaq 0x28(%rsp), %rdi
callq 0x3c1f4
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 0x5d9d7(%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 0x5db84(%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 0xa14e9(%rip), %rbp     # 0xe5b98
movsd (%rbp), %xmm2
movapd %xmm2, %xmm1
xorpd %xmm8, %xmm1
ucomisd %xmm1, %xmm9
jb 0x446ec
ucomisd %xmm9, %xmm2
jb 0x446ec
ucomisd %xmm1, %xmm4
jb 0x446db
ucomisd %xmm4, %xmm2
jae 0x4484c
xorpd 0x5d93d(%rip), %xmm7    # 0xa2020
divsd %xmm4, %xmm7
jmp 0x4479f
movsd 0x6485c(%rip), %xmm3    # 0xa8f50
mulsd %xmm9, %xmm3
mulsd %xmm7, %xmm3
movapd %xmm4, %xmm0
mulsd %xmm4, %xmm0
addsd %xmm3, %xmm0
ucomisd %xmm0, %xmm1
ja 0x4484c
ucomisd %xmm2, %xmm0
ja 0x4472c
mulsd 0x5db3f(%rip), %xmm4    # 0xa2260
divsd %xmm9, %xmm4
movapd %xmm4, %xmm7
jmp 0x4479f
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x4473c
sqrtsd %xmm0, %xmm0
jmp 0x44762
movapd %xmm9, (%rsp)
movapd %xmm4, 0x70(%rsp)
callq 0xe340
movapd 0x70(%rsp), %xmm4
movapd (%rsp), %xmm9
movapd 0x5d8be(%rip), %xmm8    # 0xa2020
movapd %xmm4, %xmm7
xorpd %xmm8, %xmm7
unpcklpd %xmm0, %xmm7            # xmm7 = xmm7[0],xmm0[0]
unpcklpd %xmm4, %xmm0            # xmm0 = xmm0[0],xmm4[0]
subpd %xmm0, %xmm7
mulpd 0x5d891(%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
movsd (%rbp), %xmm1
xorpd %xmm8, %xmm1
ucomisd %xmm7, %xmm1
ja 0x4484c
leaq 0x58(%rsp), %rdi
leaq 0x28(%rsp), %rsi
movapd %xmm7, (%rsp)
movapd %xmm7, %xmm0
callq 0x3c132
movsd 0x68(%rsp), %xmm0
movsd (%rbp), %xmm1
movapd 0x5d84c(%rip), %xmm2    # 0xa2020
xorpd %xmm1, %xmm2
xorl %ebp, %ebp
ucomisd %xmm0, %xmm2
ja 0x4484e
subsd 0x50(%r13), %xmm0
ucomisd %xmm1, %xmm0
ja 0x4484e
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
movapd (%rsp), %xmm0
callq 0x3c132
leaq 0x10(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x3ce5a
testq %r14, %r14
je 0x4481f
movapd (%rsp), %xmm0
movsd %xmm0, (%r14)
testq %r15, %r15
je 0x44831
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x3c89a
testq %rbx, %rbx
je 0x44845
leaq 0x63ea3(%rip), %rsi     # 0xa86e0
movq %rbx, %rdi
callq 0x3d752
movl $0x1, %ebp
jmp 0x4484e
xorl %ebp, %ebp
leaq 0x88(%rsp), %rdi
callq 0x2a028
movl %ebp, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x44875
jmp 0x44875
jmp 0x44875
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x2a028
movq %rbx, %rdi
callq 0xe7e0