-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added Custom Types Everything runs abut half as fast but it is much less error prone and more readable * Changed Raytrace Imports Change the Raytrace library so that mesh classes can be imported directly * Restructured Raytrace Library Moved the mesh classes into their own folder * Fixed Typo Related Bug Passing a Vector3 to the Vector3 constructor no longer causes an error * Redid Custom Types Custom types are now better integrated with numpy to get speed back to around where they were (well, 2/3rds the speed but whos counting)
- Loading branch information
Showing
18 changed files
with
351 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__pycache__/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from .custom_np import CustomNP as CustomNP | ||
from .custom_types import ( | ||
Vector3 as Vector3, | ||
Triangle as Triangle, | ||
Ray as Ray, | ||
) | ||
from .custom_arrays import ( | ||
FloatArray as FloatArray, | ||
TriangleArray as TriangleArray, | ||
Vector3Array as Vector3Array, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from typing import Any, Iterable, Literal, overload | ||
import numpy as np | ||
|
||
from . import CustomNP, Triangle, Vector3 | ||
|
||
class FloatArray(CustomNP): | ||
def __new__(cls, | ||
content: Iterable[float]|np.ndarray|None = None, | ||
): | ||
if isinstance(content, np.ndarray): | ||
if content.ndim != 1: raise ValueError(content.shape) # must be 1d | ||
return content.view(cls) | ||
if content is None: | ||
return np.ndarray((0,), float).view(cls) | ||
return np.array(content).view(cls) | ||
def __getitem__(self, index): | ||
if isinstance(index, slice): return super().__getitem__(index).view(type(self)) | ||
return super().__getitem__(index) | ||
|
||
class Vector3Array(CustomNP): | ||
def __new__(cls, | ||
content: Iterable[Vector3]|np.ndarray|None = None, | ||
): | ||
if isinstance(content, np.ndarray): | ||
if content.ndim == 1: content = content.reshape((1,) + content.shape) | ||
if content.ndim != 2: raise ValueError(content.shape) # must be 1d or 2d | ||
if content.shape[1] != 3: raise ValueError(content.shape) # must be shape (n, 3) | ||
return content.view(cls) | ||
if content is None: | ||
return np.ndarray((0, 3), float).view(cls) | ||
return np.array(content).view(cls) | ||
def __getitem__(self, index): | ||
if isinstance(index, slice): return super().__getitem__(index).view(type(self)) | ||
return super().__getitem__(index) | ||
|
||
class TriangleArray(CustomNP): | ||
def __new__(cls, | ||
content: Iterable[Triangle]|np.ndarray|None = None, | ||
): | ||
if isinstance(content, np.ndarray): | ||
if content.ndim == 2: content = content.reshape((1,) + content.shape) | ||
if content.ndim != 3: raise ValueError(content.shape) # must be 2d or 3d | ||
if content.shape[1] != 3 or content.shape[2] != 3: raise ValueError(content.shape) # must be shape (n, 3, 3) | ||
return content.view(cls) | ||
if content is None: | ||
return np.ndarray((0, 3, 3), float).view(cls) | ||
return np.array(content).view(cls) | ||
def __getitem__(self, index): | ||
if isinstance(index, slice): return super().__getitem__(index).view(type(self)) | ||
return super().__getitem__(index) | ||
@property | ||
def verticies(self) -> tuple[Vector3Array, Vector3Array, Vector3Array]: | ||
v0, v1, v2 = self.swapaxes(0, 1) | ||
return (v0.view(Vector3Array), v1.view(Vector3Array), v2.view(Vector3Array)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import numpy as np | ||
from typing import Any, Iterable, Literal, Self, Type, TypeVar, overload | ||
|
||
from . import CustomNP, Triangle, Vector3 | ||
|
||
class FloatArray(CustomNP[tuple[int, Literal[3]], np.dtype[np.float_]]): | ||
@overload | ||
def __new__(cls) -> FloatArray:... | ||
@overload | ||
def __new__(cls, content: Iterable[float]) -> FloatArray:... | ||
@overload | ||
def __new__(cls, content: np.ndarray[tuple[int], np.dtype[np.float_]]) -> FloatArray:... | ||
|
||
@overload # type: ignore | ||
def __getitem__(self, index:int) -> float: ... | ||
@overload | ||
def __getitem__(self, index:slice) -> Self: ... | ||
|
||
class Vector3Array(CustomNP[tuple[int, Literal[3]], np.dtype[np.float_]]): | ||
@overload | ||
def __new__(cls) -> Vector3Array:... | ||
@overload | ||
def __new__(cls, content: Iterable[Vector3]) -> Vector3Array:... | ||
@overload | ||
def __new__(cls, content: np.ndarray[tuple[int, Literal[3]], np.dtype[np.float_]]) -> Vector3Array:... | ||
|
||
@overload # type: ignore | ||
def __getitem__(self, index:int) -> Vector3: ... | ||
@overload | ||
def __getitem__(self, index:tuple[int, Literal[0, 1, 2]]) -> float: ... | ||
@overload | ||
def __getitem__(self, index:slice) -> Self: ... | ||
|
||
class TriangleArray(CustomNP[tuple[int, Literal[3], Literal[3]], np.dtype[np.float_]]): | ||
@overload | ||
def __new__(cls) -> TriangleArray:... | ||
@overload | ||
def __new__(cls, content: Iterable[Triangle]) -> TriangleArray:... | ||
@overload | ||
def __new__(cls, content: np.ndarray[tuple[int, Literal[3], Literal[3]], np.dtype[np.float_]]) -> TriangleArray:... | ||
|
||
@overload # type: ignore | ||
def __getitem__(self, index:tuple[int, Literal[0, 1, 2]]) -> Vector3: ... | ||
@overload | ||
def __getitem__(self, index:tuple[int, Literal[0, 1, 2], Literal[0, 1, 2]]) -> float: ... | ||
@overload | ||
def __getitem__(self, index:slice) -> Self: ... | ||
|
||
@property | ||
def verticies(self) -> tuple[Vector3Array, Vector3Array, Vector3Array]: ... | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
from typing import Generic, TypeVar, Self | ||
import numpy as np | ||
|
||
|
||
_S = TypeVar('_S', bound=tuple) | ||
_T = TypeVar('_T', bound=np.dtype) | ||
class CustomNP(np.ndarray[_S, _T]): | ||
def __add__(self, other): | ||
return super().__add__(other).view(type(self)) | ||
def __sub__(self, other): | ||
return super().__sub__(other).view(type(self)) | ||
def __mul__(self, other): | ||
return super().__mul__(other).view(type(self)) | ||
def __truediv__(self, other): | ||
return super().__truediv__(other).view(type(self)) | ||
def __rtruediv__(self, other): | ||
return super().__rtruediv__(other).view(type(self)) | ||
@property | ||
def nd(self): | ||
return self.view(np.ndarray) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from typing import TypeVar, Self | ||
import numpy as np | ||
|
||
|
||
_S = TypeVar('_S', bound=tuple) | ||
_T = TypeVar('_T', bound=np.dtype) | ||
class CustomNP(np.ndarray[_S, _T]): | ||
def __add__(self, other:Self) -> Self: ... # type: ignore | ||
def __sub__(self, other:Self) -> Self: ... # type: ignore | ||
def __mul__(self, other:Self|float) -> Self: ... # type: ignore | ||
def __truediv__(self, other:Self|float) -> Self: ... # type: ignore | ||
def __rtruediv__(self, other:float) -> Self: ... # type: ignore | ||
|
||
@property | ||
def nd(self) -> np.ndarray: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from typing import Any, Iterable, Literal, Self, overload | ||
import numpy as np | ||
from . import CustomNP | ||
|
||
class Vector3(CustomNP): | ||
def __new__(cls, | ||
x: float | tuple[float, float, float] | Self, y = 0, z = 0): | ||
if isinstance(x, tuple): | ||
x, y, z = x | ||
elif isinstance(x, Vector3): | ||
x, y, z = x | ||
assert isinstance(x, (float, int)) | ||
return np.array([x, y, z]).view(cls) | ||
def __str__(self): | ||
return f"({self[0]}, {self[1]}, {self[2]})" | ||
def __repr__(self): | ||
return f"Vector3({self[0]}, {self[1]}, {self[2]})" | ||
|
||
class Triangle(CustomNP): | ||
def __new__(cls, | ||
v0: Vector3 | tuple[Vector3, Vector3, Vector3], | ||
v1: Vector3 = Vector3(0, 0, 0), | ||
v2: Vector3 = Vector3(0, 0, 0), | ||
) -> 'Triangle': | ||
if isinstance(v0, tuple): | ||
v0, v1, v2 = v0 | ||
return np.array([v0, v1, v2]).view(cls) | ||
|
||
def __str__(self) -> str: | ||
return f"({self[0]}, {self[1]}, {self[2]})" | ||
def __repr__(self) -> str: | ||
return f"Vector3({self[0]}, {self[1]}, {self[2]})" | ||
|
||
class Ray: | ||
direction:Vector3 | ||
origin:Vector3 | ||
def __init__(self, | ||
direction:Vector3, | ||
origin:Vector3, | ||
) -> None: | ||
self.direction = direction | ||
self.origin = origin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from typing import Any, Iterable, Iterator, Literal, TypeAlias, TypeVar, overload | ||
import numpy as np | ||
from . import CustomNP | ||
|
||
class Vector3(CustomNP[tuple[Literal[3]], np.dtype[np.float_]]): | ||
@overload | ||
def __new__(cls, x: float, y: float, z: float) -> Vector3: ... | ||
@overload | ||
def __new__(cls, x: tuple[float, float, float]) -> Vector3: ... | ||
@overload | ||
def __new__(cls, x: Vector3) -> Vector3: ... | ||
|
||
def __getitem__(self, index:Literal[0, 1, 2]) -> float: ... # type: ignore | ||
|
||
class Triangle(CustomNP[tuple[Literal[3], Literal[3]], np.dtype[np.float_]], Iterable[Vector3]): | ||
@overload | ||
def __new__(cls, v0: Vector3, v1: Vector3, v2: Vector3) -> Triangle: ... | ||
@overload | ||
def __new__(cls, v0: tuple[Vector3, Vector3, Vector3]) -> Triangle: ... | ||
@overload | ||
def __new__(cls, v0: Triangle) -> Triangle: ... | ||
|
||
@overload # type: ignore | ||
def __getitem__(self, index:Literal[0, 1, 2]) -> Vector3: ... | ||
@overload | ||
def __getitem__(self, index:tuple[Literal[0, 1, 2], Literal[0, 1, 2]]) -> float: ... | ||
|
||
def __add__(self, other:Triangle) -> Triangle: ... # type: ignore | ||
def __sub__(self, other:Triangle) -> Triangle: ... # type: ignore | ||
|
||
def __iter__(self) -> Iterator[Vector3]: ... | ||
class Ray: | ||
direction:Vector3 | ||
origin:Vector3 | ||
def __init__(self, | ||
direction:Vector3, | ||
origin:Vector3, | ||
) -> None: | ||
self.direction = direction | ||
self.origin = origin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .triangle_mesh import TriangleMesh as TriangleMesh | ||
from .bvh_mesh import BVHMesh as BVHMesh | ||
from .composite_mesh import CompositeMesh as CompositeMesh |
Oops, something went wrong.