Tutorial 6 - Array of Tuples

Arrays and tuples can be combined to create common attribute types such as float[3][], and array of 3 floats. This node takes two arrays of float[3]s and generates an output array consisting of the element-wise dot products.

The ogn file shows the implementation of a node named “omni.graph.tutorials.TupleArrays”, which has two tuple-array inputs and a simple array output.

OgnTutorialTupleArrays.ogn

 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
{
    "TupleArrays": {
        "version": 1,
        "categories": "tutorials",
        "description": ["This is a tutorial node. It will compute the float array 'result' as the elementwise dot ",
                        "product of the input arrays 'a' and 'b'."
        ],
        "metadata":
        {
           "uiName": "Tutorial Node: Attributes With Arrays of Tuples"
        },
        "inputs": {
            "a": {
                "description": "First array",
                "type": "float[3][]",
                "default": []
            },
            "b": {
                "description": "Second array",
                "type": "float[3][]",
                "default": []
            }
        },
        "outputs": {
            "result": {
                "description": "Dot-product array",
                "type": "float[]",
                "default": []
            }
        },
        "tests": [
            {
                "inputs:a": [[1.0, 2.0, 3.0],[2.0, 3.0, 4.0]],
                "inputs:b": [[10.0, 5.0, 1.0],[1.0, 5.0, 10.0]],
                "outputs:result": [23.0, 57.0]
            }
        ]
    }
}

OgnTutorialTupleArrays.cpp

The cpp file contains the implementation of the compute method, which computes the dot products.

 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
// Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto.  Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION is strictly prohibited.
//
#include <OgnTutorialTupleArraysDatabase.h>
#include <algorithm>

class OgnTutorialTupleArrays
{
public:
    static bool compute(OgnTutorialTupleArraysDatabase& db)
    {
        // Use the "auto&" declarations to avoid long type names, prone to error
        const auto& a = db.inputs.a();
        const auto& b = db.inputs.b();
        auto& result = db.outputs.result();

        // Some simple error checking never hurts
        if (a.size() != b.size())
        {
            db.logWarning("Input sizes did not match (%zu versus %zu)", a.size(), b.size());
            return false;
        }

        // The output contents are unrelated to the input contents so resize rather than copying
        result.resize(a.size());

        // Illustrating how simple these operations are thanks to iterators and the built-in operator*
        std::transform(a.begin(), a.end(), b.begin(), result.begin(),
                       [](const GfVec3f& valueA, const GfVec3f& valueB) -> float { return valueA * valueB; });

        return true;
    }
};

REGISTER_OGN_NODE()

There are typedefs set up for USD-compatible types, e.g. for float[3] you get GfVec3f. Other types, for whom there is no USD equivalent, are implemented as ogn::tuple<TYPE, N>. See the complete table of data types in Attribute Data Types.

Database Function

Returned Type

inputs.a()

const ogn::const_array<GfVec3f>&

inputs.b()

const ogn::const_array<GfVec3f>&

outputs.result()

ogn::array<GfVec3f>&

Note that the tuple array access is identical to the simple data array access, except that the types are now the compound tuple types.