Tutorial 5 - Array Data Node

Array data consists of multiple elements of a simple type whose count is only known at runtime. For example float[] or double[]. This node takes an array of floats and a multiplier and generates an output array consisting of the product of the two.

OgnTutorialArrayData.ogn

The ogn file shows the implementation of a node named “omni.graph.tutorials.ArrayData”, which has a float value and float array input with one float array output.

 1{
 2    "ArrayData": {
 3        "version": 1,
 4        "categories": "tutorials",
 5        "scheduling": ["threadsafe"],
 6        "description": ["This is a tutorial node. It will compute the array 'result' as the input array 'original' ",
 7                        "with every element multiplied by the constant 'multiplier'."
 8        ],
 9        "$todo": "Enable the string attributes when fabric fully supports them",
10        "metadata":
11        {
12           "uiName": "Tutorial Node: Array Attributes"
13
14        },
15        "inputs": {
16            "original": {
17                "description": "Array to be multiplied",
18                "type": "float[]",
19                "default": []
20            },
21            "gates": {
22                "description": "Boolean mask telling which elements of the array should be multiplied",
23                "type": "bool[]",
24                "default": []
25            },
26            "multiplier": {
27                "description": "Multiplier of the array elements",
28                "type": "float",
29                "default": 1.0
30            },
31            "info": {
32                "description": "List of strings providing commentary",
33                "type": "token[]",
34                "default": ["There", "is", "no", "data"]
35            }
36        },
37        "outputs": {
38            "result": {
39                "description": "Multiplied array",
40                "type": "float[]"
41            },
42            "negativeValues": {
43                "description": "Array of booleans set to true if the corresponding 'result' is negative",
44                "type": "bool[]"
45            },
46            "infoSize": {
47                "description": "Number of letters in all strings in the info input",
48                "type": "int"
49            }
50        },
51        "tests": [
52            {
53                "inputs:original": [1.0, 2.0, 3.0],
54                "inputs:gates": [true, false, true],
55                "inputs:multiplier": 2.0,
56                "outputs:result": [2.0, 2.0, 6.0],
57                "outputs:negativeValues": [false, false, false],
58                "outputs:infoSize": 13
59            },
60            {
61                "inputs:original": [10.0, -20.0, 30.0],
62                "inputs:gates": [true, false, true],
63                "outputs:result": [10.0, -20.0, 30.0],
64                "outputs:negativeValues": [false, true, false]
65            },
66            {
67                "inputs:info": ["Hello", "lamp\"post", "what'cha", "knowing"],
68                "outputs:infoSize": 29
69            }
70        ]
71    }
72}

OgnTutorialArrayData.cpp

The cpp file contains the implementation of the compute method, which multiplies the float value by each member of the float array.

Note how the attribute Array values can be accessed as though they were a simple std::vector type.

 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 <OgnTutorialArrayDataDatabase.h>
11#include <algorithm>
12
13class OgnTutorialArrayData
14{
15public:
16    static bool compute(OgnTutorialArrayDataDatabase& db)
17    {
18        // For clarity get the attribute values into separate variables. The type definitions are included to
19        // show exactly what data types are returned, though you should know that from your .ogn declaration.
20        // It's sufficient to use "const auto" for inputs or "auto" for outputs.
21        const float multiplier = db.inputs.multiplier();
22        const ::ogn::const_array<float>& inputArray = db.inputs.original();
23        const ::ogn::const_array<bool>& gateArray = db.inputs.gates();
24        ::ogn::array<float>& outputArray = db.outputs.result();
25        ::ogn::array<bool>& negativeValues = db.outputs.negativeValues();
26
27        if (gateArray.size() != inputArray.size())
28        {
29            db.logWarning("Gate array size %zu should equal input array size %zu", gateArray.size(), inputArray.size());
30            return false;
31        }
32
33        // Output array data has to be properly sized before filling as it will contain the same number
34        // of elements as the input the size is already known. You could also do an assignment and modify the
35        // output in place if that makes your algorithm more efficient:
36        //     outputArray = inputArray;
37        outputArray.resize(inputArray.size());
38        negativeValues.resize(inputArray.size());
39
40        // The attribute array data wrapper is compatible with the STL algorithms so computations can be
41        // performed in a single std::algorithm call.
42        std::transform(inputArray.begin(), inputArray.end(), gateArray.begin(), outputArray.begin(),
43                       [multiplier](const float& value, const bool& gate) -> float
44                       { return gate ? value * multiplier : value; });
45        std::transform(outputArray.begin(), outputArray.end(), negativeValues.begin(),
46                       [](const float& value) -> bool { return value < 0.0f; });
47
48        // Regular iterators can also be applied
49        size_t totalCharacters{ 0 };
50        for (const auto& stringInput : db.inputs.info())
51        {
52            totalCharacters += std::strlen(db.tokenToString(stringInput));
53        }
54        db.outputs.infoSize() = int(totalCharacters);
55        return true;
56    }
57};
58
59REGISTER_OGN_NODE()

Array Attribute Access

The attribute access is as described in Tutorial 2 - Simple Data Node except that the exact return types of the attributes are different in order to support array member access.

Full definition of the array wrapper classes can be found in the interface file omni/graph/core/ogn/array.h, though they do behave in a manner consistent with std::array, supporting iterators, standard algorithms, random access through either operator[] or the at(index) function, the empty() and size() functions, and access to the raw underlying data using the function data(). The non-const version also supports assignment and the resize() function for modifying its contents.

Database Function

Returned Type

inputs.original()

const ogn::const_array<float>&

inputs.multiplier()

const float&

outputs.result()

ogn::array<float>&

These wrapper classes are similar in concept to std::span, which handles unmanaged pointer+size data. In this case the data is being managed by the Fabric. Modifications to the wrapper class data will directly modify the underlying data in the Fabric.

You can still use the auto declarations on these types, and the array attributes have an additional size() method added for convenience.

bool compute(OgnTutorialArrayDataDatabase& db)
{
    const auto& multiplier = db.inputs.multiplier();
    const auto& original = db.inputs.original();
    size_t originalSize = db.inputs.original.size();
    auto& result = db.outputs.result();
};