def setUp(self): self.tk_renderer = TKRenderer(scale=2) self.stationary_object = StationaryObject((100, 100)) self.viewable_objects = [self.stationary_object] self.viewable_object_renderer = ViewableObjectsRenderer( viewable_objects=self.viewable_objects, ) return self
class ElementRenderer: def __init__(self, **kargs): self.tk_renderer = TKRenderer() self.moving_rendered_elements = [] self.stationary_rendered_elements = [] def render(self, timestamp=None, **kargs): if timestamp is None: self.render_stationary_elements() else: self.render_moving_elements(timestamp) return self # implement in child class def render_stationary_elements(self): pass # implement in child class def render_moving_elements(self, timestamp): pass def delete_moving_rendered_elements(self): self.tk_renderer.delete_elements(self.moving_rendered_elements) self.moving_rendered_elements = [] return self def delete_stationary_rendered_elements(self): self.tk_renderer.delete_elements(self.stationary_rendered_elements) return self def delete_all_rendered_elements(self): self.delete_moving_rendered_elements() self.delete_stationary_rendered_elements()
class TestCameraRenderer(unittest.TestCase): def setUp(self): self.num_timestamps = 180 self.tk_renderer = TKRenderer(canvas_width=1200, canvas_height=800, scale=2) self.camera = Camera() self.camera.set_actual_position((200,200)).\ set_gps_max_error(10).\ set_num_timestamps(self.num_timestamps).\ set_gps_position((200, 210)) for timestamp in range(self.num_timestamps): self.camera.set_state_of_pan_motor_angle_at_timestamp(timestamp*2, timestamp) self.camera_renderer = CameraRenderer(self.camera) self.camera_renderer.render() self.animator = Animator( [self.camera_renderer], num_timestamps=self.num_timestamps, seconds_per_timestamp=0.05 ) self.tk_renderer.set_mouse_click_callback(self.animator.play) return self def visualize(self): self.tk_renderer.start_tk_event_loop()
def __init__(self, camera, **kargs): self.camera = camera self.tk_renderer = TKRenderer() self.camera_color = 'green' self.camera_gps_color = 'red' self.gps_err_color = 'red' self.view_triangle_color = '#FEFAED' super().__init__(**kargs)
def setUp(self): self.canvas_width = 2000 self.canvas_height = 1000 self.bounary_points = [(100, 100), (300, 100), (300, 200), (100, 200)] self.tk_renderer = TKRenderer(scale=2, canvas_width=self.canvas_width, canvas_height=self.canvas_height) self.boundary = Boundary(self.bounary_points) return self
def __init__(self, element_renderers=[], num_timestamps=0, seconds_per_timestamp=1): self.element_renderers = element_renderers self.tk_renderer = TKRenderer() self.current_timestamp = 0 self.num_timestamps = num_timestamps self.seconds_per_timestamp = seconds_per_timestamp self.render_stationary_sub_elements()
class TestViewableObjectRenderer(unittest.TestCase): def setUp(self): self.tk_renderer = TKRenderer(scale=2) self.stationary_object = StationaryObject((100, 100)) self.viewable_objects = [self.stationary_object] self.viewable_object_renderer = ViewableObjectsRenderer( viewable_objects=self.viewable_objects, ) return self def visualize(self): self.viewable_object_renderer.render(0) self.tk_renderer.start_tk_event_loop()
class Animator: def __init__(self, element_renderers=[], num_timestamps=0, seconds_per_timestamp=1): self.element_renderers = element_renderers self.tk_renderer = TKRenderer() self.current_timestamp = 0 self.num_timestamps = num_timestamps self.seconds_per_timestamp = seconds_per_timestamp self.render_stationary_sub_elements() def set_element_renderers(self, element_renderers): self.element_renderers = element_renderers self.render_stationary_sub_elements() return self def get_element_renderers(self): return self.element_renderers def add_element_renderers(self, element_renderers): if type(element_renderers) != list: element_renderers = [element_renderers] self.element_renderers += element_renderers self.render_stationary_sub_elements() return self def render_stationary_sub_elements(self): for element in self.element_renderers: element.render() self.tk_renderer.update() def set_seconds_per_timestamp(self, seconds): self.seconds_per_timestamp = seconds return self def get_seconds_per_timestamp(self): return self.seconds_per_timestamp def play(self, *pargs, **kargs): for timestamp in range(self.current_timestamp, self.num_timestamps): for element in self.element_renderers: element.render(timestamp=timestamp) self.tk_renderer.update() time.sleep(self.seconds_per_timestamp)
def run(self): self._add_renderers_for_objects() self._set_animator() TKRenderer().set_canvas_height(self.canvas_height).\ set_canvas_width(self.canvas_width).\ set_scale(self.scale).\ set_mouse_click_callback(self.animator.play).\ start_tk_event_loop()
class BoundaryRenderer(ElementRenderer): def __init__(self, boundary, **kargs): self.tk_renderer = TKRenderer() self.boundary = boundary super().__init__(**kargs) def render_stationary_elements(self): self.delete_stationary_rendered_elements() self.stationary_rendered_elements.append(self.tk_renderer.create_polygon(self.boundary.exterior.coords, fill='', outline='orange'))
class TestBoundaryRenderer(unittest.TestCase): def setUp(self): self.canvas_width = 2000 self.canvas_height = 1000 self.bounary_points = [(100, 100), (300, 100), (300, 200), (100, 200)] self.tk_renderer = TKRenderer(scale=2, canvas_width=self.canvas_width, canvas_height=self.canvas_height) self.boundary = Boundary(self.bounary_points) return self def test_params_are_passed_to_tk_renderer_singleton(self): self.assertEqual(self.tk_renderer.canvas_width, self.canvas_width) self.assertEqual(self.tk_renderer.canvas_height, self.canvas_height) def visualize(self): boundary_renderer = BoundaryRenderer(self.boundary) boundary_renderer.render() self.tk_renderer.start_tk_event_loop()
def setUp(self): self.num_timestamps = 1000 self.tk_renderer = TKRenderer(canvas_width=800, canvas_height=700) self.boundary = Boundary([(100, 100), (300, 100), (300, 200), (100, 200)]) self.boundary_renderer = BoundaryRenderer(self.boundary) self.random_moving_objects = [ RandomlyMovingObject(boundary=self.boundary, num_timestamps=self.num_timestamps), RandomlyMovingObject(boundary=self.boundary, num_timestamps=self.num_timestamps) ] self.viewable_objects_renderer = ViewableObjectsRenderer( viewable_objects=self.random_moving_objects) self.animator = Animator(num_timestamps=self.num_timestamps, seconds_per_timestamp=0.2) self.animator.add_element_renderers( [self.viewable_objects_renderer, self.boundary_renderer]) self.tk_renderer.set_mouse_click_callback(self.animator.play) return self
import sys, os sys.path.insert(0, os.getcwd()) from tk_canvas_renderers.tk_renderer import TKRenderer from tk_canvas_renderers.element_renderers import PhotoRenderer from tk_canvas_renderers.animator import Animator import PIL photo_url = os.getcwd() + '/data/images/will.jpg' image = PIL.Image.open(photo_url) image_r = image.resize((400, 300)) image_renderer = PhotoRenderer(image_r) animator = Animator([image_renderer]) TKRenderer().set_canvas_height(500).\ set_canvas_width(1200).\ set_scale(1).\ set_mouse_click_callback(animator.play) TKRenderer().start_tk_event_loop() # self.boundary_renderer = BoundaryRenderer(self.boundary) # self.renderers = [ # self.camera_renderer, # self.viewable_objects_renderer, # self.boundary_renderer # ] # self.animator = Animator(element_renderers=self.renderers, num_timestamps=self.num_timestamps, seconds_per_timestamp=0.2) # TKRenderer().set_canvas_height(800).\ # set_canvas_width(900).\
def __init__(self, boundary, **kargs): self.tk_renderer = TKRenderer() self.boundary = boundary super().__init__(**kargs)
class CameraRenderer(ElementRenderer): def __init__(self, camera, **kargs): self.camera = camera self.tk_renderer = TKRenderer() self.camera_color = 'green' self.camera_gps_color = 'red' self.gps_err_color = 'red' self.view_triangle_color = '#FEFAED' super().__init__(**kargs) def render_stationary_elements(self): self.delete_stationary_rendered_elements() self.render_camera_gps() self.render_gps_error_circle() self.render_camera() return self def render_moving_elements(self, timestamp): self.delete_moving_rendered_elements() self.render_view_triangle(timestamp) return self def render_camera(self): pos = self.camera.get_actual_position() text = 'c' + str(pos) self.stationary_rendered_elements.append( self.tk_renderer.create_dot(pos, fill=self.camera_color, outline=self.camera_color) ) self.stationary_rendered_elements.append( self.tk_renderer.create_dot_label(pos, text=text, fill=self.camera_color) ) return self def render_camera_gps(self): pos = self.camera.get_gps_position() label_text = 'gps' + str(self.camera.get_gps_position()) self.stationary_rendered_elements.append( self.tk_renderer.create_dot( pos, outline=self.camera_gps_color, fill=self.camera_gps_color, ) ) self.stationary_rendered_elements.append( self.tk_renderer.create_dot_label( pos, text=label_text, fill=self.camera_gps_color, ) ) return self def render_gps_error_circle(self): err = self.camera.get_gps_max_error() self.stationary_rendered_elements.append( self.tk_renderer.create_circle_with_center_and_radius( self.camera.get_gps_position(), err, outline=self.gps_err_color, ) ) return self def render_view_triangle(self, timestamp): vt = self.camera.get_view_triangle(timestamp).exterior.coords rvt = self.tk_renderer.create_polygon(vt, fill=self.view_triangle_color, outline=self.view_triangle_color) self.tk_renderer.lower_element(rvt) self.moving_rendered_elements.append(rvt) return self
def __init__(self, **kargs): self.tk_renderer = TKRenderer() self.moving_rendered_elements = [] self.stationary_rendered_elements = []