Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit-tests for GGUI features #5917

Closed
9 tasks done
Morcki opened this issue Aug 30, 2022 · 9 comments
Closed
9 tasks done

Add unit-tests for GGUI features #5917

Morcki opened this issue Aug 30, 2022 · 9 comments
Assignees
Labels
feature request Suggest an idea on this project

Comments

@Morcki
Copy link
Member

Morcki commented Aug 30, 2022

Concisely describe the proposed feature
Cause there might be problems when using GGUI if we donnot cover the features that we supported and sometimes it would not report bugs. So unit-tests based on visual-checking are needy for CI to automaticlly check if there are some visual bugs here.

GGUI Unit-test cover range

Notice
The renderring result on mac is different from other platform, we should handle it separately.

@Morcki Morcki added the feature request Suggest an idea on this project label Aug 30, 2022
@Morcki Morcki self-assigned this Aug 30, 2022
@taichi-gardener taichi-gardener moved this to Untriaged in Taichi Lang Aug 30, 2022
@Morcki
Copy link
Member Author

Morcki commented Aug 30, 2022

Codes to generate ground-truth image

  • test_fetching_color_attachment
def test_fetching_color_attachment(filename):
    def write_temp_image_attachment(img, file):
        ti.tools.imwrite(img, file)
        
    ti.init(arch=ti.vulkan)
    
    window = ti.ui.Window('test', (640, 480), show_window=False)
    canvas = window.get_canvas()

    img = ti.Vector.field(4, ti.f32, (512, 512))

    @ti.kernel
    def init_img():
        for i, j in img:
            img[i, j] = ti.Vector([i, j, 0, 512], dt=ti.f32) / 512

    init_img()

    def render():
        canvas.set_image(img)
        
    render()
    write_temp_image_attachment(window.get_image_buffer_as_numpy(), filename)
    window.destroy()

@Morcki
Copy link
Member Author

Morcki commented Aug 30, 2022

Codes to generate ground-truth image

  • test_fetching_depth_attachment
def test_fetching_depth_attachment(filename):
    def write_temp_image_attachment(img, file):
        ti.tools.imwrite(img, file)
    ti.init(arch=ti.vulkan)
        
    window = ti.ui.Window("test", (512, 512), vsync=True, show_window=False)
    canvas = window.get_canvas()
    scene = ti.ui.Scene()
    camera = ti.ui.make_camera()
    
    ball_center = ti.Vector.field(3, dtype=float, shape=(1, ))
    ball_center[0] = ti.math.vec3(0, 0, 0.5)
    
    def render():
        camera.position(0.0, 0.0, 1)
        camera.lookat(0.0, 0.0, 0)
        scene.set_camera(camera)
        scene.point_light(pos=(0, 1, 2), color=(1, 1, 1))
        scene.ambient_light((0.5, 0.5, 0.5))
        scene.particles(ball_center, radius=0.05, color=(0.5, 0.42, 0.8))
        canvas.scene(scene)
        
    render()
    write_temp_image_attachment(window.get_depth_buffer_as_numpy(), filename)
    window.destroy()

@Morcki
Copy link
Member Author

Morcki commented Aug 30, 2022

Codes to generate ground-truth image

  • test_draw_lines
def test_draw_lines(filename):
    def write_temp_image_attachment(img, file):
        ti.tools.imwrite(img, file)
    ti.init(arch=ti.vulkan)        
    N = 10
    particles_pos = ti.Vector.field(3, dtype=ti.f32, shape = N)
    points_pos = ti.Vector.field(3, dtype=ti.f32, shape = N)
            
    @ti.kernel
    def init_points_pos(points : ti.template()):
        for i in range(points.shape[0]):
            points[i] = [i for j in ti.static(range(3))]

    init_points_pos(particles_pos)
    init_points_pos(points_pos)

    window = ti.ui.Window("Test for Drawing 3d-lines", (768, 768), show_window=False)
    canvas = window.get_canvas()
    scene = ti.ui.Scene()
    camera = ti.ui.make_camera()
    camera.position(0, 5, -10)
    camera.lookat(3, 3, 1)

    def render():
        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.5)
        scene.lines(points_pos, color = (0.28, 0.68, 0.99), width = 5.0)
        canvas.scene(scene)
        
    render()
    write_temp_image_attachment(window.get_image_buffer_as_numpy(), filename)
    window.destroy()

