API Reference¶
Math Utils¶
- class modelviewprojection.mathutils.Vector[source]¶
Bases:
object- __add__(rhs: Self) Self[source]¶
Add together two Vectors, component-wise.
- Parameters:
rhs (Vector) – The vector on the right hand side of the addition symbol
- Returns:
The Vector that represents the additon of the two input Vectors
- Return type:
Example
>>> from modelviewprojection.mathutils import Vector1D >>> a = Vector1D(x=2) >>> b = Vector1D(x=5) >>> a + b Vector1D(x=7) >>> from modelviewprojection.mathutils import Vector2D >>> a = Vector2D(x=2, y=3) >>> b = Vector2D(x=5, y=6) >>> a + b Vector2D(x=7, y=9) >>> from modelviewprojection.mathutils import Vector3D >>> a = Vector3D(x=2, y=3, z=1) >>> b = Vector3D(x=5, y=6, z=10) >>> a + b Vector3D(x=7, y=9, z=11)
- __mul__(scalar: float) Self[source]¶
Multiply the Vector by a scalar number, component-wise
- Parameters:
rhs (Vector) – The scalar to be multiplied to the Vector’s component subtraction symbol
- Returns:
The Vector that represents scalar times the amount of the input Vector
- Return type:
Example
>>> from modelviewprojection.mathutils import Vector1D >>> a = Vector1D(x=2) >>> a * 4 Vector1D(x=8) >>> from modelviewprojection.mathutils import Vector2D >>> a = Vector2D(x=2, y=3) >>> a * 4 Vector2D(x=8, y=12) >>> from modelviewprojection.mathutils import Vector3D >>> a = Vector3D(x=2, y=3, z=5) >>> a * 4 Vector3D(x=8, y=12, z=20)
- __neg__() Self[source]¶
Let \(\vec{a}\) and constant \(-1\):
\[-1 * \vec{a}\]Example
>>> from modelviewprojection.mathutils import Vector1D >>> a = Vector1D(x=2) >>> -a Vector1D(x=-2)
- class modelviewprojection.mathutils.Vector1D(x: float)[source]¶
Bases:
Vector- x: float¶
The value of the 1D Vector
- class modelviewprojection.mathutils.InvertibleFunction(func: Callable[[Vector], Vector], inverse: Callable[[Vector], Vector], latex_repr: str, latex_repr_inv: str)[source]¶
Bases:
objectClass 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.
- latex_repr: str¶
The LaTeX representation of the function
- latex_repr_inv: str¶
The LaTeX representation of the inverse function
- __call__(x: Vector) Vector[source]¶
Execute a function with the given value.
- Parameters:
- Returns:
The result of calling func(value).
Will be the same type as the input value.
- Return type:
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, latex_repr="", latex_repr_inv="") >>> foo InvertibleFunction(func=<...>, inverse=<...>, latex_repr=..., latex_repr_inv=...) >>> foo(5) 7 >>> inverse(foo) InvertibleFunction(func=<...>, inverse=<...>, latex_repr=..., latex_repr_inv=...) >>> inverse(foo)(foo(5)) 5
- __matmul__(f2: InvertibleFunction) InvertibleFunction[source]¶
Override @ for function composition. This is abusing the @ symbol, which is normally for matrix multiplication.
- Parameters:
f2 (mathutils.InvertibleFunction) – A function that self is composed with and returns a value of the same type Vector.
- Returns:
The composed function.
- Return type:
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, latex_repr="", latex_repr_inv="") >>> foo(5) 7 >>> (foo @ foo)(5) 9 >>> inverse(foo @ foo)(5) 1 >>> (foo @ inverse(foo))(5) 5
- __rmatmul__(f2: InvertibleFunction) InvertibleFunction[source]¶
- modelviewprojection.mathutils.identity() InvertibleFunction[source]¶
- modelviewprojection.mathutils.inverse(f: InvertibleFunction) InvertibleFunction[source]¶
Get the inverse of the InvertibleFunction
- Parameters:
f – InvertibleFunction: A function with it’s associated inverse function.
- Returns:
The Inverse of the function.
- Return type:
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, latex_repr="", latex_repr_inv="") >>> foo InvertibleFunction(func=<...>, inverse=<...>, latex_repr=..., latex_repr_inv=...) >>> foo(5) 7 >>> inverse(foo) InvertibleFunction(func=<...>, inverse=<...>, latex_repr=..., latex_repr_inv=...) >>> inverse(foo)(foo(5)) 5
- modelviewprojection.mathutils.compose(functions: list[InvertibleFunction]) InvertibleFunction[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 (list[InvertibleFunction]) – Variable number of InvertibleFunctions to compose. At least on value must be provided.
- Returns:
One function that is the aggregate function.
- Return type:
Example
>>> from modelviewprojection.mathutils import compose >>> from modelviewprojection.mathutils import translate as T >>> from modelviewprojection.mathutils import uniform_scale as S >>> from modelviewprojection.mathutils import Vector2D >>> fn = compose([S(2), T(Vector2D(3, 4))]) >>> fn(Vector2D(1,1)) Vector2D(x=8, y=10)
- modelviewprojection.mathutils.compose_intermediate_fns(functions: list[InvertibleFunction], relative_basis: bool = False) Iterable[InvertibleFunction][source]¶
Like compose, but returns a list of all of the partial compositions
Example
>>> from modelviewprojection.mathutils import compose_intermediate_fns >>> from modelviewprojection.mathutils import InvertibleFunction >>> from modelviewprojection.mathutils import uniform_scale >>> from modelviewprojection.mathutils import translate >>> from modelviewprojection.mathutils import Vector1D >>> from pytest import approx >>> m = 5 >>> b = 2 >>> # natural basis >>> fns: list[InvertibleFunction] = compose_intermediate_fns( ... [translate(Vector1D(b)), uniform_scale(m)] ... ) >>> len(fns) 3 >>> fns[0](Vector1D(1)) Vector1D(x=1) >>> fns[1](Vector1D(1)) Vector1D(x=5) >>> fns[2](Vector1D(1)) Vector1D(x=7) >>> # relative basis >>> fns: list[InvertibleFunction] = compose_intermediate_fns( ... [translate(Vector1D(b)), uniform_scale(m)], relative_basis=True ... ) >>> len(fns) 3 >>> fns[0](Vector1D(1)) Vector1D(x=1) >>> fns[1](Vector1D(1)) Vector1D(x=3) >>> fns[2](Vector1D(1)) Vector1D(x=7)
- modelviewprojection.mathutils.compose_intermediate_fns_and_fn(functions: list[InvertibleFunction], relative_basis: bool = False) list[tuple[InvertibleFunction, InvertibleFunction]][source]¶
Like compose, but returns a list of all of the partial compositions
Example
>>> from modelviewprojection.mathutils import compose_intermediate_fns_and_fn >>> from modelviewprojection.mathutils import InvertibleFunction >>> from modelviewprojection.mathutils import uniform_scale >>> from modelviewprojection.mathutils import translate >>> from modelviewprojection.mathutils import Vector1D >>> from pytest import approx >>> m = 5 >>> b = 2 >>> # 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)))) ... print("current " + str(current_fn(Vector1D(1)))) ... agg Vector1D(x=1) current Vector1D(x=5) agg Vector1D(x=5) current Vector1D(x=3) agg Vector1D(x=7) current Vector1D(x=1) >>> # 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)))) ... print("current " + str(current_fn(Vector1D(1)))) ... agg Vector1D(x=1) current Vector1D(x=1) agg Vector1D(x=3) current Vector1D(x=3) agg Vector1D(x=7) current Vector1D(x=5)
- modelviewprojection.mathutils.translate(b: Vector) InvertibleFunction[source]¶
- modelviewprojection.mathutils.uniform_scale(m: float) InvertibleFunction[source]¶
- class modelviewprojection.mathutils.Vector2D(x: float, y: float)[source]¶
Bases:
Vector1D- y: float¶
The y-component of the 2D Vector
- modelviewprojection.mathutils.scale_non_uniform_2d(m_x: float, m_y: float) InvertibleFunction[source]¶
- modelviewprojection.mathutils.rotate_90_degrees() InvertibleFunction[source]¶
- modelviewprojection.mathutils.rotate(angle_in_radians: float) InvertibleFunction[source]¶
- modelviewprojection.mathutils.rotate_around(angle_in_radians: float, center: Vector2D) InvertibleFunction[source]¶
- class modelviewprojection.mathutils.Vector3D(x: float, y: float, z: float)[source]¶
Bases:
Vector2D- z: float¶
The z-component of the 3D Vector
- modelviewprojection.mathutils.scale_non_uniform_3d(m_x: float, m_y: float, m_z: float) InvertibleFunction[source]¶
- modelviewprojection.mathutils.rotate_x(angle_in_radians: float) InvertibleFunction[source]¶
- modelviewprojection.mathutils.rotate_y(angle_in_radians: float) InvertibleFunction[source]¶
- modelviewprojection.mathutils.rotate_z(angle_in_radians: float) InvertibleFunction[source]¶
- modelviewprojection.mathutils.ortho(left: float, right: float, bottom: float, top: float, near: float, far: float) InvertibleFunction[source]¶
- modelviewprojection.mathutils.perspective(field_of_view: float, aspect_ratio: float, near_z: float, far_z: float) InvertibleFunction[source]¶
- modelviewprojection.mathutils.cs_to_ndc_space_fn(vector: Vector3D) InvertibleFunction[source]¶
- class modelviewprojection.mathutils.FunctionStack(stack: list[modelviewprojection.mathutils.InvertibleFunction] = <factory>)[source]¶
Bases:
object- stack: list[InvertibleFunction]¶
- push(o: InvertibleFunction)[source]¶
- modelspace_to_ndc_fn() InvertibleFunction[source]¶