Esempio n. 1
0
 def draw_line(self, *args):
     mapview = self.parent
     self.zoom = mapview.zoom
     global coordinates
     global corridors
     #corridor = corr
    
     # When zooming we must undo the current scatter transform
     # or the animation distorts it
     scatter = mapview._scatter
     map_source = mapview.map_source
     sx,sy,ss = scatter.x, scatter.y, scatter.scale
     vx,vy,vs = mapview.viewport_pos[0], mapview.viewport_pos[1], mapview.scale       
     # Account for map source tile size and mapview zoom
     ms = pow(2.0,mapview.zoom) * map_source.dp_tile_size        
     #: Since lat is not a linear transform we must compute manually 
            
     #line_points = []
     with self.canvas:
             # Clear old line
             self.canvas.clear()
     
     for corridor in corridors:
         line_points = []
         for lat,lon in corridor:
             line_points.extend((self.get_x(lon),self.get_y(lat)))
             print("linepointslist:")
             print (line_points)
             #line_points.extend(mapview.get_window_xy_from(lat,lon,mapview.zoom))
     
      
         with self.canvas:
             # Clear old line
             #self.canvas.clear()
             # Undo the scatter animation transform
             Scale(1/ss,1/ss,1)
             Translate(-sx,-sy)
             
             # Apply the get window xy from transforms
             Scale(vs,vs,1)
             Translate(-vx,-vy)
                
             # Apply the what we can factor out
             # of the mapsource long,lat to x,y conversion
             Scale(ms/360.0,ms/2.0,1)
             Translate(180,0)
              
             # Draw new
             Color(1, 0, 0, 1)
             print("linepointslist Line:")
             print (line_points)
             Line(points=line_points, width=1)#4/ms)#, joint="round",joint_precision=100)
             self.canvas.ask_update()
Esempio n. 2
0
def screenshot(path, root_widget=None):
    """
    Take screenshot of the current state of the app and save it under ``path``.

    The sleep- and mainthread-decorator ensure that the app shows the current state properly.
    """
    if root_widget is None:
        root_widget = MDApp.get_running_app().root

    fbo = Fbo(
        size=(root_widget.width, root_widget.height),
        with_stencilbuffer=True,
    )

    with fbo:
        ClearColor(*MDApp.get_running_app().theme_cls.bg_normal)
        ClearBuffers()
        Scale(1, -1, 1)
        Translate(-root_widget.x, -root_widget.y - root_widget.height, 0)

    fbo.add(root_widget.canvas)
    fbo.draw()
    img = KivyImage(fbo.texture)

    img.save(path)
Esempio n. 3
0
    def _draw_line(self, *args):
        mapview = self.parent
        self.zoom = mapview.zoom

        # When zooming we must undo the current scatter transform
        # or the animation distorts it
        scatter = mapview._scatter
        sx, sy, ss = scatter.x, scatter.y, scatter.scale

        # Account for map source tile size and mapview zoom
        vx, vy, vs = mapview.viewport_pos[0], mapview.viewport_pos[
            1], mapview.scale

        with self.canvas:
            # Clear old line
            self.canvas.clear()

            # Offset by the MapView's position in the window
            Translate(*mapview.pos)

            # Undo the scatter animation transform
            Scale(1 / ss, 1 / ss, 1)
            Translate(-sx, -sy)

            # Apply the get window xy from transforms
            Scale(vs, vs, 1)
            Translate(-vx, -vy)

            # Apply the what we can factor out of the mapsource long, lat to x, y conversion
            Translate(self.ms / 2, 0)

            # Translate by the offset of the line points (this keeps the points closer to the origin)
            Translate(*self.line_points_offset)

            # Draw line
            Color(0, 0.2, 0.7, 0.25)
            Line(points=self.line_points, width=6.5 / 2)
            Color(0, 0.2, 0.7, 1)
            Line(points=self.line_points, width=6 / 2)
            Color(0, 0.3, 1, 1)
            Line(points=self.line_points, width=4 / 2)
