DomainOperator 7.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield 
 *
 * This library is open source and may be redistributed and/or modified under  
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * OpenSceneGraph Public License for more details.
*/
// Written by Wang Rui, (C) 2010

#ifndef OSGPARTICLE_DOMAINOPERATOR
#define OSGPARTICLE_DOMAINOPERATOR

#include <osg/Plane>
#include <osgParticle/Operator>
#include <osgParticle/Particle>

namespace osgParticle
{


/** A domain operator which accepts different domain shapes as the parameter.
    It can be derived to implement operators that require particles interacting with domains.
    Refer to David McAllister's Particle System API (http://www.particlesystems.org)
*/
class OSGPARTICLE_EXPORT DomainOperator : public Operator
{
public:
    struct Domain
    {
        enum Type
        {
            UNDEFINED_DOMAIN = 0,
            POINT_DOMAIN,
            LINE_DOMAIN,
            TRI_DOMAIN,
            RECT_DOMAIN,
            PLANE_DOMAIN,
            SPHERE_DOMAIN,
            BOX_DOMAIN,
            DISK_DOMAIN
        };
        
        Domain( Type t ) : r1(0.0f), r2(0.0f), type(t) {}
        osg::Plane plane;
        osg::Vec3 v1;
        osg::Vec3 v2;
        osg::Vec3 v3;
        osg::Vec3 s1;
        osg::Vec3 s2;
        float r1;
        float r2;
        Type type;
    };
    
    DomainOperator()
    : Operator()
    {}
    
    DomainOperator( const DomainOperator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY )
    : Operator(copy, copyop), _domains(copy._domains), _backupDomains(copy._backupDomains)
    {}
    
    META_Object( osgParticle, DomainOperator );
    
    /// Add a point domain
    inline void addPointDomain( const osg::Vec3& p );
    
    /// Add a line segment domain
    inline void addLineSegmentDomain( const osg::Vec3& v1, const osg::Vec3& v2 );
    
    /// Add a triangle domain
    inline void addTriangleDomain( const osg::Vec3& v1, const osg::Vec3& v2, const osg::Vec3& v3 );
    
    /// Add a rectangle domain
    inline void addRectangleDomain( const osg::Vec3& corner, const osg::Vec3& w, const osg::Vec3& h );
    
    /// Add a plane domain
    inline void addPlaneDomain( const osg::Plane& plane );
    
    /// Add a sphere domain
    inline void addSphereDomain( const osg::Vec3& c, float r );
    
    /// Add a box domain
    inline void addBoxDomain( const osg::Vec3& min, const osg::Vec3& max );
    
    /// Add a disk domain
    inline void addDiskDomain( const osg::Vec3& c, const osg::Vec3& n, float r1, float r2=0.0f );
    
    /// Add a domain object directly, used by the .osg wrappers and serializers.
    void addDomain( const Domain& domain ) { _domains.push_back(domain); }
    
    /// Get a domain object directly, used by the .osg wrappers and serializers.
    const Domain& getDomain( unsigned int i ) const { return _domains[i]; }
    
    /// Remove a domain at specific index
    void removeDomain( unsigned int i )
    { if (i<_domains.size()) _domains.erase(_domains.begin() + i); }
    
    /// Remove all existing domains
    void removeAllDomains() { _domains.clear(); }
    
    /// Get number of domains
    unsigned int getNumDomains() const { return _domains.size(); }
    
    /// Apply the acceleration to a particle. Do not call this method manually.
    void operate( Particle* P, double dt );
    
    /// Perform some initializations. Do not call this method manually.
    void beginOperate( Program* prg );
    
    /// Perform some post-operations. Do not call this method manually.
    void endOperate();
    
protected:
    virtual ~DomainOperator() {}
    DomainOperator& operator=( const DomainOperator& ) { return *this; }
    
    virtual void handlePoint( const Domain& domain, Particle* P, double dt ) { ignore("Point"); }
    virtual void handleLineSegment( const Domain& domain, Particle* P, double dt ) { ignore("LineSegment"); }
    virtual void handleTriangle( const Domain& domain, Particle* P, double dt ) { ignore("Triangle"); }
    virtual void handleRectangle( const Domain& domain, Particle* P, double dt ) { ignore("Rectangle"); }
    virtual void handlePlane( const Domain& domain, Particle* P, double dt ) { ignore("Plane"); }
    virtual void handleSphere( const Domain& domain, Particle* P, double dt ) { ignore("Sphere"); }
    virtual void handleBox( const Domain& domain, Particle* P, double dt ) { ignore("Box"); }
    virtual void handleDisk( const Domain& domain, Particle* P, double dt ) { ignore("Disk"); }
    
