Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (2.94 MB, 633 trang )

54

Chapter 3 A Math and Geometry Primer

the distance of a point P from L is given by (P − A) × v . This expression relates to

the test for collinearity of points, where three or more points are said to be collinear

when they all lie on a line. The three points A, B, and C are collinear if and only if

the area of the triangle ABC is zero. Letting m = (B − A) × (C − A), collinearity

can be tested by checking if m = 0, or to avoid a square root if m · m is zero.

Alternatively, if (mx , my , mz ) are the components of m the points are collinear if and

only if |mx | + my + |mz | is zero.

3.6 Planes and Halfspaces

A plane in 3D space can be thought of as a ﬂat surface extending indeﬁnitely in all

directions. It can be described in several different ways. For example by:

●

Three points not on a straight line (forming a triangle on the plane)

●

A normal and a point on the plane

●

A normal and a distance from the origin

In the ﬁrst case, the three points A, B, and C allow the parametric representation

of the plane P to be given as

P(u, v) = A + u(B − A) + v(C − A).

For the other two cases, the plane normal is a nonzero vector perpendicular to any

vector in the plane. For a given plane, there are two possible choices of normal,

pointing in opposite directions. When viewing a plane speciﬁed by a triangle ABC so

that the three points are ordered counterclockwise, the convention is to deﬁne the

plane normal as the one pointing toward the viewer. In this case, the plane normal n

is computed as the cross product n = (B − A) × (C − A). Points on the same side of

the plane as the normal pointing out of the plane are said to be in front of the plane.

The points on the other side are said to be behind the plane.

Given a normal n and a point P on the plane, all points X on the plane can be

categorized by the vector X −P being perpendicular to n, indicated by the dot product

of the two vectors being zero. This perpendicularity gives rise to an implicit equation

for the plane, the point-normal form of the plane:

n · (X − P) = 0.

The dot product is a linear operator, which allows it to be distributed across a subtraction or addition. This expression can therefore be written as n · X = d, where

3.6 Planes and Halfspaces

55

d = n · P, which is the constant-normal form of the plane. When n is unit, d equals

the distance of the plane from the origin. If n is not unit, d is still the distance, but

now in units of the length of n. When not taking the absolute value, d is interpreted

as a signed distance.

The constant-normal form of the plane equation is also often written componentwise as ax + by + cz − d = 0, where n = (a, b, c) and X = (x, y, z). In this text, the

ax +by +cz −d = 0 form is preferred over its common alternative, ax +by +cz +d = 0,

as the former tends to remove a superﬂuous negation (for example, when computing

intersections with the plane).

When a plane is precomputed, it is often useful to have the plane normal be a

unit vector. The plane normal

√ is made unit by dividing n (and d, if it has already

been computed) by n = a2 + b2 + c 2 . Having a unit plane normal simpliﬁes

most operations involving the plane. In these cases, the plane equation is said to be

normalized. When a normalized plane equation is evaluated for a given point, the

obtained result is the signed distance of the point from the plane (negative if the

point is behind the plane, otherwise positive).

A plane is computed from three noncollinear points as follows:

struct Plane {

Vector n; // Plane normal. Points x on the plane satisfy Dot(n,x) = d

float d; // d = dot(n,p) for a given point p on the plane

};

// Given three noncollinear points (ordered ccw), compute plane equation

Plane ComputePlane(Point a, Point b, Point c)

{

Plane p;

p.n = Normalize(Cross(b - a, c - a));

p.d = Dot(p.n, a);

return p;

}

A plane can also be given in a parameterized form as

P(s, t) = A + s u + t v,

where u and v are two independent vectors in the plane and A is a point on the

plane.

When two planes are not parallel to each other, they intersect in a line. Similarly,

three planes — no two parallel to each other — intersect in a single point. The angle

between two intersecting planes is referred to as the dihedral angle.

56

Chapter 3 A Math and Geometry Primer

–8x + 6y = –16

(8,8)

–8x + 6y > –16

–8x + 6y < –16

(2,0)

