gdc10 catto erin gjk

162
Computing Distance Erin Catto Blizzard Entertainment

Upload: gozul

Post on 04-Apr-2015

207 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Gdc10 Catto Erin Gjk

Computing Distance

Erin Catto

Blizzard Entertainment

Page 2: Gdc10 Catto Erin Gjk

Convex polygons

Page 3: Gdc10 Catto Erin Gjk

Closest points

Page 4: Gdc10 Catto Erin Gjk

Overlap

Page 5: Gdc10 Catto Erin Gjk

Goal

Compute the distance between convex polygons

Page 6: Gdc10 Catto Erin Gjk

Keep in mind

2D

Code not optimized

Page 7: Gdc10 Catto Erin Gjk

Approach

simple complex

Page 8: Gdc10 Catto Erin Gjk

Geometry

Page 9: Gdc10 Catto Erin Gjk

If all else fails …

Page 10: Gdc10 Catto Erin Gjk

DEMO!

Page 11: Gdc10 Catto Erin Gjk

Outline

1. Point to line segment

2. Point to triangle

3. Point to convex polygon

4. Convex polygon to convex polygon

Page 12: Gdc10 Catto Erin Gjk

Concepts

1. Voronoi regions

2. Barycentric coordinates

3. GJK distance algorithm

4. Minkowski difference

Page 13: Gdc10 Catto Erin Gjk

Point to Line SegmentSection 1

Page 14: Gdc10 Catto Erin Gjk

A line segment

A B

Page 15: Gdc10 Catto Erin Gjk

Query point

Q

A B

Page 16: Gdc10 Catto Erin Gjk

Closest point

Q

PA B

Page 17: Gdc10 Catto Erin Gjk

Projection: region A

Q

A B

Page 18: Gdc10 Catto Erin Gjk

Projection: region AB

Q

A B

Page 19: Gdc10 Catto Erin Gjk

Projection: region B

Q

A B

Page 20: Gdc10 Catto Erin Gjk

Voronoi regions

A B

region A region AB region B

Page 21: Gdc10 Catto Erin Gjk

Barycentric coordinates

G(u,v)=uA+vB

u+v=1

A G B

Page 22: Gdc10 Catto Erin Gjk

Fractional lengths

G(u,v)=uA+vB

u+v=1

A G B

u=0.5v=0.5

Page 23: Gdc10 Catto Erin Gjk

Fractional lengths

G(u,v)=uA+vB

u+v=1

A G B

u=0.75v=0.25

Page 24: Gdc10 Catto Erin Gjk

Fractional lengths

G(u,v)=uA+vB

u+v=1

AG B

u=1.25

v=-0.25

Page 25: Gdc10 Catto Erin Gjk

Unit vector

A B

B-A=

B-An

n

Page 26: Gdc10 Catto Erin Gjk

(u,v) from G

A G Buv

B-G nu=

B-A

G-A nv=

B-A

Page 27: Gdc10 Catto Erin Gjk

(u,v) from Q

B-Q nu=

B-A

A G Buv

Q-A nv=

B-A

Q

Page 28: Gdc10 Catto Erin Gjk

Voronoi region from (u,v)

A G B

u > 0 and v > 0 region AB

u > 0v > 0

Page 29: Gdc10 Catto Erin Gjk

Voronoi region from (u,v)

AG B

v <= 0 region A

u > 0

v < 0

Page 30: Gdc10 Catto Erin Gjk

Voronoi region from (u,v)

A GB

u <= 0 region B

u < 0

v > 0

Page 31: Gdc10 Catto Erin Gjk

Closet point algorithm

input: A, B, Q

compute u and v

if (u <= 0)

P = B

else if (v <= 0)

P = A

else

P = u*A + v*B

Page 32: Gdc10 Catto Erin Gjk

Point to TriangleSection 2

Page 33: Gdc10 Catto Erin Gjk

Triangle

A

B

C

Page 34: Gdc10 Catto Erin Gjk

Closest feature: vertex

A

P=B

C

Q

Page 35: Gdc10 Catto Erin Gjk

Closest feature: edge

A

C

Q

B

P

Page 36: Gdc10 Catto Erin Gjk

Closest feature: interior

A

C

B

P=Q

Page 37: Gdc10 Catto Erin Gjk

Voronoi regions

A

B

C

Region AB

Region CA

Region BC

Region ABC

Region A

Region B

Region C

Page 38: Gdc10 Catto Erin Gjk

3 line segments

A

B

C

AB ABu ,v

CA CAu ,v

BC BCu ,v

