Beispiel #1
0
def test_repeat_primitive():
    # init variables
    verts, faces = fp.prim_square()
    centers = np.array([[0, 0, 0], [5, 0, 0], [10, 0, 0]])
    dirs = np.array([[0, 1, 0], [1, 0, 0], [0, 0, 1]])
    colors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1.]])

    res = fp.repeat_primitive(vertices=verts,
                              faces=faces,
                              centers=centers,
                              directions=dirs,
                              colors=colors)

    big_verts, big_faces, big_colors, big_centers = res

    npt.assert_equal(big_verts.shape[0], verts.shape[0] * centers.shape[0])
    npt.assert_equal(big_faces.shape[0], faces.shape[0] * centers.shape[0])
    npt.assert_equal(big_colors.shape[0], verts.shape[0] * centers.shape[0])
    npt.assert_equal(big_centers.shape[0], verts.shape[0] * centers.shape[0])
Beispiel #2
0
def test_square_actor(interactive=False):
    scene = window.Scene()
    centers = np.array([[2, 0, 0], [0, 2, 0], [0, 0, 0]])
    colors = np.array([[255, 0, 0], [0, 255, 0], [0, 0, 255]])
    scale = [1, 2, 3]

    verts, faces = prim_square()
    res = repeat_primitive(verts,
                           faces,
                           centers=centers,
                           colors=colors,
                           scale=scale)

    big_verts, big_faces, big_colors, _ = res
    sq_actor = get_actor_from_primitive(big_verts, big_faces, big_colors)
    sq_actor.GetProperty().BackfaceCullingOff()
    scene.add(sq_actor)
    scene.add(actor.axes())
    if interactive:
        window.show(scene)
Beispiel #3
0
def test_vertices_from_actor():

    my_vertices = np.array([[2.5, -0.5, 0.], [1.5, -0.5, 0.], [1.5, 0.5, 0.],
                            [2.5, 0.5, 0.], [1., 1., 0.], [-1., 1., 0.],
                            [-1., 3., 0.], [1., 3., 0.], [0.5, -0.5, 0.],
                            [-0.5, -0.5, 0.], [-0.5, 0.5, 0.], [0.5, 0.5, 0.]])
    centers = np.array([[2, 0, 0], [0, 2, 0], [0, 0, 0]])
    colors = np.array([[255, 0, 0], [0, 255, 0], [0, 0, 255]])
    scale = [1, 2, 1]
    verts, faces = fp.prim_square()
    res = fp.repeat_primitive(verts,
                              faces,
                              centers=centers,
                              colors=colors,
                              scale=scale)

    big_verts = res[0]
    big_faces = res[1]
    big_colors = res[2]
    actr = get_actor_from_primitive(big_verts, big_faces, big_colors)
    actr.GetProperty().BackfaceCullingOff()
    res_vertices = vertices_from_actor(actr)
    npt.assert_array_almost_equal(my_vertices, res_vertices)
Beispiel #4
0
def test_repeat_primitive():
    # init variables
    verts, faces = fp.prim_square()
    centers = np.array([[0, 0, 0], [5, 0, 0], [10, 0, 0]])
    dirs = np.array([[0, 1, 0], [1, 0, 0], [0, 0, 1]])
    colors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1.]])

    res = fp.repeat_primitive(vertices=verts,
                              faces=faces,
                              centers=centers,
                              directions=dirs,
                              colors=colors)

    big_verts, big_faces, big_colors, big_centers = res

    npt.assert_equal(big_verts.shape[0], verts.shape[0] * centers.shape[0])
    npt.assert_equal(big_faces.shape[0], faces.shape[0] * centers.shape[0])
    npt.assert_equal(big_colors.shape[0], verts.shape[0] * centers.shape[0])
    npt.assert_equal(big_centers.shape[0], verts.shape[0] * centers.shape[0])

    npt.assert_equal(np.unique(np.concatenate(big_faces, axis=None)).tolist(),
                     list(range(len(big_verts))))

    # translate the squares primitives centers to be the origin
    big_vert_origin = big_verts - np.repeat(centers, 4, axis=0)

    # three repeated primitives
    sq1, sq2, sq3 = big_vert_origin.reshape([3, 12])

    #  primitives directed toward different directions must not be the same
    np.testing.assert_equal(np.any(np.not_equal(sq1, sq2)), True)
    np.testing.assert_equal(np.any(np.not_equal(sq1, sq3)), True)
    np.testing.assert_equal(np.any(np.not_equal(sq2, sq3)), True)

    np.testing.assert_equal(big_vert_origin.min(), -0.5)
    np.testing.assert_equal(big_vert_origin.max(), 0.5)
    np.testing.assert_equal(np.mean(big_vert_origin), 0)