Esempio n. 4
0
    def process_voronoi_output(self,
                               districts,
                               fiducial_identity,
                               fiducial_pos,
                               error=[],
                               post_callback=None,
                               largs=(),
                               data_is_old=False):
        if data_is_old:
            return

        if post_callback is not None:
            post_callback(*largs)

        if not error:
            fid_ids = [self.district_blocks_fid[i] for i in fiducial_identity]
            if self.ros_bridge is not None:
                self.ros_bridge.update_voronoi(fiducial_pos, fid_ids,
                                               fiducial_identity, districts,
                                               self.district_metrics_fn,
                                               self.state_metrics_fn)

        if self.visualize_metric_data and self.current_focus_metric:
            for graphics in self.precinct_graphics.values():
                graphics[0].a = 1.  # undo possible previous error display
        else:
            if not districts:
                self.clear_voronoi()
            else:
                self.paint_precinct_by_district()

        if error:
            for precinct in error:
                self.precinct_graphics[precinct][0].a = 0

        for item in self.district_graphics:
            self.canvas.remove(item)
        self.district_graphics = []

        if self.show_voronoi_boundaries:
            with self.canvas:
                PushMatrix()
                Translate(self.focus_region_width, 0)
                Scale(Metrics.density)
                self.district_graphics.append(Color(1, 1, 0, 1))
                for district in districts:
                    if not district.boundary:
                        continue

                    self.district_graphics.append(
                        Line(points=district.boundary + district.boundary[:2],
                             width=2))
                PopMatrix()
Esempio n. 5
0
    def _draw_widget(self, *args) -> None:
        """Establish the drawing instructions for the widget."""
        del args

        if self.canvas is None:
            return

        # TODO: allow user to set rotation/scale origin
        center = center_of_points_list(self.points)
        self.canvas.clear()

        with self.canvas:
            Color(*self.color)
            Scale(self.scale, origin=center)
            Rotate(angle=self.rotation, origin=center)
            KivyQuad(points=self.points)
Esempio n. 6
0
    def _draw_widget(self, *args) -> None:
        del args

        if self.canvas is None:
            return

        anchor = (self.x - self.anchor_offset_pos[0], self.y - self.anchor_offset_pos[1])
        self.canvas.clear()

        with self.canvas:
            Color(*self.color)
            Rotate(angle=self.rotation, origin=anchor)
            Scale(self.scale).origin = anchor
            KivyEllipse(pos=self.pos, size=self.size,
                        segments=self.segments,
                        angle_start=self.angle_start,
                        angle_end=self.angle_end)
Esempio n. 7
0
 def setup_scene(self):
     Color(1, 0, 1, 1)
     PushMatrix()
     self.translate = Translate(0, -3, -10)
     self.rotx = Rotate(0, 1, 0, 0)
     self.rot = Rotate(0.5, 0, 1, 0)
     self.scale = Scale(1.0)
     #m = random.sample(xrange(10), 10)#list(self.scene.objects.values())[0]
     #m = list(self.scene.objects.values())[0]
     self.mesh = self.generateMesh()  #Mesh(
     UpdateNormalMatrix()
     #self.mesh = Mesh(
     #    vertices=m.vertices,
     #    indices=m.indices,
     #    fmt=m.vertex_format,
     #    mode='triangles',
     #)
     PopMatrix()
Esempio n. 8
0
    def Render(self, matrix, colorTransform, renderingIndex, renderingCount,
               visible):
        if not visible or colorTransform.multi.alpha == 0:
            return

        # print(matrix)
        # print("")

        self.m_color.rgba = (colorTransform.multi.red,
                             colorTransform.multi.green,
                             colorTransform.multi.blue,
                             colorTransform.multi.alpha)
        self.m_matrix.set(flat=[
            matrix.scaleX,
            -matrix.skew0,
            0,
            0,  # matrix.skew0, -matrix.skew0
            -matrix.skew1,
            matrix.scaleY,
            0,
            0,  # matrix.scaleX, matrix.scaleY
            0,
            0,
            1,
            0,
            matrix.translateX,
            matrix.translateY,
            0,
            1
        ])  # matrix.translateX, matrix.translateY
        self.m_inst.matrix = self.m_matrix

        self.canvas.add(PushMatrix())
        self.canvas.add(Color(1, 0, 0, 1))
        self.canvas.add(Translate(575, 300))
        self.canvas.add(Ellipse(size=(5, 5)))
        self.canvas.add(self.m_origin_transform)
        self.canvas.add(Ellipse(size=(5, 5)))
        self.canvas.add(Scale(1, -1, 1, origin=(0, 0)))
        self.canvas.add(self.m_inst)
        self.canvas.add(self.m_color)
        self.canvas.add(self.m_mesh)
        self.canvas.add(PopMatrix())
        self.canvas.ask_update()