@Morcki
Copy link
Member Author

Morcki commented Aug 30, 2022

Codes to generate ground-truth image

  • test_draw_part_of_particles
def test_draw_part_of_particles(filename):
    def write_temp_image_attachment(img, file):
        ti.tools.imwrite(img, file)
        
    ti.init(arch=ti.vulkan)
    
    N = 10

    particles_pos = ti.Vector.field(3, dtype=ti.f32, shape = N)
    points_pos = ti.Vector.field(3, dtype=ti.f32, shape = N)
            
    @ti.kernel
    def init_points_pos(points : ti.template()):
        for i in range(points.shape[0]):
            points[i] = [i for j in ti.static(range(3))]

    init_points_pos(particles_pos)
    init_points_pos(points_pos)

    window = ti.ui.Window("Test for Drawing 3d-lines", (768, 768), show_window=False)
    canvas = window.get_canvas()
    scene = ti.ui.Scene()
    camera = ti.ui.make_camera()
    camera.position(0, 5, -10)
    camera.lookat(3, 3, 1)

    def render():
        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.5,
                        index_offset=2, index_count=6)
        canvas.scene(scene)
        
    render()
    write_temp_image_attachment(window.get_image_buffer_as_numpy(), filename)
    window.destroy()

@Morcki
Copy link
Member Author

Morcki commented Aug 30, 2022

Codes to generate ground-truth image

  • test_draw_part_of_mesh
def test_draw_part_of_mesh(filename):
    def write_temp_image_attachment(img, file):
        ti.tools.imwrite(img, file)
    ti.init(arch=ti.vulkan)
    N = 10
    NV = (N + 1)**2
    NT = 2 * N**2
    NE = 2 * N * (N + 1) + N**2
    pos = ti.Vector.field(3, ti.f32, shape=NV)
    tri = ti.field(ti.i32, shape=3 * NT)
    edge = ti.Vector.field(2, ti.i32, shape=NE)

    @ti.kernel
    def init_pos():
        for i, j in ti.ndrange(N + 1, N + 1):
            idx = i * (N + 1) + j
            pos[idx] = ti.Vector([i / N, 1.0 - j / N, 0.5])

    @ti.kernel
    def init_tri():
        for i, j in ti.ndrange(N, N):
            tri_idx = 6 * (i * N + j)
            pos_idx = i * (N + 1) + j
            if (i + j) % 2 == 0:
                tri[tri_idx + 0] = pos_idx
                tri[tri_idx + 1] = pos_idx + N + 2
                tri[tri_idx + 2] = pos_idx + 1
                tri[tri_idx + 3] = pos_idx
                tri[tri_idx + 4] = pos_idx + N + 1
                tri[tri_idx + 5] = pos_idx + N + 2
            else:
                tri[tri_idx + 0] = pos_idx
                tri[tri_idx + 1] = pos_idx + N + 1
                tri[tri_idx + 2] = pos_idx + 1
                tri[tri_idx + 3] = pos_idx + 1
                tri[tri_idx + 4] = pos_idx + N + 1
                tri[tri_idx + 5] = pos_idx + N + 2

    @ti.kernel
    def init_edge():
        for i, j in ti.ndrange(N + 1, N):
            edge_idx = i * N + j
            pos_idx = i * (N + 1) + j
            edge[edge_idx] = ti.Vector([pos_idx, pos_idx + 1])
        start = N * (N + 1)
        for i, j in ti.ndrange(N, N + 1):
            edge_idx = start + j * N + i
            pos_idx = i * (N + 1) + j
            edge[edge_idx] = ti.Vector([pos_idx, pos_idx + N + 1])
        start = 2 * N * (N + 1)
        for i, j in ti.ndrange(N, N):
            edge_idx = start + i * N + j
            pos_idx = i * (N + 1) + j
            if (i + j) % 2 == 0:
                edge[edge_idx] = ti.Vector([pos_idx, pos_idx + N + 2])
            else:
                edge[edge_idx] = ti.Vector([pos_idx + 1, pos_idx + N + 1])
    
    init_pos()
    init_tri()
    init_edge()

    window = ti.ui.Window("test", (1024, 1024), vsync=True,show_window=False)
    canvas = window.get_canvas()
    scene = ti.ui.Scene()
    camera = ti.ui.make_camera()
    camera.position(1.5, 1, -1)
    camera.lookat(1, 0.5,0)
    camera.fov(90)

    def render():
        camera.track_user_inputs(window, movement_speed=0.003, hold_key=ti.ui.RMB)
        scene.set_camera(camera)
        scene.point_light(pos=(0.5, 1, 2), color=(1, 1, 1))

        scene.mesh(pos, tri, color=(39/255, 123/255, 192/255), two_sided=True, index_count = 2 * NT, index_offset = 9)
        canvas.scene(scene)
        
    render()
    write_temp_image_attachment(window.get_image_buffer_as_numpy(), filename)