Figure 3.12 The 2D hyperplane −8x + 6y = −16 (a line) divides the plane into two halfspaces.

Planes in arbitrary dimensions are referred to as hyperplanes: planes with one less

dimension than the space they are in. In 2D, hyperplanes correspond to a line; in 3D,

to a plane. Any hyperplane divides the space it is in into two inﬁnite sets of points on

either side of the plane. These two sets are referred to as halfspaces (Figure 3.12). If the

points on the dividing plane are considered included in the halfspace, the halfspace

is closed (otherwise, it is called open). The positive halfspace lies on the side in which

the plane normal points, and the negative halfspace on the opposite side of the plane.

A 2D halfspace is also called a halfplane.

3.7 Polygons

A polygon is a closed ﬁgure with n sides, deﬁned by an ordered set of three or more

points in the plane in such a way that each point is connected to the next (and the

last to the ﬁrst) with a line segment. For a set of n points, the resulting polygon is also

called an n-sided polygon or just n-gon. The line segments that make up the polygon

boundary are referred to as the polygon sides or edges, and the points themselves are

called the polygon vertices (singular, vertex). Two vertices are adjacent if they are joined

by an edge. Figure 3.13 illustrates the components of a polygon.

A polygon is simple if no two nonconsecutive edges have a point in common. A

simple polygon partitions the plane into two disjoint parts: the interior (the bounded

3.7 Polygons

57

Self-intersection

Edge

Vertex

Interior

Diagonal

Exterior

(a)

(b)

Figure 3.13 The components of a polygon. Polygon (a) is simple, whereas polygon (b) is

nonsimple due to self-intersection.

area covered by the polygon) and the exterior (the unbounded area outside the polygon). Usually the term polygon refers to both the polygon boundary and the interior.

A polygon diagonal is a line segment that joins two polygon vertices and lies fully

inside the polygon. A vertex is a convex vertex if the interior angle (the angle between

the sides connected to the vertex, measured on the inside of the polygon) is less than

or equal to 180 degrees (Figure 3.14a). If the angle is larger than 180 degrees, it is

instead called a concave (or reﬂex) vertex (Figure 3.14b).

A polygon P is a convex polygon if all line segments between any two points of P lie

fully inside P. A polygon that is not convex is called a concave polygon. A polygon with

Convex

vertex

(a)

Concave

vertex

(b)

Figure 3.14 (a) For a convex polygon, the line segment connecting any two points of the

polygon must lie entirely inside the polygon. (b) If two points can be found such that the

segment is partially outside the polygon, the polygon is concave.

58

Chapter 3 A Math and Geometry Primer

S

CH(S)

Figure 3.15 Convex hull of a concave polygon. A good metaphor for the convex hull is a

large rubber band tightening around the polygonal object.

one or more concave vertices is necessarily concave, but a polygon with only convex

vertices is not always convex (see the next section). The triangle is the only n-sided

polygon always guaranteed to be convex. Convex polygons can be seen as a subset

of the concept of convex point sets in the plane. A convex point set S is a set of points

wherein the line segment between any two points in S is also in S. Given a point set

S, the convex hull of S, denoted CH(S), is the smallest convex point set fully containing

S (Figure 3.15). CH(S) can also be described as the intersection of all convex point

sets containing S.

Related to the convex hull is the afﬁne hull, AH(S). The afﬁne hull is the lowest

dimensional hyperplane that contains all points of S. That is, if S contains just one

point, AH(S) is the point; if S contains two points, AH(S) is the line through them; if

S contains three noncollinear points, AH(S) is the plane determined by them; and if

S contains four (or more) non co-planar points, AH(S) is all of R3 .

In addition to the explicit vertex representation, convex polygons can also be

described as the intersection of a ﬁnite number of halfspaces. This representation

is convenient for, for example, point containment tests. For the implicit polygon representation, a point lies inside the polygon if it lies inside all halfspaces. Figure 3.16

illustrates a triangle expressed as the intersection of three halfspaces. An alternative

deﬁnition for point set convexity is therefore that a point set S is convex if and only if

