Computational Geometry Part 2: Line Sweep, Convex Hulls Lucca - - PowerPoint PPT Presentation

computational geometry
SMART_READER_LITE
LIVE PREVIEW

Computational Geometry Part 2: Line Sweep, Convex Hulls Lucca - - PowerPoint PPT Presentation

Computational Geometry Part 2: Line Sweep, Convex Hulls Lucca Siaudzionis and Jack Spalding-Jamieson 2020/02/27 University of British Columbia Announcements A3 is due Sundayyyyy! Project topics are due Tuesday! Presentation dates


slide-1
SLIDE 1

Computational Geometry

Part 2: Line Sweep, Convex Hulls

Lucca Siaudzionis and Jack Spalding-Jamieson 2020/02/27

University of British Columbia

slide-2
SLIDE 2

Announcements

  • A3 is due Sundayyyyy!
  • Project topics are due Tuesday!
  • Presentation dates will be on March 31st and April 2nd.
  • If you can’t present in one of those two days, let us know by March 4th so we can schedule

you on the other (with a decent reason).

  • Otherwise, dates will be randomly assigned.
  • Extra office hours tomorrow from 2-4PM in ICCSX237.

1

slide-3
SLIDE 3

Discussion Problem: Remember How to do Line Sweep?

Jack is constantly busy with meetings that take up all his time. Many of his meetings even

  • verlap!

Given a list of 1 ≤ n ≤ 106 meetings along with their start and end times, find out how much time Jack spends overwhelmed with meetings.

2

slide-4
SLIDE 4

Discussion Problem: Remember How to do Line Sweep? – Insight

We can process the “events” (start and end times) in increasing order (the “x”-coordinate in Line Sweep), keeping track of how many intervals are currently under the sweep line.

3

slide-5
SLIDE 5

Line sweep: General Plan

The general method for solving sweep problems:

  • Identify important “events” that occur as we move from left to right.
  • Keep track of some auxiliary data structure that lets us
  • find the answer, and
  • update efficiently at each event.

4

slide-6
SLIDE 6

Line Sweep: Pachinko

Given N ≤ 105 non-intersecting, non-horizontal line segment obstacles, where does the pinball end up?

5

slide-7
SLIDE 7

Line Sweep: Pachinko

We only need to know for each segment, which “next segment” the pinball will fall onto. We’ll find these by sweeping from left to right.

6

slide-8
SLIDE 8

Line Sweep: Pachinko

We only need to know for each segment, which “next segment” the pinball will fall onto. We’ll find these by sweeping from left to right.

7

slide-9
SLIDE 9

Line Sweep: Pachinko

We only need to know for each segment, which “next segment” the pinball will fall onto. We’ll find these by sweeping from left to right.

8

slide-10
SLIDE 10

Line Sweep: Pachinko

Data structure: A set of lines, sorted by their order on the y-axis. Events: The line segment endpoints. Actions:

  • Left endpoint: Insert line into the set
  • Right endpoint: Remove line from the set
  • Lower endpoint of segment: Query for the next line below the point

9

slide-11
SLIDE 11

Convex Sets

A set S is convex ⇔ ∀x, y ∈ S, 0 ≤ λ ≤ 1, λx + (1 − λ)y ∈ S ⇔ the line segment x → y is inside the set

Figure 1: In a convex set, the line segment between any 2 points lies in a set

10

slide-12
SLIDE 12

Convex Hulls: Definition

A convex hull of a set is the smallest convex set containing the set.

Figure 2: Some sets and their convex hulls

Why do we care about convex hulls? They give us nice convex polygons, which we can

  • ptimize over easier.

11

slide-13
SLIDE 13

Convex Hulls: Finite Point Sets

If we have a finite set of points, we define the following about a point p and the convex hull H:

  • p may be in the interior, i.e. p is not found on H at all.
  • p may be a vertex of the convex hull, i.e. removing p would make the convex hull smaller.
  • p could lie on an edge in the convex hull.

When finding a convex hull, we need to know which of these sets we’re looking for (just the vertices? or all the points on the convex hull).

12

slide-14
SLIDE 14

Convex Hulls: Applications

Convex Hulls can be used for many real-world applications, such as:

  • In game programming, they can be used to speed up collision-detection, similarly to

bounding-boxes.

  • Can be used to find the shortest paths around physical objects (remember the circle

problem from last class?).

  • And much more...

13

slide-15
SLIDE 15

Convex Hulls: Gift Wrapping

How do we find the convex hull of a finite point-set with n points?

14

slide-16
SLIDE 16

Convex Hulls: Gift Wrapping

How do we find the convex hull of a finite point-set with n points? We can use the gift wrapping algorithm:

  • Start at some point that is guaranteed to be a vertex in the convex hull, e.g. the leftmost

point (break ties by height).

  • Look at all of the other points, and choose the one that maximizes the angle with the line

segment pointing downwards from our original point.

  • The point chosen is the next point in the convex hull.
  • Do this iteratively until we hit a point already added, but with the most recent line

segment added to the convex hull. Overall runtime: O(n2)

14

slide-17
SLIDE 17

Convex Hulls: Gift Wrapping Example (1)

15

slide-18
SLIDE 18

Convex Hulls: Gift Wrapping Example (2)

16

slide-19
SLIDE 19

Convex Hulls: Gift Wrapping Example (3)

17

slide-20
SLIDE 20

Convex Hulls: Gift Wrapping Example (4)

18

slide-21
SLIDE 21

Convex Hulls: Gift Wrapping Example (5)

19

slide-22
SLIDE 22

Convex Hulls: Gift Wrapping Example (6)

20

slide-23
SLIDE 23

Convex Hulls: Gift Wrapping Example (7)

21

slide-24
SLIDE 24

Convex Hulls: Gift Wrapping Example (8)

