//-***************************************************************************** // // Copyright (c) 2009-2014, // Sony Pictures Imageworks Inc. and // Industrial Light & Magic, a division of Lucasfilm Entertainment Company Ltd. // // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Sony Pictures Imageworks, nor // Industrial Light & Magic, nor the names of their contributors may be used // to endorse or promote products derived from this software without specific // prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "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 THE COPYRIGHT // OWNER OR CONTRIBUTORS 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. // //-***************************************************************************** #ifndef Alembic_AbcGeom_IGeomParam_h #define Alembic_AbcGeom_IGeomParam_h #include #include namespace Alembic { namespace AbcGeom { namespace ALEMBIC_VERSION_NS { //-***************************************************************************** template class ITypedGeomParam { public: typedef typename TRAITS::value_type value_type; typedef Abc::ITypedArrayProperty prop_type; class Sample { public: typedef Sample this_type; typedef Alembic::Util::shared_ptr< Abc::TypedArraySample > samp_ptr_type; Sample() {} Abc::UInt32ArraySamplePtr getIndices() const { return m_indices; } samp_ptr_type getVals() const { return m_vals; } GeometryScope getScope() const { return m_scope; } bool isIndexed() const { return m_isIndexed; } void reset() { m_vals.reset(); m_indices.reset(); m_scope = kUnknownScope; m_isIndexed = false; } bool valid() const { return m_vals.get() != NULL; } ALEMBIC_OPERATOR_BOOL( valid() ); protected: friend class ITypedGeomParam; samp_ptr_type m_vals; Abc::UInt32ArraySamplePtr m_indices; GeometryScope m_scope; bool m_isIndexed; }; //-************************************************************************* typedef ITypedGeomParam this_type; typedef typename this_type::Sample sample_type; static const char * getInterpretation() { return TRAITS::interpretation(); } static bool matches( const AbcA::PropertyHeader &iHeader, SchemaInterpMatching iMatching = kStrictMatching ) { if ( iHeader.isCompound() ) { return ( iHeader.getMetaData().get( "podName" ) == Alembic::Util::PODName( TRAITS::dataType().getPod() ) && ( std::string() == getInterpretation() || atoi( iHeader.getMetaData().get( "podExtent" ).c_str() ) == TRAITS::dataType().getExtent() ) ) && prop_type::matches( iHeader.getMetaData(), iMatching ); } else if ( iHeader.isArray() ) { return prop_type::matches( iHeader, iMatching ); } return false; } ITypedGeomParam() {m_isIndexed = false;} template ITypedGeomParam( CPROP iParent, const std::string &iName, const Abc::Argument &iArg0 = Abc::Argument(), const Abc::Argument &iArg1 = Abc::Argument() ); template ITypedGeomParam( PROP iThis, WrapExistingFlag iWrapFlag, const Abc::Argument &iArg0 = Abc::Argument(), const Abc::Argument &iArg1 = Abc::Argument() ); void getIndexed( sample_type &oSamp, const Abc::ISampleSelector &iSS = Abc::ISampleSelector() ) const; void getExpanded( sample_type &oSamp, const Abc::ISampleSelector &iSS = Abc::ISampleSelector() ) const; sample_type getIndexedValue( const Abc::ISampleSelector &iSS = \ Abc::ISampleSelector() ) const { sample_type ret; getIndexed( ret, iSS ); return ret; } sample_type getExpandedValue( const Abc::ISampleSelector &iSS = \ Abc::ISampleSelector() ) const { sample_type ret; getExpanded( ret, iSS ); return ret; } size_t getNumSamples() const; AbcA::DataType getDataType() const { return TRAITS::dataType(); } size_t getArrayExtent() const { std::string e = m_valProp.getMetaData().get( "arrayExtent" ); if ( e == "" ) { return 1; } else { return atoi( e.c_str() ); } } bool isIndexed() const { return m_isIndexed; } GeometryScope getScope() const { return GetGeometryScope( m_valProp.getMetaData() ); } AbcA::TimeSamplingPtr getTimeSampling() const; const std::string &getName() const; Abc::ICompoundProperty getParent() const; const AbcA::PropertyHeader &getHeader() const; const AbcA::MetaData &getMetaData() const; bool isConstant() const; void reset() { m_valProp.reset(); m_indicesProperty.reset(); m_cprop.reset(); m_isIndexed = false; } bool valid() const { return ( m_valProp.valid() && ( ( ! m_cprop ) || m_indicesProperty ) ); } ALEMBIC_OPERATOR_BOOL( this_type::valid() ); prop_type getValueProperty() { return m_valProp; } Abc::IUInt32ArrayProperty getIndexProperty() { return m_indicesProperty; } private: Abc::ErrorHandler &getErrorHandler() const { return m_valProp.getErrorHandler(); } protected: prop_type m_valProp; // if the GeomParam is not indexed, these will not exist. Abc::IUInt32ArrayProperty m_indicesProperty; Abc::ICompoundProperty m_cprop; bool m_isIndexed; }; //-***************************************************************************** // TEMPLATED METHODS AND CONSTRUCTORS //-***************************************************************************** template template ITypedGeomParam::ITypedGeomParam( CPROP iParent, const std::string &iName, const Abc::Argument &iArg0, const Abc::Argument &iArg1 ) { Arguments args( GetErrorHandlerPolicy( iParent ) ); iArg0.setInto( args ); iArg1.setInto( args ); getErrorHandler().setPolicy( args.getErrorHandlerPolicy() ); ALEMBIC_ABC_SAFE_CALL_BEGIN( "ITypedGeomParam::ITypedGeomParam()" ); AbcA::CompoundPropertyReaderPtr parent = GetCompoundPropertyReaderPtr( iParent ); ABCA_ASSERT( parent != NULL, "NULL CompoundPropertyReader passed into " << "ITypedGeomParam ctor" ); const AbcA::PropertyHeader *pheader = parent->getPropertyHeader( iName ); ABCA_ASSERT( pheader != NULL, "Nonexistent GeomParam: " << iName ); if ( pheader->isCompound() ) { // we're indexed m_cprop = ICompoundProperty( iParent, iName, args.getErrorHandlerPolicy() ); m_indicesProperty = IUInt32ArrayProperty( m_cprop, ".indices", iArg0, iArg1 ); m_valProp = ITypedArrayProperty( m_cprop, ".vals", iArg0, iArg1 ); m_isIndexed = true; } else if ( pheader->isArray() ) { // not indexed m_valProp = ITypedArrayProperty( iParent, iName, iArg0, iArg1 ); m_isIndexed = false; } else { ABCA_ASSERT( false, "Invalid ITypedGeomParam: " << iName ); } ALEMBIC_ABC_SAFE_CALL_END(); } //-***************************************************************************** template template ITypedGeomParam::ITypedGeomParam( PROP iThis, WrapExistingFlag iWrapFlag, const Abc::Argument &iArg0, const Abc::Argument &iArg1 ) { Arguments args( GetErrorHandlerPolicy( iThis ) ); iArg0.setInto( args ); iArg1.setInto( args ); getErrorHandler().setPolicy( args.getErrorHandlerPolicy() ); ALEMBIC_ABC_SAFE_CALL_BEGIN( "ITypedGeomParam::ITypedGeomParam( wrap )" ); const AbcA::PropertyHeader &ph = iThis.getHeader(); ABCA_ASSERT( matches( ph, Abc::GetSchemaInterpMatching( iArg0, iArg1 ) ), "Property " << ph.getName() << " is not an " << "ITypedGeomParam" ); ABCA_ASSERT( ! ph.isScalar(), "Property " << ph.getName() << " cannot be an ITypedGeomParam" ); if ( ph.isCompound() ) { // we're indexed m_cprop = ICompoundProperty( iThis, iWrapFlag, iArg0, iArg1 ); m_indicesProperty = IUInt32ArrayProperty( m_cprop, ".indices", iArg0, iArg1 ); m_valProp = ITypedArrayProperty( m_cprop, ".vals", iArg0, iArg1 ); m_isIndexed = true; } else { // not indexed m_valProp = ITypedArrayProperty( iThis, iWrapFlag, iArg0, iArg1 ); m_isIndexed = false; } ALEMBIC_ABC_SAFE_CALL_END_RESET(); } //-***************************************************************************** template void ITypedGeomParam::getIndexed( typename ITypedGeomParam::Sample &oSamp, const Abc::ISampleSelector &iSS ) const { m_valProp.get( oSamp.m_vals, iSS ); if ( m_indicesProperty ) { m_indicesProperty.get( oSamp.m_indices, iSS ); } else { uint32_t size = static_cast< uint32_t > ( oSamp.m_vals->size() ); uint32_t *v = new uint32_t[size]; for ( uint32_t i = 0 ; i < size ; ++i ) { v[i] = i; } const Alembic::Util::Dimensions dims( size ); oSamp.m_indices.reset( new Abc::UInt32ArraySample( v, dims ), AbcA::TArrayDeleter() ); } oSamp.m_scope = this->getScope(); oSamp.m_isIndexed = m_isIndexed; } //-***************************************************************************** template void ITypedGeomParam::getExpanded( typename ITypedGeomParam::Sample &oSamp, const Abc::ISampleSelector &iSS ) const { oSamp.m_scope = this->getScope(); oSamp.m_isIndexed = m_isIndexed; if ( ! m_indicesProperty ) { m_valProp.get( oSamp.m_vals, iSS ); } else { Abc::UInt32ArraySamplePtr idxPtr = m_indicesProperty.getValue( iSS ); size_t size = idxPtr->size(); // no indices? just return what we have in our values if (size == 0) { m_valProp.get( oSamp.m_vals, iSS ); return; } Alembic::Util::shared_ptr< Abc::TypedArraySample > valPtr = \ m_valProp.getValue( iSS ); typename TRAITS::value_type *v = new typename TRAITS::value_type[size]; for ( size_t i = 0 ; i < size ; ++i ) { v[i] = (*valPtr)[ (*idxPtr)[i] ]; } // NOTE: we could create an ArraySampleKey and insert this into the // cache. If it were in the cache, we'd return the shared pointer // from there and call delete[] on v, thus preventing potentially // unbounded heap growth. We should probably do this, but for now, // we'll assume that we don't have too many of these, and we're not // tightly memory-constrained. Ha! const Alembic::Util::Dimensions dims( size ); oSamp.m_vals.reset( new Abc::TypedArraySample( v, dims ), AbcA::TArrayDeleter()); } } //-***************************************************************************** template size_t ITypedGeomParam::getNumSamples() const { ALEMBIC_ABC_SAFE_CALL_BEGIN( "ITypedGeomParam::getNumSamples()" ); if ( m_isIndexed ) { return std::max( m_indicesProperty.getNumSamples(), m_valProp.getNumSamples() ); } else { if ( m_valProp ) { return m_valProp.getNumSamples(); } else { return 0; } } ALEMBIC_ABC_SAFE_CALL_END(); return 0; } //-***************************************************************************** template bool ITypedGeomParam::isConstant() const { ALEMBIC_ABC_SAFE_CALL_BEGIN( "ITypedGeomParam::isConstant()" ); if ( m_isIndexed ) { return m_valProp.isConstant() && m_indicesProperty.isConstant(); } else { return m_valProp.isConstant(); } ALEMBIC_ABC_SAFE_CALL_END(); return false; } namespace { const std::string g_emptyStr; } //-***************************************************************************** template const std::string &ITypedGeomParam::getName() const { ALEMBIC_ABC_SAFE_CALL_BEGIN( "ITypedGeomParam::getName()" ); if ( m_isIndexed ) { return m_cprop.getName(); } else { return m_valProp.getName(); } ALEMBIC_ABC_SAFE_CALL_END(); return g_emptyStr; } //-***************************************************************************** template Abc::ICompoundProperty ITypedGeomParam::getParent() const { if ( m_isIndexed ) { return m_cprop.getParent(); } else { return m_valProp.getParent(); } } //-***************************************************************************** template const AbcA::PropertyHeader &ITypedGeomParam::getHeader() const { if ( m_isIndexed ) { return m_cprop.getHeader(); } else { return m_valProp.getHeader(); } } //-***************************************************************************** template const AbcA::MetaData &ITypedGeomParam::getMetaData() const { if ( m_isIndexed ) { return m_cprop.getMetaData(); } else { return m_valProp.getMetaData(); } } //-***************************************************************************** template AbcA::TimeSamplingPtr ITypedGeomParam::getTimeSampling() const { if ( m_valProp ) { return m_valProp.getTimeSampling(); } else if ( m_indicesProperty ) { return m_indicesProperty.getTimeSampling(); } return AbcA::TimeSamplingPtr(); } //-***************************************************************************** // TYPEDEFS //-***************************************************************************** typedef ITypedGeomParam IBoolGeomParam; typedef ITypedGeomParam IUcharGeomParam; typedef ITypedGeomParam ICharGeomParam; typedef ITypedGeomParam IUInt16GeomParam; typedef ITypedGeomParam IInt16GeomParam; typedef ITypedGeomParam IUInt32GeomParam; typedef ITypedGeomParam IInt32GeomParam; typedef ITypedGeomParam IUInt64GeomParam; typedef ITypedGeomParam IInt64GeomParam; typedef ITypedGeomParam IHalfGeomParam; typedef ITypedGeomParam IFloatGeomParam; typedef ITypedGeomParam IDoubleGeomParam; typedef ITypedGeomParam IStringGeomParam; typedef ITypedGeomParam IWstringGeomParam; typedef ITypedGeomParam IV2sGeomParam; typedef ITypedGeomParam IV2iGeomParam; typedef ITypedGeomParam IV2fGeomParam; typedef ITypedGeomParam IV2dGeomParam; typedef ITypedGeomParam IV3sGeomParam; typedef ITypedGeomParam IV3iGeomParam; typedef ITypedGeomParam IV3fGeomParam; typedef ITypedGeomParam IV3dGeomParam; typedef ITypedGeomParam IP2sGeomParam; typedef ITypedGeomParam IP2iGeomParam; typedef ITypedGeomParam IP2fGeomParam; typedef ITypedGeomParam IP2dGeomParam; typedef ITypedGeomParam IP3sGeomParam; typedef ITypedGeomParam IP3iGeomParam; typedef ITypedGeomParam IP3fGeomParam; typedef ITypedGeomParam IP3dGeomParam; typedef ITypedGeomParam IBox2sGeomParam; typedef ITypedGeomParam IBox2iGeomParam; typedef ITypedGeomParam IBox2fGeomParam; typedef ITypedGeomParam IBox2dGeomParam; typedef ITypedGeomParam IBox3sGeomParam; typedef ITypedGeomParam IBox3iGeomParam; typedef ITypedGeomParam IBox3fGeomParam; typedef ITypedGeomParam IBox3dGeomParam; typedef ITypedGeomParam IM33fGeomParam; typedef ITypedGeomParam IM33dGeomParam; typedef ITypedGeomParam IM44fGeomParam; typedef ITypedGeomParam IM44dGeomParam; typedef ITypedGeomParam IQuatfGeomParam; typedef ITypedGeomParam IQuatdGeomParam; typedef ITypedGeomParam IC3hGeomParam; typedef ITypedGeomParam IC3fGeomParam; typedef ITypedGeomParam IC3cGeomParam; typedef ITypedGeomParam IC4hGeomParam; typedef ITypedGeomParam IC4fGeomParam; typedef ITypedGeomParam IC4cGeomParam; typedef ITypedGeomParam IN2fGeomParam; typedef ITypedGeomParam IN2dGeomParam; typedef ITypedGeomParam IN3fGeomParam; typedef ITypedGeomParam IN3dGeomParam; } // End namespace ALEMBIC_VERSION_NS using namespace ALEMBIC_VERSION_NS; } // End namespace AbcGeom } // End namespace Alembic #endif