From ac6330efd037285d69558ded909951d6cc5cacff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?b=CA=B0edoh=E2=82=82=20sw=C3=A9?= Date: Thu, 4 Apr 2024 23:03:47 +0500 Subject: [PATCH 1/4] Use Numba to increase performance. --- gyro.py | 30 +++++++++++++++++++++--------- lines.py | 6 ++++++ main.py | 10 +++------- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/gyro.py b/gyro.py index deafdb6..1d42191 100644 --- a/gyro.py +++ b/gyro.py @@ -1,52 +1,61 @@ -try: - from numpy import complex256 -except ImportError: - from numpy import complex128 as complex256 - +from numba import jit from cmath import sqrt from math import tanh, atanh from lines import cLineIntersection +@jit def ZeroCheck(cN): - if cN == complex256(complex(0,0)): - return complex256(complex(1,0)) + if cN == complex(0,0): + return complex(1,0) else: return cN +@jit def c_tr(cN): return cN.real, cN.imag +@jit def cNorm(cN): return ZeroCheck(cN)/abs(ZeroCheck(cN)) +@jit def cDot(cA, cB): return (cA * cB.conjugate()).real +@jit def cDist(cA, cB): return abs(cA-cB) +@jit def MobiusInt(cA,cB,cC,cD): # Bruh return Klein2Poincare(cLineIntersection(Poincare2Klein(cA),Poincare2Klein(cB),Poincare2Klein(cC),Poincare2Klein(cD))) +@jit def MobiusAdd(cA, cB): return (cA + cB) / (1 + cA.conjugate() * cB) +@jit def MobiusGyr(cA, cB): return (1 + cA * cB.conjugate()) / (1 + cA.conjugate() * cB) +@jit def MobiusAddGyr(cA, cB): return MobiusAdd(cA, cB), MobiusGyr(cA, cB) +@jit def MobiusScalar(cA, rT): m = abs(cA) return tanh(rT * atanh(m)) * cA / m +@jit def MobiusCoadd(cA,cB): return MobiusAdd(cA, (MobiusGyr(cA,-cB) * cB)) +@jit def MobiusCosub(cA,cB): return MobiusAdd(cA, -(MobiusGyr(cA,cB) * cB)) +@jit def MobiusLine(cA,cB,rT): return MobiusAdd( cA, @@ -54,19 +63,22 @@ def MobiusLine(cA,cB,rT): MobiusAdd(-cA,cB), rT)) +@jit def MobiusDist(cA,cB): return abs(MobiusAdd(-cB,cA)) +@jit def Poincare2Klein(cN): return 2*cN / (1 + cDot(cN,cN)) +@jit def Klein2Poincare(cN): return (1-sqrt(1-cDot(cN,cN)))*cN/cDot(cN,cN) class GyroVector: def __init__(self, cPos, cRot): - self.cPos = complex256(cPos) - self.cRot = complex256(cRot) + self.cPos = complex(cPos) + self.cRot = complex(cRot) self.normalize() def __add__(gA, gB): diff --git a/lines.py b/lines.py index 76b2825..4f064c1 100644 --- a/lines.py +++ b/lines.py @@ -1,14 +1,20 @@ +from numba import jit + +@jit def LineIntersection(rX1,rY1,rX2,rY2,rX3,rY3,rX4,rY4): rX = ( (rX1*rY2-rY1*rX2)*(rX3-rX4)-(rX1-rX2)*(rX3*rY4-rY3*rX4) ) / ( (rX1-rX2)*(rY3-rY4)-(rY1-rY2)*(rX3-rX4) ) rY = ( (rX1*rY2-rY1*rX2)*(rY3-rY4)-(rY1-rY2)*(rX3*rY4-rY3*rX4) ) / ( (rX1-rX2)*(rY3-rY4)-(rY1-rY2)*(rX3-rX4) ) return (rX, rY) +@jit def cLineIntersection(cA0, cA1, cB0, cB1): rX, rY = LineIntersection(cA0.real, cA0.imag, cA1.real, cA1.imag, cB0.real, cB0.imag, cB1.real, cB1.imag) return complex(rX, rY) +@jit def Between(rA,rB,rN): return (rA < rN and rN < rB) or (rB < rN and rN < rA) +@jit def cBetween(cA,cB,cN): return Between(cA.real,cB.real,cN.real) and Between(cA.imag,cB.imag,cN.imag) diff --git a/main.py b/main.py index c32f2b9..944b548 100755 --- a/main.py +++ b/main.py @@ -1,9 +1,5 @@ #!/usr/bin/env python3 -try: - from numpy import complex256 -except ImportError: - from numpy import complex128 as complex256 - print("Warning! Cannot use full precision!") +from numba import jit from time import time_ns, sleep from cmath import exp, pi @@ -15,7 +11,7 @@ from lines import * def deg2rad(rA): return rA/180*PI -I = complex256(complex(0,1)) +I = complex(0,1) WHITE = (255,255,255) BLACK = (0,0,0) OFFSET = 0.015625 @@ -25,7 +21,7 @@ IROT = 1/ROT F = 5 * 10**7 F_ = 10**9 -gOrigin = GyroVector(complex256(0),1) +gOrigin = GyroVector(0,1) SKY = (127,127,255) GROUND = (102, 51, 0) From 84baa55a0cda94e1148869f9f2c1864196baf75f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?b=CA=B0edoh=E2=82=82=20sw=C3=A9?= Date: Thu, 4 Apr 2024 23:06:31 +0500 Subject: [PATCH 2/4] Add FPS cap. --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 944b548..83aee69 100755 --- a/main.py +++ b/main.py @@ -156,7 +156,7 @@ def mainLoop(): pygame.display.update() - clock.tick() + clock.tick(60) return True def main(): From 8169e9e7c3567a0995b35ec2d8ad878924dd3a40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?b=CA=B0edoh=E2=82=82=20sw=C3=A9?= Date: Thu, 4 Apr 2024 23:25:24 +0500 Subject: [PATCH 3/4] Add a button to disable FPS cap. --- main.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 83aee69..d13e44b 100755 --- a/main.py +++ b/main.py @@ -20,6 +20,7 @@ ROT = cNorm(exp(deg2rad(2)*I)) IROT = 1/ROT F = 5 * 10**7 F_ = 10**9 +bCap = True gOrigin = GyroVector(0,1) @@ -109,6 +110,9 @@ def mainLoop(): gPlayer.cRot *= -1 if event.key == pygame.K_F3: debugInfo = not debugInfo + if event.key == pygame.K_F1: + global bCap + bCap = not bCap if event.key == pygame.K_F2: if (len(wall_buffer) == 1): wall_buffer.append(gPlayer.cPos) @@ -155,8 +159,10 @@ def mainLoop(): pygame.draw.rect(display, WHITE, (c.real * 100 + 95, (c.imag * 100 + 195), 10, 10)) pygame.display.update() - - clock.tick(60) + if bCap: + clock.tick(60) + else: + clock.tick() return True def main(): From adf47220c7b4db6f641674e996bb9c6ac556cccb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?b=CA=B0edoh=E2=82=82=20sw=C3=A9?= Date: Thu, 4 Apr 2024 23:31:45 +0500 Subject: [PATCH 4/4] Fix rotation display. --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index d13e44b..2c77a27 100755 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ from numba import jit from time import time_ns, sleep from cmath import exp, pi -from math import acos +from math import acos, copysign import pygame import pygame.freetype from gyro import * @@ -80,7 +80,7 @@ def renderDebugInfo(gPlayer, clock, fontSize = 18): fps_surf = font.render("FPS: " + str(int(clock.get_fps())), font_fg, font_bg)[0] cPos_surf = font.render("cPos: " + str(gPlayer.cPos), font_fg, font_bg)[0] - cRot_surf = font.render("cRot: " + str(acos(gPlayer.cRot.real) / pi * 180), font_fg, font_bg)[0] + cRot_surf = font.render("cRot: " + str(copysign(acos(gPlayer.cRot.real),gPlayer.cRot.imag) / pi * 180), font_fg, font_bg)[0] return fps_surf, cPos_surf, cRot_surf