Skip to content

Commit

Permalink
Handle mypy errors in utils/space_ops.py
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikmidtiby committed Nov 6, 2024
1 parent 5ca896f commit 47a0661
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
37 changes: 23 additions & 14 deletions manim/utils/space_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import itertools as it
from collections.abc import Sequence
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Callable

import numpy as np
from mapbox_earcut import triangulate_float32 as earcut
Expand Down Expand Up @@ -59,7 +59,8 @@


def norm_squared(v: float) -> float:
return np.dot(v, v)
val: float = np.dot(v, v)
return val


def cross(v1: Vector3D, v2: Vector3D) -> Vector3D:
Expand Down Expand Up @@ -200,7 +201,7 @@ def rotate_vector(
return rotation_matrix(angle, axis) @ vector


def thick_diagonal(dim: int, thickness=2) -> np.ndarray:
def thick_diagonal(dim: int, thickness: float = 2) -> np.ndarray:
row_indices = np.arange(dim).repeat(dim).reshape((dim, dim))
col_indices = np.transpose(row_indices)
return (np.abs(row_indices - col_indices) < thickness).astype("uint8")
Expand Down Expand Up @@ -318,8 +319,10 @@ def angle_of_vector(vector: Sequence[float] | np.ndarray) -> float:
c_vec = np.empty(vector.shape[1], dtype=np.complex128)
c_vec.real = vector[0]
c_vec.imag = vector[1]
return np.angle(c_vec)
return np.angle(complex(*vector[:2]))
val1: float = np.angle(c_vec)
return val1
val: float = np.angle(complex(*vector[:2]))
return val


def angle_between_vectors(v1: np.ndarray, v2: np.ndarray) -> float:
Expand All @@ -338,13 +341,17 @@ def angle_between_vectors(v1: np.ndarray, v2: np.ndarray) -> float:
float
The angle between the vectors.
"""
return 2 * np.arctan2(
val: float = 2 * np.arctan2(
np.linalg.norm(normalize(v1) - normalize(v2)),
np.linalg.norm(normalize(v1) + normalize(v2)),
)

return val

def normalize(vect: np.ndarray | tuple[float], fall_back=None) -> np.ndarray:

def normalize(
vect: np.ndarray | tuple[float], fall_back: np.ndarray | None = None
) -> np.ndarray:
norm = np.linalg.norm(vect)
if norm > 0:
return np.array(vect) / norm
Expand Down Expand Up @@ -487,11 +494,11 @@ def R3_to_complex(point: Sequence[float]) -> np.ndarray:
return complex(*point[:2])


def complex_func_to_R3_func(complex_func):
def complex_func_to_R3_func(complex_func: Callable) -> Callable:
return lambda p: complex_to_R3(complex_func(R3_to_complex(p)))


def center_of_mass(points: Sequence[float]) -> np.ndarray:
def center_of_mass(points: list[Sequence[float]]) -> np.ndarray:
"""Gets the center of mass of the points in space.
Parameters
Expand Down Expand Up @@ -619,12 +626,13 @@ def get_winding_number(points: Sequence[np.ndarray]) -> float:
>>> get_winding_number(polygon.get_vertices())
0.0
"""
total_angle = 0
total_angle: float = 0
for p1, p2 in adjacent_pairs(points):
d_angle = angle_of_vector(p2) - angle_of_vector(p1)
d_angle = ((d_angle + PI) % TAU) - PI
total_angle += d_angle
return total_angle / TAU
val: float = total_angle / TAU
return val


def shoelace(x_y: np.ndarray) -> float:
Expand All @@ -637,7 +645,8 @@ def shoelace(x_y: np.ndarray) -> float:
"""
x = x_y[:, 0]
y = x_y[:, 1]
return np.trapz(y, x)
val: float = np.trapz(y, x)
return val


def shoelace_direction(x_y: np.ndarray) -> str:
Expand Down Expand Up @@ -758,7 +767,7 @@ def earclip_triangulation(verts: np.ndarray, ring_ends: list) -> list:
raise Exception("Could not find a ring to attach")

# Setup linked list
after = []
after: list = []
end0 = 0
for end1 in ring_ends:
after.extend(range(end0 + 1, end1))
Expand Down Expand Up @@ -829,7 +838,7 @@ def spherical_to_cartesian(spherical: Sequence[float]) -> np.ndarray:

def perpendicular_bisector(
line: Sequence[np.ndarray],
norm_vector=OUT,
norm_vector: Vector3D = OUT,
) -> Sequence[np.ndarray]:
"""Returns a list of two points that correspond
to the ends of the perpendicular bisector of the
Expand Down
3 changes: 3 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ ignore_errors = False
[mypy-manim.utils.parameter_parsing.*]
ignore_errors = False

[mypy-manim.utils.space_ops.*]
ignore_errors = False

[mypy-manim.utils.simple_functions.*]
ignore_errors = False

Expand Down

0 comments on commit 47a0661

Please sign in to comment.