Skip to content

CwVector3d

src.cwmath.cwvector3d

CwVector3d

Vector class for 3D vectors.

Source code in src/cwmath/cwvector3d.py
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
class CwVector3d:
    """Vector class for 3D vectors."""

    def __init__(self, x: float, y: float, z: float):
        self._x = x
        self._y = y
        self._z = z

    @classmethod
    def from_point_3d(cls, point_3d: 'cadwork.point_3d') -> 'CwVector3d':
        """Create a CwVector3d from a cadwork.point_3d.

        Args:
            point_3d: 3d point

        Returns:
            vector
        """
        return cls(point_3d.x, point_3d.y, point_3d.z)

    @property
    def x(self) -> float:
        return self._x

    @x.setter
    def x(self, value: float) -> None:
        self._x = value

    @property
    def y(self) -> float:
        return self._y

    @y.setter
    def y(self, value: float) -> None:
        self._y = value

    @property
    def z(self) -> float:
        return self._z

    @z.setter
    def z(self, value: float) -> None:
        self._z = value

    def dot(self, other: 'CwVector3d') -> float:
        """ Calculates the dot product of two vectors.

        Args:
            other: vector

        Returns:
            dot product
        """
        return self._x * other._x + self._y * other._y + self._z * other._z

    def magnitude(self) -> float:
        """ Calculates the magnitude of the vector.

        Returns:
            magnitude
        """
        return (self._x**2 + self._y**2 + self._z**2)**0.5

    def normalize(self) -> 'CwVector3d':
        """ Normalizes the vector.

        Returns:
            normalized vector
        """
        return self / self.magnitude()

    def __add__(self, other: 'CwVector3d') -> 'CwVector3d':
        return CwVector3d(self._x + other._x, self._y + other._y, self._z + other._z)

    def __sub__(self, other: 'CwVector3d') -> 'CwVector3d':
        return CwVector3d(self._x - other._x, self._y - other._y, self._z - other._z)

    def __mul__(self, scalar: float) -> 'CwVector3d':
        return CwVector3d(self._x * scalar, self._y * scalar, self._z * scalar)

    def __truediv__(self, scalar: float) -> 'CwVector3d':
        return CwVector3d(self._x / scalar, self._y / scalar, self._z / scalar)

    def __neg__(self) -> 'CwVector3d':
        return CwVector3d(-self._x, -self._y, -self._z)

    def __eq__(self, other: 'CwVector3d') -> bool:
        return abs(self._x - other._x) < 1e-6 and abs(self._y - other._y) < 1e-6 and abs(self._z - other._z) < 1e-6

    def __ne__(self, other: 'CwVector3d') -> bool:
        return not self.__eq__(other)

    def __str__(self) -> str:
        return f'({self._x}, {self._y}, {self._z})'

    def __repr__(self) -> str:
        return f'CwVector({self._x}, {self._y}, {self._z})'

    def __iter__(self):
        yield self._x
        yield self._y
        yield self._z

    def __getitem__(self, index: int) -> float:
        return (self._x, self._y, self._z)[index]

    def __setitem__(self, index: int, value: float) -> None:
        if index == 0:
            self._x = value
        elif index == 1:
            self._y = value
        elif index == 2:
            self._z = value
        else:
            raise IndexError('Index out of range')

dot(other)

Calculates the dot product of two vectors.

Parameters:

Name Type Description Default
other CwVector3d

vector

required

Returns:

Type Description
float

dot product

Source code in src/cwmath/cwvector3d.py
49
50
51
52
53
54
55
56
57
58
def dot(self, other: 'CwVector3d') -> float:
    """ Calculates the dot product of two vectors.

    Args:
        other: vector

    Returns:
        dot product
    """
    return self._x * other._x + self._y * other._y + self._z * other._z

from_point_3d(point_3d) classmethod

Create a CwVector3d from a cadwork.point_3d.

Parameters:

Name Type Description Default
point_3d point_3d

3d point

required

Returns:

Type Description
CwVector3d

vector

Source code in src/cwmath/cwvector3d.py
13
14
15
16
17
18
19
20
21
22
23
@classmethod
def from_point_3d(cls, point_3d: 'cadwork.point_3d') -> 'CwVector3d':
    """Create a CwVector3d from a cadwork.point_3d.

    Args:
        point_3d: 3d point

    Returns:
        vector
    """
    return cls(point_3d.x, point_3d.y, point_3d.z)

magnitude()

Calculates the magnitude of the vector.

Returns:

Type Description
float

magnitude

Source code in src/cwmath/cwvector3d.py
60
61
62
63
64
65
66
def magnitude(self) -> float:
    """ Calculates the magnitude of the vector.

    Returns:
        magnitude
    """
    return (self._x**2 + self._y**2 + self._z**2)**0.5

normalize()

Normalizes the vector.

Returns:

Type Description
CwVector3d

normalized vector

Source code in src/cwmath/cwvector3d.py
68
69
70
71
72
73
74
def normalize(self) -> 'CwVector3d':
    """ Normalizes the vector.

    Returns:
        normalized vector
    """
    return self / self.magnitude()