@Morcki
Copy link
Member Author

Morcki commented Aug 30, 2022

Codes to generate ground-truth image

  • test_draw_part_of_lines
def test_draw_part_of_lines(filename):
    def write_temp_image_attachment(img, file):
        ti.tools.imwrite(img, file)
        
    ti.init(arch=ti.vulkan)
    
    N = 10
    particles_pos = ti.Vector.field(3, dtype=ti.f32, shape = N)
    points_pos = ti.Vector.field(3, dtype=ti.f32, shape = N)
            
    @ti.kernel
    def init_points_pos(points : ti.template()):
        for i in range(points.shape[0]):
            points[i] = [i for j in ti.static(range(3))]

    init_points_pos(particles_pos)
    init_points_pos(points_pos)

    window = ti.ui.Window("test", (768, 768), show_window=False)
    canvas = window.get_canvas()
    scene = ti.ui.Scene()
    camera = ti.ui.make_camera()
    camera.position(0, 5, -10)
    camera.lookat(3, 3, 1)

    def render():
        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.5)
        scene.lines(points_pos, color = (0.28, 0.68, 0.99), width = 5.0, vertex_count=6, vertex_offset=2)
        canvas.scene(scene)
        
    render()
    write_temp_image_attachment(window.get_image_buffer_as_numpy(), filename)
    window.destroy()

@Morcki
Copy link
Member Author

Morcki commented Aug 30, 2022

Codes to generate ground-truth image

  • test_draw_mesh_instances