Beispiel #5
0
def test_spheres_on_canvas():

    scene = window.Scene()
    showm = window.ShowManager(scene, reset_camera=False)

    # colors = 255 * np.array([
    #     [.85, .07, .21], [.56, .14, .85], [.16, .65, .20], [.95, .73, .06],
    #     [.95, .55, .05], [.62, .42, .75], [.26, .58, .85], [.24, .82, .95],
    #     [.95, .78, .25], [.85, .58, .35], [1., 1., 1.]
    # ])
    n_points = 2000000
    colors = np.array([[255, 0, 0], [0, 255, 0],
                       [0, 0, 255]])  #255 * np.random.rand(n_points, 3)
    # n_points = colors.shape[0]
    np.random.seed(42)
    centers = np.array(
        [[2, 0, 0], [0, 2, 0], [0, 0, 0]]
    )  # 500 * np.random.rand(n_points, 3) - 250 # np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
    radius = [2, 2, 2]  # np.random.rand(n_points) #  [1, 1, 2]

    polydata = vtk.vtkPolyData()

    verts, faces = fp.prim_square()

    big_verts = np.tile(verts, (centers.shape[0], 1))
    big_cents = np.repeat(centers, verts.shape[0], axis=0)

    big_verts += big_cents

    # print(big_verts)

    big_scales = np.repeat(radius, verts.shape[0], axis=0)

    # print(big_scales)

    big_verts *= big_scales[:, np.newaxis]

    # print(big_verts)

    tris = np.array([[0, 1, 2], [2, 3, 0]], dtype='i8')

    big_tris = np.tile(tris, (centers.shape[0], 1))
    shifts = np.repeat(
        np.arange(0, centers.shape[0] * verts.shape[0], verts.shape[0]),
        tris.shape[0])

    big_tris += shifts[:, np.newaxis]

    # print(big_tris)

    big_cols = np.repeat(colors, verts.shape[0], axis=0)

    # print(big_cols)

    big_centers = np.repeat(centers, verts.shape[0], axis=0)

    # print(big_centers)

    big_centers *= big_scales[:, np.newaxis]

    # print(big_centers)

    set_polydata_vertices(polydata, big_verts)
    set_polydata_triangles(polydata, big_tris)
    set_polydata_colors(polydata, big_cols)

    vtk_centers = numpy_support.numpy_to_vtk(big_centers, deep=True)
    vtk_centers.SetNumberOfComponents(3)
    vtk_centers.SetName("center")
    polydata.GetPointData().AddArray(vtk_centers)

    canvas_actor = get_actor_from_polydata(polydata)
    canvas_actor.GetProperty().BackfaceCullingOff()

    scene.add(canvas_actor)

    mapper = canvas_actor.GetMapper()

    mapper.MapDataArrayToVertexAttribute(
        "center", "center", vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS, -1)

    mapper.AddShaderReplacement(
        vtk.vtkShader.Vertex, "//VTK::ValuePass::Dec", True, """
        //VTK::ValuePass::Dec
        in vec3 center;

        uniform mat4 Ext_mat;

        out vec3 centeredVertexMC;
        out vec3 cameraPosition;
        out vec3 viewUp;

        """, False)

    mapper.AddShaderReplacement(
        vtk.vtkShader.Vertex, "//VTK::ValuePass::Impl", True, """
        //VTK::ValuePass::Impl
        centeredVertexMC = vertexMC.xyz - center;
        float scalingFactor = 1. / abs(centeredVertexMC.x);
        centeredVertexMC *= scalingFactor;

        vec3 CameraRight_worldspace = vec3(MCVCMatrix[0][0], MCVCMatrix[1][0], MCVCMatrix[2][0]);
        vec3 CameraUp_worldspace = vec3(MCVCMatrix[0][1], MCVCMatrix[1][1], MCVCMatrix[2][1]);

        vec3 vertexPosition_worldspace = center + CameraRight_worldspace * 1 * centeredVertexMC.x + CameraUp_worldspace * 1 * centeredVertexMC.y;
        gl_Position = MCDCMatrix * vec4(vertexPosition_worldspace, 1.);

        """, False)

    mapper.AddShaderReplacement(
        vtk.vtkShader.Fragment, "//VTK::ValuePass::Dec", True, """
        //VTK::ValuePass::Dec
        in vec3 centeredVertexMC;
        in vec3 cameraPosition;
        in vec3 viewUp;

        uniform vec3 Ext_camPos;
        uniform vec3 Ext_viewUp;
        """, False)

    mapper.AddShaderReplacement(
        vtk.vtkShader.Fragment, "//VTK::Light::Impl", True, """
        // Renaming variables passed from the Vertex Shader
        vec3 color = vertexColorVSOutput.rgb;
        vec3 point = centeredVertexMC;
        fragOutput0 = vec4(color, 0.7);
        /*
        // Comparing camera position from vertex shader and python
        float dist = distance(cameraPosition, Ext_camPos);
        if(dist < .0001)
            fragOutput0 = vec4(1, 0, 0, 1);
        else
            fragOutput0 = vec4(0, 1, 0, 1);


        // Comparing view up from vertex shader and python
        float dist = distance(viewUp, Ext_viewUp);
        if(dist < .0001)
            fragOutput0 = vec4(1, 0, 0, 1);
        else
            fragOutput0 = vec4(0, 1, 0, 1);
        */
        float len = length(point);
        // VTK Fake Spheres
        float radius = 1.;
        if(len > radius)
          discard;
        vec3 normalizedPoint = normalize(vec3(point.xy, sqrt(1. - len)));
        vec3 direction = normalize(vec3(1., 1., 1.));
        float df = max(0, dot(direction, normalizedPoint));
        float sf = pow(df, 24);
        fragOutput0 = vec4(max(df * color, sf * vec3(1)), 1);
        """, False)

    @vtk.calldata_type(vtk.VTK_OBJECT)
    def vtk_shader_callback(caller, event, calldata=None):
        res = scene.size()
        camera = scene.GetActiveCamera()
        cam_pos = camera.GetPosition()
        foc_pnt = camera.GetFocalPoint()
        view_up = camera.GetViewUp()
        # cam_light_mat = camera.GetCameraLightTransformMatrix()
        # comp_proj_mat = camera.GetCompositeProjectionTransformMatrix()
        # exp_proj_mat = camera.GetExplicitProjectionTransformMatrix()
        # eye_mat = camera.GetEyeTransformMatrix()
        # model_mat = camera.GetModelTransformMatrix()
        # model_view_mat = camera.GetModelViewTransformMatrix()
        # proj_mat = camera.GetProjectionTransformMatrix(scene)
        view_mat = camera.GetViewTransformMatrix()
        mat = view_mat
        np.set_printoptions(precision=3, suppress=True)
        np_mat = np.zeros((4, 4))
        for i in range(4):
            for j in range(4):
                np_mat[i, j] = mat.GetElement(i, j)
        program = calldata
        if program is not None:
            # print("\nCamera position: {}".format(cam_pos))
            # print("Focal point: {}".format(foc_pnt))
            # print("View up: {}".format(view_up))
            # print(mat)
            # print(np_mat)
            # print(np.dot(-np_mat[:3, 3], np_mat[:3, :3]))
            # a = np.array(cam_pos) - np.array(foc_pnt)
            # print(a / np.linalg.norm(a))
            # print(cam_light_mat)
            # #print(comp_proj_mat)
            # print(exp_proj_mat)
            # print(eye_mat)
            # print(model_mat)
            # print(model_view_mat)
            # print(proj_mat)
            # print(view_mat)
            program.SetUniform2f("Ext_res", res)
            program.SetUniform3f("Ext_camPos", cam_pos)
            program.SetUniform3f("Ext_focPnt", foc_pnt)
            program.SetUniform3f("Ext_viewUp", view_up)
            program.SetUniformMatrix("Ext_mat", mat)

    mapper.AddObserver(vtk.vtkCommand.UpdateShaderEvent, vtk_shader_callback)

    global timer
    timer = 0

    def timer_callback(obj, event):
        global timer
        timer += 1.
        showm.render()
        scene.azimuth(2)
        # scene.elevation(5)
        # scene.roll(5)

    label = vtk.vtkOpenGLBillboardTextActor3D()
    label.SetInput("FURY Rocks!!!")
    label.SetPosition(1., 1., 1)
    label.GetTextProperty().SetFontSize(40)
    label.GetTextProperty().SetColor(.5, .5, .5)
    # TODO: Get Billboard's mapper
    # l_mapper = label.GetActors()

    # scene.add(label)
    scene.add(actor.axes())

    scene.background((1, 1, 1))

    # scene.set_camera(position=(1.5, 2.5, 15), focal_point=(1.5, 2.5, 1.5),
    #                  view_up=(0, 1, 0))
    scene.set_camera(position=(1.5, 2.5, 25),
                     focal_point=(0, 0, 0),
                     view_up=(0, 1, 0))
    showm.initialize()
    showm.add_timer_callback(True, 100, timer_callback)
    showm.start()