API Reference¶
Math Utils¶
- class modelviewprojection.mathutils.InvertibleFunction(func: Callable[[T], T], inverse: Callable[[T], T])[source]¶
Bases:
Generic
[T
]Class that wraps a function and its inverse function. The function takes type T as it’s argument and it’s evaluation results in a value of type T.
- func: Callable[[T], T]¶
The wrapped function
- inverse: Callable[[T], T]¶
The inverse of the wrapped function
- __call__(x: T) T [source]¶
Execute a function with the given value.
- Parameters:
func (Callable[[T], T]) – A function that takes a value of type T and returns a value of the same type T.
value (T) – The input value to pass to the function
- Returns:
- The result of calling func(value). Will be the same type as the
input value.
- Return type:
T
- Raises:
Nothing –
Example
>>> from modelviewprojection.mathutils import InvertibleFunction >>> from modelviewprojection.mathutils import inverse >>> def f(x): ... return 2 + x ... >>> def f_inv(x): ... return x - 2 ... >>> foo = InvertibleFunction(func=f, inverse=f_inv) >>> foo InvertibleFunction(func=<function f at 0x...>, inverse=<function f_inv at 0x...>) >>> foo(5) 7 >>> inverse(foo) InvertibleFunction(func=<function f_inv at 0x...>, inverse=<function f at 0x...>) >>> inverse(foo)(foo(5)) 5
- __init__(func: Callable[[T], T], inverse: Callable[[T], T]) None ¶
- modelviewprojection.mathutils.inverse(f: InvertibleFunction[T]) InvertibleFunction[T] [source]¶
Get the inverse of the InvertibleFunction
- Parameters:
f – InvertibleFunction[T]: A function with it’s associated inverse function.
- Returns:
- The Inverse of the function
function.
- Return type:
- Raises:
Nothing –
Example
>>> from modelviewprojection.mathutils import InvertibleFunction >>> from modelviewprojection.mathutils import inverse >>> def f(x): ... return 2 + x ... >>> def f_inv(x): ... return x - 2 ... >>> foo = InvertibleFunction(func=f, inverse=f_inv) >>> foo InvertibleFunction(func=<function f at 0x...>, inverse=<function f_inv at 0x...>) >>> foo(5) 7 >>> inverse(foo) InvertibleFunction(func=<function f_inv at 0x...>, inverse=<function f at 0x...>) >>> inverse(foo)(foo(5)) 5
- modelviewprojection.mathutils.compose(*functions: InvertibleFunction[T]) InvertibleFunction[T] [source]¶
Compose a sequence of functions.
If two functions are passed as arguments, named \(f\) and \(g\):
\((f \circ g)(x) = f(g(x))\).
If \(n\) functions are passed as arguments, \(f_1...f_n\):
\((f_1 \circ (f_2 \circ (... f_n )(x) = f_1(f_2...(f_n(x))\).
- Parameters:
*functions (InvertibleFunction[T]) – Variable number of InvertibleFunctions to compose. At least on value must be provided.
- Returns:
- One function that is the aggregate function of the argument
functions composed.
- Return type:
T
- Raises:
Nothing –
Example
>>> from modelviewprojection.mathutils import compose >>> compose(lambda x: 5)(1) 5 >>> compose(lambda x: 2*x)(1) 2 >>> compose(lambda x: x+4, lambda x: 2*x)(1) 6 >>> compose(lambda x: x+ 10, lambda x: x+4, lambda x: 2*x)(1) 16
Math Utils 2D¶
- class modelviewprojection.mathutils2d.Vector2D(x: 'float', y: 'float')[source]¶
Bases:
object
- x: float¶
The x-component of the 2D Vector
- y: float¶
The y-component of the 2D Vector
- __add__(rhs: Vector2D) Vector2D [source]¶
Add together two Vector2Ds.
Let \(\vec{a} = (a_x, a_y)\) and \(\vec{b} = (b_x, b_y)\):
\[\vec{a} + \vec{b} = (a_x + b_x, a_y + b_y)\]- Parameters:
rhs (Vector2D) – The vector on the right hand side of the addition symbol
- Returns:
- The Vector2D that represents the additon of the two
input Vector2Ds
- Return type:
- Raises:
Nothing –
Example
>>> from modelviewprojection.mathutils2d import Vector2D >>> a = Vector2D(x=2.0, y=3.0) >>> b = Vector2D(x=5.0, y=6.0) >>> a + b Vector2D(x=7.0, y=9.0)
- __sub__(rhs: Vector2D) Vector2D [source]¶
Subtract the right hand side Vector2D from the left hand side Vector2D.
Let \(\vec{a} = (a_x, a_y)\) and \(\vec{b} = (b_x, b_y)\):
\[\vec{a} - \vec{b} = (a_x - b_x, a_y - b_y)\]- Parameters:
rhs (Vector2D) – The vector on the right hand side of the subtraction symbol
- Returns:
- The Vector2D that represents the subtraction of the
right hand side Vector2D from the left hand side Vector2D
- Return type:
- Raises:
Nothing –
Example
>>> from modelviewprojection.mathutils2d import Vector2D >>> a = Vector2D(x=2.0, y=3.0) >>> b = Vector2D(x=5.0, y=2.0) >>> a - b Vector2D(x=-3.0, y=1.0)
- __init__(x: float, y: float) None ¶
- modelviewprojection.mathutils2d.translate(translate_amount: Vector2D) InvertibleFunction [source]¶
- modelviewprojection.mathutils2d.uniform_scale(scalar: float) InvertibleFunction [source]¶
- modelviewprojection.mathutils2d.scale(scale_x: float, scale_y: float) InvertibleFunction [source]¶
- modelviewprojection.mathutils2d.rotate_90_degrees() InvertibleFunction[Vector2D] [source]¶
- modelviewprojection.mathutils2d.rotate(angle_in_radians: float) InvertibleFunction [source]¶
- modelviewprojection.mathutils2d.rotate_around(angle_in_radians: float, center: Vector2D) InvertibleFunction [source]¶
Math Utils 3D¶
- class modelviewprojection.mathutils3d.Vector3D(x: 'float', y: 'float', z: 'float')[source]¶
Bases:
object
- x: float¶
The x-component of the 3D Vector
- y: float¶
The y-component of the 3D Vector
- z: float¶
The z-component of the 3D Vector
- __init__(x: float, y: float, z: float) None ¶
- modelviewprojection.mathutils3d.translate(translate_amount: Vector3D) InvertibleFunction [source]¶
- modelviewprojection.mathutils3d.uniform_scale(scalar: float) InvertibleFunction [source]¶
- modelviewprojection.mathutils3d.scale(scale_x: float, scale_y: float, scale_z: float) Vector3D [source]¶
- modelviewprojection.mathutils3d.ortho(left: float, right: float, bottom: float, top: float, near: float, far: float) Vector3D [source]¶
- modelviewprojection.mathutils3d.perspective(field_of_view: float, aspect_ratio: float, near_z: float, far_z: float) Vector3D [source]¶
- class modelviewprojection.mathutils3d.FunctionStack(stack: 'List[InvertibleFunction[Vector3D]]' = <factory>)[source]¶
Bases:
object
- stack: List[InvertibleFunction[Vector3D]]¶
- modelspace_to_ndc_fn() InvertibleFunction[Vector3D] [source]¶
- __init__(stack: ~typing.List[~modelviewprojection.mathutils.InvertibleFunction[~modelviewprojection.mathutils3d.Vector3D]] = <factory>) None ¶