SOP/SOP_NURBS.C

/*
 * Copyright (c) 2013
 *      Side Effects Software Inc.  All rights reserved.
 *
 * Redistribution and use of Houdini Development Kit samples in source and
 * binary forms, with or without modification, are permitted provided that the
 * following conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. The name of Side Effects Software may not be used to endorse or
 *    promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY SIDE EFFECTS SOFTWARE `AS IS' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
 * NO EVENT SHALL SIDE EFFECTS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *----------------------------------------------------------------------------
 * The NURBS SOP
 */

#include <limits.h>
#include <UT/UT_DSOVersion.h>
#include <UT/UT_Math.h>
#include <UT/UT_Interrupt.h>
#include <GU/GU_Detail.h>
#include <GU/GU_PrimNURBSurf.h>
#include <PRM/PRM_Include.h>
#include <OP/OP_Operator.h>
#include <OP/OP_OperatorTable.h>
#include "SOP_NURBS.h"

using namespace HDK_Sample;

///
/// newSopOperator is the hook that Houdini grabs from this dll
/// and invokes to register the SOP.  In this case we add ourselves
/// to the specified operator table.
///
void
newSopOperator(OP_OperatorTable *table)
{
    table->addOperator(
            new OP_Operator("hdk_nurbs",                // Internal name
                            "NURBS",                    // UI name
                             SOP_NURBS::myConstructor,  // How to build the SOP
                             SOP_NURBS::myTemplateList, // My parameters
                             0,                         // Min # of sources
                             0,                         // Max # of sources
                             0,                         // Local variables
                             OP_FLAG_GENERATOR)         // Flag it as generator
            );
}

PRM_Template
SOP_NURBS::myTemplateList[] = {
    PRM_Template(PRM_INT,                       // Integer parameter.
                PRM_Template::PRM_EXPORT_TBX,   // Export to top of viewer
                2, &PRMdivName, PRMfourDefaults),
    PRM_Template(PRM_INT,
                PRM_Template::PRM_EXPORT_TBX,   // Export to top of viewer
                2, &PRMorderName, PRMfourDefaults,      
                NULL, &PRMorderRange),
    PRM_Template()
};

OP_Node *
SOP_NURBS::myConstructor(OP_Network *net, const char *name, OP_Operator *op)
{
    return new SOP_NURBS(net, name, op);
}

SOP_NURBS::SOP_NURBS(OP_Network *net, const char *name, OP_Operator *op)
        : SOP_Node(net, name, op)
{
}

SOP_NURBS::~SOP_NURBS() {}

unsigned
SOP_NURBS::disableParms()
{
    return 0;
}

OP_ERROR
SOP_NURBS::cookMySop(OP_Context &context)
{
    int                 u, v;
    int                 nu, nv;
    int                 uorder, vorder;
    fpreal              s, t;
    fpreal              now = context.getTime();
    GU_PrimNURBSurf     *surf;

    nu = SYSmax(COLS(now), 2);          // Number of columns
    nv = SYSmax(ROWS(now), 2);          // Number of rows
    uorder = SYSmin(UORDER(now), nu); 
    vorder = SYSmin(VORDER(now), nv); 

    gdp->clearAndDestroy();

    // Create a NURBS surface.
    surf = GU_PrimNURBSurf::build(gdp, nv, nu, uorder, vorder);

    /// @see GEO_TPSurf for basis access methods.

    for (u = 0; u < nu; u++)
    {
        s = SYSfit((fpreal)u, u, (fpreal)nu-1, 0, 1);
        for (v = 0; v < nv; v++)
        {
            t = SYSfit((fpreal)v, v, (fpreal)nv-1, 0, 1);

            GEO_Vertex  vtx = (*surf)(v, u);    // Rows/columns
            UT_Vector4  P( s, 0, t, 1);
            vtx.getPt()->setPos(P);
        }
    }

    return error();
}

Generated on Thu Jan 31 00:24:21 2013 for HDK by  doxygen 1.5.9