File size: 9,334 Bytes
66c9c8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# Copyright (c) 2023 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.

"""Helpers to author point cloud geometries represented as OmniGraph bundles."""

from math import inf
from typing import Optional

import numpy as np
import omni.graph.core as og
import warp as wp

from omni.warp.nodes._impl.attributes import (
    attr_get,
    attr_get_array_on_gpu,
    attr_set,
)
from omni.warp.nodes._impl.bundles import (
    bundle_copy_attr_value,
    bundle_create_attr,
    bundle_create_child,
    bundle_create_metadata_attr,
    bundle_get_attr,
    bundle_get_world_xform,
    bundle_set_prim_type,
    bundle_set_world_xform,
)


#   Public API
# ------------------------------------------------------------------------------


def points_create_bundle(
    dst_bundle: og.BundleContents,
    point_count: int,
    xform: Optional[np.ndarray] = None,
    create_display_color: bool = False,
    create_masses: bool = False,
    create_velocities: bool = False,
    create_widths: bool = False,
    child_idx: int = 0,
) -> None:
    """Creates and initializes point cloud attributes within a bundle."""
    child_bundle = bundle_create_child(dst_bundle, child_idx)
    bundle_create_attr(
        child_bundle,
        "points",
        og.Type(
            og.BaseDataType.FLOAT,
            tuple_count=3,
            array_depth=1,
            role=og.AttributeRole.POSITION,
        ),
        size=point_count,
    )

    bundle_set_prim_type(dst_bundle, "Points", child_idx=child_idx)

    if xform is not None:
        bundle_set_world_xform(dst_bundle, xform, child_idx=child_idx)

    if create_display_color:
        bundle_create_attr(
            child_bundle,
            "primvars:displayColor",
            og.Type(
                og.BaseDataType.FLOAT,
                tuple_count=3,
                array_depth=1,
                role=og.AttributeRole.COLOR,
            ),
            size=point_count,
        )
        interp_attr = bundle_create_metadata_attr(
            child_bundle,
            "primvars:displayColor",
            "interpolation",
            og.Type(
                og.BaseDataType.TOKEN,
                tuple_count=1,
                array_depth=0,
                role=og.AttributeRole.NONE,
            ),
        )
        attr_set(interp_attr, "vertex")

    if create_masses:
        bundle_create_attr(
            child_bundle,
            "masses",
            og.Type(
                og.BaseDataType.FLOAT,
                tuple_count=1,
                array_depth=1,
                role=og.AttributeRole.NONE,
            ),
            size=point_count,
        )

    if create_velocities:
        bundle_create_attr(
            child_bundle,
            "velocities",
            og.Type(
                og.BaseDataType.FLOAT,
                tuple_count=3,
                array_depth=1,
                role=og.AttributeRole.VECTOR,
            ),
            size=point_count,
        )

    if create_widths:
        bundle_create_attr(
            child_bundle,
            "widths",
            og.Type(
                og.BaseDataType.FLOAT,
                tuple_count=1,
                array_depth=1,
                role=og.AttributeRole.NONE,
            ),
            size=point_count,
        )


def points_copy_bundle(
    dst_bundle: og.BundleContents,
    src_bundle: og.BundleContents,
    deep_copy: bool = False,
    child_idx: int = 0,
) -> None:
    """Creates and initializes points attributes from an existing bundle."""
    dst_child_bundle = bundle_create_child(dst_bundle, child_idx)
    src_child_bundle = src_bundle.bundle.get_child_bundle(child_idx)
    dst_child_bundle.copy_bundle(src_child_bundle)

    if deep_copy:
        bundle_copy_attr_value(dst_child_bundle, src_child_bundle, "points", wp.vec3)
        bundle_copy_attr_value(dst_child_bundle, src_child_bundle, "primvars:displayColor", wp.vec3)
        bundle_copy_attr_value(dst_child_bundle, src_child_bundle, "masses", float)
        bundle_copy_attr_value(dst_child_bundle, src_child_bundle, "velocities", wp.vec3)
        bundle_copy_attr_value(dst_child_bundle, src_child_bundle, "widths", float)


def points_get_point_count(
    bundle: og.BundleContents,
    child_idx: int = 0,
) -> int:
    """Retrieves the number of points."""
    return bundle_get_attr(bundle, "points", child_idx).size()


def points_get_points(
    bundle: og.BundleContents,
    child_idx: int = 0,
) -> wp.array(dtype=wp.vec3):
    """Retrieves the bundle points attribute as a Warp array."""
    attr = bundle_get_attr(bundle, "points", child_idx)
    return attr_get_array_on_gpu(attr, wp.vec3, read_only=bundle.read_only)


