Skip to content
This repository has been archived by the owner on Nov 13, 2019. It is now read-only.

Commit

Permalink
Reworked the notion of transforms to work with Geometries
Browse files Browse the repository at this point in the history
  • Loading branch information
alcarney committed May 7, 2018
1 parent 152f488 commit bea75a7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 28 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ erratum = "*"
hypothesis = "*"
jupyter = "*"
pytest = "*"
pytest-cov = "*"
sphinx = "*"
sphinx-autobuild = "*"
"flake8" = "*"
Expand Down
1 change: 0 additions & 1 deletion topos/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .geometry import Mesh
from .primitives import Plane, Cylinder
from .transforms import translate, scale
from .vertices import Cartesian, Cylindrical
from .version import __version__
77 changes: 50 additions & 27 deletions topos/core/transforms.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,67 @@
from abc import ABC, abstractmethod
import numpy as np
from .geometry import Mesh
from .geometry import Geometry, Mesh


class Translate(object):
"""
Translate an object by [dx, dy, dz]
"""
class VertexTransform(ABC):
"""A vertex transform is a transform that only acts on the vertices
of a geometry."""

def __init__(self, dx, dy, dz):
@abstractmethod
def transform(self, verts):
pass

self._offset = np.array([dx, dy, dz])
def __rrshift__(self, other):

def __rrshift__(self, mesh):
if not isinstance(other, Geometry):
raise TypeError('Transforms can only be applied to Geometry objects')

vs = mesh._vertices
vs += self._offset
new_verts = self.transform(other.vertices.copy())

return Mesh(mesh.name, vertices=vs, faces=mesh.faces,
coord=mesh._coord)
return Mesh(verts=new_verts, faces=other.faces, name=other.name)


class Scale(object):
"""
Scale either in all directions or a specified axis
"""
def __init__(self, all=1.):
self._all = all
class Displace(VertexTransform):
"""Displace the vertices in a geometry by a given amount."""

def __rrshift__(self, mesh):
def __init__(self, x=None, y=None, z=None, r=None, t=None):

vs = mesh._vertices
vs *= self._all
self._displacements = {
'x': x,
'y': y,
'z': z,
'r': r,
't': t
}

return Mesh(mesh.name, vertices=vs, faces=mesh.faces,
coord=mesh._coord)
def transform():
pass


def translate(dx=0., dy=0., dz=0.):
return Translate(dx, dy, dz)
class Scale(VertexTransform):
"""Scale a geometry by a given amount."""

def __init__(self, dx=None, dy=None, dz=None, dr=None, dt=None):

def scale(x):
return Scale(all=x)
self._factors = {
'x': dx,
'y': dy,
'z': dz,
'r': dr,
't': dt
}

def transform(self, verts):

for coord, factor in self._factors.items():

if factor is not None:

values = verts.__getattribute__(coord)
verts.__setattr__(coord, values * factor)

return verts


def scale(dx=None, dy=None, dz=None, dr=None, dt=None):
return Scale(dx, dy, dz, dr, dt)
5 changes: 5 additions & 0 deletions topos/core/vertices.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ def fromarray(cls, array):
"""Given a numpy array, construct a :py:class:`VertexArray` from it."""
return cls(array)

def copy(self):
"""Return a copy of the array under the same coordinate system."""
vs = np.array(self.data)
return self.fromarray(vs)

@property
def system(self):
"""Return a string representing the coorindate system the array uses."""
Expand Down

0 comments on commit bea75a7

Please sign in to comment.