S is equal to the intersection of all halfspaces that fully contain S. For polygons (and

polyhedra), this is an operative deﬁnition in the sense that it can be directly used to

implement a convexity test.

Two or more polygons can be joined at their edges to form a polygon mesh. In a

polygon mesh, the degree of a vertex corresponds to the number of edges connected

to the vertex. In this text, a mesh will be considered closed if all polygons have been

joined such that each edge is part of exactly two polygons.

3.7 Polygons

59

(5,8)

3x – 2y ≥ –1

4x + 2y ≤ 36

(1,2)

(9,0)

x + 4y ≥ 9

Figure 3.16 A convex polygon can be described as the intersection of a set of (closed) halfspaces. Here, the triangle (1, 2), (9, 0), (5, 8) is deﬁned as the intersection of the halfspaces

x + 4y ≥ 9, 4x + 2y ≤ 36, and 3x − 2y ≥ −1.

3.7.1 Testing Polygonal Convexity

Most intersection tests and other operations performed on polygons in a collision

detection system are faster when applied to convex rather than concave polygons, in

that simplifying assumptions can be made in the former case. Triangles are nice in

this respect, as they are the only type of polygon guaranteed to be convex. However, it

may be more efﬁcient to perform an intersection against a single convex n-gon rather

than against multiple triangles covering the same area. To guarantee no concave faces

are present in the collision geometry database — which would not work with a faster

test, specially written for convex faces — all faces should be veriﬁed as convex, either

at tool time or during runtime (perhaps in a debug build).

Frequently, quadrilaterals (or quads, for short) are the only primitives supported

in addition to triangles. In such situations, rather than applying a generic convexity

test for arbitrary polygons, a simpler convexity test that applies speciﬁcally to quads

can be used. Assuming all vertices of the quad ABCD lie in the same plane, the quad

is convex if and only if its two diagonals lie fully in the interior of the quad (Figure

3.17a through c). This test is equivalent to testing if the two line segments AC and BD,

corresponding to the diagonals, intersect each other. If they do, the quad is convex.

If they do not, the quad is concave or self-intersecting. If the segments are parallel

60

Chapter 3 A Math and Geometry Primer

D

A

A

D

C

D

A

B

C

D

B

B

B

C

(a)

(b)

C

(c)

A

(d)

Figure 3.17 Different types of quads. (a) A convex quad. (b) A concave quad (dart). (c) A selfintersecting quad (bowtie). (d) A degenerate quad. The dashed segments illustrate the two

diagonals of the quad. The quad is convex if and only if the diagonals transversely intersect.

and overlapping, the quad is degenerate (into a line), as illustrated in Figure 3.17d. To

avoid considering a quad with three collinear vertices convex, the segments should

only be considered intersecting if they overlap on their interior (and not on their

endpoints).

It can be shown that the intersection of the segments is equivalent to the points A

and C lying on opposite sides of the line through BD, as well as to the points B and

D lying on opposite sides of the line through AC. In turn, this test is equivalent to

the triangle BDA having opposite winding to BDC, as well as ACD having opposite

winding to ACB. The opposite winding can be detected by computing (using the cross

products) the normals of the triangles and examining the sign of the dot product

between the normals of the triangles to be compared. If the dot product is negative,

the normals point in opposing directions, and the triangles therefore wind in opposite

order. To summarize, the quad is therefore convex if

(BD × BA) · (BD × BC) < 0 and

(AC × AD) · (AC × AB) < 0.

A straightforward implementation results in the following code:

// Test if quadrilateral (a, b, c, d) is convex

int IsConvexQuad(Point a, Point b, Point c, Point d)

{

// Quad is nonconvex if Dot(Cross(bd, ba), Cross(bd, bc)) >= 0

Vector bda = Cross(d - b, a - b);

Vector bdc = Cross(d - b, c - b);

if (Dot(bda, bdc) >= 0.0f) return 0;

// Quad is now convex iff Dot(Cross(ac, ad), Cross(ac, ab)) < 0

3.7 Polygons

(a)

(b)

(c)

(d)

61

(e)

Figure 3.18 Some inputs likely to be problematic for a convexity test. (a) A line segment.

(b) A quad with two vertices coincident. (c) A pentagram. (d) A quadrilateral with two extra

vertices collinear with the top edge. (e) Thousands of cocircular points.

Vector acd = Cross(c - a, d - a);

Vector acb = Cross(c - a, b - a);

return Dot(acd, acb) < 0.0f;

}