def points_get_velocities(
    bundle: og.BundleContents,
    child_idx: int = 0,
) -> wp.array(dtype=wp.vec3):
    """Retrieves the bundle velocities attribute as a Warp array."""
    attr = bundle_get_attr(bundle, "velocities", child_idx)
    return attr_get_array_on_gpu(attr, wp.vec3, read_only=bundle.read_only)


def points_get_widths(
    bundle: og.BundleContents,
    child_idx: int = 0,
) -> wp.array(dtype=float):
    """Retrieves the bundle widths attribute as a Warp array."""
    attr = bundle_get_attr(bundle, "widths", child_idx)
    return attr_get_array_on_gpu(attr, float, read_only=bundle.read_only)


def points_get_masses(
    bundle: og.BundleContents,
    child_idx: int = 0,
) -> wp.array(dtype=float):
    """Retrieves the bundle masses attribute as a Warp array."""
    attr = bundle_get_attr(bundle, "masses", child_idx)
    return attr_get_array_on_gpu(attr, float, read_only=bundle.read_only)


def points_get_display_color(
    bundle: og.BundleContents,
    child_idx: int = 0,
) -> wp.array(dtype=wp.vec3):
    """Retrieves the bundle display color attribute as a Warp array."""
    attr = bundle_get_attr(bundle, "primvars:displayColor", child_idx)
    return attr_get_array_on_gpu(attr, wp.vec3, read_only=bundle.read_only)


def points_get_local_extent(
    bundle: og.BundleContents,
    child_idx: int = 0,
) -> np.ndarray:
    """Retrieves the local extent of the geometry points."""
    # Some standard workflows include a single 'extent' attribute when defining
    # geometry primitives on the stage.
    attr = bundle_get_attr(bundle, "extent", child_idx)
    if attr is not None:
        return attr_get(attr)

    # Alternatively, the ReadPrims node offers an option to compute the bounding
    # box which results in a triple of 'bboxMinCorner', 'bboxMaxCorner',
    # and 'bboxTransform' attributes.
    min_attr = bundle_get_attr(bundle, "bboxMinCorner", child_idx)
    max_attr = bundle_get_attr(bundle, "bboxMaxCorner", child_idx)
    if min_attr is not None and max_attr is not None:
        return np.stack(
            (
                attr_get(min_attr),
                attr_get(max_attr),
            ),
        )

    # The last resort is to compute the extent ourselves from
    # the point positions.
    points = points_get_points(bundle, child_idx=child_idx)
    min_extent = wp.array((+inf, +inf, +inf), dtype=wp.vec3)
    max_extent = wp.array((-inf, -inf, -inf), dtype=wp.vec3)
    wp.launch(
        _compute_extent_kernel,
        dim=len(points),
        inputs=[points],
        outputs=[min_extent, max_extent],
    )
    return np.concatenate((min_extent.numpy(), max_extent.numpy()))


def points_get_world_extent(
    bundle: og.BundleContents,
    axis_aligned: bool = False,
    child_idx: int = 0,
) -> np.ndarray:
    """Retrieves the world extent of the geometry points."""
    extent = points_get_local_extent(bundle, child_idx=child_idx)
    xform = bundle_get_world_xform(bundle, child_idx=child_idx)

    if axis_aligned:
        points = np.array(
            (
                (extent[0][0], extent[0][1], extent[0][2]),
                (extent[0][0], extent[0][1], extent[1][2]),
                (extent[0][0], extent[1][1], extent[0][2]),
                (extent[0][0], extent[1][1], extent[1][2]),
                (extent[1][0], extent[1][1], extent[1][2]),
                (extent[1][0], extent[0][1], extent[1][2]),
                (extent[1][0], extent[1][1], extent[0][2]),
                (extent[1][0], extent[0][1], extent[0][2]),
            ),
        )
    else:
        points = extent

    points = np.pad(points, ((0, 0), (0, 1)), constant_values=1)
    points = np.dot(xform.T, points[:, :, None]).squeeze()[:-1, :].T
    return np.array(
        (
            np.amin(points, axis=0),
            np.amax(points, axis=0),
        )
    )


#   Private Helpers
# ------------------------------------------------------------------------------


@wp.kernel(enable_backward=False)
def _compute_extent_kernel(
    points: wp.array(dtype=wp.vec3),
    out_min_extent: wp.array(dtype=wp.vec3),
    out_max_extent: wp.array(dtype=wp.vec3),
):
    """Computes the extent of a point cloud."""
    tid = wp.tid()
    wp.atomic_min(out_min_extent, 0, points[tid])
    wp.atomic_max(out_max_extent, 0, points[tid])