def test_draw_mesh_instances(write_frame, filename):
    def write_temp_image_attachment(img, file):
        ti.tools.imwrite(img, file)
        
    ti.init(arch=ti.vulkan)
    
    N = 10
    NV = (N + 1)**2
    NT = 2 * N**2
    NE = 2 * N * (N + 1) + N**2
    pos = ti.Vector.field(3, ti.f32, shape=NV)
    tri = ti.field(ti.i32, shape=3 * NT)
    edge = ti.Vector.field(2, ti.i32, shape=NE)

    # Instance Attribute Information
    NInstanceRows = 100
    NInstanceCols = 100
    NInstance = NInstanceRows * NInstanceCols
    instances_transforms = ti.Matrix.field(4, 4, ti.f32, shape = (NInstance,))

    @ti.kernel
    def init_transforms_of_instances():
        identity = ti.Matrix.identity(ti.f32, 4)
        for i in range(NInstanceRows):
            for j in range(NInstanceCols):
                index = i * NInstanceCols + j
                instances_transforms[index] = identity
                translate_matrix = ti.math.translate(1.2 * j, 0, -1.2 * i)
                instances_transforms[index] = translate_matrix @ instances_transforms[index]

    @ti.kernel
    def init_pos():
        for i, j in ti.ndrange(N + 1, N + 1):
            idx = i * (N + 1) + j
            pos[idx] = ti.Vector([i / N, 1.0 - j / N, 0.5])

    @ti.kernel
    def init_tri():
        for i, j in ti.ndrange(N, N):
            tri_idx = 6 * (i * N + j)
            pos_idx = i * (N + 1) + j
            if (i + j) % 2 == 0:
                tri[tri_idx + 0] = pos_idx
                tri[tri_idx + 1] = pos_idx + N + 2
                tri[tri_idx + 2] = pos_idx + 1
                tri[tri_idx + 3] = pos_idx
                tri[tri_idx + 4] = pos_idx + N + 1
                tri[tri_idx + 5] = pos_idx + N + 2
            else:
                tri[tri_idx + 0] = pos_idx
                tri[tri_idx + 1] = pos_idx + N + 1
                tri[tri_idx + 2] = pos_idx + 1
                tri[tri_idx + 3] = pos_idx + 1
                tri[tri_idx + 4] = pos_idx + N + 1
                tri[tri_idx + 5] = pos_idx + N + 2

    @ti.kernel
    def init_edge():
        for i, j in ti.ndrange(N + 1, N):
            edge_idx = i * N + j
            pos_idx = i * (N + 1) + j
            edge[edge_idx] = ti.Vector([pos_idx, pos_idx + 1])
        start = N * (N + 1)
        for i, j in ti.ndrange(N, N + 1):
            edge_idx = start + j * N + i
            pos_idx = i * (N + 1) + j
            edge[edge_idx] = ti.Vector([pos_idx, pos_idx + N + 1])
        start = 2 * N * (N + 1)
        for i, j in ti.ndrange(N, N):
            edge_idx = start + i * N + j
            pos_idx = i * (N + 1) + j
            if (i + j) % 2 == 0:
                edge[edge_idx] = ti.Vector([pos_idx, pos_idx + N + 2])
            else:
                edge[edge_idx] = ti.Vector([pos_idx + 1, pos_idx + N + 1])


    @ti.kernel
    def update_transform(t : ti.f32):
        for i in range(NInstance):
            rotation_matrix = ti.math.rot_by_axis(ti.math.vec3(0, 1, 0), 0.01 * ti.math.sin(t))
            instances_transforms[i] = instances_transforms[i] @ rotation_matrix
    
    init_transforms_of_instances()

    init_pos()
    init_tri()
    init_edge()

    window = ti.ui.Window("test", (1024, 1024), vsync=True, show_window=False)
    canvas = window.get_canvas()
    scene = ti.ui.Scene()
    camera = ti.ui.make_camera()
    camera.position(-1.82731234, 2.26492691, 2.27800684)
    camera.lookat(-1.13230401, 2.11502124, 1.57480579)
    camera.fov(90)

    def render():
        scene.set_camera(camera)
        scene.point_light(pos=(0.5, 1, 2), color=(1, 1, 1))

        scene.mesh_instance(pos, tri, color=(39/255, 123/255, 192/255), two_sided=True, transforms=instances_transforms)
        canvas.scene(scene)
    
    for _ in range(30):
        update_transform(30)
        
    render()
    write_temp_image_attachment(window.get_image_buffer_as_numpy(), filename)
    window.destroy()

@Morcki
Copy link
Member Author

Morcki commented Aug 30, 2022

Codes to generate ground-truth image

  • test_draw_part_of_mesh_instances
