Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Gaming > Development Programming Algorithms > Re: Representin...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 5 Topic 646 of 680
Post > Topic >>

Re: Representing/returning indicies to a model

by "Jim Langston" <tazmaster@[EMAIL PROTECTED] > Sep 11, 2007 at 09:31 AM

"Gernot Frisch" <Me@[EMAIL PROTECTED]
> wrote in message 
news:5kncjiF4maonU1@[EMAIL PROTECTED]
>
>> Actually, if you look at the C++ class I was defining I was actually 
>> doing
>> that.  The parts class didn't predefine anything.  That would be up to 
>> whatever called it.
>
>
> Nathan's solution is best. If you want an "arm" pointer, you might have 
> each body assigned with a unique description string, and make an
operator:
>
> body* operator[] (const std::string& bodyname)
> {
>    if(this->m_name == bodyname) return this;
>    for(std::container<body>::iterator it=m_bodies.begin(); 
> it!=m_bodies.end(); ++it)
>    return it->operator[](bodyname);
> return NULL;
> }

This is what I wound up coming up with.

class PartIndex
{

public:

    PartIndex( const std::string& Name = "Torso" ): Name_( Name ) {}
    std::string Name() const { return Name_; }
    bool Add( const std::string& Name, size_t Index )
    {
        if ( Name_ == Name )
        {
            Indicies_.insert( Index );
            return true;
        }
        else
        {
            for ( std::vector<PartIndex>::iterator it = SubParts_.begin();

it != SubParts_.end(); ++it )
                if ( (*it).Add( Name, Index ) )
                    return true;
            return false;
        }
    }
    bool HasSubpart( const std::string& Name ) const
    {
        for ( std::vector<PartIndex>::const_iterator it =
SubParts_.begin(); 
it != SubParts_.end(); ++it )
            if ( (*it).Name_ == Name )
                return true;
        return false;
    }
    bool AddSubpart( const std::string& Part, const std::string& Name )
    {
        if ( Name_ == Part )
        {
            if ( ! HasSubpart( Name ) )
                SubParts_.push_back( PartIndex( Name ) );
            return true;
            // else do nothing, part already exists so we don't care
        }
        else
        {
            for ( std::vector<PartIndex>::iterator it = SubParts_.begin();

it != SubParts_.end(); ++it )
                if ( (*it).AddSubpart( Part, Name ) )
                    return true;
            return false;
        }
    }
    void PartIndicies( std::set<size_t>& Index ) const
    {
        std::copy( Indicies_.begin(), Indicies_.end(),
std::inserter(Index, 
Index.end()) );
    }
    bool PartIndicies( const std::string& Name, std::set<size_t>& Index )
    {
        if ( Name_ == Name )
        {
            PartIndicies( Index );
            return true;
        }
        else
        {
            for ( std::vector<PartIndex>::iterator it = SubParts_.begin();

it != SubParts_.end(); ++it )
                if ( (*it).PartIndicies ( Name, Index ) )
                    return true;
            return false;
        }
    }
    void Indicies( std::set<size_t>& Index ) const
    {
        PartIndicies( Index );
        for ( std::vector<PartIndex>::const_iterator it =
SubParts_.begin(); 
it != SubParts_.end(); ++it )
            (*it).Indicies( Index );
    }
    bool Indicies( const std::string& Name, std::set<size_t>& Index )
    {
        if ( Name_ == Name )
        {
            Indicies( Index );
            return true;
        }
        else
        {
            for ( std::vector<PartIndex>::iterator it = SubParts_.begin();

it != SubParts_.end(); ++it )
                if ( (*it).Indicies ( Name, Index ) )
                    return true;
            return false;
        }

    }

    void Dump( std::ostream& os )
    {
        Dump( os, "" );
    }
    void Dump( std::ostream& os, const std::string& Prefix )
    {
        os << Prefix << Name_ << " ";
        std::copy( Indicies_.begin(), Indicies_.end(), 
std::ostream_iterator<int>(os, " "));
        os << "\n";
        for ( std::vector<PartIndex>::iterator it = SubParts_.begin(); it
!= 
SubParts_.end(); ++it )
            (*it).Dump( os, Prefix + " " );
    }

private:

    std::string Name_;
    std::set<size_t> Indicies_;
    std::vector<PartIndex> SubParts_;

};
 




 5 Posts in Topic:
Representing/returning indicies to a model
"Jim Langston"   2007-09-05 17:36:32 
Re: Representing/returning indicies to a model
nathan@[EMAIL PROTECTED]   2007-09-06 00:59:50 
Re: Representing/returning indicies to a model
"Jim Langston"   2007-09-05 19:32:02 
Re: Representing/returning indicies to a model
"Gernot Frisch"  2007-09-11 13:31:38 
Re: Representing/returning indicies to a model
"Jim Langston"   2007-09-11 09:31:18 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 19:25:37 CDT 2008.