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: 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 1D¶
- class modelviewprojection.mathutils1d.Vector1D(x: 'float')[source]¶
Bases:
object
- x: float¶
The value of the 1D Vector
- __add__(rhs: Vector1D) Vector1D [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)
- __sub__(rhs: Vector1D) Vector1D [source]¶
Subtract the right hand side Vector1D from the left hand side Vector1D.
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 subtraction symbol
- Returns:
- The Vector1D that represents the subtraction of the
right hand side Vector1D from the left hand side Vector1D
- Return type:
- Raises:
Nothing –
Example
>>> from modelviewprojection.mathutils1d import Vector1D >>> a = Vector1D(x=2.0) >>> b = Vector1D(x=5.0) >>> a - b Vector1D(x=-3.0)
- __mul__(scalar: float) Vector1D [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 Vector’s component subtraction symbol
- Returns:
The Vector1D that represents scalar times the amount of the input Vector1d
- Return type:
- Raises:
Nothing –
Example
>>> from modelviewprojection.mathutils1d import Vector1D >>> a = Vector1D(x=2.0) >>> a * 4 Vector1D(x=8.0)
- __neg__() Vector1D [source]¶
Multiply the Vector1D by -1
Let \(\vec{a} = (a_x)\) and constant \(-1\):
\[-1 * \vec{a}\]- Returns:
The Vector1D with the opposite orientation
- Return type:
- Raises:
Nothing –
Example
>>> from modelviewprojection.mathutils1d import Vector1D >>> a = Vector1D(x=2.0) >>> -a Vector1D(x=-2.0)
- modelviewprojection.mathutils1d.translate(b: Vector1D) InvertibleFunction[Vector1D] [source]¶
TODO
- Parameters:
b (float) – The amount to translate a not-yet-bound vector
- Returns:
- The Vector1D that represents the additon of the two
input Vector1Ds
- Return type:
- Raises:
Nothing –
- modelviewprojection.mathutils1d.uniform_scale(m: float) InvertibleFunction[Vector1D] [source]¶
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} = \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)
- __sub__(rhs: Vector2D) Vector2D [source]¶
Subtract the right hand side Vector2D from the left hand side Vector2D.
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} = \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 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)
- __mul__(scalar: float) Vector2D [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)
- __neg__() Vector2D [source]¶
Multiply the Vector2D by -1
Let \(\vec{a} = \begin{pmatrix} a_x \\ a_y \end{pmatrix}\) and constant \(-1\):
\[-1 * \vec{a}\]- Returns:
The Vector2D with the opposite orientation
- Return type:
- Raises:
Nothing –
Example
>>> from modelviewprojection.mathutils2d import Vector2D >>> a = Vector2D(x=2.0, y=3.0) >>> -a Vector2D(x=-2.0, y=-3.0)
- modelviewprojection.mathutils2d.translate(b: Vector2D) InvertibleFunction[Vector2D] [source]¶
- modelviewprojection.mathutils2d.uniform_scale(m: float) InvertibleFunction[Vector2D] [source]¶
- 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:
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
- __add__(rhs: Vector3D) Vector3D [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)
- __sub__(rhs: Vector3D) Vector3D [source]¶
Subtract the right hand side Vector3D from the left hand side Vector3D.
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} = \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 subtraction symbol
- Returns:
- The Vector3D that represents the subtraction of the
right hand side Vector3D from the left hand side Vector3D
- Return type:
- Raises:
Nothing –
Example
>>> from modelviewprojection.mathutils3d import Vector3D >>> a = Vector3D(x=2.0, y=3.0, z=10.0) >>> b = Vector3D(x=5.0, y=2.0, z=1.0) >>> a - b Vector3D(x=-3.0, y=1.0, z=9.0)
- __mul__(scalar: float) Vector3D [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)
- __neg__() Vector3D [source]¶
Multiply the Vector3D by -1
Let \(\vec{a} = \begin{pmatrix} a_x \\ a_y \\ a_z \end{pmatrix}\) and constant \(-1\):
\[-1 * \vec{a}\]- Returns:
The Vector3D with the opposite orientation
- Return type:
- Raises:
Nothing –
Example
>>> from modelviewprojection.mathutils3d import Vector3D >>> a = Vector3D(x=2.0, y=3.0, z=4.0) >>> -a Vector3D(x=-2.0, y=-3.0, z=-4.0)
- modelviewprojection.mathutils3d.translate(b: Vector3D) 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.uniform_scale(m: float) InvertibleFunction[Vector3D] [source]¶
- modelviewprojection.mathutils3d.scale(m_x: float, m_y: float, m_z: 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[InvertibleFunction[Vector3D]]' = <factory>)[source]¶
Bases:
object
- stack: List[InvertibleFunction[Vector3D]]¶
- modelspace_to_ndc_fn() InvertibleFunction[Vector3D] [source]¶