Subdivision Surfaces

Properties for subdivision surfaces are defined in 'inc/real/objects/r3subdiv.h'

You can create subdivision surface objects as follows:


    #include <real/objects/r3subdiv.h>

    R3VECTOR *points;
    R3SUBDIVFACE *faces;

    /* allocate memory for points and faces */
    points = R3Alloc(sizeof(R3VECTOR)*pcount, 0);
    faces = R3Alloc(sizeof(R3SUBDIVFACE)*fcount, R3MEMF_CLEAR);

    /* initialize vertices */
    for(i = 0; i < pcount; i++)
        points[i] = ...;

    /* initialize faces, each face refers to three vertices */
     for(i = 0; i < fcount; i++) {
        faces[i].vcount = 3; // three points per face
        faces[i].vertexindices = R3Alloc(sizeof(R3INT)*faces[i].vcount, 0);
        for (j = 0; j < faces[i].vcount; j++) {
            faces[i].vertexindices[j] = ..; 
        }
    }

    /* create polygonal SDS mesh */
    obj = R3New(R3CLID_SUBDIVISION,
                R3T(R3RA_Name, "my sds"),
                R3T(R3SUBDIVA_PointCount, pcount),
                R3T(R3SUBDIVA_Points, points),
                R3T(R3SUBDIVA_FaceCount, fcount),
                R3T(R3SUBDIVA_Faces, faces),
                R3T(R3SUBDIVA_Type, R3SUBDIVTYPE_POLYGONAL),
                R3TAG_END);

To smooth desired sds vertices, pass array of R3UBYTE values with 'R3SUBDIVA_SmoothVertices' tag to create method (or R3RM_SET method).


    R3UBYTE *buf;
    R3INT t;

    /* alloc temp buffer for smoothing data */
    buf = R3Alloc(sizeof(R3UBYTE)*pcount, 0);

    /* define which vertices should be smoothed */
    for (i = 0; i < pcount; i++)
    buf[i] = TRUE; // smooth everything

    /* set smoothing info to the sds object */
    R3SetAttrs(sds,
               R3T(R3SUBDIVA_SmoothVertices, buf),
               R3TAG_END);

    /* free temp memory */
    R3Free(buf, sizeof(R3UBYTE)*pcount);

To fetch face center and face normals from the SDS object:


    R3VECTOR *centers, *normals;
    R3INT facecount;

    // fetch number of faces
    R3GetAttrs(sds,
               R3T(R3SUBDIVA_FaceCount, &facecount),
               R3TAG_END);

    // allocate memory for the face centers and normals
    centers = R3Alloc(sizeof(R3VECTOR) * facecount))
    normals = R3Alloc(sizeof(R3VECTOR) * facecount);

    // fetch
    R3DoA3(sds, R3SUBDIVM_CALCULATEFACECENTERSANDNORMALS, centers, (void*)R3SPACE_ABSOLUTE, normals);

    // use ....

    // free memory
    R3Free(centers, sizeof(R3VECTOR) * facecount);
    R3Free(normals, sizeof(R3VECTOR) * facecount);