[SESI logo]

Prisms Development Toolkit Documentation

Side Effects Software Inc. 1997

Custom SOPs

Example 1: The Star SOP

This draws a star centered at zero - this involves making a circle, and scaling every other point toward the center. This SOP is a Primitive SOP because it takes no sources.
The source for this SOP can be found at
/ufs/prisms/support/action_custom/SOPs/star.c

#include <custom.h>

static char	*sopHelp[] =
{
	" This SOP creates a star centered at the origin",
	" Options: ",
	" divisions  : The number of outer points in the star ",
	" radius     : The radius of the inner points and the ",
	"              outer points",
	" Negative Radii: This toggle will allow negative",
	"                 radii. Otherwise 0 is used.",
	" orientation: The plane in which the star is created",
	0
};

#define		NSOURCES	0	/* Takes Zero Sources */

typedef struct sop_data 
{
    SOP_EXPR    NumOfPoints;
    SOP_EXPR	Radii[2];
    SOP_EXPR	AllowNegative;
    SOP_EXPR	Orientation;
} SOP_DATA;

static SOP_PARMS	sopParms[] =
{
    { SOPPARM_EXPR,	"divisions",	NULL, {NULL} },
    { SOPPARM_DOUBLE,	"radius",	NULL, {NULL} },
    { SOPPARM_DOUBLE,	"",		NULL, {NULL} },
    { SOPPARM_TOGGLE,	"",		NULL , {NULL} },
    { SOPPARM_MENU,	"orientation",	NULL , {NULL} },
    { NULL },
};

#define XYPLANE 0
#define YZPLANE 1
#define ZXPLANE 2

static char	*orientationTexts[] =
{
    "XY",
    "YZ",
    "ZX"
};

static MENU	orientationMenu[] = {
    { NULLMFUNC, "XY", (char *)XYPLANE },
    { NULLMFUNC, "YZ", (char *)YZPLANE },
    { NULLMFUNC, "ZX", (char *)ZXPLANE },
    { NULL }
};

static void
sopDefaults(SOP *sop)
{
    SOP_DATA	*sb;

    if (sop->special) free(sop->special);
    sb = (SOP_DATA *)Calloc(sizeof(SOP_DATA), "Star sopDefaults");
    sop->special = (char *)sb;

    InitSopExpr(&sb->NumOfPoints, "5");
    InitSopExpr(&sb->Radii[0], "1");
    InitSopExpr(&sb->Radii[1], "0.5");
    InitSopToggle(&sb->AllowNegative, "Negative radius");
    InitSopMenu(&sb->Orientation, orientationMenu, 
		XYPLANE, "Orientation");
}

static void
sopScan(SOP *sop, int argc, char *argv[])
{
    SOP_DATA	*sb;
    sb = (SOP_DATA *) sop->special;	

    SopScanExpr   ('n', &sb->NumOfPoints,	sop);
    SopScanDouble ('r',  sb->Radii,		sop);
    SopScanToggle ('g', &sb->AllowNegative,	sop);
    SopScanMenu   ('O', &sb->Orientation, orientationTexts, 2);
}

static void
sopPrint(FILE *fp, SOP *sop)
{
    SOP_DATA	*sb;
    sb = (SOP_DATA *)sop->special;

    SopPrintExpr   (fp, &sb->NumOfPoints,   "", "-n");
    SopPrintDouble (fp,  sb->Radii,	    "", "", "-r");
    SopPrintToggle (fp, &sb->AllowNegative, "-g");
    SopPrintMenu   (fp, &sb->Orientation, orientationTexts, "", "-O");
}

