Rating changes for last rounds are temporarily rolled back. They will be returned soon. ×

3D Convex Hull in $$$O(n\log n)$$$

Revision en1, by Monogon, 2020-08-22 00:52:48

Warning: The following contains graphic depictions of geometry, precision errors, and degenerate cases. Viewer discretion is advised.

Prerequisites

I assume the reader is familiar with:

  • 2D Convex Hulls
  • 3D Vector Operations (dot and cross products)

Introduction

Recall that in the 2D convex hull problem, you are given a set of 2D points, and you must compute the smallest convex polygon containing all the given points. By convex, we mean that for any two points $$$A$$$ and $$$B$$$ inside the polygon, the entire line segment $$$AB$$$ is also inside the polygon.

The problem in 3D is completely analogous. You are given $$$n$$$ 3D points, and you must compute the smallest convex polyhedron containing all the given points. Similarly, a polyhedron is convex if for any two points $$$A$$$ and $$$B$$$ inside the polyhedron, the line segment $$$AB$$$ is also inside the polyhedron.

In the 2D case, it is more obvious what the output is. We can simply output a circular list of vertices on the boundary of the polygon. But how do we represent a polyhedron? Well, we can represent it as a list of triangular faces. A non-triangular face can always be divided into triangles, so this requirement isn't limiting.

In the 2D case, there are some degenerate cases that are easy to miss if you're not careful. For example, if there are 3 collinear points, the polygon might have two adjacent sides of the same slope, or you might combine them into one segment. Or if two points have the same $$$x$$$ coordinate, you need to handle this carefully when sorting the points by $$$x$$$ coordinate. Or if all the points are on the same line, then the convex hull isn't even a non-degenerate polygon!

Now, degenerate cases are bad enough with two dimensions. When you enter the third dimension, it only gets worse. What if four points lie on the same plane? Since we are requiring triangular faces, we could triangulate this in multiple ways. Or maybe we could choose to have one triangle of three points and the forth point lies inside this triangle, so we ignore it. What if all the points are on the same line? Or on the same plane even? Then the convex hull isn't a non-degenerate polyhedron. I will ignore such degenerate cases for now, and revisit them when applicable.

Brute Force Algorithm in $$$O(n^4)$$$

Suppose that $$$n$$$ is very small, and we are guaranteed that no four points are coplanar. Then how can we compute the 3D convex hull in the dumbest way possible?

We could simply consider every triplet of three points $$$(\vec{a}, \vec{b}, \vec{c})$$$, and check if they create a triangular face on the convex hull. In order for it to be a face, the remaining points must "see" the same side of the triangle. In other words, if we consider the plane containing this triangle, the remaining points should lie on the same side of the plane. To compute which side of a plane a point $$$\vec{p}$$$ is on, we can first take a vector $$$\vec{v}=(\vec{b}-\vec{a})\times (\vec{c}-\vec{a})$$$ orthogonal to the plane. Then the sign of $$$(\vec{p}-\vec{a})\cdot \vec{v}$$$ tells us the side of the plane. In particular, a result of $$$0$$$ tells us that $$$\vec{p}$$$ lies on the plane.

For each triplet, we perform this check with all points, so the total time complexity is $$$O(n^4)$$$. Because of its simplicity, this should be the approach when the constraints allow it. And by examining the brute force case, we learned how to perform the most fundamental operation in any 3D convex hull algorithm: checking which side of a plane a point is on.

Incremental Algorithm in $$$O(n^2)$$$

Randomized Incremental Algorithm in $$$O(n\log n)$$$

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en10 English Monogon 2020-08-22 20:09:06 129
en9 English Monogon 2020-08-22 20:05:52 48 Tiny change: 'Diagram)\n' -> 'Diagram)\n- https://open.kattis.com/problems/starsinacan\n'
en8 English Monogon 2020-08-22 19:47:21 0 (published)
en7 English Monogon 2020-08-22 19:44:53 3793
en6 English Monogon 2020-08-22 19:38:35 110
en5 English Monogon 2020-08-22 19:29:19 12495 Tiny change: 'tation">\n\n~~~~~\' -> 'tation">\nWarning: \n\n~~~~~\'
en4 English Monogon 2020-08-22 19:02:59 3134 Tiny change: 'oints.\n\nA simi' -> 'oints.\n\n![ ](https://i.imgur.com/GBhUyqf.jpg)\n\nA simi'
en3 English Monogon 2020-08-22 04:00:08 3189 Tiny change: 'ee it.\n\n"_Wait a minute_," I hear yo' -> 'ee it.\n\n_"Wait a minute,"_ I hear yo'
en2 English Monogon 2020-08-22 01:59:28 1254 Tiny change: '(n^2)$\n\n\n\n### Ra' -> '(n^2)$\n\n![ ](https://imgur.com/a/D7mubNu)\n\n### Ra'
en1 English Monogon 2020-08-22 00:52:48 3660 Initial revision (saved to drafts)