22

slide-25
SLIDE 25

Convex Hulls: Gift Wrapping Example (9)

23

slide-26
SLIDE 26

Convex Hulls: Gift Wrapping Example (10)

24

slide-27
SLIDE 27

Convex Hulls (Aside): Gift Wrapping – Output Sensitivity

Although the runtime of gift wrapping is in fact a tight Θ(n2), we can actually give a better bound! This is using something called output sensitivity: If we know that there will be at most h < n points on the eventual convex hull, we can actually bound the algorithm with complexity O(nh).

25

slide-28
SLIDE 28

Convex Hulls: Faster Algorithms

Are there faster algorithms for solving this?

26

slide-29
SLIDE 29

Convex Hulls: Faster Algorithms

Are there faster algorithms for solving this?

  • Yes. Lots. No less than eight:
  • Graham Scan (O(n log n))
  • Monotone Chain/Andrew’s Algorithm (O(n log n))
  • Quickhull (O(n log n) average case)
  • Divide & Conquer via axis split (O(n log n)), extendable to 3D!
  • Divide & Conquer with arbitrary merge and rotating calipers (O(n log n))
  • Kallay’s incremental algorithm (O(n log n))
  • Kirkpatrick-Seidel Algorithm (O(n log h)), aka the ”ultimate planar convex hull algorithm”
  • Chan’s Algorithm (O(n log h))

We’re going to talk about Monotone Chain/Andrew’s Algorithm, although the algorithm is very similar to Graham’s algorithm.

26

slide-30
SLIDE 30

Monotone Chain Algorithm

Step 1: scan from left to right, “wrap” around the top Step 2: scan from right to left, “wrap” around the bottom.

Figure 3: Upper and lower hulls built by monotone chain algorithm

27

slide-31
SLIDE 31

Monotone Chain Algorithm

28

slide-32
SLIDE 32

Monotone Chain Algorithm

28

slide-33
SLIDE 33

Monotone Chain Algorithm

28

slide-34
SLIDE 34

Monotone Chain Algorithm

28

slide-35
SLIDE 35

Monotone Chain Algorithm

28

slide-36
SLIDE 36

Monotone Chain Algorithm

28

slide-37
SLIDE 37

Monotone Chain Algorithm

28

slide-38
SLIDE 38

Monotone Chain Algorithm

28

slide-39
SLIDE 39

Monotone Chain Algorithm

28

slide-40
SLIDE 40

Monotone Chain Algorithm

28

slide-41
SLIDE 41

Monotone Chain Algorithm

28

slide-42
SLIDE 42

Monotone Chain Algorithm

28

slide-43
SLIDE 43

Monotone Chain Algorithm

28

slide-44
SLIDE 44

Monotone Chain Algorithm

28

slide-45
SLIDE 45

Monotone Chain Algorithm

28

slide-46
SLIDE 46

Monotone Chain Algorithm

28

slide-47
SLIDE 47

Monotone Chain Algorithm

28

slide-48
SLIDE 48

Monotone Chain Algorithm

28

slide-49
SLIDE 49

Monotone Chain Algorithm

28

slide-50
SLIDE 50

Monotone Chain Algorithm

Algorithm summary

  • Sort all points by x-coordinate
  • Start from leftmost point (i.e. smallest x-coordinate)
  • If there are ties, you can take the uppermost one (largest y-coordinate)
  • Iteratively add points into the hull
  • If adding a point causes CCW turn, pop points from hull until this is no longer the case

before adding!

  • Repeat the above steps from right to left.

29

slide-51
SLIDE 51

Monotone Chain Algorithm

Edge cases

  • Convex hull of 1, 2, 3 points
  • Collinear points

30

slide-52
SLIDE 52

Monotone Chain Algorithm

Time complexity analysis

  • Sort all points by x-coordinate: O(n log n)
  • Each point is added and removed at most once: O(n)

⇒ Time complexity: O(n log n) Without looking at output sensitivity, can we do better?

31

slide-53
SLIDE 53

Monotone Chain Algorithm

Time complexity analysis

  • Sort all points by x-coordinate: O(n log n)
  • Each point is added and removed at most once: O(n)

⇒ Time complexity: O(n log n) Without looking at output sensitivity, can we do better? With angle-based comparison, we can use the sorting lower bound: Arrange n points on a circle ⇒ equivalent to sorting in some sense, Ω(n log n).

31

slide-54
SLIDE 54

Discussion Problem: Visibility

Input: A point-set D of 1 ≤ n ≤ 105 key-points representing some convex geometric object, guaranteed to include all boundary points. Additionally, there are 1 ≤ q ≤ 105 queries. For each query, a point p is provided, guaranteed to be outside the boundary of the object. Output: For each query, output the angles over which a camera positioned at point p would not be able to see past the object.

P

32

slide-55
SLIDE 55

Discussion Problem: Visibility – Insight (1)

Observation 1: The solution for each query lies on the convex hull. Observation 2: Pick an arbitrary point on the hull, draw a line through, then one extremum is

  • n each side of the line.

P

33

slide-56
SLIDE 56

Discussion Problem: Visibility – Insight (2)

Binary search to find start/end vertices on each half of the hull, and then binary search on each half of the hull for when P → pi → pi+1 changes from CCW to CW (or vice versa). Time complexity: O(N log N + Q log N) This problem has many applications, including game programming, graphics, and more broad computational geometry problems (such as euclidean shortest path problems).

34

slide-57
SLIDE 57

Jack’s Weekend Recommendation

Metal Gear Solid (This game actually uses the visibility algorithm we just talked about.)

35

slide-58
SLIDE 58

Lucca’s Weekend Recommendation

The Meyerowitz Stories (New and Selected) (This film does not use the visibility algorithm we just talked about.)

36