nerfstudio-project / viser

Web-based 3D visualization + Python

Home Page:https://viser.studio/latest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to draw a bounding box?

zebin-dm opened this issue · comments

Neither are great solutions, but you can:

thanks very much.

Try to write a simple code as :

import viser
import numpy as np
import time

server = viser.ViserServer()


def draw_voxel(min_bound, max_bound, voxel_size, color):
    voxel_dim = np.round((max_bound - min_bound) / voxel_size).astype(int)
    xv, yv, zv = np.meshgrid(
        range(voxel_dim[0]), range(voxel_dim[1]), range(voxel_dim[2]),
        indexing='ij')
    vox_coords = np.concatenate([xv.reshape(1, -1), yv.reshape(1, -1), zv.reshape(1, -1)], axis=0).T
    vox_coords = vox_coords * voxel_size + min_bound

    # vox_coords = vox_coords[np.random.randint(0, len(vox_coords) - 1, (32,))]

    for i, vox_coord in enumerate(vox_coords):
        draw_bbox(vox_coord, voxel_size, color, i)


def draw_bbox(center, voxel_size, color, voxel_id):
    positions = np.array([
        [0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0],
        [0, 0, 1], [1, 0, 1], [0, 1, 1], [1, 1, 1]
    ]) + np.array([-0.5, -0.5, -0.5])

    positions = positions * voxel_size + center

    lines = np.array([
        [0, 1], [0, 2], [1, 3], [2, 3],
        [4, 5], [4, 6], [5, 7], [6, 7],
        [0, 4], [1, 5], [2, 6], [3, 7]
    ])

    for i in range(len(lines)):
        server.scene.add_spline_catmull_rom(
            f"/line_{positions[lines[i]]}",
            positions[lines[i]],  # [30,3]
            tension=0.8,
            line_width=1.0,
            color=color,
            segments=100,
        )


color = np.array([1.0, 0, 0])
draw_voxel(np.array([-1, -1, -1]), np.array([1, 1, 1]), 0.5, color)

while True:
    time.sleep(1)