Graphics Reference
In-Depth Information
// Intersect segment ab against plane of triangle def. If intersecting,
// return t value and position q of intersection
int IntersectSegmentPlane(Point a, Point b, Point d, Point e, Point f,
float &t, Point &q)
{
Plane p;
p.n = Cross(e - d,f-d);
p.d = Dot(p.n, d);
return IntersectSegmentPlane(a, b, p, t, q);
}
5.3.2
Intersecting Ray or Segment Against Sphere
Let a ray be given by
R
(
t
)
=
P
+
t
d
,
t
≥
0, where
P
is the ray origin and
d
a
normalized direction vector,
d
=
1. If
R
(
t
) describes a segment rather than a ray,
r
2
,
where
C
is the sphere center and
r
its radius. To find the
t
value at which the ray
intersects the surface of the sphere,
R
(
t
) is substituted for
X
, giving
then 0
≤
t
≤
t
max
. Let the sphere boundary be defined by (
X
−
C
)
·
(
X
−
C
)
=
r
2
.
(
P
+
t
d
−
C
)
·
(
P
+
t
d
−
C
)
=
Let
m
=
P
−
C
, then:
r
2
(
m
+
t
d
)
·
(
m
+
t
d
)
=
⇔
(substituting
m
=
P
−
C)
d
)
t
2
r
2
(
d
·
+
2(
m
·
d
)
t
+
(
m
·
m
)
=
⇔
(expanding the dot product)
t
2
r
2
+
2(
m
·
d
)
t
+
(
m
·
m
)
−
=
0
1;
canonical form for quadratic equation)
(simplifying
d
·
d
=
This is a quadratic equation in
t
. For
the quadratic formula
t
2
+
2
bt
+
c
=
0, the
√
b
2
r
2
.
Solving the quadratic has three outcomes, categorized by the discriminant
d
solutions are given by
t
=−
b
±
−
c
. Here,
b
=
m
·
d
and
c
=
(
m
·
m
)
−
=
b
2
0, there are no real roots, which corresponds to the ray missing the
sphere completely. If
d
−
c
.If
d
<
=
0, there is one real (double) root, corresponding to the
ray hitting the sphere tangentially in a point. If
d
>
0, there are two real roots
and the ray intersects the sphere twice: once entering and once leaving the sphere
boundary. In th
e latte
r case, the smaller intersection
t
value is the relevant one, given
by
t
√
b
2
=−
−
−
c
. However, it is important to distinguish the false intersection
case of the ray starting outside the sphere and pointing away from it, resulting in an
b