mirror of
https://github.com/3b1b/manim.git
synced 2025-09-01 00:48:45 +00:00
Allow cross to take an 'out' array
This commit is contained in:
parent
ed2dbfd9b9
commit
4de0d098ea
1 changed files with 10 additions and 4 deletions
|
@ -22,7 +22,11 @@ if TYPE_CHECKING:
|
||||||
from manimlib.typing import Vect2, Vect3, Vect4, VectN, Matrix3x3, Vect3Array, Vect2Array
|
from manimlib.typing import Vect2, Vect3, Vect4, VectN, Matrix3x3, Vect3Array, Vect2Array
|
||||||
|
|
||||||
|
|
||||||
def cross(v1: Vect3 | List[float], v2: Vect3 | List[float]) -> Vect3 | Vect3Array:
|
def cross(
|
||||||
|
v1: Vect3 | List[float],
|
||||||
|
v2: Vect3 | List[float],
|
||||||
|
out: np.ndarray | None = None
|
||||||
|
) -> Vect3 | Vect3Array:
|
||||||
is2d = isinstance(v1, np.ndarray) and len(v1.shape) == 2
|
is2d = isinstance(v1, np.ndarray) and len(v1.shape) == 2
|
||||||
if is2d:
|
if is2d:
|
||||||
x1, y1, z1 = v1[:, 0], v1[:, 1], v1[:, 2]
|
x1, y1, z1 = v1[:, 0], v1[:, 1], v1[:, 2]
|
||||||
|
@ -30,12 +34,14 @@ def cross(v1: Vect3 | List[float], v2: Vect3 | List[float]) -> Vect3 | Vect3Arra
|
||||||
else:
|
else:
|
||||||
x1, y1, z1 = v1
|
x1, y1, z1 = v1
|
||||||
x2, y2, z2 = v2
|
x2, y2, z2 = v2
|
||||||
result = np.array([
|
if out is None:
|
||||||
|
out = np.empty(np.shape(v1))
|
||||||
|
out.T[:] = [
|
||||||
y1 * z2 - z1 * y2,
|
y1 * z2 - z1 * y2,
|
||||||
z1 * x2 - x1 * z2,
|
z1 * x2 - x1 * z2,
|
||||||
x1 * y2 - y1 * x2,
|
x1 * y2 - y1 * x2,
|
||||||
])
|
]
|
||||||
return result.T if is2d else result
|
return out
|
||||||
|
|
||||||
|
|
||||||
def get_norm(vect: VectN | List[float]) -> float:
|
def get_norm(vect: VectN | List[float]) -> float:
|
||||||
|
|
Loading…
Add table
Reference in a new issue