关于GGUI视图设置

我想实现对三维空间中粒子的可视化,一个简单的代码如下:

import taichi as ti
import numpy as np

ti.init(ti.gpu)

np.random.seed(10)
N = 1000
L = 10
pos = np.random.random((N, 3)) * L
particles_pos = ti.Vector.field(3, dtype=ti.f32, shape=N)
particles_pos.from_numpy(pos)
lines = ti.Vector.field(3, dtype=ti.f32, shape=6)
# line 1
lines[0] = [0.0, 0.0, 0.0]
lines[1] = [L, 0.0, 0.0]
# line 2
lines[3] = [0.0, 0.0, 0.0]
lines[4] = [0.0, L, 0.0]
# line 3
lines[5] = [0.0, 0.0, 0.0]
lines[6] = [0.0, 0.0, L]

window = ti.ui.Window("Test for Drawing 3d-lines", (768, 768))
canvas = window.get_canvas()
scene = ti.ui.Scene()
camera = ti.ui.Camera()
camera.position(-20, -20, 30)
camera.lookat(0, 0, 10)

while window.running:
    camera.track_user_inputs(window, movement_speed=0.03, hold_key=ti.ui.LMB)
    scene.set_camera(camera)
    scene.ambient_light((0.8, 0.8, 0.8))
    scene.point_light(pos=(0.5, 1.5, 1.5), color=(1, 1, 1))
    scene.particles(particles_pos, color=(0.68, 0.26, 0.19), radius=0.4)
    scene.lines(lines, color=(0.28, 0.68, 0.99), width=2.0)
    canvas.scene(scene)
    window.show()

遇见的问题:

  1. 我这里设置了三条线,但是我画出来显示只有两条线。
  2. 我不知道如何调整观看的视角。
  3. 我想实现对这堆粒子整体旋转,缩放等操作,应该如何实现。
    感谢大家的帮助。
    这个运行出来图片如下:
  1. index写岔了,改成这样就3条线了
# line 1
lines[0] = [0.0, 0.0, 0.0]
lines[1] = [L, 0.0, 0.0]
# line 2
lines[2] = [0.0, 0.0, 0.0]
lines[3] = [0.0, L, 0.0]
# line 3
lines[4] = [0.0, 0.0, 0.0]
lines[5] = [0.0, 0.0, L]
  1. 用鼠标左键和wasd都可以调整视角。详见文档 A New UI system: GGUI | Taichi Docs
  2. 太极好像还没有直接对粒子的旋转和缩放操作。用你的代码加了个用四元数旋转的例子。参考Quaternions and spatial rotation - Wikipedia
import taichi as ti
import numpy as np
import time
ti.init(ti.gpu)

np.random.seed(10)
N = 1000
L = 10
pos = np.random.random((N, 3)) * L

x = ti.Vector.field(3, dtype=ti.f32, shape=N)
x.from_numpy(pos)

lines = ti.Vector.field(3, dtype=ti.f32, shape=6)
lines[0] = [0.0, 0.0, 0.0]
lines[1] = [L, 0.0, 0.0]
lines[2] = [0.0, 0.0, 0.0]
lines[3] = [0.0, L, 0.0]
lines[4] = [0.0, 0.0, 0.0]
lines[5] = [0.0, 0.0, L]


def axis_to_quaternion(arg):
    sin_theta = ti.math.sin(arg[0]/2)
    return ti.Vector([ti.math.cos(arg[0]/2),sin_theta * arg[1], sin_theta * arg[2], sin_theta * arg[3]])

@ti.kernel
def rotate(quaternion: ti.types.vector(4,ti.f32)): # quaternion rotation from wiki
    for p in x:
        complex_p = [0.0, x[p][0], x[p][1], x[p][2]]
        complex_q = quaternion
        complex_q_inv = [quaternion[0], -quaternion[1], -quaternion[2], -quaternion[3]]
        p_prime = quat_mul(quat_mul(complex_q, complex_p),complex_q_inv)
        x[p][0], x[p][1], x[p][2] = p_prime[1] , p_prime[2] , p_prime[3] 

@ti.func
def quat_mul(q1, q2):# quaternion multiplication from wiki
    a1, b1, c1, d1 = q1[0], q1[1], q1[2], q1[3]
    a2, b2, c2, d2 = q2[0], q2[1], q2[2], q2[3]
    t1 = a1 * a2 - b1 * b2 - c1 * c2 - d1 * d2
    t2 = a1 * b2 + b1 * a2 + c1 * d2 - d1 * c2
    t3 = a1 * c2 - b1 * d2 + c1 * a2 + d1 * b2
    t4 = a1 * d2 + b1 * c2 - c1 * b2 + d1 * a2
    result = [t1, t2, t3, t4]
    return result


window = ti.ui.Window("Test for Drawing 3d-lines", (768, 768))
canvas = window.get_canvas()
scene = ti.ui.Scene()
camera = ti.ui.Camera()
camera.position(-20, -20, 30)
camera.lookat(0, 0, 0)


counter = 0
while window.running:
    
    # rotate 1 degree around axis [1.0, 0.0, 0.0] per 0.1 second
    axis_angle = [1 / 180 * ti.math.pi, 1.0, 0.0, 0.0]
    rotate(axis_to_quaternion(axis_angle))
    time.sleep(0.1)
    
    camera.track_user_inputs(window, movement_speed=0.03, hold_key=ti.ui.LMB)
    scene.set_camera(camera)
    scene.ambient_light((0.8, 0.8, 0.8))
    scene.point_light(pos=(0.5, 1.5, 1.5), color=(1, 1, 1))
    scene.particles(x, color=(0.68, 0.26, 0.19), radius=0.4)
    scene.lines(lines, color=(0.28, 0.68, 0.99), width=2.0)
    canvas.scene(scene)
    window.show()

感谢指导