def test_draw_part_of_mesh_instances(filename):
    def write_temp_image_attachment(img, file):
        ti.tools.imwrite(img, file)
        
    ti.init(arch=ti.vulkan)
    
    N = 10
    NV = (N + 1)**2
    NT = 2 * N**2
    NE = 2 * N * (N + 1) + N**2
    pos = ti.Vector.field(3, ti.f32, shape=NV)
    tri = ti.field(ti.i32, shape=3 * NT)
    edge = ti.Vector.field(2, ti.i32, shape=NE)

    # Instance Attribute Information
    NInstanceRows = 10
    NInstanceCols = 10
    NInstance = NInstanceRows * NInstanceCols
    instances_transforms = ti.Matrix.field(4, 4, ti.f32, shape = (NInstance,))

    @ti.kernel
    def init_transforms_of_instances():
        identity = ti.Matrix.identity(ti.f32, 4)
        for i in range(NInstanceRows):
            for j in range(NInstanceCols):
                index = i * NInstanceCols + j
                instances_transforms[index] = identity
                translate_matrix = ti.math.translate(1.2 * j, 0, -1.2 * i)
                instances_transforms[index] = translate_matrix @ instances_transforms[index]

    @ti.kernel
    def init_pos():
        for i, j in ti.ndrange(N + 1, N + 1):
            idx = i * (N + 1) + j
            pos[idx] = ti.Vector([i / N, 1.0 - j / N, 0.5])

    @ti.kernel
    def init_tri():
        for i, j in ti.ndrange(N, N):
            tri_idx = 6 * (i * N + j)
            pos_idx = i * (N + 1) + j
            if (i + j) % 2 == 0:
                tri[tri_idx + 0] = pos_idx
                tri[tri_idx + 1] = pos_idx + N + 2
                tri[tri_idx + 2] = pos_idx + 1
                tri[tri_idx + 3] = pos_idx
                tri[tri_idx + 4] = pos_idx + N + 1
                tri[tri_idx + 5] = pos_idx + N + 2
            else:
                tri[tri_idx + 0] = pos_idx
                tri[tri_idx + 1] = pos_idx + N + 1
                tri[tri_idx + 2] = pos_idx + 1
                tri[tri_idx + 3] = pos_idx + 1
                tri[tri_idx + 4] = pos_idx + N + 1
                tri[tri_idx + 5] = pos_idx + N + 2

    @ti.kernel
    def init_edge():
        for i, j in ti.ndrange(N + 1, N):
            edge_idx = i * N + j
            pos_idx = i * (N + 1) + j
            edge[edge_idx] = ti.Vector([pos_idx, pos_idx + 1])
        start = N * (N + 1)
        for i, j in ti.ndrange(N, N + 1):
            edge_idx = start + j * N + i
            pos_idx = i * (N + 1) + j
            edge[edge_idx] = ti.Vector([pos_idx, pos_idx + N + 1])
        start = 2 * N * (N + 1)
        for i, j in ti.ndrange(N, N):
            edge_idx = start + i * N + j
            pos_idx = i * (N + 1) + j
            if (i + j) % 2 == 0:
                edge[edge_idx] = ti.Vector([pos_idx, pos_idx + N + 2])
            else:
                edge[edge_idx] = ti.Vector([pos_idx + 1, pos_idx + N + 1])

    init_transforms_of_instances()
    init_pos()
    init_tri()
    init_edge()

    window = ti.ui.Window("test", (1024, 1024), vsync=True, show_window=False)
    canvas = window.get_canvas()
    scene = ti.ui.Scene()
    camera = ti.ui.make_camera()
    camera.position(-1.82731234, 2.26492691, 2.27800684)
    camera.lookat(-1.13230401, 2.11502124, 1.57480579)
    camera.fov(90)

    def render():
        scene.set_camera(camera)
        scene.point_light(pos=(0.5, 1, 2), color=(1, 1, 1))

        scene.mesh_instance(pos, tri, color=(39/255, 123/255, 192/255), two_sided=True, transforms=instances_transforms, instance_count=10, instance_offset=2)
        canvas.scene(scene)
        
    render()
    write_temp_image_attachment(window.get_image_buffer_as_numpy(), filename)
    window.destroy()