Q

Page 39: Gdc10 Catto Erin Gjk

Vertex regions

A

B

C

AB

BC

u 0

v 0

CA

AB

u 0

v 0

BC

CA

u 0

v 0

Using line segment uv’s

Page 40: Gdc10 Catto Erin Gjk

Edge regions

A

B

C

AB

AB

u 0

v 0

?

Line segment uv’s are not sufficient

Page 41: Gdc10 Catto Erin Gjk

Interior region

A

B

C

?

Line segment uv’s don’t help at all

Page 42: Gdc10 Catto Erin Gjk

Triangle barycentric coordinates

Q uA vB wC

1wvu

A

C

B

Q

Page 43: Gdc10 Catto Erin Gjk

Linear algebra solution

x x x x

y y y y

A B C u Q

A B C v = Q

1 1 1 w 1

Page 44: Gdc10 Catto Erin Gjk

Fractional areas

A

B

CC

Q

Page 45: Gdc10 Catto Erin Gjk

The barycenctric coordinates are the fractional areas

B

A

C

Q

w

u Area(BCQ)

C

v

u

v Area(CAQ)

w Area(ABQ)

Page 46: Gdc10 Catto Erin Gjk

Barycentric coordinates

CC

A

B

w 0

v 0

u 1Q

Page 47: Gdc10 Catto Erin Gjk

Barycentric coordinates

area(QBC)u

area(ABC)

area(QCA)v

area(ABC)

area(QAB)w

area(ABC)

Page 48: Gdc10 Catto Erin Gjk

Barycentric coordinates are fractional

line segment : fractional length

triangles : fractional area

tetrahedrons : fractional volume

Page 49: Gdc10 Catto Erin Gjk

Computing Area

A

C

B

1

signed area= cross B-A,C-A2

Page 50: Gdc10 Catto Erin Gjk

Q outside the triangle

C

Q

C

A

B

Page 51: Gdc10 Catto Erin Gjk

Q outside the triangle

C

0v

0w

v+w>1

C

A

B

Q

Page 52: Gdc10 Catto Erin Gjk

Q outside the triangle

C

0u

C

A

B

Q

Page 53: Gdc10 Catto Erin Gjk

Voronoi versus Barycentric

Voronoi regions != barycentric coordinate regions

The barycentric regions are still useful

Page 54: Gdc10 Catto Erin Gjk

Barycentric regions of a triangle

A

B

C

Page 55: Gdc10 Catto Erin Gjk

Interior

A

B

C 0w0,v0,u

Q

Page 56: Gdc10 Catto Erin Gjk

Negative u

A

B

C

0u

Q

Page 57: Gdc10 Catto Erin Gjk

Negative v

A

B

C 0v

Q

Page 58: Gdc10 Catto Erin Gjk

Negative w

A

B

C

0w

Q

Page 59: Gdc10 Catto Erin Gjk

The uv regions are not exclusive

A

B

C

Q

P

Page 60: Gdc10 Catto Erin Gjk

Finding the Voronoi region

Use the barycentric coordinates to identify the Voronoi region

Coordinates for the 3 line segments and the triangle

Regions must be considered in the correct order

Page 61: Gdc10 Catto Erin Gjk

First: vertex regions

A

B

C

AB

BC

u 0

v 0

AB

CA

v 0

u 0

BC

CA

u 0

v 0

Page 62: Gdc10 Catto Erin Gjk

Second: edge regions

A

B

C

AB

AB

u 0

v 0

?

Page 63: Gdc10 Catto Erin Gjk

Second: edge regions solved

A

B

C

AB

AB

ABC

u 0

v 0

w 0

Page 64: Gdc10 Catto Erin Gjk

Third: interior region

A

B

C

ABC

ABC

ABC

u > 0

v > 0

w > 0

Page 65: Gdc10 Catto Erin Gjk

Closest point

Find the Voronoi region for point Q

Use the barycentric coordinates to compute the closest point Q

Page 66: Gdc10 Catto Erin Gjk

Example 1

A

B

C

Q

Page 67: Gdc10 Catto Erin Gjk

Example 1

A

B

C

uAB <= 0

Q

Page 68: Gdc10 Catto Erin Gjk

Example 1

A

B

C

uAB <= 0 and vBC <= 0

Q

Page 69: Gdc10 Catto Erin Gjk

Example 1

A

P=B

C Conclusion:P = B

Q

Page 70: Gdc10 Catto Erin Gjk

Example 2

A

B

C

Q

Page 71: Gdc10 Catto Erin Gjk

