civet.xfm

Types related to xfm transformations (param2xfm, transform_objects, transform_volume).

 1"""
 2Types related to xfm transformations
 3(`param2xfm`, `transform_objects`, `transform_volume`).
 4"""
 5import abc
 6from os import PathLike
 7from typing import Sequence, TypeVar, Literal
 8from civet.abstract_data import AbstractDataCommand
 9from civet.bases import DataSource, DataFile
10from enum import Enum
11from dataclasses import dataclass
12
13
14class Transformations(Enum):
15    CENTER = '-center'
16    TRANSLATION = '-translation'
17    ROTATIONS = '-rotations'
18    SCALES = '-scales'
19    SHEARS = '-shears'
20
21
22@dataclass(frozen=True)
23class Xfm(DataSource):
24    """
25    Represents a `.xfm` file created by `param2xfm`.
26
27    `Xfm` can only represent a single `param2xfm` option.
28    Would be nice to support multiple transformations in go, but it's not implemented.
29    """
30
31    transformation: Transformations
32    x: float
33    y: float
34    z: float
35    preferred_suffix = '.xfm'
36
37    def command(self, output: str | PathLike) -> Sequence[str | PathLike | AbstractDataCommand]:
38        return 'param2xfm', self.transformation.value, str(self.x), str(self.y), str(self.z), output
39
40
41TransformProgram = Literal['transform_objects', 'transform_volume']
42_T = TypeVar('_T', bound='TransformableMixin')
43
44
45@dataclass(frozen=True)
46class TransformableMixin(DataFile[_T], abc.ABC):
47    @property
48    @abc.abstractmethod
49    def transform_program(self) -> TransformProgram:
50        """
51        Specifies which program is used to transform objects of this type.
52        """
53        ...
54
55    def append_xfm(self, xfm: Xfm) -> _T:
56        def command(output: str | PathLike) -> Sequence[str | PathLike | AbstractDataCommand]:
57            return self.transform_program, self, xfm, output
58        return self.create_command(command)
59
60    def flip_x(self) -> _T:
61        """
62        Flip this surface along the *x*-axis.
63        """
64        return self.append_xfm(Xfm(Transformations.SCALES, -1, 1, 1))
65
66    def translate_x(self, x: float) -> _T:
67        """
68        Translate this surface along the *x*-axis.
69        """
70        return self.append_xfm(Xfm(Transformations.TRANSLATION, x, 0, 0))
71
72    def slide_left(self) -> _T:
73        """
74        Translate this surface 25 units to the left.
75        """
76        return self.translate_x(-25)
77
78    def slide_right(self) -> _T:
79        """
80        Translate this surface 25 units to the right.
81        """
82        return self.translate_x(25)
class Transformations(enum.Enum):
15class Transformations(Enum):
16    CENTER = '-center'
17    TRANSLATION = '-translation'
18    ROTATIONS = '-rotations'
19    SCALES = '-scales'
20    SHEARS = '-shears'

An enumeration.

CENTER = <Transformations.CENTER: '-center'>
TRANSLATION = <Transformations.TRANSLATION: '-translation'>
ROTATIONS = <Transformations.ROTATIONS: '-rotations'>
SCALES = <Transformations.SCALES: '-scales'>
SHEARS = <Transformations.SHEARS: '-shears'>
Inherited Members
enum.Enum
name
value
@dataclass(frozen=True)
class Xfm(civet.bases.DataSource):
23@dataclass(frozen=True)
24class Xfm(DataSource):
25    """
26    Represents a `.xfm` file created by `param2xfm`.
27
28    `Xfm` can only represent a single `param2xfm` option.
29    Would be nice to support multiple transformations in go, but it's not implemented.
30    """
31
32    transformation: Transformations
33    x: float
34    y: float
35    z: float
36    preferred_suffix = '.xfm'
37
38    def command(self, output: str | PathLike) -> Sequence[str | PathLike | AbstractDataCommand]:
39        return 'param2xfm', self.transformation.value, str(self.x), str(self.y), str(self.z), output

Represents a .xfm file created by param2xfm.

Xfm can only represent a single param2xfm option. Would be nice to support multiple transformations in go, but it's not implemented.

Xfm( transformation: civet.xfm.Transformations, x: float, y: float, z: float)
preferred_suffix = '.xfm'

Preferred output path suffix for this command.

def command( self, output: str | os.PathLike) -> Sequence[str | os.PathLike | civet.abstract_data.AbstractDataCommand]:
38    def command(self, output: str | PathLike) -> Sequence[str | PathLike | AbstractDataCommand]:
39        return 'param2xfm', self.transformation.value, str(self.x), str(self.y), str(self.z), output
Inherited Members
civet.bases.DataSource
save
@dataclass(frozen=True)
class TransformableMixin(civet.bases.DataFile[~_T], abc.ABC):
46@dataclass(frozen=True)
47class TransformableMixin(DataFile[_T], abc.ABC):
48    @property
49    @abc.abstractmethod
50    def transform_program(self) -> TransformProgram:
51        """
52        Specifies which program is used to transform objects of this type.
53        """
54        ...
55
56    def append_xfm(self, xfm: Xfm) -> _T:
57        def command(output: str | PathLike) -> Sequence[str | PathLike | AbstractDataCommand]:
58            return self.transform_program, self, xfm, output
59        return self.create_command(command)
60
61    def flip_x(self) -> _T:
62        """
63        Flip this surface along the *x*-axis.
64        """
65        return self.append_xfm(Xfm(Transformations.SCALES, -1, 1, 1))
66
67    def translate_x(self, x: float) -> _T:
68        """
69        Translate this surface along the *x*-axis.
70        """
71        return self.append_xfm(Xfm(Transformations.TRANSLATION, x, 0, 0))
72
73    def slide_left(self) -> _T:
74        """
75        Translate this surface 25 units to the left.
76        """
77        return self.translate_x(-25)
78
79    def slide_right(self) -> _T:
80        """
81        Translate this surface 25 units to the right.
82        """
83        return self.translate_x(25)
transform_program: Literal['transform_objects', 'transform_volume']

Specifies which program is used to transform objects of this type.

def append_xfm(self, xfm: civet.xfm.Xfm) -> ~_T:
56    def append_xfm(self, xfm: Xfm) -> _T:
57        def command(output: str | PathLike) -> Sequence[str | PathLike | AbstractDataCommand]:
58            return self.transform_program, self, xfm, output
59        return self.create_command(command)
def flip_x(self) -> ~_T:
61    def flip_x(self) -> _T:
62        """
63        Flip this surface along the *x*-axis.
64        """
65        return self.append_xfm(Xfm(Transformations.SCALES, -1, 1, 1))

Flip this surface along the x-axis.

def translate_x(self, x: float) -> ~_T:
67    def translate_x(self, x: float) -> _T:
68        """
69        Translate this surface along the *x*-axis.
70        """
71        return self.append_xfm(Xfm(Transformations.TRANSLATION, x, 0, 0))

Translate this surface along the x-axis.

def slide_left(self) -> ~_T:
73    def slide_left(self) -> _T:
74        """
75        Translate this surface 25 units to the left.
76        """
77        return self.translate_x(-25)

Translate this surface 25 units to the left.

def slide_right(self) -> ~_T:
79    def slide_right(self) -> _T:
80        """
81        Translate this surface 25 units to the right.
82        """
83        return self.translate_x(25)

Translate this surface 25 units to the right.