Esempio n. 9
0
	def render(self, canvas, size, dt):
		ratio = 1.0
		width_is_bigger = True
		if size[0] > size[1]:
			ratio = size[1] / self.root
		else:
			width_is_bigger = False
			ratio = size[0] / self.root
		with canvas:
			if width_is_bigger:
				Translate(size[0] / 2.0 - self.root / 2.0 * ratio, 0.0)
			else:
				Translate(0.0, size[1] / 2.0 - self.root / 2.0 * ratio)
			Scale(ratio, ratio, 1.0)
			Color(0.6, 0.2, 0.2, 1.0)
			Rectangle(pos=(0, 0), size=(self.root, self.root))
		self.player1.render(canvas, dt)
		self.player2.render(canvas, dt)
		self.ball.render(canvas, dt)
Esempio n. 10
0
 def create_image(self):
     parent = self.parent
     if parent:
         canvas_parent_index = parent.canvas.indexof(self.canvas)
         if canvas_parent_index > -1:
             parent.canvas.remove(self.canvas)
     fbo = Fbo(size=self.size, with_stencilbuffer=True)
     with fbo:
         ClearColor(0, 0, 0, 0)
         ClearBuffers()
         Scale(1, -1, 1)
         Translate(-self.x, -self.y - self.height, 0)
     fbo.add(self.canvas)
     fbo.draw()
     image = Image.frombytes('RGBA', list(map(int, self.size)),
                             fbo.texture.pixels, 'raw', 'RGBA', 0, 1)
     fbo.remove(self.canvas)
     if parent is not None and canvas_parent_index > -1:
         parent.canvas.insert(canvas_parent_index, self.canvas)
     return image
Esempio n. 11
0
    def _draw_widget(self, *args) -> None:
        """Establish the drawing instructions for the widget."""
        del args

        if self.canvas is None:
            return

        # TODO: allow user to set rotation/scale origin
        center = center_of_points_list(self.points)
        self.canvas.clear()

        with self.canvas:
            Color(*self.color)
            Scale(self.scale, origin=center)
            Rotate(angle=self.rotation, origin=center)
            KivyLine(points=self.points,
                     width=self.thickness,
                     cap=self.cap,
                     joint=self.joint,
                     cap_precision=self.cap_precision,
                     joint_precision=self.joint_precision,
                     close=self.close)
Esempio n. 12
0
    def draw_line(self, *args):
        mapview = self.parent
        self.zoom = mapview.zoom
        point_list = []

        filename = "map_data.mbtiles"
        db_mbtiles = sqlite3.connect(filename)
        c_mbtiles = db_mbtiles.cursor()
        sql = "SELECT * FROM route_coordinates WHERE day = ?"
        day = (self.day, )
        c_mbtiles.execute(sql, day)
        myresult = c_mbtiles.fetchall()
        if self.day == '6':
            coordinates_list = eval(myresult[0][1][2:-2])
        else:
            coordinates_list = eval(myresult[0][1][2:-1])
        db_mbtiles.close()

        for index, geo_coordinates in enumerate(coordinates_list):
            screen_coordinates = mapview.get_window_xy_from(
                coordinates_list[2 * index + 1], coordinates_list[2 * index],
                mapview.zoom)
            point_list.append(screen_coordinates[0])
            point_list.append(screen_coordinates[1])
            if index == int((len(coordinates_list)) / 2 - 1):
                break

        # When zooming we must undo the current scatter transform
        # or the animation makes the line misplaced
        scatter = mapview._scatter
        x, y, s = scatter.x, scatter.y, scatter.scale

        with self.canvas:
            self.canvas.clear()
            Scale(1 / s, 1 / s, 1)
            Translate(-x, -y)
            Color(1, 0, 0, 1)
            Line(points=point_list, width=2, joint="miter")
Esempio n. 13
0
    def show_precincts(self):
        precinct_graphics = self.precinct_graphics = {}
        with self.canvas:
            PushMatrix()
            Translate(self.focus_region_width, 0)
            Scale(Metrics.density)
            for precinct in self.voronoi_mapping.precincts:
                assert len(precinct.boundary) >= 6
                tess = Tesselator()
                tess.add_contour(precinct.boundary)
                tess.tesselate(WINDING_ODD, TYPE_POLYGONS)

                graphics = [Color(rgba=(0, 0, 0, 1))]
                for vertices, indices in tess.meshes:
                    graphics.append(
                        Mesh(vertices=vertices,
                             indices=indices,
                             mode="triangle_fan"))

                graphics.append(Color(rgba=(0, 1, 0, 1)))
                graphics.append(Line(points=precinct.boundary, width=1))
                precinct_graphics[precinct] = graphics
            PopMatrix()
