-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Curves (and possibly other future entities) that are defined by a large number of points create a huge bottleneck when transforming. Each Point is transformed separately and within that every transformation matrix must be constructed separately and new and new numpy arrays are created which takes a lot of time. Array holds the same numpy array with *all* the points and transforms everything in one go. Improvement on a gear case: 45s > 17s (the whole case, not just the transforms).
- Loading branch information
1 parent
4e26ef7
commit 71cc985
Showing
10 changed files
with
132 additions
and
56 deletions.
There are no files selected for viewing
Empty file.
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,78 @@ | ||
from typing import Optional | ||
|
||
import numpy as np | ||
|
||
from classy_blocks.base.element import ElementBase | ||
from classy_blocks.base.exceptions import ArrayCreationError | ||
from classy_blocks.types import PointListType, PointType, VectorType | ||
from classy_blocks.util import functions as f | ||
from classy_blocks.util.constants import DTYPE | ||
|
||
# TODO! Tests | ||
|
||
|
||
class Array(ElementBase): | ||
def __init__(self, points: PointListType): | ||
"""A list of points ('positions') in 3D space""" | ||
self.points = np.array(points, dtype=DTYPE) | ||
|
||
shape = np.shape(self.points) | ||
|
||
if shape[1] != 3: | ||
raise ArrayCreationError("Provide a list of points of 3D space!") | ||
|
||
if len(self.points) <= 1: | ||
raise ArrayCreationError("Provide at least 2 points in 3D space!") | ||
|
||
def translate(self, displacement): | ||
self.points += np.asarray(displacement, dtype=DTYPE) | ||
|
||
return self | ||
|
||
def rotate(self, angle, axis, origin: Optional[PointType] = None): | ||
if origin is None: | ||
origin = f.vector(0, 0, 0) | ||
|
||
axis = np.array(axis) | ||
matrix = f.rotation_matrix(axis, angle) | ||
rotated_points = np.dot(self.points - origin, matrix.T) | ||
|
||
self.points = rotated_points + origin | ||
|
||
return self | ||
|
||
def scale(self, ratio, origin: Optional[PointType] = None): | ||
if origin is None: | ||
origin = f.vector(0, 0, 0) | ||
|
||
self.points = origin + (self.points - origin) * ratio | ||
|
||
return self | ||
|
||
def mirror(self, normal: VectorType, origin: Optional[PointType] = None): | ||
if origin is None: | ||
origin = f.vector(0, 0, 0) | ||
|
||
normal = np.array(normal) | ||
matrix = f.mirror_matrix(normal) | ||
|
||
self.points -= origin | ||
|
||
mirrored_points = np.dot(self.points - origin, matrix.T) | ||
self.points = mirrored_points + origin | ||
|
||
return self | ||
|
||
@property | ||
def center(self): | ||
return np.average(self.points, axis=0) | ||
|
||
@property | ||
def parts(self): | ||
return [self] | ||
|
||
def __len__(self): | ||
return len(self.points) | ||
|
||
def __getitem__(self, i): | ||
return self.points[i] |
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
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
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