"vscode:/vscode.git/clone" did not exist on "b1a1c54c79ac2db1a95e5ac668254fc0da82e1a9"
vec3.py 4.21 KB
Newer Older
1
2
3
4
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/env python


"""
vec3.py: Used for managing vectors of lenght three.
"""
__author__ = "Christopher M. Bruns"
__version__ = "1.0"


import sys
import simtk.unit

class Vec3(object):
    """
    Vec3 represents a three-dimensional point or displacement in space.
    
    Examples
    
    >>> v = Vec3((1, 2, 3))
    >>> print v.dot(v)
    14
    >>> print abs(v)
    3.74165738677
    >>> v += Vec3((3,2,1))
    >>> print v
    [4, 4, 4]
    >>> print v**2
    48.0
    >>> print v**1
    6.92820323028
    >>> print v
    [4, 4, 4]
    """
    
    def __init__(self, xyz):
        """
        Create a new Vec3 object, specifying its three elements.
        
        
        Examples:
        
        >>> v = Vec3((1,2,3))
        >>> print v
        [1, 2, 3]
        
        """
        x = xyz[0]
        y = xyz[1]
        z = xyz[2]
        self.data = [x, y, z]

    def __repr__(self):
        """
        Create a string containing a python code representation of this Vec3.
        
        Examples
        
        >>> v = Vec3([1,2,3])
        >>> v
        Vec3([1, 2, 3])
        """
        return self.__class__.__name__ + \
                '([' + repr(self[0]) + ", " + repr(self[1]) + ", " + repr(self[2]) + '])'

    def __len__(self):
        """
        Returns the number of elements in this Vec3.  Always 3.
        
        Examples
        
        >>> v = Vec3((1, 2, 3))
        >>> len(v)
        3
        """
        return 3
        
    def __iter__(self):
        """
        Examples
        
        >>> v = Vec3((1,2,3))
        >>> for c in v:
        ...   print c
        ... 
        1
        2
        3
        """
        for coord in self.data:
            yield coord
        
    def __getitem__(self, key):
        return self.data[key]

    def __setitem__(self, key, value):
        self.data[key] = value

    def __abs__(self):
        return pow(self.dot(self), 0.5)
        
    def __neg__(self):
        return self.__class__([-self[0], -self[1], -self[2]])
        
    def __pos__(self):
        return self
        
    def __add__(self, other):
        return self.__class__([self[0] + other[0], self[1] + other[1], self[2] + other[2]])

    def __radd__(self, other):
        return self.__class__([other[0] + self[0], other[1] + self[1], other[2] + self[2]])

    def __sub__(self, other):
        return self.__class__([self[0] - other[0], self[1] - other[1], self[2] - other[2]])

    def __rsub__(self, other):
        return self.__class__([other[0] - self[0], other[1] - self[1], other[2] - self[2]])

    def __mul__(self, other):
        """
        Returns NotImplemented if the right hand side is a simtk.unit.Unit,
        so the Unit class can return a proper Quantity unit.
        
        Example
        
        >>> import simtk.unit
        >>> Vec3([1,2,3]) * simtk.unit.meter
        Quantity(Vec3([1, 2, 3]), meter)
        """
        # If other is a Unit, delegate to Unit.__rmul__ multiplication
        if simtk.unit.is_unit(other):
            return NotImplemented
        return self.__class__([self[0] * other, self[1] * other, self[2] * other])

    def __rmul__(self, other):
        return self.__class__([other * self[0], other * self[1], other * self[2]])

    def __div__(self, other):
        # If other is a Unit, delegate to Unit.__rmul__ multiplication
        if simtk.unit.is_unit(other):
            return NotImplemented
        return self.__class__([self[0] / other, self[1] / other, self[2] / other])
            
    def __rdiv__(self, other):
        return self.__class__([other / self[0], other / self[1], other / self[2]])
            
    def __str__(self):
        return "["+str(self[0])+", "+str(self[1])+", "+str(self[2])+"]"

    def __pow__(self, exponent):
        return pow(self.dot(self), exponent * 0.5)

    def dot(self, other):
        assert len(other) == 3
        return self[0] * other[0] + self[1] * other[1] + self[2] * other[2]


# run module directly for testing
if __name__=='__main__':
    
    # Test the examples in the docstrings
    import doctest, sys
    doctest.testmod(sys.modules[__name__])