    inline void computeNewBasis( const osg::Vec3&, const osg::Vec3&, osg::Vec3&, osg::Vec3& );
    inline void ignore( const std::string& func );
    
    std::vector<Domain> _domains;
    std::vector<Domain> _backupDomains;
};

// INLINE METHODS

inline void DomainOperator::addPointDomain( const osg::Vec3& p )
{
    Domain domain( Domain::POINT_DOMAIN );
    domain.v1 = p;
    _domains.push_back( domain );
}

inline void DomainOperator::addLineSegmentDomain( const osg::Vec3& v1, const osg::Vec3& v2 )
{
    Domain domain( Domain::LINE_DOMAIN );
    domain.v1 = v1;
    domain.v2 = v2;
    domain.r1 = (v2 - v1).length();
    _domains.push_back( domain );
}

inline void DomainOperator::addTriangleDomain( const osg::Vec3& v1, const osg::Vec3& v2, const osg::Vec3& v3 )
{
    Domain domain( Domain::TRI_DOMAIN );
    domain.v1 = v1;
    domain.v2 = v2;
    domain.v3 = v3;
    domain.plane.set(v1, v2, v3);
    computeNewBasis( v2-v1, v3-v1, domain.s1, domain.s2 );
    _domains.push_back( domain );
}

inline void DomainOperator::addRectangleDomain( const osg::Vec3& corner, const osg::Vec3& w, const osg::Vec3& h )
{
    Domain domain( Domain::RECT_DOMAIN );
    domain.v1 = corner;
    domain.v2 = w;
    domain.v3 = h;
    domain.plane.set(corner, corner+w, corner+h);
    computeNewBasis( w, h, domain.s1, domain.s2 );
    _domains.push_back( domain );
}

inline void DomainOperator::addPlaneDomain( const osg::Plane& plane )
{
    Domain domain( Domain::PLANE_DOMAIN );
    domain.plane.set(plane);
    _domains.push_back( domain );
}

inline void DomainOperator::addSphereDomain( const osg::Vec3& c, float r )
{
    Domain domain( Domain::SPHERE_DOMAIN );
    domain.v1 = c;
    domain.r1 = r;
    _domains.push_back( domain );
}

inline void DomainOperator::addBoxDomain( const osg::Vec3& min, const osg::Vec3& max )
{
    Domain domain( Domain::BOX_DOMAIN );
    domain.v1 = min;
    domain.v2 = max;
    _domains.push_back( domain );
}

inline void DomainOperator::addDiskDomain( const osg::Vec3& c, const osg::Vec3& n, float r1, float r2 )
{
    Domain domain( Domain::DISK_DOMAIN );
    domain.v1 = c;
    domain.v2 = n;
    domain.r1 = r1;
    domain.r2 = r2;
    domain.plane.set(n, c);
    _domains.push_back( domain );
}

inline void DomainOperator::computeNewBasis( const osg::Vec3& u, const osg::Vec3& v, osg::Vec3& s1, osg::Vec3& s2 )
{
    // Copied from David McAllister's Particle System API (http://www.particlesystems.org), pDomain.h
    osg::Vec3 w = u ^ v;
    float det = w.z()*u.x()*v.y() - w.z()*u.y()*v.x() - u.z()*w.x()*v.y() -
                u.x()*v.z()*w.y() + v.z()*w.x()*u.y() + u.z()*v.x()*w.y();
    det = 1.0f / det;
    
    s1.set( v.y()*w.z() - v.z()*w.y(), v.z()*w.x() - v.x()*w.z(), v.x()*w.y() - v.y()*w.x() );
    s1 = s1 * det;
    s2.set( u.y()*w.z() - u.z()*w.y(), u.z()*w.x() - u.x()*w.z(), u.x()*w.y() - u.y()*w.x() );
    s2 = s2 * (-det);
}

inline void DomainOperator::ignore( const std::string& func )
{
    OSG_NOTICE << className() << ": " << func << " domain not yet implemented. " << std::endl;
}


}

#endif