@Morcki
Copy link
Member Author

Morcki commented Aug 30, 2022

Codes to generate ground-truth image

  • test_wireframe_mode
def test_wireframe_mode(filename):
    def write_temp_image_attachment(img, file):
        ti.tools.imwrite(img, file)
    ti.init(arch=ti.vulkan)
    N = 10
    NV = (N + 1)**2
    NT = 2 * N**2
    NE = 2 * N * (N + 1) + N**2
    pos = ti.Vector.field(3, ti.f32, shape=NV)
    tri = ti.field(ti.i32, shape=3 * NT)
    edge = ti.Vector.field(2, ti.i32, shape=NE)

    @ti.kernel
    def init_pos():
        for i, j in ti.ndrange(N + 1, N + 1):
            idx = i * (N + 1) + j
            pos[idx] = ti.Vector([i / N, 1.0 - j / N, 0.5])

    @ti.kernel
    def init_tri():
        for i, j in ti.ndrange(N, N):
            tri_idx = 6 * (i * N + j)
            pos_idx = i * (N + 1) + j
            if (i + j) % 2 == 0:
                tri[tri_idx + 0] = pos_idx
                tri[tri_idx + 1] = pos_idx + N + 2
                tri[tri_idx + 2] = pos_idx + 1
                tri[tri_idx + 3] = pos_idx
                tri[tri_idx + 4] = pos_idx + N + 1
                tri[tri_idx + 5] = pos_idx + N + 2
            else:
                tri[tri_idx + 0] = pos_idx
                tri[tri_idx + 1] = pos_idx + N + 1
                tri[tri_idx + 2] = pos_idx + 1
                tri[tri_idx + 3] = pos_idx + 1
                tri[tri_idx + 4] = pos_idx + N + 1
                tri[tri_idx + 5] = pos_idx + N + 2

    @ti.kernel
    def init_edge():
        for i, j in ti.ndrange(N + 1, N):
            edge_idx = i * N + j
            pos_idx = i * (N + 1) + j
            edge[edge_idx] = ti.Vector([pos_idx, pos_idx + 1])
        start = N * (N + 1)
        for i, j in ti.ndrange(N, N + 1):
            edge_idx = start + j * N + i
            pos_idx = i * (N + 1) + j
            edge[edge_idx] = ti.Vector([pos_idx, pos_idx + N + 1])
        start = 2 * N * (N + 1)
        for i, j in ti.ndrange(N, N):
            edge_idx = start + i * N + j
            pos_idx = i * (N + 1) + j
            if (i + j) % 2 == 0:
                edge[edge_idx] = ti.Vector([pos_idx, pos_idx + N + 2])
            else:
                edge[edge_idx] = ti.Vector([pos_idx + 1, pos_idx + N + 1])
    
    init_pos()
    init_tri()
    init_edge()

    window = ti.ui.Window("test", (1024, 1024), vsync=True,show_window=False)
    canvas = window.get_canvas()
    scene = ti.ui.Scene()
    camera = ti.ui.make_camera()
    camera.position(1.5, 1, -1)
    camera.lookat(1, 0.5,0)
    camera.fov(90)

    def render():
        camera.track_user_inputs(window, movement_speed=0.003, hold_key=ti.ui.RMB)
        scene.set_camera(camera)
        scene.point_light(pos=(0.5, 1, 2), color=(1, 1, 1))

        scene.mesh(pos, tri, color=(39/255, 123/255, 192/255), two_sided=True, show_wireframe=True)
        canvas.scene(scene)
        
    render()
    write_temp_image_attachment(window.get_image_buffer_as_numpy(), filename)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Suggest an idea on this project
Projects
Status: Done
Development

No branches or pull requests

1 participant