Skip to content

Commit

Permalink
Reformat and sort out typing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
FranzBangar committed Aug 8, 2024
1 parent 1afbd1d commit 8aa2b4a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 39 deletions.
10 changes: 8 additions & 2 deletions src/classy_blocks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
from .construct.flat.sketches.disk import FourCoreDisk, HalfDisk, OneCoreDisk, Oval, WrappedDisk
from .construct.flat.sketches.grid import Grid
from .construct.flat.sketches.mapped import MappedSketch
from .construct.flat.sketches.spline_round import QuarterSplineDisk, HalfSplineDisk, SplineDisk
from .construct.flat.sketches.spline_round import QuarterSplineRing, HalfSplineRing, SplineRing
from .construct.flat.sketches.spline_round import (
HalfSplineDisk,
HalfSplineRing,
QuarterSplineDisk,
QuarterSplineRing,
SplineDisk,
SplineRing,
)
from .construct.operations.box import Box
from .construct.operations.connector import Connector
from .construct.operations.extrude import Extrude
Expand Down
39 changes: 24 additions & 15 deletions src/classy_blocks/construct/flat/sketches/mapped.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from typing import List, Union, TypeVar
from typing import List, Union

import numpy as np

from classy_blocks.construct.flat.face import Face
from classy_blocks.construct.flat.sketch import Sketch
from classy_blocks.types import IndexType, NPPointListType, NPPointType, PointListType
from classy_blocks.util import functions as f
from classy_blocks.util import constants

MappedSketchT = TypeVar("MappedSketchT", bound="MappedSketch")
from classy_blocks.util import functions as f


class MappedSketch(Sketch):
Expand Down Expand Up @@ -61,37 +59,48 @@ def positions(self) -> NPPointListType:

return np.array([all_points[indexes.index(i)] for i in range(max_index + 1)])

def merge(self, other: Union[List[MappedSketchT], MappedSketchT]):
def merge(self, other: Union[List["MappedSketch"], "MappedSketch"]):
"""Adds a sketch or list of sketches to itself.
New faces and indexes are appended and all duplicate points are removed."""
def merge_two_sketches(sketch_1: MappedSketchT, sketch_2: MappedSketchT) -> None:

def merge_two_sketches(sketch_1: MappedSketch, sketch_2: MappedSketch) -> None:
"""Add sketch_2 to sketch_1"""

# Check planes are oriented the same
if not abs(f.angle_between(sketch_1.normal, sketch_2.normal)) < constants.TOL:
print(f.angle_between(sketch_1.normal, sketch_2.normal)/np.pi, sketch_1.normal, sketch_2.normal)
raise Warning(f"Sketch {sketch_2} with normal {sketch_2.normal} is not oriented as "
f"sketch {sketch_1} with normal {sketch_1.normal}")
print(f.angle_between(sketch_1.normal, sketch_2.normal) / np.pi, sketch_1.normal, sketch_2.normal)
raise Warning(
f"Sketch {sketch_2} with normal {sketch_2.normal} is not oriented as "
f"sketch {sketch_1} with normal {sketch_1.normal}"
)

# All unique points
sketch_1_pos = sketch_1.positions
all_pos = np.asarray([*sketch_1_pos.tolist(),
*[pos for pos in sketch_2.positions if
all(np.linalg.norm(sketch_1_pos - pos.reshape((1, 3)), axis=1) >= constants.TOL)]])
all_pos = np.asarray(
[
*sketch_1_pos.tolist(),
*[
pos
for pos in sketch_2.positions
if all(np.linalg.norm(sketch_1_pos - pos.reshape((1, 3)), axis=1) >= constants.TOL)
],
]
)

sketch_2_ind = np.asarray(sketch_2.indexes)
# Change sketch_2 indexes to new position list.
for i, face in enumerate(sketch_2.faces):
for j, pos in enumerate(face.point_array):
sketch_2_ind[i, j] = np.argwhere(np.linalg.norm(all_pos - pos.reshape((1, 3)),
axis=1 < constants.TOL))[0][0]
sketch_2_ind[i, j] = np.argwhere(
np.linalg.norm(all_pos - pos.reshape((1, 3)), axis=1 < constants.TOL)
)[0][0]

# Append indexes and faces to sketch_1
sketch_1.indexes = [*list(sketch_1.indexes), *sketch_2_ind.tolist()]
sketch_1._faces = [*sketch_1.faces, *sketch_2.faces]

# If list of sketches
if hasattr(other, '__iter__'):
if isinstance(other, list):
for o in other:
merge_two_sketches(self, o)
else:
Expand Down
48 changes: 26 additions & 22 deletions src/classy_blocks/construct/flat/sketches/spline_round.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import ClassVar, List, Optional
import inspect

import numpy as np

from classy_blocks.base import transforms as tr
from classy_blocks.construct.edges import Origin, Spline
from classy_blocks.construct.flat.face import Face
from classy_blocks.construct.flat.sketches.disk import DiskBase
Expand All @@ -10,7 +11,6 @@
from classy_blocks.types import NPPointType, NPVectorType, PointType
from classy_blocks.util import constants
from classy_blocks.util import functions as f
from classy_blocks.base import transforms as tr


class SplineRound(MappedSketch):
Expand All @@ -30,12 +30,7 @@ class SplineRound(MappedSketch):
[1, 2], # axis 1
]