Testing two line segments in the plane for intersection is discussed in more detail

in Section 5.1.9.1.

For general n-gons, not just quads, a straightforward solution is to, for each polygon edge, test to see if all other vertices lie (strictly) on the same side of that edge. If the

test is true for all edges, the polygon is convex, and otherwise it is concave. A separate

check for coincident vertices is required to make the test robust. However, although

easy to implement, this test is expensive for large polygons, with an O(n2 ) complexity

in the number of vertices. Polygons involved in collision detection systems are rarely

so large that the O(n2 ) complexity becomes a problem. It is easy to come up with

tests that are faster. However, many of them correctly classify only a subset of convex

polygons and incorrectly classify some nonconvex polygons (Figure 3.18). For example, a strictly convex polygon has interior angles that are all less than 180 degrees.

However, although this test is a necessary criterion for convexity it is not a sufﬁcient

one. Testing the interior angles alone would thus incorrectly conclude that a pentagram is a convex polygon (Figure 3.18c). This test only works if the polygon is known,

a priori, not to be self-intersecting.

Another basis for a convexity test is that there are only two changes in direction

along any given axis when moving from vertex to vertex of a convex polygon, accounting for wraparound from the ﬁrst to the last vertex. To detect the zigzag case illustrated

in Figure 3.18d, a test for change of direction would have to be performed for two

different directions, such as along both the x axis and the y axis. Although seemingly

robust, this test fails (for example) when all vertices of an originally convex polygon

have been projected onto a single line. It turns out that applying both of these two

alternative tests at the same time makes for a rather robust combined test, with the

two approaches “covering” for each other (so to speak). An implementation of the

combined test is described in [Schorn94].

62

Chapter 3 A Math and Geometry Primer

Vertex

Edge

Face

(a)

(b)

Figure 3.19 (a) A convex polyhedron. (b) A concave polyhedron. A face, an edge, and a

vertex have been indicated.

3.8 Polyhedra

A polyhedron is the 3D counterpart of a polygon. It is a bounded and connected region

of space in the shape of a multifaceted solid. The polyhedron boundary consists of

a number of (ﬂat) polygonal faces connected so that each polygon edge is part of

exactly two faces (Figure 3.19). Some other deﬁnitions of a polyhedron allow it to be

unbounded; that is, extending indeﬁnitely in some directions.

As for polygons, the polyhedron boundary divides space into two disjoint regions:

the interior and the exterior. A polyhedron is convex if the point set determined by its

interior and boundary is convex. A (bounded) convex polyhedron is also referred to

as a polytope. Like polygons, polytopes can also be described as the intersection of a

ﬁnite number of halfspaces.

A d-simplex is the convex hull of d+1 afﬁnely independent points in d-dimensional

space. A simplex (plural simplices) is a d-simplex for some given d. For example, the

0-simplex is a point, the 1-simplex is a line segment, the 2-simplex is a triangle, and

the 3-simplex is a tetrahedron (Figure 3.20). A simplex has the property that removing

a point from its deﬁning set reduces the dimensionality of the simplex by one.

For a general convex set C (thus, not necessarily a polytope), a point from the

set most distant along a given direction is called a supporting point of C. More

speciﬁcally, P is a supporting point of C if for a given direction d it holds that

d · P = max d · V : V ∈ C ; that is, P is a point for which d · P is maximal. Figure 3.21

illustrates the supporting points for two different convex sets. Supporting points are

sometimes called extreme points. They are not necessarily unique. For a polytope, one

of its vertices can always be selected as a supporting point for a given direction.

3.8 Polyhedra

0-simplex

1-simplex

2-simplex

63

3-simplex

