numerics module
Warning
The FloatX/IntX types here are Python wrappers around numpy arrays. They are glacially slow and should only be used for convenience inside of Python scope and well outside of any high-traffic loops.
The numerics module offers shading-language-like syntax for float and int vector data types, as well as a few utility functions. These types are an extension of numpy arrays, and you can use them as such. Matrices are not natively supported.
from tinycio.numerics import *
import numpy as np
import torch
# Several possible inputs
Float4(4,3,2,1) # Float4([4., 3., 2., 1.])
Float4(1.23) # Float4([1.23, 1.23, 1.23, 1.23])
Float4([4,3,2,1]) # Float4([4., 3., 2., 1.])
Float4((4,3,2,1)) # Float4([4., 3., 2., 1.])
Float4(np.array([3,2,1,0])) # Float4([3., 2., 1., 0.])
Float4(torch.rand(4)) # Float4([0.22407186, 0.26193792, 0.89055574, 0.57386285])
Float4(torch.rand(4,1,1)) # Float4([0.99545109, 0.46160549, 0.78145427, 0.02302521])
# Swizzling
foo = Float3(1,2,3)
foo.y # 2.0 (float)
foo.rg # Float2([1., 2.])
foo.bgr # Float3([3., 2., 1.])
foo.xxyx # Float4([1., 1., 2., 1.])
# Utility functions
bar = Float3.y_axis() # Float3([0., 1., 0.])
bar.list() # [0., 1., 0.]
bar.tuple() # (0., 1., 0.)
lerp(foo.bbb, bar, 0.5) # Float3([1.5, 2. , 1.5])
saturate(Float2(-2,5)) # Float2([0., 1.])
sign(Float2(-2,5)) # Float2([-1., 1.])
Float4(1) == Float4.one() # Float4([ True, True, True, True])
|
Float2 type using numpy.ndarray. |
|
Float3 type using numpy.ndarray. |
|
Float4 type using numpy.ndarray. |
|
Int2 type using numpy.ndarray. |
|
Int3 type using numpy.ndarray. |
|
Int4 type using numpy.ndarray. |
|
Linearly interpolate two values. |
|
Saturate x, such that x is clamped to range [0, 1]. |
|
Sign of x as 1, -1 or 0. |
|
Normalize vector v to a unit vector. |
|
Reflect vector l over n. |
- tinycio.numerics.lerp(a, b, w)[source]
Linearly interpolate two values.
- Parameters:
a (float | list | numpy.ndarray | torch.Tensor) – first value or list to interpolate
b (float | list | numpy.ndarray | torch.Tensor) – second value or list to interpolate
w (float | list | numpy.ndarray | torch.Tensor) – 0-1 weight ratio of first to second value
- Returns:
linearly interpolated value(s)
- Return type:
float | list | numpy.ndarray | torch.Tensor
- tinycio.numerics.saturate(x)[source]
Saturate x, such that x is clamped to range [0, 1].
- Parameters:
x (float | list | numpy.ndarray | torch.Tensor) – input value
- Returns:
saturated input value(s)
- Return type:
float | list | numpy.ndarray | torch.Tensor
- tinycio.numerics.sign(x)[source]
Sign of x as 1, -1 or 0. Returns:
0 if/where x == 0
1 if/where x > 0
-1 if/where x < 0
- Parameters:
x (float | list | numpy.ndarray | torch.Tensor) – input value
- Returns:
sign(s) of input values
- Return type:
float | list | numpy.ndarray | torch.Tensor