static void
sopCook(SOP *sop)
{
    SOP_DATA	*sb;
    Point	 center;
    float	 radius1;
    float	 radius2;
    int		 numPts;
    int		 even;

    /* The following are for the FORALLPOINTS loops */
    PolyPointPointer	points, tmppt;
    int		 npts;

    sb = (SOP_DATA *)sop->special;

    /* Free whatever we had before */
    if (sop->pdp) FreePolyDetail(sop->pdp);
    sop->pdp = 0;

    EVALUATE(sop, sb->NumOfPoints);
    EVALUATE(sop, sb->Radii[0]);
    EVALUATE(sop, sb->Radii[1]);

    /* 
     * I want to create a circle with twice the number of points that
     * were specified so that after shifting alternating points I
     * will have a star with the correct number of outer points.
     * Make sure there are at least four points.
     */
    numPts = 2 * (int)sb->NumOfPoints.value.fval;
    numPts = (numPts < 4) ? 4 : numPts;

    /* PolyCircle creates a PolyDetail of a Circle, 
	the 1.0 is the radius */
    center.x = center.y = center.z = 0.0;
    sop->pdp = PolyCircle(center, 1.0, 1.0, numPts);

    /* I now figure out the radii and take care of negative ones */
    radius1 = sb->Radii[0].value.fval;
    if (sb->AllowNegative.value.ival == 0)
	radius1 = (radius1 < 0) ? 0.0 : radius1;

    radius2 = sb->Radii[1].value.fval;
    if (sb->AllowNegative.value.ival == 0)
	radius2 = (radius2 < 0) ? 0.0 : radius2;
    
    /* even is a flag so that I can alternate between 
	radius1 and radius2 */
    even = 1;
    points = sop->pdp->ppt;
    /* 
     * FOR_ALL_POINTS is a macro that will create a loop that will go
     * through all the points in the Points structure given in the 
     * first parameter, the second parameter is the current point in
     * the loop, and npts is an integer and will count the points
     */
    FOR_ALL_POINTS(points, tmppt, npts)
    {
	float radius = (even) ? radius1 : radius2;
	even = !even;
	/* stretch the point by the appropriate amount */
	tmppt->pos.x *= radius;
	tmppt->pos.y *= radius;
	tmppt->pos.z *= radius;
    }

    /* Now reorient the Star if necessary */
    if ((int)(sb->Orientation.value.ival) == YZPLANE)
	FOR_ALL_POINTS(points, tmppt, npts)
	{
	    tmppt->pos.z = center.z + tmppt->pos.x;
	    tmppt->pos.x = center.x;
	}
    else if ((int)(sb->Orientation.value.ival) == ZXPLANE)
	FOR_ALL_POINTS(points, tmppt, npts)
	{
	    tmppt->pos.z = center.z + tmppt->pos.y;
	    tmppt->pos.y = center.y;
	}

    /* Since I didn't calculate Normals, clear the flag for it. */
    CLEARSOPNML(sop)
}

static void
sopClean(SOP *sop)		
{
    int		i;
    SOP_DATA	*sb;

    sb = (SOP_DATA *)sop->special;
    FreeSopExpr((SOP_EXPR *)sb, sopParms);

    /* free the struct and zero it out (for sopDefaults) */
    free(sop->special);
    sop->special = NULL;
}

GETSOPPARMS

static void
sopGPMake(UI_DBOX *dbp, SOP *sop)
{
    InitSopParms(sop);
    BuildCommonSopGadgets(sop, NSOURCES, sopParms, sopHelp, dbp, NULL);
}

#define SOPUSAGE	\
"sstar -n NumOfPoints -r radius1 radius2 -g -O orientation\n\
\t-n    Number of outer points in the star\
\t-r    The distance from the origin to the inner and outer radius\
\t-g    Whether or not negative radii are allowed\
\t-O    The Orientation of the Star\
\tetc\n"

SOP_FUNC	StarSopRef = {
	"star",
	"n:r::gO:",
	sopScan, sopPrint, sopCook, sopDefaults, sopClean, sopGPMake,
	sopGetParms,
	SOPUSAGE,
};


Tookit Overview
Expression Functions | User Functions | Custom SOPs
Prisms Internal Format | Programmer's Reference
Compiling SOPs | Using sopbuild | SOP Routine Reference
Dynamic SOPs | Prisms Types | Macros
Math Routines | Data Routines | Polygon Routines | Winged-edge Routines

Copyright © 1997 Side Effects Software Inc.
477 Richmond Street West, Toronto, Ontario, Canada M5V 3E7