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

Result caching and visuals reduction #81

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions common_utils/fast_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import math
from functools import lru_cache

SIN_BY_DEGREES = {}
COS_BY_DEGREES = {}
Expand Down Expand Up @@ -208,13 +209,30 @@ def rangef(
stop: float,
step: float
) -> list:
"""
Generate a list of numbers between the starting point
and ending point (inclusive)

Args:
start (float): The first number in the range.
stop (float): The last number in the range. Only included in the range if it is a factor of Start & step.
step (float): How much to increment by.

Returns:
list: [description]

Yields:
Iterator[list]: [description]
"""
while start < stop:
yield float(start)
start += step


@lru_cache(maxsize=500)
def get_circle_points(
center: list,
x: int,
y: int,
radius: float,
) -> list:
"""
Expand All @@ -233,11 +251,12 @@ def get_circle_points(
arc_radians = (angle_chunks / radius)
arc_radians = max(0.1, arc_radians)

points = [[int(center[0] + (radius * math.sin(radian))), int(center[1] + (radius * math.cos(radian)))] for radian in rangef(0, TWO_PI, arc_radians)]
points = [[int(x + (radius * math.sin(radian))), int(y + (radius * math.cos(radian)))] for radian in rangef(0, TWO_PI, arc_radians)]

return points


@lru_cache(maxsize=360)
def wrap_degrees(
angle: float
) -> float:
Expand All @@ -257,6 +276,7 @@ def wrap_degrees(
return angle


@lru_cache(maxsize=360)
def wrap_radians(
radians: float
) -> float:
Expand All @@ -275,6 +295,7 @@ def wrap_radians(
return radians


@lru_cache(maxsize=1000)
def get_radians(
degrees: float
) -> float:
Expand All @@ -291,6 +312,7 @@ def get_radians(
return __RADIANS_BY_DEGREES__[clamped_degrees]


@lru_cache(maxsize=1000)
def get_degrees(
radians: float
) -> float:
Expand All @@ -309,6 +331,7 @@ def get_degrees(
return __DEGREES_BY_RADIANS___[indexed_radians]


@lru_cache(maxsize=1000)
def sin(
degrees: float
) -> float:
Expand All @@ -325,6 +348,7 @@ def sin(
return SIN_BY_DEGREES[clamped_degs]


@lru_cache(maxsize=1000)
def cos(
degrees: float
) -> float:
Expand All @@ -341,6 +365,7 @@ def cos(
return COS_BY_DEGREES[clamped_degs]


@lru_cache(maxsize=1000)
def tan(
degrees: float
) -> float:
Expand Down Expand Up @@ -394,15 +419,15 @@ def rotate_points(
"""

# 2 - determine the angle of rotation compared to our "up"
rotation_degrees = int(wrap_degrees(rotation_degrees))
radians = math.radians(rotation_degrees)

detranslated_points = translate_points(
list_of_points,
[-rotation_center[0], -rotation_center[1]])

# 3 - Rotate the zero-based points
rotation_sin = SIN_BY_DEGREES[rotation_degrees]
rotation_cos = COS_BY_DEGREES[rotation_degrees]
rotation_sin = math.sin(radians)
rotation_cos = math.cos(radians)
rotated_points = [[point[0] * rotation_cos - point[1] * rotation_sin,
point[0] * rotation_sin + point[1] * rotation_cos] for point in detranslated_points]

Expand Down
14 changes: 7 additions & 7 deletions heads_up_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ def tick(
colors.YELLOW)

self.__render_perf_task__.run()
self.__reset_perf_task__.run()
finally:
# Change the frame buffer
if CONFIGURATION.flip_horizontal or CONFIGURATION.flip_vertical:
Expand Down Expand Up @@ -494,6 +493,11 @@ def __render_perf__(
self
):
TaskProfiler.log(self.__logger__)
self.__perf_log_count = self.__perf_log_count + 1

if self.__perf_log_count >= 3:
TaskProfiler.reset()
self.__perf_log_count = 0

