def test_make_nparray_gray(self):
        def creator(file):
            make_nparray(file, draw1, 600, 400, channels=1)

        self.assertTrue(run_image_test('test_make_nparray_gray.png', creator))
    def test_make_bitmap_frame_rgba(self):
        def creator(file):
            frame = make_nparray_frame(draw4, 600, 400, channels=4)
            save_frame(file, frame)

        self.assertTrue(run_image_test('test_make_nparray_frame_rgba.png', creator))
    def test_make_nparray_frame_gray(self):
        def creator(file):
            frame = make_nparray_frame(draw1, 600, 400, channels=1)
            save_frame(file, frame)

        self.assertTrue(run_image_test('test_make_nparray_frame_gray.png', creator))
    def test_make_nparray_rgba(self):
        def creator(file):
            make_nparray(file, draw4, 600, 400, channels=4)

        self.assertTrue(run_image_test('test_make_nparray_rgba.png', creator))
    def test_make_bitmap_rgba(self):
        def creator(file):
            make_bitmap(file, draw, 400, 400, channels=4)

        self.assertTrue(run_image_test('test_make_bitmap_rgba.png', creator))
Example #6
0
    def test_simple_drawing3d(self):
        def draw(ctx, pixel_width, pixel_height, frame_no, frame_count):
            prog = ctx.program(
                vertex_shader='''
                    #version 330
                    in vec2 in_vert;
                    in vec3 in_color;
                    out vec3 v_color;    // Goes to the fragment shader
                    void main() {
                        gl_Position = vec4(in_vert, 0.0, 1.0);
                        v_color = in_color;
                    }
                ''',
                fragment_shader='''
                    #version 330
                    in vec3 v_color;
                    out vec4 f_color;
                    void main() {
                        // We're not interested in changing the alpha value
                        f_color = vec4(v_color, 1.0);
                    }
                ''',
            )

            # Point coordinates are put followed by the vec3 color values
            vertices = np.array(
                [
                    # x, y, red, green, blue
                    0.0,
                    0.8,
                    1.0,
                    0.0,
                    0.0,
                    -0.6,
                    -0.8,
                    0.0,
                    1.0,
                    0.0,
                    0.6,
                    -0.8,
                    0.0,
                    0.0,
                    1.0,
                ],
                dtype='f4')

            vbo = ctx.buffer(vertices)

            # We control the 'in_vert' and `in_color' variables
            vao = ctx.vertex_array(
                prog,
                [
                    # Map in_vert to the first 2 floats
                    # Map in_color to the next 3 floats
                    (vbo, '2f 3f', 'in_vert', 'in_color')
                ],
            )

            vao.render(moderngl.TRIANGLES)

        def creator(file):
            make_3dimage(file, draw, 700, 600, Color('grey'))

        self.assertTrue(run_image_test('test_simple_drawing3d.png', creator))
    def test_old_markers(self):
        # Deprecated marker functions
        def draw(ctx, width, height, frame_no, frame_count):
            setup(ctx, width, height, background=Color(0.8))

            ctx.set_source_rgba(*Color(0, 0, 0.5))
            ctx.set_line_width(3)

            ## Draw lines with ticks, paraticks and arrowheads
            a = (50, 50)
            b = (50, 150)
            line(ctx, a, b)
            ctx.stroke()
            tick(ctx, a, b, length=12, gap=6)
            ctx.stroke()
            arrowhead(ctx, a, b, length=24)
            ctx.stroke()

            a = (100, 50)
            b = (150, 150)
            line(ctx, a, b)
            ctx.stroke()
            tick(ctx, a, b, 2, length=12, gap=6)
            ctx.stroke()

            a = (250, 50)
            b = (200, 150)
            line(ctx, a, b)
            ctx.stroke()
            tick(ctx, a, b, 3, length=12, gap=6)
            ctx.stroke()

            a = (350, 50)
            b = (350, 150)
            line(ctx, a, b)
            ctx.stroke()
            paratick(ctx, a, b, length=12, gap=6)
            ctx.stroke()

            a = (400, 50)
            b = (450, 150)
            line(ctx, a, b)
            ctx.stroke()
            paratick(ctx, a, b, 2, length=12, gap=6)
            ctx.stroke()

            a = (550, 150)
            b = (500, 50)
            line(ctx, a, b)
            ctx.stroke()
            paratick(ctx, a, b, 3, length=12, gap=6)
            ctx.stroke()

            ## Draw lines with angles
            a = (50, 250)
            b = (50, 450)
            c = (150, 450)
            polygon(ctx, (a, b, c), closed=False)
            ctx.stroke()
            angle_marker(ctx, a, b, c, radius=24, gap=6, right_angle=True)
            ctx.stroke()

            a = (250, 250)
            b = (200, 450)
            c = (300, 450)
            polygon(ctx, (a, b, c), closed=False)
            ctx.stroke()
            angle_marker(ctx, a, b, c, 3, radius=24, gap=6)
            ctx.stroke()

            a = (300, 250)
            b = (400, 300)
            c = (500, 300)
            polygon(ctx, (a, b, c), closed=False)
            ctx.stroke()
            angle_marker(ctx, c, b, a, radius=24, gap=6)
            ctx.stroke()

            a = (300, 350)
            b = (400, 400)
            c = (500, 400)
            polygon(ctx, (a, b, c), closed=False)
            ctx.stroke()
            angle_marker(ctx, a, b, c, 2, radius=24, gap=6)
            ctx.stroke()

        def creator(file):
            make_image(file, draw, 600, 500)

        self.assertTrue(run_image_test('test_old_markers.png', creator))
    def test_complex_paths(self):
        def draw(ctx, width, height, frame_no, frame_count):
            setup(ctx, width, height, width=300, background=Color(1))

            # Fill then stroke a rectangle
            ctx.save()
            ctx.translate(0, 0)
            Rectangle(ctx).of_corner_size((20, 20), 60, 60).fill(Color('red')).stroke(Color('blue'), 10)
            ctx.restore()

            # Stroke then fill a rectangle
            ctx.save()
            ctx.translate(100, 0)
            Rectangle(ctx).of_corner_size((20, 20), 60, 60).stroke(Color('blue'), 10).fill(Color('red'))
            ctx.restore()

            # Path with two rectangles
            ctx.save()
            ctx.translate(200, 0)
            Rectangle(ctx).of_corner_size((20, 20), 60, 60).add()
            Rectangle(ctx).of_corner_size((30, 30), 60, 60).as_sub_path().fill(Color('red')).stroke(Color('blue'), 5)
            ctx.restore()

            # Path from several lines
            ctx.save()
            ctx.translate(0, 100)
            Line(ctx).of_start_end((20, 20), (60, 30)).add()
            Line(ctx).of_end((60, 60)).extend_path().add()
            Line(ctx).of_end((30, 50)).extend_path().fill(Color('red')).stroke(Color('blue'), 5)
            ctx.restore()

            # Path from several lines, closed
            ctx.save()
            ctx.translate(100, 100)
            Line(ctx).of_start_end((20, 20), (60, 30)).add()
            Line(ctx).of_end((60, 60)).extend_path().add()
            Line(ctx).of_end((30, 50)).extend_path(close=True).fill(Color('red')).stroke(Color('blue'), 5)
            ctx.restore()

            # roundrect open
            ctx.save()
            ctx.translate(0, 200)
            Circle(ctx).of_center_radius((20, 20), 10).as_arc(math.pi, math.pi * 3 / 2).add()
            Circle(ctx).of_center_radius((60, 20), 10).as_arc(math.pi * 3 / 2, 0).extend_path().add()
            Circle(ctx).of_center_radius((60, 60), 10).as_arc(0, math.pi / 2).extend_path().add()
            Circle(ctx).of_center_radius((20, 60), 10).as_arc(math.pi / 2, math.pi).extend_path().fill(
                Color('red')).stroke(Color('blue'), 5)
            ctx.restore()

            # roundrect closed
            ctx.save()
            ctx.translate(100, 200)
            Circle(ctx).of_center_radius((20, 20), 10).as_arc(math.pi, math.pi * 3 / 2).add()
            Circle(ctx).of_center_radius((60, 20), 10).as_arc(math.pi * 3 / 2, 0).extend_path().add()
            Circle(ctx).of_center_radius((60, 60), 10).as_arc(0, math.pi / 2).extend_path().add()
            Circle(ctx).of_center_radius((20, 60), 10).as_arc(math.pi / 2, math.pi).extend_path(close=True).fill(
                Color('red')).stroke(Color('blue'), 5)
            ctx.restore()

        def creator(file):
            make_image(file, draw, 600, 600)

        self.assertTrue(run_image_test('test_complex_paths.png', creator))
    def test_markers(self):
        def draw(ctx, width, height, frame_no, frame_count):
            setup(ctx, width, height, background=Color(0.8))
            red = Color('red')
            thickness = 3

            ## Draw lines with ticks and paraticks
            a = (50, 50)
            b = (50, 150)
            Line(ctx).of_start_end(a, b).stroke(red, thickness)
            TickMarker(ctx).of_start_end(a, b).with_length(12).stroke(red, thickness)

            a = (100, 50)
            b = (150, 150)
            Line(ctx).of_start_end(a, b).stroke(red, thickness)
            TickMarker(ctx).of_start_end(a, b).with_length(12).with_count(2).with_gap(6).stroke(red, thickness)

            a = (250, 50)
            b = (200, 150)
            Line(ctx).of_start_end(a, b).stroke(red, thickness)
            TickMarker(ctx).of_start_end(a, b).with_length(12).with_count(3).with_gap(6).stroke(red, thickness)

            a = (350, 50)
            b = (350, 150)
            Line(ctx).of_start_end(a, b).stroke(red, thickness)
            ParallelMarker(ctx).of_start_end(a, b).with_length(12).stroke(red, thickness)

            a = (400, 50)
            b = (450, 150)
            Line(ctx).of_start_end(a, b).stroke(red, thickness)
            ParallelMarker(ctx).of_start_end(a, b).with_length(12).with_count(2).with_gap(6).stroke(red, thickness)

            a = (550, 150)
            b = (500, 50)
            Line(ctx).of_start_end(a, b).stroke(red, thickness)
            ParallelMarker(ctx).of_start_end(a, b).with_length(12).with_count(3).with_gap(6).stroke(red, thickness)


            ## Draw lines with angles
            a = (50, 250)
            b = (50, 450)
            c = (150, 450)
            Polygon(ctx).of_points((a, b, c)).open().stroke(red, thickness)
            AngleMarker(ctx).of_points(a, b, c).with_radius(24).with_gap(6).as_right_angle().stroke(red, thickness)

            a = (250, 250)
            b = (200, 450)
            c = (300, 450)
            Polygon(ctx).of_points((a, b, c)).open().stroke(red, thickness)
            AngleMarker(ctx).of_points(a, b, c).with_count(3).with_radius(24).with_gap(6).stroke(red, thickness)

            a = (300, 250)
            b = (400, 300)
            c = (500, 300)
            Polygon(ctx).of_points((a, b, c)).open().stroke(red, thickness)
            AngleMarker(ctx).of_points(c, b, a).with_radius(24).with_gap(6).stroke(red, thickness)

            a = (300, 350)
            b = (400, 400)
            c = (500, 400)
            Polygon(ctx).of_points((a, b, c)).open().stroke(red, thickness)
            AngleMarker(ctx).of_points(a, b, c).with_count(2).with_radius(24).with_gap(6).stroke(red, thickness)


        def creator(file):
            make_image(file, draw, 600, 500)

        self.assertTrue(run_image_test('test_markers.png', creator))