Example 2

A

B

C

Q is not in any vertex region

Q

Page 72: Gdc10 Catto Erin Gjk

Example 2

A

B

C

uAB > 0

Q

Page 73: Gdc10 Catto Erin Gjk

Example 2

A

B

CuAB > 0 and vAB > 0

Q

Page 74: Gdc10 Catto Erin Gjk

Example 2

A

B

CuAB > 0 and vAB > 0and wABC <= 0

Q

Page 75: Gdc10 Catto Erin Gjk

Example 2

A

B

C

Conclusion:P = uAB*A + vAB*B

P

Q

Page 76: Gdc10 Catto Erin Gjk

Implementation

input: A, B, C, Q

compute uAB, vAB, uBC, vBC, uCA, vCA

compute uABC, vABC, wABC

// Test vertex regions

// Test edge regions

// Else interior region

Page 77: Gdc10 Catto Erin Gjk

Testing the vertex regions

// Region A

if (vAB <= 0 && uCA <= 0)

P = A

return

// Similar tests for Region B and C

Page 78: Gdc10 Catto Erin Gjk

Testing the edge regions

// Region AB

if (uAB > 0 && vAB > 0 && wABC <= 0)

P = uAB * A + vAB * B

return

// Similar for Regions BC and CA

Page 79: Gdc10 Catto Erin Gjk

Testing the interior region

// Region ABC

assert(uABC > 0 && vABC > 0 && wABC > 0)

P = Q

return

Page 80: Gdc10 Catto Erin Gjk

Point to Convex PolygonSection 3

Page 81: Gdc10 Catto Erin Gjk

Convex polygon

A

B

C

DE

Page 82: Gdc10 Catto Erin Gjk

Polygon structure

struct Polygon{Vec2* points;int count;

};

Page 83: Gdc10 Catto Erin Gjk

Convex polygon: closest point

Q

A

B

C

DE

Query point Q

Page 84: Gdc10 Catto Erin Gjk

Convex polygon: closest point

P

Closest point Q

A

B

C

DE

Q

Page 85: Gdc10 Catto Erin Gjk

How do we compute P?

Page 86: Gdc10 Catto Erin Gjk

What do we know?

Closest point to point

Closest point to line segment

Closest point to triangle

Page 87: Gdc10 Catto Erin Gjk

Simplex

0-simplex 1-simplex 2-simplex

Page 88: Gdc10 Catto Erin Gjk

Idea: inscribe a simplex

A

B

C

DE

Q

Page 89: Gdc10 Catto Erin Gjk

Idea: closest point on simplex

A

B

P = C

DE

Q

Page 90: Gdc10 Catto Erin Gjk

Idea: evolve the simplex

A

B

C

DE

Q

Page 91: Gdc10 Catto Erin Gjk

Simplex vertex

struct SimplexVertex{

Vec2 point;int index;float u;

};

Page 92: Gdc10 Catto Erin Gjk

Simplex

struct Simplex{SimplexVertex vertexA;SimplexVertex vertexB;SimplexVertex vertexC;int count;

};

Page 93: Gdc10 Catto Erin Gjk

We are onto a winner!

Page 94: Gdc10 Catto Erin Gjk

The GJK distance algorithm

Computes the closest point on a convex polygon

Invented by Gilbert, Johnson, and Keerthi

Page 95: Gdc10 Catto Erin Gjk

The GJK distance algorithm

Inscribed simplexes

Simplex evolution

Page 96: Gdc10 Catto Erin Gjk

Starting simplex

Start with arbitraryvertex. Pick E.

This is our startingsimplex.

A

B

C

DE

Q

Page 97: Gdc10 Catto Erin Gjk

Closest point on simplex

P is the closest point.

A

B

C

D

Q

P=E

Page 98: Gdc10 Catto Erin Gjk

Search vector

Draw a vectorfrom P to Q.

Call this vector d.

d

A

B

C

DP=E

Q

Page 99: Gdc10 Catto Erin Gjk

Find the support point

Find the vertex on polygon furthest in direction d.

This is the supportpoint.d

A

B

C

D

Q

P=E

Page 100: Gdc10 Catto Erin Gjk

Support point code

int Support(const Polygon& poly, const Vec2& d){int index = 0;float maxValue = Dot(d, poly.points[index]);for (int i = 1; i < poly.count; ++i){float value = Dot(d, poly.points[i]);if (value > maxValue){index = i;maxValue = value;

}}return index;

}

Page 101: Gdc10 Catto Erin Gjk

Support point found

C is the supportpoint.

d

