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    "TupleArrays": {
 3        "version": 1,
 4        "categories": "tutorials",
 5        "scheduling": ["threadsafe"],
 6        "description": ["This is a tutorial node. It will compute the float array 'result' as the elementwise dot ",
 7                        "product of the input arrays 'a' and 'b'."
 8        ],
 9        "metadata":
10        {
11           "uiName": "Tutorial Node: Attributes With Arrays of Tuples"
12        },
13        "inputs": {
14            "a": {
15                "description": "First array",
16                "type": "float[3][]",
17                "default": []
18            },
19            "b": {
20                "description": "Second array",
21                "type": "float[3][]",
22                "default": []
23            }
24        },
25        "outputs": {
26            "result": {
27                "description": "Dot-product array",
28                "type": "float[]",
29                "default": []
30            }
31        },
32        "tests": [
33            {
34                "inputs:a": [[1.0, 2.0, 3.0],[2.0, 3.0, 4.0]],
35                "inputs:b": [[10.0, 5.0, 1.0],[1.0, 5.0, 10.0]],
36                "outputs:result": [23.0, 57.0]
37            }
38        ]
39    }
40}

OgnTutorialTupleArrays.cpp

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

 1// SPDX-FileCopyrightText: Copyright (c) 2020-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 2// SPDX-License-Identifier: LicenseRef-NvidiaProprietary
 3//
 4// NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
 5// property and proprietary rights in and to this material, related
 6// documentation and any modifications thereto. Any use, reproduction,
 7// disclosure or distribution of this material and related documentation
 8// without an express license agreement from NVIDIA CORPORATION or
 9// its affiliates is strictly prohibited.
10#include <OgnTutorialTupleArraysDatabase.h>
11#include <algorithm>
12
13class OgnTutorialTupleArrays
14{
15public:
16    static bool compute(OgnTutorialTupleArraysDatabase& db)
17    {
18        // Use the "auto&" declarations to avoid long type names, prone to error
19        const auto& a = db.inputs.a();
20        const auto& b = db.inputs.b();
21        auto& result = db.outputs.result();
22
23        // Some simple error checking never hurts
24        if (a.size() != b.size())
25        {
26            db.logWarning("Input sizes did not match (%zu versus %zu)", a.size(), b.size());
27            return false;
28        }
29
30        // The output contents are unrelated to the input contents so resize rather than copying
31        result.resize(a.size());
32
33        // Illustrating how simple these operations are thanks to iterators and the built-in operator*
34        std::transform(a.begin(), a.end(), b.begin(), result.begin(),
35                       [](const GfVec3f& valueA, const GfVec3f& valueB) -> float { return valueA * valueB; });
36
37        return true;
38    }
39};
40
41REGISTER_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 omni.graph.docs.ogn_attribute_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.