//****************************************************************************//
//************ Catmull-Clark Subdivision- April 3rd, 2019 *******************//
//**************************************************************************//

- We're starting off today's lecture with a video - "Geri's Game," from Pixar wayyyy back in 1997
    - Besides being enjoyable, this is the first film Pixar ever did with sudvidision surfaces (and if you look at the "Executive Producers" in the credits, Edwin Catmull is right there!)
        - If you look at the original mesh for Geri, his face is mostly made up of quadrilaterals, but with a few triangles here and there
    - How do you end up with creases in "smooth" subdivided surfaces? Tony DeRose figured out a way to do it in this movie with Geri's wrinkles, so there must be a way!
--------------------------------------------------------------------

- If Pixar uses Catmull-Clark subdivision, then it should be good enough for us - so let's look at it!
    - CATMULL-CLARK subdivision is C2 ALMOST everywhere, and is for surfaces made up of mostly quadrilaterals
        - To subdivide this, for all the polygons in the mesh, we'll first add a new vertex at the middle of the whole polygon AND one to each edge of the polygon, and then connect the edge vertices to the center vertex

                ---------       ----*----
                |       |       |       |
                |       | =>    *   *   *   => 
                |       |       |       |
                ---------       ----*----


                - Notice that this will turn triangles into quadrilaterals, giving us an all-quad surface after the first subdivision
            - More mathematically, if we have a quadrilateral with original vertices V1 to V4, then:
                - The new center vertex, V, will have position:

                        V = 1/4 * (V1 + V2 + V3 + V4)

                    - For a polygon with "K" vertices, the center vertex in general will be:

                        V = 1/k * (V1 + ... + Vk)

                    - Once again, notice that this is a convex combination!
                - For the vertices on an edge, this is a little bit more tricky
                    - In the typical case of two quads that share an edge, the new point on the line the polygons share will be:

                            v = 1/16(v1 + v2 + v3 + v4) + 3/8(v5 + v6)

                        - Where the 1/16 vertices are the ones that don't form the edge in question, and the other are the two "line segment" vertices
                    - MORE generally, for 2 polygons of any number of sides the weight will be an even 1/4 for the 2 vertices forming the line segment and 1/4 for the 2 polygon's central vertices
        - After adding these new vertices, though, we need to move the old vertices to actually smooth the surface - how do we do that?
            - Well, the math is a little bit messy, but let's say that "ei" are the new "edge vertices" on the mesh and "fi" are the new center vertices
                - In that case, we'll say the constant points E and F are:

                        E = 1/k (e1 + ... + ek)
                        F = 1/k (f1 + ... + fk)

                    - THEN, for a given original vertex "v", its new position will be:

                            v' = E/k + F/k + v*(k-2)/k

    - With that calculated, how smooth will this Catmull-Clark surface be?
        - As we said, MOST points will have C2 continuity, and will have a valence of 4 (i.e. they'll be quads)
            - ...where will it be C1, then? Only at the "extraordinary vertices" of the surface

- Okay, that's Catmull-Clark subdivision in general, but most surfaces aren't perfectly smooth - they have creases and sharp edges on them (e.g. the edge of an otherwise-smooth desk)! How can we replicate that with subdivision?
    - To do this, we can "tag"/label certain edges of the polygon to be SHARP
        - If a new vertex is being created, we'll ONLY form a new point on that edge using this formula:

                V = 1/2(v1 + v2)

            - Where v1/v2 are the vertices making up the sharp edge's line segment
                - This way, the new points will only be on the line, and won't change that outline!
    - If an old vertex is between 2 sharp edges, then, we'll change the formula for its new position to be:

            v' = (6v + v1 + v2)/8

        - Where v1/v2 are the adjacent vertices to "v" along the sharp edge
            - This means that "v" is only allowed to slide along the existing sharp edge - again, not disturbing the outline!
    - This doesn't *quite* cover all the cases for sharp edges (what if there are 3 sharp edges leading to a vertex? What about vertices at the end of a sharp edge?), but it should give you the general idea

- Sometimes, we might also want SEMI-SHARP EDGES: cases where we don't want a knife-sharp edge, but we DO want it to hold its shape in some way
    - For these, basically, we want to have a sharpness "parameter" for a given edge, where "S=0" doesn't do any sharpening, "S=1" is a little bit sharp, "S=2" is more sharp, and so one, up until "S=infinity" (perfectly sharp)
        - The rule for this is actually surprisingly simple: we'll use the "sharp" subdivision rules on a vertex "S" times, and then apply our smoothing rules once
            - This adds a bunch of polygons without changing the edge's shape, then tries to smooth those now-smaller polygons (which'll result in a more constrained smoothing)

- Now, we'll be covering platonic solids on Friday, but before we get to that we NEED to talk about regular polygons
    - A REGULAR POLYGON is one that is convex, has all sides equal-length, and where all angles within the polygon are the same
        - Common examples of these are equilateral triangles, squares, regular pentagons/hexagons/septagons/octagons/etc.
            - "Poor Heptagon - we can't agree on a name for 7-sided shapes"
            - Years ago, Professor Turk thought that Heptagons just didn't exist out "in the wild," and then one day in CVS he found a pill container with one side for every day of the week - "so I had to buy it!"
        - What about a pentagram? It has equal angles and equal sides, but it's NOT convex!

- As it turns out, you can tile your floor with equilateral triangles, squares, and hexagons - but what about the rest? We'll get to that on Friday - adios!