SIM/SIM_SolverSNOW.h

/*
 * Copyright (c) 2012
 *      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 simplest network to see something working with this...
 *    Empty Object
 *         |
 *    Render Parms (set Data Name to SnowValue/RenderParms)
 *         |
 *    Snow Solver  (Birth rate .1, divisions 20)
 */

#ifndef __SIM_SolverSNOW_h__
#define __SIM_SolverSNOW_h__

#include <UT/UT_HashTable.h>
#include <UT/UT_Hash.h>
#include <UT/UT_IStream.h>
#include <UT/UT_VoxelArray.h>
#include <GU/GU_DetailHandle.h>
#include <SIM/SIM_OptionsUser.h>
#include <SIM/SIM_SingleSolver.h>
#include <SIM/SIM_Geometry.h>

#define SIM_NAME_SOURCEOBJECTS  "sourceobjects"
#define SIM_NAME_BIRTHRATE      "birthrate"
#define SIM_NAME_DIVISIONS      "divisions"
#define SIM_NAME_ORIGINALDEPTH  "originaldepth"

class SIM_Object;
class GEO_Point;
class SIM_Random;

typedef unsigned char           u8;

namespace HDK_Sample {

class SNOW_VoxelArray;

// This class implemented a computational fluid dynamics solver.
class SIM_SolverSNOW : public SIM_SingleSolver,
                       public SIM_OptionsUser
{
public:
    // Access methods for our configuration data.
    GETSET_DATA_FUNCS_F(SIM_NAME_BIRTHRATE, BirthRate);
    GETSET_DATA_FUNCS_I(SIM_NAME_DIVISIONS, Divisions);
    GETSET_DATA_FUNCS_I(SIM_NAME_ORIGINALDEPTH, OriginalDepth);
    
protected:
    explicit             SIM_SolverSNOW(const SIM_DataFactory *factory);
    virtual             ~SIM_SolverSNOW();

    SIM_Random          *createRandomData(SIM_Object *obj) const;

    virtual SIM_Result   solveSingleObjectSubclass(SIM_Engine &engine,
                                        SIM_Object &object,
                                        SIM_ObjectArray &feedbacktoobjects,
                                        const SIM_Time &timestep,
                                        bool newobject);

    int                  rand_choice(int numchoice, SIM_Random *rand) const;
    bool                 brownianize(int &v, int dv, int max,
                                     SIM_Random *rand) const;
    int                  clearInDirection(const SNOW_VoxelArray &snow,
                                int sx, int sy, int sz,
                                int dx, int dy, int dz,
                                int &rx, int &ry, int &rz,
                                int maxdist,
                                SIM_Random *rand) const;
    void                 clearSnow(SNOW_VoxelArray &snow,
                                int x, int y, int z,
                                SIM_Random *rand) const;

    void                 fillRow(SNOW_VoxelArray &snow,
                                fpreal startx, fpreal endx,
                                int y, int z,
                                u8 voxeltype,
                                SIM_Random *rand) const;
    void                 applyGeometry(SNOW_VoxelArray &snow,
                                const GU_ConstDetailHandle &gdh,
                                const UT_DMatrix4 &xform,
                                u8 voxletype,
                                SIM_Random *rand) const;

private:
    static const SIM_DopDescription     *getSolverSNOWDopDescription();

    void                 solveForObject(SIM_Object &object,
                                        SNOW_VoxelArray &snow,
                                        const SIM_Time &timestep) const;
    void                 setVoxelArrayAttributes(
                                        SNOW_VoxelArray *voxelarray) const;

    DECLARE_STANDARD_GETCASTTOTYPE();
    DECLARE_DATAFACTORY(SIM_SolverSNOW,
                        SIM_SingleSolver,
                        "SNOW Solver",
                        getSolverSNOWDopDescription());
};


// Standard defines for voxel elements:
#define VOXEL_EMPTY             0
#define VOXEL_SNOW              1
#define VOXEL_COMPRESSED        2
#define VOXEL_WALL              3
#define VOXEL_OBJECT            4

// This class hold an oriented bounding box tree.
class SNOW_VoxelArray : public SIM_Geometry
{
public:
    // Get the voxel array and its dimensions.
    int                  getXDivisions() const;
    void                 setXDivisions(int divisions);
    int                  getYDivisions() const;
    void                 setYDivisions(int divisions);
    int                  getZDivisions() const;
    void                 setZDivisions(int divisions);

    // Read an element:  This will return 0 for out of bound x/y/z values.
    u8                   getVoxel(int x, int y, int z) const;
    void                 setVoxel(u8 voxel, int x, int y, int z);

    void                 collapseAllTiles();

    void                 pubHandleModification()
                         { handleModification(); }

protected:
    explicit             SNOW_VoxelArray(const SIM_DataFactory *factory);
    virtual             ~SNOW_VoxelArray();

    // Overrides to properly implement this class as a SIM_Data.
    virtual void         initializeSubclass();
    virtual void         makeEqualSubclass(const SIM_Data *source);
    virtual void         saveSubclass(ostream &os) const;
    virtual bool         loadSubclass(UT_IStream &is);
    virtual void         handleModificationSubclass(int code);

    // Override the getGeometrySubclass function to construct our geometric
    // representation in a just-in-time fashion.
    virtual GU_ConstDetailHandle getGeometrySubclass() const;

private:
    GEO_Point           *createOrFindPoint(GU_Detail *gdp, int x, int y, int z);
    void                 buildFace(GU_Detail *gdp,
                                   int x0, int y0, int z0,
                                   int x1, int y1, int z1,
                                   int x2, int y2, int z2,
                                   int x3, int y3, int z3);
    void                 buildGeometryFromArray();
    void                 freeArray() const;
    void                 allocateArray() const;

    mutable GU_DetailHandle              myDetailHandle;
    mutable UT_VoxelArray<u8>           *myVoxelArray;

    int                                  myXDivisions;
    int                                  myYDivisions;
    int                                  myZDivisions;
    UT_HashTable                         myPointHash;

    DECLARE_STANDARD_GETCASTTOTYPE();
    DECLARE_DATAFACTORY(SNOW_VoxelArray,
                        SIM_Geometry,
                        "Voxel Array",
                        getEmptyDopDescription());
};

} // End the HDK_Sample namespace

#endif


Generated on Thu May 24 00:08:06 2012 for HDK by  doxygen 1.5.9