Coverage Report

Created: 2023-09-25 06:10

/src/alembic/lib/Alembic/AbcMaterial/MaterialFlatten.h
Line
Count
Source (jump to first uncovered line)
1
//-*****************************************************************************
2
//
3
// Copyright (c) 2009-2015,
4
//  Sony Pictures Imageworks Inc. and
5
//  Industrial Light & Magic, a division of Lucasfilm Entertainment Company Ltd.
6
//
7
// All rights reserved.
8
//
9
// Redistribution and use in source and binary forms, with or without
10
// modification, are permitted provided that the following conditions are
11
// met:
12
// *       Redistributions of source code must retain the above copyright
13
// notice, this list of conditions and the following disclaimer.
14
// *       Redistributions in binary form must reproduce the above
15
// copyright notice, this list of conditions and the following disclaimer
16
// in the documentation and/or other materials provided with the
17
// distribution.
18
// *       Neither the name of Sony Pictures Imageworks, nor
19
// Industrial Light & Magic, nor the names of their contributors may be used
20
// to endorse or promote products derived from this software without specific
21
// prior written permission.
22
//
23
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
//
35
//-*****************************************************************************
36
37
#ifndef Alembic_AbcMaterial_MaterialFlatten_h
38
#define Alembic_AbcMaterial_MaterialFlatten_h
39
40
#include <Alembic/Util/Export.h>
41
#include <Alembic/AbcMaterial/IMaterial.h>
42
43
namespace Alembic {
44
namespace AbcMaterial {
45
namespace ALEMBIC_VERSION_NS {
46
47
//! Utility class for querying against flattened inheritance hierarchies
48
//! or definitions.
49
class ALEMBIC_EXPORT MaterialFlatten
50
{
51
public:
52
53
    //! Create empty. Use append to add material schema manually
54
    MaterialFlatten();
55
56
    //! Create with a single materialSchema
57
    MaterialFlatten( IMaterialSchema iMaterialSchema );
58
59
    //! Create from a material object. The schemas of matching parent
60
    //! material objects are automatically append
61
    MaterialFlatten( IMaterial iMaterialObject );
62
63
    //! Create from an IObject. This will interpret values for hasMaterial
64
    //! and getMaterialAssignmentPath to flatten the full assignment
65
    //! and inheritance chain. A locally present material is first in the
66
    //! inheritance path. A material assignment is then appended with
67
    //! IMaterial rules.
68
    //!
69
    //! An alternate archive can be optionally specified. If provided,
70
    //! assigned material paths will be traversed within that archive
71
    //! instead of the archive of the object itself.
72
    MaterialFlatten( Abc::IObject iObject,
73
                     Abc::IArchive iAlternateSearchArchive=Abc::IArchive() );
74
    //TODO: need an append equivalent!
75
76
    //! Manually append a schema to the inheritance hierarchy
77
    void append( IMaterialSchema iMaterialSchema );
78
    
79
    //! Append the schemas of matching parent material objects
80
    void append( IMaterial iMaterialObject );
81
    
82
    
83
    //! Returns true is there are no schema in the inheritance path
84
    bool empty();
85
    
86
    //! Fill the list with a union of target names defined within
87
    //! the inheritance hierarchy
88
    void getTargetNames( std::vector<std::string> & oTargetNames );
89
    
90
    //! Fill the list with a union of shader types define for the specified
91
    //! target within the inheritance hierarchy
92
    void getShaderTypesForTarget( const std::string & iTargetName,
93
                                  std::vector<std::string> & oShaderTypeNames );
94
    
95
    //! Returns true and fills result with the shader name of first defined
96
    //! for the target and shaderType within the inheritance hierarchy. False
97
    //! if not defined.
98
    bool getShader( const std::string & iTarget,
99
                    const std::string & iShaderType,
100
                    std::string & oResult );
101
102
    struct ParameterEntry
103
    {
104
        ParameterEntry()
105
        : header(0)
106
0
        {
107
0
        }
108
109
        ParameterEntry( const std::string & iName,
110
                        Abc::ICompoundProperty iParent,
111
                        const AbcCoreAbstract::PropertyHeader * iHeader )
112
        : name(iName)
113
        , parent(iParent)
114
        , header(iHeader)
115
0
        {
116
0
        }
117
118
        bool operator==( const ParameterEntry &iRhs ) const
119
0
        {
120
0
            return name == iRhs.name &&
121
0
                   parent == iRhs.parent &&
122
0
                   header == iRhs.header;
123
0
        }
124
125
        std::string name;
126
        Abc::ICompoundProperty parent;
127
        const AbcCoreAbstract::PropertyHeader * header;
128
        // header is owned by parent, so it is safe to hold onto the pointer
129
    };
130
131
    typedef std::vector<ParameterEntry> ParameterEntryVector;
132
133
    //! Fills result with the parent compound and property header for 
134
    //! shader parameters defined for the target and shader type within
135
    //! the inheritance hierarchy. Shallower definitions mask deeper ones
136
    //! (i.e. you'll only get one entry for a given name)
137
    void getShaderParameters( const std::string & iTarget,
138
                              const std::string & iShaderType,
139
                              ParameterEntryVector & oResult );
140
141
    ///////////////////////////////////////////////////////////////////////////
142
    /// network stuff
143
144
    void getNetworkTerminalTargetNames(std::vector<std::string> & iTargetNames);
145
    void getNetworkTerminalShaderTypesForTarget(
146
        const std::string & iTargetName,
147
        std::vector<std::string> & oShaderTypeNames );
148
149
    bool getNetworkTerminal( const std::string & iTarget,
150
                             const std::string & iShaderType,
151
                             std::string & oNodeName,
152
                             std::string & oOutputName );
153
154
    typedef std::map<std::string, std::string> StringMap;
155
    typedef Alembic::Util::shared_ptr<StringMap> StringMapPtr;
156
    typedef std::vector<IMaterialSchema> SchemaVector;
157
158
    class ALEMBIC_EXPORT NetworkNode
159
    {
160
    public:
161
162
        NetworkNode();
163
164
        bool valid();
165
166
        std::string getName();
167
        bool getTarget( std::string & oResult );
168
        bool getNodeType( std::string & oResult );
169
        void getParameters( ParameterEntryVector & oResult );
170
        
171
        struct Connection
172
        {
173
            Connection( const std::string & iInputName,
174
                        const std::string & iConnectedNodeName,
175
                        const std::string & iConnectedOutputName )
176
            : inputName( iInputName )
177
            , connectedNodeName( iConnectedNodeName )
178
            , connectedOutputName( iConnectedOutputName )
179
0
            {
180
0
            }
181
182
            bool operator==( const Connection & iRhs ) const
183
0
            {
184
0
                return inputName == iRhs.inputName &&
185
0
                       connectedNodeName == iRhs.connectedNodeName &&
186
0
                       connectedOutputName == iRhs.connectedOutputName;
187
0
            }
188
189
            std::string inputName;
190
            std::string connectedNodeName;
191
            std::string connectedOutputName;
192
        };
193
194
        typedef std::vector<Connection> ConnectionVector;
195
196
        void getConnections( ConnectionVector & oResult );
197
198
    private:
199
200
        friend class MaterialFlatten;
201
202
        NetworkNode( const std::string & iName,
203
                     SchemaVector & iSchemas,
204
                     StringMapPtr iInterfaceMappings );
205
206
        std::string m_name;
207
        std::vector<IMaterialSchema::NetworkNode> m_nodes;
208
        std::vector<Abc::ICompoundProperty> m_networkParameters;
209
        StringMapPtr m_interfaceMappings;
210
    };
211
212
    size_t getNumNetworkNodes();
213
    NetworkNode getNetworkNode( size_t iIndex );
214
    NetworkNode getNetworkNode( const std::string & iNodeName );
215
216
    // TODO: no method to get the node names?
217
218
private:
219
220
    SchemaVector m_schemas;
221
222
    void flattenNetwork();
223
224
    bool m_networkFlattened;
225
226
    std::vector<std::string> m_nodeNames;
227
    typedef std::map<std::string, StringMapPtr> StringMapMap;
228
    StringMapMap m_nodesToInterfaceMappings;
229
230
};
231
232
}
233
234
using namespace ALEMBIC_VERSION_NS;
235
236
}
237
}
238
239
#endif