root/projects/2008/pyogp/pyogp.lib.base/trunk/pyogp/lib/base/datatypes.py

Revision 2491, 5.5 kB (checked in by kotler.linden, 5 months ago)

remerge of kotler_tests

Line 
1 # standard python libs
2 from logging import getLogger, CRITICAL, ERROR, WARNING, INFO, DEBUG
3 import uuid
4 import struct
5
6 # pyogp
7 from pyogp.lib.base.exc import DataParsingError
8
9 # pyogp messaging
10
11 # initialize logging
12 logger = getLogger('pyogp.lib.base.datatypes')
13 log = logger.log
14
15 class Vector3(object):
16     """ represents a vector as a tuple"""
17
18     def __init__(self, bytes = None, offset = 0, X = 0.0, Y = 0.0, Z = 0.0):
19
20         if bytes != None:
21
22             self.unpack_from_bytes(bytes, offset)
23
24         else:
25
26             if type(X) != float:
27                 self.X = float(X)
28             else:
29                 self.X = X
30             if type(Y) != float:
31                 self.Y = float(Y)
32             else:
33                 self.Y = Y
34             if type(Z) != float:
35                 self.Z = float(Z)
36             else:
37                 self.Z = Z
38
39     def unpack_from_bytes(self, bytes, offset):
40         """ unpack floats from binary """
41
42         # unpack from binary as Little Endian
43         self.X = struct.unpack("<f", bytes[offset:offset+4])[0]
44         self.Y = struct.unpack("<f", bytes[offset+4:offset+8])[0]
45         self.Z = struct.unpack("<f", bytes[offset+8:offset+12])[0]
46
47     def get_bytes(self):
48         """ get bytes """
49
50         return struct.pack("<3f", self.X, self.Y, self.Z)
51
52     def data(self):
53
54         return ((self.X, self.Y, self.Z))
55
56     def copy(self):
57         return Vector3( X = self.X, Y = self.Y, Z = self.Z )
58
59     def __repr__(self):
60         """ represent a vector as a string """
61
62         return str((self.X, self.Y, self.Z))
63
64     def __call__(self):
65         """ represent a vector as a tuple """
66
67         return ((self.X, self.Y, self.Z))
68
69     def dist_squared(a, b):
70         x = a.X - b.X
71         y = a.Y - b.Y
72         z = a.Z - b.Z
73         return x*x + y*y + z*z
74     dist_squared = staticmethod(dist_squared)
75    
76
77 class Quaternion(object):
78     """ represents a quaternion as a tuple"""
79
80     def __init__(self, bytes = None, offset = 0, X = 0.0, Y = 0.0, Z = 0.0, W = 0.0):
81
82         if bytes != None:
83
84             self.unpack_from_bytes(bytes, offset)
85
86         else:
87
88             if type(X) != float:
89                 self.X = float(X)
90             else:
91                 self.X = X
92             if type(Y) != float:
93                 self.Y = float(Y)
94             else:
95                 self.Y = Y           
96             if type(Z) != float:
97                 self.Z = float(Z)
98             else:
99                 self.Z = Z
100             if type(W) != float:
101                 self.W = float(W)
102             else:
103                 self.W = W
104
105     def unpack_from_bytes(self, bytes, offset):
106         """ unpack floats from binary """
107
108         # unpack from binary as Little Endian
109         self.X = struct.unpack("<f", bytes[offset:offset+4])[0]
110         self.Y = struct.unpack("<f", bytes[offset+4:offset+8])[0]
111         self.Z = struct.unpack("<f", bytes[offset+8:offset+12])[0]
112         self.W = struct.unpack("<f", bytes[offset+12:offset+16])[0]
113
114     def get_bytes(self):
115         """ get bytes """
116
117         return struct.pack("<4f", self.X, self.Y, self.Z, self.W)
118
119     def data(self):
120
121         return ((self.X, self.Y, self.Z, self.W))
122
123     def copy(self):
124         return Quaternion(X=self.X, Y=self.Y, Z=self.Z, W=self.W)
125
126     def __repr__(self):
127         """ represent a quaternion as a string """
128
129         return str((self.X, self.Y, self.Z, self.W))
130
131     def __call__(self):
132         """ represent a quaternion as a tuple """
133
134         return ((self.X, self.Y, self.Z, self.W))
135
136 class UUID(object):
137     """ represents a uuid as, well, a uuid
138
139     inbound LLUUID data from packets is already UUID(), they are
140     already the same 'datatype'
141     """
142
143     def __init__(self, string = '00000000-0000-0000-0000-000000000000', bytes = None, offset = 0):
144
145         if bytes != None:
146
147             self.unpack_from_bytes(bytes, offset)
148
149         else:
150
151             self.uuid = uuid.UUID(string)
152
153     def random(self):
154
155         if str(self.uuid) == '00000000-0000-0000-0000-000000000000':
156             self.uuid = uuid.uuid4()
157             return self.uuid
158
159         else:
160             log(WARNING, "Attempted to overwrite a stored uuid %s with a random, that is a bad idea..." % (str(self.uuid)))
161
162     def unpack_from_bytes(self, bytes, offset):
163         """ unpack uuid from binary """
164
165         # unpack from binary
166         self.uuid = uuid.UUID(bytes = bytes[offset:offset+16])
167
168     def get_bytes(self):
169         """ get bytes """
170
171         return str(self.uuid.bytes)
172
173     def data(self):
174         """ represent a uuid as, well, a uuid """
175
176         return self.uuid
177
178     def copy(self):
179         return UUID(string=str(self.uuid))
180
181     def __repr__(self):
182         """ represent a uuid as a string """
183
184         return str(self.uuid)
185
186     def __call__(self):
187         """ represent a uuid as, well, a uuid """
188
189         return self.uuid
190
191
192     def __eq__(self, other):
193         if hasattr(other,'uuid'):
194             return self.uuid == other.uuid
195         else:
196             return False
197    
198
199     def __xor__(self, arg):
200         """ the xor of two UUIDs """
201         temp = self.uuid.int ^ arg.uuid.int
202         result = uuid.UUID(int = temp)
203         return UUID(result.__str__())
204        
205
206 """
207 Contributors can be viewed at:
208 http://svn.secondlife.com/svn/linden/projects/2008/pyogp/CONTRIBUTORS.txt
209
210 $LicenseInfo:firstyear=2008&license=apachev2$
211
212 Copyright 2009, Linden Research, Inc.
213
214 Licensed under the Apache License, Version 2.0 (the "License").
215 You may obtain a copy of the License at:
216     http://www.apache.org/licenses/LICENSE-2.0
217 or in
218     http://svn.secondlife.com/svn/linden/projects/2008/pyogp/LICENSE.txt
219
220 $/LicenseInfo$
221 """
222
Note: See TracBrowser for help on using the browser.