Figure 3.20 Simplices of dimension 0 through 3: a point, a line segment, a triangle, and a

tetrahedron.

When a support point is a vertex, the point is commonly called a supporting

vertex.

A support mapping (or support function) is a function, SC (d), associated with a convex

set C that maps the direction d into a supporting point of C. For simple convex

shapes — such as spheres, boxes, cones, and cylinders — support mappings can be

given in closed form. For example, for a sphere C centered at O and with a radius

of r, the support mapping is given by SC (d) = O + r d/ d (Figure 3.21b). Convex

shapes of higher complexity require the support mapping function to determine a

supporting point using numerical methods.

For a polytope of n vertices, a supporting vertex is trivially found in O(n) time

by searching over all vertices. Assuming a data structure listing all adjacent vertex

neighbors for each vertex, an extreme vertex can be found through a simple hillclimbing algorithm, greedily visiting vertices more and more extreme until no vertex

more extreme can be found. This approach is very efﬁcient, as it explores only a

small corridor of vertices as it moves toward the extreme vertex. For larger polyhedra,

the hill climbing can be sped up by adding one or more artiﬁcial neighbors to the

adjacency list for a vertex. Through precomputation of a hierarchical representation

of the vertices, it is possible to locate a supporting point in O(log n) time. These

d

P = Sc(d)

P = SC(d)

C

(a)

C

(b)

Figure 3.21 (a) A supporting vertex P of polygon C with respect to the direction d. (b) A

supporting point P of circle C with respect to the direction d. In both cases, P is given by the

support mapping function SC (d).

64

Chapter 3 A Math and Geometry Primer

ideas of accelerating the search for supporting vertices are further elaborated on in

Chapter 9.

A supporting plane is a plane through a supporting point with the given direction

as the plane normal. A plane is supporting for a polytope if all polytope vertices lie

on the same side of the plane as the polytope centroid. The centroid of a polytope

deﬁned by the vertex set {P1 , P2 , . . . , Pn } is the arithmetic mean of the vertex positions:

(P1 + P2 + · · · + Pn )/n.

A separating plane of two convex sets is a plane such that one set is fully in the

positive (open) halfspace and the other fully in the negative (open) halfspace. An axis

orthogonal to a separating plane (parallel to its normal) is referred to as a separating

axis. For two nonintersecting convex sets, it can be shown that a separating axis

(or plane) always exists. The same is not necessarily true for concave sets, however.

Separating axes are revisited in more detail in Chapter 5.

The surface of a polyhedron is often referred to as a 2-manifold. This topological

term implies that the neighborhood of each point of the surface is topologically

equivalent (or homeomorphic) to a disk. A polyhedron surface being 2-manifold implies

that an edge of the polyhedron must connect to exactly two faces (or the neighborhood

of a point on the edge would not be disk-like).

The number of vertices (V ), faces (F) , and edges (E) of a polyhedron relate according to the Euler formula V + F − E = 2. It is possible to generalize the Euler formula

to hold for polyhedra with holes. The Euler formula is revisited in Chapter 12.

3.8.1 Testing Polyhedral Convexity

Similar to the convexity test for a polygon, a polyhedron P is convex if and only if for

all faces of P all vertices of P lie (strictly) on the same side of that face. A separate

test for coincident vertices and collinear edges of the polyhedron faces is required to

make the test robust, usually with some tolerance added for determining coincidency

and collinearity. The complexity of this test is O(n2 ).

A faster O(n) approach is to compute for each face F of P the centroid C of F,

and for all neighboring faces G of F test if C lies behind the supporting plane of

G. If some C fails to lie behind the supporting plane of one or more neighboring

faces, P is concave, and is otherwise assumed convex. However, note that just as the

corresponding polygonal convexity test may fail for a pentagram this test may fail for,

for example, a pentagram extruded out of its plane and capped at the ends.

3.9 Computing Convex Hulls

Among other uses, convex hulls can serve as tight bounding volumes for collision

geometry. Many different algorithms for computing convex hulls have been proposed.

Tải bản đầy đủ (.pdf) (633 trang)