Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/vector_swizzling/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import math
import numpy as np
from typing import Union
from typing import TypeAlias

scalar: TypeAlias = Union[int, float]
vector: TypeAlias = Union[np.ndarray, tuple, list, "SVec"]

class SVec:
def __init__(self, *components):
Expand All @@ -16,19 +20,19 @@ def __init__(self, *components):
}
flat_components = []
for item in components:
if isinstance(item, SVec):
flat_components.extend(item.components.tolist())
elif isinstance(item, (list, tuple, np.ndarray)):
if isinstance(item, vector):
flat_components.extend(item)
else:
elif isinstance(item, scalar):
flat_components.append(item)
else:
flat_components.append(float(item))
self.components = np.array(flat_components, dtype=float)

def __len__(self):
return len(self.components)

def __iter__(self):
return iter(self.components)
return iter(self.components.tolist())

def __getitem__(self, index):
return self.components[index]
Expand Down Expand Up @@ -79,22 +83,18 @@ def __setattr__(self, swizzle, other):
self.components[self.lookup[component]] = other[i]

def __add__(self, other):
if len(self) != len(other):
raise ValueError("Vectors must have the same size")
return self.__class__(*(self.components + other.components))
return self.__class__(*(self.components + other))

def __sub__(self, other):
if len(self) != len(other):
raise ValueError("Vectors must have the same size")
return self.__class__(*(self.components - other.components))
return self.__class__(*(self.components - other))

def __mul__(self, scalar: Union[int, float]):
def __mul__(self, scalar):
return self.__class__(*(self.components * scalar))

def __truediv__(self, scalar: Union[int, float]):
def __truediv__(self, scalar):
return self.__class__(*(self.components / scalar))

def __floordiv__(self, scalar: Union[int, float]):
def __floordiv__(self, scalar):
return self.__class__(*(self.components // scalar))

class SVec2(SVec):
Expand Down