Esempio n. 14
0
    def _draw_widget(self, *args) -> None:
        """Establish the drawing instructions for the widget."""
        del args

        if self.canvas is None:
            return

        anchor = (self.x - self.anchor_offset_pos[0],
                  self.y - self.anchor_offset_pos[1])
        self.canvas.clear()

        with self.canvas:
            Color(*self.color)
            Rotate(angle=self.rotation, origin=anchor)
            Scale(self.scale).origin = anchor

            if self.corner_radius > 0:
                RoundedRectangle(pos=self.pos,
                                 size=self.size,
                                 radius=(self.corner_radius,
                                         self.corner_radius),
                                 segments=self.corner_segments)
            else:
                KivyRectangle(pos=self.pos, size=self.size)
Esempio n. 15
0
    def _show_image(config,
                    img,
                    scale=None,
                    translation=None,
                    rotation=None,
                    transform_matrix=None):
        from kivy.graphics.texture import Texture
        from kivy.graphics.fbo import Fbo
        from kivy.graphics import (Mesh, StencilPush, StencilUse, StencilUnUse,
                                   StencilPop, Rectangle, Color)
        from kivy.graphics.context_instructions import (PushMatrix, PopMatrix,
                                                        Rotate, Translate,
                                                        Scale,
                                                        MatrixInstruction,
                                                        BindTexture)
        from kivy.graphics.transformation import Matrix
        img_fmt = img.get_pixel_format()
        img_w, img_h = img.get_size()
        size = config['orig_size']
        pos = config['pos']
        canvas = config['canvas']

        if img_fmt not in ('yuv420p', 'rgba', 'rgb24', 'gray', 'bgr24',
                           'bgra'):
            ofmt = get_best_pix_fmt(
                img_fmt, ('yuv420p', 'rgba', 'rgb24', 'gray', 'bgr24', 'bgra'))
            swscale = SWScale(iw=img_w,
                              ih=img_h,
                              ifmt=img_fmt,
                              ow=0,
                              oh=0,
                              ofmt=ofmt)
            img = swscale.scale(img)
            img_fmt = img.get_pixel_format()

        kivy_ofmt = {
            'yuv420p': 'yuv420p',
            'rgba': 'rgba',
            'rgb24': 'rgb',
            'gray': 'luminance',
            'bgr24': 'bgr',
            'bgra': 'bgra'
        }[img_fmt]

        if kivy_ofmt == 'yuv420p':
            w2 = int(img_w / 2)
            h2 = int(img_h / 2)
            tex_y = Texture.create(size=(img_w, img_h), colorfmt='luminance')
            tex_u = Texture.create(size=(w2, h2), colorfmt='luminance')
            tex_v = Texture.create(size=(w2, h2), colorfmt='luminance')

            with canvas:
                fbo = Fbo(size=(img_w, img_h))
            with fbo:
                BindTexture(texture=tex_u, index=1)
                BindTexture(texture=tex_v, index=2)
                Rectangle(size=fbo.size, texture=tex_y)
            fbo.shader.fs = CeedDataReader._YUV_RGB_FS
            fbo['tex_y'] = 0
            fbo['tex_u'] = 1
            fbo['tex_v'] = 2

            tex = fbo.texture
            dy, du, dv, _ = img.to_memoryview()
            tex_y.blit_buffer(dy, colorfmt='luminance')
            tex_u.blit_buffer(du, colorfmt='luminance')
            tex_v.blit_buffer(dv, colorfmt='luminance')
        else:
            tex = Texture.create(size=(img_w, img_h), colorfmt=kivy_ofmt)
            tex.blit_buffer(img.to_memoryview()[0], colorfmt=kivy_ofmt)
        tex.flip_vertical()

        with canvas:
            StencilPush()
            Rectangle(pos=pos, size=size)
            StencilUse()

            PushMatrix()
            center = pos[0] + size[0] / 2, pos[1] + size[1] / 2
            if rotation:
                Rotate(angle=rotation, axis=(0, 0, 1), origin=center)
            if scale:
                Scale(scale, scale, 1, origin=center)
            if translation:
                Translate(*translation)
            if transform_matrix is not None:
                mat = Matrix()
                mat.set(array=transform_matrix)
                m = MatrixInstruction()
                m.matrix = mat
            Rectangle(size=(img_w, img_h), texture=tex, pos=pos)
            PopMatrix()

            StencilUnUse()
            Rectangle(pos=pos, size=size)
            StencilPop()
