//-*****************************************************************************
IObjectDrw::IObjectDrw( IObject &iObj, bool iResetIfNoChildren )
  : m_object( iObj )
  , m_minTime( ( chrono_t )FLT_MAX )
  , m_maxTime( ( chrono_t )-FLT_MAX )
{
    // If not valid, just bail.
    if ( !m_object ) { return; }

    // IObject has no explicit time sampling, but its children may.
    size_t numChildren = m_object.getNumChildren();
    for ( size_t i = 0; i < numChildren; ++i )
    {
        const ObjectHeader &ohead = m_object.getChildHeader( i );

        // Decide what to make.
        DrawablePtr dptr;
        if ( IPolyMesh::matches( ohead ) )
        {
            IPolyMesh pmesh( m_object, ohead.getName() );
            if ( pmesh )
            {
                dptr.reset( new IPolyMeshDrw( pmesh ) );
            }
        }
        else if ( IPoints::matches( ohead ) )
        {
            IPoints points( m_object, ohead.getName() );
            if ( points )
            {
                dptr.reset( new IPointsDrw( points ) );
            }
        }
        else if ( ICurves::matches( ohead ) )
        {
            ICurves curves( m_object, ohead.getName() );
            if ( curves )
            {
                dptr.reset( new ICurvesDrw( curves ) );
            }
        }
        else if ( INuPatch::matches( ohead ) )
        {
            INuPatch nuPatch( m_object, ohead.getName() );
            if ( nuPatch )
            {
                dptr.reset( new INuPatchDrw( nuPatch ) );
            }
        }
        else if ( IXform::matches( ohead ) )
        {
            IXform xform( m_object, ohead.getName() );
            if ( xform )
            {
                dptr.reset( new IXformDrw( xform ) );
            }
        }
        else if ( ISubD::matches( ohead ) )
        {
            ISubD subd( m_object, ohead.getName() );
            if ( subd )
            {
                dptr.reset( new ISubDDrw( subd ) );
            }
        }
        else
        {
            IObject object( m_object, ohead.getName() );
            if ( object )
            {
                dptr.reset( new IObjectDrw( object, true ) );
            }
        }

        if ( dptr && dptr->valid() )
        {
            m_children.push_back( dptr );
            m_minTime = std::min( m_minTime, dptr->getMinTime() );
            m_maxTime = std::max( m_maxTime, dptr->getMaxTime() );
        }
    }

    // Make the bounds empty to start
    m_bounds.makeEmpty();

    // If we have no children, just leave.
    if ( m_children.size() == 0 && iResetIfNoChildren )
    {
        m_object.reset();
    }
}
//-*****************************************************************************
IObjectDrw::IObjectDrw( IObject &iObj, bool iResetIfNoChildren, std::vector<std::string> path )
  : m_object( iObj )
  , m_minTime( ( chrono_t )FLT_MAX )
  , m_maxTime( ( chrono_t )-FLT_MAX )
  , m_currentTime( ( chrono_t )-FLT_MAX )
{
    // If not valid, just bail.
    if ( !m_object ) { return; }

    if (path.size()) {
        // std::cout << "IObjectDraw path: " << path[0] << std::endl;
        const ObjectHeader *ohead = m_object.getChildHeader( path[0] );
        if ( ohead!=NULL ) {
            path.erase(path.begin());
            DrawablePtr dptr;
            if ( IXform::matches( *ohead ) ) {
                IXform xform( m_object, ohead->getName() );
                if ( xform ) {
                    dptr.reset( new IXformDrw( xform, path ) );
                }
            }
            if ( dptr && dptr->valid() ) {
                m_children.push_back( dptr );
                m_minTime = std::min( m_minTime, dptr->getMinTime() );
                m_maxTime = std::max( m_maxTime, dptr->getMaxTime() );
            }
        }
    }       
    else
    {   
        // IObject has no explicit time sampling, but its children may.
        size_t numChildren = m_object.getNumChildren();
        for ( size_t i = 0; i < numChildren; ++i )
        {
            const ObjectHeader &ohead = m_object.getChildHeader( i );
            std::cout << "IObjectDraw path: " << ohead.getName() << std::endl;

            // Decide what to make.
            DrawablePtr dptr;
            if ( IPolyMesh::matches( ohead ) )
            {

                // std::cout << "IPolyMesh path: " << ohead.getName() << std::endl;
                IPolyMesh pmesh( m_object, ohead.getName() );
                if ( pmesh )
                {
                    dptr.reset( new IPolyMeshDrw( pmesh, path ) );
                }
            }
    /*        else if ( IPoints::matches( ohead ) )
            {
                IPoints points( m_object, ohead.getName() );
                if ( points )
                {
                    dptr.reset( new IPointsDrw( points ) );
                }
            }
            else if ( IXform::matches( ohead ) )
            {
                IXform xform( m_object, ohead.getName() );
                if ( xform )
                {
                    dptr.reset( new ISimpleXformDrw( xform ) );
                }
            }
    */        else if ( IXform::matches( ohead ) )
            {
                // std::cout << "IXform path: " << ohead.getName() << std::endl;

                IXform xform( m_object, ohead.getName() );
                if ( xform )
                {
                    dptr.reset( new IXformDrw( xform, path ) );
                }
            }
            // else if ( ISubD::matches( ohead ) )
            // {
            //     // std::cout << "ISubD path: " << ohead.getName() << std::endl;
                
            //     ISubD subd( m_object, ohead.getName() );
            //     if ( subd )
            //     {
            //         dptr.reset( new ISubDDrw( subd, path  ) );
            //     }
            // }
            else
            {
                // std::cout << "IObject path: " << ohead.getName() << std::endl;
                IObject object( m_object, ohead.getName() );
                if ( object )
                {
                    dptr.reset( new IObjectDrw( object, true, path ) );
                }
            }

            if ( dptr && dptr->valid() )
            {
                m_children.push_back( dptr );
                m_minTime = std::min( m_minTime, dptr->getMinTime() );
                m_maxTime = std::max( m_maxTime, dptr->getMaxTime() );
            }
        }
    }
    
    // Make the bounds empty to start
    m_bounds.makeEmpty();

    // If we have no children, just leave.
    if ( m_children.size() == 0 && iResetIfNoChildren )
    {
        m_object.reset();
    }
}