def __init__(
self,
side_1: float,
side_2: float,
**kwargs
):
def __init__(self, side_1: float, side_2: float, **kwargs):
"""
With a normal in x direction corner 1 will be in the y direction and corner 2 the z direction.
note the vectors from the center to corner 1 and 2 should be perpendicular.
Expand All @@ -49,8 +44,8 @@ def __init__(
self.side_1 = side_1
self.side_2 = side_2

self.n_outer_spline_points = kwargs.get('n_outer_spline_points', self.n_outer_spline_points)
self.n_straight_spline_points = kwargs.get('n_straight_spline_points', self.n_straight_spline_points)
self.n_outer_spline_points = kwargs.get("n_outer_spline_points", self.n_outer_spline_points)
self.n_straight_spline_points = kwargs.get("n_straight_spline_points", self.n_straight_spline_points)

@property
def center(self):
Expand Down Expand Up @@ -120,7 +115,7 @@ def __init__(
corner_2_point: PointType,
side_1: float,
side_2: float,
**kwargs
**kwargs,
) -> None:
"""
With a normal in x direction corner 1 will be in the y direction and corner 2 the z direction.
Expand Down Expand Up @@ -264,6 +259,7 @@ def add_edges(self) -> None:

class HalfSplineDisk(QuarterSplineDisk):
"""Sketch for Half oval, elliptical and circular shapes"""

chops: ClassVar = [
[1], # axis 0
[1, 2, 5], # axis 1
Expand All @@ -276,7 +272,7 @@ def __init__(
corner_2_point: PointType,
side_1: float,
side_2: float,
**kwargs
**kwargs,
) -> None:
"""
With a normal in x direction corner 1 will be in the y direction and corner 2 the z direction.
Expand All @@ -291,8 +287,9 @@ def __init__(

super().__init__(center_point, corner_1_point, corner_2_point, side_1, side_2, **kwargs)
# Create quarter mirrored arround u_1
other_quarter = QuarterSplineDisk(self.center, self.corner_2, 2 * self.center - self.corner_1,
side_2, side_1, **kwargs)
other_quarter = QuarterSplineDisk(
self.center, self.corner_2, 2 * self.center - self.corner_1, side_2, side_1, **kwargs
)
# Merge other_quarter to this quarter.
self.merge(other_quarter)

Expand All @@ -306,18 +303,20 @@ def grid(self) -> List[List[Face]]:

class SplineDisk(HalfSplineDisk):
"""Sketch for full oval, elliptical and circular shapes"""

chops: ClassVar = [
[1], # axis 0
[1, 2, 5, 7, 8, 11], # axis 1
]

def __init__(
self,
center_point: PointType,
corner_1_point: PointType,
corner_2_point: PointType,
side_1: float,
side_2: float,
**kwargs
**kwargs,
) -> None:
"""
With a normal in x direction corner 1 will be in the y direction and corner 2 the z direction.
Expand Down Expand Up @@ -359,9 +358,8 @@ def __init__(
side_2: float,
width_1: float,
width_2: float,
**kwargs
**kwargs,
):

"""
With a normal in x direction corner 1 will be in the y direction and corner 2 the z direction.
Note the vectors from the center to corner 1 and 2 should be perpendicular.
Expand Down Expand Up @@ -401,7 +399,9 @@ def __init__(
p5 = corner_1
p5_2 = corner_1 + self.width_1 * u_1
p6 = center + (self.side_1 + 2 ** (-1 / 2) * r_1) * u_1 + (self.side_2 + 2 ** (-1 / 2) * r_2) * u_2
p6_2 = center + (self.side_1 + 2 ** (-1 / 2) * r_1_outer) * u_1 + (self.side_2 + 2 ** (-1 / 2) * r_2_outer) * u_2
p6_2 = (
center + (self.side_1 + 2 ** (-1 / 2) * r_1_outer) * u_1 + (self.side_2 + 2 ** (-1 / 2) * r_2_outer) * u_2
)

quad_map = [
[2, 3, 1, 0],
Expand Down Expand Up @@ -542,8 +542,10 @@ def add_edges(self) -> None:
self.shell[0].add_edge(3, Origin(self.center))
self.shell[1].add_edge(3, Origin(self.center))


class HalfSplineRing(QuarterSplineRing):
"""Sketch for Half oval, elliptical and circular ring"""

chops: ClassVar = [
[0], # axis 0
[0, 1, 2, 3], # axis 1
Expand All @@ -558,7 +560,7 @@ def __init__(
side_2: float,
width_1: float,
width_2: float,
**kwargs
**kwargs,
) -> None:
"""
With a normal in x direction corner 1 will be in the y direction and corner 2 the z direction.
Expand All @@ -574,13 +576,15 @@ def __init__(
"""

super().__init__(center_point, corner_1_point, corner_2_point, side_1, side_2, width_1, width_2, **kwargs)
other_quarter = QuarterSplineRing(self.center, self.corner_2, 2 * self.center - self.corner_1,
side_2, side_1, width_2, width_1, **kwargs)
other_quarter = QuarterSplineRing(
self.center, self.corner_2, 2 * self.center - self.corner_1, side_2, side_1, width_2, width_1, **kwargs
)
self.merge(other_quarter)


class SplineRing(HalfSplineRing):
"""Sketch for full oval, elliptical and circular shapes"""

chops: ClassVar = [
[0], # axis 0
[0, 1, 2, 3, 4, 5, 6, 7], # axis 1
Expand All @@ -595,7 +599,7 @@ def __init__(
side_2: float,
width_1: float,
width_2: float,
**kwargs
**kwargs,
) -> None:
"""
With a normal in x direction corner 1 will be in the y direction and corner 2 the z direction.
Expand Down

0 comments on commit 8aa2b4a

Please sign in to comment.