Esempio n. 16
0
    def generate_movie(self,
                       filename,
                       out_fmt='yuv420p',
                       codec='libx264',
                       lib_opts={'crf': '0'},
                       video_fmt='mp4',
                       start=None,
                       end=None,
                       canvas_size=(0, 0),
                       canvas_size_hint=(1, 1),
                       projector_pos=(0, 0),
                       projector_pos_hint=(None, None),
                       paint_funcs=(),
                       stimulation_transparency=1.,
                       lum=1.,
                       speed=1.,
                       hidden_shapes=None):
        from kivy.graphics import (Canvas, Translate, Fbo, ClearColor,
                                   ClearBuffers, Scale)
        from kivy.core.window import Window

        rate = float(self.view_controller.frame_rate)
        rate_int = int(rate)
        if rate != rate_int:
            raise ValueError('Frame rate should be integer')
        orig_w, orig_h = (self.view_controller.screen_width,
                          self.view_controller.screen_height)

        canvas_w, canvas_h = canvas_size
        cv_hint_w, cv_hint_h = canvas_size_hint
        w = int(canvas_w if cv_hint_w is None else orig_w * cv_hint_w)
        h = int(canvas_h if cv_hint_h is None else orig_h * cv_hint_h)

        projector_x, projector_y = projector_pos
        projector_hint_x, projector_hint_y = projector_pos_hint
        x = int(projector_x if projector_hint_x is None else orig_w *
                projector_hint_x)
        y = int(projector_y if projector_hint_y is None else orig_h *
                projector_hint_y)

        Window.size = w, h
        intensities = self.shapes_intensity

        n = len(intensities[next(iter(intensities.keys()))])
        if start is not None:
            start = int(start * rate)
            if start >= n:
                raise Exception('Start time is after the end of the data')
        else:
            start = 0

        if end is not None:
            end = int(math.ceil(end * rate)) + 1
            if end <= start:
                raise Exception('End time is before or at the start time')
        else:
            end = n

        stream = {
            'pix_fmt_in': 'rgba',
            'pix_fmt_out': out_fmt,
            'width_in': w,
            'height_in': h,
            'width_out': w,
            'height_out': h,
            'codec': codec,
            'frame_rate': (int(speed * rate_int), 1)
        }
        writer = MediaWriter(filename, [stream],
                             fmt=video_fmt,
                             lib_opts=lib_opts)

        fbo = Fbo(size=(w, h), with_stencilbuffer=True)
        with fbo:
            ClearColor(0, 0, 0, 1)
            ClearBuffers()
            Scale(1, -1, 1)
            Translate(0, -h, 0)

        config = {
            'canvas': fbo,
            'pos': (x, y),
            'size': (w, h),
            'orig_size': (orig_w, orig_h),
            'rate': rate
        }
        paint_funcs = [func(config) for func in paint_funcs]
        paint_funcs = [func for func in paint_funcs if func is not None]

        fbo.draw()
        img = Image(plane_buffers=[fbo.pixels], pix_fmt='rgba', size=(w, h))
        writer.write_frame(img, 0.)

        fbo.add(Translate(x, y))
        shape_views = self.stage_factory.get_shapes_gl_color_instructions(
            fbo, 'stage_replay')
        fbo.add(Translate(-x, -y))

        pbar = tqdm(total=(end - 1 - start) / rate,
                    file=sys.stdout,
                    unit='second',
                    unit_scale=1)

        # all shapes listed in intensities must be in shape_views. However,
        # we don't want to show shapes not given values in intensities or if
        # they are to be hidden
        unused_shapes = set(shape_views) - set(intensities)
        unused_shapes.update(set(hidden_shapes or []))
        for name in unused_shapes:
            if name in shape_views:
                shape_views[name].rgba = 0, 0, 0, 0

        for i in range(start, end):
            pbar.update(1 / rate)
            for name, intensity in intensities.items():
                r, g, b, a = intensity[i]
                if name in unused_shapes:
                    a = 0
                shape_views[name].rgba = \
                    r * lum, g * lum, b * lum, a * stimulation_transparency

            try:
                for func in paint_funcs:
                    func(i)
            except EndOfDataException:
                break

            fbo.draw()
            img = Image(plane_buffers=[fbo.pixels],
                        pix_fmt='rgba',
                        size=(w, h))
            writer.write_frame(img, (i - start + 1) / (rate * speed))
        pbar.close()
Esempio n. 17
0
 def _setup_canvas(self):
     self.canvas.clear()
     self.canvas.add(Translate(0, self.height))
     self.canvas.add(Scale(1, -1, -1))