Skip to content

Commit 029a000

Browse files
authored
Add additional math methods (#140)
1 parent 5bceba5 commit 029a000

2 files changed

Lines changed: 136 additions & 2 deletions

File tree

crates/processing_pyo3/mewnala/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@
55
# the internal structure of the native module
66
import sys as _sys
77
from . import mewnala as _native
8+
89
for _name in ("math",):
910
_sub = getattr(_native, _name, None)
1011
if _sub is not None:
1112
_sys.modules[f"{__name__}.{_name}"] = _sub
1213

14+
_color = getattr(_native, "color", None)
15+
if _color is not None:
16+
_sys.modules[f"{__name__}.color"] = _color
17+
18+
from . import math # noqa: E402 (Python submodule, extends native math)
19+
from .math import * # noqa: E402,F401,F403
20+
1321
# global var handling. for wildcard import of our module, we copy into globals, otherwise
14-
# we dispatch to get attr and call the underlying getter method
22+
# we dispatch to get attr and call the underlying getter method
1523

1624
_DYNAMIC_GRAPHICS_ATTRS = (
1725
"width",
@@ -106,7 +114,6 @@ def __getattr__(name):
106114
def __dir__():
107115
return sorted(set(list(globals().keys()) + list(_DYNAMIC)))
108116

109-
110117
__all__ = sorted(
111118
{n for n in dir(_native) if not n.startswith("_")} | set(_DYNAMIC)
112119
)
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
"""Processing math methods and vector/quaternion types."""
2+
import math as _math
3+
from .mewnala import math as _native_math
4+
from math import (
5+
sin, cos, tan,
6+
atan, atan2,
7+
ceil, floor,
8+
degrees, radians,
9+
)
10+
11+
Vec2 = _native_math.Vec2
12+
Vec3 = _native_math.Vec3
13+
Vec4 = _native_math.Vec4
14+
Quat = _native_math.Quat
15+
VecIter = _native_math.PyVecIter
16+
vec2 = _native_math.vec2
17+
vec3 = _native_math.vec3
18+
vec4 = _native_math.vec4
19+
quat = _native_math.quat
20+
21+
_NAN = float("nan")
22+
_INF = float("inf")
23+
24+
25+
def _safe_div(num, den):
26+
if den == 0:
27+
if num == 0:
28+
return _NAN
29+
return _INF if num > 0 else -_INF
30+
return num / den
31+
32+
33+
def sq(x):
34+
return x * x
35+
36+
37+
def pow(base, exponent):
38+
if base < 0 and not float(exponent).is_integer():
39+
return _NAN
40+
try:
41+
return base ** exponent
42+
except (OverflowError, ZeroDivisionError):
43+
return _INF
44+
45+
46+
def sqrt(x):
47+
if x < 0:
48+
return _NAN
49+
return _math.sqrt(x)
50+
51+
52+
def exp(x):
53+
try:
54+
return _math.exp(x)
55+
except OverflowError:
56+
return _INF
57+
58+
59+
def log(x):
60+
if x == 0:
61+
return -_INF
62+
if x < 0:
63+
return _NAN
64+
return _math.log(x)
65+
66+
67+
def asin(x):
68+
if x < -1 or x > 1:
69+
return _NAN
70+
return _math.asin(x)
71+
72+
73+
def acos(x):
74+
if x < -1 or x > 1:
75+
return _NAN
76+
return _math.acos(x)
77+
78+
79+
def round(x):
80+
return _math.floor(x + 0.5)
81+
82+
83+
def constrain(value, low, high):
84+
if value < low:
85+
return low
86+
if value > high:
87+
return high
88+
return value
89+
90+
91+
def lerp(start, stop, amt):
92+
return start + (stop - start) * amt
93+
94+
95+
def norm(value, start, stop):
96+
return _safe_div(value - start, stop - start)
97+
98+
99+
def remap(value, start1, stop1, start2, stop2, within_bounds=False):
100+
mapped = start2 + (stop2 - start2) * _safe_div(value - start1, stop1 - start1)
101+
if not within_bounds:
102+
return mapped
103+
if start2 < stop2:
104+
return constrain(mapped, start2, stop2)
105+
return constrain(mapped, stop2, start2)
106+
107+
108+
def mag(*args):
109+
if len(args) == 2:
110+
a, b = args
111+
return sqrt(a * a + b * b)
112+
if len(args) == 3:
113+
a, b, c = args
114+
return sqrt(a * a + b * b + c * c)
115+
raise TypeError(f"mag() takes 2 or 3 arguments ({len(args)} given)")
116+
117+
118+
def dist(*args):
119+
if len(args) == 4:
120+
x1, y1, x2, y2 = args
121+
dx, dy = x2 - x1, y2 - y1
122+
return sqrt(dx * dx + dy * dy)
123+
if len(args) == 6:
124+
x1, y1, z1, x2, y2, z2 = args
125+
dx, dy, dz = x2 - x1, y2 - y1, z2 - z1
126+
return sqrt(dx * dx + dy * dy + dz * dz)
127+
raise TypeError(f"dist() takes 4 or 6 arguments ({len(args)} given)")

0 commit comments

Comments
 (0)