
// CommonStrokeTessellatorAnalyzeLink.hpp - common GPU and CPU polar stroking routines

#ifndef __CommonStrokeTessellatorAnalyzeLink_hpp__
#define __CommonStrokeTessellatorAnalyzeLink_hpp__

#include "CommonStrokeTessellatorPrivate.hpp"

// Determine if the link encoded as a tessellated patch is a segment,
// join, or cap; and if cap, what kind of cap.
int StrokeTessellatorMETHOD analyzeLink()
{
    int link_type;
    CONST bool is_join_or_cap = (vertex_id[1] == 0);

    if (is_join_or_cap) {
        CONST bool is_cap = (vertex_id[5] == 0);
        if (is_cap) {
            // The non-zero status of vertex_id[2] & vertex_id[3] are combined to
            // make a 2-bit (four-valued) enumeration of the cap type:
            //   0 = initial cap
            //   1 = terminal cap
            //   2 = initial dash cap
            //   3 = terminal dash cap
            int cap_placement = int(vertex_id[2] != 0);
            if (vertex_id[3] != 0) {
                cap_placement |= 2;
            }
            // Next the 2-bit cap_placement value is then looked up into the
            // cap_placement_to_type uniform to map to specific cap choice.
            //   0 = None
            //   1 = Round (remapped to the maximum steps)
            //   2 = Triangular
            //   3 = Square
            link_type = int((cap_placement_to_type >> (cap_placement * 8)) & 0xFF);
            if (link_type == LT_ROUND_CAP_PRE_REMAP) {
                link_type = LT_HEMICIRCLE_CAP;
            }
        } else {
            link_type = LT_JOIN;  // is a join
        }
        // Copy cp[0] over all the cp elements so trivialCulling works.
        PER_THREAD if (TID >= 1) cp[TID] = cp[0];
    } else {
        bool is_quadratic = (vertex_id[2] == 0);
        if (is_quadratic) {
            link_type = LT_QUADRATIC_SEGMENT;  // quadratic path segment
            // Copy cp[1] over all the cp[2] elements so trivialCulling works.
            FOR_THREAD(2) cp[TID] = cp[1];
        } else {
            link_type = LT_CUBIC_SEGMENT;  // cubic path segment
        }
    }
    // Now link_type is one of:
    //  -3 = cubic path segment
    //  -2 = quadratic path segment
    //  -1 = join
    //   0 = None
    //   2 = Triangular
    //   3 = Square
    //   max_steps = Round (remapped from 1, aka LT_ROUND_CAP_PRE_REMAP)
    return link_type;
}

#endif // __CommonStrokeTessellatorAnalyzeLink_hpp__