def __init__(
self,
Expand All @@ -517,12 +521,6 @@ def __init__(
self.__render_perf__,
logger)

self.__reset_perf_task__ = IntermittentTask(
"Reset Performance Data",
15 * 60,
TaskProfiler.reset,
logger)

self.__logger__ = logger
self.__fps__ = RollingStats('FPS')
self.__texture_cache_size__ = RollingStats('TextureCacheSize')
Expand Down Expand Up @@ -566,6 +564,8 @@ def __init__(

self.__hud_views__ = self.__build_hud_views__(reduced_visuals)

self.__perf_log_count = 0

try:
self.web_server = configuration_server.HudServer()
except Exception as ex:
Expand Down
4 changes: 2 additions & 2 deletions rendering/opengl.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def circle(
None,
color,
True,
fast_math.get_circle_points(position, radius),
fast_math.get_circle_points(position[0], position[1], radius),
width,
is_antialiased)

Expand All @@ -143,7 +143,7 @@ def filled_circle(
polygon(
None,
color,
fast_math.get_circle_points(position, radius),
fast_math.get_circle_points(position[0], position[1], radius),
is_antialiased)


Expand Down
71 changes: 20 additions & 51 deletions rendering/software.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pygame
import pygame.gfxdraw
from common_utils import fast_math, generic_data_cache
from common_utils.local_debug import IS_SLOW
from common_utils.local_debug import IS_PI

RENDERER_NAME = "Rasterization"

Expand Down Expand Up @@ -37,7 +37,7 @@ def polygon(
framebuffer: pygame.Surface,
color: list,
points: list,
is_antialiased: bool = not IS_SLOW
is_antialiased: bool = not IS_PI
):
"""
Draws a filled polygon from the given points with the given color to the given surface.
Expand Down Expand Up @@ -70,7 +70,7 @@ def circle(
position: list,
radius: int,
width: int = 1,
is_antialiased: bool = not IS_SLOW
is_antialiased: bool = not IS_PI
):
"""
Draws an outline of a cicle at the given position with the given radius and given width
Expand All @@ -84,41 +84,24 @@ def circle(
is_antialiased (bool, optional): Should the circle be drawn anti aliased. Defaults to False.
"""

pygame.draw.circle(
segments(
framebuffer,
color,
position,
radius,
width)

if is_antialiased:
half_width = width / 2.0

# for any thickness, we need to draw
# around the outside and inside
# of the outline to make sure it is smoothe

pygame.gfxdraw.aacircle(
framebuffer,
position[0],
position[1],
int(radius + half_width - 0.5),
color)

pygame.gfxdraw.aacircle(
framebuffer,
True,
fast_math.get_circle_points(
position[0],
position[1],
int(radius - half_width - 0.5),
color)
radius),
width,
is_antialiased)


def filled_circle(
framebuffer: pygame.Surface,
color: list,
position: list,
radius: int,
is_antialiased: bool = not IS_SLOW
is_antialiased: bool = not IS_PI
):
"""
Draws a filled cicle at the given position with the given radius.
Expand All @@ -131,28 +114,14 @@ def filled_circle(
is_antialiased (bool, optional): Should the circle be drawn anti aliased. Defaults to False.
"""

# We need top draw the filled circle
# THEN the AA circle due to the filled
# circle being a traditional (thus aliased)
# polygon.
#
# Doing the aacircle ONLY draws the outline.
# Drawing both in this order allows for
# the appearence of a filled, anti-aliased circle.
pygame.gfxdraw.filled_circle(
polygon(
framebuffer,
int(position[0]),
int(position[1]),
int(radius),
color)

if is_antialiased:
pygame.gfxdraw.aacircle(
framebuffer,
int(position[0]),
int(position[1]),
int(radius),
color)
color,
fast_math.get_circle_points(
position[0],
position[1],
radius),
is_antialiased)


def segments(
Expand All @@ -161,7 +130,7 @@ def segments(
is_closed: bool,
points: list,
width: int = 1,
is_antialiased: bool = not IS_SLOW
is_antialiased: bool = not IS_PI
):
"""
Draws segements using the given points.
Expand Down Expand Up @@ -206,7 +175,7 @@ def segment(
start: list,
end: list,
width: int = 1,
is_antialiased: bool = not IS_SLOW
is_antialiased: bool = not IS_PI
):
"""
Draws a single line segment of the given color,
Expand All @@ -229,7 +198,7 @@ def segment(
# Never divide by zero. Also veritical and
# horizontal lines can not gain anything
# from anti-aliasing
if not is_antialiased or rise == 0 or run == 0:
if rise == 0 or run == 0:
pygame.draw.line(
framebuffer,
color,
Expand Down
7 changes: 4 additions & 3 deletions views/adsb_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,10 @@ def __render_info_card__(
text,
colors.BLACK,
card_color) for text in all_text]
widest_texture = max(
all_textures_and_sizes,
key=lambda x: x[2][0])[1][0]

texture_widths = [texture[2][0] for texture in all_textures_and_sizes]

widest_texture = max(texture_widths)
text_height = all_textures_and_sizes[0][2][1]

info_spacing = 1.2
Expand Down
23 changes: 14 additions & 9 deletions views/adsb_target_bugs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
Shows the "Info Cards" for ADSB information
"""

from common_utils.task_timer import TaskProfiler
from data_sources.ahrs_data import AhrsData
from data_sources.data_cache import HudDataCache
Expand All @@ -7,6 +11,10 @@


class AdsbTargetBugs(AdsbElement):
"""
Shows the "Info Cards" for ADSB information
"""

def __init__(
self,
degrees_of_pitch: float,
Expand Down Expand Up @@ -53,15 +61,12 @@ def __render_traffic_heading_bug__(
traffic_report,
orientation)

try:
self.__render_info_card__(
framebuffer,
str(traffic_report.get_display_name()),
additional_info_text,
heading_bug_x,
traffic_report.get_age())
except Exception as ex:
print("EX:{}".format(ex))
self.__render_info_card__(
framebuffer,
str(traffic_report.get_display_name()),
additional_info_text,
heading_bug_x,
traffic_report.get_age())

def render(
self,
Expand Down
2 changes: 1 addition & 1 deletion views/adsb_top_view_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ def __draw_compass_text__(
colors.BLACK,
1.0,
heading_text_rotation,
True)
not self.__reduced_visuals__)

def __draw_all_compass_headings__(
self,
Expand Down
2 changes: 1 addition & 1 deletion views/ahrs_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def __render_text__(
position,
color,
colors.BLACK,
True,
not self.__reduced_visuals__,
scale)

def __render_horizontal_centered_text__(
Expand Down
Loading