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
- 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: List[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
- modelviewprojection.mathutils.compose_intermediate_fns(functions: List[InvertibleFunction[T]], relative_basis: bool = False) Iterable[InvertibleFunction[T]] [source]¶
Like compose, but returns a list of all of the partial compositions
Example
>>> from modelviewprojection.mathutils import compose_intermediate_fns, InvertibleFunction, uniform_scale, translate >>> from modelviewprojection.mathutils1d import Vector1D >>> from pytest import approx >>> m = 5.0 >>> b = 2.0 >>> # natural basis >>> fns: typing.List[InvertibleFunction[Vector1D]] = compose_intermediate_fns( ... [translate(Vector1D(b)), uniform_scale(m)] ... ) >>> len(fns) 2 >>> fns[0](Vector1D(1)) Vector1D(x=5.0) >>> fns[1](Vector1D(1)) Vector1D(x=7.0) >>> # relative basis >>> fns: typing.List[InvertibleFunction[Vector1D]] = compose_intermediate_fns( ... [translate(Vector1D(b)), uniform_scale(m)], relative_basis=True ... ) >>> len(fns) 2 >>> fns[0](Vector1D(1)) Vector1D(x=3.0) >>> fns[1](Vector1D(1)) Vector1D(x=7.0)
- modelviewprojection.mathutils.compose_intermediate_fns_and_fn(functions: List[InvertibleFunction[T]], relative_basis: bool = False) Iterable[InvertibleFunction[T]] [source]¶
Like compose, but returns a list of all of the partial compositions
Example
>>> from modelviewprojection.mathutils import compose_intermediate_fns_and_fn, InvertibleFunction, uniform_scale, translate >>> from modelviewprojection.mathutils1d import Vector1D >>> from pytest import approx >>> m = 5.0 >>> b = 2.0 >>> # natural basis >>> for aggregate_fn, current_fn in compose_intermediate_fns_and_fn( ... [translate(Vector1D(b)), uniform_scale(m)]): ... print("agg " + str(aggregate_fn(Vector1D(1.0)))) ... print("current " + str(current_fn(Vector1D(1.0)))) ... agg Vector1D(x=5.0) current Vector1D(x=5.0) agg Vector1D(x=7.0) current Vector1D(x=3.0) >>> # relative basis >>> for aggregate_fn, current_fn in compose_intermediate_fns_and_fn( ... [translate(Vector1D(b)), uniform_scale(m)], relative_basis=True): ... print("agg " + str(aggregate_fn(Vector1D(1.0)))) ... print("current " + str(current_fn(Vector1D(1.0)))) ... agg Vector1D(x=3.0) current Vector1D(x=3.0) agg Vector1D(x=7.0) current Vector1D(x=5.0)
- modelviewprojection.mathutils.translate(b: T) InvertibleFunction[T] [source]¶
- modelviewprojection.mathutils.uniform_scale(m: float) InvertibleFunction[T] [source]¶
Math Utils 1D¶
- class modelviewprojection.mathutils1d.Vector1D(x: float)[source]¶
Bases:
Vector
- x: float¶
The value of the 1D mu.Vector
- __add__(rhs: Self) Self [source]¶
Add together two Vector1Ds.
Let \(\vec{a} = \begin{pmatrix} a_x \end{pmatrix}\) and \(\vec{b} = \begin{pmatrix} b_x \end{pmatrix}\):
\[\vec{a} + \vec{b} = \begin{pmatrix} a_x + b_x \end{pmatrix}\]- Parameters:
rhs (Vector1D) – The vector on the right hand side of the addition symbol
- Returns:
- The Vector1D that represents the additon of the two
input Vector1Ds
- Return type:
- Raises:
Nothing –
Example
>>> from modelviewprojection.mathutils1d import Vector1D >>> a = Vector1D(x=2.0) >>> b = Vector1D(x=5.0) >>> a + b Vector1D(x=7.0)
- __mul__(scalar: float) Self [source]¶
Multiply the Vector1D by a scalar number
Let \(\vec{a} = \begin{pmatrix} a_x \end{pmatrix}\) and constant scalar \(s\):
\[s*\vec{a} = \begin{pmatrix} s*a_x \end{pmatrix}\]- Parameters:
rhs (Vector1D) – The scalar to be multiplied to the mu.Vector’s component subtraction symbol
- Returns:
The Vector1D that represents scalar times the amount of the input mu.Vector1d
- Return type:
- Raises:
Nothing –
Example
>>> from modelviewprojection.mathutils1d import Vector1D >>> a = Vector1D(x=2.0) >>> a * 4 Vector1D(x=8.0)
Math Utils 2D¶
- class modelviewprojection.mathutils2d.Vector2D(x: float, y: float)[source]¶
Bases:
Vector1D
- y: float¶
The y-component of the 2D Vector
- __add__(rhs: Self) Self [source]¶
Add together two Vector2Ds.
Let \(\vec{a} = \begin{pmatrix} a_x \\ a_y \end{pmatrix}\) and \(\vec{b} = \begin{pmatrix} b_x \\ b_y \end{pmatrix}\):
\[\begin{split}\vec{a} + \vec{b} = \begin{pmatrix} a_x + b_x \\ a_y + b_y \end{pmatrix}\end{split}\]- 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)
- __mul__(scalar: float) Self [source]¶
Multiply the Vector2D by a scalar number
Let \(\vec{a} = \begin{pmatrix} a_x \\ a_y \end{pmatrix}\) and constant scalar \(s\):
\[\begin{split}s*\vec{a} = \begin{pmatrix} s*a_x \\ s*a_y \end{pmatrix}\end{split}\]- Parameters:
rhs (Vector2D) – The scalar to be multiplied to the Vector’s component subtraction symbol
- Returns:
The Vector2D that represents scalar times the amount of the input Vector2D
- Return type:
- Raises:
Nothing –
Example
>>> from modelviewprojection.mathutils2d import Vector2D >>> a = Vector2D(x=2.0, y=3.0) >>> a * 4 Vector2D(x=8.0, y=12.0)
- modelviewprojection.mathutils2d.scale(m_x: float, m_y: float) InvertibleFunction[Vector2D] [source]¶
- modelviewprojection.mathutils2d.rotate_90_degrees() InvertibleFunction[Vector2D] [source]¶
- modelviewprojection.mathutils2d.rotate(angle_in_radians: float) Callable[[Vector2D], Vector2D] [source]¶
- modelviewprojection.mathutils2d.rotate_around(angle_in_radians: float, center: Vector2D) InvertibleFunction[Vector2D] [source]¶
Math Utils 3D¶
- class modelviewprojection.mathutils3d.Vector3D(x: float, y: float, z: float)[source]¶
Bases:
Vector2D
- z: float¶
The z-component of the 3D Vector
- __add__(rhs: Self) Self [source]¶
Add together two Vector3Ds.
Let \(\vec{a} = \begin{pmatrix} a_x \\ a_y \\ a_z \end{pmatrix}\) and \(\vec{b} = \begin{pmatrix} b_x \\ b_y \\ b_z \end{pmatrix}\):
\[\begin{split}\vec{a} + \vec{b} = \begin{pmatrix} a_x + b_x \\ a_y + b_y \\ a_z + b_z \end{pmatrix}\end{split}\]- Parameters:
rhs (Vector3D) – The vector on the right hand side of the addition symbol
- Returns:
- The Vector3D that represents the additon of the two
input Vector3Ds
- Return type:
- Raises:
Nothing –
Example
>>> from modelviewprojection.mathutils3d import Vector3D >>> a = Vector3D(x=2.0, y=3.0, z=1.0) >>> b = Vector3D(x=5.0, y=6.0, z=9.0) >>> a + b Vector3D(x=7.0, y=9.0, z=10.0)
- __mul__(scalar: float) Self [source]¶
Multiply the Vector3D by a scalar number
Let \(\vec{a} = \begin{pmatrix} a_x \\ a_y \\ a_z \end{pmatrix}\) and constant scalar \(s\):
\[\begin{split}s*\vec{a} = \begin{pmatrix} s*a_x \\ s*a_y \\ s*a_z \end{pmatrix}\end{split}\]- Parameters:
rhs (Vector3D) – The scalar to be multiplied to the Vector’s component subtraction symbol
- Returns:
The Vector3D that represents scalar times the amount of the input Vector3D
- Return type:
- Raises:
Nothing –
Example
>>> from modelviewprojection.mathutils3d import Vector3D >>> a = Vector3D(x=2.0, y=3.0, z=4.0) >>> a * 4 Vector3D(x=8.0, y=12.0, z=16.0)
- modelviewprojection.mathutils3d.scale(m_x: float, m_y: float, m_z: float) InvertibleFunction[Vector3D] [source]¶
- modelviewprojection.mathutils3d.rotate_x(angle_in_radians: float) InvertibleFunction[Vector3D] [source]¶
- modelviewprojection.mathutils3d.rotate_y(angle_in_radians: float) InvertibleFunction[Vector3D] [source]¶
- modelviewprojection.mathutils3d.rotate_z(angle_in_radians: float) InvertibleFunction[Vector3D] [source]¶
- modelviewprojection.mathutils3d.ortho(left: float, right: float, bottom: float, top: float, near: float, far: float) InvertibleFunction[Vector3D] [source]¶
- modelviewprojection.mathutils3d.perspective(field_of_view: float, aspect_ratio: float, near_z: float, far_z: float) InvertibleFunction[Vector3D] [source]¶
- modelviewprojection.mathutils3d.cs_to_ndc_space_fn(vector: Vector3D) InvertibleFunction[Vector3D] [source]¶
- class modelviewprojection.mathutils3d.FunctionStack(stack: List[modelviewprojection.mathutils.InvertibleFunction[modelviewprojection.mathutils3d.Vector3D]] = <factory>)[source]¶
Bases:
object
- stack: List[InvertibleFunction[Vector3D]]¶
- modelspace_to_ndc_fn() InvertibleFunction[Vector3D] [source]¶