Example

Polyline Interpolation

The example below samples a rectangle-shaped polyline at a handful of distances.

Important

You can run the example using the script packages/lane_helpers/examples/basic_usage.py.

packages/lane_helpers/examples/basic_usage.py
 1# Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 2#
 3# Licensed under the Apache License, Version 2.0 (the "License");
 4# you may not use this file except in compliance with the License.
 5# You may obtain a copy of the License at
 6#
 7#     http://www.apache.org/licenses/LICENSE-2.0
 8#
 9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import torch
16
17from accvlab.lane_helpers import polyline
18
19
20def main() -> None:
21    if not torch.cuda.is_available():
22        raise RuntimeError("This example requires a CUDA-capable PyTorch installation.")
23
24    # @NOTE Use one rectangle polyline with shape (batch=1, num_points=5, num_dims=2).
25    points = torch.tensor(
26        [
27            [
28                [0.0, 0.0],
29                [1.0, 0.0],
30                [1.0, 2.0],
31                [0.0, 2.0],
32                [0.0, 0.0],
33            ]
34        ],
35        device="cuda",
36        dtype=torch.float32,
37    )
38
39    # @NOTE Use a tensor of distances to sample the polyline at (batch=1, num_distances=5).
40    distances = torch.tensor([[0.0, 0.5, 1.0, 3.0, 6.0]], device="cuda", dtype=torch.float32)
41
42    # @NOTE Interpolate the polyline at the given distances.
43    sampled_points = polyline.interpolate(points, distances)
44    # @NOTE Compute the length of the polyline.
45    line_lengths = polyline.lengths(points)
46
47    # @NOTE Print the results.
48    print(f"Interpolated points:\n{sampled_points}")
49    print(f"Line length(s): {line_lengths}")
50
51
52if __name__ == "__main__":
53    main()