jelka 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jelka-0.0.1/.github/workflows/test.yml +29 -0
- jelka-0.0.1/.gitignore +162 -0
- jelka-0.0.1/LICENSE +21 -0
- jelka-0.0.1/PKG-INFO +12 -0
- jelka-0.0.1/PackageREADME.md +0 -0
- jelka-0.0.1/README.md +1 -0
- jelka-0.0.1/pyproject.toml +25 -0
- jelka-0.0.1/requirements.txt +8 -0
- jelka-0.0.1/ruff.toml +58 -0
- jelka-0.0.1/src/jelka/__init__.py +7 -0
- jelka-0.0.1/src/jelka/color.py +66 -0
- jelka-0.0.1/src/jelka/jelka.py +128 -0
- jelka-0.0.1/src/jelka/path.py +37 -0
- jelka-0.0.1/src/jelka/plane.py +8 -0
- jelka-0.0.1/src/jelka/shapes.py +7 -0
- jelka-0.0.1/src/jelka/sphere.py +21 -0
- jelka-0.0.1/src/jelka/types.py +10 -0
- jelka-0.0.1/src/jelka/util.py +11 -0
- jelka-0.0.1/test/__init__.py +0 -0
- jelka-0.0.1/test/barbers.py +41 -0
- jelka-0.0.1/test/gradient.py +23 -0
- jelka-0.0.1/test/lines.py +29 -0
- jelka-0.0.1/test/main.py +34 -0
- jelka-0.0.1/test/one_color_change.py +20 -0
- jelka-0.0.1/test/positions.csv +294 -0
- jelka-0.0.1/test/pulse.py +37 -0
- jelka-0.0.1/test/rain.py +32 -0
- jelka-0.0.1/test/rotating_full.py +40 -0
- jelka-0.0.1/test/rotating_plane.py +40 -0
- jelka-0.0.1/test/scan_x.py +28 -0
- jelka-0.0.1/test/scan_y.py +28 -0
- jelka-0.0.1/test/scan_z.py +22 -0
- jelka-0.0.1/test/simulation.py +130 -0
- jelka-0.0.1/test/spiral.py +49 -0
- jelka-0.0.1/test/template.py +17 -0
- jelka-0.0.1/test/vzorec.py +16 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
name: test
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
[push, pull_request]
|
|
5
|
+
|
|
6
|
+
jobs:
|
|
7
|
+
lint-and-typecheck:
|
|
8
|
+
runs-on: ubuntu-latest
|
|
9
|
+
steps:
|
|
10
|
+
- uses: actions/checkout@v4
|
|
11
|
+
- uses: actions/setup-python@v5
|
|
12
|
+
with:
|
|
13
|
+
python-version: '3.8'
|
|
14
|
+
cache: 'pip'
|
|
15
|
+
- run: pip install -r requirements.txt
|
|
16
|
+
- name: linter
|
|
17
|
+
run: ruff check
|
|
18
|
+
- name: typecheck
|
|
19
|
+
run: pyright
|
|
20
|
+
test:
|
|
21
|
+
runs-on: ubuntu-latest
|
|
22
|
+
steps:
|
|
23
|
+
- uses: actions/checkout@v4
|
|
24
|
+
- uses: actions/setup-python@v5
|
|
25
|
+
with:
|
|
26
|
+
python-version: '3.8'
|
|
27
|
+
cache: 'pip'
|
|
28
|
+
- run: pip install -r requirements.txt
|
|
29
|
+
- run: pytest --doctest-modules
|
jelka-0.0.1/.gitignore
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py,cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# poetry
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
102
|
+
#poetry.lock
|
|
103
|
+
|
|
104
|
+
# pdm
|
|
105
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
106
|
+
#pdm.lock
|
|
107
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
108
|
+
# in version control.
|
|
109
|
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
|
110
|
+
.pdm.toml
|
|
111
|
+
.pdm-python
|
|
112
|
+
.pdm-build/
|
|
113
|
+
|
|
114
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
115
|
+
__pypackages__/
|
|
116
|
+
|
|
117
|
+
# Celery stuff
|
|
118
|
+
celerybeat-schedule
|
|
119
|
+
celerybeat.pid
|
|
120
|
+
|
|
121
|
+
# SageMath parsed files
|
|
122
|
+
*.sage.py
|
|
123
|
+
|
|
124
|
+
# Environments
|
|
125
|
+
.env
|
|
126
|
+
.venv
|
|
127
|
+
env/
|
|
128
|
+
venv/
|
|
129
|
+
ENV/
|
|
130
|
+
env.bak/
|
|
131
|
+
venv.bak/
|
|
132
|
+
|
|
133
|
+
# Spyder project settings
|
|
134
|
+
.spyderproject
|
|
135
|
+
.spyproject
|
|
136
|
+
|
|
137
|
+
# Rope project settings
|
|
138
|
+
.ropeproject
|
|
139
|
+
|
|
140
|
+
# mkdocs documentation
|
|
141
|
+
/site
|
|
142
|
+
|
|
143
|
+
# mypy
|
|
144
|
+
.mypy_cache/
|
|
145
|
+
.dmypy.json
|
|
146
|
+
dmypy.json
|
|
147
|
+
|
|
148
|
+
# Pyre type checker
|
|
149
|
+
.pyre/
|
|
150
|
+
|
|
151
|
+
# pytype static type analyzer
|
|
152
|
+
.pytype/
|
|
153
|
+
|
|
154
|
+
# Cython debug symbols
|
|
155
|
+
cython_debug/
|
|
156
|
+
|
|
157
|
+
# PyCharm
|
|
158
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
159
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
160
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
161
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
162
|
+
#.idea/
|
jelka-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Jelka FMF
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
jelka-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: jelka
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: API
|
|
5
|
+
Project-URL: Homepage, https://github.com/Jelka-FMF/JelkaPy
|
|
6
|
+
Project-URL: Issues, https://github.com/Jelka-FMF/JelkaPy/issues
|
|
7
|
+
Author-email: Bor Brudar <bor.brudar@gmail.com>
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Requires-Dist: jelka-validator
|
|
File without changes
|
jelka-0.0.1/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Jelka Python API
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "jelka"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name="Bor Brudar", email="bor.brudar@gmail.com" },
|
|
10
|
+
]
|
|
11
|
+
description = "API"
|
|
12
|
+
readme = "PackageREADME.md"
|
|
13
|
+
requires-python = ">=3.8"
|
|
14
|
+
dependencies = [
|
|
15
|
+
"jelka-validator"
|
|
16
|
+
]
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"License :: OSI Approved :: MIT License",
|
|
20
|
+
"Operating System :: OS Independent",
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
[project.urls]
|
|
24
|
+
Homepage = "https://github.com/Jelka-FMF/JelkaPy"
|
|
25
|
+
Issues = "https://github.com/Jelka-FMF/JelkaPy/issues"
|
jelka-0.0.1/ruff.toml
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
exclude = [
|
|
2
|
+
".bzr",
|
|
3
|
+
".direnv",
|
|
4
|
+
".eggs",
|
|
5
|
+
".git",
|
|
6
|
+
".git-rewrite",
|
|
7
|
+
".hg",
|
|
8
|
+
".ipynb_checkpoints",
|
|
9
|
+
".mypy_cache",
|
|
10
|
+
".nox",
|
|
11
|
+
".pants.d",
|
|
12
|
+
".pyenv",
|
|
13
|
+
".pytest_cache",
|
|
14
|
+
".pytype",
|
|
15
|
+
".ruff_cache",
|
|
16
|
+
".svn",
|
|
17
|
+
".tox",
|
|
18
|
+
".venv",
|
|
19
|
+
".vscode",
|
|
20
|
+
"__pypackages__",
|
|
21
|
+
"_build",
|
|
22
|
+
"buck-out",
|
|
23
|
+
"build",
|
|
24
|
+
"dist",
|
|
25
|
+
"node_modules",
|
|
26
|
+
"site-packages",
|
|
27
|
+
"venv",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
# Support Python 3.10+.
|
|
31
|
+
target-version = "py310"
|
|
32
|
+
|
|
33
|
+
line-length = 128
|
|
34
|
+
indent-width = 4
|
|
35
|
+
|
|
36
|
+
[lint]
|
|
37
|
+
# Add the `line-too-long` rule to the enforced rule set.
|
|
38
|
+
extend-select = ["E501"]
|
|
39
|
+
|
|
40
|
+
[format]
|
|
41
|
+
# Like Black, use double quotes for strings.
|
|
42
|
+
quote-style = "double"
|
|
43
|
+
|
|
44
|
+
# Like Black, indent with spaces, rather than tabs.
|
|
45
|
+
indent-style = "space"
|
|
46
|
+
|
|
47
|
+
# Like Black, respect magic trailing commas.
|
|
48
|
+
skip-magic-trailing-comma = false
|
|
49
|
+
|
|
50
|
+
# Like Black, automatically detect the appropriate line ending.
|
|
51
|
+
line-ending = "auto"
|
|
52
|
+
|
|
53
|
+
# Enable auto-formatting of code examples in docstrings. Markdown,
|
|
54
|
+
# reStructuredText code/literal blocks and doctests are all supported.
|
|
55
|
+
#
|
|
56
|
+
# This is currently disabled by default, but it is planned for this
|
|
57
|
+
# to be opt-out in the future.
|
|
58
|
+
docstring-code-format = false
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
from typing import Tuple
|
|
2
|
+
import random
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Color:
|
|
6
|
+
def __init__(self, r: float, g: float, b: float):
|
|
7
|
+
self.r = r
|
|
8
|
+
self.g = g
|
|
9
|
+
self.b = b
|
|
10
|
+
|
|
11
|
+
def __add__(self, other: "Color") -> "Color":
|
|
12
|
+
return Color(self.r + other.r, self.g + other.g, self.b + other.b)
|
|
13
|
+
|
|
14
|
+
def __sub__(self, other: "Color") -> "Color":
|
|
15
|
+
return Color(self.r - other.r, self.g - other.g, self.b - other.b)
|
|
16
|
+
|
|
17
|
+
def __mul__(self, other: "Color") -> "Color":
|
|
18
|
+
return Color(self.r * other.r, self.g * other.g, self.b * other.b)
|
|
19
|
+
|
|
20
|
+
def __truediv__(self, other: "Color") -> "Color":
|
|
21
|
+
return Color(self.r / other.r, self.g / other.g, self.b / other.b)
|
|
22
|
+
|
|
23
|
+
def __mul__(self, other: float) -> "Color":
|
|
24
|
+
return Color(self.r * other, self.g * other, self.b * other)
|
|
25
|
+
|
|
26
|
+
def __truediv__(self, other: float) -> "Color":
|
|
27
|
+
return Color(self.r / other, self.g / other, self.b / other)
|
|
28
|
+
|
|
29
|
+
def __eq__(self, other: "Color") -> bool:
|
|
30
|
+
return self.r == other.r and self.g == other.g and self.b == other.b
|
|
31
|
+
|
|
32
|
+
def __str__(self) -> str:
|
|
33
|
+
return f"Color({self.r}, {self.g}, {self.b})"
|
|
34
|
+
|
|
35
|
+
def __repr__(self) -> str:
|
|
36
|
+
return f"Color({self.r}, {self.g}, {self.b})"
|
|
37
|
+
|
|
38
|
+
def to_tuple(self) -> Tuple[float, float, float]:
|
|
39
|
+
return self.r, self.g, self.b
|
|
40
|
+
|
|
41
|
+
def to_list(self) -> Tuple[float, float, float]:
|
|
42
|
+
return [self.r, self.g, self.b]
|
|
43
|
+
|
|
44
|
+
def to_write(self) -> Tuple[int, int, int]:
|
|
45
|
+
def round_clamp(value):
|
|
46
|
+
return max(0, min(255, round(value)))
|
|
47
|
+
|
|
48
|
+
return round_clamp(self.r), round_clamp(self.g), round_clamp(self.b)
|
|
49
|
+
|
|
50
|
+
def vivid(self):
|
|
51
|
+
"""Makes color more vivid."""
|
|
52
|
+
return Color.vivid(self)
|
|
53
|
+
|
|
54
|
+
def random_color() -> "Color":
|
|
55
|
+
return Color(random.uniform(0, 255), random.uniform(0, 255), random.uniform(0, 255))
|
|
56
|
+
|
|
57
|
+
def vivid(c: "Color") -> "Color":
|
|
58
|
+
"""Makes color more vivid."""
|
|
59
|
+
min(c.r, c.g, c.b)
|
|
60
|
+
if c.r <= c.g and c.r <= c.b:
|
|
61
|
+
c.r = 0
|
|
62
|
+
elif c.g <= c.r and c.g <= c.b:
|
|
63
|
+
c.g = 0
|
|
64
|
+
else:
|
|
65
|
+
c.b = 0
|
|
66
|
+
return c
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
from .color import Color
|
|
2
|
+
from typing import Callable, List
|
|
3
|
+
import jelka_validator.datawriter as dw
|
|
4
|
+
import time
|
|
5
|
+
import sys
|
|
6
|
+
import os
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Jelka:
|
|
10
|
+
def __init__(self, n: int, frame_rate: int, color: Color = Color(0, 0, 0), file: str | None = None):
|
|
11
|
+
self.n = n
|
|
12
|
+
self.color = color
|
|
13
|
+
self.lights: List[Color] = [color for _ in range(n)]
|
|
14
|
+
self.dw = dw.DataWriter(300)
|
|
15
|
+
self.frame = 0
|
|
16
|
+
self.frame_rate = frame_rate
|
|
17
|
+
self.positions_raw = dict()
|
|
18
|
+
self.positions_normalized = dict()
|
|
19
|
+
self.start_time = time.perf_counter()
|
|
20
|
+
self.cur_time = 0
|
|
21
|
+
self.objects: List["Sphere"] = list()
|
|
22
|
+
self.clear = False
|
|
23
|
+
for i in range(n):
|
|
24
|
+
self.positions_raw[i] = (0, 0, 0)
|
|
25
|
+
|
|
26
|
+
# Provide default file locations
|
|
27
|
+
filenames = [
|
|
28
|
+
os.path.join(os.getcwd(), "positions.csv"),
|
|
29
|
+
os.path.join(os.path.dirname(sys.argv[0]), "positions.csv"),
|
|
30
|
+
os.path.join(os.getcwd(), "../data/positions.csv"),
|
|
31
|
+
os.path.join(os.path.dirname(sys.argv[0]), "../data/positions.csv"),
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
# Allow specifying custom path
|
|
35
|
+
if file:
|
|
36
|
+
filenames = [file]
|
|
37
|
+
|
|
38
|
+
# Try to load positions from various files
|
|
39
|
+
self.load_positions(filenames)
|
|
40
|
+
|
|
41
|
+
# Normalize the positions
|
|
42
|
+
self.normalize_positions(0, 1)
|
|
43
|
+
|
|
44
|
+
def load_positions(self, filenames: List[str]):
|
|
45
|
+
"""Loads positions from the first available file."""
|
|
46
|
+
|
|
47
|
+
for filename in filenames:
|
|
48
|
+
if not os.path.isfile(filename):
|
|
49
|
+
continue
|
|
50
|
+
|
|
51
|
+
with open(filename) as file:
|
|
52
|
+
print(f"Loading positions from {filename}", file=sys.stderr, flush=True)
|
|
53
|
+
|
|
54
|
+
for line in file.readlines():
|
|
55
|
+
line = line.strip()
|
|
56
|
+
if line == "":
|
|
57
|
+
continue
|
|
58
|
+
i, x, y, z = line.split(",")
|
|
59
|
+
self.positions_raw[int(i)] = (float(x), float(y), float(z))
|
|
60
|
+
|
|
61
|
+
return
|
|
62
|
+
|
|
63
|
+
raise FileNotFoundError("No valid file found to load positions from")
|
|
64
|
+
|
|
65
|
+
def normalize_positions(self, l: int = 0, r: int = 1, mn_=None, mx_=None):
|
|
66
|
+
mn = min([pos[0] for pos in self.positions_raw.values()])
|
|
67
|
+
mn = min(min([pos[1] for pos in self.positions_raw.values()]), mn)
|
|
68
|
+
mn = min(min([pos[2] for pos in self.positions_raw.values()]), mn)
|
|
69
|
+
if mn_ is not None:
|
|
70
|
+
mn = mn_
|
|
71
|
+
|
|
72
|
+
mx = max([pos[0] for pos in self.positions_raw.values()])
|
|
73
|
+
mx = max(max([pos[1] for pos in self.positions_raw.values()]), mx)
|
|
74
|
+
mx = max(max([pos[2] for pos in self.positions_raw.values()]), mx)
|
|
75
|
+
mx += 0.01 # to avoid division by zero
|
|
76
|
+
if mx_ is not None:
|
|
77
|
+
mx = mx_
|
|
78
|
+
|
|
79
|
+
for i, pos in self.positions_raw.items():
|
|
80
|
+
x = (pos[0] - mn) / (mx - mn) * (r - l) + l
|
|
81
|
+
y = (pos[1] - mn) / (mx - mn) * (r - l) + l
|
|
82
|
+
z = (pos[2] - mn) / (mx - mn) * (r - l) + l
|
|
83
|
+
self.positions_normalized[i] = (x, y, z)
|
|
84
|
+
|
|
85
|
+
def set_light(self, i: int, color: Color):
|
|
86
|
+
self.lights[i] = color
|
|
87
|
+
|
|
88
|
+
def write_lights(self):
|
|
89
|
+
writable = [light.to_write() for light in self.lights]
|
|
90
|
+
self.dw.write_frame(writable)
|
|
91
|
+
|
|
92
|
+
def run(self, callback: Callable[["Jelka"], None] = None, init: Callable[["Jelka"], None] = None):
|
|
93
|
+
dt = 1.0 / self.frame_rate
|
|
94
|
+
if init is not None:
|
|
95
|
+
init(self)
|
|
96
|
+
self.start_time = time.perf_counter()
|
|
97
|
+
|
|
98
|
+
while True:
|
|
99
|
+
self.elapsed_time = time.perf_counter() - self.start_time
|
|
100
|
+
current_time = time.perf_counter()
|
|
101
|
+
|
|
102
|
+
if self.clear:
|
|
103
|
+
for i in range(self.n):
|
|
104
|
+
self.set_light(i, Color(0, 0, 0))
|
|
105
|
+
# print(self.elapsed_time)
|
|
106
|
+
for obj in self.objects:
|
|
107
|
+
obj.update_pos(self.elapsed_time)
|
|
108
|
+
for i in range(self.n):
|
|
109
|
+
if obj.is_inside(self.positions_normalized[i]):
|
|
110
|
+
self.set_light(i, Color(obj.color.r, obj.color.g, obj.color.b))
|
|
111
|
+
|
|
112
|
+
if callback is not None:
|
|
113
|
+
callback(self)
|
|
114
|
+
else:
|
|
115
|
+
break
|
|
116
|
+
self.write_lights()
|
|
117
|
+
|
|
118
|
+
new_time = time.perf_counter()
|
|
119
|
+
frame_time = new_time - current_time
|
|
120
|
+
self.frame += 1
|
|
121
|
+
|
|
122
|
+
if frame_time <= dt:
|
|
123
|
+
time.sleep(dt - frame_time)
|
|
124
|
+
# print(f"FPS: {self.frame_rate}")
|
|
125
|
+
else:
|
|
126
|
+
# print(f"Frame rate is too slow: {frame_time}/ {dt}")
|
|
127
|
+
# print(f"FPS: {int(1.0 / frame_time)}")
|
|
128
|
+
pass
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
from .types import Position
|
|
2
|
+
from typing import List
|
|
3
|
+
import math
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Path:
|
|
7
|
+
def __init__(self):
|
|
8
|
+
self.positions: List[Position] = []
|
|
9
|
+
self.durations: List[float] = []
|
|
10
|
+
self.loop = True
|
|
11
|
+
|
|
12
|
+
def add_position(self, position: Position, time: float):
|
|
13
|
+
self.positions.append(position)
|
|
14
|
+
self.durations.append(time)
|
|
15
|
+
|
|
16
|
+
def current_position(self, time: float) -> Position:
|
|
17
|
+
# print(self.durations)
|
|
18
|
+
total = 0
|
|
19
|
+
for i in range(len(self.durations)):
|
|
20
|
+
total += self.durations[i]
|
|
21
|
+
|
|
22
|
+
if self.loop:
|
|
23
|
+
time = time - math.floor(time / total) * total
|
|
24
|
+
for i in range(len(self.durations)):
|
|
25
|
+
if time <= self.durations[i]:
|
|
26
|
+
# linear interpolation between this and next position
|
|
27
|
+
# do it component-wise
|
|
28
|
+
t = time / self.durations[i]
|
|
29
|
+
if i == len(self.positions) - 1:
|
|
30
|
+
return self.positions[i]
|
|
31
|
+
return (
|
|
32
|
+
self.positions[i][0] + t * (self.positions[i + 1][0] - self.positions[i][0]),
|
|
33
|
+
self.positions[i][1] + t * (self.positions[i + 1][1] - self.positions[i][1]),
|
|
34
|
+
self.positions[i][2] + t * (self.positions[i + 1][2] - self.positions[i][2]),
|
|
35
|
+
)
|
|
36
|
+
time -= self.durations[i]
|
|
37
|
+
return self.positions[-1]
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from .types import Position
|
|
2
|
+
from .util import distance
|
|
3
|
+
from jelka import Color
|
|
4
|
+
from jelka.path import Path
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Sphere:
|
|
8
|
+
def __init__(self, center: Position, radius: float, color: Color = Color(255, 192, 203)):
|
|
9
|
+
self.center = center
|
|
10
|
+
self.radius = radius
|
|
11
|
+
self.color: Color = color
|
|
12
|
+
self.path = Path()
|
|
13
|
+
self.path.add_position(center, 0.01)
|
|
14
|
+
|
|
15
|
+
def is_inside(self, pt: Position) -> bool:
|
|
16
|
+
return distance(self.center, pt) <= self.radius
|
|
17
|
+
|
|
18
|
+
def update_pos(self, time: float):
|
|
19
|
+
self.center = self.path.current_position(time)
|
|
20
|
+
# print(self.center)
|
|
21
|
+
return
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Position = tuple[float, float, float]
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
# define adding and subtracing of positions
|
|
5
|
+
def __add__(self, other: Position) -> Position:
|
|
6
|
+
return (self[0] + other[0], self[1] + other[1], self[2] + other[2])
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def __sub__(self, other: Position) -> Position:
|
|
10
|
+
return (self[0] - other[0], self[1] - other[1], self[2] - other[2])
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import math
|
|
2
|
+
from .types import Position
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def length(p: Position) -> float:
|
|
6
|
+
"""Calculates absolute value of a point."""
|
|
7
|
+
return math.sqrt(sum(x**2 for x in p))
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def distance(p1: Position, p2: Position) -> float:
|
|
11
|
+
return length((p1[0] - p2[0], p1[1] - p2[1], p1[2] - p2[2]))
|
|
File without changes
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
from jelka import Jelka, Color
|
|
2
|
+
import math
|
|
3
|
+
from jelka.sphere import Sphere
|
|
4
|
+
from jelka.types import Position
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def callback(jelka: Jelka):
|
|
8
|
+
cols = [Color(255, 0, 0), Color(0, 0, 255), Color(255, 255, 255)]
|
|
9
|
+
height = [1 - 0.005 * (jelka.frame % 210), 1 - 0.005 * ((jelka.frame + 70) % 210), 1 - 0.005 * ((jelka.frame + 140) % 210)]
|
|
10
|
+
|
|
11
|
+
rad = [(1 - height[0]) / 2, (1 - height[1]) / 2, (1 - height[2]) / 2]
|
|
12
|
+
x = [
|
|
13
|
+
0.5 + rad[0] * math.cos(height[0] * 20),
|
|
14
|
+
0.5 + rad[1] * math.cos(height[1] * 20),
|
|
15
|
+
0.5 + rad[2] * math.cos(height[2] * 20),
|
|
16
|
+
]
|
|
17
|
+
y = [
|
|
18
|
+
0.5 + rad[0] * math.sin(height[0] * 20),
|
|
19
|
+
0.5 + rad[1] * math.sin(height[1] * 20),
|
|
20
|
+
0.5 + rad[2] * math.sin(height[2] * 20),
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
sph = [
|
|
24
|
+
Sphere((x[0], y[0], height[0]), 0.2),
|
|
25
|
+
Sphere((x[1], y[1], height[1]), 0.2),
|
|
26
|
+
Sphere((x[2], y[2], height[2]), 0.2),
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
for i in range(len(jelka.lights)):
|
|
30
|
+
pos: Position = jelka.positions_normalized[i]
|
|
31
|
+
for j in range(0, 3):
|
|
32
|
+
if sph[j].is_inside(pos):
|
|
33
|
+
jelka.set_light(i, cols[j])
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def main():
|
|
37
|
+
jelka = Jelka(300, 60)
|
|
38
|
+
jelka.run(callback)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
main()
|