A

B

C

DE

Q

Page 102: Gdc10 Catto Erin Gjk

Evolve the simplex

Create a line segment CE.

We now have a1-simplex.

A

B

C

DE

Q

Page 103: Gdc10 Catto Erin Gjk

Repeat the process

Find closest pointP on CE.

A

B

C

DE

P

Q

Page 104: Gdc10 Catto Erin Gjk

New search direction

Build d as a line pointing from P to Q.

A

B

C

DE

d P

Q

Page 105: Gdc10 Catto Erin Gjk

New support point

D is the support point.

A

B

C

DE

d

Q

Page 106: Gdc10 Catto Erin Gjk

Evolve the simplex

Create triangle CDE.

This is a2-simplex.A

B

C

DE

Q

Page 107: Gdc10 Catto Erin Gjk

Closest point

Compute closest point on CDE to Q.

A

B

C

DE

P

Q

Page 108: Gdc10 Catto Erin Gjk

E is worthless

Closest point is on CD.

E does not contribute.

A

B

C

DE

P

Q

Page 109: Gdc10 Catto Erin Gjk

Reduced simplex

We dropped E,so we now havea 1-simplex.

A

B

C

DE

P

Q

Page 110: Gdc10 Catto Erin Gjk

Termination

Compute support point in direction d.

We find either C or D. Since this is a repeat, we are done.

Q

A

B

C

DE

P

d

Page 111: Gdc10 Catto Erin Gjk

GJK algorithm

Input: polygon and point Q

pick arbitrary initial simplex S

loop

compute closest point P on S

cull non-contributing vertices from S

build vector d pointing from P to Q

compute support point in direction d

add support point to S

end

Page 112: Gdc10 Catto Erin Gjk

DEMO!!!

Page 113: Gdc10 Catto Erin Gjk

Numerical Issues

Search direction

Termination

Poorly formed polygons

Page 114: Gdc10 Catto Erin Gjk

A bad direction

Q

A

B

C

DE

P

d can be built from PQ.

Due to round-off:

dot(Q-P, C-E) != 0

d

Page 115: Gdc10 Catto Erin Gjk

A real example in single precision

Line Segment

A = [0.021119118, 79.584320]

B = [0.020964622, -31.515678]

Query Point

Q = [0.0 0.0]

Barycentric Coordinates

(u, v) = (0.28366947, 0.71633047)

Search Direction

d = Q – P = [-0.021008447, 0.0]

dot(d, B – A) = 3.2457051e-006

Page 116: Gdc10 Catto Erin Gjk

Small errors matter

numerical directionexact direction

correctsupport

wrongsupport

Page 117: Gdc10 Catto Erin Gjk

An accurate search direction

A

B

C

DE

d

Directly compute a vector perpendicular toCE.

d = cross(C-E,z)

Where z is normalto the plane.

Q

Page 118: Gdc10 Catto Erin Gjk

The dot product is exactly zero

x y

-y x

-xy+yx=0

e

d

e d

edge direction:

search direction:

dot product:

Page 119: Gdc10 Catto Erin Gjk

Fixing the sign

A

B

C

DE

d

Flip the sign of d so that:

dot(d, Q – C) > 0

Perk: no dividesQ

Page 120: Gdc10 Catto Erin Gjk

Termination conditions

}

Page 121: Gdc10 Catto Erin Gjk

Case 1: repeated support point

A

B

C

DE

d

P

Q

Page 122: Gdc10 Catto Erin Gjk

Case 2: containment

We find a 2-simplex and all vertices contribute.

A

B

C

DE

P=Q

Page 123: Gdc10 Catto Erin Gjk

Case 3a: vertex overlap

We will computed=Q-P as zero.

So we terminate ifd=0.

A

B

C

EP=Q=D

Page 124: Gdc10 Catto Erin Gjk

Case 3b: edge overlap

d will have an arbitrary sign.

A

B

E

P=Q

C

D

d

Page 125: Gdc10 Catto Erin Gjk

Case 3b: d points left

If we search left, we get a duplicate support point.In this case we terminate.

A

B

E

P=Q

C

D

d

Page 126: Gdc10 Catto Erin Gjk

Case 3b: d points right

If we search right, we get a new support point (A).

A

B

E

C

D

d

Q=P

Page 127: Gdc10 Catto Erin Gjk

Case 3b: d points right

But then we get back the same P, and then the same d.

Soon, we detect a repeated support point or detect containment.

A

B

E

C

D

d

P=Q

Page 128: Gdc10 Catto Erin Gjk

Case 4: interior edge

d will have an arbitrary sign.

A

B

E

P=Q

C

D

d

Page 129: Gdc10 Catto Erin Gjk

Case 4: interior edge

Similar to Case 3b

A

B

E

P=Q

C

D

d

Page 130: Gdc10 Catto Erin Gjk

Termination in 3D

May require new/different conditions

Check for distance progression

Page 131: Gdc10 Catto Erin Gjk

Non-convex polygon

Vertex B is non-convex

A

E

C

D

B

Page 132: Gdc10 Catto Erin Gjk

Non-convex polygon

B is never a support point

AB

E

C

D

Page 133: Gdc10 Catto Erin Gjk

Collinear vertices

B, C, and D are collinear

A

E

B

D

C

Page 134: Gdc10 Catto Erin Gjk

Collinear vertices

2-simplex BCD

A

E

B

D

CQ

Page 135: Gdc10 Catto Erin Gjk

Collinear vertices

area(BCD) = 0

A

E

B

D

CQ

Page 136: Gdc10 Catto Erin Gjk

Convex Polygon to Convex PolygonSection 4

Page 137: Gdc10 Catto Erin Gjk

X Y

Closest point between convex polygons

Page 138: Gdc10 Catto Erin Gjk

What do we know?

GJK

Page 139: Gdc10 Catto Erin Gjk

What do we need to know?

???

Page 140: Gdc10 Catto Erin Gjk

Idea

Convert polygon to polygon into point to polygon

Use GJK to solve point to polygon

Page 141: Gdc10 Catto Erin Gjk

Minkowski difference

YX Y-X Z

Page 142: Gdc10 Catto Erin Gjk

Minkowski difference definition

j i i jZ = y - x : x X, y Y

Page 143: Gdc10 Catto Erin Gjk

Building the Minkowski difference

Input: polygon X and Y

array points

for all xi in X

for all yj in Y

points.push_back(yj – xi)

end

end

polygon Z = ConvexHull(points)

Page 144: Gdc10 Catto Erin Gjk

Example point cloud

YX Y-X

Compute Yi – Xj for i = 1 to 4 and j = 1 to 3

Page 145: Gdc10 Catto Erin Gjk

Building the convex hull

Compute the convex hull by shrink wrapping the points.

Page 146: Gdc10 Catto Erin Gjk

Building the convex hull

Compute the convex hull by shrink wrapping the points.

Page 147: Gdc10 Catto Erin Gjk

Building the convex hull

Compute the convex hull by shrink wrapping the points.

Page 148: Gdc10 Catto Erin Gjk

Building the convex hull

Compute the convex hull by shrink wrapping the points.

Page 149: Gdc10 Catto Erin Gjk

Building the convex hull

Compute the convex hull by shrink wrapping the points.

Page 150: Gdc10 Catto Erin Gjk

Building the convex hull

Compute the convex hull by shrink wrapping the points.

Page 151: Gdc10 Catto Erin Gjk

Building the convex hull

Compute the convex hull by shrink wrapping the points.

Page 152: Gdc10 Catto Erin Gjk

Building the convex hull

Compute the convex hull by shrink wrapping the points.

Page 153: Gdc10 Catto Erin Gjk

Building the convex hull

Compute the convex hull by shrink wrapping the points.

Page 154: Gdc10 Catto Erin Gjk

Z

The final polygon

Page 155: Gdc10 Catto Erin Gjk

Z

Property 1: distances are equal

YX

distance(X,Y) == distance(O, Y-X)

O

Page 156: Gdc10 Catto Erin Gjk

Property 2: support points

support(Z, d) = support(Y, d) – support(X, -d)

ZYX O

dd-d

Page 157: Gdc10 Catto Erin Gjk

Convex Hull?

Page 158: Gdc10 Catto Erin Gjk

Modifying GJK

Change the support function

Simplex vertices hold two indices

Page 159: Gdc10 Catto Erin Gjk

Closest point on polygons

Use the barycentric coordinates to compute the closest points on X and Y

See the demo code for details

Page 160: Gdc10 Catto Erin Gjk

DEMO!!!

Download: box2d.org

Page 161: Gdc10 Catto Erin Gjk

Further reading

Collision Detection in Interactive 3D Environments, Gino van den Bergen, 2004

Real-Time Collision Detection, ChristerEricson, 2005

Implementing GJK: http://mollyrocket.com/849, Casey Muratori.

Page 162: Gdc10 Catto Erin Gjk

Box2D

An open source 2D physics engine

http://www.box2d.org

Written in C++